/[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 64 by astrand, Thu Jul 18 16:38:31 2002 UTC revision 164 by astrand, Mon Sep 16 13:19:54 2002 UTC
# Line 20  Line 20 
20    
21  #include <X11/Xlib.h>  #include <X11/Xlib.h>
22  #include <X11/Xutil.h>  #include <X11/Xutil.h>
 #include <X11/XKBlib.h>  
23  #include <time.h>  #include <time.h>
24  #include <errno.h>  #include <errno.h>
25    #define XK_MISCELLANY
26    #include <X11/keysymdef.h>
27  #include "rdesktop.h"  #include "rdesktop.h"
28    #include "scancodes.h"
29    
 extern char keymapname[16];  
 extern int keylayout;  
30  extern int width;  extern int width;
31  extern int height;  extern int height;
32  extern BOOL sendmotion;  extern BOOL sendmotion;
33  extern BOOL fullscreen;  extern BOOL fullscreen;
34    extern BOOL grab_keyboard;
35    extern char title[];
36    
37  Display *display;  Display *display;
 XkbDescPtr xkb;  
38  static int x_socket;  static int x_socket;
39    static Screen *screen;
40  static Window wnd;  static Window wnd;
41  static GC gc;  static GC gc;
42  static Visual *visual;  static Visual *visual;
# Line 49  static BOOL xserver_be; Line 51  static BOOL xserver_be;
51  static BOOL ownbackstore;  static BOOL ownbackstore;
52  static Pixmap backstore;  static Pixmap backstore;
53    
 /* needed to keep track of the modifiers */  
 static unsigned int numlock_modifier_mask = 0;  
 static unsigned int key_down_state = 0;  
   
   
 #define DShift1Mask   (1<<0)  
 #define DLockMask     (1<<1)  
 #define DControl1Mask (1<<2)  
 #define DMod1Mask     (1<<3)  
 #define DMod2Mask     (1<<4)  
 #define DMod3Mask     (1<<5)  
 #define DMod4Mask     (1<<6)  
 #define DMod5Mask     (1<<7)  
 #define DShift2Mask   (1<<8)  
 #define DControl2Mask (1<<9)  
 #define DNumLockMask  (1<<10)  
   
54  #define FILL_RECTANGLE(x,y,cx,cy)\  #define FILL_RECTANGLE(x,y,cx,cy)\
55  { \  { \
56          XFillRectangle(display, wnd, gc, x, y, cx, cy); \          XFillRectangle(display, wnd, gc, x, y, cx, cy); \
# Line 76  static unsigned int key_down_state = 0; Line 61  static unsigned int key_down_state = 0;
61  /* colour maps */  /* colour maps */
62  static BOOL owncolmap;  static BOOL owncolmap;
63  static Colormap xcolmap;  static Colormap xcolmap;
 static uint32 white;  
64  static uint32 *colmap;  static uint32 *colmap;
65    
66    /* Compose support */
67    BOOL enable_compose = False;
68    static XIM IM = NULL;
69    static XIC IC = NULL;
70    
71    /* toggle fullscreen globals */
72    static unsigned long input_mask;
73    
74  #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )  #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )
75  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));
76  #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));  #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));
# Line 105  static int rop2_map[] = { Line 97  static int rop2_map[] = {
97  #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(display, gc, rop2_map[rop2]); }  #define SET_FUNCTION(rop2)      { if (rop2 != ROP2_COPY) XSetFunction(display, gc, rop2_map[rop2]); }
98  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }
99    
 void xwin_get_numlock_mask();  
 void xwin_mod_update(uint32 state, uint32 ev_time);  
 void xwin_mod_release(uint32 state, uint32 ev_time, uint32 scancode);  
 void xwin_mod_press(uint32 state, uint32 ev_time, uint32 scancode);  
   
