/[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 70 by astrand, Sat Jul 27 23:09:32 2002 UTC revision 376 by jsorg71, Mon May 19 21:36: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"
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;
32    extern BOOL hide_decorations;
33    extern char title[];
34    extern int server_bpp;
35    extern int win_button_size;
36    BOOL enable_compose = False;
37    BOOL focused;
38    BOOL mouse_in_wnd;
39    
40  Display *display = NULL;  Display *display;
41  static int x_socket;  static int x_socket;
42    static Screen *screen;
43  static Window wnd;  static Window wnd;
44  static GC gc;  static GC gc;
45  static Visual *visual;  static Visual *visual;
46  static int depth;  static int depth;
47  static int bpp;  static int bpp;
48    static XIM IM;
49    static XIC IC;
50    static XModifierKeymap *mod_map;
51    static Cursor current_cursor;
52    static Atom protocol_atom, kill_atom;
53    
54  /* endianness */  /* endianness */
55  static BOOL host_be;  static BOOL host_be;
# Line 47  static BOOL xserver_be; Line 59  static BOOL xserver_be;
59  static BOOL ownbackstore;  static BOOL ownbackstore;
60  static Pixmap backstore;  static Pixmap backstore;
61    
62    /* Moving in single app mode */
63    static BOOL moving_wnd;
64    static int move_x_offset = 0;
65    static int move_y_offset = 0;
66    
67    /* MWM decorations */
68    #define MWM_HINTS_DECORATIONS   (1L << 1)
69    #define PROP_MOTIF_WM_HINTS_ELEMENTS    5
70    typedef struct
71    {
72            uint32 flags;
73            uint32 functions;
74            uint32 decorations;
75            sint32 inputMode;
76            uint32 status;
77    }
78    PropMotifWmHints;
79    
80    typedef struct
81    {
82            uint32 red;
83            uint32 green;
84            uint32 blue;
85    }
86    PixelColour;
87    
88  #define FILL_RECTANGLE(x,y,cx,cy)\  #define FILL_RECTANGLE(x,y,cx,cy)\
89  { \  { \
90          XFillRectangle(display, wnd, gc, x, y, cx, cy); \          XFillRectangle(display, wnd, gc, x, y, cx, cy); \
# Line 54  static Pixmap backstore; Line 92  static Pixmap backstore;
92                  XFillRectangle(display, backstore, gc, x, y, cx, cy); \                  XFillRectangle(display, backstore, gc, x, y, cx, cy); \
93  }  }
94    
95    #define FILL_RECTANGLE_BACKSTORE(x,y,cx,cy)\
96    { \
97            XFillRectangle(display, ownbackstore ? backstore : wnd, gc, x, y, cx, cy); \
98    }
99    
100  /* colour maps */  /* colour maps */
101  static BOOL owncolmap;  BOOL owncolmap = False;
102  static Colormap xcolmap;  static Colormap xcolmap;
 static uint32 white;  
103  static uint32 *colmap;  static uint32 *colmap;
 static XIM IM = NULL;  
 static XIC IC = NULL;  
   
 /* Compose support */  
 BOOL enable_compose = False;  
104    
105  #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )  #define TRANSLATE(col)          ( server_bpp != 8 ? translate_colour(col) : owncolmap ? col : translate_colour(colmap[col]) )
106  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));
107  #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));  #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));
108    
# Line 92  static int rop2_map[] = { Line 129  static int rop2_map[] = {
129  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }
130    
131  static void  static void
132  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)  
133  {  {
134          while (out < end)          PropMotifWmHints motif_hints;
135                  *(out++) = (uint16) colmap[*(data++)];          Atom hintsatom;
 }  
136    
137  /* little endian - conversion happens when colourmap is built */          /* setup the property */
138  static void          motif_hints.flags = MWM_HINTS_DECORATIONS;
139  translate24(uint8 * data, uint8 * out, uint8 * end)          motif_hints.decorations = 0;
 {  
         uint32 value;  
140    
141          while (out < end)          /* get the atom for the property */
142            hintsatom = XInternAtom(display, "_MOTIF_WM_HINTS", False);
143            if (!hintsatom)
144          {          {
145                  value = colmap[*(data++)];                  warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n");
146                  *(out++) = value;                  return;
                 *(out++) = value >> 8;  
                 *(out++) = value >> 16;  
147          }          }
148    
149            XChangeProperty(display, wnd, hintsatom, hintsatom, 32, PropModeReplace,
150                            (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
151  }  }
152    
153  static void  static PixelColour
154  translate32(uint8 * data, uint32 * out, uint32 * end)  split_colour15(uint32 colour)
155  {  {
156          while (out < end)          PixelColour rv;
157                  *(out++) = colmap[*(data++)];          rv.red = (colour & 0x7c00) >> 10;
158            rv.red = (rv.red * 0xff) / 0x1f;
159            rv.green = (colour & 0x03e0) >> 5;
160            rv.green = (rv.green * 0xff) / 0x1f;
161            rv.blue = (colour & 0x1f);
162            rv.blue = (rv.blue * 0xff) / 0x1f;
163            return rv;
164    }
165    
166    static PixelColour
167    split_colour16(uint32 colour)
168    {
169            PixelColour rv;
170            rv.red = (colour & 0xf800) >> 11;
171            rv.red = (rv.red * 0xff) / 0x1f;
172            rv.green = (colour & 0x07e0) >> 5;
173            rv.green = (rv.green * 0xff) / 0x3f;
174            rv.blue = (colour & 0x001f);
175            rv.blue = (rv.blue * 0xff) / 0x1f;
176            return rv;
177    }
178    
179    static PixelColour
180    split_colour24(uint32 colour)
181    {
182            PixelColour rv;
183            rv.blue = (colour & 0xff0000) >> 16;
184            rv.green = (colour & 0xff00) >> 8;
185            rv.red = (colour & 0xff);
186            return rv;
187  }  }
188    
189  static uint8 *  static uint32
190  translate_image(int width, int height, uint8 * data)  make_colour16(PixelColour pc)
191  {  {
192          int size = width * height * bpp / 8;          pc.red = (pc.red * 0x1f) / 0xff;
193          uint8 *out = xmalloc(size);          pc.green = (pc.green * 0x3f) / 0xff;
194          uint8 *end = out + size;          pc.blue = (pc.blue * 0x1f) / 0xff;
195            return (pc.red << 11) | (pc.green << 5) | pc.blue;
196          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;  
197    
198                  case 32:  static uint32
199                          translate32(data, (uint32 *) out, (uint32 *) end);  make_colour24(PixelColour pc)
200                          break;  {
201          }          return (pc.red << 16) | (pc.green << 8) | pc.blue;
202    }
203    
204          return out;  static uint32
205    make_colour32(PixelColour pc)
206    {
207            return (pc.red << 16) | (pc.green << 8) | pc.blue;
208  }  }
209    
210  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
# Line 164  translate_image(int width, int height, u Line 215  translate_image(int width, int height, u
215  static uint32  static uint32
216  translate_colour(uint32 colour)  translate_colour(uint32 colour)
217  {  {
218            switch (server_bpp)
219            {
220                    case 15:
221                            switch (bpp)
222                            {
223                                    case 16:
224                                            colour = make_colour16(split_colour15(colour));
225                                            break;
226                                    case 24:
227                                            colour = make_colour24(split_colour15(colour));
228                                            break;
229                                    case 32:
230                                            colour = make_colour32(split_colour15(colour));
231                                            break;
232                            }
233                            break;
234                    case 16:
235                            switch (bpp)
236                            {
237                                    case 16:
238                                            break;
239                                    case 24:
240                                            colour = make_colour24(split_colour16(colour));
241                                            break;
242                                    case 32:
243                                            colour = make_colour32(split_colour16(colour));
244                                            break;
245                            }
246                            break;
247                    case 24:
248                            switch (bpp)
249                            {
250                                    case 16:
251                                            colour = make_colour16(split_colour24(colour));
252                                            break;
253                                    case 24:
254                                            break;
255                                    case 32:
256                                            colour = make_colour32(split_colour24(colour));
257                                            break;
258                            }
259                            break;
260            }
261          switch (bpp)          switch (bpp)
262          {          {
263                  case 16:                  case 16:
# Line 185  translate_colour(uint32 colour) Line 279  translate_colour(uint32 colour)
279          return colour;          return colour;
280  }  }
281    
282  static unsigned long  static void
283  init_inputmethod(void)  translate8to8(uint8 * data, uint8 * out, uint8 * end)
284  {  {
285          unsigned long filtered_events = 0;          while (out < end)
286                    *(out++) = (uint8) colmap[*(data++)];
287    }
288    
289          IM = XOpenIM(display, NULL, NULL, NULL);  static void
290          if (IM == NULL)  translate8to16(uint8 * data, uint16 * out, uint16 * end)
291    {
292            while (out < end)
293                    *(out++) = (uint16) colmap[*(data++)];
294    }
295    
296    /* little endian - conversion happens when colourmap is built */
297    static void
298    translate8to24(uint8 * data, uint8 * out, uint8 * end)
299    {
300            uint32 value;
301    
302            while (out < end)
303          {          {
304                  error("Failed to open input method\n");                  value = colmap[*(data++)];
305                    *(out++) = value;
306                    *(out++) = value >> 8;
307                    *(out++) = value >> 16;
308          }          }
309    }
310    
311          if (IM != NULL)  static void
312    translate8to32(uint8 * data, uint32 * out, uint32 * end)
313    {
314            while (out < end)
315                    *(out++) = colmap[*(data++)];
316    }
317    
318    /* todo the remaining translate function might need some big endian check ?? */
319    
320    static void
321    translate15to16(uint16 * data, uint16 * out, uint16 * end)
322    {
323            while (out < end)
324                    *(out++) = (uint16) make_colour16(split_colour15(*(data++)));
325    }
326    
327    static void
328    translate15to24(uint16 * data, uint8 * out, uint8 * end)
329    {
330            uint32 value;
331    
332            while (out < end)
333          {          {
334                  /* Must be done after XCreateWindow */                  value = make_colour24(split_colour15(*(data++)));
335                  IC = XCreateIC(IM, XNInputStyle,                  *(out++) = value;
336                                 (XIMPreeditNothing | XIMStatusNothing),                  *(out++) = value >> 8;
337                                 XNClientWindow, wnd, XNFocusWindow, wnd, NULL);                  *(out++) = value >> 16;
338            }
339    }
340    
341                  if (IC == NULL)  static void
342                  {  translate15to32(uint16 * data, uint32 * out, uint32 * end)
343                          error("Failed to create input context\n");  {
344                          XCloseIM(IM);          while (out < end)
345                          IM = NULL;                  *(out++) = make_colour32(split_colour15(*(data++)));
346                  }  }
347    
348    static void
349    translate16to16(uint16 * data, uint16 * out, uint16 * end)
350    {
351            while (out < end)
352                    *(out++) = (uint16) (*(data++));
353    }
354    
355    
356    static void
357    translate16to24(uint16 * data, uint8 * out, uint8 * end)
358    {
359            uint32 value;
360    
361            while (out < end)
362            {
363                    value = make_colour24(split_colour16(*(data++)));
364                    *(out++) = value;
365                    *(out++) = value >> 8;
366                    *(out++) = value >> 16;
367          }          }
368    }
369    
370          /* For correct Multi_key/Compose processing, I guess.  static void
371             It seems to work alright anyway, though. */  translate16to32(uint16 * data, uint32 * out, uint32 * end)
372          if (IC != NULL)  {
373            while (out < end)
374                    *(out++) = make_colour32(split_colour16(*(data++)));
375    }
376    
377    static void
378    translate24to16(uint8 * data, uint16 * out, uint16 * end)
379    {
380            uint32 pixel = 0;
381            while (out < end)
382          {          {
383                  if (XGetICValues(IC, XNFilterEvents, &filtered_events, NULL)                  pixel = *(data++) << 16;
384                      != NULL)                  pixel |= *(data++) << 8;
385                  {                  pixel |= *(data++);
386                          error("Failed to obtain XNFilterEvents value from IC\n");                  *(out++) = (uint16) make_colour16(split_colour24(pixel));
                         filtered_events = 0;  
                 }  
387          }          }
         return filtered_events;  
388  }  }
389    
390  static void  static void
391  close_inputmethod(void)  translate24to24(uint8 * data, uint8 * out, uint8 * end)
392  {  {
393          if (IC != NULL)          while (out < end)
394          {          {
395                  XDestroyIC(IC);                  *(out++) = (*(data++));
396                  if (IM != NULL)          }
397    }
398    
399    static void
400    translate24to32(uint8 * data, uint32 * out, uint32 * end)
401    {
402            uint32 pixel = 0;
403            while (out < end)
404            {
405                    pixel = *(data++);
406                    pixel |= *(data++) << 8;
407                    pixel |= *(data++) << 16;
408                    *(out++) = pixel;
409            }
410    }
411    
412    static uint8 *
413    translate_image(int width, int height, uint8 * data)
414    {
415            int size = width * height * bpp / 8;
416            uint8 *out = (uint8*)xmalloc(size);
417            uint8 *end = out + size;
418    
419            switch (server_bpp)
420            {
421                    case 24:
422                            switch (bpp)
423                            {
424                                    case 32:
425                                            translate24to32(data, (uint32 *) out, (uint32 *) end);
426                                            break;
427                                    case 24:
428                                            translate24to24(data, out, end);
429                                            break;
430                                    case 16:
431                                            translate24to16(data, (uint16 *) out, (uint16 *) end);
432                                            break;
433                            }
434                            break;
435                    case 16:
436                            switch (bpp)
437                            {
438                                    case 32:
439                                            translate16to32((uint16 *) data, (uint32 *) out,
440                                                            (uint32 *) end);
441                                            break;
442                                    case 24:
443                                            translate16to24((uint16 *) data, out, end);
444                                            break;
445                                    case 16:
446                                            translate16to16((uint16 *) data, (uint16 *) out,
447                                                            (uint16 *) end);
448                                            break;
449                            }
450                            break;
451                    case 15:
452                            switch (bpp)
453                            {
454                                    case 32:
455                                            translate15to32((uint16 *) data, (uint32 *) out,
456                                                            (uint32 *) end);
457                                            break;
458                                    case 24:
459                                            translate15to24((uint16 *) data, out, end);
460                                            break;
461                                    case 16:
462                                            translate15to16((uint16 *) data, (uint16 *) out,
463                                                            (uint16 *) end);
464                                            break;
465                            }
466                            break;
467                    case 8:
468                            switch (bpp)
469                            {
470                                    case 8:
471                                            translate8to8(data, out, end);
472                                            break;
473                                    case 16:
474                                            translate8to16(data, (uint16 *) out, (uint16 *) end);
475                                            break;
476                                    case 24:
477                                            translate8to24(data, out, end);
478                                            break;
479                                    case 32:
480                                            translate8to32(data, (uint32 *) out, (uint32 *) end);
481                                            break;
482                            }
483                            break;
484            }
485            return out;
486    }
487    
488    BOOL
489    get_key_state(unsigned int state, uint32 keysym)
490    {
491            int modifierpos, key, keysymMask = 0;
492            int offset;
493    
494            KeyCode keycode = XKeysymToKeycode(display, keysym);
495    
496            if (keycode == NoSymbol)
497                    return False;
498    
499            for (modifierpos = 0; modifierpos < 8; modifierpos++)
500            {
501                    offset = mod_map->max_keypermod * modifierpos;
502    
503                    for (key = 0; key < mod_map->max_keypermod; key++)
504                  {                  {
505                          XCloseIM(IM);                          if (mod_map->modifiermap[offset + key] == keycode)
506                          IM = NULL;                                  keysymMask |= 1 << modifierpos;
507                  }                  }
508          }          }
 }  
509    
510            return (state & keysymMask) ? True : False;
511    }
512    
513  BOOL  BOOL
514  ui_create_window(char *title)  ui_init(void)
515  {  {
         XSetWindowAttributes attribs;  
         XClassHint *classhints;  
         XSizeHints *sizehints;  
         unsigned long input_mask;  
516          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
         Screen *screen;  
517          uint16 test;          uint16 test;
518          int i;          int i;
519    
520          display = XOpenDisplay(NULL);          display = XOpenDisplay(NULL);
   
521          if (display == NULL)          if (display == NULL)
522          {          {
523                  error("Failed to open display\n");                  error("Failed to open display: %s\n", XDisplayName(NULL));
524                  return False;                  return False;
525          }          }
526    
# Line 272  ui_create_window(char *title) Line 536  ui_create_window(char *title)
536                     desirable, e.g. 24 bits->32 bits. */                     desirable, e.g. 24 bits->32 bits. */
537                  while (i--)                  while (i--)
538                  {                  {
539                          if ((pfm[i].depth == depth)                          if ((pfm[i].depth == depth) && (pfm[i].bits_per_pixel > bpp))
                             && (pfm[i].bits_per_pixel > bpp))  
540                          {                          {
541                                  bpp = pfm[i].bits_per_pixel;                                  bpp = pfm[i].bits_per_pixel;
542                          }                          }
# Line 288  ui_create_window(char *title) Line 551  ui_create_window(char *title)
551                  return False;                  return False;
552          }          }
553    
554          if (depth <= 8)          if (owncolmap != True)
555                  owncolmap = True;          {
         else  
556                  xcolmap = DefaultColormapOfScreen(screen);                  xcolmap = DefaultColormapOfScreen(screen);
557                    if (depth <= 8)
558                            warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n");
559            }
560    
561            gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL);
562    
563            if (DoesBackingStore(screen) != Always)
564                    ownbackstore = True;
565    
566          test = 1;          test = 1;
567          host_be = !(BOOL) (*(uint8 *) (&test));          host_be = !(BOOL) (*(uint8 *) (&test));
568          xserver_be = (ImageByteOrder(display) == MSBFirst);          xserver_be = (ImageByteOrder(display) == MSBFirst);
569    
570          white = WhitePixelOfScreen(screen);          if ((width == 0) || (height == 0))
571          attribs.background_pixel = BlackPixelOfScreen(screen);          {
572          attribs.backing_store = DoesBackingStore(screen);                  /* Fetch geometry from _NET_WORKAREA */
573                    uint32 x, y, cx, cy;
574    
575          if (attribs.backing_store == NotUseful)                  if (get_current_workarea(&x, &y, &cx, &cy) == 0)
576                  ownbackstore = True;                  {
577                            width = cx;
578                            height = cy;
579                    }
580                    else
581                    {
582                            warning("Failed to get workarea: probably your window manager does not support extended hints\n");
583                            width = 800;
584                            height = 600;
585                    }
586            }
587    
588          if (fullscreen)          if (fullscreen)
589          {          {
                 attribs.override_redirect = True;  
590                  width = WidthOfScreen(screen);                  width = WidthOfScreen(screen);
591                  height = HeightOfScreen(screen);                  height = HeightOfScreen(screen);
592          }          }
593          else  
594            /* make sure width is a multiple of 4 */
595            width = (width + 3) & ~3;
596    
597            if (ownbackstore)
598          {          {
599                  attribs.override_redirect = False;                  backstore =
600                            XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth);
601    
602                    /* clear to prevent rubbish being exposed at startup */
603                    XSetForeground(display, gc, BlackPixelOfScreen(screen));
604                    XFillRectangle(display, backstore, gc, 0, 0, width, height);
605          }          }
606    
607          width = (width + 3) & ~3;       /* make width a multiple of 32 bits */          mod_map = XGetModifierMapping(display);
608    
609            if (enable_compose)
610                    IM = XOpenIM(display, NULL, NULL, NULL);
611    
612            xkeymap_init();
613    
614            /* todo take this out when high colour is done */
615            printf("server bpp %d client bpp %d depth %d\n", server_bpp, bpp, depth);
616    
617            return True;
618    }
619    
620    void
621    ui_deinit(void)
622    {
623            if (IM != NULL)
624                    XCloseIM(IM);
625    
626            XFreeModifiermap(mod_map);
627    
628            if (ownbackstore)
629                    XFreePixmap(display, backstore);
630    
631            XFreeGC(display, gc);
632            XCloseDisplay(display);
633            display = NULL;
634    }
635    
636    BOOL
637    ui_create_window(void)
638    {
639            XSetWindowAttributes attribs;
640            XClassHint *classhints;
641            XSizeHints *sizehints;
642            int wndwidth, wndheight;
643            long input_mask, ic_input_mask;
644            XEvent xevent;
645    
646            wndwidth = fullscreen ? WidthOfScreen(screen) : width;
647            wndheight = fullscreen ? HeightOfScreen(screen) : height;
648    
649          wnd = XCreateWindow(display, RootWindowOfScreen(screen),          attribs.background_pixel = BlackPixelOfScreen(screen);
650                              0, 0, width, height, 0, CopyFromParent,          attribs.backing_store = ownbackstore ? NotUseful : Always;
651                              InputOutput, CopyFromParent,          attribs.override_redirect = fullscreen;
652                              CWBackingStore | CWBackPixel | CWOverrideRedirect,  
653                              &attribs);          wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight,
654                                0, CopyFromParent, InputOutput, CopyFromParent,
655                                CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
656    
657          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
658    
659            if (hide_decorations)
660                    mwm_hide_decorations();
661    
662          classhints = XAllocClassHint();          classhints = XAllocClassHint();
663          if (classhints != NULL)          if (classhints != NULL)
664          {          {
# Line 343  ui_create_window(char *title) Line 677  ui_create_window(char *title)
677                  XFree(sizehints);                  XFree(sizehints);
678          }          }
679    
680          xkeymap_init2();          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
681                    VisibilityChangeMask | FocusChangeMask;
682    
         input_mask =  
                 KeyPressMask | KeyReleaseMask | ButtonPressMask |  
                 ButtonReleaseMask | EnterWindowMask | LeaveWindowMask;  
683          if (sendmotion)          if (sendmotion)
684                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
   
685          if (ownbackstore)          if (ownbackstore)
686                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
687            if (fullscreen || grab_keyboard)
688                    input_mask |= EnterWindowMask;
689            if (grab_keyboard)
690                    input_mask |= LeaveWindowMask;
691    
692          if (enable_compose)          if (IM != NULL)
693                  input_mask |= init_inputmethod();          {
694                    IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
695                                   XNClientWindow, wnd, XNFocusWindow, wnd, NULL);
696    
697                    if ((IC != NULL)
698                        && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))
699                            input_mask |= ic_input_mask;
700            }
701    
702          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
703            XMapWindow(display, wnd);
704    
705          gc = XCreateGC(display, wnd, 0, NULL);          /* wait for VisibilityNotify */
706            do
707            {
708                    XMaskEvent(display, VisibilityChangeMask, &xevent);
709            }
710            while (xevent.type != VisibilityNotify);
711    
712          if (ownbackstore)          focused = False;
713                  backstore = XCreatePixmap(display, wnd, width, height, depth);          mouse_in_wnd = False;
714    
715          XMapWindow(display, wnd);          /* handle the WM_DELETE_WINDOW protocol */
716            protocol_atom = XInternAtom(display, "WM_PROTOCOLS", True);
717            kill_atom = XInternAtom(display, "WM_DELETE_WINDOW", True);
718            XSetWMProtocols(display, wnd, &kill_atom, 1);
719    
720          return True;          return True;
721  }  }
722    
723  void  void
724  ui_destroy_window()  ui_destroy_window(void)
725  {  {
726          if (ownbackstore)          if (IC != NULL)
727                  XFreePixmap(display, backstore);                  XDestroyIC(IC);
728    
729          XFreeGC(display, gc);          XDestroyWindow(display, wnd);
730    }
731    
732          close_inputmethod();  void
733    xwin_toggle_fullscreen(void)
734    {
735            Pixmap contents = 0;
736    
737          XDestroyWindow(display, wnd);          if (!ownbackstore)
738          XCloseDisplay(display);          {
739          display = NULL;                  /* need to save contents of window */
740                    contents = XCreatePixmap(display, wnd, width, height, depth);
741                    XCopyArea(display, wnd, contents, gc, 0, 0, width, height, 0, 0);
742            }
743    
744            ui_destroy_window();
745            fullscreen = !fullscreen;
746            ui_create_window();
747    
748            XDefineCursor(display, wnd, current_cursor);
749    
750            if (!ownbackstore)
751            {
752                    XCopyArea(display, contents, wnd, gc, 0, 0, width, height, 0, 0);
753                    XFreePixmap(display, contents);
754            }
755  }  }
756    
757  static void  /* Process all events in Xlib queue
758  xwin_process_events()     Returns 0 after user quit, 1 otherwise */
759    static int
760    xwin_process_events(void)
761  {  {
762          XEvent xevent;          XEvent xevent;
   
763          KeySym keysym;          KeySym keysym;
764          uint16 button, flags;          uint16 button, flags;
765          uint32 ev_time;          uint32 ev_time;
766          key_translation tr;          key_translation tr;
         char *ksname = NULL;  
767          char str[256];          char str[256];
768          Status status;          Status status;
769            unsigned int state;
770            Window wdummy;
771            int dummy;
772    
773          /* Refresh keyboard mapping if it has changed. This is important for          while (XPending(display) > 0)
            Xvnc, since it allocates keycodes dynamically */  
         if (XCheckTypedEvent(display, MappingNotify, &xevent))  
774          {          {
775                  if (xevent.xmapping.request == MappingKeyboard                  XNextEvent(display, &xevent);
                     || xevent.xmapping.request == MappingModifier)  
                         XRefreshKeyboardMapping(&xevent.xmapping);  
         }  
776    
777          while (XCheckMaskEvent(display, ~0, &xevent))                  if ((IC != NULL) && (XFilterEvent(&xevent, None) == True))
         {  
                 if (enable_compose && (XFilterEvent(&xevent, None) == True))  
778                  {                  {
779                          DEBUG_KBD("Filtering event\n");                          DEBUG_KBD(("Filtering event\n"));
780                          continue;                          continue;
781                  }                  }
782    
                 ev_time = time(NULL);  
783                  flags = 0;                  flags = 0;
784    
785                  switch (xevent.type)                  switch (xevent.type)
786                  {                  {
787                            case ClientMessage:
788                                    /* the window manager told us to quit */
789                                    if ((xevent.xclient.message_type == protocol_atom)
790                                        && ((Atom)xevent.xclient.data.l[0] == kill_atom))
791                                            /* Quit */
792                                            return 0;
793                                    break;
794    
795                          case KeyPress:                          case KeyPress:
796                                  if (IC != NULL)                                  if (IC != NULL)
797                                          /* Multi_key compatible version */                                          /* Multi_key compatible version */
798                                  {                                  {
799                                          XmbLookupString(IC,                                          XmbLookupString(IC,
800                                                          (XKeyPressedEvent *) &                                                          (XKeyPressedEvent *) &
801                                                          xevent, str,                                                          xevent, str, sizeof(str), &keysym, &status);
802                                                          sizeof(str), &keysym,                                          if (!((status == XLookupKeySym) || (status == XLookupBoth)))
                                                         &status);  
                                         if (!  
                                             ((status == XLookupKeySym)  
                                              || (status == XLookupBoth)))  
803                                          {                                          {
804                                                  error("XmbLookupString failed with status 0x%x\n", status);                                                  error("XmbLookupString failed with status 0x%x\n",
805                                                          status);
806                                                  break;                                                  break;
807                                          }                                          }
808                                  }                                  }
809                                  else                                  else
810                                  {                                  {
811                                          /* Plain old XLookupString */                                          /* Plain old XLookupString */
812                                          DEBUG_KBD                                          DEBUG_KBD(("\nNo input context, using XLookupString\n"));
                                                 ("No input context, using XLookupString\n");  
813                                          XLookupString((XKeyEvent *) & xevent,                                          XLookupString((XKeyEvent *) & xevent,
814                                                        str, sizeof(str),                                                        str, sizeof(str), &keysym, NULL);
                                                       &keysym, NULL);  
815                                  }                                  }
816    
817                                  ksname = get_ksname(keysym);                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym,
818                                  DEBUG_KBD                                             get_ksname(keysym)));
                                         ("\nKeyPress for (keysym 0x%lx, %s)\n",  
                                          keysym, ksname);  
819    
820                                  if (inhibit_key(keysym))                                  ev_time = time(NULL);
821                                  {                                  if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))
                                         DEBUG_KBD("Inhibiting key\n");  
822                                          break;                                          break;
                                 }  
823    
824                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
825                                                             xevent.xkey.                                                             xevent.xkey.keycode, xevent.xkey.state);
                                                            keycode,  
                                                            xevent.xkey.state);  
   
                                 ensure_remote_modifiers(ev_time, tr);  
