/[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 28 by matty, Wed Jun 20 13:54:48 2001 UTC revision 318 by astrand, Mon Feb 10 12:58:51 2003 UTC
# Line 1  Line 1 
1  /*  /*
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     User interface services - X-Windows     User interface services - X Window System
4     Copyright (C) Matthew Chapman 1999-2000     Copyright (C) Matthew Chapman 1999-2002
5      
6     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.     (at your option) any later version.
10      
11     This program is distributed in the hope that it will be useful,     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.     GNU General Public License for more details.
15      
16     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# Line 21  Line 21 
21  #include <X11/Xlib.h>  #include <X11/Xlib.h>
22  #include <X11/Xutil.h>  #include <X11/Xutil.h>
23  #include <time.h>  #include <time.h>
24    #include <errno.h>
25  #include "rdesktop.h"  #include "rdesktop.h"
26    
27  extern int width;  extern int width;
28  extern int height;  extern int height;
29  extern BOOL motion;  extern BOOL sendmotion;
 extern BOOL grab_keyboard;  
30  extern BOOL fullscreen;  extern BOOL fullscreen;
31  extern int private_colormap;  extern BOOL grab_keyboard;
32    extern BOOL hide_decorations;
33  static int bpp;  extern char title[];
34  static int depth;  extern int server_bpp;
35  static Display *display;  BOOL enable_compose = False;
36    BOOL focused;
37    BOOL mouse_in_wnd;
38    
39    Display *display;
40    static int x_socket;
41    static Screen *screen;
42  static Window wnd;  static Window wnd;
43  static GC gc;  static GC gc;
44  static Visual *visual;  static Visual *visual;
45  static uint32 *colmap;  static int depth;
46    static int bpp;
47    static XIM IM;
48    static XIC IC;
49    static XModifierKeymap *mod_map;
50    static Cursor current_cursor;
51    static Atom protocol_atom, kill_atom;
52    
53    /* endianness */
54    static BOOL host_be;
55    static BOOL xserver_be;
56    
57  #define Ctrans(col) ( private_colormap ? col : colmap[col])  /* software backing store */
58    static BOOL ownbackstore;
59    static Pixmap backstore;
60    
61  #define L_ENDIAN  /* MWM decorations */
62  int screen_msbfirst = 0;  #define MWM_HINTS_DECORATIONS   (1L << 1)
63    #define PROP_MOTIF_WM_HINTS_ELEMENTS    5
64    typedef struct
65    {
66            uint32 flags;
67            uint32 functions;
68            uint32 decorations;
69            sint32 inputMode;
70            uint32 status;
71    }
72    PropMotifWmHints;
73    
74  static uint8 *translate(int width, int height, uint8 *data);  typedef struct
75    {
76            uint32 red;
77            uint32 green;
78            uint32 blue;
79    }
80    PixelColour;
81    
82    #define FILL_RECTANGLE(x,y,cx,cy)\
83    { \
84            XFillRectangle(display, wnd, gc, x, y, cx, cy); \
85            if (ownbackstore) \
86                    XFillRectangle(display, backstore, gc, x, y, cx, cy); \
87    }
88    
89    #define FILL_RECTANGLE_BACKSTORE(x,y,cx,cy)\
90    { \
91            XFillRectangle(display, ownbackstore ? backstore : wnd, gc, x, y, cx, cy); \
92    }
93    
94    /* colour maps */
95    BOOL owncolmap = False;
96    static Colormap xcolmap;
97    static uint32 *colmap;
98    
99    #define TRANSLATE(col)          ( server_bpp != 8 ? translate_colour(col) : owncolmap ? col : translate_colour(colmap[col]) )
100    #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));
101    #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));
102    
103  static int rop2_map[] = {  static int rop2_map[] = {
104          GXclear,                /* 0 */          GXclear,                /* 0 */
# Line 64  static int rop2_map[] = { Line 119  static int rop2_map[] = {
119          GXset                   /* 1 */          GXset                   /* 1 */
120  };  };
121    
122    #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(display, gc, rop2_map[rop2]); }
123    #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }
124    
125    void
126    mwm_hide_decorations(void)
127    {
128            PropMotifWmHints motif_hints;
129            Atom hintsatom;
130    
131            /* setup the property */
132            motif_hints.flags = MWM_HINTS_DECORATIONS;
133            motif_hints.decorations = 0;
134    
135            /* get the atom for the property */
136            hintsatom = XInternAtom(display, "_MOTIF_WM_HINTS", False);
137            if (!hintsatom)
138            {
139                    warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n");
140                    return;
141            }
142    
143            XChangeProperty(display, wnd, hintsatom, hintsatom, 32, PropModeReplace,
144                            (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
145    }
146    
147    PixelColour
148    split_colour15(uint32 colour)
149    {
150            PixelColour rv;
151            rv.red = (colour & 0x7c00) >> 10;
152            rv.red = (rv.red * 0xff) / 0x1f;
153            rv.green = (colour & 0x03e0) >> 5;
154            rv.green = (rv.green * 0xff) / 0x1f;
155            rv.blue = (colour & 0x1f);
156            rv.blue = (rv.blue * 0xff) / 0x1f;
157            return rv;
158    }
159    
160    PixelColour
161    split_colour16(uint32 colour)
162    {
163            PixelColour rv;
164            rv.red = (colour & 0xf800) >> 11;
165            rv.red = (rv.red * 0xff) / 0x1f;
166            rv.green = (colour & 0x07e0) >> 5;
167            rv.green = (rv.green * 0xff) / 0x3f;
168            rv.blue = (colour & 0x001f);
169            rv.blue = (rv.blue * 0xff) / 0x1f;
170            return rv;
171    }
172    
173    PixelColour
174    split_colour24(uint32 colour)
175    {
176            PixelColour rv;
177            rv.blue = (colour & 0xff0000) >> 16;
178            rv.green = (colour & 0xff00) >> 8;
179            rv.red = (colour & 0xff);
180            return rv;
181    }
182    
183    uint32
184    make_colour16(PixelColour pc)
185    {
186            pc.red = (pc.red * 0x1f) / 0xff;
187            pc.green = (pc.green * 0x3f) / 0xff;
188            pc.blue = (pc.blue * 0x1f) / 0xff;
189            return (pc.red << 11) | (pc.green << 5) | pc.blue;
190    }
191    
192    uint32
193    make_colour24(PixelColour pc)
194    {
195            return (pc.red << 16) | (pc.green << 8) | pc.blue;
196    }
197    
198    uint32
199    make_colour32(PixelColour pc)
200    {
201            return (pc.red << 16) | (pc.green << 8) | pc.blue;
202    }
203    
204    #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
205    #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }
206    #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
207                            x = (x << 16) | (x >> 16); }
208    
209    static uint32
210    translate_colour(uint32 colour)
211    {
212            switch (server_bpp)
213            {
214                    case 15:
215                            switch (bpp)
216                            {
217                                    case 16:
218                                            colour = make_colour16(split_colour15(colour));
219                                            break;
220                                    case 24:
221                                            colour = make_colour24(split_colour15(colour));
222                                            break;
223                                    case 32:
224                                            colour = make_colour32(split_colour15(colour));
225                                            break;
226                            }
227                            break;
228                    case 16:
229                            switch (bpp)
230                            {
231                                    case 16:
232                                            break;
233                                    case 24:
234                                            colour = make_colour24(split_colour16(colour));
235                                            break;
236                                    case 32:
237                                            colour = make_colour32(split_colour16(colour));
238                                            break;
239                            }
240                            break;
241                    case 24:
242                            switch (bpp)
243                            {
244                                    case 16:
245                                            colour = make_colour16(split_colour24(colour));
246                                            break;
247                                    case 24:
248                                            break;
249                                    case 32:
250                                            colour = make_colour32(split_colour24(colour));
251                                            break;
252                            }
253                            break;
254            }
255            switch (bpp)
256            {
257                    case 16:
258                            if (host_be != xserver_be)
259                                    BSWAP16(colour);
260                            break;
261    
262                    case 24:
263                            if (xserver_be)
264                                    BSWAP24(colour);
265                            break;
266    
267                    case 32:
268                            if (host_be != xserver_be)
269                                    BSWAP32(colour);
270                            break;
271            }
272    
273            return colour;
274    }
275    
276  static void  static void
277  xwin_set_function(uint8 rop2)  translate8to8(uint8 * data, uint8 * out, uint8 * end)
278  {  {
279          static uint8 last_rop2 = ROP2_COPY;          while (out < end)
280                    *(out++) = (uint8) colmap[*(data++)];
281    }
282    
283          if (last_rop2 != rop2)  static void
284    translate8to16(uint8 * data, uint16 * out, uint16 * end)
285    {
286            while (out < end)
287                    *(out++) = (uint16) colmap[*(data++)];
288    }
289    
290    /* little endian - conversion happens when colourmap is built */
291    static void
292    translate8to24(uint8 * data, uint8 * out, uint8 * end)
293    {
294            uint32 value;
295    
296            while (out < end)
297          {          {
298                  XSetFunction(display, gc, rop2_map[rop2]);                  value = colmap[*(data++)];
299                  last_rop2 = rop2;                  *(out++) = value;
300                    *(out++) = value >> 8;
301                    *(out++) = value >> 16;
302          }          }
303  }  }
304    
305  static void  static void
306  xwin_grab_keyboard()  translate8to32(uint8 * data, uint32 * out, uint32 * end)
307  {  {
308          XGrabKeyboard(display, wnd, True, GrabModeAsync, GrabModeAsync,          while (out < end)
309                        CurrentTime);                  *(out++) = colmap[*(data++)];
310    }
311    
312    /* todo the remaining translate function might need some big endian check ?? */
313    
314    static void
315    translate15to16(uint16 * data, uint16 * out, uint16 * end)
316    {
317            while (out < end)
318                    *(out++) = (uint16) make_colour16(split_colour15(*(data++)));
319    }
320    
321    static void
322    translate15to24(uint16 * data, uint8 * out, uint8 * end)
323    {
324            uint32 value;
325    
326            while (out < end)
327            {
328                    value = make_colour24(split_colour15(*(data++)));
329                    *(out++) = value;
330                    *(out++) = value >> 8;
331                    *(out++) = value >> 16;
332            }
333  }  }
334    
335  static void  static void
336  xwin_ungrab_keyboard()  translate15to32(uint16 * data, uint32 * out, uint32 * end)
337    {
338            while (out < end)
339                    *(out++) = make_colour32(split_colour15(*(data++)));
340    }
341    
342    static void
343    translate16to16(uint16 * data, uint16 * out, uint16 * end)
344    {
345            while (out < end)
346                    *(out++) = (uint16) (*(data++));
347    }
348    
349    
350    static void
351    translate16to24(uint16 * data, uint8 * out, uint8 * end)
352    {
353            uint32 value;
354    
355            while (out < end)
356            {
357                    value = make_colour24(split_colour16(*(data++)));
358                    *(out++) = value;
359                    *(out++) = value >> 8;
360                    *(out++) = value >> 16;
361            }
362    }
363    
364    static void
365    translate16to32(uint16 * data, uint32 * out, uint32 * end)
366    {
367            while (out < end)
368                    *(out++) = make_colour32(split_colour16(*(data++)));
369    }
370    
371    static void
372    translate24to16(uint8 * data, uint16 * out, uint16 * end)
373    {
374            uint32 pixel = 0;
375            while (out < end)
376            {
377                    pixel = *(data++) << 16;
378                    pixel |= *(data++) << 8;
379                    pixel |= *(data++);
380                    *(out++) = (uint16) make_colour16(split_colour24(pixel));
381            }
382    }
383    
384    static void
385    translate24to24(uint8 * data, uint8 * out, uint8 * end)
386    {
387            while (out < end)
388            {
389                    *(out++) = (*(data++));
390            }
391    }
392    
393    static void
394    translate24to32(uint8 * data, uint32 * out, uint32 * end)
395    {
396            uint32 pixel = 0;
397            while (out < end)
398            {
399                    memcpy(&pixel, data, 3);
400                    data += 3;
401                    *(out++) = pixel;
402            }
403    }
404    
405    static uint8 *
406    translate_image(int width, int height, uint8 * data)
407  {  {
408          XUngrabKeyboard(display, CurrentTime);          int size = width * height * bpp / 8;
409            uint8 *out = xmalloc(size);
410            uint8 *end = out + size;
411    
412            switch (server_bpp)
413            {
414                    case 24:
415                            switch (bpp)
416                            {
417                                    case 32:
418                                            translate24to32(data, (uint32 *) out, (uint32 *) end);
419                                            break;
420                                    case 24:
421                                            translate24to24(data, out, end);
422                                            break;
423                                    case 16:
424                                            translate24to16(data, (uint16 *) out, (uint16 *) end);
425                                            break;
426                            }
427                            break;
428                    case 16:
429                            switch (bpp)
430                            {
431                                    case 32:
432                                            translate16to32((uint16 *) data, (uint32 *) out,
433                                                            (uint32 *) end);
434                                            break;
435                                    case 24:
436                                            translate16to24((uint16 *) data, out, end);
437                                            break;
438                                    case 16:
439                                            translate16to16((uint16 *) data, (uint16 *) out,
440                                                            (uint16 *) end);
441                                            break;
442                            }
443                            break;
444                    case 15:
445                            switch (bpp)
446                            {
447                                    case 32:
448                                            translate15to32((uint16 *) data, (uint32 *) out,
449                                                            (uint32 *) end);
450                                            break;
451                                    case 24:
452                                            translate15to24((uint16 *) data, out, end);
453                                            break;
454                                    case 16:
455                                            translate15to16((uint16 *) data, (uint16 *) out,
456                                                            (uint16 *) end);
457                                            break;
458                            }
459                            break;
460                    case 8:
461                            switch (bpp)
462                            {
463                                    case 8:
464                                            translate8to8(data, out, end);
465                                            break;
466                                    case 16:
467                                            translate8to16(data, (uint16 *) out, (uint16 *) end);
468                                            break;
469                                    case 24:
470                                            translate8to24(data, out, end);
471                                            break;
472                                    case 32:
473                                            translate8to32(data, (uint32 *) out, (uint32 *) end);
474                                            break;
475                            }
476                            break;
477            }
478            return out;
479  }  }
480    
481  BOOL  BOOL
482  ui_create_window(char *title)  get_key_state(unsigned int state, uint32 keysym)
483    {
484            int modifierpos, key, keysymMask = 0;
485            int offset;
486    
487            KeyCode keycode = XKeysymToKeycode(display, keysym);
488    
489            if (keycode == NoSymbol)
490                    return False;
491    
492            for (modifierpos = 0; modifierpos < 8; modifierpos++)
493            {
494                    offset = mod_map->max_keypermod * modifierpos;
495    
496                    for (key = 0; key < mod_map->max_keypermod; key++)
497                    {
498                            if (mod_map->modifiermap[offset + key] == keycode)
499                                    keysymMask |= 1 << modifierpos;
500                    }
501            }
502    
503            return (state & keysymMask) ? True : False;
504    }
505    
506    BOOL
507    ui_init(void)
508  {  {
         XSetWindowAttributes attribs;  
         XClassHint *classhints;  
         XSizeHints *sizehints;  
         unsigned long input_mask;  
509          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
510          int count;          uint16 test;
511            int i;
512    
513          display = XOpenDisplay(NULL);          display = XOpenDisplay(NULL);
514          if (display == NULL)          if (display == NULL)
515          {          {
516                  ERROR("Failed to open display\n");                  error("Failed to open display: %s\n", XDisplayName(NULL));
517                  return False;                  return False;
518          }          }
519    
520          visual = DefaultVisual(display, DefaultScreen(display));          x_socket = ConnectionNumber(display);
521          depth = DefaultDepth(display, DefaultScreen(display));          screen = DefaultScreenOfDisplay(display);
522          pfm = XListPixmapFormats(display, &count);          visual = DefaultVisualOfScreen(screen);
523            depth = DefaultDepthOfScreen(screen);
524    
525            pfm = XListPixmapFormats(display, &i);
526          if (pfm != NULL)          if (pfm != NULL)
527          {          {
528                  while (count--)                  /* Use maximum bpp for this depth - this is generally
529                       desirable, e.g. 24 bits->32 bits. */
530                    while (i--)
531                  {                  {
532                          if ((pfm + count)->depth == depth                          if ((pfm[i].depth == depth) && (pfm[i].bits_per_pixel > bpp))
                             && (pfm + count)->bits_per_pixel > bpp)  
533                          {                          {
534                                  bpp = (pfm + count)->bits_per_pixel;                                  bpp = pfm[i].bits_per_pixel;
535                          }                          }
536                  }                  }
537                  XFree(pfm);                  XFree(pfm);
# Line 124  ui_create_window(char *title) Line 539  ui_create_window(char *title)
539    
540          if (bpp < 8)          if (bpp < 8)
541          {          {
542                  ERROR("Less than 8 bpp not currently supported.\n");                  error("Less than 8 bpp not currently supported.\n");
543                  XCloseDisplay(display);                  XCloseDisplay(display);
544                  return False;                  return False;
545          }          }
546    
547          width &= ~3; /* make width nicely divisible */          if (owncolmap != True)
548            {
549                    xcolmap = DefaultColormapOfScreen(screen);
550                    if (depth <= 8)
551                            warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n");
552            }
553    
554            gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL);
555    
556            if (DoesBackingStore(screen) != Always)
557                    ownbackstore = True;
558    
559          attribs.background_pixel = BlackPixel(display, DefaultScreen(display));          test = 1;
560          attribs.backing_store = Always;          host_be = !(BOOL) (*(uint8 *) (&test));
561            xserver_be = (ImageByteOrder(display) == MSBFirst);
562    
563            if ((width == 0) || (height == 0))
564            {
565                    /* Fetch geometry from _NET_WORKAREA */
566                    uint32 x, y, cx, cy;
567    
568                    if (get_current_workarea(&x, &y, &cx, &cy) == 0)
569                    {
570                            width = cx;
571                            height = cy;
572                    }
573                    else
574                    {
575                            warning("Failed to get workarea: probably your window manager does not support extended hints\n");
576                            width = 800;
577                            height = 600;
578                    }
579            }
580    
581          if (fullscreen)          if (fullscreen)
582          {          {
583                  attribs.override_redirect = True;                  width = WidthOfScreen(screen);
584                  width = WidthOfScreen(DefaultScreenOfDisplay(display));                  height = HeightOfScreen(screen);
                 height = HeightOfScreen(DefaultScreenOfDisplay(display));  
                 XSetInputFocus(display, PointerRoot, RevertToPointerRoot,  
                                CurrentTime);  
585          }          }
586          else  
587            /* make sure width is a multiple of 4 */
588            width = (width + 3) & ~3;
589    
590            if (ownbackstore)
591          {          {
592                  attribs.override_redirect = False;                  backstore =
593                            XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth);
594    
595                    /* clear to prevent rubbish being exposed at startup */
596                    XSetForeground(display, gc, BlackPixelOfScreen(screen));
597                    XFillRectangle(display, backstore, gc, 0, 0, width, height);
598          }          }
599    
600          wnd = XCreateWindow(display, DefaultRootWindow(display),          mod_map = XGetModifierMapping(display);
601                              0, 0, width, height, 0, CopyFromParent,  
602                              InputOutput, CopyFromParent,          if (enable_compose)
603                              CWBackingStore | CWBackPixel | CWOverrideRedirect,                  IM = XOpenIM(display, NULL, NULL, NULL);
604                              &attribs);  
605            xkeymap_init();
606    
607            /* todo take this out when high colour is done */
608            printf("server bpp %d client bpp %d depth %d\n", server_bpp, bpp, depth);
609    
610            return True;
611    }
612    
613    void
614    ui_deinit(void)
615    {
616            if (IM != NULL)
617                    XCloseIM(IM);
618    
619            XFreeModifiermap(mod_map);
620    
621            if (ownbackstore)
622                    XFreePixmap(display, backstore);
623    
624            XFreeGC(display, gc);
625            XCloseDisplay(display);
626            display = NULL;
627    }
628    
629    BOOL
630    ui_create_window(void)
631    {
632            XSetWindowAttributes attribs;
633            XClassHint *classhints;
634            XSizeHints *sizehints;
635            int wndwidth, wndheight;
636            long input_mask, ic_input_mask;
637            XEvent xevent;
638    
639            wndwidth = fullscreen ? WidthOfScreen(screen) : width;
640            wndheight = fullscreen ? HeightOfScreen(screen) : height;
641    
642            attribs.background_pixel = BlackPixelOfScreen(screen);
643            attribs.backing_store = ownbackstore ? NotUseful : Always;
644            attribs.override_redirect = fullscreen;
645    
646            wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight,
647                                0, CopyFromParent, InputOutput, CopyFromParent,
648                                CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
649    
650          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
651    
652            if (hide_decorations)
653                    mwm_hide_decorations();
654    
655          classhints = XAllocClassHint();          classhints = XAllocClassHint();
656          if (classhints != NULL)          if (classhints != NULL)
   
657          {          {
658                  classhints->res_name = "rdesktop";                  classhints->res_name = classhints->res_class = "rdesktop";
                 classhints->res_class = "rdesktop";  
659                  XSetClassHint(display, wnd, classhints);                  XSetClassHint(display, wnd, classhints);
660                  XFree(classhints);                  XFree(classhints);
661          }          }
# Line 168  ui_create_window(char *title) Line 663  ui_create_window(char *title)
663          sizehints = XAllocSizeHints();          sizehints = XAllocSizeHints();
664          if (sizehints)          if (sizehints)
665          {          {
666                  sizehints->flags =                  sizehints->flags = PMinSize | PMaxSize;
667                          PPosition | PSize | PMinSize | PMaxSize | PBaseSize;                  sizehints->min_width = sizehints->max_width = width;
668                  sizehints->min_width = width;                  sizehints->min_height = sizehints->max_height = height;
                 sizehints->max_width = width;  
                 sizehints->min_height = height;  
                 sizehints->max_height = height;  
                 sizehints->base_width = width;  
                 sizehints->base_height = height;  
669                  XSetWMNormalHints(display, wnd, sizehints);                  XSetWMNormalHints(display, wnd, sizehints);
670                  XFree(sizehints);                  XFree(sizehints);
671          }          }
672    
673          input_mask = KeyPressMask | KeyReleaseMask;          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
674          input_mask |= ButtonPressMask | ButtonReleaseMask;                  VisibilityChangeMask | FocusChangeMask;
675          if (motion)  
676            if (sendmotion)
677                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
678            if (ownbackstore)
679                    input_mask |= ExposureMask;
680            if (fullscreen || grab_keyboard)
681                    input_mask |= EnterWindowMask;
682          if (grab_keyboard)          if (grab_keyboard)
683                  input_mask |= EnterWindowMask | LeaveWindowMask;                  input_mask |= LeaveWindowMask;
684    
685          XSelectInput(display, wnd, input_mask);          if (IM != NULL)
686          gc = XCreateGC(display, wnd, 0, NULL);          {
687                    IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
688                                   XNClientWindow, wnd, XNFocusWindow, wnd, NULL);
689    
690                    if ((IC != NULL)
691                        && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))
692                            input_mask |= ic_input_mask;
693            }
694    
695            XSelectInput(display, wnd, input_mask);
696          XMapWindow(display, wnd);          XMapWindow(display, wnd);
697    
698            /* wait for VisibilityNotify */
699            do
700            {
701                    XMaskEvent(display, VisibilityChangeMask, &xevent);
702            }
703            while (xevent.type != VisibilityNotify);
704    
705            focused = False;
706            mouse_in_wnd = False;
707    
708            /* handle the WM_DELETE_WINDOW protocol */
709            protocol_atom = XInternAtom(display, "WM_PROTOCOLS", True);
710            kill_atom = XInternAtom(display, "WM_DELETE_WINDOW", True);
711            XSetWMProtocols(display, wnd, &kill_atom, 1);
712    
713          return True;          return True;
714  }  }
715    
716  void  void
717  ui_destroy_window()  ui_destroy_window(void)
718  {  {
719          XFreeGC(display, gc);          if (IC != NULL)
720                    XDestroyIC(IC);
721    
722          XDestroyWindow(display, wnd);          XDestroyWindow(display, wnd);
         XCloseDisplay(display);  
         display = NULL;  
723  }  }
724    
725  static uint8  void
726  xwin_translate_key(unsigned long key)  xwin_toggle_fullscreen(void)
727  {  {
728          DEBUG("KEY(code=0x%lx)\n", key);          Pixmap contents = 0;
729    
730          if ((key > 8) && (key <= 0x60))          if (!ownbackstore)
731                  return (key - 8);          {
732                    /* need to save contents of window */
733                    contents = XCreatePixmap(display, wnd, width, height, depth);
734                    XCopyArea(display, wnd, contents, gc, 0, 0, width, height, 0, 0);
735            }
736    
737          switch (key)          ui_destroy_window();
738          {          fullscreen = !fullscreen;
739                  case 0x61:      /* home */          ui_create_window();
                         return 0x47 | 0x80;  
                 case 0x62:      /* up arrow */  
                         return 0x48 | 0x80;  
                 case 0x63:      /* page up */  
                         return 0x49 | 0x80;  
                 case 0x64:      /* left arrow */  
                         return 0x4b | 0x80;  
                 case 0x66:      /* right arrow */  
                         return 0x4d | 0x80;  
                 case 0x67:      /* end */  
                         return 0x4f | 0x80;  
                 case 0x68:      /* down arrow */  
                         return 0x50 | 0x80;  
                 case 0x69:      /* page down */  
                         return 0x51 | 0x80;  
                 case 0x6a:      /* insert */  
                         return 0x52 | 0x80;  
                 case 0x6b:      /* delete */  
                         return 0x53 | 0x80;  
                 case 0x6c:      /* keypad enter */  
                         return 0x1c | 0x80;  
                 case 0x6d:      /* right ctrl */  
                         return 0x1d | 0x80;  
                 case 0x6f:      /* ctrl - print screen */  
                         return 0x37 | 0x80;  
                 case 0x70:      /* keypad '/' */  
                         return 0x35 | 0x80;  
                 case 0x71:      /* right alt */  
                         return 0x38 | 0x80;  
                 case 0x72:      /* ctrl break */  
                         return 0x46 | 0x80;  
                 case 0x73:      /* left window key */  
                         return 0xff;    /* real scancode is 5b */  
                 case 0x74:      /* right window key */  
                         return 0xff;    /* real scancode is 5c */  
                 case 0x75:      /* menu key */  
                         return 0x5d | 0x80;  
         }  
   
         return 0;  
 }  
   
 static uint16  
 xwin_translate_mouse(unsigned long button)  
 {  
         switch (button)  
         {  
                 case Button1:   /* left */  
                         return MOUSE_FLAG_BUTTON1;  
                 case Button2:   /* middle */  
                         return MOUSE_FLAG_BUTTON3;  
                 case Button3:   /* right */  
                         return MOUSE_FLAG_BUTTON2;  
         }  
   
         return 0;  
 }  
   
 void  
 ui_process_events()  
 {  
         XEvent event;  
         uint8 scancode;  
         uint16 button;  
         uint32 ev_time;  
740    
741          if (display == NULL)          XDefineCursor(display, wnd, current_cursor);
                 return;  
742    
743          while (XCheckWindowEvent(display, wnd, ~0, &event))          if (!ownbackstore)
744          {          {
745                  ev_time = time(NULL);                  XCopyArea(display, contents, wnd, gc, 0, 0, width, height, 0, 0);
746                    XFreePixmap(display, contents);
747            }
748    }
749    
750    /* Process all events in Xlib queue
751       Returns 0 after user quit, 1 otherwise */
752    static int
753    xwin_process_events(void)
754    {
755            XEvent xevent;
756            KeySym keysym;
757            uint16 button, flags;
758            uint32 ev_time;
759            key_translation tr;
760            char str[256];
761            Status status;
762            unsigned int state;
763            Window wdummy;
764            int dummy;
765    
766            while (XPending(display) > 0)
767            {
768                    XNextEvent(display, &xevent);
769    
770                    if ((IC != NULL) && (XFilterEvent(&xevent, None) == True))
771                    {
772                            DEBUG_KBD(("Filtering event\n"));
773                            continue;
774                    }
775    
776                    flags = 0;
777    
778                  switch (event.type)                  switch (xevent.type)
779                  {                  {
780                            case ClientMessage:
781                                    /* the window manager told us to quit */
782                                    if ((xevent.xclient.message_type == protocol_atom)
783                                        && (xevent.xclient.data.l[0] == kill_atom))
784                                            /* Quit */
785                                            return 0;
786                                    break;
787    
788                          case KeyPress:                          case KeyPress:
789                                  scancode = xwin_translate_key(event.xkey.keycode);                                  if (IC != NULL)
790                                  if (scancode == 0)                                          /* Multi_key compatible version */
791                                    {
792                                            XmbLookupString(IC,
793                                                            (XKeyPressedEvent *) &
794                                                            xevent, str, sizeof(str), &keysym, &status);
795                                            if (!((status == XLookupKeySym) || (status == XLookupBoth)))
796                                            {
797                                                    error("XmbLookupString failed with status 0x%x\n",
798                                                          status);
799                                                    break;
800                                            }
801                                    }
802                                    else
803                                    {
804                                            /* Plain old XLookupString */
805                                            DEBUG_KBD(("\nNo input context, using XLookupString\n"));
806                                            XLookupString((XKeyEvent *) & xevent,
807                                                          str, sizeof(str), &keysym, NULL);
808                                    }
809    
810                                    DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym,
811                                               get_ksname(keysym)));
812    
813                                    ev_time = time(NULL);
814                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))
815                                            break;
816    
817                                    tr = xkeymap_translate_key(keysym,
818                                                               xevent.xkey.keycode, xevent.xkey.state);
819    
820                                    if (tr.scancode == 0)
821                                          break;                                          break;
822    
823                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0,                                  ensure_remote_modifiers(ev_time, tr);
824                                                 scancode, 0);  
825                                    rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
826                                  break;                                  break;
827    
828                          case KeyRelease:                          case KeyRelease:
829                                  scancode = xwin_translate_key(event.xkey.keycode);                                  XLookupString((XKeyEvent *) & xevent, str,
830                                  if (scancode == 0)                                                sizeof(str), &keysym, NULL);
831    
832                                    DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
833                                               get_ksname(keysym)));
834    
835                                    ev_time = time(NULL);
836                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False))
837                                          break;                                          break;
838    
839                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE,                                  tr = xkeymap_translate_key(keysym,
840                                                 KBD_FLAG_DOWN | KBD_FLAG_UP,                                                             xevent.xkey.keycode, xevent.xkey.state);
                                                scancode, 0);  
                                 break;  
