/[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 432 by matthewc, Tue Jul 1 09:31:25 2003 UTC revision 708 by jsorg71, Fri Jun 4 15:01:36 2004 UTC
# Line 20  Line 20 
20    
21  #include <X11/Xlib.h>  #include <X11/Xlib.h>
22  #include <X11/Xutil.h>  #include <X11/Xutil.h>
23    #include <unistd.h>
24    #include <sys/time.h>
25  #include <time.h>  #include <time.h>
26  #include <errno.h>  #include <errno.h>
27    #include <strings.h>
28  #include "rdesktop.h"  #include "rdesktop.h"
29  #include "xproto.h"  #include "xproto.h"
30    
31  extern int width;  extern int g_width;
32  extern int height;  extern int g_height;
33  extern BOOL sendmotion;  extern BOOL g_sendmotion;
34  extern BOOL fullscreen;  extern BOOL g_fullscreen;
35  extern BOOL grab_keyboard;  extern BOOL g_grab_keyboard;
36  extern BOOL hide_decorations;  extern BOOL g_hide_decorations;
37  extern char title[];  extern char g_title[];
38  extern int server_bpp;  extern int g_server_bpp;
39  extern int win_button_size;  extern int g_win_button_size;
40  BOOL enable_compose = False;  
41  BOOL focused;  Display *g_display;
42  BOOL mouse_in_wnd;  Time g_last_gesturetime;
43    static int g_x_socket;
44  Display *display;  static Screen *g_screen;
45  Time last_gesturetime;  Window g_wnd;
46  static int x_socket;  extern uint32 g_embed_wnd;
47  static Screen *screen;  BOOL g_enable_compose = False;
48  Window wnd;  BOOL g_Unobscured;              /* used for screenblt */
49  static GC gc;  static GC g_gc = NULL;
50  static Visual *visual;  static Visual *g_visual;
51  static int depth;  static int g_depth;
52  static int bpp;  static int g_bpp;
53  static XIM IM;  static XIM g_IM;
54  static XIC IC;  static XIC g_IC;
55  static XModifierKeymap *mod_map;  static XModifierKeymap *g_mod_map;
56  static Cursor current_cursor;  static Cursor g_current_cursor;
57  static Atom protocol_atom, kill_atom;  static HCURSOR g_null_cursor = NULL;
58    static Atom g_protocol_atom, g_kill_atom;
59    static BOOL g_focused;
60    static BOOL g_mouse_in_wnd;
61    static BOOL g_arch_match = False;       /* set to True if RGB XServer and little endian */
62    
63  /* endianness */  /* endianness */
64  static BOOL host_be;  static BOOL g_host_be;
65  static BOOL xserver_be;  static BOOL g_xserver_be;
66    static int g_red_shift_r, g_blue_shift_r, g_green_shift_r;
67    static int g_red_shift_l, g_blue_shift_l, g_green_shift_l;
68    
69  /* software backing store */  /* software backing store */
70  static BOOL ownbackstore;  extern BOOL g_ownbackstore;
71  static Pixmap backstore;  static Pixmap g_backstore = 0;
72    
73  /* Moving in single app mode */  /* Moving in single app mode */
74  static BOOL moving_wnd;  static BOOL g_moving_wnd;
75  static int move_x_offset = 0;  static int g_move_x_offset = 0;
76  static int move_y_offset = 0;  static int g_move_y_offset = 0;
77    
78    #ifdef WITH_RDPSND
79    extern int g_dsp_fd;
80    extern BOOL g_dsp_busy;
81    extern BOOL g_rdpsnd;
82    #endif
83    
84  /* MWM decorations */  /* MWM decorations */
85  #define MWM_HINTS_DECORATIONS   (1L << 1)  #define MWM_HINTS_DECORATIONS   (1L << 1)
# Line 90  PixelColour; Line 105  PixelColour;
105    
106  #define FILL_RECTANGLE(x,y,cx,cy)\  #define FILL_RECTANGLE(x,y,cx,cy)\
107  { \  { \
108          XFillRectangle(display, wnd, gc, x, y, cx, cy); \          XFillRectangle(g_display, g_wnd, g_gc, x, y, cx, cy); \
109          if (ownbackstore) \          if (g_ownbackstore) \
110                  XFillRectangle(display, backstore, gc, x, y, cx, cy); \                  XFillRectangle(g_display, g_backstore, g_gc, x, y, cx, cy); \
111  }  }
112    
113  #define FILL_RECTANGLE_BACKSTORE(x,y,cx,cy)\  #define FILL_RECTANGLE_BACKSTORE(x,y,cx,cy)\
114  { \  { \
115          XFillRectangle(display, ownbackstore ? backstore : wnd, gc, x, y, cx, cy); \          XFillRectangle(g_display, g_ownbackstore ? g_backstore : g_wnd, g_gc, x, y, cx, cy); \
116  }  }
117    
118  /* colour maps */  /* colour maps */
119  BOOL owncolmap = False;  extern BOOL g_owncolmap;
120  static Colormap xcolmap;  static Colormap g_xcolmap;
121  static uint32 *colmap;  static uint32 *g_colmap = NULL;
122    
123  #define TRANSLATE(col)          ( server_bpp != 8 ? translate_colour(col) : owncolmap ? col : translate_colour(colmap[col]) )  #define TRANSLATE(col)          ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : g_colmap[col] )
124  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));  #define SET_FOREGROUND(col)     XSetForeground(g_display, g_gc, TRANSLATE(col));
125  #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));  #define SET_BACKGROUND(col)     XSetBackground(g_display, g_gc, TRANSLATE(col));
126    
127  static int rop2_map[] = {  static int rop2_map[] = {
128          GXclear,                /* 0 */          GXclear,                /* 0 */
# Line 128  static int rop2_map[] = { Line 143  static int rop2_map[] = {
143          GXset                   /* 1 */          GXset                   /* 1 */
144  };  };
145    
146  #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]); }
147  #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); }
148    
149  static void  static void
150  mwm_hide_decorations(void)  mwm_hide_decorations(void)
# Line 142  mwm_hide_decorations(void) Line 157  mwm_hide_decorations(void)
157          motif_hints.decorations = 0;          motif_hints.decorations = 0;
158    
159          /* get the atom for the property */          /* get the atom for the property */
160          hintsatom = XInternAtom(display, "_MOTIF_WM_HINTS", False);          hintsatom = XInternAtom(g_display, "_MOTIF_WM_HINTS", False);
161          if (!hintsatom)          if (!hintsatom)
162          {          {
163                  warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n");                  warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n");
164                  return;                  return;
165          }          }
166    
167          XChangeProperty(display, wnd, hintsatom, hintsatom, 32, PropModeReplace,          XChangeProperty(g_display, g_wnd, hintsatom, hintsatom, 32, PropModeReplace,
168                          (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);                          (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
169  }  }
170    
# Line 157  static PixelColour Line 172  static PixelColour
172  split_colour15(uint32 colour)  split_colour15(uint32 colour)
173  {  {
174          PixelColour rv;          PixelColour rv;
175          rv.red = (colour & 0x7c00) >> 10;          rv.red = ((colour >> 7) & 0xf8) | ((colour >> 12) & 0x7);
176          rv.red = (rv.red * 0xff) / 0x1f;          rv.green = ((colour >> 2) & 0xf8) | ((colour >> 8) & 0x7);
177          rv.green = (colour & 0x03e0) >> 5;          rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7);
         rv.green = (rv.green * 0xff) / 0x1f;  
         rv.blue = (colour & 0x1f);  
         rv.blue = (rv.blue * 0xff) / 0x1f;  
178          return rv;          return rv;
179  }  }
180    
# Line 170  static PixelColour Line 182  static PixelColour
182  split_colour16(uint32 colour)  split_colour16(uint32 colour)
183  {  {
184          PixelColour rv;          PixelColour rv;
185          rv.red = (colour & 0xf800) >> 11;          rv.red = ((colour >> 8) & 0xf8) | ((colour >> 13) & 0x7);
186          rv.red = (rv.red * 0xff) / 0x1f;          rv.green = ((colour >> 3) & 0xfc) | ((colour >> 9) & 0x3);
187          rv.green = (colour & 0x07e0) >> 5;          rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7);
         rv.green = (rv.green * 0xff) / 0x3f;  
         rv.blue = (colour & 0x001f);  
         rv.blue = (rv.blue * 0xff) / 0x1f;  
188          return rv;          return rv;
189  }  }
190    
# Line 184  split_colour24(uint32 colour) Line 193  split_colour24(uint32 colour)
193  {  {
194          PixelColour rv;          PixelColour rv;
195          rv.blue = (colour & 0xff0000) >> 16;          rv.blue = (colour & 0xff0000) >> 16;
196          rv.green = (colour & 0xff00) >> 8;          rv.green = (colour & 0x00ff00) >> 8;
197          rv.red = (colour & 0xff);          rv.red = (colour & 0x0000ff);
198          return rv;          return rv;
199  }  }
200    
201  static uint32  static uint32
202  make_colour16(PixelColour pc)  make_colour(PixelColour pc)
203  {  {
204          pc.red = (pc.red * 0x1f) / 0xff;          return (((pc.red >> g_red_shift_r) << g_red_shift_l)
205          pc.green = (pc.green * 0x3f) / 0xff;                  | ((pc.green >> g_green_shift_r) << g_green_shift_l)
206          pc.blue = (pc.blue * 0x1f) / 0xff;                  | ((pc.blue >> g_blue_shift_r) << g_blue_shift_l));
         return (pc.red << 11) | (pc.green << 5) | pc.blue;  
 }  
   
 static uint32  
 make_colour24(PixelColour pc)  
 {  
         return (pc.red << 16) | (pc.green << 8) | pc.blue;  
 }  
   
 static uint32  
 make_colour32(PixelColour pc)  
 {  
         return (pc.red << 16) | (pc.green << 8) | pc.blue;  
207  }  }
208    
209  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
210  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | (x & 0xff00)); }
211  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
212                          x = (x << 16) | (x >> 16); }                          x = (x << 16) | (x >> 16); }
213    
214  static uint32  static uint32
215  translate_colour(uint32 colour)  translate_colour(uint32 colour)
216  {  {
217          switch (server_bpp)          PixelColour pc;
218            switch (g_server_bpp)
219          {          {
220                  case 15:                  case 15:
221                          switch (bpp)                          pc = split_colour15(colour);
                         {  
                                 case 16:  
                                         colour = make_colour16(split_colour15(colour));  
                                         break;  
                                 case 24:  
                                         colour = make_colour24(split_colour15(colour));  
                                         break;  
                                 case 32:  
                                         colour = make_colour32(split_colour15(colour));  
                                         break;  
                         }  
                         break;  
                 case 16:  
                         switch (bpp)  
                         {  
                                 case 16:  
                                         break;  
                                 case 24:  
                                         colour = make_colour24(split_colour16(colour));  
                                         break;  
                                 case 32:  
                                         colour = make_colour32(split_colour16(colour));  
                                         break;  
                         }  
222                          break;                          break;
                 case 24:  
                         switch (bpp)  
                         {  
                                 case 16:  
                                         colour = make_colour16(split_colour24(colour));  
                                         break;  
                                 case 24:  
                                         break;  
                                 case 32:  
                                         colour = make_colour32(split_colour24(colour));  
                                         break;  
                         }  
                         break;  
         }  
         switch (bpp)  
         {  
223                  case 16:                  case 16:
224                          if (host_be != xserver_be)                          pc = split_colour16(colour);
                                 BSWAP16(colour);  
225                          break;                          break;
   
226                  case 24:                  case 24:
227                          if (xserver_be)                          pc = split_colour24(colour);
                                 BSWAP24(colour);  
                         break;  
   
                 case 32:  
                         if (host_be != xserver_be)  
                                 BSWAP32(colour);  
228                          break;                          break;
229          }          }
230            return make_colour(pc);
231    }
232    
233          return colour;  #define UNROLL8(stm) { stm stm stm stm stm stm stm stm }
234    #define REPEAT(stm) \
235    { \
236            while (out <= end - 8 * 4) \
237                    UNROLL8(stm) \
238            while (out < end) \
239                    { stm } \
240  }  }
241    
242  static void  static void
243  translate8to8(uint8 * data, uint8 * out, uint8 * end)  translate8to8(uint8 * data, uint8 * out, uint8 * end)
244  {  {
245          while (out < end)          while (out < end)
246                  *(out++) = (uint8) colmap[*(data++)];                  *(out++) = (uint8) g_colmap[*(data++)];
247  }  }
248    
249  static void  static void
250  translate8to16(uint8 * data, uint16 * out, uint16 * end)  translate8to16(uint8 * data, uint8 * out, uint8 * end)
251    {
252            uint16 value;
253    
254            if (g_arch_match)
255                    REPEAT(*((uint16 *) out) = g_colmap[*(data++)];
256                           out += 2;)
257            else
258    if (g_xserver_be)
259  {  {
260          while (out < end)          while (out < end)
261                  *(out++) = (uint16) colmap[*(data++)];          {
262                    value = (uint16) g_colmap[*(data++)];
263                    *(out++) = value >> 8;
264                    *(out++) = value;
265            }
266    }
267    else
268    {
269            while (out < end)
270            {
271                    value = (uint16) g_colmap[*(data++)];
272                    *(out++) = value;
273                    *(out++) = value >> 8;
274            }
275    }
276  }  }
277    
278  /* little endian - conversion happens when colourmap is built */  /* little endian - conversion happens when colourmap is built */
# Line 302  translate8to24(uint8 * data, uint8 * out Line 281  translate8to24(uint8 * data, uint8 * out
281  {  {
282          uint32 value;          uint32 value;
283    
284            if (g_xserver_be)
285            {
286                    while (out < end)
287                    {
288                            value = g_colmap[*(data++)];
289                            *(out++) = value >> 16;
290                            *(out++) = value >> 8;
291                            *(out++) = value;
292                    }
293            }
294            else
295            {
296                    while (out < end)
297                    {
298                            value = g_colmap[*(data++)];
299                            *(out++) = value;
300                            *(out++) = value >> 8;
301                            *(out++) = value >> 16;
302                    }
303            }
304    }
305    
306    static void
307    translate8to32(uint8 * data, uint8 * out, uint8 * end)
308    {
309            uint32 value;
310    
311            if (g_arch_match)
312                    REPEAT(*((uint32 *) out) = g_colmap[*(data++)];
313                           out += 4;)
314            else
315    if (g_xserver_be)
316    {
317            while (out < end)
318            {
319                    value = g_colmap[*(data++)];
320                    *(out++) = value >> 24;
321                    *(out++) = value >> 16;
322                    *(out++) = value >> 8;
323                    *(out++) = value;
324            }
325    }
326    else
327    {
328          while (out < end)          while (out < end)
329          {          {
330                  value = colmap[*(data++)];                  value = g_colmap[*(data++)];
331                  *(out++) = value;                  *(out++) = value;
332                  *(out++) = value >> 8;                  *(out++) = value >> 8;
333                  *(out++) = value >> 16;                  *(out++) = value >> 16;
334                    *(out++) = value >> 24;
335          }          }
336  }  }
337    }
338    
339  static void  static void
340  translate8to32(uint8 * data, uint32 * out, uint32 * end)  translate15to16(uint16 * data, uint8 * out, uint8 * end)
341  {  {
342            uint16 pixel;
343            uint16 value;
344    
345          while (out < end)          while (out < end)
346                  *(out++) = colmap[*(data++)];          {
347  }                  pixel = *(data++);
348    
349  /* todo the remaining translate function might need some big endian check ?? */                  if (g_host_be)
350                    {
351                            BSWAP16(pixel);
352                    }
353    
354  static void                  value = make_colour(split_colour15(pixel));
355  translate15to16(uint16 * data, uint16 * out, uint16 * end)  
356  {                  if (g_xserver_be)
357          while (out < end)                  {
358                  *(out++) = (uint16) make_colour16(split_colour15(*(data++)));                          *(out++) = value >> 8;
359                            *(out++) = value;
360                    }
361                    else
362                    {
363                            *(out++) = value;
364                            *(out++) = value >> 8;
365                    }
366            }
367  }  }
368    
369  static void  static void
370  translate15to24(uint16 * data, uint8 * out, uint8 * end)  translate15to24(uint16 * data, uint8 * out, uint8 * end)
371  {  {
372          uint32 value;          uint32 value;
373            uint16 pixel;
374    
375          while (out < end)          while (out < end)
376          {          {
377                  value = make_colour24(split_colour15(*(data++)));                  pixel = *(data++);
378                  *(out++) = value;  
379                  *(out++) = value >> 8;                  if (g_host_be)
380                  *(out++) = value >> 16;                  {
381                            BSWAP16(pixel);
382                    }
383    
384                    value = make_colour(split_colour15(pixel));
385                    if (g_xserver_be)
386                    {
387                            *(out++) = value >> 16;
388                            *(out++) = value >> 8;
389                            *(out++) = value;
390                    }
391                    else
392                    {
393                            *(out++) = value;
394                            *(out++) = value >> 8;
395                            *(out++) = value >> 16;
396                    }
397          }          }
398  }  }
399    
400  static void  static void
401  translate15to32(uint16 * data, uint32 * out, uint32 * end)  translate15to32(uint16 * data, uint8 * out, uint8 * end)
402  {  {
403            uint16 pixel;
404            uint32 value;
405    
406          while (out < end)          while (out < end)
407                  *(out++) = make_colour32(split_colour15(*(data++)));          {
408                    pixel = *(data++);
409    
410                    if (g_host_be)
411                    {
412                            BSWAP16(pixel);
413                    }
414    
415                    value = make_colour(split_colour15(pixel));
416    
417                    if (g_xserver_be)
418                    {
419                            *(out++) = value >> 24;
420                            *(out++) = value >> 16;
421                            *(out++) = value >> 8;
422                            *(out++) = value;
423                    }
424                    else
425                    {
426                            *(out++) = value;
427                            *(out++) = value >> 8;
428                            *(out++) = value >> 16;
429                            *(out++) = value >> 24;
430                    }
431            }
432  }  }
433    
434  static void  static void
435  translate16to16(uint16 * data, uint16 * out, uint16 * end)  translate16to16(uint16 * data, uint8 * out, uint8 * end)
436  {  {
437            uint16 pixel;
438            uint16 value;
439    
440          while (out < end)          while (out < end)
441                  *(out++) = (uint16) (*(data++));          {
442  }                  pixel = *(data++);
443    
444                    if (g_host_be)
445                    {
446                            BSWAP16(pixel);
447                    }
448    
449                    value = make_colour(split_colour16(pixel));
450    
451                    if (g_xserver_be)
452                    {
453                            *(out++) = value >> 8;
454                            *(out++) = value;
455                    }
456                    else
457                    {
458                            *(out++) = value;
459                            *(out++) = value >> 8;
460                    }
461            }
462    }
463    
464  static void  static void
465  translate16to24(uint16 * data, uint8 * out, uint8 * end)  translate16to24(uint16 * data, uint8 * out, uint8 * end)
466  {  {
467          uint32 value;          uint32 value;
468            uint16 pixel;
469    
470          while (out < end)          while (out < end)
471          {          {
472                  value = make_colour24(split_colour16(*(data++)));                  pixel = *(data++);
473                  *(out++) = value;  
474                  *(out++) = value >> 8;                  if (g_host_be)
475                  *(out++) = value >> 16;                  {
476                            BSWAP16(pixel);
477                    }
478    
479                    value = make_colour(split_colour16(pixel));
480    
481                    if (g_xserver_be)
482                    {
483                            *(out++) = value >> 16;
484                            *(out++) = value >> 8;
485                            *(out++) = value;
486                    }
487                    else
488                    {
489                            *(out++) = value;
490                            *(out++) = value >> 8;
491                            *(out++) = value >> 16;
492                    }
493          }          }
494  }  }
495    
496  static void  static void
497  translate16to32(uint16 * data, uint32 * out, uint32 * end)  translate16to32(uint16 * data, uint8 * out, uint8 * end)
498  {  {
499            uint16 pixel;
500            uint32 value;
501    
502          while (out < end)          while (out < end)
503                  *(out++) = make_colour32(split_colour16(*(data++)));          {
504                    pixel = *(data++);
505    
506                    if (g_host_be)
507                    {
508                            BSWAP16(pixel);
509                    }
510    
511                    value = make_colour(split_colour16(pixel));
512    
513                    if (g_xserver_be)
514                    {
515                            *(out++) = value >> 24;
516                            *(out++) = value >> 16;
517                            *(out++) = value >> 8;
518                            *(out++) = value;
519                    }
520                    else
521                    {
522                            *(out++) = value;
523                            *(out++) = value >> 8;
524                            *(out++) = value >> 16;
525                            *(out++) = value >> 24;
526                    }
527            }
528  }  }
529    
530  static void  static void
531  translate24to16(uint8 * data, uint16 * out, uint16 * end)  translate24to16(uint8 * data, uint8 * out, uint8 * end)
532  {  {
533          uint32 pixel = 0;          uint32 pixel = 0;
534            uint16 value;
535          while (out < end)          while (out < end)
536          {          {
537                  pixel = *(data++) << 16;                  pixel = *(data++) << 16;
538                  pixel |= *(data++) << 8;                  pixel |= *(data++) << 8;
539                  pixel |= *(data++);                  pixel |= *(data++);
540                  *(out++) = (uint16) make_colour16(split_colour24(pixel));  
541                    value = (uint16) make_colour(split_colour24(pixel));
542    
543                    if (g_xserver_be)
544                    {
545                            *(out++) = value >> 8;
546                            *(out++) = value;
547                    }
548                    else
549                    {
550                            *(out++) = value;
551                            *(out++) = value >> 8;
552                    }
553          }          }
554  }  }
555    
556  static void  static void
557  translate24to24(uint8 * data, uint8 * out, uint8 * end)  translate24to24(uint8 * data, uint8 * out, uint8 * end)
558  {  {
559            uint32 pixel;
560            uint32 value;
561    
562          while (out < end)          while (out < end)
563          {          {
564                  *(out++) = (*(data++));                  pixel = *(data++) << 16;
565                    pixel |= *(data++) << 8;
566                    pixel |= *(data++);
567    
568                    value = make_colour(split_colour24(pixel));
569    
570                    if (g_xserver_be)
571                    {
572                            *(out++) = value >> 16;
573                            *(out++) = value >> 8;
574                            *(out++) = value;
575                    }
576                    else
577                    {
578                            *(out++) = value;
579                            *(out++) = value >> 8;
580                            *(out++) = value >> 16;
581                    }
582          }          }
583  }  }
584    
585  static void  static void
586  translate24to32(uint8 * data, uint32 * out, uint32 * end)  translate24to32(uint8 * data, uint8 * out, uint8 * end)
587  {  {
588          uint32 pixel = 0;          uint32 pixel;
589            uint32 value;
590    
591          while (out < end)          while (out < end)
592          {          {
593                  pixel = *(data++);                  pixel = *(data++) << 16;
594                  pixel |= *(data++) << 8;                  pixel |= *(data++) << 8;
595                  pixel |= *(data++) << 16;                  pixel |= *(data++);
596                  *(out++) = pixel;  
597                    value = make_colour(split_colour24(pixel));
598    
599                    if (g_xserver_be)
600                    {
601                            *(out++) = value >> 24;
602                            *(out++) = value >> 16;
603                            *(out++) = value >> 8;
604                            *(out++) = value;
605                    }
606                    else
607                    {
608                            *(out++) = value;
609                            *(out++) = value >> 8;
610                            *(out++) = value >> 16;
611                            *(out++) = value >> 24;
612                    }
613          }          }
614  }  }
615    
616  static uint8 *  static uint8 *
617  translate_image(int width, int height, uint8 * data)  translate_image(int width, int height, uint8 * data)
618  {  {
619          int size = width * height * bpp / 8;          int size;
620          uint8 *out = (uint8 *) xmalloc(size);          uint8 *out;
621          uint8 *end = out + size;          uint8 *end;
622    
623            /* if server and xserver bpp match, */
624            /* and arch(endian) matches, no need to translate */
625            /* just return data */
626            if (g_arch_match)
627            {
628                    if (g_depth == 15 && g_server_bpp == 15)
629                            return data;
630                    if (g_depth == 16 && g_server_bpp == 16)
631                            return data;
632            }
633    
634            size = width * height * (g_bpp / 8);
635            out = (uint8 *) xmalloc(size);
636            end = out + size;
637    
638          switch (server_bpp)          switch (g_server_bpp)
639          {          {
640                  case 24:                  case 24:
641                          switch (bpp)                          switch (g_bpp)
642                          {                          {
643                                  case 32:                                  case 32:
644                                          translate24to32(data, (uint32 *) out, (uint32 *) end);                                          translate24to32(data, out, end);
645                                          break;                                          break;
646                                  case 24:                                  case 24:
647                                          translate24to24(data, out, end);                                          translate24to24(data, out, end);
648                                          break;                                          break;
649                                  case 16:                                  case 16:
650                                          translate24to16(data, (uint16 *) out, (uint16 *) end);                                          translate24to16(data, out, end);
651                                          break;                                          break;
652                          }                          }
653                          break;                          break;
654                  case 16:                  case 16:
655                          switch (bpp)                          switch (g_bpp)
656                          {                          {
657                                  case 32:                                  case 32:
658                                          translate16to32((uint16 *) data, (uint32 *) out,                                          translate16to32((uint16 *) data, out, end);
                                                         (uint32 *) end);  
659                                          break;                                          break;
660                                  case 24:                                  case 24:
661                                          translate16to24((uint16 *) data, out, end);                                          translate16to24((uint16 *) data, out, end);
662                                          break;                                          break;
663                                  case 16:                                  case 16:
664                                          translate16to16((uint16 *) data, (uint16 *) out,                                          translate16to16((uint16 *) data, out, end);
                                                         (uint16 *) end);  
665                                          break;                                          break;
666                          }                          }
667                          break;                          break;
668                  case 15:                  case 15:
669                          switch (bpp)                          switch (g_bpp)
670                          {                          {
671                                  case 32:                                  case 32:
672                                          translate15to32((uint16 *) data, (uint32 *) out,                                          translate15to32((uint16 *) data, out, end);
                                                         (uint32 *) end);  
673                                          break;                                          break;
674                                  case 24:                                  case 24:
675                                          translate15to24((uint16 *) data, out, end);                                          translate15to24((uint16 *) data, out, end);
676                                          break;                                          break;
677                                  case 16:                                  case 16:
678                                          translate15to16((uint16 *) data, (uint16 *) out,                                          translate15to16((uint16 *) data, out, end);
                                                         (uint16 *) end);  
679                                          break;                                          break;
680                          }                          }
681                          break;                          break;
682                  case 8:                  case 8:
683                          switch (bpp)                          switch (g_bpp)
684                          {                          {
685                                  case 8:                                  case 8:
686                                          translate8to8(data, out, end);                                          translate8to8(data, out, end);
687                                          break;                                          break;
688                                  case 16:                                  case 16:
689                                          translate8to16(data, (uint16 *) out, (uint16 *) end);                                          translate8to16(data, out, end);
690                                          break;                                          break;
691                                  case 24:                                  case 24:
692                                          translate8to24(data, out, end);                                          translate8to24(data, out, end);
693                                          break;                                          break;
694                                  case 32:                                  case 32:
695                                          translate8to32(data, (uint32 *) out, (uint32 *) end);                                          translate8to32(data, out, end);
696                                          break;                                          break;
697                          }                          }
698                          break;                          break;
# Line 494  get_key_state(unsigned int state, uint32 Line 706  get_key_state(unsigned int state, uint32
706          int modifierpos, key, keysymMask = 0;          int modifierpos, key, keysymMask = 0;
707          int offset;          int offset;
708    
709          KeyCode keycode = XKeysymToKeycode(display, keysym);          KeyCode keycode = XKeysymToKeycode(g_display, keysym);
710    
711          if (keycode == NoSymbol)          if (keycode == NoSymbol)
712                  return False;                  return False;
713    
714          for (modifierpos = 0; modifierpos < 8; modifierpos++)          for (modifierpos = 0; modifierpos < 8; modifierpos++)
715          {          {
716                  offset = mod_map->max_keypermod * modifierpos;                  offset = g_mod_map->max_keypermod * modifierpos;
717    
718                  for (key = 0; key < mod_map->max_keypermod; key++)                  for (key = 0; key < g_mod_map->max_keypermod; key++)
719                  {                  {
720                          if (mod_map->modifiermap[offset + key] == keycode)                          if (g_mod_map->modifiermap[offset + key] == keycode)
721                                  keysymMask |= 1 << modifierpos;                                  keysymMask |= 1 << modifierpos;
722                  }                  }
723          }          }
# Line 513  get_key_state(unsigned int state, uint32 Line 725  get_key_state(unsigned int state, uint32
725          return (state & keysymMask) ? True : False;          return (state & keysymMask) ? True : False;
726  }  }
727    
728    static void
729    calculate_shifts(uint32 mask, int *shift_r, int *shift_l)
730    {
731            *shift_l = ffs(mask) - 1;
732            mask >>= *shift_l;
733            *shift_r = 8 - ffs(mask & ~(mask >> 1));
734    }
735    
736  BOOL  BOOL
737  ui_init(void)  ui_init(void)
738  {  {
739            XVisualInfo vi;
740          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
741          uint16 test;          uint16 test;
742          int i;          int i, screen_num, nvisuals;
743            XVisualInfo *vmatches = NULL;
744            XVisualInfo template;
745            Bool TrueColorVisual = False;
746    
747          display = XOpenDisplay(NULL);          g_display = XOpenDisplay(NULL);
748          if (display == NULL)          if (g_display == NULL)
749          {          {
750                  error("Failed to open display: %s\n", XDisplayName(NULL));                  error("Failed to open display: %s\n", XDisplayName(NULL));
751                  return False;                  return False;
752          }          }
753    
754          x_socket = ConnectionNumber(display);          screen_num = DefaultScreen(g_display);
755          screen = DefaultScreenOfDisplay(display);          g_x_socket = ConnectionNumber(g_display);
756          visual = DefaultVisualOfScreen(screen);          g_screen = ScreenOfDisplay(g_display, screen_num);
757          depth = DefaultDepthOfScreen(screen);          g_depth = DefaultDepthOfScreen(g_screen);
758    
759            /* Search for best TrueColor depth */
760            template.class = TrueColor;
761            vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &nvisuals);
762    
763          pfm = XListPixmapFormats(display, &i);          nvisuals--;
764            while (nvisuals >= 0)
765            {
766                    if ((vmatches + nvisuals)->depth > g_depth)
767                    {
768                            g_depth = (vmatches + nvisuals)->depth;
769                    }
770                    nvisuals--;
771                    TrueColorVisual = True;
772            }
773    
774            test = 1;
775            g_host_be = !(BOOL) (*(uint8 *) (&test));
776            g_xserver_be = (ImageByteOrder(g_display) == MSBFirst);
777    
778            if ((g_server_bpp == 8) && ((!TrueColorVisual) || (g_depth <= 8)))
779            {
780                    /* we use a colourmap, so the default visual should do */
781                    g_visual = DefaultVisualOfScreen(g_screen);
782                    g_depth = DefaultDepthOfScreen(g_screen);
783    
784                    /* Do not allocate colours on a TrueColor visual */
785                    if (g_visual->class == TrueColor)
786                    {
787                            g_owncolmap = False;
788                    }
789            }
790            else
791            {
792                    /* need a truecolour visual */
793                    if (!XMatchVisualInfo(g_display, screen_num, g_depth, TrueColor, &vi))
794                    {
795                            error("The display does not support true colour - high colour support unavailable.\n");
796                            return False;
797                    }
798    
799                    g_visual = vi.visual;
800                    g_owncolmap = False;
801                    calculate_shifts(vi.red_mask, &g_red_shift_r, &g_red_shift_l);
802                    calculate_shifts(vi.blue_mask, &g_blue_shift_r, &g_blue_shift_l);
803                    calculate_shifts(vi.green_mask, &g_green_shift_r, &g_green_shift_l);
804    
805                    /* if RGB video and averything is little endian */
806                    if (vi.red_mask > vi.green_mask && vi.green_mask > vi.blue_mask)
807                            if (!g_xserver_be && !g_host_be)
808                                    g_arch_match = True;
809            }
810    
811            pfm = XListPixmapFormats(g_display, &i);
812          if (pfm != NULL)          if (pfm != NULL)
813          {          {
814                  /* Use maximum bpp for this depth - this is generally                  /* Use maximum bpp for this depth - this is generally
815                     desirable, e.g. 24 bits->32 bits. */                     desirable, e.g. 24 bits->32 bits. */
816                  while (i--)                  while (i--)
817                  {                  {
818                          if ((pfm[i].depth == depth) && (pfm[i].bits_per_pixel > bpp))                          if ((pfm[i].depth == g_depth) && (pfm[i].bits_per_pixel > g_bpp))
819                          {                          {
820                                  bpp = pfm[i].bits_per_pixel;                                  g_bpp = pfm[i].bits_per_pixel;
821                          }                          }
822                  }                  }
823                  XFree(pfm);                  XFree(pfm);
824          }          }
825    
826          if (bpp < 8)          if (g_bpp < 8)
827          {          {
828                  error("Less than 8 bpp not currently supported.\n");                  error("Less than 8 bpp not currently supported.\n");
829                  XCloseDisplay(display);                  XCloseDisplay(g_display);
830                  return False;                  return False;
831          }          }
832    
833          if (owncolmap != True)          if (!g_owncolmap)
834          {          {
835                  xcolmap = DefaultColormapOfScreen(screen);                  g_xcolmap =
836                  if (depth <= 8)                          XCreateColormap(g_display, RootWindowOfScreen(g_screen), g_visual,
837                                            AllocNone);
838                    if (g_depth <= 8)
839                          warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n");                          warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n");
840          }          }
841    
842          gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL);          if ((!g_ownbackstore) && (DoesBackingStore(g_screen) != Always))
843            {
844          if (DoesBackingStore(screen) != Always)                  warning("External BackingStore not available, using internal\n");
845                  ownbackstore = True;                  g_ownbackstore = True;
846            }
         test = 1;  
         host_be = !(BOOL) (*(uint8 *) (&test));  
         xserver_be = (ImageByteOrder(display) == MSBFirst);  
847    
848          if ((width == 0) || (height == 0))          /*
849             * Determine desktop size
850             */
851            if (g_fullscreen)
852            {
853                    g_width = WidthOfScreen(g_screen);
854                    g_height = HeightOfScreen(g_screen);
855            }
856            else if (g_width < 0)
857            {
858                    /* Percent of screen */
859                    g_height = HeightOfScreen(g_screen) * (-g_width) / 100;
860                    g_width = WidthOfScreen(g_screen) * (-g_width) / 100;
861            }
862            else if (g_width == 0)
863          {          {
864                  /* Fetch geometry from _NET_WORKAREA */                  /* Fetch geometry from _NET_WORKAREA */
865                  uint32 x, y, cx, cy;                  uint32 x, y, cx, cy;
866    
867                  if (get_current_workarea(&x, &y, &cx, &cy) == 0)                  if (get_current_workarea(&x, &y, &cx, &cy) == 0)
868                  {                  {
869                          width = cx;                          g_width = cx;
870                          height = cy;                          g_height = cy;
871                  }                  }
872                  else                  else
873                  {                  {
874                          warning("Failed to get workarea: probably your window manager does not support extended hints\n");                          warning("Failed to get workarea: probably your window manager does not support extended hints\n");
875                          width = 800;                          g_width = 800;
876                          height = 600;                          g_height = 600;
877                  }                  }
878          }          }
879    
         if (fullscreen)  
         {  
                 width = WidthOfScreen(screen);  
                 height = HeightOfScreen(screen);  
         }  
   
880          /* make sure width is a multiple of 4 */          /* make sure width is a multiple of 4 */
881          width = (width + 3) & ~3;          g_width = (g_width + 3) & ~3;
   
         if (ownbackstore)  
         {  
                 backstore =  
                         XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth);  
882    
883                  /* clear to prevent rubbish being exposed at startup */          g_mod_map = XGetModifierMapping(g_display);
                 XSetForeground(display, gc, BlackPixelOfScreen(screen));  
                 XFillRectangle(display, backstore, gc, 0, 0, width, height);  
         }  
