/[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 620 by astrand, Wed Mar 3 10:46:35 2004 UTC revision 1232 by ossman_, Fri Apr 28 07:55:36 2006 UTC
# Line 1  Line 1 
1  /* -*- c-basic-offset: 8 -*-  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     User interface services - X Window System     User interface services - X Window System
4     Copyright (C) Matthew Chapman 1999-2002     Copyright (C) Matthew Chapman 1999-2005
5    
6     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 28  Line 28 
28  #include "rdesktop.h"  #include "rdesktop.h"
29  #include "xproto.h"  #include "xproto.h"
30    
31    /* We can't include Xproto.h because of conflicting defines for BOOL */
32    #define X_ConfigureWindow              12
33    
34  extern int g_width;  extern int g_width;
35  extern int g_height;  extern int g_height;
36    extern int g_xpos;
37    extern int g_ypos;
38    extern int g_pos;
39  extern BOOL g_sendmotion;  extern BOOL g_sendmotion;
40  extern BOOL g_fullscreen;  extern BOOL g_fullscreen;
41  extern BOOL g_grab_keyboard;  extern BOOL g_grab_keyboard;
42  extern BOOL g_hide_decorations;  extern BOOL g_hide_decorations;
43  extern char g_title[];  extern char g_title[];
44  extern int g_server_bpp;  /* Color depth of the RDP session.
45       As of RDP 5.1, it may be 8, 15, 16 or 24. */
46    extern int g_server_depth;
47  extern int g_win_button_size;  extern int g_win_button_size;
48    
49  Display *g_display;  Display *g_display;
# Line 43  Time g_last_gesturetime; Line 51  Time g_last_gesturetime;
51  static int g_x_socket;  static int g_x_socket;
52  static Screen *g_screen;  static Screen *g_screen;
53  Window g_wnd;  Window g_wnd;
54    
55    /* SeamlessRDP support */
56    typedef struct _seamless_group
57    {
58            Window wnd;
59            unsigned long id;
60            unsigned int refcnt;
61    } seamless_group;
62    typedef struct _seamless_window
63    {
64            Window wnd;
65            unsigned long id;
66            unsigned long behind;
67            seamless_group *group;
68            int xoffset, yoffset;
69            int width, height;
70            int state;              /* normal/minimized/maximized. */
71            unsigned int desktop;
72            struct timeval *position_timer;
73    
74            BOOL outstanding_position;
75            unsigned int outpos_serial;
76            int outpos_xoffset, outpos_yoffset;
77            int outpos_width, outpos_height;
78    
79            struct _seamless_window *next;
80    } seamless_window;
81    static seamless_window *g_seamless_windows = NULL;
82    static unsigned long g_seamless_focused = 0;
83    static BOOL g_seamless_started = False; /* Server end is up and running */
84    static BOOL g_seamless_active = False;  /* We are currently in seamless mode */
85    static BOOL g_seamless_hidden = False;  /* Desktop is hidden on server */
86    extern BOOL g_seamless_rdp;
87    
88    extern uint32 g_embed_wnd;
89  BOOL g_enable_compose = False;  BOOL g_enable_compose = False;
90    BOOL g_Unobscured;              /* used for screenblt */
91  static GC g_gc = NULL;  static GC g_gc = NULL;
92    static GC g_create_bitmap_gc = NULL;
93    static GC g_create_glyph_gc = NULL;
94    static XRectangle g_clip_rectangle;
95  static Visual *g_visual;  static Visual *g_visual;
96    /* Color depth of the X11 visual of our window (e.g. 24 for True Color R8G8B visual).
97       This may be 32 for R8G8B8 visuals, and then the rest of the bits are undefined
98       as far as we're concerned. */
99  static int g_depth;  static int g_depth;
100    /* Bits-per-Pixel of the pixmaps we'll be using to draw on our window.
101       This may be larger than g_depth, in which case some of the bits would
102       be kept solely for alignment (e.g. 32bpp pixmaps on a 24bpp visual). */
103  static int g_bpp;  static int g_bpp;
104  static XIM g_IM;  static XIM g_IM;
105  static XIC g_IC;  static XIC g_IC;
# Line 54  static XModifierKeymap *g_mod_map; Line 107  static XModifierKeymap *g_mod_map;
107  static Cursor g_current_cursor;  static Cursor g_current_cursor;
108  static HCURSOR g_null_cursor = NULL;  static HCURSOR g_null_cursor = NULL;
109  static Atom g_protocol_atom, g_kill_atom;  static Atom g_protocol_atom, g_kill_atom;
110    extern Atom g_net_wm_state_atom;
111    extern Atom g_net_wm_desktop_atom;
112  static BOOL g_focused;  static BOOL g_focused;
113  static BOOL g_mouse_in_wnd;  static BOOL g_mouse_in_wnd;
114    /* Indicates that:
115       1) visual has 15, 16 or 24 depth and the same color channel masks
116          as its RDP equivalent (implies X server is LE),
117       2) host is LE
118       This will trigger an optimization whose real value is questionable.
119    */
120    static BOOL g_compatible_arch;
121    /* Indicates whether RDP's bitmaps and our XImages have the same
122       binary format. If so, we can avoid an expensive translation.
123       Note that this can be true when g_compatible_arch is false,
124       e.g.:
125      
126         RDP(LE) <-> host(BE) <-> X-Server(LE)
127        
128       ('host' is the machine running rdesktop; the host simply memcpy's
129        so its endianess doesn't matter)
130     */
131    static BOOL g_no_translate_image = False;
132    
133  /* endianness */  /* endianness */
134  static BOOL g_host_be;  static BOOL g_host_be;
# Line 64  static int g_red_shift_r, g_blue_shift_r Line 137  static int g_red_shift_r, g_blue_shift_r
137  static int g_red_shift_l, g_blue_shift_l, g_green_shift_l;  static int g_red_shift_l, g_blue_shift_l, g_green_shift_l;
138    
139  /* software backing store */  /* software backing store */
140  BOOL g_ownbackstore = True;     /* We can't rely on external BackingStore */  extern BOOL g_ownbackstore;
141  static Pixmap g_backstore = 0;  static Pixmap g_backstore = 0;
142    
143  /* Moving in single app mode */  /* Moving in single app mode */
144  static BOOL g_moving_wnd;  static BOOL g_moving_wnd;
145  static int g_move_x_offset = 0;  static int g_move_x_offset = 0;
146  static int g_move_y_offset = 0;  static int g_move_y_offset = 0;
147    static BOOL g_using_full_workarea = False;
148    
149  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
150  extern int g_dsp_fd;  extern int g_dsp_fd;
# Line 83  extern BOOL g_rdpsnd; Line 157  extern BOOL g_rdpsnd;
157  #define PROP_MOTIF_WM_HINTS_ELEMENTS    5  #define PROP_MOTIF_WM_HINTS_ELEMENTS    5
158  typedef struct  typedef struct
159  {  {
160          uint32 flags;          unsigned long flags;
161          uint32 functions;          unsigned long functions;
162          uint32 decorations;          unsigned long decorations;
163          sint32 inputMode;          long inputMode;
164          uint32 status;          unsigned long status;
165  }  }
166  PropMotifWmHints;  PropMotifWmHints;
167    
# Line 99  typedef struct Line 173  typedef struct
173  }  }
174  PixelColour;  PixelColour;
175    
176    #define ON_ALL_SEAMLESS_WINDOWS(func, args) \
177            do { \
178                    seamless_window *sw; \
179                    XRectangle rect; \
180                    if (!g_seamless_windows) break; \
181                    for (sw = g_seamless_windows; sw; sw = sw->next) { \
182                        rect.x = g_clip_rectangle.x - sw->xoffset; \
183                        rect.y = g_clip_rectangle.y - sw->yoffset; \
184                        rect.width = g_clip_rectangle.width; \
185                        rect.height = g_clip_rectangle.height; \
186                        XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded); \
187                        func args; \
188                    } \
189                    XSetClipRectangles(g_display, g_gc, 0, 0, &g_clip_rectangle, 1, YXBanded); \
190            } while (0)
191    
192    static void
193    seamless_XFillPolygon(Drawable d, XPoint * points, int npoints, int xoffset, int yoffset)
194    {
195            points[0].x -= xoffset;
196            points[0].y -= yoffset;
197            XFillPolygon(g_display, d, g_gc, points, npoints, Complex, CoordModePrevious);
198            points[0].x += xoffset;
199            points[0].y += yoffset;
200    }
201    
202    static void
203    seamless_XDrawLines(Drawable d, XPoint * points, int npoints, int xoffset, int yoffset)
204    {
205            points[0].x -= xoffset;
206            points[0].y -= yoffset;
207            XDrawLines(g_display, d, g_gc, points, npoints, CoordModePrevious);
208            points[0].x += xoffset;
209            points[0].y += yoffset;
210    }
211    
212  #define FILL_RECTANGLE(x,y,cx,cy)\  #define FILL_RECTANGLE(x,y,cx,cy)\
213  { \  { \
214          XFillRectangle(g_display, g_wnd, g_gc, x, y, cx, cy); \          XFillRectangle(g_display, g_wnd, g_gc, x, y, cx, cy); \
215            ON_ALL_SEAMLESS_WINDOWS(XFillRectangle, (g_display, sw->wnd, g_gc, x-sw->xoffset, y-sw->yoffset, cx, cy)); \
216          if (g_ownbackstore) \          if (g_ownbackstore) \
217                  XFillRectangle(g_display, g_backstore, g_gc, x, y, cx, cy); \                  XFillRectangle(g_display, g_backstore, g_gc, x, y, cx, cy); \
218  }  }
# Line 112  PixelColour; Line 222  PixelColour;
222          XFillRectangle(g_display, g_ownbackstore ? g_backstore : g_wnd, g_gc, x, y, cx, cy); \          XFillRectangle(g_display, g_ownbackstore ? g_backstore : g_wnd, g_gc, x, y, cx, cy); \
223  }  }
224    
225    #define FILL_POLYGON(p,np)\
226    { \
227            XFillPolygon(g_display, g_wnd, g_gc, p, np, Complex, CoordModePrevious); \
228            if (g_ownbackstore) \
229                    XFillPolygon(g_display, g_backstore, g_gc, p, np, Complex, CoordModePrevious); \
230            ON_ALL_SEAMLESS_WINDOWS(seamless_XFillPolygon, (sw->wnd, p, np, sw->xoffset, sw->yoffset)); \
231    }
232    
233    #define DRAW_ELLIPSE(x,y,cx,cy,m)\
234    { \
235            switch (m) \
236            { \
237                    case 0: /* Outline */ \
238                            XDrawArc(g_display, g_wnd, g_gc, x, y, cx, cy, 0, 360*64); \
239                            ON_ALL_SEAMLESS_WINDOWS(XDrawArc, (g_display, sw->wnd, g_gc, x-sw->xoffset, y-sw->yoffset, cx, cy, 0, 360*64)); \
240                            if (g_ownbackstore) \
241                                    XDrawArc(g_display, g_backstore, g_gc, x, y, cx, cy, 0, 360*64); \
242                            break; \
243                    case 1: /* Filled */ \
244                            XFillArc(g_display, g_wnd, g_gc, x, y, cx, cy, 0, 360*64); \
245                            ON_ALL_SEAMLESS_WINDOWS(XCopyArea, (g_display, g_ownbackstore ? g_backstore : g_wnd, sw->wnd, g_gc, \
246                                                                x, y, cx, cy, x-sw->xoffset, y-sw->yoffset)); \
247                            if (g_ownbackstore) \
248                                    XFillArc(g_display, g_backstore, g_gc, x, y, cx, cy, 0, 360*64); \
249                            break; \
250            } \
251    }
252    
253  /* colour maps */  /* colour maps */
254  BOOL g_owncolmap = False;  extern BOOL g_owncolmap;
255  static Colormap g_xcolmap;  static Colormap g_xcolmap;
256  static uint32 *g_colmap = NULL;  static uint32 *g_colmap = NULL;
257    
258  #define TRANSLATE(col)          ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : g_colmap[col] )  #define TRANSLATE(col)          ( g_server_depth != 8 ? translate_colour(col) : g_owncolmap ? col : g_colmap[col] )
259  #define SET_FOREGROUND(col)     XSetForeground(g_display, g_gc, TRANSLATE(col));  #define SET_FOREGROUND(col)     XSetForeground(g_display, g_gc, TRANSLATE(col));
260  #define SET_BACKGROUND(col)     XSetBackground(g_display, g_gc, TRANSLATE(col));  #define SET_BACKGROUND(col)     XSetBackground(g_display, g_gc, TRANSLATE(col));
261    
# Line 143  static int rop2_map[] = { Line 281  static int rop2_map[] = {
281  #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, rop2_map[rop2]); }  #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, rop2_map[rop2]); }
282  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, GXcopy); }  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, GXcopy); }
283    
284    static seamless_window *
285    sw_get_window_by_id(unsigned long id)
286    {
287            seamless_window *sw;
288            for (sw = g_seamless_windows; sw; sw = sw->next)
289            {
290                    if (sw->id == id)
291                            return sw;
292            }
293            return NULL;
294    }
295    
296    
297    static seamless_window *
298    sw_get_window_by_wnd(Window wnd)
299    {
300            seamless_window *sw;
301            for (sw = g_seamless_windows; sw; sw = sw->next)
302            {
303                    if (sw->wnd == wnd)
304                            return sw;
305            }
306            return NULL;
307    }
308    
309    
310    static void
311    sw_remove_window(seamless_window * win)
312    {
313            seamless_window *sw, **prevnext = &g_seamless_windows;
314            for (sw = g_seamless_windows; sw; sw = sw->next)
315            {
316                    if (sw == win)
317                    {
318                            *prevnext = sw->next;
319                            sw->group->refcnt--;
320                            if (sw->group->refcnt == 0)
321                            {
322                                    XDestroyWindow(g_display, sw->group->wnd);
323                                    xfree(sw->group);
324                            }
325                            xfree(sw->position_timer);
326                            xfree(sw);
327                            return;
328                    }
329                    prevnext = &sw->next;
330            }
331            return;
332    }
333    
334    
335    /* Move all windows except wnd to new desktop */
336    static void
337    sw_all_to_desktop(Window wnd, unsigned int desktop)
338    {
339            seamless_window *sw;
340            for (sw = g_seamless_windows; sw; sw = sw->next)
341            {
342                    if (sw->wnd == wnd)
343                            continue;
344                    if (sw->desktop != desktop)
345                    {
346                            ewmh_move_to_desktop(sw->wnd, desktop);
347                            sw->desktop = desktop;
348                    }
349            }
350    }
351    
352    
353    /* Send our position */
354    static void
355    sw_update_position(seamless_window * sw)
356    {
357            XWindowAttributes wa;
358            int x, y;
359            Window child_return;
360            unsigned int serial;
361    
362            XGetWindowAttributes(g_display, sw->wnd, &wa);
363            XTranslateCoordinates(g_display, sw->wnd, wa.root,
364                                  -wa.border_width, -wa.border_width, &x, &y, &child_return);
365    
366            serial = seamless_send_position(sw->id, x, y, wa.width, wa.height, 0);
367    
368            sw->outstanding_position = True;
369            sw->outpos_serial = serial;
370    
371            sw->outpos_xoffset = x;
372            sw->outpos_yoffset = y;
373            sw->outpos_width = wa.width;
374            sw->outpos_height = wa.height;
375    }
376    
377    
378    /* Check if it's time to send our position */
379    static void
380    sw_check_timers()
381    {
382            seamless_window *sw;
383            struct timeval now;
384    
385            gettimeofday(&now, NULL);
386            for (sw = g_seamless_windows; sw; sw = sw->next)
387            {
388                    if (timerisset(sw->position_timer) && timercmp(sw->position_timer, &now, <))
389                    {
390                            timerclear(sw->position_timer);
391                            sw_update_position(sw);
392                    }
393            }
394    }
395    
396    
397    static void
398    sw_restack_window(seamless_window * sw, unsigned long behind)
399    {
400            seamless_window *sw_above;
401    
402            /* Remove window from stack */
403            for (sw_above = g_seamless_windows; sw_above; sw_above = sw_above->next)
404            {
405                    if (sw_above->behind == sw->id)
406                            break;
407            }
408    
409            if (sw_above)
410                    sw_above->behind = sw->behind;
411    
412            /* And then add it at the new position */
413            for (sw_above = g_seamless_windows; sw_above; sw_above = sw_above->next)
414            {
415                    if (sw_above->behind == behind)
416                            break;
417            }
418    
419            if (sw_above)
420                    sw_above->behind = sw->id;
421    
422            sw->behind = behind;
423    }
424    
425    
426    static void
427    sw_handle_restack(seamless_window * sw)
428    {
429            Status status;
430            Window root, parent, *children;
431            unsigned int nchildren, i;
432            seamless_window *sw_below;
433    
434            status = XQueryTree(g_display, RootWindowOfScreen(g_screen),
435                                &root, &parent, &children, &nchildren);
436            if (!status || !nchildren)
437                    return;
438    
439            sw_below = NULL;
440    
441            i = 0;
442            while (children[i] != sw->wnd)
443            {
444                    i++;
445                    if (i >= nchildren)
446                            goto end;
447            }
448    
449            for (i++; i < nchildren; i++)
450            {
451                    sw_below = sw_get_window_by_wnd(children[i]);
452                    if (sw_below)
453                            break;
454            }
455    
456            if (!sw_below && !sw->behind)
457                    goto end;
458            if (sw_below && (sw_below->id == sw->behind))
459                    goto end;
460    
461            if (sw_below)
462            {
463                    seamless_send_zchange(sw->id, sw_below->id, 0);
464                    sw_restack_window(sw, sw_below->id);
465            }
466            else
467            {
468                    seamless_send_zchange(sw->id, 0, 0);
469                    sw_restack_window(sw, 0);
470            }
471    
472          end:
473            XFree(children);
474    }
475    
476    
477    static seamless_group *
478    sw_find_group(unsigned long id, BOOL dont_create)
479    {
480            seamless_window *sw;
481            seamless_group *sg;
482            XSetWindowAttributes attribs;
483    
484            for (sw = g_seamless_windows; sw; sw = sw->next)
485            {
486                    if (sw->group->id == id)
487                            return sw->group;
488            }
489    
490            if (dont_create)
491                    return NULL;
492    
493            sg = xmalloc(sizeof(seamless_group));
494    
495            sg->wnd =
496                    XCreateWindow(g_display, RootWindowOfScreen(g_screen), -1, -1, 1, 1, 0,
497                                  CopyFromParent, CopyFromParent, CopyFromParent, 0, &attribs);
498    
499            sg->id = id;
500            sg->refcnt = 0;
501    
502            return sg;
503    }
504    
505    
506  static void  static void
507  mwm_hide_decorations(void)  mwm_hide_decorations(Window wnd)
508  {  {
509          PropMotifWmHints motif_hints;          PropMotifWmHints motif_hints;
510          Atom hintsatom;          Atom hintsatom;
# Line 161  mwm_hide_decorations(void) Line 521  mwm_hide_decorations(void)
521                  return;                  return;
522          }          }
523    
524          XChangeProperty(g_display, g_wnd, hintsatom, hintsatom, 32, PropModeReplace,          XChangeProperty(g_display, wnd, hintsatom, hintsatom, 32, PropModeReplace,
525                          (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);                          (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
526    
527  }  }
528    
529  static PixelColour  #define SPLITCOLOUR15(colour, rv) \
530  split_colour15(uint32 colour)  { \
531  {          rv.red = ((colour >> 7) & 0xf8) | ((colour >> 12) & 0x7); \
532          PixelColour rv;          rv.green = ((colour >> 2) & 0xf8) | ((colour >> 8) & 0x7); \
533          rv.red = (colour & 0x7c00) >> 7;          rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); \
         rv.green = (colour & 0x03e0) >> 2;  
         rv.blue = (colour & 0x001f) << 3;  
         return rv;  
 }  
   
 static PixelColour  
 split_colour16(uint32 colour)  
 {  
         PixelColour rv;  
         rv.red = (colour & 0xf800) >> 8;  
         rv.green = (colour & 0x07e0) >> 3;  
         rv.blue = (colour & 0x001f) << 3;  
         return rv;  
 }  
   
 static PixelColour  
 split_colour24(uint32 colour)  
 {  
         PixelColour rv;  
         rv.blue = (colour & 0xff0000) >> 16;  
         rv.green = (colour & 0x00ff00) >> 8;  
         rv.red = (colour & 0x0000ff);  
         return rv;  
534  }  }
535    
536  static uint32  #define SPLITCOLOUR16(colour, rv) \
537  make_colour(PixelColour pc)  { \
538  {          rv.red = ((colour >> 8) & 0xf8) | ((colour >> 13) & 0x7); \
539          return (((pc.red >> g_red_shift_r) << g_red_shift_l)          rv.green = ((colour >> 3) & 0xfc) | ((colour >> 9) & 0x3); \
540                  | ((pc.green >> g_green_shift_r) << g_green_shift_l)          rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); \
541                  | ((pc.blue >> g_blue_shift_r) << g_blue_shift_l));  } \
542    
543    #define SPLITCOLOUR24(colour, rv) \
544    { \
545            rv.blue = (colour & 0xff0000) >> 16; \
546            rv.green = (colour & 0x00ff00) >> 8; \
547            rv.red = (colour & 0x0000ff); \
548  }  }
549    
550    #define MAKECOLOUR(pc) \
551            ((pc.red >> g_red_shift_r) << g_red_shift_l) \
552                    | ((pc.green >> g_green_shift_r) << g_green_shift_l) \
553                    | ((pc.blue >> g_blue_shift_r) << g_blue_shift_l) \
554    
555  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
556  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | (x & 0xff00)); }  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | (x & 0xff00)); }
557  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
558                          x = (x << 16) | (x >> 16); }                          x = (x << 16) | (x >> 16); }
559    
560    /* The following macros output the same octet sequences
561       on both BE and LE hosts: */
562    
563    #define BOUT16(o, x) { *(o++) = x >> 8; *(o++) = x; }
564    #define BOUT24(o, x) { *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; }
565    #define BOUT32(o, x) { *(o++) = x >> 24; *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; }
566    #define LOUT16(o, x) { *(o++) = x; *(o++) = x >> 8; }
567    #define LOUT24(o, x) { *(o++) = x; *(o++) = x >> 8; *(o++) = x >> 16; }
568    #define LOUT32(o, x) { *(o++) = x; *(o++) = x >> 8; *(o++) = x >> 16; *(o++) = x >> 24; }
569    
570  static uint32  static uint32
571  translate_colour(uint32 colour)  translate_colour(uint32 colour)
572  {  {
573          PixelColour pc;          PixelColour pc;
574          switch (g_server_bpp)          switch (g_server_depth)
575          {          {
576                  case 15:                  case 15:
577                          pc = split_colour15(colour);                          SPLITCOLOUR15(colour, pc);
578                          break;                          break;
579                  case 16:                  case 16:
580                          pc = split_colour16(colour);                          SPLITCOLOUR16(colour, pc);
581                          break;                          break;
582                  case 24:                  case 24:
583                          pc = split_colour24(colour);                          SPLITCOLOUR24(colour, pc);
584                            break;
585                    default:
586                            /* Avoid warning */
587                            pc.red = 0;
588                            pc.green = 0;
589                            pc.blue = 0;
590                          break;                          break;
591          }          }
592          return make_colour(pc);          return MAKECOLOUR(pc);
593  }  }
594    
595    /* indent is confused by UNROLL8 */
596    /* *INDENT-OFF* */
597    
598    /* repeat and unroll, similar to bitmap.c */
599    /* potentialy any of the following translate */
600    /* functions can use repeat but just doing */
601    /* the most common ones */
602    
603    #define UNROLL8(stm) { stm stm stm stm stm stm stm stm }
604    /* 2 byte output repeat */
605    #define REPEAT2(stm) \
606    { \
607            while (out <= end - 8 * 2) \
608                    UNROLL8(stm) \
609            while (out < end) \
610                    { stm } \
611    }
612    /* 3 byte output repeat */
613    #define REPEAT3(stm) \
614    { \
615            while (out <= end - 8 * 3) \
616                    UNROLL8(stm) \
617            while (out < end) \
618                    { stm } \
619    }
620    /* 4 byte output repeat */
621    #define REPEAT4(stm) \
622    { \
623            while (out <= end - 8 * 4) \
624                    UNROLL8(stm) \
625            while (out < end) \
626                    { stm } \
627    }
628    /* *INDENT-ON* */
629    
630  static void  static void
631  translate8to8(uint8 * data, uint8 * out, uint8 * end)  translate8to8(const uint8 * data, uint8 * out, uint8 * end)
632  {  {
633          while (out < end)          while (out < end)
634                  *(out++) = (uint8) g_colmap[*(data++)];                  *(out++) = (uint8) g_colmap[*(data++)];
635  }  }
636    
637  static void  static void
638  translate8to16(uint8 * data, uint8 * out, uint8 * end)  translate8to16(const uint8 * data, uint8 * out, uint8 * end)
639  {  {
640          uint16 value;          uint16 value;
641    
642          while (out < end)          if (g_compatible_arch)
643          {          {
644                  value = (uint16) g_colmap[*(data++)];                  /* *INDENT-OFF* */
645                    REPEAT2
646                  if (g_xserver_be)                  (
647                            *((uint16 *) out) = g_colmap[*(data++)];
648                            out += 2;
649                    )
650                    /* *INDENT-ON* */
651            }
652            else if (g_xserver_be)
653            {
654                    while (out < end)
655                  {                  {
656                          *(out++) = value >> 8;                          value = (uint16) g_colmap[*(data++)];
657                          *(out++) = value;                          BOUT16(out, value);
658                  }                  }
659                  else          }
660            else
661            {
662                    while (out < end)
663                  {                  {
664                          *(out++) = value;                          value = (uint16) g_colmap[*(data++)];
665                          *(out++) = value >> 8;                          LOUT16(out, value);
666                  }                  }
667          }          }
668  }  }
669    
670  /* little endian - conversion happens when colourmap is built */  /* little endian - conversion happens when colourmap is built */
671  static void  static void
672  translate8to24(uint8 * data, uint8 * out, uint8 * end)  translate8to24(const uint8 * data, uint8 * out, uint8 * end)
673  {  {
674          uint32 value;          uint32 value;
675    
676          while (out < end)          if (g_compatible_arch)
677          {          {
678                  value = g_colmap[*(data++)];                  while (out < end)
   
                 if (g_xserver_be)  
679                  {                  {
680                          *(out++) = value >> 16;                          value = g_colmap[*(data++)];
681                          *(out++) = value >> 8;                          BOUT24(out, value);
                         *(out++) = value;  
682                  }                  }
683                  else          }
684            else
685            {
686                    while (out < end)
687                  {                  {
688                          *(out++) = value;                          value = g_colmap[*(data++)];
689                          *(out++) = value >> 8;                          LOUT24(out, value);
                         *(out++) = value >> 16;  
690                  }                  }
691          }          }
692  }  }
693    
694  static void  static void
695  translate8to32(uint8 * data, uint8 * out, uint8 * end)  translate8to32(const uint8 * data, uint8 * out, uint8 * end)
696  {  {
697          uint32 value;          uint32 value;
698    
699          while (out < end)          if (g_compatible_arch)
700          {          {
701                  value = g_colmap[*(data++)];                  /* *INDENT-OFF* */
702                    REPEAT4
703                  if (g_xserver_be)                  (
704                            *((uint32 *) out) = g_colmap[*(data++)];
705                            out += 4;
706                    )
707                    /* *INDENT-ON* */
708            }
709            else if (g_xserver_be)
710            {
711                    while (out < end)
712                  {                  {
713                          *(out++) = value >> 24;                          value = g_colmap[*(data++)];
714                          *(out++) = value >> 16;                          BOUT32(out, value);
                         *(out++) = value >> 8;  
                         *(out++) = value;  
715                  }                  }
716                  else          }
717            else
718            {
719                    while (out < end)
720                  {                  {
721                          *(out++) = value;                          value = g_colmap[*(data++)];
722                          *(out++) = value >> 8;                          LOUT32(out, value);
                         *(out++) = value >> 16;  
                         *(out++) = value >> 24;  
723                  }                  }
724          }          }
725  }  }
726    
727  static void  static void
728  translate15to16(uint16 * data, uint8 * out, uint8 * end)  translate15to16(const uint16 * data, uint8 * out, uint8 * end)
729  {  {
730          uint16 pixel;          uint16 pixel;
731          uint16 value;          uint16 value;
732            PixelColour pc;
733    
734          while (out < end)          if (g_xserver_be)
735          {          {
736                  pixel = *(data++);                  while (out < end)
   
                 if (g_host_be)  
737                  {                  {
738                          BSWAP16(pixel);                          pixel = *(data++);
739                  }                          if (g_host_be)
740                            {
741                  value = make_colour(split_colour15(pixel));                                  BSWAP16(pixel);
742                            }
743                  if (g_xserver_be)                          SPLITCOLOUR15(pixel, pc);
744                  {                          value = MAKECOLOUR(pc);
745                          *(out++) = value >> 8;                          BOUT16(out, value);
                         *(out++) = value;  
746                  }                  }
747                  else          }
748            else
749            {
750                    while (out < end)
751                  {                  {
752                          *(out++) = value;                          pixel = *(data++);
753                          *(out++) = value >> 8;                          if (g_host_be)
754                            {
755                                    BSWAP16(pixel);
756                            }
757                            SPLITCOLOUR15(pixel, pc);
758                            value = MAKECOLOUR(pc);
759                            LOUT16(out, value);
760                  }                  }
761          }          }
762  }  }
763    
764  static void  static void
765  translate15to24(uint16 * data, uint8 * out, uint8 * end)  translate15to24(const uint16 * data, uint8 * out, uint8 * end)
766  {  {
767          uint32 value;          uint32 value;
768          uint16 pixel;          uint16 pixel;
769            PixelColour pc;
770    
771          while (out < end)          if (g_compatible_arch)
772          {          {
773                  pixel = *(data++);                  /* *INDENT-OFF* */
774                    REPEAT3
775                  if (g_host_be)                  (
776                  {                          pixel = *(data++);
777                          BSWAP16(pixel);                          SPLITCOLOUR15(pixel, pc);
778                  }                          *(out++) = pc.blue;
779                            *(out++) = pc.green;
780                  value = make_colour(split_colour15(pixel));                          *(out++) = pc.red;
781                  if (g_xserver_be)                  )
782                    /* *INDENT-ON* */
783            }
784            else if (g_xserver_be)
785            {
786                    while (out < end)
787                  {                  {
788                          *(out++) = value >> 16;                          pixel = *(data++);
789                          *(out++) = value >> 8;                          if (g_host_be)
790                          *(out++) = value;                          {
791                                    BSWAP16(pixel);
792                            }
793                            SPLITCOLOUR15(pixel, pc);
794                            value = MAKECOLOUR(pc);
795                            BOUT24(out, value);
796                  }                  }
797                  else          }
798            else
799            {
800                    while (out < end)
801                  {                  {
802                          *(out++) = value;                          pixel = *(data++);
803                          *(out++) = value >> 8;                          if (g_host_be)
804                          *(out++) = value >> 16;                          {
805                                    BSWAP16(pixel);
806                            }
807                            SPLITCOLOUR15(pixel, pc);
808                            value = MAKECOLOUR(pc);
809                            LOUT24(out, value);
810                  }                  }
811          }          }
812  }  }
813    
814  static void  static void
815  translate15to32(uint16 * data, uint8 * out, uint8 * end)  translate15to32(const uint16 * data, uint8 * out, uint8 * end)
816  {  {
817          uint16 pixel;          uint16 pixel;
818          uint32 value;          uint32 value;
819            PixelColour pc;
820    
821          while (out < end)          if (g_compatible_arch)
822          {          {
823                  pixel = *(data++);                  /* *INDENT-OFF* */
824                    REPEAT4
825                  if (g_host_be)                  (
826                  {                          pixel = *(data++);
827                          BSWAP16(pixel);                          SPLITCOLOUR15(pixel, pc);
828                  }                          *(out++) = pc.blue;
829                            *(out++) = pc.green;
830                  value = make_colour(split_colour15(pixel));                          *(out++) = pc.red;
831                            *(out++) = 0;
832                  if (g_xserver_be)                  )
833                    /* *INDENT-ON* */
834            }
835            else if (g_xserver_be)
836            {
837                    while (out < end)
838                  {                  {
839                          *(out++) = value >> 24;                          pixel = *(data++);
840                          *(out++) = value >> 16;                          if (g_host_be)
841                          *(out++) = value >> 8;                          {
842                          *(out++) = value;                                  BSWAP16(pixel);
843                            }
844                            SPLITCOLOUR15(pixel, pc);
845                            value = MAKECOLOUR(pc);
846                            BOUT32(out, value);
847                  }                  }
848                  else          }
849            else
850            {
851                    while (out < end)
852                  {                  {
853                          *(out++) = value;                          pixel = *(data++);
854                          *(out++) = value >> 8;                          if (g_host_be)
855                          *(out++) = value >> 16;                          {
856                          *(out++) = value >> 24;                                  BSWAP16(pixel);
857                            }
858                            SPLITCOLOUR15(pixel, pc);
859                            value = MAKECOLOUR(pc);
860                            LOUT32(out, value);
861                  }                  }
862          }          }
863  }  }
864    
865  static void  static void
866  translate16to16(uint16 * data, uint8 * out, uint8 * end)  translate16to16(const uint16 * data, uint8 * out, uint8 * end)
867  {  {
868          uint16 pixel;          uint16 pixel;
869          uint16 value;          uint16 value;
870            PixelColour pc;
871    
872          while (out < end)          if (g_xserver_be)
873          {          {
                 pixel = *(data++);  
   
874                  if (g_host_be)                  if (g_host_be)
875                  {                  {
876                          BSWAP16(pixel);                          while (out < end)
877                            {
878                                    pixel = *(data++);
879                                    BSWAP16(pixel);
880                                    SPLITCOLOUR16(pixel, pc);
881                                    value = MAKECOLOUR(pc);
882                                    BOUT16(out, value);
883                            }
884                  }                  }
885                    else
886                  value = make_colour(split_colour16(pixel));                  {
887                            while (out < end)
888                  if (g_xserver_be)                          {
889                                    pixel = *(data++);
890                                    SPLITCOLOUR16(pixel, pc);
891                                    value = MAKECOLOUR(pc);
892                                    BOUT16(out, value);
893                            }
894                    }
895            }
896            else
897            {
898                    if (g_host_be)
899                  {                  {
900                          *(out++) = value >> 8;                          while (out < end)
901                          *(out++) = value;                          {
902                                    pixel = *(data++);
903                                    BSWAP16(pixel);
904                                    SPLITCOLOUR16(pixel, pc);
905                                    value = MAKECOLOUR(pc);
906                                    LOUT16(out, value);
907                            }
908                  }                  }
909                  else                  else
910                  {                  {
911                          *(out++) = value;                          while (out < end)
912                          *(out++) = value >> 8;                          {
913                                    pixel = *(data++);
914                                    SPLITCOLOUR16(pixel, pc);
915                                    value = MAKECOLOUR(pc);
916                                    LOUT16(out, value);
917                            }
918                  }                  }
919          }          }
920  }  }
921    
922  static void  static void
923  translate16to24(uint16 * data, uint8 * out, uint8 * end)  translate16to24(const uint16 * data, uint8 * out, uint8 * end)
924  {  {
925          uint32 value;          uint32 value;
926          uint16 pixel;          uint16 pixel;
927            PixelColour pc;
928    
929          while (out < end)          if (g_compatible_arch)
930            {
931                    /* *INDENT-OFF* */
932                    REPEAT3
933                    (
934                            pixel = *(data++);
935                            SPLITCOLOUR16(pixel, pc);
936                            *(out++) = pc.blue;
937                            *(out++) = pc.green;
938                            *(out++) = pc.red;
939                    )
940                    /* *INDENT-ON* */
941            }
942            else if (g_xserver_be)
943          {          {
                 pixel = *(data++);  
   
944                  if (g_host_be)                  if (g_host_be)
945                  {                  {
946                          BSWAP16(pixel);                          while (out < end)
947                            {
948                                    pixel = *(data++);
949                                    BSWAP16(pixel);
950                                    SPLITCOLOUR16(pixel, pc);
951                                    value = MAKECOLOUR(pc);
952                                    BOUT24(out, value);
953                            }
954                    }
955                    else
956                    {
957                            while (out < end)
958                            {
959                                    pixel = *(data++);
960                                    SPLITCOLOUR16(pixel, pc);
961                                    value = MAKECOLOUR(pc);
962                                    BOUT24(out, value);
963                            }
964                  }                  }
965            }
966                  value = make_colour(split_colour16(pixel));          else
967            {
968                  if (g_xserver_be)                  if (g_host_be)
969                  {                  {
970                          *(out++) = value >> 16;                          while (out < end)
971                          *(out++) = value >> 8;                          {
972                          *(out++) = value;                                  pixel = *(data++);
973                                    BSWAP16(pixel);
974                                    SPLITCOLOUR16(pixel, pc);
975                                    value = MAKECOLOUR(pc);
976                                    LOUT24(out, value);
977                            }
978                  }                  }
979                  else                  else
980                  {                  {
981                          *(out++) = value;                          while (out < end)
982                          *(out++) = value >> 8;                          {
983                          *(out++) = value >> 16;                                  pixel = *(data++);
984                                    SPLITCOLOUR16(pixel, pc);
985                                    value = MAKECOLOUR(pc);
986                                    LOUT24(out, value);
987                            }
988                  }                  }
989          }          }
990  }  }
991    
992  static void  static void
993  translate16to32(uint16 * data, uint8 * out, uint8 * end)  translate16to32(const uint16 * data, uint8 * out, uint8 * end)
994  {  {
995          uint16 pixel;          uint16 pixel;
996          uint32 value;          uint32 value;
997            PixelColour pc;
998    
999          while (out < end)          if (g_compatible_arch)
1000            {
1001                    /* *INDENT-OFF* */
1002                    REPEAT4
1003                    (
1004                            pixel = *(data++);
1005                            SPLITCOLOUR16(pixel, pc);
1006                            *(out++) = pc.blue;
1007                            *(out++) = pc.green;
1008                            *(out++) = pc.red;
1009                            *(out++) = 0;
1010                    )
1011                    /* *INDENT-ON* */
1012            }
1013            else if (g_xserver_be)
1014          {          {
                 pixel = *(data++);  
   
1015                  if (g_host_be)                  if (g_host_be)
1016                  {                  {
1017                          BSWAP16(pixel);                          while (out < end)
1018                            {
1019                                    pixel = *(data++);
1020                                    BSWAP16(pixel);
1021                                    SPLITCOLOUR16(pixel, pc);
1022                                    value = MAKECOLOUR(pc);
1023                                    BOUT32(out, value);
1024                            }
1025                  }                  }
1026                    else
1027                  value = make_colour(split_colour16(pixel));                  {
1028                            while (out < end)
1029                  if (g_xserver_be)                          {
1030                                    pixel = *(data++);
1031                                    SPLITCOLOUR16(pixel, pc);
1032                                    value = MAKECOLOUR(pc);
1033                                    BOUT32(out, value);
1034                            }
1035                    }
1036            }
1037            else
1038            {
1039                    if (g_host_be)
1040                  {                  {
1041                          *(out++) = value >> 24;                          while (out < end)
1042                          *(out++) = value >> 16;                          {
1043                          *(out++) = value >> 8;                                  pixel = *(data++);
1044                          *(out++) = value;                                  BSWAP16(pixel);
1045                                    SPLITCOLOUR16(pixel, pc);
1046                                    value = MAKECOLOUR(pc);
1047                                    LOUT32(out, value);
1048                            }
1049                  }                  }
1050                  else                  else
1051                  {                  {
1052                          *(out++) = value;                          while (out < end)
1053                          *(out++) = value >> 8;                          {
1054                          *(out++) = value >> 16;                                  pixel = *(data++);
1055                          *(out++) = value >> 24;                                  SPLITCOLOUR16(pixel, pc);
1056                                    value = MAKECOLOUR(pc);
1057                                    LOUT32(out, value);
1058                            }
1059                  }                  }
1060          }          }
1061  }  }
1062    
1063  static void  static void
1064  translate24to16(uint8 * data, uint8 * out, uint8 * end)  translate24to16(const uint8 * data, uint8 * out, uint8 * end)
1065  {  {
1066          uint32 pixel = 0;          uint32 pixel = 0;
1067          uint16 value;          uint16 value;
1068            PixelColour pc;
1069    
1070          while (out < end)          while (out < end)
1071          {          {
1072                  pixel = *(data++) << 16;                  pixel = *(data++) << 16;
1073                  pixel |= *(data++) << 8;                  pixel |= *(data++) << 8;
1074                  pixel |= *(data++);                  pixel |= *(data++);
1075                    SPLITCOLOUR24(pixel, pc);
1076                  value = (uint16) make_colour(split_colour24(pixel));                  value = MAKECOLOUR(pc);
   
1077                  if (g_xserver_be)                  if (g_xserver_be)
1078                  {                  {
1079                          *(out++) = value >> 8;                          BOUT16(out, value);
                         *(out++) = value;  
1080                  }                  }
1081                  else                  else
1082                  {                  {
1083                          *(out++) = value;                          LOUT16(out, value);
                         *(out++) = value >> 8;  
1084                  }                  }
1085          }          }
1086  }  }
1087    
1088  static void  static void
1089  translate24to24(uint8 * data, uint8 * out, uint8 * end)  translate24to24(const uint8 * data, uint8 * out, uint8 * end)
1090  {  {
1091          uint32 pixel;          uint32 pixel;
1092          uint32 value;          uint32 value;
1093            PixelColour pc;
1094    
1095          while (out < end)          if (g_xserver_be)
1096          {          {
1097                  pixel = *(data++) << 16;                  while (out < end)
                 pixel |= *(data++) << 8;  
                 pixel |= *(data++);  
   
                 value = make_colour(split_colour24(pixel));  
   
                 if (g_xserver_be)  
1098                  {                  {
1099                          *(out++) = value >> 16;                          pixel = *(data++) << 16;
1100                          *(out++) = value >> 8;                          pixel |= *(data++) << 8;
1101                          *(out++) = value;                          pixel |= *(data++);
1102                            SPLITCOLOUR24(pixel, pc);
1103                            value = MAKECOLOUR(pc);
1104                            BOUT24(out, value);
1105                  }                  }
1106                  else          }
1107            else
1108            {
1109                    while (out < end)
1110                  {                  {
1111                          *(out++) = value;                          pixel = *(data++) << 16;
1112                          *(out++) = value >> 8;                          pixel |= *(data++) << 8;
1113                          *(out++) = value >> 16;                          pixel |= *(data++);
1114                            SPLITCOLOUR24(pixel, pc);
1115                            value = MAKECOLOUR(pc);
1116                            LOUT24(out, value);
1117                  }                  }
1118          }          }
1119  }  }
1120    
1121  static void  static void
1122  translate24to32(uint8 * data, uint8 * out, uint8 * end)  translate24to32(const uint8 * data, uint8 * out, uint8 * end)
1123  {  {
1124          uint32 pixel;          uint32 pixel;
1125          uint32 value;          uint32 value;
1126            PixelColour pc;
1127    
1128          while (out < end)          if (g_compatible_arch)
1129          {          {
1130                  pixel = *(data++) << 16;                  /* *INDENT-OFF* */
1131                  pixel |= *(data++) << 8;  #ifdef NEED_ALIGN
1132                  pixel |= *(data++);                  REPEAT4
1133                    (
1134                  value = make_colour(split_colour24(pixel));                          *(out++) = *(data++);
1135                            *(out++) = *(data++);
1136                  if (g_xserver_be)                          *(out++) = *(data++);
1137                            *(out++) = 0;
1138                    )
1139    #else
1140                    REPEAT4
1141                    (
1142                     /* Only read 3 bytes. Reading 4 bytes means reading beyond buffer. */
1143                     *((uint32 *) out) = *((uint16 *) data) + (*((uint8 *) data + 2) << 16);
1144                     out += 4;
1145                     data += 3;
1146                    )
1147    #endif
1148                    /* *INDENT-ON* */
1149            }
1150            else if (g_xserver_be)
1151            {
1152                    while (out < end)
1153                  {                  {
1154                          *(out++) = value >> 24;                          pixel = *(data++) << 16;
1155                          *(out++) = value >> 16;                          pixel |= *(data++) << 8;
1156                          *(out++) = value >> 8;                          pixel |= *(data++);
1157                          *(out++) = value;                          SPLITCOLOUR24(pixel, pc);
1158                            value = MAKECOLOUR(pc);
1159                            BOUT32(out, value);
1160                  }                  }
1161                  else          }
1162            else
1163            {
1164                    while (out < end)
1165                  {                  {
1166                          *(out++) = value;                          pixel = *(data++) << 16;
1167                          *(out++) = value >> 8;                          pixel |= *(data++) << 8;
1168                          *(out++) = value >> 16;                          pixel |= *(data++);
1169                          *(out++) = value >> 24;                          SPLITCOLOUR24(pixel, pc);
1170                            value = MAKECOLOUR(pc);
1171                            LOUT32(out, value);
1172                  }                  }
1173          }          }
1174  }  }
# Line 587  translate24to32(uint8 * data, uint8 * ou Line 1176  translate24to32(uint8 * data, uint8 * ou
1176  static uint8 *  static uint8 *
1177  translate_image(int width, int height, uint8 * data)  translate_image(int width, int height, uint8 * data)
1178  {  {
1179          int size = width * height * g_bpp / 8;          int size;
1180          uint8 *out = (uint8 *) xmalloc(size);          uint8 *out;
1181          uint8 *end = out + size;          uint8 *end;
1182    
1183          switch (g_server_bpp)          /*
1184               If RDP depth and X Visual depths match,
1185               and arch(endian) matches, no need to translate:
1186               just return data.
1187               Note: select_visual should've already ensured g_no_translate
1188               is only set for compatible depths, but the RDP depth might've
1189               changed during connection negotiations.
1190             */
1191            if (g_no_translate_image)
1192            {
1193                    if ((g_depth == 15 && g_server_depth == 15) ||
1194                        (g_depth == 16 && g_server_depth == 16) ||
1195                        (g_depth == 24 && g_server_depth == 24))
1196                            return data;
1197            }
1198    
1199            size = width * height * (g_bpp / 8);
1200            out = (uint8 *) xmalloc(size);
1201            end = out + size;
1202    
1203            switch (g_server_depth)
1204          {          {
1205                  case 24:                  case 24:
1206                          switch (g_bpp)                          switch (g_bpp)
# Line 689  calculate_shifts(uint32 mask, int *shift Line 1298  calculate_shifts(uint32 mask, int *shift
1298          *shift_r = 8 - ffs(mask & ~(mask >> 1));          *shift_r = 8 - ffs(mask & ~(mask >> 1));
1299  }  }
1300    
1301  BOOL  /* Given a mask of a colour channel (e.g. XVisualInfo.red_mask),
1302  ui_init(void)     calculates the bits-per-pixel of this channel (a.k.a. colour weight).
1303     */
1304    static unsigned
1305    calculate_mask_weight(uint32 mask)
1306    {
1307            unsigned weight = 0;
1308            do
1309            {
1310                    weight += (mask & 1);
1311            }
1312            while (mask >>= 1);
1313            return weight;
1314    }
1315    
1316    static BOOL
1317    select_visual()
1318  {  {
         XVisualInfo vi;  
1319          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
1320          uint16 test;          int pixmap_formats_count, visuals_count;
         int i, screen_num, nvisuals;  
1321          XVisualInfo *vmatches = NULL;          XVisualInfo *vmatches = NULL;
1322          XVisualInfo template;          XVisualInfo template;
1323          Bool TrueColorVisual = False;          int i;
1324            unsigned red_weight, blue_weight, green_weight;
1325    
1326          g_display = XOpenDisplay(NULL);          red_weight = blue_weight = green_weight = 0;
1327          if (g_display == NULL)  
1328            if (g_server_depth == -1)
1329          {          {
1330                  error("Failed to open display: %s\n", XDisplayName(NULL));                  g_server_depth = DisplayPlanes(g_display, DefaultScreen(g_display));
                 return False;  
1331          }          }
1332    
1333          screen_num = DefaultScreen(g_display);          pfm = XListPixmapFormats(g_display, &pixmap_formats_count);
1334          g_x_socket = ConnectionNumber(g_display);          if (pfm == NULL)
1335          g_screen = ScreenOfDisplay(g_display, screen_num);          {
1336          g_depth = DefaultDepthOfScreen(g_screen);                  error("Unable to get list of pixmap formats from display.\n");
1337                    XCloseDisplay(g_display);
1338                    return False;
1339            }
1340    
1341          /* Search for best TrueColor depth */          /* Search for best TrueColor visual */
1342          template.class = TrueColor;          template.class = TrueColor;
1343          vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &nvisuals);          vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &visuals_count);
1344            g_visual = NULL;
1345            g_no_translate_image = False;
1346            g_compatible_arch = False;
1347            if (vmatches != NULL)
1348            {
1349                    for (i = 0; i < visuals_count; ++i)
1350                    {
1351                            XVisualInfo *visual_info = &vmatches[i];
1352                            BOOL can_translate_to_bpp = False;
1353                            int j;
1354    
1355                            /* Try to find a no-translation visual that'll
1356                               allow us to use RDP bitmaps directly as ZPixmaps. */
1357                            if (!g_xserver_be && (((visual_info->depth == 15) &&
1358                                                   /* R5G5B5 */
1359                                                   (visual_info->red_mask == 0x7c00) &&
1360                                                   (visual_info->green_mask == 0x3e0) &&
1361                                                   (visual_info->blue_mask == 0x1f)) ||
1362                                                  ((visual_info->depth == 16) &&
1363                                                   /* R5G6B5 */
1364                                                   (visual_info->red_mask == 0xf800) &&
1365                                                   (visual_info->green_mask == 0x7e0) &&
1366                                                   (visual_info->blue_mask == 0x1f)) ||
1367                                                  ((visual_info->depth == 24) &&
1368                                                   /* R8G8B8 */
1369                                                   (visual_info->red_mask == 0xff0000) &&
1370                                                   (visual_info->green_mask == 0xff00) &&
1371                                                   (visual_info->blue_mask == 0xff))))
1372                            {
1373                                    g_visual = visual_info->visual;
1374                                    g_depth = visual_info->depth;
1375                                    g_compatible_arch = !g_host_be;
1376                                    g_no_translate_image = (visual_info->depth == g_server_depth);
1377                                    if (g_no_translate_image)
1378                                            /* We found the best visual */
1379                                            break;
1380                            }
1381                            else
1382                            {
1383                                    g_compatible_arch = False;
1384                            }
1385    
1386          nvisuals--;                          if (visual_info->depth > 24)
1387          while (nvisuals >= 0)                          {
1388          {                                  /* Avoid 32-bit visuals and likes like the plague.
1389                  if ((vmatches + nvisuals)->depth > g_depth)                                     They're either untested or proven to work bad
1390                  {                                     (e.g. nvidia's Composite 32-bit visual).
1391                          g_depth = (vmatches + nvisuals)->depth;                                     Most implementation offer a 24-bit visual anyway. */
1392                                    continue;
1393                            }
1394    
1395                            /* Only care for visuals, for whose BPPs (not depths!)
1396                               we have a translateXtoY function. */
1397                            for (j = 0; j < pixmap_formats_count; ++j)
1398                            {
1399                                    if (pfm[j].depth == visual_info->depth)
1400                                    {
1401                                            if ((pfm[j].bits_per_pixel == 16) ||
1402                                                (pfm[j].bits_per_pixel == 24) ||
1403                                                (pfm[j].bits_per_pixel == 32))
1404                                            {
1405                                                    can_translate_to_bpp = True;
1406                                            }
1407                                            break;
1408                                    }
1409                            }
1410    
1411                            /* Prefer formats which have the most colour depth.
1412                               We're being truly aristocratic here, minding each
1413                               weight on its own. */
1414                            if (can_translate_to_bpp)
1415                            {
1416                                    unsigned vis_red_weight =
1417                                            calculate_mask_weight(visual_info->red_mask);
1418                                    unsigned vis_green_weight =
1419                                            calculate_mask_weight(visual_info->green_mask);
1420                                    unsigned vis_blue_weight =
1421                                            calculate_mask_weight(visual_info->blue_mask);
1422                                    if ((vis_red_weight >= red_weight)
1423                                        && (vis_green_weight >= green_weight)
1424                                        && (vis_blue_weight >= blue_weight))
1425                                    {
1426                                            red_weight = vis_red_weight;
1427                                            green_weight = vis_green_weight;
1428                                            blue_weight = vis_blue_weight;
1429                                            g_visual = visual_info->visual;
1430                                            g_depth = visual_info->depth;
1431                                    }
1432                            }
1433                  }                  }
1434                  nvisuals--;                  XFree(vmatches);
                 TrueColorVisual = True;  
1435          }          }
1436    
1437          if ((g_server_bpp == 8) && ((!TrueColorVisual) || (g_depth <= 8)))          if (g_visual != NULL)
1438          {          {
1439                  /* we use a colourmap, so the default visual should do */                  g_owncolmap = False;
1440                  g_visual = DefaultVisualOfScreen(g_screen);                  calculate_shifts(g_visual->red_mask, &g_red_shift_r, &g_red_shift_l);
1441                  g_depth = DefaultDepthOfScreen(g_screen);                  calculate_shifts(g_visual->green_mask, &g_green_shift_r, &g_green_shift_l);
1442                    calculate_shifts(g_visual->blue_mask, &g_blue_shift_r, &g_blue_shift_l);
                 /* Do not allocate colours on a TrueColor visual */  
                 if (g_visual->class == TrueColor)  
                 {  
                         g_owncolmap = False;  
                 }  
1443          }          }
1444          else          else
1445          {          {
1446                  /* need a truecolour visual */                  template.class = PseudoColor;
1447                  if (!XMatchVisualInfo(g_display, screen_num, g_depth, TrueColor, &vi))                  template.depth = 8;
1448                  {                  template.colormap_size = 256;
1449                          error("The display does not support true colour - high colour support unavailable.\n");                  vmatches =
1450                            XGetVisualInfo(g_display,
1451                                           VisualClassMask | VisualDepthMask | VisualColormapSizeMask,
1452                                           &template, &visuals_count);
1453                    if (vmatches == NULL)
1454                    {
1455                            error("No usable TrueColor or PseudoColor visuals on this display.\n");
1456                            XCloseDisplay(g_display);
1457                            XFree(pfm);
1458                          return False;                          return False;
1459                  }                  }
1460    
1461                  g_visual = vi.visual;                  /* we use a colourmap, so the default visual should do */
1462                  g_owncolmap = False;                  g_owncolmap = True;
1463                  calculate_shifts(vi.red_mask, &g_red_shift_r, &g_red_shift_l);                  g_visual = vmatches[0].visual;
1464                  calculate_shifts(vi.blue_mask, &g_blue_shift_r, &g_blue_shift_l);                  g_depth = vmatches[0].depth;
                 calculate_shifts(vi.green_mask, &g_green_shift_r, &g_green_shift_l);  
1465          }          }
1466    
1467          pfm = XListPixmapFormats(g_display, &i);          g_bpp = 0;
1468          if (pfm != NULL)          for (i = 0; i < pixmap_formats_count; ++i)
1469          {          {
1470                  /* Use maximum bpp for this depth - this is generally                  XPixmapFormatValues *pf = &pfm[i];
1471                     desirable, e.g. 24 bits->32 bits. */                  if (pf->depth == g_depth)
                 while (i--)  
1472                  {                  {
1473                          if ((pfm[i].depth == g_depth) && (pfm[i].bits_per_pixel > g_bpp))                          g_bpp = pf->bits_per_pixel;
1474    
1475                            if (g_no_translate_image)
1476                          {                          {
1477                                  g_bpp = pfm[i].bits_per_pixel;                                  switch (g_server_depth)
1478                                    {
1479                                            case 15:
1480                                            case 16:
1481                                                    if (g_bpp != 16)
1482                                                            g_no_translate_image = False;
1483                                                    break;
1484                                            case 24:
1485                                                    /* Yes, this will force image translation
1486                                                       on most modern servers which use 32 bits
1487                                                       for R8G8B8. */
1488                                                    if (g_bpp != 24)
1489                                                            g_no_translate_image = False;
1490                                                    break;
1491                                            default:
1492                                                    g_no_translate_image = False;
1493                                                    break;
1494                                    }
1495                          }                          }
1496    
1497                            /* Pixmap formats list is a depth-to-bpp mapping --
1498                               there's just a single entry for every depth,
1499                               so we can safely break here */
1500                            break;
1501                  }                  }
                 XFree(pfm);  
1502          }          }
1503            XFree(pfm);
1504            pfm = NULL;
1505            return True;
1506    }
1507    
1508          if (g_bpp < 8)  static XErrorHandler g_old_error_handler;
1509    
1510    static int
1511    error_handler(Display * dpy, XErrorEvent * eev)
1512    {
1513            if ((eev->error_code == BadMatch) && (eev->request_code == X_ConfigureWindow))
1514          {          {
1515                  error("Less than 8 bpp not currently supported.\n");                  fprintf(stderr, "Got \"BadMatch\" when trying to restack windows.\n");
1516                  XCloseDisplay(g_display);                  fprintf(stderr,
1517                            "This is most likely caused by a broken window manager (commonly KWin).\n");
1518                    return 0;
1519            }
1520    
1521            return g_old_error_handler(dpy, eev);
1522    }
1523    
1524    BOOL
1525    ui_init(void)
1526    {
1527            int screen_num;
1528    
1529            g_display = XOpenDisplay(NULL);
1530            if (g_display == NULL)
1531            {
1532                    error("Failed to open display: %s\n", XDisplayName(NULL));
1533                  return False;                  return False;
1534          }          }
1535    
1536            {
1537                    uint16 endianess_test = 1;
1538                    g_host_be = !(BOOL) (*(uint8 *) (&endianess_test));
1539            }
1540    
1541            g_old_error_handler = XSetErrorHandler(error_handler);
1542            g_xserver_be = (ImageByteOrder(g_display) == MSBFirst);
1543            screen_num = DefaultScreen(g_display);
1544            g_x_socket = ConnectionNumber(g_display);
1545            g_screen = ScreenOfDisplay(g_display, screen_num);
1546            g_depth = DefaultDepthOfScreen(g_screen);
1547    
1548            if (!select_visual())
1549                    return False;
1550    
1551            if (g_no_translate_image)
1552            {
1553                    DEBUG(("Performance optimization possible: avoiding image translation (colour depth conversion).\n"));
1554            }
1555    
1556            if (g_server_depth > g_bpp)
1557            {
1558                    warning("Remote desktop colour depth %d higher than display colour depth %d.\n",
1559                            g_server_depth, g_bpp);
1560            }
1561    
1562            DEBUG(("RDP depth: %d, display depth: %d, display bpp: %d, X server BE: %d, host BE: %d\n",
1563                   g_server_depth, g_depth, g_bpp, g_xserver_be, g_host_be));
1564    
1565          if (!g_owncolmap)          if (!g_owncolmap)
1566          {          {
1567                  g_xcolmap =                  g_xcolmap =
1568                          XCreateColormap(g_display, RootWindowOfScreen(g_screen), g_visual,                          XCreateColormap(g_display, RootWindowOfScreen(g_screen), g_visual,
1569                                          AllocNone);                                          AllocNone);
1570                  if (g_depth <= 8)                  if (g_depth <= 8)
1571                          warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n");                          warning("Display colour depth is %d bit: you may want to use -C for a private colourmap.\n", g_depth);
1572          }          }
1573    
1574          if ((!g_ownbackstore) && (DoesBackingStore(g_screen) != Always))          if ((!g_ownbackstore) && (DoesBackingStore(g_screen) != Always))
1575          {          {
1576                  warning("External BackingStore not available, using internal\n");                  warning("External BackingStore not available. Using internal.\n");
1577                  g_ownbackstore = True;                  g_ownbackstore = True;
1578          }          }
1579    
         test = 1;  
         g_host_be = !(BOOL) (*(uint8 *) (&test));  
         g_xserver_be = (ImageByteOrder(g_display) == MSBFirst);  
   
1580          /*          /*
1581           * Determine desktop size           * Determine desktop size
1582           */           */
# Line 803  ui_init(void) Line 1584  ui_init(void)
1584          {          {
1585                  g_width = WidthOfScreen(g_screen);                  g_width = WidthOfScreen(g_screen);
1586                  g_height = HeightOfScreen(g_screen);                  g_height = HeightOfScreen(g_screen);
1587                    g_using_full_workarea = True;
1588          }          }
1589          else if (g_width < 0)          else if (g_width < 0)
1590          {          {
1591                  /* Percent of screen */                  /* Percent of screen */
1592                    if (-g_width >= 100)
1593                            g_using_full_workarea = True;
1594                  g_height = HeightOfScreen(g_screen) * (-g_width) / 100;                  g_height = HeightOfScreen(g_screen) * (-g_width) / 100;
1595                  g_width = WidthOfScreen(g_screen) * (-g_width) / 100;                  g_width = WidthOfScreen(g_screen) * (-g_width) / 100;
1596          }          }
# Line 814  ui_init(void) Line 1598  ui_init(void)
1598          {          {
1599                  /* Fetch geometry from _NET_WORKAREA */                  /* Fetch geometry from _NET_WORKAREA */
1600                  uint32 x, y, cx, cy;                  uint32 x, y, cx, cy;
   
1601                  if (get_current_workarea(&x, &y, &cx, &cy) == 0)                  if (get_current_workarea(&x, &y, &cx, &cy) == 0)
1602                  {                  {
1603                          g_width = cx;                          g_width = cx;
1604                          g_height = cy;                          g_height = cy;
1605                            g_using_full_workarea = True;
1606                  }                  }
1607                  else                  else
1608                  {                  {
1609                          warning("Failed to get workarea: probably your window manager does not support extended hints\n");                          warning("Failed to get workarea: probably your window manager does not support extended hints\n");
1610                          g_width = 800;                          g_width = WidthOfScreen(g_screen);
1611                          g_height = 600;                          g_height = HeightOfScreen(g_screen);
1612                  }                  }
1613          }          }
1614    
# Line 839  ui_init(void) Line 1623  ui_init(void)
1623                  g_IM = XOpenIM(g_display, NULL, NULL, NULL);                  g_IM = XOpenIM(g_display, NULL, NULL, NULL);
1624    
1625          xclip_init();          xclip_init();
1626            ewmh_init();
1627            if (g_seamless_rdp)
1628                    seamless_init();
1629    
1630          DEBUG_RDP5(("server bpp %d client bpp %d depth %d\n", g_server_bpp, g_bpp, g_depth));          DEBUG_RDP5(("server bpp %d client bpp %d depth %d\n", g_server_depth, g_bpp, g_depth));
1631    
1632          return True;          return True;
1633  }  }
# Line 848  ui_init(void) Line 1635  ui_init(void)
1635  void  void
1636  ui_deinit(void)  ui_deinit(void)
1637  {  {
1638            while (g_seamless_windows)
1639            {
1640                    XDestroyWindow(g_display, g_seamless_windows->wnd);
1641                    sw_remove_window(g_seamless_windows);
1642            }
1643    
1644            xclip_deinit();
1645    
1646          if (g_IM != NULL)          if (g_IM != NULL)
1647                  XCloseIM(g_IM);                  XCloseIM(g_IM);
1648    
# Line 864  ui_deinit(void) Line 1659  ui_deinit(void)
1659          g_display = NULL;          g_display = NULL;
1660  }  }
1661    
1662    
1663    static void
1664    get_window_attribs(XSetWindowAttributes * attribs)
1665    {
1666            attribs->background_pixel = BlackPixelOfScreen(g_screen);
1667            attribs->background_pixel = WhitePixelOfScreen(g_screen);
1668            attribs->border_pixel = WhitePixelOfScreen(g_screen);
1669            attribs->backing_store = g_ownbackstore ? NotUseful : Always;
1670            attribs->override_redirect = g_fullscreen;
1671            attribs->colormap = g_xcolmap;
1672    }
1673    
1674    static void
1675    get_input_mask(long *input_mask)
1676    {
1677            *input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
1678                    VisibilityChangeMask | FocusChangeMask | StructureNotifyMask;
1679    
1680            if (g_sendmotion)
1681                    *input_mask |= PointerMotionMask;
1682            if (g_ownbackstore)
1683                    *input_mask |= ExposureMask;
1684            if (g_fullscreen || g_grab_keyboard)
1685                    *input_mask |= EnterWindowMask;
1686            if (g_grab_keyboard)
1687                    *input_mask |= LeaveWindowMask;
1688    }
1689    
1690  BOOL  BOOL
1691  ui_create_window(void)  ui_create_window(void)
1692  {  {
1693          uint8 null_pointer_mask[1] = { 0x80 };          uint8 null_pointer_mask[1] = { 0x80 };
1694          uint8 null_pointer_data[4] = { 0x00, 0x00, 0x00, 0x00 };          uint8 null_pointer_data[24] = { 0x00 };
1695    
1696          XSetWindowAttributes attribs;          XSetWindowAttributes attribs;
1697          XClassHint *classhints;          XClassHint *classhints;
1698          XSizeHints *sizehints;          XSizeHints *sizehints;
# Line 879  ui_create_window(void) Line 1703  ui_create_window(void)
1703          wndwidth = g_fullscreen ? WidthOfScreen(g_screen) : g_width;          wndwidth = g_fullscreen ? WidthOfScreen(g_screen) : g_width;
1704          wndheight = g_fullscreen ? HeightOfScreen(g_screen) : g_height;          wndheight = g_fullscreen ? HeightOfScreen(g_screen) : g_height;
1705    
1706          attribs.background_pixel = BlackPixelOfScreen(g_screen);          /* Handle -x-y portion of geometry string */
1707          attribs.border_pixel = WhitePixelOfScreen(g_screen);          if (g_xpos < 0 || (g_xpos == 0 && (g_pos & 2)))
1708          attribs.backing_store = g_ownbackstore ? NotUseful : Always;                  g_xpos = WidthOfScreen(g_screen) + g_xpos - g_width;
1709          attribs.override_redirect = g_fullscreen;          if (g_ypos < 0 || (g_ypos == 0 && (g_pos & 4)))
1710          attribs.colormap = g_xcolmap;                  g_ypos = HeightOfScreen(g_screen) + g_ypos - g_height;
1711    
1712          g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), 0, 0, wndwidth, wndheight,          get_window_attribs(&attribs);
1713                                0, g_depth, InputOutput, g_visual,  
1714                                CWBackPixel | CWBackingStore | CWOverrideRedirect |          g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), g_xpos, g_ypos, wndwidth,
1715                                CWColormap | CWBorderPixel, &attribs);                                wndheight, 0, g_depth, InputOutput, g_visual,
1716                                  CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap |
1717                                  CWBorderPixel, &attribs);
1718    
1719          if (g_gc == NULL)          if (g_gc == NULL)
1720            {
1721                  g_gc = XCreateGC(g_display, g_wnd, 0, NULL);                  g_gc = XCreateGC(g_display, g_wnd, 0, NULL);
1722                    ui_reset_clip();
1723            }
1724    
1725            if (g_create_bitmap_gc == NULL)
1726                    g_create_bitmap_gc = XCreateGC(g_display, g_wnd, 0, NULL);
1727    
1728          if ((g_ownbackstore) && (g_backstore == 0))          if ((g_ownbackstore) && (g_backstore == 0))
1729          {          {
# Line 905  ui_create_window(void) Line 1737  ui_create_window(void)
1737          XStoreName(g_display, g_wnd, g_title);          XStoreName(g_display, g_wnd, g_title);
1738    
1739          if (g_hide_decorations)          if (g_hide_decorations)
1740                  mwm_hide_decorations();                  mwm_hide_decorations(g_wnd);
1741    
1742          classhints = XAllocClassHint();          classhints = XAllocClassHint();
1743          if (classhints != NULL)          if (classhints != NULL)
# Line 919  ui_create_window(void) Line 1751  ui_create_window(void)
1751          if (sizehints)          if (sizehints)
1752          {          {
1753                  sizehints->flags = PMinSize | PMaxSize;                  sizehints->flags = PMinSize | PMaxSize;
1754                    if (g_pos)
1755                            sizehints->flags |= PPosition;
1756                  sizehints->min_width = sizehints->max_width = g_width;                  sizehints->min_width = sizehints->max_width = g_width;
1757                  sizehints->min_height = sizehints->max_height = g_height;                  sizehints->min_height = sizehints->max_height = g_height;
1758                  XSetWMNormalHints(g_display, g_wnd, sizehints);                  XSetWMNormalHints(g_display, g_wnd, sizehints);
1759                  XFree(sizehints);                  XFree(sizehints);
1760          }          }
1761    
1762          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |          if (g_embed_wnd)
1763                  VisibilityChangeMask | FocusChangeMask;          {
1764                    XReparentWindow(g_display, g_wnd, (Window) g_embed_wnd, 0, 0);
1765            }
1766    
1767          if (g_sendmotion)          get_input_mask(&input_mask);
                 input_mask |= PointerMotionMask;  
         if (g_ownbackstore)  
                 input_mask |= ExposureMask;  
         if (g_fullscreen || g_grab_keyboard)  
                 input_mask |= EnterWindowMask;  
         if (g_grab_keyboard)  
                 input_mask |= LeaveWindowMask;  
1768    
1769          if (g_IM != NULL)          if (g_IM != NULL)
1770          {          {
# Line 956  ui_create_window(void) Line 1785  ui_create_window(void)
1785                  XMaskEvent(g_display, VisibilityChangeMask, &xevent);                  XMaskEvent(g_display, VisibilityChangeMask, &xevent);
1786          }          }
1787          while (xevent.type != VisibilityNotify);          while (xevent.type != VisibilityNotify);
1788            g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured;
1789    
1790          g_focused = False;          g_focused = False;
1791          g_mouse_in_wnd = False;          g_mouse_in_wnd = False;
# Line 973  ui_create_window(void) Line 1803  ui_create_window(void)
1803  }  }
1804    
1805  void  void
1806    ui_resize_window()
1807    {
1808            XSizeHints *sizehints;
1809            Pixmap bs;
1810    
1811            sizehints = XAllocSizeHints();
1812            if (sizehints)
1813            {
1814                    sizehints->flags = PMinSize | PMaxSize;
1815                    sizehints->min_width = sizehints->max_width = g_width;
1816                    sizehints->min_height = sizehints->max_height = g_height;
1817                    XSetWMNormalHints(g_display, g_wnd, sizehints);
1818                    XFree(sizehints);
1819            }
1820    
1821            if (!(g_fullscreen || g_embed_wnd))
1822            {
1823                    XResizeWindow(g_display, g_wnd, g_width, g_height);
1824            }
1825    
1826            /* create new backstore pixmap */
1827            if (g_backstore != 0)
1828            {
1829                    bs = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth);
1830                    XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen));
1831                    XFillRectangle(g_display, bs, g_gc, 0, 0, g_width, g_height);
1832                    XCopyArea(g_display, g_backstore, bs, g_gc, 0, 0, g_width, g_height, 0, 0);
1833                    XFreePixmap(g_display, g_backstore);
1834                    g_backstore = bs;
1835            }
1836    }
1837    
1838    void
1839  ui_destroy_window(void)  ui_destroy_window(void)
1840  {  {
1841          if (g_IC != NULL)          if (g_IC != NULL)
# Line 986  xwin_toggle_fullscreen(void) Line 1849  xwin_toggle_fullscreen(void)
1849  {  {
1850          Pixmap contents = 0;          Pixmap contents = 0;
1851    
1852            if (g_seamless_active)
1853                    /* Turn off SeamlessRDP mode */
1854                    ui_seamless_toggle();
1855    
1856          if (!g_ownbackstore)          if (!g_ownbackstore)
1857          {          {
1858                  /* need to save contents of window */                  /* need to save contents of window */
# Line 1006  xwin_toggle_fullscreen(void) Line 1873  xwin_toggle_fullscreen(void)
1873          }          }
1874  }  }
1875    
1876  /* Process all events in Xlib queue  static void
1877    handle_button_event(XEvent xevent, BOOL down)
1878    {
1879            uint16 button, flags = 0;
1880            g_last_gesturetime = xevent.xbutton.time;
1881            button = xkeymap_translate_button(xevent.xbutton.button);
1882            if (button == 0)
1883                    return;
1884    
1885            if (down)
1886                    flags = MOUSE_FLAG_DOWN;
1887    
1888            /* Stop moving window when button is released, regardless of cursor position */
1889            if (g_moving_wnd && (xevent.type == ButtonRelease))
1890                    g_moving_wnd = False;
1891    
1892            /* If win_button_size is nonzero, enable single app mode */
1893            if (xevent.xbutton.y < g_win_button_size)
1894            {
1895                    /*  Check from right to left: */
1896                    if (xevent.xbutton.x >= g_width - g_win_button_size)
1897                    {
1898                            /* The close button, continue */
1899                            ;
1900                    }
1901                    else if (xevent.xbutton.x >= g_width - g_win_button_size * 2)
1902                    {
1903                            /* The maximize/restore button. Do not send to
1904                               server.  It might be a good idea to change the
1905                               cursor or give some other visible indication
1906                               that rdesktop inhibited this click */
1907                            if (xevent.type == ButtonPress)
1908                                    return;
1909                    }
1910                    else if (xevent.xbutton.x >= g_width - g_win_button_size * 3)
1911                    {
1912                            /* The minimize button. Iconify window. */
1913                            if (xevent.type == ButtonRelease)
1914                            {
1915                                    /* Release the mouse button outside the minimize button, to prevent the
1916                                       actual minimazation to happen */
1917                                    rdp_send_input(time(NULL), RDP_INPUT_MOUSE, button, 1, 1);
1918                                    XIconifyWindow(g_display, g_wnd, DefaultScreen(g_display));
1919                                    return;
1920                            }
1921                    }
1922                    else if (xevent.xbutton.x <= g_win_button_size)
1923                    {
1924                            /* The system menu. Ignore. */
1925                            if (xevent.type == ButtonPress)
1926                                    return;
1927                    }
1928                    else
1929                    {
1930                            /* The title bar. */
1931                            if (xevent.type == ButtonPress)
1932                            {
1933                                    if (!g_fullscreen && g_hide_decorations && !g_using_full_workarea)
1934                                    {
1935                                            g_moving_wnd = True;
1936                                            g_move_x_offset = xevent.xbutton.x;
1937                                            g_move_y_offset = xevent.xbutton.y;
1938                                    }
1939                                    return;
1940                            }
1941                    }
1942            }
1943    
1944            if (xevent.xmotion.window == g_wnd)
1945            {
1946                    rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
1947                                   flags | button, xevent.xbutton.x, xevent.xbutton.y);
1948            }
1949            else
1950            {
1951                    /* SeamlessRDP */
1952                    rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
1953                                   flags | button, xevent.xbutton.x_root, xevent.xbutton.y_root);
1954            }
1955    }
1956    
1957    
1958    /* Process events in Xlib queue
1959     Returns 0 after user quit, 1 otherwise */     Returns 0 after user quit, 1 otherwise */
1960  static int  static int
1961  xwin_process_events(void)  xwin_process_events(void)
1962  {  {
1963          XEvent xevent;          XEvent xevent;
1964          KeySym keysym;          KeySym keysym;
         uint16 button, flags;  
1965          uint32 ev_time;          uint32 ev_time;
         key_translation tr;  
1966          char str[256];          char str[256];
1967          Status status;          Status status;
1968            int events = 0;
1969            seamless_window *sw;
1970    
1971          while (XPending(g_display) > 0)          while ((XPending(g_display) > 0) && events++ < 20)
1972          {          {
1973                  XNextEvent(g_display, &xevent);                  XNextEvent(g_display, &xevent);
1974    
# Line 1029  xwin_process_events(void) Line 1978  xwin_process_events(void)
1978                          continue;                          continue;
1979                  }                  }
1980    
                 flags = 0;  
   
1981                  switch (xevent.type)                  switch (xevent.type)
1982                  {                  {
1983                            case VisibilityNotify:
1984                                    if (xevent.xvisibility.window == g_wnd)
1985                                            g_Unobscured =
1986                                                    xevent.xvisibility.state == VisibilityUnobscured;
1987    
1988                                    break;
1989                          case ClientMessage:                          case ClientMessage:
1990                                  /* the window manager told us to quit */                                  /* the window manager told us to quit */
1991                                  if ((xevent.xclient.message_type == g_protocol_atom)                                  if ((xevent.xclient.message_type == g_protocol_atom)
# Line 1064  xwin_process_events(void) Line 2017  xwin_process_events(void)
2017                                                        str, sizeof(str), &keysym, NULL);                                                        str, sizeof(str), &keysym, NULL);
2018                                  }                                  }
2019    
2020                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym,                                  DEBUG_KBD(("KeyPress for keysym (0x%lx, %s)\n", keysym,
2021                                             get_ksname(keysym)));                                             get_ksname(keysym)));
2022    
2023                                  ev_time = time(NULL);                                  ev_time = time(NULL);
2024                                  if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))                                  if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))
2025                                          break;                                          break;
2026    
2027                                  tr = xkeymap_translate_key(keysym,                                  xkeymap_send_keys(keysym, xevent.xkey.keycode, xevent.xkey.state,
2028                                                             xevent.xkey.keycode, xevent.xkey.state);                                                    ev_time, True, 0);
   
                                 if (tr.scancode == 0)  
                                         break;  
   
                                 save_remote_modifiers(tr.scancode);  
                                 ensure_remote_modifiers(ev_time, tr);  
                                 rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);  
                                 restore_remote_modifiers(ev_time, tr.scancode);  
   
