/[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 185 by n-ki, Wed Sep 18 12:13:08 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 57  static Pixmap backstore; Line 59  static Pixmap backstore;
59  }  }
60    
61  /* colour maps */  /* colour maps */
 static BOOL owncolmap;  
62  static Colormap xcolmap;  static Colormap xcolmap;
 static uint32 white;  
63  static uint32 *colmap;  static uint32 *colmap;
 static XIM IM = NULL;  
 static XIC IC = NULL;  
64    
65  /* Compose support */  /* Compose support */
66  BOOL enable_compose = False;  BOOL enable_compose = False;
67    static XIM IM = NULL;
68    static XIC IC = NULL;
69    
70  #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )  /* toggle fullscreen globals */
71  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));  static unsigned long input_mask;
72  #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));  
73    #define SET_FOREGROUND(col)     XSetForeground(display, gc, translate_colour(colmap[col]));
74    #define SET_BACKGROUND(col)     XSetBackground(display, gc, translate_colour(colmap[col]));
75    
76  static int rop2_map[] = {  static int rop2_map[] = {
77          GXclear,                /* 0 */          GXclear,                /* 0 */
# Line 241  close_inputmethod(void) Line 243  close_inputmethod(void)
243  }  }
244    
245  BOOL  BOOL
246  ui_init()  get_key_state(int keysym)
247  {  {
248          Screen *screen;          int keysymMask = 0, modifierpos, key;
249          display = XOpenDisplay(NULL);          Window wDummy1, wDummy2;
250          if (display == NULL)          int iDummy3, iDummy4, iDummy5, iDummy6;
251          {          unsigned int current_state;
252                  error("Failed to open display\n");          int offset;
253    
254            XModifierKeymap *map = XGetModifierMapping(display);
255            KeyCode keycode = XKeysymToKeycode(display, keysym);
256    
257            if (keycode == NoSymbol)
258                  return False;                  return False;
259          }  
260          if (fullscreen)          for (modifierpos = 0; modifierpos < 8; modifierpos++)
261          {          {
262                  screen = DefaultScreenOfDisplay(display);                  offset = map->max_keypermod * modifierpos;
263                  width = WidthOfScreen(screen);  
264                  height = HeightOfScreen(screen);                  for (key = 0; key < map->max_keypermod; key++)
265                    {
266                            if (map->modifiermap[offset + key] == keycode)
267                                    keysymMask = 1 << modifierpos;
268                    }
269          }          }
270          return True;  
271            XQueryPointer(display, DefaultRootWindow(display), &wDummy1,
272                          &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, &current_state);
273    
274            XFreeModifiermap(map);
275    
276            return (current_state & keysymMask) ? True : False;
277    }
278    
279    static void
280    xwin_map_window()
281    {
282            XEvent xevent;
283    
284            XMapWindow(display, wnd);
285    
286            /* wait for VisibilityChange */
287            XMaskEvent(display, VisibilityChangeMask, &xevent);
288    
289            if (fullscreen)
290                    XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);
291  }  }
292    
293  BOOL  BOOL
294  ui_create_window(char *title)  ui_init()
295  {  {
         XSetWindowAttributes attribs;  
         XClassHint *classhints;  
         XSizeHints *sizehints;  
         unsigned long input_mask;  
296          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
         Screen *screen;  
297          uint16 test;          uint16 test;
298          int i;          int i;
299    
300            display = XOpenDisplay(NULL);
301            if (display == NULL)
302            {
303                    error("Failed to open display\n");
304                    return False;
305            }
306    
307          x_socket = ConnectionNumber(display);          x_socket = ConnectionNumber(display);
308          screen = DefaultScreenOfDisplay(display);          screen = DefaultScreenOfDisplay(display);
309          visual = DefaultVisualOfScreen(screen);          visual = DefaultVisualOfScreen(screen);
# Line 298  ui_create_window(char *title) Line 331  ui_create_window(char *title)
331                  return False;                  return False;
332          }          }
333    
334          if (depth <= 8)          xcolmap = DefaultColormapOfScreen(screen);
335                  owncolmap = True;  
336          else          if (DoesBackingStore(screen) == NotUseful)
337                  xcolmap = DefaultColormapOfScreen(screen);                  ownbackstore = True;
338    
339          test = 1;          test = 1;
340          host_be = !(BOOL) (*(uint8 *) (&test));          host_be = !(BOOL) (*(uint8 *) (&test));
341          xserver_be = (ImageByteOrder(display) == MSBFirst);          xserver_be = (ImageByteOrder(display) == MSBFirst);
342    
         white = WhitePixelOfScreen(screen);  
         attribs.background_pixel = BlackPixelOfScreen(screen);  
         attribs.backing_store = DoesBackingStore(screen);  
   
         if (attribs.backing_store == NotUseful)  
                 ownbackstore = True;  
   