841    
842                          case ButtonPress:                                  if (tr.scancode == 0)
                                 button = xwin_translate_mouse(event.xbutton.button);  
                                 if (button == 0)  
843                                          break;                                          break;
844    
845                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);
                                                button | MOUSE_FLAG_DOWN,  
                                                event.xbutton.x,  
                                                event.xbutton.y);  
846                                  break;                                  break;
847    
848                            case ButtonPress:
849                                    flags = MOUSE_FLAG_DOWN;
850                                    /* fall through */
851    
852                          case ButtonRelease:                          case ButtonRelease:
853                                  button = xwin_translate_mouse(event.xbutton.button);                                  button = xkeymap_translate_button(xevent.xbutton.button);
854                                  if (button == 0)                                  if (button == 0)
855                                          break;                                          break;
856    
857                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
858                                                 button,                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);
                                                event.xbutton.x,  
                                                event.xbutton.y);  
859                                  break;                                  break;
860    
861                          case MotionNotify:                          case MotionNotify:
862                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  if (fullscreen && !focused)
863                                                 MOUSE_FLAG_MOVE,                                          XSetInputFocus(display, wnd, RevertToPointerRoot,
864                                                 event.xmotion.x,                                                         CurrentTime);
865                                                 event.xmotion.y);                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
866                                                   MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
867                                    break;
868    
869                            case FocusIn:
870                                    if (xevent.xfocus.mode == NotifyGrab)
871                                            break;
872                                    focused = True;
873                                    XQueryPointer(display, wnd, &wdummy, &wdummy, &dummy, &dummy,
874                                                  &dummy, &dummy, &state);
875                                    reset_modifier_keys(state);
876                                    if (grab_keyboard && mouse_in_wnd)
877                                            XGrabKeyboard(display, wnd, True,
878                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
879                                    break;
880    
881                            case FocusOut:
882                                    if (xevent.xfocus.mode == NotifyUngrab)
883                                            break;
884                                    focused = False;
885                                    if (xevent.xfocus.mode == NotifyWhileGrabbed)
886                                            XUngrabKeyboard(display, CurrentTime);
887                                  break;                                  break;
888    
889                          case EnterNotify:                          case EnterNotify:
890                                  if (grab_keyboard)                                  /* we only register for this event when in fullscreen mode */
891                                          xwin_grab_keyboard();                                  /* or grab_keyboard */
892                                    mouse_in_wnd = True;
893                                    if (fullscreen)
894                                    {
895                                            XSetInputFocus(display, wnd, RevertToPointerRoot,
896                                                           CurrentTime);
897                                            break;
898                                    }
899                                    if (focused)
900                                            XGrabKeyboard(display, wnd, True,
901                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
902                                  break;                                  break;
903    
904                          case LeaveNotify:                          case LeaveNotify:
905                                  if (grab_keyboard)                                  /* we only register for this event when grab_keyboard */
906                                          xwin_ungrab_keyboard();                                  mouse_in_wnd = False;
907                                    XUngrabKeyboard(display, CurrentTime);
908                                    break;
909    
910                            case Expose:
911                                    XCopyArea(display, backstore, wnd, gc,
912                                              xevent.xexpose.x, xevent.xexpose.y,
913                                              xevent.xexpose.width,
914                                              xevent.xexpose.height,
915                                              xevent.xexpose.x, xevent.xexpose.y);
916                                  break;                                  break;
917    
918                            case MappingNotify:
919                                    /* Refresh keyboard mapping if it has changed. This is important for
920                                       Xvnc, since it allocates keycodes dynamically */
921                                    if (xevent.xmapping.request == MappingKeyboard
922                                        || xevent.xmapping.request == MappingModifier)
923                                            XRefreshKeyboardMapping(&xevent.xmapping);
924    
925                                    if (xevent.xmapping.request == MappingModifier)
926                                    {
927                                            XFreeModifiermap(mod_map);
928                                            mod_map = XGetModifierMapping(display);
929                                    }
930                                    break;
931    
932                  }                  }
933          }          }
934            /* Keep going */
935            return 1;
936    }
937    
938    /* Returns 0 after user quit, 1 otherwise */
939    int
940    ui_select(int rdp_socket)
941    {
942            int n = (rdp_socket > x_socket) ? rdp_socket + 1 : x_socket + 1;
943            fd_set rfds;
944    
945            FD_ZERO(&rfds);
946    
947            while (True)
948            {
949                    /* Process any events already waiting */
950                    if (!xwin_process_events())
951                            /* User quit */
952                            return 0;
953    
954                    FD_ZERO(&rfds);
955                    FD_SET(rdp_socket, &rfds);
956                    FD_SET(x_socket, &rfds);
957    
958                    switch (select(n, &rfds, NULL, NULL, NULL))
959                    {
960                            case -1:
961                                    error("select: %s\n", strerror(errno));
962    
963                            case 0:
964                                    continue;
965                    }
966    
967                    if (FD_ISSET(rdp_socket, &rfds))
968                            return 1;
969            }
970  }  }
971    
972  void  void
# Line 357  ui_move_pointer(int x, int y) Line 976  ui_move_pointer(int x, int y)
976  }  }
977    
978  HBITMAP  HBITMAP
979  ui_create_bitmap(int width, int height, uint8 *data)  ui_create_bitmap(int width, int height, uint8 * data)
980  {  {
981          XImage *image;          XImage *image;
982          Pixmap bitmap;          Pixmap bitmap;
983          uint8 *tdata;          uint8 *tdata;
984          tdata = (private_colormap ? data : translate(width, height, data));  
985            tdata = (owncolmap ? data : translate_image(width, height, data));
986          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(display, wnd, width, height, depth);
987          image =          image = XCreateImage(display, visual, depth, ZPixmap, 0,
988                  XCreateImage(display, visual,                               (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0);
                              depth, ZPixmap,  
                              0, tdata, width, height, BitmapPad(display), 0);  
989    
         xwin_set_function(ROP2_COPY);  
990          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
991    
992          XFree(image);          XFree(image);
993          if (!private_colormap)          if (!owncolmap)
994                  xfree(tdata);                  xfree(tdata);
995          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
996  }  }
997    
998  void  void
999  ui_paint_bitmap(int x, int y, int cx, int cy,  ui_paint_bitmap(int x, int y, int cx, int cy, int width, int height, uint8 * data)
                 int width, int height, uint8 *data)  
1000  {  {
1001          XImage *image;          XImage *image;
1002          uint8 *tdata =          uint8 *tdata;
1003                  (private_colormap ? data : translate(width, height, data));          tdata = (owncolmap ? data : translate_image(width, height, data));
1004          image =          image = XCreateImage(display, visual, depth, ZPixmap, 0,
1005                  XCreateImage(display, visual, depth, ZPixmap, 0, tdata, width,                               (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0);
                              height, BitmapPad(display), 0);  
   
         xwin_set_function(ROP2_COPY);  
   
         /* Window */  
         XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);  
         XFree(image);  
         if (!private_colormap)  
                 xfree(tdata);  
 }  
   
 void  
 ui_destroy_bitmap(HBITMAP bmp)  
 {  
         XFreePixmap(display, (Pixmap) bmp);  
 }  
   
 HCURSOR  
 ui_create_cursor(unsigned int x, unsigned int y, int width,  
                  int height, uint8 *mask, uint8 *data)  
 {  
         XImage *imagecursor;  
         XImage *imagemask;  
         Pixmap maskbitmap, cursorbitmap;  
         Cursor cursor;  
         XColor bg, fg;  
         GC lgc;  
         int i, x1, y1, scanlinelen;  
         uint8 *cdata, *cmask;  
         uint8 c;  
         cdata = (uint8 *) malloc(sizeof(uint8) * width * height);  
         if (!cdata)  
                 return NULL;  
         scanlinelen = (width + 7) >> 3;  
         cmask = (uint8 *) malloc(sizeof(uint8) * scanlinelen * height);  
         if (!cmask)  
         {  
                 free(cdata);  
                 return NULL;  
         }  
         i = (height - 1) * scanlinelen;  
1006    
1007          if (!screen_msbfirst)          if (ownbackstore)
1008          {          {
1009                  while (i >= 0)                  XPutImage(display, backstore, gc, image, 0, 0, x, y, cx, cy);
1010                  {                  XCopyArea(display, backstore, wnd, gc, x, y, cx, cy, x, y);
                         for (x1 = 0; x1 < scanlinelen; x1++)  
                         {  
                                 c = *(mask++);  
                                 cmask[i + x1] =  
                                         ((c & 0x1) << 7) | ((c & 0x2) << 5) |  
                                         ((c & 0x4) << 3) | ((c & 0x8) << 1) |  
                                         ((c & 0x10) >> 1) | ((c & 0x20) >> 3)  
                                         | ((c & 0x40) >> 5) | ((c & 0x80) >>  
                                                                7);  
                         }  
                         i -= scanlinelen;  
                 }  
1011          }          }
1012          else          else
1013          {          {
1014                  while (i >= 0)                  XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);
                 {  
                         for (x1 = 0; x1 < scanlinelen; x1++)  
                         {  
                                 cmask[i + x1] = *(mask++);  
                         }  
                         i -= scanlinelen;  
                 }  
1015          }          }
1016    
1017            XFree(image);
1018          fg.red = 0;          if (!owncolmap)
1019          fg.blue = 0;                  xfree(tdata);
         fg.green = 0;  
         fg.flags = DoRed | DoBlue | DoGreen;  
         bg.red = 65535;  
         bg.blue = 65535;  
         bg.green = 65535;  
         bg.flags = DoRed | DoBlue | DoGreen;  
         maskbitmap = XCreatePixmap(display, wnd, width, height, 1);  
         cursorbitmap = XCreatePixmap(display, wnd, width, height, 1);  
         lgc = XCreateGC(display, maskbitmap, 0, NULL);  
         XSetFunction(display, lgc, GXcopy);  
         imagemask =  
                 XCreateImage(display, visual, 1, XYBitmap, 0, cmask, width,  
                              height, 8, 0);  
         imagecursor =  
                 XCreateImage(display, visual, 1, XYBitmap, 0, cdata, width,  
                              height, 8, 0);  
         for (y1 = height - 1; y1 >= 0; y1--)  
                 for (x1 = 0; x1 < width; x1++)  
                 {  
                         if (data[0] >= 0x80 || data[1] >= 0x80  
                             || data[2] >= 0x80)  
                                 if (XGetPixel(imagemask, x1, y1))  
   
                                 {  
                                         XPutPixel(imagecursor, x1, y1, 0);  
                                         XPutPixel(imagemask, x1, y1, 0);        /* mask is blank for text cursor! */  
                                 }  
   
                                 else  
                                         XPutPixel(imagecursor, x1, y1, 1);  
   
                         else  
                                 XPutPixel(imagecursor, x1, y1,  
                                           XGetPixel(imagemask, x1, y1));  
                         data += 3;  
                 }  
         XPutImage(display, maskbitmap, lgc, imagemask, 0, 0, 0, 0, width,  
                   height);  
         XPutImage(display, cursorbitmap, lgc, imagecursor, 0, 0, 0, 0, width,  
                   height); XFree(imagemask);  
         XFree(imagecursor);  
         free(cmask);  
         free(cdata);  
         XFreeGC(display, lgc);  
         cursor =  
                 XCreatePixmapCursor(display, cursorbitmap, maskbitmap, &fg,  
                                     &bg, x, y);  
         XFreePixmap(display, maskbitmap);  
         XFreePixmap(display, cursorbitmap);  
         return (HCURSOR) cursor;  
 }  
   
 void  
 ui_set_cursor(HCURSOR cursor)  
 {  
         XDefineCursor(display, wnd, (Cursor) cursor);  
1020  }  }
1021    
1022  void  void
1023  ui_destroy_cursor(HCURSOR cursor)  ui_destroy_bitmap(HBITMAP bmp)
1024  {  {
1025          XFreeCursor(display, (Cursor) cursor);          XFreePixmap(display, (Pixmap) bmp);
1026  }  }
1027    
1028  HGLYPH  HGLYPH
1029  ui_create_glyph(int width, int height, uint8 *data)  ui_create_glyph(int width, int height, uint8 * data)
1030  {  {
1031          XImage *image;          XImage *image;
1032          Pixmap bitmap;          Pixmap bitmap;
# Line 538  ui_create_glyph(int width, int height, u Line 1038  ui_create_glyph(int width, int height, u
1038          bitmap = XCreatePixmap(display, wnd, width, height, 1);          bitmap = XCreatePixmap(display, wnd, width, height, 1);
1039          gc = XCreateGC(display, bitmap, 0, NULL);          gc = XCreateGC(display, bitmap, 0, NULL);
1040    
1041          image = XCreateImage(display, visual, 1, ZPixmap, 0,          image = XCreateImage(display, visual, 1, ZPixmap, 0, (char *) data,
1042                               data, width, height, 8, scanline);                               width, height, 8, scanline);
1043          image->byte_order = MSBFirst;          image->byte_order = MSBFirst;
1044          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
1045          XInitImage(image);          XInitImage(image);
1046    
         XSetFunction(display, gc, GXcopy);  
1047          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
1048    
1049          XFree(image);          XFree(image);
1050          XFreeGC(display, gc);          XFreeGC(display, gc);
   
1051          return (HGLYPH) bitmap;          return (HGLYPH) bitmap;
1052  }  }
1053    
# Line 558  ui_destroy_glyph(HGLYPH glyph) Line 1057  ui_destroy_glyph(HGLYPH glyph)
1057          XFreePixmap(display, (Pixmap) glyph);          XFreePixmap(display, (Pixmap) glyph);
1058  }  }
1059    
1060  HCOLOURMAP  HCURSOR
1061  ui_create_colourmap(COLOURMAP *colours)  ui_create_cursor(unsigned int x, unsigned int y, int width, int height,
1062                     uint8 * andmask, uint8 * xormask)
1063  {  {
1064          if (!private_colormap)          HGLYPH maskglyph, cursorglyph;
1065            XColor bg, fg;
1066            Cursor xcursor;
1067            uint8 *cursor, *pcursor;
1068            uint8 *mask, *pmask;
1069            uint8 nextbit;
1070            int scanline, offset;
1071            int i, j;
1072    
1073            scanline = (width + 7) / 8;
1074            offset = scanline * height;
1075    
1076            cursor = xmalloc(offset);
1077            memset(cursor, 0, offset);
1078    
1079            mask = xmalloc(offset);
1080            memset(mask, 0, offset);
1081    
1082            /* approximate AND and XOR masks with a monochrome X pointer */
1083            for (i = 0; i < height; i++)
1084          {          {
1085                  COLOURENTRY *entry;                  offset -= scanline;
1086                  int i, ncolours = colours->ncolours;                  pcursor = &cursor[offset];
1087                  uint32 *nc = xmalloc(sizeof(*colmap) * ncolours);                  pmask = &mask[offset];
1088    
1089                    for (j = 0; j < scanline; j++)
1090                    {
1091                            for (nextbit = 0x80; nextbit != 0; nextbit >>= 1)
1092                            {
1093                                    if (xormask[0] || xormask[1] || xormask[2])
1094                                    {
1095                                            *pcursor |= (~(*andmask) & nextbit);
1096                                            *pmask |= nextbit;
1097                                    }
1098                                    else
1099                                    {
1100                                            *pcursor |= ((*andmask) & nextbit);
1101                                            *pmask |= (~(*andmask) & nextbit);
1102                                    }
1103    
1104                                    xormask += 3;
1105                            }
1106    
1107                            andmask++;
1108                            pcursor++;
1109                            pmask++;
1110                    }
1111            }
1112    
1113            fg.red = fg.blue = fg.green = 0xffff;
1114            bg.red = bg.blue = bg.green = 0x0000;
1115            fg.flags = bg.flags = DoRed | DoBlue | DoGreen;
1116    
1117            cursorglyph = ui_create_glyph(width, height, cursor);
1118            maskglyph = ui_create_glyph(width, height, mask);
1119    
1120            xcursor =
1121                    XCreatePixmapCursor(display, (Pixmap) cursorglyph,
1122                                        (Pixmap) maskglyph, &fg, &bg, x, y);
1123    
1124            ui_destroy_glyph(maskglyph);
1125            ui_destroy_glyph(cursorglyph);
1126            xfree(mask);
1127            xfree(cursor);
1128            return (HCURSOR) xcursor;
1129    }
1130    
1131    void
1132    ui_set_cursor(HCURSOR cursor)
1133    {
1134            current_cursor = (Cursor) cursor;
1135            XDefineCursor(display, wnd, current_cursor);
1136    }
1137    
1138    void
1139    ui_destroy_cursor(HCURSOR cursor)
1140    {
1141            XFreeCursor(display, (Cursor) cursor);
1142    }
1143    
1144    #define MAKE_XCOLOR(xc,c) \
1145                    (xc)->red   = ((c)->red   << 8) | (c)->red; \
1146                    (xc)->green = ((c)->green << 8) | (c)->green; \
1147                    (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \
1148                    (xc)->flags = DoRed | DoGreen | DoBlue;
1149    
1150    
1151    HCOLOURMAP
1152    ui_create_colourmap(COLOURMAP * colours)
1153    {
1154            COLOURENTRY *entry;
1155            int i, ncolours = colours->ncolours;
1156            if (!owncolmap)
1157            {
1158                    uint32 *map = xmalloc(sizeof(*colmap) * ncolours);
1159                    XColor xentry;
1160                    XColor xc_cache[256];
1161                    uint32 colour;
1162                    int colLookup = 256;
1163                  for (i = 0; i < ncolours; i++)                  for (i = 0; i < ncolours; i++)
1164                  {                  {
                         XColor xc;  
1165                          entry = &colours->colours[i];                          entry = &colours->colours[i];
1166                          xc.red = entry->red << 8;                          MAKE_XCOLOR(&xentry, entry);
1167                          xc.green = entry->green << 8;  
1168                          xc.blue = entry->blue << 8;                          if (XAllocColor(display, xcolmap, &xentry) == 0)
1169                          XAllocColor(display,                          {
1170                                      DefaultColormap(display,                                  /* Allocation failed, find closest match. */
1171                                                      DefaultScreen(display)),                                  int j = 256;
1172                                      &xc);                                  int nMinDist = 3 * 256 * 256;
1173                          /* XXX Check return value */                                  long nDist = nMinDist;
1174                          nc[i] = xc.pixel;  
1175                                    /* only get the colors once */
1176                                    while (colLookup--)
1177                                    {
1178                                            xc_cache[colLookup].pixel = colLookup;
1179                                            xc_cache[colLookup].red = xc_cache[colLookup].green =
1180                                                    xc_cache[colLookup].blue = 0;
1181                                            xc_cache[colLookup].flags = 0;
1182                                            XQueryColor(display,
1183                                                        DefaultColormap(display,
1184                                                                        DefaultScreen(display)),
1185                                                        &xc_cache[colLookup]);
1186                                    }
1187                                    colLookup = 0;
1188    
1189                                    /* approximate the pixel */
1190                                    while (j--)
1191                                    {
1192                                            if (xc_cache[j].flags)
1193                                            {
1194                                                    nDist = ((long) (xc_cache[j].red >> 8) -
1195                                                             (long) (xentry.red >> 8)) *
1196                                                            ((long) (xc_cache[j].red >> 8) -
1197                                                             (long) (xentry.red >> 8)) +
1198                                                            ((long) (xc_cache[j].green >> 8) -
1199                                                             (long) (xentry.green >> 8)) *
1200                                                            ((long) (xc_cache[j].green >> 8) -
1201                                                             (long) (xentry.green >> 8)) +
1202                                                            ((long) (xc_cache[j].blue >> 8) -
1203                                                             (long) (xentry.blue >> 8)) *
1204                                                            ((long) (xc_cache[j].blue >> 8) -
1205                                                             (long) (xentry.blue >> 8));
1206                                            }
1207                                            if (nDist < nMinDist)
1208                                            {
1209                                                    nMinDist = nDist;
1210                                                    xentry.pixel = j;
1211                                            }
1212                                    }
1213                            }
1214                            colour = xentry.pixel;
1215    
1216                            /* update our cache */
1217                            if (xentry.pixel < 256)
1218                            {
1219                                    xc_cache[xentry.pixel].red = xentry.red;
1220                                    xc_cache[xentry.pixel].green = xentry.green;
1221                                    xc_cache[xentry.pixel].blue = xentry.blue;
1222    
1223                            }
1224    
1225    
1226                            /* byte swap here to make translate_image faster */
1227                            map[i] = translate_colour(colour);
1228                  }                  }
1229                  return nc;                  return map;
1230          }          }
1231          else          else
1232          {          {
                 COLOURENTRY *entry;  
1233                  XColor *xcolours, *xentry;                  XColor *xcolours, *xentry;
1234                  Colormap map;                  Colormap map;
1235                  int i, ncolours = colours->ncolours;  
1236                  xcolours = xmalloc(sizeof(XColor) * ncolours);                  xcolours = xmalloc(sizeof(XColor) * ncolours);
1237                  for (i = 0; i < ncolours; i++)                  for (i = 0; i < ncolours; i++)
1238                  {                  {
1239                          entry = &colours->colours[i];                          entry = &colours->colours[i];
1240                          xentry = &xcolours[i];                          xentry = &xcolours[i];
   
1241                          xentry->pixel = i;                          xentry->pixel = i;
1242                          xentry->red = entry->red << 8;                          MAKE_XCOLOR(xentry, entry);
                         xentry->blue = entry->blue << 8;  
                         xentry->green = entry->green << 8;  
                         xentry->flags = DoRed | DoBlue | DoGreen;  
1243                  }                  }
1244    
1245                  map = XCreateColormap(display, wnd, visual, AllocAll);                  map = XCreateColormap(display, wnd, visual, AllocAll);
# Line 612  ui_create_colourmap(COLOURMAP *colours) Line 1253  ui_create_colourmap(COLOURMAP *colours)
1253  void  void
1254  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
1255  {  {
1256          XFreeColormap(display, (Colormap) map);          if (!owncolmap)
1257                    xfree(map);
1258            else
1259                    XFreeColormap(display, (Colormap) map);
1260  }  }
1261    
1262  void  void
1263  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
1264  {  {
1265            if (!owncolmap)
         /* XXX, change values of all pixels on the screen if the new colmap  
          * doesn't have the same values as the old one? */  
         if (!private_colormap)  
1266                  colmap = map;                  colmap = map;
1267          else          else
         {  
1268                  XSetWindowColormap(display, wnd, (Colormap) map);                  XSetWindowColormap(display, wnd, (Colormap) map);
                 if (fullscreen)  
                         XInstallColormap(display, (Colormap) map);  
         }  
1269  }  }
1270    
1271  void  void
# Line 644  ui_set_clip(int x, int y, int cx, int cy Line 1281  ui_set_clip(int x, int y, int cx, int cy
1281  }  }
1282    
1283  void  void
1284  ui_reset_clip()  ui_reset_clip(void)
1285  {  {
1286          XRectangle rect;          XRectangle rect;
1287    
# Line 656  ui_reset_clip() Line 1293  ui_reset_clip()
1293  }  }
1294    
1295  void  void
1296  ui_bell()  ui_bell(void)
1297  {  {
1298          XBell(display, 0);          XBell(display, 0);
1299  }  }
# Line 665  void Line 1302  void
1302  ui_destblt(uint8 opcode,  ui_destblt(uint8 opcode,
1303             /* dest */ int x, int y, int cx, int cy)             /* dest */ int x, int y, int cx, int cy)
1304  {  {
1305          xwin_set_function(opcode);          SET_FUNCTION(opcode);
1306            FILL_RECTANGLE(x, y, cx, cy);
1307          XFillRectangle(display, wnd, gc, x, y, cx, cy);          RESET_FUNCTION(opcode);
1308  }  }
1309    
1310  void  void
1311  ui_patblt(uint8 opcode,  ui_patblt(uint8 opcode,
1312            /* dest */ int x, int y, int cx, int cy,            /* dest */ int x, int y, int cx, int cy,
1313            /* brush */ BRUSH *brush, int bgcolour, int fgcolour)            /* brush */ BRUSH * brush, int bgcolour, int fgcolour)
1314  {  {
         Display *dpy = display;  
1315          Pixmap fill;          Pixmap fill;
1316          uint8 i, ipattern[8];          uint8 i, ipattern[8];
1317    
1318          xwin_set_function(opcode);          SET_FUNCTION(opcode);
1319    
1320          switch (brush->style)          switch (brush->style)
1321          {          {
1322                  case 0: /* Solid */                  case 0: /* Solid */
1323                          XSetForeground(dpy, gc, Ctrans(fgcolour));                          SET_FOREGROUND(fgcolour);
1324                          XFillRectangle(dpy, wnd, gc, x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
1325                          break;                          break;
1326    
1327                  case 3: /* Pattern */                  case 3: /* Pattern */
1328                          for (i = 0; i != 8; i++)                          for (i = 0; i != 8; i++)
1329                                  ipattern[i] = ~brush->pattern[i];                                  ipattern[7 - i] = brush->pattern[i];
1330                          fill = (Pixmap) ui_create_glyph(8, 8, ipattern);                          fill = (Pixmap) ui_create_glyph(8, 8, ipattern);
1331    
1332                          XSetForeground(dpy, gc, Ctrans(fgcolour));                          SET_FOREGROUND(bgcolour);
1333                          XSetBackground(dpy, gc, Ctrans(bgcolour));                          SET_BACKGROUND(fgcolour);
1334                          XSetFillStyle(dpy, gc, FillOpaqueStippled);                          XSetFillStyle(display, gc, FillOpaqueStippled);
1335                          XSetStipple(dpy, gc, fill);                          XSetStipple(display, gc, fill);
1336                            XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin);
1337    
1338                          XFillRectangle(dpy, wnd, gc, x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
1339    
1340                          XSetFillStyle(dpy, gc, FillSolid);                          XSetFillStyle(display, gc, FillSolid);
1341                            XSetTSOrigin(display, gc, 0, 0);
1342                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
1343                          break;                          break;
1344    
1345                  default:                  default:
1346                          NOTIMP("brush %d\n", brush->style);                          unimpl("brush %d\n", brush->style);
1347          }          }
1348    
1349            RESET_FUNCTION(opcode);
1350  }  }
1351    
1352  void  void
# Line 714  ui_screenblt(uint8 opcode, Line 1354  ui_screenblt(uint8 opcode,
1354               /* dest */ int x, int y, int cx, int cy,               /* dest */ int x, int y, int cx, int cy,
1355               /* src */ int srcx, int srcy)               /* src */ int srcx, int srcy)
1356  {  {
1357          xwin_set_function(opcode);          SET_FUNCTION(opcode);
   
1358          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);
1359            if (ownbackstore)
1360                    XCopyArea(display, backstore, backstore, gc, srcx, srcy, cx, cy, x, y);
1361            RESET_FUNCTION(opcode);
1362  }  }
1363    
1364  void  void
# Line 724  ui_memblt(uint8 opcode, Line 1366  ui_memblt(uint8 opcode,
1366            /* dest */ int x, int y, int cx, int cy,            /* dest */ int x, int y, int cx, int cy,
1367            /* src */ HBITMAP src, int srcx, int srcy)            /* src */ HBITMAP src, int srcx, int srcy)
1368  {  {
1369          xwin_set_function(opcode);          SET_FUNCTION(opcode);
   
1370          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);
1371            if (ownbackstore)
1372                    XCopyArea(display, (Pixmap) src, backstore, gc, srcx, srcy, cx, cy, x, y);
1373            RESET_FUNCTION(opcode);
1374  }  }
1375    
1376  void  void
1377  ui_triblt(uint8 opcode,  ui_triblt(uint8 opcode,
1378            /* dest */ int x, int y, int cx, int cy,            /* dest */ int x, int y, int cx, int cy,
1379            /* src */ HBITMAP src, int srcx, int srcy,            /* src */ HBITMAP src, int srcx, int srcy,
1380            /* brush */ BRUSH *brush, int bgcolour, int fgcolour)            /* brush */ BRUSH * brush, int bgcolour, int fgcolour)
1381  {  {
1382          /* This is potentially difficult to do in general. Until someone          /* This is potentially difficult to do in general. Until someone
1383             comes up with a more efficient way of doing it I am using cases. */             comes up with a more efficient way of doing it I am using cases. */
# Line 742  ui_triblt(uint8 opcode, Line 1386  ui_triblt(uint8 opcode,
1386          {          {
1387                  case 0x69:      /* PDSxxn */                  case 0x69:      /* PDSxxn */
1388                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);
1389                          ui_patblt(ROP2_NXOR, x, y, cx, cy,                          ui_patblt(ROP2_NXOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   brush, bgcolour, fgcolour);  
1390                          break;                          break;
1391    
1392                  case 0xb8:      /* PSDPxax */                  case 0xb8:      /* PSDPxax */
1393                          ui_patblt(ROP2_XOR, x, y, cx, cy,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   brush, bgcolour, fgcolour);  
1394                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);
1395                          ui_patblt(ROP2_XOR, x, y, cx, cy,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   brush, bgcolour, fgcolour);  
1396                          break;                          break;
1397    
1398                  case 0xc0:                  case 0xc0:      /* PSa */
1399                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
1400                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1401                          break;                          break;
1402    
1403                  default:                  default:
1404                          NOTIMP("triblt 0x%x\n", opcode);                          unimpl("triblt 0x%x\n", opcode);
1405                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
1406          }          }
1407  }  }
# Line 769  ui_triblt(uint8 opcode, Line 1409  ui_triblt(uint8 opcode,
1409  void  void
1410  ui_line(uint8 opcode,  ui_line(uint8 opcode,
1411          /* dest */ int startx, int starty, int endx, int endy,          /* dest */ int startx, int starty, int endx, int endy,
1412          /* pen */ PEN *pen)          /* pen */ PEN * pen)
1413  {  {
1414          xwin_set_function(opcode);          SET_FUNCTION(opcode);
1415            SET_FOREGROUND(pen->colour);
         XSetForeground(display, gc, Ctrans(pen->colour));  
1416          XDrawLine(display, wnd, gc, startx, starty, endx, endy);          XDrawLine(display, wnd, gc, startx, starty, endx, endy);
1417            if (ownbackstore)
1418                    XDrawLine(display, backstore, gc, startx, starty, endx, endy);
1419            RESET_FUNCTION(opcode);
1420  }  }
1421    
1422  void  void
# Line 782  ui_rect( Line 1424  ui_rect(
1424                 /* dest */ int x, int y, int cx, int cy,                 /* dest */ int x, int y, int cx, int cy,
1425                 /* brush */ int colour)                 /* brush */ int colour)
1426  {  {
1427          xwin_set_function(ROP2_COPY);          SET_FOREGROUND(colour);
1428            FILL_RECTANGLE(x, y, cx, cy);
         XSetForeground(display, gc, Ctrans(colour));  
         XFillRectangle(display, wnd, gc, x, y, cx, cy);  
1429  }  }
1430    
1431    /* warning, this function only draws on wnd or backstore, not both */
1432  void  void
1433  ui_draw_glyph(int mixmode,  ui_draw_glyph(int mixmode,
1434                /* dest */ int x, int y, int cx, int cy,                /* dest */ int x, int y, int cx, int cy,
1435                /* src */ HGLYPH glyph, int srcx, int srcy, int bgcolour,                /* src */ HGLYPH glyph, int srcx, int srcy,
1436                int fgcolour)                int bgcolour, int fgcolour)
1437  {  {
1438          Pixmap pixmap = (Pixmap) glyph;          SET_FOREGROUND(fgcolour);
1439            SET_BACKGROUND(bgcolour);
         xwin_set_function(ROP2_COPY);  
1440    
1441            XSetFillStyle(display, gc,
1442          XSetForeground(display, gc, Ctrans(fgcolour));                        (mixmode == MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);
1443          switch (mixmode)          XSetStipple(display, gc, (Pixmap) glyph);
1444          {          XSetTSOrigin(display, gc, x, y);
1445                  case MIX_TRANSPARENT:  
1446                          XSetStipple(display, gc, pixmap);          FILL_RECTANGLE_BACKSTORE(x, y, cx, cy);
1447                          XSetFillStyle(display, gc, FillStippled);  
1448                          XSetTSOrigin(display, gc, x, y);          XSetFillStyle(display, gc, FillSolid);
1449                          XFillRectangle(display, wnd, gc, x, y, cx, cy);  }
1450                          XSetFillStyle(display, gc, FillSolid);  
1451                          break;  #define DO_GLYPH(ttext,idx) \
1452    {\
1453                  case MIX_OPAQUE:    glyph = cache_get_font (font, ttext[idx]);\
1454                          XSetBackground(display, gc, Ctrans(bgcolour));    if (!(flags & TEXT2_IMPLICIT_X))\
1455  /*      XCopyPlane (display, pixmap, back_pixmap, back_gc, srcx, srcy, cx, cy, x, y, 1); */      {\
1456                          XSetStipple(display, gc, pixmap);        xyoffset = ttext[++idx];\
1457                          XSetFillStyle(display, gc, FillOpaqueStippled);        if ((xyoffset & 0x80))\
1458                          XSetTSOrigin(display, gc, x, y);          {\
1459                          XFillRectangle(display, wnd, gc, x, y, cx, cy);            if (flags & TEXT2_VERTICAL) \
1460                          XSetFillStyle(display, gc, FillSolid);              y += ttext[idx+1] | (ttext[idx+2] << 8);\
1461                          break;            else\
1462                x += ttext[idx+1] | (ttext[idx+2] << 8);\
1463                  default:            idx += 2;\
1464                          NOTIMP("mix %d\n", mixmode);          }\
1465          }        else\
1466            {\
1467              if (flags & TEXT2_VERTICAL) \
1468                y += xyoffset;\
1469              else\
1470                x += xyoffset;\
1471            }\
1472        }\
1473      if (glyph != NULL)\
1474        {\
1475          ui_draw_glyph (mixmode, x + glyph->offset,\
1476                         y + glyph->baseline,\
1477                         glyph->width, glyph->height,\
1478                         glyph->pixmap, 0, 0, bgcolour, fgcolour);\
1479          if (flags & TEXT2_IMPLICIT_X)\
1480            x += glyph->width;\
1481        }\
1482  }  }
1483    
1484  void  void
1485  ui_draw_text(uint8 font, uint8 flags, int mixmode, int x, int y,  ui_draw_text(uint8 font, uint8 flags, int mixmode, int x, int y,
1486               int clipx, int clipy, int clipcx, int clipcy,               int clipx, int clipy, int clipcx, int clipcy,
1487               int boxx, int boxy, int boxcx, int boxcy,               int boxx, int boxy, int boxcx, int boxcy, int bgcolour,
1488               int bgcolour, int fgcolour, uint8 *text, uint8 length)               int fgcolour, uint8 * text, uint8 length)
1489  {  {
1490          FONTGLYPH *glyph;          FONTGLYPH *glyph;
1491          int i, xyoffset;          int i, j, xyoffset;
1492            DATABLOB *entry;
1493    
1494          xwin_set_function(ROP2_COPY);          SET_FOREGROUND(bgcolour);
         XSetForeground(display, gc, Ctrans(bgcolour));  
1495    
1496          if (boxcx > 1)          if (boxcx > 1)
1497                  XFillRectangle(display, wnd, gc, boxx, boxy, boxcx, boxcy);          {
1498                    FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy);
1499            }
1500          else if (mixmode == MIX_OPAQUE)          else if (mixmode == MIX_OPAQUE)
1501                  XFillRectangle(display, wnd, gc, clipx, clipy, clipcx, clipcy);          {
1502                    FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy);
1503            }
1504    
1505          /* Paint text, character by character */          /* Paint text, character by character */
1506          for (i = 0; i < length; i++)          for (i = 0; i < length;)
1507          {          {
1508                  glyph = cache_get_font(font, text[i]);                  switch (text[i])
   
                 if (!(flags & TEXT2_IMPLICIT_X))  
   
1509                  {                  {
1510                          xyoffset = text[++i];                          case 0xff:
1511                          if ((xyoffset & 0x80))                                  if (i + 2 < length)
1512                          {                                          cache_put_text(text[i + 1], text, text[i + 2]);
                                 if (flags & 0x04)       /* vertical text */  
                                         y += text[++i] | (text[++i] << 8);  
1513                                  else                                  else
1514                                          x += text[++i] | (text[++i] << 8);                                  {
1515                          }                                          error("this shouldn't be happening\n");
1516                          else                                          exit(1);
1517                          {                                  }
1518                                  if (flags & 0x04)       /* vertical text */                                  /* this will move pointer from start to first character after FF command */
1519                                          y += xyoffset;                                  length -= i + 3;
1520                                  else                                  text = &(text[i + 3]);
1521                                          x += xyoffset;                                  i = 0;
1522                          }                                  break;
1523    
1524                  }                          case 0xfe:
1525                  if (glyph != NULL)                                  entry = cache_get_text(text[i + 1]);
1526                  {                                  if (entry != NULL)
1527                          ui_draw_glyph(mixmode, x + (short) glyph->offset,                                  {
1528                                        y + (short) glyph->baseline,                                          if ((((uint8 *) (entry->data))[1] ==
1529                                        glyph->width, glyph->height,                                               0) && (!(flags & TEXT2_IMPLICIT_X)))
1530                                        glyph->pixmap, 0, 0,                                          {
1531                                        bgcolour, fgcolour);                                                  if (flags & TEXT2_VERTICAL)
1532                                                            y += text[i + 2];
1533                                                    else
1534                                                            x += text[i + 2];
1535                                            }
1536                                            for (j = 0; j < entry->size; j++)
1537                                                    DO_GLYPH(((uint8 *) (entry->data)), j);
1538                                    }
1539                                    if (i + 2 < length)
1540                                            i += 3;
1541                                    else
1542                                            i += 2;
1543                                    length -= i;
1544                                    /* this will move pointer from start to first character after FE command */
1545                                    text = &(text[i]);
1546                                    i = 0;
1547                                    break;
1548    
1549                          if (flags & TEXT2_IMPLICIT_X)                          default:
1550                                  x += glyph->width;                                  DO_GLYPH(text, i);
1551                                    i++;
1552                                    break;
1553                  }                  }
1554          }          }
1555            if (ownbackstore)
1556            {
1557                    if (boxcx > 1)
1558                            XCopyArea(display, backstore, wnd, gc, boxx,
1559                                      boxy, boxcx, boxcy, boxx, boxy);
1560                    else
1561                            XCopyArea(display, backstore, wnd, gc, clipx,
1562                                      clipy, clipcx, clipcy, clipx, clipy);
1563            }
1564  }  }
1565    
1566  void  void
# Line 887  ui_desktop_save(uint32 offset, int x, in Line 1569  ui_desktop_save(uint32 offset, int x, in
1569          Pixmap pix;          Pixmap pix;
1570          XImage *image;          XImage *image;
1571    
1572          pix = XCreatePixmap(display, wnd, cx, cy, depth);          if (ownbackstore)
1573          xwin_set_function(ROP2_COPY);          {
1574                    image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes, ZPixmap);
1575          XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);          }
1576          image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);          else
1577            {
1578                    pix = XCreatePixmap(display, wnd, cx, cy, depth);
1579                    XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);
1580                    image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);
1581                    XFreePixmap(display, pix);
1582            }
1583    
1584          offset *= bpp/8;          offset *= bpp / 8;
1585          cache_put_desktop(offset, cx, cy, image->bytes_per_line,          cache_put_desktop(offset, cx, cy, image->bytes_per_line, bpp / 8, (uint8 *) image->data);
                           bpp/8, image->data);  
