/[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 80 by jsorg71, Tue Jul 30 00:56:48 2002 UTC revision 524 by matthewc, Wed Oct 29 06:29:05 2003 UTC
# Line 1  Line 1 
1  /*  /* -*- c-basic-offset: 8 -*-
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  #include "scancodes.h"  #include "xproto.h"
27    
28  extern int width;  extern int g_width;
29  extern int height;  extern int g_height;
30  extern BOOL sendmotion;  extern BOOL g_sendmotion;
31  extern BOOL fullscreen;  extern BOOL g_fullscreen;
32  extern BOOL grab_keyboard;  extern BOOL g_grab_keyboard;
33    extern BOOL g_hide_decorations;
34  Display *display = NULL;  extern char g_title[];
35  static int x_socket;  extern int g_server_bpp;
36  static Window wnd;  extern int g_win_button_size;
37  static GC gc;  
38  static Visual *visual;  Display *g_display;
39  static int depth;  Time g_last_gesturetime;
40  static int bpp;  static int g_x_socket;
41    static Screen *g_screen;
42    Window g_wnd;
43    BOOL g_enable_compose = False;
44    static GC g_gc;
45    static Visual *g_visual;
46    static int g_depth;
47    static int g_bpp;
48    static XIM g_IM;
49    static XIC g_IC;
50    static XModifierKeymap *g_mod_map;
51    static Cursor g_current_cursor;
52    static HCURSOR g_null_cursor;
53    static Atom g_protocol_atom, g_kill_atom;
54    static BOOL g_focused;
55    static BOOL g_mouse_in_wnd;
56    
57  /* endianness */  /* endianness */
58  static BOOL host_be;  static BOOL g_host_be;
59  static BOOL xserver_be;  static BOOL g_xserver_be;
60    static BOOL g_xserver_bgr;
61    
62  /* software backing store */  /* software backing store */
63  static BOOL ownbackstore;  static BOOL g_ownbackstore;
64  static Pixmap backstore;  static Pixmap g_backstore;
65    
66    /* Moving in single app mode */
67    static BOOL g_moving_wnd;
68    static int g_move_x_offset = 0;
69    static int g_move_y_offset = 0;
70    
71    #ifdef WITH_RDPSND
72    extern int g_dsp_fd;
73    extern BOOL g_dsp_busy;
74    extern BOOL g_rdpsnd;
75    #endif
76    
77    /* MWM decorations */
78    #define MWM_HINTS_DECORATIONS   (1L << 1)
79    #define PROP_MOTIF_WM_HINTS_ELEMENTS    5
80    typedef struct
81    {
82            uint32 flags;
83            uint32 functions;
84            uint32 decorations;
85            sint32 inputMode;
86            uint32 status;
87    }
88    PropMotifWmHints;
89    
90    typedef struct
91    {
92            uint32 red;
93            uint32 green;
94            uint32 blue;
95    }
96    PixelColour;
97    
98    
99  #define FILL_RECTANGLE(x,y,cx,cy)\  #define FILL_RECTANGLE(x,y,cx,cy)\
100  { \  { \
101          XFillRectangle(display, wnd, gc, x, y, cx, cy); \          XFillRectangle(g_display, g_wnd, g_gc, x, y, cx, cy); \
102          if (ownbackstore) \          if (g_ownbackstore) \
103                  XFillRectangle(display, backstore, gc, x, y, cx, cy); \                  XFillRectangle(g_display, g_backstore, g_gc, x, y, cx, cy); \
104    }
105    
106    #define FILL_RECTANGLE_BACKSTORE(x,y,cx,cy)\
107    { \
108            XFillRectangle(g_display, g_ownbackstore ? g_backstore : g_wnd, g_gc, x, y, cx, cy); \
109  }  }
110    
111  /* colour maps */  /* colour maps */
112  static BOOL owncolmap;  BOOL g_owncolmap = False;
113  static Colormap xcolmap;  static Colormap g_xcolmap;
114  static uint32 white;  static uint32 *g_colmap = NULL;
115  static uint32 *colmap;  
116  static XIM IM = NULL;  #define TRANSLATE(col)          ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : translate_colour(g_colmap[col]) )
117  static XIC IC = NULL;  #define SET_FOREGROUND(col)     XSetForeground(g_display, g_gc, TRANSLATE(col));
118    #define SET_BACKGROUND(col)     XSetBackground(g_display, g_gc, TRANSLATE(col));
 /* Compose support */  
 BOOL enable_compose = False;  
   
 #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )  
 #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));  
 #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));  
119    
120  static int rop2_map[] = {  static int rop2_map[] = {
121          GXclear,                /* 0 */          GXclear,                /* 0 */
# Line 90  static int rop2_map[] = { Line 136  static int rop2_map[] = {
136          GXset                   /* 1 */          GXset                   /* 1 */
137  };  };
138    
139  #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(display, gc, rop2_map[rop2]); }  #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, rop2_map[rop2]); }
140  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, GXcopy); }
141    
142  static void  static void
143  translate8(uint8 * data, uint8 * out, uint8 * end)  mwm_hide_decorations(void)
144    {
145            PropMotifWmHints motif_hints;
146            Atom hintsatom;
147    
148            /* setup the property */
149            motif_hints.flags = MWM_HINTS_DECORATIONS;
150            motif_hints.decorations = 0;
151    
152            /* get the atom for the property */
153            hintsatom = XInternAtom(g_display, "_MOTIF_WM_HINTS", False);
154            if (!hintsatom)
155            {
156                    warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n");
157                    return;
158            }
159    
160            XChangeProperty(g_display, g_wnd, hintsatom, hintsatom, 32, PropModeReplace,
161                            (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
162    }
163    
164    static PixelColour
165    split_colour15(uint32 colour)
166    {
167            PixelColour rv;
168            rv.red = (colour & 0x7c00) >> 10;
169            rv.red = (rv.red * 0xff) / 0x1f;
170            rv.green = (colour & 0x03e0) >> 5;
171            rv.green = (rv.green * 0xff) / 0x1f;
172            rv.blue = (colour & 0x1f);
173            rv.blue = (rv.blue * 0xff) / 0x1f;
174            return rv;
175    }
176    
177    static PixelColour
178    split_colour16(uint32 colour)
179    {
180            PixelColour rv;
181            rv.red = (colour & 0xf800) >> 11;
182            rv.red = (rv.red * 0xff) / 0x1f;
183            rv.green = (colour & 0x07e0) >> 5;
184            rv.green = (rv.green * 0xff) / 0x3f;
185            rv.blue = (colour & 0x001f);
186            rv.blue = (rv.blue * 0xff) / 0x1f;
187            return rv;
188    }
189    
190    static PixelColour
191    split_colour24(uint32 colour)
192    {
193            PixelColour rv;
194            rv.blue = (colour & 0xff0000) >> 16;
195            rv.green = (colour & 0xff00) >> 8;
196            rv.red = (colour & 0xff);
197            return rv;
198    }
199    
200    static uint32
201    make_colour16(PixelColour pc)
202    {
203            pc.red = (pc.red * 0x1f) / 0xff;
204            pc.green = (pc.green * 0x3f) / 0xff;
205            pc.blue = (pc.blue * 0x1f) / 0xff;
206            if (g_xserver_bgr)
207                    return (pc.blue << 11) | (pc.green << 5) | pc.red;
208            else
209                    return (pc.red << 11) | (pc.green << 5) | pc.blue;
210                    
211    }
212    
213    static uint32
214    make_colour24(PixelColour pc)
215    {
216            if (g_xserver_bgr)
217                    return (pc.blue << 16) | (pc.green << 8) | pc.red;
218            else
219                    return (pc.red << 16) | (pc.green << 8) | pc.blue;
220    }
221    
222    static uint32
223    make_colour32(PixelColour pc)
224    {
225            if (g_xserver_bgr)
226                    return (pc.blue << 16) | (pc.green << 8) | pc.red;
227            else
228                    return (pc.red << 16) | (pc.green << 8) | pc.blue;
229    }
230    
231    #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
232    #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }
233    #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
234                            x = (x << 16) | (x >> 16); }
235    
236    static uint32
237    translate_colour(uint32 colour)
238    {
239            switch (g_server_bpp)
240            {
241                    case 15:
242                            switch (g_bpp)
243                            {
244                                    case 16:
245                                            colour = make_colour16(split_colour15(colour));
246                                            break;
247                                    case 24:
248                                            colour = make_colour24(split_colour15(colour));
249                                            break;
250                                    case 32:
251                                            colour = make_colour32(split_colour15(colour));
252                                            break;
253                            }
254                            break;
255                    case 16:
256                            switch (g_bpp)
257                            {
258                                    case 16:
259                                            break;
260                                    case 24:
261                                            colour = make_colour24(split_colour16(colour));
262                                            break;
263                                    case 32:
264                                            colour = make_colour32(split_colour16(colour));
265                                            break;
266                            }
267                            break;
268                    case 24:
269                            switch (g_bpp)
270                            {
271                                    case 16:
272                                            colour = make_colour16(split_colour24(colour));
273                                            break;
274                                    case 24:
275                                            colour = make_colour24(split_colour24(colour));
276                                            break;
277                                    case 32:
278                                            colour = make_colour32(split_colour24(colour));
279                                            break;
280                            }
281                            break;
282            }
283            return colour;
284    }
285    
286    static void
287    translate8to8(uint8 * data, uint8 * out, uint8 * end)
288  {  {
289          while (out < end)          while (out < end)
290                  *(out++) = (uint8) colmap[*(data++)];                  *(out++) = (uint8) g_colmap[*(data++)];
291  }  }
292    
293  static void  static void
294  translate16(uint8 * data, uint16 * out, uint16 * end)  translate8to16(uint8 * data, uint8 * out, uint8 * end)
295  {  {
296            uint16 value;
297    
298          while (out < end)          while (out < end)
299                  *(out++) = (uint16) colmap[*(data++)];          {
300                    value = (uint16) g_colmap[*(data++)];
301                    
302                    if (g_xserver_be)
303                    {
304                            *(out++) = value >> 8;
305                            *(out++) = value;
306                    }
307                    else
308                    {
309                            *(out++) = value;
310                            *(out++) = value >> 8;
311                    }
312            }
313  }  }
314    
315  /* little endian - conversion happens when colourmap is built */  /* little endian - conversion happens when colourmap is built */
316  static void  static void
317  translate24(uint8 * data, uint8 * out, uint8 * end)  translate8to24(uint8 * data, uint8 * out, uint8 * end)
318  {  {
319          uint32 value;          uint32 value;
320    
321          while (out < end)          while (out < end)
322          {          {
323                  value = colmap[*(data++)];                  value = g_colmap[*(data++)];
324                  *(out++) = value;                  
325                  *(out++) = value >> 8;                  if (g_xserver_be)
326                  *(out++) = value >> 16;                  {
327                            *(out++) = value >> 16;
328                            *(out++) = value >> 8;
329                            *(out++) = value;
330                    }
331                    else
332                    {
333                            *(out++) = value;
334                            *(out++) = value >> 8;
335                            *(out++) = value >> 16;
336                    }
337          }          }
338  }  }
339    
340  static void  static void
341  translate32(uint8 * data, uint32 * out, uint32 * end)  translate8to32(uint8 * data, uint8 * out, uint8 * end)
342  {  {
343            uint32 value;
344    
345          while (out < end)          while (out < end)
346                  *(out++) = colmap[*(data++)];          {
347                    value = g_colmap[*(data++)];
348    
349                    if (g_xserver_be)
350                    {
351                            *(out++) = value >> 24;
352                            *(out++) = value >> 16;
353                            *(out++) = value >> 8;
354                            *(out++) = value;
355                    }
356                    else
357                    {
358                            *(out++) = value;
359                            *(out++) = value >> 8;
360                            *(out++) = value >> 16;
361                            *(out++) = value >> 24;
362                    }
363            }
364  }  }
365    
366  static uint8 *  /* todo the remaining translate function might need some big endian check ?? */
367  translate_image(int width, int height, uint8 * data)  
368    static void
369    translate15to16(uint16 * data, uint8 * out, uint8 * end)
370  {  {
371          int size = width * height * bpp / 8;          uint16 pixel;
372          uint8 *out = xmalloc(size);          uint16 value;
         uint8 *end = out + size;  
373    
374          switch (bpp)          while (out < end)
375          {          {
376                  case 8:                  pixel = *(data++);
                         translate8(data, out, end);  
                         break;  
377    
378                  case 16:                  if (g_host_be)
379                          translate16(data, (uint16 *) out, (uint16 *) end);                  {
380                          break;                          BSWAP16(pixel);
381                    }
382    
383                  case 24:                  value = make_colour16(split_colour15(pixel));
                         translate24(data, out, end);  
                         break;  
384    
385                  case 32:                  if (g_xserver_be)
386                          translate32(data, (uint32 *) out, (uint32 *) end);                  {
387                          break;                          *(out++) = value >> 8;
388                            *(out++) = value;
389                    }
390                    else
391                    {
392                            *(out++) = value;
393                            *(out++) = value >> 8;
394                    }
395          }          }
   
         return out;  
396  }  }
397    
398  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }  static void
399  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }  translate15to24(uint16 * data, uint8 * out, uint8 * end)
400  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \  {
401                          x = (x << 16) | (x >> 16); }          uint32 value;
402            uint16 pixel;
403    
404  static uint32          while (out < end)
405  translate_colour(uint32 colour)          {
406                    pixel = *(data++);
407    
408                    if (g_host_be)
409                    {
410                            BSWAP16(pixel);
411                    }
412    
413                    value = make_colour24(split_colour15(pixel));
414                    if (g_xserver_be)
415                    {
416                            *(out++) = value >> 16;
417                            *(out++) = value >> 8;
418                            *(out++) = value;
419                    }
420                    else
421                    {
422                            *(out++) = value;
423                            *(out++) = value >> 8;
424                            *(out++) = value >> 16;
425                    }
426            }
427    }
428    
429    static void
430    translate15to32(uint16 * data, uint8 * out, uint8 * end)
431  {  {
432          switch (bpp)          uint16 pixel;
433            uint32 value;
434    
435            while (out < end)
436          {          {
437                  case 16:                  pixel = *(data++);
                         if (host_be != xserver_be)  
                                 BSWAP16(colour);  
                         break;  
438    
439                  case 24:                  if (g_host_be)
440                          if (xserver_be)                  {
441                                  BSWAP24(colour);                          BSWAP16(pixel);
442                          break;                  }
443    
444                  case 32:                  value = make_colour32(split_colour15(pixel));
445                          if (host_be != xserver_be)  
446                                  BSWAP32(colour);                  if (g_xserver_be)
447                          break;                  {
448                            *(out++) = value >> 24;
449                            *(out++) = value >> 16;
450                            *(out++) = value >> 8;
451                            *(out++) = value;
452                    }
453                    else
454                    {
455                            *(out++) = value;
456                            *(out++) = value >> 8;
457                            *(out++) = value >> 16;
458                            *(out++) = value >> 24;
459                    }
460          }          }
461    }
462    
463          return colour;  static void
464    translate16to16(uint16 * data, uint16 * out, uint16 * end)
465    {
466            uint16 value;
467    
468            if (g_xserver_be)
469            {
470                    while (out < end)
471                    {
472                            value = *data;
473                            BSWAP16(value);
474                            *out = value;
475                            data++;
476                            out++;
477                    }
478    
479            }
480            else
481            {
482                    while (out < end)
483                    {
484                            *out = *data;
485                            out++;
486                            data++;
487                    }
488            }
489  }  }
490    
491  static unsigned long  
492  init_inputmethod(void)  static void
493    translate16to24(uint16 * data, uint8 * out, uint8 * end)
494  {  {
495          unsigned long filtered_events = 0;          uint32 value;
496            uint16 pixel;
497    
498          IM = XOpenIM(display, NULL, NULL, NULL);          while (out < end)
         if (IM == NULL)  
499          {          {
500                  error("Failed to open input method\n");                  pixel = *(data++);
501    
502                    if (g_host_be)
503                    {
504                            BSWAP16(pixel);
505                    }
506    
507                    value = make_colour24(split_colour16(pixel));
508    
509                    if (g_xserver_be)
510                    {
511                            *(out++) = value >> 16;
512                            *(out++) = value >> 8;
513                            *(out++) = value;
514                    }
515                    else
516                    {
517                            *(out++) = value;
518                            *(out++) = value >> 8;
519                            *(out++) = value >> 16;
520                    }
521          }          }
522    }
523    
524    static void
525    translate16to32(uint16 * data, uint8 * out, uint8 * end)
526    {
527            uint16 pixel;
528            uint32 value;
529    
530          if (IM != NULL)          while (out < end)
531          {          {
532                  /* Must be done after XCreateWindow */                  pixel = *(data++);
533                  IC = XCreateIC(IM, XNInputStyle,  
534                                 (XIMPreeditNothing | XIMStatusNothing),                  if (g_host_be)
535                                 XNClientWindow, wnd, XNFocusWindow, wnd, NULL);                  {
536                    BSWAP16(pixel)}
537    
538                  if (IC == NULL)                  value = make_colour32(split_colour16(pixel));
539    
540                    if (g_xserver_be)
541                    {
542                            *(out++) = value >> 24;
543                            *(out++) = value >> 16;
544                            *(out++) = value >> 8;
545                            *(out++) = value;
546                    }
547                    else
548                  {                  {
549                          error("Failed to create input context\n");                          *(out++) = value;
550                          XCloseIM(IM);                          *(out++) = value >> 8;
551                          IM = NULL;                          *(out++) = value >> 16;
552                            *(out++) = value >> 24;
553                  }                  }
554          }          }
555    }
556    
557          /* For correct Multi_key/Compose processing, I guess.  static void
558             It seems to work alright anyway, though. */  translate24to16(uint8 * data, uint8 * out, uint8 * end)
559          if (IC != NULL)  {
560            uint32 pixel = 0;
561            uint16 value;
562            while (out < end)
563          {          {
564                  if (XGetICValues(IC, XNFilterEvents, &filtered_events, NULL)                  pixel = *(data++) << 16;
565                      != NULL)                  pixel |= *(data++) << 8;
566                    pixel |= *(data++);
567    
568                    value = (uint16) make_colour16(split_colour24(pixel));
569    
570                    if (g_xserver_be)
571                    {
572                            *(out++) = value >> 8;
573                            *(out++) = value;
574                    }
575                    else
576                  {                  {
577                          error("Failed to obtain XNFilterEvents value from IC\n");                          *(out++) = value;
578                          filtered_events = 0;                          *(out++) = value >> 8;
579                  }                  }
580          }          }
         return filtered_events;  
581  }  }
582    
583  static void  static void
584  close_inputmethod(void)  translate24to24(uint8 * data, uint8 * out, uint8 * end)
585  {  {
586          if (IC != NULL)          while (out < end)
587            {
588                    *(out++) = (*(data++));
589            }
590    }
591    
592    static void
593    translate24to32(uint8 * data, uint8 * out, uint8 * end)
594    {
595            while (out < end)
596          {          {
597                  XDestroyIC(IC);                  if (g_xserver_be)
                 if (IM != NULL)  
598                  {                  {
599                          XCloseIM(IM);                          *(out++) = 0x00;
600                          IM = NULL;                          *(out++) = *(data++);
601                            *(out++) = *(data++);
602                            *(out++) = *(data++);
603                  }                  }
604                    else
605                    {
606                            *(out++) = *(data++);
607                            *(out++) = *(data++);
608                            *(out++) = *(data++);
609                            *(out++) = 0x00;
610                    }
611            }
612    }
613    
614    static uint8 *
615    translate_image(int width, int height, uint8 * data)
616    {
617            int size = width * height * g_bpp / 8;
618            uint8 *out = (uint8 *) xmalloc(size);
619            uint8 *end = out + size;
620    
621            switch (g_server_bpp)
622            {
623                    case 24:
624                            switch (g_bpp)
625                            {
626                                    case 32:
627                                            translate24to32(data, out, end);
628                                            break;
629                                    case 24:
630                                            translate24to24(data, out, end);
631                                            break;
632                                    case 16:
633                                            translate24to16(data, out, end);
634                                            break;
635                            }
636                            break;
637                    case 16:
638                            switch (g_bpp)
639                            {
640                                    case 32:
641                                            translate16to32((uint16 *) data, out, end);
642                                            break;
643                                    case 24:
644                                            translate16to24((uint16 *) data, out, end);
645                                            break;
646                                    case 16:
647                                            translate16to16((uint16 *) data, (uint16 *) out,
648                                                            (uint16 *) end);
649                                            break;
650                            }
651                            break;
652                    case 15:
653                            switch (g_bpp)
654                            {
655                                    case 32:
656                                            translate15to32((uint16 *) data, out, end);
657                                            break;
658                                    case 24:
659                                            translate15to24((uint16 *) data, out, end);
660                                            break;
661                                    case 16:
662                                            translate15to16((uint16 *) data, out, end);
663                                            break;
664                            }
665                            break;
666                    case 8:
667                            switch (g_bpp)
668                            {
669                                    case 8:
670                                            translate8to8(data, out, end);
671                                            break;
672                                    case 16:
673                                            translate8to16(data, out, end);
674                                            break;
675                                    case 24:
676                                            translate8to24(data, out, end);
677                                            break;
678                                    case 32:
679                                            translate8to32(data, out, end);
680                                            break;
681                            }
682                            break;
683          }          }
684            return out;
685  }  }
686    
687    BOOL
688    get_key_state(unsigned int state, uint32 keysym)
689    {
690            int modifierpos, key, keysymMask = 0;
691            int offset;
692    
693            KeyCode keycode = XKeysymToKeycode(g_display, keysym);
694    
695            if (keycode == NoSymbol)
696                    return False;
697    
698            for (modifierpos = 0; modifierpos < 8; modifierpos++)
699            {
700                    offset = g_mod_map->max_keypermod * modifierpos;
701    
702                    for (key = 0; key < g_mod_map->max_keypermod; key++)
703                    {
704                            if (g_mod_map->modifiermap[offset + key] == keycode)
705                                    keysymMask |= 1 << modifierpos;
706                    }
707            }
708    
709            return (state & keysymMask) ? True : False;
710    }
711    
712  BOOL  BOOL
713  ui_create_window(char *title)  ui_init(void)
714  {  {
         XSetWindowAttributes attribs;  
         XClassHint *classhints;  
         XSizeHints *sizehints;  
         unsigned long input_mask;  
715          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
         Screen *screen;  
716          uint16 test;          uint16 test;
717          int i;          int i;
718    
719          display = XOpenDisplay(NULL);          g_display = XOpenDisplay(NULL);
720            if (g_display == NULL)
         if (display == NULL)  
721          {          {
722                  error("Failed to open display\n");                  error("Failed to open display: %s\n", XDisplayName(NULL));
723                  return False;                  return False;
724          }          }
725    
726          x_socket = ConnectionNumber(display);          g_x_socket = ConnectionNumber(g_display);
727          screen = DefaultScreenOfDisplay(display);          g_screen = DefaultScreenOfDisplay(g_display);
728          visual = DefaultVisualOfScreen(screen);          g_visual = DefaultVisualOfScreen(g_screen);
729          depth = DefaultDepthOfScreen(screen);          g_depth = DefaultDepthOfScreen(g_screen);
730    
731          pfm = XListPixmapFormats(display, &i);          pfm = XListPixmapFormats(g_display, &i);
732          if (pfm != NULL)          if (pfm != NULL)
733          {          {
734                  /* Use maximum bpp for this depth - this is generally                  /* Use maximum bpp for this depth - this is generally
735                     desirable, e.g. 24 bits->32 bits. */                     desirable, e.g. 24 bits->32 bits. */
736                  while (i--)                  while (i--)
737                  {                  {
738                          if ((pfm[i].depth == depth)                          if ((pfm[i].depth == g_depth) && (pfm[i].bits_per_pixel > g_bpp))
                             && (pfm[i].bits_per_pixel > bpp))  
739                          {                          {
740                                  bpp = pfm[i].bits_per_pixel;                                  g_bpp = pfm[i].bits_per_pixel;
741                          }                          }
742                  }                  }
743                  XFree(pfm);                  XFree(pfm);
744          }          }
745    
746          if (bpp < 8)          if (g_bpp < 8)
747          {          {
748                  error("Less than 8 bpp not currently supported.\n");                  error("Less than 8 bpp not currently supported.\n");
749                  XCloseDisplay(display);                  XCloseDisplay(g_display);
750                  return False;                  return False;
751          }          }
752    
753          if (depth <= 8)          /* private colour map code only works for 8 bpp */
754                  owncolmap = True;          if (g_owncolmap && (g_bpp > 8))
755          else                  g_owncolmap = False;
                 xcolmap = DefaultColormapOfScreen(screen);  
756    
757          test = 1;          if (!g_owncolmap)
758          host_be = !(BOOL) (*(uint8 *) (&test));          {
759          xserver_be = (ImageByteOrder(display) == MSBFirst);                  g_xcolmap = DefaultColormapOfScreen(g_screen);
760                    if (g_depth <= 8)
761                            warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n");
762            }
763    
764          white = WhitePixelOfScreen(screen);          g_gc = XCreateGC(g_display, RootWindowOfScreen(g_screen), 0, NULL);
         attribs.background_pixel = BlackPixelOfScreen(screen);  
         attribs.backing_store = DoesBackingStore(screen);  
765    
766          if (attribs.backing_store == NotUseful)          if (DoesBackingStore(g_screen) != Always)
767                  ownbackstore = True;                  g_ownbackstore = True;
768    
769            test = 1;
770            g_host_be = !(BOOL) (*(uint8 *) (&test));
771            g_xserver_be = (ImageByteOrder(g_display) == MSBFirst);
772            g_xserver_bgr = (g_visual->blue_mask > g_visual->red_mask);
773    
774          if (fullscreen)          /*
775             * Determine desktop size
776             */
777            if (g_width < 0)
778          {          {
779                  attribs.override_redirect = True;                  /* Percent of screen */
780                  width = WidthOfScreen(screen);                  g_height = HeightOfScreen(g_screen) * (-g_width) / 100;
781                  height = HeightOfScreen(screen);                  g_width = WidthOfScreen(g_screen) * (-g_width) / 100;
782          }          }
783          else          else if (g_width == 0)
784            {
785                    /* Fetch geometry from _NET_WORKAREA */
786                    uint32 x, y, cx, cy;
787    
788                    if (get_current_workarea(&x, &y, &cx, &cy) == 0)
789                    {
790                            g_width = cx;
791                            g_height = cy;
792                    }
793                    else
794                    {
795                            warning("Failed to get workarea: probably your window manager does not support extended hints\n");
796                            g_width = 800;
797                            g_height = 600;
798                    }
799            }
800            else if (g_fullscreen)
801          {          {
802                  attribs.override_redirect = False;                  g_width = WidthOfScreen(g_screen);
803                    g_height = HeightOfScreen(g_screen);
804          }          }
805    
806          width = (width + 3) & ~3;       /* make width a multiple of 32 bits */          /* make sure width is a multiple of 4 */
807            g_width = (g_width + 3) & ~3;
808    
809            if (g_ownbackstore)
810            {
811                    g_backstore =
812                            XCreatePixmap(g_display, RootWindowOfScreen(g_screen), g_width, g_height,
813                                          g_depth);
814    
815                    /* clear to prevent rubbish being exposed at startup */
816                    XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen));
817                    XFillRectangle(g_display, g_backstore, g_gc, 0, 0, g_width, g_height);
818            }
819    
820            g_mod_map = XGetModifierMapping(g_display);
821    
822            xkeymap_init();
823    
824            if (g_enable_compose)
825                    g_IM = XOpenIM(g_display, NULL, NULL, NULL);
826    
827            xclip_init();
828    
829            DEBUG_RDP5(("server bpp %d client bpp %d depth %d\n", g_server_bpp, g_bpp, g_depth));
830    
831            return True;
832    }
833    
834    void
835    ui_deinit(void)
836    {
837            if (g_IM != NULL)
838                    XCloseIM(g_IM);
839    
840            XFreeModifiermap(g_mod_map);
841    
842          wnd = XCreateWindow(display, RootWindowOfScreen(screen),          if (g_ownbackstore)
843                              0, 0, width, height, 0, CopyFromParent,                  XFreePixmap(g_display, g_backstore);
844                              InputOutput, CopyFromParent,  
845                              CWBackingStore | CWBackPixel | CWOverrideRedirect,          XFreeGC(g_display, g_gc);
846                              &attribs);          XCloseDisplay(g_display);
847            g_display = NULL;
848    }
849    
850    #define NULL_POINTER_MASK       "\x80"
851    #define NULL_POINTER_DATA       "\x0\x0\x0"
852            
853    BOOL
854    ui_create_window(void)
855    {
856            XSetWindowAttributes attribs;
857            XClassHint *classhints;
858            XSizeHints *sizehints;
859            int wndwidth, wndheight;
860            long input_mask, ic_input_mask;
861            XEvent xevent;
862    
863          XStoreName(display, wnd, title);          wndwidth = g_fullscreen ? WidthOfScreen(g_screen) : g_width;
864            wndheight = g_fullscreen ? HeightOfScreen(g_screen) : g_height;
865    
866            attribs.background_pixel = BlackPixelOfScreen(g_screen);
867            attribs.backing_store = g_ownbackstore ? NotUseful : Always;
868            attribs.override_redirect = g_fullscreen;
869    
870            g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), 0, 0, wndwidth, wndheight,
871                                  0, CopyFromParent, InputOutput, CopyFromParent,
872                                  CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
873    
874            XStoreName(g_display, g_wnd, g_title);
875    
876            if (g_hide_decorations)
877                    mwm_hide_decorations();
878    
879          classhints = XAllocClassHint();          classhints = XAllocClassHint();
880          if (classhints != NULL)          if (classhints != NULL)
881          {          {
882                  classhints->res_name = classhints->res_class = "rdesktop";                  classhints->res_name = classhints->res_class = "rdesktop";
883                  XSetClassHint(display, wnd, classhints);                  XSetClassHint(g_display, g_wnd, classhints);
884                  XFree(classhints);                  XFree(classhints);
885          }          }
886    
# Line 339  ui_create_window(char *title) Line 888  ui_create_window(char *title)
888          if (sizehints)          if (sizehints)
889          {          {
890                  sizehints->flags = PMinSize | PMaxSize;                  sizehints->flags = PMinSize | PMaxSize;
891                  sizehints->min_width = sizehints->max_width = width;                  sizehints->min_width = sizehints->max_width = g_width;
892                  sizehints->min_height = sizehints->max_height = height;                  sizehints->min_height = sizehints->max_height = g_height;
893                  XSetWMNormalHints(display, wnd, sizehints);                  XSetWMNormalHints(g_display, g_wnd, sizehints);
894                  XFree(sizehints);                  XFree(sizehints);
895          }          }
896    
897          xkeymap_init2();          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
898                    VisibilityChangeMask | FocusChangeMask;
899    
900          input_mask =          if (g_sendmotion)
                 KeyPressMask | KeyReleaseMask | ButtonPressMask |  
                 ButtonReleaseMask;  
         if (grab_keyboard)  
                 input_mask |= EnterWindowMask | LeaveWindowMask;  
         if (sendmotion)  
