/[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 157 by matthewc, Sun Sep 15 11:39:16 2002 UTC revision 188 by matthewc, Tue Sep 24 06:09:09 2002 UTC
# Line 33  extern BOOL sendmotion; Line 33  extern BOOL sendmotion;
33  extern BOOL fullscreen;  extern BOOL fullscreen;
34  extern BOOL grab_keyboard;  extern BOOL grab_keyboard;
35  extern char title[];  extern char title[];
36    BOOL enable_compose = False;
37    
38  Display *display;  Display *display;
39  static int x_socket;  static int x_socket;
# Line 42  static GC gc; Line 43  static GC gc;
43  static Visual *visual;  static Visual *visual;
44  static int depth;  static int depth;
45  static int bpp;  static int bpp;
46    static XIM IM;
47    static XIC IC;
48    static Cursor current_cursor;
49    
50  /* endianness */  /* endianness */
51  static BOOL host_be;  static BOOL host_be;
# Line 59  static Pixmap backstore; Line 63  static Pixmap backstore;
63  }  }
64    
65  /* colour maps */  /* colour maps */
 static BOOL owncolmap;  
66  static Colormap xcolmap;  static Colormap xcolmap;
67  static uint32 *colmap;  static uint32 *colmap;
68    
69  /* Compose support */  #define SET_FOREGROUND(col)     XSetForeground(display, gc, translate_colour(colmap[col]));
70  BOOL enable_compose = False;  #define SET_BACKGROUND(col)     XSetBackground(display, gc, translate_colour(colmap[col]));
 static XIM IM = NULL;  
 static XIC IC = NULL;  
   
 /* toggle fullscreen globals */  
 static unsigned long input_mask;  
   
 #define TRANSLATE(col)          ( owncolmap ? col : translate_colour(colmap[col]) )  
 #define SET_FOREGROUND(col)     XSetForeground(display, gc, TRANSLATE(col));  
 #define SET_BACKGROUND(col)     XSetBackground(display, gc, TRANSLATE(col));  
71    
72  static int rop2_map[] = {  static int rop2_map[] = {
73          GXclear,                /* 0 */          GXclear,                /* 0 */
# Line 191  translate_colour(uint32 colour) Line 185  translate_colour(uint32 colour)
185          return colour;          return colour;
186  }  }
187    
 static unsigned long  
 init_inputmethod(void)  
 {  
         unsigned long filtered_events = 0;  
   
         IM = XOpenIM(display, NULL, NULL, NULL);  
         if (IM == NULL)  
         {  
                 error("Failed to open input method\n");  
         }  
   
         if (IM != NULL)  
         {  
                 /* Must be done after XCreateWindow */  
                 IC = XCreateIC(IM, XNInputStyle,  
                                (XIMPreeditNothing | XIMStatusNothing),  
                                XNClientWindow, wnd, XNFocusWindow, wnd, NULL);  
   
                 if (IC == NULL)  
                 {  
                         error("Failed to create input context\n");  
                         XCloseIM(IM);  
                         IM = NULL;  
                 }  
         }  
   
         /* For correct Multi_key/Compose processing, I guess.  
            It seems to work alright anyway, though. */  
         if (IC != NULL)  
         {  
                 if (XGetICValues(IC, XNFilterEvents, &filtered_events, NULL) != NULL)  
                 {  
                         error("Failed to obtain XNFilterEvents value from IC\n");  
                         filtered_events = 0;  
                 }  
         }  
         return filtered_events;  
 }  
   
 static void  
 close_inputmethod(void)  
 {  
         if (IC != NULL)  
         {  
                 XDestroyIC(IC);  
                 if (IM != NULL)  
                 {  
                         XCloseIM(IM);  
                         IM = NULL;  
                 }  
         }  
 }  
   
