1 namespace gui::drivers;
4 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
5 import "OpenGLDisplayDriver"
8 #if (defined(__unix__) || defined(__APPLE__)) && !defined(ECERE_MINIGLX)
13 #define property _property
18 #include <linux/joystick.h>
20 #include <sys/param.h>
23 #include <sys/select.h>
29 #define Window X11Window
30 #define Cursor X11Cursor
32 #define Display X11Display
34 #define KeyCode X11KeyCode
35 #define Picture X11Picture
37 #include <X11/Xatom.h>
39 #include <X11/Xresource.h>
40 #include <X11/Xutil.h>
41 #include <X11/XKBlib.h>
42 #include <X11/keysym.h>
43 #include <X11/cursorfont.h>
45 #if !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
48 #include <X11/extensions/Xrender.h>
49 #include <X11/extensions/XShm.h>
66 import "XDisplayDriver"
68 // These are the events we care about
69 #define EVENT_MASK StructureNotifyMask | \
70 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | \
71 KeyPressMask | KeyReleaseMask | ExposureMask | FocusChangeMask | \
74 void * xGlobalDisplay;
77 static XContext windowContext;
78 static Window lastActive;
80 static char * clipBoardData;
82 static Thread /* xThread, */ timerThread;
83 static bool xTerminate;
84 static Semaphore xSemaphore { };
85 static Mutex xMutex { };
86 static bool fullScreenMode;
87 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
88 X11Window confineWindow;
89 static X11Cursor nullCursor;
90 static X11Window capturedWindow = None;
91 static Window restrictedWindow = null;
92 static bool gotAnXEvent = false;
94 static int joystickFD[4];
95 static X11Window activeWindow;
96 static X11Cursor systemCursors[SystemCursor];
98 static enum AtomIdents
100 clipboard, multiple, targets, utf8_string, wm_delete_window, wm_hints, wm_name, wm_protocols, wm_state, wm_take_focus, wm_transient_for,
102 _net_active_window, _net_current_desktop, _net_number_of_desktops, _net_wm_icon, _net_wm_name, _net_wm_pid,
103 _net_wm_state, _net_wm_state_demands_attention, _net_wm_state_hidden, _net_wm_user_time, _net_wm_window_type,
104 _net_wm_window_type_desktop, _net_wm_window_type_dialog, _net_wm_window_type_dock, _net_wm_window_type_dropdown_menu,
105 _net_wm_window_type_menu, _net_wm_window_type_normal, _net_wm_window_type_popup_menu, _net_wm_window_type_splash,
106 _net_wm_window_type_toolbar, _net_wm_window_type_utility, _net_workarea, _net_frame_extents, _net_request_frame_extents,
107 _net_wm_state_maximized_vert, _net_wm_state_maximized_horz, _net_wm_state_modal, app_selection, _net_supported
110 static Atom atoms[AtomIdents];
111 static bool atomsSupported[AtomIdents];
113 static const char *atomNames[AtomIdents] = {
114 "CLIPBOARD", //clipboard
115 "MULTIPLE", //multiple
117 "UTF8_STRING", //utf8_string
118 "WM_DELETE_WINDOW", //wm_delete_window
119 "WM_HINTS", //wm_hints
121 "WM_PROTOCOLS", //wm_protocols
122 "WM_STATE", //wm_state
123 "WM_TAKE_FOCUS", //wm_take_focus
124 "WM_TRANSIENT_FOR", //wm_transient_for
125 "_MOTIF_WM_HINTS", //_motif_wm_hints
126 "_NET_ACTIVE_WINDOW", //_net_active_window
127 "_NET_CURRENT_DESKTOP", //_net_current_desktop
128 "_NET_NUMBER_OF_DESKTOPS", //_net_number_of_desktops
129 "_NET_WM_ICON", //_net_wm_icon
130 "_NET_WM_NAME", //_net_wm_name
131 "_NET_WM_PID", //_net_wm_pid
132 "_NET_WM_STATE", //_net_wm_state
133 "_NET_WM_STATE_DEMANDS_ATTENTION", //_net_wm_state_demands_attention
134 "_NET_WM_STATE_HIDDEN", //_net_wm_state_hidden
135 "_NET_WM_USER_TIME", //_net_wm_user_time
136 "_NET_WM_WINDOW_TYPE", //_net_wm_window_type
137 "_NET_WM_WINDOW_TYPE_DESKTOP", //_net_wm_window_type_desktop
138 "_NET_WM_WINDOW_TYPE_DIALOG", //_net_wm_window_type_dialog
139 "_NET_WM_WINDOW_TYPE_DOCK", //_net_wm_window_type_dock
140 "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", //_net_wm_window_type_dropdown_menu
141 "_NET_WM_WINDOW_TYPE_MENU", //_net_wm_window_type_menu
142 "_NET_WM_WINDOW_TYPE_NORMAL", //_net_wm_window_type_normal
143 "_NET_WM_WINDOW_TYPE_POPUP_MENU", //_net_wm_window_type_popup_menu
144 "_NET_WM_WINDOW_TYPE_SPLASH", //_net_wm_window_type_splash
145 "_NET_WM_WINDOW_TYPE_TOOLBAR", //_net_wm_window_type_toolbar
146 "_NET_WM_WINDOW_TYPE_UTILITY", //_net_wm_window_type_utility
147 "_NET_WORKAREA", //_net_workarea
148 "_NET_FRAME_EXTENTS", // _net_frame_extents
149 "_NET_REQUEST_FRAME_EXTENTS", // _net_request_frame_extents
150 "_NET_WM_STATE_MAXIMIZED_VERT", // _net_wm_state_maximized_vert
151 "_NET_WM_STATE_MAXIMIZED_HORZ", // _net_wm_state_maximized_horz
152 "_NET_WM_STATE_MODAL", // _net_wm_state_modal
157 _NET_WM_STATE_STICKY, ATOM
158 _NET_WM_STATE_MAXIMIZED_VERT, ATOM
159 _NET_WM_STATE_MAXIMIZED_HORZ, ATOM
160 _NET_WM_STATE_SHADED, ATOM
161 _NET_WM_STATE_SKIP_TASKBAR, ATOM
162 _NET_WM_STATE_SKIP_PAGER, ATOM
163 _NET_WM_STATE_HIDDEN, ATOM
164 _NET_WM_STATE_FULLSCREEN, ATOM
165 _NET_WM_STATE_ABOVE, ATOM
166 _NET_WM_STATE_BELOW, ATOM
167 _NET_WM_STATE_DEMANDS_ATTENTION, ATOM
170 static bool autoRepeatDetectable;
171 static bool setICPosition;
173 PixelFormat xSystemPixelFormat;
174 Visual * xSystemVisual;
177 static Time timeStamp;
179 class XWindowData : struct
182 XVisualInfo * visual;
187 bool gotFrameExtents;
190 bool XGetBorderWidths(Window window, Box box)
192 XWindowData windowData = window.windowData;
195 if(window.state == maximized)
196 box = { 0, 0, 0, 0 };
198 box = windowData.decor;
204 static Visual * FindFullColorVisual(X11Display *dpy, int * depth)
207 XVisualInfo *vinfo_ret;
210 vinfo._class = TrueColor;
211 vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
217 if(vinfo_ret[numitems-1].depth > maxdepth)
218 maxdepth = vinfo_ret[numitems-1].depth;
221 XFree((void *) vinfo_ret);
224 if(XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, TrueColor, &vinfo))
234 static void RepositionDesktop(bool updateChildren)
238 Screen * x_screen = XDefaultScreenOfDisplay(xGlobalDisplay);
243 unsigned long len, fill;
245 static double lastTime = 0, time;
248 if(desktopW && desktopH && time - lastTime < 1.5) return;
251 w = XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
252 h = XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
253 x_root = XRootWindowOfScreen(x_screen);
255 if(atomsSupported[_net_number_of_desktops])
257 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_number_of_desktops], 0, 1, False,
258 XA_CARDINAL, &type, &format, &len, &fill,
261 printf("cant get xa desktops property\n");
267 desktops = (int)*(long *)data;
269 //printf("_NET_NUMBER_OF_DESKTOPS is %d\n", desktops);
276 if(atomsSupported[_net_current_desktop])
278 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_current_desktop], 0, 1, False,
279 XA_CARDINAL, &type, &format, &len, &fill,
282 printf("cant get xa current property\n");
287 current = (int)*(long *)data;
291 //printf("_NET_CURRENT_DESKTOP is %d\n", current);
294 if(atomsSupported[_net_workarea])
298 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_workarea], 0, (4 * 32),
299 False, AnyPropertyType, &type, &format, &len,
300 &fill, &data) != Success)
302 //printf("warning\n");
306 if(type == None || format == 0)
318 workareas = (long *)data;
320 x = (int)workareas[current * 4];
321 y = (int)workareas[current * 4 + 1];
322 w = (int)workareas[current * 4 + 2];
323 h = (int)workareas[current * 4 + 3];
325 //printf("_NET_WORKAREA is x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
329 // printf("Work Area width: %d, height %d\n", w, h);
332 if(desktopX != x || desktopY != y || desktopW != w || desktopH != h)
334 guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
342 /****************************************************************************
343 /// PRIVATE UTILITY FUNCTIONS /////////////
344 ****************************************************************************/
345 // --- Keyboard Input ---
346 #define KEYCODE_HOME 0x59
347 #define KEYCODE_UP 0x5A
348 #define KEYCODE_PGUP 0x5B
349 #define KEYCODE_LEFT 0x5C
350 #define KEYCODE_RIGHT 0x5E
351 #define KEYCODE_END 0x5F
352 #define KEYCODE_DOWN 0x60
353 #define KEYCODE_PGDWN 0x61
354 #define KEYCODE_INS 0x62
355 #define KEYCODE_DEL 0x63
356 #define KEYCODE_SLASH 0x68
359 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
360 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
361 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
362 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
363 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
364 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
365 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
366 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
367 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
368 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
369 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
370 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
371 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
372 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
375 static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEvent * event)
380 KeySym keysym = NoSymbol;
383 static long bufsize = 16;
384 static char *buf = NULL;
385 XWindowData windowData = window.windowData;
388 if(window.destroyed) return result;
390 // Logf("Got 0x%x (%d)\n", keyCode, key);
392 // Translate the key code
395 //Logf("Got 0x%x (%d)\n", key, key);
399 case KEYCODE_HOME: key = home; break;
400 case KEYCODE_UP: key = up; break;
401 case KEYCODE_PGUP: key = pageUp; break;
402 case KEYCODE_LEFT: key = left; break;
403 case KEYCODE_RIGHT: key = right; break;
404 case KEYCODE_END: key = end; break;
405 case KEYCODE_DOWN: key = down; break;
406 case KEYCODE_PGDWN: key = pageDown; break;
407 case KEYCODE_INS: key = insert; break;
408 case KEYCODE_DEL: key = del; break;
409 case KEYCODE_SLASH: key = keyPadSlash; break;
412 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
416 Logf("Key up: %3d (%2x)\n", key, key);
418 Logf("Key down: %3d (%2x)\n", key, key);
422 buf = malloc((uint)bufsize);
423 if(windowData && windowData.ic)
425 buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
426 if (status == XBufferOverflow)
428 buf = realloc(buf, (uint)(bufsize = buflength));
429 buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
431 if(status != XLookupKeySym && status != XLookupBoth && release == 1)
432 keysym = XLookupKeysym(event, 0);
436 keysym = XLookupKeysym(event, 0);
439 // printf("Got keysym %d (%x)\n", keysym, keysym);
440 if(keysym != NoSymbol) //status == XLookupKeySym || status == XLookupBoth)
444 case XK_BackSpace: key = backSpace; break;
445 case XK_ISO_Left_Tab: key = tab; break;
446 case XK_Tab: key = tab; break;
449 case XK_Return: key = enter; break;
450 case XK_Pause: key = pauseBreak; break;
451 case XK_Scroll_Lock: key = scrollLock; break;
452 case XK_Sys_Req: key = printScreen; break;
453 case XK_Escape: key = escape; break;
454 case XK_Delete: key = del; break;
456 case XK_Home: key = home; break;
457 case XK_Left: key = left; break;
458 case XK_Up: key = up; break;
459 case XK_Right: key = right; break;
460 case XK_Down: key = down; break;
462 case XK_Page_Up: key = pageUp; break;
464 case XK_Page_Down: key = pageDown; break;
465 case XK_End: key = end; break;
471 case XK_Insert: key = insert; break;
478 case XK_Help: key = insert; break;
480 case XK_Break: key = Key { pauseBreak, ctrl = true }; break;
482 case XK_Mode_switch: key = leftAlt; break;
484 // case XK_script_switch:
485 case XK_Num_Lock: key = numLock; break;
489 case XK_KP_Enter: key = keyPadEnter; break;
494 case XK_KP_Home: key = keyPadHome; break;
495 case XK_KP_Left: key = keyPadLeft; break;
496 case XK_KP_Up: key = keyPadUp; break;
497 case XK_KP_Right: key = keyPadRight; break;
498 case XK_KP_Down: key = keyPadDown; break;
500 case XK_KP_Page_Up: key = keyPadPageUp; break;
502 case XK_KP_Page_Down:key = keyPadPageDown; break;
503 case XK_KP_End: key = keyPadEnd; break;
505 case XK_KP_Insert: key = keyPadInsert; break;
506 case XK_KP_Delete: key = keyPadDelete; break;
508 case XK_KP_Multiply: key = keyPadStar; break;
509 case XK_KP_Add: key = keyPadPlus; break;
510 case XK_KP_Separator:key = keyPadDelete; break;
511 case XK_KP_Subtract: key = keyPadMinus; break;
512 // case XK_KP_Decimal:
513 case XK_KP_Divide: key = keyPadSlash; break;
515 case XK_KP_0: key = keyPad0; break;
516 case XK_KP_1: key = keyPad1; break;
517 case XK_KP_2: key = keyPad2; break;
518 case XK_KP_3: key = keyPad3; break;
519 case XK_KP_4: key = keyPad4; break;
520 case XK_KP_5: key = keyPad5; break;
521 case XK_KP_6: key = keyPad6; break;
522 case XK_KP_7: key = keyPad7; break;
523 case XK_KP_8: key = keyPad8; break;
524 case XK_KP_9: key = keyPad9; break;
526 case XK_F1: key = f1; break;
527 case XK_F2: key = f2; break;
528 case XK_F3: key = f3; break;
529 case XK_F4: key = f4; break;
530 case XK_F5: key = f5; break;
531 case XK_F6: key = f6; break;
532 case XK_F7: key = f7; break;
533 case XK_F8: key = f8; break;
534 case XK_F9: key = f9; break;
535 case XK_F10: key = f10; break;
536 case XK_F11: key = f11; break;
538 case XK_F12: key = f12; break;
588 case XK_Shift_L: key = leftShift; break;
589 case XK_Shift_R: key = rightShift; break;
590 case XK_Control_L: key = leftControl; break;
591 case XK_Control_R: key = rightControl; break;
592 case XK_Caps_Lock: key = capsLock; break;
593 // case XK_Shift_Lock:
596 case XK_Alt_L: key = leftAlt; break;
597 case XK_Alt_R: key = rightAlt; break;
603 case XK_space: key = space; break;
604 case XK_exclam: key = bang; break;
605 case XK_quotedbl: key = doubleQuote; break;
606 case XK_numbersign: key = pound; break;
607 case XK_dollar: key = dollar; break;
608 case XK_percent: key = percent; break;
609 case XK_ampersand: key = ampersand; break;
610 case XK_apostrophe: key = quote; break;
611 // case XK_quoteright: key = quote; break;
612 case XK_parenleft: key = leftParanthesis; break;
613 case XK_parenright: key = rightParanthesis; break;
614 case XK_asterisk: key = star; break;
615 case XK_plus: key = plus; break;
616 case XK_comma: key = comma; break;
617 case XK_minus: key = minus; break;
618 case XK_period: key = period; break;
619 case XK_slash: key = slash; break;
620 case XK_0: key = k0; break;
621 case XK_1: key = k1; break;
622 case XK_2: key = k2; break;
623 case XK_3: key = k3; break;
624 case XK_4: key = k4; break;
625 case XK_5: key = k5; break;
626 case XK_6: key = k6; break;
627 case XK_7: key = k7; break;
628 case XK_8: key = k8; break;
629 case XK_9: key = k9; break;
630 case XK_colon: key = colon; break;
631 case XK_semicolon: key = semicolon; break;
632 case XK_less: key = smallerThan; break;
633 case XK_equal: key = equal; break;
634 case XK_greater: key = greaterThan; break;
635 case XK_question: key = questionMark; break;
636 case XK_at: key = ampersand; break;
637 case XK_A: key = a; break;
638 case XK_B: key = b; break;
639 case XK_C: key = c; break;
640 case XK_D: key = d; break;
641 case XK_E: key = e; break;
642 case XK_F: key = f; break;
643 case XK_G: key = g; break;
644 case XK_H: key = h; break;
645 case XK_I: key = i; break;
646 case XK_J: key = j; break;
647 case XK_K: key = k; break;
648 case XK_L: key = l; break;
649 case XK_M: key = m; break;
650 case XK_N: key = n; break;
651 case XK_O: key = o; break;
652 case XK_P: key = p; break;
653 case XK_Q: key = q; break;
654 case XK_R: key = r; break;
655 case XK_S: key = s; break;
656 case XK_T: key = t; break;
657 case XK_U: key = u; break;
658 case XK_V: key = v; break;
659 case XK_W: key = w; break;
660 case XK_X: key = x; break;
661 case XK_Y: key = y; break;
662 case XK_Z: key = z; break;
663 case XK_bracketleft: key = leftBracket; break;
664 case XK_backslash: key = backSlash; break;
665 case XK_bracketright: key = rightBracket; break;
666 case XK_asciicircum: key = circumflex; break;
667 case XK_underscore: key = underscore; break;
668 // case XK_grave: key = backQuote; break;
669 case XK_quoteleft: key = backQuote; break;
670 case XK_a: key = a; break;
671 case XK_b: key = b; break;
672 case XK_c: key = c; break;
673 case XK_d: key = d; break;
674 case XK_e: key = e; break;
675 case XK_f: key = f; break;
676 case XK_g: key = g; break;
677 case XK_h: key = h; break;
678 case XK_i: key = i; break;
679 case XK_j: key = j; break;
680 case XK_k: key = k; break;
681 case XK_l: key = l; break;
682 case XK_m: key = m; break;
683 case XK_n: key = n; break;
684 case XK_o: key = o; break;
685 case XK_p: key = p; break;
686 case XK_q: key = q; break;
687 case XK_r: key = r; break;
688 case XK_s: key = s; break;
689 case XK_t: key = t; break;
690 case XK_u: key = u; break;
691 case XK_v: key = v; break;
692 case XK_w: key = w; break;
693 case XK_x: key = x; break;
694 case XK_y: key = y; break;
695 case XK_z: key = z; break;
696 case XK_braceleft: key = leftBracket; break;
697 case XK_bar: key = pipe; break;
698 case XK_braceright: key = rightBracket; break;
699 case XK_asciitilde: key = tilde; break;
704 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
705 // 127 is delete, we don't treat that as a character (Use (SmartKey)key == del)
706 if(ch == 128 || ch == 127) ch = 0;
709 if(keysym == XK_ISO_Left_Tab)
711 if(key != leftShift && key != rightShift && event->state & ShiftMask)
713 if(key != leftControl && key != rightControl && event->state & ControlMask)
715 if(key != leftAlt && key != rightAlt && event->state & Mod1Mask)
719 if(key != leftAlt && key != rightAlt && event->state & (1<<13))
727 // Logf("Key Message: %s, keysym: 0x%x, key: %d state: %d, ch: %c\n", release ? ((release == 2) ? "REPEAT" : "KeyRelease") : "KeyPress", keysym, key, event->state, (byte)ch);
733 if(windowData && windowData.ic) ch = buflength ? UTF8GetChar(buf, &numBytes) : 0;
734 if(ch == 127) ch = 0;
735 // printf("Release! %d %d %d\n", keysym, code, ch);
736 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, code, ch);
743 if(windowData.ic && buflength)
745 for(c = 0; c<buflength;)
748 ch = UTF8GetChar(buf + c, &numBytes);
749 if(ch == 127) ch = 0;
750 result = window.KeyMessage((c == 0) ?
751 __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown : __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit,
752 (c == 0) ? code : 0, ch);
754 if(!numBytes) c = buflength;
758 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, code, ch);
762 if(buflength && windowData.ic)
763 for(c = 0; c<buflength;)
766 ch = UTF8GetChar(buf + c, &numBytes);
767 if(ch == 127) ch = 0;
768 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
770 if(!numBytes) c = buflength;
773 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
781 static uint E_CALL XEventThread(void * data)
787 if(xTerminate) break;
790 XPeekEvent(xGlobalDisplay, &e);
792 guiApp.SignalEvent();
799 static Bool EventChecker(void *display, XEvent *event, char * data)
801 return (!data || (event->type == (int) data)) && event->type != NoExpose && event->type != GraphicsExpose;
804 static Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
806 return (!data || (event->window == (X11Window) data)) && event->type == ConfigureNotify;
809 static enum FrameExtentSupport { unknown, working, broken };
811 static FrameExtentSupport frameExtentSupported;
812 static Time frameExtentRequest;
813 static X11Window frameExtentWindow;
815 static uint timerDelay = MAXINT;
816 #define RESOLUTION (18.2 * 100)
817 static uint XTimerThread(Thread thread)
819 int s = ConnectionNumber(xGlobalDisplay);
821 Time lastTime, thisTime;
822 lastTime = GetTime();
827 //bool waitSemaphore = false;
828 fd_set readSet, writeSet, exceptSet;
829 struct timeval tv = { (timerDelay == MAXINT) ? 0 : (timerDelay / 1000000), (timerDelay == MAXINT) ? (int)(1000000 / 18.2) : (timerDelay % 1000000) };
831 if(xTerminate) break;
836 FD_SET(s, &exceptSet);
838 if(select(s + 1, &readSet, null, null, &tv))
840 if(FD_ISSET(s, &readSet))
843 if(frameExtentSupported == unknown && frameExtentRequest && GetTime() - frameExtentRequest > 1)
845 XPropertyEvent event = { 0 };
846 event.type = PropertyNotify;
847 event.state = PropertyNewValue;
848 event.atom = atoms[_net_frame_extents];
849 event.display = xGlobalDisplay;
851 event.window = frameExtentWindow;
852 event.send_event = 1;
854 frameExtentSupported = broken;
856 XSendEvent(xGlobalDisplay, frameExtentWindow, bool::false,
857 PropertyChangeMask, (union _XEvent *)&event);
860 guiApp.SignalEvent();
865 Sleep(1.0 / RESOLUTION);
866 thisTime = GetTime();
867 if(xTerminate) break;
868 if(thisTime - lastTime > (1.0 / 18.2))
870 guiApp.SignalEvent();
878 XLockDisplay(xGlobalDisplay);
879 if(XCheckIfEvent(xGlobalDisplay, &xEvent, EventChecker, null))
882 guiApp.SignalEvent();
884 XUnlockDisplay(xGlobalDisplay);
893 static int MyXErrorHandler(X11Display * display, XErrorEvent * event)
897 XGetErrorText(xGlobalDisplay, event->error_code, buffer, sizeof(buffer));
899 Logf("X Error: %s\n", buffer);
904 static int MyXIOErrorHandler(X11Display * display)
910 // Motif Hints (to get rid of the decorations)
911 #define MWM_HINTS_FUNCTIONS (1L << 0)
912 #define MWM_HINTS_DECORATIONS (1L << 1)
913 #define MWM_HINTS_INPUT_MODE (1L << 2)
914 #define MWM_HINTS_STATUS (1L << 3)
916 #define MWM_DECOR_ALL (1L << 0)
917 #define MWM_DECOR_BORDER (1L << 1)
918 #define MWM_DECOR_RESIZEH (1L << 2)
919 #define MWM_DECOR_TITLE (1L << 3)
920 #define MWM_DECOR_MENU (1L << 4)
921 #define MWM_DECOR_MINIMIZE (1L << 5)
922 #define MWM_DECOR_MAXIMIZE (1L << 6)
924 #define MWM_FUNC_ALL (1L << 0)
925 #define MWM_FUNC_RESIZE (1L << 1)
926 #define MWM_FUNC_MOVE (1L << 2)
927 #define MWM_FUNC_MINIMIZE (1L << 3)
928 #define MWM_FUNC_MAXIMIZE (1L << 4)
929 #define MWM_FUNC_CLOSE (1L << 5)
934 unsigned long functions;
935 unsigned long decorations;
937 unsigned long status;
940 static void WaitForViewableWindow(Window window)
943 //Logf("Wait for viewable %s\n", window.name);
944 XFlush(xGlobalDisplay);
945 //while(attempts++ < 40)
948 XWindowAttributes attributes = { 0 };
950 if(!XGetWindowAttributes(xGlobalDisplay, (X11Window)window.windowHandle, &attributes))
952 if(attributes.map_state == IsViewable)
955 Sleep(1.0 / RESOLUTION);
959 static bool RequestFrameExtents(Window window)
961 if(window.nativeDecorations && frameExtentSupported != broken)
963 // Request decoration frame extents
964 XClientMessageEvent event = { 0 };
965 event.type = ClientMessage;
966 event.message_type = atoms[_net_request_frame_extents];
967 event.display = xGlobalDisplay;
969 event.window = (X11Window)window.windowHandle;
970 event.send_event = 1;
971 window.windowHandle = (void *)window.windowHandle;
974 if(frameExtentSupported == unknown && !frameExtentRequest)
976 frameExtentRequest = GetTime();
977 frameExtentWindow = (X11Window)window.windowHandle;
980 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false,
981 SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
985 static bool GetFrameExtents(Window window, bool update)
987 XWindowData windowData = window.windowData;
990 unsigned long len, fill;
994 if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.windowHandle,
995 atoms[_net_frame_extents], 0, 4,
996 False, XA_CARDINAL, &type, &format, &len,
997 &fill, &data) == Success && data)
999 long *extents = (long *)data;
1000 bool change = extents[0] != windowData.decor.left ||
1001 extents[1] != windowData.decor.right ||
1002 extents[2] != windowData.decor.top ||
1003 extents[3] != windowData.decor.bottom;
1005 bool hadFrameExtents = windowData.gotFrameExtents;
1006 Box oldDecor = windowData.decor;
1008 frameExtentSupported = working;
1009 frameExtentWindow = 0;
1010 frameExtentRequest = 0;
1012 if(!hadFrameExtents || extents[0] || extents[1] || extents[2] || extents[3])
1016 left = (int)extents[0], right = (int)extents[1],
1017 top = (int)extents[2], bottom = (int)extents[3]
1019 windowData.gotFrameExtents = true;
1020 if(update && change && ((Window)window).clientSize.w > 0)
1022 int x = window.position.x, y = window.position.y, w = window.size.w, h = window.size.h;
1023 if(!hadFrameExtents && window.state != maximized)
1025 window.ComputeAnchors(
1026 window.normalAnchor,
1027 window.normalSizeAnchor,
1032 x += windowData.decor.left - oldDecor.left;
1033 y += windowData.decor.top - oldDecor.top;
1035 w += windowData.decor.left - oldDecor.left + windowData.decor.right - oldDecor.right;
1036 h += windowData.decor.top - oldDecor.top + windowData.decor.bottom - oldDecor.bottom;
1039 if(window.state != maximized)
1041 window.Position(x, y, w, h, true, true, true, true, false, !hadFrameExtents && window.state != maximized);
1042 XInterface::UpdateRootWindow(window);
1052 /****************************************************************************
1053 /// DRIVER IMPLEMENTATION /////////////
1054 ****************************************************************************/
1056 /*static */class HiResTimer : Thread
1075 // Sleep(delay / 1000000.0);
1076 struct timeval tv = { delay / 1000000, delay % 1000000 };
1077 select(0,null,null,null, &tv);
1078 guiApp.SignalEvent();
1084 static HiResTimer hiResTimer { };
1088 #include <sys/ipc.h>
1089 #include <sys/shm.h>
1101 static int terminatePid;
1103 static void SigIntHandler(int value)
1105 // printf("SigHandler %d\n", getpid());
1106 if(!terminatePid || terminatePid == getpid())
1109 terminatePid = getpid();
1110 // printf("terminateX now equals %d\n", terminateX);
1111 if(guiApp && guiApp.semaphore)
1112 guiApp.semaphore.Release();
1115 struct shmid_ds info;
1116 int maxid = shmctl (0, SHM_INFO, &info);
1117 int pid = getpgrp();
1118 int thisPid = getpid();
1119 //if(thisPid == pid)
1125 for(id = 0; id <= maxid; id++)
1127 struct shmid_ds shmseg;
1129 if((shmid = shmctl(id, SHM_STAT, &shmseg)) >= 0)
1131 if(shmseg.shm_cpid == pid || shmseg.shm_cpid == thisPid)
1133 printf("%d (%d) belongs to us (%d)\n", shmid, id, shmseg.shm_cpid);
1134 shmctl(shmid, IPC_RMID, 0);
1142 /*else if(guiApp.desktop)
1143 guiApp.desktop.Destroy(0);*/
1146 class XInterface : Interface
1148 class_property(name) = "X";
1150 // --- User Interface System ---
1153 setlocale(LC_ALL, "en_US.UTF-8");
1156 XSetLocaleModifiers("");
1157 XSetErrorHandler(MyXErrorHandler);
1158 XSetIOErrorHandler(MyXIOErrorHandler);
1160 signal(SIGINT, SigIntHandler);
1163 xGlobalDisplay = XOpenDisplay(null);
1164 frameExtentSupported = unknown;
1166 joystickFD[0] = open("/dev/js0", O_RDONLY);
1167 joystickFD[1] = open("/dev/js1", O_RDONLY);
1168 joystickFD[2] = open("/dev/js2", O_RDONLY);
1169 joystickFD[3] = open("/dev/js3", O_RDONLY);
1171 systemCursors[iBeam] = XCreateFontCursor(xGlobalDisplay, XC_xterm);
1172 systemCursors[cross] = XCreateFontCursor(xGlobalDisplay, XC_tcross);
1173 systemCursors[moving] = XCreateFontCursor(xGlobalDisplay, XC_fleur);
1174 systemCursors[sizeNESW] = XCreateFontCursor(xGlobalDisplay, XC_bottom_left_corner);
1175 systemCursors[sizeNS] = XCreateFontCursor(xGlobalDisplay, XC_sb_v_double_arrow);
1176 systemCursors[sizeNWSE] = XCreateFontCursor(xGlobalDisplay, XC_bottom_right_corner);
1177 systemCursors[sizeWE] = XCreateFontCursor(xGlobalDisplay, XC_sb_h_double_arrow);
1178 systemCursors[hand] = XCreateFontCursor(xGlobalDisplay, XC_hand2);
1179 systemCursors[arrow] = XCreateFontCursor(xGlobalDisplay, XC_left_ptr);
1183 XWindowAttributes attributes = { 0 };
1184 XGetWindowAttributes(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &attributes);
1185 xSystemDepth = attributes.depth;
1186 xSystemVisual = attributes.visual;
1187 switch(xSystemDepth)
1189 case 32: case 24: xSystemPixelFormat = pixelFormat888; break;
1194 XVisualInfo *vinfo_ret;
1197 vinfo.visualid = XVisualIDFromVisual(xSystemVisual);
1198 vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &vinfo, &numitems);
1201 xSystemPixelFormat = (vinfo_ret->green_mask == 0x3E0) ? pixelFormat555 : pixelFormat565;
1203 if(GetXRenderFormat(xSystemPixelFormat, 0))
1205 // printf("Got X Render format %d\n", xSystemPixelFormat);
1209 printf("No X Format?\n");
1210 xSystemPixelFormat = (xSystemPixelFormat == pixelFormat555) ? pixelFormat565 : pixelFormat555;
1212 // vinfo._class = DirectColor;
1214 vinfo.bits_per_rgb = 5;
1215 vinfo.depth = xSystemDepth;
1216 vinfo.green_mask = (xSystemPixelFormat == pixelFormat555) ? 0x3E0 : 0x7E0;
1217 vinfo.red_mask = (xSystemPixelFormat == pixelFormat555) ? 0x7C00 : 0xF800;
1218 XFree((void *) vinfo_ret);
1219 if(XMatchVisualInfo(xGlobalDisplay, DefaultScreen(xGlobalDisplay), vinfo.depth, vinfo._class, &vinfo))
1220 //vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualDepthMask|VisualRedMaskMask|VisualGreenMaskMask|VisualBlueMaskMask, &vinfo, &numitems);
1223 //vinfo = *vinfo_ret;
1224 if(GetXRenderFormat(xSystemPixelFormat, 0))
1226 // printf("Got X Render format %d (second try)\n", xSystemPixelFormat);
1227 // printf("red mask: %x, green mask: %x, blue mask: %x\n", vinfo.red_mask,vinfo.green_mask, vinfo.blue_mask);
1229 xSystemVisual = vinfo.visual;
1232 printf("Could not get a 555 visual\n");
1238 // printf("Got a depth of %d\n", xSystemDepth);
1241 int major, minor, pixmaps;
1242 xSharedMemory = XShmQueryExtension(xGlobalDisplay) && XShmQueryVersion(xGlobalDisplay, &major, &minor, &pixmaps) && pixmaps;
1245 // printf("Opening IM\n");
1246 im = XOpenIM(xGlobalDisplay, null, null, null);
1249 XColor fore = { 0 }, back = { 0 };
1250 Pixmap pixmap = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1251 Pixmap mask = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1252 XSetWindowAttributes attributes = { 0 };
1254 XkbSetDetectableAutoRepeat(xGlobalDisplay, True, &autoRepeatDetectable);
1256 XInternAtoms(xGlobalDisplay, (char**)atomNames, AtomIdents::enumSize, False, atoms);
1258 // Check which atoms are supported by the WM
1261 unsigned long count, fill;
1265 if(XGetWindowProperty(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), atoms[_net_supported],
1266 0, 10000, False, XA_ATOM, &type, &format, &count, &fill, (void *)&data) == Success)
1269 for (i = 0; i < count; i++)
1272 for(j = 0; j < AtomIdents::enumSize; j++)
1274 if(atoms[j] == data[i])
1276 atomsSupported[j] = true;
1286 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
1288 XSetWMProtocols(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), protocols, 2);
1292 if(atomsSupported[_net_workarea])
1293 printf("Warning: _NET_WORKAREA extension not supported\n");
1296 attributes.override_redirect = True;
1299 windowContext = XUniqueContext();
1302 nullCursor = XCreatePixmapCursor(xGlobalDisplay, pixmap, mask, &fore, &back, 0, 0);
1303 confineWindow = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
1304 0,0,1,1,0, CopyFromParent, InputOnly, xSystemVisual /*CopyFromParent*/, CWOverrideRedirect, &attributes);
1307 // IM initialization
1309 /*ic = XCreateIC(im, XNInputStyle,
1310 XIMPreeditNothing | XIMStatusNothing,
1311 // XIMPreeditNone | XIMStatusNone | XIMPreeditPosition | XIMStatusArea | XIMPreeditArea | XIMStatusArea | XIMPreeditNothing | XIMStatusNothing,
1312 XNClientWindow, confineWindow, XNFocusWindow, confineWindow, 0);
1317 XIMStyle clientCanDo;
1318 XIMStyle styleWeWillUse = null;
1320 XVaNestedList arglist;
1321 unsigned long imEventMask;
1323 XGetImValues(im, XNQueryInputStyle, &IMcando, null);
1326 XIMPreeditNone | XIMStatusNone |
1327 XIMPreeditPosition | XIMStatusArea |
1328 XIMPreeditArea | XIMStatusArea |
1329 XIMPreeditNothing | XIMStatusNothing;
1331 for(i=0; i<IMcando->count_styles; i++)
1334 tmpStyle = IMcando->support_styles[i];
1335 if ( ((tmpStyle & clientCanDo) == tmpStyle) && prefer(tmpStyle, styleWeWillUse) )
1336 styleWeWillUse = tmpStyle;
1338 if(styleWeWillUse = null)
1342 arglist = XVaCreateNestedList(0, XNFontSet, fontset,
1344 WhitePixel(xGlobalDisplay, screen),
1346 BlackPixel(xGlobalDisplay, screen),
1348 ic = XCreateIC(im, XNInputStyle, styleWeWillUse,
1349 XNClientWindow, window, XNFocusWindow, window,
1350 XNStatusAttributes, arglist,
1351 XNPreeditAttributes, arglist, NULL);
1356 XGetWindowAttributes(xGlobalDisplay, win, &winAtts);
1357 XGetICValues(ic, XNFilterEvents, &imEventMask, null);
1358 imEventMask |= winAtts.your_event_mask;
1359 XSelectInput(xGlobalDisplay, window, imEventMask);
1365 timerThread = Thread { };
1367 timerThread.Main = XTimerThread;
1368 timerThread.Create();
1381 // WHY WAS THIS COMMENTED HERE?
1382 // Probably because it was causing crashes, the proper fix should now be in DestroyRootWindow
1386 xSemaphore.Release();
1392 XFreeCursor(xGlobalDisplay, systemCursors[iBeam]);
1393 XFreeCursor(xGlobalDisplay, systemCursors[cross]);
1394 XFreeCursor(xGlobalDisplay, systemCursors[moving]);
1395 XFreeCursor(xGlobalDisplay, systemCursors[sizeNESW]);
1396 XFreeCursor(xGlobalDisplay, systemCursors[sizeNS]);
1397 XFreeCursor(xGlobalDisplay, systemCursors[sizeNWSE]);
1398 XFreeCursor(xGlobalDisplay, systemCursors[sizeWE]);
1399 XFreeCursor(xGlobalDisplay, systemCursors[hand]);
1400 XFreeCursor(xGlobalDisplay, systemCursors[arrow]);
1402 //XPutBackEvent(xGlobalDisplay, &e);
1406 /*if(windowData && windowData.ic)
1408 XDestroyIC(windowData.ic);
1409 windowData.ic = null;
1416 XCloseDisplay(xGlobalDisplay);
1417 xGlobalDisplay = null;
1419 if(joystickFD[0] != -1) close(joystickFD[0]);
1420 if(joystickFD[1] != -1) close(joystickFD[1]);
1421 if(joystickFD[2] != -1) close(joystickFD[2]);
1422 if(joystickFD[3] != -1) close(joystickFD[3]);
1425 #define DBLCLICK_DELAY 300 // 0.3 second
1426 #define DBLCLICK_DELTA 1
1428 bool ProcessInput(bool processAll)
1430 bool eventAvailable = false;
1433 if(!fullScreenMode) RepositionDesktop(true);
1435 //* XLockDisplay(xGlobalDisplay);
1436 while(!xTerminate && (/*gotAnXEvent || */XCheckIfEvent(xGlobalDisplay, &e, EventChecker, null)))
1438 Window window = null;
1439 XAnyEvent * thisEvent = (XAnyEvent *)&e;
1440 // printf("Got an event: %d\n", thisEvent->type);
1442 //if(gotAnXEvent) { thisEvent = (XAnyEvent *)&xEvent; gotAnXEvent = false; }
1444 if(im && XFilterEvent((union _XEvent *)thisEvent, None))
1446 eventAvailable = true;
1447 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1450 XWindowData windowData = window.windowData;
1451 static uint lastKeyCode = 0;
1452 switch(thisEvent->type)
1456 XKeyEvent * event = (XKeyEvent *) thisEvent;
1457 //*XUnlockDisplay(xGlobalDisplay);
1458 ProcessKeyMessage(window, event->keycode, (event->keycode == lastKeyCode) ? 2 : 0, event);
1459 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1460 lastKeyCode = event->keycode;
1465 XKeyEvent * event = (XKeyEvent *) thisEvent;
1468 if(!autoRepeatDetectable && XCheckIfEvent(xGlobalDisplay, (XEvent *)&nextEvent, EventChecker, (void *)KeyPress))
1470 if(im && XFilterEvent(&nextEvent, None))
1473 if(((XKeyEvent *)&nextEvent)->keycode == event->keycode)
1475 //*XUnlockDisplay(xGlobalDisplay);
1476 ProcessKeyMessage(window, event->keycode, 2, event);
1480 //*XUnlockDisplay(xGlobalDisplay);
1481 // printf("Keycode not the same :(\n");
1482 ProcessKeyMessage(window, event->keycode, 1, event);
1483 ProcessKeyMessage(window, ((XKeyEvent *)&nextEvent)->keycode, 0, (XKeyEvent *)&nextEvent);
1488 //*XUnlockDisplay(xGlobalDisplay);
1489 // printf("No KeyPress ahead\n");
1490 ProcessKeyMessage(window, event->keycode, 1, event);
1492 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1497 XButtonEvent * event = (XButtonEvent *) thisEvent;
1499 static double lastTime[3];
1500 static Window lastWindow[3];
1501 static Point lastPos[3];
1503 Modifiers keyFlags = 0;
1505 uint button, buttonDouble, whichButton;
1507 int x = event->x_root, y = event->y_root;
1509 if(event->button == Button1)
1511 // Force a raise on click here to deal with confused active state preventing to bring the window up
1512 if(!atomsSupported[_net_active_window] && !window.isRemote)
1514 XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
1515 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
1517 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
1518 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
1520 buttonMask = Button1Mask;
1521 keyFlags.left = true;
1523 else if(event->button == Button3)
1525 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
1526 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
1528 buttonMask = Button3Mask;
1529 keyFlags.right = true;
1533 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
1534 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
1536 buttonMask = Button2Mask;
1537 keyFlags.middle = true;
1539 if(event->state & buttonMask)
1542 doubleClick = event->time - lastTime[whichButton] < DBLCLICK_DELAY &&
1543 window == lastWindow[whichButton] &&
1544 Abs(event->x_root - lastPos[whichButton].x) < DBLCLICK_DELTA &&
1545 Abs(event->y_root - lastPos[whichButton].y) < DBLCLICK_DELTA;
1546 lastTime[whichButton] = doubleClick ? 0 : event->time;
1547 lastWindow[whichButton] = window;
1548 lastPos[whichButton].x = event->x_root;
1549 lastPos[whichButton].y = event->y_root;
1551 if(event->state & ShiftMask) keyFlags.shift = true;
1552 if(event->state & ControlMask) keyFlags.ctrl = true;
1553 if(event->state & Mod1Mask) keyFlags.alt = true;
1554 if(event->state & Button1Mask) keyFlags.left = true;
1555 if(event->state & Button2Mask) keyFlags.middle = true;
1556 if(event->state & Button3Mask) keyFlags.right = true;
1557 //*XUnlockDisplay(xGlobalDisplay);
1560 if(event->button == Button4 || event->button == Button5)
1562 window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, (event->button == Button4) ? wheelUp : wheelDown, 0);
1568 if(!window.MouseMessage(buttonDouble, x, y, &keyFlags, false, true))
1570 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1575 window.MouseMessage(button, x, y, &keyFlags, false, /*doubleClick? false : */true);
1577 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1583 Modifiers keyFlags = 0;
1584 XButtonEvent * event = (XButtonEvent *) thisEvent;
1587 int x = event->x_root, y = event->y_root;
1588 if(event->button == Button1)
1590 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
1591 buttonMask = Button1Mask;
1593 else if(event->button == Button3)
1595 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
1596 buttonMask = Button3Mask;
1600 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
1601 buttonMask = Button2Mask;
1603 if(!(event->state & buttonMask)) break;
1604 if(event->state & ShiftMask) keyFlags.shift = true;
1605 if(event->state & ControlMask) keyFlags.ctrl = true;
1606 if(event->state & Mod1Mask) keyFlags.alt = true;
1607 if(event->state & Button1Mask) keyFlags.left = true;
1608 if(event->state & Button2Mask) keyFlags.middle = true;
1609 if(event->state & Button3Mask) keyFlags.right = true;
1610 if(guiApp.windowCaptured && guiApp.windowCaptured != window)
1612 // X hasn't noticed the capture yet, so fix it!
1613 x += window.absPosition.x;
1614 y += window.absPosition.y;
1615 window = guiApp.windowCaptured.rootWindow;
1616 x -= window.absPosition.x;
1617 y -= window.absPosition.y;
1619 //*XUnlockDisplay(xGlobalDisplay);
1621 window.MouseMessage(button, x, y, &keyFlags, false, false);
1623 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1628 static uint lastTime = 0;
1629 XMotionEvent * event = (XMotionEvent *) thisEvent;
1630 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, EventChecker, (void *)MotionNotify));
1631 // if(event->time - lastTime > 15)
1633 Modifiers keyFlags = 0;
1634 // int x = event->x_root, y = event->y_root;
1636 if(event->state & ShiftMask) keyFlags.shift = true;
1637 if(event->state & ControlMask) keyFlags.ctrl = true;
1638 if(event->state & Mod1Mask) keyFlags.alt = true;
1639 if(event->state & Button1Mask) keyFlags.left = true;
1640 if(event->state & Button2Mask) keyFlags.middle = true;
1641 if(event->state & Button3Mask) keyFlags.right = true;
1642 //*XUnlockDisplay(xGlobalDisplay);
1644 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove,
1645 event->x_root, event->y_root, &keyFlags, false, false);
1647 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1648 lastTime = (uint)event->time;
1654 XExposeEvent * event = (XExposeEvent *) thisEvent;
1656 box.left = event->x - window.clientStart.x;
1657 box.top = event->y - window.clientStart.y;
1658 box.right = box.left + event->width - 1;
1659 box.bottom = box.top + event->height - 1;
1661 box.left += window.clientStart.x;
1662 box.top += window.clientStart.y;
1663 box.right += window.clientStart.x;
1664 box.bottom += window.clientStart.y;
1665 window.UpdateDirty(box);
1668 case SelectionRequest:
1670 XSelectionRequestEvent *req = (XSelectionRequestEvent *) thisEvent;
1672 if(req->target == atoms[targets] && clipBoardData)
1674 Atom * supportedTargets = new Atom[4];
1675 supportedTargets[0] = atoms[targets];
1676 supportedTargets[1] = atoms[multiple];
1677 supportedTargets[2] = XA_STRING;
1678 supportedTargets[3] = atoms[utf8_string];
1679 XChangeProperty(xGlobalDisplay,req->requestor, req->_property,
1680 XA_ATOM,32,PropModeReplace, (byte *) supportedTargets, 4*sizeof(Atom));
1681 respond.xselection._property = req->_property;
1682 delete supportedTargets;
1684 else if((req->target == XA_STRING || req->target == atoms[utf8_string]) && clipBoardData)
1686 Atom _property = (req->_property == None) ? req->target : req->_property;
1687 XChangeProperty(xGlobalDisplay,req->requestor, _property,
1688 req->target/*req->_property*/,8,PropModeReplace, (byte *) clipBoardData, strlen(clipBoardData));
1689 respond.xselection._property = _property;
1692 respond.xselection._property = None;
1694 respond.xselection.type = SelectionNotify;
1695 respond.xselection.display = req->display;
1696 respond.xselection.requestor = req->requestor;
1697 respond.xselection.selection =req->selection;
1698 respond.xselection.target = req->target;
1699 respond.xselection.time = CurrentTime;
1700 XSendEvent(xGlobalDisplay, req->requestor,0,0,&respond);
1703 case SelectionClear:
1705 delete clipBoardData;
1710 if(activeWindow != (X11Window)window.windowHandle)
1712 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1713 Window modalRoot = window.FindModal();
1714 XWindowData windowData;
1716 activeWindow = (X11Window)window.windowHandle;
1718 if(window.parent && window == window.parent.activeChild) break;
1720 //if(window.creationActivation == activate)
1723 modalRoot.ExternalActivate(true, true, window, null); // lastActive);
1725 window.ExternalActivate(true, true, window, null); // lastActive);
1727 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1728 if(windowData && windowData.ic)
1730 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
1731 XSetICFocus(windowData.ic);
1733 //delete lastActive;
1734 //lastActive = window;
1735 //incref lastActive;
1743 //printf("Processing a FocusOut Event for %s (%x)\n", window._class.name, window);
1746 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusIn, (XEvent *)thisEvent))
1750 if(thisEvent->window == activeWindow)
1751 activeWindow = (X11Window)null;
1753 if(XCheckTypedEvent(xGlobalDisplay, FocusIn, (XEvent *)thisEvent))
1755 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusOut, (XEvent *)thisEvent))
1757 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1759 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1762 Window windowCopy = window;
1763 XWindowData windowData;
1764 if(windowCopy == windowCopy.parent.activeChild) break;
1766 windowCopy.ExternalActivate(true, true, windowCopy, lastActive);
1768 windowData = windowCopy.windowData;
1769 if(windowData && windowData.ic)
1770 XSetICFocus(windowData.ic);
1773 lastActive = windowCopy;
1778 // XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1781 if(window != window.parent.activeChild) break;
1783 window.ExternalActivate(false, true, window, lastActive);
1789 XWindowData windowData;
1790 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1794 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1795 Window modalRoot = window.FindModal();
1800 modalRoot.ExternalActivate(true, true, window, lastActive);
1804 window.ExternalActivate(true, true, window, lastActive);
1806 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1807 if(windowData && windowData.ic)
1808 XSetICFocus(windowData.ic);
1811 lastActive = window;
1820 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1821 if(window != window.parent.activeChild && window != guiApp.interimWindow) break;
1825 //printf("Deactivating %s\n", window._class.name);
1828 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
1830 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
1833 //delete lastActive;
1835 lastActive = window;
1842 case ConfigureNotify:
1844 XConfigureEvent * event = (XConfigureEvent *) thisEvent;
1845 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, (void *)ConfigureNotifyChecker, (void *)window.windowHandle));
1846 //if(event->x - desktopX != window.position.x || event->y - desktopY != window.position.y || event->width != window.size.w || event->height != window.size.h)
1848 if(window.nativeDecorations)
1851 unsigned long len, fill;
1854 if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.systemHandle, atoms[_net_wm_state], 0, 32, False,
1855 XA_ATOM, &type, &format, &len, &fill, &data) == Success)
1857 bool maxVert = false, maxHorz = false, isMinimized = false;
1858 Atom * hints = (Atom *)data;
1860 for(c = 0; c < len && hints[c]; c++)
1862 if(hints[c] == atoms[_net_wm_state_maximized_vert])
1864 else if(hints[c] == atoms[_net_wm_state_maximized_horz])
1866 else if(hints[c] == atoms[_net_wm_state_hidden])
1871 if(maxVert && maxHorz)
1873 if(window.state != maximized)
1875 *&window.state = maximized;
1876 if(!window.nativeDecorations)
1877 window.CreateSystemChildren();
1880 else if(isMinimized)
1882 if(window.state != minimized)
1884 *&window.state = minimized;
1885 if(!window.nativeDecorations)
1886 window.CreateSystemChildren();
1889 else if(window.state != normal)
1891 if(window.state == maximized)
1893 *&window.state = normal;
1894 if(!window.nativeDecorations)
1895 window.CreateSystemChildren();
1902 int w = event->width, h = event->height;
1904 //if(event->send_event)
1906 X11Window rootChild;
1908 XTranslateCoordinates(xGlobalDisplay, event->window,
1909 RootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 0, 0,
1910 &rootX, &rootY, &rootChild);
1918 if(window.nativeDecorations && window.state != maximized)
1920 x -= windowData.decor.left;
1921 y -= windowData.decor.top;
1922 w += windowData.decor.left + windowData.decor.right;
1923 h += windowData.decor.top + windowData.decor.bottom;
1925 x -= window.clientStart.x;
1926 y -= window.clientStart.y - (window.hasMenuBar ? skinMenuHeight : 0);
1927 w += window.size.w - window.clientSize.w;
1928 h += window.size.h - window.clientSize.h;
1932 // Break the anchors for moveable/resizable windows
1933 if(window.style.fixed && window.state == normal)
1935 window.normalAnchor = Anchor { left = x, top = y };
1936 window.normalSizeAnchor = SizeAnchor { { w, h } };
1937 window.anchored = false;
1940 window.Position(x, y, w, h, true, true, true, true, false, false);
1946 XClientMessageEvent * event = (XClientMessageEvent *) thisEvent;
1948 if(event->data.l[0] == atoms[wm_delete_window])
1953 if(event->data.l[0] == atoms[wm_take_focus])
1956 XWindowData windowData;
1958 activeWindow = (X11Window)window.windowHandle;
1960 timeStamp = (int)event->data.l[1];
1962 windowData = window.windowData;
1963 laterFocus = windowData.laterFocus;
1964 windowData.laterFocus = true;
1967 //printf("Processing a ClientMessage WM_TAKE_FOCUS Event for %s (%x)\n", window._class.name, window);
1969 if(guiApp.interimWindow && guiApp.interimWindow.created && window != guiApp.interimWindow) break; //window == window.parent.activeChild) break;
1970 // if(window == window.parent.activeChild) break;
1975 //XFlush(xGlobalDisplay);
1976 while(XCheckTypedEvent(xGlobalDisplay, FocusOut, &checkEvent))
1978 XFocusChangeEvent *event = (XFocusChangeEvent *) &checkEvent;
1980 XFindContext(xGlobalDisplay, event->window, windowContext, (XPointer *) &window);
1981 if(window != window.parent.activeChild) break;
1985 //printf("Found a FocusOut ahead, deactivating %s (%d)\n", window._class.name, window);
1988 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
1990 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
1997 modalRoot = window.FindModal();
1998 windowData = modalRoot ? modalRoot.windowData : window.windowData;
2001 if(laterFocus || (modalRoot ? modalRoot : window).creationActivation == activate)
2005 XRaiseWindow(xGlobalDisplay, (X11Window)modalRoot.windowHandle);
2006 WaitForViewableWindow(modalRoot);
2007 if(atomsSupported[_net_active_window])
2009 XClientMessageEvent event = { 0 };
2010 event.type = ClientMessage;
2011 event.message_type = atoms[_net_active_window];
2012 event.display = xGlobalDisplay;
2014 event.window = (X11Window)modalRoot.windowHandle;
2015 event.send_event = 1;
2017 event.data.l[0] = 0;
2019 event.data.l[0] = 1;
2020 event.data.l[1] = atoms[_net_wm_user_time];
2021 event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
2024 //printf("(ClientMessage - %s) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, modalRoot._class.name, modalRoot);
2027 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2028 XSetInputFocus(xGlobalDisplay, (X11Window)modalRoot.windowHandle, RevertToPointerRoot, (uint)timeStamp);
2030 //XFlush(xGlobalDisplay);
2031 //printf("Done.\n");
2036 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, (uint)timeStamp);
2037 window.ExternalActivate(true, true, window, null); // lastActive);
2038 if(windowData && windowData.ic)
2040 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
2041 //XSetICFocus(windowData.ic);
2045 windowData.laterFocus = true;
2047 lastActive = window;
2054 case PropertyNotify:
2056 XWindowData windowData = window.windowData;
2057 XPropertyEvent * event = (XPropertyEvent *) thisEvent;
2058 if(event->atom == atoms[_net_frame_extents] &&
2059 event->state == PropertyNewValue && windowData)
2061 if(!GetFrameExtents(window, true))
2062 windowData.gotFrameExtents = true; // Unsupported?
2067 if(!processAll) break;
2070 //*if(xGlobalDisplay) XUnlockDisplay(xGlobalDisplay);
2079 gotAnXEvent = false;
2081 xSemaphore.Release();
2086 void Lock(Window window)
2088 //*XLockDisplay(xGlobalDisplay);
2091 void Unlock(Window window)
2093 //*XUnlockDisplay(xGlobalDisplay);
2096 char ** GraphicsDrivers(int * numDrivers)
2098 static char *graphicsDrivers[] = { "X", "OpenGL" };
2099 *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
2100 return (char **)graphicsDrivers;
2103 void GetCurrentMode(bool * fullScreen, int * resolution, int * colorDepth, int * refreshRate)
2105 *fullScreen = fullScreenMode;
2108 void EnsureFullScreen(bool *fullScreen)
2113 bool ScreenMode(bool fullScreen, int resolution, int colorDepth, int refreshRate, bool * textMode)
2117 fullScreenMode = fullScreen;
2125 static bool firstTime = true;
2127 desktopX = desktopY = desktopW = desktopH = 0;
2129 RepositionDesktop(false);
2134 // --- Window Creation ---
2135 void * CreateRootWindow(Window window)
2137 X11Window windowHandle;
2138 XSetWindowAttributes attributes = { 0 };
2139 XVisualInfo * visualInfo = null;
2143 unsigned long mask = EVENT_MASK;
2145 // Old WM (e.g. TWM), use built-in decorations
2146 if(!atomsSupported[_net_wm_state])
2147 window.nativeDecorations = false;
2148 attributes.override_redirect = (window.interim || (!atomsSupported[_net_wm_state] && !window.nativeDecorations)) ? True : False;
2149 attributes.event_mask = EVENT_MASK;
2150 //printf("%s\n", guiApp.defaultDisplayDriver);
2151 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
2152 if(window.dispDriver == class(OpenGLDisplayDriver) || !strcmp(guiApp.defaultDisplayDriver, "OpenGL"))
2156 for(samples = 4;; samples /= 2)
2161 GLX_RENDER_TYPE, GLX_RGBA_BIT,
2162 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
2163 GLX_DOUBLEBUFFER, True,
2169 int numAttribs = 14;
2171 GLXFBConfig *fbconfigs = null, fbconfig;
2174 //printf("Samples = %d, alpha = %d\n", samples, alpha);
2177 attrib[numAttribs++] = GLX_ALPHA_SIZE;
2178 attrib[numAttribs++] = 1;
2182 attrib[numAttribs++] = GLX_SAMPLE_BUFFERS_ARB;
2183 attrib[numAttribs++] = GL_TRUE;
2184 attrib[numAttribs++] = GLX_SAMPLES_ARB;
2185 attrib[numAttribs++] = samples;
2187 attrib[numAttribs] = None;
2189 // visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib);
2191 //printf("Trying %d samples...\n", samples);
2192 fbconfigs = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &numfbconfigs);
2195 for (i = 0; i < numfbconfigs; i++)
2197 XRenderPictFormat * format;
2198 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfigs[i]);
2199 if (!visualInfo) continue;
2200 if(window.alphaBlend)
2202 format = XRenderFindVisualFormat(xGlobalDisplay, visualInfo->visual);
2203 if (!format) { XFree(visualInfo); continue; }
2204 if(format->direct.alphaMask > 0)
2206 //printf("Found what we're looking for (alphaBlend)\n");
2207 fbconfig = fbconfigs[i];
2214 //printf("Found what we're looking for\n");
2219 if (i == numfbconfigs)
2221 fbconfig = fbconfigs[0];
2222 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfig);
2227 if(found || (!samples && !alpha))
2229 //printf("Stopping now\n");
2234 if(samples == 1) samples = 0;
2235 else if(!samples) alpha = false;
2242 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
2244 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
2248 visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrList);
2251 depth = visualInfo ? visualInfo->depth : (window.alphaBlend ? 32 : xSystemDepth);
2252 visual = visualInfo ? visualInfo->visual : (window.alphaBlend ? FindFullColorVisual (xGlobalDisplay, &depth) : xSystemVisual);
2253 // printf("visual: %d, depth: %d\n", visual, depth);
2257 attributes.colormap = XCreateColormap(xGlobalDisplay, XRootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), visual, AllocNone);
2258 attributes.border_pixel = 0;
2265 windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
2266 0,0,guiApp.desktop.size.w,guiApp.desktop.size.h,0, depth, InputOutput, visual ? visual : CopyFromParent,
2267 CWEventMask | (visual ? (CWColormap | CWBorderPixel) : 0)/*| CWOverrideRedirect*/, &attributes);
2270 Unsupported for now...
2271 else if(window.systemParent)
2278 if(window.windowHandle)
2279 windowHandle = (X11Window)window.windowHandle;
2282 X11Window parentWindow = (X11Window)null;
2283 int x = window.position.x, y = window.position.y;
2284 int w = window.state == normal ? Max(1, window.size.w) : Max(1, window.normalSizeAnchor.size.w);
2285 int h = window.state == normal ? Max(1, window.size.h) : Max(1, window.normalSizeAnchor.size.h);
2287 if(window.master.rootWindow && window.master.rootWindow != guiApp.desktop && (window._isModal || window.style.interim))
2289 Window master = window.master;
2290 Window rootWindow = master.rootWindow;
2292 parentWindow = rootWindow.is3D ? (X11Window)rootWindow.parent.windowHandle : (X11Window)rootWindow.windowHandle;
2294 // parentWindow = window.master.rootWindow.is3D ? window.master.rootWindow.parent.windowHandle : window.master.rootWindow.windowHandle;
2296 if(window.style.showInTaskBar)
2297 parentWindow = (X11Window)null;
2299 windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
2301 0, depth, InputOutput, visual ? visual : CopyFromParent,
2302 CWEventMask | CWOverrideRedirect | (visual ? (CWColormap | CWBorderPixel) : 0), &attributes);
2304 if(parentWindow && (window.interim || window.isModal))
2306 //printf("Setting WM_TRANSIENT_FOR of %s to %s\n", window._class.name, window.master.rootWindow._class.name);
2307 XSetTransientForHint(xGlobalDisplay, windowHandle, parentWindow);
2308 //XFlush(xGlobalDisplay);
2309 //printf("Done.\n");
2310 //XChangeProperty(xGlobalDisplay, windowHandle, atoms[wm_transient_for], XA_WINDOW, 32, PropModeReplace, (unsigned char*)&parentWindow, 1);
2313 Atom hints[1] = { atoms[_net_wm_state_modal] };
2314 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_state], XA_ATOM, 32, PropModeReplace, (unsigned char*)&hints, 1);
2321 parentWindow ? atoms[_net_wm_window_type_menu] : atoms[_net_wm_window_type_normal],
2322 parentWindow ? atoms[_net_wm_window_type_popup_menu] : 0
2324 #if defined(__APPLE__) || defined(__FreeBSD__)
2325 // Don't set this on non-interim windows for OS X...
2326 if(parentWindow && window.interim)
2329 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
2330 PropModeReplace, (unsigned char*)&hints, parentWindow ? 2 : 1);
2333 xwmHints.flags = InputHint;
2335 XSetWMHints(xGlobalDisplay, windowHandle, &xwmHints);
2338 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
2339 XSetWMProtocols(xGlobalDisplay, windowHandle, protocols, 2);
2342 // Set Normal hints for minimum/maximum size
2343 if(true) //window.minSize.w || window.minSize.h || window.maxSize.w < MAXINT || window.maxSize.h < MAXINT)
2345 XSizeHints hints = { 0 };
2347 window.SetWindowMinimum(&mw, &mh);
2348 if(window.minSize.w || window.minSize.h)
2350 hints.min_width = Max(window.minSize.w, mw);
2351 hints.min_height = Max(window.minSize.h, mh);
2352 hints.flags |= PMinSize;
2354 if(window.maxSize.w < MAXINT || window.minSize.h < MAXINT)
2356 hints.max_width = window.maxSize.w;
2357 hints.max_height = window.maxSize.h;
2358 hints.flags |= PMaxSize;
2362 hints.flags |= PPosition;
2366 hints.flags |= PSize;
2368 XSetWMNormalHints(xGlobalDisplay, windowHandle, &hints);
2376 char fontString[1024] = "--helvetica-*-r-*-*-*-120-*-*-*-*-*-*,-misc-fixed-*-r-*-*-*-130-*-*-*-*-*-*";
2377 XPoint cursor_location = { 0, 0 };
2378 char **missing_charsets;
2379 int num_missing_charsets = 0;
2380 char *default_string;
2382 XRectangle area = { 0, 0, 400, 400 };
2383 XVaNestedList argList;
2385 // sprintf(fontString, "-*-%s-*-r-*-*-*-%d-*-*-*-*-*-*", "Helvetica" /*window.font.faceName*/, (int)(window.font.size * 20));
2386 fontset = XCreateFontSet(xGlobalDisplay, fontString, &missing_charsets, &num_missing_charsets, &default_string);
2387 argList = XVaCreateNestedList(0,
2388 XNSpotLocation, &cursor_location,
2390 XNFontSet, fontset,/*
2392 WhitePixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),
2394 BlackPixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),*/
2396 ic = XCreateIC(im, XNInputStyle,
2397 XIMStatusNothing | XIMPreeditPosition,
2398 XNPreeditAttributes, argList,
2399 XNClientWindow, windowHandle, XNFocusWindow, windowHandle, NULL);
2401 // Should we free the fontset or not?
2402 // XFreeFontSet(xGlobalDisplay, fontset);
2404 setICPosition = true;
2406 ic = XCreateIC(im, XNInputStyle, XIMStatusNothing | XIMPreeditNothing, XNClientWindow, windowHandle, XNFocusWindow, windowHandle, null);
2410 XGetICValues(ic, XNFilterEvents, &mask, NULL);
2414 XSelectInput(xGlobalDisplay, windowHandle, mask);
2416 if(capturedWindow == None && !restrictedWindow)
2418 XGrabPointer(xGlobalDisplay, (X11Window)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2419 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2420 XUngrabPointer(xGlobalDisplay, CurrentTime);
2425 if ( atoms[_motif_wm_hints] != None )
2429 (window.nativeDecorations ? 0 : MWM_HINTS_DECORATIONS)|MWM_HINTS_FUNCTIONS,
2430 (window.hasClose ? MWM_FUNC_CLOSE : 0) |
2431 (window.hasMaximize ? MWM_FUNC_MAXIMIZE : 0) |
2432 (window.hasMinimize ? MWM_FUNC_MINIMIZE : 0) |
2433 ((window.moveable || ((BorderBits)window.borderStyle).fixed) ? MWM_FUNC_MOVE : 0) |
2434 (((BorderBits)window.borderStyle).sizable ? MWM_FUNC_RESIZE : 0),
2437 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
2438 PropModeReplace, (unsigned char*)&hints, 5);
2441 // *** We set this for ourselves, so don't check atomsSupported !!! ***
2442 if(atoms[_net_wm_pid])
2445 // printf("Setting _NET_WM_PID to %d\n", pid);
2446 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_pid], XA_CARDINAL, 32,
2447 PropModeReplace, (unsigned char*)&pid, 1);
2453 Atom protocolsAtom = XInternAtom(xGlobalDisplay, "WM_PROTOCOLS", False);
2454 if ( protocolsAtom != None )
2456 MWM_Hints hints = { MWM_HINTS_DECORATIONS|MWM_HINTS_FUNCTIONS, 0, 0, 0, 0 };
2457 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
2458 PropModeReplace, (unsigned char*)&hints, sizeof(hints)/4);
2461 // XFlush(xGlobalDisplay);
2462 window.windowData = XWindowData { visualInfo, ic };
2464 XSaveContext(xGlobalDisplay, windowHandle, windowContext, (XPointer)window);
2466 XSelectInput(xGlobalDisplay, windowHandle, mask);
2468 if(capturedWindow == None && !restrictedWindow)
2470 XGrabPointer(xGlobalDisplay, (X11Window)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2471 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2472 XUngrabPointer(xGlobalDisplay, CurrentTime);
2475 if(!window.nativeDecorations || !RequestFrameExtents(window))
2476 ((XWindowData)window.windowData).gotFrameExtents = true;
2477 return (void *)windowHandle;
2480 void DestroyRootWindow(Window window)
2484 XDeleteContext(xGlobalDisplay, (XID)window, windowContext);
2485 XSaveContext(xGlobalDisplay, (X11Window)window.windowHandle, windowContext, null);
2486 XDestroyWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2487 XSync(xGlobalDisplay, 0);
2488 while(XCheckWindowEvent(xGlobalDisplay, (X11Window)window.windowHandle, 0xFFFFFFFF, &event));
2489 window.windowHandle = null;
2490 if(window.windowData)
2492 XWindowData windowData = window.windowData;
2493 XFree(windowData.visual);
2494 if(windowData && windowData.ic)
2495 XDestroyIC(windowData.ic);
2497 // printf("Setting windowData for %s to null\n", window._class.name);
2498 window.windowData = null;
2500 if(lastActive == window)
2504 // -- Window manipulation ---
2506 void SetRootWindowCaption(Window window, char * name)
2508 if(window.windowHandle)
2510 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_name],
2511 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2512 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[wm_name],
2513 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2517 void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
2519 //Logf("Position root window %s\n", window.name);
2520 if(window.windowHandle && (!window.parent || !window.parent.display))
2522 bool visible = window.visible;
2523 if(window.visible && window.created)
2524 XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2525 if(window.state == minimized && atomsSupported[_net_wm_state]) return;
2527 if(window.nativeDecorations)
2529 XWindowData windowData = window.windowData;
2531 // Was commenting this out was part of #700/#795 fix, but this causes jumping of e.g. About box after getting frame extents PropertyNotify
2533 // && window.state != maximized -- required for Cinnamon on Mint 14/15
2534 if(!windowData.gotFrameExtents && window.state != maximized) return;
2536 x -= windowData.decor.left;
2537 y -= windowData.decor.top;
2539 w -= windowData.decor.left + windowData.decor.right;
2540 h -= windowData.decor.top + windowData.decor.bottom;
2542 // Tweak for first unmaximize on Unity on Ubuntu 11.10
2543 if(window.state == maximized && (desktopX + w > desktopW || desktopY + h > desktopH))
2554 XMoveResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, x, y, w, h);
2556 XMoveWindow(xGlobalDisplay, (X11Window)window.windowHandle, x, y);
2558 XResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, w, h);
2559 #if defined(__APPLE__)
2560 // if(window.created && !visible)
2561 // XUnmapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2566 void OrderRootWindow(Window window, bool topMost)
2571 void SetRootWindowColor(Window window)
2576 void OffsetWindow(Window window, int * x, int * y)
2581 void UpdateRootWindow(Window window)
2583 if(!window.parent || !window.parent.display)
2587 Box box = window.box;
2588 box.left -= window.clientStart.x;
2589 box.top -= window.clientStart.y;
2590 box.right -= window.clientStart.x;
2591 box.bottom -= window.clientStart.y;
2592 // Logf("Update root window %s\n", window.name);
2593 window.Update(null);
2594 box.left += window.clientStart.x;
2595 box.top += window.clientStart.y;
2596 box.right += window.clientStart.x;
2597 box.bottom += window.clientStart.y;
2598 window.UpdateDirty(box);
2603 void SetRootWindowState(Window window, WindowState state, bool visible)
2605 // Old WM (e.g. TWM), use built-in decorations
2606 if(!atomsSupported[_net_wm_state])
2607 window.nativeDecorations = false;
2608 if(!window.parent || !window.parent.display)
2610 //Logf("Set root window state %d %s\n", state, window.name);
2613 XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2614 WaitForViewableWindow(window);
2615 if(window.creationActivation == activate && state != minimized)
2616 ActivateRootWindow(window);
2618 if(state == minimized && atomsSupported[_net_wm_state])
2620 uint iconic = IconicState;
2622 XChangeProperty(xGlobalDisplay, window.windowHandle, atoms[_net_wm_state], XA_ATOM, 32,
2623 PropModeReplace, (unsigned char*)&atoms[_net_wm_state_hidden], 1);
2626 XChangeProperty(xGlobalDisplay, window.windowHandle, atoms[wm_state], XA_CARDINAL, 32,
2627 PropModeReplace, &iconic, 1);
2631 XClientMessageEvent event = { 0 };
2632 event.type = ClientMessage;
2633 event.message_type = atoms[_net_wm_state];
2634 event.display = xGlobalDisplay;
2636 event.window = window.windowHandle;
2637 event.send_event = 1;
2639 event.data.l[0] = 2; // 1;
2640 event.data.l[1] = atoms[_net_wm_state_hidden];
2641 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, &event);
2644 XClientMessageEvent event = { 0 };
2645 event.type = ClientMessage;
2646 event.message_type = atoms[wm_state];
2647 event.display = xGlobalDisplay;
2648 event.window = window.windowHandle;
2649 event.send_event = 1;
2651 event.data.l[0] = IconicState;
2652 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, &event);
2655 // printf("Attempting to minimize %s\n", window._class.name);
2656 XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
2660 if(!atomsSupported[_net_wm_state] || !window.nativeDecorations || (!((XWindowData)window.windowData).gotFrameExtents && window.state == maximized)) //((XWindowData)window.windowData).gotFrameExtents && (!window.nativeDecorations || window.state == state))
2662 // With native decorations, we do it the first time
2663 // or the WM (Gnome) is sticking it to the top/right!
2665 int x = window.position.x;
2666 int y = window.position.y;
2667 int w = window.size.w;
2668 int h = window.size.h;
2670 if(window.nativeDecorations)
2672 XWindowData windowData = window.windowData;
2673 x -= windowData.decor.left;
2674 y -= windowData.decor.top;
2676 w -= windowData.decor.left + windowData.decor.right;
2677 h -= windowData.decor.top + windowData.decor.bottom;
2682 XMoveResizeWindow(xGlobalDisplay,
2683 (X11Window)window.windowHandle,
2686 UpdateRootWindow(window);
2688 if(atomsSupported[_net_wm_state])
2690 // Maximize / Restore the window
2691 XClientMessageEvent event = { 0 };
2692 event.type = ClientMessage;
2693 event.message_type = atoms[_net_wm_state];
2694 event.display = xGlobalDisplay;
2696 event.window = (X11Window)window.windowHandle;
2697 event.send_event = 1;
2699 event.data.l[0] = (state == maximized) ? 1 : 0;
2700 event.data.l[1] = atoms[_net_wm_state_maximized_vert];
2701 event.data.l[2] = atoms[_net_wm_state_maximized_horz];
2702 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false,
2703 SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2708 XUnmapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2709 //XFlush(xGlobalDisplay);
2713 void FlashRootWindow(Window window)
2715 XClientMessageEvent event = { 0 };
2716 // printf("Attempting to flash root window\n");
2717 event.type = ClientMessage;
2718 event.message_type = atoms[_net_wm_state];
2719 event.display = xGlobalDisplay;
2721 event.window = (X11Window)window.windowHandle;
2722 event.send_event = 1;
2724 event.data.l[0] = 1;
2725 event.data.l[1] = atoms[_net_wm_state_demands_attention];
2726 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2729 void ActivateRootWindow(Window window)
2731 if(!window.parent || !window.parent.display)
2733 if(!window.style.hidden && window.created)
2735 //printf("Activate root window %s\n", window._class.name);
2736 XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2737 XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2738 WaitForViewableWindow(window);
2739 if(atomsSupported[_net_active_window])
2741 XClientMessageEvent event = { 0 };
2742 event.type = ClientMessage;
2743 event.message_type = atoms[_net_active_window];
2744 event.display = xGlobalDisplay;
2746 event.window = (X11Window)window.windowHandle;
2747 event.send_event = 1;
2749 event.data.l[0] = 0;
2751 //event.data.l[0] = 2;
2752 //event.data.l[1] = timeStamp;
2755 //event.data.l[1] = atoms[_net_wm_user_time];
2756 //event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
2759 //printf("(ActivateRootWindow) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, window);
2762 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2763 //#if defined(__APPLE__)
2764 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
2768 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
2773 // --- Mouse-based window movement ---
2775 void StartMoving(Window window, int x, int y, bool fromKeyBoard)
2780 void StopMoving(Window window)
2785 // -- Mouse manipulation ---
2787 void GetMousePosition(int *x, int *y)
2789 X11Window rootWindow, childWindow;
2792 ((GuiApplication)__thisModule.application).Lock();
2793 //XLockDisplay(xGlobalDisplay);
2794 XQueryPointer(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &childWindow,
2795 &rootWindow, x, y, &mx, &my, &state);
2796 //XUnlockDisplay(xGlobalDisplay);
2797 ((GuiApplication)__thisModule.application).Unlock();
2800 void SetMousePosition(int x, int y)
2805 void SetMouseRange(Window window, Box box)
2807 ((GuiApplication)__thisModule.application).Lock();
2808 //XLockDisplay(xGlobalDisplay);
2809 if(box && box.left > 0 && box.top > 0 &&
2810 box.right < guiApp.desktop.clientSize.w - 1 && box.bottom < guiApp.desktop.clientSize.h - 1)
2812 if(!window.parent || !window.parent.display)
2814 XMoveResizeWindow(xGlobalDisplay, confineWindow, box.left + desktopX, box.top + desktopY,
2815 box.right - box.left + 1, box.bottom - box.top + 1);
2817 if(!restrictedWindow)
2818 XMapWindow(xGlobalDisplay, confineWindow);
2820 XGrabPointer(xGlobalDisplay, (X11Window) window.rootWindow.windowHandle, False,
2821 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2822 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
2824 restrictedWindow = window;
2827 else if(restrictedWindow)
2829 if(capturedWindow != None)
2831 XGrabPointer(xGlobalDisplay, (X11Window)capturedWindow,
2832 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2833 GrabModeAsync, None, fullScreenMode ? nullCursor : None, CurrentTime);
2836 XUngrabPointer(xGlobalDisplay, CurrentTime);
2838 if(restrictedWindow)
2839 XUnmapWindow(xGlobalDisplay, confineWindow);
2841 restrictedWindow = null;
2843 //XUnlockDisplay(xGlobalDisplay);
2844 ((GuiApplication)__thisModule.application).Unlock();
2847 void SetMouseCapture(Window window)
2849 //*XLockDisplay(xGlobalDisplay);
2852 if(!window.parent || !window.parent.display)
2854 XGrabPointer(xGlobalDisplay, (X11Window)window.windowHandle,
2855 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2856 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2858 capturedWindow = (X11Window) window.windowHandle;
2861 else if(capturedWindow != None)
2863 if(restrictedWindow)
2864 XGrabPointer(xGlobalDisplay, (X11Window) restrictedWindow.rootWindow.windowHandle, False,
2865 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2866 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
2868 XUngrabPointer(xGlobalDisplay, CurrentTime);
2869 capturedWindow = None;
2871 //*XUnlockDisplay(xGlobalDisplay);
2874 // -- Mouse cursor ---
2876 void SetMouseCursor(Window window, int cursor)
2878 XDefineCursor(xGlobalDisplay, (X11Window) window.rootWindow.windowHandle,
2879 cursor == -1 ? (X11Cursor)0 : systemCursors[(SystemCursor)cursor]);
2884 void SetCaret(int x, int y, int size)
2886 Window caretOwner = guiApp.caretOwner;
2887 Window window = caretOwner ? caretOwner.rootWindow : null;
2888 if(window && window.windowData && setICPosition)
2890 XWindowData windowData = window.windowData;
2891 if(windowData && windowData.ic)
2893 XPoint cursor_location =
2895 (short)(caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x),
2896 (short)(caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y)
2898 XVaNestedList argList = XVaCreateNestedList(0, XNSpotLocation, &cursor_location, NULL);
2899 XSetICValues(windowData.ic, XNPreeditAttributes, argList, 0);
2904 void ClearClipboard()
2906 //*XLockDisplay(xGlobalDisplay);
2909 delete clipBoardData;
2910 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard], None, CurrentTime);
2912 //*XUnlockDisplay(xGlobalDisplay);
2915 bool AllocateClipboard(ClipBoard clipBoard, uint size)
2917 bool result = false;
2918 if((clipBoard.text = new0 byte[size]))
2923 bool SaveClipboard(ClipBoard clipBoard)
2925 bool result = false;
2926 //*XLockDisplay(xGlobalDisplay);
2929 Window rootWindow = guiApp.desktop;
2932 for(rootWindow = rootWindow.children.first;
2933 rootWindow && !rootWindow.windowHandle;
2934 rootWindow = rootWindow.next);
2937 delete clipBoardData;
2939 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard],
2940 (X11Window) rootWindow.windowHandle, CurrentTime);
2941 clipBoardData = clipBoard.text;
2942 clipBoard.text = null;
2945 //*XUnlockDisplay(xGlobalDisplay);
2949 bool LoadClipboard(ClipBoard clipBoard)
2951 bool result = false;
2953 //*XLockDisplay(xGlobalDisplay);
2954 // The data is inside this client...
2957 clipBoard.text = new char[strlen(clipBoardData)+1];
2958 strcpy(clipBoard.text, clipBoardData);
2961 // The data is with another client...
2964 Window rootWindow = guiApp.desktop;
2967 for(rootWindow = rootWindow.children.first;
2968 rootWindow && !rootWindow.windowHandle;
2969 rootWindow = rootWindow.next);
2973 Atom selAtom = atoms[clipboard];
2974 X11Window owner = XGetSelectionOwner(xGlobalDisplay, selAtom);
2978 for(atom = atoms[utf8_string]; atom; atom = ((atom == atoms[utf8_string]) ? XA_STRING : 0))
2981 XConvertSelection(xGlobalDisplay, selAtom, atom, atoms[app_selection] /*None*/, (X11Window) rootWindow.windowHandle, CurrentTime);
2982 XIfEvent(xGlobalDisplay, (XEvent *) &e, EventChecker, (void *)SelectionNotify);
2983 if(e.type == SelectionNotify)
2985 XSelectionEvent * selection = (XSelectionEvent *) &e;
2986 //printf("Got a SelectionNotify with %d (%s)\n", selection->_property, XGetAtomName(xGlobalDisplay, selection->_property));
2988 unsigned long len, size = 0, dummy;
2991 XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, 0, False, AnyPropertyType, &type, &format, &len, &size, &data);
2999 if(XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, size, False,
3000 AnyPropertyType, &type,&format,&len, &dummy, &data) == Success)
3002 clipBoard.text = new char[size+1];
3003 strncpy(clipBoard.text, data, size);
3004 clipBoard.text[size] = '\0';
3015 //*XUnlockDisplay(xGlobalDisplay);
3019 void UnloadClipboard(ClipBoard clipBoard)
3021 delete clipBoard.text;
3024 // --- State based input ---
3026 bool AcquireInput(Window window, bool state)
3031 bool GetMouseState(MouseButtons * buttons, int * x, int * y)
3033 bool result = false;
3039 bool GetJoystickState(int device, Joystick joystick)
3041 bool result = false;
3042 #if defined(__linux__)
3043 if(joystick && device < 4)
3045 struct JS_DATA_TYPE js = { 0 };
3046 memset(joystick, 0, sizeof(Joystick));
3047 if(joystickFD[device] && read(joystickFD[device], &js, JS_RETURN) == JS_RETURN)
3049 joystick.buttons = js.buttons;
3050 joystick.x = js.x - 128;
3051 joystick.y = js.y - 128;
3059 bool GetKeyState(Key key)
3065 void SetTimerResolution(uint hertz)
3067 timerDelay = hertz ? (1000000 / hertz) : MAXINT;
3072 hiResTimer.delay = 1000000 / hertz;
3073 hiResTimer.Create();
3078 bool SetIcon(Window window, BitmapResource resource)
3083 if(bitmap.Load(resource.fileName, null, null))
3085 unsigned long * icon = new unsigned long[2 + bitmap.width * bitmap.height];
3086 bitmap.Convert(null, pixelFormat888, null);
3087 icon[0] = bitmap.width;
3088 icon[1] = bitmap.height;
3089 if(sizeof(long) != sizeof(uint32))
3092 for(c = 0; c < bitmap.width * bitmap.height; c++)
3093 icon[c+2] = ((uint32 *)bitmap.picture)[c];
3096 memcpy(icon + 2, bitmap.picture, bitmap.width * bitmap.height * sizeof(uint32));
3097 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_icon],
3098 XA_CARDINAL,32,PropModeReplace, (byte *)icon, 2+bitmap.width*bitmap.height);
3107 default dllexport void * __attribute__((stdcall)) IS_XGetDisplay()
3109 return xGlobalDisplay;
3112 default dllexport void * __attribute__((stdcall)) IS_XGetWindow(Window window)
3114 return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;