/[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 478 by matthewc, Sun Oct 5 12:07:56 2003 UTC revision 828 by stargo, Sun Mar 6 21:11:18 2005 UTC
# Line 1  Line 1 
1  /* -*- c-basic-offset: 8 -*-  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     User interface services - X Window System     User interface services - X Window System
4     Copyright (C) Matthew Chapman 1999-2002     Copyright (C) Matthew Chapman 1999-2005
5    
6     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 20  Line 20 
20    
21  #include <X11/Xlib.h>  #include <X11/Xlib.h>
22  #include <X11/Xutil.h>  #include <X11/Xutil.h>
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 g_width;  extern int g_width;
32  extern int g_height;  extern int g_height;
33    extern int g_xpos;
34    extern int g_ypos;
35  extern BOOL g_sendmotion;  extern BOOL g_sendmotion;
36  extern BOOL g_fullscreen;  extern BOOL g_fullscreen;
37  extern BOOL g_grab_keyboard;  extern BOOL g_grab_keyboard;
# Line 34  extern BOOL g_hide_decorations; Line 39  extern BOOL g_hide_decorations;
39  extern char g_title[];  extern char g_title[];
40  extern int g_server_bpp;  extern int g_server_bpp;
41  extern int g_win_button_size;  extern int g_win_button_size;
 BOOL g_enable_compose = False;  
 BOOL g_focused;  
 BOOL g_mouse_in_wnd;  
42    
43  Display *g_display;  Display *g_display;
44  Time g_last_gesturetime;  Time g_last_gesturetime;
45  static int g_x_socket;  static int g_x_socket;
46  static Screen *g_screen;  static Screen *g_screen;
47  Window g_wnd;  Window g_wnd;
48  static GC g_gc;  extern uint32 g_embed_wnd;
49    BOOL g_enable_compose = False;
50    BOOL g_Unobscured;              /* used for screenblt */
51    static GC g_gc = NULL;
52    static GC g_create_bitmap_gc = NULL;
53    static GC g_create_glyph_gc = NULL;
54  static Visual *g_visual;  static Visual *g_visual;
55  static int g_depth;  static int g_depth;
56  static int g_bpp;  static int g_bpp;
# Line 51  static XIM g_IM; Line 58  static XIM g_IM;
58  static XIC g_IC;  static XIC g_IC;
59  static XModifierKeymap *g_mod_map;  static XModifierKeymap *g_mod_map;
60  static Cursor g_current_cursor;  static Cursor g_current_cursor;
61    static HCURSOR g_null_cursor = NULL;
62  static Atom g_protocol_atom, g_kill_atom;  static Atom g_protocol_atom, g_kill_atom;
63    static BOOL g_focused;
64    static BOOL g_mouse_in_wnd;
65    static BOOL g_arch_match = False;       /* set to True if RGB XServer and little endian */
66    
67  /* endianness */  /* endianness */
68  static BOOL g_host_be;  static BOOL g_host_be;
69  static BOOL g_xserver_be;  static BOOL g_xserver_be;
70    static int g_red_shift_r, g_blue_shift_r, g_green_shift_r;
71    static int g_red_shift_l, g_blue_shift_l, g_green_shift_l;
72    
73  /* software backing store */  /* software backing store */
74  static BOOL g_ownbackstore;  extern BOOL g_ownbackstore;
75  static Pixmap g_backstore;  static Pixmap g_backstore = 0;
76    
77  /* Moving in single app mode */  /* Moving in single app mode */
78  static BOOL g_moving_wnd;  static BOOL g_moving_wnd;
# Line 69  static int g_move_y_offset = 0; Line 82  static int g_move_y_offset = 0;
82  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
83  extern int g_dsp_fd;  extern int g_dsp_fd;
84  extern BOOL g_dsp_busy;  extern BOOL g_dsp_busy;
85    extern BOOL g_rdpsnd;
86  #endif  #endif
87    
88  /* MWM decorations */  /* MWM decorations */
# Line 106  PixelColour; Line 120  PixelColour;
120  }  }
121    
122  /* colour maps */  /* colour maps */
123  BOOL g_owncolmap = False;  extern BOOL g_owncolmap;
124  static Colormap g_xcolmap;  static Colormap g_xcolmap;
125  static uint32 *g_colmap = NULL;  static uint32 *g_colmap = NULL;
126    
127  #define TRANSLATE(col)          ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : translate_colour(g_colmap[col]) )  #define TRANSLATE(col)          ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : g_colmap[col] )
128  #define SET_FOREGROUND(col)     XSetForeground(g_display, g_gc, TRANSLATE(col));  #define SET_FOREGROUND(col)     XSetForeground(g_display, g_gc, TRANSLATE(col));
129  #define SET_BACKGROUND(col)     XSetBackground(g_display, g_gc, TRANSLATE(col));  #define SET_BACKGROUND(col)     XSetBackground(g_display, g_gc, TRANSLATE(col));
130    
# Line 158  mwm_hide_decorations(void) Line 172  mwm_hide_decorations(void)
172                          (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);                          (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
173  }  }
174    
175  static PixelColour  #define SPLITCOLOUR15(colour, rv) \
176  split_colour15(uint32 colour)  { \
177  {          rv.red = ((colour >> 7) & 0xf8) | ((colour >> 12) & 0x7); \
178          PixelColour rv;          rv.green = ((colour >> 2) & 0xf8) | ((colour >> 8) & 0x7); \
179          rv.red = (colour & 0x7c00) >> 10;          rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); \
         rv.red = (rv.red * 0xff) / 0x1f;  
         rv.green = (colour & 0x03e0) >> 5;  
         rv.green = (rv.green * 0xff) / 0x1f;  
         rv.blue = (colour & 0x1f);  
         rv.blue = (rv.blue * 0xff) / 0x1f;  
         return rv;  
 }  
   
 static PixelColour  
 split_colour16(uint32 colour)  
 {  
         PixelColour rv;  
         rv.red = (colour & 0xf800) >> 11;  
         rv.red = (rv.red * 0xff) / 0x1f;  
         rv.green = (colour & 0x07e0) >> 5;  
         rv.green = (rv.green * 0xff) / 0x3f;  
         rv.blue = (colour & 0x001f);  
         rv.blue = (rv.blue * 0xff) / 0x1f;  
         return rv;  
 }  
   
 static PixelColour  
 split_colour24(uint32 colour)  
 {  
         PixelColour rv;  
         rv.blue = (colour & 0xff0000) >> 16;  
         rv.green = (colour & 0xff00) >> 8;  
         rv.red = (colour & 0xff);  
         return rv;  
180  }  }
181    
182  static uint32  #define SPLITCOLOUR16(colour, rv) \
183  make_colour16(PixelColour pc)  { \
184  {          rv.red = ((colour >> 8) & 0xf8) | ((colour >> 13) & 0x7); \
185          pc.red = (pc.red * 0x1f) / 0xff;          rv.green = ((colour >> 3) & 0xfc) | ((colour >> 9) & 0x3); \
186          pc.green = (pc.green * 0x3f) / 0xff;          rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); \
187          pc.blue = (pc.blue * 0x1f) / 0xff;  } \
         return (pc.red << 11) | (pc.green << 5) | pc.blue;  
 }  
188    
189  static uint32  #define SPLITCOLOUR24(colour, rv) \
190  make_colour24(PixelColour pc)  { \
191  {          rv.blue = (colour & 0xff0000) >> 16; \
192          if (g_xserver_be)          rv.green = (colour & 0x00ff00) >> 8; \
193          {          rv.red = (colour & 0x0000ff); \
                 return pc.red | (pc.green << 8) | (pc.blue << 16);  
         }  
         else  
         {  
                 return (pc.red << 16) | (pc.green << 8) | pc.blue;  
         }  
194  }  }
195    
196  static uint32  #define MAKECOLOUR(pc) \
197  make_colour32(PixelColour pc)          ((pc.red >> g_red_shift_r) << g_red_shift_l) \
198  {                  | ((pc.green >> g_green_shift_r) << g_green_shift_l) \
199          if (g_xserver_be)                  | ((pc.blue >> g_blue_shift_r) << g_blue_shift_l) \
         {  
                 return pc.red | (pc.green << 8) | (pc.blue << 16);  
         }  
         else  
         {  
                 return (pc.red << 16) | (pc.green << 8) | pc.blue;  
         }  
 }  
