/[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 53 by matthewc, Tue May 28 11:48:55 2002 UTC
# Line 1  Line 1 
1  /*  /*
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     User interface services - X-Windows     User interface services - X Window System
4     Copyright (C) Matthew Chapman 1999-2000     Copyright (C) Matthew Chapman 1999-2001
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 20  Line 20 
20    
21  #include <X11/Xlib.h>  #include <X11/Xlib.h>
22  #include <X11/Xutil.h>  #include <X11/Xutil.h>
23    #include <X11/XKBlib.h>
24  #include <time.h>  #include <time.h>
25    #include <errno.h>
26  #include "rdesktop.h"  #include "rdesktop.h"
27    
28    extern char keymapname[16];
29    extern int keylayout;
30  extern int width;  extern int width;
31  extern int height;  extern int height;
32  extern BOOL motion;  extern BOOL sendmotion;
 extern BOOL grab_keyboard;  
33  extern BOOL fullscreen;  extern BOOL fullscreen;
 extern int private_colormap;  
34    
35  static int bpp;  Display *display;
36  static int depth;  XkbDescPtr xkb;
37  static Display *display;  static int x_socket;
38  static Window wnd;  static Window wnd;
39  static GC gc;  static GC gc;
40  static Visual *visual;  static Visual *visual;
41  static uint32 *colmap;  static int depth;
42    static int bpp;
 #define Ctrans(col) ( private_colormap ? col : colmap[col])  
43    
44  #define L_ENDIAN  /* endianness */
45  int screen_msbfirst = 0;  static BOOL host_be;
46    static BOOL xserver_be;
47    
48    /* software backing store */
49    static BOOL ownbackstore;
50    static Pixmap backstore;
51    
52    /* needed to keep track of the modifiers */
53    static unsigned int key_modifier_state = 0;
54    static unsigned int key_down_state = 0;
55    
56    #define DShift1Mask   (1<<0)
57    #define DShift2Mask   (1<<1)
58    #define DControl1Mask (1<<2)
59    #define DControl2Mask (1<<3)
60    #define DMod1Mask     (1<<4)
61    #define DMod2Mask     (1<<5)
62    
63    #define FILL_RECTANGLE(x,y,cx,cy)\
64    { \
65            XFillRectangle(display, wnd, gc, x, y, cx, cy); \
66            if (ownbackstore) \
67                    XFillRectangle(display, backstore, gc, x, y, cx, cy); \
68    }
69    
70    /* colour maps */
71    static BOOL owncolmap;
72    static Colormap xcolmap;
73    static uint32 white;
74    static uint32 *colmap;
75    
76  static uint8 *translate(int width, int height, uint8 *data);  #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )
77    #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));
78    #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));
79    
80  static int rop2_map[] = {  static int rop2_map[] = {
81          GXclear,                /* 0 */          GXclear,                /* 0 */
# Line 64  static int rop2_map[] = { Line 96  static int rop2_map[] = {
96          GXset                   /* 1 */          GXset                   /* 1 */
97  };  };
98    
99    #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(display, gc, rop2_map[rop2]); }
100    #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }
101    
102    void xwin_release_modifiers(XKeyEvent* ev, uint32 ev_time, uint32 scancode);
103    void xwin_press_modifiers(XKeyEvent* ev, uint32 ev_time, uint32 scancode);
104    
105  static void  static void
106  xwin_set_function(uint8 rop2)  translate8(uint8 *data, uint8 *out, uint8 *end)
107  {  {
108          static uint8 last_rop2 = ROP2_COPY;          while (out < end)
109                    *(out++) = (uint8)colmap[*(data++)];
110    }
111    
112    static void
113    translate16(uint8 *data, uint16 *out, uint16 *end)
114    {
115            while (out < end)
116                    *(out++) = (uint16)colmap[*(data++)];
117    }
118    
119    /* little endian - conversion happens when colourmap is built */
120    static void
121    translate24(uint8 *data, uint8 *out, uint8 *end)
122    {
123            uint32 value;
124    
125          if (last_rop2 != rop2)          while (out < end)
126          {          {
127                  XSetFunction(display, gc, rop2_map[rop2]);                  value = colmap[*(data++)];
128                  last_rop2 = rop2;                  *(out++) = value;
129                    *(out++) = value >> 8;
130                    *(out++) = value >> 16;
131          }          }
132  }  }
133    
134  static void  static void
135  xwin_grab_keyboard()  translate32(uint8 *data, uint32 *out, uint32 *end)
136  {  {
137          XGrabKeyboard(display, wnd, True, GrabModeAsync, GrabModeAsync,          while (out < end)
138                        CurrentTime);                  *(out++) = colmap[*(data++)];
139  }  }
140    
141  static void  static uint8 *
142  xwin_ungrab_keyboard()  translate_image(int width, int height, uint8 *data)
143    {
144            int size = width * height * bpp/8;
145            uint8 *out = xmalloc(size);
146            uint8 *end = out + size;
147    
148            switch (bpp)
149            {
150                    case 8:
151                            translate8(data, out, end);
152                            break;
153    
154                    case 16:
155                            translate16(data, (uint16 *)out, (uint16 *)end);
156                            break;
157    
158                    case 24:
159                            translate24(data, out, end);
160                            break;
161    
162                    case 32:
163                            translate32(data, (uint32 *)out, (uint32 *)end);
164                            break;
165            }
166    
167            return out;
168    }
169    
170    #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
171    #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }
172    #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
173                            x = (x << 16) | (x >> 16); }
174    
175    static uint32
176    translate_colour(uint32 colour)
177  {  {
178          XUngrabKeyboard(display, CurrentTime);          switch (bpp)
179            {
180                    case 16:
181                            if (host_be != xserver_be)
182                                    BSWAP16(colour);
183                            break;
184    
185                    case 24:
186                            if (xserver_be)
187                                    BSWAP24(colour);
188                            break;
189    
190                    case 32:
191                            if (host_be != xserver_be)
192                                    BSWAP32(colour);
193                            break;
194            }
195    
196            return colour;
197  }  }
198    
199  BOOL  BOOL
# Line 97  ui_create_window(char *title) Line 204  ui_create_window(char *title)
204          XSizeHints *sizehints;          XSizeHints *sizehints;
205          unsigned long input_mask;          unsigned long input_mask;
206          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
207          int count;          Screen *screen;
208            uint16 test;
209            int i;
210            
211            int xkb_minor, xkb_major;
212            int xkb_event, xkb_error, xkb_reason;
213    
214            /* compare compiletime libs with runtime libs. */
215            xkb_major = XkbMajorVersion;
216            xkb_minor = XkbMinorVersion;
217            if( XkbLibraryVersion( &xkb_major, &xkb_minor ) == False )
218            {
219                    error("please re-compile rdesktop\ncompile time version of xkb is not compatible with\nyour runtime version of the library\n");
220                    return False;
221            }
222    
223    
224            /* XKB is the 'new' keyboard handler in x.. ( the xkb code in Xfree86 originates from SGI, years 1993 and 1995 from what I could tell. )
225             * it makes it possible for people with disabilities to use rdesktop, stickykeys, bouncekeys etc. VERY MUCH useful.
226             * XFree86 has had support for it since it's earliest incarnation. I believe it is a reasonable dependency.
227             */
228            display = XkbOpenDisplay( NULL, &xkb_event, &xkb_error, &xkb_major, &xkb_minor, &xkb_reason );
229            switch(xkb_reason)
230            {
231                    case XkbOD_BadLibraryVersion:
232                            error("XkbOD_BadLibraryVersion: XKB extensions in server and the library rdesktop is linked against aren't compatible with each other.\n");
233                            break;
234                    case XkbOD_ConnectionRefused:
235                            error("XkbOD_ConnectionRefused\n");
236                            break;
237                    case XkbOD_BadServerVersion:
238                            error("XkbOD_BadServerVersion\n");
239                            break;
240                    case XkbOD_NonXkbServer:
241                            error("XkbOD_NonXkbServer: XKB extension not present in server\nupdate your X server.\n");
242                            break;
243                    case XkbOD_Success:
244                            DEBUG("XkbOD_Success: Connection established with display\n");
245                            break;
246            }
247    
         display = XOpenDisplay(NULL);  
