--- sourceforge.net/trunk/rdesktop/xwin.c 2002/08/29 14:18:24 103 +++ sourceforge.net/trunk/rdesktop/xwin.c 2002/09/17 08:18:41 170 @@ -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]) ) @@ -247,7 +244,7 @@ } } -static BOOL +BOOL get_key_state(int keysym) { int keysymMask = 0, modifierpos, key; @@ -281,97 +278,34 @@ return (current_state & keysymMask) ? True : False; } - -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; -} - -void -ui_create_window_obj(int xpos, int ypos, int width, int height, int valuemask) +static void +xwin_map_window() { - 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); - - - XStoreName(display, wnd, title); - - classhints = XAllocClassHint(); - if (classhints != NULL) - { - classhints->res_name = classhints->res_class = "rdesktop"; - XSetClassHint(display, wnd, classhints); - XFree(classhints); - } - - 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); - } - - if (enable_compose) - input_mask |= init_inputmethod(); - - XSelectInput(display, wnd, input_mask); - - gc = XCreateGC(display, wnd, 0, NULL); XMapWindow(display, wnd); - /* Wait for VisibilityNotify Event */ - for (;;) - { - XNextEvent(display, &xevent); - if (xevent.type == VisibilityNotify) - break; - } + /* wait for VisibilityChange */ + XMaskEvent(display, VisibilityChangeMask, &xevent); - 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); + 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); @@ -404,29 +338,62 @@ 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); } input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | @@ -436,17 +403,19 @@ 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; } @@ -466,62 +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; @@ -530,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")); @@ -574,32 +504,20 @@ str, sizeof(str), &keysym, NULL); } - if (keysym == XK_Break) /* toggle full screen */ - { - if (get_key_state(XK_Alt_L) || get_key_state(XK_Alt_R)) - { - 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: @@ -610,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, @@ -640,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, @@ -663,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; + } } } @@ -677,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)) { @@ -694,9 +619,6 @@ continue; } - if (FD_ISSET(x_socket, &rfds)) - xwin_process_events(); - if (FD_ISSET(rdp_socket, &rfds)) return; } @@ -920,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);