/[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 101 by astrand, Mon Aug 26 17:12:43 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 = NULL;  Display *display;
39  static int x_socket;  static int x_socket;
40    static Screen *screen;
41  static Window wnd;  static Window wnd;
42  static GC gc;  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 int dpy_width;  static XIM IM;
47  static int dpy_height;  static XIC IC;
48    static Cursor current_cursor;
49    
50  /* endianness */  /* endianness */
51  static BOOL host_be;  static BOOL host_be;
# Line 60  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;
 static uint32 white;  
67  static uint32 *colmap;  static uint32 *colmap;
 static XIM IM = NULL;  
 static XIC IC = NULL;  
   
 /* Compose support */  
 BOOL enable_compose = False;  
68    
69  /* toggle fullscreen globals */  #define SET_FOREGROUND(col)     XSetForeground(display, gc, translate_colour(colmap[col]));
70  static XSetWindowAttributes attribs;  #define SET_BACKGROUND(col)     XSetBackground(display, gc, translate_colour(colmap[col]));
 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 194  translate_colour(uint32 colour) Line 185  translate_colour(uint32 colour)
185          return colour;          return colour;
186  }  }
187    
188  static unsigned long  BOOL
189  init_inputmethod(void)  get_key_state(int keysym)
190  {  {
191          unsigned long filtered_events = 0;          int keysymMask = 0, modifierpos, key;
192            Window wDummy1, wDummy2;
193            int iDummy3, iDummy4, iDummy5, iDummy6;
194            unsigned int current_state;
195            int offset;
196    
197          IM = XOpenIM(display, NULL, NULL, NULL);          XModifierKeymap *map = XGetModifierMapping(display);
198          if (IM == NULL)          KeyCode keycode = XKeysymToKeycode(display, keysym);
         {  
                 error("Failed to open input method\n");  
         }  
199    
200          if (IM != NULL)          if (keycode == NoSymbol)
201                    return False;
202    
203            for (modifierpos = 0; modifierpos < 8; modifierpos++)
204          {          {
205                  /* Must be done after XCreateWindow */                  offset = map->max_keypermod * modifierpos;
                 IC = XCreateIC(IM, XNInputStyle,  
                                (XIMPreeditNothing | XIMStatusNothing),  
                                XNClientWindow, wnd, XNFocusWindow, wnd, NULL);  
206    
207                  if (IC == NULL)                  for (key = 0; key < map->max_keypermod; key++)
208                  {                  {
209                          error("Failed to create input context\n");                          if (map->modifiermap[offset + key] == keycode)
210                          XCloseIM(IM);                                  keysymMask = 1 << modifierpos;
                         IM = NULL;  
211                  }                  }
212          }          }
213    
214          /* For correct Multi_key/Compose processing, I guess.          XQueryPointer(display, DefaultRootWindow(display), &wDummy1,
215             It seems to work alright anyway, though. */                        &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, &current_state);
         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;  
 }  