884    
885          mod_map = XGetModifierMapping(display);          xkeymap_init();
886    
887          if (enable_compose)          if (g_enable_compose)
888                  IM = XOpenIM(display, NULL, NULL, NULL);                  g_IM = XOpenIM(g_display, NULL, NULL, NULL);
889    
         xkeymap_init();  
890          xclip_init();          xclip_init();
891    
892          /* todo take this out when high colour is done */          DEBUG_RDP5(("server bpp %d client bpp %d depth %d\n", g_server_bpp, g_bpp, g_depth));
         printf("server bpp %d client bpp %d depth %d\n", server_bpp, bpp, depth);  
893    
894          return True;          return True;
895  }  }
# Line 624  ui_init(void) Line 897  ui_init(void)
897  void  void
898  ui_deinit(void)  ui_deinit(void)
899  {  {
900          if (IM != NULL)          if (g_IM != NULL)
901                  XCloseIM(IM);                  XCloseIM(g_IM);
902    
903          XFreeModifiermap(mod_map);          if (g_null_cursor != NULL)
904                    ui_destroy_cursor(g_null_cursor);
905    
906          if (ownbackstore)          XFreeModifiermap(g_mod_map);
                 XFreePixmap(display, backstore);  
907    
908          XFreeGC(display, gc);          if (g_ownbackstore)
909          XCloseDisplay(display);                  XFreePixmap(g_display, g_backstore);
910          display = NULL;  
911            XFreeGC(g_display, g_gc);
912            XCloseDisplay(g_display);
913            g_display = NULL;
914  }  }
915    
916  BOOL  BOOL
917  ui_create_window(void)  ui_create_window(void)
918  {  {
919            uint8 null_pointer_mask[1] = { 0x80 };
920            uint8 null_pointer_data[4] = { 0x00, 0x00, 0x00, 0x00 };
921          XSetWindowAttributes attribs;          XSetWindowAttributes attribs;
922          XClassHint *classhints;          XClassHint *classhints;
923          XSizeHints *sizehints;          XSizeHints *sizehints;
# Line 647  ui_create_window(void) Line 925  ui_create_window(void)
925          long input_mask, ic_input_mask;          long input_mask, ic_input_mask;
926          XEvent xevent;          XEvent xevent;
927    
928          wndwidth = fullscreen ? WidthOfScreen(screen) : width;          wndwidth = g_fullscreen ? WidthOfScreen(g_screen) : g_width;
929          wndheight = fullscreen ? HeightOfScreen(screen) : height;          wndheight = g_fullscreen ? HeightOfScreen(g_screen) : g_height;
930    
931            attribs.background_pixel = BlackPixelOfScreen(g_screen);
932            attribs.border_pixel = WhitePixelOfScreen(g_screen);
933            attribs.backing_store = g_ownbackstore ? NotUseful : Always;
934            attribs.override_redirect = g_fullscreen;
935            attribs.colormap = g_xcolmap;
936    
937            g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), 0, 0, wndwidth, wndheight,
938                                  0, g_depth, InputOutput, g_visual,
939                                  CWBackPixel | CWBackingStore | CWOverrideRedirect |
940                                  CWColormap | CWBorderPixel, &attribs);
941    
942          attribs.background_pixel = BlackPixelOfScreen(screen);          if (g_gc == NULL)
943          attribs.backing_store = ownbackstore ? NotUseful : Always;                  g_gc = XCreateGC(g_display, g_wnd, 0, NULL);
         attribs.override_redirect = fullscreen;  
