/[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 203 by matthewc, Thu Sep 26 14:04:30 2002 UTC revision 501 by stargo, Fri Oct 17 08:23:47 2003 UTC
# Line 1  Line 1 
1  /*  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     User interface services - X Window System     User interface services - X Window System
4     Copyright (C) Matthew Chapman 1999-2001     Copyright (C) Matthew Chapman 1999-2002
5    
6     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 23  Line 23 
23  #include <time.h>  #include <time.h>
24  #include <errno.h>  #include <errno.h>
25  #include "rdesktop.h"  #include "rdesktop.h"
26    #include "xproto.h"
27    
28  extern int width;  extern int g_width;
29  extern int height;  extern int g_height;
30  extern BOOL sendmotion;  extern BOOL g_sendmotion;
31  extern BOOL fullscreen;  extern BOOL g_fullscreen;
32  extern BOOL grab_keyboard;  extern BOOL g_grab_keyboard;
33  extern char title[];  extern BOOL g_hide_decorations;
34  BOOL enable_compose = False;  extern char g_title[];
35    extern int g_server_bpp;
36  Display *display;  extern int g_win_button_size;
37  static int x_socket;  
38  static Screen *screen;  Display *g_display;
39  static Window wnd;  Time g_last_gesturetime;
40  static GC gc;  static int g_x_socket;
41  static Visual *visual;  static Screen *g_screen;
42  static int depth;  Window g_wnd;
43  static int bpp;  BOOL g_enable_compose = False;
44  static XIM IM;  static GC g_gc;
45  static XIC IC;  static Visual *g_visual;
46  static XModifierKeymap *mod_map;  static int g_depth;
47  static Cursor current_cursor;  static int g_bpp;
48    static XIM g_IM;
49    static XIC g_IC;
50    static XModifierKeymap *g_mod_map;
51    static Cursor g_current_cursor;
52    static Atom g_protocol_atom, g_kill_atom;
53    static BOOL g_focused;
54    static BOOL g_mouse_in_wnd;
55    
56  /* endianness */  /* endianness */
57  static BOOL host_be;  static BOOL g_host_be;
58  static BOOL xserver_be;  static BOOL g_xserver_be;
59    
60  /* software backing store */  /* software backing store */
61  static BOOL ownbackstore;  static BOOL g_ownbackstore;
62  static Pixmap backstore;  static Pixmap g_backstore;
63    
64    /* Moving in single app mode */
65    static BOOL g_moving_wnd;
66    static int g_move_x_offset = 0;
67    static int g_move_y_offset = 0;
68    
69    #ifdef WITH_RDPSND
70    extern int g_dsp_fd;
71    extern BOOL g_dsp_busy;
72    extern BOOL g_rdpsnd;
73    #endif
74    
75    /* MWM decorations */
76    #define MWM_HINTS_DECORATIONS   (1L << 1)
77    #define PROP_MOTIF_WM_HINTS_ELEMENTS    5
78    typedef struct
79    {
80            uint32 flags;
81            uint32 functions;
82            uint32 decorations;
83            sint32 inputMode;
84            uint32 status;
85    }
86    PropMotifWmHints;
87    
88    typedef struct
89    {
90            uint32 red;
91            uint32 green;
92            uint32 blue;
93    }
94    PixelColour;
95    
96    
97  #define FILL_RECTANGLE(x,y,cx,cy)\  #define FILL_RECTANGLE(x,y,cx,cy)\
98  { \  { \
99          XFillRectangle(display, wnd, gc, x, y, cx, cy); \          XFillRectangle(g_display, g_wnd, g_gc, x, y, cx, cy); \
100          if (ownbackstore) \          if (g_ownbackstore) \
101                  XFillRectangle(display, backstore, gc, x, y, cx, cy); \                  XFillRectangle(g_display, g_backstore, g_gc, x, y, cx, cy); \
102  }  }
103    
104  /* colour maps */  #define FILL_RECTANGLE_BACKSTORE(x,y,cx,cy)\
105  static Colormap xcolmap;  { \
106  static uint32 *colmap;          XFillRectangle(g_display, g_ownbackstore ? g_backstore : g_wnd, g_gc, x, y, cx, cy); \
107    }
108    
109  #define SET_FOREGROUND(col)     XSetForeground(display, gc, translate_colour(colmap[col]));  /* colour maps */
110  #define SET_BACKGROUND(col)     XSetBackground(display, gc, translate_colour(colmap[col]));  BOOL g_owncolmap = False;
111    static Colormap g_xcolmap;
112    static uint32 *g_colmap = NULL;
113    
114    #define TRANSLATE(col)          ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : translate_colour(g_colmap[col]) )
115    #define SET_FOREGROUND(col)     XSetForeground(g_display, g_gc, TRANSLATE(col));
116    #define SET_BACKGROUND(col)     XSetBackground(g_display, g_gc, TRANSLATE(col));
117    
118  static int rop2_map[] = {  static int rop2_map[] = {
119          GXclear,                /* 0 */          GXclear,                /* 0 */
# Line 86  static int rop2_map[] = { Line 134  static int rop2_map[] = {
134          GXset                   /* 1 */          GXset                   /* 1 */
135  };  };
136    
137  #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]); }
138  #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); }
139    
140  static void  static void
141  translate8(uint8 * data, uint8 * out, uint8 * end)  mwm_hide_decorations(void)
142    {
143            PropMotifWmHints motif_hints;
144            Atom hintsatom;
145    
146            /* setup the property */
147            motif_hints.flags = MWM_HINTS_DECORATIONS;
148            motif_hints.decorations = 0;
149    
150            /* get the atom for the property */
151            hintsatom = XInternAtom(g_display, "_MOTIF_WM_HINTS", False);
152            if (!hintsatom)
153            {
154                    warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n");
155                    return;
156            }
157    
158            XChangeProperty(g_display, g_wnd, hintsatom, hintsatom, 32, PropModeReplace,
159                            (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
160    }
161    
162    static PixelColour
163    split_colour15(uint32 colour)
164    {
165            PixelColour rv;
166            rv.red = (colour & 0x7c00) >> 10;
167            rv.red = (rv.red * 0xff) / 0x1f;
168            rv.green = (colour & 0x03e0) >> 5;
169            rv.green = (rv.green * 0xff) / 0x1f;
170            rv.blue = (colour & 0x1f);
171            rv.blue = (rv.blue * 0xff) / 0x1f;
172            return rv;
173    }
174    
175    static PixelColour
176    split_colour16(uint32 colour)
177    {
178            PixelColour rv;
179            rv.red = (colour & 0xf800) >> 11;
180            rv.red = (rv.red * 0xff) / 0x1f;
181            rv.green = (colour & 0x07e0) >> 5;
182            rv.green = (rv.green * 0xff) / 0x3f;
183            rv.blue = (colour & 0x001f);
184            rv.blue = (rv.blue * 0xff) / 0x1f;
185            return rv;
186    }
187    
188    static PixelColour
189    split_colour24(uint32 colour)
190    {
191            PixelColour rv;
192            rv.blue = (colour & 0xff0000) >> 16;
193            rv.green = (colour & 0xff00) >> 8;
194            rv.red = (colour & 0xff);
195            return rv;
196    }
197    
198    static uint32
199    make_colour16(PixelColour pc)
200    {
201            pc.red = (pc.red * 0x1f) / 0xff;
202            pc.green = (pc.green * 0x3f) / 0xff;
203            pc.blue = (pc.blue * 0x1f) / 0xff;
204            return (pc.red << 11) | (pc.green << 5) | pc.blue;
205    }
206    
207    static uint32
208    make_colour24(PixelColour pc)
209    {
210            if (g_xserver_be)
211            {
212                    return pc.red | (pc.green << 8) | (pc.blue << 16);
213            }
214            else
215            {
216                    return (pc.red << 16) | (pc.green << 8) | pc.blue;
217            }
218    }
219    
220    static uint32
221    make_colour32(PixelColour pc)
222    {
223            if (g_xserver_be)
224            {
225                    return pc.red | (pc.green << 8) | (pc.blue << 16);
226            }
227            else
228            {
229                    return (pc.red << 16) | (pc.green << 8) | pc.blue;
230            }
231    }
232    
233    #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
234    #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }
235    #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
236                            x = (x << 16) | (x >> 16); }
237    
238    static uint32
239    translate_colour(uint32 colour)
240    {
241            switch (g_server_bpp)
242            {
243                    case 15:
244                            switch (g_bpp)
245                            {
246                                    case 16:
247                                            colour = make_colour16(split_colour15(colour));
248                                            break;
249                                    case 24:
250                                            colour = make_colour24(split_colour15(colour));
251                                            break;
252                                    case 32:
253                                            colour = make_colour32(split_colour15(colour));
254                                            break;
255                            }
256                            break;
257                    case 16:
258                            switch (g_bpp)
259                            {
260                                    case 16:
261                                            break;
262                                    case 24:
263                                            colour = make_colour24(split_colour16(colour));
264                                            break;
265                                    case 32:
266                                            colour = make_colour32(split_colour16(colour));
267                                            break;
268                            }
269                            break;
270                    case 24:
271                            switch (g_bpp)
272                            {
273                                    case 16:
274                                            colour = make_colour16(split_colour24(colour));
275                                            break;
276                                    case 24:
277                                            break;
278                                    case 32:
279                                            colour = make_colour32(split_colour24(colour));
280                                            break;
281                            }
282                            break;
283            }
284            return colour;
285    }
286    
287    static void
288    translate8to8(uint8 * data, uint8 * out, uint8 * end)
289  {  {
290          while (out < end)          while (out < end)
291                  *(out++) = (uint8) colmap[*(data++)];                  *(out++) = (uint8) g_colmap[*(data++)];
292  }  }
293    
294  static void  static void
295  translate16(uint8 * data, uint16 * out, uint16 * end)  translate8to16(uint8 * data, uint16 * out, uint16 * end)
296  {  {
297          while (out < end)          while (out < end)
298                  *(out++) = (uint16) colmap[*(data++)];                  *(out++) = (uint16) g_colmap[*(data++)];
299  }  }
300    
301  /* little endian - conversion happens when colourmap is built */  /* little endian - conversion happens when colourmap is built */
302  static void  static void
303  translate24(uint8 * data, uint8 * out, uint8 * end)  translate8to24(uint8 * data, uint8 * out, uint8 * end)
304  {  {
305          uint32 value;          uint32 value;
306    
307          while (out < end)          while (out < end)
308          {          {
309                  value = colmap[*(data++)];                  value = g_colmap[*(data++)];
310                  *(out++) = value;                  *(out++) = value;
311                  *(out++) = value >> 8;                  *(out++) = value >> 8;
312                  *(out++) = value >> 16;                  *(out++) = value >> 16;
# Line 119  translate24(uint8 * data, uint8 * out, u Line 314  translate24(uint8 * data, uint8 * out, u
314  }  }
315    
316  static void  static void
317  translate32(uint8 * data, uint32 * out, uint32 * end)  translate8to32(uint8 * data, uint32 * out, uint32 * end)
318  {  {
319          while (out < end)          while (out < end)
320                  *(out++) = colmap[*(data++)];                  *(out++) = g_colmap[*(data++)];
321  }  }
322    
323  static uint8 *  /* todo the remaining translate function might need some big endian check ?? */
324  translate_image(int width, int height, uint8 * data)  
325    static void
326    translate15to16(uint16 * data, uint8 * out, uint8 * end)
327  {  {
328          int size = width * height * bpp / 8;          uint16 pixel;
329          uint8 *out = xmalloc(size);          uint16 value;
         uint8 *end = out + size;  
330    
331          switch (bpp)          while (out < end)
332          {          {
333                  case 8:                  pixel = *(data++);
                         translate8(data, out, end);  
                         break;  
334    
335                  case 16:                  if (g_host_be)
336                          translate16(data, (uint16 *) out, (uint16 *) end);                  {
337                          break;                  BSWAP16(pixel)}
338    
339                  case 24:                  value = make_colour16(split_colour15(pixel));
                         translate24(data, out, end);  
                         break;  
340    
341                  case 32:                  if (g_xserver_be)
342                          translate32(data, (uint32 *) out, (uint32 *) end);                  {
343                          break;                          *(out++) = value >> 8;
344                            *(out++) = value;
345                    }
346                    else
347                    {
348                            *(out++) = value;
349                            *(out++) = value >> 8;
350                    }
351          }          }
352    }
353    
354          return out;  static void
355    translate15to24(uint16 * data, uint8 * out, uint8 * end)
356    {
357            uint32 value;
358            uint16 pixel;
359    
360            while (out < end)
361            {
362                    pixel = *(data++);
363    
364                    if (g_host_be)
365                    {
366                    BSWAP16(pixel)}
367    
368                    value = make_colour24(split_colour15(pixel));
369                    if (g_xserver_be)
370                    {
371                            *(out++) = value >> 16;
372                            *(out++) = value >> 8;
373                            *(out++) = value;
374                    }
375                    else
376                    {
377                            *(out++) = value;
378                            *(out++) = value >> 8;
379                            *(out++) = value >> 16;
380                    }
381            }
382  }  }
383    
384  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }  static void
385  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }  translate15to32(uint16 * data, uint8 * out, uint8 * end)
386  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \  {
387                          x = (x << 16) | (x >> 16); }          uint16 pixel;
388            uint32 value;
389    
390  static uint32          while (out < end)
391  translate_colour(uint32 colour)          {
392                    pixel = *(data++);
393    
394                    if (g_host_be)
395                    {
396                            BSWAP16(pixel);
397                    }
398    
399                    value = make_colour32(split_colour15(pixel));
400    
401                    if (g_xserver_be)
402                    {
403                            *(out++) = value >> 24;
404                            *(out++) = value >> 16;
405                            *(out++) = value >> 8;
406                            *(out++) = value;
407                    }
408                    else
409                    {
410                            *(out++) = value;
411                            *(out++) = value >> 8;
412                            *(out++) = value >> 16;
413                            *(out++) = value >> 24;
414                    }
415            }
416    }
417    
418    static void
419    translate16to16(uint16 * data, uint16 * out, uint16 * end)
420  {  {
421          switch (bpp)          uint16 value;
422    
423            if (g_xserver_be)
424          {          {
425                  case 16:                  while (out < end)
426                          if (host_be != xserver_be)                  {
427                                  BSWAP16(colour);                          value = *data;
428                          break;                          BSWAP16(value);
429                            *out = value;
430                            data++;
431                            out++;
432                    }
433    
434            }
435            else
436            {
437                    while (out < end)
438                    {
439                            *out = *data;
440                            out++;
441                            data++;
442                    }
443            }
444    }
445    
446    
447    static void
448    translate16to24(uint16 * data, uint8 * out, uint8 * end)
449    {
450            uint32 value;
451            uint16 pixel;
452    
453            while (out < end)
454            {
455                    pixel = *(data++);
456    
457                    if (g_host_be)
458                    {
459                    BSWAP16(pixel)}
460    
461                    value = make_colour24(split_colour16(pixel));
462    
463                    if (g_xserver_be)
464                    {
465                            *(out++) = value >> 16;
466                            *(out++) = value >> 8;
467                            *(out++) = value;
468                    }
469                    else
470                    {
471                            *(out++) = value;
472                            *(out++) = value >> 8;
473                            *(out++) = value >> 16;
474                    }
475            }
476    }
477    
478    static void
479    translate16to32(uint16 * data, uint8 * out, uint8 * end)
480    {
481            uint16 pixel;
482            uint32 value;
483    
484            while (out < end)
485            {
486                    pixel = *(data++);
487    
488                    if (g_host_be)
489                    {
490                    BSWAP16(pixel)}
491    
492                    value = make_colour32(split_colour16(pixel));
493    
494                    if (g_xserver_be)
495                    {
496                            *(out++) = value >> 24;
497                            *(out++) = value >> 16;
498                            *(out++) = value >> 8;
499                            *(out++) = value;
500                    }
501                    else
502                    {
503                            *(out++) = value;
504                            *(out++) = value >> 8;
505                            *(out++) = value >> 16;
506                            *(out++) = value >> 24;
507                    }
508            }
509    }
510    
511    static void
512    translate24to16(uint8 * data, uint8 * out, uint8 * end)
513    {
514            uint32 pixel = 0;
515            uint16 value;
516            while (out < end)
517            {
518                    pixel = *(data++) << 16;
519                    pixel |= *(data++) << 8;
520                    pixel |= *(data++);
521    
522                    value = (uint16) make_colour16(split_colour24(pixel));
523    
524                    if (g_xserver_be)
525                    {
526                            *(out++) = value >> 8;
527                            *(out++) = value;
528                    }
529                    else
530                    {
531                            *(out++) = value;
532                            *(out++) = value >> 8;
533                    }
534            }
535    }
536    
537    static void
538    translate24to24(uint8 * data, uint8 * out, uint8 * end)
539    {
540            while (out < end)
541            {
542                    *(out++) = (*(data++));
543            }
544    }
545    
546    static void
547    translate24to32(uint8 * data, uint8 * out, uint8 * end)
548    {
549            while (out < end)
550            {
551                    if (g_xserver_be)
552                    {
553                            *(out++) = 0x00;
554                            *(out++) = *(data++);
555                            *(out++) = *(data++);
556                            *(out++) = *(data++);
557                    }
558                    else
559                    {
560                            *(out++) = *(data++);
561                            *(out++) = *(data++);
562                            *(out++) = *(data++);
563                            *(out++) = 0x00;
564                    }
565            }
566    }
567    
568    static uint8 *
569    translate_image(int width, int height, uint8 * data)
570    {
571            int size = width * height * g_bpp / 8;
572            uint8 *out = (uint8 *) xmalloc(size);
573            uint8 *end = out + size;
574    
575            switch (g_server_bpp)
576            {
577                  case 24:                  case 24:
578                          if (xserver_be)                          switch (g_bpp)
579                                  BSWAP24(colour);                          {
580                                    case 32:
581                                            translate24to32(data, out, end);
582                                            break;
583                                    case 24:
584                                            translate24to24(data, out, end);
585                                            break;
586                                    case 16:
587                                            translate24to16(data, out, end);
588                                            break;
589                            }
590                          break;                          break;
591                    case 16:
592                  case 32:                          switch (g_bpp)
593                          if (host_be != xserver_be)                          {
594                                  BSWAP32(colour);                                  case 32:
595                                            translate16to32((uint16 *) data, out, end);
596                                            break;
597                                    case 24:
598                                            translate16to24((uint16 *) data, out, end);
599                                            break;
600                                    case 16:
601                                            translate16to16((uint16 *) data, (uint16 *) out,
602                                                            (uint16 *) end);
603                                            break;
604                            }
605                            break;
606                    case 15:
607                            switch (g_bpp)
608                            {
609                                    case 32:
610                                            translate15to32((uint16 *) data, out, end);
611                                            break;
612                                    case 24:
613                                            translate15to24((uint16 *) data, out, end);
614                                            break;
615                                    case 16:
616                                            translate15to16((uint16 *) data, out, end);
617                                            break;
618                            }
619                            break;
620                    case 8:
621                            switch (g_bpp)
622                            {
623                                    case 8:
624                                            translate8to8(data, out, end);
625                                            break;
626                                    case 16:
627                                            translate8to16(data, (uint16 *) out, (uint16 *) end);
628                                            break;
629                                    case 24:
630                                            translate8to24(data, out, end);
631                                            break;
632                                    case 32:
633                                            translate8to32(data, (uint32 *) out, (uint32 *) end);
634                                            break;
635                            }
636                          break;                          break;
637          }          }
638            return out;
         return colour;  
639  }  }
640    
641  BOOL  BOOL
642  get_key_state(uint32 keysym, unsigned int state)  get_key_state(unsigned int state, uint32 keysym)
643  {  {
644          int modifierpos, key, keysymMask = 0;          int modifierpos, key, keysymMask = 0;
645          int offset;          int offset;
646    
647          KeyCode keycode = XKeysymToKeycode(display, keysym);          KeyCode keycode = XKeysymToKeycode(g_display, keysym);
648    
649          if (keycode == NoSymbol)          if (keycode == NoSymbol)
650                  return False;                  return False;
651    
652          for (modifierpos = 0; modifierpos < 8; modifierpos++)          for (modifierpos = 0; modifierpos < 8; modifierpos++)
653          {          {
654                  offset = mod_map->max_keypermod * modifierpos;                  offset = g_mod_map->max_keypermod * modifierpos;
655    
656                  for (key = 0; key < mod_map->max_keypermod; key++)                  for (key = 0; key < g_mod_map->max_keypermod; key++)
657                  {                  {
658                          if (mod_map->modifiermap[offset + key] == keycode)                          if (g_mod_map->modifiermap[offset + key] == keycode)
659                                  keysymMask |= 1 << modifierpos;                                  keysymMask |= 1 << modifierpos;
660                  }                  }
661          }          }
# Line 215  ui_init(void) Line 670  ui_init(void)
670          uint16 test;          uint16 test;
671          int i;          int i;
672    
673          display = XOpenDisplay(NULL);          g_display = XOpenDisplay(NULL);
674          if (display == NULL)          if (g_display == NULL)
675          {          {
676                  error("Failed to open display\n");                  error("Failed to open display: %s\n", XDisplayName(NULL));
677                  return False;                  return False;
678          }          }
679    
680          x_socket = ConnectionNumber(display);          g_x_socket = ConnectionNumber(g_display);
681          screen = DefaultScreenOfDisplay(display);          g_screen = DefaultScreenOfDisplay(g_display);
682          visual = DefaultVisualOfScreen(screen);          g_visual = DefaultVisualOfScreen(g_screen);
683          depth = DefaultDepthOfScreen(screen);          g_depth = DefaultDepthOfScreen(g_screen);
684    
685          pfm = XListPixmapFormats(display, &i);          pfm = XListPixmapFormats(g_display, &i);
686          if (pfm != NULL)          if (pfm != NULL)
687          {          {
688                  /* Use maximum bpp for this depth - this is generally                  /* Use maximum bpp for this depth - this is generally
689                     desirable, e.g. 24 bits->32 bits. */                     desirable, e.g. 24 bits->32 bits. */
690                  while (i--)                  while (i--)
691                  {                  {
692                          if ((pfm[i].depth == depth) && (pfm[i].bits_per_pixel > bpp))                          if ((pfm[i].depth == g_depth) && (pfm[i].bits_per_pixel > g_bpp))
693                          {                          {
694                                  bpp = pfm[i].bits_per_pixel;                                  g_bpp = pfm[i].bits_per_pixel;
695                          }                          }
696                  }                  }
697                  XFree(pfm);                  XFree(pfm);
698          }          }
699    
700          if (bpp < 8)          if (g_bpp < 8)
701          {          {
702                  error("Less than 8 bpp not currently supported.\n");                  error("Less than 8 bpp not currently supported.\n");
703                  XCloseDisplay(display);                  XCloseDisplay(g_display);
704                  return False;                  return False;
705          }          }
706    
707          xcolmap = DefaultColormapOfScreen(screen);          if (g_owncolmap != True)
708          gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL);          {
709                    g_xcolmap = DefaultColormapOfScreen(g_screen);
710                    if (g_depth <= 8)
711                            warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n");
712            }
713    
714            g_gc = XCreateGC(g_display, RootWindowOfScreen(g_screen), 0, NULL);
715    
716          if (DoesBackingStore(screen) != Always)          if (DoesBackingStore(g_screen) != Always)
717                  ownbackstore = True;                  g_ownbackstore = True;
718    
719          test = 1;          test = 1;
720          host_be = !(BOOL) (*(uint8 *) (&test));          g_host_be = !(BOOL) (*(uint8 *) (&test));
721          xserver_be = (ImageByteOrder(display) == MSBFirst);          g_xserver_be = (ImageByteOrder(g_display) == MSBFirst);
722    
723          if (fullscreen)          /*
724             * Determine desktop size
725             */
726            if (g_width < 0)
727          {          {
728                  width = WidthOfScreen(screen);                  /* Percent of screen */
729                  height = HeightOfScreen(screen);                  g_height = HeightOfScreen(g_screen) * (-g_width) / 100;
730                    g_width = WidthOfScreen(g_screen) * (-g_width) / 100;
731            }
732            else if (g_width == 0)
733            {
734                    /* Fetch geometry from _NET_WORKAREA */
735                    uint32 x, y, cx, cy;
736    
737                    if (get_current_workarea(&x, &y, &cx, &cy) == 0)
738                    {
739                            g_width = cx;
740                            g_height = cy;
741                    }
742                    else
743                    {
744                            warning("Failed to get workarea: probably your window manager does not support extended hints\n");
745                            g_width = 800;
746                            g_height = 600;
747                    }
748            }
749            else if (g_fullscreen)
750            {
751                    g_width = WidthOfScreen(g_screen);
752                    g_height = HeightOfScreen(g_screen);
753          }          }
754    
755          /* make sure width is a multiple of 4 */          /* make sure width is a multiple of 4 */
756          width = (width + 3) & ~3;          g_width = (g_width + 3) & ~3;
757    
758          if (ownbackstore)          if (g_ownbackstore)
759          {          {
760                  backstore =                  g_backstore =
761                          XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth);                          XCreatePixmap(g_display, RootWindowOfScreen(g_screen), g_width, g_height,
762                                          g_depth);
763    
764                  /* clear to prevent rubbish being exposed at startup */                  /* clear to prevent rubbish being exposed at startup */
765                  XSetForeground(display, gc, BlackPixelOfScreen(screen));                  XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen));
766                  XFillRectangle(display, backstore, gc, 0, 0, width, height);                  XFillRectangle(g_display, g_backstore, g_gc, 0, 0, g_width, g_height);
767          }          }
768    
769          mod_map = XGetModifierMapping(display);          g_mod_map = XGetModifierMapping(g_display);
   
         if (enable_compose)  
                 IM = XOpenIM(display, NULL, NULL, NULL);  