248          if (display == NULL)          if (display == NULL)
249          {          {
250                  ERROR("Failed to open display\n");                  error("Failed to open display\n");
251                  return False;                  return False;
252          }          }
253    
254          visual = DefaultVisual(display, DefaultScreen(display));          x_socket = ConnectionNumber(display);
255          depth = DefaultDepth(display, DefaultScreen(display));          screen = DefaultScreenOfDisplay(display);
256          pfm = XListPixmapFormats(display, &count);          visual = DefaultVisualOfScreen(screen);
257            depth = DefaultDepthOfScreen(screen);
258            
259            pfm = XListPixmapFormats(display, &i);
260          if (pfm != NULL)          if (pfm != NULL)
261          {          {
262                  while (count--)                  /* Use maximum bpp for this depth - this is generally
263                       desirable, e.g. 24 bits->32 bits. */
264                    while (i--)
265                  {                  {
266                          if ((pfm + count)->depth == depth                          if ((pfm[i].depth == depth)
267                              && (pfm + count)->bits_per_pixel > bpp)                              && (pfm[i].bits_per_pixel > bpp))
268                          {                          {
269                                  bpp = (pfm + count)->bits_per_pixel;                                  bpp = pfm[i].bits_per_pixel;
270                          }                          }
271                  }                  }
272                  XFree(pfm);                  XFree(pfm);
# Line 124  ui_create_window(char *title) Line 274  ui_create_window(char *title)
274    
275          if (bpp < 8)          if (bpp < 8)
276          {          {
277                  ERROR("Less than 8 bpp not currently supported.\n");                  error("Less than 8 bpp not currently supported.\n");
278                  XCloseDisplay(display);                  XCloseDisplay(display);
279                  return False;                  return False;
280          }          }
281    
282          width &= ~3; /* make width nicely divisible */          if (depth <= 8)
283                    owncolmap = True;
284            else
285                    xcolmap = DefaultColormapOfScreen(screen);
286    
287            test = 1;
288            host_be = !(BOOL)(*(uint8 *)(&test));
289            xserver_be = (ImageByteOrder(display) == MSBFirst);
290    
291            white = WhitePixelOfScreen(screen);
292            attribs.background_pixel = BlackPixelOfScreen(screen);
293            attribs.backing_store = DoesBackingStore(screen);
294    
295          attribs.background_pixel = BlackPixel(display, DefaultScreen(display));          if (attribs.backing_store == NotUseful)
296          attribs.backing_store = Always;                  ownbackstore = True;
297    
298          if (fullscreen)          if (fullscreen)
299          {          {
300                  attribs.override_redirect = True;                  attribs.override_redirect = True;
301                  width = WidthOfScreen(DefaultScreenOfDisplay(display));                  width = WidthOfScreen(screen);
302                  height = HeightOfScreen(DefaultScreenOfDisplay(display));                  height = HeightOfScreen(screen);
                 XSetInputFocus(display, PointerRoot, RevertToPointerRoot,  
                                CurrentTime);  
303          }          }
304          else          else
305          {          {
306                  attribs.override_redirect = False;                  attribs.override_redirect = False;
307          }          }
308    
309          wnd = XCreateWindow(display, DefaultRootWindow(display),          width = (width + 3) & ~3; /* make width a multiple of 32 bits */
310    
311            wnd = XCreateWindow(display, RootWindowOfScreen(screen),
312                              0, 0, width, height, 0, CopyFromParent,                              0, 0, width, height, 0, CopyFromParent,
313                              InputOutput, CopyFromParent,                              InputOutput, CopyFromParent,
314                              CWBackingStore | CWBackPixel | CWOverrideRedirect,                              CWBackingStore | CWBackPixel | CWOverrideRedirect,
# Line 157  ui_create_window(char *title) Line 318  ui_create_window(char *title)
318    
319          classhints = XAllocClassHint();          classhints = XAllocClassHint();
320          if (classhints != NULL)          if (classhints != NULL)
   
321          {          {
322                  classhints->res_name = "rdesktop";                  classhints->res_name = classhints->res_class = "rdesktop";
                 classhints->res_class = "rdesktop";  
323                  XSetClassHint(display, wnd, classhints);                  XSetClassHint(display, wnd, classhints);
324                  XFree(classhints);                  XFree(classhints);
325          }          }
# Line 168  ui_create_window(char *title) Line 327  ui_create_window(char *title)
327          sizehints = XAllocSizeHints();          sizehints = XAllocSizeHints();
328          if (sizehints)          if (sizehints)
329          {          {
330                  sizehints->flags =                  sizehints->flags = PMinSize | PMaxSize;
331                          PPosition | PSize | PMinSize | PMaxSize | PBaseSize;                  sizehints->min_width = sizehints->max_width = width;
332                  sizehints->min_width = width;                  sizehints->min_height = sizehints->max_height = height;
                 sizehints->max_width = width;  
                 sizehints->min_height = height;  
                 sizehints->max_height = height;  
                 sizehints->base_width = width;  
                 sizehints->base_height = height;  
333                  XSetWMNormalHints(display, wnd, sizehints);                  XSetWMNormalHints(display, wnd, sizehints);
334                  XFree(sizehints);                  XFree(sizehints);
335          }          }
336    
337          input_mask = KeyPressMask | KeyReleaseMask;          xkeymap_init();
338          input_mask |= ButtonPressMask | ButtonReleaseMask;  
339          if (motion)          input_mask = KeyPressMask | KeyReleaseMask |
340                             ButtonPressMask | ButtonReleaseMask |
341                             EnterWindowMask | LeaveWindowMask | KeymapStateMask;
342            if (sendmotion)
343                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
344          if (grab_keyboard)  
345                  input_mask |= EnterWindowMask | LeaveWindowMask;          if (ownbackstore)
346                    input_mask |= ExposureMask;
347    
348          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
349          gc = XCreateGC(display, wnd, 0, NULL);          gc = XCreateGC(display, wnd, 0, NULL);
350    
351            if (ownbackstore)
352                    backstore = XCreatePixmap(display, wnd, width, height, depth);
353    
354          XMapWindow(display, wnd);          XMapWindow(display, wnd);
355    
356            /* TODO: error texts... make them friendly. */
357            xkb = XkbGetKeyboard(display, XkbAllComponentsMask, XkbUseCoreKbd);
358            if ((int)xkb == BadAlloc || xkb == NULL)
359            {
360                            error( "XkbGetKeyboard failed.\n");
361                            exit(0);
362            }
363    
364            /* TODO: error texts... make them friendly. */
365            if( XkbSelectEvents(display, xkb->device_spec, XkbAllEventsMask, XkbAllEventsMask) == False )
366            {
367                            error( "XkbSelectEvents failed.\n");
368                            exit(0);
369            }
370    
371          return True;          return True;
372  }  }
373    
374  void  void
375  ui_destroy_window()  ui_destroy_window()
376  {  {
377            if( xkb != NULL )
378                    XkbFreeKeyboard(xkb, XkbAllControlsMask, True);
379    
380            if (ownbackstore)
381                    XFreePixmap(display, backstore);
382    
383          XFreeGC(display, gc);          XFreeGC(display, gc);
384          XDestroyWindow(display, wnd);          XDestroyWindow(display, wnd);
385          XCloseDisplay(display);          XCloseDisplay(display);
386          display = NULL;          display = NULL;
387  }  }
388    
389  static uint8  static void
390  xwin_translate_key(unsigned long key)  xwin_process_events()
 {  
         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()  
391  {  {
392          XEvent event;          XkbEvent xkbevent;
393            
394            KeySym keysym;
395          uint8 scancode;          uint8 scancode;
396          uint16 button;          uint16 button, flags;
397          uint32 ev_time;          uint32 ev_time;
398            uint32 tmpmods;
399    
400          if (display == NULL)          while (XCheckMaskEvent(display, ~0, &xkbevent.core))
                 return;  
   
         while (XCheckWindowEvent(display, wnd, ~0, &event))  
401          {          {
402                  ev_time = time(NULL);                  ev_time = time(NULL);
403                    flags = 0;
404    
405                  switch (event.type)                  switch (xkbevent.type)
406                  {                  {
407                          case KeyPress:                          case KeymapNotify:
408                                  scancode = xwin_translate_key(event.xkey.keycode);                                  /* TODO:
409                                  if (scancode == 0)                                   * read modifier status at focus in, and update the local masks, and the other end as well..
410                                          break;                                   * if not, we may get out of sync.
411                                     * xkbevent.core.xkeymap.key_vector
412                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0,                                   * char key_vector[32];
413                                                 scancode, 0);                                   */
414                                  break;                                  break;
415    
416                          case KeyRelease:                          case KeyRelease:
417                                  scancode = xwin_translate_key(event.xkey.keycode);                                  flags = KBD_FLAG_DOWN | KBD_FLAG_UP;
418                                  if (scancode == 0)                                  /* fall through */
                                         break;  
419    
420                                  rdp_send_input(ev_time, RDP_INPUT_SCANCODE,                          case KeyPress:
421                                                 KBD_FLAG_DOWN | KBD_FLAG_UP,                                  if( XkbTranslateKeyCode(xkb, xkbevent.core.xkey.keycode, xkbevent.core.xkey.state, &tmpmods, &keysym) == False )
422                                                 scancode, 0);                                          break;
423                                  break;                                  scancode = xkeymap_translate_key(keysym, xkbevent.core.xkey.keycode, &flags);
424    
425                          case ButtonPress:                                  if (scancode == 0 )
                                 button = xwin_translate_mouse(event.xbutton.button);  
                                 if (button == 0)  
426                                          break;                                          break;
427    
428                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  /* keep track of the modifiers -- needed for stickykeys... */
429                                                 button | MOUSE_FLAG_DOWN,                                  if( xkbevent.type == KeyPress )
430                                                 event.xbutton.x,                                          xwin_press_modifiers( &xkbevent.core.xkey, ev_time, scancode );
431                                                 event.xbutton.y);  
432                                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, flags, scancode, 0);
433    
434                                    if( xkbevent.type == KeyRelease )
435                                            xwin_release_modifiers( &xkbevent.core.xkey, ev_time, scancode );
436    
437                                  break;                                  break;
438    
439                            case ButtonPress:
440                                    flags = MOUSE_FLAG_DOWN;
441                                    /* fall through */
442    
443                          case ButtonRelease:                          case ButtonRelease:
444                                  button = xwin_translate_mouse(event.xbutton.button);                                  button = xkeymap_translate_button(xkbevent.core.xbutton.button);
445                                  if (button == 0)                                  if (button == 0)
446                                          break;                                          break;
447    
448                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,
449                                                 button,                                                 flags | button,
450                                                 event.xbutton.x,                                                 xkbevent.core.xbutton.x,
451                                                 event.xbutton.y);                                                 xkbevent.core.xbutton.y);
452                                  break;                                  break;
453    
454                          case MotionNotify:                          case MotionNotify:
455                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,
456                                                 MOUSE_FLAG_MOVE,                                                 MOUSE_FLAG_MOVE,
457                                                 event.xmotion.x,                                                 xkbevent.core.xmotion.x,
458                                                 event.xmotion.y);                                                 xkbevent.core.xmotion.y);
459                                  break;                                  break;
460    
461                          case EnterNotify:                          case EnterNotify:
462                                  if (grab_keyboard)                                  XGrabKeyboard(display, wnd, True, GrabModeAsync,
463                                          xwin_grab_keyboard();                                                GrabModeAsync, CurrentTime);
464                                  break;                                  break;
465    
466                          case LeaveNotify:                          case LeaveNotify:
467                                  if (grab_keyboard)                                  XUngrabKeyboard(display, CurrentTime);
468                                          xwin_ungrab_keyboard();                                  break;
469    
470                            case Expose:
471                                    XCopyArea(display, backstore, wnd, gc,
472                                              xkbevent.core.xexpose.x, xkbevent.core.xexpose.y,
473                                              xkbevent.core.xexpose.width, xkbevent.core.xexpose.height,
474                                              xkbevent.core.xexpose.x, xkbevent.core.xexpose.y);
475                                  break;                                  break;
476                  }                  }
477          }          }
478  }  }
479    
480  void  void
481    xwin_release_modifiers(XKeyEvent* ev, uint32 ev_time, uint32 scancode)
482    {
483            switch (scancode) {
484            case 0x2a:
485                    key_down_state &= ~DShift1Mask;
486                    break;
487            case 0x36:
488                    key_down_state &= ~DShift2Mask;
489                    break;
490            case 0x1d:
491                    key_down_state &= ~DControl1Mask;
492                    break;
493            case 0x9d:
494                    key_down_state &= ~DControl2Mask;
495                    break;
496            case 0x38:
497                    key_down_state &= ~DMod1Mask;
498                    break;
499            case 0xb8:
500                    key_down_state &= ~DMod2Mask;
501                    break;
502            }
503    
504            if( !(ShiftMask & ev->state) && (key_down_state & DShift1Mask))
505            {
506                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0x2a, 0);
507                    key_down_state &= ~DShift1Mask;
508    
509            }
510    
511            if( !(ControlMask & ev->state) && (key_down_state & DControl1Mask))
512            {
513                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0x1d, 0);
514                    key_down_state &= ~DControl1Mask;
515    
516            }
517            
518            if( !(Mod1Mask & ev->state) && (key_down_state & DMod1Mask))
519            {
520                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0x38, 0);
521                    key_down_state &= ~DMod1Mask;
522    
523            }
524            
525            if( !(Mod2Mask & ev->state) && (key_down_state & DMod2Mask))
526            {
527                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0xb8, 0);
528                    key_down_state &= ~DMod2Mask;
529            }
530    }
531    
532    
533    void
534    xwin_press_modifiers(XKeyEvent* ev, uint32 ev_time, uint32 scancode)
535    {
536            key_modifier_state = ev->state;
537    
538            switch (scancode) {
539            case 0x2a:
540                    key_down_state |= DShift1Mask;
541                    break;
542            case 0x36:
543                    key_down_state |= DShift2Mask;
544                    break;
545            case 0x1d:
546                    key_down_state |= DControl1Mask;
547                    break;
548            case 0x9d:
549                    key_down_state |= DControl2Mask;
550                    break;
551            case 0x38:
552                    key_down_state |= DMod1Mask;
553                    break;
554            case 0xb8:
555                    key_down_state |= DMod2Mask;
556                    break;
557            }
558    
559            if( (ShiftMask & ev->state) && !((key_down_state & DShift1Mask) || (key_down_state & DShift2Mask)))
560            {
561                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN, 0x2a, 0);
562                    key_down_state |= DShift1Mask;
563    
564            }
565    
566            if( (ControlMask & ev->state) && !((key_down_state & DControl1Mask) || (key_down_state & DControl2Mask)))
567            {
568                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN, 0x1d, 0);
569                    key_down_state |= DControl1Mask;
570    
571            }
572    
573            if( (Mod1Mask & ev->state) && !(key_down_state & DMod1Mask))
574            {
575                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN, 0x38, 0);
576                    key_down_state |= DMod1Mask;
577    
578            }
579    
580            if( (Mod2Mask & ev->state) && !(key_down_state & DMod2Mask))
581            {
582                    rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN, 0xb8, 0);
583                    key_down_state |= DMod2Mask;
584    
585            }
586    }
587    
588    void
589    ui_select(int rdp_socket)
590    {
591            int n = (rdp_socket > x_socket) ? rdp_socket+1 : x_socket+1;
592            fd_set rfds;
593    
594            FD_ZERO(&rfds);
595    
596            while (True)
597            {
598                    FD_ZERO(&rfds);
599                    FD_SET(rdp_socket, &rfds);
600                    if (display != NULL)
601                    {
602                            FD_SET(x_socket, &rfds);
603                            XFlush(display);
604                    }
605    
606                    switch (select(n, &rfds, NULL, NULL, NULL))
607                    {
608                            case -1:
609                                    error("select: %s\n", strerror(errno));
610    
611                            case 0:
612                                    continue;
613                    }
614    
615                    if (FD_ISSET(x_socket, &rfds))
616                            xwin_process_events();
617    
618                    if (FD_ISSET(rdp_socket, &rfds))
619                            return;
620            }
621    }
622    
623    void
624  ui_move_pointer(int x, int y)  ui_move_pointer(int x, int y)
625  {  {
626          XWarpPointer(display, wnd, wnd, 0, 0, 0, 0, x, y);          XWarpPointer(display, wnd, wnd, 0, 0, 0, 0, x, y);
# Line 362  ui_create_bitmap(int width, int height, Line 632  ui_create_bitmap(int width, int height,
632          XImage *image;          XImage *image;
633          Pixmap bitmap;          Pixmap bitmap;
634          uint8 *tdata;          uint8 *tdata;
635          tdata = (private_colormap ? data : translate(width, height, data));  
636            tdata = (owncolmap ? data : translate_image(width, height, data));
637          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(display, wnd, width, height, depth);
638          image =          image = XCreateImage(display, visual, depth, ZPixmap,
639                  XCreateImage(display, visual,                               0, tdata, width, height, 8, 0);
                              depth, ZPixmap,  
                              0, tdata, width, height, BitmapPad(display), 0);  
640    
         xwin_set_function(ROP2_COPY);  
641          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
642    
643          XFree(image);          XFree(image);
644          if (!private_colormap)          if (!owncolmap)
645                  xfree(tdata);                  xfree(tdata);
646          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
647  }  }
# Line 383  ui_paint_bitmap(int x, int y, int cx, in Line 651  ui_paint_bitmap(int x, int y, int cx, in
651                  int width, int height, uint8 *data)                  int width, int height, uint8 *data)
652  {  {
653          XImage *image;          XImage *image;
654          uint8 *tdata =          uint8 *tdata;
                 (private_colormap ? data : translate(width, height, data));  
         image =  
                 XCreateImage(display, visual, depth, ZPixmap, 0, tdata, width,  
                              height, BitmapPad(display), 0);  
   
         xwin_set_function(ROP2_COPY);  
   
         /* Window */  
         XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);  
         XFree(image);  
         if (!private_colormap)  
                 xfree(tdata);  
 }  
   
 void  
 ui_destroy_bitmap(HBITMAP bmp)  
 {  
         XFreePixmap(display, (Pixmap) bmp);  
 }  
