--- sourceforge.net/trunk/rdesktop/xwin.c 2002/11/19 14:45:13 274 +++ sourceforge.net/trunk/rdesktop/xwin.c 2003/02/04 05:32:13 309 @@ -31,6 +31,7 @@ extern BOOL grab_keyboard; extern BOOL hide_decorations; extern char title[]; +extern int server_bpp; BOOL enable_compose = False; BOOL focused; BOOL mouse_in_wnd; @@ -47,6 +48,7 @@ static XIC IC; static XModifierKeymap *mod_map; static Cursor current_cursor; +static Atom protocol_atom, kill_atom; /* endianness */ static BOOL host_be; @@ -61,11 +63,11 @@ #define PROP_MOTIF_WM_HINTS_ELEMENTS 5 typedef struct { - unsigned long flags; - unsigned long functions; - unsigned long decorations; - long inputMode; - unsigned long status; + uint32 flags; + uint32 functions; + uint32 decorations; + sint32 inputMode; + uint32 status; } PropMotifWmHints; @@ -77,12 +79,19 @@ XFillRectangle(display, backstore, gc, x, y, cx, cy); \ } +#define FILL_RECTANGLE_BACKSTORE(x,y,cx,cy)\ +{ \ + XFillRectangle(display, ownbackstore ? backstore : wnd, gc, x, y, cx, cy); \ +} + /* colour maps */ +BOOL owncolmap = False; static Colormap xcolmap; static uint32 *colmap; -#define SET_FOREGROUND(col) XSetForeground(display, gc, translate_colour(colmap[col])); -#define SET_BACKGROUND(col) XSetBackground(display, gc, translate_colour(colmap[col])); +#define TRANSLATE(col) ( server_bpp != 8 ? col : owncolmap ? col : translate_colour(colmap[col]) ) +#define SET_FOREGROUND(col) XSetForeground(display, gc, TRANSLATE(col)); +#define SET_BACKGROUND(col) XSetBackground(display, gc, TRANSLATE(col)); static int rop2_map[] = { GXclear, /* 0 */ @@ -120,7 +129,7 @@ hintsatom = XInternAtom(display, "_MOTIF_WM_HINTS", False); if (!hintsatom) { - error("Failed to get atom _MOTIF_WM_HINTS\n"); + warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n"); return; } @@ -288,7 +297,13 @@ return False; } - xcolmap = DefaultColormapOfScreen(screen); + if (owncolmap != True) + { + xcolmap = DefaultColormapOfScreen(screen); + if (depth <= 8) + warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n"); + } + gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL); if (DoesBackingStore(screen) != Always) @@ -301,13 +316,16 @@ if ((width == 0) || (height == 0)) { /* Fetch geometry from _NET_WORKAREA */ - uint32 xpos, ypos; + uint32 x, y, cx, cy; - if (get_current_workarea(&xpos, &ypos, &width, &height) < 0) + if (get_current_workarea(&x, &y, &cx, &cy) == 0) + { + width = cx; + height = cy; + } + else { - error("Failed to get workarea.\n"); - error("Perhaps your window manager does not support EWMH?\n"); - error("Defaulting to geometry 800x600\n"); + warning("Failed to get workarea: probably your window manager does not support extended hints\n"); width = 800; height = 600; } @@ -436,6 +454,11 @@ focused = False; mouse_in_wnd = False; + /* handle the WM_DELETE_WINDOW protocol */ + protocol_atom = XInternAtom(display, "WM_PROTOCOLS", True); + kill_atom = XInternAtom(display, "WM_DELETE_WINDOW", True); + XSetWMProtocols(display, wnd, &kill_atom, 1); + return True; } @@ -473,8 +496,9 @@ } } -/* Process all events in Xlib queue */ -static void +/* Process all events in Xlib queue + Returns 0 after user quit, 1 otherwise */ +static int xwin_process_events(void) { XEvent xevent; @@ -502,6 +526,14 @@ switch (xevent.type) { + case ClientMessage: + /* the window manager told us to quit */ + if ((xevent.xclient.message_type == protocol_atom) + && (xevent.xclient.data.l[0] == kill_atom)) + /* Quit */ + return 0; + break; + case KeyPress: if (IC != NULL) /* Multi_key compatible version */ @@ -576,6 +608,9 @@ break; case MotionNotify: + if (fullscreen && !focused) + XSetInputFocus(display, wnd, RevertToPointerRoot, + CurrentTime); rdp_send_input(time(NULL), RDP_INPUT_MOUSE, MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y); break; @@ -645,9 +680,12 @@ } } + /* Keep going */ + return 1; } -void +/* Returns 0 after user quit, 1 otherwise */ +int ui_select(int rdp_socket) { int n = (rdp_socket > x_socket) ? rdp_socket + 1 : x_socket + 1; @@ -658,7 +696,9 @@ while (True) { /* Process any events already waiting */ - xwin_process_events(); + if (!xwin_process_events()) + /* User quit */ + return 0; FD_ZERO(&rfds); FD_SET(rdp_socket, &rfds); @@ -674,7 +714,7 @@ } if (FD_ISSET(rdp_socket, &rfds)) - return; + return 1; } } @@ -691,7 +731,7 @@ Pixmap bitmap; uint8 *tdata; - tdata = translate_image(width, height, data); + tdata = (owncolmap ? data : translate_image(width, height, data)); bitmap = XCreatePixmap(display, wnd, width, height, depth); image = XCreateImage(display, visual, depth, ZPixmap, 0, (char *) tdata, width, height, 8, 0); @@ -699,7 +739,8 @@ XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height); XFree(image); - xfree(tdata); + if (!owncolmap) + xfree(tdata); return (HBITMAP) bitmap; } @@ -709,7 +750,25 @@ XImage *image; uint8 *tdata; - tdata = translate_image(width, height, data); + if (server_bpp == 16) + { + image = XCreateImage(display, visual, depth, ZPixmap, 0, + (char *) data, width, height, 16, 0); + + if (ownbackstore) + { + XPutImage(display, backstore, gc, image, 0, 0, x, y, cx, cy); + XCopyArea(display, backstore, wnd, gc, x, y, cx, cy, x, y); + } + else + { + XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy); + } + + XFree(image); + return; + } + tdata = (owncolmap ? data : translate_image(width, height, data)); image = XCreateImage(display, visual, depth, ZPixmap, 0, (char *) tdata, width, height, 8, 0); @@ -724,7 +783,8 @@ } XFree(image); - xfree(tdata); + if (!owncolmap) + xfree(tdata); } void @@ -855,95 +915,125 @@ (xc)->blue = ((c)->blue << 8) | (c)->blue; \ (xc)->flags = DoRed | DoGreen | DoBlue; + HCOLOURMAP ui_create_colourmap(COLOURMAP * colours) { COLOURENTRY *entry; int i, ncolours = colours->ncolours; - uint32 *map = xmalloc(sizeof(*colmap) * ncolours); - XColor xentry; - XColor xc_cache[256]; - uint32 colour; - int colLookup = 256; - for (i = 0; i < ncolours; i++) + if (!owncolmap) { - entry = &colours->colours[i]; - MAKE_XCOLOR(&xentry, entry); - - if (XAllocColor(display, xcolmap, &xentry) == 0) + uint32 *map = xmalloc(sizeof(*colmap) * ncolours); + XColor xentry; + XColor xc_cache[256]; + uint32 colour; + int colLookup = 256; + for (i = 0; i < ncolours; i++) { - /* Allocation failed, find closest match. */ - int j = 256; - int nMinDist = 3 * 256 * 256; - long nDist = nMinDist; + entry = &colours->colours[i]; + MAKE_XCOLOR(&xentry, entry); - /* only get the colors once */ - while (colLookup--) + if (XAllocColor(display, xcolmap, &xentry) == 0) { - xc_cache[colLookup].pixel = colLookup; - xc_cache[colLookup].red = xc_cache[colLookup].green = - xc_cache[colLookup].blue = 0; - xc_cache[colLookup].flags = 0; - XQueryColor(display, - DefaultColormap(display, DefaultScreen(display)), - &xc_cache[colLookup]); - } - colLookup = 0; + /* Allocation failed, find closest match. */ + int j = 256; + int nMinDist = 3 * 256 * 256; + long nDist = nMinDist; - /* approximate the pixel */ - while (j--) - { - if (xc_cache[j].flags) + /* only get the colors once */ + while (colLookup--) { - nDist = ((long) (xc_cache[j].red >> 8) - - (long) (xentry.red >> 8)) * - ((long) (xc_cache[j].red >> 8) - - (long) (xentry.red >> 8)) + - ((long) (xc_cache[j].green >> 8) - - (long) (xentry.green >> 8)) * - ((long) (xc_cache[j].green >> 8) - - (long) (xentry.green >> 8)) + - ((long) (xc_cache[j].blue >> 8) - - (long) (xentry.blue >> 8)) * - ((long) (xc_cache[j].blue >> 8) - - (long) (xentry.blue >> 8)); + xc_cache[colLookup].pixel = colLookup; + xc_cache[colLookup].red = xc_cache[colLookup].green = + xc_cache[colLookup].blue = 0; + xc_cache[colLookup].flags = 0; + XQueryColor(display, + DefaultColormap(display, + DefaultScreen(display)), + &xc_cache[colLookup]); } - if (nDist < nMinDist) + colLookup = 0; + + /* approximate the pixel */ + while (j--) { - nMinDist = nDist; - xentry.pixel = j; + if (xc_cache[j].flags) + { + nDist = ((long) (xc_cache[j].red >> 8) - + (long) (xentry.red >> 8)) * + ((long) (xc_cache[j].red >> 8) - + (long) (xentry.red >> 8)) + + ((long) (xc_cache[j].green >> 8) - + (long) (xentry.green >> 8)) * + ((long) (xc_cache[j].green >> 8) - + (long) (xentry.green >> 8)) + + ((long) (xc_cache[j].blue >> 8) - + (long) (xentry.blue >> 8)) * + ((long) (xc_cache[j].blue >> 8) - + (long) (xentry.blue >> 8)); + } + if (nDist < nMinDist) + { + nMinDist = nDist; + xentry.pixel = j; + } } } + colour = xentry.pixel; + + /* update our cache */ + if (xentry.pixel < 256) + { + xc_cache[xentry.pixel].red = xentry.red; + xc_cache[xentry.pixel].green = xentry.green; + xc_cache[xentry.pixel].blue = xentry.blue; + + } + + + /* byte swap here to make translate_image faster */ + map[i] = translate_colour(colour); } - colour = xentry.pixel; + return map; + } + else + { + XColor *xcolours, *xentry; + Colormap map; - /* update our cache */ - if (xentry.pixel < 256) + xcolours = xmalloc(sizeof(XColor) * ncolours); + for (i = 0; i < ncolours; i++) { - xc_cache[xentry.pixel].red = xentry.red; - xc_cache[xentry.pixel].green = xentry.green; - xc_cache[xentry.pixel].blue = xentry.blue; - + entry = &colours->colours[i]; + xentry = &xcolours[i]; + xentry->pixel = i; + MAKE_XCOLOR(xentry, entry); } + map = XCreateColormap(display, wnd, visual, AllocAll); + XStoreColors(display, map, xcolours, ncolours); - /* byte swap here to make translate_image faster */ - map[i] = translate_colour(colour); + xfree(xcolours); + return (HCOLOURMAP) map; } - - return map; } void ui_destroy_colourmap(HCOLOURMAP map) { - xfree(map); + if (!owncolmap) + xfree(map); + else + XFreeColormap(display, (Colormap) map); } void ui_set_colourmap(HCOLOURMAP map) { - colmap = map; + if (!owncolmap) + colmap = map; + else + XSetWindowColormap(display, wnd, (Colormap) map); } void @@ -1106,6 +1196,7 @@ FILL_RECTANGLE(x, y, cx, cy); } +/* warning, this function only draws on wnd or backstore, not both */ void ui_draw_glyph(int mixmode, /* dest */ int x, int y, int cx, int cy, @@ -1120,7 +1211,7 @@ XSetStipple(display, gc, (Pixmap) glyph); XSetTSOrigin(display, gc, x, y); - FILL_RECTANGLE(x, y, cx, cy); + FILL_RECTANGLE_BACKSTORE(x, y, cx, cy); XSetFillStyle(display, gc, FillSolid); } @@ -1149,8 +1240,8 @@ }\ if (glyph != NULL)\ {\ - ui_draw_glyph (mixmode, x + (short) glyph->offset,\ - y + (short) glyph->baseline,\ + 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)\ @@ -1172,11 +1263,11 @@ if (boxcx > 1) { - FILL_RECTANGLE(boxx, boxy, boxcx, boxcy); + FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy); } else if (mixmode == MIX_OPAQUE) { - FILL_RECTANGLE(clipx, clipy, clipcx, clipcy); + FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy); } /* Paint text, character by character */ @@ -1210,17 +1301,17 @@ else x += text[i + 2]; } - if (i + 2 < length) - i += 3; - else - i += 2; - length -= i; - /* this will move pointer from start to first character after FE command */ - text = &(text[i]); - i = 0; for (j = 0; j < entry->size; j++) DO_GLYPH(((uint8 *) (entry->data)), j); } + if (i + 2 < length) + i += 3; + else + i += 2; + length -= i; + /* this will move pointer from start to first character after FE command */ + text = &(text[i]); + i = 0; break; default: @@ -1229,8 +1320,15 @@ break; } } - - + if (ownbackstore) + { + if (boxcx > 1) + XCopyArea(display, backstore, wnd, gc, boxx, + boxy, boxcx, boxcy, boxx, boxy); + else + XCopyArea(display, backstore, wnd, gc, clipx, + clipy, clipcx, clipcy, clipx, clipy); + } } void