100  static void  static void
101  translate8(uint8 * data, uint8 * out, uint8 * end)  translate8(uint8 * data, uint8 * out, uint8 * end)
102  {  {
# Line 204  translate_colour(uint32 colour) Line 191  translate_colour(uint32 colour)
191          return colour;          return colour;
192  }  }
193    
194  BOOL  static unsigned long
195  ui_create_window(char *title)  init_inputmethod(void)
196  {  {
197          XSetWindowAttributes attribs;          unsigned long filtered_events = 0;
         XClassHint *classhints;  
         XSizeHints *sizehints;  
         unsigned long input_mask;  
         XPixmapFormatValues *pfm;  
         Screen *screen;  
         uint16 test;  
         int i;  
198    
199          int xkb_minor, xkb_major;          IM = XOpenIM(display, NULL, NULL, NULL);
200          int xkb_event, xkb_error, xkb_reason;          if (IM == NULL)
201            {
202                    error("Failed to open input method\n");
203            }
204    
205          /* compare compiletime libs with runtime libs. */          if (IM != NULL)
         xkb_major = XkbMajorVersion;  
         xkb_minor = XkbMinorVersion;  
         if (XkbLibraryVersion(&xkb_major, &xkb_minor) == False)  
206          {          {
207                  error("please re-compile rdesktop\ncompile time version of xkb is not compatible with\nyour runtime version of the library\n");                  /* Must be done after XCreateWindow */
208                  return False;                  IC = XCreateIC(IM, XNInputStyle,
209                                   (XIMPreeditNothing | XIMStatusNothing),
210                                   XNClientWindow, wnd, XNFocusWindow, wnd, NULL);
211    
212                    if (IC == NULL)
213                    {
214                            error("Failed to create input context\n");
215                            XCloseIM(IM);
216                            IM = NULL;
217                    }
218          }          }
219    
220            /* For correct Multi_key/Compose processing, I guess.
221               It seems to work alright anyway, though. */
222            if (IC != NULL)
223            {
224                    if (XGetICValues(IC, XNFilterEvents, &filtered_events, NULL) != NULL)
225                    {
226                            error("Failed to obtain XNFilterEvents value from IC\n");
227                            filtered_events = 0;
228                    }
229            }
230            return filtered_events;
231    }
232    
233          display =  static void
234                  XkbOpenDisplay(NULL, &xkb_event, &xkb_error, &xkb_major,  close_inputmethod(void)
235                                 &xkb_minor, &xkb_reason);  {
236          switch (xkb_reason)          if (IC != NULL)
237          {          {
238                  case XkbOD_BadLibraryVersion:                  XDestroyIC(IC);
239                          error("XkbOD_BadLibraryVersion: XKB extensions in server and the library rdesktop is linked against aren't compatible with each other.\n");                  if (IM != NULL)
240                          break;                  {
241                  case XkbOD_ConnectionRefused:                          XCloseIM(IM);
242                          error("XkbOD_ConnectionRefused\n");                          IM = NULL;
243                          break;                  }
                 case XkbOD_BadServerVersion:  
                         error("XkbOD_BadServerVersion\n");  
                         break;  
                 case XkbOD_NonXkbServer:  
                         error("XkbOD_NonXkbServer: XKB extension not present in server\nupdate your X server.\n");  
                         break;  
                 case XkbOD_Success:  
                         DEBUG("XkbOD_Success: Connection established with display\n");  
                         break;  
244          }          }
245    }
246    
247    BOOL
248    get_key_state(int keysym)
249    {
250            int keysymMask = 0, modifierpos, key;
251            Window wDummy1, wDummy2;
252            int iDummy3, iDummy4, iDummy5, iDummy6;
253            unsigned int current_state;
254            int offset;
255    
256            XModifierKeymap *map = XGetModifierMapping(display);
257            KeyCode keycode = XKeysymToKeycode(display, keysym);
258    
259            if (keycode == NoSymbol)
260                    return False;
261    
262            for (modifierpos = 0; modifierpos < 8; modifierpos++)
263            {
264                    offset = map->max_keypermod * modifierpos;
265    
266                    for (key = 0; key < map->max_keypermod; key++)
267                    {
268                            if (map->modifiermap[offset + key] == keycode)
269                                    keysymMask = 1 << modifierpos;
270                    }
271            }
272    
273            XQueryPointer(display, DefaultRootWindow(display), &wDummy1,
274                          &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, &current_state);
275    
276            XFreeModifiermap(map);
277    
278            return (current_state & keysymMask) ? True : False;
279    }
280    
281    static void
282    xwin_map_window()
283    {
284            XEvent xevent;
285    
286            XMapWindow(display, wnd);
287    
288            /* wait for VisibilityChange */
289            XMaskEvent(display, VisibilityChangeMask, &xevent);
290    
291            if (fullscreen)
292                    XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);
293    }
294    
295    BOOL
296    ui_init()
297    {
298            XPixmapFormatValues *pfm;
299            uint16 test;
300            int i;
301    
302            display = XOpenDisplay(NULL);
303          if (display == NULL)          if (display == NULL)
304          {          {
305                  error("Failed to open display\n");                  error("Failed to open display\n");
# Line 269  ui_create_window(char *title) Line 318  ui_create_window(char *title)
318                     desirable, e.g. 24 bits->32 bits. */                     desirable, e.g. 24 bits->32 bits. */
319                  while (i--)                  while (i--)
320                  {                  {
321                          if ((pfm[i].depth == depth)                          if ((pfm[i].depth == depth) && (pfm[i].bits_per_pixel > bpp))
                             && (pfm[i].bits_per_pixel > bpp))  
322                          {                          {
323                                  bpp = pfm[i].bits_per_pixel;                                  bpp = pfm[i].bits_per_pixel;
324                          }                          }
# Line 290  ui_create_window(char *title) Line 338  ui_create_window(char *title)
338          else          else
339                  xcolmap = DefaultColormapOfScreen(screen);                  xcolmap = DefaultColormapOfScreen(screen);
340    
341            if (DoesBackingStore(screen) == NotUseful)
342                    ownbackstore = True;
343    
344          test = 1;          test = 1;
345          host_be = !(BOOL) (*(uint8 *) (&test));          host_be = !(BOOL) (*(uint8 *) (&test));
346          xserver_be = (ImageByteOrder(display) == MSBFirst);          xserver_be = (ImageByteOrder(display) == MSBFirst);
347    
         white = WhitePixelOfScreen(screen);  
         attribs.background_pixel = BlackPixelOfScreen(screen);  
         attribs.backing_store = DoesBackingStore(screen);  
   
         if (attribs.backing_store == NotUseful)  
                 ownbackstore = True;  
   
348          if (fullscreen)          if (fullscreen)
349          {          {
                 attribs.override_redirect = True;  
350                  width = WidthOfScreen(screen);                  width = WidthOfScreen(screen);
351                  height = HeightOfScreen(screen);                  height = HeightOfScreen(screen);
352          }          }
         else  
         {  
                 attribs.override_redirect = False;  
         }  
353    
354          width = (width + 3) & ~3;       /* make width a multiple of 32 bits */          /* make sure width is a multiple of 4 */
355            width = (width + 3) & ~3;
356    
357          wnd = XCreateWindow(display, RootWindowOfScreen(screen),          xkeymap_init();
358                              0, 0, width, height, 0, CopyFromParent,          return True;
359                              InputOutput, CopyFromParent,  }
360                              CWBackingStore | CWBackPixel | CWOverrideRedirect,  
361                              &attribs);  BOOL
362    ui_create_window()
363    {
364            XSetWindowAttributes attribs;
365            XClassHint *classhints;
366            XSizeHints *sizehints;
367            XEvent xevent;
368    
369            attribs.background_pixel = BlackPixelOfScreen(screen);
370            attribs.backing_store = ownbackstore ? NotUseful : Always;
371            attribs.override_redirect = fullscreen;
372            wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height,
373                                0, CopyFromParent, InputOutput, CopyFromParent,
374                                CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
375    
376            if (ownbackstore)
377                    backstore = XCreatePixmap(display, wnd, width, height, depth);
378    
379          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
380    
# Line 340  ui_create_window(char *title) Line 396  ui_create_window(char *title)
396                  XFree(sizehints);                  XFree(sizehints);
397          }          }
398    
399          xkeymap_init();          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
400                    VisibilityChangeMask | FocusChangeMask;
401    
402          input_mask = KeyPressMask | KeyReleaseMask |          if (grab_keyboard)
403                  ButtonPressMask | ButtonReleaseMask |                  input_mask |= EnterWindowMask | LeaveWindowMask;
                 EnterWindowMask | LeaveWindowMask;  
404          if (sendmotion)          if (sendmotion)
405                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
   
406          if (ownbackstore)          if (ownbackstore)
407                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
408            if (enable_compose)
409                    input_mask |= init_inputmethod();
410    
411          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
         gc = XCreateGC(display, wnd, 0, NULL);  
   
         if (ownbackstore)  
                 backstore = XCreatePixmap(display, wnd, width, height, depth);  
412    
413          XMapWindow(display, wnd);          xwin_map_window();
   
         /* TODO: error texts... make them friendly. */  
         xkb = XkbGetKeyboard(display, XkbAllComponentsMask, XkbUseCoreKbd);  
         if ((int) xkb == BadAlloc || xkb == NULL)  
         {  
                 error("XkbGetKeyboard failed.\n");  
                 exit(0);  
         }  
414    
415          /* TODO: error texts... make them friendly. */          /* clear the window so that cached data is not seen */
416          if (XkbSelectEvents          gc = XCreateGC(display, wnd, 0, NULL);
417              (display, xkb->device_spec, XkbAllEventsMask,          XSetForeground(display, gc, 0);
418               XkbAllEventsMask) == False)          FILL_RECTANGLE(0, 0, width, height);
         {  
                 error("XkbSelectEvents failed.\n");  
                 exit(0);  
         }  
   
         xwin_get_numlock_mask();  
419    
420          return True;          return True;
421  }  }
422    
423  void  void
424  xwin_get_numlock_mask()  ui_destroy_window()
425  {  {
426          KeyCode numlockcode;          if (ownbackstore)
427          KeyCode *keycode;                  XFreePixmap(display, backstore);
         XModifierKeymap *modmap;  
         int i, j;  
428    
429          /* Find out if numlock is already defined as a modifier key, and if so where */          XFreeGC(display, gc);
         numlockcode = XKeysymToKeycode(display, 0xFF7F);        /* XF_Num_Lock = 0xFF7F */  
         if (numlockcode)  
         {  
                 modmap = XGetModifierMapping(display);  
                 if (modmap)  
                 {  
                         keycode = modmap->modifiermap;  
                         for (i = 0; i < 8; i++)  
                                 for (j = modmap->max_keypermod; j--;)  
                                 {  
                                         if (*keycode == numlockcode)  
                                         {  
                                                 numlock_modifier_mask =  
                                                         (1 << i);  
                                                 i = 8;  
                                                 break;  
                                         }  
                                         keycode++;  
                                 }  
                         if (!numlock_modifier_mask)  
                         {  
                                 modmap->modifiermap[7 *  
                                                     modmap->max_keypermod] =  
                                         numlockcode;  
                                 if (XSetModifierMapping(display, modmap) ==  
                                     MappingSuccess)  
                                         numlock_modifier_mask = (1 << 7);  
                                 else  
                                         printf("XSetModifierMapping failed!\n");  
                         }  
                         XFreeModifiermap(modmap);  
                 }  
         }  
430    
431          if (!numlock_modifier_mask)          close_inputmethod();
                 printf("WARNING: Failed to get a numlock modifier mapping.\n");  
432    
433            XDestroyWindow(display, wnd);
434            XCloseDisplay(display);
435            display = NULL;
436    }
437    
438    static void
439    xwin_reset_keys()
440    {
441            /* reset keys */
442            uint32 ev_time;
443            ev_time = time(NULL);
444            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);
445            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);
446            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);
447            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL);
448            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
449            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);
450  }  }
451    
452  void  void
453  ui_destroy_window()  xwin_toggle_fullscreen()
454  {  {
455          if (xkb != NULL)          XEvent xevent;
456                  XkbFreeKeyboard(xkb, XkbAllControlsMask, True);          XSetWindowAttributes attribs;
457            int newwidth, newheight;
458    
459          if (ownbackstore)          fullscreen = !fullscreen;
460                  XFreePixmap(display, backstore);          newwidth = fullscreen ? WidthOfScreen(screen) : width;
461            newheight = fullscreen ? HeightOfScreen(screen) : height;
462    
463          XFreeGC(display, gc);          XUnmapWindow(display, wnd);
464          XDestroyWindow(display, wnd);          attribs.override_redirect = fullscreen;
465          XCloseDisplay(display);          XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight);
466          display = NULL;          XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs);
467            xwin_map_window();
468  }  }
469    
470    /* Process all events in Xlib queue */
471  static void  static void
472  xwin_process_events()  xwin_process_events()
473  {  {
474          XEvent xevent;          XEvent xevent;
   
475          KeySym keysym;          KeySym keysym;
         uint8 scancode;  
476          uint16 button, flags;          uint16 button, flags;
477          uint32 ev_time;          uint32 ev_time;
478          uint32 tmpmods;          key_translation tr;
479            char *ksname = NULL;
480            char str[256];
481            Status status;
482    
483          while (XCheckMaskEvent(display, ~0, &xevent))          while (XPending(display) > 0)
484          {          {
485                    XNextEvent(display, &xevent);
486    
487                    if (enable_compose && (XFilterEvent(&xevent, None) == True))
488                    {
489                            DEBUG_KBD(("Filtering event\n"));
490                            continue;
491                    }
492    
493                  ev_time = time(NULL);                  ev_time = time(NULL);
494                  flags = 0;                  flags = 0;
495    
496                  switch (xevent.type)                  switch (xevent.type)
497                  {                  {
                         case KeyRelease:  
                                 flags = KBD_FLAG_DOWN | KBD_FLAG_UP;  
                                 /* fall through */  
498                          case KeyPress:                          case KeyPress:
499                                  if (XkbTranslateKeyCode                                  if (IC != NULL)
500                                      (xkb, xevent.xkey.keycode,                                          /* Multi_key compatible version */
501                                       xevent.xkey.state, &tmpmods,                                  {
502                                       &keysym) == False)                                          XmbLookupString(IC,
503                                                            (XKeyPressedEvent *) &
504                                                            xevent, str, sizeof(str), &keysym, &status);
505                                            if (!((status == XLookupKeySym) || (status == XLookupBoth)))
506                                            {
507                                                    error("XmbLookupString failed with status 0x%x\n",
508                                                          status);
509                                                    break;
510                                            }
511                                    }
512                                    else
513                                    {
514                                            /* Plain old XLookupString */
515                                            DEBUG_KBD(("No input context, using XLookupString\n"));
516                                            XLookupString((XKeyEvent *) & xevent,
517                                                          str, sizeof(str), &keysym, NULL);
518                                    }
519    
520                                    ksname = get_ksname(keysym);
521                                    DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));
522    
523                                    if (handle_special_keys(keysym, ev_time, True))
524                                          break;                                          break;
                                 scancode =  
                                         xkeymap_translate_key(keysym,  
                                                               xevent.xkey.  
                                                               keycode,  
                                                               &flags);  
525    
526                                  if (scancode == 0)                                  tr = xkeymap_translate_key(keysym,
527                                                               xevent.xkey.keycode, xevent.xkey.state);
528    
529                                    if (tr.scancode == 0)
530                                          break;                                          break;
531    
532                                  /* keep track of the modifiers -- needed for stickykeys... */                                  ensure_remote_modifiers(ev_time, tr);
                                 if (xevent.type == KeyPress)  
                                         xwin_mod_press(xevent.xkey.state,  
                                                        ev_time, scancode);  
   
                                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                                flags, scancode, 0);  
   
                                 if (xevent.type == KeyRelease)  
                                         xwin_mod_release(xevent.xkey.state,  
                                                          ev_time, scancode);  
533    
534                                    rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
535                                    break;
536                            case KeyRelease:
537                                    XLookupString((XKeyEvent *) & xevent, str,
538                                                  sizeof(str), &keysym, NULL);
539    
540                                    ksname = get_ksname(keysym);
541                                    DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
542                                               ksname));
543    
544                                    if (handle_special_keys(keysym, ev_time, False))
545                                            break;
546    
547                                    tr = xkeymap_translate_key(keysym,
548                                                               xevent.xkey.keycode, xevent.xkey.state);
549    
550                                    if (tr.scancode == 0)
551                                            break;
552    
553                                    rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode);
554                                  break;                                  break;
555    
556                          case ButtonPress:                          case ButtonPress:
# Line 499  xwin_process_events() Line 558  xwin_process_events()
558                                  /* fall through */                                  /* fall through */
559    
560                          case ButtonRelease:                          case ButtonRelease:
561                                  button = xkeymap_translate_button(xevent.                                  button = xkeymap_translate_button(xevent.xbutton.button);
                                                                   xbutton.  
                                                                   button);  
