--- sourceforge.net/trunk/rdesktop/xwin.c 2003/09/02 09:40:07 461 +++ sourceforge.net/trunk/rdesktop/xwin.c 2004/06/27 17:51:54 725 @@ -20,8 +20,11 @@ #include #include +#include +#include #include #include +#include #include "rdesktop.h" #include "xproto.h" @@ -34,16 +37,18 @@ extern char g_title[]; extern int g_server_bpp; extern int g_win_button_size; -BOOL g_enable_compose = False; -BOOL g_focused; -BOOL g_mouse_in_wnd; Display *g_display; Time g_last_gesturetime; static int g_x_socket; static Screen *g_screen; Window g_wnd; -static GC g_gc; +extern uint32 g_embed_wnd; +BOOL g_enable_compose = False; +BOOL g_Unobscured; /* used for screenblt */ +static GC g_gc = NULL; +static GC g_create_bitmap_gc = NULL; +static GC g_create_glyph_gc = NULL; static Visual *g_visual; static int g_depth; static int g_bpp; @@ -51,21 +56,33 @@ static XIC g_IC; static XModifierKeymap *g_mod_map; static Cursor g_current_cursor; +static HCURSOR g_null_cursor = NULL; static Atom g_protocol_atom, g_kill_atom; +static BOOL g_focused; +static BOOL g_mouse_in_wnd; +static BOOL g_arch_match = False; /* set to True if RGB XServer and little endian */ /* endianness */ static BOOL g_host_be; static BOOL g_xserver_be; +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; -static Pixmap g_backstore; +extern BOOL g_ownbackstore; +static Pixmap g_backstore = 0; /* Moving in single app mode */ static BOOL g_moving_wnd; static int g_move_x_offset = 0; static int g_move_y_offset = 0; +#ifdef WITH_RDPSND +extern int g_dsp_fd; +extern BOOL g_dsp_busy; +extern BOOL g_rdpsnd; +#endif + /* MWM decorations */ #define MWM_HINTS_DECORATIONS (1L << 1) #define PROP_MOTIF_WM_HINTS_ELEMENTS 5 @@ -101,11 +118,11 @@ } /* colour maps */ -BOOL g_owncolmap = False; +extern BOOL g_owncolmap; 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)); @@ -157,12 +174,9 @@ split_colour15(uint32 colour) { PixelColour rv; - rv.red = (colour & 0x7c00) >> 10; - rv.red = (rv.red * 0xff) / 0x1f; - rv.green = (colour & 0x03e0) >> 5; - rv.green = (rv.green * 0xff) / 0x1f; - rv.blue = (colour & 0x1f); - rv.blue = (rv.blue * 0xff) / 0x1f; + rv.red = ((colour >> 7) & 0xf8) | ((colour >> 12) & 0x7); + rv.green = ((colour >> 2) & 0xf8) | ((colour >> 8) & 0x7); + rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); return rv; } @@ -170,12 +184,9 @@ split_colour16(uint32 colour) { PixelColour rv; - rv.red = (colour & 0xf800) >> 11; - rv.red = (rv.red * 0xff) / 0x1f; - rv.green = (colour & 0x07e0) >> 5; - rv.green = (rv.green * 0xff) / 0x3f; - rv.blue = (colour & 0x001f); - rv.blue = (rv.blue * 0xff) / 0x1f; + rv.red = ((colour >> 8) & 0xf8) | ((colour >> 13) & 0x7); + rv.green = ((colour >> 3) & 0xfc) | ((colour >> 9) & 0x3); + rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); return rv; } @@ -184,102 +195,67 @@ { PixelColour rv; rv.blue = (colour & 0xff0000) >> 16; - rv.green = (colour & 0xff00) >> 8; - rv.red = (colour & 0xff); + rv.green = (colour & 0x00ff00) >> 8; + rv.red = (colour & 0x0000ff); return rv; } static uint32 -make_colour16(PixelColour pc) -{ - pc.red = (pc.red * 0x1f) / 0xff; - pc.green = (pc.green * 0x3f) / 0xff; - pc.blue = (pc.blue * 0x1f) / 0xff; - return (pc.red << 11) | (pc.green << 5) | pc.blue; -} - -static uint32 -make_colour24(PixelColour pc) -{ - return (pc.red << 16) | (pc.green << 8) | pc.blue; -} - -static uint32 -make_colour32(PixelColour pc) +make_colour(PixelColour pc) { - 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: - break; - case 32: - colour = make_colour32(split_colour24(colour)); - break; - } + pc = split_colour24(colour); break; } - switch (g_bpp) - { - case 16: - if (g_host_be != g_xserver_be) - BSWAP16(colour); - break; + return make_colour(pc); +} - case 24: - if (g_xserver_be) - BSWAP24(colour); - break; +/* indent is confused by UNROLL8 */ +/* *INDENT-OFF* */ - case 32: - if (g_host_be != g_xserver_be) - BSWAP32(colour); - break; - } +/* repeat and unroll, similar to bitmap.c */ +/* potentialy any of the following translate */ +/* functions can use repeat but just doing */ +/* the most common ones */ - return colour; +#define UNROLL8(stm) { stm stm stm stm stm stm stm stm } +/* 2 byte output repeat */ +#define REPEAT2(stm) \ +{ \ + while (out <= end - 8 * 2) \ + UNROLL8(stm) \ + while (out < end) \ + { stm } \ +} +/* 4 byte output repeat */ +#define REPEAT4(stm) \ +{ \ + while (out <= end - 8 * 4) \ + UNROLL8(stm) \ + while (out < end) \ + { stm } \ } static void @@ -290,10 +266,36 @@ } static void -translate8to16(uint8 * data, uint16 * out, uint16 * end) +translate8to16(uint8 * data, uint8 * out, uint8 * end) { - while (out < end) - *(out++) = (uint16) g_colmap[*(data++)]; + uint16 value; + + if (g_arch_match) + { + REPEAT2 + ( + *((uint16 *) out) = g_colmap[*(data++)]; + out += 2; + ) + } + else if (g_xserver_be) + { + while (out < end) + { + value = (uint16) g_colmap[*(data++)]; + *(out++) = value >> 8; + *(out++) = value; + } + } + else + { + while (out < end) + { + value = (uint16) g_colmap[*(data++)]; + *(out++) = value; + *(out++) = value >> 8; + } + } } /* little endian - conversion happens when colourmap is built */ @@ -302,122 +304,365 @@ { uint32 value; - while (out < end) + if (g_xserver_be) { - value = g_colmap[*(data++)]; - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; + while (out < end) + { + value = g_colmap[*(data++)]; + *(out++) = value >> 16; + *(out++) = value >> 8; + *(out++) = value; + } + } + else + { + while (out < end) + { + value = g_colmap[*(data++)]; + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + } } } static void -translate8to32(uint8 * data, uint32 * out, uint32 * end) +translate8to32(uint8 * data, uint8 * out, uint8 * end) { - while (out < end) - *(out++) = g_colmap[*(data++)]; + uint32 value; + + if (g_arch_match) + { + REPEAT4 + ( + *((uint32 *) out) = g_colmap[*(data++)]; + out += 4; + ) + } + else if (g_xserver_be) + { + while (out < end) + { + value = g_colmap[*(data++)]; + *(out++) = value >> 24; + *(out++) = value >> 16; + *(out++) = value >> 8; + *(out++) = value; + } + } + else + { + while (out < end) + { + value = g_colmap[*(data++)]; + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + *(out++) = value >> 24; + } + } } -/* todo the remaining translate function might need some big endian check ?? */ +/* *INDENT-ON* */ static void -translate15to16(uint16 * data, uint16 * out, uint16 * end) +translate15to16(uint16 * data, uint8 * out, uint8 * end) { + uint16 pixel; + uint16 value; + while (out < end) - *(out++) = (uint16) make_colour16(split_colour15(*(data++))); + { + pixel = *(data++); + + if (g_host_be) + { + BSWAP16(pixel); + } + + value = make_colour(split_colour15(pixel)); + + if (g_xserver_be) + { + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; + } + } } static void translate15to24(uint16 * data, uint8 * out, uint8 * end) { uint32 value; + uint16 pixel; while (out < end) { - value = make_colour24(split_colour15(*(data++))); - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; + pixel = *(data++); + + if (g_host_be) + { + BSWAP16(pixel); + } + + value = make_colour(split_colour15(pixel)); + if (g_xserver_be) + { + *(out++) = value >> 16; + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + } } } static void -translate15to32(uint16 * data, uint32 * out, uint32 * end) +translate15to32(uint16 * data, uint8 * out, uint8 * end) { + uint16 pixel; + uint32 value; + while (out < end) - *(out++) = make_colour32(split_colour15(*(data++))); + { + pixel = *(data++); + + if (g_host_be) + { + BSWAP16(pixel); + } + + value = make_colour(split_colour15(pixel)); + + if (g_xserver_be) + { + *(out++) = value >> 24; + *(out++) = value >> 16; + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + *(out++) = value >> 24; + } + } } static void -translate16to16(uint16 * data, uint16 * out, uint16 * end) +translate16to16(uint16 * data, uint8 * out, uint8 * end) { + uint16 pixel; + uint16 value; + while (out < end) - *(out++) = (uint16) (*(data++)); -} + { + pixel = *(data++); + + if (g_host_be) + { + BSWAP16(pixel); + } + value = make_colour(split_colour16(pixel)); + + if (g_xserver_be) + { + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; + } + } +} static void translate16to24(uint16 * data, uint8 * out, uint8 * end) { uint32 value; + uint16 pixel; while (out < end) { - value = make_colour24(split_colour16(*(data++))); - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; + pixel = *(data++); + + if (g_host_be) + { + BSWAP16(pixel); + } + + value = make_colour(split_colour16(pixel)); + + if (g_xserver_be) + { + *(out++) = value >> 16; + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + } } } static void -translate16to32(uint16 * data, uint32 * out, uint32 * end) +translate16to32(uint16 * data, uint8 * out, uint8 * end) { + uint16 pixel; + uint32 value; + while (out < end) - *(out++) = make_colour32(split_colour16(*(data++))); + { + pixel = *(data++); + + if (g_host_be) + { + BSWAP16(pixel); + } + + value = make_colour(split_colour16(pixel)); + + if (g_xserver_be) + { + *(out++) = value >> 24; + *(out++) = value >> 16; + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + *(out++) = value >> 24; + } + } } static void -translate24to16(uint8 * data, uint16 * out, uint16 * end) +translate24to16(uint8 * data, uint8 * out, uint8 * end) { uint32 pixel = 0; + uint16 value; while (out < end) { pixel = *(data++) << 16; pixel |= *(data++) << 8; pixel |= *(data++); - *(out++) = (uint16) make_colour16(split_colour24(pixel)); + + value = (uint16) make_colour(split_colour24(pixel)); + + if (g_xserver_be) + { + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; + } } } 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, uint32 * out, uint32 * end) +translate24to32(uint8 * data, uint8 * out, uint8 * end) { - uint32 pixel = 0; + uint32 pixel; + uint32 value; + while (out < end) { - pixel = *(data++); + pixel = *(data++) << 16; pixel |= *(data++) << 8; - pixel |= *(data++) << 16; - *(out++) = pixel; + pixel |= *(data++); + + value = make_colour(split_colour24(pixel)); + + if (g_xserver_be) + { + *(out++) = value >> 24; + *(out++) = value >> 16; + *(out++) = value >> 8; + *(out++) = value; + } + else + { + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + *(out++) = value >> 24; + } } } static uint8 * translate_image(int width, int height, uint8 * data) { - int size = width * height * g_bpp / 8; - uint8 *out = (uint8 *) xmalloc(size); - uint8 *end = out + size; + int size; + uint8 *out; + uint8 *end; + + /* if server and xserver bpp match, */ + /* and arch(endian) matches, no need to translate */ + /* just return data */ + if (g_arch_match) + { + if (g_depth == 15 && g_server_bpp == 15) + return data; + if (g_depth == 16 && g_server_bpp == 16) + return data; + } + + size = width * height * (g_bpp / 8); + out = (uint8 *) xmalloc(size); + end = out + size; switch (g_server_bpp) { @@ -425,13 +670,13 @@ switch (g_bpp) { case 32: - translate24to32(data, (uint32 *) out, (uint32 *) end); + translate24to32(data, out, end); break; case 24: translate24to24(data, out, end); break; case 16: - translate24to16(data, (uint16 *) out, (uint16 *) end); + translate24to16(data, out, end); break; } break; @@ -439,15 +684,13 @@ switch (g_bpp) { case 32: - translate16to32((uint16 *) data, (uint32 *) out, - (uint32 *) end); + translate16to32((uint16 *) data, out, end); break; case 24: translate16to24((uint16 *) data, out, end); break; case 16: - translate16to16((uint16 *) data, (uint16 *) out, - (uint16 *) end); + translate16to16((uint16 *) data, out, end); break; } break; @@ -455,15 +698,13 @@ switch (g_bpp) { case 32: - translate15to32((uint16 *) data, (uint32 *) out, - (uint32 *) end); + translate15to32((uint16 *) data, out, end); break; case 24: translate15to24((uint16 *) data, out, end); break; case 16: - translate15to16((uint16 *) data, (uint16 *) out, - (uint16 *) end); + translate15to16((uint16 *) data, out, end); break; } break; @@ -474,13 +715,13 @@ translate8to8(data, out, end); break; case 16: - translate8to16(data, (uint16 *) out, (uint16 *) end); + translate8to16(data, out, end); break; case 24: translate8to24(data, out, end); break; case 32: - translate8to32(data, (uint32 *) out, (uint32 *) end); + translate8to32(data, out, end); break; } break; @@ -513,12 +754,24 @@ 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, nvisuals; + XVisualInfo *vmatches = NULL; + XVisualInfo template; + Bool TrueColorVisual = False; g_display = XOpenDisplay(NULL); if (g_display == NULL) @@ -527,11 +780,63 @@ 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); + /* Search for best TrueColor depth */ + template.class = TrueColor; + vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &nvisuals); + + nvisuals--; + while (nvisuals >= 0) + { + if ((vmatches + nvisuals)->depth > g_depth) + { + g_depth = (vmatches + nvisuals)->depth; + } + nvisuals--; + TrueColorVisual = True; + } + + test = 1; + g_host_be = !(BOOL) (*(uint8 *) (&test)); + g_xserver_be = (ImageByteOrder(g_display) == MSBFirst); + + if ((g_server_bpp == 8) && ((!TrueColorVisual) || (g_depth <= 8))) + { + /* we use a colourmap, so the default visual should do */ + g_visual = DefaultVisualOfScreen(g_screen); + g_depth = DefaultDepthOfScreen(g_screen); + + /* Do not allocate colours on a TrueColor visual */ + if (g_visual->class == TrueColor) + { + g_owncolmap = False; + } + } + 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); + + /* if RGB video and averything is little endian */ + if (vi.red_mask > vi.green_mask && vi.green_mask > vi.blue_mask) + if (!g_xserver_be && !g_host_be) + g_arch_match = True; + } + pfm = XListPixmapFormats(g_display, &i); if (pfm != NULL) { @@ -554,23 +859,36 @@ return False; } - if (g_owncolmap != True) + if (!g_owncolmap) { - g_xcolmap = DefaultColormapOfScreen(g_screen); + g_xcolmap = + XCreateColormap(g_display, RootWindowOfScreen(g_screen), g_visual, + AllocNone); if (g_depth <= 8) warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n"); } - g_gc = XCreateGC(g_display, RootWindowOfScreen(g_screen), 0, NULL); - - if (DoesBackingStore(g_screen) != Always) + if ((!g_ownbackstore) && (DoesBackingStore(g_screen) != Always)) + { + warning("External BackingStore not available, using internal\n"); g_ownbackstore = True; + } - test = 1; - g_host_be = !(BOOL) (*(uint8 *) (&test)); - g_xserver_be = (ImageByteOrder(g_display) == MSBFirst); - - if ((g_width == 0) || (g_height == 0)) + /* + * Determine desktop size + */ + if (g_fullscreen) + { + g_width = WidthOfScreen(g_screen); + g_height = HeightOfScreen(g_screen); + } + else if (g_width < 0) + { + /* Percent of screen */ + g_height = HeightOfScreen(g_screen) * (-g_width) / 100; + g_width = WidthOfScreen(g_screen) * (-g_width) / 100; + } + else if (g_width == 0) { /* Fetch geometry from _NET_WORKAREA */ uint32 x, y, cx, cy; @@ -588,36 +906,19 @@ } } - if (g_fullscreen) - { - g_width = WidthOfScreen(g_screen); - g_height = HeightOfScreen(g_screen); - } - /* make sure width is a multiple of 4 */ g_width = (g_width + 3) & ~3; - if (g_ownbackstore) - { - g_backstore = - XCreatePixmap(g_display, RootWindowOfScreen(g_screen), g_width, g_height, - g_depth); - - /* clear to prevent rubbish being exposed at startup */ - XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen)); - XFillRectangle(g_display, g_backstore, g_gc, 0, 0, g_width, g_height); - } - g_mod_map = XGetModifierMapping(g_display); + xkeymap_init(); + if (g_enable_compose) g_IM = XOpenIM(g_display, NULL, NULL, NULL); - xkeymap_init(); xclip_init(); - /* todo take this out when high colour is done */ - printf("server bpp %d client bpp %d depth %d\n", g_server_bpp, g_bpp, g_depth); + DEBUG_RDP5(("server bpp %d client bpp %d depth %d\n", g_server_bpp, g_bpp, g_depth)); return True; } @@ -628,6 +929,9 @@ if (g_IM != NULL) XCloseIM(g_IM); + if (g_null_cursor != NULL) + ui_destroy_cursor(g_null_cursor); + XFreeModifiermap(g_mod_map); if (g_ownbackstore) @@ -641,6 +945,8 @@ 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; @@ -652,12 +958,30 @@ wndheight = g_fullscreen ? HeightOfScreen(g_screen) : g_height; attribs.background_pixel = BlackPixelOfScreen(g_screen); + attribs.border_pixel = WhitePixelOfScreen(g_screen); attribs.backing_store = g_ownbackstore ? NotUseful : Always; attribs.override_redirect = g_fullscreen; + attribs.colormap = g_xcolmap; g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), 0, 0, wndwidth, wndheight, - 0, CopyFromParent, InputOutput, CopyFromParent, - CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs); + 0, g_depth, InputOutput, g_visual, + CWBackPixel | CWBackingStore | CWOverrideRedirect | + CWColormap | CWBorderPixel, &attribs); + + if (g_gc == NULL) + g_gc = XCreateGC(g_display, g_wnd, 0, NULL); + + if (g_create_bitmap_gc == NULL) + g_create_bitmap_gc = XCreateGC(g_display, g_wnd, 0, NULL); + + if ((g_ownbackstore) && (g_backstore == 0)) + { + g_backstore = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth); + + /* clear to prevent rubbish being exposed at startup */ + XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen)); + XFillRectangle(g_display, g_backstore, g_gc, 0, 0, g_width, g_height); + } XStoreName(g_display, g_wnd, g_title); @@ -682,6 +1006,11 @@ XFree(sizehints); } + if (g_embed_wnd) + { + XReparentWindow(g_display, g_wnd, (Window) g_embed_wnd, 0, 0); + } + input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | VisibilityChangeMask | FocusChangeMask; @@ -713,6 +1042,7 @@ XMaskEvent(g_display, VisibilityChangeMask, &xevent); } while (xevent.type != VisibilityNotify); + g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured; g_focused = False; g_mouse_in_wnd = False; @@ -722,10 +1052,47 @@ g_kill_atom = XInternAtom(g_display, "WM_DELETE_WINDOW", True); XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1); + /* create invisible 1x1 cursor to be used as null cursor */ + if (g_null_cursor == NULL) + g_null_cursor = ui_create_cursor(0, 0, 1, 1, null_pointer_mask, null_pointer_data); + return True; } void +ui_resize_window() +{ + XSizeHints *sizehints; + Pixmap bs; + + sizehints = XAllocSizeHints(); + if (sizehints) + { + sizehints->flags = PMinSize | PMaxSize; + sizehints->min_width = sizehints->max_width = g_width; + sizehints->min_height = sizehints->max_height = g_height; + XSetWMNormalHints(g_display, g_wnd, sizehints); + XFree(sizehints); + } + + if (!(g_fullscreen || g_embed_wnd)) + { + XResizeWindow(g_display, g_wnd, g_width, g_height); + } + + /* create new backstore pixmap */ + if (g_backstore != 0) + { + bs = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth); + XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen)); + XFillRectangle(g_display, bs, g_gc, 0, 0, g_width, g_height); + XCopyArea(g_display, g_backstore, bs, g_gc, 0, 0, g_width, g_height, 0, 0); + XFreePixmap(g_display, g_backstore); + g_backstore = bs; + } +} + +void ui_destroy_window(void) { if (g_IC != NULL) @@ -771,9 +1138,6 @@ key_translation tr; char str[256]; Status status; - unsigned int state; - Window wdummy; - int dummy; while (XPending(g_display) > 0) { @@ -789,6 +1153,9 @@ switch (xevent.type) { + case VisibilityNotify: + g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured; + break; case ClientMessage: /* the window manager told us to quit */ if ((xevent.xclient.message_type == g_protocol_atom) @@ -833,10 +1200,10 @@ if (tr.scancode == 0) break; - save_remote_modifiers(); + save_remote_modifiers(tr.scancode); ensure_remote_modifiers(ev_time, tr); rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); - restore_remote_modifiers(ev_time); + restore_remote_modifiers(ev_time, tr.scancode); break; @@ -946,9 +1313,7 @@ if (xevent.xfocus.mode == NotifyGrab) break; g_focused = True; - XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy, - &dummy, &dummy, &state); - reset_modifier_keys(state); + reset_modifier_keys(); if (g_grab_keyboard && g_mouse_in_wnd) XGrabKeyboard(g_display, g_wnd, True, GrabModeAsync, GrabModeAsync, CurrentTime); @@ -1028,33 +1393,65 @@ int ui_select(int rdp_socket) { - int n = (rdp_socket > g_x_socket) ? rdp_socket + 1 : g_x_socket + 1; - fd_set rfds; - - FD_ZERO(&rfds); + int n; + fd_set rfds, wfds; + struct timeval tv; + BOOL s_timeout = False; while (True) { + n = (rdp_socket > g_x_socket) ? rdp_socket : g_x_socket; /* Process any events already waiting */ if (!xwin_process_events()) /* User quit */ return 0; FD_ZERO(&rfds); + FD_ZERO(&wfds); FD_SET(rdp_socket, &rfds); FD_SET(g_x_socket, &rfds); - switch (select(n, &rfds, NULL, NULL, NULL)) +#ifdef WITH_RDPSND + /* FIXME: there should be an API for registering fds */ + if (g_dsp_busy) + { + FD_SET(g_dsp_fd, &wfds); + n = (g_dsp_fd > n) ? g_dsp_fd : n; + } +#endif + /* default timeout */ + tv.tv_sec = 60; + tv.tv_usec = 0; + + /* add redirection handles */ + rdpdr_add_fds(&n, &rfds, &wfds, &tv, &s_timeout); + + n++; + + switch (select(n, &rfds, &wfds, NULL, &tv)) { case -1: error("select: %s\n", strerror(errno)); case 0: + /* TODO: if tv.tv_sec just times out + * we will segfault. + * FIXME: + */ + //s_timeout = True; + //rdpdr_check_fds(&rfds, &wfds, (BOOL) True); continue; } + rdpdr_check_fds(&rfds, &wfds, (BOOL) False); + if (FD_ISSET(rdp_socket, &rfds)) return 1; + +#ifdef WITH_RDPSND + if (g_dsp_busy && FD_ISSET(g_dsp_fd, &wfds)) + wave_out_play(); +#endif } } @@ -1070,16 +1467,29 @@ XImage *image; Pixmap bitmap; uint8 *tdata; + int bitmap_pad; + + if (g_server_bpp == 8) + { + bitmap_pad = 8; + } + else + { + bitmap_pad = g_bpp; + + if (g_bpp == 24) + bitmap_pad = 32; + } tdata = (g_owncolmap ? data : translate_image(width, height, data)); bitmap = XCreatePixmap(g_display, g_wnd, width, height, g_depth); image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0, - (char *) tdata, width, height, g_server_bpp == 8 ? 8 : g_bpp, 0); + (char *) tdata, width, height, bitmap_pad, 0); - XPutImage(g_display, bitmap, g_gc, image, 0, 0, 0, 0, width, height); + XPutImage(g_display, bitmap, g_create_bitmap_gc, image, 0, 0, 0, 0, width, height); XFree(image); - if (!g_owncolmap) + if (tdata != data) xfree(tdata); return (HBITMAP) bitmap; } @@ -1089,9 +1499,23 @@ { XImage *image; uint8 *tdata; + int bitmap_pad; + + if (g_server_bpp == 8) + { + bitmap_pad = 8; + } + else + { + bitmap_pad = g_bpp; + + if (g_bpp == 24) + bitmap_pad = 32; + } + tdata = (g_owncolmap ? data : translate_image(width, height, data)); image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0, - (char *) tdata, width, height, g_server_bpp == 8 ? 8 : g_bpp, 0); + (char *) tdata, width, height, bitmap_pad, 0); if (g_ownbackstore) { @@ -1104,7 +1528,7 @@ } XFree(image); - if (!g_owncolmap) + if (tdata != data) xfree(tdata); } @@ -1120,12 +1544,12 @@ XImage *image; Pixmap bitmap; int scanline; - GC gc; scanline = (width + 7) / 8; bitmap = XCreatePixmap(g_display, g_wnd, width, height, 1); - gc = XCreateGC(g_display, bitmap, 0, NULL); + if (g_create_glyph_gc == 0) + g_create_glyph_gc = XCreateGC(g_display, bitmap, 0, NULL); image = XCreateImage(g_display, g_visual, 1, ZPixmap, 0, (char *) data, width, height, 8, scanline); @@ -1133,10 +1557,9 @@ image->bitmap_bit_order = MSBFirst; XInitImage(image); - XPutImage(g_display, bitmap, gc, image, 0, 0, 0, 0, width, height); + XPutImage(g_display, bitmap, g_create_glyph_gc, image, 0, 0, 0, 0, width, height); XFree(image); - XFreeGC(g_display, gc); return (HGLYPH) bitmap; } @@ -1230,6 +1653,12 @@ XFreeCursor(g_display, (Cursor) cursor); } +void +ui_set_null_cursor(void) +{ + ui_set_cursor(g_null_cursor); +} + #define MAKE_XCOLOR(xc,c) \ (xc)->red = ((c)->red << 8) | (c)->red; \ (xc)->green = ((c)->green << 8) | (c)->green; \ @@ -1311,9 +1740,7 @@ } - - /* byte swap here to make translate_image faster */ - map[i] = translate_colour(colour); + map[i] = colour; } return map; } @@ -1424,18 +1851,18 @@ { case 0: /* Solid */ SET_FOREGROUND(fgcolour); - FILL_RECTANGLE(x, y, cx, cy); + FILL_RECTANGLE_BACKSTORE(x, y, cx, cy); break; case 2: /* Hatch */ fill = (Pixmap) ui_create_glyph(8, 8, hatch_patterns + brush->pattern[0] * 8); - SET_FOREGROUND(bgcolour); - SET_BACKGROUND(fgcolour); + SET_FOREGROUND(fgcolour); + SET_BACKGROUND(bgcolour); XSetFillStyle(g_display, g_gc, FillOpaqueStippled); XSetStipple(g_display, g_gc, fill); XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin); - FILL_RECTANGLE(x, y, cx, cy); + FILL_RECTANGLE_BACKSTORE(x, y, cx, cy); XSetFillStyle(g_display, g_gc, FillSolid); XSetTSOrigin(g_display, g_gc, 0, 0); ui_destroy_glyph((HGLYPH) fill); @@ -1445,15 +1872,12 @@ for (i = 0; i != 8; i++) ipattern[7 - i] = brush->pattern[i]; fill = (Pixmap) ui_create_glyph(8, 8, ipattern); - SET_FOREGROUND(bgcolour); SET_BACKGROUND(fgcolour); XSetFillStyle(g_display, g_gc, FillOpaqueStippled); XSetStipple(g_display, g_gc, fill); XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin); - - FILL_RECTANGLE(x, y, cx, cy); - + FILL_RECTANGLE_BACKSTORE(x, y, cx, cy); XSetFillStyle(g_display, g_gc, FillSolid); XSetTSOrigin(g_display, g_gc, 0, 0); ui_destroy_glyph((HGLYPH) fill); @@ -1464,6 +1888,9 @@ } RESET_FUNCTION(opcode); + + if (g_ownbackstore) + XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y); } void @@ -1472,9 +1899,25 @@ /* src */ int srcx, int srcy) { SET_FUNCTION(opcode); - XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y); if (g_ownbackstore) - XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, y); + { + if (g_Unobscured) + { + XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y); + XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, + y); + } + else + { + XCopyArea(g_display, g_backstore, g_wnd, g_gc, srcx, srcy, cx, cy, x, y); + XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, + y); + } + } + else + { + XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y); + } RESET_FUNCTION(opcode); } @@ -1569,33 +2012,34 @@ {\ glyph = cache_get_font (font, ttext[idx]);\ if (!(flags & TEXT2_IMPLICIT_X))\ + {\ + xyoffset = ttext[++idx];\ + if ((xyoffset & 0x80))\ {\ - xyoffset = ttext[++idx];\ - if ((xyoffset & 0x80))\ - {\ - if (flags & TEXT2_VERTICAL) \ - y += ttext[idx+1] | (ttext[idx+2] << 8);\ - else\ - x += ttext[idx+1] | (ttext[idx+2] << 8);\ - idx += 2;\ - }\ + if (flags & TEXT2_VERTICAL)\ + y += ttext[idx+1] | (ttext[idx+2] << 8);\ else\ - {\ - if (flags & TEXT2_VERTICAL) \ - y += xyoffset;\ - else\ - x += xyoffset;\ - }\ + x += ttext[idx+1] | (ttext[idx+2] << 8);\ + idx += 2;\ }\ - if (glyph != NULL)\ + else\ {\ - ui_draw_glyph (mixmode, x + glyph->offset,\ - y + glyph->baseline,\ - glyph->width, glyph->height,\ - glyph->pixmap, 0, 0, bgcolour, fgcolour);\ - if (flags & TEXT2_IMPLICIT_X)\ - x += glyph->width;\ + if (flags & TEXT2_VERTICAL)\ + y += xyoffset;\ + else\ + x += xyoffset;\ }\ + }\ + if (glyph != NULL)\ + {\ + x1 = x + glyph->offset;\ + y1 = y + glyph->baseline;\ + XSetStipple(g_display, g_gc, (Pixmap) glyph->pixmap);\ + XSetTSOrigin(g_display, g_gc, x1, y1);\ + FILL_RECTANGLE_BACKSTORE(x1, y1, glyph->width, glyph->height);\ + if (flags & TEXT2_IMPLICIT_X)\ + x += glyph->width;\ + }\ } void @@ -1605,11 +2049,17 @@ int fgcolour, uint8 * text, uint8 length) { FONTGLYPH *glyph; - int i, j, xyoffset; + int i, j, xyoffset, x1, y1; DATABLOB *entry; SET_FOREGROUND(bgcolour); + /* Sometimes, the boxcx value is something really large, like + 32691. This makes XCopyArea fail with Xvnc. The code below + is a quick fix. */ + if (boxx + boxcx > g_width) + boxcx = g_width - boxx; + if (boxcx > 1) { FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy); @@ -1619,6 +2069,10 @@ FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy); } + SET_FOREGROUND(fgcolour); + SET_BACKGROUND(bgcolour); + XSetFillStyle(g_display, g_gc, FillStippled); + /* Paint text, character by character */ for (i = 0; i < length;) { @@ -1669,6 +2123,9 @@ break; } } + + XSetFillStyle(g_display, g_gc, FillSolid); + if (g_ownbackstore) { if (boxcx > 1) @@ -1730,3 +2187,14 @@ XFree(image); } + +/* these do nothing here but are used in uiports */ +void +ui_begin_update(void) +{ +} + +void +ui_end_update(void) +{ +}