200    
201  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
202  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | (x & 0xff00)); }
203  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
204                          x = (x << 16) | (x >> 16); }                          x = (x << 16) | (x >> 16); }
205    
206    #define BOUT16(o, x) { *(o++) = x >> 8; *(o++) = x; }
207    #define BOUT24(o, x) { *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; }
208    #define BOUT32(o, x) { *(o++) = x >> 24; *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; }
209    #define LOUT16(o, x) { *(o++) = x; *(o++) = x >> 8; }
210    #define LOUT24(o, x) { *(o++) = x; *(o++) = x >> 8; *(o++) = x >> 16; }
211    #define LOUT32(o, x) { *(o++) = x; *(o++) = x >> 8; *(o++) = x >> 16; *(o++) = x >> 24; }
212    
213  static uint32  static uint32
214  translate_colour(uint32 colour)  translate_colour(uint32 colour)
215  {  {
216            PixelColour pc;
217          switch (g_server_bpp)          switch (g_server_bpp)
218          {          {
219                  case 15:                  case 15:
220                          switch (g_bpp)                          SPLITCOLOUR15(colour, pc);
                         {  
                                 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;  
                         }  
221                          break;                          break;
222                  case 16:                  case 16:
223                          switch (g_bpp)                          SPLITCOLOUR16(colour, pc);
                         {  
                                 case 16:  
                                         break;  
                                 case 24:  
                                         colour = make_colour24(split_colour16(colour));  
                                         break;  
                                 case 32:  
                                         colour = make_colour32(split_colour16(colour));  
                                         break;  
                         }  
224                          break;                          break;
225                  case 24:                  case 24:
226                          switch (g_bpp)                          SPLITCOLOUR24(colour, pc);
                         {  
                                 case 16:  
                                         colour = make_colour16(split_colour24(colour));  
                                         break;  
                                 case 24:  
                                         break;  
                                 case 32:  
                                         colour = make_colour32(split_colour24(colour));  
                                         break;  
                         }  
227                          break;                          break;
228          }          }
229          switch (g_bpp)          return MAKECOLOUR(pc);
230          {  }
                 case 16:  
                         if (g_host_be != g_xserver_be)  
                                 BSWAP16(colour);  
                         break;  
231    
232                  case 24:  /* indent is confused by UNROLL8 */
233                          if (g_xserver_be)  /* *INDENT-OFF* */
                                 BSWAP24(colour);  
                         break;  
234    
235                  case 32:  /* repeat and unroll, similar to bitmap.c */
236                          if (g_host_be != g_xserver_be)  /* potentialy any of the following translate */
237                                  BSWAP32(colour);  /* functions can use repeat but just doing */
238                          break;  /* the most common ones */
         }  
239    
240          return colour;  #define UNROLL8(stm) { stm stm stm stm stm stm stm stm }
241    /* 2 byte output repeat */
242    #define REPEAT2(stm) \
243    { \
244            while (out <= end - 8 * 2) \
245                    UNROLL8(stm) \
246            while (out < end) \
247                    { stm } \
248  }  }
249    /* 3 byte output repeat */
250    #define REPEAT3(stm) \
251    { \
252            while (out <= end - 8 * 3) \
253                    UNROLL8(stm) \
254            while (out < end) \
255                    { stm } \
256    }
257    /* 4 byte output repeat */
258    #define REPEAT4(stm) \
259    { \
260            while (out <= end - 8 * 4) \
261                    UNROLL8(stm) \
262            while (out < end) \
263                    { stm } \
264    }
265    /* *INDENT-ON* */
266    
267  static void  static void
268  translate8to8(uint8 * data, uint8 * out, uint8 * end)  translate8to8(const uint8 * data, uint8 * out, uint8 * end)
269  {  {
270          while (out < end)          while (out < end)
271                  *(out++) = (uint8) g_colmap[*(data++)];                  *(out++) = (uint8) g_colmap[*(data++)];
272  }  }
273    
274  static void  static void
275  translate8to16(uint8 * data, uint16 * out, uint16 * end)  translate8to16(const uint8 * data, uint8 * out, uint8 * end)
276  {  {
277          while (out < end)          uint16 value;
278                  *(out++) = (uint16) g_colmap[*(data++)];  
279            if (g_arch_match)
280            {
281                    /* *INDENT-OFF* */
282                    REPEAT2
283                    (
284                            *((uint16 *) out) = g_colmap[*(data++)];
285                            out += 2;
286                    )
287                    /* *INDENT-ON* */
288            }
289            else if (g_xserver_be)
290            {
291                    while (out < end)
292                    {
293                            value = (uint16) g_colmap[*(data++)];
294                            BOUT16(out, value);
295                    }
296            }
297            else
298            {
299                    while (out < end)
300                    {
301                            value = (uint16) g_colmap[*(data++)];
302                            LOUT16(out, value);
303                    }
304            }
305  }  }
306    
307  /* little endian - conversion happens when colourmap is built */  /* little endian - conversion happens when colourmap is built */
308  static void  static void
309  translate8to24(uint8 * data, uint8 * out, uint8 * end)  translate8to24(const uint8 * data, uint8 * out, uint8 * end)
310  {  {
311          uint32 value;          uint32 value;
312    
313          while (out < end)          if (g_xserver_be)
314          {          {
315                  value = g_colmap[*(data++)];                  while (out < end)
316                  *(out++) = value;                  {
317                  *(out++) = value >> 8;                          value = g_colmap[*(data++)];
318                  *(out++) = value >> 16;                          BOUT24(out, value);
319                    }
320            }
321            else
322            {
323                    while (out < end)
324                    {
325                            value = g_colmap[*(data++)];
326                            LOUT24(out, value);
327                    }
328          }          }
329  }  }
330    
331  static void  static void
332  translate8to32(uint8 * data, uint32 * out, uint32 * end)  translate8to32(const uint8 * data, uint8 * out, uint8 * end)
333  {  {
334          while (out < end)          uint32 value;
                 *(out++) = g_colmap[*(data++)];  
 }  
335    
336  /* todo the remaining translate function might need some big endian check ?? */          if (g_arch_match)
337            {
338                    /* *INDENT-OFF* */
339                    REPEAT4
340                    (
341                            *((uint32 *) out) = g_colmap[*(data++)];
342                            out += 4;
343                    )
344                    /* *INDENT-ON* */
345            }
346            else if (g_xserver_be)
347            {
348                    while (out < end)
349                    {
350                            value = g_colmap[*(data++)];
351                            BOUT32(out, value);
352                    }
353            }
354            else
355            {
356                    while (out < end)
357                    {
358                            value = g_colmap[*(data++)];
359                            LOUT32(out, value);
360                    }
361            }
362    }
363    
364  static void  static void
365  translate15to16(uint16 * data, uint16 * out, uint16 * end)  translate15to16(const uint16 * data, uint8 * out, uint8 * end)
366  {  {
367          while (out < end)          uint16 pixel;
368                  *(out++) = (uint16) make_colour16(split_colour15(*(data++)));          uint16 value;
369            PixelColour pc;
370    
371            if (g_xserver_be)
372            {
373                    while (out < end)
374                    {
375                            pixel = *(data++);
376                            if (g_host_be)
377                            {
378                                    BSWAP16(pixel);
379                            }
380                            SPLITCOLOUR15(pixel, pc);
381                            value = MAKECOLOUR(pc);
382                            BOUT16(out, value);
383                    }
384            }
385            else
386            {
387                    while (out < end)
388                    {
389                            pixel = *(data++);
390                            if (g_host_be)
391                            {
392                                    BSWAP16(pixel);
393                            }
394                            SPLITCOLOUR15(pixel, pc);
395                            value = MAKECOLOUR(pc);
396                            LOUT16(out, value);
397                    }
398            }
399  }  }
400    
401  static void  static void
402  translate15to24(uint16 * data, uint8 * out, uint8 * end)  translate15to24(const uint16 * data, uint8 * out, uint8 * end)
403  {  {
404          uint32 value;          uint32 value;
405            uint16 pixel;
406            PixelColour pc;
407    
408          while (out < end)          if (g_arch_match)
409          {          {
410                  value = make_colour24(split_colour15(*(data++)));                  /* *INDENT-OFF* */
411                  *(out++) = value;                  REPEAT3
412                  *(out++) = value >> 8;                  (
413                  *(out++) = value >> 16;                          pixel = *(data++);
414                            SPLITCOLOUR15(pixel, pc);
415                            *(out++) = pc.blue;
416                            *(out++) = pc.green;
417                            *(out++) = pc.red;
418                    )
419                    /* *INDENT-ON* */
420            }
421            else if (g_xserver_be)
422            {
423                    while (out < end)
424                    {
425                            pixel = *(data++);
426                            if (g_host_be)
427                            {
428                                    BSWAP16(pixel);
429                            }
430                            SPLITCOLOUR15(pixel, pc);
431                            value = MAKECOLOUR(pc);
432                            BOUT24(out, value);
433                    }
434            }
435            else
436            {
437                    while (out < end)
438                    {
439                            pixel = *(data++);
440                            if (g_host_be)
441                            {
442                                    BSWAP16(pixel);
443                            }
444                            SPLITCOLOUR15(pixel, pc);
445                            value = MAKECOLOUR(pc);
446                            LOUT24(out, value);
447                    }
448          }          }
449  }  }
450    
451  static void  static void
452  translate15to32(uint16 * data, uint32 * out, uint32 * end)  translate15to32(const uint16 * data, uint8 * out, uint8 * end)
453  {  {
454          uint16 pixel;          uint16 pixel;
455            uint32 value;
456            PixelColour pc;
457    
458          while (out < end)          if (g_arch_match)
459          {          {
460                  if (g_host_be)                  /* *INDENT-OFF* */
461                    REPEAT4
462                    (
463                            pixel = *(data++);
464                            SPLITCOLOUR15(pixel, pc);
465                            *(out++) = pc.blue;
466                            *(out++) = pc.green;
467                            *(out++) = pc.red;
468                            *(out++) = 0;
469                    )
470                    /* *INDENT-ON* */
471            }
472            else if (g_xserver_be)
473            {
474                    while (out < end)
475                  {                  {
476                          pixel = *(data++);                          pixel = *(data++);
477                          pixel = (pixel & 0xff) << 8 | (pixel & 0xff00) >> 8;                          if (g_host_be)
478                          *(out++) = make_colour32(split_colour15(pixel));                          {
479                                    BSWAP16(pixel);
480                            }
481                            SPLITCOLOUR15(pixel, pc);
482                            value = MAKECOLOUR(pc);
483                            BOUT32(out, value);
484                  }                  }
485                  else          }
486            else
487            {
488                    while (out < end)
489                  {                  {
490                          *(out++) = make_colour32(split_colour15(*(data++)));                          pixel = *(data++);
491                            if (g_host_be)
492                            {
493                                    BSWAP16(pixel);
494                            }
495                            SPLITCOLOUR15(pixel, pc);
496                            value = MAKECOLOUR(pc);
497                            LOUT32(out, value);
498                  }                  }
499          }          }
500  }  }
501    
502  static void  static void
503  translate16to16(uint16 * data, uint16 * out, uint16 * end)  translate16to16(const uint16 * data, uint8 * out, uint8 * end)
504  {  {
505          while (out < end)          uint16 pixel;
506                  *(out++) = (uint16) (*(data++));          uint16 value;
507  }          PixelColour pc;
508    
509            if (g_xserver_be)
510            {
511                    if (g_host_be)
512                    {
513                            while (out < end)
514                            {
515                                    pixel = *(data++);
516                                    BSWAP16(pixel);
517                                    SPLITCOLOUR16(pixel, pc);
518                                    value = MAKECOLOUR(pc);
519                                    BOUT16(out, value);
520                            }
521                    }
522                    else
523                    {
524                            while (out < end)
525                            {
526                                    pixel = *(data++);
527                                    SPLITCOLOUR16(pixel, pc);
528                                    value = MAKECOLOUR(pc);
529                                    BOUT16(out, value);
530                            }
531                    }
532            }
533            else
534            {
535                    if (g_host_be)
536                    {
537                            while (out < end)
538                            {
539                                    pixel = *(data++);
540                                    BSWAP16(pixel);
541                                    SPLITCOLOUR16(pixel, pc);
542                                    value = MAKECOLOUR(pc);
543                                    LOUT16(out, value);
544                            }
545                    }
546                    else
547                    {
548                            while (out < end)
549                            {
550                                    pixel = *(data++);
551                                    SPLITCOLOUR16(pixel, pc);
552                                    value = MAKECOLOUR(pc);
553                                    LOUT16(out, value);
554                            }
555                    }
556            }
557    }
558    
559  static void  static void
560  translate16to24(uint16 * data, uint8 * out, uint8 * end)  translate16to24(const uint16 * data, uint8 * out, uint8 * end)
561  {  {
562          uint32 value;          uint32 value;
563            uint16 pixel;
564            PixelColour pc;
565    
566          while (out < end)          if (g_arch_match)
567          {          {
568                  value = make_colour24(split_colour16(*(data++)));                  /* *INDENT-OFF* */
569                  *(out++) = value;                  REPEAT3
570                  *(out++) = value >> 8;                  (
571                  *(out++) = value >> 16;                          pixel = *(data++);
572                            SPLITCOLOUR16(pixel, pc);
573                            *(out++) = pc.blue;
574                            *(out++) = pc.green;
575                            *(out++) = pc.red;
576                    )
577                    /* *INDENT-ON* */
578            }
579            else if (g_xserver_be)
580            {
581                    if (g_host_be)
582                    {
583                            while (out < end)
584                            {
585                                    pixel = *(data++);
586                                    BSWAP16(pixel);
587                                    SPLITCOLOUR16(pixel, pc);
588                                    value = MAKECOLOUR(pc);
589                                    BOUT24(out, value);
590                            }
591                    }
592                    else
593                    {
594                            while (out < end)
595                            {
596                                    pixel = *(data++);
597                                    SPLITCOLOUR16(pixel, pc);
598                                    value = MAKECOLOUR(pc);
599                                    BOUT24(out, value);
600                            }
601                    }
602            }
603            else
604            {
605                    if (g_host_be)
606                    {
607                            while (out < end)
608                            {
609                                    pixel = *(data++);
610                                    BSWAP16(pixel);
611                                    SPLITCOLOUR16(pixel, pc);
612                                    value = MAKECOLOUR(pc);
613                                    LOUT24(out, value);
614                            }
615                    }
616                    else
617                    {
618                            while (out < end)
619                            {
620                                    pixel = *(data++);
621                                    SPLITCOLOUR16(pixel, pc);
622                                    value = MAKECOLOUR(pc);
623                                    LOUT24(out, value);
624                            }
625                    }
626          }          }
627  }  }
628    
629  static void  static void
630  translate16to32(uint16 * data, uint32 * out, uint32 * end)  translate16to32(const uint16 * data, uint8 * out, uint8 * end)
631  {  {
632          uint16 pixel;          uint16 pixel;
633            uint32 value;
634            PixelColour pc;
635    
636          while (out < end)          if (g_arch_match)
637            {
638                    /* *INDENT-OFF* */
639                    REPEAT4
640                    (
641                            pixel = *(data++);
642                            SPLITCOLOUR16(pixel, pc);
643                            *(out++) = pc.blue;
644                            *(out++) = pc.green;
645                            *(out++) = pc.red;
646                            *(out++) = 0;
647                    )
648                    /* *INDENT-ON* */
649            }
650            else if (g_xserver_be)
651          {          {
652                  if (g_host_be)                  if (g_host_be)
653                  {                  {
654                          pixel = *(data++);                          while (out < end)
655                          pixel = (pixel & 0xff) << 8 | (pixel & 0xff00) >> 8;                          {
656                          *(out++) = make_colour32(split_colour16(pixel));                                  pixel = *(data++);
657                                    BSWAP16(pixel);
658                                    SPLITCOLOUR16(pixel, pc);
659                                    value = MAKECOLOUR(pc);
660                                    BOUT32(out, value);
661                            }
662                    }
663                    else
664                    {
665                            while (out < end)
666                            {
667                                    pixel = *(data++);
668                                    SPLITCOLOUR16(pixel, pc);
669                                    value = MAKECOLOUR(pc);
670                                    BOUT32(out, value);
671                            }
672                    }
673            }
674            else
675            {
676                    if (g_host_be)
677                    {
678                            while (out < end)
679                            {
680                                    pixel = *(data++);
681                                    BSWAP16(pixel);
682                                    SPLITCOLOUR16(pixel, pc);
683                                    value = MAKECOLOUR(pc);
684                                    LOUT32(out, value);
685                            }
686                  }                  }
687                  else                  else
688                  {                  {
689                          *(out++) = make_colour32(split_colour16(*(data++)));                          while (out < end)
690                            {
691                                    pixel = *(data++);
692                                    SPLITCOLOUR16(pixel, pc);
693                                    value = MAKECOLOUR(pc);
694                                    LOUT32(out, value);
695                            }
696                  }                  }
697          }          }
698  }  }
699    
700  static void  static void
701  translate24to16(uint8 * data, uint16 * out, uint16 * end)  translate24to16(const uint8 * data, uint8 * out, uint8 * end)
702  {  {
703          uint32 pixel = 0;          uint32 pixel = 0;
704            uint16 value;
705            PixelColour pc;
706    
707          while (out < end)          while (out < end)
708          {          {
709                  pixel = *(data++) << 16;                  pixel = *(data++) << 16;
710                  pixel |= *(data++) << 8;                  pixel |= *(data++) << 8;
711                  pixel |= *(data++);                  pixel |= *(data++);
712                  *(out++) = (uint16) make_colour16(split_colour24(pixel));                  SPLITCOLOUR24(pixel, pc);
713                    value = MAKECOLOUR(pc);
714                    if (g_xserver_be)
715                    {
716                            BOUT16(out, value);
717                    }
718                    else
719                    {
720                            LOUT16(out, value);
721                    }
722          }          }
723  }  }
724    
725  static void  static void
726  translate24to24(uint8 * data, uint8 * out, uint8 * end)  translate24to24(const uint8 * data, uint8 * out, uint8 * end)
727  {  {
728          while (out < end)          uint32 pixel;
729            uint32 value;
730            PixelColour pc;
731    
732            if (g_xserver_be)
733            {
734                    while (out < end)
735                    {
736                            pixel = *(data++) << 16;
737                            pixel |= *(data++) << 8;
738                            pixel |= *(data++);
739                            SPLITCOLOUR24(pixel, pc);
740                            value = MAKECOLOUR(pc);
741                            BOUT24(out, value);
742                    }
743            }
744            else
745          {          {
746                  *(out++) = (*(data++));                  while (out < end)
747                    {
748                            pixel = *(data++) << 16;
749                            pixel |= *(data++) << 8;
750                            pixel |= *(data++);
751                            SPLITCOLOUR24(pixel, pc);
752                            value = MAKECOLOUR(pc);
753                            LOUT24(out, value);
754                    }
755          }          }
756  }  }
757    
758  static void  static void
759  translate24to32(uint8 * data, uint32 * out, uint32 * end)  translate24to32(const uint8 * data, uint8 * out, uint8 * end)
760  {  {
761          uint32 pixel = 0;          uint32 pixel;
762          while (out < end)          uint32 value;
763            PixelColour pc;
764    
765            if (g_arch_match)
766          {          {
767                  if (g_host_be)                  /* *INDENT-OFF* */
768    #ifdef NEED_ALIGN
769                    REPEAT4
770                    (
771                            *(out++) = *(data++);
772                            *(out++) = *(data++);
773                            *(out++) = *(data++);
774                            *(out++) = 0;
775                    )
776    #else
777                    REPEAT4
778                    (
779                            *((uint32 *) out) = *((uint32 *) data);
780                            out += 4;
781                            data += 3;
782                    )
783    #endif
784                    /* *INDENT-ON* */
785            }
786            else if (g_xserver_be)
787            {
788                    while (out < end)
789                  {                  {
790                          pixel = *(data++) << 16;                          pixel = *(data++) << 16;
791                          pixel |= *(data++) << 8;                          pixel |= *(data++) << 8;
792                          pixel |= *(data++);                          pixel |= *(data++);
793                            SPLITCOLOUR24(pixel, pc);
794                            value = MAKECOLOUR(pc);
795                            BOUT32(out, value);
796                  }                  }
797                  else          }
798            else
799            {
800                    while (out < end)
801                  {                  {
802                          pixel = *(data++);                          pixel = *(data++) << 16;
803                          pixel |= *(data++) << 8;                          pixel |= *(data++) << 8;
804                          pixel |= *(data++) << 16;                          pixel |= *(data++);
805                            SPLITCOLOUR24(pixel, pc);
806                            value = MAKECOLOUR(pc);
807                            LOUT32(out, value);
808                  }                  }
                 *(out++) = pixel;  
809          }          }
810  }  }
811    
812  static uint8 *  static uint8 *
813  translate_image(int width, int height, uint8 * data)  translate_image(int width, int height, uint8 * data)
814  {  {
815          int size = width * height * g_bpp / 8;          int size;
816          uint8 *out = (uint8 *) xmalloc(size);          uint8 *out;
817          uint8 *end = out + size;          uint8 *end;
818    
819            /* if server and xserver bpp match, */
820            /* and arch(endian) matches, no need to translate */
821            /* just return data */
822            if (g_arch_match)
823            {
824                    if (g_depth == 15 && g_server_bpp == 15)
825                            return data;
826                    if (g_depth == 16 && g_server_bpp == 16)
827                            return data;
828                    if (g_depth == 24 && g_bpp == 24 && g_server_bpp == 24)
829                            return data;
830            }
831    
832            size = width * height * (g_bpp / 8);
833            out = (uint8 *) xmalloc(size);
834            end = out + size;
835    
836          switch (g_server_bpp)          switch (g_server_bpp)
837          {          {
# Line 479  translate_image(int width, int height, u Line 839  translate_image(int width, int height, u
839                          switch (g_bpp)                          switch (g_bpp)
840                          {                          {
841                                  case 32:                                  case 32:
842                                          translate24to32(data, (uint32 *) out, (uint32 *) end);                                          translate24to32(data, out, end);
843                                          break;                                          break;
844                                  case 24:                                  case 24:
845                                          translate24to24(data, out, end);                                          translate24to24(data, out, end);
846                                          break;                                          break;
847                                  case 16:                                  case 16:
848                                          translate24to16(data, (uint16 *) out, (uint16 *) end);                                          translate24to16(data, out, end);
849                                          break;                                          break;
850                          }                          }
851                          break;                          break;
# Line 493  translate_image(int width, int height, u Line 853  translate_image(int width, int height, u
853                          switch (g_bpp)                          switch (g_bpp)
854                          {                          {
855                                  case 32:                                  case 32:
856                                          translate16to32((uint16 *) data, (uint32 *) out,                                          translate16to32((uint16 *) data, out, end);
                                                         (uint32 *) end);  
857                                          break;                                          break;
858                                  case 24:                                  case 24:
859                                          translate16to24((uint16 *) data, out, end);                                          translate16to24((uint16 *) data, out, end);
860                                          break;                                          break;
861                                  case 16:                                  case 16:
862                                          translate16to16((uint16 *) data, (uint16 *) out,                                          translate16to16((uint16 *) data, out, end);
                                                         (uint16 *) end);  
863                                          break;                                          break;
864                          }                          }
865                          break;                          break;
# Line 509  translate_image(int width, int height, u Line 867  translate_image(int width, int height, u
867                          switch (g_bpp)                          switch (g_bpp)
868                          {                          {
869                                  case 32:                                  case 32:
870                                          translate15to32((uint16 *) data, (uint32 *) out,                                          translate15to32((uint16 *) data, out, end);
                                                         (uint32 *) end);  
871                                          break;                                          break;
872                                  case 24:                                  case 24:
873                                          translate15to24((uint16 *) data, out, end);                                          translate15to24((uint16 *) data, out, end);
874                                          break;                                          break;
875                                  case 16:                                  case 16:
876                                          translate15to16((uint16 *) data, (uint16 *) out,                                          translate15to16((uint16 *) data, out, end);
                                                         (uint16 *) end);  
877                                          break;                                          break;
878                          }                          }
879                          break;                          break;
# Line 528  translate_image(int width, int height, u Line 884  translate_image(int width, int height, u
884                                          translate8to8(data, out, end);                                          translate8to8(data, out, end);
885                                          break;                                          break;
886                                  case 16:                                  case 16:
887                                          translate8to16(data, (uint16 *) out, (uint16 *) end);                                          translate8to16(data, out, end);
888                                          break;                                          break;
889                                  case 24:                                  case 24:
890                                          translate8to24(data, out, end);                                          translate8to24(data, out, end);
891                                          break;                                          break;
892                                  case 32:                                  case 32:
893                                          translate8to32(data, (uint32 *) out, (uint32 *) end);                                          translate8to32(data, out, end);
894                                          break;                                          break;
895                          }                          }
896                          break;                          break;
# Line 567  get_key_state(unsigned int state, uint32 Line 923  get_key_state(unsigned int state, uint32
923          return (state & keysymMask) ? True : False;          return (state & keysymMask) ? True : False;
924  }  }
925    
926    static void
927    calculate_shifts(uint32 mask, int *shift_r, int *shift_l)
928    {
929            *shift_l = ffs(mask) - 1;
930            mask >>= *shift_l;
931            *shift_r = 8 - ffs(mask & ~(mask >> 1));
932    }
933    
934  BOOL  BOOL
935  ui_init(void)  ui_init(void)
936  {  {
937            XVisualInfo vi;
938          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
939          uint16 test;          uint16 test;
940          int i;          int i, screen_num, nvisuals;
941            XVisualInfo *vmatches = NULL;
942            XVisualInfo template;
943            Bool TrueColorVisual = False;
944    
945          g_display = XOpenDisplay(NULL);          g_display = XOpenDisplay(NULL);
946          if (g_display == NULL)          if (g_display == NULL)
# Line 581  ui_init(void) Line 949  ui_init(void)
949                  return False;                  return False;
950          }          }
951    
952            screen_num = DefaultScreen(g_display);
953          g_x_socket = ConnectionNumber(g_display);          g_x_socket = ConnectionNumber(g_display);
954          g_screen = DefaultScreenOfDisplay(g_display);          g_screen = ScreenOfDisplay(g_display, screen_num);
         g_visual = DefaultVisualOfScreen(g_screen);  