770    
771          xkeymap_init();          xkeymap_init();
772    
773            if (g_enable_compose)
774                    g_IM = XOpenIM(g_display, NULL, NULL, NULL);
775    
776            xclip_init();
777    
778            /* todo take this out when high colour is done */
779            printf("server bpp %d client bpp %d depth %d\n", g_server_bpp, g_bpp, g_depth);
780    
781          return True;          return True;
782  }  }
783    
784  void  void
785  ui_deinit(void)  ui_deinit(void)
786  {  {
787          if (IM != NULL)          if (g_IM != NULL)
788                  XCloseIM(IM);                  XCloseIM(g_IM);
789    
790          XFreeModifierMap(mod_map);          XFreeModifiermap(g_mod_map);
791    
792          if (ownbackstore)          if (g_ownbackstore)
793                  XFreePixmap(display, backstore);                  XFreePixmap(g_display, g_backstore);
794    
795          XFreeGC(display, gc);          XFreeGC(g_display, g_gc);
796          XCloseDisplay(display);          XCloseDisplay(g_display);
797          display = NULL;          g_display = NULL;
798  }  }
799    
800  BOOL  BOOL
# Line 313  ui_create_window(void) Line 807  ui_create_window(void)
807          long input_mask, ic_input_mask;          long input_mask, ic_input_mask;
808          XEvent xevent;          XEvent xevent;
809    
810          wndwidth = fullscreen ? WidthOfScreen(screen) : width;          wndwidth = g_fullscreen ? WidthOfScreen(g_screen) : g_width;
811          wndheight = fullscreen ? HeightOfScreen(screen) : height;          wndheight = g_fullscreen ? HeightOfScreen(g_screen) : g_height;
812    
813          attribs.background_pixel = BlackPixelOfScreen(screen);          attribs.background_pixel = BlackPixelOfScreen(g_screen);
814          attribs.backing_store = ownbackstore ? NotUseful : Always;          attribs.backing_store = g_ownbackstore ? NotUseful : Always;
815          attribs.override_redirect = fullscreen;          attribs.override_redirect = g_fullscreen;
   
         wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight,  
                             0, CopyFromParent, InputOutput, CopyFromParent,  
                             CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);  