562                                  if (button == 0)                                  if (button == 0)
563                                          break;                                          break;
564    
565                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,
566                                                 flags | button,                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);
                                                xevent.xbutton.x,  
                                                xevent.xbutton.y);  
567                                  break;                                  break;
568    
569                          case MotionNotify:                          case MotionNotify:
570                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,
571                                                 MOUSE_FLAG_MOVE,                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
                                                xevent.xmotion.x,  
                                                xevent.xmotion.y);  
572                                  break;                                  break;
573    
574                          case EnterNotify:                          case EnterNotify:
575                                  XGrabKeyboard(display, wnd, True,                                  if (grab_keyboard)
576                                                GrabModeAsync, GrabModeAsync,                                          XGrabKeyboard(display, wnd, True,
577                                                CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
   
                                 xwin_mod_update(xevent.xcrossing.state,  
                                                 ev_time);  
578                                  break;                                  break;
579    
580                          case LeaveNotify:                          case LeaveNotify:
581                                  XUngrabKeyboard(display, CurrentTime);                                  if (grab_keyboard)
582                                            XUngrabKeyboard(display, CurrentTime);
583                                    break;
584    
585                            case FocusOut:
586                                    xwin_reset_keys();
587                                  break;                                  break;
588    
589                          case Expose:                          case Expose:
# Line 538  xwin_process_events() Line 593  xwin_process_events()
593                                            xevent.xexpose.height,                                            xevent.xexpose.height,
594                                            xevent.xexpose.x, xevent.xexpose.y);                                            xevent.xexpose.x, xevent.xexpose.y);
595                                  break;                                  break;
                 }  
         }  
 }  
   
 void  
 xwin_mod_update(uint32 state, uint32 ev_time)  
 {  
         xwin_mod_press(state, ev_time, 0);  
         xwin_mod_release(state, ev_time, 0);  
 }  
