/[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 84 by astrand, Tue Jul 30 07:30:12 2002 UTC revision 158 by matthewc, Sun Sep 15 11:53:50 2002 UTC
# Line 32  extern int height; Line 32  extern int height;
32  extern BOOL sendmotion;  extern BOOL sendmotion;
33  extern BOOL fullscreen;  extern BOOL fullscreen;
34  extern BOOL grab_keyboard;  extern BOOL grab_keyboard;
35    extern char title[];
36    
37  Display *display = NULL;  Display *display;
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 59  static Pixmap backstore; Line 61  static Pixmap backstore;
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;
 static XIM IM = NULL;  
 static XIC IC = NULL;  
65    
66  /* Compose support */  /* Compose support */
67  BOOL enable_compose = False;  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));
# Line 241  close_inputmethod(void) Line 245  close_inputmethod(void)
245  }  }
246    
247  BOOL  BOOL
248  ui_init()  get_key_state(int keysym)
249  {  {
250          Screen *screen;          int keysymMask = 0, modifierpos, key;
251          display = XOpenDisplay(NULL);          Window wDummy1, wDummy2;
252          if (display == NULL)          int iDummy3, iDummy4, iDummy5, iDummy6;
253          {          unsigned int current_state;
254                  error("Failed to open display\n");          int offset;
255    
256            XModifierKeymap *map = XGetModifierMapping(display);
257            KeyCode keycode = XKeysymToKeycode(display, keysym);
258    
259            if (keycode == NoSymbol)
260                  return False;                  return False;
261          }  
262          if (fullscreen)          for (modifierpos = 0; modifierpos < 8; modifierpos++)
263          {          {
264                  screen = DefaultScreenOfDisplay(display);                  offset = map->max_keypermod * modifierpos;
265                  width = WidthOfScreen(screen);  
266                  height = HeightOfScreen(screen);                  for (key = 0; key < map->max_keypermod; key++)
267                    {
268                            if (map->modifiermap[offset + key] == keycode)
269                                    keysymMask = 1 << modifierpos;
270                    }
271          }          }
272          return True;  
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  BOOL
296  ui_create_window(char *title)  ui_init()
297  {  {
         XSetWindowAttributes attribs;  
         XClassHint *classhints;  
         XSizeHints *sizehints;  
         unsigned long input_mask;  
298          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
         Screen *screen;  
299          uint16 test;          uint16 test;
300          int i;          int i;
301    
302            display = XOpenDisplay(NULL);
303            if (display == NULL)
304            {
305                    error("Failed to open display\n");
306                    return False;
307            }
308    
309          x_socket = ConnectionNumber(display);          x_socket = ConnectionNumber(display);
310          screen = DefaultScreenOfDisplay(display);          screen = DefaultScreenOfDisplay(display);
311          visual = DefaultVisualOfScreen(screen);          visual = DefaultVisualOfScreen(screen);
# Line 303  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 */          xkeymap_init();
355            return True;
356    }
357    
358    BOOL
359    ui_create_window()
360    {
361            XSetWindowAttributes attribs;
362            XClassHint *classhints;
363            XSizeHints *sizehints;
364            XEvent xevent;
365    
366            attribs.background_pixel = BlackPixelOfScreen(screen);
367            attribs.backing_store = ownbackstore ? NotUseful : Always;
368            attribs.override_redirect = fullscreen;
369            wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height,
370                                0, CopyFromParent, InputOutput, CopyFromParent,
371                                CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
372    
373          wnd = XCreateWindow(display, RootWindowOfScreen(screen),          if (ownbackstore)
374                              0, 0, width, height, 0, CopyFromParent,                  backstore = XCreatePixmap(display, wnd, width, height, depth);
                             InputOutput, CopyFromParent,  
                             CWBackingStore | CWBackPixel | CWOverrideRedirect, &attribs);  
375    
376          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
377    
# Line 352  ui_create_window(char *title) Line 393  ui_create_window(char *title)
393                  XFree(sizehints);                  XFree(sizehints);
394          }          }
395    
396          xkeymap_init2();          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
397                    VisibilityChangeMask | FocusChangeMask;
398    
         input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;  
399          if (grab_keyboard)          if (grab_keyboard)
400                  input_mask |= EnterWindowMask | LeaveWindowMask;                  input_mask |= EnterWindowMask | LeaveWindowMask;
401          if (sendmotion)          if (sendmotion)
402                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
   
403          if (ownbackstore)          if (ownbackstore)
404                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
   
405          if (enable_compose)          if (enable_compose)
406                  input_mask |= init_inputmethod();                  input_mask |= init_inputmethod();
407    
408          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
409    
410          gc = XCreateGC(display, wnd, 0, NULL);          xwin_map_window();
   
         if (ownbackstore)  
                 backstore = XCreatePixmap(display, wnd, width, height, depth);  
411    
412          XMapWindow(display, wnd);          /* clear the window so that cached data is not seen */
413            gc = XCreateGC(display, wnd, 0, NULL);
414            XSetForeground(display, gc, 0);
415            FILL_RECTANGLE(0, 0, width, height);
416    
417          return True;          return True;
418  }  }
# Line 394  ui_destroy_window() Line 433  ui_destroy_window()
433  }  }
434    
435  static void  static void
436  xwin_process_events()  xwin_reset_keys()
437    {
438            /* reset keys */
439            uint32 ev_time;
440            ev_time = time(NULL);
441            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);
442            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);
443            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);
444            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL);
445            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);
446            rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);
447    }
448    
449    void
450    xwin_toggle_fullscreen()
451  {  {
452          XEvent xevent;          XEvent xevent;
453            XSetWindowAttributes attribs;
454            int newwidth, newheight;
455    
456            fullscreen = !fullscreen;
457            newwidth  = fullscreen ? WidthOfScreen(screen) : width;
458            newheight = fullscreen ? HeightOfScreen(screen) : height;
459    
460            XUnmapWindow(display, wnd);
461            attribs.override_redirect = fullscreen;
462            XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight);
463            XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs);
464            xwin_map_window();
465    }
466    
467    /* Process all events in Xlib queue */
468    static void
469    xwin_process_events()
470    {
471            XEvent xevent;
472          KeySym keysym;          KeySym keysym;
473          uint16 button, flags;          uint16 button, flags;
474          uint32 ev_time;          uint32 ev_time;
# Line 406  xwin_process_events() Line 477  xwin_process_events()
477          char str[256];          char str[256];
478          Status status;          Status status;
479    
480          /* Refresh keyboard mapping if it has changed. This is important for          while (XPending(display) > 0)
            Xvnc, since it allocates keycodes dynamically */  
         if (XCheckTypedEvent(display, MappingNotify, &xevent))  
481          {          {
482                  if (xevent.xmapping.request == MappingKeyboard                  XNextEvent(display, &xevent);
                     || xevent.xmapping.request == MappingModifier)  
                         XRefreshKeyboardMapping(&xevent.xmapping);  
         }  
483    
         while (XCheckMaskEvent(display, ~0, &xevent))  
         {  
484                  if (enable_compose && (XFilterEvent(&xevent, None) == True))                  if (enable_compose && (XFilterEvent(&xevent, None) == True))
485                  {                  {
486                          DEBUG_KBD(("Filtering event\n"));                          DEBUG_KBD(("Filtering event\n"));
# Line 453  xwin_process_events() Line 517  xwin_process_events()
517                                  ksname = get_ksname(keysym);                                  ksname = get_ksname(keysym);
518                                  DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));                                  DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));
519    
520                                  if (inhibit_key(keysym))                                  if (handle_special_keys(keysym, ev_time, True))
                                 {  
                                         DEBUG_KBD(("Inhibiting key\n"));  
521                                          break;                                          break;
                                 }  
522    
523                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
524                                                             xevent.xkey.keycode, xevent.xkey.state);                                                             xevent.xkey.keycode, xevent.xkey.state);
525    
                                 ensure_remote_modifiers(ev_time, tr);  
   