216    
217  static void          XFreeModifiermap(map);
218  close_inputmethod(void)  
219  {          return (current_state & keysymMask) ? True : False;
         if (IC != NULL)  
         {  
                 XDestroyIC(IC);  
                 if (IM != NULL)  
                 {  
                         XCloseIM(IM);  
                         IM = NULL;  
                 }  
         }  
220  }  }
221    
222  BOOL  BOOL
223  ui_init()  ui_init()
224  {  {
225          Screen *screen;          XPixmapFormatValues *pfm;
226            uint16 test;
227            int i;
228    
229          display = XOpenDisplay(NULL);          display = XOpenDisplay(NULL);
230          if (display == NULL)          if (display == NULL)
231          {          {
232                  error("Failed to open display\n");                  error("Failed to open display\n");
233                  return False;                  return False;
234          }          }
         if (fullscreen)  
         {  
                 screen = DefaultScreenOfDisplay(display);  
                 width = WidthOfScreen(screen);  
                 height = HeightOfScreen(screen);  
         }  
         return True;  
 }  
   
 BOOL  
 ui_create_window_obj(int xpos, int ypos, int width, int height, int valuemask)  
 {  
         XClassHint *classhints;  
         XSizeHints *sizehints;  
         XEvent xevent;  
         Screen *screen;  
   
         screen = DefaultScreenOfDisplay(display);  
   
         wnd = XCreateWindow(display, RootWindowOfScreen(screen), xpos,  
                             ypos, width, height, 0, CopyFromParent,  
                             InputOutput, CopyFromParent, valuemask, &attribs);  
   
   
         XStoreName(display, wnd, title);  
   
         classhints = XAllocClassHint();  
         if (classhints != NULL)  
         {  
                 classhints->res_name = classhints->res_class = "rdesktop";  
                 XSetClassHint(display, wnd, classhints);  
                 XFree(classhints);  
         }  
   
         sizehints = XAllocSizeHints();  
         if (sizehints)  
         {  
                 sizehints->flags = PMinSize | PMaxSize;  
                 sizehints->min_width = sizehints->max_width = width;  
                 sizehints->min_height = sizehints->max_height = height;  
                 XSetWMNormalHints(display, wnd, sizehints);  
                 XFree(sizehints);  
         }  
   
         if (enable_compose)  
                 input_mask |= init_inputmethod();  
   
         XSelectInput(display, wnd, input_mask);  
   
         gc = XCreateGC(display, wnd, 0, NULL);  
   
         XMapWindow(display, wnd);  
   
         /* Wait for VisibilityNotify Event */  
         for (;;)  
         {  
                 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);  
         XSetForeground(display, gc, 0);  
         FILL_RECTANGLE(0, 0, width, height);  
         /* make sure the window is focused */  
         XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);  
 }  
   
 BOOL  
 ui_create_window()  
 {  
         XPixmapFormatValues *pfm;  
         Screen *screen;  
         uint16 test;  
         int i;  
235    
236          x_socket = ConnectionNumber(display);          x_socket = ConnectionNumber(display);
237          screen = DefaultScreenOfDisplay(display);          screen = DefaultScreenOfDisplay(display);
# Line 364  ui_create_window() Line 260  ui_create_window()
260                  return False;                  return False;
261          }          }
262    
263          if (depth <= 8)          xcolmap = DefaultColormapOfScreen(screen);
264                  owncolmap = True;          gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL);
265          else  
266                  xcolmap = DefaultColormapOfScreen(screen);          if (DoesBackingStore(screen) != Always)
267                    ownbackstore = True;
268    
269          test = 1;          test = 1;
270          host_be = !(BOOL) (*(uint8 *) (&test));          host_be = !(BOOL) (*(uint8 *) (&test));
271          xserver_be = (ImageByteOrder(display) == MSBFirst);          xserver_be = (ImageByteOrder(display) == MSBFirst);
272    
273          white = WhitePixelOfScreen(screen);          if (fullscreen)
274            {
275                    width = WidthOfScreen(screen);
276                    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();
295            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
313    ui_create_window()
314    {
315            XSetWindowAttributes attribs;
316            XClassHint *classhints;
317            XSizeHints *sizehints;
318            int wndwidth, wndheight;
319            long input_mask, ic_input_mask;
320            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 = DoesBackingStore(screen);          attribs.backing_store = ownbackstore ? NotUseful : Always;
327            attribs.override_redirect = fullscreen;
328    
329          if (attribs.backing_store == NotUseful)          wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight,
330                  ownbackstore = True;                              0, CopyFromParent, InputOutput, CopyFromParent,
331                                CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs);
332    
333          dpy_width = WidthOfScreen(screen);          XStoreName(display, wnd, title);
         dpy_height = HeightOfScreen(screen);  
334    
335          if (fullscreen)          classhints = XAllocClassHint();
336            if (classhints != NULL)
337          {          {
338                  attribs.override_redirect = True;                  classhints->res_name = classhints->res_class = "rdesktop";
339                  width = dpy_width;                  XSetClassHint(display, wnd, classhints);
340                  height = dpy_height;                  XFree(classhints);
341          }          }
342          else  
343            sizehints = XAllocSizeHints();
344            if (sizehints)
345          {          {
346                  attribs.override_redirect = False;                  sizehints->flags = PMinSize | PMaxSize;
347                    sizehints->min_width = sizehints->max_width = width;
348                    sizehints->min_height = sizehints->max_height = height;
349                    XSetWMNormalHints(display, wnd, sizehints);
350                    XFree(sizehints);
351          }          }
352    
         width = (width + 3) & ~3;       /* make width a multiple of 32 bits */  
   
   
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;
358          if (sendmotion)          if (sendmotion)
359                  input_mask |= PointerMotionMask;                  input_mask |= PointerMotionMask;
   
360          if (ownbackstore)          if (ownbackstore)
361                  input_mask |= ExposureMask;                  input_mask |= ExposureMask;
362    
363          if (fullscreen)          if (IM != NULL)
364                  ui_create_window_obj(0, 0, width, height,          {
365                                       CWBackingStore | CWBackPixel | CWOverrideRedirect);                  IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing),
366          else                                 XNClientWindow, wnd, XNFocusWindow, wnd, NULL);
367                  ui_create_window_obj(0, 0, width, height, CWBackingStore | CWBackPixel);  
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);
373            XMapWindow(display, wnd);
374    
375          xkeymap_init2();          /* wait for MapNotify */
376            do {
377                    XMaskEvent(display, StructureNotifyMask, &xevent);
378            } while (xevent.type != MapNotify);
379    
380            if (fullscreen)
381                    XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime);
382    
383          return True;          return True;
384  }  }
# Line 422  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;  
393  }  }
394    
395  void  void
396  reset_keys()  xwin_toggle_fullscreen()
397  {  {
398          /* reset keys */          Pixmap contents = 0;
         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);  
 }  