596    
597  void                          case MappingNotify:
598  xwin_mod_release(uint32 state, uint32 ev_time, uint32 scancode)                                  /* Refresh keyboard mapping if it has changed. This is important for
599  {                                     Xvnc, since it allocates keycodes dynamically */
600          switch (scancode)                                  if (xevent.xmapping.request == MappingKeyboard
601          {                                      || xevent.xmapping.request == MappingModifier)
602                  case 0x2a:                                          XRefreshKeyboardMapping(&xevent.xmapping);
603                          key_down_state &= ~DShift1Mask;                                  break;
                         break;  
                 case 0x36:  
                         key_down_state &= ~DShift2Mask;  
                         break;  
                 case 0x1d:  
                         key_down_state &= ~DControl1Mask;  
                         break;  
                 case 0x9d:  
                         key_down_state &= ~DControl2Mask;  
                         break;  
                 case 0x38:  
                         key_down_state &= ~DMod1Mask;  
                         break;  
                 case 0xb8:  
                         key_down_state &= ~DMod2Mask;  
                         break;  
         }  
   
         if (!(numlock_modifier_mask & state)  
             && (key_down_state & DNumLockMask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0, 0x45, 0);  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                KBD_FLAG_DOWN | KBD_FLAG_UP, 0x45, 0);  
                 key_down_state &= ~DNumLockMask;  
         }  
   
         if (!(LockMask & state) && (key_down_state & DLockMask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0, 0x3a, 0);  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                KBD_FLAG_DOWN | KBD_FLAG_UP, 0x3a, 0);  
                 key_down_state &= ~DLockMask;  
   
         }  
   
   
         if (!(ShiftMask & state) && (key_down_state & DShift1Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0x2a,  
                                0);  
                 key_down_state &= ~DShift1Mask;  
   
         }  
   
         if (!(ControlMask & state) && (key_down_state & DControl1Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0x1d,  
                                0);  
                 key_down_state &= ~DControl1Mask;  
   
         }  
   
         if (!(Mod1Mask & state) && (key_down_state & DMod1Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0x38,  
                                0);  
                 key_down_state &= ~DMod1Mask;  
   
         }  
   
         if (!(Mod2Mask & state) && (key_down_state & DMod2Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_UP, 0xb8,  
                                0);  
                 key_down_state &= ~DMod2Mask;  
         }  
 }  
   
   
 void  
 xwin_mod_press(uint32 state, uint32 ev_time, uint32 scancode)  
 {  
   
         switch (scancode)  
         {  
                 case 0x2a:  
                         key_down_state |= DShift1Mask;  
                         break;  
                 case 0x36:  
                         key_down_state |= DShift2Mask;  
                         break;  
                 case 0x1d:  
                         key_down_state |= DControl1Mask;  
                         break;  
                 case 0x9d:  
                         key_down_state |= DControl2Mask;  
                         break;  
                 case 0x3a:  
                         key_down_state ^= DLockMask;  
                         break;  
                 case 0x45:  
                         key_down_state ^= DNumLockMask;  
                         break;  
                 case 0x38:  
                         key_down_state |= DMod1Mask;  
                         break;  
                 case 0xb8:  
                         key_down_state |= DMod2Mask;  
                         break;  
         }  
   
         if ((numlock_modifier_mask && state)  
             && !(key_down_state & DNumLockMask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0, 0x45, 0);  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                KBD_FLAG_DOWN | KBD_FLAG_UP, 0x45, 0);  
                 key_down_state |= DNumLockMask;  
         }  
   
         if ((LockMask & state) && !(key_down_state & DLockMask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, 0, 0x3a, 0);  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                KBD_FLAG_DOWN | KBD_FLAG_UP, 0x3a, 0);  
                 key_down_state |= DLockMask;  
   
         }  
   
   
         if ((ShiftMask & state)  
             && !((key_down_state & DShift1Mask)  
                  || (key_down_state & DShift2Mask)))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN,  
                                0x2a, 0);  
                 key_down_state |= DShift1Mask;  
   
         }  
   
         if ((ControlMask & state)  
             && !((key_down_state & DControl1Mask)  
                  || (key_down_state & DControl2Mask)))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN,  
                                0x1d, 0);  
                 key_down_state |= DControl1Mask;  
   
         }  
   
         if ((Mod1Mask & state) && !(key_down_state & DMod1Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN,  
                                0x38, 0);  
                 key_down_state |= DMod1Mask;  
   
         }  
   
         if ((Mod2Mask & state) && !(key_down_state & DMod2Mask))  
         {  
                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE, KBD_FLAG_DOWN,  
                                0xb8, 0);  
                 key_down_state |= DMod2Mask;  
