/[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 121 by matthewc, Sat Sep 14 11:48:44 2002 UTC revision 185 by n-ki, Wed Sep 18 12:13:08 2002 UTC
# Line 42  static GC gc; Line 42  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 61  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;
63  static uint32 *colmap;  static uint32 *colmap;
64    
# Line 73  static XIC IC = NULL; Line 70  static XIC IC = NULL;
70  /* toggle fullscreen globals */  /* toggle fullscreen globals */
71  static unsigned long input_mask;  static unsigned long input_mask;
72    
73  #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )  #define SET_FOREGROUND(col)     XSetForeground(display, gc, translate_colour(colmap[col]));
74  #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));  #define SET_BACKGROUND(col)     XSetBackground(display, gc, translate_colour(colmap[col]));
 #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));  
75    
76  static int rop2_map[] = {  static int rop2_map[] = {
77          GXclear,                /* 0 */          GXclear,                /* 0 */
# Line 280  get_key_state(int keysym) Line 276  get_key_state(int keysym)
276          return (current_state & keysymMask) ? True : False;          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_init()  ui_init()
# Line 322  ui_init() Line 331  ui_init()
331                  return False;                  return False;
332          }          }
333    
334          if (depth <= 8)          xcolmap = DefaultColormapOfScreen(screen);
                 owncolmap = True;  
         else  
                 xcolmap = DefaultColormapOfScreen(screen);  
335    
336          if (DoesBackingStore(screen) == NotUseful)          if (DoesBackingStore(screen) == NotUseful)
337                  ownbackstore = True;                  ownbackstore = True;
# Line 340  ui_init() Line 346  ui_init()
346                  height = HeightOfScreen(screen);                  height = HeightOfScreen(screen);
347          }          }
348    
349            /* make sure width is a multiple of 4 */
350            width = (width + 3) & ~3;
351    
352          xkeymap_init();          xkeymap_init();
353          return True;          return True;
354  }  }
# Line 359  ui_create_window() Line 368  ui_create_window()
368                              0, CopyFromParent, InputOutput, CopyFromParent,                              0, CopyFromParent, InputOutput, CopyFromParent,
369                              CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);                              CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
370    
371            if (ownbackstore)
372                    backstore = XCreatePixmap(display, wnd, width, height, depth);
373    
374          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
375    
376          classhints = XAllocClassHint();          classhints = XAllocClassHint();
# Line 393  ui_create_window() Line 405  ui_create_window()
405    
406          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
407    
408          gc = XCreateGC(display, wnd, 0, NULL);          xwin_map_window();
   
         XMapWindow(display, wnd);  
409    
410          /* Wait for VisibilityNotify Event */          /* clear the window so that cached data is not seen */
411          for (;;)          gc = XCreateGC(display, wnd, 0, NULL);
         {  
                 XNextEvent(display, &xevent);  
                 if (xevent.type == VisibilityNotify)  
                         break;  
         }  
   
         if (ownbackstore)  
                 backstore = XCreatePixmap(display, wnd, width, height, depth);  
   
         /* clear the window so that cached data is not viewed upon start... */  
         XSetBackground(display, gc, 0);  
412          XSetForeground(display, gc, 0);          XSetForeground(display, gc, 0);
413          FILL_RECTANGLE(0, 0, width, height);          FILL_RECTANGLE(0, 0, width, height);
         /* make sure the window is focused */  
         XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);  
414    
415          return True;          return True;
416  }  }
# Line 433  ui_destroy_window() Line 430  ui_destroy_window()
430          display = NULL;          display = NULL;
431  }  }
432    
 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);  
 }  
433    
434  void  void
435  toggle_fullscreen()  xwin_toggle_fullscreen()
436  {  {
437          /* save window contents */          XEvent xevent;
438          Pixmap pixmap;          XSetWindowAttributes attribs;
439          pixmap = XCreatePixmap(display, wnd, width, height, depth);          int newwidth, newheight;
440          if (ownbackstore)  
441                  XCopyArea(display, backstore, pixmap, gc, 0, 0, width, height, 0, 0);          fullscreen = !fullscreen;
442          else          newwidth = fullscreen ? WidthOfScreen(screen) : width;
443                  XCopyArea(display, wnd, pixmap, gc, 0, 0, width, height, 0, 0);          newheight = fullscreen ? HeightOfScreen(screen) : height;
444          fullscreen = fullscreen ? False : True;  
445          close_inputmethod();          XUnmapWindow(display, wnd);
446          if (ownbackstore)          attribs.override_redirect = fullscreen;
447                  XFreePixmap(display, backstore);          XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight);
448          XFreeGC(display, gc);          XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs);
449          XDestroyWindow(display, wnd);          xwin_map_window();
         ui_create_window();  
         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);  
450  }  }
451    
452  /* Process all events in Xlib queue */  /* Process all events in Xlib queue */
# Line 479  static void Line 454  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 488  xwin_process_events() Line 462  xwin_process_events()
462          char str[256];          char str[256];
463          Status status;          Status status;
464    
465          while (XCheckMaskEvent(display, ~0, &xevent))          while (XPending(display) > 0)
466          {          {
467                    XNextEvent(display, &xevent);
468    
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 518  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 (handle_special_keys(keysym, ev_time, True))                                  if (handle_special_keys(keysym, ev_time, True))
506                                          break;                                          break;
# Line 577  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();  
                                 /* 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 624  ui_select(int rdp_socket) Line 599  ui_select(int rdp_socket)
599          while (True)          while (True)
600          {          {
601                  /* Process any events already waiting */                  /* Process any events already waiting */
                 XFlush(display);  
602                  xwin_process_events();                  xwin_process_events();
603    
604                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
# Line 658  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 666  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 677  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 692  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 828  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                            xc_cache[xentry.pixel].green = xentry.green;
852                            xc_cache[xentry.pixel].blue = xentry.blue;
853    
                 xfree(xcolours);  
                 return (HCOLOURMAP) map;  
         }  
         else  
         {  
                 uint32 *map = xmalloc(sizeof(*colmap) * ncolours);  
                 XColor xentry;  
                 uint32 colour;  
   
                 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 = WhitePixelOfScreen(screen);  
   
                         /* 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.121  
changed lines
  Added in v.185

  ViewVC Help
Powered by ViewVC 1.1.26