343          if (fullscreen)          if (fullscreen)
344          {          {
                 attribs.override_redirect = True;  
345                  width = WidthOfScreen(screen);                  width = WidthOfScreen(screen);
346                  height = HeightOfScreen(screen);                  height = HeightOfScreen(screen);
347          }          }
         else  
         {  
                 attribs.override_redirect = False;  
         }  
348    
349          width = (width + 3) & ~3;       /* make width a multiple of 32 bits */          /* make sure width is a multiple of 4 */
350            width = (width + 3) & ~3;
351    
352            xkeymap_init();
353            return True;
354    }
355    
356    BOOL
357    ui_create_window()
358    {
359            XSetWindowAttributes attribs;
360            XClassHint *classhints;
361            XSizeHints *sizehints;
362            XEvent xevent;
363    
364            attribs.background_pixel = BlackPixelOfScreen(screen);
365            attribs.backing_store = ownbackstore ? NotUseful : Always;
366            attribs.override_redirect = fullscreen;
367            wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height,
368                                0, CopyFromParent, InputOutput, CopyFromParent,
369                                CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
370    
371          wnd = XCreateWindow(display, RootWindowOfScreen(screen),          if (ownbackstore)
372                              0, 0, width, height, 0, CopyFromParent,                  backstore = XCreatePixmap(display, wnd, width, height, depth);
                             InputOutput, CopyFromParent,  
                             CWBackingStore | CWBackPixel | CWOverrideRedirect, &attribs);  
373    
374          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
375    
# Line 352  ui_create_window(char *title) Line 391  ui_create_window(char *title)
391                  XFree(sizehints);                  XFree(sizehints);
392          }          }
393    
394          xkeymap_init2();          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
395                    VisibilityChangeMask | FocusChangeMask;
396    
         input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;  
397          if (grab_keyboard)          if (grab_keyboard)
398                  input_mask |= EnterWindowMask | LeaveWindowMask;                  input_mask |= EnterWindowMask | LeaveWindowMask;
399          if (sendmotion)          if (sendmotion)
400                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
   
401          if (ownbackstore)          if (ownbackstore)
402                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
   
403          if (enable_compose)          if (enable_compose)
404                  input_mask |= init_inputmethod();                  input_mask |= init_inputmethod();
405    
406          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
407    
408          gc = XCreateGC(display, wnd, 0, NULL);          xwin_map_window();
   
         if (ownbackstore)  
                 backstore = XCreatePixmap(display, wnd, width, height, depth);  