188  BOOL  BOOL
189  get_key_state(int keysym)  get_key_state(int keysym)
190  {  {
# Line 278  get_key_state(int keysym) Line 219  get_key_state(int keysym)
219          return (current_state & keysymMask) ? True : False;          return (current_state & keysymMask) ? True : False;
220  }  }
221    
 static void  
 xwin_map_window()  
 {  
         XEvent xevent;  
   
         XMapWindow(display, wnd);  
   
         /* wait for VisibilityChange */  
         XMaskEvent(display, VisibilityChangeMask, &xevent);  
   
         if (fullscreen)  
                 XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);  
 }  
   
222  BOOL  BOOL
223  ui_init()  ui_init()
224  {  {
# Line 333  ui_init() Line 260  ui_init()
260                  return False;                  return False;
261          }          }
262    
263          if (depth <= 8)          xcolmap = DefaultColormapOfScreen(screen);
264                  owncolmap = True;          gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL);
         else  
                 xcolmap = DefaultColormapOfScreen(screen);  
265    
266          if (DoesBackingStore(screen) == NotUseful)          if (DoesBackingStore(screen) != Always)
267                  ownbackstore = True;                  ownbackstore = True;
268    
269          test = 1;          test = 1;
# Line 351  ui_init() Line 276  ui_init()
276                  height = HeightOfScreen(screen);                  height = HeightOfScreen(screen);
277          }          }
278    
279            /* make sure width is a multiple of 4 */
280            width = (width + 3) & ~3;
281    
282            if (ownbackstore)
283            {
284                    backstore = XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth);
285    
286                    /* clear to prevent rubbish being exposed at startup */
287                    XSetForeground(display, gc, BlackPixelOfScreen(screen));
288                    XFillRectangle(display, backstore, gc, 0, 0, width, height);
289            }
290    
291            if (enable_compose)
292                    IM = XOpenIM(display, NULL, NULL, NULL);
293    
294          xkeymap_init();          xkeymap_init();
295          return True;          return True;
296  }  }
297    
298    void
299    ui_deinit()
300    {
301            if (IM != NULL)
302                    XCloseIM(IM);
303    
304            if (ownbackstore)
305                    XFreePixmap(display, backstore);
306    
307            XFreeGC(display, gc);
308            XCloseDisplay(display);
309            display = NULL;
310    }
311    
312  BOOL  BOOL
313  ui_create_window()  ui_create_window()
314  {  {
315          XSetWindowAttributes attribs;          XSetWindowAttributes attribs;
316          XClassHint *classhints;          XClassHint *classhints;
317          XSizeHints *sizehints;          XSizeHints *sizehints;
318            int wndwidth, wndheight;
319            long input_mask, ic_input_mask;
320          XEvent xevent;          XEvent xevent;
321    
322            wndwidth  = fullscreen ? WidthOfScreen(screen)  : width;
323            wndheight = fullscreen ? HeightOfScreen(screen) : height;
324    
325          attribs.background_pixel = BlackPixelOfScreen(screen);          attribs.background_pixel = BlackPixelOfScreen(screen);
326          attribs.backing_store = ownbackstore ? NotUseful : Always;          attribs.backing_store = ownbackstore ? NotUseful : Always;
327          attribs.override_redirect = fullscreen;          attribs.override_redirect = fullscreen;
328          wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height,  
329            wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight,
330                              0, CopyFromParent, InputOutput, CopyFromParent,                              0, CopyFromParent, InputOutput, CopyFromParent,
331                              CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);                              CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
332    
         if (ownbackstore)  
                 backstore = XCreatePixmap(display, wnd, width, height, depth);  
   
333          XStoreName(display, wnd, title);          XStoreName(display, wnd, title);
334    
335          classhints = XAllocClassHint();          classhints = XAllocClassHint();
# Line 394  ui_create_window() Line 351  ui_create_window()
351          }          }
352    
353          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |          input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
354                  VisibilityChangeMask | FocusChangeMask;                  StructureNotifyMask | FocusChangeMask;
355    
356          if (grab_keyboard)          if (grab_keyboard)
357                  input_mask |= EnterWindowMask | LeaveWindowMask;                  input_mask |= EnterWindowMask | LeaveWindowMask;
# Line 402  ui_create_window() Line 359  ui_create_window()
359                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
360          if (ownbackstore)          if (ownbackstore)
361                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
362          if (enable_compose)  
363                  input_mask |= init_inputmethod();          if (IM != NULL)
364            {
365                    IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
366                                   XNClientWindow, wnd, XNFocusWindow, wnd, NULL);
367    
368                    if ((IC != NULL) && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL))
369                            input_mask |= ic_input_mask;
370            }
371    
372          XSelectInput(display, wnd, input_mask);          XSelectInput(display, wnd, input_mask);
373            XMapWindow(display, wnd);
374    
375          xwin_map_window();          /* wait for MapNotify */
376            do {
377                    XMaskEvent(display, StructureNotifyMask, &xevent);
378            } while (xevent.type != MapNotify);
379    
380          /* clear the window so that cached data is not seen */          if (fullscreen)
381          gc = XCreateGC(display, wnd, 0, NULL);                  XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);
         XSetForeground(display, gc, 0);  
         FILL_RECTANGLE(0, 0, width, height);  