826    
827                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
828                                          break;                                          break;
829    
830                                  rdp_send_scancode(ev_time, RDP_KEYPRESS,                                  ensure_remote_modifiers(ev_time, tr);
831                                                    tr.scancode);  
832                                    rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
833                                  break;                                  break;
834    
835                          case KeyRelease:                          case KeyRelease:
836                                  XLookupString((XKeyEvent *) & xevent, str,                                  XLookupString((XKeyEvent *) & xevent, str,
837                                                sizeof(str), &keysym, NULL);                                                sizeof(str), &keysym, NULL);
838    
839                                  ksname = get_ksname(keysym);                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
840                                  DEBUG_KBD                                             get_ksname(keysym)));
                                         ("\nKeyRelease for (keysym 0x%lx, %s)\n",  
                                          keysym, ksname);  
841    
842                                  if (inhibit_key(keysym))                                  ev_time = time(NULL);
843                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False))
844                                          break;                                          break;
845    
846                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
847                                                             xevent.xkey.                                                             xevent.xkey.keycode, xevent.xkey.state);
                                                            keycode,  
                                                            xevent.xkey.state);  
848    
849                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
850                                          break;                                          break;
851    
852                                  rdp_send_scancode(ev_time, RDP_KEYRELEASE,                                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);
                                                   tr.scancode);  