1586    
1587          XDestroyImage(image);          XDestroyImage(image);
         XFreePixmap(display, pix);  
1588  }  }
1589    
1590  void  void
# Line 907  ui_desktop_restore(uint32 offset, int x, Line 1593  ui_desktop_restore(uint32 offset, int x,
1593          XImage *image;          XImage *image;
1594          uint8 *data;          uint8 *data;
1595    
1596          offset *= bpp/8;          offset *= bpp / 8;
1597          data = cache_get_desktop(offset, cx, cy, bpp/8);          data = cache_get_desktop(offset, cx, cy, bpp / 8);
1598          if (data == NULL)          if (data == NULL)
1599                  return;                  return;
         image =  
                 XCreateImage(display, visual,  
                              depth, ZPixmap,  
                              0, data, cx, cy, BitmapPad(display),  
                              cx * bpp/8);  
         xwin_set_function(ROP2_COPY);  
         XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);  
         XFree(image);  
 }  
1600    
1601  /* unroll defines, used to make the loops a bit more readable... */          image = XCreateImage(display, visual, depth, ZPixmap, 0,
1602  #define unroll8Expr(uexp) uexp uexp uexp uexp uexp uexp uexp uexp                               (char *) data, cx, cy, BitmapPad(display), cx * bpp / 8);
 #define unroll8Lefts(uexp) case 7: uexp \  
         case 6: uexp \  
         case 5: uexp \  
         case 4: uexp \  
         case 3: uexp \  
         case 2: uexp \  
         case 1: uexp  
   
 static uint8 *  
 translate(int width, int height, uint8 *data)  
 {  
         uint32 i;  
         uint32 size = width * height;  
         uint8 *d2 = xmalloc(size * bpp/8);  
         uint8 *d3 = d2;  
         uint32 pix;  
         i = (size & ~0x7);  
   
         /* XXX: where are the bits swapped??? */  
 #ifdef L_ENDIAN                 /* little-endian */  
         /* big-endian screen */  
         if (screen_msbfirst)  
         {  
                 switch (bpp)  
                 {  
                         case 32:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix >> 24;  
                                                     *d3++ = pix >> 16;  
                                                     *d3++ = pix >> 8;  
                                                     *d3++ = pix;) i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix >>  
                                                                      24;  
                                                                      *d3++ =  
                                                                      pix >>  
                                                                      16;  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                                                      *d3++ =  
                                                                      pix;)}  
                                 break;  
                         case 24:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix >> 16;  
                                                     *d3++ = pix >> 8;  
                                                     *d3++ = pix;) i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix >>  
                                                                      16;  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                                                      *d3++ =  
                                                                      pix;)}  
                                 break;  
                         case 16:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix >> 8;  
                                                     *d3++ = pix;) i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                                                      *d3++ =  
                                                                      pix;)}  
                                 break;  
                         case 8:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;)}  
                                 break;  
                 }  
         }  
         else  
         {                       /* little-endian screen */  
                 switch (bpp)  
                 {  
                         case 32:  
                                 while (i)  
                                 {  
                                         unroll8Expr(*((uint32 *) d3) =  
                                                     colmap[*data++];  
                                                     d3 += sizeof(uint32);  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(*  
                                                                      ((uint32  
                                                                        *) d3)  
 = colmap[*data++];  
 d3 += sizeof(uint32);  
                                         )}  
                                 break;  
                         case 24:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                     *d3++ = pix >> 8;  
                                                     *d3++ = pix >> 16;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                                                      *d3++ =  
                                                                      pix >>  
                                                                      16;)}  
                                 break;  
                         case 16:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                     *d3++ = pix >> 8;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                         )}  
                                 break;  
                         case 8:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;)}  
                 }  
         }  