944    
945          wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight,          if ((g_ownbackstore) && (g_backstore == 0))
946                              0, CopyFromParent, InputOutput, CopyFromParent,          {
947                              CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);                  g_backstore = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth);
948    
949                    /* clear to prevent rubbish being exposed at startup */
950                    XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen));
951                    XFillRectangle(g_display, g_backstore, g_gc, 0, 0, g_width, g_height);
952            }
953    
954          XStoreName(display, wnd, title);          XStoreName(g_display, g_wnd, g_title);
955    
956          if (hide_decorations)          if (g_hide_decorations)
957                  mwm_hide_decorations();                  mwm_hide_decorations();
958    
959          classhints = XAllocClassHint();          classhints = XAllocClassHint();
960          if (classhints != NULL)          if (classhints != NULL)
961          {          {
962                  classhints->res_name = classhints->res_class = "rdesktop";                  classhints->res_name = classhints->res_class = "rdesktop";
963                  XSetClassHint(display, wnd, classhints);                  XSetClassHint(g_display, g_wnd, classhints);
964                  XFree(classhints);                  XFree(classhints);
965          }          }
966    
# Line 675  ui_create_window(void) Line 968  ui_create_window(void)
968          if (sizehints)          if (sizehints)
969          {          {
970                  sizehints->flags = PMinSize | PMaxSize;                  sizehints->flags = PMinSize | PMaxSize;
971                  sizehints->min_width = sizehints->max_width = width;                  sizehints->min_width = sizehints->max_width = g_width;
972                  sizehints->min_height = sizehints->max_height = height;                  sizehints->min_height = sizehints->max_height = g_height;
973                  XSetWMNormalHints(display, wnd, sizehints);                  XSetWMNormalHints(g_display, g_wnd, sizehints);
974                  XFree(sizehints);                  XFree(sizehints);
975          }          }
976    
977            if (g_embed_wnd)
978            {
979                    XReparentWindow(g_display, g_wnd, (Window) g_embed_wnd, 0, 0);
980            }
981    
982          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
983                  VisibilityChangeMask | FocusChangeMask;                  VisibilityChangeMask | FocusChangeMask;
984    
985          if (sendmotion)          if (g_sendmotion)
986                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
987          if (ownbackstore)          if (g_ownbackstore)
988                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
989          if (fullscreen || grab_keyboard)          if (g_fullscreen || g_grab_keyboard)
990                  input_mask |= EnterWindowMask;                  input_mask |= EnterWindowMask;
991          if (grab_keyboard)          if (g_grab_keyboard)
992                  input_mask |= LeaveWindowMask;                  input_mask |= LeaveWindowMask;
993    
994          if (IM != NULL)          if (g_IM != NULL)
995          {          {
996                  IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),                  g_IC = XCreateIC(g_IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
997                                 XNClientWindow, wnd, XNFocusWindow, wnd, NULL);                                   XNClientWindow, g_wnd, XNFocusWindow, g_wnd, NULL);
998    
999                  if ((IC != NULL)                  if ((g_IC != NULL)
1000                      && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))                      && (XGetICValues(g_IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))
1001                          input_mask |= ic_input_mask;                          input_mask |= ic_input_mask;
1002          }          }
1003    
1004          XSelectInput(display, wnd, input_mask);          XSelectInput(g_display, g_wnd, input_mask);
1005          XMapWindow(display, wnd);          XMapWindow(g_display, g_wnd);
1006    
1007          /* wait for VisibilityNotify */          /* wait for VisibilityNotify */
1008          do          do
1009          {          {
1010                  XMaskEvent(display, VisibilityChangeMask, &xevent);                  XMaskEvent(g_display, VisibilityChangeMask, &xevent);
1011          }          }
1012          while (xevent.type != VisibilityNotify);          while (xevent.type != VisibilityNotify);
1013            g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured;
1014    
1015          focused = False;          g_focused = False;
1016          mouse_in_wnd = False;          g_mouse_in_wnd = False;
1017    
1018          /* handle the WM_DELETE_WINDOW protocol */          /* handle the WM_DELETE_WINDOW protocol */
1019          protocol_atom = XInternAtom(display, "WM_PROTOCOLS", True);          g_protocol_atom = XInternAtom(g_display, "WM_PROTOCOLS", True);
1020          kill_atom = XInternAtom(display, "WM_DELETE_WINDOW", True);          g_kill_atom = XInternAtom(g_display, "WM_DELETE_WINDOW", True);
1021          XSetWMProtocols(display, wnd, &kill_atom, 1);          XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1);
1022    
1023            /* create invisible 1x1 cursor to be used as null cursor */
1024            if (g_null_cursor == NULL)
1025                    g_null_cursor = ui_create_cursor(0, 0, 1, 1, null_pointer_mask, null_pointer_data);
1026    
1027          return True;          return True;
1028  }  }
1029    
1030  void  void
1031    ui_resize_window()
1032    {
1033            XSizeHints *sizehints;
1034            Pixmap bs;
1035    
1036            sizehints = XAllocSizeHints();
1037            if (sizehints)
1038            {
1039                    sizehints->flags = PMinSize | PMaxSize;
1040                    sizehints->min_width = sizehints->max_width = g_width;
1041                    sizehints->min_height = sizehints->max_height = g_height;
1042                    XSetWMNormalHints(g_display, g_wnd, sizehints);
1043                    XFree(sizehints);
1044            }
1045    
1046            if (!(g_fullscreen || g_embed_wnd))
1047            {
1048                    XResizeWindow(g_display, g_wnd, g_width, g_height);
1049            }
1050    
1051            /* create new backstore pixmap */
1052            if (g_backstore != 0)
1053            {
1054                    bs = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth);
1055                    XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen));
1056                    XFillRectangle(g_display, bs, g_gc, 0, 0, g_width, g_height);
1057                    XCopyArea(g_display, g_backstore, bs, g_gc, 0, 0, g_width, g_height, 0, 0);
1058                    XFreePixmap(g_display, g_backstore);
1059                    g_backstore = bs;
1060            }
1061    }
1062    
1063    void
1064  ui_destroy_window(void)  ui_destroy_window(void)
1065  {  {
1066          if (IC != NULL)          if (g_IC != NULL)
1067                  XDestroyIC(IC);                  XDestroyIC(g_IC);
1068    
1069          XDestroyWindow(display, wnd);          XDestroyWindow(g_display, g_wnd);
1070  }  }
1071    
1072  void  void
# Line 738  xwin_toggle_fullscreen(void) Line 1074  xwin_toggle_fullscreen(void)
1074  {  {
1075          Pixmap contents = 0;          Pixmap contents = 0;
1076    
1077          if (!ownbackstore)          if (!g_ownbackstore)
1078          {          {
1079                  /* need to save contents of window */                  /* need to save contents of window */
1080                  contents = XCreatePixmap(display, wnd, width, height, depth);                  contents = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth);
1081                  XCopyArea(display, wnd, contents, gc, 0, 0, width, height, 0, 0);                  XCopyArea(g_display, g_wnd, contents, g_gc, 0, 0, g_width, g_height, 0, 0);
1082          }          }
1083    
1084          ui_destroy_window();          ui_destroy_window();
1085          fullscreen = !fullscreen;          g_fullscreen = !g_fullscreen;
1086          ui_create_window();          ui_create_window();
1087    
1088          XDefineCursor(display, wnd, current_cursor);          XDefineCursor(g_display, g_wnd, g_current_cursor);
1089    
1090          if (!ownbackstore)          if (!g_ownbackstore)
1091          {          {
1092                  XCopyArea(display, contents, wnd, gc, 0, 0, width, height, 0, 0);                  XCopyArea(g_display, contents, g_wnd, g_gc, 0, 0, g_width, g_height, 0, 0);
1093                  XFreePixmap(display, contents);                  XFreePixmap(g_display, contents);
1094          }          }
1095  }  }
1096    
1097  /* Process all events in Xlib queue  /* Process all events in Xlib queue
1098     Returns 0 after user quit, 1 otherwise */     Returns 0 after user quit, 1 otherwise */
1099  static int  static int
1100  xwin_process_events(void)  xwin_process_events(void)
# Line 770  xwin_process_events(void) Line 1106  xwin_process_events(void)
1106          key_translation tr;          key_translation tr;
1107          char str[256];          char str[256];
1108          Status status;          Status status;
         unsigned int state;  
         Window wdummy;  
         int dummy;  
