/[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 196 by astrand, Wed Sep 25 11:07:12 2002 UTC revision 275 by astrand, Tue Nov 19 14:48:02 2002 UTC
# Line 1  Line 1 
1  /*  /*
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     User interface services - X Window System     User interface services - X Window System
4     Copyright (C) Matthew Chapman 1999-2001     Copyright (C) Matthew Chapman 1999-2002
5    
6     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 29  extern int height; Line 29  extern int height;
29  extern BOOL sendmotion;  extern BOOL sendmotion;
30  extern BOOL fullscreen;  extern BOOL fullscreen;
31  extern BOOL grab_keyboard;  extern BOOL grab_keyboard;
32    extern BOOL hide_decorations;
33  extern char title[];  extern char title[];
34  BOOL enable_compose = False;  BOOL enable_compose = False;
35    BOOL focused;
36    BOOL mouse_in_wnd;
37    
38  Display *display;  Display *display;
39  static int x_socket;  static int x_socket;
# Line 42  static int depth; Line 45  static int depth;
45  static int bpp;  static int bpp;
46  static XIM IM;  static XIM IM;
47  static XIC IC;  static XIC IC;
48    static XModifierKeymap *mod_map;
49  static Cursor current_cursor;  static Cursor current_cursor;
50    static Atom protocol_atom, kill_atom;
51    
52  /* endianness */  /* endianness */
53  static BOOL host_be;  static BOOL host_be;
# Line 52  static BOOL xserver_be; Line 57  static BOOL xserver_be;
57  static BOOL ownbackstore;  static BOOL ownbackstore;
58  static Pixmap backstore;  static Pixmap backstore;
59    
60    /* MWM decorations */
61    #define MWM_HINTS_DECORATIONS   (1L << 1)
62    #define PROP_MOTIF_WM_HINTS_ELEMENTS    5
63    typedef struct
64    {
65            unsigned long flags;
66            unsigned long functions;
67            unsigned long decorations;
68            long inputMode;
69            unsigned long status;
70    }
71    PropMotifWmHints;
72    
73    
74  #define FILL_RECTANGLE(x,y,cx,cy)\  #define FILL_RECTANGLE(x,y,cx,cy)\
75  { \  { \
76          XFillRectangle(display, wnd, gc, x, y, cx, cy); \          XFillRectangle(display, wnd, gc, x, y, cx, cy); \
# Line 88  static int rop2_map[] = { Line 107  static int rop2_map[] = {
107  #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]); }
108  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }  #define RESET_FUNCTION(rop2)    { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); }
109    
110    void
111    mwm_hide_decorations(void)
112    {
113            PropMotifWmHints motif_hints;
114            Atom hintsatom;
115    
116            /* setup the property */
117            motif_hints.flags = MWM_HINTS_DECORATIONS;
118            motif_hints.decorations = 0;
119    
120            /* get the atom for the property */
121            hintsatom = XInternAtom(display, "_MOTIF_WM_HINTS", False);
122            if (!hintsatom)
123            {
124                    error("Failed to get atom _MOTIF_WM_HINTS\n");
125                    return;
126            }
127    
128            XChangeProperty(display, wnd, hintsatom, hintsatom, 32, PropModeReplace,
129                            (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
130    }
131    
132  static void  static void
133  translate8(uint8 * data, uint8 * out, uint8 * end)  translate8(uint8 * data, uint8 * out, uint8 * end)
134  {  {
# Line 183  translate_colour(uint32 colour) Line 224  translate_colour(uint32 colour)
224  }  }
225    
226  BOOL  BOOL
227  get_key_state(int keysym)  get_key_state(unsigned int state, uint32 keysym)
228  {  {
229          int keysymMask = 0, modifierpos, key;          int modifierpos, key, keysymMask = 0;
         Window wDummy1, wDummy2;  
         int iDummy3, iDummy4, iDummy5, iDummy6;  
         unsigned int current_state;  
230          int offset;          int offset;
231    
         XModifierKeymap *map = XGetModifierMapping(display);  
232          KeyCode keycode = XKeysymToKeycode(display, keysym);          KeyCode keycode = XKeysymToKeycode(display, keysym);
233    
234          if (keycode == NoSymbol)          if (keycode == NoSymbol)
# Line 199  get_key_state(int keysym) Line 236  get_key_state(int keysym)
236    
237          for (modifierpos = 0; modifierpos < 8; modifierpos++)          for (modifierpos = 0; modifierpos < 8; modifierpos++)
238          {          {
239                  offset = map->max_keypermod * modifierpos;                  offset = mod_map->max_keypermod * modifierpos;
240    
241                  for (key = 0; key < map->max_keypermod; key++)                  for (key = 0; key < mod_map->max_keypermod; key++)
242                  {                  {
243                          if (map->modifiermap[offset + key] == keycode)                          if (mod_map->modifiermap[offset + key] == keycode)
244                                  keysymMask = 1 << modifierpos;                                  keysymMask |= 1 << modifierpos;
245                  }                  }
246          }          }
247    
248          XQueryPointer(display, DefaultRootWindow(display), &wDummy1,          return (state & keysymMask) ? True : False;
                       &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, &current_state);  
   
         XFreeModifiermap(map);  
   
         return (current_state & keysymMask) ? True : False;  
249  }  }
250    
251  BOOL  BOOL
# Line 226  ui_init(void) Line 258  ui_init(void)
258          display = XOpenDisplay(NULL);          display = XOpenDisplay(NULL);
259          if (display == NULL)          if (display == NULL)
260          {          {
261                  error("Failed to open display\n");                  error("Failed to open display: %s\n", XDisplayName(NULL));
262                  return False;                  return False;
263          }          }
264    
# Line 267  ui_init(void) Line 299  ui_init(void)
299          host_be = !(BOOL) (*(uint8 *) (&test));          host_be = !(BOOL) (*(uint8 *) (&test));
300          xserver_be = (ImageByteOrder(display) == MSBFirst);          xserver_be = (ImageByteOrder(display) == MSBFirst);
301    
302            if ((width == 0) || (height == 0))
303            {
304                    /* Fetch geometry from _NET_WORKAREA */
305                    uint32 xpos, ypos;
306    
307                    if (get_current_workarea(&xpos, &ypos, &width, &height) < 0)
308                    {
309                            error("Failed to get workarea.\n");
310                            error("Perhaps your window manager does not support EWMH?\n");
311                            error("Defaulting to geometry 800x600\n");
312                            width = 800;
313                            height = 600;
314                    }
315            }
316    
317          if (fullscreen)          if (fullscreen)
318          {          {
319                  width = WidthOfScreen(screen);                  width = WidthOfScreen(screen);
# Line 286  ui_init(void) Line 333  ui_init(void)
333                  XFillRectangle(display, backstore, gc, 0, 0, width, height);                  XFillRectangle(display, backstore, gc, 0, 0, width, height);
334          }          }
335    
336            mod_map = XGetModifierMapping(display);
337    
338          if (enable_compose)          if (enable_compose)
339                  IM = XOpenIM(display, NULL, NULL, NULL);                  IM = XOpenIM(display, NULL, NULL, NULL);
340    
# Line 299  ui_deinit(void) Line 348  ui_deinit(void)
348          if (IM != NULL)          if (IM != NULL)
349                  XCloseIM(IM);                  XCloseIM(IM);
350    
351            XFreeModifiermap(mod_map);
352    
353          if (ownbackstore)          if (ownbackstore)
354                  XFreePixmap(display, backstore);                  XFreePixmap(display, backstore);
355    
# Line 330  ui_create_window(void) Line 381  ui_create_window(void)
381    
382          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
383    
384            if (hide_decorations)
385                    mwm_hide_decorations();
386    
387          classhints = XAllocClassHint();          classhints = XAllocClassHint();
388          if (classhints != NULL)          if (classhints != NULL)
389          {          {
# Line 349  ui_create_window(void) Line 403  ui_create_window(void)
403          }          }
404    
405          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
406                  StructureNotifyMask | FocusChangeMask;                  VisibilityChangeMask | FocusChangeMask;
407    
408          if (sendmotion)          if (sendmotion)
409                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
410          if (ownbackstore)          if (ownbackstore)
411                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
412            if (fullscreen || grab_keyboard)
413                    input_mask |= EnterWindowMask;
414            if (grab_keyboard)
415                    input_mask |= LeaveWindowMask;
416    
417          if (IM != NULL)          if (IM != NULL)
418          {          {
# Line 369  ui_create_window(void) Line 427  ui_create_window(void)
427          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
428          XMapWindow(display, wnd);          XMapWindow(display, wnd);
429    
430          /* wait for MapNotify */          /* wait for VisibilityNotify */
431          do          do
432          {          {
433                  XMaskEvent(display, StructureNotifyMask, &xevent);                  XMaskEvent(display, VisibilityChangeMask, &xevent);
434          }          }
435          while (xevent.type != MapNotify);          while (xevent.type != VisibilityNotify);
436    
437          if (fullscreen)          focused = False;
438                  XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);          mouse_in_wnd = False;
439    
440            /* handle the WM_DELETE_WINDOW protocol */
441            protocol_atom = XInternAtom(display, "WM_PROTOCOLS", True);
442            kill_atom = XInternAtom(display, "WM_DELETE_WINDOW", True);
443            XSetWMProtocols(display, wnd, &kill_atom, 1);
444    
445          return True;          return True;
446  }  }
# Line 416  xwin_toggle_fullscreen(void) Line 479  xwin_toggle_fullscreen(void)
479          }          }
480  }  }
481    
482  /* Process all events in Xlib queue */  /* Process all events in Xlib queue
483  static void     Returns 0 after user quit, 1 otherwise */
484    static int
485  xwin_process_events(void)  xwin_process_events(void)
486  {  {
487          XEvent xevent;          XEvent xevent;
# Line 425  xwin_process_events(void) Line 489  xwin_process_events(void)
489          uint16 button, flags;          uint16 button, flags;
490          uint32 ev_time;          uint32 ev_time;
491          key_translation tr;          key_translation tr;
         char *ksname = NULL;  
492          char str[256];          char str[256];
493          Status status;          Status status;
494            unsigned int state;
495            Window wdummy;
496            int dummy;
497    
498          while (XPending(display) > 0)          while (XPending(display) > 0)
499          {          {
# Line 439  xwin_process_events(void) Line 505  xwin_process_events(void)
505                          continue;                          continue;
506                  }                  }
507    
                 ev_time = time(NULL);  
508                  flags = 0;                  flags = 0;
509    
510                  switch (xevent.type)                  switch (xevent.type)
511                  {                  {
512                            case ClientMessage:
513                                    /* the window manager told us to quit */
514                                    if ((xevent.xclient.message_type == protocol_atom)
515                                        && (xevent.xclient.data.l[0] == kill_atom))
516                                            /* Quit */
517                                            return 0;
518                                    break;
519    
520                          case KeyPress:                          case KeyPress:
521                                  if (IC != NULL)                                  if (IC != NULL)
522                                          /* Multi_key compatible version */                                          /* Multi_key compatible version */
# Line 466  xwin_process_events(void) Line 539  xwin_process_events(void)
539                                                        str, sizeof(str), &keysym, NULL);                                                        str, sizeof(str), &keysym, NULL);
540                                  }                                  }
541    
542                                  ksname = get_ksname(keysym);                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym,
543                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));                                             get_ksname(keysym)));
544    
545                                  if (handle_special_keys(keysym, ev_time, True))                                  ev_time = time(NULL);
546                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True))
547                                          break;                                          break;
548    
549                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 482  xwin_process_events(void) Line 556  xwin_process_events(void)
556    
557                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
558                                  break;                                  break;
559    
560                          case KeyRelease:                          case KeyRelease:
561                                  XLookupString((XKeyEvent *) & xevent, str,                                  XLookupString((XKeyEvent *) & xevent, str,
562                                                sizeof(str), &keysym, NULL);                                                sizeof(str), &keysym, NULL);
563    
                                 ksname = get_ksname(keysym);  