655    
656  HCURSOR          tdata = (owncolmap ? data : translate_image(width, height, data));
657  ui_create_cursor(unsigned int x, unsigned int y, int width,          image = XCreateImage(display, visual, depth, ZPixmap,
658                   int height, uint8 *mask, uint8 *data)                               0, tdata, width, height, 8, 0);
 {  
         XImage *imagecursor;  
         XImage *imagemask;  
         Pixmap maskbitmap, cursorbitmap;  
         Cursor cursor;  
         XColor bg, fg;  
         GC lgc;  
         int i, x1, y1, scanlinelen;  
         uint8 *cdata, *cmask;  
         uint8 c;  
         cdata = (uint8 *) malloc(sizeof(uint8) * width * height);  
         if (!cdata)  
                 return NULL;  
         scanlinelen = (width + 7) >> 3;  
         cmask = (uint8 *) malloc(sizeof(uint8) * scanlinelen * height);  
         if (!cmask)  
         {  
                 free(cdata);  
                 return NULL;  
         }  
         i = (height - 1) * scanlinelen;  
659    
660          if (!screen_msbfirst)          if (ownbackstore)
661          {          {
662                  while (i >= 0)                  XPutImage(display, backstore, gc, image, 0, 0, x, y, cx, cy);
663                  {                  XCopyArea(display, backstore, wnd, gc, x, y, cx, cy, x, y);
                         for (x1 = 0; x1 < scanlinelen; x1++)  
                         {  
                                 c = *(mask++);  
                                 cmask[i + x1] =  
                                         ((c & 0x1) << 7) | ((c & 0x2) << 5) |  
                                         ((c & 0x4) << 3) | ((c & 0x8) << 1) |  
                                         ((c & 0x10) >> 1) | ((c & 0x20) >> 3)  
                                         | ((c & 0x40) >> 5) | ((c & 0x80) >>  
                                                                7);  
                         }  
                         i -= scanlinelen;  
                 }  
664          }          }
665          else          else
666          {          {
667                  while (i >= 0)                  XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);
                 {  
                         for (x1 = 0; x1 < scanlinelen; x1++)  
                         {  
                                 cmask[i + x1] = *(mask++);  
                         }  
                         i -= scanlinelen;  
                 }  
668          }          }
669    
670            XFree(image);
671          fg.red = 0;          if (!owncolmap)
672          fg.blue = 0;                  xfree(tdata);
         fg.green = 0;  
         fg.flags = DoRed | DoBlue | DoGreen;  
         bg.red = 65535;  
         bg.blue = 65535;  
         bg.green = 65535;  
         bg.flags = DoRed | DoBlue | DoGreen;  
         maskbitmap = XCreatePixmap(display, wnd, width, height, 1);  
         cursorbitmap = XCreatePixmap(display, wnd, width, height, 1);  
         lgc = XCreateGC(display, maskbitmap, 0, NULL);  
         XSetFunction(display, lgc, GXcopy);  
         imagemask =  
                 XCreateImage(display, visual, 1, XYBitmap, 0, cmask, width,  
                              height, 8, 0);  
         imagecursor =  
                 XCreateImage(display, visual, 1, XYBitmap, 0, cdata, width,  
                              height, 8, 0);  
         for (y1 = height - 1; y1 >= 0; y1--)  
                 for (x1 = 0; x1 < width; x1++)  
                 {  
                         if (data[0] >= 0x80 || data[1] >= 0x80  
                             || data[2] >= 0x80)  
                                 if (XGetPixel(imagemask, x1, y1))  
   
                                 {  
                                         XPutPixel(imagecursor, x1, y1, 0);  
                                         XPutPixel(imagemask, x1, y1, 0);        /* mask is blank for text cursor! */  
                                 }  
   
                                 else  
                                         XPutPixel(imagecursor, x1, y1, 1);  
   
                         else  
                                 XPutPixel(imagecursor, x1, y1,  
                                           XGetPixel(imagemask, x1, y1));  
                         data += 3;  
                 }  
         XPutImage(display, maskbitmap, lgc, imagemask, 0, 0, 0, 0, width,  
                   height);  
         XPutImage(display, cursorbitmap, lgc, imagecursor, 0, 0, 0, 0, width,  
                   height); XFree(imagemask);  
         XFree(imagecursor);  
         free(cmask);  
         free(cdata);  
         XFreeGC(display, lgc);  
         cursor =  
                 XCreatePixmapCursor(display, cursorbitmap, maskbitmap, &fg,  
                                     &bg, x, y);  
         XFreePixmap(display, maskbitmap);  
         XFreePixmap(display, cursorbitmap);  
         return (HCURSOR) cursor;  
 }  
   
 void  
 ui_set_cursor(HCURSOR cursor)  
 {  
         XDefineCursor(display, wnd, (Cursor) cursor);  
673  }  }
674    
675  void  void
676  ui_destroy_cursor(HCURSOR cursor)  ui_destroy_bitmap(HBITMAP bmp)
677  {  {
678          XFreeCursor(display, (Cursor) cursor);          XFreePixmap(display, (Pixmap)bmp);
679  }  }
680    
681  HGLYPH  HGLYPH
# Line 544  ui_create_glyph(int width, int height, u Line 697  ui_create_glyph(int width, int height, u
697          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
698          XInitImage(image);          XInitImage(image);
699    
         XSetFunction(display, gc, GXcopy);  
