1 namespace gui::drivers;
4 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
5 import "OpenGLDisplayDriver"
8 #if (defined(__unix__) || defined(__APPLE__)) && !defined(ECERE_MINIGLX)
13 #define property _property
18 #include <linux/joystick.h>
20 #include <sys/param.h>
23 #include <sys/select.h>
29 #define Window X11Window
30 #define Cursor X11Cursor
32 #define Display X11Display
34 #define KeyCode X11KeyCode
35 #define Picture X11Picture
37 #include <X11/Xatom.h>
39 #include <X11/Xresource.h>
40 #include <X11/Xutil.h>
41 #include <X11/XKBlib.h>
42 #include <X11/keysym.h>
43 #include <X11/cursorfont.h>
45 #if !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
48 #include <X11/extensions/Xrender.h>
49 #include <X11/extensions/XShm.h>
66 import "XDisplayDriver"
68 // These are the events we care about
69 #define EVENT_MASK StructureNotifyMask | \
70 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | \
71 KeyPressMask | KeyReleaseMask | ExposureMask | FocusChangeMask | \
74 void * xGlobalDisplay;
77 static XContext windowContext;
78 static Window lastActive;
80 static char * clipBoardData;
82 static Thread /* xThread, */ timerThread;
83 static bool xTerminate;
84 static Semaphore xSemaphore { };
85 static Mutex xMutex { };
86 static bool fullScreenMode;
87 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
88 X11Window confineWindow;
89 static X11Cursor nullCursor;
90 static X11Window capturedWindow = None;
91 static Window restrictedWindow = null;
92 static bool gotAnXEvent = false;
94 static int joystickFD[4];
95 static X11Window activeWindow;
96 static X11Cursor systemCursors[SystemCursor];
98 static enum NETWMStateAction { remove = 0, add = 1, toggle = 2 };
100 static enum AtomIdents
102 clipboard, multiple, targets, utf8_string, wm_delete_window, wm_hints, wm_name, wm_protocols, wm_state, wm_take_focus, wm_transient_for,
104 _net_active_window, _net_current_desktop, _net_number_of_desktops, _net_wm_icon, _net_wm_name, _net_wm_pid,
105 _net_wm_state, _net_wm_state_demands_attention, _net_wm_state_hidden, _net_wm_user_time, _net_wm_window_type,
106 _net_wm_window_type_desktop, _net_wm_window_type_dialog, _net_wm_window_type_dock, _net_wm_window_type_dropdown_menu,
107 _net_wm_window_type_menu, _net_wm_window_type_normal, _net_wm_window_type_popup_menu, _net_wm_window_type_splash,
108 _net_wm_window_type_toolbar, _net_wm_window_type_utility, _net_workarea, _net_frame_extents, _net_request_frame_extents,
109 _net_wm_state_maximized_vert, _net_wm_state_maximized_horz, _net_wm_state_modal, app_selection, _net_supported
112 static Atom atoms[AtomIdents];
113 static bool atomsSupported[AtomIdents];
115 static const char *atomNames[AtomIdents] = {
116 "CLIPBOARD", //clipboard
117 "MULTIPLE", //multiple
119 "UTF8_STRING", //utf8_string
120 "WM_DELETE_WINDOW", //wm_delete_window
121 "WM_HINTS", //wm_hints
123 "WM_PROTOCOLS", //wm_protocols
124 "WM_STATE", //wm_state
125 "WM_TAKE_FOCUS", //wm_take_focus
126 "WM_TRANSIENT_FOR", //wm_transient_for
127 "_MOTIF_WM_HINTS", //_motif_wm_hints
128 "_NET_ACTIVE_WINDOW", //_net_active_window
129 "_NET_CURRENT_DESKTOP", //_net_current_desktop
130 "_NET_NUMBER_OF_DESKTOPS", //_net_number_of_desktops
131 "_NET_WM_ICON", //_net_wm_icon
132 "_NET_WM_NAME", //_net_wm_name
133 "_NET_WM_PID", //_net_wm_pid
134 "_NET_WM_STATE", //_net_wm_state
135 "_NET_WM_STATE_DEMANDS_ATTENTION", //_net_wm_state_demands_attention
136 "_NET_WM_STATE_HIDDEN", //_net_wm_state_hidden
137 "_NET_WM_USER_TIME", //_net_wm_user_time
138 "_NET_WM_WINDOW_TYPE", //_net_wm_window_type
139 "_NET_WM_WINDOW_TYPE_DESKTOP", //_net_wm_window_type_desktop
140 "_NET_WM_WINDOW_TYPE_DIALOG", //_net_wm_window_type_dialog
141 "_NET_WM_WINDOW_TYPE_DOCK", //_net_wm_window_type_dock
142 "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", //_net_wm_window_type_dropdown_menu
143 "_NET_WM_WINDOW_TYPE_MENU", //_net_wm_window_type_menu
144 "_NET_WM_WINDOW_TYPE_NORMAL", //_net_wm_window_type_normal
145 "_NET_WM_WINDOW_TYPE_POPUP_MENU", //_net_wm_window_type_popup_menu
146 "_NET_WM_WINDOW_TYPE_SPLASH", //_net_wm_window_type_splash
147 "_NET_WM_WINDOW_TYPE_TOOLBAR", //_net_wm_window_type_toolbar
148 "_NET_WM_WINDOW_TYPE_UTILITY", //_net_wm_window_type_utility
149 "_NET_WORKAREA", //_net_workarea
150 "_NET_FRAME_EXTENTS", // _net_frame_extents
151 "_NET_REQUEST_FRAME_EXTENTS", // _net_request_frame_extents
152 "_NET_WM_STATE_MAXIMIZED_VERT", // _net_wm_state_maximized_vert
153 "_NET_WM_STATE_MAXIMIZED_HORZ", // _net_wm_state_maximized_horz
154 "_NET_WM_STATE_MODAL", // _net_wm_state_modal
159 _NET_WM_STATE_STICKY, ATOM
160 _NET_WM_STATE_MAXIMIZED_VERT, ATOM
161 _NET_WM_STATE_MAXIMIZED_HORZ, ATOM
162 _NET_WM_STATE_SHADED, ATOM
163 _NET_WM_STATE_SKIP_TASKBAR, ATOM
164 _NET_WM_STATE_SKIP_PAGER, ATOM
165 _NET_WM_STATE_HIDDEN, ATOM
166 _NET_WM_STATE_FULLSCREEN, ATOM
167 _NET_WM_STATE_ABOVE, ATOM
168 _NET_WM_STATE_BELOW, ATOM
169 _NET_WM_STATE_DEMANDS_ATTENTION, ATOM
172 static bool autoRepeatDetectable;
173 static bool setICPosition;
175 PixelFormat xSystemPixelFormat;
176 Visual * xSystemVisual;
179 static void SetNETWMState(X11Window windowHandle, bool throughRoot, NETWMStateAction action, Atom atom1, Atom atom2)
181 if(atomsSupported[_net_wm_state])
184 unsigned long count, fill;
187 uint state = WithdrawnState;
189 /*if(XGetWindowProperty(xGlobalDisplay, windowHandle, atoms[wm_state], 0, 3, False,
190 atoms[wm_state], &type, &format, &count, &fill, &data) == Success && count)
192 state = *(uint *)data;
195 if(!throughRoot) //state == WithdrawnState)
197 // We need to handle modifying these ourselves for withdrawn windows...
200 Atom values[2] = { atom1, atom2 };
201 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_state], XA_ATOM,
202 32, PropModeAppend, (byte *)values, atom2 ? 2 : 1);
204 else if(XGetWindowProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_state], 0, 32, False,
205 XA_ATOM, &type, &format, &count, &fill, &data) == Success)
207 Atom * values = (Atom *) data;
209 for (i = 0; i < count; i++)
211 if(values[i] == atom1 || (atom2 && values[i] == atom2))
214 memmove(values + i, values + i + 1, sizeof(Atom) * (count - i - 1));
219 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_state], XA_ATOM, 32, PropModeReplace, (byte *)values, (int)count);
225 XClientMessageEvent event = { 0 };
226 event.type = ClientMessage;
227 event.message_type = atoms[_net_wm_state];
228 event.display = xGlobalDisplay;
230 event.window = windowHandle;
231 event.send_event = 1;
233 event.data.l[0] = action;
234 event.data.l[1] = atom1;
235 event.data.l[2] = atom2;
236 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false,
237 SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
243 static X11Time timeStamp;
245 class XWindowData : struct
248 XVisualInfo * visual;
253 bool gotFrameExtents;
256 bool XGetBorderWidths(Window window, Box box)
258 XWindowData windowData = window.windowData;
261 if(window.state == maximized)
262 box = { 0, 0, 0, 0 };
264 box = windowData.decor;
270 static Visual * FindFullColorVisual(X11Display *dpy, int * depth)
273 XVisualInfo *vinfo_ret;
276 vinfo._class = TrueColor;
277 vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
283 if(vinfo_ret[numitems-1].depth > maxdepth)
284 maxdepth = vinfo_ret[numitems-1].depth;
287 XFree((void *) vinfo_ret);
290 if(XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, TrueColor, &vinfo))
300 static void RepositionDesktop(bool updateChildren)
304 Screen * x_screen = XDefaultScreenOfDisplay(xGlobalDisplay);
309 unsigned long len, fill;
311 static double lastTime = 0, time;
314 if(desktopW && desktopH && time - lastTime < 1.5) return;
317 w = XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
318 h = XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
319 x_root = XRootWindowOfScreen(x_screen);
321 if(atomsSupported[_net_number_of_desktops])
323 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_number_of_desktops], 0, 1, False,
324 XA_CARDINAL, &type, &format, &len, &fill,
327 printf("cant get xa desktops property\n");
333 desktops = (int)*(long *)data;
335 //printf("_NET_NUMBER_OF_DESKTOPS is %d\n", desktops);
342 if(atomsSupported[_net_current_desktop])
344 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_current_desktop], 0, 1, False,
345 XA_CARDINAL, &type, &format, &len, &fill,
348 printf("cant get xa current property\n");
353 current = (int)*(long *)data;
357 //printf("_NET_CURRENT_DESKTOP is %d\n", current);
360 if(atomsSupported[_net_workarea])
364 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_workarea], 0, (4 * 32),
365 False, AnyPropertyType, &type, &format, &len,
366 &fill, &data) != Success)
368 //printf("warning\n");
372 if(type == None || format == 0)
384 workareas = (long *)data;
386 x = (int)workareas[current * 4];
387 y = (int)workareas[current * 4 + 1];
388 w = (int)workareas[current * 4 + 2];
389 h = (int)workareas[current * 4 + 3];
391 //printf("_NET_WORKAREA is x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
395 // printf("Work Area width: %d, height %d\n", w, h);
398 if(desktopX != x || desktopY != y || desktopW != w || desktopH != h)
400 guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
408 /****************************************************************************
409 /// PRIVATE UTILITY FUNCTIONS /////////////
410 ****************************************************************************/
411 // --- Keyboard Input ---
412 #define KEYCODE_HOME 0x59
413 #define KEYCODE_UP 0x5A
414 #define KEYCODE_PGUP 0x5B
415 #define KEYCODE_LEFT 0x5C
416 #define KEYCODE_RIGHT 0x5E
417 #define KEYCODE_END 0x5F
418 #define KEYCODE_DOWN 0x60
419 #define KEYCODE_PGDWN 0x61
420 #define KEYCODE_INS 0x62
421 #define KEYCODE_DEL 0x63
422 #define KEYCODE_SLASH 0x68
425 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
426 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
427 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
428 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
429 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
430 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
431 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
432 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
433 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
434 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
435 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
436 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
437 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
438 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
441 static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEvent * event)
446 KeySym keysym = NoSymbol;
449 static long bufsize = 16;
450 static char *buf = NULL;
451 XWindowData windowData = window.windowData;
454 if(window.destroyed) return result;
456 // Logf("Got 0x%x (%d)\n", keyCode, key);
458 // Translate the key code
461 //Logf("Got 0x%x (%d)\n", key, key);
465 case KEYCODE_HOME: key = home; break;
466 case KEYCODE_UP: key = up; break;
467 case KEYCODE_PGUP: key = pageUp; break;
468 case KEYCODE_LEFT: key = left; break;
469 case KEYCODE_RIGHT: key = right; break;
470 case KEYCODE_END: key = end; break;
471 case KEYCODE_DOWN: key = down; break;
472 case KEYCODE_PGDWN: key = pageDown; break;
473 case KEYCODE_INS: key = insert; break;
474 case KEYCODE_DEL: key = del; break;
475 case KEYCODE_SLASH: key = keyPadSlash; break;
478 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
482 Logf("Key up: %3d (%2x)\n", key, key);
484 Logf("Key down: %3d (%2x)\n", key, key);
488 buf = malloc((uint)bufsize);
489 if(windowData && windowData.ic)
491 buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
492 if (status == XBufferOverflow)
494 buf = realloc(buf, (uint)(bufsize = buflength));
495 buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
497 if(status != XLookupKeySym && status != XLookupBoth && release == 1)
498 keysym = XLookupKeysym(event, 0);
502 keysym = XLookupKeysym(event, 0);
505 // printf("Got keysym %d (%x)\n", keysym, keysym);
506 if(keysym != NoSymbol) //status == XLookupKeySym || status == XLookupBoth)
510 case XK_BackSpace: key = backSpace; break;
511 case XK_ISO_Left_Tab: key = tab; break;
512 case XK_Tab: key = tab; break;
515 case XK_Return: key = enter; break;
516 case XK_Pause: key = pauseBreak; break;
517 case XK_Scroll_Lock: key = scrollLock; break;
518 case XK_Sys_Req: key = printScreen; break;
519 case XK_Escape: key = escape; break;
520 case XK_Delete: key = del; break;
522 case XK_Home: key = home; break;
523 case XK_Left: key = left; break;
524 case XK_Up: key = up; break;
525 case XK_Right: key = right; break;
526 case XK_Down: key = down; break;
528 case XK_Page_Up: key = pageUp; break;
530 case XK_Page_Down: key = pageDown; break;
531 case XK_End: key = end; break;
537 case XK_Insert: key = insert; break;
544 case XK_Help: key = insert; break;
546 case XK_Break: key = Key { pauseBreak, ctrl = true }; break;
548 case XK_Mode_switch: key = leftAlt; break;
550 // case XK_script_switch:
551 case XK_Num_Lock: key = numLock; break;
555 case XK_KP_Enter: key = keyPadEnter; break;
560 case XK_KP_Home: key = keyPadHome; break;
561 case XK_KP_Left: key = keyPadLeft; break;
562 case XK_KP_Up: key = keyPadUp; break;
563 case XK_KP_Right: key = keyPadRight; break;
564 case XK_KP_Down: key = keyPadDown; break;
566 case XK_KP_Page_Up: key = keyPadPageUp; break;
568 case XK_KP_Page_Down:key = keyPadPageDown; break;
569 case XK_KP_End: key = keyPadEnd; break;
571 case XK_KP_Insert: key = keyPadInsert; break;
572 case XK_KP_Delete: key = keyPadDelete; break;
574 case XK_KP_Multiply: key = keyPadStar; break;
575 case XK_KP_Add: key = keyPadPlus; break;
576 case XK_KP_Separator:key = keyPadDelete; break;
577 case XK_KP_Subtract: key = keyPadMinus; break;
578 // case XK_KP_Decimal:
579 case XK_KP_Divide: key = keyPadSlash; break;
581 case XK_KP_0: key = keyPad0; break;
582 case XK_KP_1: key = keyPad1; break;
583 case XK_KP_2: key = keyPad2; break;
584 case XK_KP_3: key = keyPad3; break;
585 case XK_KP_4: key = keyPad4; break;
586 case XK_KP_5: key = keyPad5; break;
587 case XK_KP_6: key = keyPad6; break;
588 case XK_KP_7: key = keyPad7; break;
589 case XK_KP_8: key = keyPad8; break;
590 case XK_KP_9: key = keyPad9; break;
592 case XK_F1: key = f1; break;
593 case XK_F2: key = f2; break;
594 case XK_F3: key = f3; break;
595 case XK_F4: key = f4; break;
596 case XK_F5: key = f5; break;
597 case XK_F6: key = f6; break;
598 case XK_F7: key = f7; break;
599 case XK_F8: key = f8; break;
600 case XK_F9: key = f9; break;
601 case XK_F10: key = f10; break;
602 case XK_F11: key = f11; break;
604 case XK_F12: key = f12; break;
654 case XK_Shift_L: key = leftShift; break;
655 case XK_Shift_R: key = rightShift; break;
656 case XK_Control_L: key = leftControl; break;
657 case XK_Control_R: key = rightControl; break;
658 case XK_Caps_Lock: key = capsLock; break;
659 // case XK_Shift_Lock:
662 case XK_Alt_L: key = leftAlt; break;
663 case XK_Alt_R: key = rightAlt; break;
669 case XK_space: key = space; break;
670 case XK_exclam: key = bang; break;
671 case XK_quotedbl: key = doubleQuote; break;
672 case XK_numbersign: key = pound; break;
673 case XK_dollar: key = dollar; break;
674 case XK_percent: key = percent; break;
675 case XK_ampersand: key = ampersand; break;
676 case XK_apostrophe: key = quote; break;
677 // case XK_quoteright: key = quote; break;
678 case XK_parenleft: key = leftParanthesis; break;
679 case XK_parenright: key = rightParanthesis; break;
680 case XK_asterisk: key = star; break;
681 case XK_plus: key = plus; break;
682 case XK_comma: key = comma; break;
683 case XK_minus: key = minus; break;
684 case XK_period: key = period; break;
685 case XK_slash: key = slash; break;
686 case XK_0: key = k0; break;
687 case XK_1: key = k1; break;
688 case XK_2: key = k2; break;
689 case XK_3: key = k3; break;
690 case XK_4: key = k4; break;
691 case XK_5: key = k5; break;
692 case XK_6: key = k6; break;
693 case XK_7: key = k7; break;
694 case XK_8: key = k8; break;
695 case XK_9: key = k9; break;
696 case XK_colon: key = colon; break;
697 case XK_semicolon: key = semicolon; break;
698 case XK_less: key = smallerThan; break;
699 case XK_equal: key = equal; break;
700 case XK_greater: key = greaterThan; break;
701 case XK_question: key = questionMark; break;
702 case XK_at: key = ampersand; break;
703 case XK_A: key = a; break;
704 case XK_B: key = b; break;
705 case XK_C: key = c; break;
706 case XK_D: key = d; break;
707 case XK_E: key = e; break;
708 case XK_F: key = f; break;
709 case XK_G: key = g; break;
710 case XK_H: key = h; break;
711 case XK_I: key = i; break;
712 case XK_J: key = j; break;
713 case XK_K: key = k; break;
714 case XK_L: key = l; break;
715 case XK_M: key = m; break;
716 case XK_N: key = n; break;
717 case XK_O: key = o; break;
718 case XK_P: key = p; break;
719 case XK_Q: key = q; break;
720 case XK_R: key = r; break;
721 case XK_S: key = s; break;
722 case XK_T: key = t; break;
723 case XK_U: key = u; break;
724 case XK_V: key = v; break;
725 case XK_W: key = w; break;
726 case XK_X: key = x; break;
727 case XK_Y: key = y; break;
728 case XK_Z: key = z; break;
729 case XK_bracketleft: key = leftBracket; break;
730 case XK_backslash: key = backSlash; break;
731 case XK_bracketright: key = rightBracket; break;
732 case XK_asciicircum: key = circumflex; break;
733 case XK_underscore: key = underscore; break;
734 // case XK_grave: key = backQuote; break;
735 case XK_quoteleft: key = backQuote; break;
736 case XK_a: key = a; break;
737 case XK_b: key = b; break;
738 case XK_c: key = c; break;
739 case XK_d: key = d; break;
740 case XK_e: key = e; break;
741 case XK_f: key = f; break;
742 case XK_g: key = g; break;
743 case XK_h: key = h; break;
744 case XK_i: key = i; break;
745 case XK_j: key = j; break;
746 case XK_k: key = k; break;
747 case XK_l: key = l; break;
748 case XK_m: key = m; break;
749 case XK_n: key = n; break;
750 case XK_o: key = o; break;
751 case XK_p: key = p; break;
752 case XK_q: key = q; break;
753 case XK_r: key = r; break;
754 case XK_s: key = s; break;
755 case XK_t: key = t; break;
756 case XK_u: key = u; break;
757 case XK_v: key = v; break;
758 case XK_w: key = w; break;
759 case XK_x: key = x; break;
760 case XK_y: key = y; break;
761 case XK_z: key = z; break;
762 case XK_braceleft: key = leftBracket; break;
763 case XK_bar: key = pipe; break;
764 case XK_braceright: key = rightBracket; break;
765 case XK_asciitilde: key = tilde; break;
770 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
771 // 127 is delete, we don't treat that as a character (Use (SmartKey)key == del)
772 if(ch == 128 || ch == 127) ch = 0;
775 if(keysym == XK_ISO_Left_Tab)
777 if(key != leftShift && key != rightShift && event->state & ShiftMask)
779 if(key != leftControl && key != rightControl && event->state & ControlMask)
781 if(key != leftAlt && key != rightAlt && event->state & Mod1Mask)
785 if(key != leftAlt && key != rightAlt && event->state & (1<<13))
793 // 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);
799 if(windowData && windowData.ic) ch = buflength ? UTF8GetChar(buf, &numBytes) : 0;
800 if(ch == 127) ch = 0;
801 // printf("Release! %d %d %d\n", keysym, code, ch);
802 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, code, ch);
809 if(windowData.ic && buflength)
811 for(c = 0; c<buflength;)
814 ch = UTF8GetChar(buf + c, &numBytes);
815 if(ch == 127) ch = 0;
816 result = window.KeyMessage((c == 0) ?
817 __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown : __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit,
818 (c == 0) ? code : 0, ch);
820 if(!numBytes) c = buflength;
824 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, code, ch);
828 if(buflength && windowData.ic)
829 for(c = 0; c<buflength;)
832 ch = UTF8GetChar(buf + c, &numBytes);
833 if(ch == 127) ch = 0;
834 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
836 if(!numBytes) c = buflength;
839 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
847 static uint E_CALL XEventThread(void * data)
853 if(xTerminate) break;
856 XPeekEvent(xGlobalDisplay, &e);
858 guiApp.SignalEvent();
865 static Bool EventChecker(void *display, XEvent *event, char * data)
867 return (!data || (event->type == (int) data)) && event->type != NoExpose && event->type != GraphicsExpose;
870 static Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
872 return (!data || (event->window == (X11Window) data)) && event->type == ConfigureNotify;
875 static enum FrameExtentSupport { unknown, working, broken };
877 static FrameExtentSupport frameExtentSupported;
878 static Time frameExtentRequest;
879 static X11Window frameExtentWindow;
881 static uint timerDelay = MAXINT;
882 #define RESOLUTION (18.2 * 100)
883 static uint XTimerThread(Thread thread)
885 int s = ConnectionNumber(xGlobalDisplay);
887 Time lastTime, thisTime;
888 lastTime = GetTime();
893 //bool waitSemaphore = false;
894 fd_set readSet, writeSet, exceptSet;
895 struct timeval tv = { (timerDelay == MAXINT) ? 0 : (timerDelay / 1000000), (timerDelay == MAXINT) ? (int)(1000000 / 18.2) : (timerDelay % 1000000) };
897 if(xTerminate) break;
902 FD_SET(s, &exceptSet);
904 if(select(s + 1, &readSet, null, null, &tv))
906 if(FD_ISSET(s, &readSet))
909 if(frameExtentSupported == unknown && frameExtentRequest && GetTime() - frameExtentRequest > 1)
911 XPropertyEvent event = { 0 };
912 event.type = PropertyNotify;
913 event.state = PropertyNewValue;
914 event.atom = atoms[_net_frame_extents];
915 event.display = xGlobalDisplay;
917 event.window = frameExtentWindow;
918 event.send_event = 1;
920 frameExtentSupported = broken;
922 XSendEvent(xGlobalDisplay, frameExtentWindow, bool::false,
923 PropertyChangeMask, (union _XEvent *)&event);
926 guiApp.SignalEvent();
931 Sleep(1.0 / RESOLUTION);
932 thisTime = GetTime();
933 if(xTerminate) break;
934 if(thisTime - lastTime > (1.0 / 18.2))
936 guiApp.SignalEvent();
944 XLockDisplay(xGlobalDisplay);
945 if(XCheckIfEvent(xGlobalDisplay, &xEvent, EventChecker, null))
948 guiApp.SignalEvent();
950 XUnlockDisplay(xGlobalDisplay);
959 static int MyXErrorHandler(X11Display * display, XErrorEvent * event)
963 XGetErrorText(xGlobalDisplay, event->error_code, buffer, sizeof(buffer));
965 Logf("X Error: %s\n", buffer);
970 static int MyXIOErrorHandler(X11Display * display)
976 // Motif Hints (to get rid of the decorations)
977 #define MWM_HINTS_FUNCTIONS (1L << 0)
978 #define MWM_HINTS_DECORATIONS (1L << 1)
979 #define MWM_HINTS_INPUT_MODE (1L << 2)
980 #define MWM_HINTS_STATUS (1L << 3)
982 #define MWM_DECOR_ALL (1L << 0)
983 #define MWM_DECOR_BORDER (1L << 1)
984 #define MWM_DECOR_RESIZEH (1L << 2)
985 #define MWM_DECOR_TITLE (1L << 3)
986 #define MWM_DECOR_MENU (1L << 4)
987 #define MWM_DECOR_MINIMIZE (1L << 5)
988 #define MWM_DECOR_MAXIMIZE (1L << 6)
990 #define MWM_FUNC_ALL (1L << 0)
991 #define MWM_FUNC_RESIZE (1L << 1)
992 #define MWM_FUNC_MOVE (1L << 2)
993 #define MWM_FUNC_MINIMIZE (1L << 3)
994 #define MWM_FUNC_MAXIMIZE (1L << 4)
995 #define MWM_FUNC_CLOSE (1L << 5)
1000 unsigned long functions;
1001 unsigned long decorations;
1003 unsigned long status;
1006 static void WaitForViewableWindow(Window window)
1009 //Logf("Wait for viewable %s\n", window.name);
1010 XFlush(xGlobalDisplay);
1011 //while(attempts++ < 40)
1014 XWindowAttributes attributes = { 0 };
1016 if(!XGetWindowAttributes(xGlobalDisplay, (X11Window)window.windowHandle, &attributes))
1018 if(attributes.map_state == IsViewable)
1021 Sleep(1.0 / RESOLUTION);
1025 static bool RequestFrameExtents(X11Window windowHandle)
1027 if(frameExtentSupported != broken)
1029 // Request decoration frame extents
1030 XClientMessageEvent event = { 0 };
1031 event.type = ClientMessage;
1032 event.message_type = atoms[_net_request_frame_extents];
1033 event.display = xGlobalDisplay;
1035 event.window = windowHandle;
1036 event.send_event = 1;
1039 if(frameExtentSupported == unknown && !frameExtentRequest)
1041 frameExtentRequest = GetTime();
1042 frameExtentWindow = windowHandle;
1045 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false,
1046 SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
1052 static bool GetFrameExtents(Window window, bool update)
1054 XWindowData windowData = window.windowData;
1055 bool result = false;
1057 unsigned long len, fill;
1061 if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.windowHandle,
1062 atoms[_net_frame_extents], 0, 4,
1063 False, XA_CARDINAL, &type, &format, &len,
1064 &fill, &data) == Success && data)
1066 long *extents = (long *)data;
1067 bool change = extents[0] != windowData.decor.left ||
1068 extents[1] != windowData.decor.right ||
1069 extents[2] != windowData.decor.top ||
1070 extents[3] != windowData.decor.bottom;
1072 bool hadFrameExtents = windowData.gotFrameExtents;
1073 Box oldDecor = windowData.decor;
1075 frameExtentSupported = working;
1076 frameExtentWindow = 0;
1077 frameExtentRequest = 0;
1079 if(!hadFrameExtents || extents[0] || extents[1] || extents[2] || extents[3])
1083 left = (int)extents[0], right = (int)extents[1],
1084 top = (int)extents[2], bottom = (int)extents[3]
1086 windowData.gotFrameExtents = true;
1087 if(update && change && ((Window)window).clientSize.w > 0)
1089 int x = window.position.x, y = window.position.y, w = window.size.w, h = window.size.h;
1090 if(!hadFrameExtents && window.state != maximized)
1092 window.ComputeAnchors(
1093 window.normalAnchor,
1094 window.normalSizeAnchor,
1099 x += windowData.decor.left - oldDecor.left;
1100 y += windowData.decor.top - oldDecor.top;
1102 w += windowData.decor.left - oldDecor.left + windowData.decor.right - oldDecor.right;
1103 h += windowData.decor.top - oldDecor.top + windowData.decor.bottom - oldDecor.bottom;
1106 if(window.state != maximized)
1108 window.Position(x, y, w, h, true, true, true, true, false, !hadFrameExtents && window.state != maximized);
1109 XInterface::UpdateRootWindow(window);
1119 static bool WaitForFrameExtents(Window window)
1122 //XFlush(xGlobalDisplay);
1123 while(attempts++ < 10)
1125 if(GetFrameExtents(window, false)) return true;
1126 Sleep(1.0 / RESOLUTION);
1131 /****************************************************************************
1132 /// DRIVER IMPLEMENTATION /////////////
1133 ****************************************************************************/
1135 /*static */class HiResTimer : Thread
1154 // Sleep(delay / 1000000.0);
1155 struct timeval tv = { delay / 1000000, delay % 1000000 };
1156 select(0,null,null,null, &tv);
1157 guiApp.SignalEvent();
1163 static HiResTimer hiResTimer { };
1167 #include <sys/ipc.h>
1168 #include <sys/shm.h>
1180 static int terminatePid;
1182 static void SigIntHandler(int value)
1184 // printf("SigHandler %d\n", getpid());
1185 if(!terminatePid || terminatePid == getpid())
1188 terminatePid = getpid();
1189 // printf("terminateX now equals %d\n", terminateX);
1190 if(guiApp && guiApp.semaphore)
1191 guiApp.semaphore.Release();
1194 struct shmid_ds info;
1195 int maxid = shmctl (0, SHM_INFO, &info);
1196 int pid = getpgrp();
1197 int thisPid = getpid();
1198 //if(thisPid == pid)
1204 for(id = 0; id <= maxid; id++)
1206 struct shmid_ds shmseg;
1208 if((shmid = shmctl(id, SHM_STAT, &shmseg)) >= 0)
1210 if(shmseg.shm_cpid == pid || shmseg.shm_cpid == thisPid)
1212 printf("%d (%d) belongs to us (%d)\n", shmid, id, shmseg.shm_cpid);
1213 shmctl(shmid, IPC_RMID, 0);
1221 /*else if(guiApp.desktop)
1222 guiApp.desktop.Destroy(0);*/
1225 class XInterface : Interface
1227 class_property(name) = "X";
1229 // --- User Interface System ---
1232 setlocale(LC_ALL, "en_US.UTF-8");
1235 XSetLocaleModifiers("");
1236 XSetErrorHandler(MyXErrorHandler);
1237 XSetIOErrorHandler(MyXIOErrorHandler);
1239 signal(SIGINT, SigIntHandler);
1242 xGlobalDisplay = XOpenDisplay(null);
1243 // XSynchronize(xGlobalDisplay, True);
1244 frameExtentSupported = unknown;
1246 joystickFD[0] = open("/dev/js0", O_RDONLY);
1247 joystickFD[1] = open("/dev/js1", O_RDONLY);
1248 joystickFD[2] = open("/dev/js2", O_RDONLY);
1249 joystickFD[3] = open("/dev/js3", O_RDONLY);
1251 systemCursors[iBeam] = XCreateFontCursor(xGlobalDisplay, XC_xterm);
1252 systemCursors[cross] = XCreateFontCursor(xGlobalDisplay, XC_tcross);
1253 systemCursors[moving] = XCreateFontCursor(xGlobalDisplay, XC_fleur);
1254 systemCursors[sizeNESW] = XCreateFontCursor(xGlobalDisplay, XC_bottom_left_corner);
1255 systemCursors[sizeNS] = XCreateFontCursor(xGlobalDisplay, XC_sb_v_double_arrow);
1256 systemCursors[sizeNWSE] = XCreateFontCursor(xGlobalDisplay, XC_bottom_right_corner);
1257 systemCursors[sizeWE] = XCreateFontCursor(xGlobalDisplay, XC_sb_h_double_arrow);
1258 systemCursors[hand] = XCreateFontCursor(xGlobalDisplay, XC_hand2);
1259 systemCursors[arrow] = XCreateFontCursor(xGlobalDisplay, XC_left_ptr);
1263 XWindowAttributes attributes = { 0 };
1264 XGetWindowAttributes(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &attributes);
1265 xSystemDepth = attributes.depth;
1266 xSystemVisual = attributes.visual;
1267 switch(xSystemDepth)
1269 case 32: case 24: xSystemPixelFormat = pixelFormat888; break;
1274 XVisualInfo *vinfo_ret;
1277 vinfo.visualid = XVisualIDFromVisual(xSystemVisual);
1278 vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &vinfo, &numitems);
1281 xSystemPixelFormat = (vinfo_ret->green_mask == 0x3E0) ? pixelFormat555 : pixelFormat565;
1283 if(GetXRenderFormat(xSystemPixelFormat, 0))
1285 // printf("Got X Render format %d\n", xSystemPixelFormat);
1289 printf("No X Format?\n");
1290 xSystemPixelFormat = (xSystemPixelFormat == pixelFormat555) ? pixelFormat565 : pixelFormat555;
1292 // vinfo._class = DirectColor;
1294 vinfo.bits_per_rgb = 5;
1295 vinfo.depth = xSystemDepth;
1296 vinfo.green_mask = (xSystemPixelFormat == pixelFormat555) ? 0x3E0 : 0x7E0;
1297 vinfo.red_mask = (xSystemPixelFormat == pixelFormat555) ? 0x7C00 : 0xF800;
1298 XFree((void *) vinfo_ret);
1299 if(XMatchVisualInfo(xGlobalDisplay, DefaultScreen(xGlobalDisplay), vinfo.depth, vinfo._class, &vinfo))
1300 //vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualDepthMask|VisualRedMaskMask|VisualGreenMaskMask|VisualBlueMaskMask, &vinfo, &numitems);
1303 //vinfo = *vinfo_ret;
1304 if(GetXRenderFormat(xSystemPixelFormat, 0))
1306 // printf("Got X Render format %d (second try)\n", xSystemPixelFormat);
1307 // printf("red mask: %x, green mask: %x, blue mask: %x\n", vinfo.red_mask,vinfo.green_mask, vinfo.blue_mask);
1309 xSystemVisual = vinfo.visual;
1312 printf("Could not get a 555 visual\n");
1318 // printf("Got a depth of %d\n", xSystemDepth);
1321 int major, minor, pixmaps;
1322 xSharedMemory = XShmQueryExtension(xGlobalDisplay) && XShmQueryVersion(xGlobalDisplay, &major, &minor, &pixmaps) && pixmaps;
1325 // printf("Opening IM\n");
1326 im = XOpenIM(xGlobalDisplay, null, null, null);
1329 XColor fore = { 0 }, back = { 0 };
1330 Pixmap pixmap = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1331 Pixmap mask = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1332 XSetWindowAttributes attributes = { 0 };
1334 XkbSetDetectableAutoRepeat(xGlobalDisplay, True, &autoRepeatDetectable);
1336 XInternAtoms(xGlobalDisplay, (char**)atomNames, AtomIdents::enumSize, False, atoms);
1338 // Check which atoms are supported by the WM
1341 unsigned long count, fill;
1345 if(XGetWindowProperty(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), atoms[_net_supported],
1346 0, 10000, False, XA_ATOM, &type, &format, &count, &fill, (void *)&data) == Success)
1349 for (i = 0; i < count; i++)
1352 for(j = 0; j < AtomIdents::enumSize; j++)
1354 if(atoms[j] == data[i])
1356 atomsSupported[j] = true;
1366 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
1368 XSetWMProtocols(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), protocols, 2);
1372 if(atomsSupported[_net_workarea])
1373 printf("Warning: _NET_WORKAREA extension not supported\n");
1376 attributes.override_redirect = True;
1379 windowContext = XUniqueContext();
1382 nullCursor = XCreatePixmapCursor(xGlobalDisplay, pixmap, mask, &fore, &back, 0, 0);
1383 confineWindow = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
1384 0,0,1,1,0, CopyFromParent, InputOnly, xSystemVisual /*CopyFromParent*/, CWOverrideRedirect, &attributes);
1387 // IM initialization
1389 /*ic = XCreateIC(im, XNInputStyle,
1390 XIMPreeditNothing | XIMStatusNothing,
1391 // XIMPreeditNone | XIMStatusNone | XIMPreeditPosition | XIMStatusArea | XIMPreeditArea | XIMStatusArea | XIMPreeditNothing | XIMStatusNothing,
1392 XNClientWindow, confineWindow, XNFocusWindow, confineWindow, 0);
1397 XIMStyle clientCanDo;
1398 XIMStyle styleWeWillUse = null;
1400 XVaNestedList arglist;
1401 unsigned long imEventMask;
1403 XGetImValues(im, XNQueryInputStyle, &IMcando, null);
1406 XIMPreeditNone | XIMStatusNone |
1407 XIMPreeditPosition | XIMStatusArea |
1408 XIMPreeditArea | XIMStatusArea |
1409 XIMPreeditNothing | XIMStatusNothing;
1411 for(i=0; i<IMcando->count_styles; i++)
1414 tmpStyle = IMcando->support_styles[i];
1415 if ( ((tmpStyle & clientCanDo) == tmpStyle) && prefer(tmpStyle, styleWeWillUse) )
1416 styleWeWillUse = tmpStyle;
1418 if(styleWeWillUse = null)
1422 arglist = XVaCreateNestedList(0, XNFontSet, fontset,
1424 WhitePixel(xGlobalDisplay, screen),
1426 BlackPixel(xGlobalDisplay, screen),
1428 ic = XCreateIC(im, XNInputStyle, styleWeWillUse,
1429 XNClientWindow, window, XNFocusWindow, window,
1430 XNStatusAttributes, arglist,
1431 XNPreeditAttributes, arglist, NULL);
1436 XGetWindowAttributes(xGlobalDisplay, win, &winAtts);
1437 XGetICValues(ic, XNFilterEvents, &imEventMask, null);
1438 imEventMask |= winAtts.your_event_mask;
1439 XSelectInput(xGlobalDisplay, window, imEventMask);
1445 timerThread = Thread { };
1447 timerThread.Main = XTimerThread;
1448 timerThread.Create();
1461 // WHY WAS THIS COMMENTED HERE?
1462 // Probably because it was causing crashes, the proper fix should now be in DestroyRootWindow
1466 xSemaphore.Release();
1472 XFreeCursor(xGlobalDisplay, systemCursors[iBeam]);
1473 XFreeCursor(xGlobalDisplay, systemCursors[cross]);
1474 XFreeCursor(xGlobalDisplay, systemCursors[moving]);
1475 XFreeCursor(xGlobalDisplay, systemCursors[sizeNESW]);
1476 XFreeCursor(xGlobalDisplay, systemCursors[sizeNS]);
1477 XFreeCursor(xGlobalDisplay, systemCursors[sizeNWSE]);
1478 XFreeCursor(xGlobalDisplay, systemCursors[sizeWE]);
1479 XFreeCursor(xGlobalDisplay, systemCursors[hand]);
1480 XFreeCursor(xGlobalDisplay, systemCursors[arrow]);
1482 //XPutBackEvent(xGlobalDisplay, &e);
1486 /*if(windowData && windowData.ic)
1488 XDestroyIC(windowData.ic);
1489 windowData.ic = null;
1496 XCloseDisplay(xGlobalDisplay);
1497 xGlobalDisplay = null;
1499 if(joystickFD[0] != -1) close(joystickFD[0]);
1500 if(joystickFD[1] != -1) close(joystickFD[1]);
1501 if(joystickFD[2] != -1) close(joystickFD[2]);
1502 if(joystickFD[3] != -1) close(joystickFD[3]);
1505 #define DBLCLICK_DELAY 300 // 0.3 second
1506 #define DBLCLICK_DELTA 1
1508 bool ProcessInput(bool processAll)
1510 bool eventAvailable = false;
1513 if(!fullScreenMode) RepositionDesktop(true);
1515 //* XLockDisplay(xGlobalDisplay);
1516 while(!xTerminate && (/*gotAnXEvent || */XCheckIfEvent(xGlobalDisplay, &e, EventChecker, null)))
1518 Window window = null;
1519 XAnyEvent * thisEvent = (XAnyEvent *)&e;
1520 // printf("Got an event: %d\n", thisEvent->type);
1522 //if(gotAnXEvent) { thisEvent = (XAnyEvent *)&xEvent; gotAnXEvent = false; }
1524 if(im && XFilterEvent((union _XEvent *)thisEvent, None))
1526 eventAvailable = true;
1527 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1530 XWindowData windowData = window.windowData;
1531 static uint lastKeyCode = 0;
1532 switch(thisEvent->type)
1536 XKeyEvent * event = (XKeyEvent *) thisEvent;
1538 timeStamp = event->time;
1541 Window modalRoot = window.FindModal();
1542 XWindowData windowData;
1544 activeWindow = (X11Window)window.windowHandle;
1546 if(!window.parent || window != window.parent.activeChild)
1549 modalRoot.ExternalActivate(true, true, window, null);
1551 window.ExternalActivate(true, true, window, null); // lastActive);
1552 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1553 if(windowData && windowData.ic)
1555 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
1556 XSetICFocus(windowData.ic);
1560 //*XUnlockDisplay(xGlobalDisplay);
1561 ProcessKeyMessage(window, event->keycode, (event->keycode == lastKeyCode) ? 2 : 0, event);
1562 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1563 lastKeyCode = event->keycode;
1569 XKeyEvent * event = (XKeyEvent *) thisEvent;
1572 timeStamp = event->time;
1573 if(!autoRepeatDetectable && XCheckIfEvent(xGlobalDisplay, (XEvent *)&nextEvent, EventChecker, (void *)KeyPress))
1575 if(im && XFilterEvent(&nextEvent, None))
1578 if(((XKeyEvent *)&nextEvent)->keycode == event->keycode)
1580 //*XUnlockDisplay(xGlobalDisplay);
1581 ProcessKeyMessage(window, event->keycode, 2, event);
1585 //*XUnlockDisplay(xGlobalDisplay);
1586 // printf("Keycode not the same :(\n");
1587 ProcessKeyMessage(window, event->keycode, 1, event);
1588 ProcessKeyMessage(window, ((XKeyEvent *)&nextEvent)->keycode, 0, (XKeyEvent *)&nextEvent);
1593 //*XUnlockDisplay(xGlobalDisplay);
1594 // printf("No KeyPress ahead\n");
1595 ProcessKeyMessage(window, event->keycode, 1, event);
1597 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1602 XButtonEvent * event = (XButtonEvent *) thisEvent;
1604 static double lastTime[3];
1605 static Window lastWindow[3];
1606 static Point lastPos[3];
1608 Modifiers keyFlags = 0;
1610 uint button, buttonDouble, whichButton;
1612 int x = event->x_root, y = event->y_root;
1613 timeStamp = event->time;
1614 if(event->button == Button1)
1616 // Force a raise on click here to deal with confused active state preventing to bring the window up
1617 if(!atomsSupported[_net_active_window] && !window.isRemote)
1618 XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
1619 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
1620 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
1621 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
1623 buttonMask = Button1Mask;
1624 keyFlags.left = true;
1626 else if(event->button == Button3)
1628 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
1629 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
1631 buttonMask = Button3Mask;
1632 keyFlags.right = true;
1636 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
1637 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
1639 buttonMask = Button2Mask;
1640 keyFlags.middle = true;
1642 if(event->state & buttonMask)
1645 doubleClick = event->time - lastTime[whichButton] < DBLCLICK_DELAY &&
1646 window == lastWindow[whichButton] &&
1647 Abs(event->x_root - lastPos[whichButton].x) < DBLCLICK_DELTA &&
1648 Abs(event->y_root - lastPos[whichButton].y) < DBLCLICK_DELTA;
1649 lastTime[whichButton] = doubleClick ? 0 : event->time;
1650 lastWindow[whichButton] = window;
1651 lastPos[whichButton].x = event->x_root;
1652 lastPos[whichButton].y = event->y_root;
1654 if(event->state & ShiftMask) keyFlags.shift = true;
1655 if(event->state & ControlMask) keyFlags.ctrl = true;
1656 if(event->state & Mod1Mask) keyFlags.alt = true;
1657 if(event->state & Button1Mask) keyFlags.left = true;
1658 if(event->state & Button2Mask) keyFlags.middle = true;
1659 if(event->state & Button3Mask) keyFlags.right = true;
1660 //*XUnlockDisplay(xGlobalDisplay);
1663 if(event->button == Button4 || event->button == Button5)
1665 window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, (event->button == Button4) ? wheelUp : wheelDown, 0);
1671 if(!window.MouseMessage(buttonDouble, x, y, &keyFlags, false, true))
1673 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1678 window.MouseMessage(button, x, y, &keyFlags, false, /*doubleClick? false : */true);
1680 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1686 Modifiers keyFlags = 0;
1687 XButtonEvent * event = (XButtonEvent *) thisEvent;
1690 int x = event->x_root, y = event->y_root;
1691 if(event->button == Button1)
1693 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
1694 buttonMask = Button1Mask;
1696 else if(event->button == Button3)
1698 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
1699 buttonMask = Button3Mask;
1703 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
1704 buttonMask = Button2Mask;
1706 timeStamp = event->time;
1707 if(!(event->state & buttonMask)) break;
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 if(guiApp.windowCaptured && guiApp.windowCaptured != window)
1716 // X hasn't noticed the capture yet, so fix it!
1717 x += window.absPosition.x;
1718 y += window.absPosition.y;
1719 window = guiApp.windowCaptured.rootWindow;
1720 x -= window.absPosition.x;
1721 y -= window.absPosition.y;
1723 //*XUnlockDisplay(xGlobalDisplay);
1725 window.MouseMessage(button, x, y, &keyFlags, false, false);
1727 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1732 static uint lastTime = 0;
1733 XMotionEvent * event = (XMotionEvent *) thisEvent;
1734 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, EventChecker, (void *)MotionNotify));
1735 // if(event->time - lastTime > 15)
1737 Modifiers keyFlags = 0;
1738 timeStamp = event->time;
1739 // int x = event->x_root, y = event->y_root;
1741 if(event->state & ShiftMask) keyFlags.shift = true;
1742 if(event->state & ControlMask) keyFlags.ctrl = true;
1743 if(event->state & Mod1Mask) keyFlags.alt = true;
1744 if(event->state & Button1Mask) keyFlags.left = true;
1745 if(event->state & Button2Mask) keyFlags.middle = true;
1746 if(event->state & Button3Mask) keyFlags.right = true;
1747 //*XUnlockDisplay(xGlobalDisplay);
1749 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove,
1750 event->x_root, event->y_root, &keyFlags, false, false);
1752 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1753 lastTime = (uint)event->time;
1759 XExposeEvent * event = (XExposeEvent *) thisEvent;
1761 box.left = event->x;
1763 box.right = box.left + event->width - 1;
1764 box.bottom = box.top + event->height - 1;
1765 window.UpdateDirty(box);
1768 case SelectionRequest:
1770 XSelectionRequestEvent *req = (XSelectionRequestEvent *) thisEvent;
1772 if(req->target == atoms[targets] && clipBoardData)
1774 Atom * supportedTargets = new Atom[4];
1775 supportedTargets[0] = atoms[targets];
1776 supportedTargets[1] = atoms[multiple];
1777 supportedTargets[2] = XA_STRING;
1778 supportedTargets[3] = atoms[utf8_string];
1779 XChangeProperty(xGlobalDisplay,req->requestor, req->_property,
1780 XA_ATOM,32,PropModeReplace, (byte *) supportedTargets, 4*sizeof(Atom));
1781 respond.xselection._property = req->_property;
1782 delete supportedTargets;
1784 else if((req->target == XA_STRING || req->target == atoms[utf8_string]) && clipBoardData)
1786 Atom _property = (req->_property == None) ? req->target : req->_property;
1787 XChangeProperty(xGlobalDisplay,req->requestor, _property,
1788 req->target/*req->_property*/,8,PropModeReplace, (byte *) clipBoardData, strlen(clipBoardData));
1789 respond.xselection._property = _property;
1792 respond.xselection._property = None;
1794 respond.xselection.type = SelectionNotify;
1795 respond.xselection.display = req->display;
1796 respond.xselection.requestor = req->requestor;
1797 respond.xselection.selection =req->selection;
1798 respond.xselection.target = req->target;
1799 respond.xselection.time = CurrentTime;
1800 XSendEvent(xGlobalDisplay, req->requestor,0,0,&respond);
1803 case SelectionClear:
1805 delete clipBoardData;
1810 if(activeWindow != (X11Window)window.windowHandle)
1812 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1813 Window modalRoot = window.FindModal();
1814 XWindowData windowData;
1816 activeWindow = (X11Window)window.windowHandle;
1818 if(window.parent && window == window.parent.activeChild) break;
1820 //if(window.creationActivation == activate)
1823 modalRoot.ExternalActivate(true, true, window, null); // lastActive);
1825 window.ExternalActivate(true, true, window, null); // lastActive);
1827 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1828 if(windowData && windowData.ic)
1830 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
1831 XSetICFocus(windowData.ic);
1833 //delete lastActive;
1834 //lastActive = window;
1835 //incref lastActive;
1843 //printf("Processing a FocusOut Event for %s (%x)\n", window._class.name, window);
1846 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusIn, (XEvent *)thisEvent))
1850 if(thisEvent->window == activeWindow)
1851 activeWindow = (X11Window)null;
1853 if(XCheckTypedEvent(xGlobalDisplay, FocusIn, (XEvent *)thisEvent))
1855 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusOut, (XEvent *)thisEvent))
1857 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1859 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1862 Window windowCopy = window;
1863 XWindowData windowData;
1864 if(windowCopy == windowCopy.parent.activeChild) break;
1866 windowCopy.ExternalActivate(true, true, windowCopy, lastActive);
1868 windowData = windowCopy.windowData;
1869 if(windowData && windowData.ic)
1870 XSetICFocus(windowData.ic);
1873 lastActive = windowCopy;
1878 // XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1881 if(window != window.parent.activeChild) break;
1883 window.ExternalActivate(false, true, window, lastActive);
1889 XWindowData windowData;
1890 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1894 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1895 Window modalRoot = window.FindModal();
1900 modalRoot.ExternalActivate(true, true, window, lastActive);
1904 window.ExternalActivate(true, true, window, lastActive);
1906 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1907 if(windowData && windowData.ic)
1908 XSetICFocus(windowData.ic);
1911 lastActive = window;
1920 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1921 if(window != window.parent.activeChild && window != guiApp.interimWindow) break;
1925 //printf("Deactivating %s\n", window._class.name);
1928 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
1930 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
1933 //delete lastActive;
1935 lastActive = window;
1942 case ConfigureNotify:
1944 XConfigureEvent * event = (XConfigureEvent *) thisEvent;
1945 bool unmaximized = false;
1946 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, (void *)ConfigureNotifyChecker, (void *)window.windowHandle));
1947 //if(event->x - desktopX != window.position.x || event->y - desktopY != window.position.y || event->width != window.size.w || event->height != window.size.h)
1949 if(atomsSupported[_net_wm_state]) //window.nativeDecorations)
1952 unsigned long len, fill;
1955 if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.systemHandle, atoms[_net_wm_state], 0, 32, False,
1956 XA_ATOM, &type, &format, &len, &fill, &data) == Success)
1958 bool maxVert = false, maxHorz = false, isMinimized = false;
1959 Atom * hints = (Atom *)data;
1961 for(c = 0; c < len && hints[c]; c++)
1963 if(hints[c] == atoms[_net_wm_state_maximized_vert])
1965 else if(hints[c] == atoms[_net_wm_state_maximized_horz])
1967 else if(hints[c] == atoms[_net_wm_state_hidden])
1972 if(maxVert && maxHorz)
1974 if(window.state != maximized)
1976 *&window.state = maximized;
1977 if(!window.nativeDecorations)
1978 window.CreateSystemChildren();
1981 else if(isMinimized)
1983 if(window.state != minimized)
1985 *&window.state = minimized;
1986 if(!window.nativeDecorations)
1987 window.CreateSystemChildren();
1990 else if(window.state != normal)
1992 if(window.state == maximized)
1994 *&window.state = normal;
1995 if(!window.nativeDecorations)
1996 window.CreateSystemChildren();
2001 bool offset = false;
2005 if(window.nativeDecorations && RequestFrameExtents((X11Window)window.windowHandle))
2006 WaitForFrameExtents(window);
2008 // Ensure we set the normal size anchor when un-maximizing
2009 window.ComputeAnchors(window.normalAnchor, window.normalSizeAnchor, &x, &y, &w, &h);
2015 w = event->width, h = event->height;
2017 //if(event->send_event)
2019 X11Window rootChild;
2021 XTranslateCoordinates(xGlobalDisplay, event->window,
2022 RootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 0, 0,
2023 &rootX, &rootY, &rootChild);
2024 if(x != rootX || y != rootY)
2035 if(window.nativeDecorations && window.state != maximized)
2037 x -= windowData.decor.left;
2038 y -= windowData.decor.top;
2039 w += windowData.decor.left + windowData.decor.right;
2040 h += windowData.decor.top + windowData.decor.bottom;
2043 x -= window.clientStart.x;
2044 y -= window.clientStart.y - (window.hasMenuBar ? skinMenuHeight : 0);
2045 w += window.size.w - window.clientSize.w;
2046 h += window.size.h - window.clientSize.h;
2051 window.Position(x, y, w, h, true, true, true, true, false, unmaximized);
2053 // Break the anchors for moveable/resizable windows
2054 // Avoid doing this if the translation wasn't in sync as it will cause the window to move around
2055 if(!unmaximized && !offset && window.style.fixed && window.state == normal)
2057 window.normalAnchor = Anchor
2059 left = x + windowData.decor.left,
2060 top = y + windowData.decor.top
2062 window.normalSizeAnchor =
2063 SizeAnchor { { window.clientSize.w, window.clientSize.h }, isClientW = true, isClientH = true };
2070 XClientMessageEvent * event = (XClientMessageEvent *) thisEvent;
2072 if(event->data.l[0] == atoms[wm_delete_window])
2077 if(event->data.l[0] == atoms[wm_take_focus])
2080 XWindowData windowData;
2082 activeWindow = (X11Window)window.windowHandle;
2084 timeStamp = (X11Time)event->data.l[1];
2086 windowData = window.windowData;
2087 laterFocus = windowData.laterFocus;
2088 windowData.laterFocus = true;
2091 //printf("Processing a ClientMessage WM_TAKE_FOCUS Event for %s (%x)\n", window._class.name, window);
2093 if(guiApp.interimWindow && guiApp.interimWindow.created && window != guiApp.interimWindow) break; //window == window.parent.activeChild) break;
2094 // if(window == window.parent.activeChild) break;
2099 //XFlush(xGlobalDisplay);
2100 while(XCheckTypedEvent(xGlobalDisplay, FocusOut, &checkEvent))
2102 XFocusChangeEvent *event = (XFocusChangeEvent *) &checkEvent;
2104 XFindContext(xGlobalDisplay, event->window, windowContext, (XPointer *) &window);
2105 if(window != window.parent.activeChild) break;
2109 //printf("Found a FocusOut ahead, deactivating %s (%d)\n", window._class.name, window);
2112 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
2114 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
2121 modalRoot = window.FindModal();
2122 windowData = modalRoot ? modalRoot.windowData : window.windowData;
2125 if(laterFocus || (modalRoot ? modalRoot : window).creationActivation == activate)
2129 XRaiseWindow(xGlobalDisplay, (X11Window)modalRoot.windowHandle);
2130 WaitForViewableWindow(modalRoot);
2131 if(atomsSupported[_net_active_window])
2133 XClientMessageEvent event = { 0 };
2134 event.type = ClientMessage;
2135 event.message_type = atoms[_net_active_window];
2136 event.display = xGlobalDisplay;
2138 event.window = (X11Window)modalRoot.windowHandle;
2139 event.send_event = 1;
2141 event.data.l[0] = /*0*/ 1;
2142 event.data.l[1] = timeStamp;
2143 event.data.l[2] = activeWindow;
2145 event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
2148 //printf("(ClientMessage - %s) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, modalRoot._class.name, modalRoot);
2151 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2152 XSetInputFocus(xGlobalDisplay, (X11Window)modalRoot.windowHandle, RevertToPointerRoot, timeStamp);
2154 //XFlush(xGlobalDisplay);
2155 //printf("Done.\n");
2160 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, timeStamp);
2161 window.ExternalActivate(true, true, window, null); // lastActive);
2162 if(windowData && windowData.ic)
2164 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
2165 //XSetICFocus(windowData.ic);
2169 windowData.laterFocus = true;
2171 lastActive = window;
2178 case PropertyNotify:
2180 XWindowData windowData = window.windowData;
2181 XPropertyEvent * event = (XPropertyEvent *) thisEvent;
2182 if(event->atom == atoms[_net_frame_extents] &&
2183 event->state == PropertyNewValue && windowData)
2185 if(!GetFrameExtents(window, true))
2186 windowData.gotFrameExtents = true; // Unsupported?
2191 if(!processAll) break;
2194 //*if(xGlobalDisplay) XUnlockDisplay(xGlobalDisplay);
2203 gotAnXEvent = false;
2205 xSemaphore.Release();
2210 void Lock(Window window)
2212 //*XLockDisplay(xGlobalDisplay);
2215 void Unlock(Window window)
2217 //*XUnlockDisplay(xGlobalDisplay);
2220 char ** GraphicsDrivers(int * numDrivers)
2222 static char *graphicsDrivers[] = { "X", "OpenGL" };
2223 *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
2224 return (char **)graphicsDrivers;
2227 void GetCurrentMode(bool * fullScreen, int * resolution, int * colorDepth, int * refreshRate)
2229 *fullScreen = fullScreenMode;
2232 void EnsureFullScreen(bool *fullScreen)
2237 bool ScreenMode(bool fullScreen, int resolution, int colorDepth, int refreshRate, bool * textMode)
2241 fullScreenMode = fullScreen;
2249 static bool firstTime = true;
2251 desktopX = desktopY = desktopW = desktopH = 0;
2253 RepositionDesktop(false);
2258 // --- Window Creation ---
2259 void * CreateRootWindow(Window window)
2261 X11Window windowHandle;
2262 XSetWindowAttributes attributes = { 0 };
2263 XVisualInfo * visualInfo = null;
2267 unsigned long mask = EVENT_MASK;
2269 // Old WM (e.g. TWM), use built-in decorations
2270 if(!atomsSupported[_net_wm_state])
2271 window.nativeDecorations = false;
2272 attributes.override_redirect = (window.interim || (!atomsSupported[_net_wm_state] && !window.nativeDecorations)) ? True : False;
2273 attributes.event_mask = EVENT_MASK;
2274 //printf("%s\n", guiApp.defaultDisplayDriver);
2275 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
2276 if(window.dispDriver == class(OpenGLDisplayDriver) || !strcmp(guiApp.defaultDisplayDriver, "OpenGL"))
2280 for(samples = 4;; samples /= 2)
2285 GLX_RENDER_TYPE, GLX_RGBA_BIT,
2286 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
2287 GLX_DOUBLEBUFFER, True,
2293 int numAttribs = 14;
2295 GLXFBConfig *fbconfigs = null, fbconfig;
2298 //printf("Samples = %d, alpha = %d\n", samples, alpha);
2301 attrib[numAttribs++] = GLX_ALPHA_SIZE;
2302 attrib[numAttribs++] = 1;
2306 attrib[numAttribs++] = GLX_SAMPLE_BUFFERS_ARB;
2307 attrib[numAttribs++] = GL_TRUE;
2308 attrib[numAttribs++] = GLX_SAMPLES_ARB;
2309 attrib[numAttribs++] = samples;
2311 attrib[numAttribs] = None;
2313 // visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib);
2315 //printf("Trying %d samples...\n", samples);
2316 fbconfigs = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &numfbconfigs);
2319 for (i = 0; i < numfbconfigs; i++)
2321 XRenderPictFormat * format;
2322 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfigs[i]);
2323 if (!visualInfo) continue;
2324 if(window.alphaBlend)
2326 format = XRenderFindVisualFormat(xGlobalDisplay, visualInfo->visual);
2327 if (!format) { XFree(visualInfo); continue; }
2328 if(format->direct.alphaMask > 0)
2330 //printf("Found what we're looking for (alphaBlend)\n");
2331 fbconfig = fbconfigs[i];
2338 //printf("Found what we're looking for\n");
2343 if (i == numfbconfigs)
2345 fbconfig = fbconfigs[0];
2346 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfig);
2351 if(found || (!samples && !alpha))
2353 //printf("Stopping now\n");
2358 if(samples == 1) samples = 0;
2359 else if(!samples) alpha = false;
2366 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
2368 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
2372 visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrList);
2375 depth = visualInfo ? visualInfo->depth : (window.alphaBlend ? 32 : xSystemDepth);
2376 visual = visualInfo ? visualInfo->visual : (window.alphaBlend ? FindFullColorVisual (xGlobalDisplay, &depth) : xSystemVisual);
2377 // printf("visual: %d, depth: %d\n", visual, depth);
2381 attributes.colormap = XCreateColormap(xGlobalDisplay, XRootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), visual, AllocNone);
2382 attributes.border_pixel = 0;
2389 windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
2390 0,0,guiApp.desktop.size.w,guiApp.desktop.size.h,0, depth, InputOutput, visual ? visual : CopyFromParent,
2391 CWEventMask | (visual ? (CWColormap | CWBorderPixel) : 0)/*| CWOverrideRedirect*/, &attributes);
2394 Unsupported for now...
2395 else if(window.systemParent)
2402 if(window.windowHandle)
2403 windowHandle = (X11Window)window.windowHandle;
2406 X11Window parentWindow = (X11Window)null;
2407 int x = window.position.x + desktopX, y = window.position.y + desktopY;
2408 int w = window.state == normal ? Max(1, window.size.w) : Max(1, window.normalSizeAnchor.size.w);
2409 int h = window.state == normal ? Max(1, window.size.h) : Max(1, window.normalSizeAnchor.size.h);
2410 MinMaxValue smw = 0, smh = 0;
2411 MinMaxValue minW = window.minSize.w, minH = window.minSize.h;
2412 window.OnResizing((int *)&minW, (int *)&minH);
2414 // To fix jumping message boxes on Cinnamon:
2415 if(window.state == normal && (minW > window.minSize.w || minH > window.minSize.w))
2416 window.ComputeAnchors(window.normalAnchor, window.normalSizeAnchor, &x, &y, &w, &h);
2418 window.SetWindowMinimum(&smw, &smh);
2419 minW = Max(minW, smw);
2420 minH = Max(minH, smh);
2422 if(!window.nativeDecorations && window.state != normal)
2424 w += window.size.w - window.clientSize.w;
2425 h += window.size.h - window.clientSize.h;
2428 if(window.master.rootWindow && window.master.rootWindow != guiApp.desktop && (window._isModal || window.style.interim))
2430 Window master = window.master;
2431 Window rootWindow = master.rootWindow;
2433 parentWindow = rootWindow.is3D ? (X11Window)rootWindow.parent.windowHandle : (X11Window)rootWindow.windowHandle;
2435 // parentWindow = window.master.rootWindow.is3D ? window.master.rootWindow.parent.windowHandle : window.master.rootWindow.windowHandle;
2437 if(window.style.showInTaskBar)
2438 parentWindow = (X11Window)null;
2440 windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
2442 0, depth, InputOutput, visual ? visual : CopyFromParent,
2443 CWEventMask | CWOverrideRedirect | (visual ? (CWColormap | CWBorderPixel) : 0), &attributes);
2445 if(parentWindow && (window.interim || window.isModal))
2447 //printf("Setting WM_TRANSIENT_FOR of %s to %s\n", window._class.name, window.master.rootWindow._class.name);
2448 XSetTransientForHint(xGlobalDisplay, windowHandle, parentWindow);
2449 //XFlush(xGlobalDisplay);
2450 //printf("Done.\n");
2451 //XChangeProperty(xGlobalDisplay, windowHandle, atoms[wm_transient_for], XA_WINDOW, 32, PropModeReplace, (unsigned char*)&parentWindow, 1);
2453 SetNETWMState(windowHandle, false, add, atoms[_net_wm_state_modal], 0);
2459 if(parentWindow && window.interim)
2461 hints[0] = atoms[_net_wm_window_type_dropdown_menu];
2462 hints[1] = atoms[_net_wm_window_type_popup_menu];
2463 hints[2] = atoms[_net_wm_window_type_menu];
2466 else if(parentWindow)
2468 hints[0] = atoms[_net_wm_window_type_normal];
2470 // Some WMs won't show a close button if dialog is set
2471 // Additionally, this casues jumping of all dialog windows on Cinnamon
2472 //hints[0] = atoms[_net_wm_window_type_dialog];
2477 hints[0] = atoms[_net_wm_window_type_normal];
2480 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
2481 PropModeReplace, (unsigned char*)&hints, count);
2485 xwmHints.flags = InputHint;
2487 XSetWMHints(xGlobalDisplay, windowHandle, &xwmHints);
2490 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
2491 XSetWMProtocols(xGlobalDisplay, windowHandle, protocols, 2);
2494 // Set Normal hints for minimum/maximum size
2497 XSizeHints hints = { 0 };
2498 hints.min_width = minW;
2499 hints.min_height = minH;
2500 hints.flags |= PMinSize;
2502 if(window.maxSize.w < MAXINT || window.minSize.h < MAXINT)
2504 hints.max_width = window.maxSize.w;
2505 hints.max_height = window.maxSize.h;
2506 hints.flags |= PMaxSize;
2510 hints.flags |= PPosition;
2514 hints.flags |= PSize;
2516 XSetWMNormalHints(xGlobalDisplay, windowHandle, &hints);
2524 char fontString[1024] = "--helvetica-*-r-*-*-*-120-*-*-*-*-*-*,-misc-fixed-*-r-*-*-*-130-*-*-*-*-*-*";
2525 XPoint cursor_location = { 0, 0 };
2526 char **missing_charsets;
2527 int num_missing_charsets = 0;
2528 char *default_string;
2530 XRectangle area = { 0, 0, 400, 400 };
2531 XVaNestedList argList;
2533 // sprintf(fontString, "-*-%s-*-r-*-*-*-%d-*-*-*-*-*-*", "Helvetica" /*window.font.faceName*/, (int)(window.font.size * 20));
2534 fontset = XCreateFontSet(xGlobalDisplay, fontString, &missing_charsets, &num_missing_charsets, &default_string);
2535 argList = XVaCreateNestedList(0,
2536 XNSpotLocation, &cursor_location,
2538 XNFontSet, fontset,/*
2540 WhitePixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),
2542 BlackPixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),*/
2544 ic = XCreateIC(im, XNInputStyle,
2545 XIMStatusNothing | XIMPreeditPosition,
2546 XNPreeditAttributes, argList,
2547 XNClientWindow, windowHandle, XNFocusWindow, windowHandle, NULL);
2549 // Should we free the fontset or not?
2550 // XFreeFontSet(xGlobalDisplay, fontset);
2552 setICPosition = true;
2554 ic = XCreateIC(im, XNInputStyle, XIMStatusNothing | XIMPreeditNothing, XNClientWindow, windowHandle, XNFocusWindow, windowHandle, null);
2558 XGetICValues(ic, XNFilterEvents, &mask, NULL);
2562 XSelectInput(xGlobalDisplay, windowHandle, mask);
2564 if(capturedWindow == None && !restrictedWindow)
2566 XGrabPointer(xGlobalDisplay, (X11Window)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2567 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2568 XUngrabPointer(xGlobalDisplay, CurrentTime);
2573 if ( atoms[_motif_wm_hints] != None )
2577 (window.nativeDecorations ? 0 : MWM_HINTS_DECORATIONS)|MWM_HINTS_FUNCTIONS,
2578 (window.hasClose ? MWM_FUNC_CLOSE : 0) |
2579 (window.hasMaximize ? MWM_FUNC_MAXIMIZE : 0) |
2580 (window.hasMinimize ? MWM_FUNC_MINIMIZE : 0) |
2581 ((window.moveable || ((BorderBits)window.borderStyle).fixed) ? MWM_FUNC_MOVE : 0) |
2582 (((BorderBits)window.borderStyle).sizable ? MWM_FUNC_RESIZE : 0),
2585 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
2586 PropModeReplace, (unsigned char*)&hints, 5);
2589 // *** We set this for ourselves, so don't check atomsSupported !!! ***
2590 if(atoms[_net_wm_pid])
2593 // printf("Setting _NET_WM_PID to %d\n", pid);
2594 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_pid], XA_CARDINAL, 32,
2595 PropModeReplace, (unsigned char*)&pid, 1);
2601 Atom protocolsAtom = XInternAtom(xGlobalDisplay, "WM_PROTOCOLS", False);
2602 if ( protocolsAtom != None )
2604 MWM_Hints hints = { MWM_HINTS_DECORATIONS|MWM_HINTS_FUNCTIONS, 0, 0, 0, 0 };
2605 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
2606 PropModeReplace, (unsigned char*)&hints, sizeof(hints)/4);
2609 // XFlush(xGlobalDisplay);
2610 window.windowData = XWindowData { visualInfo, ic };
2612 XSaveContext(xGlobalDisplay, windowHandle, windowContext, (XPointer)window);
2614 XSelectInput(xGlobalDisplay, windowHandle, mask);
2616 if(capturedWindow == None && !restrictedWindow)
2618 XGrabPointer(xGlobalDisplay, (X11Window)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2619 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2620 XUngrabPointer(xGlobalDisplay, CurrentTime);
2623 if(!window.nativeDecorations || !RequestFrameExtents(windowHandle))
2624 ((XWindowData)window.windowData).gotFrameExtents = true;
2626 return (void *)windowHandle;
2629 void DestroyRootWindow(Window window)
2633 XDeleteContext(xGlobalDisplay, (XID)window, windowContext);
2634 XSaveContext(xGlobalDisplay, (X11Window)window.windowHandle, windowContext, null);
2635 XDestroyWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2636 XSync(xGlobalDisplay, 0);
2637 while(XCheckWindowEvent(xGlobalDisplay, (X11Window)window.windowHandle, 0xFFFFFFFF, &event));
2638 window.windowHandle = null;
2639 if(window.windowData)
2641 XWindowData windowData = window.windowData;
2642 XFree(windowData.visual);
2643 if(windowData && windowData.ic)
2644 XDestroyIC(windowData.ic);
2646 // printf("Setting windowData for %s to null\n", window._class.name);
2647 window.windowData = null;
2649 if(lastActive == window)
2653 // -- Window manipulation ---
2655 void SetRootWindowCaption(Window window, char * name)
2657 if(window.windowHandle)
2659 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_name],
2660 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2661 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[wm_name],
2662 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2666 void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
2668 //Logf("Position root window %s\n", window.name);
2669 if(window.windowHandle && (!window.parent || !window.parent.display))
2671 bool visible = window.visible;
2672 if(window.visible && window.created)
2673 XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2674 if(window.state == minimized && atomsSupported[_net_wm_state]) return;
2676 if(window.nativeDecorations)
2678 XWindowData windowData = window.windowData;
2680 // Was commenting this out was part of #700/#795 fix, but this causes jumping of e.g. About box after getting frame extents PropertyNotify
2682 // && window.state != maximized -- required for Cinnamon on Mint 14/15
2683 if(!windowData.gotFrameExtents && window.state != maximized)
2685 if(WaitForFrameExtents(window))
2687 x += windowData.decor.left;
2688 y += windowData.decor.top ;
2690 w += windowData.decor.left + windowData.decor.right;
2691 h += windowData.decor.top + windowData.decor.bottom;
2695 x -= windowData.decor.left;
2696 y -= windowData.decor.top;
2698 w -= windowData.decor.left + windowData.decor.right;
2699 h -= windowData.decor.top + windowData.decor.bottom;
2701 // Tweak for first unmaximize on Unity on Ubuntu 11.10
2702 /*if(window.state == maximized && (desktopX + w > desktopW || desktopY + h > desktopH))
2712 if(!atomsSupported[_net_wm_state] || window.state != maximized)
2715 XMoveResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, x, y, w, h);
2717 XMoveWindow(xGlobalDisplay, (X11Window)window.windowHandle, x, y);
2719 XResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, w, h);
2721 // Reset min/max for fixed size windows on WMs not looking at MWM_FUNC_RESIZE (e.g. Cinnamon)
2722 if(window.style.fixed && !window.style.sizable)
2724 XSizeHints hints = { 0 };
2725 hints.min_width = hints.max_width = w;
2726 hints.min_height = hints.max_height = h;
2727 hints.flags |= PMinSize|PMaxSize|PPosition|PSize;
2728 XSetWMNormalHints(xGlobalDisplay, (X11Window)window.windowHandle, &hints);
2731 #if defined(__APPLE__)
2732 // if(window.created && !visible)
2733 // XUnmapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2738 void OrderRootWindow(Window window, bool topMost)
2743 void SetRootWindowColor(Window window)
2748 void OffsetWindow(Window window, int * x, int * y)
2753 void UpdateRootWindow(Window window)
2755 if(!window.parent || !window.parent.display)
2759 Box box = window.box;
2760 box.left -= window.clientStart.x;
2761 box.top -= window.clientStart.y;
2762 box.right -= window.clientStart.x;
2763 box.bottom -= window.clientStart.y;
2764 // Logf("Update root window %s\n", window.name);
2765 window.Update(null);
2766 box.left += window.clientStart.x;
2767 box.top += window.clientStart.y;
2768 box.right += window.clientStart.x;
2769 box.bottom += window.clientStart.y;
2770 window.UpdateDirty(box);
2775 void SetRootWindowState(Window window, WindowState state, bool visible)
2777 // Old WM (e.g. TWM), use built-in decorations
2778 if(!atomsSupported[_net_wm_state])
2779 window.nativeDecorations = false;
2780 if(!window.parent || !window.parent.display)
2782 //Logf("Set root window state %d %s\n", state, window.name);
2785 XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2786 WaitForViewableWindow(window);
2787 if(window.creationActivation == activate && state != minimized)
2788 ActivateRootWindow(window);
2790 if(state == minimized && atomsSupported[_net_wm_state])
2792 uint iconic = IconicState;
2794 // SetNETWMState(window.windowHandle, true, add, atoms[_net_wm_state_hidden], null);
2796 XChangeProperty(xGlobalDisplay, window.windowHandle, atoms[wm_state], XA_CARDINAL, 32,
2797 PropModeReplace, &iconic, 1);
2801 XClientMessageEvent event = { 0 };
2802 event.type = ClientMessage;
2803 event.message_type = atoms[wm_state];
2804 event.display = xGlobalDisplay;
2805 event.window = window.windowHandle;
2806 event.send_event = 1;
2808 event.data.l[0] = IconicState;
2809 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, &event);
2812 // printf("Attempting to minimize %s\n", window._class.name);
2813 XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
2817 //((XWindowData)window.windowData).gotFrameExtents && (!window.nativeDecorations || window.state == state))
2818 if(!atomsSupported[_net_wm_state] || (!((XWindowData)window.windowData).gotFrameExtents && window.state == maximized))
2820 // Running this block avoids the initial IDE maximized->unmaximized flicker
2821 //if(window.state != maximized || !atomsSupported[_net_wm_state] || window.nativeDecorations)
2823 int x = window.position.x;
2824 int y = window.position.y;
2825 int w = window.size.w;
2826 int h = window.size.h;
2828 if(window.nativeDecorations)
2830 XWindowData windowData = window.windowData;
2831 x -= windowData.decor.left;
2832 y -= windowData.decor.top;
2834 w -= windowData.decor.left + windowData.decor.right;
2835 h -= windowData.decor.top + windowData.decor.bottom;
2840 XMoveResizeWindow(xGlobalDisplay,
2841 (X11Window)window.windowHandle,
2844 UpdateRootWindow(window);
2846 if(atomsSupported[_net_wm_state])
2848 // Maximize / Restore the window
2849 SetNETWMState((X11Window)window.windowHandle, true, state == maximized ? add: remove,
2850 atoms[_net_wm_state_maximized_vert], atoms[_net_wm_state_maximized_horz]);
2851 if(state == maximized)
2853 // Prevent the code in ConfigureNotify to think the window has been unmaximized
2854 // if the Window Manager hasn't set the hints yet.
2855 XFlush(xGlobalDisplay);
2862 XUnmapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2863 //XFlush(xGlobalDisplay);
2867 void FlashRootWindow(Window window)
2869 // printf("Attempting to flash root window\n");
2870 SetNETWMState((X11Window)window.windowHandle, true, add, atoms[_net_wm_state_demands_attention], 0);
2873 void ActivateRootWindow(Window window)
2875 if(!window.parent || !window.parent.display)
2877 if(!window.style.hidden && window.created)
2879 //printf("Activate root window %s\n", window._class.name);
2880 XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2881 XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
2882 WaitForViewableWindow(window);
2883 if(atomsSupported[_net_active_window])
2885 XClientMessageEvent event = { 0 };
2886 event.type = ClientMessage;
2887 event.message_type = atoms[_net_active_window];
2888 event.display = xGlobalDisplay;
2890 event.window = (X11Window)window.windowHandle;
2891 event.send_event = 1;
2893 event.data.l[0] = /*0*/ 1;
2894 event.data.l[1] = timeStamp;
2895 event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
2898 //printf("(ActivateRootWindow) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, window);
2901 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2902 //#if defined(__APPLE__)
2903 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
2907 XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
2912 // --- Mouse-based window movement ---
2914 void StartMoving(Window window, int x, int y, bool fromKeyBoard)
2919 void StopMoving(Window window)
2924 // -- Mouse manipulation ---
2926 void GetMousePosition(int *x, int *y)
2928 X11Window rootWindow, childWindow;
2931 ((GuiApplication)__thisModule.application).Lock();
2932 //XLockDisplay(xGlobalDisplay);
2933 XQueryPointer(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &childWindow,
2934 &rootWindow, x, y, &mx, &my, &state);
2935 //XUnlockDisplay(xGlobalDisplay);
2936 ((GuiApplication)__thisModule.application).Unlock();
2939 void SetMousePosition(int x, int y)
2944 void SetMouseRange(Window window, Box box)
2946 ((GuiApplication)__thisModule.application).Lock();
2947 //XLockDisplay(xGlobalDisplay);
2948 if(box && box.left > 0 && box.top > 0 &&
2949 box.right < guiApp.desktop.clientSize.w - 1 && box.bottom < guiApp.desktop.clientSize.h - 1)
2951 if(!window.parent || !window.parent.display)
2953 XMoveResizeWindow(xGlobalDisplay, confineWindow, box.left /*+ desktopX*/, box.top /*+ desktopY*/,
2954 box.right - box.left + 1, box.bottom - box.top + 1);
2956 if(!restrictedWindow)
2957 XMapWindow(xGlobalDisplay, confineWindow);
2959 XGrabPointer(xGlobalDisplay, (X11Window) window.rootWindow.windowHandle, False,
2960 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2961 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
2963 restrictedWindow = window;
2966 else if(restrictedWindow)
2968 if(capturedWindow != None)
2970 XGrabPointer(xGlobalDisplay, (X11Window)capturedWindow,
2971 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2972 GrabModeAsync, None, fullScreenMode ? nullCursor : None, CurrentTime);
2975 XUngrabPointer(xGlobalDisplay, CurrentTime);
2977 if(restrictedWindow)
2978 XUnmapWindow(xGlobalDisplay, confineWindow);
2980 restrictedWindow = null;
2982 //XUnlockDisplay(xGlobalDisplay);
2983 ((GuiApplication)__thisModule.application).Unlock();
2986 void SetMouseCapture(Window window)
2988 //*XLockDisplay(xGlobalDisplay);
2991 if(!window.parent || !window.parent.display)
2993 XGrabPointer(xGlobalDisplay, (X11Window)window.windowHandle,
2994 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2995 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2997 capturedWindow = (X11Window) window.windowHandle;
3000 else if(capturedWindow != None)
3002 if(restrictedWindow)
3003 XGrabPointer(xGlobalDisplay, (X11Window) restrictedWindow.rootWindow.windowHandle, False,
3004 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
3005 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
3007 XUngrabPointer(xGlobalDisplay, CurrentTime);
3008 capturedWindow = None;
3010 //*XUnlockDisplay(xGlobalDisplay);
3013 // -- Mouse cursor ---
3015 void SetMouseCursor(Window window, int cursor)
3017 if(window.rootWindow.windowHandle)
3018 XDefineCursor(xGlobalDisplay, (X11Window) window.rootWindow.windowHandle,
3019 cursor == -1 ? (X11Cursor)0 : systemCursors[(SystemCursor)cursor]);
3024 void SetCaret(int x, int y, int size)
3026 Window caretOwner = guiApp.caretOwner;
3027 Window window = caretOwner ? caretOwner.rootWindow : null;
3028 if(window && window.windowData && setICPosition)
3030 XWindowData windowData = window.windowData;
3031 if(windowData && windowData.ic)
3033 XPoint cursor_location =
3035 (short)(caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x),
3036 (short)(caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y)
3038 XVaNestedList argList = XVaCreateNestedList(0, XNSpotLocation, &cursor_location, NULL);
3039 XSetICValues(windowData.ic, XNPreeditAttributes, argList, 0);
3044 void ClearClipboard()
3046 //*XLockDisplay(xGlobalDisplay);
3049 delete clipBoardData;
3050 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard], None, CurrentTime);
3052 //*XUnlockDisplay(xGlobalDisplay);
3055 bool AllocateClipboard(ClipBoard clipBoard, uint size)
3057 bool result = false;
3058 if((clipBoard.text = new0 byte[size]))
3063 bool SaveClipboard(ClipBoard clipBoard)
3065 bool result = false;
3066 //*XLockDisplay(xGlobalDisplay);
3069 Window rootWindow = guiApp.desktop;
3072 for(rootWindow = rootWindow.children.first;
3073 rootWindow && !rootWindow.windowHandle;
3074 rootWindow = rootWindow.next);
3077 delete clipBoardData;
3079 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard],
3080 (X11Window) rootWindow.windowHandle, CurrentTime);
3081 clipBoardData = clipBoard.text;
3082 clipBoard.text = null;
3085 //*XUnlockDisplay(xGlobalDisplay);
3089 bool LoadClipboard(ClipBoard clipBoard)
3091 bool result = false;
3093 //*XLockDisplay(xGlobalDisplay);
3094 // The data is inside this client...
3097 clipBoard.text = new char[strlen(clipBoardData)+1];
3098 strcpy(clipBoard.text, clipBoardData);
3101 // The data is with another client...
3104 Window rootWindow = guiApp.desktop;
3107 for(rootWindow = rootWindow.children.first;
3108 rootWindow && !rootWindow.windowHandle;
3109 rootWindow = rootWindow.next);
3113 Atom selAtom = atoms[clipboard];
3114 X11Window owner = XGetSelectionOwner(xGlobalDisplay, selAtom);
3118 for(atom = atoms[utf8_string]; atom; atom = ((atom == atoms[utf8_string]) ? XA_STRING : 0))
3121 XConvertSelection(xGlobalDisplay, selAtom, atom, atoms[app_selection] /*None*/, (X11Window) rootWindow.windowHandle, CurrentTime);
3122 XIfEvent(xGlobalDisplay, (XEvent *) &e, EventChecker, (void *)SelectionNotify);
3123 if(e.type == SelectionNotify)
3125 XSelectionEvent * selection = (XSelectionEvent *) &e;
3126 //printf("Got a SelectionNotify with %d (%s)\n", selection->_property, XGetAtomName(xGlobalDisplay, selection->_property));
3128 unsigned long len, size = 0, dummy;
3131 XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, 0, False, AnyPropertyType, &type, &format, &len, &size, &data);
3139 if(XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, size, False,
3140 AnyPropertyType, &type,&format,&len, &dummy, &data) == Success)
3142 clipBoard.text = new char[size+1];
3143 strncpy(clipBoard.text, data, size);
3144 clipBoard.text[size] = '\0';
3155 //*XUnlockDisplay(xGlobalDisplay);
3159 void UnloadClipboard(ClipBoard clipBoard)
3161 delete clipBoard.text;
3164 // --- State based input ---
3166 bool AcquireInput(Window window, bool state)
3171 bool GetMouseState(MouseButtons * buttons, int * x, int * y)
3173 bool result = false;
3179 bool GetJoystickState(int device, Joystick joystick)
3181 bool result = false;
3182 #if defined(__linux__)
3183 if(joystick && device < 4)
3185 struct JS_DATA_TYPE js = { 0 };
3186 memset(joystick, 0, sizeof(Joystick));
3187 if(joystickFD[device] && read(joystickFD[device], &js, JS_RETURN) == JS_RETURN)
3189 joystick.buttons = js.buttons;
3190 joystick.x = js.x - 128;
3191 joystick.y = js.y - 128;
3199 bool GetKeyState(Key key)
3205 void SetTimerResolution(uint hertz)
3207 timerDelay = hertz ? (1000000 / hertz) : MAXINT;
3212 hiResTimer.delay = 1000000 / hertz;
3213 hiResTimer.Create();
3218 bool SetIcon(Window window, BitmapResource resource)
3223 if(bitmap.Load(resource.fileName, null, null))
3225 unsigned long * icon = new unsigned long[2 + bitmap.width * bitmap.height];
3226 bitmap.Convert(null, pixelFormat888, null);
3227 icon[0] = bitmap.width;
3228 icon[1] = bitmap.height;
3229 if(sizeof(long) != sizeof(uint32))
3232 for(c = 0; c < bitmap.width * bitmap.height; c++)
3233 icon[c+2] = ((uint32 *)bitmap.picture)[c];
3236 memcpy(icon + 2, bitmap.picture, bitmap.width * bitmap.height * sizeof(uint32));
3237 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_icon],
3238 XA_CARDINAL,32,PropModeReplace, (byte *)icon, 2+bitmap.width*bitmap.height);
3247 default dllexport void * __attribute__((stdcall)) IS_XGetDisplay()
3249 return xGlobalDisplay;
3252 default dllexport void * __attribute__((stdcall)) IS_XGetWindow(Window window)
3254 return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;