526                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
527                                          break;                                          break;
528    
529                                    ensure_remote_modifiers(ev_time, tr);
530    
531                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
532                                  break;                                  break;
533                          case KeyRelease:                          case KeyRelease:
# Line 477  xwin_process_events() Line 538  xwin_process_events()
538                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
539                                             ksname));                                             ksname));
540    
541                                  if (inhibit_key(keysym))                                  if (handle_special_keys(keysym, ev_time, False))
542                                          break;                                          break;
543    
544                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 507  xwin_process_events() Line 568  xwin_process_events()
568                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
569                                  break;                                  break;
570    
                         case FocusIn:  
                                 /* fall through */  
571                          case EnterNotify:                          case EnterNotify:
572                                  if (grab_keyboard)                                  if (grab_keyboard)
573                                          XGrabKeyboard(display, wnd, True,                                          XGrabKeyboard(display, wnd, True,
574                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
575                                  break;                                  break;
576    
                         case FocusOut:  
                                 /* reset keys */  
                                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                                KBD_FLAG_DOWN | KBD_FLAG_UP, SCANCODE_CHAR_LCTRL, 0);  
                                 rdp_send_input(ev_time, RDP_INPUT_SCANCODE,  
                                                KBD_FLAG_DOWN | KBD_FLAG_UP, SCANCODE_CHAR_LALT, 0);  
                                 /* fall through */  
577                          case LeaveNotify:                          case LeaveNotify:
578                                  if (grab_keyboard)                                  if (grab_keyboard)
579                                          XUngrabKeyboard(display, CurrentTime);                                          XUngrabKeyboard(display, CurrentTime);
580                                  break;                                  break;
581    
582                            case FocusOut:
583                                    xwin_reset_keys();
584                                    break;
585    
586                          case Expose:                          case Expose:
587                                  XCopyArea(display, backstore, wnd, gc,                                  XCopyArea(display, backstore, wnd, gc,
588                                            xevent.xexpose.x, xevent.xexpose.y,                                            xevent.xexpose.x, xevent.xexpose.y,
# Line 534  xwin_process_events() Line 590  xwin_process_events()
590                                            xevent.xexpose.height,                                            xevent.xexpose.height,
591                                            xevent.xexpose.x, xevent.xexpose.y);                                            xevent.xexpose.x, xevent.xexpose.y);
592                                  break;                                  break;
593    
594                            case MappingNotify:
595                                    /* Refresh keyboard mapping if it has changed. This is important for
596                                       Xvnc, since it allocates keycodes dynamically */
597                                    if (xevent.xmapping.request == MappingKeyboard
598                                        || xevent.xmapping.request == MappingModifier)
599                                            XRefreshKeyboardMapping(&xevent.xmapping);
600                                    break;
601    
602                  }                  }
603          }          }
604  }  }
# Line 548  ui_select(int rdp_socket) Line 613  ui_select(int rdp_socket)
613    
614          while (True)          while (True)
615          {          {
616                    /* Process any events already waiting */
617                    xwin_process_events();
618    
619                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
620                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
621                  if (display != NULL)                  FD_SET(x_socket, &rfds);
                 {  
                         FD_SET(x_socket, &rfds);  
                         XFlush(display);  
                 }  
622    
623                  switch (select(n, &rfds, NULL, NULL, NULL))                  switch (select(n, &rfds, NULL, NULL, NULL))
624                  {                  {
# Line 565  ui_select(int rdp_socket) Line 629  ui_select(int rdp_socket)
629                                  continue;                                  continue;
630                  }                  }
631    
                 if (FD_ISSET(x_socket, &rfds))  
                         xwin_process_events();  
   
632                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
633                          return;                          return;
634          }          }
# Line 791  ui_create_colourmap(COLOURMAP * colours) Line 852  ui_create_colourmap(COLOURMAP * colours)
852                          if (XAllocColor(display, xcolmap, &xentry) != 0)                          if (XAllocColor(display, xcolmap, &xentry) != 0)
853                                  colour = xentry.pixel;                                  colour = xentry.pixel;
854                          else                          else
855                                  colour = white;                                  colour = WhitePixelOfScreen(screen);
856    
857                          /* byte swap here to make translate_image faster */                          /* byte swap here to make translate_image faster */
858                          map[i] = translate_colour(colour);                          map[i] = translate_colour(colour);

Legend:
Removed from v.84  
changed lines
  Added in v.158

  ViewVC Help
Powered by ViewVC 1.1.26