700          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
701    
702          XFree(image);          XFree(image);
703          XFreeGC(display, gc);          XFreeGC(display, gc);
704            return (HGLYPH)bitmap;
         return (HGLYPH) bitmap;  
705  }  }
706    
707  void  void
708  ui_destroy_glyph(HGLYPH glyph)  ui_destroy_glyph(HGLYPH glyph)
709  {  {
710          XFreePixmap(display, (Pixmap) glyph);          XFreePixmap(display, (Pixmap)glyph);
711  }  }
712    
713  HCOLOURMAP  HCURSOR
714  ui_create_colourmap(COLOURMAP *colours)  ui_create_cursor(unsigned int x, unsigned int y, int width,
715                     int height, uint8 *andmask, uint8 *xormask)
716  {  {
717          if (!private_colormap)          HGLYPH maskglyph, cursorglyph;
718            XColor bg, fg;
719            Cursor xcursor;
720            uint8 *cursor, *pcursor;
721            uint8 *mask, *pmask;
722            uint8 nextbit;
723            int scanline, offset;
724            int i, j;
725    
726            scanline = (width + 7) / 8;
727            offset = scanline * height;
728    
729            cursor = xmalloc(offset);
730            memset(cursor, 0, offset);
731    
732            mask = xmalloc(offset);
733            memset(mask, 0, offset);
734    
735            /* approximate AND and XOR masks with a monochrome X pointer */
736            for (i = 0; i < height; i++)
737          {          {
738                  COLOURENTRY *entry;                  offset -= scanline;
739                  int i, ncolours = colours->ncolours;                  pcursor = &cursor[offset];
740                  uint32 *nc = xmalloc(sizeof(*colmap) * ncolours);                  pmask = &mask[offset];
741                  for (i = 0; i < ncolours; i++)  
742                    for (j = 0; j < scanline; j++)
743                  {                  {
744                          XColor xc;                          for (nextbit = 0x80; nextbit != 0; nextbit >>= 1)
745                          entry = &colours->colours[i];                          {
746                          xc.red = entry->red << 8;                                  if (xormask[0] || xormask[1] || xormask[2])
747                          xc.green = entry->green << 8;                                  {
748                          xc.blue = entry->blue << 8;                                          *pcursor |= (~(*andmask) & nextbit);
749                          XAllocColor(display,                                          *pmask |= nextbit;
750                                      DefaultColormap(display,                                  }
751                                                      DefaultScreen(display)),                                  else
752                                      &xc);                                  {
753                          /* XXX Check return value */                                          *pcursor |= ((*andmask) & nextbit);
754                          nc[i] = xc.pixel;                                          *pmask |= (~(*andmask) & nextbit);
755                                    }
756    
757                                    xormask += 3;
758                            }
759    
760                            andmask++;
761                            pcursor++;
762                            pmask++;
763                  }                  }
                 return nc;  
764          }          }
765          else  
766            fg.red = fg.blue = fg.green = 0xffff;
767            bg.red = bg.blue = bg.green = 0x0000;
768            fg.flags = bg.flags = DoRed | DoBlue | DoGreen;
769    
770            cursorglyph = ui_create_glyph(width, height, cursor);
771            maskglyph = ui_create_glyph(width, height, mask);
772            
773            xcursor = XCreatePixmapCursor(display, (Pixmap)cursorglyph,
774                                    (Pixmap)maskglyph, &fg, &bg, x, y);
775    
776            ui_destroy_glyph(maskglyph);
777            ui_destroy_glyph(cursorglyph);
778            xfree(mask);
779            xfree(cursor);
780            return (HCURSOR)xcursor;
781    }
782    
783    void
784    ui_set_cursor(HCURSOR cursor)
785    {
786            XDefineCursor(display, wnd, (Cursor)cursor);
787    }
788    
789    void
790    ui_destroy_cursor(HCURSOR cursor)
791    {
792            XFreeCursor(display, (Cursor)cursor);
793    }
794    
795    #define MAKE_XCOLOR(xc,c) \
796                    (xc)->red   = ((c)->red   << 8) | (c)->red; \
797                    (xc)->green = ((c)->green << 8) | (c)->green; \
798                    (xc)->blue  = ((c)->blue  << 8) | (c)->blue; \
799                    (xc)->flags = DoRed | DoGreen | DoBlue;
800    
801    HCOLOURMAP
802    ui_create_colourmap(COLOURMAP *colours)
803    {
804            COLOURENTRY *entry;
805            int i, ncolours = colours->ncolours;
806    
807            if (owncolmap)
808          {          {
                 COLOURENTRY *entry;  
809                  XColor *xcolours, *xentry;                  XColor *xcolours, *xentry;
810                  Colormap map;                  Colormap map;
811                  int i, ncolours = colours->ncolours;  
812                  xcolours = xmalloc(sizeof(XColor) * ncolours);                  xcolours = xmalloc(sizeof(XColor) * ncolours);
813                  for (i = 0; i < ncolours; i++)                  for (i = 0; i < ncolours; i++)
814                  {                  {
815                          entry = &colours->colours[i];                          entry = &colours->colours[i];
816                          xentry = &xcolours[i];                          xentry = &xcolours[i];
   
817                          xentry->pixel = i;                          xentry->pixel = i;
818                          xentry->red = entry->red << 8;                          MAKE_XCOLOR(xentry, entry);
                         xentry->blue = entry->blue << 8;  
                         xentry->green = entry->green << 8;  
                         xentry->flags = DoRed | DoBlue | DoGreen;  
819                  }                  }
820    
821                  map = XCreateColormap(display, wnd, visual, AllocAll);                  map = XCreateColormap(display, wnd, visual, AllocAll);
822                  XStoreColors(display, map, xcolours, ncolours);                  XStoreColors(display, map, xcolours, ncolours);
823    
824                  xfree(xcolours);                  xfree(xcolours);
825                  return (HCOLOURMAP) map;                  return (HCOLOURMAP)map;
826            }
827            else
828            {
829                    uint32 *map = xmalloc(sizeof(*colmap) * ncolours);
830                    XColor xentry;
831                    uint32 colour;
832    
833                    for (i = 0; i < ncolours; i++)
834                    {
835                            entry = &colours->colours[i];
836                            MAKE_XCOLOR(&xentry, entry);
837    
838                            if (XAllocColor(display, xcolmap, &xentry) != 0)
839                                    colour = xentry.pixel;
840                            else
841                                    colour = white;
842    
843                            /* byte swap here to make translate_image faster */
844                            map[i] = translate_colour(colour);
845                    }
846    
847                    return map;
848          }          }
849  }  }
850    
851  void  void
852  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
853  {  {
854          XFreeColormap(display, (Colormap) map);          if (owncolmap)
855                    XFreeColormap(display, (Colormap)map);
856            else
857                    xfree(map);
858  }  }
859    
860  void  void
861  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
862  {  {
863            if (owncolmap)
864          /* XXX, change values of all pixels on the screen if the new colmap                  XSetWindowColormap(display, wnd, (Colormap)map);
          * doesn't have the same values as the old one? */  
         if (!private_colormap)  
                 colmap = map;  
865          else          else
866          {                  colmap = map;
                 XSetWindowColormap(display, wnd, (Colormap) map);  
                 if (fullscreen)  
                         XInstallColormap(display, (Colormap) map);  
         }  
867  }  }
868    
869  void  void
# Line 665  void Line 900  void
900  ui_destblt(uint8 opcode,  ui_destblt(uint8 opcode,
901             /* dest */ int x, int y, int cx, int cy)             /* dest */ int x, int y, int cx, int cy)
902  {  {
903          xwin_set_function(opcode);          SET_FUNCTION(opcode);
904            FILL_RECTANGLE(x, y, cx, cy);
905          XFillRectangle(display, wnd, gc, x, y, cx, cy);          RESET_FUNCTION(opcode);
906  }  }
907    
908  void  void
# Line 675  ui_patblt(uint8 opcode, Line 910  ui_patblt(uint8 opcode,
910            /* dest */ int x, int y, int cx, int cy,            /* dest */ int x, int y, int cx, int cy,
911            /* brush */ BRUSH *brush, int bgcolour, int fgcolour)            /* brush */ BRUSH *brush, int bgcolour, int fgcolour)
912  {  {
         Display *dpy = display;  
913          Pixmap fill;          Pixmap fill;
         uint8 i, ipattern[8];  
914    
915          xwin_set_function(opcode);          SET_FUNCTION(opcode);
916    
917          switch (brush->style)          switch (brush->style)
918          {          {
919                  case 0: /* Solid */                  case 0: /* Solid */
920                          XSetForeground(dpy, gc, Ctrans(fgcolour));                          SET_FOREGROUND(fgcolour);
921                          XFillRectangle(dpy, wnd, gc, x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
922                          break;                          break;
923    
924                  case 3: /* Pattern */                  case 3: /* Pattern */
925                          for (i = 0; i != 8; i++)                          fill = (Pixmap)ui_create_glyph(8, 8, brush->pattern);
                                 ipattern[i] = ~brush->pattern[i];  
                         fill = (Pixmap) ui_create_glyph(8, 8, ipattern);  
   
                         XSetForeground(dpy, gc, Ctrans(fgcolour));  
                         XSetBackground(dpy, gc, Ctrans(bgcolour));  
                         XSetFillStyle(dpy, gc, FillOpaqueStippled);  
                         XSetStipple(dpy, gc, fill);  
926    
927                          XFillRectangle(dpy, wnd, gc, x, y, cx, cy);                          SET_FOREGROUND(bgcolour);
928                            SET_BACKGROUND(fgcolour);
929                            XSetFillStyle(display, gc, FillOpaqueStippled);
930                            XSetStipple(display, gc, fill);
931                            XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin);
932    
933                            FILL_RECTANGLE(x, y, cx, cy);
934    
935                          XSetFillStyle(dpy, gc, FillSolid);                          XSetFillStyle(display, gc, FillSolid);
936                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH)fill);
937                          break;                          break;
938    
939                  default:                  default:
940                          NOTIMP("brush %d\n", brush->style);                          unimpl("brush %d\n", brush->style);
941          }          }
942    
943            RESET_FUNCTION(opcode);
944  }  }
945    
946  void  void
# Line 714  ui_screenblt(uint8 opcode, Line 948  ui_screenblt(uint8 opcode,
948               /* dest */ int x, int y, int cx, int cy,               /* dest */ int x, int y, int cx, int cy,
949               /* src */ int srcx, int srcy)               /* src */ int srcx, int srcy)
950  {  {
951          xwin_set_function(opcode);          SET_FUNCTION(opcode);
   
952          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);
953            if (ownbackstore)
954                    XCopyArea(display, backstore, backstore, gc, srcx, srcy,
955                              cx, cy, x, y);
956            RESET_FUNCTION(opcode);
957  }  }
958    
959  void  void
# Line 724  ui_memblt(uint8 opcode, Line 961  ui_memblt(uint8 opcode,
961            /* dest */ int x, int y, int cx, int cy,            /* dest */ int x, int y, int cx, int cy,
962            /* src */ HBITMAP src, int srcx, int srcy)            /* src */ HBITMAP src, int srcx, int srcy)
963  {  {
964          xwin_set_function(opcode);          SET_FUNCTION(opcode);
965            XCopyArea(display, (Pixmap)src, wnd, gc, srcx, srcy, cx, cy, x, y);
966          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);          if (ownbackstore)
967                    XCopyArea(display, (Pixmap)src, backstore, gc, srcx, srcy,
968                              cx, cy, x, y);
969            RESET_FUNCTION(opcode);
970  }  }
971    
972  void  void
# Line 754  ui_triblt(uint8 opcode, Line 994  ui_triblt(uint8 opcode,
994                                    brush, bgcolour, fgcolour);                                    brush, bgcolour, fgcolour);
995                          break;                          break;
996    
997                  case 0xc0:                  case 0xc0:      /* PSa */
998                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
999                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour,
1000                                    fgcolour);                                    fgcolour);
1001                          break;                          break;
1002    
1003                  default:                  default:
1004                          NOTIMP("triblt 0x%x\n", opcode);                          unimpl("triblt 0x%x\n", opcode);
1005                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
1006          }          }
1007  }  }
# Line 771  ui_line(uint8 opcode, Line 1011  ui_line(uint8 opcode,
1011          /* dest */ int startx, int starty, int endx, int endy,          /* dest */ int startx, int starty, int endx, int endy,
1012          /* pen */ PEN *pen)          /* pen */ PEN *pen)
1013  {  {
1014          xwin_set_function(opcode);          SET_FUNCTION(opcode);
1015            SET_FOREGROUND(pen->colour);
         XSetForeground(display, gc, Ctrans(pen->colour));  
1016          XDrawLine(display, wnd, gc, startx, starty, endx, endy);          XDrawLine(display, wnd, gc, startx, starty, endx, endy);
1017            if (ownbackstore)
1018                    XDrawLine(display, backstore, gc, startx, starty, endx, endy);
1019            RESET_FUNCTION(opcode);
1020  }  }
1021    
1022  void  void
# Line 782  ui_rect( Line 1024  ui_rect(
1024                 /* dest */ int x, int y, int cx, int cy,                 /* dest */ int x, int y, int cx, int cy,
1025                 /* brush */ int colour)                 /* brush */ int colour)
1026  {  {
1027          xwin_set_function(ROP2_COPY);          SET_FOREGROUND(colour);
1028            FILL_RECTANGLE(x, y, cx, cy);
         XSetForeground(display, gc, Ctrans(colour));  
         XFillRectangle(display, wnd, gc, x, y, cx, cy);  
1029  }  }
1030    
1031  void  void
# Line 794  ui_draw_glyph(int mixmode, Line 1034  ui_draw_glyph(int mixmode,
1034                /* src */ HGLYPH glyph, int srcx, int srcy, int bgcolour,                /* src */ HGLYPH glyph, int srcx, int srcy, int bgcolour,
1035                int fgcolour)                int fgcolour)
1036  {  {
1037          Pixmap pixmap = (Pixmap) glyph;          SET_FOREGROUND(fgcolour);
1038            SET_BACKGROUND(bgcolour);
         xwin_set_function(ROP2_COPY);  
1039    
1040            XSetFillStyle(display, gc, (mixmode == MIX_TRANSPARENT)
1041          XSetForeground(display, gc, Ctrans(fgcolour));                        ? FillStippled : FillOpaqueStippled);
1042          switch (mixmode)          XSetStipple(display, gc, (Pixmap)glyph);
1043          {          XSetTSOrigin(display, gc, x, y);
1044                  case MIX_TRANSPARENT:  
1045                          XSetStipple(display, gc, pixmap);          FILL_RECTANGLE(x, y, cx, cy);
1046                          XSetFillStyle(display, gc, FillStippled);  
1047                          XSetTSOrigin(display, gc, x, y);          XSetFillStyle(display, gc, FillSolid);
1048                          XFillRectangle(display, wnd, gc, x, y, cx, cy);  }
1049                          XSetFillStyle(display, gc, FillSolid);  
1050                          break;  #define DO_GLYPH(ttext,idx) \
1051    {\
1052                  case MIX_OPAQUE:    glyph = cache_get_font (font, ttext[idx]);\
1053                          XSetBackground(display, gc, Ctrans(bgcolour));    if (!(flags & TEXT2_IMPLICIT_X))\
1054  /*      XCopyPlane (display, pixmap, back_pixmap, back_gc, srcx, srcy, cx, cy, x, y, 1); */      {\
1055                          XSetStipple(display, gc, pixmap);        xyoffset = ttext[++idx];\
1056                          XSetFillStyle(display, gc, FillOpaqueStippled);        if ((xyoffset & 0x80))\
1057                          XSetTSOrigin(display, gc, x, y);          {\
1058                          XFillRectangle(display, wnd, gc, x, y, cx, cy);            if (flags & TEXT2_VERTICAL) \
1059                          XSetFillStyle(display, gc, FillSolid);              y += ttext[++idx] | (ttext[++idx] << 8);\
1060                          break;            else\
1061                x += ttext[++idx] | (ttext[++idx] << 8);\
1062                  default:          }\
1063                          NOTIMP("mix %d\n", mixmode);        else\
1064          }          {\
1065              if (flags & TEXT2_VERTICAL) \
1066                y += xyoffset;\
1067              else\
1068                x += xyoffset;\
1069            }\
1070        }\
1071      if (glyph != NULL)\
1072        {\
1073          ui_draw_glyph (mixmode, x + (short) glyph->offset,\
1074                         y + (short) glyph->baseline,\
1075                         glyph->width, glyph->height,\
1076                         glyph->pixmap, 0, 0, bgcolour, fgcolour);\
1077          if (flags & TEXT2_IMPLICIT_X)\
1078            x += glyph->width;\
1079        }\
1080  }  }
1081    
1082  void  void
1083  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,
1084               int clipx, int clipy, int clipcx, int clipcy,               int clipx, int clipy, int clipcx, int clipcy, int boxx,
1085               int boxx, int boxy, int boxcx, int boxcy,               int boxy, int boxcx, int boxcy, int bgcolour,
1086               int bgcolour, int fgcolour, uint8 *text, uint8 length)               int fgcolour, uint8 * text, uint8 length)
1087  {  {
1088          FONTGLYPH *glyph;          FONTGLYPH *glyph;
1089          int i, xyoffset;          int i, j, xyoffset;
1090            DATABLOB *entry;
1091    
1092          xwin_set_function(ROP2_COPY);          SET_FOREGROUND(bgcolour);
         XSetForeground(display, gc, Ctrans(bgcolour));  
1093    
1094          if (boxcx > 1)          if (boxcx > 1)
1095                  XFillRectangle(display, wnd, gc, boxx, boxy, boxcx, boxcy);          {
1096                    FILL_RECTANGLE(boxx, boxy, boxcx, boxcy);
1097            }
1098          else if (mixmode == MIX_OPAQUE)          else if (mixmode == MIX_OPAQUE)
                 XFillRectangle(display, wnd, gc, clipx, clipy, clipcx, clipcy);  
   
         /* Paint text, character by character */  
         for (i = 0; i < length; i++)  
1099          {          {
1100                  glyph = cache_get_font(font, text[i]);                  FILL_RECTANGLE(clipx, clipy, clipcx, clipcy);
1101            }
                 if (!(flags & TEXT2_IMPLICIT_X))  
1102    
1103                  {          /* Paint text, character by character */
1104                          xyoffset = text[++i];          for (i = 0; i < length;) {
1105                          if ((xyoffset & 0x80))                  switch (text[i]) {
1106                          {                  case 0xff:
1107                                  if (flags & 0x04)       /* vertical text */                          if (i + 2 < length)
1108                                          y += text[++i] | (text[++i] << 8);                                  cache_put_text(text[i + 1], text, text[i + 2]);
1109                                  else                          else {
1110                                          x += text[++i] | (text[++i] << 8);                                  error("this shouldn't be happening\n");
1111                                    break;
1112                          }                          }
1113                          else                          /* this will move pointer from start to first character after FF command */
1114                          {                          length -= i + 3;
1115                                  if (flags & 0x04)       /* vertical text */                          text = &(text[i + 3]);
1116                                          y += xyoffset;                          i = 0;
1117                            break;
1118    
1119                    case 0xfe:
1120                            entry = cache_get_text(text[i + 1]);
1121                            if (entry != NULL) {
1122                                    if ((((uint8 *) (entry->data))[1] == 0)
1123                                        && (!(flags & TEXT2_IMPLICIT_X))) {
1124                                            if (flags & TEXT2_VERTICAL)
1125                                                    y += text[i + 2];
1126                                            else
1127                                                    x += text[i + 2];
1128                                    }
1129                                    if (i + 2 < length)
1130                                            i += 3;
1131                                  else                                  else
1132                                          x += xyoffset;                                          i += 2;
1133                                    length -= i;
1134                                    /* this will move pointer from start to first character after FE command */
1135                                    text = &(text[i]);
1136                                    i = 0;
1137                                    for (j = 0; j < entry->size; j++)
1138                                            DO_GLYPH(((uint8 *) (entry->data)), j);
1139                          }                          }
1140                            break;
1141    
1142                  }                  default:
1143                  if (glyph != NULL)                          DO_GLYPH(text, i);
1144                  {                          i++;
1145                          ui_draw_glyph(mixmode, x + (short) glyph->offset,                          break;
                                       y + (short) glyph->baseline,  
                                       glyph->width, glyph->height,  
                                       glyph->pixmap, 0, 0,  
                                       bgcolour, fgcolour);  
   
                         if (flags & TEXT2_IMPLICIT_X)  
                                 x += glyph->width;  
1146                  }                  }
1147          }          }
1148    
1149    
1150  }  }
1151    
1152  void  void
# Line 887  ui_desktop_save(uint32 offset, int x, in Line 1155  ui_desktop_save(uint32 offset, int x, in
1155          Pixmap pix;          Pixmap pix;
1156          XImage *image;          XImage *image;
1157    
1158          pix = XCreatePixmap(display, wnd, cx, cy, depth);          if (ownbackstore)
1159          xwin_set_function(ROP2_COPY);          {
1160                    image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes,
1161          XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);                                    ZPixmap);
1162          image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);          }
1163            else
1164            {
1165                    pix = XCreatePixmap(display, wnd, cx, cy, depth);
1166                    XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);
1167                    image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes,
1168                                      ZPixmap);
1169                    XFreePixmap(display, pix);
1170            }
1171    
1172          offset *= bpp/8;          offset *= bpp/8;
1173          cache_put_desktop(offset, cx, cy, image->bytes_per_line,          cache_put_desktop(offset, cx, cy, image->bytes_per_line,
1174                            bpp/8, image->data);                            bpp/8, (uint8 *)image->data);
1175    
1176          XDestroyImage(image);          XDestroyImage(image);
         XFreePixmap(display, pix);  