2029                                  break;                                  break;
2030    
2031                          case KeyRelease:                          case KeyRelease:
# Line 1089  xwin_process_events(void) Line 2033  xwin_process_events(void)
2033                                  XLookupString((XKeyEvent *) & xevent, str,                                  XLookupString((XKeyEvent *) & xevent, str,
2034                                                sizeof(str), &keysym, NULL);                                                sizeof(str), &keysym, NULL);
2035    
2036                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,                                  DEBUG_KBD(("\nKeyRelease for keysym (0x%lx, %s)\n", keysym,
2037                                             get_ksname(keysym)));                                             get_ksname(keysym)));
2038    
2039                                  ev_time = time(NULL);                                  ev_time = time(NULL);
2040                                  if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False))                                  if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False))
2041                                          break;                                          break;
2042    
2043                                  tr = xkeymap_translate_key(keysym,                                  xkeymap_send_keys(keysym, xevent.xkey.keycode, xevent.xkey.state,
2044                                                             xevent.xkey.keycode, xevent.xkey.state);                                                    ev_time, False, 0);
   
                                 if (tr.scancode == 0)  
                                         break;  
   
                                 rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);  
2045                                  break;                                  break;
2046    
2047                          case ButtonPress:                          case ButtonPress:
2048                                  flags = MOUSE_FLAG_DOWN;                                  handle_button_event(xevent, True);
2049                                  /* fall through */                                  break;
2050    
2051                          case ButtonRelease:                          case ButtonRelease:
2052                                  g_last_gesturetime = xevent.xbutton.time;                                  handle_button_event(xevent, False);
                                 button = xkeymap_translate_button(xevent.xbutton.button);  
                                 if (button == 0)  
                                         break;  
   
                                 /* If win_button_size is nonzero, enable single app mode */  
                                 if (xevent.xbutton.y < g_win_button_size)  
                                 {  
                                         /* Stop moving window when button is released, regardless of cursor position */  
                                         if (g_moving_wnd && (xevent.type == ButtonRelease))  
                                                 g_moving_wnd = False;  
   
                                         /*  Check from right to left: */  
   
                                         if (xevent.xbutton.x >= g_width - g_win_button_size)  
                                         {  
                                                 /* The close button, continue */  
                                                 ;  
                                         }  
                                         else if (xevent.xbutton.x >=  
                                                  g_width - g_win_button_size * 2)  
                                         {  
                                                 /* The maximize/restore button. Do not send to  
                                                    server.  It might be a good idea to change the  
                                                    cursor or give some other visible indication  
                                                    that rdesktop inhibited this click */  
                                                 break;  
                                         }  
                                         else if (xevent.xbutton.x >=  
                                                  g_width - g_win_button_size * 3)  
                                         {  
                                                 /* The minimize button. Iconify window. */  
                                                 XIconifyWindow(g_display, g_wnd,  
                                                                DefaultScreen(g_display));  
                                                 break;  
                                         }  
                                         else if (xevent.xbutton.x <= g_win_button_size)  
                                         {  
                                                 /* The system menu. Ignore. */  
                                                 break;  
                                         }  
                                         else  
                                         {  
                                                 /* The title bar. */  
                                                 if ((xevent.type == ButtonPress) && !g_fullscreen  
                                                     && g_hide_decorations)  
                                                 {  
                                                         g_moving_wnd = True;  
                                                         g_move_x_offset = xevent.xbutton.x;  
                                                         g_move_y_offset = xevent.xbutton.y;  
                                                 }  
                                                 break;  
   
                                         }  
                                 }  
   
                                 rdp_send_input(time(NULL), RDP_INPUT_MOUSE,  
                                                flags | button, xevent.xbutton.x, xevent.xbutton.y);  
