1 namespace gui::drivers;
4 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
5 import "OpenGLDisplayDriver"
8 #if (defined(__unix__) || defined(__APPLE__)) && !defined(ECERE_MINIGLX)
12 #define property _property
17 #include <linux/joystick.h>
19 #include <sys/param.h>
31 #define Window X11Window
32 #define Cursor X11Cursor
34 #define Display X11Display
36 #define KeyCode X11KeyCode
37 #define Picture X11Picture
39 #include <X11/Xatom.h>
41 #include <X11/Xresource.h>
42 #include <X11/Xutil.h>
43 #include <X11/XKBlib.h>
44 #include <X11/keysym.h>
45 #include <sys/fcntl.h>
47 #include <X11/extensions/Xrender.h>
48 #include <X11/extensions/XShm.h>
65 import "XDisplayDriver"
67 // These are the events we care about
68 #define EVENT_MASK StructureNotifyMask | \
69 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | \
70 KeyPressMask | KeyReleaseMask | ExposureMask | FocusChangeMask | \
73 void * xGlobalDisplay;
76 static XContext windowContext;
77 static Window lastActive;
79 static char * clipBoardData;
81 static Thread /* xThread, */ timerThread;
82 static bool xTerminate;
83 static Semaphore xSemaphore { };
84 static Mutex xMutex { };
85 static bool fullScreenMode;
86 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
88 static int nullCursor;
89 static X11Window capturedWindow = None;
90 static Window restrictedWindow = null;
91 static bool gotAnXEvent = false;
93 static int joystickFD[4];
94 static X11Window activeWindow;
96 static enum AtomIdents
98 clipboard, multiple, targets, utf8_string, wm_delete_window, wm_hints, wm_name, wm_protocols, wm_state, wm_take_focus, wm_transient_for,
100 _net_active_window, _net_current_desktop, _net_number_of_desktops, _net_wm_icon, _net_wm_name, _net_wm_pid,
101 _net_wm_state, _net_wm_state_demands_attention, _net_wm_state_hidden, _net_wm_user_time, _net_wm_window_type,
102 _net_wm_window_type_desktop, _net_wm_window_type_dialog, _net_wm_window_type_dock, _net_wm_window_type_dropdown_menu,
103 _net_wm_window_type_menu, _net_wm_window_type_normal, _net_wm_window_type_popup_menu, _net_wm_window_type_splash,
104 _net_wm_window_type_toolbar, _net_wm_window_type_utility, _net_workarea, _net_frame_extents, _net_request_frame_extents,
105 _net_wm_state_maximized_vert, _net_wm_state_maximized_horz, _net_wm_state_hidden,
109 static Atom atoms[AtomIdents];
111 static const char *atomNames[AtomIdents] = {
112 "CLIPBOARD", //clipboard
113 "MULTIPLE", //multiple
115 "UTF8_STRING", //utf8_string
116 "WM_DELETE_WINDOW", //wm_delete_window
117 "WM_HINTS", //wm_hints
119 "WM_PROTOCOLS", //wm_protocols
120 "WM_STATE", //wm_state
121 "WM_TAKE_FOCUS", //wm_take_focus
122 "WM_TRANSIENT_FOR", //wm_transient_for
123 "_MOTIF_WM_HINTS", //_motif_wm_hints
124 "_NET_ACTIVE_WINDOW", //_net_active_window
125 "_NET_CURRENT_DESKTOP", //_net_current_desktop
126 "_NET_NUMBER_OF_DESKTOPS", //_net_number_of_desktops
127 "_NET_WM_ICON", //_net_wm_icon
128 "_NET_WM_NAME", //_net_wm_name
129 "_NET_WM_PID", //_net_wm_pid
130 "_NET_WM_STATE", //_net_wm_state
131 "_NET_WM_STATE_DEMANDS_ATTENTION", //_net_wm_state_demands_attention
132 "_NET_WM_STATE_HIDDEN", //_net_wm_state_hidden
133 "_NET_WM_USER_TIME", //_net_wm_user_time
134 "_NET_WM_WINDOW_TYPE", //_net_wm_window_type
135 "_NET_WM_WINDOW_TYPE_DESKTOP", //_net_wm_window_type_desktop
136 "_NET_WM_WINDOW_TYPE_DIALOG", //_net_wm_window_type_dialog
137 "_NET_WM_WINDOW_TYPE_DOCK", //_net_wm_window_type_dock
138 "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", //_net_wm_window_type_dropdown_menu
139 "_NET_WM_WINDOW_TYPE_MENU", //_net_wm_window_type_menu
140 "_NET_WM_WINDOW_TYPE_NORMAL", //_net_wm_window_type_normal
141 "_NET_WM_WINDOW_TYPE_POPUP_MENU", //_net_wm_window_type_popup_menu
142 "_NET_WM_WINDOW_TYPE_SPLASH", //_net_wm_window_type_splash
143 "_NET_WM_WINDOW_TYPE_TOOLBAR", //_net_wm_window_type_toolbar
144 "_NET_WM_WINDOW_TYPE_UTILITY", //_net_wm_window_type_utility
145 "_NET_WORKAREA", //_net_workarea
146 "_NET_FRAME_EXTENTS", // _net_frame_extents
147 "_NET_REQUEST_FRAME_EXTENTS", // _net_request_frame_extents
148 "_NET_WM_STATE_MAXIMIZED_VERT", // _net_wm_state_maximized_vert
149 "_NET_WM_STATE_MAXIMIZED_HORZ", // _net_wm_state_maximized_horz
150 "_NET_WM_STATE_HIDDEN", // _net_wm_state_hidden
154 static bool autoRepeatDetectable;
155 static bool setICPosition;
157 PixelFormat xSystemPixelFormat;
158 Visual * xSystemVisual;
161 static Time timeStamp;
163 class XWindowData : struct
166 XVisualInfo * visual;
171 bool gotFrameExtents;
174 bool XGetBorderWidths(Window window, Box box)
176 XWindowData windowData = window.windowData;
179 box = windowData.decor;
185 static Visual * FindFullColorVisual(X11Display *dpy, int * depth)
188 XVisualInfo *vinfo_ret;
191 vinfo._class = TrueColor;
192 vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
198 if(vinfo_ret[numitems-1].depth > maxdepth)
199 maxdepth = vinfo_ret[numitems-1].depth;
202 XFree((void *) vinfo_ret);
205 if(XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, TrueColor, &vinfo))
215 _NET_WM_STATE_MODAL, ATOM
216 _NET_WM_STATE_STICKY, ATOM
217 _NET_WM_STATE_MAXIMIZED_VERT, ATOM
218 _NET_WM_STATE_MAXIMIZED_HORZ, ATOM
219 _NET_WM_STATE_SHADED, ATOM
220 _NET_WM_STATE_SKIP_TASKBAR, ATOM
221 _NET_WM_STATE_SKIP_PAGER, ATOM
222 _NET_WM_STATE_HIDDEN, ATOM
223 _NET_WM_STATE_FULLSCREEN, ATOM
224 _NET_WM_STATE_ABOVE, ATOM
225 _NET_WM_STATE_BELOW, ATOM
226 _NET_WM_STATE_DEMANDS_ATTENTION, ATOM
230 static void RepositionDesktop(bool updateChildren)
234 Screen * x_screen = XDefaultScreenOfDisplay(xGlobalDisplay);
238 int format, len, fill;
241 w = XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
242 h = XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
243 x_root = XRootWindowOfScreen(x_screen);
245 if(atoms[_net_number_of_desktops] != None)
247 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_number_of_desktops], 0, 1, False,
248 XA_CARDINAL, &type, &format, &len, &fill,
251 printf("cant get xa desktops property\n");
257 desktops = *(int *)data;
259 //printf("_NET_NUMBER_OF_DESKTOPS is %d\n", desktops);
266 if(atoms[_net_current_desktop] != None)
268 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_current_desktop], 0, 1, False,
269 XA_CARDINAL, &type, &format, &len, &fill,
272 printf("cant get xa current property\n");
277 current = *(int *)data;
281 //printf("_NET_CURRENT_DESKTOP is %d\n", current);
284 if(atoms[_net_workarea] != None)
288 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_workarea], 0, (4 * 32),
289 False, AnyPropertyType, &type, &format, &len,
290 &fill, &data) != Success)
292 //printf("warning\n");
296 if(type == None || format == 0)
308 workareas = (int *)data;
310 x = workareas[current * 4];
311 y = workareas[current * 4 + 1];
312 w = workareas[current * 4 + 2];
313 h = workareas[current * 4 + 3];
315 //printf("_NET_WORKAREA is x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
317 // printf("Work Area width: %d, height %d\n", w, h);
320 if(desktopX != x || desktopY != y || desktopW != w || desktopH != h)
322 guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
330 /****************************************************************************
331 /// PRIVATE UTILITY FUNCTIONS /////////////
332 ****************************************************************************/
333 // --- Keyboard Input ---
334 #define KEYCODE_HOME 0x59
335 #define KEYCODE_UP 0x5A
336 #define KEYCODE_PGUP 0x5B
337 #define KEYCODE_LEFT 0x5C
338 #define KEYCODE_RIGHT 0x5E
339 #define KEYCODE_END 0x5F
340 #define KEYCODE_DOWN 0x60
341 #define KEYCODE_PGDWN 0x61
342 #define KEYCODE_INS 0x62
343 #define KEYCODE_DEL 0x63
344 #define KEYCODE_SLASH 0x68
347 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
348 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
349 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
350 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
351 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
352 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
353 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
354 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
355 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
356 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
357 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
358 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
359 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
360 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
363 static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEvent * event)
368 KeySym keysym = NoSymbol;
371 static int bufsize = 16;
372 static char *buf = NULL;
373 XWindowData windowData = window.windowData;
376 if(window.destroyed) return;
378 // Logf("Got 0x%x (%d)\n", keyCode, key);
380 // Translate the key code
383 //Logf("Got 0x%x (%d)\n", key, key);
387 case KEYCODE_HOME: key = home; break;
388 case KEYCODE_UP: key = up; break;
389 case KEYCODE_PGUP: key = pageUp; break;
390 case KEYCODE_LEFT: key = left; break;
391 case KEYCODE_RIGHT: key = right; break;
392 case KEYCODE_END: key = end; break;
393 case KEYCODE_DOWN: key = down; break;
394 case KEYCODE_PGDWN: key = pageDown; break;
395 case KEYCODE_INS: key = insert; break;
396 case KEYCODE_DEL: key = del; break;
397 case KEYCODE_SLASH: key = keyPadSlash; break;
400 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
404 Logf("Key up: %3d (%2x)\n", key, key);
406 Logf("Key down: %3d (%2x)\n", key, key);
410 buf = malloc(bufsize);
411 if(windowData && windowData.ic)
413 buflength = XmbLookupString(windowData.ic, event, buf, bufsize, &keysym, &status);
414 if (status == XBufferOverflow)
416 buf = realloc(buf, (bufsize = buflength));
417 buflength = XmbLookupString(windowData.ic, event, buf, bufsize, &keysym, &status);
419 if(status != XLookupKeySym && status != XLookupBoth && release == 1)
420 keysym = XLookupKeysym(event, 0);
424 keysym = XLookupKeysym(event, 0);
427 // printf("Got keysym %d (%x)\n", keysym, keysym);
428 if(keysym != NoSymbol) //status == XLookupKeySym || status == XLookupBoth)
432 case XK_BackSpace: key = backSpace; break;
433 case XK_ISO_Left_Tab: key = tab; break;
434 case XK_Tab: key = tab; break;
437 case XK_Return: key = enter; break;
438 case XK_Pause: key = pauseBreak; break;
439 case XK_Scroll_Lock: key = scrollLock; break;
440 case XK_Sys_Req: key = printScreen; break;
441 case XK_Escape: key = escape; break;
442 case XK_Delete: key = del; break;
444 case XK_Home: key = home; break;
445 case XK_Left: key = left; break;
446 case XK_Up: key = up; break;
447 case XK_Right: key = right; break;
448 case XK_Down: key = down; break;
450 case XK_Page_Up: key = pageUp; break;
452 case XK_Page_Down: key = pageDown; break;
453 case XK_End: key = end; break;
459 case XK_Insert: key = insert; break;
466 case XK_Help: key = insert; break;
470 case XK_Mode_switch: key = leftAlt; break;
472 // case XK_script_switch:
473 case XK_Num_Lock: key = numLock; break;
477 case XK_KP_Enter: key = keyPadEnter; break;
482 case XK_KP_Home: key = keyPadHome; break;
483 case XK_KP_Left: key = keyPadLeft; break;
484 case XK_KP_Up: key = keyPadUp; break;
485 case XK_KP_Right: key = keyPadRight; break;
486 case XK_KP_Down: key = keyPadDown; break;
488 case XK_KP_Page_Up: key = keyPadPageUp; break;
490 case XK_KP_Page_Down:key = keyPadPageDown; break;
491 case XK_KP_End: key = keyPadEnd; break;
493 case XK_KP_Insert: key = keyPadInsert; break;
494 case XK_KP_Delete: key = keyPadDelete; break;
496 case XK_KP_Multiply: key = keyPadStar; break;
497 case XK_KP_Add: key = keyPadPlus; break;
498 case XK_KP_Separator:key = keyPadDelete; break;
499 case XK_KP_Subtract: key = keyPadMinus; break;
500 // case XK_KP_Decimal:
501 case XK_KP_Divide: key = keyPadSlash; break;
503 case XK_KP_0: key = keyPad0; break;
504 case XK_KP_1: key = keyPad1; break;
505 case XK_KP_2: key = keyPad2; break;
506 case XK_KP_3: key = keyPad3; break;
507 case XK_KP_4: key = keyPad4; break;
508 case XK_KP_5: key = keyPad5; break;
509 case XK_KP_6: key = keyPad6; break;
510 case XK_KP_7: key = keyPad7; break;
511 case XK_KP_8: key = keyPad8; break;
512 case XK_KP_9: key = keyPad9; break;
514 case XK_F1: key = f1; break;
515 case XK_F2: key = f2; break;
516 case XK_F3: key = f3; break;
517 case XK_F4: key = f4; break;
518 case XK_F5: key = f5; break;
519 case XK_F6: key = f6; break;
520 case XK_F7: key = f7; break;
521 case XK_F8: key = f8; break;
522 case XK_F9: key = f9; break;
523 case XK_F10: key = f10; break;
524 case XK_F11: key = f11; break;
526 case XK_F12: key = f12; break;
576 case XK_Shift_L: key = leftShift; break;
577 case XK_Shift_R: key = rightShift; break;
578 case XK_Control_L: key = leftControl; break;
579 case XK_Control_R: key = rightControl; break;
580 case XK_Caps_Lock: key = capsLock; break;
581 // case XK_Shift_Lock:
584 case XK_Alt_L: key = leftAlt; break;
585 case XK_Alt_R: key = rightAlt; break;
591 case XK_space: key = space; break;
592 case XK_exclam: key = bang; break;
593 case XK_quotedbl: key = doubleQuote; break;
594 case XK_numbersign: key = pound; break;
595 case XK_dollar: key = dollar; break;
596 case XK_percent: key = percent; break;
597 case XK_ampersand: key = ampersand; break;
598 case XK_apostrophe: key = quote; break;
599 // case XK_quoteright: key = quote; break;
600 case XK_parenleft: key = leftParanthesis; break;
601 case XK_parenright: key = rightParanthesis; break;
602 case XK_asterisk: key = star; break;
603 case XK_plus: key = plus; break;
604 case XK_comma: key = comma; break;
605 case XK_minus: key = minus; break;
606 case XK_period: key = period; break;
607 case XK_slash: key = slash; break;
608 case XK_0: key = k0; break;
609 case XK_1: key = k1; break;
610 case XK_2: key = k2; break;
611 case XK_3: key = k3; break;
612 case XK_4: key = k4; break;
613 case XK_5: key = k5; break;
614 case XK_6: key = k6; break;
615 case XK_7: key = k7; break;
616 case XK_8: key = k8; break;
617 case XK_9: key = k9; break;
618 case XK_colon: key = colon; break;
619 case XK_semicolon: key = semicolon; break;
620 case XK_less: key = smallerThan; break;
621 case XK_equal: key = equal; break;
622 case XK_greater: key = greaterThan; break;
623 case XK_question: key = questionMark; break;
624 case XK_at: key = ampersand; break;
625 case XK_A: key = a; break;
626 case XK_B: key = b; break;
627 case XK_C: key = c; break;
628 case XK_D: key = d; break;
629 case XK_E: key = e; break;
630 case XK_F: key = f; break;
631 case XK_G: key = g; break;
632 case XK_H: key = h; break;
633 case XK_I: key = i; break;
634 case XK_J: key = j; break;
635 case XK_K: key = k; break;
636 case XK_L: key = l; break;
637 case XK_M: key = m; break;
638 case XK_N: key = n; break;
639 case XK_O: key = o; break;
640 case XK_P: key = p; break;
641 case XK_Q: key = q; break;
642 case XK_R: key = r; break;
643 case XK_S: key = s; break;
644 case XK_T: key = t; break;
645 case XK_U: key = u; break;
646 case XK_V: key = v; break;
647 case XK_W: key = w; break;
648 case XK_X: key = x; break;
649 case XK_Y: key = y; break;
650 case XK_Z: key = z; break;
651 case XK_bracketleft: key = leftBracket; break;
652 case XK_backslash: key = backSlash; break;
653 case XK_bracketright: key = rightBracket; break;
654 case XK_asciicircum: key = circumflex; break;
655 case XK_underscore: key = underscore; break;
656 // case XK_grave: key = backQuote; break;
657 case XK_quoteleft: key = backQuote; break;
658 case XK_a: key = a; break;
659 case XK_b: key = b; break;
660 case XK_c: key = c; break;
661 case XK_d: key = d; break;
662 case XK_e: key = e; break;
663 case XK_f: key = f; break;
664 case XK_g: key = g; break;
665 case XK_h: key = h; break;
666 case XK_i: key = i; break;
667 case XK_j: key = j; break;
668 case XK_k: key = k; break;
669 case XK_l: key = l; break;
670 case XK_m: key = m; break;
671 case XK_n: key = n; break;
672 case XK_o: key = o; break;
673 case XK_p: key = p; break;
674 case XK_q: key = q; break;
675 case XK_r: key = r; break;
676 case XK_s: key = s; break;
677 case XK_t: key = t; break;
678 case XK_u: key = u; break;
679 case XK_v: key = v; break;
680 case XK_w: key = w; break;
681 case XK_x: key = x; break;
682 case XK_y: key = y; break;
683 case XK_z: key = z; break;
684 case XK_braceleft: key = leftBracket; break;
685 case XK_bar: key = pipe; break;
686 case XK_braceright: key = rightBracket; break;
687 case XK_asciitilde: key = tilde; break;
692 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
693 // 127 is delete, we don't treat that as a character (Use (SmartKey)key == del)
694 if(ch == 128 || ch == 127) ch = 0;
697 if(keysym == XK_ISO_Left_Tab)
699 if(key != leftShift && key != rightShift && event->state & ShiftMask)
701 if(key != leftControl && key != rightControl && event->state & ControlMask)
703 if(key != leftAlt && key != rightAlt && event->state & Mod1Mask)
707 if(key != leftAlt && key != rightAlt && event->state & (1<<13))
715 // 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);
721 if(windowData && windowData.ic) ch = buflength ? UTF8GetChar(buf, &numBytes) : 0;
722 if(ch == 127) ch = 0;
723 // printf("Release! %d %d %d\n", keysym, code, ch);
724 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, code, ch);
731 if(windowData.ic && buflength)
733 for(c = 0; c<buflength;)
736 ch = UTF8GetChar(buf + c, &numBytes);
737 if(ch == 127) ch = 0;
738 if(!numBytes) c = buflength;
739 result = window.KeyMessage((c == 0) ?
740 __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown : __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit,
741 (c == 0) ? code : 0, ch);
746 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, code, ch);
750 if(buflength && windowData.ic)
751 for(c = 0; c<buflength;)
754 ch = UTF8GetChar(buf + c, &numBytes);
755 if(ch == 127) ch = 0;
756 if(!numBytes) c = buflength;
757 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
761 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
769 static uint E_CALL XEventThread(void * data)
775 if(xTerminate) break;
778 XPeekEvent(xGlobalDisplay, &e);
780 guiApp.SignalEvent();
787 static Bool EventChecker(void *display, XEvent *event, char * data)
789 return (!data || (event->type == (int) data)) && event->type != NoExpose && event->type != GraphicsExpose;
792 static Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
794 return (!data || ((event->window == (int) data)) && event->type == ConfigureNotify;
797 static uint timerDelay = MAXINT;
798 #define RESOLUTION (18.2 * 100)
799 static uint XTimerThread(Thread thread)
801 int s = ConnectionNumber(xGlobalDisplay);
803 Time lastTime, thisTime;
804 lastTime = GetTime();
809 bool waitSemaphore = false;
810 fd_set readSet, writeSet, exceptSet;
811 struct timeval tv = { (timerDelay == MAXINT) ? 0 : (timerDelay / 1000000), (timerDelay == MAXINT) ? (int)(1000000 / 18.2) : (timerDelay % 1000000) };
813 if(xTerminate) break;
818 FD_SET(s, &exceptSet);
820 if(select(s + 1, &readSet, null, null, &tv))
822 if(FD_ISSET(s, &readSet))
826 guiApp.SignalEvent();
831 Sleep(1.0 / RESOLUTION);
832 thisTime = GetTime();
833 if(xTerminate) break;
834 if(thisTime - lastTime > (1.0 / 18.2))
836 guiApp.SignalEvent();
844 XLockDisplay(xGlobalDisplay);
845 if(XCheckIfEvent(xGlobalDisplay, &xEvent, EventChecker, null))
848 guiApp.SignalEvent();
850 XUnlockDisplay(xGlobalDisplay);
859 static int MyXErrorHandler(X11Display * display, XErrorEvent * event)
863 XGetErrorText(xGlobalDisplay, event->error_code, buffer, sizeof(buffer));
864 Logf("X Error: %s\n", buffer);
868 static int MyXIOErrorHandler(X11Display * display)
874 // Motif Hints (to get rid of the decorations)
875 #define MWM_HINTS_FUNCTIONS (1L << 0)
876 #define MWM_HINTS_DECORATIONS (1L << 1)
877 #define MWM_HINTS_INPUT_MODE (1L << 2)
878 #define MWM_HINTS_STATUS (1L << 3)
880 #define MWM_DECOR_ALL (1L << 0)
881 #define MWM_DECOR_BORDER (1L << 1)
882 #define MWM_DECOR_RESIZEH (1L << 2)
883 #define MWM_DECOR_TITLE (1L << 3)
884 #define MWM_DECOR_MENU (1L << 4)
885 #define MWM_DECOR_MINIMIZE (1L << 5)
886 #define MWM_DECOR_MAXIMIZE (1L << 6)
888 #define MWM_FUNC_ALL (1L << 0)
889 #define MWM_FUNC_RESIZE (1L << 1)
890 #define MWM_FUNC_MOVE (1L << 2)
891 #define MWM_FUNC_MINIMIZE (1L << 3)
892 #define MWM_FUNC_MAXIMIZE (1L << 4)
893 #define MWM_FUNC_CLOSE (1L << 5)
904 static void WaitForViewableWindow(Window window)
907 //Logf("Wait for viewable %s\n", window.name);
908 XFlush(xGlobalDisplay);
909 //while(attempts++ < 40)
912 XWindowAttributes attributes = { 0 };
914 if(!XGetWindowAttributes(xGlobalDisplay, (int)window.windowHandle, &attributes))
916 if(attributes.map_state == IsViewable)
919 Sleep(1.0 / RESOLUTION);
923 /****************************************************************************
924 /// DRIVER IMPLEMENTATION /////////////
925 ****************************************************************************/
927 /*static */class HiResTimer : Thread
946 // Sleep(delay / 1000000.0);
947 struct timeval tv = { delay / 1000000, delay % 1000000 };
948 select(0,null,null,null, &tv);
949 guiApp.SignalEvent();
955 static HiResTimer hiResTimer { };
961 #include <sys/signal.h>
972 static int terminatePid;
974 static void SigIntHandler(int value)
976 // printf("SigHandler %d\n", getpid());
977 if(!terminatePid || terminatePid == getpid())
980 terminatePid = getpid();
981 // printf("terminateX now equals %d\n", terminateX);
982 if(guiApp && guiApp.semaphore)
983 guiApp.semaphore.Release();
986 struct shmid_ds info;
987 int maxid = shmctl (0, SHM_INFO, &info);
989 int thisPid = getpid();
996 for(id = 0; id <= maxid; id++)
998 struct shmid_ds shmseg;
1000 if((shmid = shmctl(id, SHM_STAT, &shmseg)) >= 0)
1002 if(shmseg.shm_cpid == pid || shmseg.shm_cpid == thisPid)
1004 printf("%d (%d) belongs to us (%d)\n", shmid, id, shmseg.shm_cpid);
1005 shmctl(shmid, IPC_RMID, 0);
1013 /*else if(guiApp.desktop)
1014 guiApp.desktop.Destroy(0);*/
1017 class XInterface : Interface
1019 class_property(name) = "X";
1021 // --- User Interface System ---
1024 setlocale(LC_ALL, "en_US.UTF-8");
1027 XSetLocaleModifiers("");
1028 XSetErrorHandler(MyXErrorHandler);
1029 XSetIOErrorHandler(MyXIOErrorHandler);
1031 signal(SIGINT, SigIntHandler);
1034 xGlobalDisplay = XOpenDisplay(null);
1036 joystickFD[0] = open("/dev/js0", O_RDONLY);
1037 joystickFD[1] = open("/dev/js1", O_RDONLY);
1038 joystickFD[2] = open("/dev/js2", O_RDONLY);
1039 joystickFD[3] = open("/dev/js3", O_RDONLY);
1043 XWindowAttributes attributes = { 0 };
1044 XGetWindowAttributes(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &attributes);
1045 xSystemDepth = attributes.depth;
1046 xSystemVisual = attributes.visual;
1047 switch(xSystemDepth)
1049 case 32: case 24: xSystemPixelFormat = pixelFormat888; break;
1054 XVisualInfo *vinfo_ret;
1057 vinfo.visualid = XVisualIDFromVisual(xSystemVisual);
1058 vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &vinfo, &numitems);
1061 xSystemPixelFormat = (vinfo_ret->green_mask == 0x3E0) ? pixelFormat555 : pixelFormat565;
1063 if(GetXRenderFormat(xSystemPixelFormat, 0))
1065 // printf("Got X Render format %d\n", xSystemPixelFormat);
1069 printf("No X Format?\n");
1070 xSystemPixelFormat = (xSystemPixelFormat == pixelFormat555) ? pixelFormat565 : pixelFormat555;
1072 // vinfo._class = DirectColor;
1074 vinfo.bits_per_rgb = 5;
1075 vinfo.depth = xSystemDepth;
1076 vinfo.green_mask = (xSystemPixelFormat == pixelFormat555) ? 0x3E0 : 0x7E0;
1077 vinfo.red_mask = (xSystemPixelFormat == pixelFormat555) ? 0x7C00 : 0xF800;
1078 XFree((void *) vinfo_ret);
1079 if(XMatchVisualInfo(xGlobalDisplay, DefaultScreen(xGlobalDisplay), vinfo.depth, vinfo._class, &vinfo))
1080 //vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualDepthMask|VisualRedMaskMask|VisualGreenMaskMask|VisualBlueMaskMask, &vinfo, &numitems);
1083 //vinfo = *vinfo_ret;
1084 if(GetXRenderFormat(xSystemPixelFormat, 0))
1086 // printf("Got X Render format %d (second try)\n", xSystemPixelFormat);
1087 // printf("red mask: %x, green mask: %x, blue mask: %x\n", vinfo.red_mask,vinfo.green_mask, vinfo.blue_mask);
1089 xSystemVisual = vinfo.visual;
1092 printf("Could not get a 555 visual\n");
1098 // printf("Got a depth of %d\n", xSystemDepth);
1101 int major, minor, pixmaps;
1102 xSharedMemory = XShmQueryExtension(xGlobalDisplay) && XShmQueryVersion(xGlobalDisplay, &major, &minor, &pixmaps) && pixmaps;
1105 // printf("Opening IM\n");
1106 im = XOpenIM(xGlobalDisplay, null, null, null);
1109 XColor fore = { 0 }, back = { 0 };
1110 Pixmap pixmap = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1111 Pixmap mask = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1112 XSetWindowAttributes attributes = { 0 };
1114 XkbSetDetectableAutoRepeat(xGlobalDisplay, True, &autoRepeatDetectable);
1116 XInternAtoms(xGlobalDisplay, (char**)atomNames, AtomIdents::enumSize, False, atoms);
1119 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
1121 XSetWMProtocols(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), protocols, 2);
1125 if(atoms[_net_workarea] == None)
1126 printf("Warning: _NET_WORKAREA extension not supported\n");
1129 attributes.override_redirect = True;
1132 windowContext = XUniqueContext();
1135 nullCursor = XCreatePixmapCursor(xGlobalDisplay, pixmap, mask, &fore, &back, 0, 0);
1136 confineWindow = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
1137 0,0,1,1,0, CopyFromParent, InputOnly, xSystemVisual /*CopyFromParent*/, CWOverrideRedirect, &attributes);
1140 // IM initialization
1142 /*ic = XCreateIC(im, XNInputStyle,
1143 XIMPreeditNothing | XIMStatusNothing,
1144 // XIMPreeditNone | XIMStatusNone | XIMPreeditPosition | XIMStatusArea | XIMPreeditArea | XIMStatusArea | XIMPreeditNothing | XIMStatusNothing,
1145 XNClientWindow, confineWindow, XNFocusWindow, confineWindow, 0);
1150 XIMStyle clientCanDo;
1151 XIMStyle styleWeWillUse = null;
1153 XVaNestedList arglist;
1154 unsigned long imEventMask;
1156 XGetImValues(im, XNQueryInputStyle, &IMcando, null);
1159 XIMPreeditNone | XIMStatusNone |
1160 XIMPreeditPosition | XIMStatusArea |
1161 XIMPreeditArea | XIMStatusArea |
1162 XIMPreeditNothing | XIMStatusNothing;
1164 for(i=0; i<IMcando->count_styles; i++)
1167 tmpStyle = IMcando->support_styles[i];
1168 if ( ((tmpStyle & clientCanDo) == tmpStyle) && prefer(tmpStyle, styleWeWillUse) )
1169 styleWeWillUse = tmpStyle;
1171 if(styleWeWillUse = null)
1175 arglist = XVaCreateNestedList(0, XNFontSet, fontset,
1177 WhitePixel(xGlobalDisplay, screen),
1179 BlackPixel(xGlobalDisplay, screen),
1181 ic = XCreateIC(im, XNInputStyle, styleWeWillUse,
1182 XNClientWindow, window, XNFocusWindow, window,
1183 XNStatusAttributes, arglist,
1184 XNPreeditAttributes, arglist, NULL);
1189 XGetWindowAttributes(xGlobalDisplay, win, &winAtts);
1190 XGetICValues(ic, XNFilterEvents, &imEventMask, null);
1191 imEventMask |= winAtts.your_event_mask;
1192 XSelectInput(xGlobalDisplay, window, imEventMask);
1198 timerThread = Thread { };
1200 timerThread.Main = XTimerThread;
1201 timerThread.Create();
1214 // WHY WAS THIS COMMENTED HERE?
1215 // Probably because it was causing crashes, the proper fix should now be in DestroyRootWindow
1219 xSemaphore.Release();
1225 //XPutBackEvent(xGlobalDisplay, &e);
1229 /*if(windowData && windowData.ic)
1231 XDestroyIC(windowData.ic);
1232 windowData.ic = null;
1239 XCloseDisplay(xGlobalDisplay);
1240 xGlobalDisplay = null;
1242 if(joystickFD[0]) close(joystickFD[0]);
1243 if(joystickFD[1]) close(joystickFD[1]);
1244 if(joystickFD[2]) close(joystickFD[2]);
1245 if(joystickFD[3]) close(joystickFD[3]);
1248 #define DBLCLICK_DELAY 300 // 0.3 second
1249 #define DBLCLICK_DELTA 1
1251 bool ProcessInput(bool processAll)
1253 bool eventAvailable = false;
1256 if(!fullScreenMode) RepositionDesktop(true);
1258 //* XLockDisplay(xGlobalDisplay);
1259 while(!xTerminate && (/*gotAnXEvent || */XCheckIfEvent(xGlobalDisplay, &e, EventChecker, null)))
1261 Window window = null;
1262 XAnyEvent * thisEvent = (XAnyEvent *)&e;
1263 // printf("Got an event: %d\n", thisEvent->type);
1265 //if(gotAnXEvent) { thisEvent = (XAnyEvent *)&xEvent; gotAnXEvent = false; }
1267 if(im && XFilterEvent((union _XEvent *)thisEvent, None))
1269 eventAvailable = true;
1270 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1273 XWindowData windowData = window.windowData;
1274 static uint lastKeyCode = 0;
1275 switch(thisEvent->type)
1279 XKeyEvent * event = (XKeyEvent *) thisEvent;
1280 //*XUnlockDisplay(xGlobalDisplay);
1281 ProcessKeyMessage(window, event->keycode, (event->keycode == lastKeyCode) ? 2 : 0, event);
1282 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1283 lastKeyCode = event->keycode;
1288 XKeyEvent * event = (XKeyEvent *) thisEvent;
1291 if(!autoRepeatDetectable && XCheckIfEvent(xGlobalDisplay, (XEvent *)&nextEvent, EventChecker, (void *)KeyPress))
1293 if(im && XFilterEvent(&nextEvent, None))
1296 if(((XKeyEvent *)&nextEvent)->keycode == event->keycode)
1298 //*XUnlockDisplay(xGlobalDisplay);
1299 ProcessKeyMessage(window, event->keycode, 2, event);
1303 //*XUnlockDisplay(xGlobalDisplay);
1304 // printf("Keycode not the same :(\n");
1305 ProcessKeyMessage(window, event->keycode, 1, event);
1306 ProcessKeyMessage(window, ((XKeyEvent *)&nextEvent)->keycode, 0, (XKeyEvent *)&nextEvent);
1311 //*XUnlockDisplay(xGlobalDisplay);
1312 // printf("No KeyPress ahead\n");
1313 ProcessKeyMessage(window, event->keycode, 1, event);
1315 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1320 XButtonEvent * event = (XButtonEvent *) thisEvent;
1322 static double lastTime[3];
1323 static Window lastWindow[3];
1324 static Point lastPos[3];
1326 Modifiers keyFlags = 0;
1328 uint button, buttonDouble, whichButton;
1330 int x = event->x_root, y = event->y_root;
1332 if(event->button == Button1)
1334 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
1335 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
1337 buttonMask = Button1Mask;
1338 keyFlags.left = true;
1340 else if(event->button == Button3)
1342 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
1343 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
1345 buttonMask = Button3Mask;
1346 keyFlags.right = true;
1350 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
1351 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
1353 buttonMask = Button2Mask;
1354 keyFlags.middle = true;
1356 if(event->state & buttonMask)
1359 doubleClick = event->time - lastTime[whichButton] < DBLCLICK_DELAY &&
1360 window == lastWindow[whichButton] &&
1361 Abs(event->x_root - lastPos[whichButton].x) < DBLCLICK_DELTA &&
1362 Abs(event->y_root - lastPos[whichButton].y) < DBLCLICK_DELTA;
1363 lastTime[whichButton] = doubleClick ? 0 : event->time;
1364 lastWindow[whichButton] = window;
1365 lastPos[whichButton].x = event->x_root;
1366 lastPos[whichButton].y = event->y_root;
1368 if(event->state & ShiftMask) keyFlags.shift = true;
1369 if(event->state & ControlMask) keyFlags.ctrl = true;
1370 if(event->state & Mod1Mask) keyFlags.alt = true;
1371 if(event->state & Button1Mask) keyFlags.left = true;
1372 if(event->state & Button2Mask) keyFlags.middle = true;
1373 if(event->state & Button3Mask) keyFlags.right = true;
1374 //*XUnlockDisplay(xGlobalDisplay);
1377 if(event->button == Button4 || event->button == Button5)
1379 window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, (event->button == Button4) ? wheelUp : wheelDown, 0);
1385 if(!window.MouseMessage(buttonDouble, x, y, &keyFlags, false, true))
1387 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1392 window.MouseMessage(button, x, y, &keyFlags, false, /*doubleClick? false : */true);
1394 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1400 Modifiers keyFlags = 0;
1401 XButtonEvent * event = (XButtonEvent *) thisEvent;
1404 int x = event->x_root, y = event->y_root;
1405 if(event->button == Button1)
1407 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
1408 buttonMask = Button1Mask;
1410 else if(event->button == Button3)
1412 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
1413 buttonMask = Button3Mask;
1417 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
1418 buttonMask = Button2Mask;
1420 if(!(event->state & buttonMask)) break;
1421 if(event->state & ShiftMask) keyFlags.shift = true;
1422 if(event->state & ControlMask) keyFlags.ctrl = true;
1423 if(event->state & Mod1Mask) keyFlags.alt = true;
1424 if(event->state & Button1Mask) keyFlags.left = true;
1425 if(event->state & Button2Mask) keyFlags.middle = true;
1426 if(event->state & Button3Mask) keyFlags.right = true;
1427 if(guiApp.windowCaptured && guiApp.windowCaptured != window)
1429 // X hasn't noticed the capture yet, so fix it!
1430 x += window.absPosition.x;
1431 y += window.absPosition.y;
1432 window = guiApp.windowCaptured.rootWindow;
1433 x -= window.absPosition.x;
1434 y -= window.absPosition.y;
1436 //*XUnlockDisplay(xGlobalDisplay);
1438 window.MouseMessage(button, x, y, &keyFlags, false, false);
1440 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1445 static uint lastTime = 0;
1446 XMotionEvent * event = (XMotionEvent *) thisEvent;
1447 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, EventChecker, (void *)MotionNotify));
1448 // if(event->time - lastTime > 15)
1450 Modifiers keyFlags = 0;
1451 int x = event->x_root, y = event->y_root;
1453 if(event->state & ShiftMask) keyFlags.shift = true;
1454 if(event->state & ControlMask) keyFlags.ctrl = true;
1455 if(event->state & Mod1Mask) keyFlags.alt = true;
1456 if(event->state & Button1Mask) keyFlags.left = true;
1457 if(event->state & Button2Mask) keyFlags.middle = true;
1458 if(event->state & Button3Mask) keyFlags.right = true;
1459 //*XUnlockDisplay(xGlobalDisplay);
1461 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove,
1462 event->x_root, event->y_root, &keyFlags, false, false);
1464 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1465 lastTime = event->time;
1471 XExposeEvent * event = (XExposeEvent *) thisEvent;
1473 box.left = event->x - window.clientStart.x;
1474 box.top = event->y - window.clientStart.y;
1475 box.right = box.left + event->width - 1;
1476 box.bottom = box.top + event->height - 1;
1478 box.left += window.clientStart.x;
1479 box.top += window.clientStart.y;
1480 box.right += window.clientStart.x;
1481 box.bottom += window.clientStart.y;
1482 window.UpdateDirty(box);
1485 case SelectionRequest:
1487 XSelectionRequestEvent *req = (XSelectionRequestEvent *) thisEvent;
1489 if(req->target == atoms[targets] && clipBoardData)
1491 Atom * supportedTargets = new Atom[4];
1492 supportedTargets[0] = atoms[targets];
1493 supportedTargets[1] = atoms[multiple];
1494 supportedTargets[2] = XA_STRING;
1495 supportedTargets[3] = atoms[utf8_string];
1496 XChangeProperty(xGlobalDisplay,req->requestor, req->_property,
1497 XA_ATOM,32,PropModeReplace, (byte *) supportedTargets, 4*sizeof(Atom));
1498 respond.xselection._property = req->_property;
1499 delete supportedTargets;
1501 else if((req->target == XA_STRING || req->target == atoms[utf8_string]) && clipBoardData)
1503 Atom _property = (req->_property == None) ? req->target : req->_property;
1504 XChangeProperty(xGlobalDisplay,req->requestor, _property,
1505 req->target/*req->_property*/,8,PropModeReplace, (byte *) clipBoardData, strlen(clipBoardData));
1506 respond.xselection._property = _property;
1509 respond.xselection._property = None;
1511 respond.xselection.type = SelectionNotify;
1512 respond.xselection.display = req->display;
1513 respond.xselection.requestor = req->requestor;
1514 respond.xselection.selection =req->selection;
1515 respond.xselection.target = req->target;
1516 respond.xselection.time = CurrentTime;
1517 XSendEvent(xGlobalDisplay, req->requestor,0,0,&respond);
1520 case SelectionClear:
1522 delete clipBoardData;
1527 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1528 Window modalRoot = window.FindModal();
1529 XWindowData windowData;
1530 activeWindow = (uint)window.windowHandle;
1532 if(window.parent && window == window.parent.activeChild) break;
1534 //if(window.creationActivation == activate)
1537 modalRoot.ExternalActivate(true, true, window, null); // lastActive);
1539 window.ExternalActivate(true, true, window, null); // lastActive);
1541 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1542 if(windowData && windowData.ic)
1544 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
1545 XSetICFocus(windowData.ic);
1547 //delete lastActive;
1548 //lastActive = window;
1549 //incref lastActive;
1556 //printf("Processing a FocusOut Event for %s (%x)\n", window._class.name, window);
1559 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusIn, (XEvent *)thisEvent))
1563 if(thisEvent->window == activeWindow)
1564 activeWindow = (uint)null;
1566 if(XCheckTypedEvent(xGlobalDisplay, FocusIn, (XEvent *)thisEvent))
1568 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusOut, (XEvent *)thisEvent))
1570 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1572 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1575 Window windowCopy = window;
1576 XWindowData windowData;
1577 if(windowCopy == windowCopy.parent.activeChild) break;
1579 windowCopy.ExternalActivate(true, true, windowCopy, lastActive);
1581 windowData = windowCopy.windowData;
1582 if(windowData && windowData.ic)
1583 XSetICFocus(windowData.ic);
1586 lastActive = windowCopy;
1591 // XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1594 if(window != window.parent.activeChild) break;
1596 window.ExternalActivate(false, true, window, lastActive);
1602 XWindowData windowData;
1603 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1607 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1608 Window modalRoot = window.FindModal();
1613 modalRoot.ExternalActivate(true, true, window, lastActive);
1617 window.ExternalActivate(true, true, window, lastActive);
1619 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1620 if(windowData && windowData.ic)
1621 XSetICFocus(windowData.ic);
1624 lastActive = window;
1633 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1634 if(window != window.parent.activeChild && window != guiApp.interimWindow) break;
1638 //printf("Deactivating %s\n", window._class.name);
1641 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
1643 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
1646 //delete lastActive;
1648 lastActive = window;
1655 case ConfigureNotify:
1657 XConfigureEvent * event = (XConfigureEvent *) thisEvent;
1658 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, (void *)ConfigureNotifyChecker, (void *)window.windowHandle));
1659 //if(event->x - desktopX != window.position.x || event->y - desktopY != window.position.y || event->width != window.size.w || event->height != window.size.h)
1661 // TODO: Support _NET_REQUEST_FRAME_EXTENTS message / _NET_FRAME_EXTENTS property for decoration size awareness
1662 if(window.nativeDecorations)
1664 int format, len, fill;
1667 if(XGetWindowProperty(xGlobalDisplay, (uint)window.systemHandle, atoms[_net_wm_state], 0, 32, False,
1668 XA_ATOM, &type, &format, &len, &fill, &data) == Success)
1670 bool maxVert = false, maxHorz = false, isMinimized = false;
1671 Atom * hints = (Atom *)data;
1673 for(c = 0; c < len && hints[c]; c++)
1675 if(hints[c] == atoms[_net_wm_state_maximized_vert])
1677 else if(hints[c] == atoms[_net_wm_state_maximized_horz])
1679 else if(hints[c] == atoms[_net_wm_state_hidden])
1684 if(maxVert && maxHorz)
1686 if(window.state != maximized)
1687 window.state = maximized;
1689 else if(isMinimized)
1691 if(window.state != minimized)
1692 window.state = minimized;
1694 else if(window.state != normal)
1695 window.state = normal;
1699 int x = event->x - desktopX;
1700 int y = event->y - desktopY;
1701 int w = event->width, h = event->height;
1702 if(window.nativeDecorations)
1705 x -= windowData.decor.left;
1706 y -= windowData.decor.top;
1707 w += windowData.decor.left + windowData.decor.right;
1708 h += windowData.decor.top + windowData.decor.bottom;
1710 x -= window.clientStart.x;
1711 y -= window.clientStart.y - (window.hasMenuBar ? skinMenuHeight : 0);
1712 w += window.size.w - window.clientSize.w;
1713 h += window.size.h - window.clientSize.h;
1716 window.ExternalPosition(x, y, w, h);
1722 XClientMessageEvent * event = (XClientMessageEvent *) thisEvent;
1724 if(event->data.l[0] == atoms[wm_delete_window])
1729 if(event->data.l[0] == atoms[wm_take_focus])
1732 XWindowData windowData;
1734 activeWindow = (uint)window.windowHandle;
1736 timeStamp = event->data.l[1];
1738 windowData = window.windowData;
1739 laterFocus = windowData.laterFocus;
1740 windowData.laterFocus = true;
1743 //printf("Processing a ClientMessage WM_TAKE_FOCUS Event for %s (%x)\n", window._class.name, window);
1745 if(guiApp.interimWindow && guiApp.interimWindow.created && window != guiApp.interimWindow) break; //window == window.parent.activeChild) break;
1746 // if(window == window.parent.activeChild) break;
1751 //XFlush(xGlobalDisplay);
1752 while(XCheckTypedEvent(xGlobalDisplay, FocusOut, &checkEvent))
1754 XFocusChangeEvent *event = (XFocusChangeEvent *) &checkEvent;
1756 XFindContext(xGlobalDisplay, event->window, windowContext, (XPointer *) &window);
1757 if(window != window.parent.activeChild) break;
1761 //printf("Found a FocusOut ahead, deactivating %s (%d)\n", window._class.name, window);
1764 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
1766 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
1773 modalRoot = window.FindModal();
1774 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1777 if(laterFocus || (modalRoot ? modalRoot : window).creationActivation == activate)
1781 XRaiseWindow(xGlobalDisplay, (int)modalRoot.windowHandle);
1782 WaitForViewableWindow(modalRoot);
1783 if(atoms[_net_active_window])
1785 XClientMessageEvent event = { 0 };
1786 event.type = ClientMessage;
1787 event.message_type = atoms[_net_active_window];
1788 event.display = xGlobalDisplay;
1790 event.window = (uint)modalRoot.windowHandle;
1791 event.send_event = 1;
1793 event.data.l[0] = 0;
1795 event.data.l[0] = 1;
1796 event.data.l[1] = atoms[_net_wm_user_time];
1797 event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
1800 //printf("(ClientMessage - %s) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, modalRoot._class.name, modalRoot);
1803 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
1804 XSetInputFocus(xGlobalDisplay, (int)modalRoot.windowHandle, RevertToPointerRoot, (uint)timeStamp);
1806 //XFlush(xGlobalDisplay);
1807 //printf("Done.\n");
1812 XSetInputFocus(xGlobalDisplay, (int)window.windowHandle, RevertToPointerRoot, (uint)timeStamp);
1813 window.ExternalActivate(true, true, window, null); // lastActive);
1814 if(windowData && windowData.ic)
1816 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
1817 //XSetICFocus(windowData.ic);
1821 windowData.laterFocus = true;
1823 lastActive = window;
1830 case PropertyNotify:
1832 XPropertyEvent * event = (XPropertyEvent *) thisEvent;
1833 if(event->atom == atoms[_net_frame_extents] &&
1834 event->state == PropertyNewValue && window.windowData)
1836 int format, len, fill;
1840 if(XGetWindowProperty(xGlobalDisplay, (uint)window.windowHandle,
1841 atoms[_net_frame_extents], 0, 4,
1842 False, XA_CARDINAL, &type, &format, &len,
1843 &fill, &data) == Success && data)
1845 int *extents = (int *)data;
1846 XWindowData windowData = window.windowData;
1847 bool hadFrameExtents = windowData.gotFrameExtents;
1850 left = extents[0], right = extents[1],
1851 top = extents[2], bottom = extents[3]
1853 windowData.gotFrameExtents = true;
1856 window.ComputeAnchors(
1857 window.normalAnchor,
1858 window.normalSizeAnchor,
1860 if(!hadFrameExtents) // || window.state == normal)
1862 bool isMaximized = window.state == maximized;
1863 window.Position(x, y, w, h, true, true, true, true, false, true);
1864 UpdateRootWindow(window);
1874 if(!processAll) break;
1877 //*if(xGlobalDisplay) XUnlockDisplay(xGlobalDisplay);
1886 gotAnXEvent = false;
1888 xSemaphore.Release();
1893 void Lock(Window window)
1895 //*XLockDisplay(xGlobalDisplay);
1898 void Unlock(Window window)
1900 //*XUnlockDisplay(xGlobalDisplay);
1903 char ** GraphicsDrivers(int * numDrivers)
1905 static char *graphicsDrivers[] = { "X", "OpenGL" };
1906 *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
1907 return (char **)graphicsDrivers;
1910 void GetCurrentMode(bool * fullScreen, int * resolution, int * colorDepth, int * refreshRate)
1912 *fullScreen = fullScreenMode;
1915 void EnsureFullScreen(bool *fullScreen)
1920 bool ScreenMode(bool fullScreen, int resolution, int colorDepth, int refreshRate, bool * textMode)
1924 fullScreenMode = fullScreen;
1932 static bool firstTime = true;
1934 desktopX = desktopY = desktopW = desktopH = 0;
1936 RepositionDesktop(false);
1941 // --- Window Creation ---
1942 void * CreateRootWindow(Window window)
1944 X11Window windowHandle;
1945 XSetWindowAttributes attributes = { 0 };
1946 XVisualInfo * visualInfo = null;
1950 unsigned int mask = EVENT_MASK;
1952 attributes.override_redirect = window.interim ? True : False;
1953 attributes.event_mask = EVENT_MASK;
1954 //printf("%s\n", guiApp.defaultDisplayDriver);
1955 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
1956 if(window.dispDriver == class(OpenGLDisplayDriver) || !strcmp(guiApp.defaultDisplayDriver, "OpenGL"))
1960 for(samples = 4;; samples /= 2)
1965 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1966 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
1967 GLX_DOUBLEBUFFER, True,
1973 int numAttribs = 14;
1975 GLXFBConfig *fbconfigs = null, fbconfig;
1978 //printf("Samples = %d, alpha = %d\n", samples, alpha);
1981 attrib[numAttribs++] = GLX_ALPHA_SIZE;
1982 attrib[numAttribs++] = 1;
1986 attrib[numAttribs++] = GLX_SAMPLE_BUFFERS_ARB;
1987 attrib[numAttribs++] = GL_TRUE;
1988 attrib[numAttribs++] = GLX_SAMPLES_ARB;
1989 attrib[numAttribs++] = samples;
1991 attrib[numAttribs] = None;
1993 // visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib);
1995 //printf("Trying %d samples...\n", samples);
1996 fbconfigs = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &numfbconfigs);
1999 for (i = 0; i < numfbconfigs; i++)
2001 XRenderPictFormat * format;
2002 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfigs[i]);
2003 if (!visualInfo) continue;
2004 if(window.alphaBlend)
2006 format = XRenderFindVisualFormat(xGlobalDisplay, visualInfo->visual);
2007 if (!format) { XFree(visualInfo); continue; }
2008 if(format->direct.alphaMask > 0)
2010 //printf("Found what we're looking for (alphaBlend)\n");
2011 fbconfig = fbconfigs[i];
2018 //printf("Found what we're looking for\n");
2023 if (i == numfbconfigs)
2025 fbconfig = fbconfigs[0];
2026 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfig);
2031 if(found || (!samples && !alpha))
2033 //printf("Stopping now\n");
2038 if(samples == 1) samples = 0;
2039 else if(!samples) alpha = false;
2043 depth = visualInfo ? visualInfo->depth : (window.alphaBlend ? 32 : xSystemDepth);
2044 visual = visualInfo ? visualInfo->visual : (window.alphaBlend ? FindFullColorVisual (xGlobalDisplay, &depth) : xSystemVisual);
2045 // printf("visual: %d, depth: %d\n", visual, depth);
2049 attributes.colormap = XCreateColormap(xGlobalDisplay, XRootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), visual, AllocNone);
2050 attributes.border_pixel = 0;
2057 windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
2058 0,0,guiApp.desktop.size.w,guiApp.desktop.size.h,0, depth, InputOutput, visual ? visual : CopyFromParent,
2059 CWEventMask | (visual ? (CWColormap | CWBorderPixel) : 0)/*| CWOverrideRedirect*/, &attributes);
2062 Unsupported for now...
2063 else if(window.systemParent)
2070 if(window.windowHandle)
2071 windowHandle = (int)window.windowHandle;
2074 X11Window parentWindow = (uint)null;
2076 if(window.master.rootWindow && window.master.rootWindow != guiApp.desktop && (window._isModal || window.style.interim))
2078 Window master = window.master;
2079 Window rootWindow = master.rootWindow;
2081 parentWindow = rootWindow.is3D ? (uint)rootWindow.parent.windowHandle : (uint)rootWindow.windowHandle;
2083 // parentWindow = window.master.rootWindow.is3D ? window.master.rootWindow.parent.windowHandle : window.master.rootWindow.windowHandle;
2085 if(window.style.showInTaskBar)
2086 parentWindow = (uint)null;
2088 windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
2089 0,0,1,1,0, depth, InputOutput, visual ? visual : CopyFromParent,
2090 CWEventMask | CWOverrideRedirect | (visual ? (CWColormap | CWBorderPixel) : 0), &attributes);
2092 if(parentWindow && window.interim)
2094 //printf("Setting WM_TRANSIENT_FOR of %s to %s\n", window._class.name, window.master.rootWindow._class.name);
2095 XSetTransientForHint(xGlobalDisplay, windowHandle, parentWindow);
2096 //XFlush(xGlobalDisplay);
2097 //printf("Done.\n");
2098 //XChangeProperty(xGlobalDisplay, windowHandle, atoms[wm_transient_for], XA_WINDOW, 32, PropModeReplace, (unsigned char*)&parentWindow, 1);
2102 #if defined(__APPLE__)
2105 parentWindow ? atoms[_net_wm_window_type_popup_menu] : atoms[_net_wm_window_type_normal]
2108 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
2109 PropModeReplace, (unsigned char*)&hints, 1);
2113 parentWindow ? atoms[_net_wm_window_type_menu] : atoms[_net_wm_window_type_normal],
2114 parentWindow ? atoms[_net_wm_window_type_popup_menu] : 0
2116 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
2117 PropModeReplace, (unsigned char*)&hints, parentWindow ? 2 : 1);
2121 xwmHints.flags = InputHint;
2123 XSetWMHints(xGlobalDisplay, windowHandle, &xwmHints);
2126 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
2127 XSetWMProtocols(xGlobalDisplay, windowHandle, protocols, 2);
2135 char fontString[1024] = "--helvetica-*-r-*-*-*-120-*-*-*-*-*-*,-misc-fixed-*-r-*-*-*-130-*-*-*-*-*-*";
2136 XPoint cursor_location = { 0, 0 };
2137 char **missing_charsets;
2138 int num_missing_charsets = 0;
2139 char *default_string;
2141 XRectangle area = { 0, 0, 400, 400 };
2142 XVaNestedList argList;
2144 // sprintf(fontString, "-*-%s-*-r-*-*-*-%d-*-*-*-*-*-*", "Helvetica" /*window.font.faceName*/, (int)(window.font.size * 20));
2145 fontset = XCreateFontSet(xGlobalDisplay, fontString, &missing_charsets, &num_missing_charsets, &default_string);
2146 argList = XVaCreateNestedList(0,
2147 XNSpotLocation, &cursor_location,
2149 XNFontSet, fontset,/*
2151 WhitePixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),
2153 BlackPixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),*/
2155 ic = XCreateIC(im, XNInputStyle,
2156 XIMStatusNothing | XIMPreeditPosition,
2157 XNPreeditAttributes, argList,
2158 XNClientWindow, windowHandle, XNFocusWindow, windowHandle, 0);
2160 // Should we free the fontset or not?
2161 // XFreeFontSet(xGlobalDisplay, fontset);
2163 setICPosition = true;
2165 ic = XCreateIC(im, XNInputStyle, XIMStatusNothing | XIMPreeditNothing, XNClientWindow, windowHandle, XNFocusWindow, windowHandle, 0);
2169 XGetICValues(ic, XNFilterEvents, &mask, NULL);
2173 XSelectInput(xGlobalDisplay, windowHandle, mask);
2175 if(capturedWindow == None && !restrictedWindow)
2177 XGrabPointer(xGlobalDisplay, (int)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2178 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2179 XUngrabPointer(xGlobalDisplay, CurrentTime);
2184 if ( atoms[_motif_wm_hints] != None )
2188 (window.nativeDecorations ? 0 : MWM_HINTS_DECORATIONS)|MWM_HINTS_FUNCTIONS,
2189 (window.hasClose ? MWM_FUNC_CLOSE : 0) |
2190 (window.hasMaximize ? MWM_FUNC_MAXIMIZE : 0) |
2191 (window.hasMinimize ? MWM_FUNC_MINIMIZE : 0) |
2192 ((window.moveable || ((BorderBits)window.borderStyle).fixed) ? MWM_FUNC_MOVE : 0) |
2193 (((BorderBits)window.borderStyle).sizable ? MWM_FUNC_RESIZE : 0),
2196 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
2197 PropModeReplace, (unsigned char*)&hints, sizeof(hints)/4);
2199 if(atoms[_net_wm_pid] != None)
2202 // printf("Setting _NET_WM_PID to %d\n", pid);
2203 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_pid], XA_CARDINAL, 32,
2204 PropModeReplace, (unsigned char*)&pid, 1);
2210 Atom protocolsAtom = XInternAtom(xGlobalDisplay, "WM_PROTOCOLS", False);
2211 if ( protocolsAtom != None )
2213 MWM_Hints hints = { MWM_HINTS_DECORATIONS|MWM_HINTS_FUNCTIONS, 0, 0, 0, 0 };
2214 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
2215 PropModeReplace, (unsigned char*)&hints, sizeof(hints)/4);
2218 // XFlush(xGlobalDisplay);
2219 window.windowData = XWindowData { visualInfo, ic };
2221 XSaveContext(xGlobalDisplay, windowHandle, windowContext, (XPointer)window);
2223 XSelectInput(xGlobalDisplay, windowHandle, mask);
2225 if(capturedWindow == None && !restrictedWindow)
2227 XGrabPointer(xGlobalDisplay, (int)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2228 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2229 XUngrabPointer(xGlobalDisplay, CurrentTime);
2232 if(window.nativeDecorations)
2234 // Maximize / Restore the window
2235 XClientMessageEvent event = { 0 };
2236 event.type = ClientMessage;
2237 event.message_type = atoms[_net_request_frame_extents];
2238 event.display = xGlobalDisplay;
2240 event.window = (uint)windowHandle;
2241 event.send_event = 1;
2242 window.windowHandle = (void *)windowHandle;
2244 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false,
2245 SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2247 return (void *)windowHandle;
2250 void DestroyRootWindow(Window window)
2254 XDeleteContext(xGlobalDisplay, (XID)window, windowContext);
2255 XSaveContext(xGlobalDisplay, (int)window.windowHandle, windowContext, null);
2256 XDestroyWindow(xGlobalDisplay, (int)window.windowHandle);
2257 XSync(xGlobalDisplay, 0);
2258 while(XCheckWindowEvent(xGlobalDisplay, (int)window.windowHandle, 0xFFFFFFFF, &event));
2259 window.windowHandle = null;
2260 if(window.windowData)
2262 XWindowData windowData = window.windowData;
2263 XFree(windowData.visual);
2264 if(windowData && windowData.ic)
2265 XDestroyIC(windowData.ic);
2267 // printf("Setting windowData for %s to null\n", window._class.name);
2268 window.windowData = null;
2270 if(lastActive == window)
2274 // -- Window manipulation ---
2276 void SetRootWindowCaption(Window window, char * name)
2278 if(window.windowHandle)
2280 XChangeProperty(xGlobalDisplay, (int)window.windowHandle, atoms[_net_wm_name],
2281 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2282 XChangeProperty(xGlobalDisplay, (int)window.windowHandle, atoms[wm_name],
2283 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2287 void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
2289 //Logf("Position root window %s\n", window.name);
2290 if(window.windowHandle && (!window.parent || !window.parent.display))
2292 #if defined(__APPLE__)
2293 bool visible = window.visible;
2296 XMapWindow(xGlobalDisplay, (int)window.windowHandle);
2297 WaitForViewableWindow(window);
2300 if(window.state == minimized) return;
2302 if(window.nativeDecorations)
2304 XWindowData windowData = window.windowData;
2305 if(!windowData.gotFrameExtents || window.state == maximized) return;
2306 w -= window.size.w - window.clientSize.w;
2307 h -= window.size.h - window.clientSize.h;
2310 XMoveResizeWindow(xGlobalDisplay, (int)window.windowHandle, x + desktopX, y + desktopY, w, h);
2312 XMoveWindow(xGlobalDisplay, (int)window.windowHandle, x + desktopX, y + desktopY);
2314 XResizeWindow(xGlobalDisplay, (int)window.windowHandle, w, h);
2315 #if defined(__APPLE__)
2316 // if(window.created && !visible)
2317 // XUnmapWindow(xGlobalDisplay, (int)window.windowHandle);
2322 void OrderRootWindow(Window window, bool topMost)
2327 void SetRootWindowColor(Window window)
2332 void OffsetWindow(Window window, int * x, int * y)
2337 void UpdateRootWindow(Window window)
2339 if(!window.parent || !window.parent.display)
2343 Box box = window.box;
2344 box.left -= window.clientStart.x;
2345 box.top -= window.clientStart.y;
2346 box.right -= window.clientStart.x;
2347 box.bottom -= window.clientStart.y;
2348 // Logf("Update root window %s\n", window.name);
2349 window.Update(null);
2350 box.left += window.clientStart.x;
2351 box.top += window.clientStart.y;
2352 box.right += window.clientStart.x;
2353 box.bottom += window.clientStart.y;
2354 window.UpdateDirty(box);
2360 void SetRootWindowState(Window window, WindowState state, bool visible)
2362 if(!window.parent || !window.parent.display)
2364 //Logf("Set root window state %d %s\n", state, window.name);
2367 XMapWindow(xGlobalDisplay, (int)window.windowHandle);
2368 #if defined(__APPLE__)
2369 WaitForViewableWindow(window);
2371 if(window.creationActivation == activate && state != minimized)
2372 ActivateRootWindow(window);
2374 if(state == minimized)
2376 uint iconic = IconicState;
2378 XChangeProperty(xGlobalDisplay, window.windowHandle, atoms[_net_wm_state], XA_ATOM, 32,
2379 PropModeReplace, (unsigned char*)&atoms[_net_wm_state_hidden], 1);
2382 XChangeProperty(xGlobalDisplay, window.windowHandle, atoms[wm_state], XA_CARDINAL, 32,
2383 PropModeReplace, &iconic, 1);
2387 XClientMessageEvent event = { 0 };
2388 event.type = ClientMessage;
2389 event.message_type = atoms[_net_wm_state];
2390 event.display = xGlobalDisplay;
2392 event.window = window.windowHandle;
2393 event.send_event = 1;
2395 event.data.l[0] = 2; // 1;
2396 event.data.l[1] = atoms[_net_wm_state_hidden];
2397 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, &event);
2400 XClientMessageEvent event = { 0 };
2401 event.type = ClientMessage;
2402 event.message_type = atoms[wm_state];
2403 event.display = xGlobalDisplay;
2404 event.window = window.windowHandle;
2405 event.send_event = 1;
2407 event.data.l[0] = IconicState;
2408 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, &event);
2411 // printf("Attempting to minimize %s\n", window._class.name);
2412 XIconifyWindow(xGlobalDisplay, (uint)window.windowHandle, DefaultScreen(xGlobalDisplay));
2416 if(!window.nativeDecorations || (!((XWindowData)window.windowData).gotFrameExtents && window.state == maximized)) //((XWindowData)window.windowData).gotFrameExtents && (!window.nativeDecorations || window.state == state))
2418 // With native decorations, we do it the first time
2419 // or the WM (Gnome) is sticking it to the top/right!
2420 XMoveResizeWindow(xGlobalDisplay,
2421 (int)window.windowHandle,
2422 window.position.x + desktopX,
2423 window.position.y + desktopY,
2424 window.nativeDecorations ? window.clientSize.w : window.size.w,
2425 window.nativeDecorations ? window.clientSize.h : window.size.h);
2426 UpdateRootWindow(window);
2428 if(window.nativeDecorations)
2430 // Maximize / Restore the window
2431 XClientMessageEvent event = { 0 };
2432 event.type = ClientMessage;
2433 event.message_type = atoms[_net_wm_state];
2434 event.display = xGlobalDisplay;
2436 event.window = (uint)window.windowHandle;
2437 event.send_event = 1;
2439 event.data.l[0] = (state == maximized) ? 1 : 0;
2440 event.data.l[1] = atoms[_net_wm_state_maximized_vert];
2441 event.data.l[2] = atoms[_net_wm_state_maximized_horz];
2442 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false,
2443 SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2448 XUnmapWindow(xGlobalDisplay, (int)window.windowHandle);
2449 //XFlush(xGlobalDisplay);
2453 void FlashRootWindow(Window window)
2455 XClientMessageEvent event = { 0 };
2456 // printf("Attempting to flash root window\n");
2457 event.type = ClientMessage;
2458 event.message_type = atoms[_net_wm_state];
2459 event.display = xGlobalDisplay;
2461 event.window = (uint)window.windowHandle;
2462 event.send_event = 1;
2464 event.data.l[0] = 1;
2465 event.data.l[1] = atoms[_net_wm_state_demands_attention];
2466 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2469 void ActivateRootWindow(Window window)
2471 if(!window.parent || !window.parent.display)
2473 if(!window.style.hidden && window.created)
2475 //printf("Activate root window %s\n", window._class.name);
2476 XRaiseWindow(xGlobalDisplay, (int)window.windowHandle);
2477 XMapWindow(xGlobalDisplay, (int)window.windowHandle);
2478 WaitForViewableWindow(window);
2479 if(atoms[_net_active_window])
2481 XClientMessageEvent event = { 0 };
2482 event.type = ClientMessage;
2483 event.message_type = atoms[_net_active_window];
2484 event.display = xGlobalDisplay;
2486 event.window = (uint)window.windowHandle;
2487 event.send_event = 1;
2489 event.data.l[0] = 0;
2491 //event.data.l[0] = 2;
2492 //event.data.l[1] = timeStamp;
2495 //event.data.l[1] = atoms[_net_wm_user_time];
2496 //event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
2499 //printf("(ActivateRootWindow) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, window);
2502 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2503 //#if defined(__APPLE__)
2504 XSetInputFocus(xGlobalDisplay, (int)window.windowHandle, RevertToPointerRoot, CurrentTime);
2508 XSetInputFocus(xGlobalDisplay, (int)window.windowHandle, RevertToPointerRoot, CurrentTime);
2513 // --- Mouse-based window movement ---
2515 void StartMoving(Window window, int x, int y, bool fromKeyBoard)
2520 void StopMoving(Window window)
2525 // -- Mouse manipulation ---
2527 void GetMousePosition(int *x, int *y)
2529 int rootWindow, childWindow;
2532 ((GuiApplication)__thisModule.application).Lock();
2533 //XLockDisplay(xGlobalDisplay);
2534 XQueryPointer(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &childWindow,
2535 &rootWindow, x, y, &mx, &my, &state);
2536 //XUnlockDisplay(xGlobalDisplay);
2537 ((GuiApplication)__thisModule.application).Unlock();
2540 void SetMousePosition(int x, int y)
2545 void SetMouseRange(Window window, Box box)
2547 ((GuiApplication)__thisModule.application).Lock();
2548 //XLockDisplay(xGlobalDisplay);
2549 if(box && box.left > 0 && box.top > 0 &&
2550 box.right < guiApp.desktop.clientSize.w - 1 && box.bottom < guiApp.desktop.clientSize.h - 1)
2552 if(!window.parent || !window.parent.display)
2554 XMoveResizeWindow(xGlobalDisplay, confineWindow, box.left + desktopX, box.top + desktopY,
2555 box.right - box.left + 1, box.bottom - box.top + 1);
2557 if(!restrictedWindow)
2558 XMapWindow(xGlobalDisplay, confineWindow);
2560 XGrabPointer(xGlobalDisplay, (int) window.rootWindow.windowHandle, False,
2561 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2562 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
2564 restrictedWindow = window;
2567 else if(restrictedWindow)
2569 if(capturedWindow != None)
2571 XGrabPointer(xGlobalDisplay, (int)capturedWindow,
2572 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2573 GrabModeAsync, None, fullScreenMode ? nullCursor : None, CurrentTime);
2576 XUngrabPointer(xGlobalDisplay, CurrentTime);
2578 if(restrictedWindow)
2579 XUnmapWindow(xGlobalDisplay, confineWindow);
2581 restrictedWindow = null;
2583 //XUnlockDisplay(xGlobalDisplay);
2584 ((GuiApplication)__thisModule.application).Unlock();
2587 void SetMouseCapture(Window window)
2589 //*XLockDisplay(xGlobalDisplay);
2592 if(!window.parent || !window.parent.display)
2594 XGrabPointer(xGlobalDisplay, (int)window.windowHandle,
2595 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2596 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2598 capturedWindow = (X11Window) window.windowHandle;
2601 else if(capturedWindow != None)
2603 if(restrictedWindow)
2604 XGrabPointer(xGlobalDisplay, (int) restrictedWindow.rootWindow.windowHandle, False,
2605 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2606 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
2608 XUngrabPointer(xGlobalDisplay, CurrentTime);
2609 capturedWindow = None;
2611 //*XUnlockDisplay(xGlobalDisplay);
2614 // -- Mouse cursor ---
2616 void SetMouseCursor(int cursor)
2618 //*XLockDisplay(xGlobalDisplay);
2621 XDefineCursor(xGlobalDisplay, (int) guiApp.desktop.windowHandle, nullCursor);
2623 //*XUnlockDisplay(xGlobalDisplay);
2628 void SetCaret(int x, int y, int size)
2630 Window caretOwner = guiApp.caretOwner;
2631 Window window = caretOwner ? caretOwner.rootWindow : null;
2632 if(window && window.windowData && setICPosition)
2634 XWindowData windowData = window.windowData;
2635 if(windowData && windowData.ic)
2637 XPoint cursor_location =
2639 (short)(caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x),
2640 (short)(caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y)
2642 XVaNestedList argList = XVaCreateNestedList(0, XNSpotLocation, &cursor_location, NULL);
2643 XSetICValues(windowData.ic, XNPreeditAttributes, argList, 0);
2648 void ClearClipboard()
2650 //*XLockDisplay(xGlobalDisplay);
2653 delete clipBoardData;
2654 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard], None, CurrentTime);
2656 //*XUnlockDisplay(xGlobalDisplay);
2659 bool AllocateClipboard(ClipBoard clipBoard, uint size)
2661 bool result = false;
2662 if((clipBoard.text = new0 byte[size]))
2667 bool SaveClipboard(ClipBoard clipBoard)
2669 bool result = false;
2670 //*XLockDisplay(xGlobalDisplay);
2673 Window rootWindow = guiApp.desktop;
2676 for(rootWindow = rootWindow.children.first;
2677 rootWindow && !rootWindow.windowHandle;
2678 rootWindow = rootWindow.next);
2681 delete clipBoardData;
2683 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard],
2684 (int) rootWindow.windowHandle, CurrentTime);
2685 clipBoardData = clipBoard.text;
2686 clipBoard.text = null;
2689 //*XUnlockDisplay(xGlobalDisplay);
2693 bool LoadClipboard(ClipBoard clipBoard)
2695 bool result = false;
2697 //*XLockDisplay(xGlobalDisplay);
2698 // The data is inside this client...
2701 clipBoard.text = new char[strlen(clipBoardData)+1];
2702 strcpy(clipBoard.text, clipBoardData);
2705 // The data is with another client...
2708 Window rootWindow = guiApp.desktop;
2711 for(rootWindow = rootWindow.children.first;
2712 rootWindow && !rootWindow.windowHandle;
2713 rootWindow = rootWindow.next);
2717 int selAtom = atoms[clipboard];
2718 int owner = XGetSelectionOwner(xGlobalDisplay, selAtom);
2722 for(atom = atoms[utf8_string]; atom; atom = ((atom == atoms[utf8_string]) ? XA_STRING : 0))
2725 XConvertSelection(xGlobalDisplay, selAtom, atom, atoms[app_selection] /*None*/, (int) rootWindow.windowHandle, CurrentTime);
2726 XIfEvent(xGlobalDisplay, (XEvent *) &e, EventChecker, (void *)SelectionNotify);
2727 if(e.type == SelectionNotify)
2729 XSelectionEvent * selection = (XSelectionEvent *) &e;
2730 //printf("Got a SelectionNotify with %d (%s)\n", selection->_property, XGetAtomName(xGlobalDisplay, selection->_property));
2732 uint len, size = 0, dummy;
2735 XGetWindowProperty(xGlobalDisplay, (int) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, 0, 0, AnyPropertyType, &type, &format, &len, &size, &data);
2738 if(XGetWindowProperty(xGlobalDisplay, (int) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0,size,0,
2739 AnyPropertyType, &type,&format,&len, &dummy, &data) == Success)
2741 clipBoard.text = new char[size+1];
2742 strncpy(clipBoard.text, data, size);
2743 clipBoard.text[size] = '\0';
2754 //*XUnlockDisplay(xGlobalDisplay);
2758 void UnloadClipboard(ClipBoard clipBoard)
2760 delete clipBoard.text;
2763 // --- State based input ---
2765 bool AcquireInput(Window window, bool state)
2770 bool GetMouseState(MouseButtons * buttons, int * x, int * y)
2772 bool result = false;
2778 bool GetJoystickState(int device, Joystick joystick)
2780 bool result = false;
2781 #if defined(__linux__)
2782 if(joystick && device < 4)
2784 struct JS_DATA_TYPE js = { 0 };
2785 memset(joystick, 0, sizeof(Joystick));
2786 if(joystickFD[device] && read(joystickFD[device], &js, JS_RETURN) == JS_RETURN)
2788 joystick.buttons = js.buttons;
2789 joystick.x = js.x - 128;
2790 joystick.y = js.y - 128;
2798 bool GetKeyState(Key key)
2804 void SetTimerResolution(uint hertz)
2806 timerDelay = hertz ? (1000000 / hertz) : MAXINT;
2811 hiResTimer.delay = 1000000 / hertz;
2812 hiResTimer.Create();
2817 bool SetIcon(Window window, BitmapResource resource)
2822 if(bitmap.Load(resource.fileName, null, null))
2824 uint * icon = new uint[2 + bitmap.width * bitmap.height];
2825 bitmap.Convert(null, pixelFormat888, null);
2826 icon[0] = bitmap.width;
2827 icon[1] = bitmap.height;
2828 memcpy(icon + 2, bitmap.picture, bitmap.width * bitmap.height * 4);
2829 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_icon],
2830 XA_CARDINAL,32,PropModeReplace, (byte *)icon, 2+bitmap.width*bitmap.height);
2839 default dllexport void * __attribute__((stdcall)) IS_XGetDisplay()
2841 return xGlobalDisplay;
2844 default dllexport void * __attribute__((stdcall)) IS_XGetWindow(Window window)
2846 return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;