382    
383          return True;          return True;
384  }  }
# Line 420  ui_create_window() Line 386  ui_create_window()
386  void  void
387  ui_destroy_window()  ui_destroy_window()
388  {  {
389          if (ownbackstore)          if (IC != NULL)
390                  XFreePixmap(display, backstore);                  XDestroyIC(IC);
   
         XFreeGC(display, gc);  
   
         close_inputmethod();  
391    
392          XDestroyWindow(display, wnd);          XDestroyWindow(display, wnd);
         XCloseDisplay(display);  
         display = NULL;  
 }  
   
 static void  
 xwin_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);  
393  }  }
394    
395  void  void
396  xwin_toggle_fullscreen()  xwin_toggle_fullscreen()
397  {  {
398          XEvent xevent;          Pixmap contents = 0;
         XSetWindowAttributes attribs;  
         int newwidth, newheight;  
399    
400            if (!ownbackstore)
401            {
402                    /* need to save contents of window */
403                    contents = XCreatePixmap(display, wnd, width, height, depth);
404                    XCopyArea(display, wnd, contents, gc, 0, 0, width, height, 0, 0);
405            }
406    
407            ui_destroy_window();
408          fullscreen = !fullscreen;          fullscreen = !fullscreen;
409          newwidth  = fullscreen ? WidthOfScreen(screen) : width;          ui_create_window();
         newheight = fullscreen ? HeightOfScreen(screen) : height;  
410    
411          XUnmapWindow(display, wnd);          XDefineCursor(display, wnd, current_cursor);
412          attribs.override_redirect = fullscreen;  
413          XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight);          if (!ownbackstore)
414          XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs);          {
415          xwin_map_window();                  XCopyArea(display, contents, wnd, gc, 0, 0, width, height, 0, 0);
416                    XFreePixmap(display, contents);
417            }
418  }  }
419    
420  /* Process all events in Xlib queue */  /* Process all events in Xlib queue */
# Line 481  xwin_process_events() Line 434  xwin_process_events()
434          {          {
435                  XNextEvent(display, &xevent);                  XNextEvent(display, &xevent);
436    
437                  if (enable_compose && (XFilterEvent(&xevent, None) == True))                  if ((IC != NULL) && (XFilterEvent(&xevent, None) == True))
438                  {                  {
439                          DEBUG_KBD(("Filtering event\n"));                          DEBUG_KBD(("Filtering event\n"));
440                          continue;                          continue;
# Line 509  xwin_process_events() Line 462  xwin_process_events()
462                                  else                                  else
463                                  {                                  {
464                                          /* Plain old XLookupString */                                          /* Plain old XLookupString */
465                                          DEBUG_KBD(("No input context, using XLookupString\n"));                                          DEBUG_KBD(("\nNo input context, using XLookupString\n"));
466                                          XLookupString((XKeyEvent *) & xevent,                                          XLookupString((XKeyEvent *) & xevent,
467                                                        str, sizeof(str), &keysym, NULL);                                                        str, sizeof(str), &keysym, NULL);
468                                  }                                  }
469    
470                                  ksname = get_ksname(keysym);                                  ksname = get_ksname(keysym);
471                                  DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));                                  DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, ksname));
472    
473                                  if (handle_special_keys(keysym, ev_time, True))                                  if (handle_special_keys(keysym, ev_time, True))
474                                          break;                                          break;
# Line 568  xwin_process_events() Line 521  xwin_process_events()
521                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);                                                 MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y);
522                                  break;                                  break;
523    
                         case FocusIn:  
                                 /* fall through */  