901                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
902            if (g_ownbackstore)
         if (ownbackstore)  
903                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
904            if (g_fullscreen || g_grab_keyboard)
905                    input_mask |= EnterWindowMask;
906            if (g_grab_keyboard)
907                    input_mask |= LeaveWindowMask;
908    
909          if (enable_compose)          if (g_IM != NULL)
910                  input_mask |= init_inputmethod();          {
911                    g_IC = XCreateIC(g_IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
912                                     XNClientWindow, g_wnd, XNFocusWindow, g_wnd, NULL);
913    
914          XSelectInput(display, wnd, input_mask);                  if ((g_IC != NULL)
915                        && (XGetICValues(g_IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))
916                            input_mask |= ic_input_mask;
917            }
918    
919          gc = XCreateGC(display, wnd, 0, NULL);          XSelectInput(g_display, g_wnd, input_mask);
920            XMapWindow(g_display, g_wnd);
921    
922          if (ownbackstore)          /* wait for VisibilityNotify */
923                  backstore = XCreatePixmap(display, wnd, width, height, depth);          do
924            {
925                    XMaskEvent(g_display, VisibilityChangeMask, &xevent);
926            }
927            while (xevent.type != VisibilityNotify);
928    
929            g_focused = False;
930            g_mouse_in_wnd = False;
931    
932          XMapWindow(display, wnd);          /* handle the WM_DELETE_WINDOW protocol */
933            g_protocol_atom = XInternAtom(g_display, "WM_PROTOCOLS", True);
934            g_kill_atom = XInternAtom(g_display, "WM_DELETE_WINDOW", True);
935            XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1);
936    
937            /* create invisible 1x1 cursor to be used as null cursor */
938            g_null_cursor = ui_create_cursor(0, 0, 1, 1, NULL_POINTER_MASK, NULL_POINTER_DATA);
939    
940          return True;          return True;
941  }  }
942    
943  void  void
944  ui_destroy_window()  ui_destroy_window(void)
945    {
946            ui_destroy_cursor(g_null_cursor);
947            
948            if (g_IC != NULL)
949                    XDestroyIC(g_IC);
950    
951            XDestroyWindow(g_display, g_wnd);
952    }
953    
954    void
955    xwin_toggle_fullscreen(void)
956  {  {
957          if (ownbackstore)          Pixmap contents = 0;
958                  XFreePixmap(display, backstore);  
959            if (!g_ownbackstore)
960            {
961                    /* need to save contents of window */
962                    contents = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth);
963                    XCopyArea(g_display, g_wnd, contents, g_gc, 0, 0, g_width, g_height, 0, 0);
964            }
965    
966          XFreeGC(display, gc);          ui_destroy_window();
967            g_fullscreen = !g_fullscreen;
968            ui_create_window();
969    
970          close_inputmethod();          XDefineCursor(g_display, g_wnd, g_current_cursor);
971    
972          XDestroyWindow(display, wnd);          if (!g_ownbackstore)
973          XCloseDisplay(display);          {
974          display = NULL;                  XCopyArea(g_display, contents, g_wnd, g_gc, 0, 0, g_width, g_height, 0, 0);
975                    XFreePixmap(g_display, contents);
976            }
977  }  }
978    
979  static void  /* Process all events in Xlib queue
980  xwin_process_events()     Returns 0 after user quit, 1 otherwise */
981    static int
982    xwin_process_events(void)
983  {  {
984          XEvent xevent;          XEvent xevent;
   
985          KeySym keysym;          KeySym keysym;
986          uint16 button, flags;          uint16 button, flags;
987          uint32 ev_time;          uint32 ev_time;
988          key_translation tr;          key_translation tr;
         char *ksname = NULL;  
989          char str[256];          char str[256];
990          Status status;          Status status;
991            unsigned int state;
992            Window wdummy;
993            int dummy;
994    
995          /* Refresh keyboard mapping if it has changed. This is important for          while (XPending(g_display) > 0)
            Xvnc, since it allocates keycodes dynamically */  
         if (XCheckTypedEvent(display, MappingNotify, &xevent))  
996          {          {
997                  if (xevent.xmapping.request == MappingKeyboard                  XNextEvent(g_display, &xevent);
                     || xevent.xmapping.request == MappingModifier)  
                         XRefreshKeyboardMapping(&xevent.xmapping);  
         }  
998    
999          while (XCheckMaskEvent(display, ~0, &xevent))                  if ((g_IC != NULL) && (XFilterEvent(&xevent, None) == True))
         {  
                 if (enable_compose && (XFilterEvent(&xevent, None) == True))  
1000                  {                  {
1001                          DEBUG_KBD("Filtering event\n");                          DEBUG_KBD(("Filtering event\n"));
1002                          continue;                          continue;
1003                  }                  }
1004    
                 ev_time = time(NULL);  
1005                  flags = 0;                  flags = 0;
1006    
1007                  switch (xevent.type)                  switch (xevent.type)
1008                  {                  {
1009                            case ClientMessage:
1010                                    /* the window manager told us to quit */
1011                                    if ((xevent.xclient.message_type == g_protocol_atom)
1012                                        && ((Atom) xevent.xclient.data.l[0] == g_kill_atom))
1013                                            /* Quit */
1014                                            return 0;
1015                                    break;
1016    
1017                          case KeyPress:                          case KeyPress:
1018                                  if (IC != NULL)                                  g_last_gesturetime = xevent.xkey.time;
1019                                    if (g_IC != NULL)
1020                                          /* Multi_key compatible version */                                          /* Multi_key compatible version */
1021                                  {                                  {
1022                                          XmbLookupString(IC,                                          XmbLookupString(g_IC,
1023                                                          (XKeyPressedEvent *) &                                                          &xevent.xkey, str, sizeof(str), &keysym,
                                                         xevent, str,  
                                                         sizeof(str), &keysym,  
1024                                                          &status);                                                          &status);
1025                                          if (!                                          if (!((status == XLookupKeySym) || (status == XLookupBoth)))
                                             ((status == XLookupKeySym)  
                                              || (status == XLookupBoth)))  
1026                                          {                                          {
1027                                                  error("XmbLookupString failed with status 0x%x\n", status);                                                  error("XmbLookupString failed with status 0x%x\n",
1028                                                          status);
1029                                                  break;                                                  break;
1030                                          }                                          }
1031                                  }                                  }
1032                                  else                                  else
1033                                  {                                  {
1034                                          /* Plain old XLookupString */                                          /* Plain old XLookupString */
1035                                          DEBUG_KBD                                          DEBUG_KBD(("\nNo input context, using XLookupString\n"));
                                                 ("No input context, using XLookupString\n");  
1036                                          XLookupString((XKeyEvent *) & xevent,                                          XLookupString((XKeyEvent *) & xevent,
1037                                                        str, sizeof(str),                                                        str, sizeof(str), &keysym, NULL);
                                                       &keysym, NULL);  
1038                                  }                                  }
1039    
1040                                  ksname = get_ksname(keysym);                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym,
1041                                  DEBUG_KBD                                             get_ksname(keysym)));
                                         ("\nKeyPress for (keysym 0x%lx, %s)\n",  
                                          keysym, ksname);  
1042    
1043                                  if (inhibit_key(keysym))                                  ev_time = time(NULL);
1044                                  {                                  if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))
                                         DEBUG_KBD("Inhibiting key\n");  
1045                                          break;                                          break;
                                 }  
1046    
1047                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
1048                                                             xevent.xkey.                                                             xevent.xkey.keycode, xevent.xkey.state);
                                                            keycode,  
                                                            xevent.xkey.state);  
   
                                 ensure_remote_modifiers(ev_time, tr);  
