--- sourceforge.net/trunk/rdesktop/xwin.c 2002/08/03 22:28:05 87 +++ sourceforge.net/trunk/rdesktop/xwin.c 2002/09/14 11:48:44 121 @@ -32,14 +32,18 @@ extern BOOL sendmotion; extern BOOL fullscreen; extern BOOL grab_keyboard; +extern char title[]; -Display *display = NULL; +Display *display; static int x_socket; +static Screen *screen; static Window wnd; static GC gc; static Visual *visual; static int depth; static int bpp; +static int dpy_width; +static int dpy_height; /* endianness */ static BOOL host_be; @@ -59,13 +63,15 @@ /* colour maps */ static BOOL owncolmap; static Colormap xcolmap; -static uint32 white; static uint32 *colmap; -static XIM IM = NULL; -static XIC IC = NULL; /* Compose support */ BOOL enable_compose = False; +static XIM IM = NULL; +static XIC IC = NULL; + +/* toggle fullscreen globals */ +static unsigned long input_mask; #define TRANSLATE(col) ( owncolmap ? col : translate_colour(colmap[col]) ) #define SET_FOREGROUND(col) XSetForeground(display, gc, TRANSLATE(col)); @@ -241,36 +247,53 @@ } BOOL -ui_init() +get_key_state(int keysym) { - Screen *screen; - display = XOpenDisplay(NULL); - if (display == NULL) - { - error("Failed to open display\n"); + int keysymMask = 0, modifierpos, key; + Window wDummy1, wDummy2; + int iDummy3, iDummy4, iDummy5, iDummy6; + unsigned int current_state; + int offset; + + XModifierKeymap *map = XGetModifierMapping(display); + KeyCode keycode = XKeysymToKeycode(display, keysym); + + if (keycode == NoSymbol) return False; - } - if (fullscreen) + + for (modifierpos = 0; modifierpos < 8; modifierpos++) { - screen = DefaultScreenOfDisplay(display); - width = WidthOfScreen(screen); - height = HeightOfScreen(screen); + offset = map->max_keypermod * modifierpos; + + for (key = 0; key < map->max_keypermod; key++) + { + if (map->modifiermap[offset + key] == keycode) + keysymMask = 1 << modifierpos; + } } - return True; + + XQueryPointer(display, DefaultRootWindow(display), &wDummy1, + &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, ¤t_state); + + XFreeModifiermap(map); + + return (current_state & keysymMask) ? True : False; } + BOOL -ui_create_window(char *title) +ui_init() { - XSetWindowAttributes attribs; - XClassHint *classhints; - XSizeHints *sizehints; - unsigned long input_mask; XPixmapFormatValues *pfm; - Screen *screen; uint16 test; int i; - XEvent xevent; + + display = XOpenDisplay(NULL); + if (display == NULL) + { + error("Failed to open display\n"); + return False; + } x_socket = ConnectionNumber(display); screen = DefaultScreenOfDisplay(display); @@ -304,34 +327,37 @@ else xcolmap = DefaultColormapOfScreen(screen); + if (DoesBackingStore(screen) == NotUseful) + ownbackstore = True; + test = 1; host_be = !(BOOL) (*(uint8 *) (&test)); xserver_be = (ImageByteOrder(display) == MSBFirst); - white = WhitePixelOfScreen(screen); - attribs.background_pixel = BlackPixelOfScreen(screen); - attribs.backing_store = DoesBackingStore(screen); - - if (attribs.backing_store == NotUseful) - ownbackstore = True; - if (fullscreen) { - attribs.override_redirect = True; width = WidthOfScreen(screen); height = HeightOfScreen(screen); } - else - { - attribs.override_redirect = False; - } - width = (width + 3) & ~3; /* make width a multiple of 32 bits */ + xkeymap_init(); + return True; +} - wnd = XCreateWindow(display, RootWindowOfScreen(screen), - 0, 0, width, height, 0, CopyFromParent, - InputOutput, CopyFromParent, - CWBackingStore | CWBackPixel | CWOverrideRedirect, &attribs); +BOOL +ui_create_window() +{ + XSetWindowAttributes attribs; + XClassHint *classhints; + XSizeHints *sizehints; + XEvent xevent; + + attribs.background_pixel = BlackPixelOfScreen(screen); + attribs.backing_store = ownbackstore ? NotUseful : Always; + attribs.override_redirect = fullscreen; + wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height, + 0, CopyFromParent, InputOutput, CopyFromParent, + CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs); XStoreName(display, wnd, title); @@ -353,18 +379,15 @@ XFree(sizehints); } - xkeymap_init2(); - input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | - VisibilityChangeMask; + VisibilityChangeMask | FocusChangeMask; + if (grab_keyboard) input_mask |= EnterWindowMask | LeaveWindowMask; if (sendmotion) input_mask |= PointerMotionMask; - if (ownbackstore) input_mask |= ExposureMask; - if (enable_compose) input_mask |= init_inputmethod(); @@ -372,22 +395,25 @@ gc = XCreateGC(display, wnd, 0, NULL); - if (ownbackstore) - backstore = XCreatePixmap(display, wnd, width, height, depth); - XMapWindow(display, wnd); /* Wait for VisibilityNotify Event */ - for (;;) { + for (;;) + { XNextEvent(display, &xevent); if (xevent.type == VisibilityNotify) break; } + if (ownbackstore) + backstore = XCreatePixmap(display, wnd, width, height, depth); + /* clear the window so that cached data is not viewed upon start... */ XSetBackground(display, gc, 0); XSetForeground(display, gc, 0); FILL_RECTANGLE(0, 0, width, height); + /* make sure the window is focused */ + XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime); return True; } @@ -407,6 +433,48 @@ display = NULL; } +void +reset_keys() +{ + /* reset keys */ + uint32 ev_time; + ev_time = time(NULL); + rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); + rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); + rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); + rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); + rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); + rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); +} + +void +toggle_fullscreen() +{ + /* save window contents */ + Pixmap pixmap; + pixmap = XCreatePixmap(display, wnd, width, height, depth); + if (ownbackstore) + XCopyArea(display, backstore, pixmap, gc, 0, 0, width, height, 0, 0); + else + XCopyArea(display, wnd, pixmap, gc, 0, 0, width, height, 0, 0); + fullscreen = fullscreen ? False : True; + close_inputmethod(); + if (ownbackstore) + XFreePixmap(display, backstore); + XFreeGC(display, gc); + XDestroyWindow(display, wnd); + ui_create_window(); + ui_set_cursor(cache_get_cursor(0)); + ui_move_pointer(width / 2, height / 2); + reset_keys(); + /* restore window contents */ + if (ownbackstore) + XCopyArea(display, pixmap, backstore, gc, 0, 0, width, height, 0, 0); + XCopyArea(display, pixmap, wnd, gc, 0, 0, width, height, 0, 0); + XFreePixmap(display, pixmap); +} + +/* Process all events in Xlib queue */ static void xwin_process_events() { @@ -420,15 +488,6 @@ char str[256]; Status status; - /* Refresh keyboard mapping if it has changed. This is important for - Xvnc, since it allocates keycodes dynamically */ - if (XCheckTypedEvent(display, MappingNotify, &xevent)) - { - if (xevent.xmapping.request == MappingKeyboard - || xevent.xmapping.request == MappingModifier) - XRefreshKeyboardMapping(&xevent.xmapping); - } - while (XCheckMaskEvent(display, ~0, &xevent)) { if (enable_compose && (XFilterEvent(&xevent, None) == True)) @@ -467,20 +526,17 @@ ksname = get_ksname(keysym); DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname)); - if (inhibit_key(keysym)) - { - DEBUG_KBD(("Inhibiting key\n")); + if (handle_special_keys(keysym, ev_time, True)) break; - } tr = xkeymap_translate_key(keysym, xevent.xkey.keycode, xevent.xkey.state); - ensure_remote_modifiers(ev_time, tr); - if (tr.scancode == 0) break; + ensure_remote_modifiers(ev_time, tr); + rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); break; case KeyRelease: @@ -491,7 +547,7 @@ DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym, ksname)); - if (inhibit_key(keysym)) + if (handle_special_keys(keysym, ev_time, False)) break; tr = xkeymap_translate_key(keysym, @@ -530,11 +586,7 @@ break; case FocusOut: - /* reset keys */ - rdp_send_input(ev_time, RDP_INPUT_SCANCODE, - KBD_FLAG_DOWN | KBD_FLAG_UP, SCANCODE_CHAR_LCTRL, 0); - rdp_send_input(ev_time, RDP_INPUT_SCANCODE, - KBD_FLAG_DOWN | KBD_FLAG_UP, SCANCODE_CHAR_LALT, 0); + reset_keys(); /* fall through */ case LeaveNotify: if (grab_keyboard) @@ -548,6 +600,15 @@ xevent.xexpose.height, xevent.xexpose.x, xevent.xexpose.y); break; + + case MappingNotify: + /* Refresh keyboard mapping if it has changed. This is important for + Xvnc, since it allocates keycodes dynamically */ + if (xevent.xmapping.request == MappingKeyboard + || xevent.xmapping.request == MappingModifier) + XRefreshKeyboardMapping(&xevent.xmapping); + break; + } } } @@ -562,13 +623,13 @@ while (True) { + /* Process any events already waiting */ + XFlush(display); + xwin_process_events(); + FD_ZERO(&rfds); FD_SET(rdp_socket, &rfds); - if (display != NULL) - { - FD_SET(x_socket, &rfds); - XFlush(display); - } + FD_SET(x_socket, &rfds); switch (select(n, &rfds, NULL, NULL, NULL)) { @@ -579,9 +640,6 @@ continue; } - if (FD_ISSET(x_socket, &rfds)) - xwin_process_events(); - if (FD_ISSET(rdp_socket, &rfds)) return; } @@ -805,7 +863,7 @@ if (XAllocColor(display, xcolmap, &xentry) != 0) colour = xentry.pixel; else - colour = white; + colour = WhitePixelOfScreen(screen); /* byte swap here to make translate_image faster */ map[i] = translate_colour(colour);