604    
605                    }
606          }          }
607  }  }
608    
# Line 724  ui_select(int rdp_socket) Line 616  ui_select(int rdp_socket)
616    
617          while (True)          while (True)
618          {          {
619                    /* Process any events already waiting */
620                    xwin_process_events();
621    
622                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
623                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
624                  if (display != NULL)                  FD_SET(x_socket, &rfds);
                 {  
                         FD_SET(x_socket, &rfds);  
                         XFlush(display);  
                 }  
625    
626                  switch (select(n, &rfds, NULL, NULL, NULL))                  switch (select(n, &rfds, NULL, NULL, NULL))
627                  {                  {
# Line 741  ui_select(int rdp_socket) Line 632  ui_select(int rdp_socket)
632                                  continue;                                  continue;
633                  }                  }
634    
                 if (FD_ISSET(x_socket, &rfds))  
                         xwin_process_events();  
   
635                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
636                          return;                          return;
637          }          }
# Line 764  ui_create_bitmap(int width, int height, Line 652  ui_create_bitmap(int width, int height,
652    
653          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = (owncolmap ? data : translate_image(width, height, data));
654          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(display, wnd, width, height, depth);
655          image = XCreateImage(display, visual, depth, ZPixmap,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
656                               0, tdata, width, height, 8, 0);                               (char *) tdata, width, height, 8, 0);
657    
658          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
659    
# Line 776  ui_create_bitmap(int width, int height, Line 664  ui_create_bitmap(int width, int height,
664  }  }
665    
666  void  void
667  ui_paint_bitmap(int x, int y, int cx, int cy,  ui_paint_bitmap(int x, int y, int cx, int cy, int width, int height, uint8 * data)
                 int width, int height, uint8 * data)  
