/[rdesktop]/sourceforge.net/branches/seamlessrdp-branch/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/branches/seamlessrdp-branch/rdesktop/xwin.c

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

revision 64 by astrand, Thu Jul 18 16:38:31 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 20  Line 20 
20    
21  #include <X11/Xlib.h>  #include <X11/Xlib.h>
22  #include <X11/Xutil.h>  #include <X11/Xutil.h>
 #include <X11/XKBlib.h>  
23  #include <time.h>  #include <time.h>
24  #include <errno.h>  #include <errno.h>
25  #include "rdesktop.h"  #include "rdesktop.h"
26    
 extern char keymapname[16];  
 extern int keylayout;  
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;
32    extern BOOL hide_decorations;
33    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;
 XkbDescPtr xkb;  
40  static int x_socket;  static int x_socket;
41    static Screen *screen;
42  static Window wnd;  static Window wnd;
43  static GC gc;  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 49  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  /* needed to keep track of the modifiers */  /* MWM decorations */
62  static unsigned int numlock_modifier_mask = 0;  #define MWM_HINTS_DECORATIONS   (1L << 1)
63  static unsigned int key_down_state = 0;  #define PROP_MOTIF_WM_HINTS_ELEMENTS    5
64    typedef struct
65    {
66  #define DShift1Mask   (1<<0)          uint32 flags;
67  #define DLockMask     (1<<1)          uint32 functions;
68  #define DControl1Mask (1<<2)          uint32 decorations;
69  #define DMod1Mask     (1<<3)          sint32 inputMode;
70  #define DMod2Mask     (1<<4)          uint32 status;
71  #define DMod3Mask     (1<<5)  }
72  #define DMod4Mask     (1<<6)  PropMotifWmHints;
73  #define DMod5Mask     (1<<7)  
 #define DShift2Mask   (1<<8)  
 #define DControl2Mask (1<<9)  
 #define DNumLockMask  (1<<10)  
74    
75  #define FILL_RECTANGLE(x,y,cx,cy)\  #define FILL_RECTANGLE(x,y,cx,cy)\
76  { \  { \
# Line 73  static unsigned int key_down_state = 0; Line 79  static unsigned int key_down_state = 0;
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;
 static uint32 white;  
90  static uint32 *colmap;  static uint32 *colmap;
91    
92  #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )  #define TRANSLATE(col)          ( server_bpp != 8 ? translate_colour(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 105  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 xwin_get_numlock_mask();  void
119  void xwin_mod_update(uint32 state, uint32 ev_time);  mwm_hide_decorations(void)
120  void xwin_mod_release(uint32 state, uint32 ev_time, uint32 scancode);  {
121  void xwin_mod_press(uint32 state, uint32 ev_time, uint32 scancode);          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    uint32
141    colour16to24(uint32 colour)
142    {
143            int r;
144            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    uint32
156    colour16to32(uint32 colour)
157    {
158            return colour16to24(colour);
159    }
160    
161    uint32
162    colour24to32(uint32 colour)
163    {
164            return colour;
165    }
166    
167    #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
168    #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }
169    #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
170                            x = (x << 16) | (x >> 16); }
171    
172    static uint32
173    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)
202            {
203                    case 16:
204                            if (host_be != xserver_be)
205                                    BSWAP16(colour);
206                            break;
207    
208                    case 24:
209                            if (xserver_be)
210                                    BSWAP24(colour);
211                            break;
212    
213                    case 32:
214                            if (host_be != xserver_be)
215                                    BSWAP32(colour);
216                            break;
217            }
218    
219            return colour;
220    }
221    
222  static void  static void
223  translate8(uint8 * data, uint8 * out, uint8 * end)  translate8to8(uint8 * data, uint8 * out, uint8 * end)
224  {  {
225          while (out < end)          while (out < end)
226                  *(out++) = (uint8) colmap[*(data++)];                  *(out++) = (uint8) colmap[*(data++)];
227  }  }
228    
229  static void  static void
230  translate16(uint8 * data, uint16 * out, uint16 * end)  translate8to16(uint8 * data, uint16 * out, uint16 * end)
231  {  {
232          while (out < end)          while (out < end)
233                  *(out++) = (uint16) colmap[*(data++)];                  *(out++) = (uint16) colmap[*(data++)];
234  }  }
235    
236    static void
237    translate16to16(uint16 * data, uint16 * out, uint16 * end)
238    {
239            while (out < end)
240                    *(out++) = (uint16) translate_colour(*(data++));
241    }
242    
243  /* little endian - conversion happens when colourmap is built */  /* little endian - conversion happens when colourmap is built */
244  static void  static void
245  translate24(uint8 * data, uint8 * out, uint8 * end)  translate8to24(uint8 * data, uint8 * out, uint8 * end)
246  {  {
247          uint32 value;          uint32 value;
248    
# Line 140  translate24(uint8 * data, uint8 * out, u Line 256  translate24(uint8 * data, uint8 * out, u
256  }  }
257    
258  static void  static void
259  translate32(uint8 * data, uint32 * out, uint32 * end)  translate16to24(uint16 * data, uint8 * out, uint8 * end)
260    {
261            uint32 value;
262    
263            while (out < end)
264            {
265                    value = translate_colour(*(data++));
266                    *(out++) = value;
267                    *(out++) = value >> 8;
268                    *(out++) = value >> 16;
269            }
270    }
271    
272    static void
273    translate8to32(uint8 * data, uint32 * out, uint32 * end)
274  {  {
275          while (out < end)          while (out < end)
276                  *(out++) = colmap[*(data++)];                  *(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 *  static uint8 *
287  translate_image(int width, int height, uint8 * data)  translate_image(int width, int height, uint8 * data)
288  {  {
# Line 153  translate_image(int width, int height, u Line 290  translate_image(int width, int height, u
290          uint8 *out = xmalloc(size);          uint8 *out = xmalloc(size);
291          uint8 *end = out + size;          uint8 *end = out + size;
292    
293            if (server_bpp == 16)
294            {
295                    if (bpp == 16)
296                            translate16to16((uint16 *) data, (uint16 *) out, (uint16 *) end);
297                    else if (bpp == 24)
298                            translate16to24((uint16 *) data, out, end); /* todo, check this one */
299                    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)          switch (bpp)
305          {          {
306                  case 8:                  case 8:
307                          translate8(data, out, end);                          translate8to8(data, out, end);
308                          break;                          break;
309    
310                  case 16:                  case 16:
311                          translate16(data, (uint16 *) out, (uint16 *) end);                          translate8to16(data, (uint16 *) out, (uint16 *) end);
312                          break;                          break;
313    
314                  case 24:                  case 24:
315                          translate24(data, out, end);                          translate8to24(data, out, end);
316                          break;                          break;
317    
318                  case 32:                  case 32:
319                          translate32(data, (uint32 *) out, (uint32 *) end);                          translate8to32(data, (uint32 *) out, (uint32 *) end);
320                          break;                          break;
321          }          }
322    
323          return out;          return out;
324  }  }
325    
326  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }  BOOL
327  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }  get_key_state(unsigned int state, uint32 keysym)
 #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \  
                         x = (x << 16) | (x >> 16); }  
   
 static uint32  
 translate_colour(uint32 colour)  
328  {  {
329          switch (bpp)          int modifierpos, key, keysymMask = 0;
330          {          int offset;
                 case 16:  
                         if (host_be != xserver_be)  
                                 BSWAP16(colour);  
                         break;  
331    
332                  case 24:          KeyCode keycode = XKeysymToKeycode(display, keysym);
                         if (xserver_be)  
                                 BSWAP24(colour);  
                         break;  
333    
334                  case 32:          if (keycode == NoSymbol)
335                          if (host_be != xserver_be)                  return False;
336                                  BSWAP32(colour);  
337                          break;          for (modifierpos = 0; modifierpos < 8; modifierpos++)
338            {
339                    offset = mod_map->max_keypermod * modifierpos;
340    
341                    for (key = 0; key < mod_map->max_keypermod; key++)
342                    {
343                            if (mod_map->modifiermap[offset + key] == keycode)
344                                    keysymMask |= 1 << modifierpos;
345                    }
346          }          }
347    
348          return colour;          return (state & keysymMask) ? True : False;
349  }  }
350    
351  BOOL  BOOL
352  ui_create_window(char *title)  ui_init(void)
353  {  {
         XSetWindowAttributes attribs;  
         XClassHint *classhints;  
         XSizeHints *sizehints;  
         unsigned long input_mask;  
354          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
         Screen *screen;  
355          uint16 test;          uint16 test;
356          int i;          int i;
357    
358          int xkb_minor, xkb_major;          display = XOpenDisplay(NULL);
         int xkb_event, xkb_error, xkb_reason;  
   
         /* compare compiletime libs with runtime libs. */  
         xkb_major = XkbMajorVersion;  
         xkb_minor = XkbMinorVersion;  
         if (XkbLibraryVersion(&xkb_major, &xkb_minor) == False)  
         {  
                 error("please re-compile rdesktop\ncompile time version of xkb is not compatible with\nyour runtime version of the library\n");  
                 return False;  
         }  
   
   
         display =  
                 XkbOpenDisplay(NULL, &xkb_event, &xkb_error, &xkb_major,  
                                &xkb_minor, &xkb_reason);  
         switch (xkb_reason)  
         {  
                 case XkbOD_BadLibraryVersion:  
                         error("XkbOD_BadLibraryVersion: XKB extensions in server and the library rdesktop is linked against aren't compatible with each other.\n");  
                         break;  
                 case XkbOD_ConnectionRefused:  
                         error("XkbOD_ConnectionRefused\n");  
                         break;  
                 case XkbOD_BadServerVersion:  
                         error("XkbOD_BadServerVersion\n");  
                         break;  
                 case XkbOD_NonXkbServer:  
                         error("XkbOD_NonXkbServer: XKB extension not present in server\nupdate your X server.\n");  
                         break;  
                 case XkbOD_Success:  
                         DEBUG("XkbOD_Success: Connection established with display\n");  
                         break;  
         }  
   
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 269  ui_create_window(char *title) Line 374  ui_create_window(char *title)
374                     desirable, e.g. 24 bits->32 bits. */                     desirable, e.g. 24 bits->32 bits. */
375                  while (i--)                  while (i--)
376                  {                  {
377                          if ((pfm[i].depth == depth)                          if ((pfm[i].depth == depth) && (pfm[i].bits_per_pixel > bpp))
                             && (pfm[i].bits_per_pixel > bpp))  
378                          {                          {
379                                  bpp = pfm[i].bits_per_pixel;                                  bpp = pfm[i].bits_per_pixel;
380                          }                          }
# Line 285  ui_create_window(char *title) Line 389  ui_create_window(char *title)
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) != Always)
402                    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          white = WhitePixelOfScreen(screen);          if ((width == 0) || (height == 0))
409          attribs.background_pixel = BlackPixelOfScreen(screen);          {
410          attribs.backing_store = DoesBackingStore(screen);                  /* Fetch geometry from _NET_WORKAREA */
411                    uint32 x, y, cx, cy;
412    
413          if (attribs.backing_store == NotUseful)                  if (get_current_workarea(&x, &y, &cx, &cy) == 0)
414                  ownbackstore = True;                  {
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          {          {
                 attribs.override_redirect = True;  
428                  width = WidthOfScreen(screen);                  width = WidthOfScreen(screen);
429                  height = HeightOfScreen(screen);                  height = HeightOfScreen(screen);
430          }          }
431          else  
432            /* make sure width is a multiple of 4 */
433            width = (width + 3) & ~3;
434    
435            if (ownbackstore)
436          {          {
437                  attribs.override_redirect = False;                  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          width = (width + 3) & ~3;       /* make width a multiple of 32 bits */          mod_map = XGetModifierMapping(display);
446    
447          wnd = XCreateWindow(display, RootWindowOfScreen(screen),          if (enable_compose)
448                              0, 0, width, height, 0, CopyFromParent,                  IM = XOpenIM(display, NULL, NULL, NULL);
449                              InputOutput, CopyFromParent,  
450                              CWBackingStore | CWBackPixel | CWOverrideRedirect,          xkeymap_init();
451                              &attribs);          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
471    ui_create_window(void)
472    {
473            XSetWindowAttributes attribs;
474            XClassHint *classhints;
475            XSizeHints *sizehints;
476            int wndwidth, wndheight;
477            long input_mask, ic_input_mask;
478            XEvent xevent;
479    
480            wndwidth = fullscreen ? WidthOfScreen(screen) : width;
481            wndheight = fullscreen ? HeightOfScreen(screen) : height;
482    
483            attribs.background_pixel = BlackPixelOfScreen(screen);
484            attribs.backing_store = ownbackstore ? NotUseful : Always;
485            attribs.override_redirect = fullscreen;
486    
487            wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight,
488                                0, CopyFromParent, InputOutput, CopyFromParent,
489                                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 340  ui_create_window(char *title) Line 511  ui_create_window(char *title)
511                  XFree(sizehints);                  XFree(sizehints);
512          }          }
513    
514          xkeymap_init();          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
515                    VisibilityChangeMask | FocusChangeMask;
516    
         input_mask = KeyPressMask | KeyReleaseMask |  
                 ButtonPressMask | ButtonReleaseMask |  
                 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 (fullscreen || grab_keyboard)
522                    input_mask |= EnterWindowMask;
523            if (grab_keyboard)
524                    input_mask |= LeaveWindowMask;
525    
526            if (IM != NULL)
527            {
528                    IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
529                                   XNClientWindow, wnd, XNFocusWindow, wnd, NULL);
530    
531                    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);          XSelectInput(display, wnd, input_mask);
         gc = XCreateGC(display, wnd, 0, NULL);  
   
         if (ownbackstore)  
                 backstore = XCreatePixmap(display, wnd, width, height, depth);  
   
537          XMapWindow(display, wnd);          XMapWindow(display, wnd);
538    
539          /* TODO: error texts... make them friendly. */          /* wait for VisibilityNotify */
540          xkb = XkbGetKeyboard(display, XkbAllComponentsMask, XkbUseCoreKbd);          do
         if ((int) xkb == BadAlloc || xkb == NULL)  
541          {          {
542                  error("XkbGetKeyboard failed.\n");                  XMaskEvent(display, VisibilityChangeMask, &xevent);
                 exit(0);  
543          }          }
544            while (xevent.type != VisibilityNotify);
545    
546          /* TODO: error texts... make them friendly. */          focused = False;
547          if (XkbSelectEvents          mouse_in_wnd = False;
             (display, xkb->device_spec, XkbAllEventsMask,  
              XkbAllEventsMask) == False)  
         {  
                 error("XkbSelectEvents failed.\n");  
                 exit(0);  
         }  
548    
549          xwin_get_numlock_mask();          /* handle the WM_DELETE_WINDOW protocol */
550            protocol_atom = XInternAtom(display, "WM_PROTOCOLS", True);
551            kill_atom = XInternAtom(display, "WM_DELETE_WINDOW", True);
552            XSetWMProtocols(display, wnd, &kill_atom, 1);
553    
554          return True;          return True;
555  }  }
556    
557  void  void
558  xwin_get_numlock_mask()  ui_destroy_window(void)
559  {  {
560          KeyCode numlockcode;          if (IC != NULL)
561          KeyCode *keycode;                  XDestroyIC(IC);
         XModifierKeymap *modmap;  
         int i, j;  
   
         /* Find out if numlock is already defined as a modifier key, and if so where */  
         numlockcode = XKeysymToKeycode(display, 0xFF7F);        /* XF_Num_Lock = 0xFF7F */  
         if (numlockcode)  
         {  
                 modmap = XGetModifierMapping(display);  
                 if (modmap)  
                 {  
                         keycode = modmap->modifiermap;  
                         for (i = 0; i < 8; i++)  
                                 for (j = modmap->max_keypermod; j--;)  
                                 {  
                                         if (*keycode == numlockcode)  
                                         {  
                                                 numlock_modifier_mask =  
                                                         (1 << i);  
                                                 i = 8;  
                                                 break;  
                                         }  
                                         keycode++;  
                                 }  
                         if (!numlock_modifier_mask)  
                         {  
                                 modmap->modifiermap[7 *  
                                                     modmap->max_keypermod] =  
                                         numlockcode;  
                                 if (XSetModifierMapping(display, modmap) ==  
                                     MappingSuccess)  
                                         numlock_modifier_mask = (1 << 7);  
                                 else  
                                         printf("XSetModifierMapping failed!\n");  
                         }  
                         XFreeModifiermap(modmap);  
                 }  
         }  
   
         if (!numlock_modifier_mask)  
                 printf("WARNING: Failed to get a numlock modifier mapping.\n");  
562    
563            XDestroyWindow(display, wnd);
564  }  }
565    
566  void  void
567  ui_destroy_window()  xwin_toggle_fullscreen(void)
568  {  {
569          if (xkb != NULL)          Pixmap contents = 0;
                 XkbFreeKeyboard(xkb, XkbAllControlsMask, True);  
570    
571          if (ownbackstore)          if (!ownbackstore)
572                  XFreePixmap(display, backstore);          {
573                    /* need to save contents of window */
574                    contents = XCreatePixmap(display, wnd, width, height, depth);
575                    XCopyArea(display, wnd, contents, gc, 0, 0, width, height, 0, 0);
576            }
577    
578          XFreeGC(display, gc);          ui_destroy_window();
579          XDestroyWindow(display, wnd);          fullscreen = !fullscreen;
580          XCloseDisplay(display);          ui_create_window();
581          display = NULL;  
582            XDefineCursor(display, wnd, current_cursor);
583    
584            if (!ownbackstore)
585            {
586                    XCopyArea(display, contents, wnd, gc, 0, 0, width, height, 0, 0);
587                    XFreePixmap(display, contents);
588            }
589  }  }
590    
591  static void  /* Process all events in Xlib queue
592  xwin_process_events()     Returns 0 after user quit, 1 otherwise */
593    static int
594    xwin_process_events(void)
595  {  {
596          XEvent xevent;          XEvent xevent;
   
597          KeySym keysym;          KeySym keysym;
         uint8 scancode;  
598          uint16 button, flags;          uint16 button, flags;
599          uint32 ev_time;          uint32 ev_time;
600          uint32 tmpmods;          key_translation tr;
601            char str[256];
602            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                  ev_time = time(NULL);                  XNextEvent(display, &xevent);
610    
611                    if ((IC != NULL) && (XFilterEvent(&xevent, None) == True))
612                    {
613                            DEBUG_KBD(("Filtering event\n"));
614                            continue;
615                    }
616    
617                  flags = 0;                  flags = 0;
618    
619                  switch (xevent.type)                  switch (xevent.type)
620                  {                  {
621                          case KeyRelease:                          case ClientMessage:
622                                  flags = KBD_FLAG_DOWN | KBD_FLAG_UP;                                  /* the window manager told us to quit */
623                                  /* fall through */                                  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 (XkbTranslateKeyCode                                  if (IC != NULL)
631                                      (xkb, xevent.xkey.keycode,                                          /* Multi_key compatible version */
632                                       xevent.xkey.state, &tmpmods,                                  {
633                                       &keysym) == False)                                          XmbLookupString(IC,
634                                                            (XKeyPressedEvent *) &
635                                                            xevent, str, sizeof(str), &keysym, &status);
636                                            if (!((status == XLookupKeySym) || (status == XLookupBoth)))
637                                            {
638                                                    error("XmbLookupString failed with status 0x%x\n",
639                                                          status);
640                                                    break;
641                                            }
642                                    }
643                                    else
644                                    {
645                                            /* Plain old XLookupString */
646                                            DEBUG_KBD(("\nNo input context, using XLookupString\n"));
647                                            XLookupString((XKeyEvent *) & xevent,
648                                                          str, sizeof(str), &keysym, NULL);
649                                    }
650    
651                                    DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym,
652                                               get_ksname(keysym)));
653    
654                                    ev_time = time(NULL);
655                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))
656                                          break;                                          break;
                                 scancode =  
                                         xkeymap_translate_key(keysym,  
                                                               xevent.xkey.  
                                                               keycode,  
                                                               &flags);  
657    
658                                  if (scancode == 0)                                  tr = xkeymap_translate_key(keysym,
659                                                               xevent.xkey.keycode, xevent.xkey.state);
660    
661                                    if (tr.scancode == 0)
662                                          break;                                          break;
663    
664                                  /* keep track of the modifiers -- needed for stickykeys... */                                  ensure_remote_modifiers(ev_time, tr);
665                                  if (xevent.type == KeyPress)  
666                                          xwin_mod_press(xevent.xkey.state,                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
667                                                         ev_time, scancode);                                  break;
668    
669                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE,                          case KeyRelease:
670                                                 flags, scancode, 0);                                  XLookupString((XKeyEvent *) & xevent, str,
671                                                  sizeof(str), &keysym, NULL);
672                                  if (xevent.type == KeyRelease)  
673                                          xwin_mod_release(xevent.xkey.state,                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
674                                                           ev_time, scancode);                                             get_ksname(keysym)));
675    
676                                    ev_time = time(NULL);
677                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False))
678                                            break;
679    
680                                    tr = xkeymap_translate_key(keysym,
681                                                               xevent.xkey.keycode, xevent.xkey.state);
682    
683                                    if (tr.scancode == 0)
684                                            break;
685    
686                                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);
687                                  break;                                  break;
688    
689                          case ButtonPress:                          case ButtonPress:
# Line 499  xwin_process_events() Line 691  xwin_process_events()
691                                  /* fall through */                                  /* fall through */
692    
693                          case ButtonRelease:                          case ButtonRelease:
694                                  button = xkeymap_translate_button(xevent.                                  button = xkeymap_translate_button(xevent.xbutton.button);
                                                                   xbutton.  
                                                                   button);  
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,                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);
                                                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                                                 MOUSE_FLAG_MOVE,                                          XSetInputFocus(display, wnd, RevertToPointerRoot,
705                                                 xevent.xmotion.x,                                                         CurrentTime);
706                                                 xevent.xmotion.y);                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
707                                                   MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
708                                  break;                                  break;
709    
710                          case EnterNotify:                          case FocusIn:
711                                  XGrabKeyboard(display, wnd, True,                                  if (xevent.xfocus.mode == NotifyGrab)
712                                                GrabModeAsync, GrabModeAsync,                                          break;
713                                                CurrentTime);                                  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,
719                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
720                                    break;
721    
722                                  xwin_mod_update(xevent.xcrossing.state,                          case FocusOut:
723                                                  ev_time);                                  if (xevent.xfocus.mode == NotifyUngrab)
724                                            break;
725                                    focused = False;
726                                    if (xevent.xfocus.mode == NotifyWhileGrabbed)
727                                            XUngrabKeyboard(display, CurrentTime);
728                                    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;                                  break;
744    
745                          case LeaveNotify:                          case LeaveNotify:
746                                    /* we only register for this event when grab_keyboard */
747                                    mouse_in_wnd = False;
748                                  XUngrabKeyboard(display, CurrentTime);                                  XUngrabKeyboard(display, CurrentTime);
749                                  break;                                  break;
750    
# Line 538  xwin_process_events() Line 755  xwin_process_events()
755                                            xevent.xexpose.height,                                            xevent.xexpose.height,
756                                            xevent.xexpose.x, xevent.xexpose.y);                                            xevent.xexpose.x, xevent.xexpose.y);
757                                  break;                                  break;
                 }  
         }  
 }  
   
 void  
 xwin_mod_update(uint32 state, uint32 ev_time)  
 {  
         xwin_mod_press(state, ev_time, 0);  
         xwin_mod_release(state, ev_time, 0);  
 }  
   
 void  
 xwin_mod_release(uint32 state, uint32 ev_time, uint32 scancode)  
 {  
         switch (scancode)  
         {  
                 case 0x2a:  
                         key_down_state &= ~DShift1Mask;  
                         break;  
                 case 0x36:  
                         key_down_state &= ~DShift2Mask;  
                         break;  
                 case 0x1d:  
                         key_down_state &= ~DControl1Mask;  
                         break;  
                 case 0x9d:  
                         key_down_state &= ~DControl2Mask;  
                         break;  
                 case 0x38:  
                         key_down_state &= ~DMod1Mask;  
                         break;  
                 case 0xb8:  
                         key_down_state &= ~DMod2Mask;  
                         break;  
         }  
   
         if (!(numlock_modifier_mask & state)  
             && (key_down_state & DNumLockMask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0, 0x45, 0);  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                KBD_FLAG_DOWN | KBD_FLAG_UP, 0x45, 0);  
                 key_down_state &= ~DNumLockMask;  
         }  
   
         if (!(LockMask & state) && (key_down_state & DLockMask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0, 0x3a, 0);  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                KBD_FLAG_DOWN | KBD_FLAG_UP, 0x3a, 0);  
                 key_down_state &= ~DLockMask;  
   
         }  
   
   
         if (!(ShiftMask & state) && (key_down_state & DShift1Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0x2a,  
                                0);  
                 key_down_state &= ~DShift1Mask;  
   
         }  
   
         if (!(ControlMask & state) && (key_down_state & DControl1Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0x1d,  
                                0);  
                 key_down_state &= ~DControl1Mask;  
   
         }  
   
         if (!(Mod1Mask & state) && (key_down_state & DMod1Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0x38,  
                                0);  
                 key_down_state &= ~DMod1Mask;  
   
         }  
   
         if (!(Mod2Mask & state) && (key_down_state & DMod2Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0xb8,  
                                0);  
                 key_down_state &= ~DMod2Mask;  
         }  
 }  
   
   
 void  
 xwin_mod_press(uint32 state, uint32 ev_time, uint32 scancode)  
 {  
   
         switch (scancode)  
         {  
                 case 0x2a:  
                         key_down_state |= DShift1Mask;  
                         break;  
                 case 0x36:  
                         key_down_state |= DShift2Mask;  
                         break;  
                 case 0x1d:  
                         key_down_state |= DControl1Mask;  
                         break;  
                 case 0x9d:  
                         key_down_state |= DControl2Mask;  
                         break;  
                 case 0x3a:  
                         key_down_state ^= DLockMask;  
                         break;  
                 case 0x45:  
                         key_down_state ^= DNumLockMask;  
                         break;  
                 case 0x38:  
                         key_down_state |= DMod1Mask;  
                         break;  
                 case 0xb8:  
                         key_down_state |= DMod2Mask;  
                         break;  
         }  
   
         if ((numlock_modifier_mask && state)  
             && !(key_down_state & DNumLockMask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0, 0x45, 0);  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                KBD_FLAG_DOWN | KBD_FLAG_UP, 0x45, 0);  
                 key_down_state |= DNumLockMask;  
         }  
   
         if ((LockMask & state) && !(key_down_state & DLockMask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0, 0x3a, 0);  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                KBD_FLAG_DOWN | KBD_FLAG_UP, 0x3a, 0);  
                 key_down_state |= DLockMask;  
   
         }  
   
