33 |
extern BOOL fullscreen; |
extern BOOL fullscreen; |
34 |
extern BOOL grab_keyboard; |
extern BOOL grab_keyboard; |
35 |
extern char title[]; |
extern char title[]; |
36 |
|
BOOL enable_compose = False; |
37 |
|
|
38 |
Display *display; |
Display *display; |
39 |
static int x_socket; |
static int x_socket; |
43 |
static Visual *visual; |
static Visual *visual; |
44 |
static int depth; |
static int depth; |
45 |
static int bpp; |
static int bpp; |
46 |
|
static XIM IM; |
47 |
|
static XIC IC; |
48 |
|
static Cursor current_cursor; |
49 |
|
|
50 |
/* endianness */ |
/* endianness */ |
51 |
static BOOL host_be; |
static BOOL host_be; |
63 |
} |
} |
64 |
|
|
65 |
/* colour maps */ |
/* colour maps */ |
|
static BOOL owncolmap; |
|
66 |
static Colormap xcolmap; |
static Colormap xcolmap; |
67 |
static uint32 *colmap; |
static uint32 *colmap; |
68 |
|
|
69 |
/* Compose support */ |
#define SET_FOREGROUND(col) XSetForeground(display, gc, translate_colour(colmap[col])); |
70 |
BOOL enable_compose = False; |
#define SET_BACKGROUND(col) XSetBackground(display, gc, translate_colour(colmap[col])); |
|
static XIM IM = NULL; |
|
|
static XIC IC = NULL; |
|
|
|
|
|
/* toggle fullscreen globals */ |
|
|
static unsigned long input_mask; |
|
|
|
|
|
#define TRANSLATE(col) ( owncolmap ? col : translate_colour(colmap[col]) ) |
|
|
#define SET_FOREGROUND(col) XSetForeground(display, gc, TRANSLATE(col)); |
|
|
#define SET_BACKGROUND(col) XSetBackground(display, gc, TRANSLATE(col)); |
|
71 |
|
|
72 |
static int rop2_map[] = { |
static int rop2_map[] = { |
73 |
GXclear, /* 0 */ |
GXclear, /* 0 */ |
185 |
return colour; |
return colour; |
186 |
} |
} |
187 |
|
|
|
static unsigned long |
|
|
init_inputmethod(void) |
|
|
{ |
|
|
unsigned long filtered_events = 0; |
|
|
|
|
|
IM = XOpenIM(display, NULL, NULL, NULL); |
|
|
if (IM == NULL) |
|
|
{ |
|
|
error("Failed to open input method\n"); |
|
|
} |
|
|
|
|
|
if (IM != NULL) |
|
|
{ |
|
|
/* Must be done after XCreateWindow */ |
|
|
IC = XCreateIC(IM, XNInputStyle, |
|
|
(XIMPreeditNothing | XIMStatusNothing), |
|
|
XNClientWindow, wnd, XNFocusWindow, wnd, NULL); |
|
|
|
|
|
if (IC == NULL) |
|
|
{ |
|
|
error("Failed to create input context\n"); |
|
|
XCloseIM(IM); |
|
|
IM = NULL; |
|
|
} |
|
|
} |
|
|
|
|
|
/* For correct Multi_key/Compose processing, I guess. |
|
|
It seems to work alright anyway, though. */ |
|
|
if (IC != NULL) |
|
|
{ |
|
|
if (XGetICValues(IC, XNFilterEvents, &filtered_events, NULL) != NULL) |
|
|
{ |
|
|
error("Failed to obtain XNFilterEvents value from IC\n"); |
|
|
filtered_events = 0; |
|
|
} |
|
|
} |
|
|
return filtered_events; |
|
|
} |
|
|
|
|
|
static void |
|
|
close_inputmethod(void) |
|
|
{ |
|
|
if (IC != NULL) |
|
|
{ |
|
|
XDestroyIC(IC); |
|
|
if (IM != NULL) |
|
|
{ |
|
|
XCloseIM(IM); |
|
|
IM = NULL; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
188 |
BOOL |
BOOL |
189 |
get_key_state(int keysym) |
get_key_state(int keysym) |
190 |
{ |
{ |
219 |
return (current_state & keysymMask) ? True : False; |
return (current_state & keysymMask) ? True : False; |
220 |
} |
} |
221 |
|
|
|
static void |
|
|
xwin_map_window() |
|
|
{ |
|
|
XEvent xevent; |
|
|
|
|
|
XMapWindow(display, wnd); |
|
|
|
|
|
/* wait for VisibilityChange */ |
|
|
XMaskEvent(display, VisibilityChangeMask, &xevent); |
|
|
|
|
|
if (fullscreen) |
|
|
XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime); |
|
|
} |
|
|
|
|
222 |
BOOL |
BOOL |
223 |
ui_init() |
ui_init() |
224 |
{ |
{ |
260 |
return False; |
return False; |
261 |
} |
} |
262 |
|
|
263 |
if (depth <= 8) |
xcolmap = DefaultColormapOfScreen(screen); |
264 |
owncolmap = True; |
gc = XCreateGC(display, RootWindowOfScreen(screen), 0, NULL); |
|
else |
|
|
xcolmap = DefaultColormapOfScreen(screen); |
|
265 |
|
|
266 |
if (DoesBackingStore(screen) == NotUseful) |
if (DoesBackingStore(screen) != Always) |
267 |
ownbackstore = True; |
ownbackstore = True; |
268 |
|
|
269 |
test = 1; |
test = 1; |
279 |
/* make sure width is a multiple of 4 */ |
/* make sure width is a multiple of 4 */ |
280 |
width = (width + 3) & ~3; |
width = (width + 3) & ~3; |
281 |
|
|
282 |
|
if (ownbackstore) |
283 |
|
{ |
284 |
|
backstore = XCreatePixmap(display, RootWindowOfScreen(screen), width, height, depth); |
285 |
|
|
286 |
|
/* clear to prevent rubbish being exposed at startup */ |
287 |
|
XSetForeground(display, gc, BlackPixelOfScreen(screen)); |
288 |
|
XFillRectangle(display, backstore, gc, 0, 0, width, height); |
289 |
|
} |
290 |
|
|
291 |
|
if (enable_compose) |
292 |
|
IM = XOpenIM(display, NULL, NULL, NULL); |
293 |
|
|
294 |
xkeymap_init(); |
xkeymap_init(); |
295 |
return True; |
return True; |
296 |
} |
} |
297 |
|
|
298 |
|
void |
299 |
|
ui_deinit() |
300 |
|
{ |
301 |
|
if (IM != NULL) |
302 |
|
XCloseIM(IM); |
303 |
|
|
304 |
|
if (ownbackstore) |
305 |
|
XFreePixmap(display, backstore); |
306 |
|
|
307 |
|
XFreeGC(display, gc); |
308 |
|
XCloseDisplay(display); |
309 |
|
display = NULL; |
310 |
|
} |
311 |
|
|
312 |
BOOL |
BOOL |
313 |
ui_create_window() |
ui_create_window() |
314 |
{ |
{ |
315 |
XSetWindowAttributes attribs; |
XSetWindowAttributes attribs; |
316 |
XClassHint *classhints; |
XClassHint *classhints; |
317 |
XSizeHints *sizehints; |
XSizeHints *sizehints; |
318 |
|
int wndwidth, wndheight; |
319 |
|
long input_mask, ic_input_mask; |
320 |
XEvent xevent; |
XEvent xevent; |
321 |
|
|
322 |
|
wndwidth = fullscreen ? WidthOfScreen(screen) : width; |
323 |
|
wndheight = fullscreen ? HeightOfScreen(screen) : height; |
324 |
|
|
325 |
attribs.background_pixel = BlackPixelOfScreen(screen); |
attribs.background_pixel = BlackPixelOfScreen(screen); |
326 |
attribs.backing_store = ownbackstore ? NotUseful : Always; |
attribs.backing_store = ownbackstore ? NotUseful : Always; |
327 |
attribs.override_redirect = fullscreen; |
attribs.override_redirect = fullscreen; |
328 |
wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, width, height, |
|
329 |
|
wnd = XCreateWindow(display, RootWindowOfScreen(screen), 0, 0, wndwidth, wndheight, |
330 |
0, CopyFromParent, InputOutput, CopyFromParent, |
0, CopyFromParent, InputOutput, CopyFromParent, |
331 |
CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs); |
CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs); |
332 |
|
|
|
if (ownbackstore) |
|
|
backstore = XCreatePixmap(display, wnd, width, height, depth); |
|
|
|
|
333 |
XStoreName(display, wnd, title); |
XStoreName(display, wnd, title); |
334 |
|
|
335 |
classhints = XAllocClassHint(); |
classhints = XAllocClassHint(); |
351 |
} |
} |
352 |
|
|
353 |
input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | |
input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | |
354 |
VisibilityChangeMask | FocusChangeMask; |
StructureNotifyMask | FocusChangeMask; |
355 |
|
|
356 |
if (grab_keyboard) |
if (grab_keyboard) |
357 |
input_mask |= EnterWindowMask | LeaveWindowMask; |
input_mask |= EnterWindowMask | LeaveWindowMask; |
359 |
input_mask |= PointerMotionMask; |
input_mask |= PointerMotionMask; |
360 |
if (ownbackstore) |
if (ownbackstore) |
361 |
input_mask |= ExposureMask; |
input_mask |= ExposureMask; |
362 |
if (enable_compose) |
|
363 |
input_mask |= init_inputmethod(); |
if (IM != NULL) |
364 |
|
{ |
365 |
|
IC = XCreateIC(IM, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing), |
366 |
|
XNClientWindow, wnd, XNFocusWindow, wnd, NULL); |
367 |
|
|
368 |
|
if ((IC != NULL) && (XGetICValues(IC, XNFilterEvents, &ic_input_mask, NULL) == NULL)) |
369 |
|
input_mask |= ic_input_mask; |
370 |
|
} |
371 |
|
|
372 |
XSelectInput(display, wnd, input_mask); |
XSelectInput(display, wnd, input_mask); |
373 |
|
XMapWindow(display, wnd); |
374 |
|
|
375 |
xwin_map_window(); |
/* wait for MapNotify */ |
376 |
|
do { |
377 |
|
XMaskEvent(display, StructureNotifyMask, &xevent); |
378 |
|
} while (xevent.type != MapNotify); |
379 |
|
|
380 |
/* clear the window so that cached data is not seen */ |
if (fullscreen) |
381 |
gc = XCreateGC(display, wnd, 0, NULL); |
XSetInputFocus(display, wnd, RevertToPointerRoot, CurrentTime); |
|
XSetForeground(display, gc, 0); |
|
|
FILL_RECTANGLE(0, 0, width, height); |
|
382 |
|
|
383 |
return True; |
return True; |
384 |
} |
} |
386 |
void |
void |
387 |
ui_destroy_window() |
ui_destroy_window() |
388 |
{ |
{ |
389 |
if (ownbackstore) |
if (IC != NULL) |
390 |
XFreePixmap(display, backstore); |
XDestroyIC(IC); |
|
|
|
|
XFreeGC(display, gc); |
|
|
|
|
|
close_inputmethod(); |
|
391 |
|
|
392 |
XDestroyWindow(display, wnd); |
XDestroyWindow(display, wnd); |
|
XCloseDisplay(display); |
|
|
display = NULL; |
|
393 |
} |
} |
394 |
|
|
|
|
|
395 |
void |
void |
396 |
xwin_toggle_fullscreen() |
xwin_toggle_fullscreen() |
397 |
{ |
{ |
398 |
XEvent xevent; |
Pixmap contents = 0; |
|
XSetWindowAttributes attribs; |
|
|
int newwidth, newheight; |
|
399 |
|
|
400 |
|
if (!ownbackstore) |
401 |
|
{ |
402 |
|
/* need to save contents of window */ |
403 |
|
contents = XCreatePixmap(display, wnd, width, height, depth); |
404 |
|
XCopyArea(display, wnd, contents, gc, 0, 0, width, height, 0, 0); |
405 |
|
} |
406 |
|
|
407 |
|
ui_destroy_window(); |
408 |
fullscreen = !fullscreen; |
fullscreen = !fullscreen; |
409 |
newwidth = fullscreen ? WidthOfScreen(screen) : width; |
ui_create_window(); |
|
newheight = fullscreen ? HeightOfScreen(screen) : height; |
|
410 |
|
|
411 |
XUnmapWindow(display, wnd); |
XDefineCursor(display, wnd, current_cursor); |
412 |
attribs.override_redirect = fullscreen; |
|
413 |
XMoveResizeWindow(display, wnd, 0, 0, newwidth, newheight); |
if (!ownbackstore) |
414 |
XChangeWindowAttributes(display, wnd, CWOverrideRedirect, &attribs); |
{ |
415 |
xwin_map_window(); |
XCopyArea(display, contents, wnd, gc, 0, 0, width, height, 0, 0); |
416 |
|
XFreePixmap(display, contents); |
417 |
|
} |
418 |
} |
} |
419 |
|
|
420 |
/* Process all events in Xlib queue */ |
/* Process all events in Xlib queue */ |
434 |
{ |
{ |
435 |
XNextEvent(display, &xevent); |
XNextEvent(display, &xevent); |
436 |
|
|
437 |
if (enable_compose && (XFilterEvent(&xevent, None) == True)) |
if ((IC != NULL) && (XFilterEvent(&xevent, None) == True)) |
438 |
{ |
{ |
439 |
DEBUG_KBD(("Filtering event\n")); |
DEBUG_KBD(("Filtering event\n")); |
440 |
continue; |
continue; |
600 |
Pixmap bitmap; |
Pixmap bitmap; |
601 |
uint8 *tdata; |
uint8 *tdata; |
602 |
|
|
603 |
tdata = (owncolmap ? data : translate_image(width, height, data)); |
tdata = translate_image(width, height, data); |
604 |
bitmap = XCreatePixmap(display, wnd, width, height, depth); |
bitmap = XCreatePixmap(display, wnd, width, height, depth); |
605 |
image = XCreateImage(display, visual, depth, ZPixmap, 0, |
image = XCreateImage(display, visual, depth, ZPixmap, 0, |
606 |
(char *) tdata, width, height, 8, 0); |
(char *) tdata, width, height, 8, 0); |
608 |
XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height); |
XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height); |
609 |
|
|
610 |
XFree(image); |
XFree(image); |
611 |
if (!owncolmap) |
xfree(tdata); |
|
xfree(tdata); |
|
612 |
return (HBITMAP) bitmap; |
return (HBITMAP) bitmap; |
613 |
} |
} |
614 |
|
|
618 |
XImage *image; |
XImage *image; |
619 |
uint8 *tdata; |
uint8 *tdata; |
620 |
|
|
621 |
tdata = (owncolmap ? data : translate_image(width, height, data)); |
tdata = translate_image(width, height, data); |
622 |
image = XCreateImage(display, visual, depth, ZPixmap, 0, |
image = XCreateImage(display, visual, depth, ZPixmap, 0, |
623 |
(char *) tdata, width, height, 8, 0); |
(char *) tdata, width, height, 8, 0); |
624 |
|
|
633 |
} |
} |
634 |
|
|
635 |
XFree(image); |
XFree(image); |
636 |
if (!owncolmap) |
xfree(tdata); |
|
xfree(tdata); |
|
637 |
} |
} |
638 |
|
|
639 |
void |
void |
748 |
void |
void |
749 |
ui_set_cursor(HCURSOR cursor) |
ui_set_cursor(HCURSOR cursor) |
750 |
{ |
{ |
751 |
XDefineCursor(display, wnd, (Cursor) cursor); |
current_cursor = (Cursor) cursor; |
752 |
|
XDefineCursor(display, wnd, current_cursor); |
753 |
} |
} |
754 |
|
|
755 |
void |
void |
769 |
{ |
{ |
770 |
COLOURENTRY *entry; |
COLOURENTRY *entry; |
771 |
int i, ncolours = colours->ncolours; |
int i, ncolours = colours->ncolours; |
772 |
|
uint32 *map = xmalloc(sizeof(*colmap) * ncolours); |
773 |
|
XColor xentry; |
774 |
|
XColor xc_cache[256]; |
775 |
|
uint32 colour; |
776 |
|
int colLookup = 256; |
777 |
|
for (i = 0; i < ncolours; i++) |
778 |
|
{ |
779 |
|
entry = &colours->colours[i]; |
780 |
|
MAKE_XCOLOR(&xentry, entry); |
781 |
|
|
782 |
|
if (XAllocColor(display, xcolmap, &xentry) == 0) |
783 |
|
{ |
784 |
|
/* Allocation failed, find closest match. */ |
785 |
|
int j = 256; |
786 |
|
int nMinDist = 3 * 256 * 256; |
787 |
|
long nDist = nMinDist; |
788 |
|
|
789 |
|
/* only get the colors once */ |
790 |
|
while( colLookup-- ){ |
791 |
|
xc_cache[colLookup].pixel = colLookup; |
792 |
|
xc_cache[colLookup].red = xc_cache[colLookup].green = xc_cache[colLookup].blue = 0; |
793 |
|
xc_cache[colLookup].flags = 0; |
794 |
|
XQueryColor(display, DefaultColormap(display, DefaultScreen(display)), &xc_cache[colLookup]); |
795 |
|
} |
796 |
|
colLookup = 0; |
797 |
|
|
798 |
if (owncolmap) |
/* approximate the pixel */ |
799 |
{ |
while( j-- ){ |
800 |
XColor *xcolours, *xentry; |
if( xc_cache[j].flags ){ |
801 |
Colormap map; |
nDist = |
802 |
|
((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) * |
803 |
xcolours = xmalloc(sizeof(XColor) * ncolours); |
((long) (xc_cache[j].red >> 8) - (long) (xentry.red >> 8)) + |
804 |
for (i = 0; i < ncolours; i++) |
((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) * |
805 |
{ |
((long) (xc_cache[j].green >> 8) - (long) (xentry.green >> 8)) + |
806 |
entry = &colours->colours[i]; |
((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8)) * |
807 |
xentry = &xcolours[i]; |
((long) (xc_cache[j].blue >> 8) - (long) (xentry.blue >> 8)); |
808 |
xentry->pixel = i; |
} |
809 |
MAKE_XCOLOR(xentry, entry); |
if( nDist < nMinDist ){ |
810 |
|
nMinDist = nDist; |
811 |
|
xentry.pixel = j; |
812 |
|
} |
813 |
|
} |
814 |
} |
} |
815 |
|
colour = xentry.pixel; |
816 |
|
|
817 |
map = XCreateColormap(display, wnd, visual, AllocAll); |
/* update our cache */ |
818 |
XStoreColors(display, map, xcolours, ncolours); |
if( xentry.pixel < 256 ){ |
819 |
|
xc_cache[xentry.pixel].red = xentry.red; |
820 |
xfree(xcolours); |
xc_cache[xentry.pixel].green = xentry.green; |
821 |
return (HCOLOURMAP) map; |
xc_cache[xentry.pixel].blue = xentry.blue; |
|
} |
|
|
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 = WhitePixelOfScreen(screen); |
|
822 |
|
|
|
/* byte swap here to make translate_image faster */ |
|
|
map[i] = translate_colour(colour); |
|
823 |
} |
} |
824 |
|
|
825 |
return map; |
|
826 |
|
/* byte swap here to make translate_image faster */ |
827 |
|
map[i] = translate_colour(colour); |
828 |
} |
} |
829 |
|
|
830 |
|
return map; |
831 |
} |
} |
832 |
|
|
833 |
void |
void |
834 |
ui_destroy_colourmap(HCOLOURMAP map) |
ui_destroy_colourmap(HCOLOURMAP map) |
835 |
{ |
{ |
836 |
if (owncolmap) |
xfree(map); |
|
XFreeColormap(display, (Colormap) map); |
|
|
else |
|
|
xfree(map); |
|
837 |
} |
} |
838 |
|
|
839 |
void |
void |
840 |
ui_set_colourmap(HCOLOURMAP map) |
ui_set_colourmap(HCOLOURMAP map) |
841 |
{ |
{ |
842 |
if (owncolmap) |
colmap = map; |
|
XSetWindowColormap(display, wnd, (Colormap) map); |
|
|
else |
|
|
colmap = map; |
|
843 |
} |
} |
844 |
|
|
845 |
void |
void |