853                                  break;                                  break;
854    
855                          case ButtonPress:                          case ButtonPress:
# Line 499  xwin_process_events() Line 857  xwin_process_events()
857                                  /* fall through */                                  /* fall through */
858    
859                          case ButtonRelease:                          case ButtonRelease:
860                                  button = xkeymap_translate_button(xevent.                                  button = xkeymap_translate_button(xevent.xbutton.button);
                                                                   xbutton.  
                                                                   button);  
861                                  if (button == 0)                                  if (button == 0)
862                                          break;                                          break;
863    
864                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  /* If win_button_size is nonzero, enable single app mode */
865                                                 flags | button,                                  if (xevent.xbutton.y < win_button_size)
866                                                 xevent.xbutton.x,                                  {
867                                                 xevent.xbutton.y);                                          /* Stop moving window when button is released, regardless of cursor position */
868                                            if (moving_wnd && (xevent.type == ButtonRelease))
869                                                    moving_wnd = False;
870    
871                                            /*  Check from right to left: */
872    
873                                            if (xevent.xbutton.x >= width - win_button_size)
874                                            {
875                                                    /* The close button, continue */
876                                                    ;
877                                            }
878                                            else if (xevent.xbutton.x >= width - win_button_size * 2)
879                                            {
880                                                    /* The maximize/restore button. Do not send to
881                                                       server.  It might be a good idea to change the
882                                                       cursor or give some other visible indication
883                                                       that rdesktop inhibited this click */
884                                                    break;
885                                            }
886                                            else if (xevent.xbutton.x >= width - win_button_size * 3)
887                                            {
888                                                    /* The minimize button. Iconify window. */
889                                                    XIconifyWindow(display, wnd,
890                                                                   DefaultScreen(display));
891                                                    break;
892                                            }
893                                            else if (xevent.xbutton.x <= win_button_size)
894                                            {
895                                                    /* The system menu. Ignore. */
896                                                    break;
897                                            }
898                                            else
899                                            {
900                                                    /* The title bar. */
901                                                    if ((xevent.type == ButtonPress) && !fullscreen
902                                                        && hide_decorations)
903                                                    {
904                                                            moving_wnd = True;
905                                                            move_x_offset = xevent.xbutton.x;
906                                                            move_y_offset = xevent.xbutton.y;
907                                                    }
908                                                    break;
909    
910                                            }
911                                    }
912    
913                                    rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
914                                                   flags | button, xevent.xbutton.x, xevent.xbutton.y);
915                                  break;                                  break;
916    
917                          case MotionNotify:                          case MotionNotify:
918                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  if (moving_wnd)
919                                                 MOUSE_FLAG_MOVE,                                  {
920                                                 xevent.xmotion.x,                                          XMoveWindow(display, wnd,
921                                                 xevent.xmotion.y);                                                      xevent.xmotion.x_root - move_x_offset,
922                                                        xevent.xmotion.y_root - move_y_offset);
923                                            break;
924                                    }
925    
926                                    if (fullscreen && !focused)
927                                            XSetInputFocus(display, wnd, RevertToPointerRoot,
928                                                           CurrentTime);
929                                    rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
930                                                   MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
931                                    break;
932    
933                            case FocusIn:
934                                    if (xevent.xfocus.mode == NotifyGrab)
935                                            break;
936                                    focused = True;
937                                    XQueryPointer(display, wnd, &wdummy, &wdummy, &dummy, &dummy,
938                                                  &dummy, &dummy, &state);
939                                    reset_modifier_keys(state);
940                                    if (grab_keyboard && mouse_in_wnd)
941                                            XGrabKeyboard(display, wnd, True,
942                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
943                                    break;
944    
945                            case FocusOut:
946                                    if (xevent.xfocus.mode == NotifyUngrab)
947                                            break;
948                                    focused = False;
949                                    if (xevent.xfocus.mode == NotifyWhileGrabbed)
950                                            XUngrabKeyboard(display, CurrentTime);
951                                  break;                                  break;
952    
953                          case EnterNotify:                          case EnterNotify:
954                                  XGrabKeyboard(display, wnd, True,                                  /* we only register for this event when in fullscreen mode */
955                                                GrabModeAsync, GrabModeAsync,                                  /* or grab_keyboard */
956                                                CurrentTime);                                  mouse_in_wnd = True;
957                                    if (fullscreen)
958                                    {
959                                            XSetInputFocus(display, wnd, RevertToPointerRoot,
960                                                           CurrentTime);
961                                            break;
962                                    }
963                                    if (focused)
964                                            XGrabKeyboard(display, wnd, True,
965                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
966                                  break;                                  break;
967    
968                          case LeaveNotify:                          case LeaveNotify:
969                                    /* we only register for this event when grab_keyboard */
970                                    mouse_in_wnd = False;
971                                  XUngrabKeyboard(display, CurrentTime);                                  XUngrabKeyboard(display, CurrentTime);
972                                  break;                                  break;
973    
# Line 535  xwin_process_events() Line 978  xwin_process_events()
978                                            xevent.xexpose.height,                                            xevent.xexpose.height,
979                                            xevent.xexpose.x, xevent.xexpose.y);                                            xevent.xexpose.x, xevent.xexpose.y);
980                                  break;                                  break;
981    
982                            case MappingNotify:
983                                    /* Refresh keyboard mapping if it has changed. This is important for
984                                       Xvnc, since it allocates keycodes dynamically */
985                                    if (xevent.xmapping.request == MappingKeyboard
986                                        || xevent.xmapping.request == MappingModifier)
987                                            XRefreshKeyboardMapping(&xevent.xmapping);
988    
989                                    if (xevent.xmapping.request == MappingModifier)
990                                    {
991                                            XFreeModifiermap(mod_map);
992                                            mod_map = XGetModifierMapping(display);
993                                    }
994                                    break;
995    
996                  }                  }
997          }          }
998            /* Keep going */
999            return 1;
1000  }  }
1001    
1002  void  /* Returns 0 after user quit, 1 otherwise */
1003    int
1004  ui_select(int rdp_socket)  ui_select(int rdp_socket)
1005  {  {
1006          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 549  ui_select(int rdp_socket) Line 1010  ui_select(int rdp_socket)
1010    
1011          while (True)          while (True)
1012          {          {
1013                    /* Process any events already waiting */
1014                    if (!xwin_process_events())
1015                            /* User quit */
1016                            return 0;
1017    
1018                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
1019                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
1020                  if (display != NULL)                  FD_SET(x_socket, &rfds);
                 {  
                         FD_SET(x_socket, &rfds);  
                         XFlush(display);  
                 }  
1021    
1022                  switch (select(n, &rfds, NULL, NULL, NULL))                  switch (select(n, &rfds, NULL, NULL, NULL))
1023                  {                  {
# Line 566  ui_select(int rdp_socket) Line 1028  ui_select(int rdp_socket)
1028                                  continue;                                  continue;
1029                  }                  }
1030    
                 if (FD_ISSET(x_socket, &rfds))  
                         xwin_process_events();  
   
1031                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
1032                          return;                          return 1;
1033          }          }
1034  }  }
1035    
# Line 589  ui_create_bitmap(int width, int height, Line 1048  ui_create_bitmap(int width, int height,
1048    
1049          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = (owncolmap ? data : translate_image(width, height, data));
1050          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(display, wnd, width, height, depth);
1051          image = XCreateImage(display, visual, depth, ZPixmap, 0, tdata, width,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
1052                               height, 8, 0);                               (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0);
1053    
1054          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
1055    
# Line 601  ui_create_bitmap(int width, int height, Line 1060  ui_create_bitmap(int width, int height,
1060  }  }
1061    
1062  void  void
1063  ui_paint_bitmap(int x, int y, int cx, int cy, int width, int height,  ui_paint_bitmap(int x, int y, int cx, int cy, int width, int height, uint8 * data)
                 uint8 * data)  
