/[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 121 by matthewc, Sat Sep 14 11:48:44 2002 UTC revision 311 by jsorg71, Wed Feb 5 14:16:33 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 int dpy_width;  static XIM IM;
48  static int dpy_height;  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 53  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 60  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  static BOOL owncolmap;  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 ? translate_colour(col) : owncolmap ? col : translate_colour(colmap[col]) )
 BOOL enable_compose = False;  
 static XIM IM = NULL;  
 static XIC IC = NULL;  
   
 /* toggle fullscreen globals */  
 static unsigned long input_mask;  
   
 #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )  
93  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));
94  #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));  #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));
95    
# Line 99  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  static void  void
119  translate8(uint8 * data, uint8 * out, uint8 * end)  mwm_hide_decorations(void)
 {  
         while (out < end)  
                 *(out++) = (uint8) colmap[*(data++)];  
 }  
   
 static void  
 translate16(uint8 * data, uint16 * out, uint16 * end)  
120  {  {
121          while (out < end)          PropMotifWmHints motif_hints;
122                  *(out++) = (uint16) colmap[*(data++)];          Atom hintsatom;
 }  
123    
124  /* little endian - conversion happens when colourmap is built */          /* setup the property */
125  static void          motif_hints.flags = MWM_HINTS_DECORATIONS;
126  translate24(uint8 * data, uint8 * out, uint8 * end)          motif_hints.decorations = 0;
 {  
         uint32 value;  
127    
128          while (out < end)          /* get the atom for the property */
129            hintsatom = XInternAtom(display, "_MOTIF_WM_HINTS", False);
130            if (!hintsatom)
131          {          {
132                  value = colmap[*(data++)];                  warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n");
133                  *(out++) = value;                  return;
                 *(out++) = value >> 8;  
                 *(out++) = value >> 16;  
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  uint32
141  translate32(uint8 * data, uint32 * out, uint32 * end)  colour16to24(uint32 colour)
142  {  {
143          while (out < end)          int r;
144                  *(out++) = colmap[*(data++)];          int g;
145            int b;
146            r = (colour & 0xf800) >> 11;
147            r = (r * 0xff) / 0x1f;
148            g = (colour & 0x07e0) >> 5;
149            g = (g * 0xff) / 0x3f;
150            b = (colour & 0x001f);
151            b = (b * 0xff) / 0x1f;
152            return (r << 16) | (g << 8) | b;
153  }  }
154    
155  static uint8 *  uint32
156  translate_image(int width, int height, uint8 * data)  colour16to32(uint32 colour)
157  {  {
158          int size = width * height * bpp / 8;          return colour16to24(colour);
159          uint8 *out = xmalloc(size);  }
         uint8 *end = out + size;  
   
         switch (bpp)  
         {  
                 case 8:  
                         translate8(data, out, end);  
                         break;  
   
                 case 16:  
                         translate16(data, (uint16 *) out, (uint16 *) end);  
                         break;  
   
                 case 24:  
                         translate24(data, out, end);  
                         break;  
   
                 case 32:  
                         translate32(data, (uint32 *) out, (uint32 *) end);  
                         break;  
         }  
160    
161          return out;  uint32
162    colour24to32(uint32 colour)
163    {
164            return colour;
165  }  }
166    
167  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
# Line 172  translate_image(int width, int height, u Line 172  translate_image(int width, int height, u
172  static uint32  static uint32
173  translate_colour(uint32 colour)  translate_colour(uint32 colour)
174  {  {
175            switch (server_bpp)
176            {
177                    case 16:
178                            switch (bpp)
179                            {
180                                    case 16:
181                                            break;
182                                    case 24:
183                                            colour = colour16to24(colour);
184                                            break;
185                                    case 32:
186                                            colour = colour16to32(colour);
187                                            break;
188                            }
189                            break;
190                    case 24:
191                            switch (bpp)
192                            {
193                                    case 24:
194                                            break;
195                                    case 32:
196                                            colour = colour24to32(colour);
197                                            break;
198                            }
199                            break;
200            }
201          switch (bpp)          switch (bpp)
202          {          {
203                  case 16:                  case 16:
# Line 193  translate_colour(uint32 colour) Line 219  translate_colour(uint32 colour)
219          return colour;          return colour;
220  }  }
221    
222  static unsigned long  static void
223  init_inputmethod(void)  translate8to8(uint8 * data, uint8 * out, uint8 * end)
224  {  {
225          unsigned long filtered_events = 0;          while (out < end)
226                    *(out++) = (uint8) colmap[*(data++)];
227    }
228    
229          IM = XOpenIM(display, NULL, NULL, NULL);  static void
230          if (IM == NULL)  translate8to16(uint8 * data, uint16 * out, uint16 * end)
231          {  {
232                  error("Failed to open input method\n");          while (out < end)
233          }                  *(out++) = (uint16) colmap[*(data++)];
234    }
235    
236          if (IM != NULL)  static void
237          {  translate16to16(uint16 * data, uint16 * out, uint16 * end)
238                  /* Must be done after XCreateWindow */  {
239                  IC = XCreateIC(IM, XNInputStyle,          while (out < end)
240                                 (XIMPreeditNothing | XIMStatusNothing),                  *(out++) = (uint16) translate_colour(*(data++));
241                                 XNClientWindow, wnd, XNFocusWindow, wnd, NULL);  }
242    
243                  if (IC == NULL)  /* little endian - conversion happens when colourmap is built */
244                  {  static void
245                          error("Failed to create input context\n");  translate8to24(uint8 * data, uint8 * out, uint8 * end)
246                          XCloseIM(IM);  {
247                          IM = NULL;          uint32 value;
248                  }  
249            while (out < end)
250            {
251                    value = colmap[*(data++)];
252                    *(out++) = value;
253                    *(out++) = value >> 8;
254                    *(out++) = value >> 16;
255          }          }
256    }
257    
258          /* For correct Multi_key/Compose processing, I guess.  static void
259             It seems to work alright anyway, though. */  translate16to24(uint16 * data, uint8 * out, uint8 * end)
260          if (IC != NULL)  {
261            uint32 value;
262    
263            while (out < end)
264          {          {
265                  if (XGetICValues(IC, XNFilterEvents, &filtered_events, NULL) != NULL)                  value = translate_colour(*(data++));
266                  {                  *(out++) = value;
267                          error("Failed to obtain XNFilterEvents value from IC\n");                  *(out++) = value >> 8;
268                          filtered_events = 0;                  *(out++) = value >> 16;
                 }  
269          }          }
         return filtered_events;  
270  }  }
271    
272  static void  static void
273  close_inputmethod(void)  translate8to32(uint8 * data, uint32 * out, uint32 * end)
274  {  {
275          if (IC != NULL)          while (out < end)
276                    *(out++) = colmap[*(data++)];
277    }
278    
279    static void
280    translate16to32(uint16 * data, uint32 * out, uint32 * end)
281    {
282            while (out < end)
283                    *(out++) = translate_colour(*(data++));
284    }
285    
286    static uint8 *
287    translate_image(int width, int height, uint8 * data)
288    {
289            int size = width * height * bpp / 8;
290            uint8 *out = xmalloc(size);
291            uint8 *end = out + size;
292    
293            if (server_bpp == 16)
294          {          {
295                  XDestroyIC(IC);                  if (bpp == 16)
296                  if (IM != NULL)                          translate16to16((uint16 *) data, (uint16 *) out, (uint16 *) end);
297                  {                  else if (bpp == 24)
298                          XCloseIM(IM);                          translate16to24((uint16 *) data, out, end); /* todo, check this one */
299                          IM = NULL;                  else if (bpp == 32)
300                  }                          translate16to32((uint16 *) data, (uint32 *) out, (uint32 *) end);
301                    return out;
302          }          }
303            /* todo needs server_bpp == 24 */
304            switch (bpp)
305            {
306                    case 8:
307                            translate8to8(data, out, end);
308                            break;
309    
310                    case 16:
311                            translate8to16(data, (uint16 *) out, (uint16 *) end);
312                            break;
313    
314                    case 24:
315                            translate8to24(data, out, end);
316                            break;
317    
318                    case 32:
319                            translate8to32(data, (uint32 *) out, (uint32 *) end);
320                            break;
321            }
322    
323            return out;
324  }  }
325    
326  BOOL  BOOL
327  get_key_state(int keysym)  get_key_state(unsigned int state, uint32 keysym)
328  {  {
329          int keysymMask = 0, modifierpos, key;          int modifierpos, key, keysymMask = 0;
         Window wDummy1, wDummy2;  
         int iDummy3, iDummy4, iDummy5, iDummy6;  
         unsigned int current_state;  
330          int offset;          int offset;
331    
         XModifierKeymap *map = XGetModifierMapping(display);  
332          KeyCode keycode = XKeysymToKeycode(display, keysym);          KeyCode keycode = XKeysymToKeycode(display, keysym);
333    
334          if (keycode == NoSymbol)          if (keycode == NoSymbol)
# Line 263  get_key_state(int keysym) Line 336  get_key_state(int keysym)
336    
337          for (modifierpos = 0; modifierpos < 8; modifierpos++)          for (modifierpos = 0; modifierpos < 8; modifierpos++)
338          {          {
339                  offset = map->max_keypermod * modifierpos;                  offset = mod_map->max_keypermod * modifierpos;
340    
341                  for (key = 0; key < map->max_keypermod; key++)                  for (key = 0; key < mod_map->max_keypermod; key++)
342                  {                  {
343                          if (map->modifiermap[offset + key] == keycode)                          if (mod_map->modifiermap[offset + key] == keycode)
344                                  keysymMask = 1 << modifierpos;                                  keysymMask |= 1 << modifierpos;
345                  }                  }
346          }          }
347    
348          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;  
349  }  }
350    
   
351  BOOL  BOOL
352  ui_init()  ui_init(void)
353  {  {
354          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
355          uint16 test;          uint16 test;
# Line 291  ui_init() Line 358  ui_init()
358          display = XOpenDisplay(NULL);          display = XOpenDisplay(NULL);
359          if (display == NULL)          if (display == NULL)
360          {          {
361                  error("Failed to open display\n");                  error("Failed to open display: %s\n", XDisplayName(NULL));
362                  return False;                  return False;
363          }          }
364    
# Line 322  ui_init() Line 389  ui_init()
389                  return False;                  return False;
390          }          }
391    
392          if (depth <= 8)          if (owncolmap != True)
393                  owncolmap = True;          {
         else  
394                  xcolmap = DefaultColormapOfScreen(screen);                  xcolmap = DefaultColormapOfScreen(screen);
395                    if (depth <= 8)
396                            warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n");
397            }
398    
399            gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL);
400    
401          if (DoesBackingStore(screen) == NotUseful)          if (DoesBackingStore(screen) != Always)
402                  ownbackstore = True;                  ownbackstore = True;
403    
404          test = 1;          test = 1;
405          host_be = !(BOOL) (*(uint8 *) (&test));          host_be = !(BOOL) (*(uint8 *) (&test));
406          xserver_be = (ImageByteOrder(display) == MSBFirst);          xserver_be = (ImageByteOrder(display) == MSBFirst);
407    
408            if ((width == 0) || (height == 0))
409            {
410                    /* Fetch geometry from _NET_WORKAREA */
411                    uint32 x, y, cx, cy;
412    
413                    if (get_current_workarea(&x, &y, &cx, &cy) == 0)
414                    {
415                            width = cx;
416                            height = cy;
417                    }
418                    else
419                    {
420                            warning("Failed to get workarea: probably your window manager does not support extended hints\n");
421                            width = 800;
422                            height = 600;
423                    }
424            }
425    
426          if (fullscreen)          if (fullscreen)
427          {          {
428                  width = WidthOfScreen(screen);                  width = WidthOfScreen(screen);
429                  height = HeightOfScreen(screen);                  height = HeightOfScreen(screen);
430          }          }
431    
432            /* make sure width is a multiple of 4 */
433            width = (width + 3) & ~3;
434    
435            if (ownbackstore)
436            {
437                    backstore =
438                            XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth);
439    
440                    /* clear to prevent rubbish being exposed at startup */
441                    XSetForeground(display, gc, BlackPixelOfScreen(screen));
442                    XFillRectangle(display, backstore, gc, 0, 0, width, height);
443            }
444    
445            mod_map = XGetModifierMapping(display);
446    
447            if (enable_compose)
448                    IM = XOpenIM(display, NULL, NULL, NULL);
449    
450          xkeymap_init();          xkeymap_init();
451          return True;          return True;
452  }  }
453    
454    void
455    ui_deinit(void)
456    {
457            if (IM != NULL)
458                    XCloseIM(IM);
459    
460            XFreeModifiermap(mod_map);
461    
462            if (ownbackstore)
463                    XFreePixmap(display, backstore);
464    
465            XFreeGC(display, gc);
466            XCloseDisplay(display);
467            display = NULL;
468    }
469    
470  BOOL  BOOL
471  ui_create_window()  ui_create_window(void)
472  {  {
473          XSetWindowAttributes attribs;          XSetWindowAttributes attribs;
474          XClassHint *classhints;          XClassHint *classhints;
475          XSizeHints *sizehints;          XSizeHints *sizehints;
476            int wndwidth, wndheight;
477            long input_mask, ic_input_mask;
478          XEvent xevent;          XEvent xevent;
479    
480            wndwidth = fullscreen ? WidthOfScreen(screen) : width;
481            wndheight = fullscreen ? HeightOfScreen(screen) : height;
482    
483          attribs.background_pixel = BlackPixelOfScreen(screen);          attribs.background_pixel = BlackPixelOfScreen(screen);
484          attribs.backing_store = ownbackstore ? NotUseful : Always;          attribs.backing_store = ownbackstore ? NotUseful : Always;
485          attribs.override_redirect = fullscreen;          attribs.override_redirect = fullscreen;
486          wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height,  
487            wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight,
488                              0, CopyFromParent, InputOutput, CopyFromParent,                              0, CopyFromParent, InputOutput, CopyFromParent,
489                              CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);                              CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
490    
491          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
492    
493            if (hide_decorations)
494                    mwm_hide_decorations();
495    
496          classhints = XAllocClassHint();          classhints = XAllocClassHint();
497          if (classhints != NULL)          if (classhints != NULL)
498          {          {
# Line 382  ui_create_window() Line 514  ui_create_window()
514          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
515                  VisibilityChangeMask | FocusChangeMask;                  VisibilityChangeMask | FocusChangeMask;
516    
         if (grab_keyboard)  
                 input_mask |= EnterWindowMask | LeaveWindowMask;  
517          if (sendmotion)          if (sendmotion)
518                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
519          if (ownbackstore)          if (ownbackstore)
520                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
521          if (enable_compose)          if (fullscreen || grab_keyboard)
522                  input_mask |= init_inputmethod();                  input_mask |= EnterWindowMask;
523            if (grab_keyboard)
524                    input_mask |= LeaveWindowMask;
525    
526          XSelectInput(display, wnd, input_mask);          if (IM != NULL)
527            {
528                    IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
529                                   XNClientWindow, wnd, XNFocusWindow, wnd, NULL);
530    
531          gc = XCreateGC(display, wnd, 0, NULL);                  if ((IC != NULL)
532                        && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))
533                            input_mask |= ic_input_mask;
534            }
535    
536            XSelectInput(display, wnd, input_mask);
537          XMapWindow(display, wnd);          XMapWindow(display, wnd);
538    
539          /* Wait for VisibilityNotify Event */          /* wait for VisibilityNotify */
540          for (;;)          do
541          {          {
542                  XNextEvent(display, &xevent);                  XMaskEvent(display, VisibilityChangeMask, &xevent);
                 if (xevent.type == VisibilityNotify)  
                         break;  
543          }          }
544            while (xevent.type != VisibilityNotify);
545    
546          if (ownbackstore)          focused = False;
547                  backstore = XCreatePixmap(display, wnd, width, height, depth);          mouse_in_wnd = False;
548    
549          /* clear the window so that cached data is not viewed upon start... */          /* handle the WM_DELETE_WINDOW protocol */
550          XSetBackground(display, gc, 0);          protocol_atom = XInternAtom(display, "WM_PROTOCOLS", True);
551          XSetForeground(display, gc, 0);          kill_atom = XInternAtom(display, "WM_DELETE_WINDOW", True);
552          FILL_RECTANGLE(0, 0, width, height);          XSetWMProtocols(display, wnd, &kill_atom, 1);
         /* make sure the window is focused */  
         XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);  
553    
554          return True;          return True;
555  }  }
556    
557  void  void
558  ui_destroy_window()  ui_destroy_window(void)
559  {  {
560          if (ownbackstore)          if (IC != NULL)
561                  XFreePixmap(display, backstore);                  XDestroyIC(IC);
   
         XFreeGC(display, gc);  
   
         close_inputmethod();  
562    
563          XDestroyWindow(display, wnd);          XDestroyWindow(display, wnd);
         XCloseDisplay(display);  
         display = NULL;  
564  }  }
565    
566  void  void
567  reset_keys()  xwin_toggle_fullscreen(void)
568  {  {
569          /* reset keys */          Pixmap contents = 0;
         uint32 ev_time;  
         ev_time = time(NULL);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);  
 }  
