/[rdesktop]/sourceforge.net/trunk/rdesktop/xwin.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sourceforge.net/trunk/rdesktop/xwin.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 185 by n-ki, Wed Sep 18 12:13:08 2002 UTC revision 309 by jsorg71, Tue Feb 4 05:32:13 2003 UTC
# Line 1  Line 1 
1  /*  /*
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     User interface services - X Window System     User interface services - X Window System
4     Copyright (C) Matthew Chapman 1999-2001     Copyright (C) Matthew Chapman 1999-2002
5    
6     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 22  Line 22 
22  #include <X11/Xutil.h>  #include <X11/Xutil.h>
23  #include <time.h>  #include <time.h>
24  #include <errno.h>  #include <errno.h>
 #define XK_MISCELLANY  
 #include <X11/keysymdef.h>  
25  #include "rdesktop.h"  #include "rdesktop.h"
 #include "scancodes.h"  
26    
27  extern int width;  extern int width;
28  extern int height;  extern int height;
29  extern BOOL sendmotion;  extern BOOL sendmotion;
30  extern BOOL fullscreen;  extern BOOL fullscreen;
31  extern BOOL grab_keyboard;  extern BOOL grab_keyboard;
32    extern BOOL hide_decorations;
33  extern char title[];  extern char title[];
34    extern int server_bpp;
35    BOOL enable_compose = False;
36    BOOL focused;
37    BOOL mouse_in_wnd;
38    
39  Display *display;  Display *display;
40  static int x_socket;  static int x_socket;
# Line 42  static GC gc; Line 44  static GC gc;
44  static Visual *visual;  static Visual *visual;
45  static int depth;  static int depth;
46  static int bpp;  static int bpp;
47    static XIM IM;
48    static XIC IC;
49    static XModifierKeymap *mod_map;
50    static Cursor current_cursor;
51    static Atom protocol_atom, kill_atom;
52    
53  /* endianness */  /* endianness */
54  static BOOL host_be;  static BOOL host_be;
# Line 51  static BOOL xserver_be; Line 58  static BOOL xserver_be;
58  static BOOL ownbackstore;  static BOOL ownbackstore;
59  static Pixmap backstore;  static Pixmap backstore;
60    
61    /* MWM decorations */
62    #define MWM_HINTS_DECORATIONS   (1L << 1)
63    #define PROP_MOTIF_WM_HINTS_ELEMENTS    5
64    typedef struct
65    {
66            uint32 flags;
67            uint32 functions;
68            uint32 decorations;
69            sint32 inputMode;
70            uint32 status;
71    }
72    PropMotifWmHints;
73    
74    
75  #define FILL_RECTANGLE(x,y,cx,cy)\  #define FILL_RECTANGLE(x,y,cx,cy)\
76  { \  { \
77          XFillRectangle(display, wnd, gc, x, y, cx, cy); \          XFillRectangle(display, wnd, gc, x, y, cx, cy); \
# Line 58  static Pixmap backstore; Line 79  static Pixmap backstore;
79                  XFillRectangle(display, backstore, gc, x, y, cx, cy); \                  XFillRectangle(display, backstore, gc, x, y, cx, cy); \
80  }  }
81    
82    #define FILL_RECTANGLE_BACKSTORE(x,y,cx,cy)\
83    { \
84            XFillRectangle(display, ownbackstore ? backstore : wnd, gc, x, y, cx, cy); \
85    }
86    
87  /* colour maps */  /* colour maps */
88    BOOL owncolmap = False;
89  static Colormap xcolmap;  static Colormap xcolmap;
90  static uint32 *colmap;  static uint32 *colmap;
91    
92  /* Compose support */  #define TRANSLATE(col)          ( server_bpp != 8 ? col : owncolmap ? col : translate_colour(colmap[col]) )
93  BOOL enable_compose = False;  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));
94  static XIM IM = NULL;  #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));
 static XIC IC = NULL;  
   
 /* toggle fullscreen globals */  
 static unsigned long input_mask;  
   
 #define SET_FOREGROUND(col)     XSetForeground(display, gc, translate_colour(colmap[col]));  
 #define SET_BACKGROUND(col)     XSetBackground(display, gc, translate_colour(colmap[col]));  