1177  }  }
1178    
1179  void  void
# Line 911  ui_desktop_restore(uint32 offset, int x, Line 1186  ui_desktop_restore(uint32 offset, int x,
1186          data = cache_get_desktop(offset, cx, cy, bpp/8);          data = cache_get_desktop(offset, cx, cy, bpp/8);
1187          if (data == NULL)          if (data == NULL)
1188                  return;                  return;
1189          image =  
1190                  XCreateImage(display, visual,          image = XCreateImage(display, visual, depth, ZPixmap,
                              depth, ZPixmap,  
1191                               0, data, cx, cy, BitmapPad(display),                               0, data, cx, cy, BitmapPad(display),
1192                               cx * bpp/8);                               cx * bpp/8);
         xwin_set_function(ROP2_COPY);  
         XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);  
         XFree(image);  
 }  
1193    
1194  /* unroll defines, used to make the loops a bit more readable... */          if (ownbackstore)
 #define unroll8Expr(uexp) uexp uexp uexp uexp uexp uexp uexp uexp  
 #define unroll8Lefts(uexp) case 7: uexp \  
         case 6: uexp \  
         case 5: uexp \  
         case 4: uexp \  
         case 3: uexp \  
         case 2: uexp \  
         case 1: uexp  
   
 static uint8 *  
 translate(int width, int height, uint8 *data)  
 {  
         uint32 i;  
         uint32 size = width * height;  
         uint8 *d2 = xmalloc(size * bpp/8);  
         uint8 *d3 = d2;  
         uint32 pix;  
         i = (size & ~0x7);  
   
         /* XXX: where are the bits swapped??? */  
 #ifdef L_ENDIAN                 /* little-endian */  
         /* big-endian screen */  
         if (screen_msbfirst)  
1195          {          {
1196                  switch (bpp)                  XPutImage(display, backstore, gc, image, 0, 0, x, y, cx, cy);
1197                  {                  XCopyArea(display, backstore, wnd, 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;  
                 }  
1198          }          }
1199          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)  
1200          {          {
1201                  /* big-endian screen */                  XPutImage(display, wnd, 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;)}  
                 }  
1202          }          }
         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  
1203    
1204          return d2;          XFree(image);
1205  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26