524                          case EnterNotify:                          case EnterNotify:
525                                  if (grab_keyboard)                                  if (grab_keyboard)
526                                          XGrabKeyboard(display, wnd, True,                                          XGrabKeyboard(display, wnd, True,
527                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
528                                  break;                                  break;
529    
                         case FocusOut:  
                                 xwin_reset_keys();  
                                 /* fall through */  
530                          case LeaveNotify:                          case LeaveNotify:
531                                  if (grab_keyboard)                                  if (grab_keyboard)
532                                          XUngrabKeyboard(display, CurrentTime);                                          XUngrabKeyboard(display, CurrentTime);
533                                  break;                                  break;
534    
535                            case FocusIn:
536                                    reset_modifier_keys();
537                                    break;
538    
539                          case Expose:                          case Expose:
540                                  XCopyArea(display, backstore, wnd, gc,                                  XCopyArea(display, backstore, wnd, gc,
541                                            xevent.xexpose.x, xevent.xexpose.y,                                            xevent.xexpose.x, xevent.xexpose.y,
# Line 648  ui_create_bitmap(int width, int height, Line 600  ui_create_bitmap(int width, int height,
600          Pixmap bitmap;          Pixmap bitmap;
601          uint8 *tdata;          uint8 *tdata;
602    
603          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = translate_image(width, height, data);
604          bitmap = XCreatePixmap(display, wnd, width, height, depth);          bitmap = XCreatePixmap(display, wnd, width, height, depth);
605          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
606                               (char *) tdata, width, height, 8, 0);                               (char *) tdata, width, height, 8, 0);
# Line 656  ui_create_bitmap(int width, int height, Line 608  ui_create_bitmap(int width, int height,
608          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);          XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
609    
610          XFree(image);          XFree(image);
611          if (!owncolmap)          xfree(tdata);
                 xfree(tdata);  
612          return (HBITMAP) bitmap;          return (HBITMAP) bitmap;
613  }  }
614    
# Line 667  ui_paint_bitmap(int x, int y, int cx, in Line 618  ui_paint_bitmap(int x, int y, int cx, in
618          XImage *image;          XImage *image;
619          uint8 *tdata;          uint8 *tdata;
620    
621          tdata = (owncolmap ? data : translate_image(width, height, data));          tdata = translate_image(width, height, data);
622          image = XCreateImage(display, visual, depth, ZPixmap, 0,          image = XCreateImage(display, visual, depth, ZPixmap, 0,
623                               (char *) tdata, width, height, 8, 0);                               (char *) tdata, width, height, 8, 0);
624    
# Line 682  ui_paint_bitmap(int x, int y, int cx, in Line 633  ui_paint_bitmap(int x, int y, int cx, in
633          }          }
634    
635          XFree(image);          XFree(image);
636          if (!owncolmap)          xfree(tdata);
                 xfree(tdata);  
