--- sourceforge.net/trunk/rdesktop/xwin.c 2002/09/26 14:04:30 203 +++ 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; @@ -184,7 +186,7 @@ } BOOL -get_key_state(uint32 keysym, unsigned int state) +get_key_state(unsigned int state, uint32 keysym) { int modifierpos, key, keysymMask = 0; int offset; @@ -218,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; } @@ -293,7 +295,7 @@ if (IM != NULL) XCloseIM(IM); - XFreeModifierMap(mod_map); + XFreeModifiermap(mod_map); if (ownbackstore) XFreePixmap(display, backstore); @@ -345,12 +347,16 @@ } input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | - StructureNotifyMask | FocusChangeMask; + VisibilityChangeMask | FocusChangeMask; 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) { @@ -365,15 +371,15 @@ XSelectInput(display, wnd, input_mask); XMapWindow(display, wnd); - /* wait for MapNotify */ + /* wait for VisibilityNotify */ do { - XMaskEvent(display, StructureNotifyMask, &xevent); + XMaskEvent(display, VisibilityChangeMask, &xevent); } - while (xevent.type != MapNotify); + while (xevent.type != VisibilityNotify); - if (fullscreen) - XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime); + focused = False; + mouse_in_wnd = False; return True; } @@ -519,18 +525,44 @@ break; 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) + if (grab_keyboard && mouse_in_wnd) XGrabKeyboard(display, wnd, True, GrabModeAsync, GrabModeAsync, CurrentTime); break; case FocusOut: + if (xevent.xfocus.mode == NotifyUngrab) + break; + focused = False; if (xevent.xfocus.mode == NotifyWhileGrabbed) XUngrabKeyboard(display, CurrentTime); break; + 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: XCopyArea(display, backstore, wnd, gc, xevent.xexpose.x, xevent.xexpose.y, @@ -548,7 +580,7 @@ if (xevent.xmapping.request == MappingModifier) { - XFreeModifierMap(mod_map); + XFreeModifiermap(mod_map); mod_map = XGetModifierMapping(display); } break;