409    
410          XMapWindow(display, wnd);          /* clear the window so that cached data is not seen */
411            gc = XCreateGC(display, wnd, 0, NULL);
412            XSetForeground(display, gc, 0);
413            FILL_RECTANGLE(0, 0, width, height);
414    
415          return True;          return True;
416  }  }
# Line 393  ui_destroy_window() Line 430  ui_destroy_window()
430          display = NULL;          display = NULL;
431  }  }
432    
433    
434    void
435    xwin_toggle_fullscreen()
436    {
437            XEvent xevent;
438            XSetWindowAttributes attribs;
439            int newwidth, newheight;
440    
441            fullscreen = !fullscreen;
442            newwidth = fullscreen ? WidthOfScreen(screen) : width;
443            newheight = fullscreen ? HeightOfScreen(screen) : height;
444    
445            XUnmapWindow(display, wnd);
446            attribs.override_redirect = fullscreen;
447            XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight);
448            XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs);
449            xwin_map_window();
450    }
451    
452    /* Process all events in Xlib queue */
453  static void  static void
454  xwin_process_events()  xwin_process_events()
455  {  {
456          XEvent xevent;          XEvent xevent;
   
457          KeySym keysym;          KeySym keysym;
458          uint16 button, flags;          uint16 button, flags;
459          uint32 ev_time;          uint32 ev_time;
# Line 406  xwin_process_events() Line 462  xwin_process_events()
462          char str[256];          char str[256];
463          Status status;          Status status;
464    
465          /* 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))  
466          {          {
467                  if (xevent.xmapping.request == MappingKeyboard                  XNextEvent(display, &xevent);
                     || xevent.xmapping.request == MappingModifier)  
                         XRefreshKeyboardMapping(&xevent.xmapping);  
         }  
468    
         while (XCheckMaskEvent(display, ~0, &xevent))  
         {  
469                  if (enable_compose && (XFilterEvent(&xevent, None) == True))                  if (enable_compose && (XFilterEvent(&xevent, None) == True))
470                  {                  {
471                          DEBUG_KBD(("Filtering event\n"));                          DEBUG_KBD(("Filtering event\n"));
# Line 445  xwin_process_events() Line 494  xwin_process_events()
494                                  else                                  else
495                                  {                                  {
496                                          /* Plain old XLookupString */                                          /* Plain old XLookupString */
497                                          DEBUG_KBD(("No input context, using XLookupString\n"));                                          DEBUG_KBD(("\nNo input context, using XLookupString\n"));
498                                          XLookupString((XKeyEvent *) & xevent,                                          XLookupString((XKeyEvent *) & xevent,
499                                                        str, sizeof(str), &keysym, NULL);                                                        str, sizeof(str), &keysym, NULL);
500                                  }                                  }
501    
502                                  ksname = get_ksname(keysym);                                  ksname = get_ksname(keysym);
503                                  DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));
504    
505                                  if (inhibit_key(keysym))                                  if (handle_special_keys(keysym, ev_time, True))
                                 {  
                                         DEBUG_KBD(("Inhibiting key\n"));  
506                                          break;                                          break;
                                 }  
507    
508                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
509                                                             xevent.xkey.keycode, xevent.xkey.state);                                                             xevent.xkey.keycode, xevent.xkey.state);
510    
                                 ensure_remote_modifiers(ev_time, tr);  
   
511                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
512                                          break;                                          break;
513    
514                                    ensure_remote_modifiers(ev_time, tr);
515    
516                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
517                                  break;                                  break;
518                          case KeyRelease:                          case KeyRelease:
# Line 477  xwin_process_events() Line 523  xwin_process_events()
523                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
524                                             ksname));                                             ksname));
525    
526                                  if (inhibit_key(keysym))                                  if (handle_special_keys(keysym, ev_time, False))
527                                          break;                                          break;
528    
529                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 507  xwin_process_events() Line 553  xwin_process_events()
553                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
554                                  break;                                  break;
555    
                         case FocusIn:  
                                 /* fall through */  
