--- sourceforge.net/trunk/rdesktop/xwin.c 2002/09/24 06:09:09 188 +++ sourceforge.net/trunk/rdesktop/xwin.c 2003/05/19 21:36:33 376 @@ -1,7 +1,7 @@ /* rdesktop: A Remote Desktop Protocol client. User interface services - X Window System - Copyright (C) Matthew Chapman 1999-2001 + Copyright (C) Matthew Chapman 1999-2002 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,18 +22,20 @@ #include #include #include -#define XK_MISCELLANY -#include #include "rdesktop.h" -#include "scancodes.h" extern int width; extern int height; extern BOOL sendmotion; extern BOOL fullscreen; extern BOOL grab_keyboard; +extern BOOL hide_decorations; extern char title[]; +extern int server_bpp; +extern int win_button_size; BOOL enable_compose = False; +BOOL focused; +BOOL mouse_in_wnd; Display *display; static int x_socket; @@ -45,7 +47,9 @@ static int bpp; static XIM IM; static XIC IC; +static XModifierKeymap *mod_map; static Cursor current_cursor; +static Atom protocol_atom, kill_atom; /* endianness */ static BOOL host_be; @@ -55,6 +59,32 @@ static BOOL ownbackstore; static Pixmap backstore; +/* Moving in single app mode */ +static BOOL moving_wnd; +static int move_x_offset = 0; +static int move_y_offset = 0; + +/* MWM decorations */ +#define MWM_HINTS_DECORATIONS (1L << 1) +#define PROP_MOTIF_WM_HINTS_ELEMENTS 5 +typedef struct +{ + uint32 flags; + uint32 functions; + uint32 decorations; + sint32 inputMode; + uint32 status; +} +PropMotifWmHints; + +typedef struct +{ + uint32 red; + uint32 green; + uint32 blue; +} +PixelColour; + #define FILL_RECTANGLE(x,y,cx,cy)\ { \ XFillRectangle(display, wnd, gc, x, y, cx, cy); \ @@ -62,12 +92,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 ? translate_colour(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 */ @@ -92,14 +129,165 @@ #define RESET_FUNCTION(rop2) { if (rop2 != ROP2_COPY) XSetFunction(display, gc, GXcopy); } static void -translate8(uint8 * data, uint8 * out, uint8 * end) +mwm_hide_decorations(void) +{ + PropMotifWmHints motif_hints; + Atom hintsatom; + + /* setup the property */ + motif_hints.flags = MWM_HINTS_DECORATIONS; + motif_hints.decorations = 0; + + /* get the atom for the property */ + hintsatom = XInternAtom(display, "_MOTIF_WM_HINTS", False); + if (!hintsatom) + { + warning("Failed to get atom _MOTIF_WM_HINTS: probably your window manager does not support MWM hints\n"); + return; + } + + XChangeProperty(display, wnd, hintsatom, hintsatom, 32, PropModeReplace, + (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS); +} + +static PixelColour +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; + return rv; +} + +static PixelColour +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; + return rv; +} + +static PixelColour +split_colour24(uint32 colour) +{ + PixelColour rv; + rv.blue = (colour & 0xff0000) >> 16; + rv.green = (colour & 0xff00) >> 8; + rv.red = (colour & 0xff); + 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) +{ + return (pc.red << 16) | (pc.green << 8) | pc.blue; +} + +#define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); } +#define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); } +#define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \ + x = (x << 16) | (x >> 16); } + +static uint32 +translate_colour(uint32 colour) +{ + switch (server_bpp) + { + case 15: + switch (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; + } + break; + case 16: + switch (bpp) + { + case 16: + break; + case 24: + colour = make_colour24(split_colour16(colour)); + break; + case 32: + colour = make_colour32(split_colour16(colour)); + break; + } + break; + case 24: + switch (bpp) + { + case 16: + colour = make_colour16(split_colour24(colour)); + break; + case 24: + break; + case 32: + colour = make_colour32(split_colour24(colour)); + break; + } + break; + } + switch (bpp) + { + case 16: + if (host_be != xserver_be) + BSWAP16(colour); + break; + + case 24: + if (xserver_be) + BSWAP24(colour); + break; + + case 32: + if (host_be != xserver_be) + BSWAP32(colour); + break; + } + + return colour; +} + +static void +translate8to8(uint8 * data, uint8 * out, uint8 * end) { while (out < end) *(out++) = (uint8) colmap[*(data++)]; } static void -translate16(uint8 * data, uint16 * out, uint16 * end) +translate8to16(uint8 * data, uint16 * out, uint16 * end) { while (out < end) *(out++) = (uint16) colmap[*(data++)]; @@ -107,7 +295,7 @@ /* little endian - conversion happens when colourmap is built */ static void -translate24(uint8 * data, uint8 * out, uint8 * end) +translate8to24(uint8 * data, uint8 * out, uint8 * end) { uint32 value; @@ -121,80 +309,188 @@ } static void -translate32(uint8 * data, uint32 * out, uint32 * end) +translate8to32(uint8 * data, uint32 * out, uint32 * end) { while (out < end) *(out++) = colmap[*(data++)]; } -static uint8 * -translate_image(int width, int height, uint8 * data) +/* todo the remaining translate function might need some big endian check ?? */ + +static void +translate15to16(uint16 * data, uint16 * out, uint16 * end) { - int size = width * height * bpp / 8; - uint8 *out = xmalloc(size); - uint8 *end = out + size; + while (out < end) + *(out++) = (uint16) make_colour16(split_colour15(*(data++))); +} - switch (bpp) +static void +translate15to24(uint16 * data, uint8 * out, uint8 * end) +{ + uint32 value; + + while (out < end) { - case 8: - translate8(data, out, end); - break; + value = make_colour24(split_colour15(*(data++))); + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; + } +} - case 16: - translate16(data, (uint16 *) out, (uint16 *) end); - break; +static void +translate15to32(uint16 * data, uint32 * out, uint32 * end) +{ + while (out < end) + *(out++) = make_colour32(split_colour15(*(data++))); +} - case 24: - translate24(data, out, end); - break; +static void +translate16to16(uint16 * data, uint16 * out, uint16 * end) +{ + while (out < end) + *(out++) = (uint16) (*(data++)); +} - case 32: - translate32(data, (uint32 *) out, (uint32 *) end); - break; + +static void +translate16to24(uint16 * data, uint8 * out, uint8 * end) +{ + uint32 value; + + while (out < end) + { + value = make_colour24(split_colour16(*(data++))); + *(out++) = value; + *(out++) = value >> 8; + *(out++) = value >> 16; } +} - return out; +static void +translate16to32(uint16 * data, uint32 * out, uint32 * end) +{ + while (out < end) + *(out++) = make_colour32(split_colour16(*(data++))); } -#define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); } -#define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); } -#define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \ - x = (x << 16) | (x >> 16); } +static void +translate24to16(uint8 * data, uint16 * out, uint16 * end) +{ + uint32 pixel = 0; + while (out < end) + { + pixel = *(data++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + *(out++) = (uint16) make_colour16(split_colour24(pixel)); + } +} -static uint32 -translate_colour(uint32 colour) +static void +translate24to24(uint8 * data, uint8 * out, uint8 * end) { - switch (bpp) + while (out < end) { - case 16: - if (host_be != xserver_be) - BSWAP16(colour); - break; + *(out++) = (*(data++)); + } +} + +static void +translate24to32(uint8 * data, uint32 * out, uint32 * end) +{ + uint32 pixel = 0; + while (out < end) + { + pixel = *(data++); + pixel |= *(data++) << 8; + pixel |= *(data++) << 16; + *(out++) = pixel; + } +} +static uint8 * +translate_image(int width, int height, uint8 * data) +{ + int size = width * height * bpp / 8; + uint8 *out = (uint8*)xmalloc(size); + uint8 *end = out + size; + + switch (server_bpp) + { case 24: - if (xserver_be) - BSWAP24(colour); + switch (bpp) + { + case 32: + translate24to32(data, (uint32 *) out, (uint32 *) end); + break; + case 24: + translate24to24(data, out, end); + break; + case 16: + translate24to16(data, (uint16 *) out, (uint16 *) end); + break; + } break; - - case 32: - if (host_be != xserver_be) - BSWAP32(colour); + case 16: + switch (bpp) + { + case 32: + translate16to32((uint16 *) data, (uint32 *) out, + (uint32 *) end); + break; + case 24: + translate16to24((uint16 *) data, out, end); + break; + case 16: + translate16to16((uint16 *) data, (uint16 *) out, + (uint16 *) end); + break; + } + break; + case 15: + switch (bpp) + { + case 32: + translate15to32((uint16 *) data, (uint32 *) out, + (uint32 *) end); + break; + case 24: + translate15to24((uint16 *) data, out, end); + break; + case 16: + translate15to16((uint16 *) data, (uint16 *) out, + (uint16 *) end); + break; + } + break; + case 8: + switch (bpp) + { + case 8: + translate8to8(data, out, end); + break; + case 16: + translate8to16(data, (uint16 *) out, (uint16 *) end); + break; + case 24: + translate8to24(data, out, end); + break; + case 32: + translate8to32(data, (uint32 *) out, (uint32 *) end); + break; + } break; } - - return colour; + return out; } BOOL -get_key_state(int keysym) +get_key_state(unsigned int state, uint32 keysym) { - int keysymMask = 0, modifierpos, key; - Window wDummy1, wDummy2; - int iDummy3, iDummy4, iDummy5, iDummy6; - unsigned int current_state; + int modifierpos, key, keysymMask = 0; int offset; - XModifierKeymap *map = XGetModifierMapping(display); KeyCode keycode = XKeysymToKeycode(display, keysym); if (keycode == NoSymbol) @@ -202,25 +498,20 @@ for (modifierpos = 0; modifierpos < 8; modifierpos++) { - offset = map->max_keypermod * modifierpos; + offset = mod_map->max_keypermod * modifierpos; - for (key = 0; key < map->max_keypermod; key++) + for (key = 0; key < mod_map->max_keypermod; key++) { - if (map->modifiermap[offset + key] == keycode) - keysymMask = 1 << modifierpos; + if (mod_map->modifiermap[offset + key] == keycode) + keysymMask |= 1 << modifierpos; } } - XQueryPointer(display, DefaultRootWindow(display), &wDummy1, - &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, ¤t_state); - - XFreeModifiermap(map); - - return (current_state & keysymMask) ? True : False; + return (state & keysymMask) ? True : False; } BOOL -ui_init() +ui_init(void) { XPixmapFormatValues *pfm; uint16 test; @@ -229,7 +520,7 @@ display = XOpenDisplay(NULL); if (display == NULL) { - error("Failed to open display\n"); + error("Failed to open display: %s\n", XDisplayName(NULL)); return False; } @@ -260,7 +551,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) @@ -270,6 +567,24 @@ host_be = !(BOOL) (*(uint8 *) (&test)); xserver_be = (ImageByteOrder(display) == MSBFirst); + if ((width == 0) || (height == 0)) + { + /* Fetch geometry from _NET_WORKAREA */ + uint32 x, y, cx, cy; + + if (get_current_workarea(&x, &y, &cx, &cy) == 0) + { + width = cx; + height = cy; + } + else + { + warning("Failed to get workarea: probably your window manager does not support extended hints\n"); + width = 800; + height = 600; + } + } + if (fullscreen) { width = WidthOfScreen(screen); @@ -281,26 +596,35 @@ if (ownbackstore) { - backstore = XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth); + backstore = + XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth); /* clear to prevent rubbish being exposed at startup */ XSetForeground(display, gc, BlackPixelOfScreen(screen)); XFillRectangle(display, backstore, gc, 0, 0, width, height); } + mod_map = XGetModifierMapping(display); + if (enable_compose) IM = XOpenIM(display, NULL, NULL, NULL); xkeymap_init(); + + /* todo take this out when high colour is done */ + printf("server bpp %d client bpp %d depth %d\n", server_bpp, bpp, depth); + return True; } void -ui_deinit() +ui_deinit(void) { if (IM != NULL) XCloseIM(IM); + XFreeModifiermap(mod_map); + if (ownbackstore) XFreePixmap(display, backstore); @@ -310,7 +634,7 @@ } BOOL -ui_create_window() +ui_create_window(void) { XSetWindowAttributes attribs; XClassHint *classhints; @@ -319,7 +643,7 @@ long input_mask, ic_input_mask; XEvent xevent; - wndwidth = fullscreen ? WidthOfScreen(screen) : width; + wndwidth = fullscreen ? WidthOfScreen(screen) : width; wndheight = fullscreen ? HeightOfScreen(screen) : height; attribs.background_pixel = BlackPixelOfScreen(screen); @@ -332,6 +656,9 @@ XStoreName(display, wnd, title); + if (hide_decorations) + mwm_hide_decorations(); + classhints = XAllocClassHint(); if (classhints != NULL) { @@ -351,40 +678,50 @@ } input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | - StructureNotifyMask | FocusChangeMask; + VisibilityChangeMask | FocusChangeMask; - if (grab_keyboard) - input_mask |= EnterWindowMask | LeaveWindowMask; if (sendmotion) input_mask |= PointerMotionMask; if (ownbackstore) input_mask |= ExposureMask; + if (fullscreen || grab_keyboard) + input_mask |= EnterWindowMask; + if (grab_keyboard) + input_mask |= LeaveWindowMask; if (IM != NULL) { IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing), XNClientWindow, wnd, XNFocusWindow, wnd, NULL); - if ((IC != NULL) && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL)) + if ((IC != NULL) + && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL)) input_mask |= ic_input_mask; } XSelectInput(display, wnd, input_mask); XMapWindow(display, wnd); - /* wait for MapNotify */ - do { - XMaskEvent(display, StructureNotifyMask, &xevent); - } while (xevent.type != MapNotify); + /* wait for VisibilityNotify */ + do + { + XMaskEvent(display, VisibilityChangeMask, &xevent); + } + while (xevent.type != VisibilityNotify); - if (fullscreen) - XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime); + 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; } void -ui_destroy_window() +ui_destroy_window(void) { if (IC != NULL) XDestroyIC(IC); @@ -393,7 +730,7 @@ } void -xwin_toggle_fullscreen() +xwin_toggle_fullscreen(void) { Pixmap contents = 0; @@ -417,18 +754,21 @@ } } -/* Process all events in Xlib queue */ -static void -xwin_process_events() +/* Process all events in Xlib queue + Returns 0 after user quit, 1 otherwise */ +static int +xwin_process_events(void) { XEvent xevent; KeySym keysym; uint16 button, flags; uint32 ev_time; key_translation tr; - char *ksname = NULL; char str[256]; Status status; + unsigned int state; + Window wdummy; + int dummy; while (XPending(display) > 0) { @@ -440,11 +780,18 @@ continue; } - ev_time = time(NULL); flags = 0; switch (xevent.type) { + case ClientMessage: + /* the window manager told us to quit */ + if ((xevent.xclient.message_type == protocol_atom) + && ((Atom)xevent.xclient.data.l[0] == kill_atom)) + /* Quit */ + return 0; + break; + case KeyPress: if (IC != NULL) /* Multi_key compatible version */ @@ -467,10 +814,11 @@ str, sizeof(str), &keysym, NULL); } - ksname = get_ksname(keysym); - DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, ksname)); + DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, + get_ksname(keysym))); - if (handle_special_keys(keysym, ev_time, True)) + ev_time = time(NULL); + if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True)) break; tr = xkeymap_translate_key(keysym, @@ -483,15 +831,16 @@ rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); break; + case KeyRelease: XLookupString((XKeyEvent *) & xevent, str, sizeof(str), &keysym, NULL); - ksname = get_ksname(keysym); DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym, - ksname)); + get_ksname(keysym))); - if (handle_special_keys(keysym, ev_time, False)) + ev_time = time(NULL); + if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False)) break; tr = xkeymap_translate_key(keysym, @@ -512,28 +861,114 @@ if (button == 0) break; - rdp_send_input(ev_time, RDP_INPUT_MOUSE, + /* If win_button_size is nonzero, enable single app mode */ + if (xevent.xbutton.y < win_button_size) + { + /* Stop moving window when button is released, regardless of cursor position */ + if (moving_wnd && (xevent.type == ButtonRelease)) + moving_wnd = False; + + /* Check from right to left: */ + + if (xevent.xbutton.x >= width - win_button_size) + { + /* The close button, continue */ + ; + } + else if (xevent.xbutton.x >= width - win_button_size * 2) + { + /* The maximize/restore button. Do not send to + server. It might be a good idea to change the + cursor or give some other visible indication + that rdesktop inhibited this click */ + break; + } + else if (xevent.xbutton.x >= width - win_button_size * 3) + { + /* The minimize button. Iconify window. */ + XIconifyWindow(display, wnd, + DefaultScreen(display)); + break; + } + else if (xevent.xbutton.x <= win_button_size) + { + /* The system menu. Ignore. */ + break; + } + else + { + /* The title bar. */ + if ((xevent.type == ButtonPress) && !fullscreen + && hide_decorations) + { + moving_wnd = True; + move_x_offset = xevent.xbutton.x; + move_y_offset = xevent.xbutton.y; + } + break; + + } + } + + rdp_send_input(time(NULL), RDP_INPUT_MOUSE, flags | button, xevent.xbutton.x, xevent.xbutton.y); break; case MotionNotify: - rdp_send_input(ev_time, RDP_INPUT_MOUSE, + if (moving_wnd) + { + XMoveWindow(display, wnd, + xevent.xmotion.x_root - move_x_offset, + xevent.xmotion.y_root - move_y_offset); + break; + } + + 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; - case EnterNotify: - if (grab_keyboard) + case FocusIn: + if (xevent.xfocus.mode == NotifyGrab) + break; + focused = True; + XQueryPointer(display, wnd, &wdummy, &wdummy, &dummy, &dummy, + &dummy, &dummy, &state); + reset_modifier_keys(state); + if (grab_keyboard && mouse_in_wnd) XGrabKeyboard(display, wnd, True, GrabModeAsync, GrabModeAsync, CurrentTime); break; - case LeaveNotify: - if (grab_keyboard) + case FocusOut: + if (xevent.xfocus.mode == NotifyUngrab) + break; + focused = False; + if (xevent.xfocus.mode == NotifyWhileGrabbed) XUngrabKeyboard(display, CurrentTime); break; - case FocusIn: - reset_modifier_keys(); + case EnterNotify: + /* we only register for this event when in fullscreen mode */ + /* or grab_keyboard */ + mouse_in_wnd = True; + if (fullscreen) + { + XSetInputFocus(display, wnd, RevertToPointerRoot, + CurrentTime); + break; + } + if (focused) + XGrabKeyboard(display, wnd, True, + GrabModeAsync, GrabModeAsync, CurrentTime); + break; + + case LeaveNotify: + /* we only register for this event when grab_keyboard */ + mouse_in_wnd = False; + XUngrabKeyboard(display, CurrentTime); break; case Expose: @@ -550,13 +985,22 @@ if (xevent.xmapping.request == MappingKeyboard || xevent.xmapping.request == MappingModifier) XRefreshKeyboardMapping(&xevent.xmapping); + + if (xevent.xmapping.request == MappingModifier) + { + XFreeModifiermap(mod_map); + mod_map = XGetModifierMapping(display); + } break; } } + /* 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; @@ -567,7 +1011,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); @@ -583,7 +1029,7 @@ } if (FD_ISSET(rdp_socket, &rfds)) - return; + return 1; } } @@ -600,15 +1046,16 @@ 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); + (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0); XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height); XFree(image); - xfree(tdata); + if (!owncolmap) + xfree(tdata); return (HBITMAP) bitmap; } @@ -617,10 +1064,9 @@ { XImage *image; uint8 *tdata; - - tdata = translate_image(width, height, data); + tdata = (owncolmap ? data : translate_image(width, height, data)); image = XCreateImage(display, visual, depth, ZPixmap, 0, - (char *) tdata, width, height, 8, 0); + (char *) tdata, width, height, server_bpp == 8 ? 8 : bpp, 0); if (ownbackstore) { @@ -633,7 +1079,8 @@ } XFree(image); - xfree(tdata); + if (!owncolmap) + xfree(tdata); } void @@ -690,10 +1137,10 @@ scanline = (width + 7) / 8; offset = scanline * height; - cursor = xmalloc(offset); + cursor = (uint8*)xmalloc(offset); memset(cursor, 0, offset); - mask = xmalloc(offset); + mask = (uint8*)xmalloc(offset); memset(mask, 0, offset); /* approximate AND and XOR masks with a monochrome X pointer */ @@ -764,82 +1211,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 = (uint32*)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; - - /* only get the colors once */ - while( colLookup-- ){ - 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; + entry = &colours->colours[i]; + MAKE_XCOLOR(&xentry, entry); + + if (XAllocColor(display, xcolmap, &xentry) == 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 ){ - 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)); + /* only get the colors once */ + while (colLookup--) + { + 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 ){ - nMinDist = nDist; - xentry.pixel = j; + colLookup = 0; + + /* approximate the pixel */ + while (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; + 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; + /* 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); + /* byte swap here to make translate_image faster */ + map[i] = translate_colour(colour); + } + return map; } + else + { + XColor *xcolours, *xentry; + Colormap map; - return map; + xcolours = (XColor*)xmalloc(sizeof(XColor) * ncolours); + for (i = 0; i < ncolours; i++) + { + 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); + + xfree(xcolours); + return (HCOLOURMAP) 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 = (uint32*)map; + else + XSetWindowColormap(display, wnd, (Colormap) map); } void @@ -855,7 +1345,7 @@ } void -ui_reset_clip() +ui_reset_clip(void) { XRectangle rect; @@ -867,7 +1357,7 @@ } void -ui_bell() +ui_bell(void) { XBell(display, 0); } @@ -881,6 +1371,15 @@ RESET_FUNCTION(opcode); } +static uint8 hatch_patterns[] = { + 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, /* 0 - bsHorizontal */ + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, /* 1 - bsVertical */ + 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, /* 2 - bsFDiagonal */ + 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, /* 3 - bsBDiagonal */ + 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08, /* 4 - bsCross */ + 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 /* 5 - bsDiagCross */ +}; + void ui_patblt(uint8 opcode, /* dest */ int x, int y, int cx, int cy, @@ -898,6 +1397,19 @@ FILL_RECTANGLE(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); + XSetFillStyle(display, gc, FillOpaqueStippled); + XSetStipple(display, gc, fill); + XSetTSOrigin(display, gc, brush->xorigin, brush->yorigin); + FILL_RECTANGLE(x, y, cx, cy); + XSetFillStyle(display, gc, FillSolid); + XSetTSOrigin(display, gc, 0, 0); + ui_destroy_glyph((HGLYPH) fill); + break; + case 3: /* Pattern */ for (i = 0; i != 8; i++) ipattern[7 - i] = brush->pattern[i]; @@ -1002,6 +1514,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, @@ -1016,7 +1529,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); } @@ -1045,8 +1558,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)\ @@ -1068,11 +1581,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 */ @@ -1086,7 +1599,7 @@ else { error("this shouldn't be happening\n"); - break; + exit(1); } /* this will move pointer from start to first character after FF command */ length -= i + 3; @@ -1106,17 +1619,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: @@ -1125,8 +1638,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