--- sourceforge.net/trunk/rdesktop/xwin.c 2003/10/31 04:29:57 536 +++ sourceforge.net/trunk/rdesktop/xwin.c 2004/03/25 22:58:45 644 @@ -20,8 +20,11 @@ #include #include +#include +#include #include #include +#include #include "rdesktop.h" #include "xproto.h" @@ -40,8 +43,9 @@ static int g_x_socket; static Screen *g_screen; Window g_wnd; +uint32 g_embed_wnd; BOOL g_enable_compose = False; -static GC g_gc; +static GC g_gc = NULL; static Visual *g_visual; static int g_depth; static int g_bpp; @@ -49,10 +53,11 @@ static XIC g_IC; static XModifierKeymap *g_mod_map; static Cursor g_current_cursor; -static HCURSOR g_null_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; @@ -61,8 +66,8 @@ 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; +BOOL g_ownbackstore = True; /* We can't rely on external BackingStore */ +static Pixmap g_backstore = 0; /* Moving in single app mode */ static BOOL g_moving_wnd; @@ -166,12 +171,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 & 0x7c00) >> 7; + rv.green = (colour & 0x03e0) >> 2; + rv.blue = (colour & 0x001f) << 3; return rv; } @@ -179,12 +181,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 & 0xf800) >> 8; + rv.green = (colour & 0x07e0) >> 3; + rv.blue = (colour & 0x001f) << 3; return rv; } @@ -193,8 +192,8 @@ { 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; } @@ -242,17 +241,20 @@ { uint16 value; - while (out < end) + if (g_xserver_be) { - value = (uint16) g_colmap[*(data++)]; - - if (g_xserver_be) + while (out < end) { + value = (uint16) g_colmap[*(data++)]; *(out++) = value >> 8; *(out++) = value; } - else + } + else + { + while (out < end) { + value = (uint16) g_colmap[*(data++)]; *(out++) = value; *(out++) = value >> 8; } @@ -265,18 +267,21 @@ { uint32 value; - while (out < end) + if (g_xserver_be) { - value = g_colmap[*(data++)]; - - if (g_xserver_be) + while (out < end) { + value = g_colmap[*(data++)]; *(out++) = value >> 16; *(out++) = value >> 8; *(out++) = value; } - else + } + else + { + while (out < end) { + value = g_colmap[*(data++)]; *(out++) = value; *(out++) = value >> 8; *(out++) = value >> 16; @@ -289,19 +294,22 @@ { uint32 value; - while (out < end) + if (g_xserver_be) { - value = g_colmap[*(data++)]; - - if (g_xserver_be) + while (out < end) { + value = g_colmap[*(data++)]; *(out++) = value >> 24; *(out++) = value >> 16; *(out++) = value >> 8; *(out++) = value; } - else + } + else + { + while (out < end) { + value = g_colmap[*(data++)]; *(out++) = value; *(out++) = value >> 8; *(out++) = value >> 16; @@ -310,8 +318,6 @@ } } -/* todo the remaining translate function might need some big endian check ?? */ - static void translate15to16(uint16 * data, uint8 * out, uint8 * end) { @@ -542,7 +548,7 @@ pixel |= *(data++); value = make_colour(split_colour24(pixel)); - + if (g_xserver_be) { *(out++) = value >> 16; @@ -592,9 +598,21 @@ 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_depth > 8) + if (g_arch_match) + if (g_depth == g_server_bpp) + return data; + + size = width * height * (g_bpp / 8); + out = (uint8 *) xmalloc(size); + end = out + size; switch (g_server_bpp) { @@ -700,7 +718,10 @@ XVisualInfo vi; XPixmapFormatValues *pfm; uint16 test; - int i, screen_num; + int i, screen_num, nvisuals; + XVisualInfo *vmatches = NULL; + XVisualInfo template; + Bool TrueColorVisual = False; g_display = XOpenDisplay(NULL); if (g_display == NULL) @@ -714,10 +735,36 @@ g_screen = ScreenOfDisplay(g_display, screen_num); g_depth = DefaultDepthOfScreen(g_screen); - if (g_server_bpp == 8) + /* 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 any visual should do */ + /* 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 { @@ -733,6 +780,11 @@ 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); @@ -759,24 +811,28 @@ 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); + } /* * Determine desktop size */ - if (g_width < 0) + 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; @@ -799,26 +855,10 @@ g_height = 600; } } - else 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(); @@ -839,6 +879,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) @@ -865,12 +908,27 @@ 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_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); @@ -895,6 +953,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; @@ -936,7 +999,8 @@ 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); + if (g_null_cursor == NULL) + g_null_cursor = ui_create_cursor(0, 0, 1, 1, null_pointer_mask, null_pointer_data); return True; } @@ -944,8 +1008,6 @@ void ui_destroy_window(void) { - ui_destroy_cursor(g_null_cursor); - if (g_IC != NULL) XDestroyIC(g_IC); @@ -989,9 +1051,6 @@ key_translation tr; char str[256]; Status status; - unsigned int state; - Window wdummy; - int dummy; while (XPending(g_display) > 0) { @@ -1164,9 +1223,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); @@ -1246,11 +1303,14 @@ int ui_select(int rdp_socket) { - int n = (rdp_socket > g_x_socket) ? rdp_socket + 1 : g_x_socket + 1; + 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 */ @@ -1266,19 +1326,35 @@ if (g_dsp_busy) { FD_SET(g_dsp_fd, &wfds); - n = (g_dsp_fd + 1 > n) ? g_dsp_fd + 1 : n; + n = (g_dsp_fd > n) ? g_dsp_fd : n; } #endif + /* default timeout */ + tv.tv_sec = 60; + tv.tv_usec = 0; - switch (select(n, &rfds, &wfds, NULL, NULL)) + /* 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; @@ -1323,7 +1399,7 @@ XPutImage(g_display, bitmap, g_gc, image, 0, 0, 0, 0, width, height); XFree(image); - if (!g_owncolmap) + if (tdata != data) xfree(tdata); return (HBITMAP) bitmap; } @@ -1362,7 +1438,7 @@ } XFree(image); - if (!g_owncolmap) + if (tdata != data) xfree(tdata); } @@ -1734,9 +1810,15 @@ /* 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_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); } @@ -1831,33 +1913,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 @@ -1867,11 +1950,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); @@ -1881,6 +1970,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;) { @@ -1931,6 +2024,9 @@ break; } } + + XSetFillStyle(g_display, g_gc, FillSolid); + if (g_ownbackstore) { if (boxcx > 1)