668  {  {
669          XImage *image;          XImage *image;
670          uint8 *tdata;          uint8 *tdata;
671    
672          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = (owncolmap ? data : translate_image(width, height, data));
673          image = XCreateImage(display, visual, depth, ZPixmap,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
674                               0, tdata, width, height, 8, 0);                               (char *) tdata, width, height, 8, 0);
675    
676          if (ownbackstore)          if (ownbackstore)
677          {          {
# Line 820  ui_create_glyph(int width, int height, u Line 707  ui_create_glyph(int width, int height, u
707          bitmap = XCreatePixmap(display, wnd, width, height, 1);          bitmap = XCreatePixmap(display, wnd, width, height, 1);
708          gc = XCreateGC(display, bitmap, 0, NULL);          gc = XCreateGC(display, bitmap, 0, NULL);
709    
710          image = XCreateImage(display, visual, 1, ZPixmap, 0,          image = XCreateImage(display, visual, 1, ZPixmap, 0, (char *) data,
711                               data, width, height, 8, scanline);                               width, height, 8, scanline);
712          image->byte_order = MSBFirst;          image->byte_order = MSBFirst;
713          image->bitmap_bit_order = MSBFirst;          image->bitmap_bit_order = MSBFirst;
714          XInitImage(image);          XInitImage(image);
# Line 840  ui_destroy_glyph(HGLYPH glyph) Line 727  ui_destroy_glyph(HGLYPH glyph)
727  }  }
728    
729  HCURSOR  HCURSOR
730  ui_create_cursor(unsigned int x, unsigned int y, int width,  ui_create_cursor(unsigned int x, unsigned int y, int width, int height,
731                   int height, uint8 * andmask, uint8 * xormask)                   uint8 * andmask, uint8 * xormask)
732  {  {
733          HGLYPH maskglyph, cursorglyph;          HGLYPH maskglyph, cursorglyph;
734          XColor bg, fg;          XColor bg, fg;
# Line 899  ui_create_cursor(unsigned int x, unsigne Line 786  ui_create_cursor(unsigned int x, unsigne
786          cursorglyph = ui_create_glyph(width, height, cursor);          cursorglyph = ui_create_glyph(width, height, cursor);
787          maskglyph = ui_create_glyph(width, height, mask);          maskglyph = ui_create_glyph(width, height, mask);
788    
789          xcursor = XCreatePixmapCursor(display, (Pixmap) cursorglyph,          xcursor =
790                                        (Pixmap) maskglyph, &fg, &bg, x, y);                  XCreatePixmapCursor(display, (Pixmap) cursorglyph,
791                                        (Pixmap) maskglyph, &fg, &bg, x, y);
792    
793          ui_destroy_glyph(maskglyph);          ui_destroy_glyph(maskglyph);
794          ui_destroy_glyph(cursorglyph);          ui_destroy_glyph(cursorglyph);
# Line 967  ui_create_colourmap(COLOURMAP * colours) Line 855  ui_create_colourmap(COLOURMAP * colours)
855                          if (XAllocColor(display, xcolmap, &xentry) != 0)                          if (XAllocColor(display, xcolmap, &xentry) != 0)
856                                  colour = xentry.pixel;                                  colour = xentry.pixel;
857                          else                          else
858                                  colour = white;                                  colour = WhitePixelOfScreen(screen);
859    
860                          /* byte swap here to make translate_image faster */                          /* byte swap here to make translate_image faster */
861                          map[i] = translate_colour(colour);                          map[i] = translate_colour(colour);
# Line 1060  ui_patblt(uint8 opcode, Line 948  ui_patblt(uint8 opcode,
948                          SET_BACKGROUND(fgcolour);                          SET_BACKGROUND(fgcolour);
949                          XSetFillStyle(display, gc, FillOpaqueStippled);                          XSetFillStyle(display, gc, FillOpaqueStippled);
950                          XSetStipple(display, gc, fill);                          XSetStipple(display, gc, fill);
951                          XSetTSOrigin(display, gc, brush->xorigin,                          XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin);
                                      brush->yorigin);  
952    
953                          FILL_RECTANGLE(x, y, cx, cy);                          FILL_RECTANGLE(x, y, cx, cy);
954    
955                          XSetFillStyle(display, gc, FillSolid);                          XSetFillStyle(display, gc, FillSolid);
956                            XSetTSOrigin(display, gc, 0, 0);
957                          ui_destroy_glyph((HGLYPH) fill);                          ui_destroy_glyph((HGLYPH) fill);
958                          break;                          break;
959    
# Line 1084  ui_screenblt(uint8 opcode, Line 972  ui_screenblt(uint8 opcode,
972          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
973          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(display, wnd, wnd, gc, srcx, srcy, cx, cy, x, y);
974          if (ownbackstore)          if (ownbackstore)
975                  XCopyArea(display, backstore, backstore, gc, srcx, srcy,                  XCopyArea(display, backstore, backstore, gc, srcx, srcy, cx, cy, x, y);
                           cx, cy, x, y);  
976          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
977  }  }
978    
# Line 1097  ui_memblt(uint8 opcode, Line 984  ui_memblt(uint8 opcode,
984          SET_FUNCTION(opcode);          SET_FUNCTION(opcode);
985          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);          XCopyArea(display, (Pixmap) src, wnd, gc, srcx, srcy, cx, cy, x, y);
986          if (ownbackstore)          if (ownbackstore)
987                  XCopyArea(display, (Pixmap) src, backstore, gc, srcx, srcy,                  XCopyArea(display, (Pixmap) src, backstore, gc, srcx, srcy, cx, cy, x, y);
                           cx, cy, x, y);  