1109    
1110          while (XPending(display) > 0)          while (XPending(g_display) > 0)
1111          {          {
1112                  XNextEvent(display, &xevent);                  XNextEvent(g_display, &xevent);
1113    
1114                  if ((IC != NULL) && (XFilterEvent(&xevent, None) == True))                  if ((g_IC != NULL) && (XFilterEvent(&xevent, None) == True))
1115                  {                  {
1116                          DEBUG_KBD(("Filtering event\n"));                          DEBUG_KBD(("Filtering event\n"));
1117                          continue;                          continue;
# Line 788  xwin_process_events(void) Line 1121  xwin_process_events(void)
1121    
1122                  switch (xevent.type)                  switch (xevent.type)
1123                  {                  {
1124                            case VisibilityNotify:
1125                                    g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured;
1126                                    break;
1127                          case ClientMessage:                          case ClientMessage:
1128                                  /* the window manager told us to quit */                                  /* the window manager told us to quit */
1129                                  if ((xevent.xclient.message_type == protocol_atom)                                  if ((xevent.xclient.message_type == g_protocol_atom)
1130                                      && ((Atom) xevent.xclient.data.l[0] == kill_atom))                                      && ((Atom) xevent.xclient.data.l[0] == g_kill_atom))
1131                                          /* Quit */                                          /* Quit */
1132                                          return 0;                                          return 0;
1133                                  break;                                  break;
1134    
1135                          case KeyPress:                          case KeyPress:
1136                                  last_gesturetime = xevent.xkey.time;                                  g_last_gesturetime = xevent.xkey.time;
1137                                  if (IC != NULL)                                  if (g_IC != NULL)
1138                                          /* Multi_key compatible version */                                          /* Multi_key compatible version */
1139                                  {                                  {
1140                                          XmbLookupString(IC,                                          XmbLookupString(g_IC,
1141                                                          &xevent.xkey, str, sizeof(str), &keysym, &status);                                                          &xevent.xkey, str, sizeof(str), &keysym,
1142                                                            &status);
1143                                          if (!((status == XLookupKeySym) || (status == XLookupBoth)))                                          if (!((status == XLookupKeySym) || (status == XLookupBoth)))
1144                                          {                                          {
1145                                                  error("XmbLookupString failed with status 0x%x\n",                                                  error("XmbLookupString failed with status 0x%x\n",
# Line 831  xwin_process_events(void) Line 1168  xwin_process_events(void)
1168                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
1169                                          break;                                          break;
1170    
1171                                    save_remote_modifiers(tr.scancode);
1172                                  ensure_remote_modifiers(ev_time, tr);                                  ensure_remote_modifiers(ev_time, tr);
   
1173                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
1174                                    restore_remote_modifiers(ev_time, tr.scancode);
1175    
1176                                  break;                                  break;
1177    
1178                          case KeyRelease:                          case KeyRelease:
1179                                  last_gesturetime = xevent.xkey.time;                                  g_last_gesturetime = xevent.xkey.time;
1180                                  XLookupString((XKeyEvent *) & xevent, str,                                  XLookupString((XKeyEvent *) & xevent, str,
1181                                                sizeof(str), &keysym, NULL);                                                sizeof(str), &keysym, NULL);
1182    
# Line 862  xwin_process_events(void) Line 1201  xwin_process_events(void)
1201                                  /* fall through */                                  /* fall through */
1202    
1203                          case ButtonRelease:                          case ButtonRelease:
1204                                  last_gesturetime = xevent.xbutton.time;                                  g_last_gesturetime = xevent.xbutton.time;
1205                                  button = xkeymap_translate_button(xevent.xbutton.button);                                  button = xkeymap_translate_button(xevent.xbutton.button);
1206                                  if (button == 0)                                  if (button == 0)
1207                                          break;                                          break;
1208    
1209                                  /* If win_button_size is nonzero, enable single app mode */                                  /* If win_button_size is nonzero, enable single app mode */
1210                                  if (xevent.xbutton.y < win_button_size)                                  if (xevent.xbutton.y < g_win_button_size)
1211                                  {                                  {
1212                                          /* Stop moving window when button is released, regardless of cursor position */                                          /* Stop moving window when button is released, regardless of cursor position */
1213                                          if (moving_wnd && (xevent.type == ButtonRelease))                                          if (g_moving_wnd && (xevent.type == ButtonRelease))
1214                                                  moving_wnd = False;                                                  g_moving_wnd = False;
1215    
1216                                          /*  Check from right to left: */                                          /*  Check from right to left: */
1217    
1218                                          if (xevent.xbutton.x >= width - win_button_size)                                          if (xevent.xbutton.x >= g_width - g_win_button_size)
1219                                          {                                          {
1220                                                  /* The close button, continue */                                                  /* The close button, continue */
1221                                                  ;                                                  ;
1222                                          }                                          }
1223                                          else if (xevent.xbutton.x >= width - win_button_size * 2)                                          else if (xevent.xbutton.x >=
1224                                                     g_width - g_win_button_size * 2)
1225                                          {                                          {
1226                                                  /* The maximize/restore button. Do not send to                                                  /* The maximize/restore button. Do not send to
1227                                                     server.  It might be a good idea to change the                                                     server.  It might be a good idea to change the
1228                                                     cursor or give some other visible indication                                                     cursor or give some other visible indication
1229                                                     that rdesktop inhibited this click */                                                     that rdesktop inhibited this click */
1230                                                  break;                                                  break;
1231                                          }                                          }
1232                                          else if (xevent.xbutton.x >= width - win_button_size * 3)                                          else if (xevent.xbutton.x >=
1233                                                     g_width - g_win_button_size * 3)
1234                                          {                                          {
1235                                                  /* The minimize button. Iconify window. */                                                  /* The minimize button. Iconify window. */
1236                                                  XIconifyWindow(display, wnd,                                                  XIconifyWindow(g_display, g_wnd,
1237                                                                 DefaultScreen(display));                                                                 DefaultScreen(g_display));
1238                                                  break;                                                  break;
1239                                          }                                          }
1240                                          else if (xevent.xbutton.x <= win_button_size)                                          else if (xevent.xbutton.x <= g_win_button_size)
1241                                          {                                          {
1242                                                  /* The system menu. Ignore. */                                                  /* The system menu. Ignore. */
1243                                                  break;                                                  break;
# Line 904  xwin_process_events(void) Line 1245  xwin_process_events(void)
1245                                          else                                          else
1246                                          {                                          {
1247                                                  /* The title bar. */                                                  /* The title bar. */
1248                                                  if ((xevent.type == ButtonPress) && !fullscreen                                                  if ((xevent.type == ButtonPress) && !g_fullscreen
1249                                                      && hide_decorations)                                                      && g_hide_decorations)
1250                                                  {                                                  {
1251                                                          moving_wnd = True;                                                          g_moving_wnd = True;
1252                                                          move_x_offset = xevent.xbutton.x;                                                          g_move_x_offset = xevent.xbutton.x;
1253                                                          move_y_offset = xevent.xbutton.y;                                                          g_move_y_offset = xevent.xbutton.y;
1254                                                  }                                                  }
1255                                                  break;                                                  break;
1256    
# Line 921  xwin_process_events(void) Line 1262  xwin_process_events(void)
1262                                  break;                                  break;
1263    
1264                          case MotionNotify:                          case MotionNotify:
1265                                  if (moving_wnd)                                  if (g_moving_wnd)
1266                                  {                                  {
1267                                          XMoveWindow(display, wnd,                                          XMoveWindow(g_display, g_wnd,
1268                                                      xevent.xmotion.x_root - move_x_offset,                                                      xevent.xmotion.x_root - g_move_x_offset,
1269                                                      xevent.xmotion.y_root - move_y_offset);                                                      xevent.xmotion.y_root - g_move_y_offset);
1270                                          break;                                          break;
1271                                  }                                  }
1272    
1273                                  if (fullscreen && !focused)                                  if (g_fullscreen && !g_focused)
1274                                          XSetInputFocus(display, wnd, RevertToPointerRoot,                                          XSetInputFocus(g_display, g_wnd, RevertToPointerRoot,
1275                                                         CurrentTime);                                                         CurrentTime);
1276                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
1277                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
# Line 939  xwin_process_events(void) Line 1280  xwin_process_events(void)
1280                          case FocusIn:                          case FocusIn:
1281                                  if (xevent.xfocus.mode == NotifyGrab)                                  if (xevent.xfocus.mode == NotifyGrab)
1282                                          break;                                          break;
1283                                  focused = True;                                  g_focused = True;
1284                                  XQueryPointer(display, wnd, &wdummy, &wdummy, &dummy, &dummy,                                  reset_modifier_keys();
1285                                                &dummy, &dummy, &state);                                  if (g_grab_keyboard && g_mouse_in_wnd)
1286                                  reset_modifier_keys(state);                                          XGrabKeyboard(g_display, g_wnd, True,
                                 if (grab_keyboard && mouse_in_wnd)  
                                         XGrabKeyboard(display, wnd, True,  
1287                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
1288                                  break;                                  break;
1289    
1290                          case FocusOut:                          case FocusOut:
1291                                  if (xevent.xfocus.mode == NotifyUngrab)                                  if (xevent.xfocus.mode == NotifyUngrab)
1292                                          break;                                          break;
1293                                  focused = False;                                  g_focused = False;
1294                                  if (xevent.xfocus.mode == NotifyWhileGrabbed)                                  if (xevent.xfocus.mode == NotifyWhileGrabbed)
1295                                          XUngrabKeyboard(display, CurrentTime);                                          XUngrabKeyboard(g_display, CurrentTime);
1296                                  break;                                  break;
1297    
1298                          case EnterNotify:                          case EnterNotify:
1299                                  /* we only register for this event when in fullscreen mode */                                  /* we only register for this event when in fullscreen mode */
1300                                  /* or grab_keyboard */                                  /* or grab_keyboard */
1301                                  mouse_in_wnd = True;                                  g_mouse_in_wnd = True;
1302                                  if (fullscreen)                                  if (g_fullscreen)
1303                                  {                                  {
1304                                          XSetInputFocus(display, wnd, RevertToPointerRoot,                                          XSetInputFocus(g_display, g_wnd, RevertToPointerRoot,
1305                                                         CurrentTime);                                                         CurrentTime);
1306                                          break;                                          break;
1307                                  }                                  }
1308                                  if (focused)                                  if (g_focused)
1309                                          XGrabKeyboard(display, wnd, True,                                          XGrabKeyboard(g_display, g_wnd, True,
1310                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
1311                                  break;                                  break;
1312    
1313                          case LeaveNotify:                          case LeaveNotify:
1314                                  /* we only register for this event when grab_keyboard */                                  /* we only register for this event when grab_keyboard */
1315                                  mouse_in_wnd = False;                                  g_mouse_in_wnd = False;
1316                                  XUngrabKeyboard(display, CurrentTime);                                  XUngrabKeyboard(g_display, CurrentTime);
1317                                  break;                                  break;
1318    
1319                          case Expose:                          case Expose:
1320                                  XCopyArea(display, backstore, wnd, gc,                                  XCopyArea(g_display, g_backstore, g_wnd, g_gc,
1321                                            xevent.xexpose.x, xevent.xexpose.y,                                            xevent.xexpose.x, xevent.xexpose.y,
1322                                            xevent.xexpose.width,                                            xevent.xexpose.width,
1323                                            xevent.xexpose.height,                                            xevent.xexpose.height,
# Line 994  xwin_process_events(void) Line 1333  xwin_process_events(void)
1333    
1334                                  if (xevent.xmapping.request == MappingModifier)                                  if (xevent.xmapping.request == MappingModifier)
1335                                  {                                  {
1336                                          XFreeModifiermap(mod_map);                                          XFreeModifiermap(g_mod_map);
1337                                          mod_map = XGetModifierMapping(display);                                          g_mod_map = XGetModifierMapping(g_display);
1338                                  }                                  }
1339                                  break;                                  break;
1340    
1341                          /* clipboard stuff */                                  /* clipboard stuff */
1342                          case SelectionNotify:                          case SelectionNotify:
1343                                  xclip_handle_SelectionNotify(&xevent.xselection);                                  xclip_handle_SelectionNotify(&xevent.xselection);
1344                                  break;                                  break;
# Line 1022  xwin_process_events(void) Line 1361  xwin_process_events(void)
1361  int  int
1362  ui_select(int rdp_socket)  ui_select(int rdp_socket)
1363  {  {
1364          int n = (rdp_socket > x_socket) ? rdp_socket + 1 : x_socket + 1;          int n;
1365          fd_set rfds;          fd_set rfds, wfds;
1366            struct timeval tv;
1367          FD_ZERO(&rfds);          BOOL s_timeout = False;
1368    
1369          while (True)          while (True)
1370          {          {
1371                    n = (rdp_socket > g_x_socket) ? rdp_socket : g_x_socket;
1372                  /* Process any events already waiting */                  /* Process any events already waiting */
1373                  if (!xwin_process_events())                  if (!xwin_process_events())
1374                          /* User quit */                          /* User quit */
1375                          return 0;                          return 0;
1376    
1377                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
1378                    FD_ZERO(&wfds);
1379                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
1380                  FD_SET(x_socket, &rfds);                  FD_SET(g_x_socket, &rfds);
1381    
1382                  switch (select(n, &rfds, NULL, NULL, NULL))  #ifdef WITH_RDPSND
1383                    /* FIXME: there should be an API for registering fds */
1384                    if (g_dsp_busy)
1385                    {
1386                            FD_SET(g_dsp_fd, &wfds);
1387                            n = (g_dsp_fd > n) ? g_dsp_fd : n;
1388                    }
1389    #endif
1390                    /* default timeout */
1391                    tv.tv_sec = 60;
1392                    tv.tv_usec = 0;
1393    
1394                    /* add redirection handles */
1395                    rdpdr_add_fds(&n, &rfds, &wfds, &tv, &s_timeout);
1396    
1397                    n++;
1398    
1399                    switch (select(n, &rfds, &wfds, NULL, &tv))
1400                  {                  {
1401                          case -1:                          case -1:
1402                                  error("select: %s\n", strerror(errno));                                  error("select: %s\n", strerror(errno));
1403    
1404                          case 0:                          case 0:
1405                                    /* TODO: if tv.tv_sec just times out
1406                                     * we will segfault.
1407                                     * FIXME:
1408                                     */
1409                                    //s_timeout = True;
1410                                    //rdpdr_check_fds(&rfds, &wfds, (BOOL) True);
1411                                  continue;                                  continue;
1412                  }                  }
1413    
1414                    rdpdr_check_fds(&rfds, &wfds, (BOOL) False);
1415    
1416                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
1417                          return 1;                          return 1;
1418    
1419    #ifdef WITH_RDPSND
1420                    if (g_dsp_busy && FD_ISSET(g_dsp_fd, &wfds))
1421                            wave_out_play();
1422    #endif
1423          }          }
1424  }  }
1425    
1426  void  void
1427  ui_move_pointer(int x, int y)  ui_move_pointer(int x, int y)
1428  {  {
1429          XWarpPointer(display, wnd, wnd, 0, 0, 0, 0, x, y);          XWarpPointer(g_display, g_wnd, g_wnd, 0, 0, 0, 0, x, y);
1430  }  }
1431    
1432  HBITMAP  HBITMAP
# Line 1064  ui_create_bitmap(int width, int height, Line 1435  ui_create_bitmap(int width, int height,
1435          XImage *image;          XImage *image;
1436          Pixmap bitmap;          Pixmap bitmap;
1437          uint8 *tdata;          uint8 *tdata;
1438            int bitmap_pad;
1439    
1440            if (g_server_bpp == 8)
1441            {
1442                    bitmap_pad = 8;
1443            }
1444            else
1445            {
1446                    bitmap_pad = g_bpp;
1447    
1448                    if (g_bpp == 24)
1449                            bitmap_pad = 32;
1450            }
1451    
1452          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = (g_owncolmap ? data : translate_image(width, height, data));
1453          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(g_display, g_wnd, width, height, g_depth);
1454          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1455                               (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0);                               (char *) tdata, width, height, bitmap_pad, 0);
1456    
1457          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, g_gc, image, 0, 0, 0, 0, width, height);
1458    
1459          XFree(image);          XFree(image);
1460          if (!owncolmap)          if (tdata != data)
1461                  xfree(tdata);                  xfree(tdata);
1462          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
1463  }  }
# Line 1083  ui_paint_bitmap(int x, int y, int cx, in Line 1467  ui_paint_bitmap(int x, int y, int cx, in
1467  {  {
1468          XImage *image;          XImage *image;
1469          uint8 *tdata;          uint8 *tdata;
1470          tdata = (owncolmap ? data : translate_image(width, height, data));          int bitmap_pad;
         image = XCreateImage(display, visual, depth, ZPixmap, 0,  
                              (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0);  
1471    
1472          if (ownbackstore)          if (g_server_bpp == 8)
1473          {          {
1474                  XPutImage(display, backstore, gc, image, 0, 0, x, y, cx, cy);                  bitmap_pad = 8;
                 XCopyArea(display, backstore, wnd, gc, x, y, cx, cy, x, y);  
1475          }          }
1476          else          else
1477          {          {
1478                  XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);                  bitmap_pad = g_bpp;
1479    
1480                    if (g_bpp == 24)
1481                            bitmap_pad = 32;
1482            }
1483    
1484            tdata = (g_owncolmap ? data : translate_image(width, height, data));
1485            image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1486                                 (char *) tdata, width, height, bitmap_pad, 0);
1487    
1488            if (g_ownbackstore)
1489            {
1490                    XPutImage(g_display, g_backstore, g_gc, image, 0, 0, x, y, cx, cy);
1491                    XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);
1492            }
1493            else
1494            {
1495                    XPutImage(g_display, g_wnd, g_gc, image, 0, 0, x, y, cx, cy);
1496          }          }
1497    
1498          XFree(image);          XFree(image);
1499          if (!owncolmap)          if (tdata != data)
1500                  xfree(tdata);                  xfree(tdata);
1501  }  }
1502    
1503  void  void
1504  ui_destroy_bitmap(HBITMAP bmp)  ui_destroy_bitmap(HBITMAP bmp)
1505  {  {
1506          XFreePixmap(display, (Pixmap) bmp);          XFreePixmap(g_display, (Pixmap) bmp);
1507  }  }
1508    
1509  HGLYPH  HGLYPH
# Line 1118  ui_create_glyph(int width, int height, u Line 1516  ui_create_glyph(int width, int height, u
1516    
1517          scanline = (width + 7) / 8;          scanline = (width + 7) / 8;
1518    
1519          bitmap = XCreatePixmap(display, wnd, width, height, 1);          bitmap = XCreatePixmap(g_display, g_wnd, width, height, 1);
1520          gc = XCreateGC(display, bitmap, 0, NULL);          gc = XCreateGC(g_display, bitmap, 0, NULL);
1521    
1522          image = XCreateImage(display, visual, 1, ZPixmap, 0, (char *) data,          image = XCreateImage(g_display, g_visual, 1, ZPixmap, 0, (char *) data,
1523                               width, height, 8, scanline);                               width, height, 8, scanline);
1524          image->byte_order = MSBFirst;          image->byte_order = MSBFirst;
1525          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
1526          XInitImage(image);          XInitImage(image);
1527    
1528          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, gc, image, 0, 0, 0, 0, width, height);
1529    
1530          XFree(image);          XFree(image);
1531          XFreeGC(display, gc);          XFreeGC(g_display, gc);
1532          return (HGLYPH) bitmap;          return (HGLYPH) bitmap;
1533  }  }
1534    
1535  void  void
1536  ui_destroy_glyph(HGLYPH glyph)  ui_destroy_glyph(HGLYPH glyph)
1537  {  {
1538          XFreePixmap(display, (Pixmap) glyph);          XFreePixmap(g_display, (Pixmap) glyph);
1539  }  }
1540    
1541  HCURSOR  HCURSOR
# Line 1201  ui_create_cursor(unsigned int x, unsigne Line 1599  ui_create_cursor(unsigned int x, unsigne
1599          maskglyph = ui_create_glyph(width, height, mask);          maskglyph = ui_create_glyph(width, height, mask);
1600    
1601          xcursor =          xcursor =
1602                  XCreatePixmapCursor(display, (Pixmap) cursorglyph,                  XCreatePixmapCursor(g_display, (Pixmap) cursorglyph,
1603                                      (Pixmap) maskglyph, &fg, &bg, x, y);                                      (Pixmap) maskglyph, &fg, &bg, x, y);
1604    
1605          ui_destroy_glyph(maskglyph);          ui_destroy_glyph(maskglyph);
# Line 1214  ui_create_cursor(unsigned int x, unsigne Line 1612  ui_create_cursor(unsigned int x, unsigne
1612  void  void
1613  ui_set_cursor(HCURSOR cursor)  ui_set_cursor(HCURSOR cursor)
1614  {  {
1615          current_cursor = (Cursor) cursor;          g_current_cursor = (Cursor) cursor;
1616          XDefineCursor(display, wnd, current_cursor);          XDefineCursor(g_display, g_wnd, g_current_cursor);
1617  }  }
1618    
1619  void  void
1620  ui_destroy_cursor(HCURSOR cursor)  ui_destroy_cursor(HCURSOR cursor)
1621  {  {
1622          XFreeCursor(display, (Cursor) cursor);          XFreeCursor(g_display, (Cursor) cursor);
1623    }
1624    
1625    void
1626    ui_set_null_cursor(void)
1627    {
1628            ui_set_cursor(g_null_cursor);
1629  }  }
1630    
1631  #define MAKE_XCOLOR(xc,c) \  #define MAKE_XCOLOR(xc,c) \
# Line 1236  ui_create_colourmap(COLOURMAP * colours) Line 1640  ui_create_colourmap(COLOURMAP * colours)
1640  {  {
1641          COLOURENTRY *entry;          COLOURENTRY *entry;
1642          int i, ncolours = colours->ncolours;          int i, ncolours = colours->ncolours;
1643          if (!owncolmap)          if (!g_owncolmap)
1644          {          {
1645                  uint32 *map = (uint32 *) xmalloc(sizeof(*colmap) * ncolours);                  uint32 *map = (uint32 *) xmalloc(sizeof(*g_colmap) * ncolours);
1646                  XColor xentry;                  XColor xentry;
1647                  XColor xc_cache[256];                  XColor xc_cache[256];
1648                  uint32 colour;                  uint32 colour;
# Line 1248  ui_create_colourmap(COLOURMAP * colours) Line 1652  ui_create_colourmap(COLOURMAP * colours)
1652                          entry = &colours->colours[i];                          entry = &colours->colours[i];
1653                          MAKE_XCOLOR(&xentry, entry);                          MAKE_XCOLOR(&xentry, entry);
1654    
1655                          if (XAllocColor(display, xcolmap, &xentry) == 0)                          if (XAllocColor(g_display, g_xcolmap, &xentry) == 0)
1656                          {                          {
1657                                  /* Allocation failed, find closest match. */                                  /* Allocation failed, find closest match. */
1658                                  int j = 256;                                  int j = 256;
# Line 1262  ui_create_colourmap(COLOURMAP * colours) Line 1666  ui_create_colourmap(COLOURMAP * colours)
1666                                          xc_cache[colLookup].red = xc_cache[colLookup].green =                                          xc_cache[colLookup].red = xc_cache[colLookup].green =
1667                                                  xc_cache[colLookup].blue = 0;                                                  xc_cache[colLookup].blue = 0;
1668                                          xc_cache[colLookup].flags = 0;                                          xc_cache[colLookup].flags = 0;
1669                                          XQueryColor(display,                                          XQueryColor(g_display,
1670                                                      DefaultColormap(display,                                                      DefaultColormap(g_display,
1671                                                                      DefaultScreen(display)),                                                                      DefaultScreen(g_display)),
1672                                                      &xc_cache[colLookup]);                                                      &xc_cache[colLookup]);
1673                                  }                                  }
1674                                  colLookup = 0;                                  colLookup = 0;
# Line 1305  ui_create_colourmap(COLOURMAP * colours) Line 1709  ui_create_colourmap(COLOURMAP * colours)
1709    
1710                          }                          }
1711    
1712                            map[i] = colour;
                         /* byte swap here to make translate_image faster */  
                         map[i] = translate_colour(colour);  
1713                  }                  }
1714                  return map;                  return map;
1715          }          }
# Line 1325  ui_create_colourmap(COLOURMAP * colours) Line 1727  ui_create_colourmap(COLOURMAP * colours)
1727                          MAKE_XCOLOR(xentry, entry);                          MAKE_XCOLOR(xentry, entry);
1728                  }                  }
1729    
1730                  map = XCreateColormap(display, wnd, visual, AllocAll);                  map = XCreateColormap(g_display, g_wnd, g_visual, AllocAll);
1731                  XStoreColors(display, map, xcolours, ncolours);                  XStoreColors(g_display, map, xcolours, ncolours);
1732    
1733                  xfree(xcolours);                  xfree(xcolours);
1734                  return (HCOLOURMAP) map;                  return (HCOLOURMAP) map;
# Line 1336  ui_create_colourmap(COLOURMAP * colours) Line 1738  ui_create_colourmap(COLOURMAP * colours)
1738  void  void
1739  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
1740  {  {
1741          if (!owncolmap)          if (!g_owncolmap)
1742                  xfree(map);                  xfree(map);
1743          else          else
1744                  XFreeColormap(display, (Colormap) map);                  XFreeColormap(g_display, (Colormap) map);
1745  }  }
1746    
1747  void  void
1748  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
1749  {  {
1750          if (!owncolmap)          if (!g_owncolmap)
1751                  colmap = (uint32 *) map;          {
1752                    if (g_colmap)
1753                            xfree(g_colmap);
1754    
1755                    g_colmap = (uint32 *) map;
1756            }
1757          else          else
1758                  XSetWindowColormap(display, wnd, (Colormap) map);                  XSetWindowColormap(g_display, g_wnd, (Colormap) map);
1759  }  }
1760    
1761  void  void
# Line 1360  ui_set_clip(int x, int y, int cx, int cy Line 1767  ui_set_clip(int x, int y, int cx, int cy
1767          rect.y = y;          rect.y = y;
1768          rect.width = cx;          rect.width = cx;
1769          rect.height = cy;          rect.height = cy;
1770          XSetClipRectangles(display, gc, 0, 0, &rect, 1, YXBanded);          XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded);
1771  }  }
1772    
1773  void  void
# Line 1370  ui_reset_clip(void) Line 1777  ui_reset_clip(void)
1777    
1778          rect.x = 0;          rect.x = 0;
1779          rect.y = 0;          rect.y = 0;
1780          rect.width = width;          rect.width = g_width;
1781          rect.height = height;          rect.height = g_height;
1782          XSetClipRectangles(display, gc, 0, 0, &rect, 1, YXBanded);          XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded);
1783  }  }
1784    
1785  void  void
1786  ui_bell(void)  ui_bell(void)
1787  {  {
1788          XBell(display, 0);          XBell(g_display, 0);
1789  }  }
1790    
1791  void  void
# Line 1413  ui_patblt(uint8 opcode, Line 1820  ui_patblt(uint8 opcode,
1820          {          {
1821                  case 0: /* Solid */                  case 0: /* Solid */
1822                          SET_FOREGROUND(fgcolour);                          SET_FOREGROUND(fgcolour);
1823                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1824                          break;                          break;
1825    
1826                  case 2: /* Hatch */                  case 2: /* Hatch */
1827                          fill = (Pixmap) ui_create_glyph(8, 8,                          fill = (Pixmap) ui_create_glyph(8, 8,
1828                                                          hatch_patterns + brush->pattern[0] * 8);                                                          hatch_patterns + brush->pattern[0] * 8);
1829                          SET_FOREGROUND(bgcolour);                          SET_FOREGROUND(fgcolour);
1830                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(bgcolour);
1831                          XSetFillStyle(display, gc, FillOpaqueStippled);                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
1832                          XSetStipple(display, gc, fill);                          XSetStipple(g_display, g_gc, fill);
1833                          XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin);                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
1834                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1835                          XSetFillStyle(display, gc, FillSolid);                          XSetFillStyle(g_display, g_gc, FillSolid);
1836                          XSetTSOrigin(display, gc, 0, 0);                          XSetTSOrigin(g_display, g_gc, 0, 0);
1837                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
1838                          break;                          break;
1839    
# Line 1434  ui_patblt(uint8 opcode, Line 1841  ui_patblt(uint8 opcode,
1841                          for (i = 0; i != 8; i++)                          for (i = 0; i != 8; i++)
1842                                  ipattern[7 - i] = brush->pattern[i];                                  ipattern[7 - i] = brush->pattern[i];
1843                          fill = (Pixmap) ui_create_glyph(8, 8, ipattern);                          fill = (Pixmap) ui_create_glyph(8, 8, ipattern);
   
1844                          SET_FOREGROUND(bgcolour);                          SET_FOREGROUND(bgcolour);
1845                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(fgcolour);
1846                          XSetFillStyle(display, gc, FillOpaqueStippled);                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
1847                          XSetStipple(display, gc, fill);                          XSetStipple(g_display, g_gc, fill);
1848                          XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin);                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
1849                            FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1850                          FILL_RECTANGLE(x, y, cx, cy);                          XSetFillStyle(g_display, g_gc, FillSolid);
1851                            XSetTSOrigin(g_display, g_gc, 0, 0);
                         XSetFillStyle(display, gc, FillSolid);  
                         XSetTSOrigin(display, gc, 0, 0);  
1852                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
1853                          break;                          break;
1854    
# Line 1453  ui_patblt(uint8 opcode, Line 1857  ui_patblt(uint8 opcode,
1857          }          }
1858    
1859          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1860    
1861            if (g_ownbackstore)
1862                    XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);
1863  }  }
1864    
1865  void  void
# Line 1461  ui_screenblt(uint8 opcode, Line 1868  ui_screenblt(uint8 opcode,
1868               /* src */ int srcx, int srcy)               /* src */ int srcx, int srcy)
1869  {  {
1870          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1871          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);          if (g_ownbackstore)
1872          if (ownbackstore)          {
1873                  XCopyArea(display, backstore, backstore, gc, srcx, srcy, cx, cy, x, y);                  if (g_Unobscured)
1874                    {
1875                            XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
1876                            XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x,
1877                                      y);
1878                    }
1879                    else
1880                    {
1881                            XCopyArea(g_display, g_backstore, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
1882                            XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x,
1883                                      y);
1884                    }
1885            }
1886            else
1887            {
1888                    XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
1889            }
1890          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1891  }  }
1892    
# Line 1473  ui_memblt(uint8 opcode, Line 1896  ui_memblt(uint8 opcode,
1896            /* src */ HBITMAP src, int srcx, int srcy)            /* src */ HBITMAP src, int srcx, int srcy)
1897  {  {
1898          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1899          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);
1900          if (ownbackstore)          if (g_ownbackstore)
1901                  XCopyArea(display, (Pixmap) src, backstore, gc, srcx, srcy, cx, cy, x, y);                  XCopyArea(g_display, (Pixmap) src, g_backstore, g_gc, srcx, srcy, cx, cy, x, y);
1902          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1903  }  }
1904    
# Line 1519  ui_line(uint8 opcode, Line 1942  ui_line(uint8 opcode,
1942  {  {
1943          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1944          SET_FOREGROUND(pen->colour);          SET_FOREGROUND(pen->colour);
1945          XDrawLine(display, wnd, gc, startx, starty, endx, endy);          XDrawLine(g_display, g_wnd, g_gc, startx, starty, endx, endy);
1946          if (ownbackstore)          if (g_ownbackstore)
1947                  XDrawLine(display, backstore, gc, startx, starty, endx, endy);                  XDrawLine(g_display, g_backstore, g_gc, startx, starty, endx, endy);
1948          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1949  }  }
1950    
# Line 1544  ui_draw_glyph(int mixmode, Line 1967  ui_draw_glyph(int mixmode,
1967          SET_FOREGROUND(fgcolour);          SET_FOREGROUND(fgcolour);
1968          SET_BACKGROUND(bgcolour);          SET_BACKGROUND(bgcolour);
1969    
1970          XSetFillStyle(display, gc,          XSetFillStyle(g_display, g_gc,
1971                        (mixmode == MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);                        (mixmode == MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);
1972          XSetStipple(display, gc, (Pixmap) glyph);          XSetStipple(g_display, g_gc, (Pixmap) glyph);
1973          XSetTSOrigin(display, gc, x, y);          XSetTSOrigin(g_display, g_gc, x, y);
1974    
1975          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1976    
1977          XSetFillStyle(display, gc, FillSolid);          XSetFillStyle(g_display, g_gc, FillSolid);
1978  }  }
1979    
1980  #define DO_GLYPH(ttext,idx) \  #define DO_GLYPH(ttext,idx) \
1981  {\  {\
1982    glyph = cache_get_font (font, ttext[idx]);\    glyph = cache_get_font (font, ttext[idx]);\
1983    if (!(flags & TEXT2_IMPLICIT_X))\    if (!(flags & TEXT2_IMPLICIT_X))\
1984      {\
1985        xyoffset = ttext[++idx];\
1986        if ((xyoffset & 0x80))\
1987      {\      {\
1988        xyoffset = ttext[++idx];\        if (flags & TEXT2_VERTICAL)\
1989        if ((xyoffset & 0x80))\          y += ttext[idx+1] | (ttext[idx+2] << 8);\
         {\  
           if (flags & TEXT2_VERTICAL) \  
             y += ttext[idx+1] | (ttext[idx+2] << 8);\  
           else\  
             x += ttext[idx+1] | (ttext[idx+2] << 8);\  
           idx += 2;\  
         }\  
1990        else\        else\
1991          {\          x += ttext[idx+1] | (ttext[idx+2] << 8);\
1992            if (flags & TEXT2_VERTICAL) \        idx += 2;\
             y += xyoffset;\  
           else\  
             x += xyoffset;\  
         }\  
1993      }\      }\
1994    if (glyph != NULL)\      else\
1995      {\      {\
1996        ui_draw_glyph (mixmode, x + glyph->offset,\        if (flags & TEXT2_VERTICAL)\
1997                       y + glyph->baseline,\          y += xyoffset;\
1998                       glyph->width, glyph->height,\        else\
1999                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\          x += xyoffset;\
       if (flags & TEXT2_IMPLICIT_X)\  
         x += glyph->width;\  
2000      }\      }\
2001      }\
2002      if (glyph != NULL)\
2003      {\
2004        x1 = x + glyph->offset;\
2005        y1 = y + glyph->baseline;\
2006        XSetStipple(g_display, g_gc, (Pixmap) glyph->pixmap);\
2007        XSetTSOrigin(g_display, g_gc, x1, y1);\
2008        FILL_RECTANGLE_BACKSTORE(x1, y1, glyph->width, glyph->height);\
2009        if (flags & TEXT2_IMPLICIT_X)\
2010          x += glyph->width;\
2011      }\
2012  }  }
2013    
2014  void  void
# Line 1594  ui_draw_text(uint8 font, uint8 flags, in Line 2018  ui_draw_text(uint8 font, uint8 flags, in
2018               int fgcolour, uint8 * text, uint8 length)               int fgcolour, uint8 * text, uint8 length)
2019  {  {
2020          FONTGLYPH *glyph;          FONTGLYPH *glyph;
2021          int i, j, xyoffset;          int i, j, xyoffset, x1, y1;
2022          DATABLOB *entry;          DATABLOB *entry;
2023    
2024          SET_FOREGROUND(bgcolour);          SET_FOREGROUND(bgcolour);
2025    
2026            /* Sometimes, the boxcx value is something really large, like
2027               32691. This makes XCopyArea fail with Xvnc. The code below
2028               is a quick fix. */
2029            if (boxx + boxcx > g_width)
2030                    boxcx = g_width - boxx;
2031    
2032          if (boxcx > 1)          if (boxcx > 1)
2033          {          {
2034                  FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);                  FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);
# Line 1608  ui_draw_text(uint8 font, uint8 flags, in Line 2038  ui_draw_text(uint8 font, uint8 flags, in
2038                  FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);                  FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);
2039          }          }
2040    
2041            SET_FOREGROUND(fgcolour);
2042            SET_BACKGROUND(bgcolour);
2043            XSetFillStyle(g_display, g_gc, FillStippled);
2044    
2045          /* Paint text, character by character */          /* Paint text, character by character */
2046          for (i = 0; i < length;)          for (i = 0; i < length;)
2047          {          {
# Line 1658  ui_draw_text(uint8 font, uint8 flags, in Line 2092  ui_draw_text(uint8 font, uint8 flags, in
2092                                  break;                                  break;
2093                  }                  }
2094          }          }
2095          if (ownbackstore)  
2096            XSetFillStyle(g_display, g_gc, FillSolid);
2097    
2098            if (g_ownbackstore)
2099          {          {
2100                  if (boxcx > 1)                  if (boxcx > 1)
2101                          XCopyArea(display, backstore, wnd, gc, boxx,                          XCopyArea(g_display, g_backstore, g_wnd, g_gc, boxx,
2102                                    boxy, boxcx, boxcy, boxx, boxy);                                    boxy, boxcx, boxcy, boxx, boxy);
2103                  else                  else
2104                          XCopyArea(display, backstore, wnd, gc, clipx,                          XCopyArea(g_display, g_backstore, g_wnd, g_gc, clipx,
2105                                    clipy, clipcx, clipcy, clipx, clipy);                                    clipy, clipcx, clipcy, clipx, clipy);
2106          }          }
2107  }  }
# Line 1675  ui_desktop_save(uint32 offset, int x, in Line 2112  ui_desktop_save(uint32 offset, int x, in
2112          Pixmap pix;          Pixmap pix;
2113          XImage *image;          XImage *image;
2114    
2115          if (ownbackstore)          if (g_ownbackstore)
2116          {          {
2117                  image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes, ZPixmap);                  image = XGetImage(g_display, g_backstore, x, y, cx, cy, AllPlanes, ZPixmap);
2118          }          }
2119          else          else
2120          {          {
2121                  pix = XCreatePixmap(display, wnd, cx, cy, depth);                  pix = XCreatePixmap(g_display, g_wnd, cx, cy, g_depth);
2122                  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);
2123                  image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);                  image = XGetImage(g_display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);
2124                  XFreePixmap(display, pix);                  XFreePixmap(g_display, pix);
2125          }          }
2126    
2127          offset *= bpp / 8;          offset *= g_bpp / 8;
2128          cache_put_desktop(offset, cx, cy, image->bytes_per_line, bpp / 8, (uint8 *) image->data);          cache_put_desktop(offset, cx, cy, image->bytes_per_line, g_bpp / 8, (uint8 *) image->data);
2129    
2130          XDestroyImage(image);          XDestroyImage(image);
2131  }  }
# Line 1699  ui_desktop_restore(uint32 offset, int x, Line 2136  ui_desktop_restore(uint32 offset, int x,
2136          XImage *image;          XImage *image;
2137          uint8 *data;          uint8 *data;
2138    
2139          offset *= bpp / 8;          offset *= g_bpp / 8;
2140          data = cache_get_desktop(offset, cx, cy, bpp / 8);          data = cache_get_desktop(offset, cx, cy, g_bpp / 8);
2141          if (data == NULL)          if (data == NULL)
2142                  return;                  return;
2143    
2144          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
2145                               (char *) data, cx, cy, BitmapPad(display), cx * bpp / 8);                               (char *) data, cx, cy, BitmapPad(g_display), cx * g_bpp / 8);
2146    
2147          if (ownbackstore)          if (g_ownbackstore)
2148          {          {
2149                  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);
2150                  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);
2151          }          }
2152          else          else
2153          {          {
2154                  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);
2155          }          }
2156    
2157          XFree(image);          XFree(image);
2158  }  }
   

Legend:
Removed from v.432  
changed lines
  Added in v.708

  ViewVC Help
Powered by ViewVC 1.1.26