95    
96  static int rop2_map[] = {  static int rop2_map[] = {
97          GXclear,                /* 0 */          GXclear,                /* 0 */
# Line 95  static int rop2_map[] = { Line 115  static int rop2_map[] = {
115  #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(display, gc, rop2_map[rop2]); }  #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(display, gc, rop2_map[rop2]); }
116  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }
117    
118    void
119    mwm_hide_decorations(void)
120    {
121            PropMotifWmHints motif_hints;
122            Atom hintsatom;
123    
124            /* setup the property */
125            motif_hints.flags = MWM_HINTS_DECORATIONS;
126            motif_hints.decorations = 0;
127    
128            /* get the atom for the property */
129            hintsatom = XInternAtom(display, "_MOTIF_WM_HINTS", False);
130            if (!hintsatom)
131            {
132                    warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n");
133                    return;
134            }
135    
136            XChangeProperty(display, wnd, hintsatom, hintsatom, 32, PropModeReplace,
137                            (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
138    }
139    
140  static void  static void
141  translate8(uint8 * data, uint8 * out, uint8 * end)  translate8(uint8 * data, uint8 * out, uint8 * end)
142  {  {
# Line 189  translate_colour(uint32 colour) Line 231  translate_colour(uint32 colour)
231          return colour;          return colour;
232  }  }
233    
 static unsigned long  
 init_inputmethod(void)  
 {  
         unsigned long filtered_events = 0;  
   
         IM = XOpenIM(display, NULL, NULL, NULL);  
         if (IM == NULL)  
         {  
                 error("Failed to open input method\n");  
         }  
   
         if (IM != NULL)  
         {  
                 /* Must be done after XCreateWindow */  
                 IC = XCreateIC(IM, XNInputStyle,  
                                (XIMPreeditNothing | XIMStatusNothing),  
                                XNClientWindow, wnd, XNFocusWindow, wnd, NULL);  
   
                 if (IC == NULL)  
                 {  
                         error("Failed to create input context\n");  
                         XCloseIM(IM);  
                         IM = NULL;  
                 }  
         }  
   
         /* For correct Multi_key/Compose processing, I guess.  
            It seems to work alright anyway, though. */  
         if (IC != NULL)  
         {  
                 if (XGetICValues(IC, XNFilterEvents, &filtered_events, NULL) != NULL)  
                 {  
                         error("Failed to obtain XNFilterEvents value from IC\n");  
                         filtered_events = 0;  
                 }  
         }  
         return filtered_events;  
 }  
   
 static void  
 close_inputmethod(void)  
 {  
         if (IC != NULL)  
         {  
                 XDestroyIC(IC);  
                 if (IM != NULL)  
                 {  
                         XCloseIM(IM);  
                         IM = NULL;  
                 }  
         }  
 }  
   
234  BOOL  BOOL
235  get_key_state(int keysym)  get_key_state(unsigned int state, uint32 keysym)
236  {  {
237          int keysymMask = 0, modifierpos, key;          int modifierpos, key, keysymMask = 0;
         Window wDummy1, wDummy2;  
         int iDummy3, iDummy4, iDummy5, iDummy6;  
         unsigned int current_state;  
238          int offset;          int offset;
239    
         XModifierKeymap *map = XGetModifierMapping(display);  
240          KeyCode keycode = XKeysymToKeycode(display, keysym);          KeyCode keycode = XKeysymToKeycode(display, keysym);
241    
242          if (keycode == NoSymbol)          if (keycode == NoSymbol)
# Line 259  get_key_state(int keysym) Line 244  get_key_state(int keysym)
244    
245          for (modifierpos = 0; modifierpos < 8; modifierpos++)          for (modifierpos = 0; modifierpos < 8; modifierpos++)
246          {          {
247                  offset = map->max_keypermod * modifierpos;                  offset = mod_map->max_keypermod * modifierpos;
248    
249                  for (key = 0; key < map->max_keypermod; key++)                  for (key = 0; key < mod_map->max_keypermod; key++)
250                  {                  {
251                          if (map->modifiermap[offset + key] == keycode)                          if (mod_map->modifiermap[offset + key] == keycode)
252                                  keysymMask = 1 << modifierpos;                                  keysymMask |= 1 << modifierpos;
253                  }                  }
254          }          }
255    
256          XQueryPointer(display, DefaultRootWindow(display), &wDummy1,          return (state & keysymMask) ? True : False;
                       &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, &current_state);  
   
         XFreeModifiermap(map);  
   
         return (current_state & keysymMask) ? True : False;  
 }  
   
 static void  
 xwin_map_window()  
 {  
         XEvent xevent;  
   
         XMapWindow(display, wnd);  
   
         /* wait for VisibilityChange */  
         XMaskEvent(display, VisibilityChangeMask, &xevent);  
   
         if (fullscreen)  
                 XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);  
257  }  }
258    
259  BOOL  BOOL
260  ui_init()  ui_init(void)
261  {  {
262          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
263          uint16 test;          uint16 test;
# Line 300  ui_init() Line 266  ui_init()
266          display = XOpenDisplay(NULL);          display = XOpenDisplay(NULL);
267          if (display == NULL)          if (display == NULL)
268          {          {
269                  error("Failed to open display\n");                  error("Failed to open display: %s\n", XDisplayName(NULL));
270                  return False;                  return False;
271          }          }
272    
# Line 331  ui_init() Line 297  ui_init()
297                  return False;                  return False;
298          }          }
299    
300          xcolmap = DefaultColormapOfScreen(screen);          if (owncolmap != True)
301            {
302                    xcolmap = DefaultColormapOfScreen(screen);
303                    if (depth <= 8)
304                            warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n");
305            }
306    
307          if (DoesBackingStore(screen) == NotUseful)          gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL);
308    
309            if (DoesBackingStore(screen) != Always)
310                  ownbackstore = True;                  ownbackstore = True;
311    
312          test = 1;          test = 1;
313          host_be = !(BOOL) (*(uint8 *) (&test));          host_be = !(BOOL) (*(uint8 *) (&test));
314          xserver_be = (ImageByteOrder(display) == MSBFirst);          xserver_be = (ImageByteOrder(display) == MSBFirst);
315    
316            if ((width == 0) || (height == 0))
317            {
318                    /* Fetch geometry from _NET_WORKAREA */
319                    uint32 x, y, cx, cy;
320    
321                    if (get_current_workarea(&x, &y, &cx, &cy) == 0)
322                    {
323                            width = cx;
324                            height = cy;
325                    }
326                    else
327                    {
328                            warning("Failed to get workarea: probably your window manager does not support extended hints\n");
329                            width = 800;
330                            height = 600;
331                    }
332            }
333    
334          if (fullscreen)          if (fullscreen)
335          {          {
336                  width = WidthOfScreen(screen);                  width = WidthOfScreen(screen);
# Line 349  ui_init() Line 340  ui_init()
340          /* make sure width is a multiple of 4 */          /* make sure width is a multiple of 4 */
341          width = (width + 3) & ~3;          width = (width + 3) & ~3;
342    
343            if (ownbackstore)
344            {
345                    backstore =
346                            XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth);
347    
348                    /* clear to prevent rubbish being exposed at startup */
349                    XSetForeground(display, gc, BlackPixelOfScreen(screen));
350                    XFillRectangle(display, backstore, gc, 0, 0, width, height);
351            }
352    
353            mod_map = XGetModifierMapping(display);
354    
355            if (enable_compose)
356                    IM = XOpenIM(display, NULL, NULL, NULL);
357    
358          xkeymap_init();          xkeymap_init();
359          return True;          return True;
360  }  }
361    
362    void
363    ui_deinit(void)
364    {
365            if (IM != NULL)
366                    XCloseIM(IM);
367    
368            XFreeModifiermap(mod_map);
369    
370            if (ownbackstore)
371                    XFreePixmap(display, backstore);
372    
373            XFreeGC(display, gc);
374            XCloseDisplay(display);
375            display = NULL;
376    }
377    
378  BOOL  BOOL
379  ui_create_window()  ui_create_window(void)
380  {  {
381          XSetWindowAttributes attribs;          XSetWindowAttributes attribs;
382          XClassHint *classhints;          XClassHint *classhints;
383          XSizeHints *sizehints;          XSizeHints *sizehints;
384            int wndwidth, wndheight;
385            long input_mask, ic_input_mask;
386          XEvent xevent;          XEvent xevent;
387    
388            wndwidth = fullscreen ? WidthOfScreen(screen) : width;
389            wndheight = fullscreen ? HeightOfScreen(screen) : height;
390    
391          attribs.background_pixel = BlackPixelOfScreen(screen);          attribs.background_pixel = BlackPixelOfScreen(screen);
392          attribs.backing_store = ownbackstore ? NotUseful : Always;          attribs.backing_store = ownbackstore ? NotUseful : Always;
393          attribs.override_redirect = fullscreen;          attribs.override_redirect = fullscreen;
394          wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height,  
395            wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight,
396                              0, CopyFromParent, InputOutput, CopyFromParent,                              0, CopyFromParent, InputOutput, CopyFromParent,
397                              CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);                              CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
398    
         if (ownbackstore)  
                 backstore = XCreatePixmap(display, wnd, width, height, depth);  
   
399          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
400    
401            if (hide_decorations)
402                    mwm_hide_decorations();
403    
404          classhints = XAllocClassHint();          classhints = XAllocClassHint();
405          if (classhints != NULL)          if (classhints != NULL)
406          {          {
# Line 394  ui_create_window() Line 422  ui_create_window()
422          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
423                  VisibilityChangeMask | FocusChangeMask;                  VisibilityChangeMask | FocusChangeMask;
424    
         if (grab_keyboard)  
                 input_mask |= EnterWindowMask | LeaveWindowMask;  
425          if (sendmotion)          if (sendmotion)
426                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
427          if (ownbackstore)          if (ownbackstore)
428                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
429          if (enable_compose)          if (fullscreen || grab_keyboard)
430                  input_mask |= init_inputmethod();                  input_mask |= EnterWindowMask;
431            if (grab_keyboard)
432                    input_mask |= LeaveWindowMask;
433    
434            if (IM != NULL)
435            {
436                    IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
437                                   XNClientWindow, wnd, XNFocusWindow, wnd, NULL);
438    
439                    if ((IC != NULL)
440                        && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))
441                            input_mask |= ic_input_mask;
442            }
443    
444          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
445            XMapWindow(display, wnd);
446    
447            /* wait for VisibilityNotify */
448            do
449            {
450                    XMaskEvent(display, VisibilityChangeMask, &xevent);
451            }
452            while (xevent.type != VisibilityNotify);
453    
454          xwin_map_window();          focused = False;
455            mouse_in_wnd = False;
456    
457          /* clear the window so that cached data is not seen */          /* handle the WM_DELETE_WINDOW protocol */
458          gc = XCreateGC(display, wnd, 0, NULL);          protocol_atom = XInternAtom(display, "WM_PROTOCOLS", True);
459          XSetForeground(display, gc, 0);          kill_atom = XInternAtom(display, "WM_DELETE_WINDOW", True);
460          FILL_RECTANGLE(0, 0, width, height);          XSetWMProtocols(display, wnd, &kill_atom, 1);
461    
462          return True;          return True;
463  }  }
464    
465  void  void
466  ui_destroy_window()  ui_destroy_window(void)
467  {  {
468          if (ownbackstore)          if (IC != NULL)
469                  XFreePixmap(display, backstore);                  XDestroyIC(IC);
   
         XFreeGC(display, gc);  
   
         close_inputmethod();  
470    
471          XDestroyWindow(display, wnd);          XDestroyWindow(display, wnd);
         XCloseDisplay(display);  
         display = NULL;  
472  }  }
473    
   