570    
571  void          if (!ownbackstore)
572  toggle_fullscreen()          {
573  {                  /* need to save contents of window */
574          /* save window contents */                  contents = XCreatePixmap(display, wnd, width, height, depth);
575          Pixmap pixmap;                  XCopyArea(display, wnd, contents, gc, 0, 0, width, height, 0, 0);
576          pixmap = XCreatePixmap(display, wnd, width, height, depth);          }
577          if (ownbackstore)  
578                  XCopyArea(display, backstore, pixmap, gc, 0, 0, width, height, 0, 0);          ui_destroy_window();
579          else          fullscreen = !fullscreen;
                 XCopyArea(display, wnd, pixmap, gc, 0, 0, width, height, 0, 0);  
         fullscreen = fullscreen ? False : True;  
         close_inputmethod();  
         if (ownbackstore)  
                 XFreePixmap(display, backstore);  
         XFreeGC(display, gc);  
         XDestroyWindow(display, wnd);  
580          ui_create_window();          ui_create_window();
581          ui_set_cursor(cache_get_cursor(0));  
582          ui_move_pointer(width / 2, height / 2);          XDefineCursor(display, wnd, current_cursor);
583          reset_keys();  
584          /* restore window contents */          if (!ownbackstore)
585          if (ownbackstore)          {
586                  XCopyArea(display, pixmap, backstore, gc, 0, 0, width, height, 0, 0);                  XCopyArea(display, contents, wnd, gc, 0, 0, width, height, 0, 0);
587          XCopyArea(display, pixmap, wnd, gc, 0, 0, width, height, 0, 0);                  XFreePixmap(display, contents);
588          XFreePixmap(display, pixmap);          }
589  }  }
590    
591  /* Process all events in Xlib queue */  /* Process all events in Xlib queue
592  static void     Returns 0 after user quit, 1 otherwise */
593  xwin_process_events()  static int
594    xwin_process_events(void)
595  {  {
596          XEvent xevent;          XEvent xevent;
   
597          KeySym keysym;          KeySym keysym;
598          uint16 button, flags;          uint16 button, flags;
599          uint32 ev_time;          uint32 ev_time;
600          key_translation tr;          key_translation tr;
         char *ksname = NULL;  
601          char str[256];          char str[256];
602          Status status;          Status status;
603            unsigned int state;
604            Window wdummy;
605            int dummy;
606    
607          while (XCheckMaskEvent(display, ~0, &xevent))          while (XPending(display) > 0)
608          {          {
609                  if (enable_compose && (XFilterEvent(&xevent, None) == True))                  XNextEvent(display, &xevent);
610    
611                    if ((IC != NULL) && (XFilterEvent(&xevent, None) == True))
612                  {                  {
613                          DEBUG_KBD(("Filtering event\n"));                          DEBUG_KBD(("Filtering event\n"));
614                          continue;                          continue;
615                  }                  }
616    
                 ev_time = time(NULL);  
617                  flags = 0;                  flags = 0;
618    
619                  switch (xevent.type)                  switch (xevent.type)
620                  {                  {
621                            case ClientMessage:
622                                    /* the window manager told us to quit */
623                                    if ((xevent.xclient.message_type == protocol_atom)
624                                        && (xevent.xclient.data.l[0] == kill_atom))
625                                            /* Quit */
626                                            return 0;
627                                    break;
628    
629                          case KeyPress:                          case KeyPress:
630                                  if (IC != NULL)                                  if (IC != NULL)
631                                          /* Multi_key compatible version */                                          /* Multi_key compatible version */
# Line 518  xwin_process_events() Line 643  xwin_process_events()
643                                  else                                  else
644                                  {                                  {
645                                          /* Plain old XLookupString */                                          /* Plain old XLookupString */
646                                          DEBUG_KBD(("No input context, using XLookupString\n"));                                          DEBUG_KBD(("\nNo input context, using XLookupString\n"));
647                                          XLookupString((XKeyEvent *) & xevent,                                          XLookupString((XKeyEvent *) & xevent,
648                                                        str, sizeof(str), &keysym, NULL);                                                        str, sizeof(str), &keysym, NULL);
649                                  }                                  }
650    
651                                  ksname = get_ksname(keysym);                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym,
652                                  DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));                                             get_ksname(keysym)));
653    
654                                  if (handle_special_keys(keysym, ev_time, True))                                  ev_time = time(NULL);
655                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))
656                                          break;                                          break;
657    
658                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 539  xwin_process_events() Line 665  xwin_process_events()
665    
666                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
667                                  break;                                  break;
668    
669                          case KeyRelease:                          case KeyRelease:
670                                  XLookupString((XKeyEvent *) & xevent, str,                                  XLookupString((XKeyEvent *) & xevent, str,
671                                                sizeof(str), &keysym, NULL);                                                sizeof(str), &keysym, NULL);
672    
                                 ksname = get_ksname(keysym);  