1064  {  {
1065          XImage *image;          XImage *image;
1066          uint8 *tdata;          uint8 *tdata;
   
1067          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = (owncolmap ? data : translate_image(width, height, data));
1068          image = XCreateImage(display, visual, depth, ZPixmap, 0, tdata, width,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
1069                               height, 8, 0);                               (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0);
1070    
1071          if (ownbackstore)          if (ownbackstore)
1072          {          {
# Line 645  ui_create_glyph(int width, int height, u Line 1102  ui_create_glyph(int width, int height, u
1102          bitmap = XCreatePixmap(display, wnd, width, height, 1);          bitmap = XCreatePixmap(display, wnd, width, height, 1);
1103          gc = XCreateGC(display, bitmap, 0, NULL);          gc = XCreateGC(display, bitmap, 0, NULL);
1104    
1105          image = XCreateImage(display, visual, 1, ZPixmap, 0, data, width,          image = XCreateImage(display, visual, 1, ZPixmap, 0, (char *) data,
1106                               height, 8, scanline);                               width, height, 8, scanline);
1107          image->byte_order = MSBFirst;          image->byte_order = MSBFirst;
1108          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
1109          XInitImage(image);          XInitImage(image);
# Line 680  ui_create_cursor(unsigned int x, unsigne Line 1137  ui_create_cursor(unsigned int x, unsigne
1137          scanline = (width + 7) / 8;          scanline = (width + 7) / 8;
1138          offset = scanline * height;          offset = scanline * height;
1139    
1140          cursor = xmalloc(offset);          cursor = (uint8*)xmalloc(offset);
1141          memset(cursor, 0, offset);          memset(cursor, 0, offset);
1142    
1143          mask = xmalloc(offset);          mask = (uint8*)xmalloc(offset);
1144          memset(mask, 0, offset);          memset(mask, 0, offset);
1145    
1146          /* approximate AND and XOR masks with a monochrome X pointer */          /* approximate AND and XOR masks with a monochrome X pointer */
# Line 738  ui_create_cursor(unsigned int x, unsigne Line 1195  ui_create_cursor(unsigned int x, unsigne
1195  void  void
1196  ui_set_cursor(HCURSOR cursor)  ui_set_cursor(HCURSOR cursor)
1197  {  {
1198          XDefineCursor(display, wnd, (Cursor) cursor);          current_cursor = (Cursor) cursor;
1199            XDefineCursor(display, wnd, current_cursor);
1200  }  }
1201    
1202  void  void
# Line 753  ui_destroy_cursor(HCURSOR cursor) Line 1211  ui_destroy_cursor(HCURSOR cursor)
1211                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \
1212                  (xc)->flags = DoRed | DoGreen | DoBlue;                  (xc)->flags = DoRed | DoGreen | DoBlue;
1213    
1214    
1215  HCOLOURMAP  HCOLOURMAP
1216  ui_create_colourmap(COLOURMAP * colours)  ui_create_colourmap(COLOURMAP * colours)
1217  {  {
1218          COLOURENTRY *entry;          COLOURENTRY *entry;
1219          int i, ncolours = colours->ncolours;          int i, ncolours = colours->ncolours;
1220            if (!owncolmap)
1221            {
1222                    uint32 *map = (uint32*)xmalloc(sizeof(*colmap) * ncolours);
1223                    XColor xentry;
1224                    XColor xc_cache[256];
1225                    uint32 colour;
1226                    int colLookup = 256;
1227                    for (i = 0; i < ncolours; i++)
1228                    {
1229                            entry = &colours->colours[i];
1230                            MAKE_XCOLOR(&xentry, entry);
1231    
1232                            if (XAllocColor(display, xcolmap, &xentry) == 0)
1233                            {
1234                                    /* Allocation failed, find closest match. */
1235                                    int j = 256;
1236                                    int nMinDist = 3 * 256 * 256;
1237                                    long nDist = nMinDist;
1238    
1239          if (owncolmap)                                  /* only get the colors once */
1240                                    while (colLookup--)
1241                                    {
1242                                            xc_cache[colLookup].pixel = colLookup;
1243                                            xc_cache[colLookup].red = xc_cache[colLookup].green =
1244                                                    xc_cache[colLookup].blue = 0;
1245                                            xc_cache[colLookup].flags = 0;
1246                                            XQueryColor(display,
1247                                                        DefaultColormap(display,
1248                                                                        DefaultScreen(display)),
1249                                                        &xc_cache[colLookup]);
1250                                    }
1251                                    colLookup = 0;
1252    
1253                                    /* approximate the pixel */
1254                                    while (j--)
1255                                    {
1256                                            if (xc_cache[j].flags)
1257                                            {
1258                                                    nDist = ((long) (xc_cache[j].red >> 8) -
1259                                                             (long) (xentry.red >> 8)) *
1260                                                            ((long) (xc_cache[j].red >> 8) -
1261                                                             (long) (xentry.red >> 8)) +
1262                                                            ((long) (xc_cache[j].green >> 8) -
1263                                                             (long) (xentry.green >> 8)) *
1264                                                            ((long) (xc_cache[j].green >> 8) -
1265                                                             (long) (xentry.green >> 8)) +
1266                                                            ((long) (xc_cache[j].blue >> 8) -
1267                                                             (long) (xentry.blue >> 8)) *
1268                                                            ((long) (xc_cache[j].blue >> 8) -
1269                                                             (long) (xentry.blue >> 8));
1270                                            }
1271                                            if (nDist < nMinDist)
1272                                            {
1273                                                    nMinDist = nDist;
1274                                                    xentry.pixel = j;
1275                                            }
1276                                    }
1277                            }
1278                            colour = xentry.pixel;
1279    
1280                            /* update our cache */
1281                            if (xentry.pixel < 256)
1282                            {
1283                                    xc_cache[xentry.pixel].red = xentry.red;
1284                                    xc_cache[xentry.pixel].green = xentry.green;
1285                                    xc_cache[xentry.pixel].blue = xentry.blue;
1286    
1287                            }
1288    
1289    
1290                            /* byte swap here to make translate_image faster */
1291                            map[i] = translate_colour(colour);
1292                    }
1293                    return map;
1294            }
1295            else
1296          {          {
1297                  XColor *xcolours, *xentry;                  XColor *xcolours, *xentry;
1298                  Colormap map;                  Colormap map;
1299    
1300                  xcolours = xmalloc(sizeof(XColor) * ncolours);                  xcolours = (XColor*)xmalloc(sizeof(XColor) * ncolours);
1301                  for (i = 0; i < ncolours; i++)                  for (i = 0; i < ncolours; i++)
1302                  {                  {
1303                          entry = &colours->colours[i];                          entry = &colours->colours[i];
# Line 779  ui_create_colourmap(COLOURMAP * colours) Line 1312  ui_create_colourmap(COLOURMAP * colours)
1312                  xfree(xcolours);                  xfree(xcolours);
1313                  return (HCOLOURMAP) map;                  return (HCOLOURMAP) map;
1314          }          }
         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;  
         }  
1315  }  }
1316    
1317  void  void
1318  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
1319  {  {
1320          if (owncolmap)          if (!owncolmap)
                 XFreeColormap(display, (Colormap) map);  
         else  
1321                  xfree(map);                  xfree(map);
1322            else
1323                    XFreeColormap(display, (Colormap) map);
1324  }  }
1325    
1326  void  void
1327  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
1328  {  {
1329          if (owncolmap)          if (!owncolmap)
1330                  XSetWindowColormap(display, wnd, (Colormap) map);                  colmap = (uint32*)map;
1331          else          else
1332                  colmap = map;                  XSetWindowColormap(display, wnd, (Colormap) map);
1333  }  }
1334    
1335  void  void
# Line 834  ui_set_clip(int x, int y, int cx, int cy Line 1345  ui_set_clip(int x, int y, int cx, int cy
1345  }  }
1346    
1347  void  void
1348  ui_reset_clip()  ui_reset_clip(void)
1349  {  {
1350          XRectangle rect;          XRectangle rect;
1351    
# Line 846  ui_reset_clip() Line 1357  ui_reset_clip()
1357  }  }
1358    
1359  void  void
1360  ui_bell()  ui_bell(void)
1361  {  {
1362          XBell(display, 0);          XBell(display, 0);
1363  }  }
# Line 860  ui_destblt(uint8 opcode, Line 1371  ui_destblt(uint8 opcode,
1371          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1372  }  }
1373    
1374    static uint8 hatch_patterns[] = {
1375            0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, /* 0 - bsHorizontal */
1376            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, /* 1 - bsVertical */
1377            0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, /* 2 - bsFDiagonal */
1378            0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, /* 3 - bsBDiagonal */
1379            0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08, /* 4 - bsCross */
1380            0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81  /* 5 - bsDiagCross */
1381    };
1382    
1383  void  void
1384  ui_patblt(uint8 opcode,  ui_patblt(uint8 opcode,
1385            /* dest */ int x, int y, int cx, int cy,            /* dest */ int x, int y, int cx, int cy,
# Line 877  ui_patblt(uint8 opcode, Line 1397  ui_patblt(uint8 opcode,
1397                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
1398                          break;                          break;
1399    
1400                    case 2: /* Hatch */
1401                            fill = (Pixmap) ui_create_glyph(8, 8, hatch_patterns + brush->pattern[0] * 8);
1402                            SET_FOREGROUND(bgcolour);
1403                            SET_BACKGROUND(fgcolour);
1404                            XSetFillStyle(display, gc, FillOpaqueStippled);
1405                            XSetStipple(display, gc, fill);
1406                            XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin);
1407                            FILL_RECTANGLE(x, y, cx, cy);
1408                            XSetFillStyle(display, gc, FillSolid);
1409                            XSetTSOrigin(display, gc, 0, 0);
1410                            ui_destroy_glyph((HGLYPH) fill);
1411                            break;
1412    
1413                  case 3: /* Pattern */                  case 3: /* Pattern */
1414                          for (i = 0; i != 8; i++)                          for (i = 0; i != 8; i++)
1415                                  ipattern[7 - i] = brush->pattern[i];                                  ipattern[7 - i] = brush->pattern[i];
# Line 886  ui_patblt(uint8 opcode, Line 1419  ui_patblt(uint8 opcode,
1419                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(fgcolour);
1420                          XSetFillStyle(display, gc, FillOpaqueStippled);                          XSetFillStyle(display, gc, FillOpaqueStippled);
1421                          XSetStipple(display, gc, fill);                          XSetStipple(display, gc, fill);
1422                          XSetTSOrigin(display, gc, brush->xorigin,                          XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin);
                                      brush->yorigin);  
1423    
1424                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
1425    
1426                          XSetFillStyle(display, gc, FillSolid);                          XSetFillStyle(display, gc, FillSolid);
1427                            XSetTSOrigin(display, gc, 0, 0);
1428                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
1429                          break;                          break;
1430    
# Line 910  ui_screenblt(uint8 opcode, Line 1443  ui_screenblt(uint8 opcode,
1443          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1444          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);
1445          if (ownbackstore)          if (ownbackstore)
1446                  XCopyArea(display, backstore, backstore, gc, srcx, srcy, cx,                  XCopyArea(display, backstore, backstore, gc, srcx, srcy, cx, cy, x, y);
                           cy, x, y);  