955          g_depth = DefaultDepthOfScreen(g_screen);          g_depth = DefaultDepthOfScreen(g_screen);
956    
957            /* Search for best TrueColor depth */
958            template.class = TrueColor;
959            vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &nvisuals);
960    
961            nvisuals--;
962            while (nvisuals >= 0)
963            {
964                    if ((vmatches + nvisuals)->depth > g_depth)
965                    {
966                            g_depth = (vmatches + nvisuals)->depth;
967                    }
968                    nvisuals--;
969                    TrueColorVisual = True;
970            }
971    
972            test = 1;
973            g_host_be = !(BOOL) (*(uint8 *) (&test));
974            g_xserver_be = (ImageByteOrder(g_display) == MSBFirst);
975    
976            if ((g_server_bpp == 8) && ((!TrueColorVisual) || (g_depth <= 8)))
977            {
978                    /* we use a colourmap, so the default visual should do */
979                    g_visual = DefaultVisualOfScreen(g_screen);
980                    g_depth = DefaultDepthOfScreen(g_screen);
981    
982                    /* Do not allocate colours on a TrueColor visual */
983                    if (g_visual->class == TrueColor)
984                    {
985                            g_owncolmap = False;
986                    }
987            }
988            else
989            {
990                    /* need a truecolour visual */
991                    if (!XMatchVisualInfo(g_display, screen_num, g_depth, TrueColor, &vi))
992                    {
993                            error("The display does not support true colour - high colour support unavailable.\n");
994                            return False;
995                    }
996    
997                    g_visual = vi.visual;
998                    g_owncolmap = False;
999                    calculate_shifts(vi.red_mask, &g_red_shift_r, &g_red_shift_l);
1000                    calculate_shifts(vi.blue_mask, &g_blue_shift_r, &g_blue_shift_l);
1001                    calculate_shifts(vi.green_mask, &g_green_shift_r, &g_green_shift_l);
1002    
1003                    /* if RGB video and everything is little endian */
1004                    if ((vi.red_mask > vi.green_mask && vi.green_mask > vi.blue_mask) &&
1005                        !g_xserver_be && !g_host_be)
1006                    {
1007                            if (g_depth <= 16 || (g_red_shift_l == 16 && g_green_shift_l == 8 &&
1008                                                  g_blue_shift_l == 0))
1009                            {
1010                                    g_arch_match = True;
1011                            }
1012                    }
1013    
1014                    if (g_arch_match)
1015                    {
1016                            DEBUG(("Architectures match, enabling little endian optimisations.\n"));
1017                    }
1018            }
1019    
1020          pfm = XListPixmapFormats(g_display, &i);          pfm = XListPixmapFormats(g_display, &i);
1021          if (pfm != NULL)          if (pfm != NULL)
1022          {          {
# Line 608  ui_init(void) Line 1039  ui_init(void)
1039                  return False;                  return False;
1040          }          }
1041    
1042          if (g_owncolmap != True)          if (!g_owncolmap)
1043          {          {
1044                  g_xcolmap = DefaultColormapOfScreen(g_screen);                  g_xcolmap =
1045                            XCreateColormap(g_display, RootWindowOfScreen(g_screen), g_visual,
1046                                            AllocNone);
1047                  if (g_depth <= 8)                  if (g_depth <= 8)
1048                          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");
1049          }          }
1050    
1051          g_gc = XCreateGC(g_display, RootWindowOfScreen(g_screen), 0, NULL);          if ((!g_ownbackstore) && (DoesBackingStore(g_screen) != Always))
1052            {
1053          if (DoesBackingStore(g_screen) != Always)                  warning("External BackingStore not available, using internal\n");
1054                  g_ownbackstore = True;                  g_ownbackstore = True;
1055            }
1056    
1057          test = 1;          /*
1058          g_host_be = !(BOOL) (*(uint8 *) (&test));           * Determine desktop size
1059          g_xserver_be = (ImageByteOrder(g_display) == MSBFirst);           */
1060            if (g_fullscreen)
1061          if ((g_width == 0) || (g_height == 0))          {
1062                    g_width = WidthOfScreen(g_screen);
1063                    g_height = HeightOfScreen(g_screen);
1064            }
1065            else if (g_width < 0)
1066            {
1067                    /* Percent of screen */
1068                    g_height = HeightOfScreen(g_screen) * (-g_width) / 100;
1069                    g_width = WidthOfScreen(g_screen) * (-g_width) / 100;
1070            }
1071            else if (g_width == 0)
1072          {          {
1073                  /* Fetch geometry from _NET_WORKAREA */                  /* Fetch geometry from _NET_WORKAREA */
1074                  uint32 x, y, cx, cy;                  uint32 x, y, cx, cy;
# Line 642  ui_init(void) Line 1086  ui_init(void)
1086                  }                  }
1087          }          }
1088    
         if (g_fullscreen)  
         {  
                 g_width = WidthOfScreen(g_screen);  
                 g_height = HeightOfScreen(g_screen);  
         }  
   