556                          case EnterNotify:                          case EnterNotify:
557                                  if (grab_keyboard)                                  if (grab_keyboard)
558                                          XGrabKeyboard(display, wnd, True,                                          XGrabKeyboard(display, wnd, True,
559                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
560                                  break;                                  break;
561    
                         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 */  
562                          case LeaveNotify:                          case LeaveNotify:
563                                  if (grab_keyboard)                                  if (grab_keyboard)
564                                          XUngrabKeyboard(display, CurrentTime);                                          XUngrabKeyboard(display, CurrentTime);
565                                  break;                                  break;
566    
567                            case FocusIn:
568                                    reset_modifier_keys();
569                                    break;
570    
571                          case Expose:                          case Expose:
572                                  XCopyArea(display, backstore, wnd, gc,                                  XCopyArea(display, backstore, wnd, gc,
573                                            xevent.xexpose.x, xevent.xexpose.y,                                            xevent.xexpose.x, xevent.xexpose.y,
# Line 534  xwin_process_events() Line 575  xwin_process_events()
575                                            xevent.xexpose.height,                                            xevent.xexpose.height,
576                                            xevent.xexpose.x, xevent.xexpose.y);                                            xevent.xexpose.x, xevent.xexpose.y);
577                                  break;                                  break;
578    
579                            case MappingNotify:
580                                    /* Refresh keyboard mapping if it has changed. This is important for
581                                       Xvnc, since it allocates keycodes dynamically */
582                                    if (xevent.xmapping.request == MappingKeyboard
583                                        || xevent.xmapping.request == MappingModifier)
584                                            XRefreshKeyboardMapping(&xevent.xmapping);
585                                    break;
586    
587                  }                  }
588          }          }
589  }  }
# Line 548  ui_select(int rdp_socket) Line 598  ui_select(int rdp_socket)
598    
599          while (True)          while (True)
600          {          {
601                    /* Process any events already waiting */
602                    xwin_process_events();
603    
604                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
605                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
606                  if (display != NULL)                  FD_SET(x_socket, &rfds);
                 {  
                         FD_SET(x_socket, &rfds);  
                         XFlush(display);  
                 }  
607    
608                  switch (select(n, &rfds, NULL, NULL, NULL))                  switch (select(n, &rfds, NULL, NULL, NULL))
609                  {                  {
# Line 565  ui_select(int rdp_socket) Line 614  ui_select(int rdp_socket)
614                                  continue;                                  continue;
615                  }                  }
616    
                 if (FD_ISSET(x_socket, &rfds))  
                         xwin_process_events();  
   
617                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
618                          return;                          return;
619          }          }
# Line 586  ui_create_bitmap(int width, int height, Line 632  ui_create_bitmap(int width, int height,
632          Pixmap bitmap;          Pixmap bitmap;
633          uint8 *tdata;          uint8 *tdata;
634    
635          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = translate_image(width, height, data);
636          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(display, wnd, width, height, depth);
637          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
638                               (char *) tdata, width, height, 8, 0);                               (char *) tdata, width, height, 8, 0);
# Line 594  ui_create_bitmap(int width, int height, Line 640  ui_create_bitmap(int width, int height,
640          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
641    
642          XFree(image);          XFree(image);
643          if (!owncolmap)          xfree(tdata);
                 xfree(tdata);  
644          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
645  }  }
646    
# Line 605  ui_paint_bitmap(int x, int y, int cx, in Line 650  ui_paint_bitmap(int x, int y, int cx, in
650          XImage *image;          XImage *image;
651          uint8 *tdata;          uint8 *tdata;
652    
653          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = translate_image(width, height, data);
654          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
655                               (char *) tdata, width, height, 8, 0);                               (char *) tdata, width, height, 8, 0);
656    
# Line 620  ui_paint_bitmap(int x, int y, int cx, in Line 665  ui_paint_bitmap(int x, int y, int cx, in
665          }          }
666    
667          XFree(image);          XFree(image);
668          if (!owncolmap)          xfree(tdata);
                 xfree(tdata);  
