/[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 82 by astrand, Tue Jul 30 07:18:48 2002 UTC revision 182 by astrand, Tue Sep 17 16:55:43 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 */          /* 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, &attribs);  
361    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 352  ui_create_window(char *title) Line 396  ui_create_window(char *title)
396                  XFree(sizehints);                  XFree(sizehints);
397          }          }
398    
399          xkeymap_init2();          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
400                    VisibilityChangeMask | FocusChangeMask;
401    
         input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;  
402          if (grab_keyboard)          if (grab_keyboard)
403                  input_mask |= EnterWindowMask | LeaveWindowMask;                  input_mask |= 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)          if (enable_compose)
409                  input_mask |= init_inputmethod();                  input_mask |= init_inputmethod();
410    
411          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
412    
413          gc = XCreateGC(display, wnd, 0, NULL);          xwin_map_window();
414    
415          if (ownbackstore)          /* clear the window so that cached data is not seen */
416                  backstore = XCreatePixmap(display, wnd, width, height, depth);          gc = XCreateGC(display, wnd, 0, NULL);
417            XSetForeground(display, gc, 0);
418          XMapWindow(display, wnd);          FILL_RECTANGLE(0, 0, width, height);
419    
420          return True;          return True;
421  }  }
# Line 393  ui_destroy_window() Line 435  ui_destroy_window()
435          display = NULL;          display = NULL;
436  }  }
437    
438    
439    void
440    xwin_toggle_fullscreen()
441    {
442            XEvent xevent;
443            XSetWindowAttributes attribs;
444            int newwidth, newheight;
445    
446            fullscreen = !fullscreen;
447            newwidth = fullscreen ? WidthOfScreen(screen) : width;
448            newheight = fullscreen ? HeightOfScreen(screen) : height;
449    
450            XUnmapWindow(display, wnd);
451            attribs.override_redirect = fullscreen;
452            XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight);
453            XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs);
454            xwin_map_window();
455    }
456    
457    /* Process all events in Xlib queue */
458  static void  static void
459  xwin_process_events()  xwin_process_events()
460  {  {
461          XEvent xevent;          XEvent xevent;
   
462          KeySym keysym;          KeySym keysym;
463          uint16 button, flags;          uint16 button, flags;
464          uint32 ev_time;          uint32 ev_time;
# Line 406  xwin_process_events() Line 467  xwin_process_events()
467          char str[256];          char str[256];
468          Status status;          Status status;
469    
470          /* 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))  
471          {          {
472                  if (xevent.xmapping.request == MappingKeyboard                  XNextEvent(display, &xevent);
                     || xevent.xmapping.request == MappingModifier)  
                         XRefreshKeyboardMapping(&xevent.xmapping);  
         }  
473    
         while (XCheckMaskEvent(display, ~0, &xevent))  
         {  
474                  if (enable_compose && (XFilterEvent(&xevent, None) == True))                  if (enable_compose && (XFilterEvent(&xevent, None) == True))
475                  {                  {
476                          DEBUG_KBD("Filtering event\n");                          DEBUG_KBD(("Filtering event\n"));
477                          continue;                          continue;
478                  }                  }
479    
# Line 445  xwin_process_events() Line 499  xwin_process_events()
499                                  else                                  else
500                                  {                                  {
501                                          /* Plain old XLookupString */                                          /* Plain old XLookupString */
502                                          DEBUG_KBD("No input context, using XLookupString\n");                                          DEBUG_KBD(("\nNo input context, using XLookupString\n"));
503                                          XLookupString((XKeyEvent *) & xevent,                                          XLookupString((XKeyEvent *) & xevent,
504                                                        str, sizeof(str), &keysym, NULL);                                                        str, sizeof(str), &keysym, NULL);
505                                  }                                  }
506    
507                                  ksname = get_ksname(keysym);                                  ksname = get_ksname(keysym);
508                                  DEBUG_KBD("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname);                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));
509    
510                                  if (inhibit_key(keysym))                                  if (handle_special_keys(keysym, ev_time, True))
                                 {  
                                         DEBUG_KBD("Inhibiting key\n");  
511                                          break;                                          break;
                                 }  
512    
513                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
514                                                             xevent.xkey.keycode, xevent.xkey.state);                                                             xevent.xkey.keycode, xevent.xkey.state);
515    
                                 ensure_remote_modifiers(ev_time, tr);  
   