1049    
1050                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
1051                                          break;                                          break;
1052    
1053                                  rdp_send_scancode(ev_time, RDP_KEYPRESS,                                  save_remote_modifiers(tr.scancode);
1054                                                    tr.scancode);                                  ensure_remote_modifiers(ev_time, tr);
1055                                    rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
1056                                    restore_remote_modifiers(ev_time, tr.scancode);
1057    
1058                                  break;                                  break;
1059    
1060                          case KeyRelease:                          case KeyRelease:
1061                                    g_last_gesturetime = xevent.xkey.time;
1062                                  XLookupString((XKeyEvent *) & xevent, str,                                  XLookupString((XKeyEvent *) & xevent, str,
1063                                                sizeof(str), &keysym, NULL);                                                sizeof(str), &keysym, NULL);
1064    
1065                                  ksname = get_ksname(keysym);                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
1066                                  DEBUG_KBD                                             get_ksname(keysym)));
                                         ("\nKeyRelease for (keysym 0x%lx, %s)\n",  
                                          keysym, ksname);  
1067    
1068                                  if (inhibit_key(keysym))                                  ev_time = time(NULL);
1069                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False))
1070                                          break;                                          break;
1071    
1072                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
1073                                                             xevent.xkey.                                                             xevent.xkey.keycode, xevent.xkey.state);
                                                            keycode,  
                                                            xevent.xkey.state);  