673                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
674                                             ksname));                                             get_ksname(keysym)));
675    
676                                  if (handle_special_keys(keysym, ev_time, False))                                  ev_time = time(NULL);
677                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False))
678                                          break;                                          break;
679    
680                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 568  xwin_process_events() Line 695  xwin_process_events()
695                                  if (button == 0)                                  if (button == 0)
696                                          break;                                          break;
697    
698                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
699                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);
700                                  break;                                  break;
701    
702                          case MotionNotify:                          case MotionNotify:
703                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  if (fullscreen && !focused)
704                                            XSetInputFocus(display, wnd, RevertToPointerRoot,
705                                                           CurrentTime);
706                                    rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
707                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
708                                  break;                                  break;
709    
710                          case FocusIn:                          case FocusIn:
711                                  /* fall through */                                  if (xevent.xfocus.mode == NotifyGrab)
712                          case EnterNotify:                                          break;
713                                  if (grab_keyboard)                                  focused = True;
714                                    XQueryPointer(display, wnd, &wdummy, &wdummy, &dummy, &dummy,
715                                                  &dummy, &dummy, &state);
716                                    reset_modifier_keys(state);
717                                    if (grab_keyboard && mouse_in_wnd)
718                                          XGrabKeyboard(display, wnd, True,                                          XGrabKeyboard(display, wnd, True,
719                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
720                                  break;                                  break;
721    
722                          case FocusOut:                          case FocusOut:
723                                  reset_keys();                                  if (xevent.xfocus.mode == NotifyUngrab)
724                                  /* fall through */                                          break;
725                          case LeaveNotify:                                  focused = False;
726                                  if (grab_keyboard)                                  if (xevent.xfocus.mode == NotifyWhileGrabbed)
727                                          XUngrabKeyboard(display, CurrentTime);                                          XUngrabKeyboard(display, CurrentTime);
728                                  break;                                  break;
729    
730                            case EnterNotify:
731                                    /* we only register for this event when in fullscreen mode */
732                                    /* or grab_keyboard */
733                                    mouse_in_wnd = True;
734                                    if (fullscreen)
735                                    {
736                                            XSetInputFocus(display, wnd, RevertToPointerRoot,
737                                                           CurrentTime);
738                                            break;
739                                    }
740                                    if (focused)
741                                            XGrabKeyboard(display, wnd, True,
742                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
743                                    break;
744    
745                            case LeaveNotify:
746                                    /* we only register for this event when grab_keyboard */
747                                    mouse_in_wnd = False;
748                                    XUngrabKeyboard(display, CurrentTime);
749                                    break;
750    
751                          case Expose:                          case Expose:
752                                  XCopyArea(display, backstore, wnd, gc,                                  XCopyArea(display, backstore, wnd, gc,
753                                            xevent.xexpose.x, xevent.xexpose.y,                                            xevent.xexpose.x, xevent.xexpose.y,
# Line 607  xwin_process_events() Line 762  xwin_process_events()
762                                  if (xevent.xmapping.request == MappingKeyboard                                  if (xevent.xmapping.request == MappingKeyboard
763                                      || xevent.xmapping.request == MappingModifier)                                      || xevent.xmapping.request == MappingModifier)
764                                          XRefreshKeyboardMapping(&xevent.xmapping);                                          XRefreshKeyboardMapping(&xevent.xmapping);
765    
766                                    if (xevent.xmapping.request == MappingModifier)
767                                    {
768                                            XFreeModifiermap(mod_map);
769                                            mod_map = XGetModifierMapping(display);
770                                    }
771                                  break;                                  break;
772    
773                  }                  }
774          }          }
775            /* Keep going */
776            return 1;
777  }  }
778    
779  void  /* Returns 0 after user quit, 1 otherwise */
780    int
781  ui_select(int rdp_socket)  ui_select(int rdp_socket)
782  {  {
783          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 624  ui_select(int rdp_socket) Line 788  ui_select(int rdp_socket)
788          while (True)          while (True)
789          {          {
790                  /* Process any events already waiting */                  /* Process any events already waiting */
791                  XFlush(display);                  if (!xwin_process_events())
792                  xwin_process_events();                          /* User quit */
793                            return 0;
794    
795                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
796                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
# Line 641  ui_select(int rdp_socket) Line 806  ui_select(int rdp_socket)
806                  }                  }
807    
808                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
809                          return;                          return 1;
810          }          }
811  }  }
812    
# Line 661  ui_create_bitmap(int width, int height, Line 826  ui_create_bitmap(int width, int height,
826          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = (owncolmap ? data : translate_image(width, height, data));
827          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(display, wnd, width, height, depth);
828          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
829                               (char *) tdata, width, height, 8, 0);                               (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0);
830    
831          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
832    
# Line 676  ui_paint_bitmap(int x, int y, int cx, in Line 841  ui_paint_bitmap(int x, int y, int cx, in
841  {  {
842          XImage *image;          XImage *image;
843          uint8 *tdata;          uint8 *tdata;
   
844          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = (owncolmap ? data : translate_image(width, height, data));
845          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
846                               (char *) tdata, width, height, 8, 0);                               (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0);
847    
848          if (ownbackstore)          if (ownbackstore)
849          {          {
# Line 808  ui_create_cursor(unsigned int x, unsigne Line 972  ui_create_cursor(unsigned int x, unsigne
972  void  void
973  ui_set_cursor(HCURSOR cursor)  ui_set_cursor(HCURSOR cursor)
974  {  {
975          XDefineCursor(display, wnd, (Cursor) cursor);          current_cursor = (Cursor) cursor;
976            XDefineCursor(display, wnd, current_cursor);
977  }  }
978    
979  void  void
# Line 823  ui_destroy_cursor(HCURSOR cursor) Line 988  ui_destroy_cursor(HCURSOR cursor)
988                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \
989                  (xc)->flags = DoRed | DoGreen | DoBlue;                  (xc)->flags = DoRed | DoGreen | DoBlue;
990    
991    
992  HCOLOURMAP  HCOLOURMAP
993  ui_create_colourmap(COLOURMAP * colours)  ui_create_colourmap(COLOURMAP * colours)
994  {  {
995          COLOURENTRY *entry;          COLOURENTRY *entry;
996          int i, ncolours = colours->ncolours;          int i, ncolours = colours->ncolours;
997            if (!owncolmap)
998            {
999                    uint32 *map = xmalloc(sizeof(*colmap) * ncolours);
1000                    XColor xentry;
1001                    XColor xc_cache[256];
1002                    uint32 colour;
1003                    int colLookup = 256;
1004                    for (i = 0; i < ncolours; i++)
1005                    {
1006                            entry = &colours->colours[i];
1007                            MAKE_XCOLOR(&xentry, entry);
1008    
1009          if (owncolmap)                          if (XAllocColor(display, xcolmap, &xentry) == 0)
1010                            {
1011                                    /* Allocation failed, find closest match. */
1012                                    int j = 256;
1013                                    int nMinDist = 3 * 256 * 256;
1014                                    long nDist = nMinDist;
1015    
1016                                    /* only get the colors once */
1017                                    while (colLookup--)
1018                                    {
1019                                            xc_cache[colLookup].pixel = colLookup;
1020                                            xc_cache[colLookup].red = xc_cache[colLookup].green =
1021                                                    xc_cache[colLookup].blue = 0;
1022                                            xc_cache[colLookup].flags = 0;
1023                                            XQueryColor(display,
1024                                                        DefaultColormap(display,
1025                                                                        DefaultScreen(display)),
1026                                                        &xc_cache[colLookup]);
1027                                    }
1028                                    colLookup = 0;
1029    
1030                                    /* approximate the pixel */
1031                                    while (j--)
1032                                    {
1033                                            if (xc_cache[j].flags)
1034                                            {
1035                                                    nDist = ((long) (xc_cache[j].red >> 8) -
1036                                                             (long) (xentry.red >> 8)) *
1037                                                            ((long) (xc_cache[j].red >> 8) -
1038                                                             (long) (xentry.red >> 8)) +
1039                                                            ((long) (xc_cache[j].green >> 8) -
1040                                                             (long) (xentry.green >> 8)) *
1041                                                            ((long) (xc_cache[j].green >> 8) -
1042                                                             (long) (xentry.green >> 8)) +
1043                                                            ((long) (xc_cache[j].blue >> 8) -
1044                                                             (long) (xentry.blue >> 8)) *
1045                                                            ((long) (xc_cache[j].blue >> 8) -
1046                                                             (long) (xentry.blue >> 8));
1047                                            }
1048                                            if (nDist < nMinDist)
1049                                            {
1050                                                    nMinDist = nDist;
1051                                                    xentry.pixel = j;
1052                                            }
1053                                    }
1054                            }
1055                            colour = xentry.pixel;
1056    
1057                            /* update our cache */
1058                            if (xentry.pixel < 256)
1059                            {
1060                                    xc_cache[xentry.pixel].red = xentry.red;
1061                                    xc_cache[xentry.pixel].green = xentry.green;
1062                                    xc_cache[xentry.pixel].blue = xentry.blue;
1063    
1064                            }
1065    
1066    
1067                            /* byte swap here to make translate_image faster */
1068                            map[i] = translate_colour(colour);
1069                    }
1070                    return map;
1071            }
1072            else
1073          {          {
1074                  XColor *xcolours, *xentry;                  XColor *xcolours, *xentry;
1075                  Colormap map;                  Colormap map;
# Line 849  ui_create_colourmap(COLOURMAP * colours) Line 1089  ui_create_colourmap(COLOURMAP * colours)
1089                  xfree(xcolours);                  xfree(xcolours);
1090                  return (HCOLOURMAP) map;                  return (HCOLOURMAP) map;
1091          }          }
         else  
         {  
                 uint32 *map = xmalloc(sizeof(*colmap) * ncolours);  
                 XColor xentry;  
                 uint32 colour;  
   
                 for (i = 0; i < ncolours; i++)  
                 {  
                         entry = &colours->colours[i];  
                         MAKE_XCOLOR(&xentry, entry);  
   
                         if (XAllocColor(display, xcolmap, &xentry) != 0)  
                                 colour = xentry.pixel;  
                         else  
                                 colour = WhitePixelOfScreen(screen);  
   
                         /* byte swap here to make translate_image faster */  
                         map[i] = translate_colour(colour);  
                 }  
   
                 return map;  
         }  
1092  }  }
1093    
1094  void  void
1095  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
1096  {  {
1097          if (owncolmap)          if (!owncolmap)
                 XFreeColormap(display, (Colormap) map);  
         else  
1098                  xfree(map);                  xfree(map);
1099            else
1100                    XFreeColormap(display, (Colormap) map);
1101  }  }
1102    
1103  void  void
1104  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
1105  {  {
1106          if (owncolmap)          if (!owncolmap)
                 XSetWindowColormap(display, wnd, (Colormap) map);  
         else  
1107                  colmap = map;                  colmap = map;
1108            else
1109                    XSetWindowColormap(display, wnd, (Colormap) map);
1110  }  }
1111    
1112  void  void
# Line 904  ui_set_clip(int x, int y, int cx, int cy Line 1122  ui_set_clip(int x, int y, int cx, int cy
1122  }  }
1123    
1124  void  void
1125  ui_reset_clip()  ui_reset_clip(void)
1126  {  {
1127          XRectangle rect;          XRectangle rect;
1128    
# Line 916  ui_reset_clip() Line 1134  ui_reset_clip()
1134  }  }
1135    
1136  void  void
1137  ui_bell()  ui_bell(void)
1138  {  {
1139          XBell(display, 0);          XBell(display, 0);
1140  }  }
# Line 1051  ui_rect( Line 1269  ui_rect(
1269          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE(x, y, cx, cy);
1270  }  }
1271    
1272    /* warning, this function only draws on wnd or backstore, not both */
1273  void  void
1274  ui_draw_glyph(int mixmode,  ui_draw_glyph(int mixmode,
1275                /* dest */ int x, int y, int cx, int cy,                /* dest */ int x, int y, int cx, int cy,
# Line 1065  ui_draw_glyph(int mixmode, Line 1284  ui_draw_glyph(int mixmode,
1284          XSetStipple(display, gc, (Pixmap) glyph);          XSetStipple(display, gc, (Pixmap) glyph);
1285          XSetTSOrigin(display, gc, x, y);          XSetTSOrigin(display, gc, x, y);
1286    
1287          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1288    
1289          XSetFillStyle(display, gc, FillSolid);          XSetFillStyle(display, gc, FillSolid);
1290  }  }
# Line 1094  ui_draw_glyph(int mixmode, Line 1313  ui_draw_glyph(int mixmode,
1313      }\      }\
1314    if (glyph != NULL)\    if (glyph != NULL)\
1315      {\      {\
1316        ui_draw_glyph (mixmode, x + (short) glyph->offset,\        ui_draw_glyph (mixmode, x + glyph->offset,\
1317                       y + (short) glyph->baseline,\                       y + glyph->baseline,\
1318                       glyph->width, glyph->height,\                       glyph->width, glyph->height,\
1319                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\
1320        if (flags & TEXT2_IMPLICIT_X)\        if (flags & TEXT2_IMPLICIT_X)\
# Line 1117  ui_draw_text(uint8 font, uint8 flags, in Line 1336  ui_draw_text(uint8 font, uint8 flags, in
1336    
1337          if (boxcx > 1)          if (boxcx > 1)
1338          {          {
1339                  FILL_RECTANGLE(boxx, boxy, boxcx, boxcy);                  FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);
1340          }          }
1341          else if (mixmode == MIX_OPAQUE)          else if (mixmode == MIX_OPAQUE)
1342          {          {
1343                  FILL_RECTANGLE(clipx, clipy, clipcx, clipcy);                  FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);
1344          }          }
1345    
1346          /* Paint text, character by character */          /* Paint text, character by character */
# Line 1135  ui_draw_text(uint8 font, uint8 flags, in Line 1354  ui_draw_text(uint8 font, uint8 flags, in
1354                                  else                                  else
1355                                  {                                  {
1356                                          error("this shouldn't be happening\n");                                          error("this shouldn't be happening\n");
1357                                          break;                                          exit(1);
1358                                  }                                  }
1359                                  /* this will move pointer from start to first character after FF command */                                  /* this will move pointer from start to first character after FF command */
1360                                  length -= i + 3;                                  length -= i + 3;
# Line 1155  ui_draw_text(uint8 font, uint8 flags, in Line 1374  ui_draw_text(uint8 font, uint8 flags, in
1374                                                  else                                                  else
1375                                                          x += text[i + 2];                                                          x += text[i + 2];
1376                                          }                                          }
                                         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;  
