--- sourceforge.net/trunk/rdesktop/xwin.c 2002/09/24 07:33:17 190 +++ sourceforge.net/trunk/rdesktop/xwin.c 2002/11/11 23:15:27 257 @@ -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 @@ -31,6 +31,8 @@ extern BOOL grab_keyboard; extern char title[]; BOOL enable_compose = False; +BOOL focused; +BOOL mouse_in_wnd; Display *display; static int x_socket; @@ -42,6 +44,7 @@ static int bpp; static XIM IM; static XIC IC; +static XModifierKeymap *mod_map; static Cursor current_cursor; /* endianness */ @@ -183,15 +186,11 @@ } 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) @@ -199,25 +198,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; @@ -226,7 +220,7 @@ display = XOpenDisplay(NULL); if (display == NULL) { - error("Failed to open display\n"); + error("Failed to open display: %s\n", XDisplayName(NULL)); return False; } @@ -278,13 +272,16 @@ 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); @@ -293,11 +290,13 @@ } void -ui_deinit() +ui_deinit(void) { if (IM != NULL) XCloseIM(IM); + XFreeModifiermap(mod_map); + if (ownbackstore) XFreePixmap(display, backstore); @@ -307,7 +306,7 @@ } BOOL -ui_create_window() +ui_create_window(void) { XSetWindowAttributes attribs; XClassHint *classhints; @@ -316,7 +315,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); @@ -348,40 +347,45 @@ } 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; return True; } void -ui_destroy_window() +ui_destroy_window(void) { if (IC != NULL) XDestroyIC(IC); @@ -390,7 +394,7 @@ } void -xwin_toggle_fullscreen() +xwin_toggle_fullscreen(void) { Pixmap contents = 0; @@ -416,16 +420,18 @@ /* Process all events in Xlib queue */ static void -xwin_process_events() +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) { @@ -437,7 +443,6 @@ continue; } - ev_time = time(NULL); flags = 0; switch (xevent.type) @@ -464,10 +469,10 @@ 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, @@ -480,15 +485,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, @@ -509,28 +515,52 @@ if (button == 0) break; - rdp_send_input(ev_time, RDP_INPUT_MOUSE, + 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, + 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: @@ -547,6 +577,12 @@ 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; } @@ -784,26 +820,38 @@ long nDist = nMinDist; /* only get the colors once */ - while( colLookup-- ){ + while (colLookup--) + { xc_cache[colLookup].pixel = colLookup; - xc_cache[colLookup].red = xc_cache[colLookup].green = xc_cache[colLookup].blue = 0; + 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]); + XQueryColor(display, + DefaultColormap(display, DefaultScreen(display)), + &xc_cache[colLookup]); } 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)); + 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 ){ + if (nDist < nMinDist) + { nMinDist = nDist; xentry.pixel = j; } @@ -812,7 +860,8 @@ colour = xentry.pixel; /* update our cache */ - if( xentry.pixel < 256 ){ + 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; @@ -852,7 +901,7 @@ } void -ui_reset_clip() +ui_reset_clip(void) { XRectangle rect; @@ -864,7 +913,7 @@ } void -ui_bell() +ui_bell(void) { XBell(display, 0); }