1074    
1075                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
1076                                          break;                                          break;
1077    
1078                                  rdp_send_scancode(ev_time, RDP_KEYRELEASE,                                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);
                                                   tr.scancode);  
1079                                  break;                                  break;
1080    
1081                          case ButtonPress:                          case ButtonPress:
# Line 503  xwin_process_events() Line 1083  xwin_process_events()
1083                                  /* fall through */                                  /* fall through */
1084    
1085                          case ButtonRelease:                          case ButtonRelease:
1086                                  button = xkeymap_translate_button(xevent.                                  g_last_gesturetime = xevent.xbutton.time;
1087                                                                    xbutton.                                  button = xkeymap_translate_button(xevent.xbutton.button);
                                                                   button);  
1088                                  if (button == 0)                                  if (button == 0)
1089                                          break;                                          break;
1090    
1091                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  /* If win_button_size is nonzero, enable single app mode */
1092                                                 flags | button,                                  if (xevent.xbutton.y < g_win_button_size)
1093                                                 xevent.xbutton.x,                                  {
1094                                                 xevent.xbutton.y);                                          /* Stop moving window when button is released, regardless of cursor position */
1095                                            if (g_moving_wnd && (xevent.type == ButtonRelease))
1096                                                    g_moving_wnd = False;
1097    
1098                                            /*  Check from right to left: */
1099    
1100                                            if (xevent.xbutton.x >= g_width - g_win_button_size)
1101                                            {
1102                                                    /* The close button, continue */
1103                                                    ;
1104                                            }
1105                                            else if (xevent.xbutton.x >=
1106                                                     g_width - g_win_button_size * 2)
1107                                            {
1108                                                    /* The maximize/restore button. Do not send to
1109                                                       server.  It might be a good idea to change the
1110                                                       cursor or give some other visible indication
1111                                                       that rdesktop inhibited this click */
1112                                                    break;
1113                                            }
1114                                            else if (xevent.xbutton.x >=
1115                                                     g_width - g_win_button_size * 3)
1116                                            {
1117                                                    /* The minimize button. Iconify window. */
1118                                                    XIconifyWindow(g_display, g_wnd,
1119                                                                   DefaultScreen(g_display));
1120                                                    break;
1121                                            }
1122                                            else if (xevent.xbutton.x <= g_win_button_size)
1123                                            {
1124                                                    /* The system menu. Ignore. */
1125                                                    break;
1126                                            }
1127                                            else
1128                                            {
1129                                                    /* The title bar. */
1130                                                    if ((xevent.type == ButtonPress) && !g_fullscreen
1131                                                        && g_hide_decorations)
1132                                                    {
1133                                                            g_moving_wnd = True;
1134                                                            g_move_x_offset = xevent.xbutton.x;
1135                                                            g_move_y_offset = xevent.xbutton.y;
1136                                                    }
1137                                                    break;
1138    
1139                                            }
1140                                    }
1141    
1142                                    rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
1143                                                   flags | button, xevent.xbutton.x, xevent.xbutton.y);
1144                                  break;                                  break;
1145    
1146                          case MotionNotify:                          case MotionNotify:
1147                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  if (g_moving_wnd)
1148                                                 MOUSE_FLAG_MOVE,                                  {
1149                                                 xevent.xmotion.x,                                          XMoveWindow(g_display, g_wnd,
1150                                                 xevent.xmotion.y);                                                      xevent.xmotion.x_root - g_move_x_offset,
1151                                                        xevent.xmotion.y_root - g_move_y_offset);
1152                                            break;
1153                                    }
1154    
1155                                    if (g_fullscreen && !g_focused)
1156                                            XSetInputFocus(g_display, g_wnd, RevertToPointerRoot,
1157                                                           CurrentTime);
1158                                    rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
1159                                                   MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
1160                                  break;                                  break;
1161    
1162                          case FocusIn:                          case FocusIn:
1163                                  /* fall through */                                  if (xevent.xfocus.mode == NotifyGrab)
1164                          case EnterNotify:                                          break;
1165                                  if (grab_keyboard)                                  g_focused = True;
1166                                          XGrabKeyboard(display, wnd, True,                                  XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy,
1167                                                        GrabModeAsync,                                                &dummy, &dummy, &state);
1168                                                        GrabModeAsync,                                  reset_modifier_keys(state);
1169                                                        CurrentTime);                                  if (g_grab_keyboard && g_mouse_in_wnd)
1170                                            XGrabKeyboard(g_display, g_wnd, True,
1171                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
1172                                  break;                                  break;
1173    
1174                          case FocusOut:                          case FocusOut:
1175                                  /* reset keys */                                  if (xevent.xfocus.mode == NotifyUngrab)
1176                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE,                                          break;
1177                                                 KBD_FLAG_DOWN | KBD_FLAG_UP,                                  g_focused = False;
1178                                                 SCANCODE_CHAR_LCTRL, 0);                                  if (xevent.xfocus.mode == NotifyWhileGrabbed)
1179                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE,                                          XUngrabKeyboard(g_display, CurrentTime);
1180                                                 KBD_FLAG_DOWN | KBD_FLAG_UP,                                  break;
1181                                                 SCANCODE_CHAR_LALT, 0);  
1182                                  /* fall through */                          case EnterNotify:
1183                                    /* we only register for this event when in fullscreen mode */
1184                                    /* or grab_keyboard */
1185                                    g_mouse_in_wnd = True;
1186                                    if (g_fullscreen)
1187                                    {
1188                                            XSetInputFocus(g_display, g_wnd, RevertToPointerRoot,
1189                                                           CurrentTime);
1190                                            break;
1191                                    }
1192                                    if (g_focused)
1193                                            XGrabKeyboard(g_display, g_wnd, True,
1194                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
1195                                    break;
1196    
1197                          case LeaveNotify:                          case LeaveNotify:
1198                                  if (grab_keyboard)                                  /* we only register for this event when grab_keyboard */
1199                                          XUngrabKeyboard(display, CurrentTime);                                  g_mouse_in_wnd = False;
1200                                    XUngrabKeyboard(g_display, CurrentTime);
1201                                  break;                                  break;
1202    
1203                          case Expose:                          case Expose:
1204                                  XCopyArea(display, backstore, wnd, gc,                                  XCopyArea(g_display, g_backstore, g_wnd, g_gc,
1205                                            xevent.xexpose.x, xevent.xexpose.y,                                            xevent.xexpose.x, xevent.xexpose.y,
1206                                            xevent.xexpose.width,                                            xevent.xexpose.width,
1207                                            xevent.xexpose.height,                                            xevent.xexpose.height,
1208                                            xevent.xexpose.x, xevent.xexpose.y);                                            xevent.xexpose.x, xevent.xexpose.y);
1209                                  break;                                  break;
1210    
1211                            case MappingNotify:
1212                                    /* Refresh keyboard mapping if it has changed. This is important for
1213                                       Xvnc, since it allocates keycodes dynamically */
1214                                    if (xevent.xmapping.request == MappingKeyboard
1215                                        || xevent.xmapping.request == MappingModifier)
1216                                            XRefreshKeyboardMapping(&xevent.xmapping);
1217    
1218                                    if (xevent.xmapping.request == MappingModifier)
1219                                    {
1220                                            XFreeModifiermap(g_mod_map);
1221                                            g_mod_map = XGetModifierMapping(g_display);
1222                                    }
1223                                    break;
1224    
1225                                    /* clipboard stuff */
1226                            case SelectionNotify:
1227                                    xclip_handle_SelectionNotify(&xevent.xselection);
1228                                    break;
1229                            case SelectionRequest:
1230                                    xclip_handle_SelectionRequest(&xevent.xselectionrequest);
1231                                    break;
1232                            case SelectionClear:
1233                                    xclip_handle_SelectionClear();
1234                                    break;
1235                            case PropertyNotify:
1236                                    xclip_handle_PropertyNotify(&xevent.xproperty);
1237                                    break;
1238                  }                  }
1239          }          }
1240            /* Keep going */
1241            return 1;
1242  }  }
1243    
1244  void  /* Returns 0 after user quit, 1 otherwise */
1245    int
1246  ui_select(int rdp_socket)  ui_select(int rdp_socket)
1247  {  {
1248          int n = (rdp_socket > x_socket) ? rdp_socket + 1 : x_socket + 1;          int n = (rdp_socket > g_x_socket) ? rdp_socket + 1 : g_x_socket + 1;
1249          fd_set rfds;          fd_set rfds, wfds;
   
         FD_ZERO(&rfds);  
1250    
1251          while (True)          while (True)
1252          {          {
1253                    /* Process any events already waiting */
1254                    if (!xwin_process_events())
1255                            /* User quit */
1256                            return 0;
1257    
1258                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
1259                    FD_ZERO(&wfds);
1260                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
1261                  if (display != NULL)                  FD_SET(g_x_socket, &rfds);
1262    
1263    #ifdef WITH_RDPSND
1264                    /* FIXME: there should be an API for registering fds */
1265                    if (g_dsp_busy)
1266                  {                  {
1267                          FD_SET(x_socket, &rfds);                          FD_SET(g_dsp_fd, &wfds);
1268                          XFlush(display);                          n = (g_dsp_fd + 1 > n) ? g_dsp_fd + 1 : n;
1269                  }                  }
1270    #endif
1271    
1272                  switch (select(n, &rfds, NULL, NULL, NULL))                  switch (select(n, &rfds, &wfds, NULL, NULL))
1273                  {                  {
1274                          case -1:                          case -1:
1275                                  error("select: %s\n", strerror(errno));                                  error("select: %s\n", strerror(errno));
# Line 584  ui_select(int rdp_socket) Line 1278  ui_select(int rdp_socket)
1278                                  continue;                                  continue;
1279                  }                  }
1280    
                 if (FD_ISSET(x_socket, &rfds))  
                         xwin_process_events();  
   
1281                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
1282                          return;                          return 1;
1283    
1284    #ifdef WITH_RDPSND
1285                    if (g_dsp_busy && FD_ISSET(g_dsp_fd, &wfds))
1286                            wave_out_play();
1287    #endif
1288          }          }
1289  }  }
1290    
1291  void  void
1292  ui_move_pointer(int x, int y)  ui_move_pointer(int x, int y)
1293  {  {
1294          XWarpPointer(display, wnd, wnd, 0, 0, 0, 0, x, y);          XWarpPointer(g_display, g_wnd, g_wnd, 0, 0, 0, 0, x, y);
1295  }  }
1296    
1297  HBITMAP  HBITMAP
# Line 604  ui_create_bitmap(int width, int height, Line 1300  ui_create_bitmap(int width, int height,
1300          XImage *image;          XImage *image;
1301          Pixmap bitmap;          Pixmap bitmap;
1302          uint8 *tdata;          uint8 *tdata;
1303            int bitmap_pad;
1304    
1305          tdata = (owncolmap ? data : translate_image(width, height, data));          if (g_server_bpp == 8)
1306          bitmap = XCreatePixmap(display, wnd, width, height, depth);          {
1307          image = XCreateImage(display, visual, depth, ZPixmap, 0,                  bitmap_pad = 8;
1308                               (char *) tdata, width, height, 8, 0);          }
1309            else
1310            {
1311                    bitmap_pad = g_bpp;
1312    
1313                    if (g_bpp == 24)
1314                            bitmap_pad = 32;
1315            }
1316    
1317            tdata = (g_owncolmap ? data : translate_image(width, height, data));
1318            bitmap = XCreatePixmap(g_display, g_wnd, width, height, g_depth);
1319            image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1320                                 (char *) tdata, width, height, bitmap_pad, 0);
1321    
1322          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, g_gc, image, 0, 0, 0, 0, width, height);
1323    
1324          XFree(image);          XFree(image);
1325          if (!owncolmap)          if (!g_owncolmap)
1326                  xfree(tdata);                  xfree(tdata);
1327          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
1328  }  }
1329    
1330  void  void
1331  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)  
1332  {  {
1333          XImage *image;          XImage *image;
1334          uint8 *tdata;          uint8 *tdata;
1335            int bitmap_pad;
1336    
1337          tdata = (owncolmap ? data : translate_image(width, height, data));          if (g_server_bpp == 8)
1338          image = XCreateImage(display, visual, depth, ZPixmap, 0,          {
1339                               (char *) tdata, width, height, 8, 0);                  bitmap_pad = 8;
1340            }
1341            else
1342            {
1343                    bitmap_pad = g_bpp;
1344    
1345                    if (g_bpp == 24)
1346                            bitmap_pad = 32;
1347            }
1348    
1349          if (ownbackstore)          tdata = (g_owncolmap ? data : translate_image(width, height, data));
1350            image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1351                                 (char *) tdata, width, height, bitmap_pad, 0);
1352    
1353            if (g_ownbackstore)
1354          {          {
1355                  XPutImage(display, backstore, gc, image, 0, 0, x, y, cx, cy);                  XPutImage(g_display, g_backstore, g_gc, image, 0, 0, x, y, cx, cy);
1356                  XCopyArea(display, backstore, wnd, gc, x, y, cx, cy, x, y);                  XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);
1357          }          }
1358          else          else
1359          {          {
1360                  XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);                  XPutImage(g_display, g_wnd, g_gc, image, 0, 0, x, y, cx, cy);
1361          }          }
1362    
1363          XFree(image);          XFree(image);
1364          if (!owncolmap)          if (!g_owncolmap)
1365                  xfree(tdata);                  xfree(tdata);
1366  }  }
1367    
1368  void  void
1369  ui_destroy_bitmap(HBITMAP bmp)  ui_destroy_bitmap(HBITMAP bmp)
1370  {  {
1371          XFreePixmap(display, (Pixmap) bmp);          XFreePixmap(g_display, (Pixmap) bmp);
1372  }  }
1373    
1374  HGLYPH  HGLYPH
# Line 660  ui_create_glyph(int width, int height, u Line 1381  ui_create_glyph(int width, int height, u
1381    
1382          scanline = (width + 7) / 8;          scanline = (width + 7) / 8;
1383    
1384          bitmap = XCreatePixmap(display, wnd, width, height, 1);          bitmap = XCreatePixmap(g_display, g_wnd, width, height, 1);
1385          gc = XCreateGC(display, bitmap, 0, NULL);          gc = XCreateGC(g_display, bitmap, 0, NULL);
1386    
1387          image = XCreateImage(display, visual, 1, ZPixmap, 0, (char *) data,          image = XCreateImage(g_display, g_visual, 1, ZPixmap, 0, (char *) data,
1388                               width, height, 8, scanline);                               width, height, 8, scanline);
1389          image->byte_order = MSBFirst;          image->byte_order = MSBFirst;
1390          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
1391          XInitImage(image);          XInitImage(image);
1392    
1393          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, gc, image, 0, 0, 0, 0, width, height);
1394    
1395          XFree(image);          XFree(image);
1396          XFreeGC(display, gc);          XFreeGC(g_display, gc);
1397          return (HGLYPH) bitmap;          return (HGLYPH) bitmap;
1398  }  }
1399    
1400  void  void
1401  ui_destroy_glyph(HGLYPH glyph)  ui_destroy_glyph(HGLYPH glyph)
1402  {  {
1403          XFreePixmap(display, (Pixmap) glyph);          XFreePixmap(g_display, (Pixmap) glyph);
1404  }  }
1405    
1406  HCURSOR  HCURSOR
# Line 698  ui_create_cursor(unsigned int x, unsigne Line 1419  ui_create_cursor(unsigned int x, unsigne
1419          scanline = (width + 7) / 8;          scanline = (width + 7) / 8;
1420          offset = scanline * height;          offset = scanline * height;
1421    
1422          cursor = xmalloc(offset);          cursor = (uint8 *) xmalloc(offset);
1423          memset(cursor, 0, offset);          memset(cursor, 0, offset);
1424    
1425          mask = xmalloc(offset);          mask = (uint8 *) xmalloc(offset);
1426          memset(mask, 0, offset);          memset(mask, 0, offset);
1427    
1428          /* approximate AND and XOR masks with a monochrome X pointer */          /* approximate AND and XOR masks with a monochrome X pointer */
# Line 743  ui_create_cursor(unsigned int x, unsigne Line 1464  ui_create_cursor(unsigned int x, unsigne
1464          maskglyph = ui_create_glyph(width, height, mask);          maskglyph = ui_create_glyph(width, height, mask);
1465    
1466          xcursor =          xcursor =
1467                  XCreatePixmapCursor(display, (Pixmap) cursorglyph,                  XCreatePixmapCursor(g_display, (Pixmap) cursorglyph,
1468                                      (Pixmap) maskglyph, &fg, &bg, x, y);                                      (Pixmap) maskglyph, &fg, &bg, x, y);
1469    
1470          ui_destroy_glyph(maskglyph);          ui_destroy_glyph(maskglyph);
# Line 756  ui_create_cursor(unsigned int x, unsigne Line 1477  ui_create_cursor(unsigned int x, unsigne
1477  void  void
1478  ui_set_cursor(HCURSOR cursor)  ui_set_cursor(HCURSOR cursor)
1479  {  {
1480          XDefineCursor(display, wnd, (Cursor) cursor);          g_current_cursor = (Cursor) cursor;
1481            XDefineCursor(g_display, g_wnd, g_current_cursor);
1482  }  }
1483    
1484  void  void
1485  ui_destroy_cursor(HCURSOR cursor)  ui_destroy_cursor(HCURSOR cursor)
1486  {  {
1487          XFreeCursor(display, (Cursor) cursor);          XFreeCursor(g_display, (Cursor) cursor);
1488    }
1489    
1490    void
1491    ui_set_null_cursor(void)
1492    {
1493            ui_set_cursor(g_null_cursor);
1494  }  }
1495    
1496  #define MAKE_XCOLOR(xc,c) \  #define MAKE_XCOLOR(xc,c) \
# Line 771  ui_destroy_cursor(HCURSOR cursor) Line 1499  ui_destroy_cursor(HCURSOR cursor)
1499                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \
1500                  (xc)->flags = DoRed | DoGreen | DoBlue;                  (xc)->flags = DoRed | DoGreen | DoBlue;
1501    
1502    
1503  HCOLOURMAP  HCOLOURMAP
1504  ui_create_colourmap(COLOURMAP * colours)  ui_create_colourmap(COLOURMAP * colours)
1505  {  {
1506          COLOURENTRY *entry;          COLOURENTRY *entry;
1507          int i, ncolours = colours->ncolours;          int i, ncolours = colours->ncolours;
1508            if (!g_owncolmap)
1509            {
1510                    uint32 *map = (uint32 *) xmalloc(sizeof(*g_colmap) * ncolours);
1511                    XColor xentry;
1512                    XColor xc_cache[256];
1513                    uint32 colour;
1514                    int colLookup = 256;
1515                    for (i = 0; i < ncolours; i++)
1516                    {
1517                            entry = &colours->colours[i];
1518                            MAKE_XCOLOR(&xentry, entry);
1519    
1520                            if (XAllocColor(g_display, g_xcolmap, &xentry) == 0)
1521                            {
1522                                    /* Allocation failed, find closest match. */
1523                                    int j = 256;
1524                                    int nMinDist = 3 * 256 * 256;
1525                                    long nDist = nMinDist;
1526    
1527                                    /* only get the colors once */
1528                                    while (colLookup--)
1529                                    {
1530                                            xc_cache[colLookup].pixel = colLookup;
1531                                            xc_cache[colLookup].red = xc_cache[colLookup].green =
1532                                                    xc_cache[colLookup].blue = 0;
1533                                            xc_cache[colLookup].flags = 0;
1534                                            XQueryColor(g_display,
1535                                                        DefaultColormap(g_display,
1536                                                                        DefaultScreen(g_display)),
1537                                                        &xc_cache[colLookup]);
1538                                    }
1539                                    colLookup = 0;
1540    
1541          if (owncolmap)                                  /* approximate the pixel */
1542                                    while (j--)
1543                                    {
1544                                            if (xc_cache[j].flags)
1545                                            {
1546                                                    nDist = ((long) (xc_cache[j].red >> 8) -
1547                                                             (long) (xentry.red >> 8)) *
1548                                                            ((long) (xc_cache[j].red >> 8) -
1549                                                             (long) (xentry.red >> 8)) +
1550                                                            ((long) (xc_cache[j].green >> 8) -
1551                                                             (long) (xentry.green >> 8)) *
1552                                                            ((long) (xc_cache[j].green >> 8) -
1553                                                             (long) (xentry.green >> 8)) +
1554                                                            ((long) (xc_cache[j].blue >> 8) -
1555                                                             (long) (xentry.blue >> 8)) *
1556                                                            ((long) (xc_cache[j].blue >> 8) -
1557                                                             (long) (xentry.blue >> 8));
1558                                            }
1559                                            if (nDist < nMinDist)
1560                                            {
1561                                                    nMinDist = nDist;
1562                                                    xentry.pixel = j;
1563                                            }
1564                                    }
1565                            }
1566                            colour = xentry.pixel;
1567    
1568                            /* update our cache */
1569                            if (xentry.pixel < 256)
1570                            {
1571                                    xc_cache[xentry.pixel].red = xentry.red;
1572                                    xc_cache[xentry.pixel].green = xentry.green;
1573                                    xc_cache[xentry.pixel].blue = xentry.blue;
1574    
1575                            }
1576    
1577    
1578                            /* byte swap here to make translate_image faster */
1579                            map[i] = translate_colour(colour);
1580                    }
1581                    return map;
1582            }
1583            else
1584          {          {
1585                  XColor *xcolours, *xentry;                  XColor *xcolours, *xentry;
1586                  Colormap map;                  Colormap map;
1587    
1588                  xcolours = xmalloc(sizeof(XColor) * ncolours);                  xcolours = (XColor *) xmalloc(sizeof(XColor) * ncolours);
1589                  for (i = 0; i < ncolours; i++)                  for (i = 0; i < ncolours; i++)
1590                  {                  {
1591                          entry = &colours->colours[i];                          entry = &colours->colours[i];
# Line 791  ui_create_colourmap(COLOURMAP * colours) Line 1594  ui_create_colourmap(COLOURMAP * colours)
1594                          MAKE_XCOLOR(xentry, entry);                          MAKE_XCOLOR(xentry, entry);
1595                  }                  }
1596    
1597                  map = XCreateColormap(display, wnd, visual, AllocAll);                  map = XCreateColormap(g_display, g_wnd, g_visual, AllocAll);
1598                  XStoreColors(display, map, xcolours, ncolours);                  XStoreColors(g_display, map, xcolours, ncolours);
1599    
1600                  xfree(xcolours);                  xfree(xcolours);
1601                  return (HCOLOURMAP) map;                  return (HCOLOURMAP) map;
1602          }          }
         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;  
         }  
1603  }  }
1604    
1605  void  void
1606  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
1607  {  {
1608          if (owncolmap)          if (!g_owncolmap)
                 XFreeColormap(display, (Colormap) map);  
         else  
1609                  xfree(map);                  xfree(map);
1610            else
1611                    XFreeColormap(g_display, (Colormap) map);
1612  }  }
1613    
1614  void  void
1615  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
1616  {  {
1617          if (owncolmap)          if (!g_owncolmap)
1618                  XSetWindowColormap(display, wnd, (Colormap) map);          {
1619                    if (g_colmap)
1620                            xfree(g_colmap);
1621    
1622                    g_colmap = (uint32 *) map;
1623            }
1624          else          else
1625                  colmap = map;                  XSetWindowColormap(g_display, g_wnd, (Colormap) map);
1626  }  }
1627    
1628  void  void
# Line 848  ui_set_clip(int x, int y, int cx, int cy Line 1634  ui_set_clip(int x, int y, int cx, int cy
1634          rect.y = y;          rect.y = y;
1635          rect.width = cx;          rect.width = cx;
1636          rect.height = cy;          rect.height = cy;
1637          XSetClipRectangles(display, gc, 0, 0, &rect, 1, YXBanded);          XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded);
1638  }  }
1639    
1640  void  void
1641  ui_reset_clip()  ui_reset_clip(void)
1642  {  {
1643          XRectangle rect;          XRectangle rect;
1644    
1645          rect.x = 0;          rect.x = 0;
1646          rect.y = 0;          rect.y = 0;
1647          rect.width = width;          rect.width = g_width;
1648          rect.height = height;          rect.height = g_height;
1649          XSetClipRectangles(display, gc, 0, 0, &rect, 1, YXBanded);          XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded);
1650  }  }
1651    
1652  void  void
1653  ui_bell()  ui_bell(void)
1654  {  {
1655          XBell(display, 0);          XBell(g_display, 0);
1656  }  }
1657    
1658  void  void
# Line 878  ui_destblt(uint8 opcode, Line 1664  ui_destblt(uint8 opcode,
1664          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1665  }  }
1666    
1667    static uint8 hatch_patterns[] = {
1668            0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, /* 0 - bsHorizontal */
1669            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, /* 1 - bsVertical */
1670            0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, /* 2 - bsFDiagonal */
1671            0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, /* 3 - bsBDiagonal */
1672            0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08, /* 4 - bsCross */
1673            0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81  /* 5 - bsDiagCross */
1674    };
1675    
1676  void  void
1677  ui_patblt(uint8 opcode,  ui_patblt(uint8 opcode,
1678            /* dest */ int x, int y, int cx, int cy,            /* dest */ int x, int y, int cx, int cy,
# Line 895  ui_patblt(uint8 opcode, Line 1690  ui_patblt(uint8 opcode,
1690                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
1691                          break;                          break;
1692    
1693                    case 2: /* Hatch */
1694                            fill = (Pixmap) ui_create_glyph(8, 8,
1695                                                            hatch_patterns + brush->pattern[0] * 8);
1696                            SET_FOREGROUND(fgcolour);
1697                            SET_BACKGROUND(bgcolour);
1698                            XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
1699                            XSetStipple(g_display, g_gc, fill);
1700                            XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
1701                            FILL_RECTANGLE(x, y, cx, cy);
1702                            XSetFillStyle(g_display, g_gc, FillSolid);
1703                            XSetTSOrigin(g_display, g_gc, 0, 0);
1704                            ui_destroy_glyph((HGLYPH) fill);
1705                            break;
1706    
1707                  case 3: /* Pattern */                  case 3: /* Pattern */
1708                          for (i = 0; i != 8; i++)                          for (i = 0; i != 8; i++)
1709                                  ipattern[7 - i] = brush->pattern[i];                                  ipattern[7 - i] = brush->pattern[i];
# Line 902  ui_patblt(uint8 opcode, Line 1711  ui_patblt(uint8 opcode,
1711    
1712                          SET_FOREGROUND(bgcolour);                          SET_FOREGROUND(bgcolour);
1713                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(fgcolour);
1714                          XSetFillStyle(display, gc, FillOpaqueStippled);                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
1715                          XSetStipple(display, gc, fill);                          XSetStipple(g_display, g_gc, fill);
1716                          XSetTSOrigin(display, gc, brush->xorigin,                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
                                      brush->yorigin);  
1717    
1718                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
1719    
1720                          XSetFillStyle(display, gc, FillSolid);                          XSetFillStyle(g_display, g_gc, FillSolid);
1721                          XSetTSOrigin(display, gc, 0, 0);                          XSetTSOrigin(g_display, g_gc, 0, 0);
1722                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
1723                          break;                          break;
1724    
# Line 927  ui_screenblt(uint8 opcode, Line 1735  ui_screenblt(uint8 opcode,
1735               /* src */ int srcx, int srcy)               /* src */ int srcx, int srcy)
1736  {  {
1737          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1738          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
1739          if (ownbackstore)          if (g_ownbackstore)
1740                  XCopyArea(display, backstore, backstore, gc, srcx, srcy, cx,                  XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, y);
                           cy, x, y);  
1741          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1742  }  }
1743    
# Line 940  ui_memblt(uint8 opcode, Line 1747  ui_memblt(uint8 opcode,
1747            /* src */ HBITMAP src, int srcx, int srcy)            /* src */ HBITMAP src, int srcx, int srcy)
1748  {  {
1749          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1750          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(g_display, (Pixmap) src, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
1751          if (ownbackstore)          if (g_ownbackstore)
1752                  XCopyArea(display, (Pixmap) src, backstore, gc, srcx, srcy,                  XCopyArea(g_display, (Pixmap) src, g_backstore, g_gc, srcx, srcy, cx, cy, x, y);
                           cx, cy, x, y);  
1753          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1754  }  }
1755    
# Line 960  ui_triblt(uint8 opcode, Line 1766  ui_triblt(uint8 opcode,
1766          {          {
1767                  case 0x69:      /* PDSxxn */                  case 0x69:      /* PDSxxn */
1768                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);
1769                          ui_patblt(ROP2_NXOR, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_NXOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1770                          break;                          break;
1771    
1772                  case 0xb8:      /* PSDPxax */                  case 0xb8:      /* PSDPxax */
1773                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1774                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);
1775                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1776                          break;                          break;
1777    
1778                  case 0xc0:      /* PSa */                  case 0xc0:      /* PSa */
1779                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
1780                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1781                          break;                          break;
1782    
1783                  default:                  default:
# Line 991  ui_line(uint8 opcode, Line 1793  ui_line(uint8 opcode,
1793  {  {
1794          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1795          SET_FOREGROUND(pen->colour);          SET_FOREGROUND(pen->colour);
1796          XDrawLine(display, wnd, gc, startx, starty, endx, endy);          XDrawLine(g_display, g_wnd, g_gc, startx, starty, endx, endy);
1797          if (ownbackstore)          if (g_ownbackstore)
1798                  XDrawLine(display, backstore, gc, startx, starty, endx, endy);                  XDrawLine(g_display, g_backstore, g_gc, startx, starty, endx, endy);
1799          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1800  }  }
1801    
# Line 1006  ui_rect( Line 1808  ui_rect(
1808          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE(x, y, cx, cy);
1809  }  }
1810    
1811    /* warning, this function only draws on wnd or backstore, not both */
1812  void  void
1813  ui_draw_glyph(int mixmode,  ui_draw_glyph(int mixmode,
1814                /* dest */ int x, int y, int cx, int cy,                /* dest */ int x, int y, int cx, int cy,
# Line 1015  ui_draw_glyph(int mixmode, Line 1818  ui_draw_glyph(int mixmode,
1818          SET_FOREGROUND(fgcolour);          SET_FOREGROUND(fgcolour);
1819          SET_BACKGROUND(bgcolour);          SET_BACKGROUND(bgcolour);
1820    
1821          XSetFillStyle(display, gc,          XSetFillStyle(g_display, g_gc,
1822                        (mixmode ==                        (mixmode == MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);
1823                         MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);          XSetStipple(g_display, g_gc, (Pixmap) glyph);
1824          XSetStipple(display, gc, (Pixmap) glyph);          XSetTSOrigin(g_display, g_gc, x, y);
         XSetTSOrigin(display, gc, x, y);  
1825    
1826          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1827    
1828          XSetFillStyle(display, gc, FillSolid);          XSetFillStyle(g_display, g_gc, FillSolid);
1829  }  }
1830    
1831  #define DO_GLYPH(ttext,idx) \  #define DO_GLYPH(ttext,idx) \
# Line 1050  ui_draw_glyph(int mixmode, Line 1852  ui_draw_glyph(int mixmode,
1852      }\      }\
1853    if (glyph != NULL)\    if (glyph != NULL)\
1854      {\      {\
1855        ui_draw_glyph (mixmode, x + (short) glyph->offset,\        ui_draw_glyph (mixmode, x + glyph->offset,\
1856                       y + (short) glyph->baseline,\                       y + glyph->baseline,\
1857                       glyph->width, glyph->height,\                       glyph->width, glyph->height,\
1858                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\
1859        if (flags & TEXT2_IMPLICIT_X)\        if (flags & TEXT2_IMPLICIT_X)\
# Line 1073  ui_draw_text(uint8 font, uint8 flags, in Line 1875  ui_draw_text(uint8 font, uint8 flags, in
1875    
1876          if (boxcx > 1)          if (boxcx > 1)
1877          {          {
1878                  FILL_RECTANGLE(boxx, boxy, boxcx, boxcy);                  FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);
1879          }          }
1880          else if (mixmode == MIX_OPAQUE)          else if (mixmode == MIX_OPAQUE)
1881          {          {
1882                  FILL_RECTANGLE(clipx, clipy, clipcx, clipcy);                  FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);
1883          }          }
1884    
1885          /* Paint text, character by character */          /* Paint text, character by character */
# Line 1087  ui_draw_text(uint8 font, uint8 flags, in Line 1889  ui_draw_text(uint8 font, uint8 flags, in
1889                  {                  {
1890                          case 0xff:                          case 0xff:
1891                                  if (i + 2 < length)                                  if (i + 2 < length)
1892                                          cache_put_text(text[i + 1], text,                                          cache_put_text(text[i + 1], text, text[i + 2]);
                                                        text[i + 2]);  
1893                                  else                                  else
1894                                  {                                  {
1895                                          error("this shouldn't be happening\n");                                          error("this shouldn't be happening\n");
1896                                          break;                                          exit(1);
1897                                  }                                  }
1898                                  /* this will move pointer from start to first character after FF command */                                  /* this will move pointer from start to first character after FF command */
1899                                  length -= i + 3;                                  length -= i + 3;
# Line 1105  ui_draw_text(uint8 font, uint8 flags, in Line 1906  ui_draw_text(uint8 font, uint8 flags, in
1906                                  if (entry != NULL)                                  if (entry != NULL)
1907                                  {                                  {
1908                                          if ((((uint8 *) (entry->data))[1] ==                                          if ((((uint8 *) (entry->data))[1] ==
1909                                               0)                                               0) && (!(flags & TEXT2_IMPLICIT_X)))
                                             && (!(flags & TEXT2_IMPLICIT_X)))  
1910                                          {                                          {
1911                                                  if (flags & TEXT2_VERTICAL)                                                  if (flags & TEXT2_VERTICAL)
1912                                                          y += text[i + 2];                                                          y += text[i + 2];
1913                                                  else                                                  else
1914                                                          x += text[i + 2];                                                          x += text[i + 2];
1915                                          }                                          }
                                         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;  
1916                                          for (j = 0; j < entry->size; j++)                                          for (j = 0; j < entry->size; j++)
1917                                                  DO_GLYPH(((uint8 *) (entry->                                                  DO_GLYPH(((uint8 *) (entry->data)), j);
                                                                      data)),  
                                                          j);  
1918                                  }                                  }
1919                                    if (i + 2 < length)
1920                                            i += 3;
1921                                    else
1922                                            i += 2;
1923                                    length -= i;
1924                                    /* this will move pointer from start to first character after FE command */
1925                                    text = &(text[i]);
1926                                    i = 0;
1927                                  break;                                  break;
1928    
1929                          default:                          default:
# Line 1134  ui_draw_text(uint8 font, uint8 flags, in Line 1932  ui_draw_text(uint8 font, uint8 flags, in
1932                                  break;                                  break;
1933                  }                  }
1934          }          }
1935            if (g_ownbackstore)
1936            {
1937                    if (boxcx > 1)
1938                            XCopyArea(g_display, g_backstore, g_wnd, g_gc, boxx,
1939                                      boxy, boxcx, boxcy, boxx, boxy);
1940                    else
1941                            XCopyArea(g_display, g_backstore, g_wnd, g_gc, clipx,
1942                                      clipy, clipcx, clipcy, clipx, clipy);
1943            }
1944  }  }
1945    
1946  void  void
# Line 1144  ui_desktop_save(uint32 offset, int x, in Line 1949  ui_desktop_save(uint32 offset, int x, in
1949          Pixmap pix;          Pixmap pix;
1950          XImage *image;          XImage *image;
1951    
1952          if (ownbackstore)          if (g_ownbackstore)
1953          {          {
1954                  image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes,                  image = XGetImage(g_display, g_backstore, x, y, cx, cy, AllPlanes, ZPixmap);
                                   ZPixmap);  
1955          }          }
1956          else          else
1957          {          {
1958                  pix = XCreatePixmap(display, wnd, cx, cy, depth);                  pix = XCreatePixmap(g_display, g_wnd, cx, cy, g_depth);
1959                  XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);                  XCopyArea(g_display, g_wnd, pix, g_gc, x, y, cx, cy, 0, 0);
1960                  image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes,                  image = XGetImage(g_display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);
1961                                    ZPixmap);                  XFreePixmap(g_display, pix);
                 XFreePixmap(display, pix);  