2053                                  break;                                  break;
2054    
2055                          case MotionNotify:                          case MotionNotify:
# Line 1182  xwin_process_events(void) Line 2064  xwin_process_events(void)
2064                                  if (g_fullscreen && !g_focused)                                  if (g_fullscreen && !g_focused)
2065                                          XSetInputFocus(g_display, g_wnd, RevertToPointerRoot,                                          XSetInputFocus(g_display, g_wnd, RevertToPointerRoot,
2066                                                         CurrentTime);                                                         CurrentTime);
2067                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,  
2068                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                  if (xevent.xmotion.window == g_wnd)
2069                                    {
2070                                            rdp_send_input(time(NULL), RDP_INPUT_MOUSE, MOUSE_FLAG_MOVE,
2071                                                           xevent.xmotion.x, xevent.xmotion.y);
2072                                    }
2073                                    else
2074                                    {
2075                                            /* SeamlessRDP */
2076                                            rdp_send_input(time(NULL), RDP_INPUT_MOUSE, MOUSE_FLAG_MOVE,
2077                                                           xevent.xmotion.x_root,
2078                                                           xevent.xmotion.y_root);
2079                                    }
2080                                  break;                                  break;
2081    
2082                          case FocusIn:                          case FocusIn:
# Line 1194  xwin_process_events(void) Line 2087  xwin_process_events(void)
2087                                  if (g_grab_keyboard && g_mouse_in_wnd)                                  if (g_grab_keyboard && g_mouse_in_wnd)
2088                                          XGrabKeyboard(g_display, g_wnd, True,                                          XGrabKeyboard(g_display, g_wnd, True,
2089                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
2090    
2091                                    sw = sw_get_window_by_wnd(xevent.xfocus.window);
2092                                    if (!sw)
2093                                            break;
2094    
2095                                    if (sw->id != g_seamless_focused)
2096                                    {
2097                                            seamless_send_focus(sw->id, 0);
2098                                            g_seamless_focused = sw->id;
2099                                    }
2100                                  break;                                  break;
2101    
2102                          case FocusOut:                          case FocusOut:
# Line 1226  xwin_process_events(void) Line 2129  xwin_process_events(void)
2129                                  break;                                  break;
2130    
2131                          case Expose:                          case Expose:
2132                                  XCopyArea(g_display, g_backstore, g_wnd, g_gc,                                  if (xevent.xexpose.window == g_wnd)
2133                                            xevent.xexpose.x, xevent.xexpose.y,                                  {
2134                                            xevent.xexpose.width,                                          XCopyArea(g_display, g_backstore, xevent.xexpose.window,
2135                                            xevent.xexpose.height,                                                    g_gc,
2136                                            xevent.xexpose.x, xevent.xexpose.y);                                                    xevent.xexpose.x, xevent.xexpose.y,
2137                                                      xevent.xexpose.width, xevent.xexpose.height,
2138                                                      xevent.xexpose.x, xevent.xexpose.y);
2139                                    }
2140                                    else
2141                                    {
2142                                            sw = sw_get_window_by_wnd(xevent.xexpose.window);
2143                                            if (!sw)
2144                                                    break;
2145                                            XCopyArea(g_display, g_backstore,
2146                                                      xevent.xexpose.window, g_gc,
2147                                                      xevent.xexpose.x + sw->xoffset,
2148                                                      xevent.xexpose.y + sw->yoffset,
2149                                                      xevent.xexpose.width,
2150                                                      xevent.xexpose.height, xevent.xexpose.x,
2151                                                      xevent.xexpose.y);
2152                                    }
2153    
2154                                  break;                                  break;
2155    
2156                          case MappingNotify:                          case MappingNotify:
# Line 1259  xwin_process_events(void) Line 2179  xwin_process_events(void)
2179                                  break;                                  break;
2180                          case PropertyNotify:                          case PropertyNotify:
2181                                  xclip_handle_PropertyNotify(&xevent.xproperty);                                  xclip_handle_PropertyNotify(&xevent.xproperty);
2182                                    if (xevent.xproperty.window == g_wnd)
2183                                            break;
2184                                    if (xevent.xproperty.window == DefaultRootWindow(g_display))
2185                                            break;
2186    
2187                                    /* seamless */
2188                                    sw = sw_get_window_by_wnd(xevent.xproperty.window);
2189                                    if (!sw)
2190                                            break;
2191    
2192                                    if ((xevent.xproperty.atom == g_net_wm_state_atom)
2193                                        && (xevent.xproperty.state == PropertyNewValue))
2194                                    {
2195                                            sw->state = ewmh_get_window_state(sw->wnd);
2196                                            seamless_send_state(sw->id, sw->state, 0);
2197                                    }
2198    
2199                                    if ((xevent.xproperty.atom == g_net_wm_desktop_atom)
2200                                        && (xevent.xproperty.state == PropertyNewValue))
2201                                    {
2202                                            sw->desktop = ewmh_get_window_desktop(sw->wnd);
2203                                            sw_all_to_desktop(sw->wnd, sw->desktop);
2204                                    }
2205    
2206                                    break;
2207                            case MapNotify:
2208                                    if (!g_seamless_active)
2209                                            rdp_send_client_window_status(1);
2210                                    break;
2211                            case UnmapNotify:
2212                                    if (!g_seamless_active)
2213                                            rdp_send_client_window_status(0);
2214                                    break;
2215                            case ConfigureNotify:
2216                                    if (!g_seamless_active)
2217                                            break;
2218    
2219                                    sw = sw_get_window_by_wnd(xevent.xconfigure.window);
2220                                    if (!sw)
2221                                            break;
2222    
2223                                    gettimeofday(sw->position_timer, NULL);
2224                                    if (sw->position_timer->tv_usec + SEAMLESSRDP_POSITION_TIMER >=
2225                                        1000000)
2226                                    {
2227                                            sw->position_timer->tv_usec +=
2228                                                    SEAMLESSRDP_POSITION_TIMER - 1000000;
2229                                            sw->position_timer->tv_sec += 1;
2230                                    }
2231                                    else
2232                                    {
2233                                            sw->position_timer->tv_usec += SEAMLESSRDP_POSITION_TIMER;
2234                                    }
2235    
2236                                    sw_handle_restack(sw);
2237                                  break;                                  break;
2238                  }                  }
2239          }          }
# Line 1283  ui_select(int rdp_socket) Line 2258  ui_select(int rdp_socket)
2258                          /* User quit */                          /* User quit */
2259                          return 0;                          return 0;
2260    
2261                    if (g_seamless_active)
2262                            sw_check_timers();
2263    
2264                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
2265                  FD_ZERO(&wfds);                  FD_ZERO(&wfds);
2266                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
# Line 1302  ui_select(int rdp_socket) Line 2280  ui_select(int rdp_socket)
2280    
2281                  /* add redirection handles */                  /* add redirection handles */
2282                  rdpdr_add_fds(&n, &rfds, &wfds, &tv, &s_timeout);                  rdpdr_add_fds(&n, &rfds, &wfds, &tv, &s_timeout);
2283                    seamless_select_timeout(&tv);
2284    
2285                  n++;                  n++;
2286    
# Line 1311  ui_select(int rdp_socket) Line 2290  ui_select(int rdp_socket)
2290                                  error("select: %s\n", strerror(errno));                                  error("select: %s\n", strerror(errno));
2291    
2292                          case 0:                          case 0:
2293                                  /* TODO: if tv.tv_sec just times out                                  /* Abort serial read calls */
2294                                   * we will segfault.                                  if (s_timeout)
2295                                   * FIXME:                                          rdpdr_check_fds(&rfds, &wfds, (BOOL) True);
                                  */  
                                 //s_timeout = True;  
                                 //rdpdr_check_fds(&rfds, &wfds, (BOOL) True);  
