--- sourceforge.net/trunk/rdesktop/xwin.c 2005/11/23 13:26:29 1031 +++ sourceforge.net/branches/seamlessrdp-branch/rdesktop/xwin.c 2006/03/10 12:44:32 1098 @@ -38,7 +38,9 @@ extern BOOL g_grab_keyboard; extern BOOL g_hide_decorations; extern char g_title[]; -extern int g_server_bpp; +/* Color depth of the RDP session. + As of RDP 5.1, it may be 8, 15, 16 or 24. */ +extern int g_server_depth; extern int g_win_button_size; Display *g_display; @@ -46,14 +48,34 @@ static int g_x_socket; static Screen *g_screen; Window g_wnd; + +/* SeamlessRDP support */ +typedef struct _seamless_window +{ + Window wnd; + unsigned long id; + int xoffset, yoffset; + int width, height; + struct _seamless_window *next; +} seamless_window; +static seamless_window *g_seamless_windows = NULL; +extern BOOL g_seamless_rdp; + 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 XRectangle g_clip_rectangle; static Visual *g_visual; +/* Color depth of the X11 visual of our window (e.g. 24 for True Color R8G8B visual). + This may be 32 for R8G8B8 visuals, and then the rest of the bits are undefined + as far as we're concerned. */ static int g_depth; +/* Bits-per-Pixel of the pixmaps we'll be using to draw on our window. + This may be larger than g_depth, in which case some of the bits would + be kept solely for alignment (e.g. 32bpp pixmaps on a 24bpp visual). */ static int g_bpp; static XIM g_IM; static XIC g_IC; @@ -63,7 +85,24 @@ 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 */ +/* Indicates that: + 1) visual has 15, 16 or 24 depth and the same color channel masks + as its RDP equivalent (implies X server is LE), + 2) host is LE + This will trigger an optimization whose real value is questionable. +*/ +static BOOL g_compatible_arch; +/* Indicates whether RDP's bitmaps and our XImages have the same + binary format. If so, we can avoid an expensive translation. + Note that this can be true when g_compatible_arch is false, + e.g.: + + RDP(LE) <-> host(BE) <-> X-Server(LE) + + ('host' is the machine running rdesktop; the host simply memcpy's + so its endianess doesn't matter) + */ +static BOOL g_no_translate_image = False; /* endianness */ static BOOL g_host_be; @@ -92,11 +131,11 @@ #define PROP_MOTIF_WM_HINTS_ELEMENTS 5 typedef struct { - uint32 flags; - uint32 functions; - uint32 decorations; - sint32 inputMode; - uint32 status; + unsigned long flags; + unsigned long functions; + unsigned long decorations; + long inputMode; + unsigned long status; } PropMotifWmHints; @@ -108,10 +147,45 @@ } PixelColour; +#define ON_ALL_SEAMLESS_WINDOWS(func, args) \ + do { \ + seamless_window *sw; \ + XRectangle rect; \ + for (sw = g_seamless_windows; sw; sw = sw->next) { \ + rect.x = g_clip_rectangle.x - sw->xoffset; \ + rect.y = g_clip_rectangle.y - sw->yoffset; \ + rect.width = g_clip_rectangle.width; \ + rect.height = g_clip_rectangle.height; \ + XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded); \ + func args; \ + } \ + XSetClipRectangles(g_display, g_gc, 0, 0, &g_clip_rectangle, 1, YXBanded); \ + } while (0) + +static void +seamless_XFillPolygon(Drawable d, XPoint * points, int npoints, int xoffset, int yoffset) +{ + points[0].x -= xoffset; + points[0].y -= yoffset; + XFillPolygon(g_display, d, g_gc, points, npoints, Complex, CoordModePrevious); + points[0].x += xoffset; + points[0].y += yoffset; +} + +static void +seamless_XDrawLines(Drawable d, XPoint * points, int npoints, int xoffset, int yoffset) +{ + points[0].x -= xoffset; + points[0].y -= yoffset; + XDrawLines(g_display, d, g_gc, points, npoints, CoordModePrevious); + points[0].x += xoffset; + points[0].y += yoffset; +} #define FILL_RECTANGLE(x,y,cx,cy)\ { \ XFillRectangle(g_display, g_wnd, g_gc, x, y, cx, cy); \ + ON_ALL_SEAMLESS_WINDOWS(XFillRectangle, (g_display, sw->wnd, g_gc, x-sw->xoffset, y-sw->yoffset, cx, cy)); \ if (g_ownbackstore) \ XFillRectangle(g_display, g_backstore, g_gc, x, y, cx, cy); \ } @@ -126,6 +200,7 @@ XFillPolygon(g_display, g_wnd, g_gc, p, np, Complex, CoordModePrevious); \ if (g_ownbackstore) \ XFillPolygon(g_display, g_backstore, g_gc, p, np, Complex, CoordModePrevious); \ + ON_ALL_SEAMLESS_WINDOWS(seamless_XFillPolygon, (sw->wnd, p, np, sw->xoffset, sw->yoffset)); \ } #define DRAW_ELLIPSE(x,y,cx,cy,m)\ @@ -134,11 +209,14 @@ { \ case 0: /* Outline */ \ XDrawArc(g_display, g_wnd, g_gc, x, y, cx, cy, 0, 360*64); \ + ON_ALL_SEAMLESS_WINDOWS(XDrawArc, (g_display, sw->wnd, g_gc, x-sw->xoffset, y-sw->yoffset, cx, cy, 0, 360*64)); \ if (g_ownbackstore) \ XDrawArc(g_display, g_backstore, g_gc, x, y, cx, cy, 0, 360*64); \ break; \ case 1: /* Filled */ \ - XFillArc(g_display, g_wnd, g_gc, x, y, cx, cy, 0, 360*64); \ + XFillArc(g_display, g_wnd, g_gc, x, y, \ + cx, cy, 0, 360*64); \ + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, (g_display, g_ownbackstore ? g_backstore : g_wnd, sw->wnd, g_gc, x, y, cx, cy, x-sw->xoffset, y-sw->yoffset)); \ if (g_ownbackstore) \ XFillArc(g_display, g_backstore, g_gc, x, y, cx, cy, 0, 360*64); \ break; \ @@ -150,7 +228,7 @@ static Colormap g_xcolmap; static uint32 *g_colmap = NULL; -#define TRANSLATE(col) ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : g_colmap[col] ) +#define TRANSLATE(col) ( g_server_depth != 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)); @@ -176,8 +254,52 @@ #define SET_FUNCTION(rop2) { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, rop2_map[rop2]); } #define RESET_FUNCTION(rop2) { if (rop2 != ROP2_COPY) XSetFunction(g_display, g_gc, GXcopy); } +static seamless_window * +seamless_get_window_by_id(unsigned long id) +{ + seamless_window *sw; + for (sw = g_seamless_windows; sw; sw = sw->next) + { + if (sw->id == id) + return sw; + } + return NULL; +} + + +static seamless_window * +seamless_get_window_by_wnd(Window wnd) +{ + seamless_window *sw; + for (sw = g_seamless_windows; sw; sw = sw->next) + { + if (sw->wnd == wnd) + return sw; + } + return NULL; +} + + static void -mwm_hide_decorations(void) +seamless_remove_window(seamless_window * win) +{ + seamless_window *sw, **prevnext = &g_seamless_windows; + for (sw = g_seamless_windows; sw; sw = sw->next) + { + if (sw == win) + { + *prevnext = sw->next; + xfree(sw); + return; + } + prevnext = &sw->next; + } + return; +} + + +static void +mwm_hide_decorations(Window wnd) { PropMotifWmHints motif_hints; Atom hintsatom; @@ -194,8 +316,9 @@ return; } - XChangeProperty(g_display, g_wnd, hintsatom, hintsatom, 32, PropModeReplace, + XChangeProperty(g_display, wnd, hintsatom, hintsatom, 32, PropModeReplace, (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS); + } #define SPLITCOLOUR15(colour, rv) \ @@ -229,6 +352,9 @@ #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \ x = (x << 16) | (x >> 16); } +/* The following macros output the same octet sequences + on both BE and LE hosts: */ + #define BOUT16(o, x) { *(o++) = x >> 8; *(o++) = x; } #define BOUT24(o, x) { *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; } #define BOUT32(o, x) { *(o++) = x >> 24; *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; } @@ -240,7 +366,7 @@ translate_colour(uint32 colour) { PixelColour pc; - switch (g_server_bpp) + switch (g_server_depth) { case 15: SPLITCOLOUR15(colour, pc); @@ -251,6 +377,12 @@ case 24: SPLITCOLOUR24(colour, pc); break; + default: + /* Avoid warning */ + pc.red = 0; + pc.green = 0; + pc.blue = 0; + break; } return MAKECOLOUR(pc); } @@ -302,7 +434,7 @@ { uint16 value; - if (g_arch_match) + if (g_compatible_arch) { /* *INDENT-OFF* */ REPEAT2 @@ -336,7 +468,7 @@ { uint32 value; - if (g_xserver_be) + if (g_compatible_arch) { while (out < end) { @@ -359,7 +491,7 @@ { uint32 value; - if (g_arch_match) + if (g_compatible_arch) { /* *INDENT-OFF* */ REPEAT4 @@ -431,7 +563,7 @@ uint16 pixel; PixelColour pc; - if (g_arch_match) + if (g_compatible_arch) { /* *INDENT-OFF* */ REPEAT3 @@ -481,7 +613,7 @@ uint32 value; PixelColour pc; - if (g_arch_match) + if (g_compatible_arch) { /* *INDENT-OFF* */ REPEAT4 @@ -589,7 +721,7 @@ uint16 pixel; PixelColour pc; - if (g_arch_match) + if (g_compatible_arch) { /* *INDENT-OFF* */ REPEAT3 @@ -659,7 +791,7 @@ uint32 value; PixelColour pc; - if (g_arch_match) + if (g_compatible_arch) { /* *INDENT-OFF* */ REPEAT4 @@ -788,7 +920,7 @@ uint32 value; PixelColour pc; - if (g_arch_match) + if (g_compatible_arch) { /* *INDENT-OFF* */ #ifdef NEED_ALIGN @@ -802,9 +934,10 @@ #else REPEAT4 ( - *((uint32 *) out) = *((uint32 *) data); - out += 4; - data += 3; + /* Only read 3 bytes. Reading 4 bytes means reading beyond buffer. */ + *((uint32 *) out) = *((uint16 *) data) + (*((uint8 *) data + 2) << 16); + out += 4; + data += 3; ) #endif /* *INDENT-ON* */ @@ -842,16 +975,19 @@ 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 RDP depth and X Visual depths match, + and arch(endian) matches, no need to translate: + just return data. + Note: select_visual should've already ensured g_no_translate + is only set for compatible depths, but the RDP depth might've + changed during connection negotiations. + */ + if (g_no_translate_image) { - if (g_depth == 15 && g_server_bpp == 15) - return data; - if (g_depth == 16 && g_server_bpp == 16) - return data; - if (g_depth == 24 && g_bpp == 24 && g_server_bpp == 24) + if ((g_depth == 15 && g_server_depth == 15) || + (g_depth == 16 && g_server_depth == 16) || + (g_depth == 24 && g_server_depth == 24)) return data; } @@ -859,7 +995,7 @@ out = (uint8 *) xmalloc(size); end = out + size; - switch (g_server_bpp) + switch (g_server_depth) { case 24: switch (g_bpp) @@ -957,126 +1093,260 @@ *shift_r = 8 - ffs(mask & ~(mask >> 1)); } -BOOL -ui_init(void) +/* Given a mask of a colour channel (e.g. XVisualInfo.red_mask), + calculates the bits-per-pixel of this channel (a.k.a. colour weight). + */ +static unsigned +calculate_mask_weight(uint32 mask) +{ + unsigned weight = 0; + do + { + weight += (mask & 1); + } + while (mask >>= 1); + return weight; +} + +static BOOL +select_visual() { - XVisualInfo vi; XPixmapFormatValues *pfm; - uint16 test; - int i, screen_num, nvisuals; + int pixmap_formats_count, visuals_count; XVisualInfo *vmatches = NULL; XVisualInfo template; - Bool TrueColorVisual = False; + int i; + unsigned red_weight, blue_weight, green_weight; - g_display = XOpenDisplay(NULL); - if (g_display == NULL) + red_weight = blue_weight = green_weight = 0; + + pfm = XListPixmapFormats(g_display, &pixmap_formats_count); + if (pfm == NULL) { - error("Failed to open display: %s\n", XDisplayName(NULL)); + error("Unable to get list of pixmap formats from display.\n"); + XCloseDisplay(g_display); return False; } - screen_num = DefaultScreen(g_display); - g_x_socket = ConnectionNumber(g_display); - g_screen = ScreenOfDisplay(g_display, screen_num); - g_depth = DefaultDepthOfScreen(g_screen); - - /* Search for best TrueColor depth */ + /* Search for best TrueColor visual */ template.class = TrueColor; - vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &nvisuals); + vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &visuals_count); + g_visual = NULL; + g_no_translate_image = False; + g_compatible_arch = False; + if (vmatches != NULL) + { + for (i = 0; i < visuals_count; ++i) + { + XVisualInfo *visual_info = &vmatches[i]; + + /* Try to find a no-translation visual that'll + allow us to use RDP bitmaps directly as ZPixmaps. */ + if (!g_xserver_be && (((visual_info->depth == 15) && + /* R5G5B5 */ + (visual_info->red_mask == 0x7c00) && + (visual_info->green_mask == 0x3e0) && + (visual_info->blue_mask == 0x1f)) || + ((visual_info->depth == 16) && + /* R5G6B5 */ + (visual_info->red_mask == 0xf800) && + (visual_info->green_mask == 0x7e0) && + (visual_info->blue_mask == 0x1f)) || + ((visual_info->depth == 24) && + /* R8G8B8 */ + (visual_info->red_mask == 0xff0000) && + (visual_info->green_mask == 0xff00) && + (visual_info->blue_mask == 0xff)))) + { + g_visual = visual_info->visual; + g_depth = visual_info->depth; + g_compatible_arch = !g_host_be; + g_no_translate_image = (visual_info->depth == g_server_depth); + if (g_no_translate_image) + /* We found the best visual */ + break; + } + else + { + g_compatible_arch = False; + } + + if (visual_info->depth > 24) + { + /* Avoid 32-bit visuals and likes like the plague. + They're either untested or proven to work bad + (e.g. nvidia's Composite 32-bit visual). + Most implementation offer a 24-bit visual anyway. */ + continue; + } - nvisuals--; - while (nvisuals >= 0) - { - if ((vmatches + nvisuals)->depth > g_depth) - { - g_depth = (vmatches + nvisuals)->depth; + /* Only care for visuals, for whose BPPs (not depths!) + we have a translateXtoY function. */ + BOOL can_translate_to_bpp = False; + int j; + for (j = 0; j < pixmap_formats_count; ++j) + { + if (pfm[j].depth == visual_info->depth) + { + if ((pfm[j].bits_per_pixel == 16) || + (pfm[j].bits_per_pixel == 24) || + (pfm[j].bits_per_pixel == 32)) + { + can_translate_to_bpp = True; + } + break; + } + } + + /* Prefer formats which have the most colour depth. + We're being truly aristocratic here, minding each + weight on its own. */ + if (can_translate_to_bpp) + { + unsigned vis_red_weight = + calculate_mask_weight(visual_info->red_mask); + unsigned vis_green_weight = + calculate_mask_weight(visual_info->green_mask); + unsigned vis_blue_weight = + calculate_mask_weight(visual_info->blue_mask); + if ((vis_red_weight >= red_weight) + && (vis_green_weight >= green_weight) + && (vis_blue_weight >= blue_weight)) + { + red_weight = vis_red_weight; + green_weight = vis_green_weight; + blue_weight = vis_blue_weight; + g_visual = visual_info->visual; + g_depth = visual_info->depth; + } + } } - nvisuals--; - TrueColorVisual = True; + XFree(vmatches); } - test = 1; - g_host_be = !(BOOL) (*(uint8 *) (&test)); - g_xserver_be = (ImageByteOrder(g_display) == MSBFirst); - - if ((g_server_bpp == 8) && ((!TrueColorVisual) || (g_depth <= 8))) + if (g_visual != NULL) { - /* 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; - } + g_owncolmap = False; + calculate_shifts(g_visual->red_mask, &g_red_shift_r, &g_red_shift_l); + calculate_shifts(g_visual->green_mask, &g_green_shift_r, &g_green_shift_l); + calculate_shifts(g_visual->blue_mask, &g_blue_shift_r, &g_blue_shift_l); } 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"); + template.class = PseudoColor; + template.depth = 8; + template.colormap_size = 256; + vmatches = + XGetVisualInfo(g_display, + VisualClassMask | VisualDepthMask | VisualColormapSizeMask, + &template, &visuals_count); + if (vmatches == NULL) + { + error("No usable TrueColor or PseudoColor visuals on this display.\n"); + XCloseDisplay(g_display); + XFree(pfm); 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); + /* we use a colourmap, so the default visual should do */ + g_owncolmap = True; + g_visual = vmatches[0].visual; + g_depth = vmatches[0].depth; + } - /* if RGB video and everything is little endian */ - if ((vi.red_mask > vi.green_mask && vi.green_mask > vi.blue_mask) && - !g_xserver_be && !g_host_be) + g_bpp = 0; + for (i = 0; i < pixmap_formats_count; ++i) + { + XPixmapFormatValues *pf = &pfm[i]; + if (pf->depth == g_depth) { - if (g_depth <= 16 || (g_red_shift_l == 16 && g_green_shift_l == 8 && - g_blue_shift_l == 0)) + g_bpp = pf->bits_per_pixel; + + if (g_no_translate_image) { - g_arch_match = True; + switch (g_server_depth) + { + case 15: + case 16: + if (g_bpp != 16) + g_no_translate_image = False; + break; + case 24: + /* Yes, this will force image translation + on most modern servers which use 32 bits + for R8G8B8. */ + if (g_bpp != 24) + g_no_translate_image = False; + break; + default: + g_no_translate_image = False; + break; + } } - } - if (g_arch_match) - { - DEBUG(("Architectures match, enabling little endian optimisations.\n")); + /* Pixmap formats list is a depth-to-bpp mapping -- + there's just a single entry for every depth, + so we can safely break here */ + break; } } + XFree(pfm); + pfm = NULL; + return True; +} + +BOOL +ui_init(void) +{ + int screen_num; - pfm = XListPixmapFormats(g_display, &i); - if (pfm != NULL) + g_display = XOpenDisplay(NULL); + if (g_display == NULL) { - /* Use maximum bpp for this depth - this is generally - desirable, e.g. 24 bits->32 bits. */ - while (i--) - { - if ((pfm[i].depth == g_depth) && (pfm[i].bits_per_pixel > g_bpp)) - { - g_bpp = pfm[i].bits_per_pixel; - } - } - XFree(pfm); + error("Failed to open display: %s\n", XDisplayName(NULL)); + return False; } - if (g_bpp < 8) { - error("Less than 8 bpp not currently supported.\n"); - XCloseDisplay(g_display); + uint16 endianess_test = 1; + g_host_be = !(BOOL) (*(uint8 *) (&endianess_test)); + } + + g_xserver_be = (ImageByteOrder(g_display) == MSBFirst); + screen_num = DefaultScreen(g_display); + g_x_socket = ConnectionNumber(g_display); + g_screen = ScreenOfDisplay(g_display, screen_num); + g_depth = DefaultDepthOfScreen(g_screen); + + if (!select_visual()) return False; + + if (g_no_translate_image) + { + DEBUG(("Performance optimization possible: avoiding image translation (colour depth conversion).\n")); + } + + if (g_server_depth > g_bpp) + { + warning("Remote desktop colour depth %d higher than display colour depth %d.\n", + g_server_depth, g_bpp); } + DEBUG(("RDP depth: %d, display depth: %d, display bpp: %d, X server BE: %d, host BE: %d\n", + g_server_depth, g_depth, g_bpp, g_xserver_be, g_host_be)); + if (!g_owncolmap) { 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"); + warning("Display colour depth is %d bit: you may want to use -C for a private colourmap.\n", g_depth); } if ((!g_ownbackstore) && (DoesBackingStore(g_screen) != Always)) { - warning("External BackingStore not available, using internal\n"); + warning("External BackingStore not available. Using internal.\n"); g_ownbackstore = True; } @@ -1087,6 +1357,7 @@ { g_width = WidthOfScreen(g_screen); g_height = HeightOfScreen(g_screen); + g_using_full_workarea = True; } else if (g_width < 0) { @@ -1100,18 +1371,17 @@ { /* Fetch geometry from _NET_WORKAREA */ uint32 x, y, cx, cy; - g_using_full_workarea = True; - if (get_current_workarea(&x, &y, &cx, &cy) == 0) { g_width = cx; g_height = cy; + g_using_full_workarea = True; } else { warning("Failed to get workarea: probably your window manager does not support extended hints\n"); - g_width = 800; - g_height = 600; + g_width = WidthOfScreen(g_screen); + g_height = HeightOfScreen(g_screen); } } @@ -1126,8 +1396,10 @@ g_IM = XOpenIM(g_display, NULL, NULL, NULL); xclip_init(); + if (g_seamless_rdp) + seamless_init(); - DEBUG_RDP5(("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_depth, g_bpp, g_depth)); return True; } @@ -1151,6 +1423,34 @@ g_display = NULL; } + +static void +get_window_attribs(XSetWindowAttributes * attribs) +{ + attribs->background_pixel = BlackPixelOfScreen(g_screen); + attribs->background_pixel = WhitePixelOfScreen(g_screen); + attribs->border_pixel = WhitePixelOfScreen(g_screen); + attribs->backing_store = g_ownbackstore ? NotUseful : Always; + attribs->override_redirect = g_fullscreen; + attribs->colormap = g_xcolmap; +} + +static void +get_input_mask(long *input_mask) +{ + *input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | + VisibilityChangeMask | FocusChangeMask | StructureNotifyMask; + + if (g_sendmotion) + *input_mask |= PointerMotionMask; + if (g_ownbackstore) + *input_mask |= ExposureMask; + if (g_fullscreen || g_grab_keyboard) + *input_mask |= EnterWindowMask; + if (g_grab_keyboard) + *input_mask |= LeaveWindowMask; +} + BOOL ui_create_window(void) { @@ -1173,11 +1473,7 @@ if (g_ypos < 0 || (g_ypos == 0 && (g_pos & 4))) g_ypos = HeightOfScreen(g_screen) + g_ypos - 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; + get_window_attribs(&attribs); g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), g_xpos, g_ypos, wndwidth, wndheight, 0, g_depth, InputOutput, g_visual, @@ -1185,7 +1481,10 @@ CWBorderPixel, &attribs); if (g_gc == NULL) + { g_gc = XCreateGC(g_display, g_wnd, 0, NULL); + ui_reset_clip(); + } if (g_create_bitmap_gc == NULL) g_create_bitmap_gc = XCreateGC(g_display, g_wnd, 0, NULL); @@ -1202,7 +1501,7 @@ XStoreName(g_display, g_wnd, g_title); if (g_hide_decorations) - mwm_hide_decorations(); + mwm_hide_decorations(g_wnd); classhints = XAllocClassHint(); if (classhints != NULL) @@ -1229,17 +1528,7 @@ XReparentWindow(g_display, g_wnd, (Window) g_embed_wnd, 0, 0); } - input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | - VisibilityChangeMask | FocusChangeMask | StructureNotifyMask; - - if (g_sendmotion) - input_mask |= PointerMotionMask; - if (g_ownbackstore) - input_mask |= ExposureMask; - if (g_fullscreen || g_grab_keyboard) - input_mask |= EnterWindowMask; - if (g_grab_keyboard) - input_mask |= LeaveWindowMask; + get_input_mask(&input_mask); if (g_IM != NULL) { @@ -1252,15 +1541,17 @@ } XSelectInput(g_display, g_wnd, input_mask); - XMapWindow(g_display, g_wnd); - - /* wait for VisibilityNotify */ - do + if (!g_seamless_rdp) { - XMaskEvent(g_display, VisibilityChangeMask, &xevent); + XMapWindow(g_display, g_wnd); + /* wait for VisibilityNotify */ + do + { + XMaskEvent(g_display, VisibilityChangeMask, &xevent); + } + while (xevent.type != VisibilityNotify); + g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured; } - while (xevent.type != VisibilityNotify); - g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured; g_focused = False; g_mouse_in_wnd = False; @@ -1324,6 +1615,10 @@ { Pixmap contents = 0; + if (g_seamless_rdp) + /* Turn off SeamlessRDP mode */ + ui_seamless_toggle(); + if (!g_ownbackstore) { /* need to save contents of window */ @@ -1412,8 +1707,17 @@ } } - rdp_send_input(time(NULL), RDP_INPUT_MOUSE, - flags | button, xevent.xbutton.x, xevent.xbutton.y); + if (xevent.xmotion.window == g_wnd) + { + rdp_send_input(time(NULL), RDP_INPUT_MOUSE, + flags | button, xevent.xbutton.x, xevent.xbutton.y); + } + else + { + /* SeamlessRDP */ + rdp_send_input(time(NULL), RDP_INPUT_MOUSE, + flags | button, xevent.xbutton.x_root, xevent.xbutton.y_root); + } } /* Process events in Xlib queue @@ -1427,6 +1731,7 @@ char str[256]; Status status; int events = 0; + seamless_window *sw; while ((XPending(g_display) > 0) && events++ < 20) { @@ -1441,7 +1746,10 @@ switch (xevent.type) { case VisibilityNotify: - g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured; + if (xevent.xvisibility.window == g_wnd) + g_Unobscured = + xevent.xvisibility.state == VisibilityUnobscured; + break; case ClientMessage: /* the window manager told us to quit */ @@ -1521,8 +1829,19 @@ if (g_fullscreen && !g_focused) XSetInputFocus(g_display, g_wnd, RevertToPointerRoot, CurrentTime); - rdp_send_input(time(NULL), RDP_INPUT_MOUSE, - MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y); + + if (xevent.xmotion.window == g_wnd) + { + rdp_send_input(time(NULL), RDP_INPUT_MOUSE, MOUSE_FLAG_MOVE, + xevent.xmotion.x, xevent.xmotion.y); + } + else + { + /* SeamlessRDP */ + rdp_send_input(time(NULL), RDP_INPUT_MOUSE, MOUSE_FLAG_MOVE, + xevent.xmotion.x_root, + xevent.xmotion.y_root); + } break; case FocusIn: @@ -1565,11 +1884,27 @@ break; case Expose: - XCopyArea(g_display, g_backstore, g_wnd, g_gc, - xevent.xexpose.x, xevent.xexpose.y, - xevent.xexpose.width, - xevent.xexpose.height, - xevent.xexpose.x, xevent.xexpose.y); + if (xevent.xexpose.window == g_wnd) + { + XCopyArea(g_display, g_backstore, xevent.xexpose.window, + g_gc, + xevent.xexpose.x, xevent.xexpose.y, + xevent.xexpose.width, xevent.xexpose.height, + xevent.xexpose.x, xevent.xexpose.y); + } + else + { + sw = seamless_get_window_by_wnd(xevent.xexpose.window); + if (sw) + XCopyArea(g_display, g_backstore, + xevent.xexpose.window, g_gc, + xevent.xexpose.x + sw->xoffset, + xevent.xexpose.y + sw->yoffset, + xevent.xexpose.width, + xevent.xexpose.height, xevent.xexpose.x, + xevent.xexpose.y); + } + break; case MappingNotify: @@ -1600,10 +1935,12 @@ xclip_handle_PropertyNotify(&xevent.xproperty); break; case MapNotify: - rdp_send_client_window_status(1); + if (!g_seamless_rdp) + rdp_send_client_window_status(1); break; case UnmapNotify: - rdp_send_client_window_status(0); + if (!g_seamless_rdp) + rdp_send_client_window_status(0); break; } } @@ -1688,7 +2025,7 @@ uint8 *tdata; int bitmap_pad; - if (g_server_bpp == 8) + if (g_server_depth == 8) { bitmap_pad = 8; } @@ -1720,7 +2057,7 @@ uint8 *tdata; int bitmap_pad; - if (g_server_bpp == 8) + if (g_server_depth == 8) { bitmap_pad = 8; } @@ -1740,10 +2077,16 @@ { XPutImage(g_display, g_backstore, g_gc, image, 0, 0, x, y, cx, cy); XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y); + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, + (g_display, g_backstore, sw->wnd, g_gc, x, y, cx, cy, + x - sw->xoffset, y - sw->yoffset)); } else { XPutImage(g_display, g_wnd, g_gc, image, 0, 0, x, y, cx, cy); + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, + (g_display, g_wnd, sw->wnd, g_gc, x, y, cx, cy, + x - sw->xoffset, y - sw->yoffset)); } XFree(image); @@ -1864,6 +2207,7 @@ { g_current_cursor = (Cursor) cursor; XDefineCursor(g_display, g_wnd, g_current_cursor); + ON_ALL_SEAMLESS_WINDOWS(XDefineCursor, (g_display, sw->wnd, g_current_cursor)); } void @@ -2005,31 +2349,30 @@ g_colmap = (uint32 *) map; } else + { XSetWindowColormap(g_display, g_wnd, (Colormap) map); + ON_ALL_SEAMLESS_WINDOWS(XSetWindowColormap, (g_display, sw->wnd, (Colormap) map)); + } } void ui_set_clip(int x, int y, int cx, int cy) { - XRectangle rect; - - rect.x = x; - rect.y = y; - rect.width = cx; - rect.height = cy; - XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded); + g_clip_rectangle.x = x; + g_clip_rectangle.y = y; + g_clip_rectangle.width = cx; + g_clip_rectangle.height = cy; + XSetClipRectangles(g_display, g_gc, 0, 0, &g_clip_rectangle, 1, YXBanded); } void ui_reset_clip(void) { - XRectangle rect; - - rect.x = 0; - rect.y = 0; - rect.width = g_width; - rect.height = g_height; - XSetClipRectangles(g_display, g_gc, 0, 0, &rect, 1, YXBanded); + g_clip_rectangle.x = 0; + g_clip_rectangle.y = 0; + g_clip_rectangle.width = g_width; + g_clip_rectangle.height = g_height; + XSetClipRectangles(g_display, g_gc, 0, 0, &g_clip_rectangle, 1, YXBanded); } void @@ -2110,6 +2453,9 @@ if (g_ownbackstore) XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y); + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, + (g_display, g_ownbackstore ? g_backstore : g_wnd, sw->wnd, g_gc, + x, y, cx, cy, x - sw->xoffset, y - sw->yoffset)); } void @@ -2120,23 +2466,19 @@ SET_FUNCTION(opcode); if (g_ownbackstore) { - 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); - } + XCopyArea(g_display, g_Unobscured ? g_wnd : 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); } + + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, + (g_display, g_ownbackstore ? g_backstore : g_wnd, + sw->wnd, g_gc, x, y, cx, cy, x - sw->xoffset, y - sw->yoffset)); + RESET_FUNCTION(opcode); } @@ -2147,6 +2489,9 @@ { SET_FUNCTION(opcode); XCopyArea(g_display, (Pixmap) src, g_wnd, g_gc, srcx, srcy, cx, cy, x, y); + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, + (g_display, (Pixmap) src, sw->wnd, g_gc, + srcx, srcy, cx, cy, x - sw->xoffset, y - sw->yoffset)); if (g_ownbackstore) XCopyArea(g_display, (Pixmap) src, g_backstore, g_gc, srcx, srcy, cx, cy, x, y); RESET_FUNCTION(opcode); @@ -2193,6 +2538,9 @@ SET_FUNCTION(opcode); SET_FOREGROUND(pen->colour); XDrawLine(g_display, g_wnd, g_gc, startx, starty, endx, endy); + ON_ALL_SEAMLESS_WINDOWS(XDrawLine, (g_display, sw->wnd, g_gc, + startx - sw->xoffset, starty - sw->yoffset, + endx - sw->xoffset, endy - sw->yoffset)); if (g_ownbackstore) XDrawLine(g_display, g_backstore, g_gc, startx, starty, endx, endy); RESET_FUNCTION(opcode); @@ -2290,6 +2638,10 @@ if (g_ownbackstore) XDrawLines(g_display, g_backstore, g_gc, (XPoint *) points, npoints, CoordModePrevious); + + ON_ALL_SEAMLESS_WINDOWS(seamless_XDrawLines, + (sw->wnd, (XPoint *) points, npoints, sw->xoffset, sw->yoffset)); + RESET_FUNCTION(opcode); } @@ -2510,11 +2862,25 @@ if (g_ownbackstore) { if (boxcx > 1) + { XCopyArea(g_display, g_backstore, g_wnd, g_gc, boxx, boxy, boxcx, boxcy, boxx, boxy); + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, + (g_display, g_backstore, sw->wnd, g_gc, + boxx, boxy, + boxcx, boxcy, + boxx - sw->xoffset, boxy - sw->yoffset)); + } else + { XCopyArea(g_display, g_backstore, g_wnd, g_gc, clipx, clipy, clipcx, clipcy, clipx, clipy); + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, + (g_display, g_backstore, sw->wnd, g_gc, + clipx, clipy, + clipcx, clipcy, clipx - sw->xoffset, + clipy - sw->yoffset)); + } } } @@ -2560,10 +2926,16 @@ { XPutImage(g_display, g_backstore, g_gc, image, 0, 0, x, y, cx, cy); XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y); + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, + (g_display, g_backstore, sw->wnd, g_gc, + x, y, cx, cy, x - sw->xoffset, y - sw->yoffset)); } else { XPutImage(g_display, g_wnd, g_gc, image, 0, 0, x, y, cx, cy); + ON_ALL_SEAMLESS_WINDOWS(XCopyArea, + (g_display, g_wnd, sw->wnd, g_gc, x, y, cx, cy, + x - sw->xoffset, y - sw->yoffset)); } XFree(image); @@ -2579,3 +2951,178 @@ ui_end_update(void) { } + +void +ui_seamless_toggle() +{ + if (g_seamless_rdp) + { + /* Deactivate */ + while (g_seamless_windows) + { + XDestroyWindow(g_display, g_seamless_windows->wnd); + seamless_remove_window(g_seamless_windows); + } + XMapWindow(g_display, g_wnd); + } + else + { + /* Activate */ + if (g_win_button_size) + { + error("SeamlessRDP mode cannot be activated when using single application mode\n"); + return; + } + if (!g_using_full_workarea) + { + error("SeamlessRDP mode requires a session that covers the whole screen"); + return; + } + + XUnmapWindow(g_display, g_wnd); + seamless_send_sync(); + } + + g_seamless_rdp = !g_seamless_rdp; +} + +void +ui_seamless_create_window(unsigned long id, unsigned long parent, unsigned long flags) +{ + Window wnd; + XSetWindowAttributes attribs; + XClassHint *classhints; + long input_mask; + seamless_window *sw; + + get_window_attribs(&attribs); + + attribs.override_redirect = False; + + /* FIXME: Do not assume that -1, -1 is outside screen Consider + wait with showing the window until STATE and others have + been recieved. */ + wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), -1, -1, 1, 1, 0, 0, + InputOutput, g_visual, + CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap | + CWBorderPixel, &attribs); + + XStoreName(g_display, wnd, "rdesktop-seamless"); + + mwm_hide_decorations(wnd); + + classhints = XAllocClassHint(); + if (classhints != NULL) + { + classhints->res_name = classhints->res_class = "rdesktop"; + XSetClassHint(g_display, wnd, classhints); + XFree(classhints); + } + + /* FIXME: Support for Input Context:s */ + + get_input_mask(&input_mask); + + XSelectInput(g_display, wnd, input_mask); + + XMapWindow(g_display, wnd); + + /* handle the WM_DELETE_WINDOW protocol. FIXME: When killing a + seamless window, we could try to close the window on the + serverside, instead of terminating rdesktop */ + XSetWMProtocols(g_display, wnd, &g_kill_atom, 1); + + sw = malloc(sizeof(seamless_window)); + sw->wnd = wnd; + sw->id = id; + sw->xoffset = 0; + sw->yoffset = 0; + sw->width = 0; + sw->height = 0; + sw->next = g_seamless_windows; + g_seamless_windows = sw; +} + + +void +ui_seamless_destroy_window(unsigned long id, unsigned long flags) +{ + seamless_window *sw; + sw = seamless_get_window_by_id(id); + + if (!sw) + { + warning("ui_seamless_destroy_window: No information for window 0x%lx\n", id); + return; + } + + XDestroyWindow(g_display, sw->wnd); + seamless_remove_window(sw); +} + + +void +ui_seamless_move_window(unsigned long id, int x, int y, int width, int height, unsigned long flags) +{ + seamless_window *sw; + + sw = seamless_get_window_by_id(id); + + if (!sw) + { + warning("ui_seamless_move_window: No information for window 0x%lx\n", id); + return; + } + + if (!width || !height) + /* X11 windows must be at least 1x1 */ + return; + + /* About MAX and MIN: Windows allows moving a window outside + the desktop. This happens, for example, when maximizing an + application. In this case, the position is set to something + like -4,-4,1288,1032. Many WMs does not allow windows + outside the desktop, however. Therefore, clip the window + ourselves. */ + sw->xoffset = MAX(0, x); + sw->yoffset = MAX(0, y); + sw->width = MIN(MIN(width, width + x), g_width - sw->xoffset); + sw->height = MIN(MIN(height, height + y), g_height - sw->yoffset); + + /* FIXME: Perhaps use ewmh_net_moveresize_window instead */ + XMoveResizeWindow(g_display, sw->wnd, sw->xoffset, sw->yoffset, sw->width, sw->height); +} + + +void +ui_seamless_settitle(unsigned long id, const char *title, unsigned long flags) +{ + seamless_window *sw; + + sw = seamless_get_window_by_id(id); + + XStoreName(g_display, sw->wnd, title); +} + + +void +ui_seamless_setstate(unsigned long id, unsigned int state, unsigned long flags) +{ + seamless_window *sw; + + sw = seamless_get_window_by_id(id); + + switch (state) + { + case SEAMLESSRDP_NORMAL: + case SEAMLESSRDP_MAXIMIZED: + /* FIXME */ + break; + case SEAMLESSRDP_MINIMIZED: + XIconifyWindow(g_display, sw->wnd, DefaultScreen(g_display)); + break; + default: + warning("SeamlessRDP: Invalid state %d\n", state); + break; + } +}