637  }  }
638    
639  void  void
# Line 798  ui_create_cursor(unsigned int x, unsigne Line 748  ui_create_cursor(unsigned int x, unsigne
748  void  void
749  ui_set_cursor(HCURSOR cursor)  ui_set_cursor(HCURSOR cursor)
750  {  {
751          XDefineCursor(display, wnd, (Cursor) cursor);          current_cursor = (Cursor) cursor;
752            XDefineCursor(display, wnd, current_cursor);
753  }  }
754    
755  void  void
# Line 818  ui_create_colourmap(COLOURMAP * colours) Line 769  ui_create_colourmap(COLOURMAP * colours)
769  {  {
770          COLOURENTRY *entry;          COLOURENTRY *entry;
771          int i, ncolours = colours->ncolours;          int i, ncolours = colours->ncolours;
772            uint32 *map = xmalloc(sizeof(*colmap) * ncolours);
773            XColor xentry;
774            XColor xc_cache[256];
775            uint32 colour;
776            int colLookup = 256;
777            for (i = 0; i < ncolours; i++)
778            {
779                    entry = &colours->colours[i];
780                    MAKE_XCOLOR(&xentry, entry);
781    
782                    if (XAllocColor(display, xcolmap, &xentry) == 0)
783                    {
784                            /* Allocation failed, find closest match. */
785                            int j = 256;
786                            int nMinDist = 3 * 256 * 256;
787                            long nDist = nMinDist;
788    
789                            /* only get the colors once */
790                            while( colLookup-- ){
791                                    xc_cache[colLookup].pixel = colLookup;
792                                    xc_cache[colLookup].red = xc_cache[colLookup].green = xc_cache[colLookup].blue = 0;
793                                    xc_cache[colLookup].flags = 0;
794                                    XQueryColor(display, DefaultColormap(display, DefaultScreen(display)), &xc_cache[colLookup]);
795                            }
796                            colLookup = 0;
797    
798          if (owncolmap)                          /* approximate the pixel */
799          {                          while( j-- ){
800                  XColor *xcolours, *xentry;                                  if( xc_cache[j].flags ){
801                  Colormap map;                                          nDist =
802                                            ((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) *
803                  xcolours = xmalloc(sizeof(XColor) * ncolours);                                          ((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) +
804                  for (i = 0; i < ncolours; i++)                                          ((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) *
805                  {                                          ((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) +
806                          entry = &colours->colours[i];                                          ((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8)) *
807                          xentry = &xcolours[i];                                          ((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8));
808                          xentry->pixel = i;                                  }
809                          MAKE_XCOLOR(xentry, entry);                                  if( nDist < nMinDist ){
810                                            nMinDist = nDist;
811                                            xentry.pixel = j;
812                                    }
813                            }
814                  }                  }
815                    colour = xentry.pixel;
816    
817                  map = XCreateColormap(display, wnd, visual, AllocAll);                  /* update our cache */
818                  XStoreColors(display, map, xcolours, ncolours);                  if( xentry.pixel < 256 ){
819                            xc_cache[xentry.pixel].red = xentry.red;
820                  xfree(xcolours);                          xc_cache[xentry.pixel].green = xentry.green;
821                  return (HCOLOURMAP) map;                          xc_cache[xentry.pixel].blue = xentry.blue;
         }  
         else  
         {  
                 uint32 *map = xmalloc(sizeof(*colmap) * ncolours);  
                 XColor xentry;  
                 uint32 colour;  
822    
                 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);  
823                  }                  }
824    
825                  return map;  
826                    /* byte swap here to make translate_image faster */
827                    map[i] = translate_colour(colour);
828          }          }
829    
830            return map;
831  }  }
832    
833  void  void
834  ui_destroy_colourmap(HCOLOURMAP map)  ui_destroy_colourmap(HCOLOURMAP map)
835  {  {
836          if (owncolmap)          xfree(map);
                 XFreeColormap(display, (Colormap) map);  
         else  
                 xfree(map);  
837  }  }
838    
839  void  void
840  ui_set_colourmap(HCOLOURMAP map)  ui_set_colourmap(HCOLOURMAP map)
841  {  {
842          if (owncolmap)          colmap = map;
                 XSetWindowColormap(display, wnd, (Colormap) map);  
         else  
                 colmap = map;  
843  }  }
844    
845  void  void

Legend:
Removed from v.157  
changed lines
  Added in v.188

  ViewVC Help
Powered by ViewVC 1.1.26