34 |
extern BOOL grab_keyboard; |
extern BOOL grab_keyboard; |
35 |
extern char title[]; |
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; |
43 |
static int depth; |
static int depth; |
44 |
static int bpp; |
static int bpp; |
|
static int dpy_width; |
|
|
static int dpy_height; |
|
45 |
|
|
46 |
/* endianness */ |
/* endianness */ |
47 |
static BOOL host_be; |
static BOOL host_be; |
59 |
} |
} |
60 |
|
|
61 |
/* colour maps */ |
/* colour maps */ |
|
static BOOL owncolmap; |
|
62 |
static Colormap xcolmap; |
static Colormap xcolmap; |
|
static uint32 white; |
|
63 |
static uint32 *colmap; |
static uint32 *colmap; |
|
static XIM IM = NULL; |
|
|
static XIC IC = NULL; |
|
64 |
|
|
65 |
/* Compose support */ |
/* Compose support */ |
66 |
BOOL enable_compose = False; |
BOOL enable_compose = False; |
67 |
|
static XIM IM = NULL; |
68 |
|
static XIC IC = NULL; |
69 |
|
|
70 |
/* toggle fullscreen globals */ |
/* toggle fullscreen globals */ |
|
static XSetWindowAttributes attribs; |
|
71 |
static unsigned long input_mask; |
static unsigned long input_mask; |
72 |
|
|
73 |
#define TRANSLATE(col) ( owncolmap ? col : translate_colour(colmap[col]) ) |
#define SET_FOREGROUND(col) XSetForeground(display, gc, translate_colour(colmap[col])); |
74 |
#define SET_FOREGROUND(col) XSetForeground(display, gc, TRANSLATE(col)); |
#define SET_BACKGROUND(col) XSetBackground(display, gc, translate_colour(colmap[col])); |
|
#define SET_BACKGROUND(col) XSetBackground(display, gc, TRANSLATE(col)); |
|
75 |
|
|
76 |
static int rop2_map[] = { |
static int rop2_map[] = { |
77 |
GXclear, /* 0 */ |
GXclear, /* 0 */ |
243 |
} |
} |
244 |
|
|
245 |
BOOL |
BOOL |
246 |
ui_init() |
get_key_state(int keysym) |
|
{ |
|
|
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; |
|
|
} |
|
|
|
|
|
BOOL |
|
|
ui_create_window_obj(int xpos, int ypos, int width, int height, int valuemask) |
|
247 |
{ |
{ |
248 |
XClassHint *classhints; |
int keysymMask = 0, modifierpos, key; |
249 |
XSizeHints *sizehints; |
Window wDummy1, wDummy2; |
250 |
XEvent xevent; |
int iDummy3, iDummy4, iDummy5, iDummy6; |
251 |
Screen *screen; |
unsigned int current_state; |
252 |
|
int offset; |
|
screen = DefaultScreenOfDisplay(display); |
|
|
|
|
|
wnd = XCreateWindow(display, RootWindowOfScreen(screen), xpos, |
|
|
ypos, width, height, 0, CopyFromParent, |
|
|
InputOutput, CopyFromParent, valuemask, &attribs); |
|
253 |
|
|
254 |
|
XModifierKeymap *map = XGetModifierMapping(display); |
255 |
|
KeyCode keycode = XKeysymToKeycode(display, keysym); |
256 |
|
|
257 |
XStoreName(display, wnd, title); |
if (keycode == NoSymbol) |
258 |
|
return False; |
259 |
|
|
260 |
classhints = XAllocClassHint(); |
for (modifierpos = 0; modifierpos < 8; modifierpos++) |
|
if (classhints != NULL) |
|
261 |
{ |
{ |
262 |
classhints->res_name = classhints->res_class = "rdesktop"; |
offset = map->max_keypermod * modifierpos; |
|
XSetClassHint(display, wnd, classhints); |
|
|
XFree(classhints); |
|
|
} |
|
263 |
|
|
264 |
sizehints = XAllocSizeHints(); |
for (key = 0; key < map->max_keypermod; key++) |
265 |
if (sizehints) |
{ |
266 |
{ |
if (map->modifiermap[offset + key] == keycode) |
267 |
sizehints->flags = PMinSize | PMaxSize; |
keysymMask = 1 << modifierpos; |
268 |
sizehints->min_width = sizehints->max_width = width; |
} |
|
sizehints->min_height = sizehints->max_height = height; |
|
|
XSetWMNormalHints(display, wnd, sizehints); |
|
|
XFree(sizehints); |
|
269 |
} |
} |
270 |
|
|
271 |
if (enable_compose) |
XQueryPointer(display, DefaultRootWindow(display), &wDummy1, |
272 |
input_mask |= init_inputmethod(); |
&wDummy2, &iDummy3, &iDummy4, &iDummy5, &iDummy6, ¤t_state); |
273 |
|
|
274 |
XSelectInput(display, wnd, input_mask); |
XFreeModifiermap(map); |
275 |
|
|
276 |
gc = XCreateGC(display, wnd, 0, NULL); |
return (current_state & keysymMask) ? True : False; |
277 |
|
} |
278 |
|
|
279 |
XMapWindow(display, wnd); |
static void |
280 |
|
xwin_map_window() |
281 |
|
{ |
282 |
|
XEvent xevent; |
283 |
|
|
284 |
/* Wait for VisibilityNotify Event */ |
XMapWindow(display, wnd); |
|
for (;;) |
|
|
{ |
|
|
XNextEvent(display, &xevent); |
|
|
if (xevent.type == VisibilityNotify) |
|
|
break; |
|
|
} |
|
285 |
|
|
286 |
if (ownbackstore) |
/* wait for VisibilityChange */ |
287 |
backstore = XCreatePixmap(display, wnd, width, height, depth); |
XMaskEvent(display, VisibilityChangeMask, &xevent); |
288 |
|
|
289 |
/* clear the window so that cached data is not viewed upon start... */ |
if (fullscreen) |
290 |
XSetBackground(display, gc, 0); |
XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime); |
|
XSetForeground(display, gc, 0); |
|
|
FILL_RECTANGLE(0, 0, width, height); |
|
|
/* make sure the window is focused */ |
|
|
XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime); |
|
291 |
} |
} |
292 |
|
|
293 |
BOOL |
BOOL |
294 |
ui_create_window() |
ui_init() |
295 |
{ |
{ |
296 |
XPixmapFormatValues *pfm; |
XPixmapFormatValues *pfm; |
|
Screen *screen; |
|
297 |
uint16 test; |
uint16 test; |
298 |
int i; |
int i; |
299 |
|
|
300 |
|
display = XOpenDisplay(NULL); |
301 |
|
if (display == NULL) |
302 |
|
{ |
303 |
|
error("Failed to open display\n"); |
304 |
|
return False; |
305 |
|
} |
306 |
|
|
307 |
x_socket = ConnectionNumber(display); |
x_socket = ConnectionNumber(display); |
308 |
screen = DefaultScreenOfDisplay(display); |
screen = DefaultScreenOfDisplay(display); |
309 |
visual = DefaultVisualOfScreen(screen); |
visual = DefaultVisualOfScreen(screen); |
331 |
return False; |
return False; |
332 |
} |
} |
333 |
|
|
334 |
if (depth <= 8) |
xcolmap = DefaultColormapOfScreen(screen); |
335 |
owncolmap = True; |
|
336 |
else |
if (DoesBackingStore(screen) == NotUseful) |
337 |
xcolmap = DefaultColormapOfScreen(screen); |
ownbackstore = True; |
338 |
|
|
339 |
test = 1; |
test = 1; |
340 |
host_be = !(BOOL) (*(uint8 *) (&test)); |
host_be = !(BOOL) (*(uint8 *) (&test)); |
341 |
xserver_be = (ImageByteOrder(display) == MSBFirst); |
xserver_be = (ImageByteOrder(display) == MSBFirst); |
342 |
|
|
343 |
white = WhitePixelOfScreen(screen); |
if (fullscreen) |
344 |
|
{ |
345 |
|
width = WidthOfScreen(screen); |
346 |
|
height = HeightOfScreen(screen); |
347 |
|
} |
348 |
|
|
349 |
|
/* make sure width is a multiple of 4 */ |
350 |
|
width = (width + 3) & ~3; |
351 |
|
|
352 |
|
xkeymap_init(); |
353 |
|
return True; |
354 |
|
} |
355 |
|
|
356 |
|
BOOL |
357 |
|
ui_create_window() |
358 |
|
{ |
359 |
|
XSetWindowAttributes attribs; |
360 |
|
XClassHint *classhints; |
361 |
|
XSizeHints *sizehints; |
362 |
|
XEvent xevent; |
363 |
|
|
364 |
attribs.background_pixel = BlackPixelOfScreen(screen); |
attribs.background_pixel = BlackPixelOfScreen(screen); |
365 |
attribs.backing_store = DoesBackingStore(screen); |
attribs.backing_store = ownbackstore ? NotUseful : Always; |
366 |
|
attribs.override_redirect = fullscreen; |
367 |
|
wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height, |
368 |
|
0, CopyFromParent, InputOutput, CopyFromParent, |
369 |
|
CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs); |
370 |
|
|
371 |
if (attribs.backing_store == NotUseful) |
if (ownbackstore) |
372 |
ownbackstore = True; |
backstore = XCreatePixmap(display, wnd, width, height, depth); |
373 |
|
|
374 |
dpy_width = WidthOfScreen(screen); |
XStoreName(display, wnd, title); |
|
dpy_height = HeightOfScreen(screen); |
|
375 |
|
|
376 |
if (fullscreen) |
classhints = XAllocClassHint(); |
377 |
|
if (classhints != NULL) |
378 |
{ |
{ |
379 |
attribs.override_redirect = True; |
classhints->res_name = classhints->res_class = "rdesktop"; |
380 |
width = dpy_width; |
XSetClassHint(display, wnd, classhints); |
381 |
height = dpy_height; |
XFree(classhints); |
382 |
} |
} |
383 |
else |
|
384 |
|
sizehints = XAllocSizeHints(); |
385 |
|
if (sizehints) |
386 |
{ |
{ |
387 |
attribs.override_redirect = False; |
sizehints->flags = PMinSize | PMaxSize; |
388 |
|
sizehints->min_width = sizehints->max_width = width; |
389 |
|
sizehints->min_height = sizehints->max_height = height; |
390 |
|
XSetWMNormalHints(display, wnd, sizehints); |
391 |
|
XFree(sizehints); |
392 |
} |
} |
393 |
|
|
|
width = (width + 3) & ~3; /* make width a multiple of 32 bits */ |
|
|
|
|
|
|
|
394 |
input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | |
input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | |
395 |
VisibilityChangeMask | FocusChangeMask; |
VisibilityChangeMask | FocusChangeMask; |
396 |
|
|
398 |
input_mask |= EnterWindowMask | LeaveWindowMask; |
input_mask |= EnterWindowMask | LeaveWindowMask; |
399 |
if (sendmotion) |
if (sendmotion) |
400 |
input_mask |= PointerMotionMask; |
input_mask |= PointerMotionMask; |
|
|
|
401 |
if (ownbackstore) |
if (ownbackstore) |
402 |
input_mask |= ExposureMask; |
input_mask |= ExposureMask; |
403 |
|
if (enable_compose) |
404 |
|
input_mask |= init_inputmethod(); |
405 |
|
|
406 |
if (fullscreen) |
XSelectInput(display, wnd, input_mask); |
407 |
ui_create_window_obj(0, 0, width, height, |
|
408 |
CWBackingStore | CWBackPixel | CWOverrideRedirect); |
xwin_map_window(); |
|
else |
|
|
ui_create_window_obj(0, 0, width, height, CWBackingStore | CWBackPixel); |
|
409 |
|
|
410 |
xkeymap_init2(); |
/* clear the window so that cached data is not seen */ |
411 |
|
gc = XCreateGC(display, wnd, 0, NULL); |
412 |
|
XSetForeground(display, gc, 0); |
413 |
|
FILL_RECTANGLE(0, 0, width, height); |
414 |
|
|
415 |
return True; |
return True; |
416 |
} |
} |
430 |
display = NULL; |
display = NULL; |
431 |
} |
} |
432 |
|
|
|
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); |
|
|
} |
|
433 |
|
|
434 |
void |
void |
435 |
toggle_fullscreen() |
xwin_toggle_fullscreen() |
436 |
{ |
{ |
437 |
/* save window contents */ |
XEvent xevent; |
438 |
Pixmap pixmap; |
XSetWindowAttributes attribs; |
439 |
pixmap = XCreatePixmap(display, wnd, width, height, depth); |
int newwidth, newheight; |
440 |
if (ownbackstore) |
|
441 |
XCopyArea(display, backstore, pixmap, gc, 0, 0, width, height, 0, 0); |
fullscreen = !fullscreen; |
442 |
else |
newwidth = fullscreen ? WidthOfScreen(screen) : width; |
443 |
XCopyArea(display, wnd, pixmap, gc, 0, 0, width, height, 0, 0); |
newheight = fullscreen ? HeightOfScreen(screen) : height; |
444 |
fullscreen = fullscreen ? False : True; |
|
445 |
close_inputmethod(); |
XUnmapWindow(display, wnd); |
446 |
if (ownbackstore) |
attribs.override_redirect = fullscreen; |
447 |
XFreePixmap(display, backstore); |
XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight); |
448 |
XFreeGC(display, gc); |
XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs); |
449 |
XDestroyWindow(display, wnd); |
xwin_map_window(); |
|
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); |
|
450 |
} |
} |
451 |
|
|
452 |
|
/* Process all events in Xlib queue */ |
453 |
static void |
static void |
454 |
xwin_process_events() |
xwin_process_events() |
455 |
{ |
{ |
456 |
XEvent xevent; |
XEvent xevent; |
|
|
|
457 |
KeySym keysym; |
KeySym keysym; |
458 |
uint16 button, flags; |
uint16 button, flags; |
459 |
uint32 ev_time; |
uint32 ev_time; |
462 |
char str[256]; |
char str[256]; |
463 |
Status status; |
Status status; |
464 |
|
|
465 |
/* 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)) |
|
466 |
{ |
{ |
467 |
if (xevent.xmapping.request == MappingKeyboard |
XNextEvent(display, &xevent); |
|
|| xevent.xmapping.request == MappingModifier) |
|
|
XRefreshKeyboardMapping(&xevent.xmapping); |
|
|
} |
|
468 |
|
|
|
while (XCheckMaskEvent(display, ~0, &xevent)) |
|
|
{ |
|
469 |
if (enable_compose && (XFilterEvent(&xevent, None) == True)) |
if (enable_compose && (XFilterEvent(&xevent, None) == True)) |
470 |
{ |
{ |
471 |
DEBUG_KBD(("Filtering event\n")); |
DEBUG_KBD(("Filtering event\n")); |
494 |
else |
else |
495 |
{ |
{ |
496 |
/* Plain old XLookupString */ |
/* Plain old XLookupString */ |
497 |
DEBUG_KBD(("No input context, using XLookupString\n")); |
DEBUG_KBD(("\nNo input context, using XLookupString\n")); |
498 |
XLookupString((XKeyEvent *) & xevent, |
XLookupString((XKeyEvent *) & xevent, |
499 |
str, sizeof(str), &keysym, NULL); |
str, sizeof(str), &keysym, NULL); |
500 |
} |
} |
501 |
|
|
|
/* FIXME needs alt modifier */ |
|
|
if (keysym == XK_Break) /* toggle full screen */ |
|
|
{ |
|
|
toggle_fullscreen(); |
|
|
break; |
|
|
} |
|
502 |
ksname = get_ksname(keysym); |
ksname = get_ksname(keysym); |
503 |
DEBUG_KBD(("\nKeyPress for (keysym 0x%lx, %s)\n", keysym, ksname)); |
DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, ksname)); |
504 |
|
|
505 |
if (inhibit_key(keysym)) |
if (handle_special_keys(keysym, ev_time, True)) |
|
{ |
|
|
DEBUG_KBD(("Inhibiting key\n")); |
|
506 |
break; |
break; |
|
} |
|
507 |
|
|
508 |
tr = xkeymap_translate_key(keysym, |
tr = xkeymap_translate_key(keysym, |
509 |
xevent.xkey.keycode, xevent.xkey.state); |
xevent.xkey.keycode, xevent.xkey.state); |
510 |
|
|
|
ensure_remote_modifiers(ev_time, tr); |
|
|
|
|
511 |
if (tr.scancode == 0) |
if (tr.scancode == 0) |
512 |
break; |
break; |
513 |
|
|
514 |
|
ensure_remote_modifiers(ev_time, tr); |
515 |
|
|
516 |
rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); |
rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); |
517 |
break; |
break; |
518 |
case KeyRelease: |
case KeyRelease: |
523 |
DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym, |
DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym, |
524 |
ksname)); |
ksname)); |
525 |
|
|
526 |
if (inhibit_key(keysym)) |
if (handle_special_keys(keysym, ev_time, False)) |
527 |
break; |
break; |
528 |
|
|
529 |
tr = xkeymap_translate_key(keysym, |
tr = xkeymap_translate_key(keysym, |
553 |
MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y); |
MOUSE_FLAG_MOVE, xevent.xmotion.x, xevent.xmotion.y); |
554 |
break; |
break; |
555 |
|
|
|
case FocusIn: |
|
|
/* fall through */ |
|
556 |
case EnterNotify: |
case EnterNotify: |
557 |
if (grab_keyboard) |
if (grab_keyboard) |
558 |
XGrabKeyboard(display, wnd, True, |
XGrabKeyboard(display, wnd, True, |
559 |
GrabModeAsync, GrabModeAsync, CurrentTime); |
GrabModeAsync, GrabModeAsync, CurrentTime); |
560 |
break; |
break; |
561 |
|
|
|
case FocusOut: |
|
|
reset_keys(); |
|
|
/* fall through */ |
|
562 |
case LeaveNotify: |
case LeaveNotify: |
563 |
if (grab_keyboard) |
if (grab_keyboard) |
564 |
XUngrabKeyboard(display, CurrentTime); |
XUngrabKeyboard(display, CurrentTime); |
565 |
break; |
break; |
566 |
|
|
567 |
|
case FocusIn: |
568 |
|
reset_modifier_keys(); |
569 |
|
break; |
570 |
|
|
571 |
case Expose: |
case Expose: |
572 |
XCopyArea(display, backstore, wnd, gc, |
XCopyArea(display, backstore, wnd, gc, |
573 |
xevent.xexpose.x, xevent.xexpose.y, |
xevent.xexpose.x, xevent.xexpose.y, |
575 |
xevent.xexpose.height, |
xevent.xexpose.height, |
576 |
xevent.xexpose.x, xevent.xexpose.y); |
xevent.xexpose.x, xevent.xexpose.y); |
577 |
break; |
break; |
578 |
|
|
579 |
|
case MappingNotify: |
580 |
|
/* Refresh keyboard mapping if it has changed. This is important for |
581 |
|
Xvnc, since it allocates keycodes dynamically */ |
582 |
|
if (xevent.xmapping.request == MappingKeyboard |
583 |
|
|| xevent.xmapping.request == MappingModifier) |
584 |
|
XRefreshKeyboardMapping(&xevent.xmapping); |
585 |
|
break; |
586 |
|
|
587 |
} |
} |
588 |
} |
} |
589 |
} |
} |
598 |
|
|
599 |
while (True) |
while (True) |
600 |
{ |
{ |
601 |
|
/* Process any events already waiting */ |
602 |
|
xwin_process_events(); |
603 |
|
|
604 |
FD_ZERO(&rfds); |
FD_ZERO(&rfds); |
605 |
FD_SET(rdp_socket, &rfds); |
FD_SET(rdp_socket, &rfds); |
606 |
if (display != NULL) |
FD_SET(x_socket, &rfds); |
|
{ |
|
|
FD_SET(x_socket, &rfds); |
|
|
XFlush(display); |
|
|
} |
|
607 |
|
|
608 |
switch (select(n, &rfds, NULL, NULL, NULL)) |
switch (select(n, &rfds, NULL, NULL, NULL)) |
609 |
{ |
{ |
614 |
continue; |
continue; |
615 |
} |
} |
616 |
|
|
|
if (FD_ISSET(x_socket, &rfds)) |
|
|
xwin_process_events(); |
|
|
|
|
617 |
if (FD_ISSET(rdp_socket, &rfds)) |
if (FD_ISSET(rdp_socket, &rfds)) |
618 |
return; |
return; |
619 |
} |
} |
632 |
Pixmap bitmap; |
Pixmap bitmap; |
633 |
uint8 *tdata; |
uint8 *tdata; |
634 |
|
|
635 |
tdata = (owncolmap ? data : translate_image(width, height, data)); |
tdata = translate_image(width, height, data); |
636 |
bitmap = XCreatePixmap(display, wnd, width, height, depth); |
bitmap = XCreatePixmap(display, wnd, width, height, depth); |
637 |
image = XCreateImage(display, visual, depth, ZPixmap, 0, |
image = XCreateImage(display, visual, depth, ZPixmap, 0, |
638 |
(char *) tdata, width, height, 8, 0); |
(char *) tdata, width, height, 8, 0); |
640 |
XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height); |
XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height); |
641 |
|
|
642 |
XFree(image); |
XFree(image); |
643 |
if (!owncolmap) |
xfree(tdata); |
|
xfree(tdata); |
|
644 |
return (HBITMAP) bitmap; |
return (HBITMAP) bitmap; |
645 |
} |
} |
646 |
|
|
650 |
XImage *image; |
XImage *image; |
651 |
uint8 *tdata; |
uint8 *tdata; |
652 |
|
|
653 |
tdata = (owncolmap ? data : translate_image(width, height, data)); |
tdata = translate_image(width, height, data); |
654 |
image = XCreateImage(display, visual, depth, ZPixmap, 0, |
image = XCreateImage(display, visual, depth, ZPixmap, 0, |
655 |
(char *) tdata, width, height, 8, 0); |
(char *) tdata, width, height, 8, 0); |
656 |
|
|
665 |
} |
} |
666 |
|
|
667 |
XFree(image); |
XFree(image); |
668 |
if (!owncolmap) |
xfree(tdata); |
|
xfree(tdata); |
|
669 |
} |
} |
670 |
|
|
671 |
void |
void |
800 |
{ |
{ |
801 |
COLOURENTRY *entry; |
COLOURENTRY *entry; |
802 |
int i, ncolours = colours->ncolours; |
int i, ncolours = colours->ncolours; |
803 |
|
uint32 *map = xmalloc(sizeof(*colmap) * ncolours); |
804 |
|
XColor xentry; |
805 |
|
XColor xc_cache[256]; |
806 |
|
uint32 colour; |
807 |
|
int colLookup = 256; |
808 |
|
for (i = 0; i < ncolours; i++) |
809 |
|
{ |
810 |
|
entry = &colours->colours[i]; |
811 |
|
MAKE_XCOLOR(&xentry, entry); |
812 |
|
|
813 |
|
if (XAllocColor(display, xcolmap, &xentry) == 0) |
814 |
|
{ |
815 |
|
/* Allocation failed, find closest match. */ |
816 |
|
int j = 256; |
817 |
|
int nMinDist = 3 * 256 * 256; |
818 |
|
long nDist = nMinDist; |
819 |
|
|
820 |
|
/* only get the colors once */ |
821 |
|
while( colLookup-- ){ |
822 |
|
xc_cache[colLookup].pixel = colLookup; |
823 |
|
xc_cache[colLookup].red = xc_cache[colLookup].green = xc_cache[colLookup].blue = 0; |
824 |
|
xc_cache[colLookup].flags = 0; |
825 |
|
XQueryColor(display, DefaultColormap(display, DefaultScreen(display)), &xc_cache[colLookup]); |
826 |
|
} |
827 |
|
colLookup = 0; |
828 |
|
|
829 |
if (owncolmap) |
/* approximate the pixel */ |
830 |
{ |
while( j-- ){ |
831 |
XColor *xcolours, *xentry; |
if( xc_cache[j].flags ){ |
832 |
Colormap map; |
nDist = |
833 |
|
((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) * |
834 |
xcolours = xmalloc(sizeof(XColor) * ncolours); |
((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) + |
835 |
for (i = 0; i < ncolours; i++) |
((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) * |
836 |
{ |
((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) + |
837 |
entry = &colours->colours[i]; |
((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8)) * |
838 |
xentry = &xcolours[i]; |
((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8)); |
839 |
xentry->pixel = i; |
} |
840 |
MAKE_XCOLOR(xentry, entry); |
if( nDist < nMinDist ){ |
841 |
|
nMinDist = nDist; |
842 |
|
xentry.pixel = j; |
843 |
|
} |
844 |
|
} |
845 |
} |
} |
846 |
|
colour = xentry.pixel; |
847 |
|
|
848 |
map = XCreateColormap(display, wnd, visual, AllocAll); |
/* update our cache */ |
849 |
XStoreColors(display, map, xcolours, ncolours); |
if( xentry.pixel < 256 ){ |
850 |
|
xc_cache[xentry.pixel].red = xentry.red; |
851 |
|
xc_cache[xentry.pixel].green = xentry.green; |
852 |
|
xc_cache[xentry.pixel].blue = xentry.blue; |
853 |
|
|
|
xfree(xcolours); |
|
|
return (HCOLOURMAP) map; |
|
|
} |
|
|
else |
|
|
{ |
|
|
uint32 *map = xmalloc(sizeof(*colmap) * ncolours); |
|
|
XColor xentry; |
|
|
uint32 colour; |
|
|
|
|
|
for (i = 0; i < ncolours; i++) |
|
|
{ |
|
|
entry = &colours->colours[i]; |
|
|
MAKE_XCOLOR(&xentry, entry); |
|
|
|
|
|
if (XAllocColor(display, xcolmap, &xentry) != 0) |
|
|
colour = xentry.pixel; |
|
|
else |
|
|
colour = white; |
|
|
|
|
|
/* byte swap here to make translate_image faster */ |
|
|
map[i] = translate_colour(colour); |
|
854 |
} |
} |
855 |
|
|
856 |
return map; |
|
857 |
|
/* byte swap here to make translate_image faster */ |
858 |
|
map[i] = translate_colour(colour); |
859 |
} |
} |
860 |
|
|
861 |
|
return map; |
862 |
} |
} |
863 |
|
|
864 |
void |
void |
865 |
ui_destroy_colourmap(HCOLOURMAP map) |
ui_destroy_colourmap(HCOLOURMAP map) |
866 |
{ |
{ |
867 |
if (owncolmap) |
xfree(map); |
|
XFreeColormap(display, (Colormap) map); |
|
|
else |
|
|
xfree(map); |
|
868 |
} |
} |
869 |
|
|
870 |
void |
void |
871 |
ui_set_colourmap(HCOLOURMAP map) |
ui_set_colourmap(HCOLOURMAP map) |
872 |
{ |
{ |
873 |
if (owncolmap) |
colmap = map; |
|
XSetWindowColormap(display, wnd, (Colormap) map); |
|
|
else |
|
|
colmap = map; |
|
874 |
} |
} |
875 |
|
|
876 |
void |
void |