/[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 543 by astrand, Mon Nov 3 13:33:35 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 380  translate15to16(uint16 * data, uint8 * o Line 328  translate15to16(uint16 * data, uint8 * o
328                          BSWAP16(pixel);                          BSWAP16(pixel);
329                  }                  }
330    
331                  value = make_colour16(split_colour15(pixel));                  value = make_colour(split_colour15(pixel));
332    
333                  if (g_xserver_be)                  if (g_xserver_be)
334                  {                  {
# Line 410  translate15to24(uint16 * data, uint8 * o Line 358  translate15to24(uint16 * data, uint8 * o
358                          BSWAP16(pixel);                          BSWAP16(pixel);
359                  }                  }
360    
361                  value = make_colour24(split_colour15(pixel));                  value = make_colour(split_colour15(pixel));
362                  if (g_xserver_be)                  if (g_xserver_be)
363                  {                  {
364                          *(out++) = value >> 16;                          *(out++) = value >> 16;
# Line 441  translate15to32(uint16 * data, uint8 * o Line 389  translate15to32(uint16 * data, uint8 * o
389                          BSWAP16(pixel);                          BSWAP16(pixel);
390                  }                  }
391    
392                  value = make_colour32(split_colour15(pixel));                  value = make_colour(split_colour15(pixel));
393    
394                  if (g_xserver_be)                  if (g_xserver_be)
395                  {                  {
# Line 461  translate15to32(uint16 * data, uint8 * o Line 409  translate15to32(uint16 * data, uint8 * o
409  }  }
410    
411  static void  static void
412  translate16to16(uint16 * data, uint16 * out, uint16 * end)  translate16to16(uint16 * data, uint8 * out, uint8 * end)
413  {  {
414            uint16 pixel;
415          uint16 value;          uint16 value;
416    
417          if (g_xserver_be)          while (out < end)
418          {          {
419                  while (out < end)                  pixel = *(data++);
420    
421                    if (g_host_be)
422                  {                  {
423                          value = *data;                          BSWAP16(pixel);
                         BSWAP16(value);  
                         *out = value;  
                         data++;  
                         out++;  
424                  }                  }
425    
426          }                  value = make_colour(split_colour16(pixel));
427          else  
428          {                  if (g_xserver_be)
                 while (out < end)  
429                  {                  {
430                          *out = *data;                          *(out++) = value >> 8;
431                          out++;                          *(out++) = value;
432                          data++;                  }
433                    else
434                    {
435                            *(out++) = value;
436                            *(out++) = value >> 8;
437                  }                  }
438          }          }
439  }  }
440    
   
441  static void  static void
442  translate16to24(uint16 * data, uint8 * out, uint8 * end)  translate16to24(uint16 * data, uint8 * out, uint8 * end)
443  {  {
# Line 504  translate16to24(uint16 * data, uint8 * o Line 453  translate16to24(uint16 * data, uint8 * o
453                          BSWAP16(pixel);                          BSWAP16(pixel);
454                  }                  }
455    
456                  value = make_colour24(split_colour16(pixel));                  value = make_colour(split_colour16(pixel));
457    
458                  if (g_xserver_be)                  if (g_xserver_be)
459                  {                  {
# Line 533  translate16to32(uint16 * data, uint8 * o Line 482  translate16to32(uint16 * data, uint8 * o
482    
483                  if (g_host_be)                  if (g_host_be)
484                  {                  {
485                  BSWAP16(pixel)}                          BSWAP16(pixel);
486                    }
487    
488                  value = make_colour32(split_colour16(pixel));                  value = make_colour(split_colour16(pixel));
489    
490                  if (g_xserver_be)                  if (g_xserver_be)
491                  {                  {
# Line 565  translate24to16(uint8 * data, uint8 * ou Line 515  translate24to16(uint8 * data, uint8 * ou
515                  pixel |= *(data++) << 8;                  pixel |= *(data++) << 8;
516                  pixel |= *(data++);                  pixel |= *(data++);
517    
518                  value = (uint16) make_colour16(split_colour24(pixel));                  value = (uint16) make_colour(split_colour24(pixel));
519    
520                  if (g_xserver_be)                  if (g_xserver_be)
521                  {                  {
# Line 583  translate24to16(uint8 * data, uint8 * ou Line 533  translate24to16(uint8 * data, uint8 * ou
533  static void  static void
534  translate24to24(uint8 * data, uint8 * out, uint8 * end)  translate24to24(uint8 * data, uint8 * out, uint8 * end)
535  {  {
536            uint32 pixel;
537            uint32 value;
538    
539          while (out < end)          while (out < end)
540          {          {
541                  *(out++) = (*(data++));                  pixel = *(data++) << 16;
542                    pixel |= *(data++) << 8;
543                    pixel |= *(data++);
544    
545                    value = make_colour(split_colour24(pixel));
546    
547                    if (g_xserver_be)
548                    {
549                            *(out++) = value >> 16;
550                            *(out++) = value >> 8;
551                            *(out++) = value;
552                    }
553                    else
554                    {
555                            *(out++) = value;
556                            *(out++) = value >> 8;
557                            *(out++) = value >> 16;
558                    }
559          }          }
560  }  }
561    
562  static void  static void
563  translate24to32(uint8 * data, uint8 * out, uint8 * end)  translate24to32(uint8 * data, uint8 * out, uint8 * end)
564  {  {
565            uint32 pixel;
566            uint32 value;
567    
568          while (out < end)          while (out < end)
569          {          {
570                    pixel = *(data++) << 16;
571                    pixel |= *(data++) << 8;
572                    pixel |= *(data++);
573    
574                    value = make_colour(split_colour24(pixel));
575    
576                  if (g_xserver_be)                  if (g_xserver_be)
577                  {                  {
578                          *(out++) = 0x00;                          *(out++) = value >> 24;
579                          *(out++) = *(data++);                          *(out++) = value >> 16;
580                          *(out++) = *(data++);                          *(out++) = value >> 8;
581                          *(out++) = *(data++);                          *(out++) = value;
582                  }                  }
583                  else                  else
584                  {                  {
585                          *(out++) = *(data++);                          *(out++) = value;
586                          *(out++) = *(data++);                          *(out++) = value >> 8;
587                          *(out++) = *(data++);                          *(out++) = value >> 16;
588                          *(out++) = 0x00;                          *(out++) = value >> 24;
589                  }                  }
590          }          }
591  }  }
# Line 644  translate_image(int width, int height, u Line 623  translate_image(int width, int height, u
623                                          translate16to24((uint16 *) data, out, end);                                          translate16to24((uint16 *) data, out, end);
624                                          break;                                          break;
625                                  case 16:                                  case 16:
626                                          translate16to16((uint16 *) data, (uint16 *) out,                                          translate16to16((uint16 *) data, out, end);
                                                         (uint16 *) end);  
627                                          break;                                          break;
628                          }                          }
629                          break;                          break;
# Line 709  get_key_state(unsigned int state, uint32 Line 687  get_key_state(unsigned int state, uint32
687          return (state & keysymMask) ? True : False;          return (state & keysymMask) ? True : False;
688  }  }
689    
690    static void
691    calculate_shifts(uint32 mask, int *shift_r, int *shift_l)
692    {
693            *shift_l = ffs(mask) - 1;
694            mask >>= *shift_l;
695            *shift_r = 8 - ffs(mask & ~(mask >> 1));
696    }
697    
698  BOOL  BOOL
699  ui_init(void)  ui_init(void)
700  {  {
701            XVisualInfo vi;
702          XPixmapFormatValues *pfm;          XPixmapFormatValues *pfm;
703          uint16 test;          uint16 test;
704          int i;          int i, screen_num;
705    
706          g_display = XOpenDisplay(NULL);          g_display = XOpenDisplay(NULL);
707          if (g_display == NULL)          if (g_display == NULL)
# Line 723  ui_init(void) Line 710  ui_init(void)
710                  return False;                  return False;
711          }          }
712    
713            screen_num = DefaultScreen(g_display);
714          g_x_socket = ConnectionNumber(g_display);          g_x_socket = ConnectionNumber(g_display);
715          g_screen = DefaultScreenOfDisplay(g_display);          g_screen = ScreenOfDisplay(g_display, screen_num);
         g_visual = DefaultVisualOfScreen(g_screen);  
716          g_depth = DefaultDepthOfScreen(g_screen);          g_depth = DefaultDepthOfScreen(g_screen);
717    
718            if (g_server_bpp == 8)
719            {
720                    /* we use a colourmap, so any visual should do */
721                    g_visual = DefaultVisualOfScreen(g_screen);
722            }
723            else
724            {
725                    /* need a truecolour visual */
726                    if (!XMatchVisualInfo(g_display, screen_num, g_depth, TrueColor, &vi))
727                    {
728                            error("The display does not support true colour - high colour support unavailable.\n");
729                            return False;
730                    }
731    
732                    g_visual = vi.visual;
733                    g_owncolmap = False;
734                    calculate_shifts(vi.red_mask, &g_red_shift_r, &g_red_shift_l);
735                    calculate_shifts(vi.blue_mask, &g_blue_shift_r, &g_blue_shift_l);
736                    calculate_shifts(vi.green_mask, &g_green_shift_r, &g_green_shift_l);
737            }
738    
739          pfm = XListPixmapFormats(g_display, &i);          pfm = XListPixmapFormats(g_display, &i);
740          if (pfm != NULL)          if (pfm != NULL)
741          {          {
# Line 750  ui_init(void) Line 758  ui_init(void)
758                  return False;                  return False;
759          }          }
760    
         /* private colour map code only works for 8 bpp */  
         if (g_owncolmap && (g_bpp > 8))  
                 g_owncolmap = False;  
   
761          if (!g_owncolmap)          if (!g_owncolmap)
762          {          {
763                  g_xcolmap = DefaultColormapOfScreen(g_screen);                  g_xcolmap = DefaultColormapOfScreen(g_screen);
# Line 769  ui_init(void) Line 773  ui_init(void)
773          test = 1;          test = 1;
774          g_host_be = !(BOOL) (*(uint8 *) (&test));          g_host_be = !(BOOL) (*(uint8 *) (&test));
775          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);  
776    
777          /*          /*
778           * Determine desktop size           * Determine desktop size
# Line 847  ui_deinit(void) Line 850  ui_deinit(void)
850          g_display = NULL;          g_display = NULL;
851  }  }
852    
 #define NULL_POINTER_MASK       "\x80"  
 #define NULL_POINTER_DATA       "\x0\x0\x0"  
           
853  BOOL  BOOL
854  ui_create_window(void)  ui_create_window(void)
855  {  {
856            uint8 null_pointer_mask[1] = { 0x80 };
857            uint8 null_pointer_data[4] = { 0x00, 0x00, 0x00, 0x00 };
858          XSetWindowAttributes attribs;          XSetWindowAttributes attribs;
859          XClassHint *classhints;          XClassHint *classhints;
860          XSizeHints *sizehints;          XSizeHints *sizehints;
# Line 935  ui_create_window(void) Line 937  ui_create_window(void)
937          XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1);          XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1);
938    
939          /* create invisible 1x1 cursor to be used as null cursor */          /* create invisible 1x1 cursor to be used as null cursor */
940          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);
941    
942          return True;          return True;
943  }  }
# Line 943  ui_create_window(void) Line 945  ui_create_window(void)
945  void  void
946  ui_destroy_window(void)  ui_destroy_window(void)
947  {  {
         ui_destroy_cursor(g_null_cursor);  
           
948          if (g_IC != NULL)          if (g_IC != NULL)
949                  XDestroyIC(g_IC);                  XDestroyIC(g_IC);
950    
# Line 988  xwin_process_events(void) Line 988  xwin_process_events(void)
988          key_translation tr;          key_translation tr;
989          char str[256];          char str[256];
990          Status status;          Status status;
         unsigned int state;  
         Window wdummy;  
         int dummy;  
991    
992          while (XPending(g_display) > 0)          while (XPending(g_display) > 0)
993          {          {
# Line 1163  xwin_process_events(void) Line 1160  xwin_process_events(void)
1160                                  if (xevent.xfocus.mode == NotifyGrab)                                  if (xevent.xfocus.mode == NotifyGrab)
1161                                          break;                                          break;
1162                                  g_focused = True;                                  g_focused = True;
1163                                  XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy,                                  reset_modifier_keys();
                                               &dummy, &dummy, &state);  
                                 reset_modifier_keys(state);  
1164                                  if (g_grab_keyboard && g_mouse_in_wnd)                                  if (g_grab_keyboard && g_mouse_in_wnd)
1165                                          XGrabKeyboard(g_display, g_wnd, True,                                          XGrabKeyboard(g_display, g_wnd, True,
1166                                                        GrabModeAsync, GrabModeAsync, CurrentTime);                                                        GrabModeAsync, GrabModeAsync, CurrentTime);
# Line 1574  ui_create_colourmap(COLOURMAP * colours) Line 1569  ui_create_colourmap(COLOURMAP * colours)
1569    
1570                          }                          }
1571    
1572                            map[i] = colour;
                         /* byte swap here to make translate_image faster */  
                         map[i] = translate_colour(colour);  
1573                  }                  }
1574                  return map;                  return map;
1575          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26