32 |
extern BOOL sendmotion; |
extern BOOL sendmotion; |
33 |
extern BOOL fullscreen; |
extern BOOL fullscreen; |
34 |
extern BOOL grab_keyboard; |
extern BOOL grab_keyboard; |
35 |
|
extern char title[]; |
36 |
|
|
37 |
Display *display = NULL; |
Display *display; |
38 |
static int x_socket; |
static int x_socket; |
39 |
|
static Screen *screen; |
40 |
static Window wnd; |
static Window wnd; |
41 |
static GC gc; |
static GC gc; |
42 |
static Visual *visual; |
static Visual *visual; |
61 |
/* colour maps */ |
/* colour maps */ |
62 |
static BOOL owncolmap; |
static BOOL owncolmap; |
63 |
static Colormap xcolmap; |
static Colormap xcolmap; |
|
static uint32 white; |
|
64 |
static uint32 *colmap; |
static uint32 *colmap; |
|
static XIM IM = NULL; |
|
|
static XIC IC = NULL; |
|
65 |
|
|
66 |
/* Compose support */ |
/* Compose support */ |
67 |
BOOL enable_compose = False; |
BOOL enable_compose = False; |
68 |
|
static XIM IM = NULL; |
69 |
|
static XIC IC = NULL; |
70 |
|
|
71 |
|
/* toggle fullscreen globals */ |
72 |
|
static unsigned long input_mask; |
73 |
|
|
74 |
#define TRANSLATE(col) ( owncolmap ? col : translate_colour(colmap[col]) ) |
#define TRANSLATE(col) ( owncolmap ? col : translate_colour(colmap[col]) ) |
75 |
#define SET_FOREGROUND(col) XSetForeground(display, gc, TRANSLATE(col)); |
#define SET_FOREGROUND(col) XSetForeground(display, gc, TRANSLATE(col)); |
245 |
} |
} |
246 |
|
|
247 |
BOOL |
BOOL |
248 |
ui_init() |
get_key_state(int keysym) |
249 |
{ |
{ |
250 |
Screen *screen; |
int keysymMask = 0, modifierpos, key; |
251 |
display = XOpenDisplay(NULL); |
Window wDummy1, wDummy2; |
252 |
if (display == NULL) |
int iDummy3, iDummy4, iDummy5, iDummy6; |
253 |
{ |
unsigned int current_state; |
254 |
error("Failed to open display\n"); |
int offset; |
255 |
|
|
256 |
|
XModifierKeymap *map = XGetModifierMapping(display); |
257 |
|
KeyCode keycode = XKeysymToKeycode(display, keysym); |
258 |
|
|
259 |
|
if (keycode == NoSymbol) |
260 |
return False; |
return False; |
261 |
} |
|
262 |
if (fullscreen) |
for (modifierpos = 0; modifierpos < 8; modifierpos++) |
263 |
{ |
{ |
264 |
screen = DefaultScreenOfDisplay(display); |
offset = map->max_keypermod * modifierpos; |
265 |
width = WidthOfScreen(screen); |
|
266 |
height = HeightOfScreen(screen); |
for (key = 0; key < map->max_keypermod; key++) |
267 |
|
{ |
268 |
|
if (map->modifiermap[offset + key] == keycode) |
269 |
|
keysymMask = 1 << modifierpos; |
270 |
|
} |
271 |
} |
} |
272 |
return True; |
|
273 |
|
XQueryPointer(display, DefaultRootWindow(display), &wDummy1, |
274 |
|
&wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, ¤t_state); |
275 |
|
|
276 |
|
XFreeModifiermap(map); |
277 |
|
|
278 |
|
return (current_state & keysymMask) ? True : False; |
279 |
|
} |
280 |
|
|
281 |
|
static void |
282 |
|
xwin_map_window() |
283 |
|
{ |
284 |
|
XEvent xevent; |
285 |
|
|
286 |
|
XMapWindow(display, wnd); |
287 |
|
|
288 |
|
/* wait for VisibilityChange */ |
289 |
|
XMaskEvent(display, VisibilityChangeMask, &xevent); |
290 |
|
|
291 |
|
if (fullscreen) |
292 |
|
XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime); |
293 |
} |
} |
294 |
|
|
295 |
BOOL |
BOOL |
296 |
ui_create_window(char *title) |
ui_init() |
297 |
{ |
{ |
|
XSetWindowAttributes attribs; |
|
|
XClassHint *classhints; |
|
|
XSizeHints *sizehints; |
|
|
unsigned long input_mask; |
|
298 |
XPixmapFormatValues *pfm; |
XPixmapFormatValues *pfm; |
|
Screen *screen; |
|
299 |
uint16 test; |
uint16 test; |
300 |
int i; |
int i; |
301 |
XEvent xevent; |
|
302 |
|
display = XOpenDisplay(NULL); |
303 |
|
if (display == NULL) |
304 |
|
{ |
305 |
|
error("Failed to open display\n"); |
306 |
|
return False; |
307 |
|
} |
308 |
|
|
309 |
x_socket = ConnectionNumber(display); |
x_socket = ConnectionNumber(display); |
310 |
screen = DefaultScreenOfDisplay(display); |
screen = DefaultScreenOfDisplay(display); |
338 |
else |
else |
339 |
xcolmap = DefaultColormapOfScreen(screen); |
xcolmap = DefaultColormapOfScreen(screen); |
340 |
|
|
341 |
|
if (DoesBackingStore(screen) == NotUseful) |
342 |
|
ownbackstore = True; |
343 |
|
|
344 |
test = 1; |
test = 1; |
345 |
host_be = !(BOOL) (*(uint8 *) (&test)); |
host_be = !(BOOL) (*(uint8 *) (&test)); |
346 |
xserver_be = (ImageByteOrder(display) == MSBFirst); |
xserver_be = (ImageByteOrder(display) == MSBFirst); |
347 |
|
|
|
white = WhitePixelOfScreen(screen); |
|
|
attribs.background_pixel = BlackPixelOfScreen(screen); |
|
|
attribs.backing_store = DoesBackingStore(screen); |
|
|
|
|
|
if (attribs.backing_store == NotUseful) |
|
|
ownbackstore = True; |
|
|
|
|
348 |
if (fullscreen) |
if (fullscreen) |
349 |
{ |
{ |
|
attribs.override_redirect = True; |
|
350 |
width = WidthOfScreen(screen); |
width = WidthOfScreen(screen); |
351 |
height = HeightOfScreen(screen); |
height = HeightOfScreen(screen); |
352 |
} |
} |
|
else |
|
|
{ |
|
|
attribs.override_redirect = False; |
|
|
} |
|
353 |
|
|
354 |
width = (width + 3) & ~3; /* make width a multiple of 32 bits */ |
xkeymap_init(); |
355 |
|
return True; |
356 |
|
} |
357 |
|
|
358 |
wnd = XCreateWindow(display, RootWindowOfScreen(screen), |
BOOL |
359 |
0, 0, width, height, 0, CopyFromParent, |
ui_create_window() |
360 |
InputOutput, CopyFromParent, |
{ |
361 |
CWBackingStore | CWBackPixel | CWOverrideRedirect, &attribs); |
XSetWindowAttributes attribs; |
362 |
|
XClassHint *classhints; |
363 |
|
XSizeHints *sizehints; |
364 |
|
XEvent xevent; |
365 |
|
|
366 |
|
attribs.background_pixel = BlackPixelOfScreen(screen); |
367 |
|
attribs.backing_store = ownbackstore ? NotUseful : Always; |
368 |
|
attribs.override_redirect = fullscreen; |
369 |
|
wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height, |
370 |
|
0, CopyFromParent, InputOutput, CopyFromParent, |
371 |
|
CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs); |
372 |
|
|
373 |
|
if (ownbackstore) |
374 |
|
backstore = XCreatePixmap(display, wnd, width, height, depth); |
375 |
|
|
376 |
XStoreName(display, wnd, title); |
XStoreName(display, wnd, title); |
377 |
|
|
393 |
XFree(sizehints); |
XFree(sizehints); |
394 |
} |
} |
395 |
|
|
|
xkeymap_init2(); |
|
|
|
|
396 |
input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | |
input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | |
397 |
VisibilityChangeMask | FocusChangeMask; |
VisibilityChangeMask | FocusChangeMask; |
398 |
|
|
399 |
if (grab_keyboard) |
if (grab_keyboard) |
400 |
input_mask |= EnterWindowMask | LeaveWindowMask; |
input_mask |= EnterWindowMask | LeaveWindowMask; |
401 |
if (sendmotion) |
if (sendmotion) |
402 |
input_mask |= PointerMotionMask; |
input_mask |= PointerMotionMask; |
|
|
|
403 |
if (ownbackstore) |
if (ownbackstore) |
404 |
input_mask |= ExposureMask; |
input_mask |= ExposureMask; |
|
|
|
405 |
if (enable_compose) |
if (enable_compose) |
406 |
input_mask |= init_inputmethod(); |
input_mask |= init_inputmethod(); |
407 |
|
|
408 |
XSelectInput(display, wnd, input_mask); |
XSelectInput(display, wnd, input_mask); |
409 |
|
|
410 |
gc = XCreateGC(display, wnd, 0, NULL); |
xwin_map_window(); |
|
|
|
|
if (ownbackstore) |
|
|
backstore = XCreatePixmap(display, wnd, width, height, depth); |
|
|
|
|
|
XMapWindow(display, wnd); |
|
|
|
|
|
/* Wait for VisibilityNotify Event */ |
|
|
for (;;) { |
|
|
XNextEvent(display, &xevent); |
|
|
if (xevent.type == VisibilityNotify) |
|
|
break; |
|
|
} |
|
411 |
|
|
412 |
/* clear the window so that cached data is not viewed upon start... */ |
/* clear the window so that cached data is not seen */ |
413 |
XSetBackground(display, gc, 0); |
gc = XCreateGC(display, wnd, 0, NULL); |
414 |
XSetForeground(display, gc, 0); |
XSetForeground(display, gc, 0); |
415 |
FILL_RECTANGLE(0, 0, width, height); |
FILL_RECTANGLE(0, 0, width, height); |
416 |
|
|
433 |
} |
} |
434 |
|
|
435 |
static void |
static void |
436 |
xwin_process_events() |
xwin_reset_keys() |
437 |
|
{ |
438 |
|
/* reset keys */ |
439 |
|
uint32 ev_time; |
440 |
|
ev_time = time(NULL); |
441 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LCTRL); |
442 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LALT); |
443 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_LSHIFT); |
444 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RCTRL); |
445 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RALT); |
446 |
|
rdp_send_scancode(ev_time, RDP_KEYRELEASE, SCANCODE_CHAR_RSHIFT); |
447 |
|
} |
448 |
|
|
449 |
|
void |
450 |
|
xwin_toggle_fullscreen() |
451 |
{ |
{ |
452 |
XEvent xevent; |
XEvent xevent; |
453 |
|
XSetWindowAttributes attribs; |
454 |
|
int newwidth, newheight; |
455 |
|
|
456 |
|
fullscreen = !fullscreen; |
457 |
|
newwidth = fullscreen ? WidthOfScreen(screen) : width; |
458 |
|
newheight = fullscreen ? HeightOfScreen(screen) : height; |
459 |
|
|
460 |
|
XUnmapWindow(display, wnd); |
461 |
|
attribs.override_redirect = fullscreen; |
462 |
|
XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight); |
463 |
|
XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs); |
464 |
|
xwin_map_window(); |
465 |
|
} |
466 |
|
|
467 |
|
/* Process all events in Xlib queue */ |
468 |
|
static void |
469 |
|
xwin_process_events() |
470 |
|
{ |
471 |
|
XEvent xevent; |
472 |
KeySym keysym; |
KeySym keysym; |
473 |
uint16 button, flags; |
uint16 button, flags; |
474 |
uint32 ev_time; |
uint32 ev_time; |
477 |
char str[256]; |
char str[256]; |
478 |
Status status; |
Status status; |
479 |
|
|
480 |
/* Refresh keyboard mapping if it has changed. This is important for |
while (XPending(display) > 0) |
|
Xvnc, since it allocates keycodes dynamically */ |
|
|
if (XCheckTypedEvent(display, MappingNotify, &xevent)) |
|
481 |
{ |
{ |
482 |
if (xevent.xmapping.request == MappingKeyboard |
XNextEvent(display, &xevent); |
|
|| xevent.xmapping.request == MappingModifier) |
|
|
XRefreshKeyboardMapping(&xevent.xmapping); |
|
|
} |
|
483 |
|
|
|
while (XCheckMaskEvent(display, ~0, &xevent)) |
|
|
{ |
|
484 |
if (enable_compose && (XFilterEvent(&xevent, None) == True)) |
if (enable_compose && (XFilterEvent(&xevent, None) == True)) |
485 |
{ |
{ |
486 |
DEBUG_KBD(("Filtering event\n")); |
DEBUG_KBD(("Filtering event\n")); |
517 |
ksname = get_ksname(keysym); |
ksname = get_ksname(keysym); |
518 |
DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname)); |
DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname)); |
519 |
|
|
520 |
if (inhibit_key(keysym)) |
if (handle_special_keys(keysym, ev_time, True)) |
|
{ |
|
|
DEBUG_KBD(("Inhibiting key\n")); |
|
521 |
break; |
break; |
|
} |
|
522 |
|
|
523 |
tr = xkeymap_translate_key(keysym, |
tr = xkeymap_translate_key(keysym, |
524 |
xevent.xkey.keycode, xevent.xkey.state); |
xevent.xkey.keycode, xevent.xkey.state); |
525 |
|
|
|
ensure_remote_modifiers(ev_time, tr); |
|
|
|
|
526 |
if (tr.scancode == 0) |
if (tr.scancode == 0) |
527 |
break; |
break; |
528 |
|
|
529 |
|
ensure_remote_modifiers(ev_time, tr); |
530 |
|
|
531 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); |
rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); |
532 |
break; |
break; |
533 |
case KeyRelease: |
case KeyRelease: |
538 |
DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym, |
DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym, |
539 |
ksname)); |
ksname)); |
540 |
|
|
541 |
if (inhibit_key(keysym)) |
if (handle_special_keys(keysym, ev_time, False)) |
542 |
break; |
break; |
543 |
|
|
544 |
tr = xkeymap_translate_key(keysym, |
tr = xkeymap_translate_key(keysym, |
568 |
MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y); |
MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y); |
569 |
break; |
break; |
570 |
|
|
|
case FocusIn: |
|
|
/* fall through */ |
|
571 |
case EnterNotify: |
case EnterNotify: |
572 |
if (grab_keyboard) |
if (grab_keyboard) |
573 |
XGrabKeyboard(display, wnd, True, |
XGrabKeyboard(display, wnd, True, |
574 |
GrabModeAsync, GrabModeAsync, CurrentTime); |
GrabModeAsync, GrabModeAsync, CurrentTime); |
575 |
break; |
break; |
576 |
|
|
|
case FocusOut: |
|
|
/* reset keys */ |
|
|
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); |
|
|
/* fall through */ |
|
577 |
case LeaveNotify: |
case LeaveNotify: |
578 |
if (grab_keyboard) |
if (grab_keyboard) |
579 |
XUngrabKeyboard(display, CurrentTime); |
XUngrabKeyboard(display, CurrentTime); |
580 |
break; |
break; |
581 |
|
|
582 |
|
case FocusOut: |
583 |
|
xwin_reset_keys(); |
584 |
|
break; |
585 |
|
|
586 |
case Expose: |
case Expose: |
587 |
XCopyArea(display, backstore, wnd, gc, |
XCopyArea(display, backstore, wnd, gc, |
588 |
xevent.xexpose.x, xevent.xexpose.y, |
xevent.xexpose.x, xevent.xexpose.y, |
590 |
xevent.xexpose.height, |
xevent.xexpose.height, |
591 |
xevent.xexpose.x, xevent.xexpose.y); |
xevent.xexpose.x, xevent.xexpose.y); |
592 |
break; |
break; |
593 |
|
|
594 |
|
case MappingNotify: |
595 |
|
/* Refresh keyboard mapping if it has changed. This is important for |
596 |
|
Xvnc, since it allocates keycodes dynamically */ |
597 |
|
if (xevent.xmapping.request == MappingKeyboard |
598 |
|
|| xevent.xmapping.request == MappingModifier) |
599 |
|
XRefreshKeyboardMapping(&xevent.xmapping); |
600 |
|
break; |
601 |
|
|
602 |
} |
} |
603 |
} |
} |
604 |
} |
} |
613 |
|
|
614 |
while (True) |
while (True) |
615 |
{ |
{ |
616 |
|
/* Process any events already waiting */ |
617 |
|
xwin_process_events(); |
618 |
|
|
619 |
FD_ZERO(&rfds); |
FD_ZERO(&rfds); |
620 |
FD_SET(rdp_socket, &rfds); |
FD_SET(rdp_socket, &rfds); |
621 |
if (display != NULL) |
FD_SET(x_socket, &rfds); |
|
{ |
|
|
FD_SET(x_socket, &rfds); |
|
|
XFlush(display); |
|
|
} |
|
622 |
|
|
623 |
switch (select(n, &rfds, NULL, NULL, NULL)) |
switch (select(n, &rfds, NULL, NULL, NULL)) |
624 |
{ |
{ |
629 |
continue; |
continue; |
630 |
} |
} |
631 |
|
|
|
if (FD_ISSET(x_socket, &rfds)) |
|
|
xwin_process_events(); |
|
|
|
|
632 |
if (FD_ISSET(rdp_socket, &rfds)) |
if (FD_ISSET(rdp_socket, &rfds)) |
633 |
return; |
return; |
634 |
} |
} |
852 |
if (XAllocColor(display, xcolmap, &xentry) != 0) |
if (XAllocColor(display, xcolmap, &xentry) != 0) |
853 |
colour = xentry.pixel; |
colour = xentry.pixel; |
854 |
else |
else |
855 |
colour = white; |
colour = WhitePixelOfScreen(screen); |
856 |
|
|
857 |
/* byte swap here to make translate_image faster */ |
/* byte swap here to make translate_image faster */ |
858 |
map[i] = translate_colour(colour); |
map[i] = translate_colour(colour); |