--- sourceforge.net/trunk/rdesktop/xwin.c 2002/09/11 11:45:20 118 +++ sourceforge.net/trunk/rdesktop/xwin.c 2002/09/15 11:39:16 157 @@ -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]) ) @@ -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; - } - - 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); @@ -404,29 +338,59 @@ 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); + } + + 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 +400,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); + + xwin_map_window(); - xkeymap_init2(); + /* 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,8 +432,8 @@ display = NULL; } -void -reset_keys() +static void +xwin_reset_keys() { /* reset keys */ uint32 ev_time; @@ -481,47 +447,28 @@ } 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 +477,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")); @@ -637,7 +577,7 @@ break; case FocusOut: - reset_keys(); + xwin_reset_keys(); /* fall through */ case LeaveNotify: if (grab_keyboard) @@ -651,6 +591,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; + } } } @@ -665,13 +614,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)) { @@ -682,9 +630,6 @@ continue; } - if (FD_ISSET(x_socket, &rfds)) - xwin_process_events(); - if (FD_ISSET(rdp_socket, &rfds)) return; } @@ -908,7 +853,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);