1089          /* make sure width is a multiple of 4 */          /* make sure width is a multiple of 4 */
1090          g_width = (g_width + 3) & ~3;          g_width = (g_width + 3) & ~3;
1091    
         if (g_ownbackstore)  
         {  
                 g_backstore =  
                         XCreatePixmap(g_display, RootWindowOfScreen(g_screen), g_width, g_height,  
                                       g_depth);  
   
                 /* clear to prevent rubbish being exposed at startup */  
                 XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen));  
                 XFillRectangle(g_display, g_backstore, g_gc, 0, 0, g_width, g_height);  
         }  
   
1092          g_mod_map = XGetModifierMapping(g_display);          g_mod_map = XGetModifierMapping(g_display);
1093    
1094            xkeymap_init();
1095    
1096          if (g_enable_compose)          if (g_enable_compose)
1097                  g_IM = XOpenIM(g_display, NULL, NULL, NULL);                  g_IM = XOpenIM(g_display, NULL, NULL, NULL);
1098    
         xkeymap_init();  
1099          xclip_init();          xclip_init();
1100    
1101          /* 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", g_server_bpp, g_bpp, g_depth);  
1102    
1103          return True;          return True;
1104  }  }
# Line 682  ui_deinit(void) Line 1109  ui_deinit(void)
1109          if (g_IM != NULL)          if (g_IM != NULL)
1110                  XCloseIM(g_IM);                  XCloseIM(g_IM);
1111    
1112            if (g_null_cursor != NULL)
1113                    ui_destroy_cursor(g_null_cursor);
1114    
1115          XFreeModifiermap(g_mod_map);          XFreeModifiermap(g_mod_map);
1116    
1117          if (g_ownbackstore)          if (g_ownbackstore)
# Line 695  ui_deinit(void) Line 1125  ui_deinit(void)
1125  BOOL  BOOL
1126  ui_create_window(void)  ui_create_window(void)
1127  {  {
1128            uint8 null_pointer_mask[1] = { 0x80 };
1129            uint8 null_pointer_data[24] = { 0x00 };
1130    
1131          XSetWindowAttributes attribs;          XSetWindowAttributes attribs;
1132          XClassHint *classhints;          XClassHint *classhints;
1133          XSizeHints *sizehints;          XSizeHints *sizehints;
# Line 706  ui_create_window(void) Line 1139  ui_create_window(void)
1139          wndheight = g_fullscreen ? HeightOfScreen(g_screen) : g_height;          wndheight = g_fullscreen ? HeightOfScreen(g_screen) : g_height;
1140    
1141          attribs.background_pixel = BlackPixelOfScreen(g_screen);          attribs.background_pixel = BlackPixelOfScreen(g_screen);
1142            attribs.border_pixel = WhitePixelOfScreen(g_screen);
1143          attribs.backing_store = g_ownbackstore ? NotUseful : Always;          attribs.backing_store = g_ownbackstore ? NotUseful : Always;
1144          attribs.override_redirect = g_fullscreen;          attribs.override_redirect = g_fullscreen;
1145            attribs.colormap = g_xcolmap;
1146    
1147            g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), g_xpos, g_ypos, wndwidth,
1148                                  wndheight, 0, g_depth, InputOutput, g_visual,
1149                                  CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
1150                                  CWBorderPixel, &attribs);
1151    
1152            if (g_gc == NULL)
1153                    g_gc = XCreateGC(g_display, g_wnd, 0, NULL);
1154    
1155            if (g_create_bitmap_gc == NULL)
1156                    g_create_bitmap_gc = XCreateGC(g_display, g_wnd, 0, NULL);
1157    
1158            if ((g_ownbackstore) && (g_backstore == 0))
1159            {
1160                    g_backstore = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth);
1161    
1162          g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), 0, 0, wndwidth, wndheight,                  /* clear to prevent rubbish being exposed at startup */
1163                                0, CopyFromParent, InputOutput, CopyFromParent,                  XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen));
1164                                CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);                  XFillRectangle(g_display, g_backstore, g_gc, 0, 0, g_width, g_height);
1165            }
1166    
1167          XStoreName(g_display, g_wnd, g_title);          XStoreName(g_display, g_wnd, g_title);
1168    
# Line 736  ui_create_window(void) Line 1187  ui_create_window(void)
1187                  XFree(sizehints);                  XFree(sizehints);
1188          }          }
1189    
1190            if (g_embed_wnd)
1191            {
1192                    XReparentWindow(g_display, g_wnd, (Window) g_embed_wnd, 0, 0);
1193            }
1194    
1195          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
1196                  VisibilityChangeMask | FocusChangeMask;                  VisibilityChangeMask | FocusChangeMask;
1197    
# Line 767  ui_create_window(void) Line 1223  ui_create_window(void)
1223                  XMaskEvent(g_display, VisibilityChangeMask, &xevent);                  XMaskEvent(g_display, VisibilityChangeMask, &xevent);
1224          }          }
1225          while (xevent.type != VisibilityNotify);          while (xevent.type != VisibilityNotify);
1226            g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured;
1227    
1228          g_focused = False;          g_focused = False;
1229          g_mouse_in_wnd = False;          g_mouse_in_wnd = False;
# Line 776  ui_create_window(void) Line 1233  ui_create_window(void)
1233          g_kill_atom = XInternAtom(g_display, "WM_DELETE_WINDOW", True);          g_kill_atom = XInternAtom(g_display, "WM_DELETE_WINDOW", True);
1234          XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1);          XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1);
1235    
1236            /* create invisible 1x1 cursor to be used as null cursor */
1237            if (g_null_cursor == NULL)
1238                    g_null_cursor = ui_create_cursor(0, 0, 1, 1, null_pointer_mask, null_pointer_data);
1239    
1240          return True;          return True;
1241  }  }
1242    
1243  void  void
1244    ui_resize_window()
1245    {
1246            XSizeHints *sizehints;
1247            Pixmap bs;
1248    
1249            sizehints = XAllocSizeHints();
1250            if (sizehints)
1251            {
1252                    sizehints->flags = PMinSize | PMaxSize;
1253                    sizehints->min_width = sizehints->max_width = g_width;
1254                    sizehints->min_height = sizehints->max_height = g_height;
1255                    XSetWMNormalHints(g_display, g_wnd, sizehints);
1256                    XFree(sizehints);
1257            }
1258    
1259            if (!(g_fullscreen || g_embed_wnd))
1260            {
1261                    XResizeWindow(g_display, g_wnd, g_width, g_height);
1262            }
1263    
1264            /* create new backstore pixmap */
1265            if (g_backstore != 0)
1266            {
1267                    bs = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth);
1268                    XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen));
1269                    XFillRectangle(g_display, bs, g_gc, 0, 0, g_width, g_height);
1270                    XCopyArea(g_display, g_backstore, bs, g_gc, 0, 0, g_width, g_height, 0, 0);
1271                    XFreePixmap(g_display, g_backstore);
1272                    g_backstore = bs;
1273            }
1274    }
1275    
1276    void
1277  ui_destroy_window(void)  ui_destroy_window(void)
1278  {  {
1279          if (g_IC != NULL)          if (g_IC != NULL)
# Line 825  xwin_process_events(void) Line 1319  xwin_process_events(void)
1319          key_translation tr;          key_translation tr;
1320          char str[256];          char str[256];
1321          Status status;          Status status;
         unsigned int state;  
         Window wdummy;  
         int dummy;  
1322    
1323          while (XPending(g_display) > 0)          while (XPending(g_display) > 0)
1324          {          {
# Line 843  xwin_process_events(void) Line 1334  xwin_process_events(void)
1334    
1335                  switch (xevent.type)                  switch (xevent.type)
1336                  {                  {
1337                            case VisibilityNotify:
1338                                    g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured;
1339                                    break;
1340                          case ClientMessage:                          case ClientMessage:
1341                                  /* the window manager told us to quit */                                  /* the window manager told us to quit */
1342                                  if ((xevent.xclient.message_type == g_protocol_atom)                                  if ((xevent.xclient.message_type == g_protocol_atom)
# Line 1000  xwin_process_events(void) Line 1494  xwin_process_events(void)
1494                                  if (xevent.xfocus.mode == NotifyGrab)                                  if (xevent.xfocus.mode == NotifyGrab)
1495                                          break;                                          break;
1496                                  g_focused = True;                                  g_focused = True;
1497                                  XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy,                                  reset_modifier_keys();
                                               &dummy, &dummy, &state);  
                                 reset_modifier_keys(state);  
1498                                  if (g_grab_keyboard && g_mouse_in_wnd)                                  if (g_grab_keyboard && g_mouse_in_wnd)
1499                                          XGrabKeyboard(g_display, g_wnd, True,                                          XGrabKeyboard(g_display, g_wnd, True,
1500                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
# Line 1082  xwin_process_events(void) Line 1574  xwin_process_events(void)
1574  int  int
1575  ui_select(int rdp_socket)  ui_select(int rdp_socket)
1576  {  {
1577          int n = (rdp_socket > g_x_socket) ? rdp_socket + 1 : g_x_socket + 1;          int n;
1578          fd_set rfds, wfds;          fd_set rfds, wfds;
1579            struct timeval tv;
1580            BOOL s_timeout = False;
1581    
1582          while (True)          while (True)
1583          {          {
1584                    n = (rdp_socket > g_x_socket) ? rdp_socket : g_x_socket;
1585                  /* Process any events already waiting */                  /* Process any events already waiting */
1586                  if (!xwin_process_events())                  if (!xwin_process_events())
1587                          /* User quit */                          /* User quit */
# Line 1102  ui_select(int rdp_socket) Line 1597  ui_select(int rdp_socket)
1597                  if (g_dsp_busy)                  if (g_dsp_busy)
1598                  {                  {
1599                          FD_SET(g_dsp_fd, &wfds);                          FD_SET(g_dsp_fd, &wfds);
1600                          n = (g_dsp_fd + 1 > n) ? g_dsp_fd + 1 : n;                          n = (g_dsp_fd > n) ? g_dsp_fd : n;
1601                  }                  }
1602  #endif  #endif
1603                    /* default timeout */
1604                    tv.tv_sec = 60;
1605                    tv.tv_usec = 0;
1606    
1607                    /* add redirection handles */
1608                    rdpdr_add_fds(&n, &rfds, &wfds, &tv, &s_timeout);
1609    
1610                  switch (select(n, &rfds, &wfds, NULL, NULL))                  n++;
1611    
1612                    switch (select(n, &rfds, &wfds, NULL, &tv))
1613                  {                  {
1614                          case -1:                          case -1:
1615                                  error("select: %s\n", strerror(errno));                                  error("select: %s\n", strerror(errno));
1616    
1617                          case 0:                          case 0:
1618                                    /* Abort serial read calls */
1619                                    if (s_timeout)
1620                                            rdpdr_check_fds(&rfds, &wfds, (BOOL) True);
1621                                  continue;                                  continue;
1622                  }                  }
1623    
1624                    rdpdr_check_fds(&rfds, &wfds, (BOOL) False);
1625    
1626                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
1627                          return 1;                          return 1;
1628    
# Line 1137  ui_create_bitmap(int width, int height, Line 1645  ui_create_bitmap(int width, int height,
1645          XImage *image;          XImage *image;
1646          Pixmap bitmap;          Pixmap bitmap;
1647          uint8 *tdata;          uint8 *tdata;
1648            int bitmap_pad;
1649    
1650            if (g_server_bpp == 8)
1651            {
1652                    bitmap_pad = 8;
1653            }
1654            else
1655            {
1656                    bitmap_pad = g_bpp;
1657    
1658                    if (g_bpp == 24)
1659                            bitmap_pad = 32;
1660            }
1661    
1662          tdata = (g_owncolmap ? data : translate_image(width, height, data));          tdata = (g_owncolmap ? data : translate_image(width, height, data));
1663          bitmap = XCreatePixmap(g_display, g_wnd, width, height, g_depth);          bitmap = XCreatePixmap(g_display, g_wnd, width, height, g_depth);
1664          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1665                               (char *) tdata, width, height, g_server_bpp == 8 ? 8 : g_bpp, 0);                               (char *) tdata, width, height, bitmap_pad, 0);
1666    
1667          XPutImage(g_display, bitmap, g_gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, g_create_bitmap_gc, image, 0, 0, 0, 0, width, height);
1668    
1669          XFree(image);          XFree(image);
1670          if (!g_owncolmap)          if (tdata != data)
1671                  xfree(tdata);                  xfree(tdata);
1672          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
1673  }  }
# Line 1156  ui_paint_bitmap(int x, int y, int cx, in Line 1677  ui_paint_bitmap(int x, int y, int cx, in
1677  {  {
1678          XImage *image;          XImage *image;
1679          uint8 *tdata;          uint8 *tdata;
1680            int bitmap_pad;
1681    
1682            if (g_server_bpp == 8)
1683            {
1684                    bitmap_pad = 8;
1685            }
1686            else
1687            {
1688                    bitmap_pad = g_bpp;
1689    
1690                    if (g_bpp == 24)
1691                            bitmap_pad = 32;
1692            }
1693    
1694          tdata = (g_owncolmap ? data : translate_image(width, height, data));          tdata = (g_owncolmap ? data : translate_image(width, height, data));
1695          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1696                               (char *) tdata, width, height, g_server_bpp == 8 ? 8 : g_bpp, 0);                               (char *) tdata, width, height, bitmap_pad, 0);
1697    
1698          if (g_ownbackstore)          if (g_ownbackstore)
1699          {          {
# Line 1171  ui_paint_bitmap(int x, int y, int cx, in Line 1706  ui_paint_bitmap(int x, int y, int cx, in
1706          }          }
1707    
1708          XFree(image);          XFree(image);
1709          if (!g_owncolmap)          if (tdata != data)
1710                  xfree(tdata);                  xfree(tdata);
1711  }  }
1712    
# Line 1187  ui_create_glyph(int width, int height, u Line 1722  ui_create_glyph(int width, int height, u
1722          XImage *image;          XImage *image;
1723          Pixmap bitmap;          Pixmap bitmap;
1724          int scanline;          int scanline;
         GC gc;  
1725    
1726          scanline = (width + 7) / 8;          scanline = (width + 7) / 8;
1727    
1728          bitmap = XCreatePixmap(g_display, g_wnd, width, height, 1);          bitmap = XCreatePixmap(g_display, g_wnd, width, height, 1);
1729          gc = XCreateGC(g_display, bitmap, 0, NULL);          if (g_create_glyph_gc == 0)
1730                    g_create_glyph_gc = XCreateGC(g_display, bitmap, 0, NULL);
1731    
1732          image = XCreateImage(g_display, g_visual, 1, ZPixmap, 0, (char *) data,          image = XCreateImage(g_display, g_visual, 1, ZPixmap, 0, (char *) data,
1733                               width, height, 8, scanline);                               width, height, 8, scanline);
# Line 1200  ui_create_glyph(int width, int height, u Line 1735  ui_create_glyph(int width, int height, u
1735          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
1736          XInitImage(image);          XInitImage(image);
1737    
1738          XPutImage(g_display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, g_create_glyph_gc, image, 0, 0, 0, 0, width, height);
1739    
1740          XFree(image);          XFree(image);
         XFreeGC(g_display, gc);  
1741          return (HGLYPH) bitmap;          return (HGLYPH) bitmap;
1742  }  }
1743    
# Line 1297  ui_destroy_cursor(HCURSOR cursor) Line 1831  ui_destroy_cursor(HCURSOR cursor)
1831          XFreeCursor(g_display, (Cursor) cursor);          XFreeCursor(g_display, (Cursor) cursor);
1832  }  }
1833    
1834    void
1835    ui_set_null_cursor(void)
1836    {
1837            ui_set_cursor(g_null_cursor);
1838    }
1839    
1840  #define MAKE_XCOLOR(xc,c) \  #define MAKE_XCOLOR(xc,c) \
1841                  (xc)->red   = ((c)->red   << 8) | (c)->red; \                  (xc)->red   = ((c)->red   << 8) | (c)->red; \
1842                  (xc)->green = ((c)->green << 8) | (c)->green; \                  (xc)->green = ((c)->green << 8) | (c)->green; \
# Line 1378  ui_create_colourmap(COLOURMAP * colours) Line 1918  ui_create_colourmap(COLOURMAP * colours)
1918    
1919                          }                          }
1920    
1921                            map[i] = colour;
                         /* byte swap here to make translate_image faster */  
                         map[i] = translate_colour(colour);  
1922                  }                  }
1923                  return map;                  return map;
1924          }          }
# Line 1491  ui_patblt(uint8 opcode, Line 2029  ui_patblt(uint8 opcode,
2029          {          {
2030                  case 0: /* Solid */                  case 0: /* Solid */
2031                          SET_FOREGROUND(fgcolour);                          SET_FOREGROUND(fgcolour);
2032                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
2033                          break;                          break;
2034    
2035                  case 2: /* Hatch */                  case 2: /* Hatch */
2036                          fill = (Pixmap) ui_create_glyph(8, 8,                          fill = (Pixmap) ui_create_glyph(8, 8,
2037                                                          hatch_patterns + brush->pattern[0] * 8);                                                          hatch_patterns + brush->pattern[0] * 8);
2038                          SET_FOREGROUND(bgcolour);                          SET_FOREGROUND(fgcolour);
2039                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(bgcolour);
2040                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
2041                          XSetStipple(g_display, g_gc, fill);                          XSetStipple(g_display, g_gc, fill);
2042                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
2043                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
2044                          XSetFillStyle(g_display, g_gc, FillSolid);                          XSetFillStyle(g_display, g_gc, FillSolid);
2045                          XSetTSOrigin(g_display, g_gc, 0, 0);                          XSetTSOrigin(g_display, g_gc, 0, 0);
2046                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
# Line 1512  ui_patblt(uint8 opcode, Line 2050  ui_patblt(uint8 opcode,
2050                          for (i = 0; i != 8; i++)                          for (i = 0; i != 8; i++)
2051                                  ipattern[7 - i] = brush->pattern[i];                                  ipattern[7 - i] = brush->pattern[i];
2052                          fill = (Pixmap) ui_create_glyph(8, 8, ipattern);                          fill = (Pixmap) ui_create_glyph(8, 8, ipattern);
   
2053                          SET_FOREGROUND(bgcolour);                          SET_FOREGROUND(bgcolour);
2054                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(fgcolour);
2055                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
2056                          XSetStipple(g_display, g_gc, fill);                          XSetStipple(g_display, g_gc, fill);
2057                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
2058                            FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
                         FILL_RECTANGLE(x, y, cx, cy);  
   
2059                          XSetFillStyle(g_display, g_gc, FillSolid);                          XSetFillStyle(g_display, g_gc, FillSolid);
2060                          XSetTSOrigin(g_display, g_gc, 0, 0);                          XSetTSOrigin(g_display, g_gc, 0, 0);
2061                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
# Line 1531  ui_patblt(uint8 opcode, Line 2066  ui_patblt(uint8 opcode,
2066          }          }
2067    
2068          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
2069    
2070            if (g_ownbackstore)
2071                    XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);
2072  }  }
2073    
2074  void  void
# Line 1539  ui_screenblt(uint8 opcode, Line 2077  ui_screenblt(uint8 opcode,
2077               /* src */ int srcx, int srcy)               /* src */ int srcx, int srcy)
2078  {  {
2079          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
         XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);  
2080          if (g_ownbackstore)          if (g_ownbackstore)
2081                  XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, y);          {
2082                    if (g_Unobscured)
2083                    {
2084                            XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
2085                            XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x,
2086                                      y);
2087                    }
2088                    else
2089                    {
2090                            XCopyArea(g_display, g_backstore, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
2091                            XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x,
2092                                      y);
2093                    }
2094            }
2095            else
2096            {
2097                    XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
2098            }
2099          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
2100  }  }
2101    
# Line 1636  ui_draw_glyph(int mixmode, Line 2190  ui_draw_glyph(int mixmode,
2190  {\  {\
2191    glyph = cache_get_font (font, ttext[idx]);\    glyph = cache_get_font (font, ttext[idx]);\
2192    if (!(flags & TEXT2_IMPLICIT_X))\    if (!(flags & TEXT2_IMPLICIT_X))\
2193      {\
2194        xyoffset = ttext[++idx];\
2195        if ((xyoffset & 0x80))\
2196      {\      {\
2197        xyoffset = ttext[++idx];\        if (flags & TEXT2_VERTICAL)\
2198        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;\  
         }\  
2199        else\        else\
2200          {\          x += ttext[idx+1] | (ttext[idx+2] << 8);\
2201            if (flags & TEXT2_VERTICAL) \        idx += 2;\
             y += xyoffset;\  
           else\  
             x += xyoffset;\  
         }\  
2202      }\      }\
2203    if (glyph != NULL)\      else\
2204      {\      {\
2205        ui_draw_glyph (mixmode, x + glyph->offset,\        if (flags & TEXT2_VERTICAL)\
2206                       y + glyph->baseline,\          y += xyoffset;\
2207                       glyph->width, glyph->height,\        else\
2208                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\          x += xyoffset;\
       if (flags & TEXT2_IMPLICIT_X)\  
         x += glyph->width;\  
2209      }\      }\
2210      }\
2211      if (glyph != NULL)\
2212      {\
2213        x1 = x + glyph->offset;\
2214        y1 = y + glyph->baseline;\
2215        XSetStipple(g_display, g_gc, (Pixmap) glyph->pixmap);\
2216        XSetTSOrigin(g_display, g_gc, x1, y1);\
2217        FILL_RECTANGLE_BACKSTORE(x1, y1, glyph->width, glyph->height);\
2218        if (flags & TEXT2_IMPLICIT_X)\
2219          x += glyph->width;\
2220      }\
2221  }  }
2222    
2223  void  void
# Line 1672  ui_draw_text(uint8 font, uint8 flags, in Line 2227  ui_draw_text(uint8 font, uint8 flags, in
2227               int fgcolour, uint8 * text, uint8 length)               int fgcolour, uint8 * text, uint8 length)
2228  {  {
2229          FONTGLYPH *glyph;          FONTGLYPH *glyph;
2230          int i, j, xyoffset;          int i, j, xyoffset, x1, y1;
2231          DATABLOB *entry;          DATABLOB *entry;
2232    
2233          SET_FOREGROUND(bgcolour);          SET_FOREGROUND(bgcolour);
2234    
2235            /* Sometimes, the boxcx value is something really large, like
2236               32691. This makes XCopyArea fail with Xvnc. The code below
2237               is a quick fix. */
2238            if (boxx + boxcx > g_width)
2239                    boxcx = g_width - boxx;
2240    
2241          if (boxcx > 1)          if (boxcx > 1)
2242          {          {
2243                  FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);                  FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);
# Line 1686  ui_draw_text(uint8 font, uint8 flags, in Line 2247  ui_draw_text(uint8 font, uint8 flags, in
2247                  FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);                  FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);
2248          }          }
2249    
2250            SET_FOREGROUND(fgcolour);
2251            SET_BACKGROUND(bgcolour);
2252            XSetFillStyle(g_display, g_gc, FillStippled);
2253    
2254          /* Paint text, character by character */          /* Paint text, character by character */
2255          for (i = 0; i < length;)          for (i = 0; i < length;)
2256          {          {
# Line 1736  ui_draw_text(uint8 font, uint8 flags, in Line 2301  ui_draw_text(uint8 font, uint8 flags, in
2301                                  break;                                  break;
2302                  }                  }
2303          }          }
2304    
2305            XSetFillStyle(g_display, g_gc, FillSolid);
2306    
2307          if (g_ownbackstore)          if (g_ownbackstore)
2308          {          {
2309                  if (boxcx > 1)                  if (boxcx > 1)
# Line 1797  ui_desktop_restore(uint32 offset, int x, Line 2365  ui_desktop_restore(uint32 offset, int x,
2365    
2366          XFree(image);          XFree(image);
2367  }  }
2368    
2369    /* these do nothing here but are used in uiports */
2370    void
2371    ui_begin_update(void)
2372    {
2373    }
2374    
2375    void
2376    ui_end_update(void)
2377    {
2378    }

Legend:
Removed from v.478  
changed lines
  Added in v.828

  ViewVC Help
Powered by ViewVC 1.1.26