--- sourceforge.net/trunk/rdesktop/xwin.c 2002/08/26 17:12:43 101 +++ sourceforge.net/trunk/rdesktop/xwin.c 2002/09/14 11:48:44 121 @@ -34,8 +34,9 @@ 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; @@ -62,16 +63,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,95 +247,54 @@ } BOOL -ui_init() -{ - 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; -} - -BOOL -ui_create_window_obj(int xpos, int ypos, int width, int height, int valuemask) +get_key_state(int keysym) { - 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); + 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); - XStoreName(display, wnd, title); + if (keycode == NoSymbol) + return False; - 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(); - - XSelectInput(display, wnd, input_mask); - - gc = XCreateGC(display, wnd, 0, NULL); - - XMapWindow(display, wnd); + XQueryPointer(display, DefaultRootWindow(display), &wDummy1, + &wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, ¤t_state); - /* Wait for VisibilityNotify Event */ - for (;;) - { - XNextEvent(display, &xevent); - if (xevent.type == VisibilityNotify) - break; - } - - if (ownbackstore) - backstore = XCreatePixmap(display, wnd, width, height, depth); + XFreeModifiermap(map); - /* 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 (current_state & keysymMask) ? True : False; } + 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); @@ -369,34 +327,58 @@ 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 (fullscreen) + { + width = WidthOfScreen(screen); + height = HeightOfScreen(screen); + } - if (attribs.backing_store == NotUseful) - ownbackstore = True; + xkeymap_init(); + return True; +} - dpy_width = WidthOfScreen(screen); - dpy_height = HeightOfScreen(screen); +BOOL +ui_create_window() +{ + XSetWindowAttributes attribs; + XClassHint *classhints; + XSizeHints *sizehints; + XEvent xevent; - if (fullscreen) + 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); + + 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; @@ -404,17 +386,34 @@ 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); + + gc = XCreateGC(display, wnd, 0, NULL); + + XMapWindow(display, wnd); - xkeymap_init2(); + /* Wait for VisibilityNotify Event */ + 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; } @@ -464,17 +463,7 @@ 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_create_window(); ui_set_cursor(cache_get_cursor(0)); ui_move_pointer(width / 2, height / 2); reset_keys(); @@ -485,6 +474,7 @@ XFreePixmap(display, pixmap); } +/* Process all events in Xlib queue */ static void xwin_process_events() { @@ -498,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)) @@ -542,29 +523,20 @@ 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)); - 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: @@ -575,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, @@ -628,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; + } } } @@ -642,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)) { @@ -659,9 +640,6 @@ continue; } - if (FD_ISSET(x_socket, &rfds)) - xwin_process_events(); - if (FD_ISSET(rdp_socket, &rfds)) return; } @@ -885,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);