--- sourceforge.net/trunk/rdesktop/xwin.c 2002/08/24 20:04:56 100 +++ sourceforge.net/trunk/rdesktop/xwin.c 2002/09/17 16:55:43 182 @@ -34,15 +34,14 @@ 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; @@ -62,16 +61,14 @@ /* 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 XSetWindowAttributes attribs; static unsigned long input_mask; #define TRANSLATE(col) ( owncolmap ? col : translate_colour(colmap[col]) ) @@ -248,94 +245,67 @@ } BOOL -ui_init() +get_key_state(int keysym) { - Screen *screen; - display = XOpenDisplay(NULL); - if (display == NULL) - { - error("Failed to open display\n"); - return False; - } - if (fullscreen) - { - screen = DefaultScreenOfDisplay(display); - width = WidthOfScreen(screen); - height = HeightOfScreen(screen); - } - return True; -} + int keysymMask = 0, modifierpos, key; + Window wDummy1, wDummy2; + int iDummy3, iDummy4, iDummy5, iDummy6; + unsigned int current_state; + int offset; -BOOL -ui_create_window_obj(int xpos, int ypos, int width, int height, int valuemask) -{ - XClassHint *classhints; - XSizeHints *sizehints; - XEvent xevent; - Screen *screen; - - screen = DefaultScreenOfDisplay(display); - - wnd = XCreateWindow(display, RootWindowOfScreen(screen), xpos, - ypos, width, height, 0, CopyFromParent, - InputOutput, CopyFromParent, valuemask, &attribs); + XModifierKeymap *map = XGetModifierMapping(display); + KeyCode keycode = XKeysymToKeycode(display, keysym); + if (keycode == NoSymbol) + return False; - XStoreName(display, wnd, title); - - classhints = XAllocClassHint(); - if (classhints != NULL) + for (modifierpos = 0; modifierpos < 8; modifierpos++) { - classhints->res_name = classhints->res_class = "rdesktop"; - XSetClassHint(display, wnd, classhints); - XFree(classhints); - } + offset = map->max_keypermod * modifierpos; - sizehints = XAllocSizeHints(); - if (sizehints) - { - sizehints->flags = PMinSize | PMaxSize; - sizehints->min_width = sizehints->max_width = width; - sizehints->min_height = sizehints->max_height = height; - XSetWMNormalHints(display, wnd, sizehints); - XFree(sizehints); + for (key = 0; key < map->max_keypermod; key++) + { + if (map->modifiermap[offset + key] == keycode) + keysymMask = 1 << modifierpos; + } } - if (enable_compose) - input_mask |= init_inputmethod(); + XQueryPointer(display, DefaultRootWindow(display), &wDummy1, + &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, ¤t_state); - XSelectInput(display, wnd, input_mask); + XFreeModifiermap(map); - gc = XCreateGC(display, wnd, 0, NULL); + return (current_state & keysymMask) ? True : False; +} - XMapWindow(display, wnd); +static void +xwin_map_window() +{ + XEvent xevent; - /* Wait for VisibilityNotify Event */ - for (;;) { - XNextEvent(display, &xevent); - if (xevent.type == VisibilityNotify) - break; - } + XMapWindow(display, wnd); - if (ownbackstore) - backstore = XCreatePixmap(display, wnd, width, height, depth); + /* wait for VisibilityChange */ + XMaskEvent(display, VisibilityChangeMask, &xevent); - /* 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); + if (fullscreen) + XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime); } BOOL -ui_create_window() +ui_init() { XPixmapFormatValues *pfm; - Screen *screen; uint16 test; int i; + display = XOpenDisplay(NULL); + if (display == NULL) + { + error("Failed to open display\n"); + return False; + } + x_socket = ConnectionNumber(display); screen = DefaultScreenOfDisplay(display); visual = DefaultVisualOfScreen(screen); @@ -368,51 +338,84 @@ 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); + if (fullscreen) + { + width = WidthOfScreen(screen); + height = HeightOfScreen(screen); + } + + /* make sure width is a multiple of 4 */ + width = (width + 3) & ~3; + + xkeymap_init(); + return True; +} + +BOOL +ui_create_window() +{ + XSetWindowAttributes attribs; + XClassHint *classhints; + XSizeHints *sizehints; + XEvent xevent; + attribs.background_pixel = BlackPixelOfScreen(screen); - attribs.backing_store = DoesBackingStore(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); - if (attribs.backing_store == NotUseful) - ownbackstore = True; + if (ownbackstore) + backstore = XCreatePixmap(display, wnd, width, height, depth); - dpy_width = WidthOfScreen(screen); - dpy_height = HeightOfScreen(screen); + XStoreName(display, wnd, title); - if (fullscreen) + classhints = XAllocClassHint(); + if (classhints != NULL) { - attribs.override_redirect = True; - width = dpy_width; - height = dpy_height; + classhints->res_name = classhints->res_class = "rdesktop"; + XSetClassHint(display, wnd, classhints); + XFree(classhints); } - else + + sizehints = XAllocSizeHints(); + if (sizehints) { - attribs.override_redirect = False; + sizehints->flags = PMinSize | PMaxSize; + sizehints->min_width = sizehints->max_width = width; + sizehints->min_height = sizehints->max_height = height; + XSetWMNormalHints(display, wnd, sizehints); + XFree(sizehints); } - width = (width + 3) & ~3; /* make width a multiple of 32 bits */ - - input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | - VisibilityChangeMask | FocusChangeMask; + 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(); - if (fullscreen) - ui_create_window_obj(0, 0, width, height, CWBackingStore | CWBackPixel | CWOverrideRedirect); - else - ui_create_window_obj(0, 0, width, height, CWBackingStore | CWBackPixel); + XSelectInput(display, wnd, input_mask); - xkeymap_init2(); + xwin_map_window(); + + /* clear the window so that cached data is not seen */ + gc = XCreateGC(display, wnd, 0, NULL); + XSetForeground(display, gc, 0); + FILL_RECTANGLE(0, 0, width, height); return True; } @@ -432,61 +435,30 @@ 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() +xwin_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); - if (fullscreen) { - attribs.override_redirect = True; - ui_create_window_obj(0, 0, dpy_width, dpy_height, - CWBackingStore | CWBackPixel | CWOverrideRedirect); - } - else { - attribs.override_redirect = False; - ui_create_window_obj(0, 0, width, height, - CWBackingStore | CWBackPixel); - } - 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); + XEvent xevent; + XSetWindowAttributes attribs; + int newwidth, newheight; + + fullscreen = !fullscreen; + newwidth = fullscreen ? WidthOfScreen(screen) : width; + newheight = fullscreen ? HeightOfScreen(screen) : height; + + XUnmapWindow(display, wnd); + attribs.override_redirect = fullscreen; + XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight); + XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs); + xwin_map_window(); } +/* Process all events in Xlib queue */ static void xwin_process_events() { XEvent xevent; - KeySym keysym; uint16 button, flags; uint32 ev_time; @@ -495,17 +467,10 @@ 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)) + while (XPending(display) > 0) { - if (xevent.xmapping.request == MappingKeyboard - || xevent.xmapping.request == MappingModifier) - XRefreshKeyboardMapping(&xevent.xmapping); - } + XNextEvent(display, &xevent); - while (XCheckMaskEvent(display, ~0, &xevent)) - { if (enable_compose && (XFilterEvent(&xevent, None) == True)) { DEBUG_KBD(("Filtering event\n")); @@ -534,34 +499,25 @@ else { /* Plain old XLookupString */ - DEBUG_KBD(("No input context, using XLookupString\n")); + DEBUG_KBD(("\nNo input context, using XLookupString\n")); XLookupString((XKeyEvent *) & xevent, str, sizeof(str), &keysym, NULL); } - /* FIXME needs alt modifier */ - if (keysym == XK_Break) /* toggle full screen */ - { - toggle_fullscreen(); - break; - } ksname = get_ksname(keysym); - DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname)); + DEBUG_KBD(("KeyPress 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: @@ -572,7 +528,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, @@ -602,22 +558,21 @@ MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y); break; - case FocusIn: - /* fall through */ case EnterNotify: if (grab_keyboard) XGrabKeyboard(display, wnd, True, GrabModeAsync, GrabModeAsync, CurrentTime); break; - case FocusOut: - reset_keys(); - /* fall through */ case LeaveNotify: if (grab_keyboard) XUngrabKeyboard(display, CurrentTime); break; + case FocusIn: + reset_modifier_keys(); + break; + case Expose: XCopyArea(display, backstore, wnd, gc, xevent.xexpose.x, xevent.xexpose.y, @@ -625,6 +580,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; + } } } @@ -639,13 +603,12 @@ while (True) { + /* Process any events already waiting */ + 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)) { @@ -656,9 +619,6 @@ continue; } - if (FD_ISSET(x_socket, &rfds)) - xwin_process_events(); - if (FD_ISSET(rdp_socket, &rfds)) return; } @@ -882,7 +842,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);