1603    
1604  #else /* bigendian-compiled */          if (ownbackstore)
         if (screen_msbfirst)  
1605          {          {
1606                  /* big-endian screen */                  XPutImage(display, backstore, gc, image, 0, 0, x, y, cx, cy);
1607                  switch (bpp)                  XCopyArea(display, backstore, wnd, gc, x, y, cx, cy, x, y);
                 {  
                         case 32:  
                                 while (i)  
                                 {  
                                         unroll8Expr(*((uint32 *) d3) =  
                                                     colmap[*data++];  
                                                     d3 += sizeof(uint32);  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(*  
                                                                      ((uint32  
                                                                        *) d3)  
 = colmap[*data++];  
 d3 += sizeof(uint32);  
                                         )}  
                                 break;  
                         case 24:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                     *d3++ = pix >> 8;  
                                                     *d3++ = pix >> 16;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                                                      *d3++ =  
                                                                      pix >>  
                                                                      16;)}  
                                 break;  
                         case 16:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                     *d3++ = pix >> 8;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                         )}  
                                 break;  
                         case 8:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;)}  
                 }  
1608          }          }
1609          else          else
1610          {          {
1611                  /* little-endian screen */                  XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);
                 switch (bpp)  
                 {  
                         case 32:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                     *d3++ = pix >> 8;  
                                                     *d3++ = pix >> 16;  
                                                     *d3++ = pix >> 24;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                                                      *d3++ =  
                                                                      pix >>  
                                                                      16;  
                                                                      *d3++ =  
                                                                      pix >>  
                                                                      24;)}  
                                 break;  
                         case 24:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                     *d3++ = pix >> 8;  
                                                     *d3++ = pix >> 16;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                                                      *d3++ =  
                                                                      pix >>  
                                                                      16;)}  
                                 break;  
                         case 16:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                     *d3++ = pix >> 8;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;  
                                                                      *d3++ =  
                                                                      pix >> 8;  
                                         )}  
                                 break;  
                         case 8:  
                                 while (i)  
                                 {  
                                         unroll8Expr(pix = colmap[*data++];  
                                                     *d3++ = pix;  
                                                 )i -= 8;  
                                 }  
                                 i = (size & 0x7);  
                                 if (i != 0)  
                                         switch (i)  
                                         {  
                                                         unroll8Lefts(pix =  
                                                                      colmap  
                                                                      [*data++];  
                                                                      *d3++ =  
                                                                      pix;)}  
                 }  
1612          }          }
 #endif  
1613    
1614          return d2;          XFree(image);
1615  }  }

Legend:
Removed from v.28  
changed lines
  Added in v.318

  ViewVC Help
Powered by ViewVC 1.1.26