1447          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1448  }  }
1449    
# Line 923  ui_memblt(uint8 opcode, Line 1455  ui_memblt(uint8 opcode,
1455          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1456          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);
1457          if (ownbackstore)          if (ownbackstore)
1458                  XCopyArea(display, (Pixmap) src, backstore, gc, srcx, srcy,                  XCopyArea(display, (Pixmap) src, backstore, gc, srcx, srcy, cx, cy, x, y);
                           cx, cy, x, y);  
1459          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1460  }  }
1461    
# Line 941  ui_triblt(uint8 opcode, Line 1472  ui_triblt(uint8 opcode,
1472          {          {
1473                  case 0x69:      /* PDSxxn */                  case 0x69:      /* PDSxxn */
1474                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);
1475                          ui_patblt(ROP2_NXOR, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_NXOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1476                          break;                          break;
1477    
1478                  case 0xb8:      /* PSDPxax */                  case 0xb8:      /* PSDPxax */
1479                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1480                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);
1481                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1482                          break;                          break;
1483    
1484                  case 0xc0:      /* PSa */                  case 0xc0:      /* PSa */
1485                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
1486                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1487                          break;                          break;
1488    
1489                  default:                  default:
# Line 987  ui_rect( Line 1514  ui_rect(
1514          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE(x, y, cx, cy);
1515  }  }
1516    
1517    /* warning, this function only draws on wnd or backstore, not both */
1518  void  void
1519  ui_draw_glyph(int mixmode,  ui_draw_glyph(int mixmode,
1520                /* dest */ int x, int y, int cx, int cy,                /* dest */ int x, int y, int cx, int cy,
# Line 997  ui_draw_glyph(int mixmode, Line 1525  ui_draw_glyph(int mixmode,
1525          SET_BACKGROUND(bgcolour);          SET_BACKGROUND(bgcolour);
1526    
1527          XSetFillStyle(display, gc,          XSetFillStyle(display, gc,
1528                        (mixmode ==                        (mixmode == MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);
                        MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);  
1529          XSetStipple(display, gc, (Pixmap) glyph);          XSetStipple(display, gc, (Pixmap) glyph);
1530          XSetTSOrigin(display, gc, x, y);          XSetTSOrigin(display, gc, x, y);
1531    
1532          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1533    
1534          XSetFillStyle(display, gc, FillSolid);          XSetFillStyle(display, gc, FillSolid);
1535  }  }
# Line 1016  ui_draw_glyph(int mixmode, Line 1543  ui_draw_glyph(int mixmode,
1543        if ((xyoffset & 0x80))\        if ((xyoffset & 0x80))\
1544          {\          {\
1545            if (flags & TEXT2_VERTICAL) \            if (flags & TEXT2_VERTICAL) \
1546              y += ttext[++idx] | (ttext[++idx] << 8);\              y += ttext[idx+1] | (ttext[idx+2] << 8);\
1547            else\            else\
1548              x += ttext[++idx] | (ttext[++idx] << 8);\              x += ttext[idx+1] | (ttext[idx+2] << 8);\
1549              idx += 2;\
1550          }\          }\
1551        else\        else\
1552          {\          {\
# Line 1030  ui_draw_glyph(int mixmode, Line 1558  ui_draw_glyph(int mixmode,
1558      }\      }\
1559    if (glyph != NULL)\    if (glyph != NULL)\
1560      {\      {\
1561        ui_draw_glyph (mixmode, x + (short) glyph->offset,\        ui_draw_glyph (mixmode, x + glyph->offset,\
1562                       y + (short) glyph->baseline,\                       y + glyph->baseline,\
1563                       glyph->width, glyph->height,\                       glyph->width, glyph->height,\
1564                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\
1565        if (flags & TEXT2_IMPLICIT_X)\        if (flags & TEXT2_IMPLICIT_X)\
# Line 1053  ui_draw_text(uint8 font, uint8 flags, in Line 1581  ui_draw_text(uint8 font, uint8 flags, in
1581    
1582          if (boxcx > 1)          if (boxcx > 1)
1583          {          {
1584                  FILL_RECTANGLE(boxx, boxy, boxcx, boxcy);                  FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);
1585          }          }
1586          else if (mixmode == MIX_OPAQUE)          else if (mixmode == MIX_OPAQUE)
1587          {          {
1588                  FILL_RECTANGLE(clipx, clipy, clipcx, clipcy);                  FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);
1589          }          }
1590    
1591          /* Paint text, character by character */          /* Paint text, character by character */
# Line 1067  ui_draw_text(uint8 font, uint8 flags, in Line 1595  ui_draw_text(uint8 font, uint8 flags, in
1595                  {                  {
1596                          case 0xff:                          case 0xff:
1597                                  if (i + 2 < length)                                  if (i + 2 < length)
1598                                          cache_put_text(text[i + 1], text,                                          cache_put_text(text[i + 1], text, text[i + 2]);
                                                        text[i + 2]);  
1599                                  else                                  else
1600                                  {                                  {
1601                                          error("this shouldn't be happening\n");                                          error("this shouldn't be happening\n");
1602                                          break;                                          exit(1);
1603                                  }                                  }
1604                                  /* this will move pointer from start to first character after FF command */                                  /* this will move pointer from start to first character after FF command */
1605                                  length -= i + 3;                                  length -= i + 3;
# Line 1085  ui_draw_text(uint8 font, uint8 flags, in Line 1612  ui_draw_text(uint8 font, uint8 flags, in
1612                                  if (entry != NULL)                                  if (entry != NULL)
1613                                  {                                  {
1614                                          if ((((uint8 *) (entry->data))[1] ==                                          if ((((uint8 *) (entry->data))[1] ==
1615                                               0)                                               0) && (!(flags & TEXT2_IMPLICIT_X)))
                                             && (!(flags & TEXT2_IMPLICIT_X)))  
1616                                          {                                          {
1617                                                  if (flags & TEXT2_VERTICAL)                                                  if (flags & TEXT2_VERTICAL)
1618                                                          y += text[i + 2];                                                          y += text[i + 2];
1619                                                  else                                                  else
1620                                                          x += text[i + 2];                                                          x += text[i + 2];
1621                                          }                                          }
                                         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;  
1622                                          for (j = 0; j < entry->size; j++)                                          for (j = 0; j < entry->size; j++)
1623                                                  DO_GLYPH(((uint8 *) (entry->                                                  DO_GLYPH(((uint8 *) (entry->data)), j);
                                                                      data)),  
                                                          j);  
1624                                  }                                  }
1625                                    if (i + 2 < length)
1626                                            i += 3;
1627                                    else
1628                                            i += 2;
1629                                    length -= i;
1630                                    /* this will move pointer from start to first character after FE command */
1631                                    text = &(text[i]);
1632                                    i = 0;
1633                                  break;                                  break;
1634    
1635                          default:                          default:
# Line 1114  ui_draw_text(uint8 font, uint8 flags, in Line 1638  ui_draw_text(uint8 font, uint8 flags, in
1638                                  break;                                  break;
1639                  }                  }
1640          }          }
1641            if (ownbackstore)
1642            {
1643                    if (boxcx > 1)
1644                            XCopyArea(display, backstore, wnd, gc, boxx,
1645                                      boxy, boxcx, boxcy, boxx, boxy);
1646                    else
1647                            XCopyArea(display, backstore, wnd, gc, clipx,
1648                                      clipy, clipcx, clipcy, clipx, clipy);
1649            }
1650  }  }
1651    
1652  void  void
# Line 1126  ui_desktop_save(uint32 offset, int x, in Line 1657  ui_desktop_save(uint32 offset, int x, in
1657    
1658          if (ownbackstore)          if (ownbackstore)
1659          {          {
1660                  image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes,                  image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes, ZPixmap);
                                   ZPixmap);  