399    
400  void          if (!ownbackstore)
 toggle_fullscreen()  
 {  
         /* save window contents */  
         Pixmap pixmap;  
         pixmap = XCreatePixmap(display, wnd, width, height, depth);  
         if (ownbackstore)  
                 XCopyArea(display, backstore, pixmap, gc, 0, 0, width, height, 0, 0);  
         else  
                 XCopyArea(display, wnd, pixmap, gc, 0, 0, width, height, 0, 0);  
         fullscreen = fullscreen ? False : True;  
         close_inputmethod();  
         if (ownbackstore)  
                 XFreePixmap(display, backstore);  
         XFreeGC(display, gc);  
         XDestroyWindow(display, wnd);  
         if (fullscreen)  
401          {          {
402                  attribs.override_redirect = True;                  /* need to save contents of window */
403                  ui_create_window_obj(0, 0, dpy_width, dpy_height,                  contents = XCreatePixmap(display, wnd, width, height, depth);
404                                       CWBackingStore | CWBackPixel | CWOverrideRedirect);                  XCopyArea(display, wnd, contents, gc, 0, 0, width, height, 0, 0);
405          }          }
406          else  
407            ui_destroy_window();
408            fullscreen = !fullscreen;
409            ui_create_window();
410    
411            XDefineCursor(display, wnd, current_cursor);
412    
413            if (!ownbackstore)
414          {          {
415                  attribs.override_redirect = False;                  XCopyArea(display, contents, wnd, gc, 0, 0, width, height, 0, 0);
416                  ui_create_window_obj(0, 0, width, height, CWBackingStore | CWBackPixel);                  XFreePixmap(display, contents);
417          }          }
         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);  