474  void  void
475  xwin_toggle_fullscreen()  xwin_toggle_fullscreen(void)
476  {  {
477          XEvent xevent;          Pixmap contents = 0;
         XSetWindowAttributes attribs;  
         int newwidth, newheight;  
478    
479            if (!ownbackstore)
480            {
481                    /* need to save contents of window */
482                    contents = XCreatePixmap(display, wnd, width, height, depth);
483                    XCopyArea(display, wnd, contents, gc, 0, 0, width, height, 0, 0);
484            }
485    
486            ui_destroy_window();
487          fullscreen = !fullscreen;          fullscreen = !fullscreen;
488          newwidth = fullscreen ? WidthOfScreen(screen) : width;          ui_create_window();
         newheight = fullscreen ? HeightOfScreen(screen) : height;  
489    
490          XUnmapWindow(display, wnd);          XDefineCursor(display, wnd, current_cursor);
491          attribs.override_redirect = fullscreen;  
492          XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight);          if (!ownbackstore)
493          XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs);          {
494          xwin_map_window();                  XCopyArea(display, contents, wnd, gc, 0, 0, width, height, 0, 0);
495                    XFreePixmap(display, contents);
496            }
497  }  }
498    
499  /* Process all events in Xlib queue */  /* Process all events in Xlib queue
500  static void     Returns 0 after user quit, 1 otherwise */
501  xwin_process_events()  static int
502    xwin_process_events(void)
503  {  {
504          XEvent xevent;          XEvent xevent;
505          KeySym keysym;          KeySym keysym;
506          uint16 button, flags;          uint16 button, flags;
507          uint32 ev_time;          uint32 ev_time;
508          key_translation tr;          key_translation tr;
         char *ksname = NULL;  
509          char str[256];          char str[256];
510          Status status;          Status status;
511            unsigned int state;
512            Window wdummy;
513            int dummy;
514    
515          while (XPending(display) > 0)          while (XPending(display) > 0)
516          {          {
517                  XNextEvent(display, &xevent);                  XNextEvent(display, &xevent);
518    
519                  if (enable_compose && (XFilterEvent(&xevent, None) == True))                  if ((IC != NULL) && (XFilterEvent(&xevent, None) == True))
520                  {                  {
521                          DEBUG_KBD(("Filtering event\n"));                          DEBUG_KBD(("Filtering event\n"));
522                          continue;                          continue;
523                  }                  }
524    
                 ev_time = time(NULL);  
525                  flags = 0;                  flags = 0;
526    
527                  switch (xevent.type)                  switch (xevent.type)
528                  {                  {
529                            case ClientMessage:
530                                    /* the window manager told us to quit */
531                                    if ((xevent.xclient.message_type == protocol_atom)
532                                        && (xevent.xclient.data.l[0] == kill_atom))
533                                            /* Quit */
534                                            return 0;
535                                    break;
536    
537                          case KeyPress:                          case KeyPress:
538                                  if (IC != NULL)                                  if (IC != NULL)
539                                          /* Multi_key compatible version */                                          /* Multi_key compatible version */
# Line 499  xwin_process_events() Line 556  xwin_process_events()
556                                                        str, sizeof(str), &keysym, NULL);                                                        str, sizeof(str), &keysym, NULL);
557                                  }                                  }
558    
559                                  ksname = get_ksname(keysym);                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym,
560                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));                                             get_ksname(keysym)));
561    
562                                  if (handle_special_keys(keysym, ev_time, True))                                  ev_time = time(NULL);
563                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))
564                                          break;                                          break;
565    
566                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 515  xwin_process_events() Line 573  xwin_process_events()
573    
574                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
575                                  break;                                  break;
576    
577                          case KeyRelease:                          case KeyRelease:
578                                  XLookupString((XKeyEvent *) & xevent, str,                                  XLookupString((XKeyEvent *) & xevent, str,
579                                                sizeof(str), &keysym, NULL);                                                sizeof(str), &keysym, NULL);
580    
                                 ksname = get_ksname(keysym);  