1661          }          }
1662          else          else
1663          {          {
1664                  pix = XCreatePixmap(display, wnd, cx, cy, depth);                  pix = XCreatePixmap(display, wnd, cx, cy, depth);
1665                  XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);                  XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);
1666                  image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes,                  image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);
                                   ZPixmap);  
1667                  XFreePixmap(display, pix);                  XFreePixmap(display, pix);
1668          }          }
1669    
1670          offset *= bpp / 8;          offset *= bpp / 8;
1671          cache_put_desktop(offset, cx, cy, image->bytes_per_line, bpp / 8,          cache_put_desktop(offset, cx, cy, image->bytes_per_line, bpp / 8, (uint8 *) image->data);
                           (uint8 *) image->data);  
1672    
1673          XDestroyImage(image);          XDestroyImage(image);
1674  }  }
# Line 1156  ui_desktop_restore(uint32 offset, int x, Line 1684  ui_desktop_restore(uint32 offset, int x,
1684          if (data == NULL)          if (data == NULL)
1685                  return;                  return;
1686    
1687          image = XCreateImage(display, visual, depth, ZPixmap, 0, data, cx, cy,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
1688                               BitmapPad(display), cx * bpp / 8);                               (char *) data, cx, cy, BitmapPad(display), cx * bpp / 8);
1689    
1690          if (ownbackstore)          if (ownbackstore)
1691          {          {

Legend:
Removed from v.70  
changed lines
  Added in v.376

  ViewVC Help
Powered by ViewVC 1.1.26