988          RESET_FUNCTION(opcode);          RESET_FUNCTION(opcode);
989  }  }
990    
# Line 1115  ui_triblt(uint8 opcode, Line 1001  ui_triblt(uint8 opcode,
1001          {          {
1002                  case 0x69:      /* PDSxxn */                  case 0x69:      /* PDSxxn */
1003                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);
1004                          ui_patblt(ROP2_NXOR, x, y, cx, cy,                          ui_patblt(ROP2_NXOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   brush, bgcolour, fgcolour);  
1005                          break;                          break;
1006    
1007                  case 0xb8:      /* PSDPxax */                  case 0xb8:      /* PSDPxax */
1008                          ui_patblt(ROP2_XOR, x, y, cx, cy,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   brush, bgcolour, fgcolour);  
1009                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);
1010                          ui_patblt(ROP2_XOR, x, y, cx, cy,                          ui_patblt(ROP2_XOR, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   brush, bgcolour, fgcolour);  
1011                          break;                          break;
1012    
1013                  case 0xc0:      /* PSa */                  case 0xc0:      /* PSa */
1014                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);                          ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
1015                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour,                          ui_patblt(ROP2_AND, x, y, cx, cy, brush, bgcolour, fgcolour);
                                   fgcolour);  
1016                          break;                          break;
1017    
1018                  default:                  default:
# Line 1164  ui_rect( Line 1046  ui_rect(
1046  void  void
1047  ui_draw_glyph(int mixmode,  ui_draw_glyph(int mixmode,
1048                /* dest */ int x, int y, int cx, int cy,                /* dest */ int x, int y, int cx, int cy,
1049                /* src */ HGLYPH glyph, int srcx, int srcy, int bgcolour,                /* src */ HGLYPH glyph, int srcx, int srcy,
1050                int fgcolour)                int bgcolour, int fgcolour)
1051  {  {
1052          SET_FOREGROUND(fgcolour);          SET_FOREGROUND(fgcolour);
1053          SET_BACKGROUND(bgcolour);          SET_BACKGROUND(bgcolour);
1054    
1055          XSetFillStyle(display, gc, (mixmode == MIX_TRANSPARENT)          XSetFillStyle(display, gc,
1056                        ? FillStippled : FillOpaqueStippled);                        (mixmode == MIX_TRANSPARENT) ? FillStippled : FillOpaqueStippled);
1057          XSetStipple(display, gc, (Pixmap) glyph);          XSetStipple(display, gc, (Pixmap) glyph);
1058          XSetTSOrigin(display, gc, x, y);          XSetTSOrigin(display, gc, x, y);
1059    
# Line 1189  ui_draw_glyph(int mixmode, Line 1071  ui_draw_glyph(int mixmode,
1071        if ((xyoffset & 0x80))\        if ((xyoffset & 0x80))\
1072          {\          {\
1073            if (flags & TEXT2_VERTICAL) \            if (flags & TEXT2_VERTICAL) \
1074              y += ttext[++idx] | (ttext[++idx] << 8);\              y += ttext[idx+1] | (ttext[idx+2] << 8);\
1075            else\            else\
1076              x += ttext[++idx] | (ttext[++idx] << 8);\              x += ttext[idx+1] | (ttext[idx+2] << 8);\
1077              idx += 2;\
1078          }\          }\
1079        else\        else\
1080          {\          {\
# Line 1214  ui_draw_glyph(int mixmode, Line 1097  ui_draw_glyph(int mixmode,
1097    
1098  void  void
1099  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,
1100               int clipx, int clipy, int clipcx, int clipcy, int boxx,               int clipx, int clipy, int clipcx, int clipcy,
1101               int boxy, int boxcx, int boxcy, int bgcolour,               int boxx, int boxy, int boxcx, int boxcy, int bgcolour,
1102               int fgcolour, uint8 * text, uint8 length)               int fgcolour, uint8 * text, uint8 length)
1103  {  {
1104          FONTGLYPH *glyph;          FONTGLYPH *glyph;
# Line 1240  ui_draw_text(uint8 font, uint8 flags, in Line 1123  ui_draw_text(uint8 font, uint8 flags, in
1123                  {                  {
1124                          case 0xff:                          case 0xff:
1125                                  if (i + 2 < length)                                  if (i + 2 < length)
1126                                          cache_put_text(text[i + 1], text,                                          cache_put_text(text[i + 1], text, text[i + 2]);
                                                        text[i + 2]);  
1127                                  else                                  else
1128                                  {                                  {
1129                                          error("this shouldn't be happening\n");                                          error("this shouldn't be happening\n");
# Line 1258  ui_draw_text(uint8 font, uint8 flags, in Line 1140  ui_draw_text(uint8 font, uint8 flags, in
1140                                  if (entry != NULL)                                  if (entry != NULL)
1141                                  {                                  {
1142                                          if ((((uint8 *) (entry->data))[1] ==                                          if ((((uint8 *) (entry->data))[1] ==
1143                                               0)                                               0) && (!(flags & TEXT2_IMPLICIT_X)))
                                             && (!(flags & TEXT2_IMPLICIT_X)))  
1144                                          {                                          {
1145                                                  if (flags & TEXT2_VERTICAL)                                                  if (flags & TEXT2_VERTICAL)
1146                                                          y += text[i + 2];                                                          y += text[i + 2];
# Line 1275  ui_draw_text(uint8 font, uint8 flags, in Line 1156  ui_draw_text(uint8 font, uint8 flags, in
1156                                          text = &(text[i]);                                          text = &(text[i]);
1157                                          i = 0;                                          i = 0;
1158                                          for (j = 0; j < entry->size; j++)                                          for (j = 0; j < entry->size; j++)
1159                                                  DO_GLYPH(((uint8 *) (entry->                                                  DO_GLYPH(((uint8 *) (entry->data)), j);
                                                                      data)),  
                                                          j);  
1160                                  }                                  }
1161                                  break;                                  break;
1162    
# Line 1299  ui_desktop_save(uint32 offset, int x, in Line 1178  ui_desktop_save(uint32 offset, int x, in
1178    
1179          if (ownbackstore)          if (ownbackstore)
1180          {          {
1181                  image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes,                  image = XGetImage(display, backstore, x, y, cx, cy, AllPlanes, ZPixmap);
                                   ZPixmap);  
1182          }          }
1183          else          else
1184          {          {
1185                  pix = XCreatePixmap(display, wnd, cx, cy, depth);                  pix = XCreatePixmap(display, wnd, cx, cy, depth);
1186                  XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);                  XCopyArea(display, wnd, pix, gc, x, y, cx, cy, 0, 0);
1187                  image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes,                  image = XGetImage(display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);
                                   ZPixmap);  
1188                  XFreePixmap(display, pix);                  XFreePixmap(display, pix);
1189          }          }
1190    
1191          offset *= bpp / 8;          offset *= bpp / 8;
1192          cache_put_desktop(offset, cx, cy, image->bytes_per_line,          cache_put_desktop(offset, cx, cy, image->bytes_per_line, bpp / 8, (uint8 *) image->data);
                           bpp / 8, (uint8 *) image->data);  
1193    
1194          XDestroyImage(image);          XDestroyImage(image);
1195  }  }
# Line 1329  ui_desktop_restore(uint32 offset, int x, Line 1205  ui_desktop_restore(uint32 offset, int x,
1205          if (data == NULL)          if (data == NULL)
1206                  return;                  return;
1207    
1208          image = XCreateImage(display, visual, depth, ZPixmap,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
1209                               0, data, cx, cy, BitmapPad(display),                               (char *) data, cx, cy, BitmapPad(display), cx * bpp / 8);
                              cx * bpp / 8);  
1210    
1211          if (ownbackstore)          if (ownbackstore)
1212          {          {

Legend:
Removed from v.64  
changed lines
  Added in v.164

  ViewVC Help
Powered by ViewVC 1.1.26