516                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
517                                          break;                                          break;
518    
519                                    ensure_remote_modifiers(ev_time, tr);
520    
521                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
522                                  break;                                  break;
523                          case KeyRelease:                          case KeyRelease:
# Line 474  xwin_process_events() Line 525  xwin_process_events()
525                                                sizeof(str), &keysym, NULL);                                                sizeof(str), &keysym, NULL);
526    
527                                  ksname = get_ksname(keysym);                                  ksname = get_ksname(keysym);
528                                  DEBUG_KBD("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym, ksname);                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
529                                               ksname));
530    
531                                  if (inhibit_key(keysym))                                  if (handle_special_keys(keysym, ev_time, False))
532                                          break;                                          break;
533    
534                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 506  xwin_process_events() Line 558  xwin_process_events()
558                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
559                                  break;                                  break;
560    
                         case FocusIn:  
                                 /* fall through */  
561                          case EnterNotify:                          case EnterNotify:
562                                  if (grab_keyboard)                                  if (grab_keyboard)
563                                          XGrabKeyboard(display, wnd, True,                                          XGrabKeyboard(display, wnd, True,
564                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
565                                  break;                                  break;
566    
                         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 */  
567                          case LeaveNotify:                          case LeaveNotify:
568                                  if (grab_keyboard)                                  if (grab_keyboard)
569                                          XUngrabKeyboard(display, CurrentTime);                                          XUngrabKeyboard(display, CurrentTime);
570                                  break;                                  break;
571    
572                            case FocusIn:
573                                    reset_modifier_keys();
574                                    break;
575    
576                          case Expose:                          case Expose:
577                                  XCopyArea(display, backstore, wnd, gc,                                  XCopyArea(display, backstore, wnd, gc,
578                                            xevent.xexpose.x, xevent.xexpose.y,                                            xevent.xexpose.x, xevent.xexpose.y,
# Line 533  xwin_process_events() Line 580  xwin_process_events()
580                                            xevent.xexpose.height,                                            xevent.xexpose.height,
581                                            xevent.xexpose.x, xevent.xexpose.y);                                            xevent.xexpose.x, xevent.xexpose.y);
582                                  break;                                  break;
583    
584                            case MappingNotify:
585                                    /* Refresh keyboard mapping if it has changed. This is important for
586                                       Xvnc, since it allocates keycodes dynamically */
587                                    if (xevent.xmapping.request == MappingKeyboard
588                                        || xevent.xmapping.request == MappingModifier)
589                                            XRefreshKeyboardMapping(&xevent.xmapping);
590                                    break;
591    
592                  }                  }
593          }          }
594  }  }
# Line 547  ui_select(int rdp_socket) Line 603  ui_select(int rdp_socket)
603    
604          while (True)          while (True)
605          {          {
606                    /* Process any events already waiting */
607                    xwin_process_events();
608    
609                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
610                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
611                  if (display != NULL)                  FD_SET(x_socket, &rfds);
                 {  
                         FD_SET(x_socket, &rfds);  
                         XFlush(display);  
                 }  
612    
613                  switch (select(n, &rfds, NULL, NULL, NULL))                  switch (select(n, &rfds, NULL, NULL, NULL))
614                  {                  {
# Line 564  ui_select(int rdp_socket) Line 619  ui_select(int rdp_socket)
619                                  continue;                                  continue;
620                  }                  }
621    
                 if (FD_ISSET(x_socket, &rfds))  
                         xwin_process_events();  
   
622                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
623                          return;                          return;
624          }          }
# Line 790  ui_create_colourmap(COLOURMAP * colours) Line 842  ui_create_colourmap(COLOURMAP * colours)
842                          if (XAllocColor(display, xcolmap, &xentry) != 0)                          if (XAllocColor(display, xcolmap, &xentry) != 0)
843                                  colour = xentry.pixel;                                  colour = xentry.pixel;
844                          else                          else
845                                  colour = white;                                  colour = WhitePixelOfScreen(screen);
846    
847                          /* byte swap here to make translate_image faster */                          /* byte swap here to make translate_image faster */
848                          map[i] = translate_colour(colour);                          map[i] = translate_colour(colour);

Legend:
Removed from v.82  
changed lines
  Added in v.182

  ViewVC Help
Powered by ViewVC 1.1.26