816    
817          XStoreName(display, wnd, title);          g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), 0, 0, wndwidth, wndheight,
818                                  0, CopyFromParent, InputOutput, CopyFromParent,
819                                  CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
820    
821            XStoreName(g_display, g_wnd, g_title);
822    
823            if (g_hide_decorations)
824                    mwm_hide_decorations();
825    
826          classhints = XAllocClassHint();          classhints = XAllocClassHint();
827          if (classhints != NULL)          if (classhints != NULL)
828          {          {
829                  classhints->res_name = classhints->res_class = "rdesktop";                  classhints->res_name = classhints->res_class = "rdesktop";
830                  XSetClassHint(display, wnd, classhints);                  XSetClassHint(g_display, g_wnd, classhints);
831                  XFree(classhints);                  XFree(classhints);
832          }          }
833    
# Line 338  ui_create_window(void) Line 835  ui_create_window(void)
835          if (sizehints)          if (sizehints)
836          {          {
837                  sizehints->flags = PMinSize | PMaxSize;                  sizehints->flags = PMinSize | PMaxSize;
838                  sizehints->min_width = sizehints->max_width = width;                  sizehints->min_width = sizehints->max_width = g_width;
839                  sizehints->min_height = sizehints->max_height = height;                  sizehints->min_height = sizehints->max_height = g_height;
840                  XSetWMNormalHints(display, wnd, sizehints);                  XSetWMNormalHints(g_display, g_wnd, sizehints);
841                  XFree(sizehints);                  XFree(sizehints);
842          }          }
843    
844          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
845                  StructureNotifyMask | FocusChangeMask;                  VisibilityChangeMask | FocusChangeMask;
846    
847          if (sendmotion)          if (g_sendmotion)
848                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
849          if (ownbackstore)          if (g_ownbackstore)
850                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
851            if (g_fullscreen || g_grab_keyboard)
852                    input_mask |= EnterWindowMask;
853            if (g_grab_keyboard)
854                    input_mask |= LeaveWindowMask;
855    
856          if (IM != NULL)          if (g_IM != NULL)
857          {          {
858                  IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),                  g_IC = XCreateIC(g_IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
859                                 XNClientWindow, wnd, XNFocusWindow, wnd, NULL);                                   XNClientWindow, g_wnd, XNFocusWindow, g_wnd, NULL);
860    
861                  if ((IC != NULL)                  if ((g_IC != NULL)
862                      && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))                      && (XGetICValues(g_IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))
863                          input_mask |= ic_input_mask;                          input_mask |= ic_input_mask;
864          }          }
865    
866          XSelectInput(display, wnd, input_mask);          XSelectInput(g_display, g_wnd, input_mask);
867          XMapWindow(display, wnd);          XMapWindow(g_display, g_wnd);
868    
869          /* wait for MapNotify */          /* wait for VisibilityNotify */
870          do          do
871          {          {
872                  XMaskEvent(display, StructureNotifyMask, &xevent);                  XMaskEvent(g_display, VisibilityChangeMask, &xevent);
873          }          }
874          while (xevent.type != MapNotify);          while (xevent.type != VisibilityNotify);
875    
876            g_focused = False;
877            g_mouse_in_wnd = False;
878    
879          if (fullscreen)          /* handle the WM_DELETE_WINDOW protocol */
880                  XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);          g_protocol_atom = XInternAtom(g_display, "WM_PROTOCOLS", True);
881            g_kill_atom = XInternAtom(g_display, "WM_DELETE_WINDOW", True);
882            XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1);
883    
884          return True;          return True;
885  }  }
# Line 381  ui_create_window(void) Line 887  ui_create_window(void)
887  void  void
888  ui_destroy_window(void)  ui_destroy_window(void)
889  {  {
890          if (IC != NULL)          if (g_IC != NULL)
891                  XDestroyIC(IC);                  XDestroyIC(g_IC);
892    
893          XDestroyWindow(display, wnd);          XDestroyWindow(g_display, g_wnd);
894  }  }
895    
896  void  void
# Line 392  xwin_toggle_fullscreen(void) Line 898  xwin_toggle_fullscreen(void)
898  {  {
899          Pixmap contents = 0;          Pixmap contents = 0;
900    
901          if (!ownbackstore)          if (!g_ownbackstore)
902          {          {
903                  /* need to save contents of window */                  /* need to save contents of window */
904                  contents = XCreatePixmap(display, wnd, width, height, depth);                  contents = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth);
905                  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);
906          }          }
907    
908          ui_destroy_window();          ui_destroy_window();
909          fullscreen = !fullscreen;          g_fullscreen = !g_fullscreen;
910          ui_create_window();          ui_create_window();
911    
912          XDefineCursor(display, wnd, current_cursor);          XDefineCursor(g_display, g_wnd, g_current_cursor);
913    
914          if (!ownbackstore)          if (!g_ownbackstore)
915          {          {
916                  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);
917                  XFreePixmap(display, contents);                  XFreePixmap(g_display, contents);
918          }          }
919  }  }
920    
921  /* Process all events in Xlib queue */  /* Process all events in Xlib queue
922  static void     Returns 0 after user quit, 1 otherwise */
923    static int
924  xwin_process_events(void)  xwin_process_events(void)
925  {  {
926          XEvent xevent;          XEvent xevent;
# Line 427  xwin_process_events(void) Line 934  xwin_process_events(void)
934          Window wdummy;          Window wdummy;
935          int dummy;          int dummy;
936    
937          while (XPending(display) > 0)          while (XPending(g_display) > 0)
938          {          {
939                  XNextEvent(display, &xevent);                  XNextEvent(g_display, &xevent);
940    
941                  if ((IC != NULL) && (XFilterEvent(&xevent, None) == True))                  if ((g_IC != NULL) && (XFilterEvent(&xevent, None) == True))
942                  {                  {
943                          DEBUG_KBD(("Filtering event\n"));                          DEBUG_KBD(("Filtering event\n"));
944                          continue;                          continue;
# Line 441  xwin_process_events(void) Line 948  xwin_process_events(void)
948    
949                  switch (xevent.type)                  switch (xevent.type)
950                  {                  {
951                            case ClientMessage:
952                                    /* the window manager told us to quit */
953                                    if ((xevent.xclient.message_type == g_protocol_atom)
954                                        && ((Atom) xevent.xclient.data.l[0] == g_kill_atom))
955                                            /* Quit */
956                                            return 0;
957                                    break;
958    
959                          case KeyPress:                          case KeyPress:
960                                  if (IC != NULL)                                  g_last_gesturetime = xevent.xkey.time;
961                                    if (g_IC != NULL)
962                                          /* Multi_key compatible version */                                          /* Multi_key compatible version */
963                                  {                                  {
964                                          XmbLookupString(IC,                                          XmbLookupString(g_IC,
965                                                          (XKeyPressedEvent *) &                                                          &xevent.xkey, str, sizeof(str), &keysym,
966                                                          xevent, str, sizeof(str), &keysym, &status);                                                          &status);
967                                          if (!((status == XLookupKeySym) || (status == XLookupBoth)))                                          if (!((status == XLookupKeySym) || (status == XLookupBoth)))
968                                          {                                          {
969                                                  error("XmbLookupString failed with status 0x%x\n",                                                  error("XmbLookupString failed with status 0x%x\n",
# Line 463  xwin_process_events(void) Line 979  xwin_process_events(void)
979                                                        str, sizeof(str), &keysym, NULL);                                                        str, sizeof(str), &keysym, NULL);
980                                  }                                  }
981    
982                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, get_ksname(keysym)));                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym,
983                                               get_ksname(keysym)));
984    
985                                  ev_time = time(NULL);                                  ev_time = time(NULL);
986                                  if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))                                  if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))
# Line 475  xwin_process_events(void) Line 992  xwin_process_events(void)
992                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
993                                          break;                                          break;
994    
995                                    save_remote_modifiers(tr.scancode);
996                                  ensure_remote_modifiers(ev_time, tr);                                  ensure_remote_modifiers(ev_time, tr);
   
997                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
998                                    restore_remote_modifiers(ev_time, tr.scancode);
999    
1000                                  break;                                  break;
1001    
1002                          case KeyRelease:                          case KeyRelease:
1003                                    g_last_gesturetime = xevent.xkey.time;
1004                                  XLookupString((XKeyEvent *) & xevent, str,                                  XLookupString((XKeyEvent *) & xevent, str,
1005                                                sizeof(str), &keysym, NULL);                                                sizeof(str), &keysym, NULL);
1006    
# Line 505  xwin_process_events(void) Line 1025  xwin_process_events(void)
1025                                  /* fall through */                                  /* fall through */
1026    
1027                          case ButtonRelease:                          case ButtonRelease:
1028                                    g_last_gesturetime = xevent.xbutton.time;
1029                                  button = xkeymap_translate_button(xevent.xbutton.button);                                  button = xkeymap_translate_button(xevent.xbutton.button);
1030                                  if (button == 0)                                  if (button == 0)
1031                                          break;                                          break;
1032    
1033                                    /* If win_button_size is nonzero, enable single app mode */
1034                                    if (xevent.xbutton.y < g_win_button_size)
1035                                    {
1036                                            /* Stop moving window when button is released, regardless of cursor position */
1037                                            if (g_moving_wnd && (xevent.type == ButtonRelease))
1038                                                    g_moving_wnd = False;
1039    
1040                                            /*  Check from right to left: */
1041    
1042                                            if (xevent.xbutton.x >= g_width - g_win_button_size)
1043                                            {
1044                                                    /* The close button, continue */
1045                                                    ;
1046                                            }
1047                                            else if (xevent.xbutton.x >=
1048                                                     g_width - g_win_button_size * 2)
1049                                            {
1050                                                    /* The maximize/restore button. Do not send to
1051                                                       server.  It might be a good idea to change the
1052                                                       cursor or give some other visible indication
1053                                                       that rdesktop inhibited this click */
1054                                                    break;
1055                                            }
1056                                            else if (xevent.xbutton.x >=
1057                                                     g_width - g_win_button_size * 3)
1058                                            {
1059                                                    /* The minimize button. Iconify window. */
1060                                                    XIconifyWindow(g_display, g_wnd,
1061                                                                   DefaultScreen(g_display));
1062                                                    break;
1063                                            }
1064                                            else if (xevent.xbutton.x <= g_win_button_size)
1065                                            {
1066                                                    /* The system menu. Ignore. */
1067                                                    break;
1068                                            }
1069                                            else
1070                                            {
1071                                                    /* The title bar. */
1072                                                    if ((xevent.type == ButtonPress) && !g_fullscreen
1073                                                        && g_hide_decorations)
1074                                                    {
1075                                                            g_moving_wnd = True;
1076                                                            g_move_x_offset = xevent.xbutton.x;
1077                                                            g_move_y_offset = xevent.xbutton.y;
1078                                                    }
1079                                                    break;
1080    
1081                                            }
1082                                    }
1083    
1084                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
1085                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);
1086                                  break;                                  break;
1087    
1088                          case MotionNotify:                          case MotionNotify:
1089                                    if (g_moving_wnd)
1090                                    {
1091                                            XMoveWindow(g_display, g_wnd,
1092                                                        xevent.xmotion.x_root - g_move_x_offset,
1093                                                        xevent.xmotion.y_root - g_move_y_offset);
1094                                            break;
1095                                    }
1096    
1097                                    if (g_fullscreen && !g_focused)
1098                                            XSetInputFocus(g_display, g_wnd, RevertToPointerRoot,
1099                                                           CurrentTime);
1100                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
1101                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
1102                                  break;                                  break;
1103    
1104                          case FocusIn:                          case FocusIn:
1105                                  XQueryPointer(display, wnd, &wdummy, &wdummy, &dummy, &dummy, &dummy, &dummy, &state);                                  if (xevent.xfocus.mode == NotifyGrab)
1106                                            break;
1107                                    g_focused = True;
1108                                    XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy,
1109                                                  &dummy, &dummy, &state);
1110                                  reset_modifier_keys(state);                                  reset_modifier_keys(state);
1111                                  if (grab_keyboard)                                  if (g_grab_keyboard && g_mouse_in_wnd)
1112                                          XGrabKeyboard(display, wnd, True,                                          XGrabKeyboard(g_display, g_wnd, True,
1113                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
1114                                  break;                                  break;
1115    
1116                          case FocusOut:                          case FocusOut:
1117                                    if (xevent.xfocus.mode == NotifyUngrab)
1118                                            break;
1119                                    g_focused = False;
1120                                  if (xevent.xfocus.mode == NotifyWhileGrabbed)                                  if (xevent.xfocus.mode == NotifyWhileGrabbed)
1121                                          XUngrabKeyboard(display, CurrentTime);                                          XUngrabKeyboard(g_display, CurrentTime);
1122                                    break;
1123    
1124                            case EnterNotify:
1125                                    /* we only register for this event when in fullscreen mode */
1126                                    /* or grab_keyboard */
1127                                    g_mouse_in_wnd = True;
1128                                    if (g_fullscreen)
1129                                    {
1130                                            XSetInputFocus(g_display, g_wnd, RevertToPointerRoot,
1131                                                           CurrentTime);
1132                                            break;
1133                                    }
1134                                    if (g_focused)
1135                                            XGrabKeyboard(g_display, g_wnd, True,
1136                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
1137                                    break;
1138    
1139                            case LeaveNotify:
1140                                    /* we only register for this event when grab_keyboard */
1141                                    g_mouse_in_wnd = False;
1142                                    XUngrabKeyboard(g_display, CurrentTime);
1143                                  break;                                  break;
1144    
1145                          case Expose:                          case Expose:
1146                                  XCopyArea(display, backstore, wnd, gc,                                  XCopyArea(g_display, g_backstore, g_wnd, g_gc,
1147                                            xevent.xexpose.x, xevent.xexpose.y,                                            xevent.xexpose.x, xevent.xexpose.y,
1148                                            xevent.xexpose.width,                                            xevent.xexpose.width,
1149                                            xevent.xexpose.height,                                            xevent.xexpose.height,
# Line 548  xwin_process_events(void) Line 1159  xwin_process_events(void)
1159    
1160                                  if (xevent.xmapping.request == MappingModifier)                                  if (xevent.xmapping.request == MappingModifier)
1161                                  {                                  {
1162                                          XFreeModifierMap(mod_map);                                          XFreeModifiermap(g_mod_map);
1163                                          mod_map = XGetModifierMapping(display);                                          g_mod_map = XGetModifierMapping(g_display);
1164                                  }                                  }
1165                                  break;                                  break;
1166    
1167                                    /* clipboard stuff */
1168                            case SelectionNotify:
1169                                    xclip_handle_SelectionNotify(&xevent.xselection);
1170                                    break;
1171                            case SelectionRequest:
1172                                    xclip_handle_SelectionRequest(&xevent.xselectionrequest);
1173                                    break;
1174                            case SelectionClear:
1175                                    xclip_handle_SelectionClear();
1176                                    break;
1177                            case PropertyNotify:
1178                                    xclip_handle_PropertyNotify(&xevent.xproperty);
1179                                    break;
1180                  }                  }
1181          }          }
1182            /* Keep going */
1183            return 1;
1184  }  }
1185    
1186  void  /* Returns 0 after user quit, 1 otherwise */
1187    int
1188  ui_select(int rdp_socket)  ui_select(int rdp_socket)
1189  {  {
1190          int n = (rdp_socket > x_socket) ? rdp_socket + 1 : x_socket + 1;          int n = (rdp_socket > g_x_socket) ? rdp_socket + 1 : g_x_socket + 1;
1191          fd_set rfds;          fd_set rfds, wfds;
   
         FD_ZERO(&rfds);  
1192    
1193          while (True)          while (True)
1194          {          {
1195                  /* Process any events already waiting */                  /* Process any events already waiting */
1196                  xwin_process_events();                  if (!xwin_process_events())
1197                            /* User quit */
1198                            return 0;
1199    
1200                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
1201                    FD_ZERO(&wfds);
1202                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
1203                  FD_SET(x_socket, &rfds);                  FD_SET(g_x_socket, &rfds);
1204    
1205    #ifdef WITH_RDPSND
1206                    /* FIXME: there should be an API for registering fds */
1207                    if (g_rdpsnd && g_dsp_busy)
1208                    {
1209                            FD_SET(g_dsp_fd, &wfds);
1210                            n = (g_dsp_fd + 1 > n) ? g_dsp_fd + 1 : n;
1211                    }
1212    #endif
1213    
1214                  switch (select(n, &rfds, NULL, NULL, NULL))                  switch (select(n, &rfds, &wfds, NULL, NULL))
1215                  {                  {
1216                          case -1:                          case -1:
1217                                  error("select: %s\n", strerror(errno));                                  error("select: %s\n", strerror(errno));
# Line 584  ui_select(int rdp_socket) Line 1221  ui_select(int rdp_socket)
1221                  }                  }
1222    
1223                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
1224                          return;                          return 1;
1225    
1226    #ifdef WITH_RDPSND
1227                    if (g_rdpsnd && g_dsp_busy && FD_ISSET(g_dsp_fd, &wfds))
1228                            wave_out_play();
1229    #endif
1230          }          }
1231  }  }
1232    
1233  void  void
1234  ui_move_pointer(int x, int y)  ui_move_pointer(int x, int y)
1235  {  {
1236          XWarpPointer(display, wnd, wnd, 0, 0, 0, 0, x, y);          XWarpPointer(g_display, g_wnd, g_wnd, 0, 0, 0, 0, x, y);
1237  }  }
1238    
1239  HBITMAP  HBITMAP
# Line 601  ui_create_bitmap(int width, int height, Line 1243  ui_create_bitmap(int width, int height,
1243          Pixmap bitmap;          Pixmap bitmap;
1244          uint8 *tdata;          uint8 *tdata;
1245    
1246          tdata = translate_image(width, height, data);          tdata = (g_owncolmap ? data : translate_image(width, height, data));
1247          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(g_display, g_wnd, width, height, g_depth);
1248          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1249                               (char *) tdata, width, height, 8, 0);                               (char *) tdata, width, height, g_server_bpp == 8 ? 8 : g_bpp, 0);
1250    
1251          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, g_gc, image, 0, 0, 0, 0, width, height);
1252    
1253          XFree(image);          XFree(image);
1254          xfree(tdata);          if (!g_owncolmap)
1255                    xfree(tdata);
1256          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
1257  }  }
1258    
# Line 618  ui_paint_bitmap(int x, int y, int cx, in Line 1261  ui_paint_bitmap(int x, int y, int cx, in
1261  {  {
1262          XImage *image;          XImage *image;
1263          uint8 *tdata;          uint8 *tdata;
1264            tdata = (g_owncolmap ? data : translate_image(width, height, data));
1265            image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1266                                 (char *) tdata, width, height, g_server_bpp == 8 ? 8 : g_bpp, 0);
1267    
1268          tdata = translate_image(width, height, data);          if (g_ownbackstore)
         image = XCreateImage(display, visual, depth, ZPixmap, 0,  
                              (char *) tdata, width, height, 8, 0);  
   
         if (ownbackstore)  
1269          {          {
1270                  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);
1271                  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);
1272          }          }
1273          else          else
1274          {          {
1275                  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);
1276          }          }
1277    
1278          XFree(image);          XFree(image);
1279          xfree(tdata);          if (!g_owncolmap)
1280                    xfree(tdata);
1281  }  }
1282    
1283  void  void
1284  ui_destroy_bitmap(HBITMAP bmp)  ui_destroy_bitmap(HBITMAP bmp)
1285  {  {
1286          XFreePixmap(display, (Pixmap) bmp);          XFreePixmap(g_display, (Pixmap) bmp);
1287  }  }
1288    
1289  HGLYPH  HGLYPH
# Line 653  ui_create_glyph(int width, int height, u Line 1296  ui_create_glyph(int width, int height, u
1296    
1297          scanline = (width + 7) / 8;          scanline = (width + 7) / 8;
1298    
1299          bitmap = XCreatePixmap(display, wnd, width, height, 1);          bitmap = XCreatePixmap(g_display, g_wnd, width, height, 1);
1300          gc = XCreateGC(display, bitmap, 0, NULL);          gc = XCreateGC(g_display, bitmap, 0, NULL);
1301    
1302          image = XCreateImage(display, visual, 1, ZPixmap, 0, (char *) data,          image = XCreateImage(g_display, g_visual, 1, ZPixmap, 0, (char *) data,
1303                               width, height, 8, scanline);                               width, height, 8, scanline);
1304          image->byte_order = MSBFirst;          image->byte_order = MSBFirst;
1305          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
1306          XInitImage(image);          XInitImage(image);
1307    
1308          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, gc, image, 0, 0, 0, 0, width, height);
1309    
1310          XFree(image);          XFree(image);
1311          XFreeGC(display, gc);          XFreeGC(g_display, gc);
1312          return (HGLYPH) bitmap;          return (HGLYPH) bitmap;
1313  }  }
1314    
1315  void  void
1316  ui_destroy_glyph(HGLYPH glyph)  ui_destroy_glyph(HGLYPH glyph)
1317  {  {
1318          XFreePixmap(display, (Pixmap) glyph);          XFreePixmap(g_display, (Pixmap) glyph);
1319  }  }
1320    
1321  HCURSOR  HCURSOR
# Line 691  ui_create_cursor(unsigned int x, unsigne Line 1334  ui_create_cursor(unsigned int x, unsigne
1334          scanline = (width + 7) / 8;          scanline = (width + 7) / 8;
1335          offset = scanline * height;          offset = scanline * height;
1336    
1337          cursor = xmalloc(offset);          cursor = (uint8 *) xmalloc(offset);
1338          memset(cursor, 0, offset);          memset(cursor, 0, offset);
1339    
1340          mask = xmalloc(offset);          mask = (uint8 *) xmalloc(offset);
1341          memset(mask, 0, offset);          memset(mask, 0, offset);
1342    
1343          /* approximate AND and XOR masks with a monochrome X pointer */          /* approximate AND and XOR masks with a monochrome X pointer */
# Line 736  ui_create_cursor(unsigned int x, unsigne Line 1379  ui_create_cursor(unsigned int x, unsigne
1379          maskglyph = ui_create_glyph(width, height, mask);          maskglyph = ui_create_glyph(width, height, mask);
1380    
1381          xcursor =          xcursor =
1382                  XCreatePixmapCursor(display, (Pixmap) cursorglyph,                  XCreatePixmapCursor(g_display, (Pixmap) cursorglyph,
1383                                      (Pixmap) maskglyph, &fg, &bg, x, y);                                      (Pixmap) maskglyph, &fg, &bg, x, y);
1384    
1385          ui_destroy_glyph(maskglyph);          ui_destroy_glyph(maskglyph);
# Line 749  ui_create_cursor(unsigned int x, unsigne Line 1392  ui_create_cursor(unsigned int x, unsigne
1392  void  void
1393  ui_set_cursor(HCURSOR cursor)  ui_set_cursor(HCURSOR cursor)
1394  {  {
1395          current_cursor = (Cursor) cursor;          g_current_cursor = (Cursor) cursor;
1396          XDefineCursor(display, wnd, current_cursor);          XDefineCursor(g_display, g_wnd, g_current_cursor);
1397  }  }
1398    
1399  void  void
1400  ui_destroy_cursor(HCURSOR cursor)  ui_destroy_cursor(HCURSOR cursor)
1401  {  {
1402          XFreeCursor(display, (Cursor) cursor);          XFreeCursor(g_display, (Cursor) cursor);
1403  }  }
1404    
1405  #define MAKE_XCOLOR(xc,c) \  #define MAKE_XCOLOR(xc,c) \
# Line 765  ui_destroy_cursor(HCURSOR cursor) Line 1408  ui_destroy_cursor(HCURSOR cursor)
1408                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \                  (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \
1409                  (xc)->flags = DoRed | DoGreen | DoBlue;                  (xc)->flags = DoRed | DoGreen | DoBlue;
1410    
1411    
1412  HCOLOURMAP  HCOLOURMAP
1413  ui_create_colourmap(COLOURMAP * colours)  ui_create_colourmap(COLOURMAP * colours)
1414  {  {
1415          COLOURENTRY *entry;          COLOURENTRY *entry;
1416          int i, ncolours = colours->ncolours;          int i, ncolours = colours->ncolours;
1417          uint32 *map = xmalloc(sizeof(*colmap) * ncolours);          if (!g_owncolmap)
1418          XColor xentry;          {
1419          XColor xc_cache[256];                  uint32 *map = (uint32 *) xmalloc(sizeof(*g_colmap) * ncolours);
1420          uint32 colour;                  XColor xentry;
1421          int colLookup = 256;                  XColor xc_cache[256];
1422          for (i = 0; i < ncolours; i++)                  uint32 colour;
1423          {                  int colLookup = 256;
1424                  entry = &colours->colours[i];                  for (i = 0; i < ncolours; i++)
1425                  MAKE_XCOLOR(&xentry, entry);                  {
1426                            entry = &colours->colours[i];
1427                  if (XAllocColor(display, xcolmap, &xentry) == 0)                          MAKE_XCOLOR(&xentry, entry);
                 {  
                         /* Allocation failed, find closest match. */  
                         int j = 256;  
                         int nMinDist = 3 * 256 * 256;  
                         long nDist = nMinDist;  
1428    
1429                          /* only get the colors once */                          if (XAllocColor(g_display, g_xcolmap, &xentry) == 0)
                         while (colLookup--)  
1430                          {                          {
1431                                  xc_cache[colLookup].pixel = colLookup;                                  /* Allocation failed, find closest match. */
1432                                  xc_cache[colLookup].red = xc_cache[colLookup].green =                                  int j = 256;
1433                                          xc_cache[colLookup].blue = 0;                                  int nMinDist = 3 * 256 * 256;
1434                                  xc_cache[colLookup].flags = 0;                                  long nDist = nMinDist;
                                 XQueryColor(display,  
                                             DefaultColormap(display, DefaultScreen(display)),  
                                             &xc_cache[colLookup]);  
                         }  
                         colLookup = 0;  
1435    
1436                          /* approximate the pixel */                                  /* only get the colors once */
1437                          while (j--)                                  while (colLookup--)
                         {  
                                 if (xc_cache[j].flags)  
1438                                  {                                  {
1439                                          nDist = ((long) (xc_cache[j].red >> 8) -                                          xc_cache[colLookup].pixel = colLookup;
1440                                                   (long) (xentry.red >> 8)) *                                          xc_cache[colLookup].red = xc_cache[colLookup].green =
1441                                                  ((long) (xc_cache[j].red >> 8) -                                                  xc_cache[colLookup].blue = 0;
1442                                                   (long) (xentry.red >> 8)) +                                          xc_cache[colLookup].flags = 0;
1443                                                  ((long) (xc_cache[j].green >> 8) -                                          XQueryColor(g_display,
1444                                                   (long) (xentry.green >> 8)) *                                                      DefaultColormap(g_display,
1445                                                  ((long) (xc_cache[j].green >> 8) -                                                                      DefaultScreen(g_display)),
1446                                                   (long) (xentry.green >> 8)) +                                                      &xc_cache[colLookup]);
                                                 ((long) (xc_cache[j].blue >> 8) -  
                                                  (long) (xentry.blue >> 8)) *  
                                                 ((long) (xc_cache[j].blue >> 8) -  
                                                  (long) (xentry.blue >> 8));  
1447                                  }                                  }
1448                                  if (nDist < nMinDist)                                  colLookup = 0;
1449    
1450                                    /* approximate the pixel */
1451                                    while (j--)
1452                                  {                                  {
1453                                          nMinDist = nDist;                                          if (xc_cache[j].flags)
1454                                          xentry.pixel = j;                                          {
1455                                                    nDist = ((long) (xc_cache[j].red >> 8) -
1456                                                             (long) (xentry.red >> 8)) *
1457                                                            ((long) (xc_cache[j].red >> 8) -
1458                                                             (long) (xentry.red >> 8)) +
1459                                                            ((long) (xc_cache[j].green >> 8) -
1460                                                             (long) (xentry.green >> 8)) *
1461                                                            ((long) (xc_cache[j].green >> 8) -
1462                                                             (long) (xentry.green >> 8)) +
1463                                                            ((long) (xc_cache[j].blue >> 8) -
1464                                                             (long) (xentry.blue >> 8)) *
1465                                                            ((long) (xc_cache[j].blue >> 8) -
1466                                                             (long) (xentry.blue >> 8));
1467                                            }
1468                                            if (nDist < nMinDist)
1469                                            {
1470                                                    nMinDist = nDist;
1471                                                    xentry.pixel = j;
1472                                            }
1473                                  }                                  }
1474                          }                          }
1475                            colour = xentry.pixel;
1476    
1477                            /* update our cache */
1478                            if (xentry.pixel < 256)
1479                            {
1480                                    xc_cache[xentry.pixel].red = xentry.red;
1481                                    xc_cache[xentry.pixel].green = xentry.green;
1482                                    xc_cache[xentry.pixel].blue = xentry.blue;
1483    
1484                            }
1485    
1486    
1487                            /* byte swap here to make translate_image faster */
1488                            map[i] = translate_colour(colour);
1489                  }                  }
1490                  colour = xentry.pixel;                  return map;
1491            }
1492            else
1493            {
1494                    XColor *xcolours, *xentry;
1495                    Colormap map;
1496    
1497                  /* update our cache */                  xcolours = (XColor *) xmalloc(sizeof(XColor) * ncolours);
1498                  if (xentry.pixel < 256)                  for (i = 0; i < ncolours; i++)
1499                  {                  {
1500                          xc_cache[xentry.pixel].red = xentry.red;                          entry = &colours->colours[i];
1501                          xc_cache[xentry.pixel].green = xentry.green;                          xentry = &xcolours[i];
1502                          xc_cache[xentry.pixel].blue = xentry.blue;                          xentry->pixel = i;
1503                            MAKE_XCOLOR(xentry, entry);
1504                  }                  }
1505    
1506                    map = XCreateColormap(g_display, g_wnd, g_visual, AllocAll);
1507                    XStoreColors(g_display, map, xcolours, ncolours);
1508    
1509                  /* byte swap here to make translate_image faster */                  xfree(xcolours);
1510                  map[i] = translate_colour(colour);                  return (HCOLOURMAP) map;
1511          }          }
   
         return map;  
1512  }  }
1513    
1514  void  void
1515  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
1516  {  {
1517          xfree(map);          if (!g_owncolmap)
1518                    xfree(map);
1519            else
1520                    XFreeColormap(g_display, (Colormap) map);
1521  }  }
1522    
1523  void  void
1524  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
1525  {  {
1526          colmap = map;          if (!g_owncolmap)
1527            {
1528                    if (g_colmap)
1529                            xfree(g_colmap);
1530    
1531                    g_colmap = (uint32 *) map;
1532            }
1533            else
1534                    XSetWindowColormap(g_display, g_wnd, (Colormap) map);
1535  }  }
1536    
1537  void  void
# Line 865  ui_set_clip(int x, int y, int cx, int cy Line 1543  ui_set_clip(int x, int y, int cx, int cy
1543          rect.y = y;          rect.y = y;
1544          rect.width = cx;          rect.width = cx;
1545          rect.height = cy;          rect.height = cy;
1546          XSetClipRectangles(display, gc, 0, 0, &rect, 1, YXBanded);          XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded);
1547  }  }
1548    
1549  void  void
# Line 875  ui_reset_clip(void) Line 1553  ui_reset_clip(void)
1553    
1554          rect.x = 0;          rect.x = 0;
1555          rect.y = 0;          rect.y = 0;
1556          rect.width = width;          rect.width = g_width;
1557          rect.height = height;          rect.height = g_height;
1558          XSetClipRectangles(display, gc, 0, 0, &rect, 1, YXBanded);          XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded);
1559  }  }
1560    
1561  void  void
1562  ui_bell(void)  ui_bell(void)
1563  {  {
1564          XBell(display, 0);          XBell(g_display, 0);
1565  }  }
1566    
1567  void  void
# Line 895  ui_destblt(uint8 opcode, Line 1573  ui_destblt(uint8 opcode,
1573          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1574  }  }
1575    
1576    static uint8 hatch_patterns[] = {
1577            0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, /* 0 - bsHorizontal */
1578            0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, /* 1 - bsVertical */
1579            0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, /* 2 - bsFDiagonal */
1580            0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, /* 3 - bsBDiagonal */
1581            0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08, /* 4 - bsCross */
1582            0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81  /* 5 - bsDiagCross */
1583    };
1584    
1585  void  void
1586  ui_patblt(uint8 opcode,  ui_patblt(uint8 opcode,
1587            /* dest */ int x, int y, int cx, int cy,            /* dest */ int x, int y, int cx, int cy,
# Line 912  ui_patblt(uint8 opcode, Line 1599  ui_patblt(uint8 opcode,
1599                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
1600                          break;                          break;
1601    
1602                    case 2: /* Hatch */
1603                            fill = (Pixmap) ui_create_glyph(8, 8,
1604                                                            hatch_patterns + brush->pattern[0] * 8);
1605                            SET_FOREGROUND(fgcolour);
1606                            SET_BACKGROUND(bgcolour);
1607                            XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
1608                            XSetStipple(g_display, g_gc, fill);
1609                            XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
1610                            FILL_RECTANGLE(x, y, cx, cy);
1611                            XSetFillStyle(g_display, g_gc, FillSolid);
1612                            XSetTSOrigin(g_display, g_gc, 0, 0);
1613                            ui_destroy_glyph((HGLYPH) fill);
1614                            break;
1615    
1616                  case 3: /* Pattern */                  case 3: /* Pattern */
1617                          for (i = 0; i != 8; i++)                          for (i = 0; i != 8; i++)
1618                                  ipattern[7 - i] = brush->pattern[i];                                  ipattern[7 - i] = brush->pattern[i];
# Line 919  ui_patblt(uint8 opcode, Line 1620  ui_patblt(uint8 opcode,
1620    
1621                          SET_FOREGROUND(bgcolour);                          SET_FOREGROUND(bgcolour);
1622                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(fgcolour);
1623                          XSetFillStyle(display, gc, FillOpaqueStippled);                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
1624                          XSetStipple(display, gc, fill);                          XSetStipple(g_display, g_gc, fill);
1625                          XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin);                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
1626    
1627                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
1628    
1629                          XSetFillStyle(display, gc, FillSolid);                          XSetFillStyle(g_display, g_gc, FillSolid);
1630                          XSetTSOrigin(display, gc, 0, 0);                          XSetTSOrigin(g_display, g_gc, 0, 0);
1631                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
1632                          break;                          break;
1633    
# Line 943  ui_screenblt(uint8 opcode, Line 1644  ui_screenblt(uint8 opcode,
1644               /* src */ int srcx, int srcy)               /* src */ int srcx, int srcy)
1645  {  {
1646          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1647          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
1648          if (ownbackstore)          if (g_ownbackstore)
1649                  XCopyArea(display, backstore, backstore, gc, srcx, srcy, cx, cy, x, y);                  XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, y);
1650          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1651  }  }
1652    
# Line 955  ui_memblt(uint8 opcode, Line 1656  ui_memblt(uint8 opcode,
1656            /* src */ HBITMAP src, int srcx, int srcy)            /* src */ HBITMAP src, int srcx, int srcy)
1657  {  {
1658          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1659          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);
1660          if (ownbackstore)          if (g_ownbackstore)
1661                  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);
1662          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1663  }  }
1664    
# Line 1001  ui_line(uint8 opcode, Line 1702  ui_line(uint8 opcode,
1702  {  {
1703          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
1704          SET_FOREGROUND(pen->colour);          SET_FOREGROUND(pen->colour);
1705          XDrawLine(display, wnd, gc, startx, starty, endx, endy);          XDrawLine(g_display, g_wnd, g_gc, startx, starty, endx, endy);
1706          if (ownbackstore)          if (g_ownbackstore)
1707                  XDrawLine(display, backstore, gc, startx, starty, endx, endy);                  XDrawLine(g_display, g_backstore, g_gc, startx, starty, endx, endy);
1708          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
1709  }  }
1710    
# Line 1016  ui_rect( Line 1717  ui_rect(
1717          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE(x, y, cx, cy);
1718  }  }
1719    
1720    /* warning, this function only draws on wnd or backstore, not both */
1721  void  void
1722  ui_draw_glyph(int mixmode,  ui_draw_glyph(int mixmode,
1723                /* dest */ int x, int y, int cx, int cy,                /* dest */ int x, int y, int cx, int cy,
# Line 1025  ui_draw_glyph(int mixmode, Line 1727  ui_draw_glyph(int mixmode,
1727          SET_FOREGROUND(fgcolour);          SET_FOREGROUND(fgcolour);
1728          SET_BACKGROUND(bgcolour);          SET_BACKGROUND(bgcolour);
1729    
1730          XSetFillStyle(display, gc,          XSetFillStyle(g_display, g_gc,
1731                        (mixmode == MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);                        (mixmode == MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);
1732          XSetStipple(display, gc, (Pixmap) glyph);          XSetStipple(g_display, g_gc, (Pixmap) glyph);
1733          XSetTSOrigin(display, gc, x, y);          XSetTSOrigin(g_display, g_gc, x, y);
1734    
1735          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1736    
1737          XSetFillStyle(display, gc, FillSolid);          XSetFillStyle(g_display, g_gc, FillSolid);
1738  }  }
1739    
1740  #define DO_GLYPH(ttext,idx) \  #define DO_GLYPH(ttext,idx) \
# Line 1059  ui_draw_glyph(int mixmode, Line 1761  ui_draw_glyph(int mixmode,
1761      }\      }\
1762    if (glyph != NULL)\    if (glyph != NULL)\
1763      {\      {\
1764        ui_draw_glyph (mixmode, x + (short) glyph->offset,\        ui_draw_glyph (mixmode, x + glyph->offset,\
1765                       y + (short) glyph->baseline,\                       y + glyph->baseline,\
1766                       glyph->width, glyph->height,\                       glyph->width, glyph->height,\
1767                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\                       glyph->pixmap, 0, 0, bgcolour, fgcolour);\
1768        if (flags & TEXT2_IMPLICIT_X)\        if (flags & TEXT2_IMPLICIT_X)\
# Line 1082  ui_draw_text(uint8 font, uint8 flags, in Line 1784  ui_draw_text(uint8 font, uint8 flags, in
1784    
1785          if (boxcx > 1)          if (boxcx > 1)
1786          {          {
1787                  FILL_RECTANGLE(boxx, boxy, boxcx, boxcy);                  FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);
1788          }          }
1789          else if (mixmode == MIX_OPAQUE)          else if (mixmode == MIX_OPAQUE)
1790          {          {
1791                  FILL_RECTANGLE(clipx, clipy, clipcx, clipcy);                  FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);
1792          }          }
1793    
1794          /* Paint text, character by character */          /* Paint text, character by character */
# Line 1100  ui_draw_text(uint8 font, uint8 flags, in Line 1802  ui_draw_text(uint8 font, uint8 flags, in
1802                                  else                                  else
1803                                  {                                  {
1804                                          error("this shouldn't be happening\n");                                          error("this shouldn't be happening\n");
1805                                          break;                                          exit(1);
1806                                  }                                  }
1807                                  /* this will move pointer from start to first character after FF command */                                  /* this will move pointer from start to first character after FF command */
1808                                  length -= i + 3;                                  length -= i + 3;
# Line 1120  ui_draw_text(uint8 font, uint8 flags, in Line 1822  ui_draw_text(uint8 font, uint8 flags, in
1822                                                  else                                                  else
1823                                                          x += text[i + 2];                                                          x += text[i + 2];
1824                                          }                                          }
                                         if (i + 2 < length)  
                                                 i += 3;  
                                         else  
                                                 i += 2;  
                                         length -= i;  
                                         /* this will move pointer from start to first character after FE command */  
                                         text = &(text[i]);  
                                         i = 0;  