564                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
565                                             ksname));                                             get_ksname(keysym)));
566    
567                                  if (handle_special_keys(keysym, ev_time, False))                                  ev_time = time(NULL);
568                                    if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False))
569                                          break;                                          break;
570    
571                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 511  xwin_process_events(void) Line 586  xwin_process_events(void)
586                                  if (button == 0)                                  if (button == 0)
587                                          break;                                          break;
588    
589                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
590                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);                                                 flags | button, xevent.xbutton.x, xevent.xbutton.y);
591                                  break;                                  break;
592    
593                          case MotionNotify:                          case MotionNotify:
594                                  rdp_send_input(ev_time, RDP_INPUT_MOUSE,                                  rdp_send_input(time(NULL), RDP_INPUT_MOUSE,
595                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
596                                  break;                                  break;
597    
598                          case FocusIn:                          case FocusIn:
599                                  reset_modifier_keys();                                  if (xevent.xfocus.mode == NotifyGrab)
600                                  if (grab_keyboard)                                          break;
601                                    focused = True;
602                                    XQueryPointer(display, wnd, &wdummy, &wdummy, &dummy, &dummy,
603                                                  &dummy, &dummy, &state);
604                                    reset_modifier_keys(state);
605                                    if (grab_keyboard && mouse_in_wnd)
606                                          XGrabKeyboard(display, wnd, True,                                          XGrabKeyboard(display, wnd, True,
607                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
608                                  break;                                  break;
609    
610                          case FocusOut:                          case FocusOut:
611                                  if (grab_keyboard)                                  if (xevent.xfocus.mode == NotifyUngrab)
612                                            break;
613                                    focused = False;
614                                    if (xevent.xfocus.mode == NotifyWhileGrabbed)
615                                          XUngrabKeyboard(display, CurrentTime);                                          XUngrabKeyboard(display, CurrentTime);
616                                  break;                                  break;
617    
618                            case EnterNotify:
619                                    /* we only register for this event when in fullscreen mode */
620                                    /* or grab_keyboard */
621                                    mouse_in_wnd = True;
622                                    if (fullscreen)
623                                    {
624                                            XSetInputFocus(display, wnd, RevertToPointerRoot,
625                                                           CurrentTime);
626                                            break;
627                                    }
628                                    if (focused)
629                                            XGrabKeyboard(display, wnd, True,
630                                                          GrabModeAsync, GrabModeAsync, CurrentTime);
631                                    break;
632    
633                            case LeaveNotify:
634                                    /* we only register for this event when grab_keyboard */
635                                    mouse_in_wnd = False;
636                                    XUngrabKeyboard(display, CurrentTime);
637                                    break;
638    
639                          case Expose:                          case Expose:
640                                  XCopyArea(display, backstore, wnd, gc,                                  XCopyArea(display, backstore, wnd, gc,
641                                            xevent.xexpose.x, xevent.xexpose.y,                                            xevent.xexpose.x, xevent.xexpose.y,
# Line 546  xwin_process_events(void) Line 650  xwin_process_events(void)
650                                  if (xevent.xmapping.request == MappingKeyboard                                  if (xevent.xmapping.request == MappingKeyboard
651                                      || xevent.xmapping.request == MappingModifier)                                      || xevent.xmapping.request == MappingModifier)
652                                          XRefreshKeyboardMapping(&xevent.xmapping);                                          XRefreshKeyboardMapping(&xevent.xmapping);
653    
654                                    if (xevent.xmapping.request == MappingModifier)
655                                    {
656                                            XFreeModifiermap(mod_map);
657                                            mod_map = XGetModifierMapping(display);
658                                    }
659                                  break;                                  break;
660    
661                  }                  }
662          }          }
663            /* Keep going */
664            return 1;
665  }  }
666    
667  void  /* Returns 0 after user quit, 1 otherwise */
668    int
669  ui_select(int rdp_socket)  ui_select(int rdp_socket)
670  {  {
671          int n = (rdp_socket > x_socket) ? rdp_socket + 1 : x_socket + 1;          int n = (rdp_socket > x_socket) ? rdp_socket + 1 : x_socket + 1;
# Line 563  ui_select(int rdp_socket) Line 676  ui_select(int rdp_socket)
676          while (True)          while (True)
677          {          {
678                  /* Process any events already waiting */                  /* Process any events already waiting */
679                  xwin_process_events();                  if (!xwin_process_events())
680                            /* User quit */
681                            return 0;
682    
683                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
684                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
# Line 579  ui_select(int rdp_socket) Line 694  ui_select(int rdp_socket)
694                  }                  }
695    
696                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
697                          return;                          return 1;
698          }          }
699  }  }
700    
# Line 1095  ui_draw_text(uint8 font, uint8 flags, in Line 1210  ui_draw_text(uint8 font, uint8 flags, in
1210                                  else                                  else
1211                                  {                                  {
1212                                          error("this shouldn't be happening\n");                                          error("this shouldn't be happening\n");
1213                                          break;                                          exit(1);
1214                                  }                                  }
1215                                  /* this will move pointer from start to first character after FF command */                                  /* this will move pointer from start to first character after FF command */
1216                                  length -= i + 3;                                  length -= i + 3;

Legend:
Removed from v.196  
changed lines
  Added in v.275

  ViewVC Help
Powered by ViewVC 1.1.26