/[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 100 by jsorg71, Sat Aug 24 20:04:56 2002 UTC revision 182 by astrand, Tue Sep 17 16:55:43 2002 UTC
# Line 34  extern BOOL fullscreen; Line 34  extern BOOL fullscreen;
34  extern BOOL grab_keyboard;  extern BOOL grab_keyboard;
35  extern char title[];  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;
43  static int depth;  static int depth;
44  static int bpp;  static int bpp;
 static int dpy_width;  
 static int dpy_height;  
45    
46  /* endianness */  /* endianness */
47  static BOOL host_be;  static BOOL host_be;
# Line 62  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 */  /* toggle fullscreen globals */
 static XSetWindowAttributes attribs;  
72  static unsigned long input_mask;  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]) )
# Line 248  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;
                 return False;  
         }  
         if (fullscreen)  
         {  
                 screen = DefaultScreenOfDisplay(display);  
                 width = WidthOfScreen(screen);  
                 height = HeightOfScreen(screen);  
         }  
         return True;  
 }  
255    
256  BOOL          XModifierKeymap *map = XGetModifierMapping(display);
257  ui_create_window_obj(int xpos, int ypos, int width, int height, int valuemask)          KeyCode keycode = XKeysymToKeycode(display, keysym);
 {  
         XClassHint *classhints;  
         XSizeHints *sizehints;  
         XEvent xevent;  
         Screen *screen;  
   
         screen = DefaultScreenOfDisplay(display);  
   
         wnd = XCreateWindow(display, RootWindowOfScreen(screen), xpos,  
                                 ypos, width, height, 0, CopyFromParent,  
                                 InputOutput, CopyFromParent, valuemask, &attribs);  
258    
259            if (keycode == NoSymbol)
260                    return False;
261    
262          XStoreName(display, wnd, title);          for (modifierpos = 0; modifierpos < 8; modifierpos++)
   
         classhints = XAllocClassHint();  
         if (classhints != NULL)  
263          {          {
264                  classhints->res_name = classhints->res_class = "rdesktop";                  offset = map->max_keypermod * modifierpos;
                 XSetClassHint(display, wnd, classhints);  
                 XFree(classhints);  
         }  
265    
266          sizehints = XAllocSizeHints();                  for (key = 0; key < map->max_keypermod; key++)
267          if (sizehints)                  {
268          {                          if (map->modifiermap[offset + key] == keycode)
269                  sizehints->flags = PMinSize | PMaxSize;                                  keysymMask = 1 << modifierpos;
270                  sizehints->min_width = sizehints->max_width = width;                  }
                 sizehints->min_height = sizehints->max_height = height;  
                 XSetWMNormalHints(display, wnd, sizehints);  
                 XFree(sizehints);  
271          }          }
272    
273          if (enable_compose)          XQueryPointer(display, DefaultRootWindow(display), &wDummy1,
274                  input_mask |= init_inputmethod();                        &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, &current_state);
275    
276          XSelectInput(display, wnd, input_mask);          XFreeModifiermap(map);
277    
278          gc = XCreateGC(display, wnd, 0, NULL);          return (current_state & keysymMask) ? True : False;
279    }
280    
281          XMapWindow(display, wnd);  static void
282    xwin_map_window()
283    {
284            XEvent xevent;
285    
286          /* Wait for VisibilityNotify Event */          XMapWindow(display, wnd);
         for (;;) {  
                 XNextEvent(display, &xevent);  
                 if (xevent.type == VisibilityNotify)  
                         break;  
         }  
287    
288          if (ownbackstore)          /* wait for VisibilityChange */
289                  backstore = XCreatePixmap(display, wnd, width, height, depth);          XMaskEvent(display, VisibilityChangeMask, &xevent);
290    
291          /* clear the window so that cached data is not viewed upon start... */          if (fullscreen)
292          XSetBackground(display, gc, 0);                  XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);
         XSetForeground(display, gc, 0);  
         FILL_RECTANGLE(0, 0, width, height);  
         /* make sure the window is focused */  
         XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);  
293  }  }
294    
295  BOOL  BOOL
296  ui_create_window()  ui_init()
297  {  {
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 368  ui_create_window() Line 338  ui_create_window()
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    
348          white = WhitePixelOfScreen(screen);          if (fullscreen)
349            {
350                    width = WidthOfScreen(screen);
351                    height = HeightOfScreen(screen);
352            }
353    
354            /* make sure width is a multiple of 4 */
355            width = (width + 3) & ~3;
356    
357            xkeymap_init();
358            return True;
359    }
360    
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);          attribs.background_pixel = BlackPixelOfScreen(screen);
370          attribs.backing_store = DoesBackingStore(screen);          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 (attribs.backing_store == NotUseful)          if (ownbackstore)
377                  ownbackstore = True;                  backstore = XCreatePixmap(display, wnd, width, height, depth);
378    
379          dpy_width = WidthOfScreen(screen);          XStoreName(display, wnd, title);
         dpy_height = HeightOfScreen(screen);  
