--- sourceforge.net/trunk/rdesktop/xwin.c 2003/10/29 06:29:05 524 +++ sourceforge.net/trunk/rdesktop/xwin.c 2003/10/31 04:29:57 536 @@ -57,7 +57,8 @@ /* endianness */ static BOOL g_host_be; static BOOL g_xserver_be; -static BOOL g_xserver_bgr; +static int g_red_shift_r, g_blue_shift_r, g_green_shift_r; +static int g_red_shift_l, g_blue_shift_l, g_green_shift_l; /* software backing store */ static BOOL g_ownbackstore; @@ -113,7 +114,7 @@ static Colormap g_xcolmap; static uint32 *g_colmap = NULL; -#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] ) #define SET_FOREGROUND(col) XSetForeground(g_display, g_gc, TRANSLATE(col)); #define SET_BACKGROUND(col) XSetBackground(g_display, g_gc, TRANSLATE(col)); @@ -198,89 +199,35 @@ } static uint32 -make_colour16(PixelColour pc) +make_colour(PixelColour pc) { - pc.red = (pc.red * 0x1f) / 0xff; - pc.green = (pc.green * 0x3f) / 0xff; - pc.blue = (pc.blue * 0x1f) / 0xff; - 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; + return (((pc.red >> g_red_shift_r) << g_red_shift_l) + | ((pc.green >> g_green_shift_r) << g_green_shift_l) + | ((pc.blue >> g_blue_shift_r) << g_blue_shift_l)); } #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); } -#define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); } +#define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | (x & 0xff00)); } #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \ x = (x << 16) | (x >> 16); } static uint32 translate_colour(uint32 colour) { + PixelColour pc; switch (g_server_bpp) { case 15: - switch (g_bpp) - { - 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; - } + pc = split_colour15(colour); break; case 16: - switch (g_bpp) - { - case 16: - break; - case 24: - colour = make_colour24(split_colour16(colour)); - break; - case 32: - colour = make_colour32(split_colour16(colour)); - break; - } + pc = split_colour16(colour); break; case 24: - switch (g_bpp) - { - 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; - } + pc = split_colour24(colour); break; } - return colour; + return make_colour(pc); } static void @@ -298,7 +245,7 @@ while (out < end) { value = (uint16) g_colmap[*(data++)]; - + if (g_xserver_be) { *(out++) = value >> 8; @@ -321,7 +268,7 @@ while (out < end) { value = g_colmap[*(data++)]; - + if (g_xserver_be) { *(out++) = value >> 16; @@ -380,7 +327,7 @@ BSWAP16(pixel); } - value = make_colour16(split_colour15(pixel)); + value = make_colour(split_colour15(pixel)); if (g_xserver_be) { @@ -410,7 +357,7 @@ BSWAP16(pixel); } - value = make_colour24(split_colour15(pixel)); + value = make_colour(split_colour15(pixel)); if (g_xserver_be) { *(out++) = value >> 16; @@ -441,7 +388,7 @@ BSWAP16(pixel); } - value = make_colour32(split_colour15(pixel)); + value = make_colour(split_colour15(pixel)); if (g_xserver_be) { @@ -461,34 +408,35 @@ } static void -translate16to16(uint16 * data, uint16 * out, uint16 * end) +translate16to16(uint16 * data, uint8 * out, uint8 * end) { + uint16 pixel; uint16 value; - if (g_xserver_be) + while (out < end) { - while (out < end) + pixel = *(data++); + + if (g_host_be) { - value = *data; - BSWAP16(value); - *out = value; - data++; - out++; + BSWAP16(pixel); } - } - else - { - while (out < end) + value = make_colour(split_colour16(pixel)); + + if (g_xserver_be) { - *out = *data; - out++; - data++; + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; } } } - static void translate16to24(uint16 * data, uint8 * out, uint8 * end) { @@ -504,7 +452,7 @@ BSWAP16(pixel); } - value = make_colour24(split_colour16(pixel)); + value = make_colour(split_colour16(pixel)); if (g_xserver_be) { @@ -533,9 +481,10 @@ if (g_host_be) { - BSWAP16(pixel)} + BSWAP16(pixel); + } - value = make_colour32(split_colour16(pixel)); + value = make_colour(split_colour16(pixel)); if (g_xserver_be) { @@ -565,7 +514,7 @@ pixel |= *(data++) << 8; pixel |= *(data++); - value = (uint16) make_colour16(split_colour24(pixel)); + value = (uint16) make_colour(split_colour24(pixel)); if (g_xserver_be) { @@ -583,30 +532,59 @@ static void translate24to24(uint8 * data, uint8 * out, uint8 * end) { + uint32 pixel; + uint32 value; + while (out < end) { - *(out++) = (*(data++)); + pixel = *(data++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + + value = make_colour(split_colour24(pixel)); + + if (g_xserver_be) + { + *(out++) = value >> 16; + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + } } } static void translate24to32(uint8 * data, uint8 * out, uint8 * end) { + uint32 pixel; + uint32 value; + while (out < end) { + pixel = *(data++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + + value = make_colour(split_colour24(pixel)); + if (g_xserver_be) { - *(out++) = 0x00; - *(out++) = *(data++); - *(out++) = *(data++); - *(out++) = *(data++); + *(out++) = value >> 24; + *(out++) = value >> 16; + *(out++) = value >> 8; + *(out++) = value; } else { - *(out++) = *(data++); - *(out++) = *(data++); - *(out++) = *(data++); - *(out++) = 0x00; + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + *(out++) = value >> 24; } } } @@ -644,8 +622,7 @@ translate16to24((uint16 *) data, out, end); break; case 16: - translate16to16((uint16 *) data, (uint16 *) out, - (uint16 *) end); + translate16to16((uint16 *) data, out, end); break; } break; @@ -709,12 +686,21 @@ return (state & keysymMask) ? True : False; } +static void +calculate_shifts(uint32 mask, int *shift_r, int *shift_l) +{ + *shift_l = ffs(mask) - 1; + mask >>= *shift_l; + *shift_r = 8 - ffs(mask & ~(mask >> 1)); +} + BOOL ui_init(void) { + XVisualInfo vi; XPixmapFormatValues *pfm; uint16 test; - int i; + int i, screen_num; g_display = XOpenDisplay(NULL); if (g_display == NULL) @@ -723,11 +709,32 @@ return False; } + screen_num = DefaultScreen(g_display); g_x_socket = ConnectionNumber(g_display); - g_screen = DefaultScreenOfDisplay(g_display); - g_visual = DefaultVisualOfScreen(g_screen); + g_screen = ScreenOfDisplay(g_display, screen_num); g_depth = DefaultDepthOfScreen(g_screen); + if (g_server_bpp == 8) + { + /* we use a colourmap, so any visual should do */ + g_visual = DefaultVisualOfScreen(g_screen); + } + else + { + /* need a truecolour visual */ + if (!XMatchVisualInfo(g_display, screen_num, g_depth, TrueColor, &vi)) + { + error("The display does not support true colour - high colour support unavailable.\n"); + return False; + } + + g_visual = vi.visual; + g_owncolmap = False; + calculate_shifts(vi.red_mask, &g_red_shift_r, &g_red_shift_l); + calculate_shifts(vi.blue_mask, &g_blue_shift_r, &g_blue_shift_l); + calculate_shifts(vi.green_mask, &g_green_shift_r, &g_green_shift_l); + } + pfm = XListPixmapFormats(g_display, &i); if (pfm != NULL) { @@ -750,10 +757,6 @@ return False; } - /* private colour map code only works for 8 bpp */ - if (g_owncolmap && (g_bpp > 8)) - g_owncolmap = False; - if (!g_owncolmap) { g_xcolmap = DefaultColormapOfScreen(g_screen); @@ -769,7 +772,6 @@ test = 1; g_host_be = !(BOOL) (*(uint8 *) (&test)); g_xserver_be = (ImageByteOrder(g_display) == MSBFirst); - g_xserver_bgr = (g_visual->blue_mask > g_visual->red_mask); /* * Determine desktop size @@ -847,12 +849,11 @@ g_display = NULL; } -#define NULL_POINTER_MASK "\x80" -#define NULL_POINTER_DATA "\x0\x0\x0" - BOOL ui_create_window(void) { + uint8 null_pointer_mask[1] = { 0x80 }; + uint8 null_pointer_data[4] = { 0x00, 0x00, 0x00, 0x00 }; XSetWindowAttributes attribs; XClassHint *classhints; XSizeHints *sizehints; @@ -935,7 +936,7 @@ XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1); /* create invisible 1x1 cursor to be used as null cursor */ - 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); return True; } @@ -944,7 +945,7 @@ ui_destroy_window(void) { ui_destroy_cursor(g_null_cursor); - + if (g_IC != NULL) XDestroyIC(g_IC); @@ -1574,9 +1575,7 @@ } - - /* byte swap here to make translate_image faster */ - map[i] = translate_colour(colour); + map[i] = colour; } return map; }