418  }  }
419    
420    /* Process all events in Xlib queue */
421  static void  static void
422  xwin_process_events()  xwin_process_events()
423  {  {
424          XEvent xevent;          XEvent xevent;
   
425          KeySym keysym;          KeySym keysym;
426          uint16 button, flags;          uint16 button, flags;
427          uint32 ev_time;          uint32 ev_time;
# Line 498  xwin_process_events() Line 430  xwin_process_events()
430          char str[256];          char str[256];
431          Status status;          Status status;
432    
433          /* 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))  
434          {          {
435                  if (xevent.xmapping.request == MappingKeyboard                  XNextEvent(display, &xevent);
                     || xevent.xmapping.request == MappingModifier)  
                         XRefreshKeyboardMapping(&xevent.xmapping);  
         }  
436    
437          while (XCheckMaskEvent(display, ~0, &xevent))                  if ((IC != NULL) && (XFilterEvent(&xevent, None) == True))
         {  
                 if (enable_compose && (XFilterEvent(&xevent, None) == True))  
438                  {                  {
439                          DEBUG_KBD(("Filtering event\n"));                          DEBUG_KBD(("Filtering event\n"));
440                          continue;                          continue;
# Line 537  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    
                                 /* FIXME needs alt modifier */  
                                 if (keysym == XK_Break) /* toggle full screen */  
                                 {  
                                         toggle_fullscreen();  
                                         break;  
                                 }  
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 (inhibit_key(keysym))                                  if (handle_special_keys(keysym, ev_time, True))
                                 {  
                                         DEBUG_KBD(("Inhibiting key\n"));  
474                                          break;                                          break;
                                 }  
475    
476                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
477                                                             xevent.xkey.keycode, xevent.xkey.state);                                                             xevent.xkey.keycode, xevent.xkey.state);
478    
                                 ensure_remote_modifiers(ev_time, tr);  
   
479                                  if (tr.scancode == 0)                                  if (tr.scancode == 0)
480                                          break;                                          break;
481    
482                                    ensure_remote_modifiers(ev_time, tr);
483    
484                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);                                  rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode);
485                                  break;                                  break;
486                          case KeyRelease:                          case KeyRelease:
# Line 575  xwin_process_events() Line 491  xwin_process_events()
491                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,                                  DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym,
492                                             ksname));                                             ksname));
493    
494                                  if (inhibit_key(keysym))                                  if (handle_special_keys(keysym, ev_time, False))
495                                          break;                                          break;
496    
497                                  tr = xkeymap_translate_key(keysym,                                  tr = xkeymap_translate_key(keysym,
# Line 605  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:  
                                 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 628  xwin_process_events() Line 543  xwin_process_events()
543                                            xevent.xexpose.height,                                            xevent.xexpose.height,
544                                            xevent.xexpose.x, xevent.xexpose.y);                                            xevent.xexpose.x, xevent.xexpose.y);
545                                  break;                                  break;
546    
547                            case MappingNotify:
548                                    /* Refresh keyboard mapping if it has changed. This is important for
549                                       Xvnc, since it allocates keycodes dynamically */
550                                    if (xevent.xmapping.request == MappingKeyboard
551                                        || xevent.xmapping.request == MappingModifier)
552                                            XRefreshKeyboardMapping(&xevent.xmapping);
553                                    break;
554    
555                  }                  }
556          }          }
557  }  }
# Line 642  ui_select(int rdp_socket) Line 566  ui_select(int rdp_socket)
566    
567          while (True)          while (True)
568          {          {
569                    /* Process any events already waiting */
570                    xwin_process_events();
571    
572                  FD_ZERO(&rfds);                  FD_ZERO(&rfds);
573                  FD_SET(rdp_socket, &rfds);                  FD_SET(rdp_socket, &rfds);
574                  if (display != NULL)                  FD_SET(x_socket, &rfds);
                 {  
                         FD_SET(x_socket, &rfds);  
                         XFlush(display);  
                 }  
575    
576                  switch (select(n, &rfds, NULL, NULL, NULL))                  switch (select(n, &rfds, NULL, NULL, NULL))
577                  {                  {
# Line 659  ui_select(int rdp_socket) Line 582  ui_select(int rdp_socket)
582                                  continue;                                  continue;
583                  }                  }
584    
                 if (FD_ISSET(x_socket, &rfds))  
                         xwin_process_events();  
   
585                  if (FD_ISSET(rdp_socket, &rfds))                  if (FD_ISSET(rdp_socket, &rfds))
586                          return;                          return;
587          }          }
# Line 680  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 688  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 699  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 714  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 830  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 850  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;  
   
                 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;  
822    
                         /* 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.101  
changed lines
  Added in v.188

  ViewVC Help
Powered by ViewVC 1.1.26