2296                                  continue;                                  continue;
2297                  }                  }
2298    
# Line 1346  ui_create_bitmap(int width, int height, Line 2322  ui_create_bitmap(int width, int height,
2322          uint8 *tdata;          uint8 *tdata;
2323          int bitmap_pad;          int bitmap_pad;
2324    
2325          if (g_server_bpp == 8)          if (g_server_depth == 8)
2326          {          {
2327                  bitmap_pad = 8;                  bitmap_pad = 8;
2328          }          }
# Line 1363  ui_create_bitmap(int width, int height, Line 2339  ui_create_bitmap(int width, int height,
2339          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,          image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0,
2340                               (char *) tdata, width, height, bitmap_pad, 0);                               (char *) tdata, width, height, bitmap_pad, 0);
2341    
2342          XPutImage(g_display, bitmap, g_gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, g_create_bitmap_gc, image, 0, 0, 0, 0, width, height);
2343    
2344          XFree(image);          XFree(image);
2345          if (!g_owncolmap)          if (tdata != data)
2346                  xfree(tdata);                  xfree(tdata);
2347          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
2348  }  }
# Line 1378  ui_paint_bitmap(int x, int y, int cx, in Line 2354  ui_paint_bitmap(int x, int y, int cx, in
2354          uint8 *tdata;          uint8 *tdata;
2355          int bitmap_pad;          int bitmap_pad;
2356    
2357          if (g_server_bpp == 8)          if (g_server_depth == 8)
2358          {          {
2359                  bitmap_pad = 8;                  bitmap_pad = 8;
2360          }          }
# Line 1398  ui_paint_bitmap(int x, int y, int cx, in Line 2374  ui_paint_bitmap(int x, int y, int cx, in
2374          {          {
2375                  XPutImage(g_display, g_backstore, g_gc, image, 0, 0, x, y, cx, cy);                  XPutImage(g_display, g_backstore, g_gc, image, 0, 0, x, y, cx, cy);
2376                  XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);                  XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);
2377                    ON_ALL_SEAMLESS_WINDOWS(XCopyArea,
2378                                            (g_display, g_backstore, sw->wnd, g_gc, x, y, cx, cy,
2379                                             x - sw->xoffset, y - sw->yoffset));
2380          }          }
2381          else          else
2382          {          {
2383                  XPutImage(g_display, g_wnd, g_gc, image, 0, 0, x, y, cx, cy);                  XPutImage(g_display, g_wnd, g_gc, image, 0, 0, x, y, cx, cy);
2384                    ON_ALL_SEAMLESS_WINDOWS(XCopyArea,
2385                                            (g_display, g_wnd, sw->wnd, g_gc, x, y, cx, cy,
2386                                             x - sw->xoffset, y - sw->yoffset));
2387          }          }
2388    
2389          XFree(image);          XFree(image);
2390          if (!g_owncolmap)          if (tdata != data)
2391                  xfree(tdata);                  xfree(tdata);
2392  }  }
2393    
# Line 1421  ui_create_glyph(int width, int height, u Line 2403  ui_create_glyph(int width, int height, u
2403          XImage *image;          XImage *image;
2404          Pixmap bitmap;          Pixmap bitmap;
2405          int scanline;          int scanline;
         GC gc;  