758    
759          if ((ShiftMask & state)                          case MappingNotify:
760              && !((key_down_state & DShift1Mask)                                  /* Refresh keyboard mapping if it has changed. This is important for
761                   || (key_down_state & DShift2Mask)))                                     Xvnc, since it allocates keycodes dynamically */
762          {                                  if (xevent.xmapping.request == MappingKeyboard
763                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN,                                      || xevent.xmapping.request == MappingModifier)
764                                 0x2a, 0);                                          XRefreshKeyboardMapping(&xevent.xmapping);
                 key_down_state |= DShift1Mask;  
   
         }  
765    
766          if ((ControlMask & state)                                  if (xevent.xmapping.request == MappingModifier)
767              && !((key_down_state & DControl1Mask)                                  {
768                   || (key_down_state & DControl2Mask)))                                          XFreeModifiermap(mod_map);
769          {                                          mod_map = XGetModifierMapping(display);
770                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN,                                  }
771                                 0x1d, 0);                                  break;
                 key_down_state |= DControl1Mask;  
   
         }  
   
         if ((Mod1Mask & state) && !(key_down_state & DMod1Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN,  
                                0x38, 0);  
                 key_down_state |= DMod1Mask;  
   
         }  
   
         if ((Mod2Mask & state) && !(key_down_state & DMod2Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN,  
                                0xb8, 0);  
                 key_down_state |= DMod2Mask;  
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 724  ui_select(int rdp_socket) Line 787  ui_select(int rdp_socket)
787    
788          while (True)          while (True)
789          {          {
790                    /* Process any events already waiting */
791                    if (!xwin_process_events())
792                            /* User quit */
793                            return 0;
794    
795                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
796                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
797                  if (display != NULL)                  FD_SET(x_socket, &rfds);
                 {  
                         FD_SET(x_socket, &rfds);  
                         XFlush(display);  
                 }  
798    
799                  switch (select(n, &rfds, NULL, NULL, NULL))                  switch (select(n, &rfds, NULL, NULL, NULL))
800                  {                  {
# Line 741  ui_select(int rdp_socket) Line 805  ui_select(int rdp_socket)
805                                  continue;                                  continue;
806                  }                  }
807    
                 if (FD_ISSET(x_socket, &rfds))  
                         xwin_process_events();  
   
808                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
809                          return;                          return 1;
810          }          }
811  }  }
812    
# Line 764  ui_create_bitmap(int width, int height, Line 825  ui_create_bitmap(int width, int height,
825    
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,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
829                               0, 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 776  ui_create_bitmap(int width, int height, Line 837  ui_create_bitmap(int width, int height,
837  }  }
838    
839  void  void
840  ui_paint_bitmap(int x, int y, int cx, int cy,  ui_paint_bitmap(int x, int y, int cx, int cy, int width, int height, uint8 * data)
                 int width, int height, uint8 * data)  
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,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
846                               0, tdata, width, height, 8, 0);                               (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0);
847    
848          if (ownbackstore)          if (ownbackstore)
849          {          {
# Line 820  ui_create_glyph(int width, int height, u Line 879  ui_create_glyph(int width, int height, u
879          bitmap = XCreatePixmap(display, wnd, width, height, 1);          bitmap = XCreatePixmap(display, wnd, width, height, 1);
880          gc = XCreateGC(display, bitmap, 0, NULL);          gc = XCreateGC(display, bitmap, 0, NULL);
881    
882          image = XCreateImage(display, visual, 1, ZPixmap, 0,          image = XCreateImage(display, visual, 1, ZPixmap, 0, (char *) data,
883                               data, width, height, 8, scanline);                               width, height, 8, scanline);
884          image->byte_order = MSBFirst;          image->byte_order = MSBFirst;
885          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
886          XInitImage(image);          XInitImage(image);
# Line 840  ui_destroy_glyph(HGLYPH glyph) Line 899  ui_destroy_glyph(HGLYPH glyph)
899  }  }
900    
901  HCURSOR  HCURSOR
902  ui_create_cursor(unsigned int x, unsigned int y, int width,  ui_create_cursor(unsigned int x, unsigned int y, int width, int height,
903                   int height, uint8 * andmask, uint8 * xormask)                   uint8 * andmask, uint8 * xormask)
904  {  {
905          HGLYPH maskglyph, cursorglyph;          HGLYPH maskglyph, cursorglyph;
906          XColor bg, fg;          XColor bg, fg;
# Line 899  ui_create_cursor(unsigned int x, unsigne Line 958  ui_create_cursor(unsigned int x, unsigne
958          cursorglyph = ui_create_glyph(width, height, cursor);          cursorglyph = ui_create_glyph(width, height, cursor);
959          maskglyph = ui_create_glyph(width, height, mask);          maskglyph = ui_create_glyph(width, height, mask);
960    
961          xcursor = XCreatePixmapCursor(display, (Pixmap) cursorglyph,          xcursor =
962                                        (Pixmap) maskglyph, &fg, &bg, x, y);                  XCreatePixmapCursor(display, (Pixmap) cursorglyph,
963                                        (Pixmap) maskglyph, &fg, &bg, x, y);
964    
965          ui_destroy_glyph(maskglyph);          ui_destroy_glyph(maskglyph);
966          ui_destroy_glyph(cursorglyph);          ui_destroy_glyph(cursorglyph);
# Line 912  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 927  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 953  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 = white;  
   
                         /* 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 1008  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 1020  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 1060  ui_patblt(uint8 opcode, Line 1174  ui_patblt(uint8 opcode,
1174                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(fgcolour);
1175                          XSetFillStyle(display, gc, FillOpaqueStippled);                          XSetFillStyle(display, gc, FillOpaqueStippled);
1176                          XSetStipple(display, gc, fill);                          XSetStipple(display, gc, fill);
1177                          XSetTSOrigin(display, gc, brush->xorigin,                          XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin);
                                      brush->yorigin);  
1178    
1179                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
1180    
1181                          XSetFillStyle(display, gc, FillSolid);                          XSetFillStyle(display, gc, FillSolid);
1182                            XSetTSOrigin(display, gc, 0, 0);
1183                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
1184                          break;                          break;
1185    
# Line 1084  ui_screenblt(uint8 opcode, Line 1198  ui_screenblt(uint8 opcode,
1198          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1199          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);
1200          if (ownbackstore)          if (ownbackstore)
1201                  XCopyArea(display, backstore, backstore, gc, srcx, srcy,                  XCopyArea(display, backstore, backstore, gc, srcx, srcy, cx, cy, x, y);
                           cx, cy, x, y);  
1202          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1203  }  }
1204    
# Line 1097  ui_memblt(uint8 opcode, Line 1210  ui_memblt(uint8 opcode,
1210          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1211          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);
1212          if (ownbackstore)          if (ownbackstore)
1213                  XCopyArea(display, (Pixmap) src, backstore, gc, srcx, srcy,                  XCopyArea(display, (Pixmap) src, backstore, gc, srcx, srcy, cx, cy, x, y);
                           cx, cy, x, y);  
1214          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1215  }  }
1216    
# Line 1115  ui_triblt(uint8 opcode, Line 1227  ui_triblt(uint8 opcode,
1227          {          {
1228                  case 0x69:      /* PDSxxn */                  case 0x69:      /* PDSxxn */
1229                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);
1230                          ui_patblt(ROP2_NXOR, x, y, cx, cy,                          ui_patblt(ROP2_NXOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   brush, bgcolour, fgcolour);  
1231                          break;                          break;
1232    
1233                  case 0xb8:      /* PSDPxax */                  case 0xb8:      /* PSDPxax */
1234                          ui_patblt(ROP2_XOR, x, y, cx, cy,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   brush, bgcolour, fgcolour);  
1235                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);
1236                          ui_patblt(ROP2_XOR, x, y, cx, cy,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   brush, bgcolour, fgcolour);  
1237                          break;                          break;
1238    
1239                  case 0xc0:      /* PSa */                  case 0xc0:      /* PSa */
1240                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
1241                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1242                          break;                          break;
1243    
1244                  default:                  default:
# Line 1161  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,
1276                /* src */ HGLYPH glyph, int srcx, int srcy, int bgcolour,                /* src */ HGLYPH glyph, int srcx, int srcy,
1277                int fgcolour)                int bgcolour, int fgcolour)
1278  {  {
1279          SET_FOREGROUND(fgcolour);          SET_FOREGROUND(fgcolour);
1280          SET_BACKGROUND(bgcolour);          SET_BACKGROUND(bgcolour);
1281    
1282          XSetFillStyle(display, gc, (mixmode == MIX_TRANSPARENT)          XSetFillStyle(display, gc,
1283                        ? FillStippled : FillOpaqueStippled);                        (mixmode == MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);
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 1189  ui_draw_glyph(int mixmode, Line 1298  ui_draw_glyph(int mixmode,
1298        if ((xyoffset & 0x80))\        if ((xyoffset & 0x80))\
1299          {\          {\
1300            if (flags & TEXT2_VERTICAL) \            if (flags & TEXT2_VERTICAL) \
1301              y += ttext[++idx] | (ttext[++idx] << 8);\              y += ttext[idx+1] | (ttext[idx+2] << 8);\
1302            else\            else\
1303              x += ttext[++idx] | (ttext[++idx] << 8);\              x += ttext[idx+1] | (ttext[idx+2] << 8);\
1304              idx += 2;\
1305          }\          }\
1306        else\        else\
1307          {\          {\
# Line 1203  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 1214  ui_draw_glyph(int mixmode, Line 1324  ui_draw_glyph(int mixmode,
1324    
1325  void  void
1326  ui_draw_text(uint8 font, uint8 flags, int mixmode, int x, int y,  ui_draw_text(uint8 font, uint8 flags, int mixmode, int x, int y,
1327               int clipx, int clipy, int clipcx, int clipcy, int boxx,               int clipx, int clipy, int clipcx, int clipcy,
1328               int boxy, int boxcx, int boxcy, int bgcolour,               int boxx, int boxy, int boxcx, int boxcy, int bgcolour,
1329               int fgcolour, uint8 * text, uint8 length)               int fgcolour, uint8 * text, uint8 length)
1330  {  {
1331          FONTGLYPH *glyph;          FONTGLYPH *glyph;
# Line 1226  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 1240  ui_draw_text(uint8 font, uint8 flags, in Line 1350  ui_draw_text(uint8 font, uint8 flags, in
1350                  {                  {
1351                          case 0xff:                          case 0xff:
1352                                  if (i + 2 < length)                                  if (i + 2 < length)
1353                                          cache_put_text(text[i + 1], text,                                          cache_put_text(text[i + 1], text, text[i + 2]);
                                                        text[i + 2]);  
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 1258  ui_draw_text(uint8 font, uint8 flags, in Line 1367  ui_draw_text(uint8 font, uint8 flags, in
1367                                  if (entry != NULL)                                  if (entry != NULL)
1368                                  {                                  {
1369                                          if ((((uint8 *) (entry->data))[1] ==                                          if ((((uint8 *) (entry->data))[1] ==
1370                                               0)                                               0) && (!(flags & TEXT2_IMPLICIT_X)))
                                             && (!(flags & TEXT2_IMPLICIT_X)))  
1371                                          {                                          {
1372                                                  if (flags & TEXT2_VERTICAL)                                                  if (flags & TEXT2_VERTICAL)
1373                                                          y += text[i + 2];                                                          y += text[i + 2];
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->                                                  DO_GLYPH(((uint8 *) (entry->data)), j);
                                                                      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 1287  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
# Line 1299  ui_desktop_save(uint32 offset, int x, in Line 1412  ui_desktop_save(uint32 offset, int x, in
1412    
1413          if (ownbackstore)          if (ownbackstore)
1414          {          {
1415                  image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes,                  image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes, ZPixmap);
                                   ZPixmap);  
1416          }          }
1417          else          else
1418          {          {
1419                  pix = XCreatePixmap(display, wnd, cx, cy, depth);                  pix = XCreatePixmap(display, wnd, cx, cy, depth);
1420                  XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);                  XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);
1421                  image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes,                  image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);
                                   ZPixmap);  
1422                  XFreePixmap(display, pix);                  XFreePixmap(display, pix);
1423          }          }
1424    
1425          offset *= bpp / 8;          offset *= bpp / 8;
1426          cache_put_desktop(offset, cx, cy, image->bytes_per_line,          cache_put_desktop(offset, cx, cy, image->bytes_per_line, bpp / 8, (uint8 *) image->data);
                           bpp / 8, (uint8 *) image->data);  
1427    
1428          XDestroyImage(image);          XDestroyImage(image);
1429  }  }
# Line 1329  ui_desktop_restore(uint32 offset, int x, Line 1439  ui_desktop_restore(uint32 offset, int x,
1439          if (data == NULL)          if (data == NULL)
1440                  return;                  return;
1441    
1442          image = XCreateImage(display, visual, depth, ZPixmap,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
1443                               0, data, cx, cy, BitmapPad(display),                               (char *) data, cx, cy, BitmapPad(display), cx * bpp / 8);
                              cx * bpp / 8);  
1444    
1445          if (ownbackstore)          if (ownbackstore)
1446          {          {

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

  ViewVC Help
Powered by ViewVC 1.1.26