581                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
582                                             ksname));                                             get_ksname(keysym)));
583    
584                                  if (handle_special_keys(keysym, ev_time, False))                                  ev_time = time(NULL);
585                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False))
586                                          break;                                          break;
587    
588                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 544  xwin_process_events() Line 603  xwin_process_events()
603                                  if (button == 0)                                  if (button == 0)
604                                          break;                                          break;
605    
606                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
607                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);
608                                  break;                                  break;
609    
610                          case MotionNotify:                          case MotionNotify:
611                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  if (fullscreen && !focused)
612                                            XSetInputFocus(display, wnd, RevertToPointerRoot,
613                                                           CurrentTime);
614                                    rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
615                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
616                                  break;                                  break;
617    
618                          case EnterNotify:                          case FocusIn:
619                                  if (grab_keyboard)                                  if (xevent.xfocus.mode == NotifyGrab)
620                                            break;
621                                    focused = True;
622                                    XQueryPointer(display, wnd, &wdummy, &wdummy, &dummy, &dummy,
623                                                  &dummy, &dummy, &state);
624                                    reset_modifier_keys(state);
625                                    if (grab_keyboard && mouse_in_wnd)
626                                          XGrabKeyboard(display, wnd, True,                                          XGrabKeyboard(display, wnd, True,
627                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
628                                  break;                                  break;
629    
630                          case LeaveNotify:                          case FocusOut:
631                                  if (grab_keyboard)                                  if (xevent.xfocus.mode == NotifyUngrab)
632                                            break;
633                                    focused = False;
634                                    if (xevent.xfocus.mode == NotifyWhileGrabbed)
635                                          XUngrabKeyboard(display, CurrentTime);                                          XUngrabKeyboard(display, CurrentTime);
636                                  break;                                  break;
637    
638                          case FocusIn:                          case EnterNotify:
639                                  reset_modifier_keys();                                  /* we only register for this event when in fullscreen mode */
640                                    /* or grab_keyboard */
641                                    mouse_in_wnd = True;
642                                    if (fullscreen)
643                                    {
644                                            XSetInputFocus(display, wnd, RevertToPointerRoot,
645                                                           CurrentTime);
646                                            break;
647                                    }
648                                    if (focused)
649                                            XGrabKeyboard(display, wnd, True,
650                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
651                                    break;
652    
653                            case LeaveNotify:
654                                    /* we only register for this event when grab_keyboard */
655                                    mouse_in_wnd = False;
656                                    XUngrabKeyboard(display, CurrentTime);
657                                  break;                                  break;
658    
659                          case Expose:                          case Expose:
# Line 582  xwin_process_events() Line 670  xwin_process_events()
670                                  if (xevent.xmapping.request == MappingKeyboard                                  if (xevent.xmapping.request == MappingKeyboard
671                                      || xevent.xmapping.request == MappingModifier)                                      || xevent.xmapping.request == MappingModifier)
672                                          XRefreshKeyboardMapping(&xevent.xmapping);                                          XRefreshKeyboardMapping(&xevent.xmapping);
673    
674                                    if (xevent.xmapping.request == MappingModifier)
675                                    {
676                                            XFreeModifiermap(mod_map);
677                                            mod_map = XGetModifierMapping(display);
678                                    }
679                                  break;                                  break;
680    
681                  }                  }
682          }          }
683            /* Keep going */
684            return 1;
685  }  }
686    
687  void  /* Returns 0 after user quit, 1 otherwise */
688    int
689  ui_select(int rdp_socket)  ui_select(int rdp_socket)
690  {  {
691          int n = (rdp_socket > x_socket) ? rdp_socket + 1 : x_socket + 1;          int n = (rdp_socket > x_socket) ? rdp_socket + 1 : x_socket + 1;
# Line 599  ui_select(int rdp_socket) Line 696  ui_select(int rdp_socket)
696          while (True)          while (True)
697          {          {
698                  /* Process any events already waiting */                  /* Process any events already waiting */
699                  xwin_process_events();                  if (!xwin_process_events())
700                            /* User quit */
701                            return 0;
702    
703                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
704                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
# Line 615  ui_select(int rdp_socket) Line 714  ui_select(int rdp_socket)
714                  }                  }
715    
716                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
717                          return;                          return 1;
718          }          }
719  }  }
720    
# Line 632  ui_create_bitmap(int width, int height, Line 731  ui_create_bitmap(int width, int height,
731          Pixmap bitmap;          Pixmap bitmap;
732          uint8 *tdata;          uint8 *tdata;
733    
734          tdata = translate_image(width, height, data);          tdata = (owncolmap ? data : translate_image(width, height, data));
735          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(display, wnd, width, height, depth);
736          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
737                               (char *) tdata, width, height, 8, 0);                               (char *) tdata, width, height, 8, 0);
# Line 640  ui_create_bitmap(int width, int height, Line 739  ui_create_bitmap(int width, int height,
739          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
740    
741          XFree(image);          XFree(image);
742          xfree(tdata);          if (!owncolmap)
743                    xfree(tdata);
744          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
745  }  }
746    
# Line 650  ui_paint_bitmap(int x, int y, int cx, in Line 750  ui_paint_bitmap(int x, int y, int cx, in
750          XImage *image;          XImage *image;
751          uint8 *tdata;          uint8 *tdata;
752    
753          tdata = translate_image(width, height, data);          if (server_bpp == 16)
754            {
755                    image = XCreateImage(display, visual, depth, ZPixmap, 0,
756                                         (char *) data, width, height, 16, 0);
757    
758                    if (ownbackstore)
759                    {
760                            XPutImage(display, backstore, gc, image, 0, 0, x, y, cx, cy);
761                            XCopyArea(display, backstore, wnd, gc, x, y, cx, cy, x, y);
762                    }
763                    else
764                    {
765                            XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);
766                    }
767    
768                    XFree(image);
769                    return;
770            }
771            tdata = (owncolmap ? data : translate_image(width, height, data));
772          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
773                               (char *) tdata, width, height, 8, 0);                               (char *) tdata, width, height, 8, 0);
774    
# Line 665  ui_paint_bitmap(int x, int y, int cx, in Line 783  ui_paint_bitmap(int x, int y, int cx, in
783          }          }
784    
785          XFree(image);          XFree(image);
786          xfree(tdata);          if (!owncolmap)
787                    xfree(tdata);
788  }  }
789    
790  void  void
# Line 780  ui_create_cursor(unsigned int x, unsigne Line 899  ui_create_cursor(unsigned int x, unsigne
899  void  void
900  ui_set_cursor(HCURSOR cursor)  ui_set_cursor(HCURSOR cursor)
901  {  {
902          XDefineCursor(display, wnd, (Cursor) cursor);          current_cursor = (Cursor) cursor;
903            XDefineCursor(display, wnd, current_cursor);
904  }  }
905    
906  void  void
# Line 795  ui_destroy_cursor(HCURSOR cursor) Line 915  ui_destroy_cursor(HCURSOR cursor)
915                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \
916                  (xc)->flags = DoRed | DoGreen | DoBlue;                  (xc)->flags = DoRed | DoGreen | DoBlue;
917    
918    
919  HCOLOURMAP  HCOLOURMAP
920  ui_create_colourmap(COLOURMAP * colours)  ui_create_colourmap(COLOURMAP * colours)
921  {  {
922          COLOURENTRY *entry;          COLOURENTRY *entry;
923          int i, ncolours = colours->ncolours;          int i, ncolours = colours->ncolours;
924          uint32 *map = xmalloc(sizeof(*colmap) * ncolours);          if (!owncolmap)
         XColor xentry;  
         XColor xc_cache[256];  
         uint32 colour;  
         int colLookup = 256;  
         for (i = 0; i < ncolours; i++)  
925          {          {
926                  entry = &colours->colours[i];                  uint32 *map = xmalloc(sizeof(*colmap) * ncolours);
927                  MAKE_XCOLOR(&xentry, entry);                  XColor xentry;
928                    XColor xc_cache[256];
929                  if (XAllocColor(display, xcolmap, &xentry) == 0)                  uint32 colour;
930                    int colLookup = 256;
931                    for (i = 0; i < ncolours; i++)
932                  {                  {
933                          /* Allocation failed, find closest match. */                          entry = &colours->colours[i];
934                          int j = 256;                          MAKE_XCOLOR(&xentry, entry);
                         int nMinDist = 3 * 256 * 256;  
                         long nDist = nMinDist;  
   
                         /* only get the colors once */  
                         while( colLookup-- ){  
                                 xc_cache[colLookup].pixel = colLookup;  
                                 xc_cache[colLookup].red = xc_cache[colLookup].green = xc_cache[colLookup].blue = 0;  
                                 xc_cache[colLookup].flags = 0;  
                                 XQueryColor(display, DefaultColormap(display, DefaultScreen(display)), &xc_cache[colLookup]);  
                         }  
                         colLookup = 0;  
935    
936                          /* approximate the pixel */                          if (XAllocColor(display, xcolmap, &xentry) == 0)
937                          while( j-- ){                          {
938                                  if( xc_cache[j].flags ){                                  /* Allocation failed, find closest match. */
939                                          nDist =                                  int j = 256;
940                                          ((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) *                                  int nMinDist = 3 * 256 * 256;
941                                          ((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) +                                  long nDist = nMinDist;
942                                          ((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) *  
943                                          ((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) +                                  /* only get the colors once */
944                                          ((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8)) *                                  while (colLookup--)
945                                          ((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8));                                  {
946                                            xc_cache[colLookup].pixel = colLookup;
947                                            xc_cache[colLookup].red = xc_cache[colLookup].green =
948                                                    xc_cache[colLookup].blue = 0;
949                                            xc_cache[colLookup].flags = 0;
950                                            XQueryColor(display,
951                                                        DefaultColormap(display,
952                                                                        DefaultScreen(display)),
953                                                        &xc_cache[colLookup]);
954                                  }                                  }
955                                  if( nDist < nMinDist ){                                  colLookup = 0;
956                                          nMinDist = nDist;  
957                                          xentry.pixel = j;                                  /* approximate the pixel */
958                                    while (j--)
959                                    {
960                                            if (xc_cache[j].flags)
961                                            {
962                                                    nDist = ((long) (xc_cache[j].red >> 8) -
963                                                             (long) (xentry.red >> 8)) *
964                                                            ((long) (xc_cache[j].red >> 8) -
965                                                             (long) (xentry.red >> 8)) +
966                                                            ((long) (xc_cache[j].green >> 8) -
967                                                             (long) (xentry.green >> 8)) *
968                                                            ((long) (xc_cache[j].green >> 8) -
969                                                             (long) (xentry.green >> 8)) +
970                                                            ((long) (xc_cache[j].blue >> 8) -
971                                                             (long) (xentry.blue >> 8)) *
972                                                            ((long) (xc_cache[j].blue >> 8) -
973                                                             (long) (xentry.blue >> 8));
974                                            }
975                                            if (nDist < nMinDist)
976                                            {
977                                                    nMinDist = nDist;
978                                                    xentry.pixel = j;
979                                            }
980                                  }                                  }
981                          }                          }
982                  }                          colour = xentry.pixel;
                 colour = xentry.pixel;  
983    
984                  /* update our cache */                          /* update our cache */
985                  if( xentry.pixel < 256 ){                          if (xentry.pixel < 256)
986                          xc_cache[xentry.pixel].red = xentry.red;                          {
987                          xc_cache[xentry.pixel].green = xentry.green;                                  xc_cache[xentry.pixel].red = xentry.red;
988                          xc_cache[xentry.pixel].blue = xentry.blue;                                  xc_cache[xentry.pixel].green = xentry.green;
989                                    xc_cache[xentry.pixel].blue = xentry.blue;
990    
991                  }                          }
992    
993    
994                  /* byte swap here to make translate_image faster */                          /* byte swap here to make translate_image faster */
995                  map[i] = translate_colour(colour);                          map[i] = translate_colour(colour);
996                    }
997                    return map;
998          }          }
999            else
1000            {
1001                    XColor *xcolours, *xentry;
1002                    Colormap map;
1003    
1004                    xcolours = xmalloc(sizeof(XColor) * ncolours);
1005                    for (i = 0; i < ncolours; i++)
1006                    {
1007                            entry = &colours->colours[i];
1008                            xentry = &xcolours[i];
1009                            xentry->pixel = i;
1010                            MAKE_XCOLOR(xentry, entry);
1011                    }
1012    
1013                    map = XCreateColormap(display, wnd, visual, AllocAll);
1014                    XStoreColors(display, map, xcolours, ncolours);
1015    
1016          return map;                  xfree(xcolours);
1017                    return (HCOLOURMAP) map;
1018            }
1019  }  }
1020    
1021  void  void
1022  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
1023  {  {
1024          xfree(map);          if (!owncolmap)
1025                    xfree(map);
1026            else
1027                    XFreeColormap(display, (Colormap) map);
1028  }  }
1029    
1030  void  void
1031  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
1032  {  {
1033          colmap = map;          if (!owncolmap)
1034                    colmap = map;
1035            else
1036                    XSetWindowColormap(display, wnd, (Colormap) map);
1037  }  }
1038    
1039  void  void
# Line 886  ui_set_clip(int x, int y, int cx, int cy Line 1049  ui_set_clip(int x, int y, int cx, int cy
1049  }  }
1050    
1051  void  void
1052  ui_reset_clip()  ui_reset_clip(void)
1053  {  {
1054          XRectangle rect;          XRectangle rect;
1055    
# Line 898  ui_reset_clip() Line 1061  ui_reset_clip()
1061  }  }
1062    
1063  void  void
1064  ui_bell()  ui_bell(void)
1065  {  {
1066          XBell(display, 0);          XBell(display, 0);
1067  }  }
# Line 1033  ui_rect( Line 1196  ui_rect(
1196          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE(x, y, cx, cy);
1197  }  }
1198    
1199    /* warning, this function only draws on wnd or backstore, not both */
1200  void  void
1201  ui_draw_glyph(int mixmode,  ui_draw_glyph(int mixmode,
1202                /* dest */ int x, int y, int cx, int cy,                /* dest */ int x, int y, int cx, int cy,
# Line 1047  ui_draw_glyph(int mixmode, Line 1211  ui_draw_glyph(int mixmode,
1211          XSetStipple(display, gc, (Pixmap) glyph);          XSetStipple(display, gc, (Pixmap) glyph);
1212          XSetTSOrigin(display, gc, x, y);          XSetTSOrigin(display, gc, x, y);
1213    
1214          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1215    
1216          XSetFillStyle(display, gc, FillSolid);          XSetFillStyle(display, gc, FillSolid);
1217  }  }
# Line 1076  ui_draw_glyph(int mixmode, Line 1240  ui_draw_glyph(int mixmode,
1240      }\      }\
1241    if (glyph != NULL)\    if (glyph != NULL)\
1242      {\      {\
1243        ui_draw_glyph (mixmode, x + (short) glyph->offset,\        ui_draw_glyph (mixmode, x + glyph->offset,\
1244                       y + (short) glyph->baseline,\                       y + glyph->baseline,\
1245                       glyph->width, glyph->height,\                       glyph->width, glyph->height,\
1246                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\
1247        if (flags & TEXT2_IMPLICIT_X)\        if (flags & TEXT2_IMPLICIT_X)\
# Line 1099  ui_draw_text(uint8 font, uint8 flags, in Line 1263  ui_draw_text(uint8 font, uint8 flags, in
1263    
1264          if (boxcx > 1)          if (boxcx > 1)
1265          {          {
1266                  FILL_RECTANGLE(boxx, boxy, boxcx, boxcy);                  FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);
1267          }          }
1268          else if (mixmode == MIX_OPAQUE)          else if (mixmode == MIX_OPAQUE)
1269          {          {
1270                  FILL_RECTANGLE(clipx, clipy, clipcx, clipcy);                  FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);
1271          }          }
1272    
1273          /* Paint text, character by character */          /* Paint text, character by character */
# Line 1117  ui_draw_text(uint8 font, uint8 flags, in Line 1281  ui_draw_text(uint8 font, uint8 flags, in
1281                                  else                                  else
1282                                  {                                  {
1283                                          error("this shouldn't be happening\n");                                          error("this shouldn't be happening\n");
1284                                          break;                                          exit(1);
1285                                  }                                  }
1286                                  /* this will move pointer from start to first character after FF command */                                  /* this will move pointer from start to first character after FF command */
1287                                  length -= i + 3;                                  length -= i + 3;
# Line 1137  ui_draw_text(uint8 font, uint8 flags, in Line 1301  ui_draw_text(uint8 font, uint8 flags, in
1301                                                  else                                                  else
1302                                                          x += text[i + 2];                                                          x += text[i + 2];
1303                                          }                                          }
                                         if (i + 2 < length)  
                                                 i += 3;  
                                         else  
                                                 i += 2;  
                                         length -= i;  
                                         /* this will move pointer from start to first character after FE command */  
                                         text = &(text[i]);  
                                         i = 0;  
