1 namespace gui::drivers;
5 import "OpenGLDisplayDriver"
8 #if (defined(__unix__) || defined(__APPLE__)) && !defined(ECERE_MINIGLX)
12 #define property _property
17 #include <linux/joystick.h>
19 #include <sys/param.h>
31 #define Window X11Window
32 #define Cursor X11Cursor
34 #define Display X11Display
36 #define KeyCode X11KeyCode
37 #define Picture X11Picture
39 #include <X11/Xatom.h>
41 #include <X11/Xresource.h>
42 #include <X11/Xutil.h>
43 #include <X11/XKBlib.h>
44 #include <X11/keysym.h>
45 #include <sys/fcntl.h>
47 #include <X11/extensions/Xrender.h>
48 #include <X11/extensions/XShm.h>
65 import "XDisplayDriver"
67 // These are the events we care about
68 #define EVENT_MASK StructureNotifyMask | \
69 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | \
70 KeyPressMask | KeyReleaseMask | ExposureMask | FocusChangeMask
72 void * xGlobalDisplay;
75 static XContext windowContext;
76 static Window lastActive;
78 static char * clipBoardData;
80 static Thread /* xThread, */ timerThread;
81 static bool xTerminate;
82 static Semaphore xSemaphore { };
83 static Mutex xMutex { };
84 static bool fullScreenMode;
85 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
87 static int nullCursor;
88 static X11Window capturedWindow = None;
89 static Window restrictedWindow = null;
90 static bool gotAnXEvent = false;
92 static int joystickFD[4];
93 static X11Window activeWindow;
95 static enum AtomIdents {clipboard, multiple, targets, utf8_string, wm_delete_window, wm_hints, wm_name, wm_protocols, wm_state, wm_take_focus, wm_transient_for, _motif_wm_hints, _net_active_window, _net_current_desktop, _net_number_of_desktops, _net_wm_icon, _net_wm_name, _net_wm_pid, _net_wm_state, _net_wm_state_demands_attention, _net_wm_state_hidden, _net_wm_user_time, _net_wm_window_type, _net_wm_window_type_desktop, _net_wm_window_type_dialog, _net_wm_window_type_dock, _net_wm_window_type_dropdown_menu, _net_wm_window_type_menu, _net_wm_window_type_normal, _net_wm_window_type_popup_menu, _net_wm_window_type_splash, _net_wm_window_type_toolbar, _net_wm_window_type_utility, _net_workarea, app_selection };
97 static Atom atoms[AtomIdents];
99 static const char *atomNames[AtomIdents] = {
100 "CLIPBOARD", //clipboard
101 "MULTIPLE", //multiple
103 "UTF8_STRING", //utf8_string
104 "WM_DELETE_WINDOW", //wm_delete_window
105 "WM_HINTS", //wm_hints
107 "WM_PROTOCOLS", //wm_protocols
108 "WM_STATE", //wm_state
109 "WM_TAKE_FOCUS", //wm_take_focus
110 "WM_TRANSIENT_FOR", //wm_transient_for
111 "_MOTIF_WM_HINTS", //_motif_wm_hints
112 "_NET_ACTIVE_WINDOW", //_net_active_window
113 "_NET_CURRENT_DESKTOP", //_net_current_desktop
114 "_NET_NUMBER_OF_DESKTOPS", //_net_number_of_desktops
115 "_NET_WM_ICON", //_net_wm_icon
116 "_NET_WM_NAME", //_net_wm_name
117 "_NET_WM_PID", //_net_wm_pid
118 "_NET_WM_STATE", //_net_wm_state
119 "_NET_WM_STATE_DEMANDS_ATTENTION", //_net_wm_state_demands_attention
120 "_NET_WM_STATE_HIDDEN", //_net_wm_state_hidden
121 "_NET_WM_USER_TIME", //_net_wm_user_time
122 "_NET_WM_WINDOW_TYPE", //_net_wm_window_type
123 "_NET_WM_WINDOW_TYPE_DESKTOP", //_net_wm_window_type_desktop
124 "_NET_WM_WINDOW_TYPE_DIALOG", //_net_wm_window_type_dialog
125 "_NET_WM_WINDOW_TYPE_DOCK", //_net_wm_window_type_dock
126 "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", //_net_wm_window_type_dropdown_menu
127 "_NET_WM_WINDOW_TYPE_MENU", //_net_wm_window_type_menu
128 "_NET_WM_WINDOW_TYPE_NORMAL", //_net_wm_window_type_normal
129 "_NET_WM_WINDOW_TYPE_POPUP_MENU", //_net_wm_window_type_popup_menu
130 "_NET_WM_WINDOW_TYPE_SPLASH", //_net_wm_window_type_splash
131 "_NET_WM_WINDOW_TYPE_TOOLBAR", //_net_wm_window_type_toolbar
132 "_NET_WM_WINDOW_TYPE_UTILITY", //_net_wm_window_type_utility
133 "_NET_WORKAREA", //_net_workarea
137 static bool autoRepeatDetectable;
138 static bool setICPosition;
140 PixelFormat xSystemPixelFormat;
141 Visual * xSystemVisual;
144 static Time timeStamp;
146 class XWindowData : struct
149 XVisualInfo * visual;
154 static Visual * FindFullColorVisual(X11Display *dpy, int * depth)
157 XVisualInfo *vinfo_ret;
160 vinfo._class = TrueColor;
161 vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
167 if(vinfo_ret[numitems-1].depth > maxdepth)
168 maxdepth = vinfo_ret[numitems-1].depth;
171 XFree((void *) vinfo_ret);
174 if(XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, TrueColor, &vinfo))
184 _NET_WM_STATE_MODAL, ATOM
185 _NET_WM_STATE_STICKY, ATOM
186 _NET_WM_STATE_MAXIMIZED_VERT, ATOM
187 _NET_WM_STATE_MAXIMIZED_HORZ, ATOM
188 _NET_WM_STATE_SHADED, ATOM
189 _NET_WM_STATE_SKIP_TASKBAR, ATOM
190 _NET_WM_STATE_SKIP_PAGER, ATOM
191 _NET_WM_STATE_HIDDEN, ATOM
192 _NET_WM_STATE_FULLSCREEN, ATOM
193 _NET_WM_STATE_ABOVE, ATOM
194 _NET_WM_STATE_BELOW, ATOM
195 _NET_WM_STATE_DEMANDS_ATTENTION, ATOM
199 static void RepositionDesktop(bool updateChildren)
203 Screen * x_screen = XDefaultScreenOfDisplay(xGlobalDisplay);
207 int format, len, fill;
210 w = XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
211 h = XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
212 x_root = XRootWindowOfScreen(x_screen);
214 if(atoms[_net_number_of_desktops] != None)
216 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_number_of_desktops], 0, 1, False,
217 XA_CARDINAL, &type, &format, &len, &fill,
220 printf("cant get xa desktops property\n");
226 desktops = *(int *)data;
228 //printf("_NET_NUMBER_OF_DESKTOPS is %d\n", desktops);
235 if(atoms[_net_current_desktop] != None)
237 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_current_desktop], 0, 1, False,
238 XA_CARDINAL, &type, &format, &len, &fill,
241 printf("cant get xa current property\n");
246 current = *(int *)data;
250 //printf("_NET_CURRENT_DESKTOP is %d\n", current);
253 if(atoms[_net_workarea] != None)
257 if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_workarea], 0, (4 * 32),
258 False, AnyPropertyType, &type, &format, &len,
259 &fill, &data) != Success)
261 //printf("warning\n");
265 if(type == None || format == 0)
277 workareas = (int *)data;
279 x = workareas[current * 4];
280 y = workareas[current * 4 + 1];
281 w = workareas[current * 4 + 2];
282 h = workareas[current * 4 + 3];
284 //printf("_NET_WORKAREA is x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
286 // printf("Work Area width: %d, height %d\n", w, h);
289 if(desktopX != x || desktopY != y || desktopW != w || desktopH != h)
291 guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
299 /****************************************************************************
300 /// PRIVATE UTILITY FUNCTIONS /////////////
301 ****************************************************************************/
302 // --- Keyboard Input ---
303 #define KEYCODE_HOME 0x59
304 #define KEYCODE_UP 0x5A
305 #define KEYCODE_PGUP 0x5B
306 #define KEYCODE_LEFT 0x5C
307 #define KEYCODE_RIGHT 0x5E
308 #define KEYCODE_END 0x5F
309 #define KEYCODE_DOWN 0x60
310 #define KEYCODE_PGDWN 0x61
311 #define KEYCODE_INS 0x62
312 #define KEYCODE_DEL 0x63
313 #define KEYCODE_SLASH 0x68
316 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
317 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
318 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
319 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
320 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
321 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
322 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
323 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
324 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
325 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
326 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
327 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
328 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
329 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
332 static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEvent * event)
337 KeySym keysym = NoSymbol;
340 static int bufsize = 16;
341 static char *buf = NULL;
342 XWindowData windowData = window.windowData;
345 if(window.destroyed) return;
347 // Logf("Got 0x%x (%d)\n", keyCode, key);
349 // Translate the key code
352 //Logf("Got 0x%x (%d)\n", key, key);
356 case KEYCODE_HOME: key = home; break;
357 case KEYCODE_UP: key = up; break;
358 case KEYCODE_PGUP: key = pageUp; break;
359 case KEYCODE_LEFT: key = left; break;
360 case KEYCODE_RIGHT: key = right; break;
361 case KEYCODE_END: key = end; break;
362 case KEYCODE_DOWN: key = down; break;
363 case KEYCODE_PGDWN: key = pageDown; break;
364 case KEYCODE_INS: key = insert; break;
365 case KEYCODE_DEL: key = del; break;
366 case KEYCODE_SLASH: key = keyPadSlash; break;
369 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
373 Logf("Key up: %3d (%2x)\n", key, key);
375 Logf("Key down: %3d (%2x)\n", key, key);
379 buf = malloc(bufsize);
380 if(windowData && windowData.ic)
382 buflength = XmbLookupString(windowData.ic, event, buf, bufsize, &keysym, &status);
383 if (status == XBufferOverflow)
385 buf = realloc(buf, (bufsize = buflength));
386 buflength = XmbLookupString(windowData.ic, event, buf, bufsize, &keysym, &status);
388 if(status != XLookupKeySym && status != XLookupBoth && release == 1)
389 keysym = XLookupKeysym(event, 0);
393 keysym = XLookupKeysym(event, 0);
396 // printf("Got keysym %d (%x)\n", keysym, keysym);
397 if(keysym != NoSymbol) //status == XLookupKeySym || status == XLookupBoth)
401 case XK_BackSpace: key = backSpace; break;
402 case XK_ISO_Left_Tab: key = tab; break;
403 case XK_Tab: key = tab; break;
406 case XK_Return: key = enter; break;
407 case XK_Pause: key = pauseBreak; break;
408 case XK_Scroll_Lock: key = scrollLock; break;
409 case XK_Sys_Req: key = printScreen; break;
410 case XK_Escape: key = escape; break;
411 case XK_Delete: key = del; break;
413 case XK_Home: key = home; break;
414 case XK_Left: key = left; break;
415 case XK_Up: key = up; break;
416 case XK_Right: key = right; break;
417 case XK_Down: key = down; break;
419 case XK_Page_Up: key = pageUp; break;
421 case XK_Page_Down: key = pageDown; break;
422 case XK_End: key = end; break;
428 case XK_Insert: key = insert; break;
435 case XK_Help: key = insert; break;
439 case XK_Mode_switch: key = leftAlt; break;
441 // case XK_script_switch:
442 case XK_Num_Lock: key = numLock; break;
446 case XK_KP_Enter: key = keyPadEnter; break;
451 case XK_KP_Home: key = keyPadHome; break;
452 case XK_KP_Left: key = keyPadLeft; break;
453 case XK_KP_Up: key = keyPadUp; break;
454 case XK_KP_Right: key = keyPadRight; break;
455 case XK_KP_Down: key = keyPadDown; break;
457 case XK_KP_Page_Up: key = keyPadPageUp; break;
459 case XK_KP_Page_Down:key = keyPadPageDown; break;
460 case XK_KP_End: key = keyPadEnd; break;
462 case XK_KP_Insert: key = keyPadInsert; break;
463 case XK_KP_Delete: key = keyPadDelete; break;
465 case XK_KP_Multiply: key = keyPadStar; break;
466 case XK_KP_Add: key = keyPadPlus; break;
467 case XK_KP_Separator:key = keyPadDelete; break;
468 case XK_KP_Subtract: key = keyPadMinus; break;
469 // case XK_KP_Decimal:
470 case XK_KP_Divide: key = keyPadSlash; break;
472 case XK_KP_0: key = keyPad0; break;
473 case XK_KP_1: key = keyPad1; break;
474 case XK_KP_2: key = keyPad2; break;
475 case XK_KP_3: key = keyPad3; break;
476 case XK_KP_4: key = keyPad4; break;
477 case XK_KP_5: key = keyPad5; break;
478 case XK_KP_6: key = keyPad6; break;
479 case XK_KP_7: key = keyPad7; break;
480 case XK_KP_8: key = keyPad8; break;
481 case XK_KP_9: key = keyPad9; break;
483 case XK_F1: key = f1; break;
484 case XK_F2: key = f2; break;
485 case XK_F3: key = f3; break;
486 case XK_F4: key = f4; break;
487 case XK_F5: key = f5; break;
488 case XK_F6: key = f6; break;
489 case XK_F7: key = f7; break;
490 case XK_F8: key = f8; break;
491 case XK_F9: key = f9; break;
492 case XK_F10: key = f10; break;
493 case XK_F11: key = f11; break;
495 case XK_F12: key = f12; break;
545 case XK_Shift_L: key = leftShift; break;
546 case XK_Shift_R: key = rightShift; break;
547 case XK_Control_L: key = leftControl; break;
548 case XK_Control_R: key = rightControl; break;
549 case XK_Caps_Lock: key = capsLock; break;
550 // case XK_Shift_Lock:
553 case XK_Alt_L: key = leftAlt; break;
554 case XK_Alt_R: key = rightAlt; break;
560 case XK_space: key = space; break;
561 case XK_exclam: key = bang; break;
562 case XK_quotedbl: key = doubleQuote; break;
563 case XK_numbersign: key = pound; break;
564 case XK_dollar: key = dollar; break;
565 case XK_percent: key = percent; break;
566 case XK_ampersand: key = ampersand; break;
567 case XK_apostrophe: key = quote; break;
568 // case XK_quoteright: key = quote; break;
569 case XK_parenleft: key = leftParanthesis; break;
570 case XK_parenright: key = rightParanthesis; break;
571 case XK_asterisk: key = star; break;
572 case XK_plus: key = plus; break;
573 case XK_comma: key = comma; break;
574 case XK_minus: key = minus; break;
575 case XK_period: key = period; break;
576 case XK_slash: key = slash; break;
577 case XK_0: key = k0; break;
578 case XK_1: key = k1; break;
579 case XK_2: key = k2; break;
580 case XK_3: key = k3; break;
581 case XK_4: key = k4; break;
582 case XK_5: key = k5; break;
583 case XK_6: key = k6; break;
584 case XK_7: key = k7; break;
585 case XK_8: key = k8; break;
586 case XK_9: key = k9; break;
587 case XK_colon: key = colon; break;
588 case XK_semicolon: key = semicolon; break;
589 case XK_less: key = smallerThan; break;
590 case XK_equal: key = equal; break;
591 case XK_greater: key = greaterThan; break;
592 case XK_question: key = questionMark; break;
593 case XK_at: key = ampersand; break;
594 case XK_A: key = a; break;
595 case XK_B: key = b; break;
596 case XK_C: key = c; break;
597 case XK_D: key = d; break;
598 case XK_E: key = e; break;
599 case XK_F: key = f; break;
600 case XK_G: key = g; break;
601 case XK_H: key = h; break;
602 case XK_I: key = i; break;
603 case XK_J: key = j; break;
604 case XK_K: key = k; break;
605 case XK_L: key = l; break;
606 case XK_M: key = m; break;
607 case XK_N: key = n; break;
608 case XK_O: key = o; break;
609 case XK_P: key = p; break;
610 case XK_Q: key = q; break;
611 case XK_R: key = r; break;
612 case XK_S: key = s; break;
613 case XK_T: key = t; break;
614 case XK_U: key = u; break;
615 case XK_V: key = v; break;
616 case XK_W: key = w; break;
617 case XK_X: key = x; break;
618 case XK_Y: key = y; break;
619 case XK_Z: key = z; break;
620 case XK_bracketleft: key = leftBracket; break;
621 case XK_backslash: key = backSlash; break;
622 case XK_bracketright: key = rightBracket; break;
623 case XK_asciicircum: key = circumflex; break;
624 case XK_underscore: key = underscore; break;
625 // case XK_grave: key = backQuote; break;
626 case XK_quoteleft: key = backQuote; break;
627 case XK_a: key = a; break;
628 case XK_b: key = b; break;
629 case XK_c: key = c; break;
630 case XK_d: key = d; break;
631 case XK_e: key = e; break;
632 case XK_f: key = f; break;
633 case XK_g: key = g; break;
634 case XK_h: key = h; break;
635 case XK_i: key = i; break;
636 case XK_j: key = j; break;
637 case XK_k: key = k; break;
638 case XK_l: key = l; break;
639 case XK_m: key = m; break;
640 case XK_n: key = n; break;
641 case XK_o: key = o; break;
642 case XK_p: key = p; break;
643 case XK_q: key = q; break;
644 case XK_r: key = r; break;
645 case XK_s: key = s; break;
646 case XK_t: key = t; break;
647 case XK_u: key = u; break;
648 case XK_v: key = v; break;
649 case XK_w: key = w; break;
650 case XK_x: key = x; break;
651 case XK_y: key = y; break;
652 case XK_z: key = z; break;
653 case XK_braceleft: key = leftBracket; break;
654 case XK_bar: key = pipe; break;
655 case XK_braceright: key = rightBracket; break;
656 case XK_asciitilde: key = tilde; break;
661 ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
662 if(ch == 128) ch = 0;
665 if(keysym == XK_ISO_Left_Tab)
667 if(key != leftShift && key != rightShift && event->state & ShiftMask)
669 if(key != leftControl && key != rightControl && event->state & ControlMask)
671 if(key != leftAlt && key != rightAlt && event->state & Mod1Mask)
675 if(key != leftAlt && key != rightAlt && event->state & (1<<13))
683 // 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);
689 if(windowData && windowData.ic) ch = buflength ? UTF8GetChar(buf, &numBytes) : 0;
690 // printf("Release! %d %d %d\n", keysym, code, ch);
691 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, code, ch);
698 if(windowData.ic && buflength)
700 for(c = 0; c<buflength;)
703 ch = UTF8GetChar(buf + c, &numBytes);
704 if(!numBytes) c = buflength;
705 result = window.KeyMessage((c == 0) ?
706 __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown : __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit,
707 (c == 0) ? code : 0, ch);
712 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, code, ch);
716 if(buflength && windowData.ic)
717 for(c = 0; c<buflength;)
720 ch = UTF8GetChar(buf + c, &numBytes);
721 if(!numBytes) c = buflength;
722 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
726 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, ch);
734 static uint E_CALL XEventThread(void * data)
740 if(xTerminate) break;
743 XPeekEvent(xGlobalDisplay, &e);
745 guiApp.SignalEvent();
752 static Bool EventChecker(void *display, XEvent *event, char * data)
754 return (!data || (event->type == (int) data)) && event->type != NoExpose && event->type != GraphicsExpose;
757 static Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
759 return (!data || ((event->window == (int) data)) && event->type == ConfigureNotify;
762 static uint timerDelay = MAXINT;
763 #define RESOLUTION (18.2 * 100)
764 static uint XTimerThread(Thread thread)
766 int s = ConnectionNumber(xGlobalDisplay);
768 Time lastTime, thisTime;
769 lastTime = GetTime();
774 bool waitSemaphore = false;
775 fd_set readSet, writeSet, exceptSet;
776 struct timeval tv = { (timerDelay == MAXINT) ? 0 : (timerDelay / 1000000), (timerDelay == MAXINT) ? (int)(1000000 / 18.2) : (timerDelay % 1000000) };
778 if(xTerminate) break;
783 FD_SET(s, &exceptSet);
785 if(select(s + 1, &readSet, null, null, &tv))
787 if(FD_ISSET(s, &readSet))
791 guiApp.SignalEvent();
796 Sleep(1.0 / RESOLUTION);
797 thisTime = GetTime();
798 if(xTerminate) break;
799 if(thisTime - lastTime > (1.0 / 18.2))
801 guiApp.SignalEvent();
809 XLockDisplay(xGlobalDisplay);
810 if(XCheckIfEvent(xGlobalDisplay, &xEvent, EventChecker, null))
813 guiApp.SignalEvent();
815 XUnlockDisplay(xGlobalDisplay);
824 static int MyXErrorHandler(X11Display * display, XErrorEvent * event)
828 XGetErrorText(xGlobalDisplay, event->error_code, buffer, sizeof(buffer));
829 Logf("X Error: %s\n", buffer);
833 static int MyXIOErrorHandler(X11Display * display)
839 // Motif Hints (to get rid of the decorations)
840 #define MWM_HINTS_FUNCTIONS (1L << 0)
841 #define MWM_HINTS_DECORATIONS (1L << 1)
842 #define MWM_HINTS_INPUT_MODE (1L << 2)
843 #define MWM_HINTS_STATUS (1L << 3)
845 #define MWM_DECOR_ALL (1L << 0)
846 #define MWM_DECOR_BORDER (1L << 1)
847 #define MWM_DECOR_RESIZEH (1L << 2)
848 #define MWM_DECOR_TITLE (1L << 3)
849 #define MWM_DECOR_MENU (1L << 4)
850 #define MWM_DECOR_MINIMIZE (1L << 5)
851 #define MWM_DECOR_MAXIMIZE (1L << 6)
853 #define MWM_FUNC_ALL (1L << 0)
854 #define MWM_FUNC_RESIZE (1L << 1)
855 #define MWM_FUNC_MOVE (1L << 2)
856 #define MWM_FUNC_MINIMIZE (1L << 3)
857 #define MWM_FUNC_MAXIMIZE (1L << 4)
858 #define MWM_FUNC_CLOSE (1L << 5)
869 static void WaitForViewableWindow(Window window)
872 //Logf("Wait for viewable %s\n", window.name);
873 XFlush(xGlobalDisplay);
874 //while(attempts++ < 40)
877 XWindowAttributes attributes = { 0 };
879 if(!XGetWindowAttributes(xGlobalDisplay, (int)window.windowHandle, &attributes))
881 if(attributes.map_state == IsViewable)
884 Sleep(1.0 / RESOLUTION);
888 /****************************************************************************
889 /// DRIVER IMPLEMENTATION /////////////
890 ****************************************************************************/
892 /*static */class HiResTimer : Thread
911 // Sleep(delay / 1000000.0);
912 struct timeval tv = { delay / 1000000, delay % 1000000 };
913 select(0,null,null,null, &tv);
914 guiApp.SignalEvent();
920 static HiResTimer hiResTimer { };
926 #include <sys/signal.h>
937 static int terminatePid;
939 static void SigIntHandler(int value)
941 // printf("SigHandler %d\n", getpid());
942 if(!terminatePid || terminatePid == getpid())
945 terminatePid = getpid();
946 // printf("terminateX now equals %d\n", terminateX);
947 if(guiApp && guiApp.semaphore)
948 guiApp.semaphore.Release();
951 struct shmid_ds info;
952 int maxid = shmctl (0, SHM_INFO, &info);
954 int thisPid = getpid();
961 for(id = 0; id <= maxid; id++)
963 struct shmid_ds shmseg;
965 if((shmid = shmctl(id, SHM_STAT, &shmseg)) >= 0)
967 if(shmseg.shm_cpid == pid || shmseg.shm_cpid == thisPid)
969 printf("%d (%d) belongs to us (%d)\n", shmid, id, shmseg.shm_cpid);
970 shmctl(shmid, IPC_RMID, 0);
978 /*else if(guiApp.desktop)
979 guiApp.desktop.Destroy(0);*/
982 class XInterface : Interface
984 class_property(name) = "X";
986 // --- User Interface System ---
989 setlocale(LC_ALL, "en_US.UTF-8");
992 XSetLocaleModifiers("");
993 XSetErrorHandler(MyXErrorHandler);
994 XSetIOErrorHandler(MyXIOErrorHandler);
996 signal(SIGINT, SigIntHandler);
999 xGlobalDisplay = XOpenDisplay(null);
1001 joystickFD[0] = open("/dev/js0", O_RDONLY);
1002 joystickFD[1] = open("/dev/js1", O_RDONLY);
1003 joystickFD[2] = open("/dev/js2", O_RDONLY);
1004 joystickFD[3] = open("/dev/js3", O_RDONLY);
1008 XWindowAttributes attributes = { 0 };
1009 XGetWindowAttributes(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &attributes);
1010 xSystemDepth = attributes.depth;
1011 xSystemVisual = attributes.visual;
1012 switch(xSystemDepth)
1014 case 32: case 24: xSystemPixelFormat = pixelFormat888; break;
1019 XVisualInfo *vinfo_ret;
1022 vinfo.visualid = XVisualIDFromVisual(xSystemVisual);
1023 vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &vinfo, &numitems);
1026 xSystemPixelFormat = (vinfo_ret->green_mask == 0x3E0) ? pixelFormat555 : pixelFormat565;
1028 if(GetXRenderFormat(xSystemPixelFormat, 0))
1030 // printf("Got X Render format %d\n", xSystemPixelFormat);
1034 printf("No X Format?\n");
1035 xSystemPixelFormat = (xSystemPixelFormat == pixelFormat555) ? pixelFormat565 : pixelFormat555;
1037 // vinfo._class = DirectColor;
1039 vinfo.bits_per_rgb = 5;
1040 vinfo.depth = xSystemDepth;
1041 vinfo.green_mask = (xSystemPixelFormat == pixelFormat555) ? 0x3E0 : 0x7E0;
1042 vinfo.red_mask = (xSystemPixelFormat == pixelFormat555) ? 0x7C00 : 0xF800;
1043 XFree((void *) vinfo_ret);
1044 if(XMatchVisualInfo(xGlobalDisplay, DefaultScreen(xGlobalDisplay), vinfo.depth, vinfo._class, &vinfo))
1045 //vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualDepthMask|VisualRedMaskMask|VisualGreenMaskMask|VisualBlueMaskMask, &vinfo, &numitems);
1048 //vinfo = *vinfo_ret;
1049 if(GetXRenderFormat(xSystemPixelFormat, 0))
1051 // printf("Got X Render format %d (second try)\n", xSystemPixelFormat);
1052 // printf("red mask: %x, green mask: %x, blue mask: %x\n", vinfo.red_mask,vinfo.green_mask, vinfo.blue_mask);
1054 xSystemVisual = vinfo.visual;
1057 printf("Could not get a 555 visual\n");
1063 // printf("Got a depth of %d\n", xSystemDepth);
1066 int major, minor, pixmaps;
1067 xSharedMemory = XShmQueryExtension(xGlobalDisplay) && XShmQueryVersion(xGlobalDisplay, &major, &minor, &pixmaps) && pixmaps;
1070 // printf("Opening IM\n");
1071 im = XOpenIM(xGlobalDisplay, null, null, null);
1074 XColor fore = { 0 }, back = { 0 };
1075 Pixmap pixmap = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1076 Pixmap mask = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
1077 XSetWindowAttributes attributes = { 0 };
1079 XkbSetDetectableAutoRepeat(xGlobalDisplay, True, &autoRepeatDetectable);
1081 XInternAtoms(xGlobalDisplay, (char**)atomNames, AtomIdents::enumSize, False, atoms);
1084 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
1086 XSetWMProtocols(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), protocols, 2);
1090 if(atoms[_net_workarea] == None)
1091 printf("Warning: _NET_WORKAREA extension not supported\n");
1094 attributes.override_redirect = True;
1097 windowContext = XUniqueContext();
1100 nullCursor = XCreatePixmapCursor(xGlobalDisplay, pixmap, mask, &fore, &back, 0, 0);
1101 confineWindow = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
1102 0,0,1,1,0, CopyFromParent, InputOnly, xSystemVisual /*CopyFromParent*/, CWOverrideRedirect, &attributes);
1105 // IM initialization
1107 /*ic = XCreateIC(im, XNInputStyle,
1108 XIMPreeditNothing | XIMStatusNothing,
1109 // XIMPreeditNone | XIMStatusNone | XIMPreeditPosition | XIMStatusArea | XIMPreeditArea | XIMStatusArea | XIMPreeditNothing | XIMStatusNothing,
1110 XNClientWindow, confineWindow, XNFocusWindow, confineWindow, 0);
1115 XIMStyle clientCanDo;
1116 XIMStyle styleWeWillUse = null;
1118 XVaNestedList arglist;
1119 unsigned long imEventMask;
1121 XGetImValues(im, XNQueryInputStyle, &IMcando, null);
1124 XIMPreeditNone | XIMStatusNone |
1125 XIMPreeditPosition | XIMStatusArea |
1126 XIMPreeditArea | XIMStatusArea |
1127 XIMPreeditNothing | XIMStatusNothing;
1129 for(i=0; i<IMcando->count_styles; i++)
1132 tmpStyle = IMcando->support_styles[i];
1133 if ( ((tmpStyle & clientCanDo) == tmpStyle) && prefer(tmpStyle, styleWeWillUse) )
1134 styleWeWillUse = tmpStyle;
1136 if(styleWeWillUse = null)
1140 arglist = XVaCreateNestedList(0, XNFontSet, fontset,
1142 WhitePixel(xGlobalDisplay, screen),
1144 BlackPixel(xGlobalDisplay, screen),
1146 ic = XCreateIC(im, XNInputStyle, styleWeWillUse,
1147 XNClientWindow, window, XNFocusWindow, window,
1148 XNStatusAttributes, arglist,
1149 XNPreeditAttributes, arglist, NULL);
1154 XGetWindowAttributes(xGlobalDisplay, win, &winAtts);
1155 XGetICValues(ic, XNFilterEvents, &imEventMask, null);
1156 imEventMask |= winAtts.your_event_mask;
1157 XSelectInput(xGlobalDisplay, window, imEventMask);
1163 timerThread = Thread { };
1165 timerThread.Main = XTimerThread;
1166 timerThread.Create();
1179 // WHY WAS THIS COMMENTED HERE?
1180 // Probably because it was causing crashes, the proper fix should now be in DestroyRootWindow
1184 xSemaphore.Release();
1190 //XPutBackEvent(xGlobalDisplay, &e);
1194 /*if(windowData && windowData.ic)
1196 XDestroyIC(windowData.ic);
1197 windowData.ic = null;
1204 XCloseDisplay(xGlobalDisplay);
1205 xGlobalDisplay = null;
1207 if(joystickFD[0]) close(joystickFD[0]);
1208 if(joystickFD[1]) close(joystickFD[1]);
1209 if(joystickFD[2]) close(joystickFD[2]);
1210 if(joystickFD[3]) close(joystickFD[3]);
1213 #define DBLCLICK_DELAY 300 // 0.3 second
1214 #define DBLCLICK_DELTA 1
1216 bool ProcessInput(bool processAll)
1218 bool eventAvailable = false;
1221 if(!fullScreenMode) RepositionDesktop(true);
1223 //* XLockDisplay(xGlobalDisplay);
1224 while(!xTerminate && (/*gotAnXEvent || */XCheckIfEvent(xGlobalDisplay, &e, EventChecker, null)))
1226 Window window = null;
1227 XAnyEvent * thisEvent = (XAnyEvent *)&e;
1228 // printf("Got an event: %d\n", thisEvent->type);
1230 //if(gotAnXEvent) { thisEvent = (XAnyEvent *)&xEvent; gotAnXEvent = false; }
1232 if(im && XFilterEvent((union _XEvent *)thisEvent, None))
1234 eventAvailable = true;
1235 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1238 static uint lastKeyCode = 0;
1239 switch(thisEvent->type)
1243 XKeyEvent * event = (XKeyEvent *) thisEvent;
1244 //*XUnlockDisplay(xGlobalDisplay);
1245 ProcessKeyMessage(window, event->keycode, (event->keycode == lastKeyCode) ? 2 : 0, event);
1246 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1247 lastKeyCode = event->keycode;
1252 XKeyEvent * event = (XKeyEvent *) thisEvent;
1255 if(!autoRepeatDetectable && XCheckIfEvent(xGlobalDisplay, (XEvent *)&nextEvent, EventChecker, (void *)KeyPress))
1257 if(im && XFilterEvent(&nextEvent, None))
1260 if(((XKeyEvent *)&nextEvent)->keycode == event->keycode)
1262 //*XUnlockDisplay(xGlobalDisplay);
1263 ProcessKeyMessage(window, event->keycode, 2, event);
1267 //*XUnlockDisplay(xGlobalDisplay);
1268 // printf("Keycode not the same :(\n");
1269 ProcessKeyMessage(window, event->keycode, 1, event);
1270 ProcessKeyMessage(window, ((XKeyEvent *)&nextEvent)->keycode, 0, (XKeyEvent *)&nextEvent);
1275 //*XUnlockDisplay(xGlobalDisplay);
1276 // printf("No KeyPress ahead\n");
1277 ProcessKeyMessage(window, event->keycode, 1, event);
1279 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1284 XButtonEvent * event = (XButtonEvent *) thisEvent;
1286 static double lastTime[3];
1287 static Window lastWindow[3];
1288 static Point lastPos[3];
1290 Modifiers keyFlags = 0;
1292 uint button, buttonDouble, whichButton;
1295 if(event->button == Button1)
1297 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
1298 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
1300 buttonMask = Button1Mask;
1301 keyFlags.left = true;
1303 else if(event->button == Button3)
1305 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
1306 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
1308 buttonMask = Button3Mask;
1309 keyFlags.right = true;
1313 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
1314 buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
1316 buttonMask = Button2Mask;
1317 keyFlags.middle = true;
1319 if(event->state & buttonMask)
1322 doubleClick = event->time - lastTime[whichButton] < DBLCLICK_DELAY &&
1323 window == lastWindow[whichButton] &&
1324 Abs(event->x_root - lastPos[whichButton].x) < DBLCLICK_DELTA &&
1325 Abs(event->y_root - lastPos[whichButton].y) < DBLCLICK_DELTA;
1326 lastTime[whichButton] = doubleClick ? 0 : event->time;
1327 lastWindow[whichButton] = window;
1328 lastPos[whichButton].x = event->x_root;
1329 lastPos[whichButton].y = event->y_root;
1331 if(event->state & ShiftMask) keyFlags.shift = true;
1332 if(event->state & ControlMask) keyFlags.ctrl = true;
1333 if(event->state & Mod1Mask) keyFlags.alt = true;
1334 if(event->state & Button1Mask) keyFlags.left = true;
1335 if(event->state & Button2Mask) keyFlags.middle = true;
1336 if(event->state & Button3Mask) keyFlags.right = true;
1337 //*XUnlockDisplay(xGlobalDisplay);
1340 if(event->button == Button4 || event->button == Button5)
1342 window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, (event->button == Button4) ? wheelUp : wheelDown, 0);
1348 if(!window.MouseMessage(buttonDouble, event->x_root, event->y_root, &keyFlags, false, true))
1350 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1355 window.MouseMessage(button, event->x_root, event->y_root, &keyFlags, false, doubleClick? false : true);
1357 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1363 Modifiers keyFlags = 0;
1364 XButtonEvent * event = (XButtonEvent *) thisEvent;
1367 if(event->button == Button1)
1369 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
1370 buttonMask = Button1Mask;
1372 else if(event->button == Button3)
1374 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
1375 buttonMask = Button3Mask;
1379 button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
1380 buttonMask = Button2Mask;
1382 if(!(event->state & buttonMask)) break;
1383 if(event->state & ShiftMask) keyFlags.shift = true;
1384 if(event->state & ControlMask) keyFlags.ctrl = true;
1385 if(event->state & Mod1Mask) keyFlags.alt = true;
1386 if(event->state & Button1Mask) keyFlags.left = true;
1387 if(event->state & Button2Mask) keyFlags.middle = true;
1388 if(event->state & Button3Mask) keyFlags.right = true;
1389 //*XUnlockDisplay(xGlobalDisplay);
1391 window.MouseMessage(button, event->x_root, event->y_root, &keyFlags, false, false);
1393 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1398 static uint lastTime = 0;
1399 XMotionEvent * event = (XMotionEvent *) thisEvent;
1400 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, EventChecker, (void *)MotionNotify));
1401 // if(event->time - lastTime > 15)
1403 Modifiers keyFlags = 0;
1405 if(event->state & ShiftMask) keyFlags.shift = true;
1406 if(event->state & ControlMask) keyFlags.ctrl = true;
1407 if(event->state & Mod1Mask) keyFlags.alt = true;
1408 if(event->state & Button1Mask) keyFlags.left = true;
1409 if(event->state & Button2Mask) keyFlags.middle = true;
1410 if(event->state & Button3Mask) keyFlags.right = true;
1411 //*XUnlockDisplay(xGlobalDisplay);
1413 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove,
1414 event->x_root, event->y_root, &keyFlags, false, false);
1416 //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
1417 lastTime = event->time;
1423 XExposeEvent * event = (XExposeEvent *) thisEvent;
1425 box.left = event->x - window.clientStart.x;
1426 box.top = event->y - window.clientStart.y;
1427 box.right = box.left + event->width - 1;
1428 box.bottom = box.top + event->height - 1;
1430 box.left += window.clientStart.x;
1431 box.top += window.clientStart.y;
1432 box.right += window.clientStart.x;
1433 box.bottom += window.clientStart.y;
1434 window.UpdateDirty(box);
1437 case SelectionRequest:
1439 XSelectionRequestEvent *req = (XSelectionRequestEvent *) thisEvent;
1441 if(req->target == atoms[targets] && clipBoardData)
1443 Atom * supportedTargets = new Atom[4];
1444 supportedTargets[0] = atoms[targets];
1445 supportedTargets[1] = atoms[multiple];
1446 supportedTargets[2] = XA_STRING;
1447 supportedTargets[3] = atoms[utf8_string];
1448 XChangeProperty(xGlobalDisplay,req->requestor, req->_property,
1449 XA_ATOM,32,PropModeReplace, (byte *) supportedTargets, 4*sizeof(Atom));
1450 respond.xselection._property = req->_property;
1451 delete supportedTargets;
1453 else if((req->target == XA_STRING || req->target == atoms[utf8_string]) && clipBoardData)
1455 Atom _property = (req->_property == None) ? req->target : req->_property;
1456 XChangeProperty(xGlobalDisplay,req->requestor, _property,
1457 req->target/*req->_property*/,8,PropModeReplace, (byte *) clipBoardData, strlen(clipBoardData));
1458 respond.xselection._property = _property;
1461 respond.xselection._property = None;
1463 respond.xselection.type = SelectionNotify;
1464 respond.xselection.display = req->display;
1465 respond.xselection.requestor = req->requestor;
1466 respond.xselection.selection =req->selection;
1467 respond.xselection.target = req->target;
1468 respond.xselection.time = CurrentTime;
1469 XSendEvent(xGlobalDisplay, req->requestor,0,0,&respond);
1472 case SelectionClear:
1474 delete clipBoardData;
1479 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1480 Window modalRoot = window.FindModal();
1481 XWindowData windowData;
1482 activeWindow = (uint)window.windowHandle;
1484 if(window == window.parent.activeChild) break;
1486 //if(window.creationActivation == activate)
1489 modalRoot.ExternalActivate(true, true, window, null); // lastActive);
1491 window.ExternalActivate(true, true, window, null); // lastActive);
1493 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1494 if(windowData && windowData.ic)
1496 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
1497 XSetICFocus(windowData.ic);
1499 //delete lastActive;
1500 //lastActive = window;
1501 //incref lastActive;
1508 //printf("Processing a FocusOut Event for %s (%x)\n", window._class.name, window);
1511 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusIn, (XEvent *)thisEvent))
1515 if(thisEvent->window == activeWindow)
1516 activeWindow = (uint)null;
1518 if(XCheckTypedEvent(xGlobalDisplay, FocusIn, (XEvent *)thisEvent))
1520 if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusOut, (XEvent *)thisEvent))
1522 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1524 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1527 Window windowCopy = window;
1528 XWindowData windowData;
1529 if(windowCopy == windowCopy.parent.activeChild) break;
1531 windowCopy.ExternalActivate(true, true, windowCopy, lastActive);
1533 windowData = windowCopy.windowData;
1534 if(windowData && windowData.ic)
1535 XSetICFocus(windowData.ic);
1538 lastActive = windowCopy;
1543 // XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1546 if(window != window.parent.activeChild) break;
1548 window.ExternalActivate(false, true, window, lastActive);
1554 XWindowData windowData;
1555 XFindContext(xGlobalDisplay, thisEvent->window, windowContext, (XPointer *) &window);
1559 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1560 Window modalRoot = window.FindModal();
1565 modalRoot.ExternalActivate(true, true, window, lastActive);
1569 window.ExternalActivate(true, true, window, lastActive);
1571 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1572 if(windowData && windowData.ic)
1573 XSetICFocus(windowData.ic);
1576 lastActive = window;
1585 XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
1586 if(window != window.parent.activeChild && window != guiApp.interimWindow) break;
1590 //printf("Deactivating %s\n", window._class.name);
1593 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
1595 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
1598 //delete lastActive;
1600 lastActive = window;
1607 case ConfigureNotify:
1609 XConfigureEvent * event = (XConfigureEvent *) thisEvent;
1610 while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, (void *)ConfigureNotifyChecker, (void *)window.windowHandle));
1611 //if(event->x - desktopX != window.position.x || event->y - desktopY != window.position.y || event->width != window.size.w || event->height != window.size.h)
1613 // TODO: Support _NET_REQUEST_FRAME_EXTENTS message / _NET_FRAME_EXTENTS property for decoration size awareness
1615 window.ExternalPosition(event->x - desktopX, event->y - desktopY, event->width, event->height);
1620 XClientMessageEvent * event = (XClientMessageEvent *) thisEvent;
1622 if(event->data.l[0] == atoms[wm_delete_window])
1627 if(event->data.l[0] == atoms[wm_take_focus])
1630 XWindowData windowData;
1632 activeWindow = (uint)window.windowHandle;
1634 timeStamp = event->data.l[1];
1636 windowData = window.windowData;
1637 laterFocus = windowData.laterFocus;
1638 windowData.laterFocus = true;
1641 //printf("Processing a ClientMessage WM_TAKE_FOCUS Event for %s (%x)\n", window._class.name, window);
1643 if(guiApp.interimWindow && guiApp.interimWindow.created && window != guiApp.interimWindow) break; //window == window.parent.activeChild) break;
1644 // if(window == window.parent.activeChild) break;
1649 //XFlush(xGlobalDisplay);
1650 while(XCheckTypedEvent(xGlobalDisplay, FocusOut, &checkEvent))
1652 XFocusChangeEvent *event = (XFocusChangeEvent *) &checkEvent;
1654 XFindContext(xGlobalDisplay, event->window, windowContext, (XPointer *) &window);
1655 if(window != window.parent.activeChild) break;
1659 //printf("Found a FocusOut ahead, deactivating %s (%d)\n", window._class.name, window);
1662 if(!window.ExternalActivate(false, true, window, null /*lastActive*/))
1664 XCheckTypedEvent(xGlobalDisplay, /*thisEvent->window, */ButtonPress, (XEvent *)thisEvent);
1671 modalRoot = window.FindModal();
1672 windowData = modalRoot ? modalRoot.windowData : window.windowData;
1675 if(laterFocus || (modalRoot ? modalRoot : window).creationActivation == activate)
1679 XRaiseWindow(xGlobalDisplay, (int)modalRoot.windowHandle);
1680 WaitForViewableWindow(modalRoot);
1681 if(atoms[_net_active_window])
1683 XClientMessageEvent event = { 0 };
1684 event.type = ClientMessage;
1685 event.message_type = atoms[_net_active_window];
1686 event.display = xGlobalDisplay;
1688 event.window = (uint)modalRoot.windowHandle;
1689 event.send_event = 1;
1691 event.data.l[0] = 0;
1693 event.data.l[0] = 1;
1694 event.data.l[1] = atoms[_net_wm_user_time];
1695 event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
1698 //printf("(ClientMessage - %s) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, modalRoot._class.name, modalRoot);
1701 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
1702 XSetInputFocus(xGlobalDisplay, (int)modalRoot.windowHandle, RevertToPointerRoot, (uint)timeStamp);
1704 //XFlush(xGlobalDisplay);
1705 //printf("Done.\n");
1710 XSetInputFocus(xGlobalDisplay, (int)window.windowHandle, RevertToPointerRoot, (uint)timeStamp);
1711 window.ExternalActivate(true, true, window, null); // lastActive);
1712 if(windowData && windowData.ic)
1714 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
1715 //XSetICFocus(windowData.ic);
1719 windowData.laterFocus = true;
1721 lastActive = window;
1729 if(!processAll) break;
1732 //*if(xGlobalDisplay) XUnlockDisplay(xGlobalDisplay);
1741 gotAnXEvent = false;
1743 xSemaphore.Release();
1748 void Lock(Window window)
1750 //*XLockDisplay(xGlobalDisplay);
1753 void Unlock(Window window)
1755 //*XUnlockDisplay(xGlobalDisplay);
1758 char ** GraphicsDrivers(int * numDrivers)
1760 static char *graphicsDrivers[] = { "X", "OpenGL" };
1761 *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
1762 return (char **)graphicsDrivers;
1765 void GetCurrentMode(bool * fullScreen, int * resolution, int * colorDepth, int * refreshRate)
1767 *fullScreen = fullScreenMode;
1770 void EnsureFullScreen(bool *fullScreen)
1775 bool ScreenMode(bool fullScreen, int resolution, int colorDepth, int refreshRate, bool * textMode)
1779 fullScreenMode = fullScreen;
1787 static bool firstTime = true;
1789 desktopX = desktopY = desktopW = desktopH = 0;
1791 RepositionDesktop(false);
1796 // --- Window Creation ---
1797 void * CreateRootWindow(Window window)
1799 X11Window windowHandle;
1800 XSetWindowAttributes attributes = { 0 };
1801 XVisualInfo * visualInfo = null;
1805 unsigned int mask = EVENT_MASK;
1807 attributes.override_redirect = window.interim ? True : False;
1808 attributes.event_mask = EVENT_MASK;
1809 //printf("%s\n", guiApp.defaultDisplayDriver);
1810 #ifndef ECERE_VANILLA
1811 if(window.dispDriver == class(OpenGLDisplayDriver) || !strcmp(guiApp.defaultDisplayDriver, "OpenGL"))
1815 for(samples = 4;; samples /= 2)
1820 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1821 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
1822 GLX_DOUBLEBUFFER, True,
1828 int numAttribs = 14;
1830 GLXFBConfig *fbconfigs = null, fbconfig;
1833 //printf("Samples = %d, alpha = %d\n", samples, alpha);
1836 attrib[numAttribs++] = GLX_ALPHA_SIZE;
1837 attrib[numAttribs++] = 1;
1841 attrib[numAttribs++] = GLX_SAMPLE_BUFFERS_ARB;
1842 attrib[numAttribs++] = GL_TRUE;
1843 attrib[numAttribs++] = GLX_SAMPLES_ARB;
1844 attrib[numAttribs++] = samples;
1846 attrib[numAttribs] = None;
1848 // visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib);
1850 //printf("Trying %d samples...\n", samples);
1851 fbconfigs = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &numfbconfigs);
1854 for (i = 0; i < numfbconfigs; i++)
1856 XRenderPictFormat * format;
1857 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfigs[i]);
1858 if (!visualInfo) continue;
1859 if(window.alphaBlend)
1861 format = XRenderFindVisualFormat(xGlobalDisplay, visualInfo->visual);
1862 if (!format) { XFree(visualInfo); continue; }
1863 if(format->direct.alphaMask > 0)
1865 //printf("Found what we're looking for (alphaBlend)\n");
1866 fbconfig = fbconfigs[i];
1873 //printf("Found what we're looking for\n");
1878 if (i == numfbconfigs)
1880 fbconfig = fbconfigs[0];
1881 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfig);
1886 if(found || (!samples && !alpha))
1888 //printf("Stopping now\n");
1893 if(samples == 1) samples = 0;
1894 else if(!samples) alpha = false;
1898 depth = visualInfo ? visualInfo->depth : (window.alphaBlend ? 32 : xSystemDepth);
1899 visual = visualInfo ? visualInfo->visual : (window.alphaBlend ? FindFullColorVisual (xGlobalDisplay, &depth) : xSystemVisual);
1900 // printf("visual: %d, depth: %d\n", visual, depth);
1904 attributes.colormap = XCreateColormap(xGlobalDisplay, XRootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), visual, AllocNone);
1905 attributes.border_pixel = 0;
1912 windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
1913 0,0,guiApp.desktop.size.w,guiApp.desktop.size.h,0, depth, InputOutput, visual ? visual : CopyFromParent,
1914 CWEventMask | (visual ? (CWColormap | CWBorderPixel) : 0)/*| CWOverrideRedirect*/, &attributes);
1917 Unsupported for now...
1918 else if(window.systemParent)
1925 if(window.windowHandle)
1926 windowHandle = (int)window.windowHandle;
1929 X11Window parentWindow = (uint)null;
1931 if(window.master.rootWindow && window.master.rootWindow != guiApp.desktop && (window.style.modal || window.style.interim))
1933 Window master = window.master;
1934 Window rootWindow = master.rootWindow;
1936 parentWindow = rootWindow.is3D ? (uint)rootWindow.parent.windowHandle : (uint)rootWindow.windowHandle;
1938 // parentWindow = window.master.rootWindow.is3D ? window.master.rootWindow.parent.windowHandle : window.master.rootWindow.windowHandle;
1940 if(window.style.showInTaskBar)
1941 parentWindow = (uint)null;
1943 windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
1944 0,0,1,1,0, depth, InputOutput, visual ? visual : CopyFromParent,
1945 CWEventMask | CWOverrideRedirect | (visual ? (CWColormap | CWBorderPixel) : 0), &attributes);
1947 if(parentWindow && window.interim)
1949 //printf("Setting WM_TRANSIENT_FOR of %s to %s\n", window._class.name, window.master.rootWindow._class.name);
1950 XSetTransientForHint(xGlobalDisplay, windowHandle, parentWindow);
1951 //XFlush(xGlobalDisplay);
1952 //printf("Done.\n");
1953 //XChangeProperty(xGlobalDisplay, windowHandle, atoms[wm_transient_for], XA_WINDOW, 32, PropModeReplace, (unsigned char*)&parentWindow, 1);
1957 #if defined(__APPLE__)
1960 parentWindow ? atoms[_net_wm_window_type_popup_menu] : atoms[_net_wm_window_type_normal]
1963 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
1964 PropModeReplace, (unsigned char*)&hints, 1);
1968 parentWindow ? atoms[_net_wm_window_type_menu] : atoms[_net_wm_window_type_normal],
1969 parentWindow ? atoms[_net_wm_window_type_popup_menu] : 0
1971 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
1972 PropModeReplace, (unsigned char*)&hints, parentWindow ? 2 : 1);
1976 xwmHints.flags = InputHint;
1978 XSetWMHints(xGlobalDisplay, windowHandle, &xwmHints);
1981 Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
1982 XSetWMProtocols(xGlobalDisplay, windowHandle, protocols, 2);
1990 char fontString[1024] = "--helvetica-*-r-*-*-*-120-*-*-*-*-*-*,-misc-fixed-*-r-*-*-*-130-*-*-*-*-*-*";
1991 XPoint cursor_location = { 0, 0 };
1992 char **missing_charsets;
1993 int num_missing_charsets = 0;
1994 char *default_string;
1996 XRectangle area = { 0, 0, 400, 400 };
1997 XVaNestedList argList;
1999 // sprintf(fontString, "-*-%s-*-r-*-*-*-%d-*-*-*-*-*-*", "Helvetica" /*window.font.faceName*/, (int)(window.font.size * 20));
2000 fontset = XCreateFontSet(xGlobalDisplay, fontString, &missing_charsets, &num_missing_charsets, &default_string);
2001 argList = XVaCreateNestedList(0,
2002 XNSpotLocation, &cursor_location,
2004 XNFontSet, fontset,/*
2006 WhitePixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),
2008 BlackPixel(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),*/
2010 ic = XCreateIC(im, XNInputStyle,
2011 XIMStatusNothing | XIMPreeditPosition,
2012 XNPreeditAttributes, argList,
2013 XNClientWindow, windowHandle, XNFocusWindow, windowHandle, 0);
2015 // Should we free the fontset or not?
2016 // XFreeFontSet(xGlobalDisplay, fontset);
2018 setICPosition = true;
2020 ic = XCreateIC(im, XNInputStyle, XIMStatusNothing | XIMPreeditNothing, XNClientWindow, windowHandle, XNFocusWindow, windowHandle, 0);
2024 XGetICValues(ic, XNFilterEvents, &mask, NULL);
2028 XSelectInput(xGlobalDisplay, windowHandle, mask);
2030 if(capturedWindow == None && !restrictedWindow)
2032 XGrabPointer(xGlobalDisplay, (int)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2033 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2034 XUngrabPointer(xGlobalDisplay, CurrentTime);
2039 if ( atoms[_motif_wm_hints] != None )
2043 (window.nativeDecorations ? 0 : MWM_HINTS_DECORATIONS)|MWM_HINTS_FUNCTIONS,
2044 (window.hasClose ? MWM_FUNC_CLOSE : 0) |
2045 (window.hasMaximize ? MWM_FUNC_MAXIMIZE : 0) |
2046 (window.hasMinimize ? MWM_FUNC_MINIMIZE : 0) |
2047 ((window.moveable || ((BorderBits)window.borderStyle).fixed) ? MWM_FUNC_MOVE : 0) |
2048 (((BorderBits)window.borderStyle).sizable ? MWM_FUNC_RESIZE : 0),
2051 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
2052 PropModeReplace, (unsigned char*)&hints, sizeof(hints)/4);
2054 if(atoms[_net_wm_pid] != None)
2057 // printf("Setting _NET_WM_PID to %d\n", pid);
2058 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_pid], XA_CARDINAL, 32,
2059 PropModeReplace, (unsigned char*)&pid, 1);
2065 Atom protocolsAtom = XInternAtom(xGlobalDisplay, "WM_PROTOCOLS", False);
2066 if ( protocolsAtom != None )
2068 MWM_Hints hints = { MWM_HINTS_DECORATIONS|MWM_HINTS_FUNCTIONS, 0, 0, 0, 0 };
2069 XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
2070 PropModeReplace, (unsigned char*)&hints, sizeof(hints)/4);
2073 // XFlush(xGlobalDisplay);
2074 window.windowData = XWindowData { visualInfo, ic };
2076 XSaveContext(xGlobalDisplay, windowHandle, windowContext, (XPointer)window);
2078 XSelectInput(xGlobalDisplay, windowHandle, mask);
2080 if(capturedWindow == None && !restrictedWindow)
2082 XGrabPointer(xGlobalDisplay, (int)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2083 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2084 XUngrabPointer(xGlobalDisplay, CurrentTime);
2086 return (void *)windowHandle;
2089 void DestroyRootWindow(Window window)
2093 XDeleteContext(xGlobalDisplay, (XID)window, windowContext);
2094 XSaveContext(xGlobalDisplay, (int)window.windowHandle, windowContext, null);
2095 XDestroyWindow(xGlobalDisplay, (int)window.windowHandle);
2096 XSync(xGlobalDisplay, 0);
2097 while(XCheckWindowEvent(xGlobalDisplay, (int)window.windowHandle, 0xFFFFFFFF, &event));
2098 window.windowHandle = null;
2099 if(window.windowData)
2101 XWindowData windowData = window.windowData;
2102 XFree(windowData.visual);
2103 if(windowData && windowData.ic)
2104 XDestroyIC(windowData.ic);
2106 // printf("Setting windowData for %s to null\n", window._class.name);
2107 window.windowData = null;
2109 if(lastActive == window)
2113 // -- Window manipulation ---
2115 void SetRootWindowCaption(Window window, char * name)
2117 if(window.windowHandle)
2119 XChangeProperty(xGlobalDisplay, (int)window.windowHandle, atoms[_net_wm_name],
2120 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2121 XChangeProperty(xGlobalDisplay, (int)window.windowHandle, atoms[wm_name],
2122 atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
2126 void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
2128 //Logf("Position root window %s\n", window.name);
2129 if(!window.parent || !window.parent.display)
2131 #if defined(__APPLE__)
2132 bool visible = window.visible;
2135 XMapWindow(xGlobalDisplay, (int)window.windowHandle);
2136 WaitForViewableWindow(window);
2139 if(window.state == minimized) return;
2142 XMoveResizeWindow(xGlobalDisplay, (int)window.windowHandle, x + desktopX, y + desktopY, w, h);
2144 XMoveWindow(xGlobalDisplay, (int)window.windowHandle, x + desktopX, y + desktopY);
2146 XResizeWindow(xGlobalDisplay, (int)window.windowHandle, w, h);
2147 #if defined(__APPLE__)
2148 // if(window.created && !visible)
2149 // XUnmapWindow(xGlobalDisplay, (int)window.windowHandle);
2154 void OrderRootWindow(Window window, bool topMost)
2159 void SetRootWindowColor(Window window)
2164 void OffsetWindow(Window window, int * x, int * y)
2169 void UpdateRootWindow(Window window)
2171 if(!window.parent || !window.parent.display)
2175 Box box = window.box;
2176 box.left -= window.clientStart.x;
2177 box.top -= window.clientStart.y;
2178 box.right -= window.clientStart.x;
2179 box.bottom -= window.clientStart.y;
2180 // Logf("Update root window %s\n", window.name);
2181 window.Update(null);
2182 box.left += window.clientStart.x;
2183 box.top += window.clientStart.y;
2184 box.right += window.clientStart.x;
2185 box.bottom += window.clientStart.y;
2186 window.UpdateDirty(box);
2192 void SetRootWindowState(Window window, WindowState state, bool visible)
2194 if(!window.parent || !window.parent.display)
2196 //Logf("Set root window state %d %s\n", state, window.name);
2199 XMapWindow(xGlobalDisplay, (int)window.windowHandle);
2200 #if defined(__APPLE__)
2201 WaitForViewableWindow(window);
2203 if(window.creationActivation == activate && state != minimized)
2204 ActivateRootWindow(window);
2206 if(state == minimized)
2208 uint iconic = IconicState;
2210 XChangeProperty(xGlobalDisplay, window.windowHandle, atoms[_net_wm_state], XA_ATOM, 32,
2211 PropModeReplace, (unsigned char*)&atoms[_net_wm_state_hidden], 1);
2214 XChangeProperty(xGlobalDisplay, window.windowHandle, atoms[wm_state], XA_CARDINAL, 32,
2215 PropModeReplace, &iconic, 1);
2219 XClientMessageEvent event = { 0 };
2220 event.type = ClientMessage;
2221 event.message_type = atoms[_net_wm_state];
2222 event.display = xGlobalDisplay;
2224 event.window = window.windowHandle;
2225 event.send_event = 1;
2227 event.data.l[0] = 2; // 1;
2228 event.data.l[1] = atoms[_net_wm_state_hidden];
2229 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, &event);
2232 XClientMessageEvent event = { 0 };
2233 event.type = ClientMessage;
2234 event.message_type = atoms[wm_state];
2235 event.display = xGlobalDisplay;
2236 event.window = window.windowHandle;
2237 event.send_event = 1;
2239 event.data.l[0] = IconicState;
2240 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, &event);
2243 // printf("Attempting to minimize %s\n", window._class.name);
2244 XIconifyWindow(xGlobalDisplay, (uint)window.windowHandle, DefaultScreen(xGlobalDisplay));
2248 XMoveResizeWindow(xGlobalDisplay,
2249 (int)window.windowHandle, window.position.x + desktopX, window.position.y + desktopY, window.size.w, window.size.h);
2253 XUnmapWindow(xGlobalDisplay, (int)window.windowHandle);
2254 //XFlush(xGlobalDisplay);
2258 void FlashRootWindow(Window window)
2260 XClientMessageEvent event = { 0 };
2261 // printf("Attempting to flash root window\n");
2262 event.type = ClientMessage;
2263 event.message_type = atoms[_net_wm_state];
2264 event.display = xGlobalDisplay;
2266 event.window = (uint)window.windowHandle;
2267 event.send_event = 1;
2269 event.data.l[0] = 1;
2270 event.data.l[1] = atoms[_net_wm_state_demands_attention];
2271 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2274 void ActivateRootWindow(Window window)
2276 if(!window.parent || !window.parent.display)
2278 if(!window.style.hidden && window.created)
2280 //printf("Activate root window %s\n", window._class.name);
2281 XRaiseWindow(xGlobalDisplay, (int)window.windowHandle);
2282 XMapWindow(xGlobalDisplay, (int)window.windowHandle);
2283 WaitForViewableWindow(window);
2284 if(atoms[_net_active_window])
2286 XClientMessageEvent event = { 0 };
2287 event.type = ClientMessage;
2288 event.message_type = atoms[_net_active_window];
2289 event.display = xGlobalDisplay;
2291 event.window = (uint)window.windowHandle;
2292 event.send_event = 1;
2294 event.data.l[0] = 0;
2296 //event.data.l[0] = 2;
2297 //event.data.l[1] = timeStamp;
2300 //event.data.l[1] = atoms[_net_wm_user_time];
2301 //event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
2304 //printf("(ActivateRootWindow) Setting _NET_ACTIVE_WINDOW for %s (%x)\n", window._class.name, window);
2307 XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
2308 //#if defined(__APPLE__)
2309 XSetInputFocus(xGlobalDisplay, (int)window.windowHandle, RevertToPointerRoot, CurrentTime);
2313 XSetInputFocus(xGlobalDisplay, (int)window.windowHandle, RevertToPointerRoot, CurrentTime);
2318 // --- Mouse-based window movement ---
2320 void StartMoving(Window window, int x, int y, bool fromKeyBoard)
2325 void StopMoving(Window window)
2330 // -- Mouse manipulation ---
2332 void GetMousePosition(int *x, int *y)
2334 int rootWindow, childWindow;
2337 ((GuiApplication)__thisModule.application).Lock();
2338 //XLockDisplay(xGlobalDisplay);
2339 XQueryPointer(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &childWindow,
2340 &rootWindow, x, y, &mx, &my, &state);
2341 //XUnlockDisplay(xGlobalDisplay);
2342 ((GuiApplication)__thisModule.application).Unlock();
2345 void SetMousePosition(int x, int y)
2350 void SetMouseRange(Window window, Box box)
2352 ((GuiApplication)__thisModule.application).Lock();
2353 //XLockDisplay(xGlobalDisplay);
2354 if(box && box.left > 0 && box.top > 0 &&
2355 box.right < guiApp.desktop.clientSize.w - 1 && box.bottom < guiApp.desktop.clientSize.h - 1)
2357 if(!window.parent || !window.parent.display)
2359 XMoveResizeWindow(xGlobalDisplay, confineWindow, box.left + desktopX, box.top + desktopY,
2360 box.right - box.left + 1, box.bottom - box.top + 1);
2362 if(!restrictedWindow)
2363 XMapWindow(xGlobalDisplay, confineWindow);
2365 XGrabPointer(xGlobalDisplay, (int) window.rootWindow.windowHandle, False,
2366 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2367 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
2369 restrictedWindow = window;
2372 else if(restrictedWindow)
2374 if(capturedWindow != None)
2376 XGrabPointer(xGlobalDisplay, (int)capturedWindow,
2377 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2378 GrabModeAsync, None, fullScreenMode ? nullCursor : None, CurrentTime);
2381 XUngrabPointer(xGlobalDisplay, CurrentTime);
2383 if(restrictedWindow)
2384 XUnmapWindow(xGlobalDisplay, confineWindow);
2386 restrictedWindow = null;
2388 //XUnlockDisplay(xGlobalDisplay);
2389 ((GuiApplication)__thisModule.application).Unlock();
2392 void SetMouseCapture(Window window)
2394 //*XLockDisplay(xGlobalDisplay);
2397 if(!window.parent || !window.parent.display)
2399 XGrabPointer(xGlobalDisplay, (int)window.windowHandle,
2400 False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2401 GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
2403 capturedWindow = (X11Window) window.windowHandle;
2406 else if(capturedWindow != None)
2408 if(restrictedWindow)
2409 XGrabPointer(xGlobalDisplay, (int) restrictedWindow.rootWindow.windowHandle, False,
2410 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
2411 GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
2413 XUngrabPointer(xGlobalDisplay, CurrentTime);
2414 capturedWindow = None;
2416 //*XUnlockDisplay(xGlobalDisplay);
2419 // -- Mouse cursor ---
2421 void SetMouseCursor(int cursor)
2423 //*XLockDisplay(xGlobalDisplay);
2426 XDefineCursor(xGlobalDisplay, (int) guiApp.desktop.windowHandle, nullCursor);
2428 //*XUnlockDisplay(xGlobalDisplay);
2433 void SetCaret(int x, int y, int size)
2435 Window caretOwner = guiApp.caretOwner;
2436 Window window = caretOwner ? caretOwner.rootWindow : null;
2437 if(window && window.windowData && setICPosition)
2439 XWindowData windowData = window.windowData;
2440 if(windowData && windowData.ic)
2442 XPoint cursor_location =
2444 (short)(caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x),
2445 (short)(caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y)
2447 XVaNestedList argList = XVaCreateNestedList(0, XNSpotLocation, &cursor_location, NULL);
2448 XSetICValues(windowData.ic, XNPreeditAttributes, argList, 0);
2453 void ClearClipboard()
2455 //*XLockDisplay(xGlobalDisplay);
2458 delete clipBoardData;
2459 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard], None, CurrentTime);
2461 //*XUnlockDisplay(xGlobalDisplay);
2464 bool AllocateClipboard(ClipBoard clipBoard, uint size)
2466 bool result = false;
2467 if((clipBoard.text = new0 byte[size]))
2472 bool SaveClipboard(ClipBoard clipBoard)
2474 bool result = false;
2475 //*XLockDisplay(xGlobalDisplay);
2478 Window rootWindow = guiApp.desktop;
2481 for(rootWindow = rootWindow.children.first;
2482 rootWindow && !rootWindow.windowHandle;
2483 rootWindow = rootWindow.next);
2486 delete clipBoardData;
2488 XSetSelectionOwner(xGlobalDisplay, atoms[clipboard],
2489 (int) rootWindow.windowHandle, CurrentTime);
2490 clipBoardData = clipBoard.text;
2491 clipBoard.text = null;
2494 //*XUnlockDisplay(xGlobalDisplay);
2498 bool LoadClipboard(ClipBoard clipBoard)
2500 bool result = false;
2502 //*XLockDisplay(xGlobalDisplay);
2503 // The data is inside this client...
2506 clipBoard.text = new char[strlen(clipBoardData)+1];
2507 strcpy(clipBoard.text, clipBoardData);
2510 // The data is with another client...
2513 Window rootWindow = guiApp.desktop;
2516 for(rootWindow = rootWindow.children.first;
2517 rootWindow && !rootWindow.windowHandle;
2518 rootWindow = rootWindow.next);
2522 int selAtom = atoms[clipboard];
2523 int owner = XGetSelectionOwner(xGlobalDisplay, selAtom);
2527 for(atom = atoms[utf8_string]; atom; atom = ((atom == atoms[utf8_string]) ? XA_STRING : 0))
2530 XConvertSelection(xGlobalDisplay, selAtom, atom, atoms[app_selection] /*None*/, (int) rootWindow.windowHandle, CurrentTime);
2531 XIfEvent(xGlobalDisplay, (XEvent *) &e, EventChecker, (void *)SelectionNotify);
2532 if(e.type == SelectionNotify)
2534 XSelectionEvent * selection = (XSelectionEvent *) &e;
2535 //printf("Got a SelectionNotify with %d (%s)\n", selection->_property, XGetAtomName(xGlobalDisplay, selection->_property));
2537 uint len, size = 0, dummy;
2540 XGetWindowProperty(xGlobalDisplay, (int) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, 0, 0, AnyPropertyType, &type, &format, &len, &size, &data);
2543 if(XGetWindowProperty(xGlobalDisplay, (int) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0,size,0,
2544 AnyPropertyType, &type,&format,&len, &dummy, &data) == Success)
2546 clipBoard.text = new char[size+1];
2547 strncpy(clipBoard.text, data, size);
2548 clipBoard.text[size] = '\0';
2559 //*XUnlockDisplay(xGlobalDisplay);
2563 void UnloadClipboard(ClipBoard clipBoard)
2565 delete clipBoard.text;
2568 // --- State based input ---
2570 bool AcquireInput(Window window, bool state)
2575 bool GetMouseState(MouseButtons * buttons, int * x, int * y)
2577 bool result = false;
2583 bool GetJoystickState(int device, Joystick joystick)
2585 bool result = false;
2586 #if defined(__linux__)
2587 if(joystick && device < 4)
2589 struct JS_DATA_TYPE js = { 0 };
2590 memset(joystick, 0, sizeof(Joystick));
2591 if(joystickFD[device] && read(joystickFD[device], &js, JS_RETURN) == JS_RETURN)
2593 joystick.buttons = js.buttons;
2594 joystick.x = js.x - 128;
2595 joystick.y = js.y - 128;
2603 bool GetKeyState(Key key)
2609 void SetTimerResolution(uint hertz)
2611 timerDelay = hertz ? (1000000 / hertz) : MAXINT;
2616 hiResTimer.delay = 1000000 / hertz;
2617 hiResTimer.Create();
2622 bool SetIcon(Window window, BitmapResource resource)
2627 if(bitmap.Load(resource.fileName, null, null))
2629 uint * icon = new uint[2 + bitmap.width * bitmap.height];
2630 bitmap.Convert(null, pixelFormat888, null);
2631 icon[0] = bitmap.width;
2632 icon[1] = bitmap.height;
2633 memcpy(icon + 2, bitmap.picture, bitmap.width * bitmap.height * 4);
2634 XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_icon],
2635 XA_CARDINAL,32,PropModeReplace, (byte *)icon, 2+bitmap.width*bitmap.height);
2644 default dllexport void * __attribute__((stdcall)) IS_XGetDisplay()
2646 return xGlobalDisplay;
2649 default dllexport void * __attribute__((stdcall)) IS_XGetWindow(Window window)
2651 return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;