1825                                          for (j = 0; j < entry->size; j++)                                          for (j = 0; j < entry->size; j++)
1826                                                  DO_GLYPH(((uint8 *) (entry->data)), j);                                                  DO_GLYPH(((uint8 *) (entry->data)), j);
1827                                  }                                  }
1828                                    if (i + 2 < length)
1829                                            i += 3;
1830                                    else
1831                                            i += 2;
1832                                    length -= i;
1833                                    /* this will move pointer from start to first character after FE command */
1834                                    text = &(text[i]);
1835                                    i = 0;
1836                                  break;                                  break;
1837    
1838                          default:                          default:
# Line 1139  ui_draw_text(uint8 font, uint8 flags, in Line 1841  ui_draw_text(uint8 font, uint8 flags, in
1841                                  break;                                  break;
1842                  }                  }
1843          }          }
1844            if (g_ownbackstore)
1845            {
1846                    if (boxcx > 1)
1847                            XCopyArea(g_display, g_backstore, g_wnd, g_gc, boxx,
1848                                      boxy, boxcx, boxcy, boxx, boxy);
1849                    else
1850                            XCopyArea(g_display, g_backstore, g_wnd, g_gc, clipx,
1851                                      clipy, clipcx, clipcy, clipx, clipy);
1852            }
1853  }  }
1854    
1855  void  void
# Line 1149  ui_desktop_save(uint32 offset, int x, in Line 1858  ui_desktop_save(uint32 offset, int x, in
1858          Pixmap pix;          Pixmap pix;
1859          XImage *image;          XImage *image;
1860    
1861          if (ownbackstore)          if (g_ownbackstore)
1862          {          {
1863                  image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes, ZPixmap);                  image = XGetImage(g_display, g_backstore, x, y, cx, cy, AllPlanes, ZPixmap);
1864          }          }
1865          else          else
1866          {          {
1867                  pix = XCreatePixmap(display, wnd, cx, cy, depth);                  pix = XCreatePixmap(g_display, g_wnd, cx, cy, g_depth);
1868                  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);
1869                  image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);                  image = XGetImage(g_display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);
1870                  XFreePixmap(display, pix);                  XFreePixmap(g_display, pix);
1871          }          }
1872    
1873          offset *= bpp / 8;          offset *= g_bpp / 8;
1874          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);
1875    
1876          XDestroyImage(image);          XDestroyImage(image);
1877  }  }
# Line 1173  ui_desktop_restore(uint32 offset, int x, Line 1882  ui_desktop_restore(uint32 offset, int x,
1882          XImage *image;          XImage *image;
1883          uint8 *data;          uint8 *data;
1884    
1885          offset *= bpp / 8;          offset *= g_bpp / 8;
1886          data = cache_get_desktop(offset, cx, cy, bpp / 8);          data = cache_get_desktop(offset, cx, cy, g_bpp / 8);
1887          if (data == NULL)          if (data == NULL)
1888                  return;                  return;
1889    
1890          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
1891                               (char *) data, cx, cy, BitmapPad(display), cx * bpp / 8);                               (char *) data, cx, cy, BitmapPad(g_display), cx * g_bpp / 8);
1892    
1893          if (ownbackstore)          if (g_ownbackstore)
1894          {          {
1895                  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);
1896                  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);
1897          }          }
1898          else          else
1899          {          {
1900                  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);
1901          }          }
1902    
1903          XFree(image);          XFree(image);

Legend:
Removed from v.203  
changed lines
  Added in v.501

  ViewVC Help
Powered by ViewVC 1.1.26