/[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 524 by matthewc, Wed Oct 29 06:29:05 2003 UTC revision 545 by stargo, Mon Nov 3 20:19:01 2003 UTC
# Line 20  Line 20 
20    
21  #include <X11/Xlib.h>  #include <X11/Xlib.h>
22  #include <X11/Xutil.h>  #include <X11/Xutil.h>
23    #include <unistd.h>
24  #include <time.h>  #include <time.h>
25  #include <errno.h>  #include <errno.h>
26  #include "rdesktop.h"  #include "rdesktop.h"
# Line 57  static BOOL g_mouse_in_wnd; Line 58  static BOOL g_mouse_in_wnd;
58  /* endianness */  /* endianness */
59  static BOOL g_host_be;  static BOOL g_host_be;
60  static BOOL g_xserver_be;  static BOOL g_xserver_be;
61  static BOOL g_xserver_bgr;  static int g_red_shift_r, g_blue_shift_r, g_green_shift_r;
62    static int g_red_shift_l, g_blue_shift_l, g_green_shift_l;
63    
64  /* software backing store */  /* software backing store */
65  static BOOL g_ownbackstore;  static BOOL g_ownbackstore;
# Line 113  BOOL g_owncolmap = False; Line 115  BOOL g_owncolmap = False;
115  static Colormap g_xcolmap;  static Colormap g_xcolmap;
116  static uint32 *g_colmap = NULL;  static uint32 *g_colmap = NULL;
117    
118  #define TRANSLATE(col)          ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : translate_colour(g_colmap[col]) )  #define TRANSLATE(col)          ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : g_colmap[col] )
119  #define SET_FOREGROUND(col)     XSetForeground(g_display, g_gc, TRANSLATE(col));  #define SET_FOREGROUND(col)     XSetForeground(g_display, g_gc, TRANSLATE(col));
120  #define SET_BACKGROUND(col)     XSetBackground(g_display, g_gc, TRANSLATE(col));  #define SET_BACKGROUND(col)     XSetBackground(g_display, g_gc, TRANSLATE(col));
121    
# Line 198  split_colour24(uint32 colour) Line 200  split_colour24(uint32 colour)
200  }  }
201    
202  static uint32  static uint32
203  make_colour16(PixelColour pc)  make_colour(PixelColour pc)
204  {  {
205          pc.red = (pc.red * 0x1f) / 0xff;          return (((pc.red >> g_red_shift_r) << g_red_shift_l)
206          pc.green = (pc.green * 0x3f) / 0xff;                  | ((pc.green >> g_green_shift_r) << g_green_shift_l)
207          pc.blue = (pc.blue * 0x1f) / 0xff;                  | ((pc.blue >> g_blue_shift_r) << g_blue_shift_l));
         if (g_xserver_bgr)  
                 return (pc.blue << 11) | (pc.green << 5) | pc.red;  
         else  
                 return (pc.red << 11) | (pc.green << 5) | pc.blue;  
                   
 }  
   
 static uint32  
 make_colour24(PixelColour pc)  
 {  
         if (g_xserver_bgr)  
                 return (pc.blue << 16) | (pc.green << 8) | pc.red;  
         else  
                 return (pc.red << 16) | (pc.green << 8) | pc.blue;  
 }  
   
 static uint32  
 make_colour32(PixelColour pc)  
 {  
         if (g_xserver_bgr)  
                 return (pc.blue << 16) | (pc.green << 8) | pc.red;  
         else  
                 return (pc.red << 16) | (pc.green << 8) | pc.blue;  
208  }  }
209    
210  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }  #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); }
211  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); }  #define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | (x & 0xff00)); }
212  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \  #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \
213                          x = (x << 16) | (x >> 16); }                          x = (x << 16) | (x >> 16); }
214    
215  static uint32  static uint32
216  translate_colour(uint32 colour)  translate_colour(uint32 colour)
217  {  {
218            PixelColour pc;
219          switch (g_server_bpp)          switch (g_server_bpp)
220          {          {
221                  case 15:                  case 15:
222                          switch (g_bpp)                          pc = split_colour15(colour);
                         {  
                                 case 16:  
                                         colour = make_colour16(split_colour15(colour));  
                                         break;  
                                 case 24:  
                                         colour = make_colour24(split_colour15(colour));  
                                         break;  
                                 case 32:  
                                         colour = make_colour32(split_colour15(colour));  
                                         break;  
                         }  
223                          break;                          break;
224                  case 16:                  case 16:
225                          switch (g_bpp)                          pc = split_colour16(colour);
                         {  
                                 case 16:  
                                         break;  
                                 case 24:  
                                         colour = make_colour24(split_colour16(colour));  
                                         break;  
                                 case 32:  
                                         colour = make_colour32(split_colour16(colour));  
                                         break;  
                         }  
226                          break;                          break;
227                  case 24:                  case 24:
228                          switch (g_bpp)                          pc = split_colour24(colour);
                         {  
                                 case 16:  
                                         colour = make_colour16(split_colour24(colour));  
                                         break;  
                                 case 24:  
                                         colour = make_colour24(split_colour24(colour));  
                                         break;  
                                 case 32:  
                                         colour = make_colour32(split_colour24(colour));  
                                         break;  
                         }  
229                          break;                          break;
230          }          }
231          return colour;          return make_colour(pc);
232  }  }
233    
234  static void  static void
# Line 298  translate8to16(uint8 * data, uint8 * out Line 246  translate8to16(uint8 * data, uint8 * out
246          while (out < end)          while (out < end)
247          {          {
248                  value = (uint16) g_colmap[*(data++)];                  value = (uint16) g_colmap[*(data++)];
249                    
250                  if (g_xserver_be)                  if (g_xserver_be)
251                  {                  {
252                          *(out++) = value >> 8;                          *(out++) = value >> 8;
# Line 321  translate8to24(uint8 * data, uint8 * out Line 269  translate8to24(uint8 * data, uint8 * out
269          while (out < end)          while (out < end)
270          {          {
271                  value = g_colmap[*(data++)];                  value = g_colmap[*(data++)];
272                    
273                  if (g_xserver_be)                  if (g_xserver_be)
274                  {                  {
275                          *(out++) = value >> 16;                          *(out++) = value >> 16;
# Line 363  translate8to32(uint8 * data, uint8 * out Line 311  translate8to32(uint8 * data, uint8 * out
311          }          }
312  }  }
313    
 /* todo the remaining translate function might need some big endian check ?? */  
   
314  static void  static void
315  translate15to16(uint16 * data, uint8 * out, uint8 * end)  translate15to16(uint16 * data, uint8 * out, uint8 * end)
316  {  {
# Line 380  translate15to16(uint16 * data, uint8 * o Line 326  translate15to16(uint16 * data, uint8 * o
326                          BSWAP16(pixel);                          BSWAP16(pixel);
327                  }                  }
328    
329                  value = make_colour16(split_colour15(pixel));                  value = make_colour(split_colour15(pixel));
330    
331                  if (g_xserver_be)                  if (g_xserver_be)
332                  {                  {
# Line 410  translate15to24(uint16 * data, uint8 * o Line 356  translate15to24(uint16 * data, uint8 * o
356                          BSWAP16(pixel);                          BSWAP16(pixel);
357                  }                  }
358    
359                  value = make_colour24(split_colour15(pixel));                  value = make_colour(split_colour15(pixel));
360                  if (g_xserver_be)                  if (g_xserver_be)
361                  {                  {
362                          *(out++) = value >> 16;                          *(out++) = value >> 16;
# Line 441  translate15to32(uint16 * data, uint8 * o Line 387  translate15to32(uint16 * data, uint8 * o
387                          BSWAP16(pixel);                          BSWAP16(pixel);
388                  }                  }
389    
390                  value = make_colour32(split_colour15(pixel));                  value = make_colour(split_colour15(pixel));
391    
392                  if (g_xserver_be)                  if (g_xserver_be)
393                  {                  {
# Line 461  translate15to32(uint16 * data, uint8 * o Line 407  translate15to32(uint16 * data, uint8 * o
407  }  }
408    
409  static void  static void
410  translate16to16(uint16 * data, uint16 * out, uint16 * end)  translate16to16(uint16 * data, uint8 * out, uint8 * end)
411  {  {
412            uint16 pixel;
413          uint16 value;          uint16 value;
414    
415          if (g_xserver_be)          while (out < end)
416          {          {
417                  while (out < end)                  pixel = *(data++);
418    
419                    if (g_host_be)
420                  {                  {
421                          value = *data;                          BSWAP16(pixel);
                         BSWAP16(value);  
                         *out = value;  
                         data++;  
                         out++;  
422                  }                  }
423    
424          }                  value = make_colour(split_colour16(pixel));
425          else  
426          {                  if (g_xserver_be)
427                  while (out < end)                  {
428                            *(out++) = value >> 8;
429                            *(out++) = value;
430                    }
431                    else
432                  {                  {
433                          *out = *data;                          *(out++) = value;
434                          out++;                          *(out++) = value >> 8;
                         data++;  
435                  }                  }
436          }          }
437  }  }
438    
   
439  static void  static void
440  translate16to24(uint16 * data, uint8 * out, uint8 * end)  translate16to24(uint16 * data, uint8 * out, uint8 * end)
441  {  {
# Line 504  translate16to24(uint16 * data, uint8 * o Line 451  translate16to24(uint16 * data, uint8 * o
451                          BSWAP16(pixel);                          BSWAP16(pixel);
452                  }                  }
453    
454                  value = make_colour24(split_colour16(pixel));                  value = make_colour(split_colour16(pixel));
455    
456                  if (g_xserver_be)                  if (g_xserver_be)
457                  {                  {
# Line 533  translate16to32(uint16 * data, uint8 * o Line 480  translate16to32(uint16 * data, uint8 * o
480    
481                  if (g_host_be)                  if (g_host_be)
482                  {                  {
483                  BSWAP16(pixel)}                          BSWAP16(pixel);
484                    }
485    
486                  value = make_colour32(split_colour16(pixel));                  value = make_colour(split_colour16(pixel));
487    
488                  if (g_xserver_be)                  if (g_xserver_be)
489                  {                  {
# Line 565  translate24to16(uint8 * data, uint8 * ou Line 513  translate24to16(uint8 * data, uint8 * ou
513                  pixel |= *(data++) << 8;                  pixel |= *(data++) << 8;
514                  pixel |= *(data++);                  pixel |= *(data++);
515    
516                  value = (uint16) make_colour16(split_colour24(pixel));                  value = (uint16) make_colour(split_colour24(pixel));
517    
518                  if (g_xserver_be)                  if (g_xserver_be)
519                  {                  {
# Line 583  translate24to16(uint8 * data, uint8 * ou Line 531  translate24to16(uint8 * data, uint8 * ou
531  static void  static void
532  translate24to24(uint8 * data, uint8 * out, uint8 * end)  translate24to24(uint8 * data, uint8 * out, uint8 * end)
533  {  {
534            uint32 pixel;
535            uint32 value;
536    
537          while (out < end)          while (out < end)
538          {          {
539                  *(out++) = (*(data++));                  pixel = *(data++) << 16;
540                    pixel |= *(data++) << 8;
541                    pixel |= *(data++);
542    
543                    value = make_colour(split_colour24(pixel));
544    
545                    if (g_xserver_be)
546                    {
547                            *(out++) = value >> 16;
548                            *(out++) = value >> 8;
549                            *(out++) = value;
550                    }
551                    else
552                    {
553                            *(out++) = value;
554                            *(out++) = value >> 8;
555                            *(out++) = value >> 16;
556                    }
557          }          }
558  }  }
559    
560  static void  static void
561  translate24to32(uint8 * data, uint8 * out, uint8 * end)  translate24to32(uint8 * data, uint8 * out, uint8 * end)
562  {  {
563            uint32 pixel;
564            uint32 value;
565    
566          while (out < end)          while (out < end)
567          {          {
568                    pixel = *(data++) << 16;
569                    pixel |= *(data++) << 8;
570                    pixel |= *(data++);
571    
572                    value = make_colour(split_colour24(pixel));
573    
574                  if (g_xserver_be)                  if (g_xserver_be)
575                  {                  {
576                          *(out++) = 0x00;                          *(out++) = value >> 24;
577                          *(out++) = *(data++);                          *(out++) = value >> 16;
578                          *(out++) = *(data++);                          *(out++) = value >> 8;
579                          *(out++) = *(data++);                          *(out++) = value;
580                  }                  }
581                  else                  else
582                  {                  {
583                          *(out++) = *(data++);                          *(out++) = value;
584                          *(out++) = *(data++);                          *(out++) = value >> 8;
585                          *(out++) = *(data++);                          *(out++) = value >> 16;
586                          *(out++) = 0x00;                          *(out++) = value >> 24;
587                  }                  }
588          }          }
589  }  }
# Line 644  translate_image(int width, int height, u Line 621  translate_image(int width, int height, u
621                                          translate16to24((uint16 *) data, out, end);                                          translate16to24((uint16 *) data, out, end);
622                                          break;                                          break;
623                                  case 16:                                  case 16:
624                                          translate16to16((uint16 *) data, (uint16 *) out,                                          translate16to16((uint16 *) data, out, end);
                                                         (uint16 *) end);  
625                                          break;                                          break;
626                          }                          }
627                          break;                          break;
# Line 709  get_key_state(unsigned int state, uint32 Line 685  get_key_state(unsigned int state, uint32
685          return (state & keysymMask) ? True : False;          return (state & keysymMask) ? True : False;
686  }  }
687    
688    static void
689    calculate_shifts(uint32 mask, int *shift_r, int *shift_l)
690    {
691            *shift_l = ffs(mask) - 1;
692            mask >>= *shift_l;
693            *shift_r = 8 - ffs(mask & ~(mask >> 1));
694    }
695    
696  BOOL  BOOL
697  ui_init(void)  ui_init(void)
698  {  {
699            XVisualInfo vi;
700          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
701          uint16 test;          uint16 test;
702          int i;          int i, screen_num;
703    
704          g_display = XOpenDisplay(NULL);          g_display = XOpenDisplay(NULL);
705          if (g_display == NULL)          if (g_display == NULL)
# Line 723  ui_init(void) Line 708  ui_init(void)
708                  return False;                  return False;
709          }          }
710    
711            screen_num = DefaultScreen(g_display);
712          g_x_socket = ConnectionNumber(g_display);          g_x_socket = ConnectionNumber(g_display);
713          g_screen = DefaultScreenOfDisplay(g_display);          g_screen = ScreenOfDisplay(g_display, screen_num);
         g_visual = DefaultVisualOfScreen(g_screen);  
714          g_depth = DefaultDepthOfScreen(g_screen);          g_depth = DefaultDepthOfScreen(g_screen);
715    
716            if (g_server_bpp == 8)
717            {
718                    /* we use a colourmap, so any visual should do */
719                    g_visual = DefaultVisualOfScreen(g_screen);
720            }
721            else
722            {
723                    /* need a truecolour visual */
724                    if (!XMatchVisualInfo(g_display, screen_num, g_depth, TrueColor, &vi))
725                    {
726                            error("The display does not support true colour - high colour support unavailable.\n");
727                            return False;
728                    }
729    
730                    g_visual = vi.visual;
731                    g_owncolmap = False;
732                    calculate_shifts(vi.red_mask, &g_red_shift_r, &g_red_shift_l);
733                    calculate_shifts(vi.blue_mask, &g_blue_shift_r, &g_blue_shift_l);
734                    calculate_shifts(vi.green_mask, &g_green_shift_r, &g_green_shift_l);
735            }
736    
737          pfm = XListPixmapFormats(g_display, &i);          pfm = XListPixmapFormats(g_display, &i);
738          if (pfm != NULL)          if (pfm != NULL)
739          {          {
# Line 750  ui_init(void) Line 756  ui_init(void)
756                  return False;                  return False;
757          }          }
758    
         /* private colour map code only works for 8 bpp */  
         if (g_owncolmap && (g_bpp > 8))  
                 g_owncolmap = False;  
   
759          if (!g_owncolmap)          if (!g_owncolmap)
760          {          {
761                  g_xcolmap = DefaultColormapOfScreen(g_screen);                  g_xcolmap = DefaultColormapOfScreen(g_screen);
# Line 769  ui_init(void) Line 771  ui_init(void)
771          test = 1;          test = 1;
772          g_host_be = !(BOOL) (*(uint8 *) (&test));          g_host_be = !(BOOL) (*(uint8 *) (&test));
773          g_xserver_be = (ImageByteOrder(g_display) == MSBFirst);          g_xserver_be = (ImageByteOrder(g_display) == MSBFirst);
         g_xserver_bgr = (g_visual->blue_mask > g_visual->red_mask);  
774    
775          /*          /*
776           * Determine desktop size           * Determine desktop size
# Line 847  ui_deinit(void) Line 848  ui_deinit(void)
848          g_display = NULL;          g_display = NULL;
849  }  }
850    
 #define NULL_POINTER_MASK       "\x80"  
 #define NULL_POINTER_DATA       "\x0\x0\x0"  
           
851  BOOL  BOOL
852  ui_create_window(void)  ui_create_window(void)
853  {  {
854            uint8 null_pointer_mask[1] = { 0x80 };
855            uint8 null_pointer_data[4] = { 0x00, 0x00, 0x00, 0x00 };
856          XSetWindowAttributes attribs;          XSetWindowAttributes attribs;
857          XClassHint *classhints;          XClassHint *classhints;
858          XSizeHints *sizehints;          XSizeHints *sizehints;
# Line 935  ui_create_window(void) Line 935  ui_create_window(void)
935          XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1);          XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1);
936    
937          /* create invisible 1x1 cursor to be used as null cursor */          /* create invisible 1x1 cursor to be used as null cursor */
938          g_null_cursor = ui_create_cursor(0, 0, 1, 1, NULL_POINTER_MASK, NULL_POINTER_DATA);          g_null_cursor = ui_create_cursor(0, 0, 1, 1, null_pointer_mask, null_pointer_data);
939    
940          return True;          return True;
941  }  }
# Line 943  ui_create_window(void) Line 943  ui_create_window(void)
943  void  void
944  ui_destroy_window(void)  ui_destroy_window(void)
945  {  {
         ui_destroy_cursor(g_null_cursor);  
           
946          if (g_IC != NULL)          if (g_IC != NULL)
947                  XDestroyIC(g_IC);                  XDestroyIC(g_IC);
948    
# Line 988  xwin_process_events(void) Line 986  xwin_process_events(void)
986          key_translation tr;          key_translation tr;
987          char str[256];          char str[256];
988          Status status;          Status status;
         unsigned int state;  
         Window wdummy;  
         int dummy;  
989    
990          while (XPending(g_display) > 0)          while (XPending(g_display) > 0)
991          {          {
# Line 1163  xwin_process_events(void) Line 1158  xwin_process_events(void)
1158                                  if (xevent.xfocus.mode == NotifyGrab)                                  if (xevent.xfocus.mode == NotifyGrab)
1159                                          break;                                          break;
1160                                  g_focused = True;                                  g_focused = True;
1161                                  XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy,                                  reset_modifier_keys();
                                               &dummy, &dummy, &state);  
                                 reset_modifier_keys(state);  
1162                                  if (g_grab_keyboard && g_mouse_in_wnd)                                  if (g_grab_keyboard && g_mouse_in_wnd)
1163                                          XGrabKeyboard(g_display, g_wnd, True,                                          XGrabKeyboard(g_display, g_wnd, True,
1164                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
# Line 1574  ui_create_colourmap(COLOURMAP * colours) Line 1567  ui_create_colourmap(COLOURMAP * colours)
1567    
1568                          }                          }
1569    
1570                            map[i] = colour;
                         /* byte swap here to make translate_image faster */  
                         map[i] = translate_colour(colour);  
1571                  }                  }
1572                  return map;                  return map;
1573          }          }

Legend:
Removed from v.524  
changed lines
  Added in v.545

  ViewVC Help
Powered by ViewVC 1.1.26