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
38 #define _XTYPEDEF_BOOL
41 #include <X11/Xatom.h>
43 #include <X11/Xresource.h>
44 #include <X11/Xutil.h>
45 #include <X11/XKBlib.h>
46 #include <X11/keysym.h>
47 #include <X11/cursorfont.h>
49 #if !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
52 #include <X11/extensions/Xrender.h>
53 #include <X11/extensions/XShm.h>
71 import "XDisplayDriver"
73 // These are the events we care about
74 #define EVENT_MASK StructureNotifyMask | \
75 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | \
76 KeyPressMask | KeyReleaseMask | ExposureMask | FocusChangeMask | \
79 void * xGlobalDisplay;
82 static XContext windowContext;
83 static Window lastActive;
85 static char * clipBoardData;
87 static Thread /* xThread, */ timerThread;
88 static bool xTerminate;
89 static Semaphore xSemaphore { };
90 static Mutex xMutex { };
91 static bool fullScreenMode;
92 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
93 X11Window confineWindow;
94 static X11Cursor nullCursor;
95 static X11Window capturedWindow = None;
96 static Window restrictedWindow = null;
97 static bool gotAnXEvent = false;
99 static int joystickFD[4];
100 static X11Window activeWindow;
101 static X11Cursor systemCursors[SystemCursor];
103 static Window acquiredInputWindow;
104 static Point acquireStart;
105 static Point lastMouse;
106 static MouseButtons buttonsState;
108 static bool keyStates[KeyCode];
110 static enum NETWMStateAction { remove = 0, add = 1, toggle = 2 };
112 static enum AtomIdents
114 clipboard, multiple, targets, utf8_string, wm_delete_window, wm_hints, wm_name, wm_protocols, wm_state, wm_take_focus, wm_transient_for,
116 _net_active_window, _net_current_desktop, _net_number_of_desktops, _net_wm_icon, _net_wm_name, _net_wm_pid,
117 _net_wm_state, _net_wm_state_demands_attention, _net_wm_state_hidden, _net_wm_user_time, _net_wm_window_type,
118 _net_wm_window_type_desktop, _net_wm_window_type_dialog, _net_wm_window_type_dock, _net_wm_window_type_dropdown_menu,
119 _net_wm_window_type_menu, _net_wm_window_type_normal, _net_wm_window_type_popup_menu, _net_wm_window_type_splash,
120 _net_wm_window_type_toolbar, _net_wm_window_type_utility, _net_workarea, _net_frame_extents, _net_request_frame_extents,
121 _net_wm_state_maximized_vert, _net_wm_state_maximized_horz, _net_wm_state_modal, app_selection, _net_supported,
122 _net_wm_state_skip_taskbar, _net_wm_state_fullscreen, _net_wm_state_above, capsLock, numLock, scrollLock
125 static Atom atoms[AtomIdents];
126 static bool atomsSupported[AtomIdents];
128 static const char *atomNames[AtomIdents] = {
129 "CLIPBOARD", //clipboard
130 "MULTIPLE", //multiple
132 "UTF8_STRING", //utf8_string
133 "WM_DELETE_WINDOW", //wm_delete_window
134 "WM_HINTS", //wm_hints
136 "WM_PROTOCOLS", //wm_protocols
137 "WM_STATE", //wm_state
138 "WM_TAKE_FOCUS", //wm_take_focus
139 "WM_TRANSIENT_FOR", //wm_transient_for
140 "_MOTIF_WM_HINTS", //_motif_wm_hints
141 "_NET_ACTIVE_WINDOW", //_net_active_window
142 "_NET_CURRENT_DESKTOP", //_net_current_desktop
143 "_NET_NUMBER_OF_DESKTOPS", //_net_number_of_desktops
144 "_NET_WM_ICON", //_net_wm_icon
145 "_NET_WM_NAME", //_net_wm_name
146 "_NET_WM_PID", //_net_wm_pid
147 "_NET_WM_STATE", //_net_wm_state
148 "_NET_WM_STATE_DEMANDS_ATTENTION", //_net_wm_state_demands_attention
149 "_NET_WM_STATE_HIDDEN", //_net_wm_state_hidden
150 "_NET_WM_USER_TIME", //_net_wm_user_time
151 "_NET_WM_WINDOW_TYPE", //_net_wm_window_type
152 "_NET_WM_WINDOW_TYPE_DESKTOP", //_net_wm_window_type_desktop
153 "_NET_WM_WINDOW_TYPE_DIALOG", //_net_wm_window_type_dialog
154 "_NET_WM_WINDOW_TYPE_DOCK", //_net_wm_window_type_dock
155 "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", //_net_wm_window_type_dropdown_menu
156 "_NET_WM_WINDOW_TYPE_MENU", //_net_wm_window_type_menu
157 "_NET_WM_WINDOW_TYPE_NORMAL", //_net_wm_window_type_normal
158 "_NET_WM_WINDOW_TYPE_POPUP_MENU", //_net_wm_window_type_popup_menu
159 "_NET_WM_WINDOW_TYPE_SPLASH", //_net_wm_window_type_splash
160 "_NET_WM_WINDOW_TYPE_TOOLBAR", //_net_wm_window_type_toolbar
161 "_NET_WM_WINDOW_TYPE_UTILITY", //_net_wm_window_type_utility
162 "_NET_WORKAREA", //_net_workarea
163 "_NET_FRAME_EXTENTS", // _net_frame_extents
164 "_NET_REQUEST_FRAME_EXTENTS", // _net_request_frame_extents
165 "_NET_WM_STATE_MAXIMIZED_VERT", // _net_wm_state_maximized_vert
166 "_NET_WM_STATE_MAXIMIZED_HORZ", // _net_wm_state_maximized_horz
167 "_NET_WM_STATE_MODAL", // _net_wm_state_modal
170 "_NET_WM_STATE_SKIP_TASKBAR",
171 "_NET_WM_STATE_FULLSCREEN",
172 "_NET_WM_STATE_ABOVE",
178 _NET_WM_STATE_STICKY, ATOM
179 _NET_WM_STATE_SHADED, ATOM
180 _NET_WM_STATE_SKIP_PAGER, ATOM
181 _NET_WM_STATE_BELOW, ATOM
184 static bool autoRepeatDetectable;
185 static bool setICPosition;
187 PixelFormat xSystemPixelFormat;
188 Visual * xSystemVisual;
191 static void SetNETWMState(X11Window windowHandle, bool throughRoot, NETWMStateAction action, Atom atom1, Atom atom2)
193 if(atomsSupported[_net_wm_state])
196 unsigned long count, fill;
199 uint state = WithdrawnState;
201 /*if(XGetWindowProperty(xGlobalDisplay, windowHandle, atoms[wm_state], 0, 3, False,
202 atoms[wm_state], &type, &format, &count, &fill, &data) == Success && count)
204 state = *(uint *)data;
207 if(!throughRoot) //state == WithdrawnState)
209 // We need to handle modifying these ourselves for withdrawn windows...
212 Atom values[2] = { atom1, atom2 };
213 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_state], XA_ATOM,
214 32, PropModeAppend, (byte *)values, atom2 ? 2 : 1);
216 else if(XGetWindowProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_state], 0, 32, False,
217 XA_ATOM, &type, &format, &count, &fill, &data) == Success)
219 Atom * values = (Atom *) data;
221 for (i = 0; i < count; i++)
223 if(values[i] == atom1 || (atom2 && values[i] == atom2))
226 memmove(values + i, values + i + 1, sizeof(Atom) * (count - i - 1));
231 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_state], XA_ATOM, 32, PropModeReplace, (byte *)values, (int)count);
237 XClientMessageEvent event = { 0 };
238 event.type = ClientMessage;
239 event.message_type = atoms[_net_wm_state];
240 event.display = xGlobalDisplay;
242 event.window = windowHandle;
243 event.send_event = 1;
245 event.data.l[0] = action;
246 event.data.l[1] = atom1;
247 event.data.l[2] = atom2;
248 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false,
249 SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
255 static X11Time timeStamp;
257 class XWindowData : struct
260 XVisualInfo * visual;
265 bool gotFrameExtents;
266 bool currentlyVisible;
269 bool XGetBorderWidths(Window window, Box box)
271 XWindowData windowData = window.windowData;
274 if(window.state == maximized)
275 box = { 0, 0, 0, 0 };
277 box = windowData.decor;
283 static Visual * FindFullColorVisual(X11Display *dpy, int * depth)
286 XVisualInfo *vinfo_ret;
289 vinfo._class = TrueColor;
290 vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
296 if(vinfo_ret[numitems-1].depth > maxdepth)
297 maxdepth = vinfo_ret[numitems-1].depth;
300 XFree((void *) vinfo_ret);
303 if(XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, TrueColor, &vinfo))
313 static void RepositionDesktop(bool updateChildren)
317 Screen * x_screen = XDefaultScreenOfDisplay(xGlobalDisplay);
322 unsigned long len, fill;
324 static double lastTime = 0, time;
327 if(desktopW && desktopH && time - lastTime < 1.5) return;
330 w = XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
331 h = XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
332 x_root = XRootWindowOfScreen(x_screen);
334 if(atomsSupported[_net_number_of_desktops])
336 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_number_of_desktops], 0, 1, False,
337 XA_CARDINAL, &type, &format, &len, &fill,
340 printf("cant get xa desktops property\n");
346 desktops = (int)*(long *)data;
348 //printf("_NET_NUMBER_OF_DESKTOPS is %d\n", desktops);
355 if(atomsSupported[_net_current_desktop])
357 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_current_desktop], 0, 1, False,
358 XA_CARDINAL, &type, &format, &len, &fill,
361 printf("cant get xa current property\n");
366 current = (int)*(long *)data;
370 //printf("_NET_CURRENT_DESKTOP is %d\n", current);
373 if(atomsSupported[_net_workarea])
377 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_workarea], 0, (4 * 32),
378 False, AnyPropertyType, &type, &format, &len,
379 &fill, &data) != Success)
381 //printf("warning\n");
385 if(type == None || format == 0)
397 workareas = (long *)data;
399 x = (int)workareas[current * 4];
400 y = (int)workareas[current * 4 + 1];
401 w = (int)workareas[current * 4 + 2];
402 h = (int)workareas[current * 4 + 3];
404 //printf("_NET_WORKAREA is x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
408 // printf("Work Area width: %d, height %d\n", w, h);
411 if(desktopX != x || desktopY != y || desktopW != w || desktopH != h)
414 // Don't change the desktop area if another fullscreen application is running
415 // (Attemps to solve the debugging IDE being re-activated while switching debugged app between fullscreen/windowed on Cinnamon)
416 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_active_window], 0, 32,
417 False, AnyPropertyType, &type, &format, &len,
418 &fill, &data) == Success && data)
420 X11Window active = *(long *)data;
422 if(XGetWindowProperty(xGlobalDisplay, active, atoms[_net_wm_state], 0, 32, False,
423 XA_ATOM, &type, &format, &len, &fill, &data) == Success)
425 Atom * values = (Atom *) data;
427 for (i = 0; i < len; i++)
429 if(values[i] == atoms[_net_wm_state_fullscreen])
440 guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
449 /****************************************************************************
450 /// PRIVATE UTILITY FUNCTIONS /////////////
451 ****************************************************************************/
452 // --- Keyboard Input ---
453 #define KEYCODE_HOME 0x59
454 #define KEYCODE_UP 0x5A
455 #define KEYCODE_PGUP 0x5B
456 #define KEYCODE_LEFT 0x5C
457 #define KEYCODE_RIGHT 0x5E
458 #define KEYCODE_END 0x5F
459 #define KEYCODE_DOWN 0x60
460 #define KEYCODE_PGDWN 0x61
461 #define KEYCODE_INS 0x62
462 #define KEYCODE_DEL 0x63
463 #define KEYCODE_SLASH 0x68
466 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
467 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
468 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
469 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
470 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
471 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
472 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
473 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
474 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
475 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
476 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
477 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
478 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
479 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
482 static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEvent * event)
487 KeySym keysym = NoSymbol;
490 static long bufsize = 16;
491 static char *buf = NULL;
492 XWindowData windowData = window.windowData;
495 if(window.destroyed) return result;
497 // Logf("Got 0x%x (%d)\n", keyCode, key);
499 // Translate the key code
502 //Logf("Got 0x%x (%d)\n", key, key);
506 case KEYCODE_HOME: key = home; break;
507 case KEYCODE_UP: key = up; break;
508 case KEYCODE_PGUP: key = pageUp; break;
509 case KEYCODE_LEFT: key = left; break;
510 case KEYCODE_RIGHT: key = right; break;
511 case KEYCODE_END: key = end; break;
512 case KEYCODE_DOWN: key = down; break;
513 case KEYCODE_PGDWN: key = pageDown; break;
514 case KEYCODE_INS: key = insert; break;
515 case KEYCODE_DEL: key = del; break;
516 case KEYCODE_SLASH: key = keyPadSlash; break;
519 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
523 Logf("Key up: %3d (%2x)\n", key, key);
525 Logf("Key down: %3d (%2x)\n", key, key);
529 buf = malloc((uint)bufsize);
530 if(windowData && windowData.ic)
532 buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
533 if (status == XBufferOverflow)
535 buf = realloc(buf, (uint)(bufsize = buflength));
536 buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
538 if(status != XLookupKeySym && status != XLookupBoth && release == 1)
539 keysym = XLookupKeysym(event, 0);
543 keysym = XLookupKeysym(event, 0);
546 // printf("Got keysym %d (%x)\n", keysym, keysym);
547 if(keysym != NoSymbol) //status == XLookupKeySym || status == XLookupBoth)
551 case XK_BackSpace: key = backSpace; break;
552 case XK_ISO_Left_Tab: key = tab; break;
553 case XK_Tab: key = tab; break;
556 case XK_Return: key = enter; break;
557 case XK_Pause: key = pauseBreak; break;
558 case XK_Scroll_Lock: key = scrollLock; break;
559 case XK_Sys_Req: key = printScreen; break;
560 case XK_Escape: key = escape; break;
561 case XK_Delete: key = del; break;
563 case XK_Home: key = home; break;
564 case XK_Left: key = left; break;
565 case XK_Up: key = up; break;
566 case XK_Right: key = right; break;
567 case XK_Down: key = down; break;
569 case XK_Page_Up: key = pageUp; break;
571 case XK_Page_Down: key = pageDown; break;
572 case XK_End: key = end; break;
578 case XK_Insert: key = insert; break;
585 case XK_Help: key = insert; break;
587 case XK_Break: key = Key { pauseBreak, ctrl = true }; break;
589 case XK_Mode_switch: key = leftAlt; break;
591 // case XK_script_switch:
592 case XK_Num_Lock: key = numLock; break;
596 case XK_KP_Enter: key = keyPadEnter; break;
601 case XK_KP_Home: key = keyPadHome; break;
602 case XK_KP_Left: key = keyPadLeft; break;
603 case XK_KP_Up: key = keyPadUp; break;
604 case XK_KP_Right: key = keyPadRight; break;
605 case XK_KP_Down: key = keyPadDown; break;
607 case XK_KP_Page_Up: key = keyPadPageUp; break;
609 case XK_KP_Page_Down:key = keyPadPageDown; break;
610 case XK_KP_End: key = keyPadEnd; break;
612 case XK_KP_Insert: key = keyPadInsert; break;
613 case XK_KP_Delete: key = keyPadDelete; break;
615 case XK_KP_Multiply: key = keyPadStar; break;
616 case XK_KP_Add: key = keyPadPlus; break;
617 case XK_KP_Separator:key = keyPadDelete; break;
618 case XK_KP_Subtract: key = keyPadMinus; break;
619 // case XK_KP_Decimal:
620 case XK_KP_Divide: key = keyPadSlash; break;
622 case XK_KP_0: key = keyPad0; break;
623 case XK_KP_1: key = keyPad1; break;
624 case XK_KP_2: key = keyPad2; break;
625 case XK_KP_3: key = keyPad3; break;
626 case XK_KP_4: key = keyPad4; break;
627 case XK_KP_5: key = keyPad5; break;
628 case XK_KP_6: key = keyPad6; break;
629 case XK_KP_7: key = keyPad7; break;
630 case XK_KP_8: key = keyPad8; break;
631 case XK_KP_9: key = keyPad9; break;
633 case XK_F1: key = f1; break;
634 case XK_F2: key = f2; break;
635 case XK_F3: key = f3; break;
636 case XK_F4: key = f4; break;
637 case XK_F5: key = f5; break;
638 case XK_F6: key = f6; break;
639 case XK_F7: key = f7; break;
640 case XK_F8: key = f8; break;
641 case XK_F9: key = f9; break;
642 case XK_F10: key = f10; break;
643 case XK_F11: key = f11; break;
645 case XK_F12: key = f12; break;
695 case XK_Shift_L: key = leftShift; break;
696 case XK_Shift_R: key = rightShift; break;
697 case XK_Control_L: key = leftControl; break;
698 case XK_Control_R: key = rightControl; break;
699 case XK_Caps_Lock: key = capsLock; break;
700 // case XK_Shift_Lock:
703 case XK_Alt_L: key = leftAlt; break;
704 case XK_Alt_R: key = rightAlt; break;
710 case XK_space: key = space; break;
711 case XK_exclam: key = bang; break;
712 case XK_quotedbl: key = doubleQuote; break;
713 case XK_numbersign: key = pound; break;
714 case XK_dollar: key = dollar; break;
715 case XK_percent: key = percent; break;
716 case XK_ampersand: key = ampersand; break;
717 case XK_apostrophe: key = quote; break;
718 // case XK_quoteright: key = quote; break;
719 case XK_parenleft: key = leftParanthesis; break;
720 case XK_parenright: key = rightParanthesis; break;
721 case XK_asterisk: key = star; break;
722 case XK_plus: key = plus; break;
723 case XK_comma: key = comma; break;
724 case XK_minus: key = minus; break;
725 case XK_period: key = period; break;
726 case XK_slash: key = slash; break;
727 case XK_0: key = k0; break;
728 case XK_1: key = k1; break;
729 case XK_2: key = k2; break;
730 case XK_3: key = k3; break;
731 case XK_4: key = k4; break;
732 case XK_5: key = k5; break;
733 case XK_6: key = k6; break;
734 case XK_7: key = k7; break;
735 case XK_8: key = k8; break;
736 case XK_9: key = k9; break;
737 case XK_colon: key = colon; break;
738 case XK_semicolon: key = semicolon; break;
739 case XK_less: key = smallerThan; break;
740 case XK_equal: key = equal; break;
741 case XK_greater: key = greaterThan; break;
742 case XK_question: key = questionMark; break;
743 case XK_at: key = ampersand; break;
744 case XK_A: key = a; break;
745 case XK_B: key = b; break;
746 case XK_C: key = c; break;
747 case XK_D: key = d; break;
748 case XK_E: key = e; break;
749 case XK_F: key = f; break;
750 case XK_G: key = g; break;
751 case XK_H: key = h; break;
752 case XK_I: key = i; break;
753 case XK_J: key = j; break;
754 case XK_K: key = k; break;
755 case XK_L: key = l; break;
756 case XK_M: key = m; break;
757 case XK_N: key = n; break;
758 case XK_O: key = o; break;
759 case XK_P: key = p; break;
760 case XK_Q: key = q; break;
761 case XK_R: key = r; break;
762 case XK_S: key = s; break;
763 case XK_T: key = t; break;
764 case XK_U: key = u; break;
765 case XK_V: key = v; break;
766 case XK_W: key = w; break;
767 case XK_X: key = x; break;
768 case XK_Y: key = y; break;
769 case XK_Z: key = z; break;
770 case XK_bracketleft: key = leftBracket; break;
771 case XK_backslash: key = backSlash; break;
772 case XK_bracketright: key = rightBracket; break;
773 case XK_asciicircum: key = circumflex; break;
774 case XK_underscore: key = underscore; break;
775 // case XK_grave: key = backQuote; break;
776 case XK_quoteleft: key = backQuote; break;
777 case XK_a: key = a; break;
778 case XK_b: key = b; break;
779 case XK_c: key = c; break;
780 case XK_d: key = d; break;
781 case XK_e: key = e; break;
782 case XK_f: key = f; break;
783 case XK_g: key = g; break;
784 case XK_h: key = h; break;
785 case XK_i: key = i; break;
786 case XK_j: key = j; break;
787 case XK_k: key = k; break;
788 case XK_l: key = l; break;
789 case XK_m: key = m; break;
790 case XK_n: key = n; break;
791 case XK_o: key = o; break;
792 case XK_p: key = p; break;
793 case XK_q: key = q; break;
794 case XK_r: key = r; break;
795 case XK_s: key = s; break;
796 case XK_t: key = t; break;
797 case XK_u: key = u; break;
798 case XK_v: key = v; break;
799 case XK_w: key = w; break;
800 case XK_x: key = x; break;
801 case XK_y: key = y; break;
802 case XK_z: key = z; break;
803 case XK_braceleft: key = leftBracket; break;
804 case XK_bar: key = pipe; break;
805 case XK_braceright: key = rightBracket; break;
806 case XK_asciitilde: key = tilde; break;
811 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
812 // 127 is delete, we don't treat that as a character (Use (SmartKey)key == del)
813 if(ch == 128 || ch == 127) ch = 0;
816 if(keysym == XK_ISO_Left_Tab)
818 if(key != leftShift && key != rightShift && event->state & ShiftMask)
820 if(key != leftControl && key != rightControl && event->state & ControlMask)
822 if(key != leftAlt && key != rightAlt && event->state & Mod1Mask)
824 if(fullScreenMode && key == tab)
826 XUngrabKeyboard(xGlobalDisplay, CurrentTime);
827 guiApp.SetAppFocus(false);
828 SetNETWMState((X11Window)window.windowHandle, true, remove, atoms[_net_wm_state_fullscreen], 0);
829 XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
835 if(key != leftAlt && key != rightAlt && event->state & (1<<13))
843 // 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);
850 if(code < KeyCode::enumSize) keyStates[code] = false;
851 if(windowData && windowData.ic) ch = buflength ? UTF8GetChar(buf, &numBytes) : 0;
852 if(ch == 127) ch = 0;
853 // printf("Release! %d %d %d\n", keysym, code, ch);
854 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, code, ch);
861 if(code < KeyCode::enumSize) keyStates[code] = true;
863 if(windowData.ic && buflength)
865 for(c = 0; c<buflength;)
868 ch = UTF8GetChar(buf + c, &numBytes);
869 if(ch == 127) ch = 0;
870 result = window.KeyMessage((c == 0) ?
871 __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown : __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit,
872 (c == 0) ? code : 0, ch);
874 if(!numBytes) c = buflength;
878 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, code, ch);
882 if(buflength && windowData.ic)
883 for(c = 0; c<buflength;)
886 ch = UTF8GetChar(buf + c, &numBytes);
887 if(ch == 127) ch = 0;
888 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
890 if(!numBytes) c = buflength;
893 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
901 static uint E_CALL XEventThread(void * data)
907 if(xTerminate) break;
910 XPeekEvent(xGlobalDisplay, &e);
912 guiApp.SignalEvent();
919 static X11Bool EventChecker(void *display, XEvent *event, char * data)
921 return (!data || (event->type == (int) data)) && event->type != NoExpose && event->type != GraphicsExpose;
924 static X11Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
926 return (!data || (event->window == (X11Window) data)) && event->type == ConfigureNotify;
929 static enum FrameExtentSupport { unknown, working, broken };
931 static FrameExtentSupport frameExtentSupported;
932 static Time frameExtentRequest;
933 static X11Window frameExtentWindow;
935 static uint timerDelay = MAXINT;
936 #define RESOLUTION (18.2 * 100)
937 static uint XTimerThread(Thread thread)
939 int s = ConnectionNumber(xGlobalDisplay);
941 Time lastTime, thisTime;
942 lastTime = GetTime();
947 //bool waitSemaphore = false;
948 fd_set readSet, writeSet, exceptSet;
949 struct timeval tv = { (timerDelay == MAXINT) ? 0 : (timerDelay / 1000000), (timerDelay == MAXINT) ? (int)(1000000 / 18.2) : (timerDelay % 1000000) };
951 if(xTerminate) break;
956 FD_SET(s, &exceptSet);
958 if(select(s + 1, &readSet, null, null, &tv))
960 if(FD_ISSET(s, &readSet))
963 if(frameExtentSupported == unknown && frameExtentRequest && GetTime() - frameExtentRequest > 1)
965 XPropertyEvent event = { 0 };
966 event.type = PropertyNotify;
967 event.state = PropertyNewValue;
968 event.atom = atoms[_net_frame_extents];
969 event.display = xGlobalDisplay;
971 event.window = frameExtentWindow;
972 event.send_event = 1;
974 frameExtentSupported = broken;
976 XSendEvent(xGlobalDisplay, frameExtentWindow, bool::false,
977 PropertyChangeMask, (union _XEvent *)&event);
980 guiApp.SignalEvent();
985 Sleep(1.0 / RESOLUTION);
986 thisTime = GetTime();
987 if(xTerminate) break;
988 if(thisTime - lastTime > (1.0 / 18.2))
990 guiApp.SignalEvent();
998 XLockDisplay(xGlobalDisplay);
999 if(XCheckIfEvent(xGlobalDisplay, &xEvent, EventChecker, null))
1002 guiApp.SignalEvent();
1004 XUnlockDisplay(xGlobalDisplay);
1013 static int MyXErrorHandler(X11Display * display, XErrorEvent * event)
1017 XGetErrorText(xGlobalDisplay, event->error_code, buffer, sizeof(buffer));
1019 Logf("X Error: %s\n", buffer);
1024 static int MyXIOErrorHandler(X11Display * display)
1026 Log("X IO Error\n");
1030 // Motif Hints (to get rid of the decorations)
1031 #define MWM_HINTS_FUNCTIONS (1L << 0)
1032 #define MWM_HINTS_DECORATIONS (1L << 1)
1033 #define MWM_HINTS_INPUT_MODE (1L << 2)
1034 #define MWM_HINTS_STATUS (1L << 3)
1036 #define MWM_DECOR_ALL (1L << 0)
1037 #define MWM_DECOR_BORDER (1L << 1)
1038 #define MWM_DECOR_RESIZEH (1L << 2)
1039 #define MWM_DECOR_TITLE (1L << 3)
1040 #define MWM_DECOR_MENU (1L << 4)
1041 #define MWM_DECOR_MINIMIZE (1L << 5)
1042 #define MWM_DECOR_MAXIMIZE (1L << 6)
1044 #define MWM_FUNC_ALL (1L << 0)
1045 #define MWM_FUNC_RESIZE (1L << 1)
1046 #define MWM_FUNC_MOVE (1L << 2)
1047 #define MWM_FUNC_MINIMIZE (1L << 3)
1048 #define MWM_FUNC_MAXIMIZE (1L << 4)
1049 #define MWM_FUNC_CLOSE (1L << 5)
1053 unsigned long flags;
1054 unsigned long functions;
1055 unsigned long decorations;
1057 unsigned long status;
1060 static void WaitForViewableWindow(Window window)
1063 //Logf("Wait for viewable %s\n", window.name);
1064 XFlush(xGlobalDisplay);
1065 //while(attempts++ < 40)
1068 XWindowAttributes attributes = { 0 };
1070 if(!XGetWindowAttributes(xGlobalDisplay, (X11Window)window.windowHandle, &attributes))
1072 if(attributes.map_state == IsViewable)
1075 Sleep(1.0 / RESOLUTION);
1079 static bool RequestFrameExtents(X11Window windowHandle)
1081 if(frameExtentSupported != broken)
1083 // Request decoration frame extents
1084 XClientMessageEvent event = { 0 };
1085 event.type = ClientMessage;
1086 event.message_type = atoms[_net_request_frame_extents];
1087 event.display = xGlobalDisplay;
1089 event.window = windowHandle;
1090 event.send_event = 1;
1093 if(frameExtentSupported == unknown && !frameExtentRequest)
1095 frameExtentRequest = GetTime();
1096 frameExtentWindow = windowHandle;
1099 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false,
1100 SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
1106 static bool GetFrameExtents(Window window, bool update)
1108 XWindowData windowData = window.windowData;
1109 bool result = false;
1111 unsigned long len, fill;
1115 if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.windowHandle,
1116 atoms[_net_frame_extents], 0, 4,
1117 False, XA_CARDINAL, &type, &format, &len,
1118 &fill, &data) == Success && data)
1120 long *extents = (long *)data;
1121 bool change = extents[0] != windowData.decor.left ||
1122 extents[1] != windowData.decor.right ||
1123 extents[2] != windowData.decor.top ||
1124 extents[3] != windowData.decor.bottom;
1126 bool hadFrameExtents = windowData.gotFrameExtents;
1127 Box oldDecor = windowData.decor;
1129 frameExtentSupported = working;
1130 frameExtentWindow = 0;
1131 frameExtentRequest = 0;
1133 if(!hadFrameExtents || extents[0] || extents[1] || extents[2] || extents[3])
1137 left = (int)extents[0], right = (int)extents[1],
1138 top = (int)extents[2], bottom = (int)extents[3]
1140 windowData.gotFrameExtents = true;
1141 if(update && change && ((Window)window).clientSize.w > 0)
1143 int x = window.position.x, y = window.position.y, w = window.size.w, h = window.size.h;
1144 if(!hadFrameExtents && window.state != maximized)
1146 window.ComputeAnchors(
1147 window.normalAnchor,
1148 window.normalSizeAnchor,
1153 x += windowData.decor.left - oldDecor.left;
1154 y += windowData.decor.top - oldDecor.top;
1156 w += windowData.decor.left - oldDecor.left + windowData.decor.right - oldDecor.right;
1157 h += windowData.decor.top - oldDecor.top + windowData.decor.bottom - oldDecor.bottom;
1160 if(window.state != maximized)
1162 window.Position(x, y, w, h, true, true, true, true, false, !hadFrameExtents && window.state != maximized);
1163 XInterface::UpdateRootWindow(window);
1173 static bool WaitForFrameExtents(Window window)
1176 //XFlush(xGlobalDisplay);
1177 while(attempts++ < 10)
1179 if(GetFrameExtents(window, false)) return true;
1180 Sleep(1.0 / RESOLUTION);
1185 /****************************************************************************
1186 /// DRIVER IMPLEMENTATION /////////////
1187 ****************************************************************************/
1189 /*static */class HiResTimer : Thread
1208 // Sleep(delay / 1000000.0);
1209 struct timeval tv = { delay / 1000000, delay % 1000000 };
1210 select(0,null,null,null, &tv);
1211 guiApp.SignalEvent();
1217 static HiResTimer hiResTimer { };
1221 #include <sys/ipc.h>
1222 #include <sys/shm.h>
1234 static int terminatePid;
1236 static void SigIntHandler(int value)
1238 // printf("SigHandler %d\n", getpid());
1239 if(!terminatePid || terminatePid == getpid())
1242 terminatePid = getpid();
1243 // printf("terminateX now equals %d\n", terminateX);
1244 if(guiApp && guiApp.semaphore)
1245 guiApp.semaphore.Release();
1248 struct shmid_ds info;
1249 int maxid = shmctl (0, SHM_INFO, &info);
1250 int pid = getpgrp();
1251 int thisPid = getpid();
1252 //if(thisPid == pid)
1258 for(id = 0; id <= maxid; id++)
1260 struct shmid_ds shmseg;
1262 if((shmid = shmctl(id, SHM_STAT, &shmseg)) >= 0)
1264 if(shmseg.shm_cpid == pid || shmseg.shm_cpid == thisPid)
1266 printf("%d (%d) belongs to us (%d)\n", shmid, id, shmseg.shm_cpid);
1267 shmctl(shmid, IPC_RMID, 0);
1275 /*else if(guiApp.desktop)
1276 guiApp.desktop.Destroy(0);*/
1279 class XInterface : Interface
1281 class_property(name) = "X";
1283 // --- User Interface System ---
1286 setlocale(LC_ALL, "en_US.UTF-8");
1289 XSetLocaleModifiers("");
1290 XSetErrorHandler(MyXErrorHandler);
1291 XSetIOErrorHandler(MyXIOErrorHandler);
1293 signal(SIGINT, SigIntHandler);
1296 xGlobalDisplay = XOpenDisplay(null);
1297 // XSynchronize(xGlobalDisplay, True);
1298 frameExtentSupported = unknown;
1300 joystickFD[0] = open("/dev/js0", O_RDONLY);
1301 joystickFD[1] = open("/dev/js1", O_RDONLY);
1302 joystickFD[2] = open("/dev/js2", O_RDONLY);
1303 joystickFD[3] = open("/dev/js3", O_RDONLY);
1305 systemCursors[iBeam] = XCreateFontCursor(xGlobalDisplay, XC_xterm);
1306 systemCursors[cross] = XCreateFontCursor(xGlobalDisplay, XC_tcross);
1307 systemCursors[moving] = XCreateFontCursor(xGlobalDisplay, XC_fleur);
1308 systemCursors[sizeNESW] = XCreateFontCursor(xGlobalDisplay, XC_bottom_left_corner);
1309 systemCursors[sizeNS] = XCreateFontCursor(xGlobalDisplay, XC_sb_v_double_arrow);
1310 systemCursors[sizeNWSE] = XCreateFontCursor(xGlobalDisplay, XC_bottom_right_corner);
1311 systemCursors[sizeWE] = XCreateFontCursor(xGlobalDisplay, XC_sb_h_double_arrow);
1312 systemCursors[hand] = XCreateFontCursor(xGlobalDisplay, XC_hand2);
1313 systemCursors[arrow] = XCreateFontCursor(xGlobalDisplay, XC_left_ptr);
1317 XWindowAttributes attributes = { 0 };
1318 XGetWindowAttributes(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &attributes);
1319 xSystemDepth = attributes.depth;
1320 xSystemVisual = attributes.visual;
1321 switch(xSystemDepth)
1323 case 32: case 24: xSystemPixelFormat = pixelFormat888; break;
1328 XVisualInfo *vinfo_ret;
1331 vinfo.visualid = XVisualIDFromVisual(xSystemVisual);
1332 vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &vinfo, &numitems);
1335 xSystemPixelFormat = (vinfo_ret->green_mask == 0x3E0) ? pixelFormat555 : pixelFormat565;
1337 if(GetXRenderFormat(xSystemPixelFormat, 0))
1339 // printf("Got X Render format %d\n", xSystemPixelFormat);
1343 printf("No X Format?\n");
1344 xSystemPixelFormat = (xSystemPixelFormat == pixelFormat555) ? pixelFormat565 : pixelFormat555;
1346 // vinfo._class = DirectColor;
1348 vinfo.bits_per_rgb = 5;
1349 vinfo.depth = xSystemDepth;
1350 vinfo.green_mask = (xSystemPixelFormat == pixelFormat555) ? 0x3E0 : 0x7E0;
1351 vinfo.red_mask = (xSystemPixelFormat == pixelFormat555) ? 0x7C00 : 0xF800;
1352 XFree((void *) vinfo_ret);
1353 if(XMatchVisualInfo(xGlobalDisplay, DefaultScreen(xGlobalDisplay), vinfo.depth, vinfo._class, &vinfo))
1354 //vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualDepthMask|VisualRedMaskMask|VisualGreenMaskMask|VisualBlueMaskMask, &vinfo, &numitems);
1357 //vinfo = *vinfo_ret;
1358 if(GetXRenderFormat(xSystemPixelFormat, 0))
1360 // printf("Got X Render format %d (second try)\n", xSystemPixelFormat);
1361 // printf("red mask: %x, green mask: %x, blue mask: %x\n", vinfo.red_mask,vinfo.green_mask, vinfo.blue_mask);
1363 xSystemVisual = vinfo.visual;
1366 printf("Could not get a 555 visual\n");
1372 // printf("Got a depth of %d\n", xSystemDepth);
1375 int major, minor, pixmaps;
1376 xSharedMemory = XShmQueryExtension(xGlobalDisplay) && XShmQueryVersion(xGlobalDisplay, &major, &minor, &pixmaps) && pixmaps;
1379 // printf("Opening IM\n");
1380 im = XOpenIM(xGlobalDisplay, null, null, null);
1383 XColor fore = { 0 }, back = { 0 };
1384 Pixmap pixmap = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1385 Pixmap mask = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1386 XSetWindowAttributes attributes = { 0 };
1388 XkbSetDetectableAutoRepeat(xGlobalDisplay, True, &autoRepeatDetectable);
1390 XInternAtoms(xGlobalDisplay, (char**)atomNames, AtomIdents::enumSize, False, atoms);
1392 // Check which atoms are supported by the WM
1395 unsigned long count, fill;
1399 if(XGetWindowProperty(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), atoms[_net_supported],
1400 0, 10000, False, XA_ATOM, &type, &format, &count, &fill, (void *)&data) == Success)
1403 for (i = 0; i < count; i++)
1406 for(j = 0; j < AtomIdents::enumSize; j++)
1408 if(atoms[j] == data[i])
1410 atomsSupported[j] = true;
1420 if(atomsSupported[_net_workarea])
1421 printf("Warning: _NET_WORKAREA extension not supported\n");
1424 attributes.override_redirect = True;
1427 windowContext = XUniqueContext();
1430 nullCursor = XCreatePixmapCursor(xGlobalDisplay, pixmap, mask, &fore, &back, 0, 0);
1431 confineWindow = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
1432 0,0,1,1,0, CopyFromParent, InputOnly, xSystemVisual /*CopyFromParent*/, CWOverrideRedirect, &attributes);
1435 // IM initialization
1437 /*ic = XCreateIC(im, XNInputStyle,
1438 XIMPreeditNothing | XIMStatusNothing,
1439 // XIMPreeditNone | XIMStatusNone | XIMPreeditPosition | XIMStatusArea | XIMPreeditArea | XIMStatusArea | XIMPreeditNothing | XIMStatusNothing,
1440 XNClientWindow, confineWindow, XNFocusWindow, confineWindow, 0);
1445 XIMStyle clientCanDo;
1446 XIMStyle styleWeWillUse = null;
1448 XVaNestedList arglist;
1449 unsigned long imEventMask;
1451 XGetImValues(im, XNQueryInputStyle, &IMcando, null);
1454 XIMPreeditNone | XIMStatusNone |
1455 XIMPreeditPosition | XIMStatusArea |
1456 XIMPreeditArea | XIMStatusArea |
1457 XIMPreeditNothing | XIMStatusNothing;
1459 for(i=0; i<IMcando->count_styles; i++)
1462 tmpStyle = IMcando->support_styles[i];
1463 if ( ((tmpStyle & clientCanDo) == tmpStyle) && prefer(tmpStyle, styleWeWillUse) )
1464 styleWeWillUse = tmpStyle;
1466 if(styleWeWillUse = null)
1470 arglist = XVaCreateNestedList(0, XNFontSet, fontset,
1472 WhitePixel(xGlobalDisplay, screen),
1474 BlackPixel(xGlobalDisplay, screen),
1476 ic = XCreateIC(im, XNInputStyle, styleWeWillUse,
1477 XNClientWindow, window, XNFocusWindow, window,
1478 XNStatusAttributes, arglist,
1479 XNPreeditAttributes, arglist, NULL);
1484 XGetWindowAttributes(xGlobalDisplay, win, &winAtts);
1485 XGetICValues(ic, XNFilterEvents, &imEventMask, null);
1486 imEventMask |= winAtts.your_event_mask;
1487 XSelectInput(xGlobalDisplay, window, imEventMask);
1493 timerThread = Thread { };
1495 timerThread.Main = XTimerThread;
1496 timerThread.Create();
1509 // WHY WAS THIS COMMENTED HERE?
1510 // Probably because it was causing crashes, the proper fix should now be in DestroyRootWindow
1514 xSemaphore.Release();
1520 XFreeCursor(xGlobalDisplay, systemCursors[iBeam]);
1521 XFreeCursor(xGlobalDisplay, systemCursors[cross]);
1522 XFreeCursor(xGlobalDisplay, systemCursors[moving]);
1523 XFreeCursor(xGlobalDisplay, systemCursors[sizeNESW]);
1524 XFreeCursor(xGlobalDisplay, systemCursors[sizeNS]);
1525 XFreeCursor(xGlobalDisplay, systemCursors[sizeNWSE]);
1526 XFreeCursor(xGlobalDisplay, systemCursors[sizeWE]);
1527 XFreeCursor(xGlobalDisplay, systemCursors[hand]);
1528 XFreeCursor(xGlobalDisplay, systemCursors[arrow]);
1530 //XPutBackEvent(xGlobalDisplay, &e);
1534 /*if(windowData && windowData.ic)
1536 XDestroyIC(windowData.ic);
1537 windowData.ic = null;
1544 XCloseDisplay(xGlobalDisplay);
1545 xGlobalDisplay = null;
1547 if(joystickFD[0] != -1) close(joystickFD[0]);
1548 if(joystickFD[1] != -1) close(joystickFD[1]);
1549 if(joystickFD[2] != -1) close(joystickFD[2]);
1550 if(joystickFD[3] != -1) close(joystickFD[3]);
1553 #define DBLCLICK_DELAY 300 // 0.3 second
1554 #define DBLCLICK_DELTA 1
1556 bool ProcessInput(bool processAll)
1558 bool eventAvailable = false;
1561 if(!fullScreenMode) RepositionDesktop(true);
1563 //* XLockDisplay(xGlobalDisplay);
1564 while(!xTerminate && (/*gotAnXEvent || */XCheckIfEvent(xGlobalDisplay, &e, EventChecker, null)))
1566 Window window = null;
1567 XAnyEvent * thisEvent = (XAnyEvent *)&e;
1568 // printf("Got an event: %d\n", thisEvent->type);
1570 //if(gotAnXEvent) { thisEvent = (XAnyEvent *)&xEvent; gotAnXEvent = false; }
1572 if(im && XFilterEvent((union _XEvent *)thisEvent, None))
1574 eventAvailable = true;
1575 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1578 XWindowData windowData = window.windowData;
1579 static uint lastKeyCode = 0;
1580 switch(thisEvent->type)
1584 XKeyEvent * event = (XKeyEvent *) thisEvent;
1586 timeStamp = event->time;
1589 Window modalRoot = window.FindModal();
1590 XWindowData windowData;
1592 activeWindow = (X11Window)window.windowHandle;
1594 if(!window.parent || window != window.parent.activeChild)
1597 modalRoot.ExternalActivate(true, true, window, null);
1599 window.ExternalActivate(true, true, window, null); // lastActive);
1600 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1601 if(windowData && windowData.ic)
1603 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
1604 XSetICFocus(windowData.ic);
1608 //*XUnlockDisplay(xGlobalDisplay);
1609 ProcessKeyMessage(window, event->keycode, (event->keycode == lastKeyCode) ? 2 : 0, event);
1610 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1611 lastKeyCode = event->keycode;
1617 XKeyEvent * event = (XKeyEvent *) thisEvent;
1620 timeStamp = event->time;
1621 if(!autoRepeatDetectable && XCheckIfEvent(xGlobalDisplay, (XEvent *)&nextEvent, EventChecker, (void *)KeyPress))
1623 if(im && XFilterEvent(&nextEvent, None))
1626 if(((XKeyEvent *)&nextEvent)->keycode == event->keycode)
1628 //*XUnlockDisplay(xGlobalDisplay);
1629 ProcessKeyMessage(window, event->keycode, 2, event);
1633 //*XUnlockDisplay(xGlobalDisplay);
1634 // printf("Keycode not the same :(\n");
1635 ProcessKeyMessage(window, event->keycode, 1, event);
1636 ProcessKeyMessage(window, ((XKeyEvent *)&nextEvent)->keycode, 0, (XKeyEvent *)&nextEvent);
1641 //*XUnlockDisplay(xGlobalDisplay);
1642 // printf("No KeyPress ahead\n");
1643 ProcessKeyMessage(window, event->keycode, 1, event);
1645 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1650 XButtonEvent * event = (XButtonEvent *) thisEvent;
1652 static double lastTime[3];
1653 static Window lastWindow[3];
1654 static Point lastPos[3];
1656 Modifiers keyFlags = 0;
1658 uint button, buttonDouble, whichButton;
1660 int x = event->x_root, y = event->y_root;
1661 timeStamp = event->time;
1662 if(event->button == Button1)
1664 // Force a raise on click here to deal with confused active state preventing to bring the window up
1667 if(!atomsSupported[_net_active_window] && !window.isRemote)
1668 XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
1669 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, CurrentTime);
1671 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
1672 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
1674 buttonMask = Button1Mask;
1675 keyFlags.left = true;
1676 buttonsState.left = true;
1678 else if(event->button == Button3)
1680 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
1681 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
1683 buttonMask = Button3Mask;
1684 keyFlags.right = true;
1685 buttonsState.right = true;
1689 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
1690 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
1692 buttonMask = Button2Mask;
1693 keyFlags.middle = true;
1694 buttonsState.middle = true;
1696 if(event->state & buttonMask)
1699 doubleClick = event->time - lastTime[whichButton] < DBLCLICK_DELAY &&
1700 window == lastWindow[whichButton] &&
1701 Abs(event->x_root - lastPos[whichButton].x) < DBLCLICK_DELTA &&
1702 Abs(event->y_root - lastPos[whichButton].y) < DBLCLICK_DELTA;
1703 lastTime[whichButton] = doubleClick ? 0 : event->time;
1704 lastWindow[whichButton] = window;
1705 lastPos[whichButton].x = event->x_root;
1706 lastPos[whichButton].y = event->y_root;
1708 if(event->state & ShiftMask) keyFlags.shift = true;
1709 if(event->state & ControlMask) keyFlags.ctrl = true;
1710 if(event->state & Mod1Mask) keyFlags.alt = true;
1711 if(event->state & Button1Mask) keyFlags.left = true;
1712 if(event->state & Button2Mask) keyFlags.middle = true;
1713 if(event->state & Button3Mask) keyFlags.right = true;
1714 //*XUnlockDisplay(xGlobalDisplay);
1717 if(event->button == Button4 || event->button == Button5)
1719 window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, (event->button == Button4) ? wheelUp : wheelDown, 0);
1725 if(!window.MouseMessage(buttonDouble, x, y, &keyFlags, false, true))
1727 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1732 window.MouseMessage(button, x, y, &keyFlags, false, /*doubleClick? false : */true);
1734 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1740 Modifiers keyFlags = 0;
1741 XButtonEvent * event = (XButtonEvent *) thisEvent;
1744 int x = event->x_root, y = event->y_root;
1745 if(event->button == Button1)
1747 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
1748 buttonMask = Button1Mask;
1749 buttonsState.left = false;
1751 else if(event->button == Button3)
1753 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
1754 buttonMask = Button3Mask;
1755 buttonsState.right = false;
1759 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
1760 buttonMask = Button2Mask;
1761 buttonsState.middle = false;
1763 timeStamp = event->time;
1764 if(!(event->state & buttonMask)) break;
1765 if(event->state & ShiftMask) keyFlags.shift = true;
1766 if(event->state & ControlMask) keyFlags.ctrl = true;
1767 if(event->state & Mod1Mask) keyFlags.alt = true;
1768 if(event->state & Button1Mask) keyFlags.left = true;
1769 if(event->state & Button2Mask) keyFlags.middle = true;
1770 if(event->state & Button3Mask) keyFlags.right = true;
1771 if(guiApp.windowCaptured && guiApp.windowCaptured != window)
1773 // X hasn't noticed the capture yet, so fix it!
1774 x += window.absPosition.x;
1775 y += window.absPosition.y;
1776 window = guiApp.windowCaptured.rootWindow;
1777 x -= window.absPosition.x;
1778 y -= window.absPosition.y;
1780 //*XUnlockDisplay(xGlobalDisplay);
1782 window.MouseMessage(button, x, y, &keyFlags, false, false);
1784 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1789 static uint lastTime = 0;
1790 XMotionEvent * event = (XMotionEvent *) thisEvent;
1791 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, EventChecker, (void *)MotionNotify));
1792 // if(event->time - lastTime > 15)
1794 Modifiers keyFlags = 0;
1795 timeStamp = event->time;
1796 // int x = event->x_root, y = event->y_root;
1798 if(event->state & ShiftMask) keyFlags.shift = true;
1799 if(event->state & ControlMask) keyFlags.ctrl = true;
1800 if(event->state & Mod1Mask) keyFlags.alt = true;
1801 if(event->state & Button1Mask) keyFlags.left = true;
1802 if(event->state & Button2Mask) keyFlags.middle = true;
1803 if(event->state & Button3Mask) keyFlags.right = true;
1804 //*XUnlockDisplay(xGlobalDisplay);
1806 if(window == acquiredInputWindow)
1808 lastMouse.x = event->x_root;
1809 lastMouse.y = event->y_root;
1811 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove,
1812 event->x_root, event->y_root, &keyFlags, false, false);
1814 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1815 lastTime = (uint)event->time;
1821 XExposeEvent * event = (XExposeEvent *) thisEvent;
1823 box.left = event->x;
1825 box.right = box.left + event->width - 1;
1826 box.bottom = box.top + event->height - 1;
1827 window.UpdateDirty(box);
1830 case SelectionRequest:
1832 XSelectionRequestEvent *req = (XSelectionRequestEvent *) thisEvent;
1834 if(req->target == atoms[targets] && clipBoardData)
1836 Atom * supportedTargets = new Atom[4];
1837 supportedTargets[0] = atoms[targets];
1838 supportedTargets[1] = atoms[multiple];
1839 supportedTargets[2] = XA_STRING;
1840 supportedTargets[3] = atoms[utf8_string];
1841 XChangeProperty(xGlobalDisplay,req->requestor, req->_property,
1842 XA_ATOM,32,PropModeReplace, (byte *) supportedTargets, 4*sizeof(Atom));
1843 respond.xselection._property = req->_property;
1844 delete supportedTargets;
1846 else if((req->target == XA_STRING || req->target == atoms[utf8_string]) && clipBoardData)
1848 Atom _property = (req->_property == None) ? req->target : req->_property;
1849 XChangeProperty(xGlobalDisplay,req->requestor, _property,
1850 req->target/*req->_property*/,8,PropModeReplace, (byte *) clipBoardData, strlen(clipBoardData));
1851 respond.xselection._property = _property;
1854 respond.xselection._property = None;
1856 respond.xselection.type = SelectionNotify;
1857 respond.xselection.display = req->display;
1858 respond.xselection.requestor = req->requestor;
1859 respond.xselection.selection =req->selection;
1860 respond.xselection.target = req->target;
1861 respond.xselection.time = CurrentTime;
1862 XSendEvent(xGlobalDisplay, req->requestor,0,0,&respond);
1865 case SelectionClear:
1867 delete clipBoardData;
1872 lastMouse = acquireStart;
1874 guiApp.SetAppFocus(true);
1878 XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
1879 SetNETWMState((X11Window)window.windowHandle, true, add, atoms[_net_wm_state_fullscreen], 0);
1880 XGrabKeyboard(xGlobalDisplay, (X11Window)window.windowHandle, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1881 (xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, timeStamp);
1882 XInterface::UpdateRootWindow(window);
1886 if(activeWindow != (X11Window)window.windowHandle)
1888 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1889 Window modalRoot = window.FindModal();
1890 XWindowData windowData;
1892 activeWindow = (X11Window)window.windowHandle;
1894 if(window.parent && window == window.parent.activeChild) break;
1896 //if(window.creationActivation == activate)
1899 modalRoot.ExternalActivate(true, true, window, null); // lastActive);
1901 window.ExternalActivate(true, true, window, null); // lastActive);
1903 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1904 if(windowData && windowData.ic)
1906 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
1907 XSetICFocus(windowData.ic);
1909 //delete lastActive;
1910 //lastActive = window;
1911 //incref lastActive;
1918 if((X11Window)window.windowHandle == activeWindow)
1919 guiApp.SetAppFocus(false);
1923 SetNETWMState((X11Window)window.windowHandle, true, remove, atoms[_net_wm_state_fullscreen], 0);
1924 XUngrabKeyboard(xGlobalDisplay, CurrentTime);
1925 // -- This XIconifyWindow causes trouble on Gnome Classic
1926 XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
1931 //printf("Processing a FocusOut Event for %s (%x)\n", window._class.name, window);
1934 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusIn, (XEvent *)thisEvent))
1938 if(thisEvent->window == activeWindow)
1939 activeWindow = (X11Window)null;
1941 if(XCheckTypedEvent(xGlobalDisplay, FocusIn, (XEvent *)thisEvent))
1943 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusOut, (XEvent *)thisEvent))
1945 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1947 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1950 Window windowCopy = window;
1951 XWindowData windowData;
1952 if(windowCopy == windowCopy.parent.activeChild) break;
1954 windowCopy.ExternalActivate(true, true, windowCopy, lastActive);
1956 windowData = windowCopy.windowData;
1957 if(windowData && windowData.ic)
1958 XSetICFocus(windowData.ic);
1961 lastActive = windowCopy;
1966 // XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1969 if(window != window.parent.activeChild) break;
1971 window.ExternalActivate(false, true, window, lastActive);
1977 XWindowData windowData;
1978 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1982 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1983 Window modalRoot = window.FindModal();
1988 modalRoot.ExternalActivate(true, true, window, lastActive);
1992 window.ExternalActivate(true, true, window, lastActive);
1994 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1995 if(windowData && windowData.ic)
1996 XSetICFocus(windowData.ic);
1999 lastActive = window;
2008 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
2009 if(window.parent && window != window.parent.activeChild && window != guiApp.interimWindow) break;
2013 //printf("Deactivating %s\n", window._class.name);
2016 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
2018 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
2021 //delete lastActive;
2023 lastActive = window;
2030 case ConfigureNotify:
2032 XConfigureEvent * event = (XConfigureEvent *) thisEvent;
2033 bool unmaximized = false;
2034 if(!window.visible || fullScreenMode) break;
2035 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, (void *)ConfigureNotifyChecker, (void *)window.windowHandle));
2036 //if(event->x - desktopX != window.position.x || event->y - desktopY != window.position.y || event->width != window.size.w || event->height != window.size.h)
2038 if(atomsSupported[_net_wm_state]) //window.nativeDecorations)
2041 unsigned long len, fill;
2044 if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.systemHandle, atoms[_net_wm_state], 0, 32, False,
2045 XA_ATOM, &type, &format, &len, &fill, &data) == Success)
2047 bool maxVert = false, maxHorz = false, isMinimized = false;
2048 Atom * hints = (Atom *)data;
2050 for(c = 0; c < len && hints[c]; c++)
2052 if(hints[c] == atoms[_net_wm_state_maximized_vert])
2054 else if(hints[c] == atoms[_net_wm_state_maximized_horz])
2056 else if(hints[c] == atoms[_net_wm_state_hidden])
2061 if(maxVert && maxHorz)
2063 if(window.state != maximized)
2065 *&window.state = maximized;
2066 if(!window.nativeDecorations)
2067 window.CreateSystemChildren();
2070 else if(isMinimized)
2072 if(window.state != minimized)
2074 *&window.state = minimized;
2075 if(!window.nativeDecorations)
2076 window.CreateSystemChildren();
2079 else if(window.state != normal)
2081 if(window.state == maximized)
2083 *&window.state = normal;
2084 if(!window.nativeDecorations)
2085 window.CreateSystemChildren();
2090 bool offset = false;
2092 if(unmaximized && window.nativeDecorations)
2094 if(window.nativeDecorations && RequestFrameExtents((X11Window)window.windowHandle))
2095 WaitForFrameExtents(window);
2097 // Ensure we set the normal size anchor when un-maximizing
2098 window.ComputeAnchors(window.normalAnchor, window.normalSizeAnchor, &x, &y, &w, &h);
2104 w = event->width, h = event->height;
2106 //if(event->send_event)
2108 X11Window rootChild;
2110 XTranslateCoordinates(xGlobalDisplay, event->window,
2111 RootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 0, 0,
2112 &rootX, &rootY, &rootChild);
2114 if(x != rootX || y != rootY)
2116 /*if(event->send_event)
2126 if(window.nativeDecorations && window.state != maximized)
2128 x -= windowData.decor.left;
2129 y -= windowData.decor.top;
2130 w += windowData.decor.left + windowData.decor.right;
2131 h += windowData.decor.top + windowData.decor.bottom;
2135 window.Position(x, y, w, h, true, true, true, true, false, unmaximized);
2137 // Break the anchors for moveable/resizable windows
2138 // Avoid doing this if the translation wasn't in sync as it will cause the window to move around
2139 if(!unmaximized && !offset && window.style.fixed && window.state == normal)
2141 window.normalAnchor = Anchor
2143 left = x + windowData.decor.left,
2144 top = y + windowData.decor.top
2146 window.normalSizeAnchor =
2147 SizeAnchor { { window.clientSize.w, window.clientSize.h }, isClientW = true, isClientH = true };
2154 XClientMessageEvent * event = (XClientMessageEvent *) thisEvent;
2156 if(event->data.l[0] == atoms[wm_delete_window])
2161 if(event->data.l[0] == atoms[wm_take_focus])
2164 XWindowData windowData;
2169 XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2170 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, timeStamp);
2171 XGrabKeyboard(xGlobalDisplay, (X11Window)window.windowHandle, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2172 guiApp.SetAppFocus(true);
2176 //activeWindow = (X11Window)window.windowHandle;
2178 timeStamp = (X11Time)event->data.l[1];
2180 windowData = window.windowData;
2181 laterFocus = windowData.laterFocus;
2182 windowData.laterFocus = true;
2185 //printf("Processing a ClientMessage WM_TAKE_FOCUS Event for %s (%x)\n", window._class.name, window);
2187 if(guiApp.interimWindow && guiApp.interimWindow.created && window != guiApp.interimWindow) break; //window == window.parent.activeChild) break;
2188 // if(window == window.parent.activeChild) break;
2193 //XFlush(xGlobalDisplay);
2194 while(XCheckTypedEvent(xGlobalDisplay, FocusOut, &checkEvent))
2196 XFocusChangeEvent *event = (XFocusChangeEvent *) &checkEvent;
2198 XFindContext(xGlobalDisplay, event->window, windowContext, (XPointer *) &window);
2199 if(window.parent && window != window.parent.activeChild) break;
2203 //printf("Found a FocusOut ahead, deactivating %s (%d)\n", window._class.name, window);
2206 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
2208 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
2215 lastMouse = acquireStart;
2216 modalRoot = window.FindModal();
2217 windowData = modalRoot ? modalRoot.windowData : window.windowData;
2220 if(laterFocus || (modalRoot ? modalRoot : window).creationActivation == activate)
2224 XRaiseWindow(xGlobalDisplay, (X11Window)modalRoot.windowHandle);
2225 WaitForViewableWindow(modalRoot);
2226 if(atomsSupported[_net_active_window])
2228 XClientMessageEvent event = { 0 };
2229 event.type = ClientMessage;
2230 event.message_type = atoms[_net_active_window];
2231 event.display = xGlobalDisplay;
2233 event.window = (X11Window)modalRoot.windowHandle;
2234 event.send_event = 1;
2236 event.data.l[0] = /*0*/ 1;
2237 event.data.l[1] = timeStamp;
2238 event.data.l[2] = activeWindow;
2240 event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
2243 //printf("(ClientMessage - %s) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, modalRoot._class.name, modalRoot);
2246 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2247 XSetInputFocus(xGlobalDisplay, (X11Window)modalRoot.windowHandle, RevertToParent, timeStamp);
2248 guiApp.SetAppFocus(true);
2249 activeWindow = (X11Window)window.windowHandle;
2251 //XFlush(xGlobalDisplay);
2252 //printf("Done.\n");
2257 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, timeStamp);
2258 guiApp.SetAppFocus(true);
2259 activeWindow = (X11Window)window.windowHandle;
2260 window.ExternalActivate(true, true, window, null); // lastActive);
2261 if(windowData && windowData.ic)
2263 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
2264 //XSetICFocus(windowData.ic);
2268 windowData.laterFocus = true;
2270 lastActive = window;
2277 case PropertyNotify:
2279 XWindowData windowData = window.windowData;
2280 XPropertyEvent * event = (XPropertyEvent *) thisEvent;
2281 if(!fullScreenMode && event->atom == atoms[_net_frame_extents] &&
2282 event->state == PropertyNewValue && windowData)
2284 if(!GetFrameExtents(window, true))
2285 windowData.gotFrameExtents = true; // Unsupported?
2290 if(!processAll) break;
2293 //*if(xGlobalDisplay) XUnlockDisplay(xGlobalDisplay);
2302 gotAnXEvent = false;
2304 xSemaphore.Release();
2309 void Lock(Window window)
2311 //*XLockDisplay(xGlobalDisplay);
2314 void Unlock(Window window)
2316 //*XUnlockDisplay(xGlobalDisplay);
2319 char ** GraphicsDrivers(int * numDrivers)
2321 static char *graphicsDrivers[] = { "X", "OpenGL" };
2322 *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
2323 return (char **)graphicsDrivers;
2326 void GetCurrentMode(bool * fullScreen, int * resolution, int * colorDepth, int * refreshRate)
2328 *fullScreen = fullScreenMode;
2331 void EnsureFullScreen(bool *fullScreen)
2336 bool ScreenMode(bool fullScreen, int resolution, int colorDepth, int refreshRate, bool * textMode)
2340 fullScreenMode = fullScreen;
2348 static bool firstTime = true;
2350 desktopX = desktopY = desktopW = desktopH = 0;
2352 RepositionDesktop(false);
2357 // --- Window Creation ---
2358 void * CreateRootWindow(Window window)
2360 X11Window windowHandle;
2361 XSetWindowAttributes attributes = { 0 };
2362 XVisualInfo * visualInfo = null;
2366 unsigned long mask = EVENT_MASK;
2368 // Old WM (e.g. TWM), use built-in decorations
2369 if(!atomsSupported[_net_wm_state])
2370 window.nativeDecorations = false;
2371 attributes.override_redirect = (window.interim || (!atomsSupported[_net_wm_state] && !window.nativeDecorations)) ? True : False;
2372 attributes.event_mask = EVENT_MASK;
2373 //printf("%s\n", guiApp.defaultDisplayDriver);
2374 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
2375 if(window.dispDriver == class(OpenGLDisplayDriver) || !strcmp(guiApp.defaultDisplayDriver, "OpenGL"))
2379 for(samples = 4;; samples /= 2)
2384 GLX_RENDER_TYPE, GLX_RGBA_BIT,
2385 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
2386 GLX_DOUBLEBUFFER, True,
2392 int numAttribs = 14;
2394 GLXFBConfig *fbconfigs = null, fbconfig;
2397 //printf("Samples = %d, alpha = %d\n", samples, alpha);
2400 attrib[numAttribs++] = GLX_ALPHA_SIZE;
2401 attrib[numAttribs++] = 1;
2405 attrib[numAttribs++] = GLX_SAMPLE_BUFFERS_ARB;
2406 attrib[numAttribs++] = GL_TRUE;
2407 attrib[numAttribs++] = GLX_SAMPLES_ARB;
2408 attrib[numAttribs++] = samples;
2410 attrib[numAttribs] = None;
2412 // visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib);
2414 //printf("Trying %d samples...\n", samples);
2415 fbconfigs = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &numfbconfigs);
2418 for (i = 0; i < numfbconfigs; i++)
2420 XRenderPictFormat * format;
2421 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfigs[i]);
2422 if (!visualInfo) continue;
2423 if(window.alphaBlend)
2425 format = XRenderFindVisualFormat(xGlobalDisplay, visualInfo->visual);
2426 if (!format) { XFree(visualInfo); continue; }
2427 if(format->direct.alphaMask > 0)
2429 //printf("Found what we're looking for (alphaBlend)\n");
2430 fbconfig = fbconfigs[i];
2437 //printf("Found what we're looking for\n");
2442 if (i == numfbconfigs)
2444 fbconfig = fbconfigs[0];
2445 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfig);
2450 if(found || (!samples && !alpha))
2452 //printf("Stopping now\n");
2457 if(samples == 1) samples = 0;
2458 else if(!samples) alpha = false;
2465 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
2467 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
2471 visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrList);
2474 depth = visualInfo ? visualInfo->depth : (window.alphaBlend ? 32 : xSystemDepth);
2475 visual = visualInfo ? visualInfo->visual : (window.alphaBlend ? FindFullColorVisual (xGlobalDisplay, &depth) : xSystemVisual);
2476 // printf("visual: %d, depth: %d\n", visual, depth);
2480 attributes.colormap = XCreateColormap(xGlobalDisplay, XRootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), visual, AllocNone);
2481 attributes.border_pixel = 0;
2488 windowHandle = XCreateWindow(xGlobalDisplay,
2489 DefaultRootWindow(xGlobalDisplay),
2491 XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),
2492 XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),
2493 0, depth, InputOutput, visual ? visual : CopyFromParent,
2494 CWEventMask | (visual ? (CWColormap | CWBorderPixel) : 0) | CWOverrideRedirect,
2498 XSizeHints hints = { 0 };
2499 XSetWMNormalHints(xGlobalDisplay, windowHandle, &hints);
2503 String caption = window.caption;
2504 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_name],
2505 atoms[utf8_string], 8, PropModeReplace, (byte *)window.caption, caption ? strlen(caption) : 0);
2506 XChangeProperty(xGlobalDisplay, windowHandle, atoms[wm_name],
2507 atoms[utf8_string], 8, PropModeReplace, (byte *)window.caption, caption ? strlen(caption) : 0);
2510 SetNETWMState((X11Window)windowHandle, false, add, atoms[_net_wm_state_fullscreen], 0);
2511 //SetNETWMState((X11Window)windowHandle, false, add, atoms[_net_wm_state_above], 0);
2516 hints[0] = atoms[_net_wm_window_type_normal];
2518 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
2519 PropModeReplace, (unsigned char*)&hints, count);
2524 xwmHints.flags = InputHint;
2526 XSetWMHints(xGlobalDisplay, windowHandle, &xwmHints);
2529 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
2530 XSetWMProtocols(xGlobalDisplay, windowHandle, protocols, 2);
2534 Unsupported for now...
2535 else if(window.systemParent)
2542 if(window.windowHandle)
2543 windowHandle = (X11Window)window.windowHandle;
2546 X11Window parentWindow = (X11Window)null;
2547 int x = window.position.x + desktopX, y = window.position.y + desktopY;
2548 int w = window.state == normal ? Max(1, window.size.w) : Max(1, window.normalSizeAnchor.size.w);
2549 int h = window.state == normal ? Max(1, window.size.h) : Max(1, window.normalSizeAnchor.size.h);
2550 MinMaxValue smw = 0, smh = 0;
2551 MinMaxValue minW = window.minSize.w, minH = window.minSize.h;
2552 window.OnResizing((int *)&minW, (int *)&minH);
2554 // To fix jumping message boxes on Cinnamon:
2555 if(window.state == normal && (minW > window.minSize.w || minH > window.minSize.w))
2556 window.ComputeAnchors(window.normalAnchor, window.normalSizeAnchor, &x, &y, &w, &h);
2558 window.SetWindowMinimum(&smw, &smh);
2559 minW = Max(minW, smw);
2560 minH = Max(minH, smh);
2562 if(!window.nativeDecorations && window.state != normal)
2564 w += window.size.w - window.clientSize.w;
2565 h += window.size.h - window.clientSize.h;
2568 if(window.master.rootWindow && window.master.rootWindow != guiApp.desktop && (window._isModal || window.style.interim))
2570 Window master = window.master;
2571 Window rootWindow = master.rootWindow;
2573 parentWindow = rootWindow.is3D ? (X11Window)rootWindow.parent.windowHandle : (X11Window)rootWindow.windowHandle;
2575 // parentWindow = window.master.rootWindow.is3D ? window.master.rootWindow.parent.windowHandle : window.master.rootWindow.windowHandle;
2577 if(window.style.showInTaskBar)
2578 parentWindow = (X11Window)null;
2580 windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
2582 0, depth, InputOutput, visual ? visual : CopyFromParent,
2583 CWEventMask | CWOverrideRedirect | (visual ? (CWColormap | CWBorderPixel) : 0), &attributes);
2585 if(parentWindow && (window.interim || window.isModal))
2587 //printf("Setting WM_TRANSIENT_FOR of %s to %s\n", window._class.name, window.master.rootWindow._class.name);
2588 XSetTransientForHint(xGlobalDisplay, windowHandle, parentWindow);
2589 //XFlush(xGlobalDisplay);
2590 //printf("Done.\n");
2591 //XChangeProperty(xGlobalDisplay, windowHandle, atoms[wm_transient_for], XA_WINDOW, 32, PropModeReplace, (unsigned char*)&parentWindow, 1);
2593 SetNETWMState(windowHandle, false, add, atoms[_net_wm_state_modal], 0);
2599 if(parentWindow && window.interim)
2601 hints[0] = atoms[_net_wm_window_type_dropdown_menu];
2602 hints[1] = atoms[_net_wm_window_type_popup_menu];
2603 hints[2] = atoms[_net_wm_window_type_menu];
2606 else if(parentWindow)
2608 hints[0] = atoms[_net_wm_window_type_normal];
2609 SetNETWMState(windowHandle, false, add, atoms[_net_wm_state_skip_taskbar], 0);
2611 // Some WMs won't show a close button if dialog is set
2612 // Additionally, this casues jumping of all dialog windows on Cinnamon
2613 //hints[0] = atoms[_net_wm_window_type_dialog];
2618 hints[0] = atoms[_net_wm_window_type_normal];
2621 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
2622 PropModeReplace, (unsigned char*)&hints, count);
2626 xwmHints.flags = InputHint;
2628 XSetWMHints(xGlobalDisplay, windowHandle, &xwmHints);
2631 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
2632 XSetWMProtocols(xGlobalDisplay, windowHandle, protocols, 2);
2635 // Set Normal hints for minimum/maximum size
2637 XSizeHints hints = { 0 };
2638 hints.min_width = minW;
2639 hints.min_height = minH;
2640 hints.flags |= PMinSize;
2642 if(window.maxSize.w < MAXINT || window.minSize.h < MAXINT)
2644 hints.max_width = window.maxSize.w;
2645 hints.max_height = window.maxSize.h;
2646 hints.flags |= PMaxSize;
2650 hints.flags |= PPosition;
2654 hints.flags |= PSize;
2656 XSetWMNormalHints(xGlobalDisplay, windowHandle, &hints);
2664 char fontString[1024] = "--helvetica-*-r-*-*-*-120-*-*-*-*-*-*,-misc-fixed-*-r-*-*-*-130-*-*-*-*-*-*";
2665 XPoint cursor_location = { 0, 0 };
2666 char **missing_charsets;
2667 int num_missing_charsets = 0;
2668 char *default_string;
2670 XRectangle area = { 0, 0, 400, 400 };
2671 XVaNestedList argList;
2673 // sprintf(fontString, "-*-%s-*-r-*-*-*-%d-*-*-*-*-*-*", "Helvetica" /*window.font.faceName*/, (int)(window.font.size * 20));
2674 fontset = XCreateFontSet(xGlobalDisplay, fontString, &missing_charsets, &num_missing_charsets, &default_string);
2675 argList = XVaCreateNestedList(0,
2676 XNSpotLocation, &cursor_location,
2678 XNFontSet, fontset,/*
2680 WhitePixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),
2682 BlackPixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),*/
2684 ic = XCreateIC(im, XNInputStyle,
2685 XIMStatusNothing | XIMPreeditPosition,
2686 XNPreeditAttributes, argList,
2687 XNClientWindow, windowHandle, XNFocusWindow, windowHandle, NULL);
2689 // Should we free the fontset or not?
2690 // XFreeFontSet(xGlobalDisplay, fontset);
2692 setICPosition = true;
2694 ic = XCreateIC(im, XNInputStyle, XIMStatusNothing | XIMPreeditNothing, XNClientWindow, windowHandle, XNFocusWindow, windowHandle, null);
2698 XGetICValues(ic, XNFilterEvents, &mask, NULL);
2702 XSelectInput(xGlobalDisplay, windowHandle, mask);
2704 if(capturedWindow == None && !restrictedWindow)
2706 XGrabPointer(xGlobalDisplay, (X11Window)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2707 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2708 XUngrabPointer(xGlobalDisplay, CurrentTime);
2713 if ( atoms[_motif_wm_hints] != None )
2717 (window.nativeDecorations ? 0 : MWM_HINTS_DECORATIONS)|MWM_HINTS_FUNCTIONS,
2718 (window.hasClose ? MWM_FUNC_CLOSE : 0) |
2719 (fullScreenMode || window.hasMaximize ? MWM_FUNC_MAXIMIZE : 0) |
2720 (fullScreenMode || window.hasMinimize ? MWM_FUNC_MINIMIZE : 0) |
2721 ((fullScreenMode || window.moveable || ((BorderBits)window.borderStyle).fixed) ? MWM_FUNC_MOVE : 0) |
2722 (fullScreenMode || ((BorderBits)window.borderStyle).sizable ? MWM_FUNC_RESIZE : 0),
2725 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
2726 PropModeReplace, (unsigned char*)&hints, 5);
2729 // *** We set this for ourselves, so don't check atomsSupported !!! ***
2730 if(atoms[_net_wm_pid])
2733 // printf("Setting _NET_WM_PID to %d\n", pid);
2734 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_pid], XA_CARDINAL, 32,
2735 PropModeReplace, (unsigned char*)&pid, 1);
2740 XWMHints wmHints = { 0 };
2741 wmHints.input = True;
2742 XSetWMHints(xGlobalDisplay, windowHandle, &wmHints);
2745 // XFlush(xGlobalDisplay);
2746 window.windowData = XWindowData { visualInfo, ic };
2748 XSaveContext(xGlobalDisplay, windowHandle, windowContext, (XPointer)window);
2750 XSelectInput(xGlobalDisplay, windowHandle, mask);
2752 if(capturedWindow == None && !restrictedWindow)
2754 XGrabPointer(xGlobalDisplay, (X11Window)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2755 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2756 XUngrabPointer(xGlobalDisplay, CurrentTime);
2759 if(!fullScreenMode && !window.nativeDecorations || !RequestFrameExtents(windowHandle))
2760 ((XWindowData)window.windowData).gotFrameExtents = true;
2763 XMapWindow(xGlobalDisplay, windowHandle);
2764 XGrabKeyboard(xGlobalDisplay, windowHandle, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2766 return (void *)windowHandle;
2769 void DestroyRootWindow(Window window)
2772 if(window == acquiredInputWindow)
2773 delete acquiredInputWindow;
2775 XDeleteContext(xGlobalDisplay, (XID)window, windowContext);
2776 XSaveContext(xGlobalDisplay, (X11Window)window.windowHandle, windowContext, null);
2777 XDestroyWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2778 XSync(xGlobalDisplay, 0);
2779 while(XCheckWindowEvent(xGlobalDisplay, (X11Window)window.windowHandle, 0xFFFFFFFF, &event));
2780 window.windowHandle = null;
2781 if(window.windowData)
2783 XWindowData windowData = window.windowData;
2784 XFree(windowData.visual);
2785 if(windowData && windowData.ic)
2786 XDestroyIC(windowData.ic);
2788 // printf("Setting windowData for %s to null\n", window._class.name);
2789 window.windowData = null;
2791 if(lastActive == window)
2795 // -- Window manipulation ---
2797 void SetRootWindowCaption(Window window, char * name)
2799 if(window.windowHandle)
2801 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_name],
2802 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2803 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[wm_name],
2804 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2808 void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
2810 //Logf("Position root window %s\n", window.name);
2811 if(window.windowHandle && (!window.parent || !window.parent.display))
2813 bool visible = window.visible;
2814 if(window.visible && window.created)
2815 XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2816 if(window.state == minimized && atomsSupported[_net_wm_state]) return;
2818 if(window.nativeDecorations)
2820 XWindowData windowData = window.windowData;
2822 // Was commenting this out was part of #700/#795 fix, but this causes jumping of e.g. About box after getting frame extents PropertyNotify
2824 // && window.state != maximized -- required for Cinnamon on Mint 14/15
2825 if(!windowData.gotFrameExtents && window.state != maximized)
2827 if(WaitForFrameExtents(window))
2829 x += windowData.decor.left;
2830 y += windowData.decor.top ;
2832 w += windowData.decor.left + windowData.decor.right;
2833 h += windowData.decor.top + windowData.decor.bottom;
2837 x -= windowData.decor.left;
2838 y -= windowData.decor.top;
2840 w -= windowData.decor.left + windowData.decor.right;
2841 h -= windowData.decor.top + windowData.decor.bottom;
2843 // Tweak for first unmaximize on Unity on Ubuntu 11.10
2844 /*if(window.state == maximized && (desktopX + w > desktopW || desktopY + h > desktopH))
2854 if(!fullScreenMode && (!atomsSupported[_net_wm_state] || window.state != maximized))
2857 XMoveResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, x, y, w, h);
2859 XMoveWindow(xGlobalDisplay, (X11Window)window.windowHandle, x, y);
2861 XResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, w, h);
2863 // Reset min/max for fixed size windows on WMs not looking at MWM_FUNC_RESIZE (e.g. Cinnamon)
2864 if(window.style.fixed && !window.style.sizable)
2866 XSizeHints hints = { 0 };
2868 XGetWMNormalHints(xGlobalDisplay, (X11Window)window.windowHandle, &hints, &supplied);
2869 hints.min_width = hints.max_width = w;
2870 hints.min_height = hints.max_height = h;
2871 hints.flags |= PMinSize|PMaxSize;
2872 XSetWMNormalHints(xGlobalDisplay, (X11Window)window.windowHandle, &hints);
2878 void OrderRootWindow(Window window, bool topMost)
2883 void SetRootWindowColor(Window window)
2888 void OffsetWindow(Window window, int * x, int * y)
2893 void UpdateRootWindow(Window window)
2895 if(!window.parent || !window.parent.display)
2899 Box box = window.box;
2900 box.left -= window.clientStart.x;
2901 box.top -= window.clientStart.y;
2902 box.right -= window.clientStart.x;
2903 box.bottom -= window.clientStart.y;
2904 // Logf("Update root window %s\n", window.name);
2905 window.Update(null);
2906 box.left += window.clientStart.x;
2907 box.top += window.clientStart.y;
2908 box.right += window.clientStart.x;
2909 box.bottom += window.clientStart.y;
2910 window.UpdateDirty(box);
2915 void SetRootWindowState(Window window, WindowState state, bool visible)
2917 // Old WM (e.g. TWM), use built-in decorations
2918 if(!atomsSupported[_net_wm_state])
2919 window.nativeDecorations = false;
2920 if(!window.parent || !window.parent.display)
2922 XWindowData windowData = window.windowData;
2923 //Logf("Set root window state %d %s\n", state, window.name);
2926 if(!windowData.currentlyVisible)
2928 XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2929 windowData.currentlyVisible = true;
2930 WaitForViewableWindow(window);
2931 if(window.creationActivation == activate && state != minimized)
2932 ActivateRootWindow(window);
2935 if(fullScreenMode && state != minimized)
2937 int w = XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
2938 int h = XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
2939 SetNETWMState((X11Window)window.windowHandle, true, add, atoms[_net_wm_state_fullscreen], 0);
2940 XMoveResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, 0, 0, w, h);
2942 guiApp.SetDesktopPosition(0, 0, w, h, true);
2943 window.Position(0, 0, w, h, true, true, true, true, false, false);
2946 if(state == minimized && atomsSupported[_net_wm_state])
2948 uint iconic = IconicState;
2950 // SetNETWMState(window.windowHandle, true, add, atoms[_net_wm_state_hidden], null);
2952 XChangeProperty(xGlobalDisplay, window.windowHandle, atoms[wm_state], XA_CARDINAL, 32,
2953 PropModeReplace, &iconic, 1);
2957 XClientMessageEvent event = { 0 };
2958 event.type = ClientMessage;
2959 event.message_type = atoms[wm_state];
2960 event.display = xGlobalDisplay;
2961 event.window = window.windowHandle;
2962 event.send_event = 1;
2964 event.data.l[0] = IconicState;
2965 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, &event);
2968 // printf("Attempting to minimize %s\n", window._class.name);
2970 XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
2972 else if(!fullScreenMode)
2974 //((XWindowData)window.windowData).gotFrameExtents && (!window.nativeDecorations || window.state == state))
2975 if(!atomsSupported[_net_wm_state] || (!((XWindowData)window.windowData).gotFrameExtents && window.state == maximized))
2977 // Running this block avoids the initial IDE maximized->unmaximized flicker
2978 //if(window.state != maximized || !atomsSupported[_net_wm_state] || window.nativeDecorations)
2980 int x = window.position.x;
2981 int y = window.position.y;
2982 int w = window.size.w;
2983 int h = window.size.h;
2985 if(window.nativeDecorations)
2987 XWindowData windowData = window.windowData;
2988 x -= windowData.decor.left;
2989 y -= windowData.decor.top;
2991 w -= windowData.decor.left + windowData.decor.right;
2992 h -= windowData.decor.top + windowData.decor.bottom;
2997 XMoveResizeWindow(xGlobalDisplay,
2998 (X11Window)window.windowHandle,
3001 UpdateRootWindow(window);
3003 if(atomsSupported[_net_wm_state])
3005 // Maximize / Restore the window
3006 SetNETWMState((X11Window)window.windowHandle, true, state == maximized ? add : remove,
3007 atoms[_net_wm_state_maximized_vert], atoms[_net_wm_state_maximized_horz]);
3008 if(state == maximized)
3010 // Prevent the code in ConfigureNotify to think the window has been unmaximized
3011 // if the Window Manager hasn't set the hints yet.
3012 XFlush(xGlobalDisplay);
3020 XUnmapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
3021 windowData.currentlyVisible = false;
3023 //XFlush(xGlobalDisplay);
3027 void FlashRootWindow(Window window)
3029 // printf("Attempting to flash root window\n");
3030 SetNETWMState((X11Window)window.windowHandle, true, add, atoms[_net_wm_state_demands_attention], 0);
3033 void ActivateRootWindow(Window window)
3035 if(!window.parent || !window.parent.display)
3037 if(!window.style.hidden && window.created)
3039 XWindowData windowData = window.windowData;
3040 //printf("Activate root window %s\n", window._class.name);
3041 if(!windowData.currentlyVisible)
3043 XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
3044 WaitForViewableWindow(window);
3045 windowData.currentlyVisible = true;
3047 XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
3048 if(atomsSupported[_net_active_window])
3050 XClientMessageEvent event = { 0 };
3051 event.type = ClientMessage;
3052 event.message_type = atoms[_net_active_window];
3053 event.display = xGlobalDisplay;
3055 event.window = (X11Window)window.windowHandle;
3056 event.send_event = 1;
3058 event.data.l[0] = /*0*/ 1;
3059 event.data.l[1] = timeStamp;
3060 event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
3063 //printf("(ActivateRootWindow) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, window);
3066 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
3067 //#if defined(__APPLE__)
3068 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, CurrentTime);
3072 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, CurrentTime);
3077 // --- Mouse-based window movement ---
3079 void StartMoving(Window window, int x, int y, bool fromKeyBoard)
3084 void StopMoving(Window window)
3089 // -- Mouse manipulation ---
3091 void GetMousePosition(int *x, int *y)
3093 X11Window rootWindow, childWindow;
3096 ((GuiApplication)__thisModule.application).Lock();
3097 //XLockDisplay(xGlobalDisplay);
3098 XQueryPointer(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &childWindow,
3099 &rootWindow, x, y, &mx, &my, &state);
3100 //XUnlockDisplay(xGlobalDisplay);
3101 ((GuiApplication)__thisModule.application).Unlock();
3104 void SetMousePosition(int x, int y)
3106 XWarpPointer(xGlobalDisplay, None, DefaultRootWindow(xGlobalDisplay), 0, 0, 0, 0, x, y);
3107 XFlush(xGlobalDisplay);
3110 void SetMouseRange(Window window, Box box)
3112 ((GuiApplication)__thisModule.application).Lock();
3113 //XLockDisplay(xGlobalDisplay);
3114 if(box && box.left > 0 && box.top > 0 &&
3115 box.right < guiApp.desktop.clientSize.w - 1 && box.bottom < guiApp.desktop.clientSize.h - 1)
3117 if(!window.parent || !window.parent.display)
3119 XMoveResizeWindow(xGlobalDisplay, confineWindow, box.left /*+ desktopX*/, box.top /*+ desktopY*/,
3120 box.right - box.left + 1, box.bottom - box.top + 1);
3122 if(!restrictedWindow)
3123 XMapWindow(xGlobalDisplay, confineWindow);
3125 XGrabPointer(xGlobalDisplay, (X11Window) window.rootWindow.windowHandle, False,
3126 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
3127 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
3129 restrictedWindow = window;
3132 else if(restrictedWindow)
3134 if(capturedWindow != None)
3136 XGrabPointer(xGlobalDisplay, (X11Window)capturedWindow,
3137 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
3138 GrabModeAsync, None, fullScreenMode ? nullCursor : None, CurrentTime);
3141 XUngrabPointer(xGlobalDisplay, CurrentTime);
3143 if(restrictedWindow)
3144 XUnmapWindow(xGlobalDisplay, confineWindow);
3146 restrictedWindow = null;
3148 //XUnlockDisplay(xGlobalDisplay);
3149 ((GuiApplication)__thisModule.application).Unlock();
3152 void SetMouseCapture(Window window)
3154 //*XLockDisplay(xGlobalDisplay);
3157 if(!window.parent || !window.parent.display)
3159 XGrabPointer(xGlobalDisplay, (X11Window)window.windowHandle,
3160 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
3161 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
3163 capturedWindow = (X11Window) window.windowHandle;
3166 else if(capturedWindow != None)
3168 if(restrictedWindow)
3169 XGrabPointer(xGlobalDisplay, (X11Window) restrictedWindow.rootWindow.windowHandle, False,
3170 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
3171 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
3173 XUngrabPointer(xGlobalDisplay, CurrentTime);
3174 capturedWindow = None;
3176 //*XUnlockDisplay(xGlobalDisplay);
3179 // -- Mouse cursor ---
3181 void SetMouseCursor(Window window, int cursor)
3183 if(window.rootWindow.windowHandle)
3184 XDefineCursor(xGlobalDisplay, (X11Window) window.rootWindow.windowHandle,
3185 (acquiredInputWindow || cursor == -1) ? nullCursor : systemCursors[(SystemCursor)cursor]);
3190 void SetCaret(int x, int y, int size)
3192 Window caretOwner = guiApp.caretOwner;
3193 Window window = caretOwner ? caretOwner.rootWindow : null;
3194 if(window && window.windowData && setICPosition)
3196 XWindowData windowData = window.windowData;
3197 if(windowData && windowData.ic)
3199 XPoint cursor_location =
3201 (short)(caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x),
3202 (short)(caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y)
3204 XVaNestedList argList = XVaCreateNestedList(0, XNSpotLocation, &cursor_location, NULL);
3205 XSetICValues(windowData.ic, XNPreeditAttributes, argList, 0);
3210 void ClearClipboard()
3212 //*XLockDisplay(xGlobalDisplay);
3215 delete clipBoardData;
3216 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard], None, CurrentTime);
3218 //*XUnlockDisplay(xGlobalDisplay);
3221 bool AllocateClipboard(ClipBoard clipBoard, uint size)
3223 bool result = false;
3224 if((clipBoard.text = new0 byte[size]))
3229 bool SaveClipboard(ClipBoard clipBoard)
3231 bool result = false;
3232 //*XLockDisplay(xGlobalDisplay);
3235 Window rootWindow = guiApp.desktop;
3238 for(rootWindow = rootWindow.children.first;
3239 rootWindow && !rootWindow.windowHandle;
3240 rootWindow = rootWindow.next);
3243 delete clipBoardData;
3245 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard],
3246 (X11Window) rootWindow.windowHandle, CurrentTime);
3247 clipBoardData = clipBoard.text;
3248 clipBoard.text = null;
3251 //*XUnlockDisplay(xGlobalDisplay);
3255 bool LoadClipboard(ClipBoard clipBoard)
3257 bool result = false;
3259 //*XLockDisplay(xGlobalDisplay);
3260 // The data is inside this client...
3263 clipBoard.text = new char[strlen(clipBoardData)+1];
3264 strcpy(clipBoard.text, clipBoardData);
3267 // The data is with another client...
3270 Window rootWindow = guiApp.desktop;
3273 for(rootWindow = rootWindow.children.first;
3274 rootWindow && !rootWindow.windowHandle;
3275 rootWindow = rootWindow.next);
3279 Atom selAtom = atoms[clipboard];
3280 X11Window owner = XGetSelectionOwner(xGlobalDisplay, selAtom);
3284 for(atom = atoms[utf8_string]; atom; atom = ((atom == atoms[utf8_string]) ? XA_STRING : 0))
3287 XConvertSelection(xGlobalDisplay, selAtom, atom, atoms[app_selection] /*None*/, (X11Window) rootWindow.windowHandle, CurrentTime);
3288 XIfEvent(xGlobalDisplay, (XEvent *) &e, EventChecker, (void *)SelectionNotify);
3289 if(e.type == SelectionNotify)
3291 XSelectionEvent * selection = (XSelectionEvent *) &e;
3292 //printf("Got a SelectionNotify with %d (%s)\n", selection->_property, XGetAtomName(xGlobalDisplay, selection->_property));
3294 unsigned long len, size = 0, dummy;
3297 XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, 0, False, AnyPropertyType, &type, &format, &len, &size, &data);
3305 if(XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, size, False,
3306 AnyPropertyType, &type,&format,&len, &dummy, &data) == Success)
3308 clipBoard.text = new char[size+1];
3309 strncpy(clipBoard.text, data, size);
3310 clipBoard.text[size] = '\0';
3321 //*XUnlockDisplay(xGlobalDisplay);
3325 void UnloadClipboard(ClipBoard clipBoard)
3327 delete clipBoard.text;
3330 // --- State based input ---
3332 bool AcquireInput(Window window, bool state)
3336 GetMousePosition(&acquireStart.x, &acquireStart.y);
3337 lastMouse = acquireStart;
3338 acquiredInputWindow = window;
3339 incref acquiredInputWindow;
3341 else if(acquiredInputWindow)
3342 delete acquiredInputWindow;
3346 bool GetMouseState(MouseButtons * buttons, int * x, int * y)
3348 bool result = false;
3349 if(acquiredInputWindow && guiApp.desktop.active)
3351 if(x) *x = lastMouse.x - acquireStart.x;
3352 if(y) *y = lastMouse.y - acquireStart.y;
3353 *buttons = buttonsState;
3354 SetMousePosition(acquireStart.x, acquireStart.y);
3355 lastMouse = acquireStart;
3362 if(buttons) *buttons = 0;
3367 bool GetJoystickState(int device, Joystick joystick)
3369 bool result = false;
3370 #if defined(__linux__)
3371 if(joystick && device < 4)
3373 struct JS_DATA_TYPE js = { 0 };
3374 memset(joystick, 0, sizeof(Joystick));
3375 if(joystickFD[device] && read(joystickFD[device], &js, JS_RETURN) == JS_RETURN)
3377 joystick.buttons = js.buttons;
3378 joystick.x = js.x - 128;
3379 joystick.y = js.y - 128;
3387 bool GetKeyState(Key key)
3389 if(key == capsState || key == numState || key == scrollState)
3396 case capsState: atom = atoms[capsLock]; idx = 0; break;
3397 case numState: atom = atoms[numLock]; idx = 1; break;
3398 case scrollState: atom = atoms[scrollLock]; idx = 2; break;
3400 /*XkbGetIndicatorState(xGlobalDisplay, XkbUseCoreKbd, &state);
3401 state = (state & (1 << idx)) ? 1 : 0;*/
3402 XkbGetNamedIndicator(xGlobalDisplay, /*XkbUseCoreKbd,*/ atom, &idx, &state, NULL, NULL);
3406 return keyStates[key.code];
3409 void SetTimerResolution(uint hertz)
3411 timerDelay = hertz ? (1000000 / hertz) : MAXINT;
3416 hiResTimer.delay = 1000000 / hertz;
3417 hiResTimer.Create();
3422 bool SetIcon(Window window, BitmapResource resource)
3427 if(bitmap.Load(resource.fileName, null, null))
3429 unsigned long * icon = new unsigned long[2 + bitmap.width * bitmap.height];
3430 bitmap.Convert(null, pixelFormat888, null);
3431 icon[0] = bitmap.width;
3432 icon[1] = bitmap.height;
3433 if(sizeof(long) != sizeof(uint32))
3436 for(c = 0; c < bitmap.width * bitmap.height; c++)
3437 icon[c+2] = ((uint32 *)bitmap.picture)[c];
3440 memcpy(icon + 2, bitmap.picture, bitmap.width * bitmap.height * sizeof(uint32));
3441 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_icon],
3442 XA_CARDINAL,32,PropModeReplace, (byte *)icon, 2+bitmap.width*bitmap.height);
3451 default dllexport void * __attribute__((stdcall)) IS_XGetDisplay()
3453 return xGlobalDisplay;
3456 default dllexport void * __attribute__((stdcall)) IS_XGetWindow(Window window)
3458 return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;