1304                                          for (j = 0; j < entry->size; j++)                                          for (j = 0; j < entry->size; j++)
1305                                                  DO_GLYPH(((uint8 *) (entry->data)), j);                                                  DO_GLYPH(((uint8 *) (entry->data)), j);
1306                                  }                                  }
1307                                    if (i + 2 < length)
1308                                            i += 3;
1309                                    else
1310                                            i += 2;
1311                                    length -= i;
1312                                    /* this will move pointer from start to first character after FE command */
1313                                    text = &(text[i]);
1314                                    i = 0;
1315                                  break;                                  break;
1316    
1317                          default:                          default:
# Line 1156  ui_draw_text(uint8 font, uint8 flags, in Line 1320  ui_draw_text(uint8 font, uint8 flags, in
1320                                  break;                                  break;
1321                  }                  }
1322          }          }
1323            if (ownbackstore)
1324            {
1325                    if (boxcx > 1)
1326                            XCopyArea(display, backstore, wnd, gc, boxx,
1327                                      boxy, boxcx, boxcy, boxx, boxy);
1328                    else
1329                            XCopyArea(display, backstore, wnd, gc, clipx,
1330                                      clipy, clipcx, clipcy, clipx, clipy);
1331            }
1332  }  }
1333    
1334  void  void

Legend:
Removed from v.185  
changed lines
  Added in v.309

  ViewVC Help
Powered by ViewVC 1.1.26