380    
381          if (fullscreen)          classhints = XAllocClassHint();
382            if (classhints != NULL)
383          {          {
384                  attribs.override_redirect = True;                  classhints->res_name = classhints->res_class = "rdesktop";
385                  width = dpy_width;                  XSetClassHint(display, wnd, classhints);
386                  height = dpy_height;                  XFree(classhints);
387          }          }
388          else  
389            sizehints = XAllocSizeHints();
390            if (sizehints)
391          {          {
392                  attribs.override_redirect = False;                  sizehints->flags = PMinSize | PMaxSize;
393                    sizehints->min_width = sizehints->max_width = width;
394                    sizehints->min_height = sizehints->max_height = height;
395                    XSetWMNormalHints(display, wnd, sizehints);
396                    XFree(sizehints);
397          }          }
398    
         width = (width + 3) & ~3;       /* make width a multiple of 32 bits */  
   
   
399          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
400                          VisibilityChangeMask | FocusChangeMask;                  VisibilityChangeMask | FocusChangeMask;
401    
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)
409                    input_mask |= init_inputmethod();
410    
411          if (fullscreen)          XSelectInput(display, wnd, input_mask);
                 ui_create_window_obj(0, 0, width, height, CWBackingStore | CWBackPixel | CWOverrideRedirect);  
         else  
                 ui_create_window_obj(0, 0, width, height, CWBackingStore | CWBackPixel);  
412    
413          xkeymap_init2();          xwin_map_window();
414    
415            /* clear the window so that cached data is not seen */
416            gc = XCreateGC(display, wnd, 0, NULL);
417            XSetForeground(display, gc, 0);
418            FILL_RECTANGLE(0, 0, width, height);
419    
420          return True;          return True;
421  }  }
# Line 432  ui_destroy_window() Line 435  ui_destroy_window()
435          display = NULL;          display = NULL;
436  }  }
437    
 void  
 reset_keys()  
 {  
         /* reset keys */  
         uint32 ev_time;  
         ev_time = time(NULL);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT);  
         rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT);  
 }  
438    
439  void  void
440  toggle_fullscreen()  xwin_toggle_fullscreen()
441  {  {
442          /* save window contents */          XEvent xevent;
443          Pixmap pixmap;          XSetWindowAttributes attribs;
444          pixmap = XCreatePixmap(display, wnd, width, height, depth);          int newwidth, newheight;
445          if (ownbackstore)  
446                  XCopyArea(display, backstore, pixmap, gc, 0, 0, width, height, 0, 0);          fullscreen = !fullscreen;
447          else          newwidth = fullscreen ? WidthOfScreen(screen) : width;
448                  XCopyArea(display, wnd, pixmap, gc, 0, 0, width, height, 0, 0);          newheight = fullscreen ? HeightOfScreen(screen) : height;
449          fullscreen = fullscreen ? False : True;  
450          close_inputmethod();          XUnmapWindow(display, wnd);
451          if (ownbackstore)          attribs.override_redirect = fullscreen;
452                  XFreePixmap(display, backstore);          XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight);
453          XFreeGC(display, gc);          XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs);
454          XDestroyWindow(display, wnd);          xwin_map_window();
         if (fullscreen) {  
                 attribs.override_redirect = True;  
                 ui_create_window_obj(0, 0, dpy_width, dpy_height,  
                                                                         CWBackingStore | CWBackPixel | CWOverrideRedirect);  
         }  
         else {  
                 attribs.override_redirect = False;  
                 ui_create_window_obj(0, 0, width, height,  
                                                                         CWBackingStore | CWBackPixel);  
         }  
         ui_set_cursor(cache_get_cursor(0));  
         ui_move_pointer(width / 2, height / 2);  
         reset_keys();  
         /* restore window contents */  
         if (ownbackstore)  
                 XCopyArea(display, pixmap, backstore, gc, 0, 0, width, height, 0, 0);  
         XCopyArea(display, pixmap, wnd, gc, 0, 0, width, height, 0, 0);  
         XFreePixmap(display, pixmap);  
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 495  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"));
# Line 534  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    
                                 /* FIXME needs alt modifier */  
                                 if (keysym == XK_Break) /* toggle full screen */  
                                 {  
                                         toggle_fullscreen();  
                                         break;  
                                 }  
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 572  xwin_process_events() Line 528  xwin_process_events()
528                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
529                                             ksname));                                             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 602  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();  
                                 /* 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 625  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 639  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 656  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 882  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.100  
changed lines
  Added in v.182

  ViewVC Help
Powered by ViewVC 1.1.26