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

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

  ViewVC Help
Powered by ViewVC 1.1.26