2406    
2407          scanline = (width + 7) / 8;          scanline = (width + 7) / 8;
2408    
2409          bitmap = XCreatePixmap(g_display, g_wnd, width, height, 1);          bitmap = XCreatePixmap(g_display, g_wnd, width, height, 1);
2410          gc = XCreateGC(g_display, bitmap, 0, NULL);          if (g_create_glyph_gc == 0)
2411                    g_create_glyph_gc = XCreateGC(g_display, bitmap, 0, NULL);
2412    
2413          image = XCreateImage(g_display, g_visual, 1, ZPixmap, 0, (char *) data,          image = XCreateImage(g_display, g_visual, 1, ZPixmap, 0, (char *) data,
2414                               width, height, 8, scanline);                               width, height, 8, scanline);
# Line 1434  ui_create_glyph(int width, int height, u Line 2416  ui_create_glyph(int width, int height, u
2416          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
2417          XInitImage(image);          XInitImage(image);
2418    
2419          XPutImage(g_display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(g_display, bitmap, g_create_glyph_gc, image, 0, 0, 0, 0, width, height);
2420    
2421          XFree(image);          XFree(image);
         XFreeGC(g_display, gc);  
2422          return (HGLYPH) bitmap;          return (HGLYPH) bitmap;
2423  }  }
2424    
# Line 1523  ui_set_cursor(HCURSOR cursor) Line 2504  ui_set_cursor(HCURSOR cursor)
2504  {  {
2505          g_current_cursor = (Cursor) cursor;          g_current_cursor = (Cursor) cursor;
2506          XDefineCursor(g_display, g_wnd, g_current_cursor);          XDefineCursor(g_display, g_wnd, g_current_cursor);
2507            ON_ALL_SEAMLESS_WINDOWS(XDefineCursor, (g_display, sw->wnd, g_current_cursor));
2508  }  }
2509    
2510  void  void
# Line 1664  ui_set_colourmap(HCOLOURMAP map) Line 2646  ui_set_colourmap(HCOLOURMAP map)
2646                  g_colmap = (uint32 *) map;                  g_colmap = (uint32 *) map;
2647          }          }
2648          else          else
2649            {
2650                  XSetWindowColormap(g_display, g_wnd, (Colormap) map);                  XSetWindowColormap(g_display, g_wnd, (Colormap) map);
2651                    ON_ALL_SEAMLESS_WINDOWS(XSetWindowColormap, (g_display, sw->wnd, (Colormap) map));
2652            }
2653  }  }
2654    
2655  void  void
2656  ui_set_clip(int x, int y, int cx, int cy)  ui_set_clip(int x, int y, int cx, int cy)
2657  {  {
2658          XRectangle rect;          g_clip_rectangle.x = x;
2659            g_clip_rectangle.y = y;
2660          rect.x = x;          g_clip_rectangle.width = cx;
2661          rect.y = y;          g_clip_rectangle.height = cy;
2662          rect.width = cx;          XSetClipRectangles(g_display, g_gc, 0, 0, &g_clip_rectangle, 1, YXBanded);
         rect.height = cy;  
         XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded);  
2663  }  }
2664    
2665  void  void
2666  ui_reset_clip(void)  ui_reset_clip(void)
2667  {  {
2668          XRectangle rect;          g_clip_rectangle.x = 0;
2669            g_clip_rectangle.y = 0;
2670          rect.x = 0;          g_clip_rectangle.width = g_width;
2671          rect.y = 0;          g_clip_rectangle.height = g_height;
2672          rect.width = g_width;          XSetClipRectangles(g_display, g_gc, 0, 0, &g_clip_rectangle, 1, YXBanded);
         rect.height = g_height;  
         XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded);  
2673  }  }
2674    
2675  void  void
# Line 1729  ui_patblt(uint8 opcode, Line 2710  ui_patblt(uint8 opcode,
2710          {          {
2711                  case 0: /* Solid */                  case 0: /* Solid */
2712                          SET_FOREGROUND(fgcolour);                          SET_FOREGROUND(fgcolour);
2713                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
2714                          break;                          break;
2715    
2716                  case 2: /* Hatch */                  case 2: /* Hatch */
# Line 1740  ui_patblt(uint8 opcode, Line 2721  ui_patblt(uint8 opcode,
2721                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
2722                          XSetStipple(g_display, g_gc, fill);                          XSetStipple(g_display, g_gc, fill);
2723                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
2724                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
2725                          XSetFillStyle(g_display, g_gc, FillSolid);                          XSetFillStyle(g_display, g_gc, FillSolid);
2726                          XSetTSOrigin(g_display, g_gc, 0, 0);                          XSetTSOrigin(g_display, g_gc, 0, 0);
2727                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
# Line 1750  ui_patblt(uint8 opcode, Line 2731  ui_patblt(uint8 opcode,
2731                          for (i = 0; i != 8; i++)                          for (i = 0; i != 8; i++)
2732                                  ipattern[7 - i] = brush->pattern[i];                                  ipattern[7 - i] = brush->pattern[i];
2733                          fill = (Pixmap) ui_create_glyph(8, 8, ipattern);                          fill = (Pixmap) ui_create_glyph(8, 8, ipattern);
   
2734                          SET_FOREGROUND(bgcolour);                          SET_FOREGROUND(bgcolour);
2735                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(fgcolour);
2736                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);                          XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
2737                          XSetStipple(g_display, g_gc, fill);                          XSetStipple(g_display, g_gc, fill);
2738                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);                          XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
2739                            FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
                         FILL_RECTANGLE(x, y, cx, cy);  
   