1377                                          for (j = 0; j < entry->size; j++)                                          for (j = 0; j < entry->size; j++)
1378                                                  DO_GLYPH(((uint8 *) (entry->data)), j);                                                  DO_GLYPH(((uint8 *) (entry->data)), j);
1379                                  }                                  }
1380                                    if (i + 2 < length)
1381                                            i += 3;
1382                                    else
1383                                            i += 2;
1384                                    length -= i;
1385                                    /* this will move pointer from start to first character after FE command */
1386                                    text = &(text[i]);
1387                                    i = 0;
1388                                  break;                                  break;
1389    
1390                          default:                          default:
# Line 1174  ui_draw_text(uint8 font, uint8 flags, in Line 1393  ui_draw_text(uint8 font, uint8 flags, in
1393                                  break;                                  break;
1394                  }                  }
1395          }          }
1396            if (ownbackstore)
1397            {
1398                    if (boxcx > 1)
1399                            XCopyArea(display, backstore, wnd, gc, boxx,
1400                                      boxy, boxcx, boxcy, boxx, boxy);
1401                    else
1402                            XCopyArea(display, backstore, wnd, gc, clipx,
1403                                      clipy, clipcx, clipcy, clipx, clipy);
1404            }
1405  }  }
1406    
1407  void  void

Legend:
Removed from v.121  
changed lines
  Added in v.311

  ViewVC Help
Powered by ViewVC 1.1.26