1962          }          }
1963    
1964          offset *= bpp / 8;          offset *= g_bpp / 8;
1965          cache_put_desktop(offset, cx, cy, image->bytes_per_line, bpp / 8,          cache_put_desktop(offset, cx, cy, image->bytes_per_line, g_bpp / 8, (uint8 *) image->data);
                           (uint8 *) image->data);  
1966    
1967          XDestroyImage(image);          XDestroyImage(image);
1968  }  }
# Line 1171  ui_desktop_restore(uint32 offset, int x, Line 1973  ui_desktop_restore(uint32 offset, int x,
1973          XImage *image;          XImage *image;
1974          uint8 *data;          uint8 *data;
1975    
1976          offset *= bpp / 8;          offset *= g_bpp / 8;
1977          data = cache_get_desktop(offset, cx, cy, bpp / 8);          data = cache_get_desktop(offset, cx, cy, g_bpp / 8);
1978          if (data == NULL)          if (data == NULL)
1979                  return;                  return;
1980    
1981          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1982                               (char *) data, cx, cy, BitmapPad(display),                               (char *) data, cx, cy, BitmapPad(g_display), cx * g_bpp / 8);
                              cx * bpp / 8);  
1983    
1984          if (ownbackstore)          if (g_ownbackstore)
1985          {          {
1986                  XPutImage(display, backstore, gc, image, 0, 0, x, y, cx, cy);                  XPutImage(g_display, g_backstore, g_gc, image, 0, 0, x, y, cx, cy);
1987                  XCopyArea(display, backstore, wnd, gc, x, y, cx, cy, x, y);                  XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);
1988          }          }
1989          else          else
1990          {          {
1991                  XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);                  XPutImage(g_display, g_wnd, g_gc, image, 0, 0, x, y, cx, cy);
1992          }          }
1993    
1994          XFree(image);          XFree(image);

Legend:
Removed from v.80  
changed lines
  Added in v.524

  ViewVC Help
Powered by ViewVC 1.1.26