2740                          XSetFillStyle(g_display, g_gc, FillSolid);                          XSetFillStyle(g_display, g_gc, FillSolid);
2741                          XSetTSOrigin(g_display, g_gc, 0, 0);                          XSetTSOrigin(g_display, g_gc, 0, 0);
2742                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
# Line 1769  ui_patblt(uint8 opcode, Line 2747  ui_patblt(uint8 opcode,
2747          }          }
2748    
2749          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
2750    
2751            if (g_ownbackstore)
2752                    XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);
2753            ON_ALL_SEAMLESS_WINDOWS(XCopyArea,
2754                                    (g_display, g_ownbackstore ? g_backstore : g_wnd, sw->wnd, g_gc,
2755                                     x, y, cx, cy, x - sw->xoffset, y - sw->yoffset));
2756  }  }
2757    
2758  void  void
# Line 1779  ui_screenblt(uint8 opcode, Line 2763  ui_screenblt(uint8 opcode,
2763          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
2764          if (g_ownbackstore)          if (g_ownbackstore)
2765          {          {
2766                  XCopyArea(g_display, g_backstore, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);                  XCopyArea(g_display, g_Unobscured ? g_wnd : g_backstore,
2767                              g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
2768                  XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, y);                  XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, y);
2769          }          }
2770          else          else
2771          {          {
2772                  XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);                  XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
2773          }          }
2774    
2775            ON_ALL_SEAMLESS_WINDOWS(XCopyArea,
2776                                    (g_display, g_ownbackstore ? g_backstore : g_wnd,
2777                                     sw->wnd, g_gc, x, y, cx, cy, x - sw->xoffset, y - sw->yoffset));
2778    
2779          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
2780  }  }
2781    
# Line 1796  ui_memblt(uint8 opcode, Line 2786  ui_memblt(uint8 opcode,
2786  {  {
2787          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
2788          XCopyArea(g_display, (Pixmap) src, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);          XCopyArea(g_display, (Pixmap) src, g_wnd, g_gc, srcx, srcy, cx, cy, x, y);
2789            ON_ALL_SEAMLESS_WINDOWS(XCopyArea,
2790                                    (g_display, (Pixmap) src, sw->wnd, g_gc,
2791                                     srcx, srcy, cx, cy, x - sw->xoffset, y - sw->yoffset));
2792          if (g_ownbackstore)          if (g_ownbackstore)
2793                  XCopyArea(g_display, (Pixmap) src, g_backstore, g_gc, srcx, srcy, cx, cy, x, y);                  XCopyArea(g_display, (Pixmap) src, g_backstore, g_gc, srcx, srcy, cx, cy, x, y);
2794          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
# Line 1842  ui_line(uint8 opcode, Line 2835  ui_line(uint8 opcode,
2835          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
2836          SET_FOREGROUND(pen->colour);          SET_FOREGROUND(pen->colour);
2837          XDrawLine(g_display, g_wnd, g_gc, startx, starty, endx, endy);          XDrawLine(g_display, g_wnd, g_gc, startx, starty, endx, endy);
2838            ON_ALL_SEAMLESS_WINDOWS(XDrawLine, (g_display, sw->wnd, g_gc,
2839                                                startx - sw->xoffset, starty - sw->yoffset,
2840                                                endx - sw->xoffset, endy - sw->yoffset));
2841          if (g_ownbackstore)          if (g_ownbackstore)
2842                  XDrawLine(g_display, g_backstore, g_gc, startx, starty, endx, endy);                  XDrawLine(g_display, g_backstore, g_gc, startx, starty, endx, endy);
2843          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
# Line 1856  ui_rect( Line 2852  ui_rect(
2852          FILL_RECTANGLE(x, y, cx, cy);          FILL_RECTANGLE(x, y, cx, cy);
2853  }  }
2854    
2855    void
2856    ui_polygon(uint8 opcode,
2857               /* mode */ uint8 fillmode,
2858               /* dest */ POINT * point, int npoints,
2859               /* brush */ BRUSH * brush, int bgcolour, int fgcolour)
2860    {
2861            uint8 style, i, ipattern[8];
2862            Pixmap fill;
2863    
2864            SET_FUNCTION(opcode);
2865    
2866            switch (fillmode)
2867            {
2868                    case ALTERNATE:
2869                            XSetFillRule(g_display, g_gc, EvenOddRule);
2870                            break;
2871                    case WINDING:
2872                            XSetFillRule(g_display, g_gc, WindingRule);
2873                            break;
2874                    default:
2875                            unimpl("fill mode %d\n", fillmode);
2876            }
2877    
2878            if (brush)
2879                    style = brush->style;
2880            else
2881                    style = 0;
2882    
2883            switch (style)
2884            {
2885                    case 0: /* Solid */
2886                            SET_FOREGROUND(fgcolour);
2887                            FILL_POLYGON((XPoint *) point, npoints);
2888                            break;
2889    
2890                    case 2: /* Hatch */
2891                            fill = (Pixmap) ui_create_glyph(8, 8,
2892                                                            hatch_patterns + brush->pattern[0] * 8);
2893                            SET_FOREGROUND(fgcolour);
2894                            SET_BACKGROUND(bgcolour);
2895                            XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
2896                            XSetStipple(g_display, g_gc, fill);
2897                            XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
2898                            FILL_POLYGON((XPoint *) point, npoints);
2899                            XSetFillStyle(g_display, g_gc, FillSolid);
2900                            XSetTSOrigin(g_display, g_gc, 0, 0);
2901                            ui_destroy_glyph((HGLYPH) fill);
2902                            break;
2903    
2904                    case 3: /* Pattern */
2905                            for (i = 0; i != 8; i++)
2906                                    ipattern[7 - i] = brush->pattern[i];
2907                            fill = (Pixmap) ui_create_glyph(8, 8, ipattern);
2908                            SET_FOREGROUND(bgcolour);
2909                            SET_BACKGROUND(fgcolour);
2910                            XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
2911                            XSetStipple(g_display, g_gc, fill);
2912                            XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
2913                            FILL_POLYGON((XPoint *) point, npoints);
2914                            XSetFillStyle(g_display, g_gc, FillSolid);
2915                            XSetTSOrigin(g_display, g_gc, 0, 0);
2916                            ui_destroy_glyph((HGLYPH) fill);
2917                            break;
2918    
2919                    default:
2920                            unimpl("brush %d\n", brush->style);
2921            }
2922    
2923            RESET_FUNCTION(opcode);
2924    }
2925    
2926    void
2927    ui_polyline(uint8 opcode,
2928                /* dest */ POINT * points, int npoints,
2929                /* pen */ PEN * pen)
2930    {
2931            /* TODO: set join style */
2932            SET_FUNCTION(opcode);
2933            SET_FOREGROUND(pen->colour);
2934            XDrawLines(g_display, g_wnd, g_gc, (XPoint *) points, npoints, CoordModePrevious);
2935            if (g_ownbackstore)
2936                    XDrawLines(g_display, g_backstore, g_gc, (XPoint *) points, npoints,
2937                               CoordModePrevious);
2938    
2939            ON_ALL_SEAMLESS_WINDOWS(seamless_XDrawLines,
2940                                    (sw->wnd, (XPoint *) points, npoints, sw->xoffset, sw->yoffset));
2941    
2942            RESET_FUNCTION(opcode);
2943    }
2944    
2945    void
2946    ui_ellipse(uint8 opcode,
2947               /* mode */ uint8 fillmode,
2948               /* dest */ int x, int y, int cx, int cy,
2949               /* brush */ BRUSH * brush, int bgcolour, int fgcolour)
2950    {
2951            uint8 style, i, ipattern[8];
2952            Pixmap fill;
2953    
2954            SET_FUNCTION(opcode);
2955    
2956            if (brush)
2957                    style = brush->style;
2958            else
2959                    style = 0;
2960    
2961            switch (style)
2962            {
2963                    case 0: /* Solid */
2964                            SET_FOREGROUND(fgcolour);
2965                            DRAW_ELLIPSE(x, y, cx, cy, fillmode);
2966                            break;
2967    
2968                    case 2: /* Hatch */
2969                            fill = (Pixmap) ui_create_glyph(8, 8,
2970                                                            hatch_patterns + brush->pattern[0] * 8);
2971                            SET_FOREGROUND(fgcolour);
2972                            SET_BACKGROUND(bgcolour);
2973                            XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
2974                            XSetStipple(g_display, g_gc, fill);
2975                            XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
2976                            DRAW_ELLIPSE(x, y, cx, cy, fillmode);
2977                            XSetFillStyle(g_display, g_gc, FillSolid);
2978                            XSetTSOrigin(g_display, g_gc, 0, 0);
2979                            ui_destroy_glyph((HGLYPH) fill);
2980                            break;
2981    
2982                    case 3: /* Pattern */
2983                            for (i = 0; i != 8; i++)
2984                                    ipattern[7 - i] = brush->pattern[i];
2985                            fill = (Pixmap) ui_create_glyph(8, 8, ipattern);
2986                            SET_FOREGROUND(bgcolour);
2987                            SET_BACKGROUND(fgcolour);
2988                            XSetFillStyle(g_display, g_gc, FillOpaqueStippled);
2989                            XSetStipple(g_display, g_gc, fill);
2990                            XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin);
2991                            DRAW_ELLIPSE(x, y, cx, cy, fillmode);
2992                            XSetFillStyle(g_display, g_gc, FillSolid);
2993                            XSetTSOrigin(g_display, g_gc, 0, 0);
2994                            ui_destroy_glyph((HGLYPH) fill);
2995                            break;
2996    
2997                    default:
2998                            unimpl("brush %d\n", brush->style);
2999            }
3000    
3001            RESET_FUNCTION(opcode);
3002    }
3003    
3004  /* warning, this function only draws on wnd or backstore, not both */  /* warning, this function only draws on wnd or backstore, not both */
3005  void  void
3006  ui_draw_glyph(int mixmode,  ui_draw_glyph(int mixmode,
# Line 1911  ui_draw_glyph(int mixmode, Line 3056  ui_draw_glyph(int mixmode,
3056  }  }
3057    
3058  void  void
3059  ui_draw_text(uint8 font, uint8 flags, int mixmode, int x, int y,  ui_draw_text(uint8 font, uint8 flags, uint8 opcode, int mixmode, int x, int y,
3060               int clipx, int clipy, int clipcx, int clipcy,               int clipx, int clipy, int clipcx, int clipcy,
3061               int boxx, int boxy, int boxcx, int boxcy, int bgcolour,               int boxx, int boxy, int boxcx, int boxcy, BRUSH * brush,
3062               int fgcolour, uint8 * text, uint8 length)               int bgcolour, int fgcolour, uint8 * text, uint8 length)
3063  {  {
3064            /* TODO: use brush appropriately */
3065    
3066          FONTGLYPH *glyph;          FONTGLYPH *glyph;
3067          int i, j, xyoffset, x1, y1;          int i, j, xyoffset, x1, y1;
3068          DATABLOB *entry;          DATABLOB *entry;
# Line 1947  ui_draw_text(uint8 font, uint8 flags, in Line 3094  ui_draw_text(uint8 font, uint8 flags, in
3094                  switch (text[i])                  switch (text[i])
3095                  {                  {
3096                          case 0xff:                          case 0xff:
3097                                  if (i + 2 < length)                                  /* At least two bytes needs to follow */
3098                                          cache_put_text(text[i + 1], text, text[i + 2]);                                  if (i + 3 > length)
                                 else  
3099                                  {                                  {
3100                                          error("this shouldn't be happening\n");                                          warning("Skipping short 0xff command:");
3101                                          exit(1);                                          for (j = 0; j < length; j++)
3102                                                    fprintf(stderr, "%02x ", text[j]);
3103                                            fprintf(stderr, "\n");
3104                                            i = length = 0;
3105                                            break;
3106                                  }                                  }
3107                                    cache_put_text(text[i + 1], text, text[i + 2]);
3108                                    i += 3;
3109                                    length -= i;
3110                                  /* this will move pointer from start to first character after FF command */                                  /* this will move pointer from start to first character after FF command */
3111                                  length -= i + 3;                                  text = &(text[i]);
                                 text = &(text[i + 3]);  
3112                                  i = 0;                                  i = 0;
3113                                  break;                                  break;
3114    
3115                          case 0xfe:                          case 0xfe:
3116                                    /* At least one byte needs to follow */
3117                                    if (i + 2 > length)
3118                                    {
3119                                            warning("Skipping short 0xfe command:");
3120                                            for (j = 0; j < length; j++)
3121                                                    fprintf(stderr, "%02x ", text[j]);
3122                                            fprintf(stderr, "\n");
3123                                            i = length = 0;
3124                                            break;
3125                                    }
3126                                  entry = cache_get_text(text[i + 1]);                                  entry = cache_get_text(text[i + 1]);
3127                                  if (entry != NULL)                                  if (entry->data != NULL)
3128                                  {                                  {
3129                                          if ((((uint8 *) (entry->data))[1] ==                                          if ((((uint8 *) (entry->data))[1] == 0)
3130                                               0) && (!(flags & TEXT2_IMPLICIT_X)))                                              && (!(flags & TEXT2_IMPLICIT_X)) && (i + 2 < length))
3131                                          {                                          {
3132                                                  if (flags & TEXT2_VERTICAL)                                                  if (flags & TEXT2_VERTICAL)
3133                                                          y += text[i + 2];                                                          y += text[i + 2];
# Line 1997  ui_draw_text(uint8 font, uint8 flags, in Line 3159  ui_draw_text(uint8 font, uint8 flags, in
3159          if (g_ownbackstore)          if (g_ownbackstore)
3160          {          {
3161                  if (boxcx > 1)                  if (boxcx > 1)
3162                    {
3163                          XCopyArea(g_display, g_backstore, g_wnd, g_gc, boxx,                          XCopyArea(g_display, g_backstore, g_wnd, g_gc, boxx,
3164                                    boxy, boxcx, boxcy, boxx, boxy);                                    boxy, boxcx, boxcy, boxx, boxy);
3165                            ON_ALL_SEAMLESS_WINDOWS(XCopyArea,
3166                                                    (g_display, g_backstore, sw->wnd, g_gc,
3167                                                     boxx, boxy,
3168                                                     boxcx, boxcy,
3169                                                     boxx - sw->xoffset, boxy - sw->yoffset));
3170                    }
3171                  else                  else
3172                    {
3173                          XCopyArea(g_display, g_backstore, g_wnd, g_gc, clipx,                          XCopyArea(g_display, g_backstore, g_wnd, g_gc, clipx,
3174                                    clipy, clipcx, clipcy, clipx, clipy);                                    clipy, clipcx, clipcy, clipx, clipy);
3175                            ON_ALL_SEAMLESS_WINDOWS(XCopyArea,
3176                                                    (g_display, g_backstore, sw->wnd, g_gc,
3177                                                     clipx, clipy,
3178                                                     clipcx, clipcy, clipx - sw->xoffset,
3179                                                     clipy - sw->yoffset));
3180                    }
3181          }          }
3182  }  }
3183    
# Line 2047  ui_desktop_restore(uint32 offset, int x, Line 3223  ui_desktop_restore(uint32 offset, int x,
3223          {          {
3224                  XPutImage(g_display, g_backstore, g_gc, image, 0, 0, x, y, cx, cy);                  XPutImage(g_display, g_backstore, g_gc, image, 0, 0, x, y, cx, cy);
3225                  XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);                  XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y);
3226                    ON_ALL_SEAMLESS_WINDOWS(XCopyArea,
3227                                            (g_display, g_backstore, sw->wnd, g_gc,
3228                                             x, y, cx, cy, x - sw->xoffset, y - sw->yoffset));
3229          }          }
3230          else          else
3231          {          {
3232                  XPutImage(g_display, g_wnd, g_gc, image, 0, 0, x, y, cx, cy);                  XPutImage(g_display, g_wnd, g_gc, image, 0, 0, x, y, cx, cy);
3233                    ON_ALL_SEAMLESS_WINDOWS(XCopyArea,
3234                                            (g_display, g_wnd, sw->wnd, g_gc, x, y, cx, cy,
3235                                             x - sw->xoffset, y - sw->yoffset));
3236          }          }
3237    
3238          XFree(image);          XFree(image);
3239  }  }
3240    
3241    /* these do nothing here but are used in uiports */
3242    void
3243    ui_begin_update(void)
3244    {
3245    }
3246    
3247    void
3248    ui_end_update(void)
3249    {
3250    }
3251    
3252    
3253    void
3254    ui_seamless_begin(BOOL hidden)
3255    {
3256            if (!g_seamless_rdp)
3257                    return;
3258    
3259            if (g_seamless_started)
3260                    return;
3261    
3262            g_seamless_started = True;
3263            g_seamless_hidden = hidden;
3264    
3265            if (!hidden)
3266                    ui_seamless_toggle();
3267    }
3268    
3269    
3270    void
3271    ui_seamless_hide_desktop()
3272    {
3273            if (!g_seamless_rdp)
3274                    return;
3275    
3276            if (!g_seamless_started)
3277                    return;
3278    
3279            if (g_seamless_active)
3280                    ui_seamless_toggle();
3281    
3282            g_seamless_hidden = True;
3283    }
3284    
3285    
3286    void
3287    ui_seamless_unhide_desktop()
3288    {
3289            if (!g_seamless_rdp)
3290                    return;
3291    
3292            if (!g_seamless_started)
3293                    return;
3294    
3295            g_seamless_hidden = False;
3296    
3297            ui_seamless_toggle();
3298    }
3299    
3300    
3301    void
3302    ui_seamless_toggle()
3303    {
3304            if (!g_seamless_rdp)
3305                    return;
3306    
3307            if (!g_seamless_started)
3308                    return;
3309    
3310            if (g_seamless_hidden)
3311                    return;
3312    
3313            if (g_seamless_active)
3314            {
3315                    /* Deactivate */
3316                    while (g_seamless_windows)
3317                    {
3318                            XDestroyWindow(g_display, g_seamless_windows->wnd);
3319                            sw_remove_window(g_seamless_windows);
3320                    }
3321                    XMapWindow(g_display, g_wnd);
3322            }
3323            else
3324            {
3325                    /* Activate */
3326                    XUnmapWindow(g_display, g_wnd);
3327                    seamless_send_sync();
3328            }
3329    
3330            g_seamless_active = !g_seamless_active;
3331    }
3332    
3333    
3334    void
3335    ui_seamless_create_window(unsigned long id, unsigned long group, unsigned long parent,
3336                              unsigned long flags)
3337    {
3338            Window wnd;
3339            XSetWindowAttributes attribs;
3340            XClassHint *classhints;
3341            XSizeHints *sizehints;
3342            XWMHints *wmhints;
3343            long input_mask;
3344            seamless_window *sw, *sw_parent;
3345    
3346            if (!g_seamless_active)
3347                    return;
3348    
3349            /* Ignore CREATEs for existing windows */
3350            sw = sw_get_window_by_id(id);
3351            if (sw)
3352                    return;
3353    
3354            get_window_attribs(&attribs);
3355            wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), -1, -1, 1, 1, 0, g_depth,
3356                                InputOutput, g_visual,
3357                                CWBackPixel | CWBackingStore | CWColormap | CWBorderPixel, &attribs);
3358    
3359            XStoreName(g_display, wnd, "SeamlessRDP");
3360            ewmh_set_wm_name(wnd, "SeamlessRDP");
3361    
3362            mwm_hide_decorations(wnd);
3363    
3364            classhints = XAllocClassHint();
3365            if (classhints != NULL)
3366            {
3367                    classhints->res_name = "rdesktop";
3368                    classhints->res_class = "SeamlessRDP";
3369                    XSetClassHint(g_display, wnd, classhints);
3370                    XFree(classhints);
3371            }
3372    
3373            /* WM_NORMAL_HINTS */
3374            sizehints = XAllocSizeHints();
3375            if (sizehints != NULL)
3376            {
3377                    sizehints->flags = USPosition;
3378                    XSetWMNormalHints(g_display, wnd, sizehints);
3379                    XFree(sizehints);
3380            }
3381    
3382            /* Parent-less transient windows */
3383            if (parent == 0xFFFFFFFF)
3384            {
3385                    XSetTransientForHint(g_display, wnd, RootWindowOfScreen(g_screen));
3386                    /* Some buggy wm:s (kwin) do not handle the above, so fake it
3387                       using some other hints. */
3388                    ewmh_set_window_popup(wnd);
3389            }
3390            /* Normal transient windows */
3391            else if (parent != 0x00000000)
3392            {
3393                    sw_parent = sw_get_window_by_id(parent);
3394                    if (sw_parent)
3395                            XSetTransientForHint(g_display, wnd, sw_parent->wnd);
3396                    else
3397                            warning("ui_seamless_create_window: No parent window 0x%lx\n", parent);
3398            }
3399    
3400            if (flags & SEAMLESSRDP_CREATE_MODAL)
3401            {
3402                    /* We do this to support buggy wm:s (*cough* metacity *cough*)
3403                       somewhat at least */
3404                    if (parent == 0x00000000)
3405                            XSetTransientForHint(g_display, wnd, RootWindowOfScreen(g_screen));
3406                    ewmh_set_window_modal(wnd);
3407            }
3408    
3409            /* FIXME: Support for Input Context:s */
3410    
3411            get_input_mask(&input_mask);
3412            input_mask |= PropertyChangeMask;
3413    
3414            XSelectInput(g_display, wnd, input_mask);
3415    
3416            /* handle the WM_DELETE_WINDOW protocol. FIXME: When killing a
3417               seamless window, we could try to close the window on the
3418               serverside, instead of terminating rdesktop */
3419            XSetWMProtocols(g_display, wnd, &g_kill_atom, 1);
3420    
3421            sw = xmalloc(sizeof(seamless_window));
3422            sw->wnd = wnd;
3423            sw->id = id;
3424            sw->behind = 0;
3425            sw->group = sw_find_group(group, False);
3426            sw->group->refcnt++;
3427            sw->xoffset = 0;
3428            sw->yoffset = 0;
3429            sw->width = 0;
3430            sw->height = 0;
3431            sw->state = SEAMLESSRDP_NOTYETMAPPED;
3432            sw->desktop = 0;
3433            sw->position_timer = xmalloc(sizeof(struct timeval));
3434            timerclear(sw->position_timer);
3435    
3436            sw->outstanding_position = False;
3437            sw->outpos_serial = 0;
3438            sw->outpos_xoffset = sw->outpos_yoffset = 0;
3439            sw->outpos_width = sw->outpos_height = 0;
3440    
3441            sw->next = g_seamless_windows;
3442            g_seamless_windows = sw;
3443    
3444            /* WM_HINTS */
3445            wmhints = XAllocWMHints();
3446            if (wmhints)
3447            {
3448                    wmhints->flags = WindowGroupHint;
3449                    wmhints->window_group = sw->group->wnd;
3450                    XSetWMHints(g_display, sw->wnd, wmhints);
3451                    XFree(wmhints);
3452            }
3453    }
3454    
3455    
3456    void
3457    ui_seamless_destroy_window(unsigned long id, unsigned long flags)
3458    {
3459            seamless_window *sw;
3460    
3461            if (!g_seamless_active)
3462                    return;
3463    
3464            sw = sw_get_window_by_id(id);
3465            if (!sw)
3466            {
3467                    warning("ui_seamless_destroy_window: No information for window 0x%lx\n", id);
3468                    return;
3469            }
3470    
3471            XDestroyWindow(g_display, sw->wnd);
3472            sw_remove_window(sw);
3473    }
3474    
3475    
3476    void
3477    ui_seamless_destroy_group(unsigned long id, unsigned long flags)
3478    {
3479            seamless_window *sw, *sw_next;
3480    
3481            if (!g_seamless_active)
3482                    return;
3483    
3484            for (sw = g_seamless_windows; sw; sw = sw_next)
3485            {
3486                    sw_next = sw->next;
3487    
3488                    if (sw->group->id == id)
3489                    {
3490                            XDestroyWindow(g_display, sw->wnd);
3491                            sw_remove_window(sw);
3492                    }
3493            }
3494    }
3495    
3496    
3497    void
3498    ui_seamless_move_window(unsigned long id, int x, int y, int width, int height, unsigned long flags)
3499    {
3500            seamless_window *sw;
3501    
3502            if (!g_seamless_active)
3503                    return;
3504    
3505            sw = sw_get_window_by_id(id);
3506            if (!sw)
3507            {
3508                    warning("ui_seamless_move_window: No information for window 0x%lx\n", id);
3509                    return;
3510            }
3511    
3512            /* We ignore server updates until it has handled our request. */
3513            if (sw->outstanding_position)
3514                    return;
3515    
3516            if (!width || !height)
3517                    /* X11 windows must be at least 1x1 */
3518                    return;
3519    
3520            sw->xoffset = x;
3521            sw->yoffset = y;
3522            sw->width = width;
3523            sw->height = height;
3524    
3525            /* If we move the window in a maximized state, then KDE won't
3526               accept restoration */
3527            switch (sw->state)
3528            {
3529                    case SEAMLESSRDP_MINIMIZED:
3530                    case SEAMLESSRDP_MAXIMIZED:
3531                            return;
3532            }
3533    
3534            /* FIXME: Perhaps use ewmh_net_moveresize_window instead */
3535            XMoveResizeWindow(g_display, sw->wnd, sw->xoffset, sw->yoffset, sw->width, sw->height);
3536    }
3537    
3538    
3539    void
3540    ui_seamless_restack_window(unsigned long id, unsigned long behind, unsigned long flags)
3541    {
3542            seamless_window *sw;
3543    
3544            if (!g_seamless_active)
3545                    return;
3546    
3547            sw = sw_get_window_by_id(id);
3548            if (!sw)
3549            {
3550                    warning("ui_seamless_restack_window: No information for window 0x%lx\n", id);
3551                    return;
3552            }
3553    
3554            if (behind)
3555            {
3556                    seamless_window *sw_behind;
3557                    Window wnds[2];
3558    
3559                    sw_behind = sw_get_window_by_id(behind);
3560                    if (!sw_behind)
3561                    {
3562                            warning("ui_seamless_restack_window: No information for window 0x%lx\n",
3563                                    behind);
3564                            return;
3565                    }
3566    
3567                    wnds[1] = sw_behind->wnd;
3568                    wnds[0] = sw->wnd;
3569    
3570                    XRestackWindows(g_display, wnds, 2);
3571            }
3572            else
3573            {
3574                    XRaiseWindow(g_display, sw->wnd);
3575            }
3576    
3577            sw_restack_window(sw, behind);
3578    }
3579    
3580    
3581    void
3582    ui_seamless_settitle(unsigned long id, const char *title, unsigned long flags)
3583    {
3584            seamless_window *sw;
3585    
3586            if (!g_seamless_active)
3587                    return;
3588    
3589            sw = sw_get_window_by_id(id);
3590            if (!sw)
3591            {
3592                    warning("ui_seamless_settitle: No information for window 0x%lx\n", id);
3593                    return;
3594            }
3595    
3596            /* FIXME: Might want to convert the name for non-EWMH WMs */
3597            XStoreName(g_display, sw->wnd, title);
3598            ewmh_set_wm_name(sw->wnd, title);
3599    }
3600    
3601    
3602    void
3603    ui_seamless_setstate(unsigned long id, unsigned int state, unsigned long flags)
3604    {
3605            seamless_window *sw;
3606    
3607            if (!g_seamless_active)
3608                    return;
3609    
3610            sw = sw_get_window_by_id(id);
3611            if (!sw)
3612            {
3613                    warning("ui_seamless_setstate: No information for window 0x%lx\n", id);
3614                    return;
3615            }
3616    
3617            switch (state)
3618            {
3619                    case SEAMLESSRDP_NORMAL:
3620                    case SEAMLESSRDP_MAXIMIZED:
3621                            ewmh_change_state(sw->wnd, state);
3622                            XMapWindow(g_display, sw->wnd);
3623                            break;
3624                    case SEAMLESSRDP_MINIMIZED:
3625                            /* EWMH says: "if an Application asks to toggle _NET_WM_STATE_HIDDEN
3626                               the Window Manager should probably just ignore the request, since
3627                               _NET_WM_STATE_HIDDEN is a function of some other aspect of the window
3628                               such as minimization, rather than an independent state." Besides,
3629                               XIconifyWindow is easier. */
3630                            if (sw->state == SEAMLESSRDP_NOTYETMAPPED)
3631                            {
3632                                    XWMHints *hints;
3633                                    hints = XGetWMHints(g_display, sw->wnd);
3634                                    if (hints)
3635                                    {
3636                                            hints->flags |= StateHint;
3637                                            hints->initial_state = IconicState;
3638                                            XSetWMHints(g_display, sw->wnd, hints);
3639                                            XFree(hints);
3640                                    }
3641                                    XMapWindow(g_display, sw->wnd);
3642                            }
3643                            else
3644                                    XIconifyWindow(g_display, sw->wnd, DefaultScreen(g_display));
3645                            break;
3646                    default:
3647                            warning("SeamlessRDP: Invalid state %d\n", state);
3648                            break;
3649            }
3650    
3651            sw->state = state;
3652    }
3653    
3654    
3655    void
3656    ui_seamless_syncbegin(unsigned long flags)
3657    {
3658            if (!g_seamless_active)
3659                    return;
3660    
3661            /* Destroy all seamless windows */
3662            while (g_seamless_windows)
3663            {
3664                    XDestroyWindow(g_display, g_seamless_windows->wnd);
3665                    sw_remove_window(g_seamless_windows);
3666            }
3667    }
3668    
3669    
3670    void
3671    ui_seamless_ack(unsigned int serial)
3672    {
3673            seamless_window *sw;
3674            for (sw = g_seamless_windows; sw; sw = sw->next)
3675            {
3676                    if (sw->outstanding_position && (sw->outpos_serial == serial))
3677                    {
3678                            sw->xoffset = sw->outpos_xoffset;
3679                            sw->yoffset = sw->outpos_yoffset;
3680                            sw->width = sw->outpos_width;
3681                            sw->height = sw->outpos_height;
3682                            sw->outstanding_position = False;
3683    
3684                            /* Do a complete redraw of the window as part of the
3685                               completion of the move. This is to remove any
3686                               artifacts caused by our lack of synchronization. */
3687                            XCopyArea(g_display, g_backstore,
3688                                      sw->wnd, g_gc,
3689                                      sw->xoffset, sw->yoffset, sw->width, sw->height, 0, 0);
3690    
3691                            break;
3692                    }
3693            }
3694    }

Legend:
Removed from v.620  
changed lines
  Added in v.1232

  ViewVC Help
Powered by ViewVC 1.1.26