669  }  }
670    
671  void  void
# Line 756  ui_create_colourmap(COLOURMAP * colours) Line 800  ui_create_colourmap(COLOURMAP * colours)
800  {  {
801          COLOURENTRY *entry;          COLOURENTRY *entry;
802          int i, ncolours = colours->ncolours;          int i, ncolours = colours->ncolours;
803            uint32 *map = xmalloc(sizeof(*colmap) * ncolours);
804            XColor xentry;
805            XColor xc_cache[256];
806            uint32 colour;
807            int colLookup = 256;
808            for (i = 0; i < ncolours; i++)
809            {
810                    entry = &colours->colours[i];
811                    MAKE_XCOLOR(&xentry, entry);
812    
813                    if (XAllocColor(display, xcolmap, &xentry) == 0)
814                    {
815                            /* Allocation failed, find closest match. */
816                            int j = 256;
817                            int nMinDist = 3 * 256 * 256;
818                            long nDist = nMinDist;
819    
820                            /* only get the colors once */
821                            while( colLookup-- ){
822                                    xc_cache[colLookup].pixel = colLookup;
823                                    xc_cache[colLookup].red = xc_cache[colLookup].green = xc_cache[colLookup].blue = 0;
824                                    xc_cache[colLookup].flags = 0;
825                                    XQueryColor(display, DefaultColormap(display, DefaultScreen(display)), &xc_cache[colLookup]);
826                            }
827                            colLookup = 0;
828    
829          if (owncolmap)                          /* approximate the pixel */
830          {                          while( j-- ){
831                  XColor *xcolours, *xentry;                                  if( xc_cache[j].flags ){
832                  Colormap map;                                          nDist =
833                                            ((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) *
834                  xcolours = xmalloc(sizeof(XColor) * ncolours);                                          ((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) +
835                  for (i = 0; i < ncolours; i++)                                          ((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) *
836                  {                                          ((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) +
837                          entry = &colours->colours[i];                                          ((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8)) *
838                          xentry = &xcolours[i];                                          ((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8));
839                          xentry->pixel = i;                                  }
840                          MAKE_XCOLOR(xentry, entry);                                  if( nDist < nMinDist ){
841                                            nMinDist = nDist;
842                                            xentry.pixel = j;
843                                    }
844                            }
845                  }                  }
846                    colour = xentry.pixel;
847    
848                  map = XCreateColormap(display, wnd, visual, AllocAll);                  /* update our cache */
849                  XStoreColors(display, map, xcolours, ncolours);                  if( xentry.pixel < 256 ){
850                            xc_cache[xentry.pixel].red = xentry.red;
851                  xfree(xcolours);                          xc_cache[xentry.pixel].green = xentry.green;
852                  return (HCOLOURMAP) map;                          xc_cache[xentry.pixel].blue = xentry.blue;
         }  
         else  
         {  
                 uint32 *map = xmalloc(sizeof(*colmap) * ncolours);  
                 XColor xentry;  
                 uint32 colour;  
853    
                 for (i = 0; i < ncolours; i++)  
                 {  
                         entry = &colours->colours[i];  
                         MAKE_XCOLOR(&xentry, entry);  
   
                         if (XAllocColor(display, xcolmap, &xentry) != 0)  
                                 colour = xentry.pixel;  
                         else  
                                 colour = white;  
   
                         /* byte swap here to make translate_image faster */  
                         map[i] = translate_colour(colour);  
854                  }                  }
855    
856                  return map;  
857                    /* byte swap here to make translate_image faster */
858                    map[i] = translate_colour(colour);
859          }          }
860    
861            return map;
862  }  }
863    
864  void  void
865  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
866  {  {
867          if (owncolmap)          xfree(map);
                 XFreeColormap(display, (Colormap) map);  
         else  
                 xfree(map);  
868  }  }
869    
870  void  void
871  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
872  {  {
873          if (owncolmap)          colmap = map;
                 XSetWindowColormap(display, wnd, (Colormap) map);  
         else  
                 colmap = map;  
874  }  }
875    
876  void  void

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

  ViewVC Help
Powered by ViewVC 1.1.26