1 // a copy of this software and associated documentation files (the
2 // "Software"), to deal in the Software without restriction, including
3 // without limitation the rights to use, copy, modify, merge, publish,
4 // distribute, sublicense, and/or sell copies of the Software, and to
5 // permit persons to whom the Software is furnished to do so, subject to
6 // the following conditions:
8 // The above copyright notice and this permission notice shall be
9 // included in all copies or substantial portions of the Software.
11 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
15 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
24 using System.Collections;
25 using System.Diagnostics;
27 using System.Drawing.Drawing2D;
28 using System.Drawing.Imaging;
31 using System.Net.Sockets;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
35 using System.Threading;
36 using System.Windows.Forms;
37 // Only do the poll when building with mono for now
39 using Mono.Unix.Native;
42 namespace System.Windows.Forms.X11Internal {
44 internal class X11Display {
46 IntPtr display; /* our X handle */
48 // XXX internal because X11Hwnd needs them
49 internal IntPtr CustomVisual; // Visual for window creation
50 internal IntPtr CustomColormap; // Colormap for window creation
53 internal X11Dnd Dnd; // XXX X11Hwnd needs it to enable Dnd
54 bool detectable_key_auto_repeat;
57 X11RootHwnd root_hwnd;
62 ClipboardStruct Clipboard; // Our clipboard
65 internal X11Hwnd ActiveWindow;
69 Stack ModalWindows; // Stack of our modal windows
74 // mouse hover message generation
75 // XXX internal because X11Atoms needs to access it..
76 internal HoverStruct HoverState;
78 // double click message generation
79 ClickStruct ClickPending;
80 int DoubleClickInterval; // msec; max interval between clicks to count as double click
82 // Support for mouse grab
86 IntPtr LastCursorWindow; // The last window we set the cursor on
87 IntPtr LastCursorHandle; // The handle that was last set on LastCursorWindow
88 IntPtr OverrideCursorHandle; // The cursor that is set to override any other cursors
91 Point MousePosition; // Last position of mouse, in screen coords
92 MouseButtons MouseState; // Last state of mouse buttons
94 XErrorHandler ErrorHandler; // Error handler delegate
95 bool ErrorExceptions; // Throw exceptions on X errors
97 Thread event_thread; // the background thread that just watches our X socket
103 public X11Display (IntPtr display)
105 if (display == IntPtr.Zero) {
106 throw new ArgumentNullException("Display",
107 "Could not open display (X-Server required. Check you DISPLAY environment variable)");
110 this.display = display;
113 if (Environment.GetEnvironmentVariable ("MONO_XSYNC") != null) {
114 Xlib.XSynchronize (display, true);
117 if (Environment.GetEnvironmentVariable ("MONO_XEXCEPTIONS") != null) {
118 ErrorExceptions = true;
121 atoms = new X11Atoms (this);
123 DoubleClickInterval = 500;
125 HoverState.Interval = 500;
126 HoverState.Timer = new Timer();
127 HoverState.Timer.Enabled = false;
128 HoverState.Timer.Interval = HoverState.Interval;
129 HoverState.Timer.Tick += new EventHandler(MouseHover);
130 HoverState.Size = new Size(4, 4);
136 ModalWindows = new Stack(3);
138 MouseState = MouseButtons.None;
139 MousePosition = new Point(0, 0);
141 Caret.Timer = new Timer();
142 Caret.Timer.Interval = 500; // FIXME - where should this number come from?
143 Caret.Timer.Tick += new EventHandler(CaretCallback);
145 // XXX multiscreen work here
146 root_hwnd = new X11RootHwnd (this, Xlib.XRootWindow (display, DefaultScreen));
148 // XXX do we need a per-screen foster parent?
149 // Create the foster parent
150 foster_hwnd = new X11Hwnd (this,
151 Xlib.XCreateSimpleWindow (display, root_hwnd.WholeWindow,
152 0, 0, 1, 1, 4, UIntPtr.Zero, UIntPtr.Zero));
155 pollfds = new Pollfd [1];
156 pollfds [0] = new Pollfd ();
157 pollfds [0].fd = Xlib.XConnectionNumber (display);
158 pollfds [0].events = PollEvents.POLLIN;
161 Keyboard = new X11Keyboard(display, foster_hwnd.Handle);
162 Dnd = new X11Dnd (display);
164 ErrorExceptions = false;
166 // Handle any upcoming errors
167 ErrorHandler = new XErrorHandler (HandleError);
168 Xlib.XSetErrorHandler (ErrorHandler);
170 X11DesktopColors.Initialize(); // XXX we need to figure out how to make this display specific?
172 // Disable keyboard autorepeat
174 Xlib.XkbSetDetectableAutoRepeat (display, true, IntPtr.Zero);
175 detectable_key_auto_repeat = true;
177 Console.Error.WriteLine ("Could not disable keyboard auto repeat, will attempt to disable manually.");
178 detectable_key_auto_repeat = false;
181 // we re-set our error handler here, X11DesktopColor stuff might have stolen it (gtk does)
182 Xlib.XSetErrorHandler (ErrorHandler);
184 // create our event thread (just sits on the X socket waiting for events)
185 event_thread = new Thread (new ThreadStart (XEventThread));
186 event_thread.IsBackground = true;
187 event_thread.Start ();
191 private void MouseHover(object sender, EventArgs e)
193 HoverState.Timer.Enabled = false;
195 if (HoverState.Window != IntPtr.Zero) {
196 X11Hwnd hwnd = (X11Hwnd)Hwnd.GetObjectFromWindow (HoverState.Window);
198 XEvent xevent = new XEvent ();
200 xevent.type = XEventName.ClientMessage;
201 xevent.ClientMessageEvent.display = display;
202 xevent.ClientMessageEvent.window = HoverState.Window;
203 xevent.ClientMessageEvent.message_type = HoverState.Atom;
204 xevent.ClientMessageEvent.format = 32;
205 xevent.ClientMessageEvent.ptr1 = (IntPtr) (HoverState.Y << 16 | HoverState.X);
207 hwnd.Queue.Enqueue (xevent);
212 private void CaretCallback (object sender, EventArgs e)
217 Caret.On = !Caret.On;
219 Xlib.XDrawLine (display, Caret.Hwnd, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
222 internal string WhereString ()
231 newline = String.Format("{0}\t {1} ", Environment.NewLine, Locale.GetText("at"));
232 unknown = Locale.GetText("<unknown method>");
233 sb = new StringBuilder();
234 stack = new StackTrace(true);
236 for (int i = 0; i < stack.FrameCount; i++) {
237 frame = stack.GetFrame (i);
240 method = frame.GetMethod();
241 if (method != null) {
242 if (frame.GetFileLineNumber() != 0)
243 sb.AppendFormat ("{0}.{1} () [{2}:{3}]",
244 method.DeclaringType.FullName, method.Name,
245 Path.GetFileName(frame.GetFileName()), frame.GetFileLineNumber());
247 sb.AppendFormat ("{0}.{1} ()", method.DeclaringType.FullName, method.Name);
252 return sb.ToString();
255 private int HandleError (IntPtr display, ref XErrorEvent error_event)
258 throw new X11Exception (error_event.display, error_event.resourceid,
259 error_event.serial, error_event.error_code,
260 error_event.request_code, error_event.minor_code);
262 Console.WriteLine ("X11 Error encountered: {0}{1}\n",
263 X11Exception.GetMessage(error_event.display, error_event.resourceid,
264 error_event.serial, error_event.error_code,
265 error_event.request_code, error_event.minor_code),
269 #endregion // Callbacks
271 private void ShowCaret()
273 if ((Caret.gc == IntPtr.Zero) || Caret.On) {
278 Xlib.XDrawLine (display, Caret.Window, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
281 private void HideCaret()
283 if ((Caret.gc == IntPtr.Zero) || !Caret.On) {
288 Xlib.XDrawLine (display, Caret.Window, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
291 public void CaretVisible (IntPtr handle, bool visible)
293 if (Caret.Hwnd == handle) {
295 if (!Caret.Visible) {
296 Caret.Visible = true;
301 Caret.Visible = false;
308 public void AudibleAlert ()
310 Xlib.XBell (display, 0);
315 Xlib.XFlush (display);
320 // XXX shut down the event_thread
321 Xlib.XCloseDisplay (display);
324 public IntPtr XGetParent(IntPtr handle)
331 Xlib.XQueryTree (display, handle, out Root, out Parent, out Children, out ChildCount);
333 if (Children!=IntPtr.Zero) {
334 Xlib.XFree(Children);
340 public bool SystrayAdd(IntPtr handle, string tip, Icon icon, out ToolTip tt)
342 IntPtr SystrayMgrWindow;
344 Xlib.XGrabServer (display);
345 SystrayMgrWindow = Xlib.XGetSelectionOwner (display, Atoms._NET_SYSTEM_TRAY_S);
346 Xlib.XUngrabServer (display);
348 if (SystrayMgrWindow != IntPtr.Zero) {
349 XSizeHints size_hints;
352 hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
354 Console.WriteLine("Adding Systray Whole:{0:X}, Client:{1:X}",
355 hwnd.WholeWindow.ToInt32(), hwnd.ClientWindow.ToInt32());
359 if (hwnd.ClientWindow != hwnd.WholeWindow) {
360 Xlib.XDestroyWindow (display, hwnd.ClientWindow);
361 hwnd.ClientWindow = hwnd.WholeWindow;
366 /* by virtue of the way the tests are ordered when determining if it's PAINT
367 or NCPAINT, ClientWindow == WholeWindow will always be PAINT. So, if we're
368 waiting on an nc_expose, drop it and remove the hwnd from the list (unless
369 there's a pending expose). */
370 hwnd.PendingNCExpose = false;
373 hwnd.Queue.Unlock ();
377 size_hints = new XSizeHints();
379 size_hints.flags = (IntPtr)(XSizeHintsFlags.PMinSize | XSizeHintsFlags.PMaxSize | XSizeHintsFlags.PBaseSize);
381 size_hints.min_width = 24;
382 size_hints.min_height = 24;
383 size_hints.max_width = 24;
384 size_hints.max_height = 24;
385 size_hints.base_width = 24;
386 size_hints.base_height = 24;
388 Xlib.XSetWMNormalHints (display, hwnd.WholeWindow, ref size_hints);
390 int[] atoms = new int[2];
391 atoms [0] = 1; // Version 1
392 atoms [1] = 1; // we want to be mapped
394 // This line cost me 3 days...
395 Xlib.XChangeProperty (display,
396 hwnd.WholeWindow, Atoms._XEMBED_INFO, Atoms._XEMBED_INFO, 32,
397 PropertyMode.Replace, atoms, 2);
399 // Need to pick some reasonable defaults
401 tt.AutomaticDelay = 100;
402 tt.InitialDelay = 250;
403 tt.ReshowDelay = 250;
404 tt.ShowAlways = true;
406 if ((tip != null) && (tip != string.Empty)) {
407 tt.SetToolTip(Control.FromHandle(handle), tip);
413 SendNetClientMessage (SystrayMgrWindow,
414 Atoms._NET_SYSTEM_TRAY_OPCODE,
416 (IntPtr)SystrayRequest.SYSTEM_TRAY_REQUEST_DOCK,
426 public bool SystrayChange (IntPtr handle, string tip, Icon icon, ref ToolTip tt)
430 control = Control.FromHandle(handle);
431 if (control != null && tt != null) {
432 tt.SetToolTip(control, tip);
440 public void SystrayRemove(IntPtr handle, ref ToolTip tt)
442 #if GTKSOCKET_SUPPORTS_REPARENTING
445 hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
447 /* in the XEMBED spec, it mentions 3 ways for a client window to break the protocol with the embedder.
448 * 1. The embedder can unmap the window and reparent to the root window (we should probably handle this...)
449 * 2. The client can reparent its window out of the embedder window.
450 * 3. The client can destroy its window.
452 * this call to SetParent is case 2, but in
453 * the spec it also mentions that gtk doesn't
454 * support this at present. Looking at HEAD
455 * gtksocket-x11.c jives with this statement.
457 * so we can't reparent. we have to destroy.
459 SetParent(hwnd.WholeWindow, FosterParent);
461 Control control = Control.FromHandle(handle);
462 if (control is NotifyIcon.NotifyIconWindow)
463 ((NotifyIcon.NotifyIconWindow)control).InternalRecreateHandle ();
466 // The caller can now re-dock it later...
473 public void ResetMouseHover (X11Hwnd hovering)
475 HoverState.Timer.Enabled = hovering != null;
476 HoverState.X = MousePosition.X;
477 HoverState.Y = MousePosition.Y;
478 HoverState.Window = hovering == null ? IntPtr.Zero : hovering.Handle;
481 public void ShowCursor (bool show)
483 ; // FIXME - X11 doesn't 'hide' the cursor. we could create an empty cursor
486 public void SetModal (X11Hwnd hwnd, bool Modal)
489 ModalWindows.Push(hwnd);
491 // XXX do we need to pop until the
492 // hwnd is off the stack? or just the
493 // most recently pushed hwnd?
494 if (ModalWindows.Contains(hwnd)) {
498 if (ModalWindows.Count > 0) {
499 X11Hwnd top_hwnd = (X11Hwnd)ModalWindows.Peek();
505 public TransparencySupport SupportsTransparency ()
507 // compiz adds _NET_WM_WINDOW_OPACITY to _NET_SUPPORTED on the root window, check for that
508 return ((IList)root_hwnd._NET_SUPPORTED).Contains (Atoms._NET_WM_WINDOW_OPACITY) ? TransparencySupport.GetSet : TransparencySupport.None;
511 public void SendAsyncMethod (AsyncMethodData method)
513 X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(method.Handle);
514 XEvent xevent = new XEvent ();
516 xevent.type = XEventName.ClientMessage;
517 xevent.ClientMessageEvent.display = display;
518 xevent.ClientMessageEvent.window = method.Handle;
519 xevent.ClientMessageEvent.message_type = Atoms.AsyncAtom;
520 xevent.ClientMessageEvent.format = 32;
521 xevent.ClientMessageEvent.ptr1 = (IntPtr) GCHandle.Alloc (method);
523 hwnd.Queue.Enqueue (xevent);
526 delegate IntPtr WndProcDelegate (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam);
528 public IntPtr SendMessage (IntPtr handle, Msg message, IntPtr wParam, IntPtr lParam)
530 X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
534 if (hwnd.Queue.Thread != Thread.CurrentThread) {
535 AsyncMethodResult result;
536 AsyncMethodData data;
538 result = new AsyncMethodResult ();
539 data = new AsyncMethodData ();
541 data.Handle = hwnd.Handle;
542 data.Method = new WndProcDelegate (NativeWindow.WndProc);
543 data.Args = new object[] { hwnd.Handle, message, wParam, lParam };
544 data.Result = result;
546 SendAsyncMethod (data);
547 #if DriverDebug || DriverDebugThreads
548 Console.WriteLine ("Sending {0} message across.", message);
554 return NativeWindow.WndProc (hwnd.Handle, message, wParam, lParam);
558 public int SendInput (IntPtr handle, Queue keys) {
559 if (handle == IntPtr.Zero)
562 int count = keys.Count;
563 Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
565 while (keys.Count > 0) {
567 MSG msg = (MSG)keys.Dequeue();
569 XEvent xevent = new XEvent ();
571 xevent.type = (msg.message == Msg.WM_KEYUP ? XEventName.KeyRelease : XEventName.KeyPress);
572 xevent.KeyEvent.display = display;
575 xevent.KeyEvent.window = hwnd.whole_window;
577 xevent.KeyEvent.window = IntPtr.Zero;
580 xevent.KeyEvent.keycode = Keyboard.ToKeycode((int)msg.wParam);
582 hwnd.Queue.EnqueueLocked (xevent);
587 // FIXME - I think this should just enqueue directly
588 public bool PostMessage (IntPtr handle, Msg message, IntPtr wparam, IntPtr lparam)
590 XEvent xevent = new XEvent ();
591 X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
593 xevent.type = XEventName.ClientMessage;
594 xevent.ClientMessageEvent.display = display;
597 xevent.ClientMessageEvent.window = hwnd.WholeWindow;
599 xevent.ClientMessageEvent.window = IntPtr.Zero;
602 xevent.ClientMessageEvent.message_type = Atoms.PostAtom;
603 xevent.ClientMessageEvent.format = 32;
604 xevent.ClientMessageEvent.ptr1 = handle;
605 xevent.ClientMessageEvent.ptr2 = (IntPtr) message;
606 xevent.ClientMessageEvent.ptr3 = wparam;
607 xevent.ClientMessageEvent.ptr4 = lparam;
609 hwnd.Queue.Enqueue (xevent);
614 public void SendNetWMMessage (IntPtr window, IntPtr message_type, IntPtr l0, IntPtr l1, IntPtr l2)
619 xev.ClientMessageEvent.type = XEventName.ClientMessage;
620 xev.ClientMessageEvent.send_event = true;
621 xev.ClientMessageEvent.window = window;
622 xev.ClientMessageEvent.message_type = message_type;
623 xev.ClientMessageEvent.format = 32;
624 xev.ClientMessageEvent.ptr1 = l0;
625 xev.ClientMessageEvent.ptr2 = l1;
626 xev.ClientMessageEvent.ptr3 = l2;
628 Xlib.XSendEvent (display, root_hwnd.Handle, false,
629 new IntPtr ((int) (EventMask.SubstructureRedirectMask | EventMask.SubstructureNotifyMask)), ref xev);
632 public void SendNetClientMessage (IntPtr window, IntPtr message_type, IntPtr l0, IntPtr l1, IntPtr l2)
637 xev.ClientMessageEvent.type = XEventName.ClientMessage;
638 xev.ClientMessageEvent.send_event = true;
639 xev.ClientMessageEvent.window = window;
640 xev.ClientMessageEvent.message_type = message_type;
641 xev.ClientMessageEvent.format = 32;
642 xev.ClientMessageEvent.ptr1 = l0;
643 xev.ClientMessageEvent.ptr2 = l1;
644 xev.ClientMessageEvent.ptr3 = l2;
646 Xlib.XSendEvent (display, window, false, new IntPtr ((int)EventMask.NoEventMask), ref xev);
649 public bool TranslateMessage (ref MSG msg)
651 return Keyboard.TranslateMessage (ref msg);
654 public IntPtr DispatchMessage (ref MSG msg)
656 return NativeWindow.WndProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
659 private void QueryPointer (IntPtr w, out IntPtr root, out IntPtr child,
660 out int root_x, out int root_y, out int child_x, out int child_y,
663 /* this code was written with the help of
664 glance at gdk. I never would have realized we
665 needed a loop in order to traverse down in the
666 hierarchy. I would have assumed you'd get the
667 most deeply nested child and have to do
668 XQueryTree to move back up the hierarchy..
669 stupid me, of course. */
672 // Xlib.XGrabServer (display);
674 Xlib.XQueryPointer (display, w, out root, out c,
675 out root_x, out root_y, out child_x, out child_y,
681 IntPtr child_last = IntPtr.Zero;
682 while (c != IntPtr.Zero) {
684 Xlib.XQueryPointer (display, c, out root, out c,
685 out root_x, out root_y, out child_x, out child_y,
689 // Xlib.XUngrabServer (display);
694 public void SetCursorPos (int x, int y)
697 int root_x, root_y, child_x, child_y, mask;
700 * QueryPointer before warping
701 * because if the warp is on
702 * the RootWindow, the x/y are
703 * relative to the current
706 QueryPointer (RootWindow.Handle,
709 out root_x, out root_y,
710 out child_x, out child_y,
713 Xlib.XWarpPointer (display, IntPtr.Zero, IntPtr.Zero, 0, 0, 0, 0, x - root_x, y - root_y);
715 Xlib.XFlush (display);
718 * QueryPointer after warping
719 * to manually generate a
720 * motion event for the window
723 QueryPointer (RootWindow.Handle,
726 out root_x, out root_y,
727 out child_x, out child_y,
730 X11Hwnd child_hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(child);
731 if (child_hwnd == null)
734 XEvent xevent = new XEvent ();
736 xevent.type = XEventName.MotionNotify;
737 xevent.MotionEvent.display = display;
738 xevent.MotionEvent.window = child_hwnd.Handle;
739 xevent.MotionEvent.root = RootWindow.Handle;
740 xevent.MotionEvent.x = child_x;
741 xevent.MotionEvent.y = child_y;
742 xevent.MotionEvent.x_root = root_x;
743 xevent.MotionEvent.y_root = root_y;
744 xevent.MotionEvent.state = mask;
746 child_hwnd.Queue.Enqueue (xevent);
749 public void SetFocus (X11Hwnd new_focus)
751 if (new_focus == FocusWindow)
754 X11Hwnd prev_focus = FocusWindow;
755 FocusWindow = new_focus;
757 if (prev_focus != null)
758 SendMessage (prev_focus.Handle, Msg.WM_KILLFOCUS,
759 FocusWindow == null ? IntPtr.Zero : FocusWindow.Handle, IntPtr.Zero);
760 if (FocusWindow != null)
761 SendMessage (FocusWindow.Handle, Msg.WM_SETFOCUS,
762 prev_focus == null ? IntPtr.Zero : prev_focus.Handle, IntPtr.Zero);
764 //XSetInputFocus(DisplayHandle, Hwnd.ObjectFromHandle(handle).ClientWindow, RevertTo.None, IntPtr.Zero);
767 public IntPtr DefineCursor (Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot)
770 Bitmap cursor_bitmap;
778 IntPtr cursor_pixmap;
785 if (Xlib.XQueryBestCursor (display, RootWindow.Handle, bitmap.Width, bitmap.Height, out width, out height) == 0) {
789 // Win32 only allows creation cursors of a certain size
790 if ((bitmap.Width != width) || (bitmap.Width != height)) {
791 cursor_bitmap = new Bitmap(bitmap, new Size(width, height));
792 cursor_mask = new Bitmap(mask, new Size(width, height));
794 cursor_bitmap = bitmap;
798 width = cursor_bitmap.Width;
799 height = cursor_bitmap.Height;
801 cursor_bits = new Byte[(width / 8) * height];
802 mask_bits = new Byte[(width / 8) * height];
804 for (int y = 0; y < height; y++) {
805 for (int x = 0; x < width; x++) {
806 c_pixel = cursor_bitmap.GetPixel(x, y);
807 m_pixel = cursor_mask.GetPixel(x, y);
809 and = c_pixel == cursor_pixel;
810 xor = m_pixel == mask_pixel;
814 // cursor_bits[y * width / 8 + x / 8] &= (byte)~((1 << (x % 8))); // The bit already is 0
815 mask_bits[y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
816 } else if (and && !xor) {
818 cursor_bits[y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
819 mask_bits[y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
821 } else if (and && !xor) {
823 } else if (and && xor) {
826 // X11 doesn't know the 'reverse screen' concept, so we'll treat them the same
827 // we want both to be 0 so nothing to be done
828 //cursor_bits[y * width / 8 + x / 8] &= (byte)~((1 << (x % 8)));
829 //mask_bits[y * width / 8 + x / 8] |= (byte)(01 << (x % 8));
835 cursor_pixmap = Xlib.XCreatePixmapFromBitmapData (display, RootWindow.Handle,
836 cursor_bits, width, height, (IntPtr)1, (IntPtr)0, 1);
837 mask_pixmap = Xlib.XCreatePixmapFromBitmapData (display, RootWindow.Handle,
838 mask_bits, width, height, (IntPtr)1, (IntPtr)0, 1);
842 fg.pixel = Xlib.XWhitePixel (display, DefaultScreen);
843 fg.red = (ushort)65535;
844 fg.green = (ushort)65535;
845 fg.blue = (ushort)65535;
847 bg.pixel = Xlib.XBlackPixel (display, DefaultScreen);
849 cursor = Xlib.XCreatePixmapCursor (display, cursor_pixmap, mask_pixmap, ref fg, ref bg, xHotSpot, yHotSpot);
851 Xlib.XFreePixmap (display, cursor_pixmap);
852 Xlib.XFreePixmap (display, mask_pixmap);
857 public IntPtr DefineStdCursor (StdCursor id)
859 CursorFontShape shape;
861 // FIXME - define missing shapes
864 case StdCursor.AppStarting:
865 shape = CursorFontShape.XC_watch;
868 case StdCursor.Arrow:
869 shape = CursorFontShape.XC_top_left_arrow;
872 case StdCursor.Cross:
873 shape = CursorFontShape.XC_crosshair;
876 case StdCursor.Default:
877 shape = CursorFontShape.XC_top_left_arrow;
881 shape = CursorFontShape.XC_hand1;
885 shape = CursorFontShape.XC_question_arrow;
888 case StdCursor.HSplit:
889 shape = CursorFontShape.XC_sb_v_double_arrow;
892 case StdCursor.IBeam:
893 shape = CursorFontShape.XC_xterm;
897 shape = CursorFontShape.XC_circle;
900 case StdCursor.NoMove2D:
901 shape = CursorFontShape.XC_fleur;
904 case StdCursor.NoMoveHoriz:
905 shape = CursorFontShape.XC_fleur;
908 case StdCursor.NoMoveVert:
909 shape = CursorFontShape.XC_fleur;
912 case StdCursor.PanEast:
913 shape = CursorFontShape.XC_fleur;
916 case StdCursor.PanNE:
917 shape = CursorFontShape.XC_fleur;
920 case StdCursor.PanNorth:
921 shape = CursorFontShape.XC_fleur;
924 case StdCursor.PanNW:
925 shape = CursorFontShape.XC_fleur;
928 case StdCursor.PanSE:
929 shape = CursorFontShape.XC_fleur;
932 case StdCursor.PanSouth:
933 shape = CursorFontShape.XC_fleur;
936 case StdCursor.PanSW:
937 shape = CursorFontShape.XC_fleur;
940 case StdCursor.PanWest:
941 shape = CursorFontShape.XC_sizing;
944 case StdCursor.SizeAll:
945 shape = CursorFontShape.XC_fleur;
948 case StdCursor.SizeNESW:
949 shape = CursorFontShape.XC_top_right_corner;
952 case StdCursor.SizeNS:
953 shape = CursorFontShape.XC_sb_v_double_arrow;
956 case StdCursor.SizeNWSE:
957 shape = CursorFontShape.XC_top_left_corner;
960 case StdCursor.SizeWE:
961 shape = CursorFontShape.XC_sb_h_double_arrow;
964 case StdCursor.UpArrow:
965 shape = CursorFontShape.XC_center_ptr;
968 case StdCursor.VSplit:
969 shape = CursorFontShape.XC_sb_h_double_arrow;
972 case StdCursor.WaitCursor:
973 shape = CursorFontShape.XC_watch;
980 return Xlib.XCreateFontCursor (display, shape);
983 // XXX this should take an X11Hwnd.
984 public void CreateCaret (IntPtr handle, int width, int height)
989 hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
991 if (Caret.Hwnd != IntPtr.Zero)
992 DestroyCaret(Caret.Hwnd);
995 Caret.Window = hwnd.ClientWindow;
997 Caret.Height = height;
998 Caret.Visible = false;
1001 gc_values = new XGCValues();
1002 gc_values.line_width = width;
1004 Caret.gc = Xlib.XCreateGC (display, Caret.Window, new IntPtr ((int)GCFunction.GCLineWidth), ref gc_values);
1005 if (Caret.gc == IntPtr.Zero) {
1006 Caret.Hwnd = IntPtr.Zero;
1010 Xlib.XSetFunction (display, Caret.gc, GXFunction.GXinvert);
1014 // XXX this should take an X11Hwnd.
1015 public void DestroyCaret (IntPtr handle)
1017 if (Caret.Hwnd == handle) {
1018 if (Caret.Visible == true) {
1019 Caret.Timer.Stop ();
1021 if (Caret.gc != IntPtr.Zero) {
1022 Xlib.XFreeGC (display, Caret.gc);
1023 Caret.gc = IntPtr.Zero;
1025 Caret.Hwnd = IntPtr.Zero;
1026 Caret.Visible = false;
1031 public void SetCaretPos (IntPtr handle, int x, int y)
1033 if (Caret.Hwnd == handle) {
1040 if (Caret.Visible == true) {
1042 Caret.Timer.Start();
1047 public void DestroyCursor (IntPtr cursor)
1049 Xlib.XFreeCursor (display, cursor);
1052 private void AccumulateDestroyedHandles (Control c, ArrayList list)
1055 Control[] controls = c.Controls.GetAllControls ();
1057 if (c.IsHandleCreated && !c.IsDisposed) {
1058 X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(c.Handle);
1060 #if DriverDebug || DriverDebugDestroy
1061 Console.WriteLine (" + adding {0} to the list of zombie windows", XplatUI.Window (hwnd.Handle));
1062 Console.WriteLine (" + parent X window is {0:X}", XGetParent (hwnd.WholeWindow).ToInt32());
1066 CleanupCachedWindows (hwnd);
1070 for (int i = 0; i < controls.Length; i ++) {
1071 AccumulateDestroyedHandles (controls[i], list);
1077 void CleanupCachedWindows (X11Hwnd hwnd)
1079 if (ActiveWindow == hwnd) {
1080 SendMessage (hwnd.ClientWindow, Msg.WM_ACTIVATE, (IntPtr)WindowActiveFlags.WA_INACTIVE, IntPtr.Zero);
1081 ActiveWindow = null;
1084 if (FocusWindow == hwnd) {
1085 SendMessage (hwnd.ClientWindow, Msg.WM_KILLFOCUS, IntPtr.Zero, IntPtr.Zero);
1089 if (Grab.Hwnd == hwnd.Handle) {
1090 Grab.Hwnd = IntPtr.Zero;
1091 Grab.Confined = false;
1094 DestroyCaret (hwnd.Handle);
1098 public void DestroyWindow (X11Hwnd hwnd)
1100 CleanupCachedWindows (hwnd);
1102 ArrayList windows = new ArrayList ();
1104 AccumulateDestroyedHandles (Control.ControlNativeWindow.ControlFromHandle(hwnd.Handle), windows);
1106 hwnd.DestroyWindow ();
1108 foreach (X11Hwnd h in windows) {
1109 SendMessage (h.Handle, Msg.WM_DESTROY, IntPtr.Zero, IntPtr.Zero);
1113 public X11Hwnd GetActiveWindow ()
1119 IntPtr prop = IntPtr.Zero;
1120 IntPtr active = IntPtr.Zero;
1122 Xlib.XGetWindowProperty (display, RootWindow.Handle,
1123 Atoms._NET_ACTIVE_WINDOW, IntPtr.Zero, new IntPtr (1), false,
1124 Atoms.XA_WINDOW, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
1126 if (((long)nitems > 0) && (prop != IntPtr.Zero)) {
1127 active = (IntPtr)Marshal.ReadInt32(prop);
1131 return (X11Hwnd)Hwnd.GetObjectFromWindow(active);
1134 public void SetActiveWindow (X11Hwnd new_active_window)
1136 if (new_active_window != ActiveWindow) {
1137 if (ActiveWindow != null)
1138 PostMessage (ActiveWindow.Handle, Msg.WM_ACTIVATE,
1139 (IntPtr)WindowActiveFlags.WA_INACTIVE, IntPtr.Zero);
1141 ActiveWindow = new_active_window;
1143 if (ActiveWindow != null)
1144 PostMessage (ActiveWindow.Handle, Msg.WM_ACTIVATE,
1145 (IntPtr)WindowActiveFlags.WA_ACTIVE, IntPtr.Zero);
1148 if (ModalWindows.Count > 0) {
1149 // Modality handling, if we are modal and the new active window is one
1150 // of ours but not the modal one, switch back to the modal window
1152 if (ActiveWindow != null &&
1153 NativeWindow.FindWindow (ActiveWindow.Handle) != null) {
1154 if (ActiveWindow != (X11Hwnd)ModalWindows.Peek())
1155 ((X11Hwnd)ModalWindows.Peek()).Activate ();
1160 public void GetDisplaySize (out Size size)
1162 XWindowAttributes attributes = new XWindowAttributes();
1164 // FIXME - use _NET_WM messages instead?
1165 Xlib.XGetWindowAttributes (display, RootWindow.Handle, ref attributes);
1167 size = new Size(attributes.width, attributes.height);
1170 // XXX this method doesn't really fit well anywhere in the backend
1171 public SizeF GetAutoScaleSize (Font font)
1175 string magic_string = "The quick brown fox jumped over the lazy dog.";
1176 double magic_number = 44.549996948242189; // XXX my god, where did this number come from?
1178 g = Graphics.FromHwnd (FosterParent.Handle);
1180 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
1181 return new SizeF(width, font.Height);
1184 public void GetCursorPos (X11Hwnd hwnd, out int x, out int y)
1196 use_handle = hwnd.Handle;
1198 use_handle = RootWindow.Handle;
1200 QueryPointer (use_handle, out root, out child, out root_x, out root_y, out win_x, out win_y, out keys_buttons);
1211 public IntPtr GetFocus ()
1213 return FocusWindow.Handle;
1216 public IntPtr GetMousewParam (int Delta)
1220 if ((MouseState & MouseButtons.Left) != 0) {
1221 result |= (int)MsgButtons.MK_LBUTTON;
1224 if ((MouseState & MouseButtons.Middle) != 0) {
1225 result |= (int)MsgButtons.MK_MBUTTON;
1228 if ((MouseState & MouseButtons.Right) != 0) {
1229 result |= (int)MsgButtons.MK_RBUTTON;
1232 Keys mods = ModifierKeys;
1233 if ((mods & Keys.Control) != 0) {
1234 result |= (int)MsgButtons.MK_CONTROL;
1237 if ((mods & Keys.Shift) != 0) {
1238 result |= (int)MsgButtons.MK_SHIFT;
1241 result |= Delta << 16;
1243 return (IntPtr)result;
1246 public void GrabInfo (out IntPtr handle, out bool GrabConfined, out Rectangle GrabArea)
1249 GrabConfined = Grab.Confined;
1250 GrabArea = Grab.Area;
1253 public void GrabWindow (X11Hwnd hwnd, X11Hwnd confine_to)
1255 IntPtr confine_to_window;
1257 confine_to_window = IntPtr.Zero;
1259 if (confine_to != null) {
1260 Console.WriteLine (Environment.StackTrace);
1262 XWindowAttributes attributes = new XWindowAttributes();
1264 Xlib.XGetWindowAttributes (display, confine_to.ClientWindow, ref attributes);
1266 Grab.Area.X = attributes.x;
1267 Grab.Area.Y = attributes.y;
1268 Grab.Area.Width = attributes.width;
1269 Grab.Area.Height = attributes.height;
1270 Grab.Confined = true;
1271 confine_to_window = confine_to.ClientWindow;
1274 Grab.Hwnd = hwnd.ClientWindow;
1276 Xlib.XGrabPointer (display, hwnd.ClientWindow, false,
1277 EventMask.ButtonPressMask | EventMask.ButtonMotionMask |
1278 EventMask.ButtonReleaseMask | EventMask.PointerMotionMask,
1279 GrabMode.GrabModeAsync, GrabMode.GrabModeAsync, confine_to_window, IntPtr.Zero, IntPtr.Zero);
1282 public void UngrabWindow (X11Hwnd hwnd)
1284 Xlib.XUngrabPointer (display, IntPtr.Zero);
1285 Xlib.XFlush (display);
1287 // XXX make sure hwnd is what should have the grab and throw if not
1288 Grab.Hwnd = IntPtr.Zero;
1289 Grab.Confined = false;
1293 private void TranslatePropertyToClipboard (IntPtr property)
1299 IntPtr prop = IntPtr.Zero;
1301 Clipboard.Item = null;
1303 Xlib.XGetWindowProperty (display, FosterParent.Handle,
1304 property, IntPtr.Zero, new IntPtr (0x7fffffff), true,
1305 Atoms.AnyPropertyType, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
1307 if ((long)nitems > 0) {
1308 if (property == Atoms.XA_STRING) {
1309 Clipboard.Item = Marshal.PtrToStringAnsi(prop);
1310 } else if (property == Atoms.XA_BITMAP) {
1311 // FIXME - convert bitmap to image
1312 } else if (property == Atoms.XA_PIXMAP) {
1313 // FIXME - convert pixmap to image
1314 } else if (property == Atoms.OEMTEXT) {
1315 Clipboard.Item = Marshal.PtrToStringAnsi(prop);
1316 } else if (property == Atoms.UNICODETEXT) {
1317 Clipboard.Item = Marshal.PtrToStringAnsi(prop);
1325 // XXX should we be using @handle instead of Atoms.CLIPBOARD here?
1326 public int[] ClipboardAvailableFormats (IntPtr handle)
1328 // XXX deal with the updatemessagequeue stuff
1332 DataFormats.Format f;
1335 f = DataFormats.Format.List;
1337 if (Xlib.XGetSelectionOwner (display, Atoms.CLIPBOARD) == IntPtr.Zero) {
1341 Clipboard.Formats = new ArrayList();
1344 Xlib.XConvertSelection (display, Atoms.CLIPBOARD, (IntPtr)f.Id, (IntPtr)f.Id, FosterParent.Handle, IntPtr.Zero);
1346 Clipboard.Enumerating = true;
1347 while (Clipboard.Enumerating) {
1348 UpdateMessageQueue(null);
1353 result = new int[Clipboard.Formats.Count];
1355 for (int i = 0; i < Clipboard.Formats.Count; i++) {
1356 result[i] = ((IntPtr)Clipboard.Formats[i]).ToInt32 ();
1359 Clipboard.Formats = null;
1364 public void ClipboardClose (IntPtr handle)
1366 if (handle != ClipMagic) {
1367 throw new ArgumentException("handle is not a valid clipboard handle");
1372 public int ClipboardGetID (IntPtr handle, string format)
1374 if (handle != ClipMagic) {
1375 throw new ArgumentException("handle is not a valid clipboard handle");
1378 if (format == "Text" ) return Atoms.XA_STRING.ToInt32();
1379 else if (format == "Bitmap" ) return Atoms.XA_BITMAP.ToInt32();
1380 //else if (format == "MetaFilePict" ) return 3;
1381 //else if (format == "SymbolicLink" ) return 4;
1382 //else if (format == "DataInterchangeFormat" ) return 5;
1383 //else if (format == "Tiff" ) return 6;
1384 else if (format == "OEMText" ) return Atoms.OEMTEXT.ToInt32();
1385 else if (format == "DeviceIndependentBitmap" ) return Atoms.XA_PIXMAP.ToInt32();
1386 else if (format == "Palette" ) return Atoms.XA_COLORMAP.ToInt32(); // Useless
1387 //else if (format == "PenData" ) return 10;
1388 //else if (format == "RiffAudio" ) return 11;
1389 //else if (format == "WaveAudio" ) return 12;
1390 else if (format == "UnicodeText" ) return Atoms.UNICODETEXT.ToInt32();
1391 //else if (format == "EnhancedMetafile" ) return 14;
1392 //else if (format == "FileDrop" ) return 15;
1393 //else if (format == "Locale" ) return 16;
1395 return Xlib.XInternAtom (display, format, false).ToInt32();
1398 public IntPtr ClipboardOpen (bool primary_selection)
1400 if (!primary_selection)
1401 ClipMagic = Atoms.CLIPBOARD;
1403 ClipMagic = Atoms.PRIMARY;
1409 public object ClipboardRetrieve (IntPtr handle, int type, XplatUI.ClipboardToObject converter)
1411 // XXX deal with the UpdateMessageQueue stuff
1415 Xlib.XConvertSelection (display, handle, (IntPtr)type, (IntPtr)type, FosterParent, IntPtr.Zero);
1417 Clipboard.Retrieving = true;
1418 while (Clipboard.Retrieving) {
1419 UpdateMessageQueue(null);
1422 return Clipboard.Item;
1426 public void ClipboardStore (IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter)
1428 Clipboard.Item = obj;
1429 Clipboard.Type = type;
1430 Clipboard.Converter = converter;
1433 Xlib.XSetSelectionOwner (display, Atoms.CLIPBOARD, FosterParent.Handle, IntPtr.Zero);
1435 // Clearing the selection
1436 Xlib.XSetSelectionOwner (display, Atoms.CLIPBOARD, IntPtr.Zero, IntPtr.Zero);
1441 public PaintEventArgs PaintEventStart (IntPtr handle, bool client)
1443 X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
1445 if (Caret.Visible == true) {
1446 Caret.Paused = true;
1450 return hwnd.PaintEventStart (client);
1453 public void PaintEventEnd (IntPtr handle, bool client)
1455 X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
1457 hwnd.PaintEventEnd (client);
1459 if (Caret.Visible == true) {
1461 Caret.Paused = false;
1465 public void SetCursor (IntPtr handle, IntPtr cursor)
1469 if (OverrideCursorHandle == IntPtr.Zero) {
1470 if ((LastCursorWindow == handle) && (LastCursorHandle == cursor))
1473 LastCursorHandle = cursor;
1474 LastCursorWindow = handle;
1476 hwnd = Hwnd.ObjectFromHandle(handle);
1477 if (cursor != IntPtr.Zero)
1478 Xlib.XDefineCursor (display, hwnd.whole_window, cursor);
1480 Xlib.XUndefineCursor (display, hwnd.whole_window);
1481 Xlib.XFlush (display);
1484 hwnd = Hwnd.ObjectFromHandle(handle);
1485 Xlib.XDefineCursor (display, hwnd.whole_window, OverrideCursorHandle);
1489 public DragDropEffects StartDrag (IntPtr handle, object data,
1490 DragDropEffects allowed_effects)
1492 X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle (handle);
1495 throw new ArgumentException ("Attempt to begin drag from invalid window handle (" + handle.ToInt32 () + ").");
1497 return Dnd.StartDrag (hwnd.ClientWindow, data, allowed_effects);
1500 public X11Atoms Atoms {
1501 get { return atoms; }
1504 public int CurrentTimestamp {
1506 TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
1508 return (int) t.TotalSeconds;
1512 public Size CursorSize {
1517 if (Xlib.XQueryBestCursor (display, RootWindow.Handle, 32, 32, out x, out y) != 0) {
1518 return new Size (x, y);
1520 return new Size (16, 16);
1525 public IntPtr Handle {
1526 get { return display; }
1529 public Size IconSize {
1535 if (Xlib.XGetIconSizes (display, RootWindow.Handle, out list, out count) != 0) {
1539 current = (long)list;
1542 size = new XIconSize();
1544 for (int i = 0; i < count; i++) {
1545 size = (XIconSize)Marshal.PtrToStructure((IntPtr)current, size.GetType());
1546 current += Marshal.SizeOf(size);
1548 // Look for our preferred size
1549 if (size.min_width == 32) {
1551 return new Size(32, 32);
1554 if (size.max_width == 32) {
1556 return new Size(32, 32);
1559 if (size.min_width < 32 && size.max_width > 32) {
1562 // check if we can fit one
1564 while (x < size.max_width) {
1565 x += size.width_inc;
1568 return new Size(32, 32);
1573 if (largest < size.max_width) {
1574 largest = size.max_width;
1578 // We didn't find a match or we wouldn't be here
1579 return new Size(largest, largest);
1582 return new Size(32, 32);
1587 public int KeyboardSpeed {
1590 // A lot harder: need to do:
1591 // XkbQueryExtension(0x08051008, 0xbfffdf4c, 0xbfffdf50, 0xbfffdf54, 0xbfffdf58) = 1
1592 // XkbAllocKeyboard(0x08051008, 0xbfffdf4c, 0xbfffdf50, 0xbfffdf54, 0xbfffdf58) = 0x080517a8
1593 // XkbGetControls(0x08051008, 1, 0x080517a8, 0xbfffdf54, 0xbfffdf58) = 0
1595 // And from that we can tell the repetition rate
1597 // Notice, the values must map to:
1598 // [0, 31] which maps to 2.5 to 30 repetitions per second.
1604 public int KeyboardDelay {
1607 // Return values must range from 0 to 4, 0 meaning 250ms,
1608 // and 4 meaning 1000 ms.
1610 return 1; // ie, 500 ms
1614 public int DefaultScreen {
1615 get { return Xlib.XDefaultScreen (display); }
1618 public IntPtr DefaultColormap {
1620 get { return Xlib.XDefaultColormap (display, DefaultScreen); }
1623 public Keys ModifierKeys {
1624 get { return Keyboard.ModifierKeys; }
1627 public IntPtr OverrideCursor {
1628 get { return OverrideCursorHandle; }
1629 set { OverrideCursorHandle = value; }
1632 public X11RootHwnd RootWindow {
1633 get { return root_hwnd; }
1636 public Size SmallIconSize {
1642 if (Xlib.XGetIconSizes (display, RootWindow.Handle, out list, out count) != 0) {
1646 current = (long)list;
1649 size = new XIconSize();
1651 for (int i = 0; i < count; i++) {
1652 size = (XIconSize)Marshal.PtrToStructure((IntPtr)current, size.GetType());
1653 current += Marshal.SizeOf(size);
1655 // Look for our preferred size
1656 if (size.min_width == 16) {
1658 return new Size(16, 16);
1661 if (size.max_width == 16) {
1663 return new Size(16, 16);
1666 if (size.min_width < 16 && size.max_width > 16) {
1669 // check if we can fit one
1671 while (x < size.max_width) {
1672 x += size.width_inc;
1675 return new Size(16, 16);
1680 if (smallest == 0 || smallest > size.min_width) {
1681 smallest = size.min_width;
1685 // We didn't find a match or we wouldn't be here
1686 return new Size(smallest, smallest);
1689 return new Size(16, 16);
1694 public X11Hwnd FosterParent {
1695 get { return foster_hwnd; }
1698 public int MouseHoverTime {
1699 get { return HoverState.Interval; }
1702 public Rectangle WorkingArea {
1708 IntPtr prop = IntPtr.Zero;
1711 int current_desktop;
1715 Xlib.XGetWindowProperty (display, RootWindow.Handle,
1716 Atoms._NET_CURRENT_DESKTOP, IntPtr.Zero, new IntPtr(1), false, Atoms.XA_CARDINAL,
1717 out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
1719 if ((long)nitems < 1) {
1723 current_desktop = Marshal.ReadIntPtr(prop, 0).ToInt32();
1726 Xlib.XGetWindowProperty (display, RootWindow.Handle,
1727 Atoms._NET_WORKAREA, IntPtr.Zero, new IntPtr (256), false, Atoms.XA_CARDINAL,
1728 out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
1730 if ((long)nitems < 4 * current_desktop) {
1734 x = Marshal.ReadIntPtr(prop, IntPtr.Size * 4 * current_desktop).ToInt32();
1735 y = Marshal.ReadIntPtr(prop, IntPtr.Size * 4 * current_desktop + IntPtr.Size).ToInt32();
1736 width = Marshal.ReadIntPtr(prop, IntPtr.Size * 4 * current_desktop + IntPtr.Size * 2).ToInt32();
1737 height = Marshal.ReadIntPtr(prop, IntPtr.Size * 4 * current_desktop + IntPtr.Size * 3).ToInt32();
1740 return new Rectangle(x, y, width, height);
1743 XWindowAttributes attributes = new XWindowAttributes();
1745 Xlib.XGetWindowAttributes (display, RootWindow.Handle, ref attributes);
1747 return new Rectangle(0, 0, attributes.width, attributes.height);
1751 private void XEventThread ()
1755 Syscall.poll (pollfds, 1U, -1);
1757 while (Xlib.XPending (display) > 0) {
1759 XEvent xevent = new XEvent ();
1760 Xlib.XNextEvent (display, ref xevent);
1762 // this is kind of a gross place to put this, but we don't know about the
1763 // key repeat state in X11ThreadQueue, nor to we want the queue code calling
1765 if (!detectable_key_auto_repeat &&
1766 xevent.type == XEventName.KeyRelease &&
1767 Xlib.XPending (display) > 0) {
1769 XEvent nextevent = new XEvent ();
1770 Xlib.XPeekEvent (display, ref nextevent);
1772 if (nextevent.type == XEventName.KeyPress &&
1773 nextevent.KeyEvent.keycode == xevent.KeyEvent.keycode &&
1774 nextevent.KeyEvent.time == xevent.KeyEvent.time) {
1779 X11Hwnd hwnd = (X11Hwnd)Hwnd.GetObjectFromWindow(xevent.AnyEvent.window);
1781 hwnd.Queue.Enqueue (xevent);
1788 private void RedirectMsgToEnabledAncestor (X11Hwnd hwnd, MSG msg, IntPtr window,
1789 ref int event_x, ref int event_y)
1794 msg.hwnd = hwnd.EnabledHwnd;
1795 Xlib.XTranslateCoordinates (display, window,
1796 Hwnd.ObjectFromHandle(msg.hwnd).ClientWindow,
1798 out x, out y, out dummy);
1801 msg.lParam = (IntPtr)(MousePosition.Y << 16 | MousePosition.X);
1805 // This is called from the thread owning the corresponding X11ThreadQueue
1806 [MonoTODO("Implement filtering")]
1807 public bool GetMessage (object queue_id, ref MSG msg, IntPtr handle, int wFilterMin, int wFilterMax)
1809 X11ThreadQueue queue = (X11ThreadQueue)queue_id;
1812 bool got_xevent = false;
1818 got_xevent = queue.Dequeue (out xevent);
1822 Console.WriteLine (">");
1823 Console.Out.Flush ();
1829 Console.Write ("-");
1830 Console.Out.Flush ();
1833 hwnd = (X11Hwnd)Hwnd.GetObjectFromWindow (xevent.AnyEvent.window);
1835 // Handle messages for windows that are already or are about to be destroyed.
1837 // we need a special block for this because unless we remove the hwnd from the paint
1838 // queue it will always stay there (since we don't handle the expose), and we'll
1839 // effectively loop infinitely trying to repaint a non-existant window.
1840 if (hwnd != null && hwnd.zombie && xevent.type == XEventName.Expose) {
1841 hwnd.PendingExpose = hwnd.PendingNCExpose = false;
1842 goto ProcessNextMessage;
1845 // We need to make sure we only allow DestroyNotify events through for zombie
1846 // hwnds, since much of the event handling code makes requests using the hwnd's
1847 // ClientWindow, and that'll result in BadWindow errors if there's some lag
1848 // between the XDestroyWindow call and the DestroyNotify event.
1849 if (hwnd == null || hwnd.zombie) {
1850 #if DriverDebug || DriverDebugDestroy
1851 Console.WriteLine("GetMessage(): Got message {0} for non-existent or already destroyed window {1:X}",
1852 xevent.type, xevent.AnyEvent.window.ToInt32());
1854 goto ProcessNextMessage;
1857 client = hwnd.ClientWindow == xevent.AnyEvent.window;
1859 msg.hwnd = hwnd.Handle;
1861 switch (xevent.type) {
1862 case XEventName.KeyPress:
1863 Keyboard.KeyEvent (FocusWindow.Handle, xevent, ref msg);
1866 case XEventName.KeyRelease:
1867 Keyboard.KeyEvent (FocusWindow.Handle, xevent, ref msg);
1870 case XEventName.ButtonPress: {
1871 switch(xevent.ButtonEvent.button) {
1873 MouseState |= MouseButtons.Left;
1875 msg.message = Msg.WM_LBUTTONDOWN;
1877 msg.message = Msg.WM_NCLBUTTONDOWN;
1878 hwnd.MenuToScreen (ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
1880 // TODO: For WM_NCLBUTTONDOWN wParam specifies a hit-test value not the virtual keys down
1881 msg.wParam=GetMousewParam(0);
1885 MouseState |= MouseButtons.Middle;
1887 msg.message = Msg.WM_MBUTTONDOWN;
1889 msg.message = Msg.WM_NCMBUTTONDOWN;
1890 hwnd.MenuToScreen (ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
1892 msg.wParam=GetMousewParam(0);
1896 MouseState |= MouseButtons.Right;
1898 msg.message = Msg.WM_RBUTTONDOWN;
1900 msg.message = Msg.WM_NCRBUTTONDOWN;
1901 hwnd.MenuToScreen (ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
1903 msg.wParam=GetMousewParam(0);
1907 msg.hwnd = FocusWindow.Handle;
1908 msg.message=Msg.WM_MOUSEWHEEL;
1909 msg.wParam=GetMousewParam(120);
1913 msg.hwnd = FocusWindow.Handle;
1914 msg.message=Msg.WM_MOUSEWHEEL;
1915 msg.wParam=GetMousewParam(-120);
1919 msg.lParam=(IntPtr) (xevent.ButtonEvent.y << 16 | xevent.ButtonEvent.x);
1920 MousePosition.X = xevent.ButtonEvent.x;
1921 MousePosition.Y = xevent.ButtonEvent.y;
1923 if (!hwnd.Enabled) {
1924 RedirectMsgToEnabledAncestor (hwnd, msg, xevent.AnyEvent.window,
1925 ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
1928 if (Grab.Hwnd != IntPtr.Zero)
1929 msg.hwnd = Grab.Hwnd;
1931 if (ClickPending.Pending &&
1932 ((((long)xevent.ButtonEvent.time - ClickPending.Time) < DoubleClickInterval) &&
1933 (msg.wParam == ClickPending.wParam) &&
1934 (msg.lParam == ClickPending.lParam) &&
1935 (msg.message == ClickPending.Message))) {
1936 // Looks like a genuine double click, clicked twice on the same spot with the same keys
1937 switch(xevent.ButtonEvent.button) {
1939 msg.message = client ? Msg.WM_LBUTTONDBLCLK : Msg.WM_NCLBUTTONDBLCLK;
1943 msg.message = client ? Msg.WM_MBUTTONDBLCLK : Msg.WM_NCMBUTTONDBLCLK;
1947 msg.message = client ? Msg.WM_RBUTTONDBLCLK : Msg.WM_NCRBUTTONDBLCLK;
1951 ClickPending.Pending = false;
1955 ClickPending.Pending = true;
1956 ClickPending.Hwnd = msg.hwnd;
1957 ClickPending.Message = msg.message;
1958 ClickPending.wParam = msg.wParam;
1959 ClickPending.lParam = msg.lParam;
1960 ClickPending.Time = (long)xevent.ButtonEvent.time;
1966 case XEventName.ButtonRelease:
1968 Dnd.HandleButtonRelease (ref xevent);
1969 // Don't return here, so that the BUTTONUP message can get through
1972 switch(xevent.ButtonEvent.button) {
1975 msg.message = Msg.WM_LBUTTONUP;
1977 msg.message = Msg.WM_NCLBUTTONUP;
1978 hwnd.MenuToScreen (ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
1980 MouseState &= ~MouseButtons.Left;
1981 msg.wParam=GetMousewParam(0);
1986 msg.message = Msg.WM_MBUTTONUP;
1988 msg.message = Msg.WM_NCMBUTTONUP;
1989 hwnd.MenuToScreen (ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
1991 MouseState &= ~MouseButtons.Middle;
1992 msg.wParam=GetMousewParam(0);
1997 msg.message = Msg.WM_RBUTTONUP;
1999 msg.message = Msg.WM_NCRBUTTONUP;
2000 hwnd.MenuToScreen (ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2002 MouseState &= ~MouseButtons.Right;
2003 msg.wParam=GetMousewParam(0);
2007 goto ProcessNextMessage;
2010 goto ProcessNextMessage;
2013 if (!hwnd.Enabled) {
2014 RedirectMsgToEnabledAncestor (hwnd, msg, xevent.AnyEvent.window,
2015 ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2018 if (Grab.Hwnd != IntPtr.Zero)
2019 msg.hwnd = Grab.Hwnd;
2021 msg.lParam=(IntPtr) (xevent.ButtonEvent.y << 16 | xevent.ButtonEvent.x);
2022 MousePosition.X = xevent.ButtonEvent.x;
2023 MousePosition.Y = xevent.ButtonEvent.y;
2026 case XEventName.MotionNotify:
2027 /* XXX move the compression stuff here */
2030 #if DriverDebugExtra
2031 Console.WriteLine("GetMessage(): Window {0:X} MotionNotify x={1} y={2}",
2032 client ? hwnd.ClientWindow.ToInt32() : hwnd.WholeWindow.ToInt32(),
2033 xevent.MotionEvent.x, xevent.MotionEvent.y);
2036 if (Dnd.HandleMotionNotify (ref xevent))
2037 goto ProcessNextMessage;
2039 if (Grab.Hwnd != IntPtr.Zero)
2040 msg.hwnd = Grab.Hwnd;
2042 NativeWindow.WndProc(msg.hwnd, Msg.WM_SETCURSOR, msg.hwnd, (IntPtr)HitTest.HTCLIENT);
2044 msg.message = Msg.WM_MOUSEMOVE;
2045 msg.wParam = GetMousewParam(0);
2046 msg.lParam = (IntPtr) (xevent.MotionEvent.y << 16 | xevent.MotionEvent.x & 0xFFFF);
2048 if (!hwnd.Enabled) {
2049 RedirectMsgToEnabledAncestor (hwnd, msg, xevent.AnyEvent.window,
2050 ref xevent.MotionEvent.x, ref xevent.MotionEvent.y);
2053 MousePosition.X = xevent.MotionEvent.x;
2054 MousePosition.Y = xevent.MotionEvent.y;
2056 if ((HoverState.Timer.Enabled) &&
2057 (((MousePosition.X + HoverState.Size.Width) < HoverState.X) ||
2058 ((MousePosition.X - HoverState.Size.Width) > HoverState.X) ||
2059 ((MousePosition.Y + HoverState.Size.Height) < HoverState.Y) ||
2060 ((MousePosition.Y - HoverState.Size.Height) > HoverState.Y))) {
2062 HoverState.Timer.Stop();
2063 HoverState.Timer.Start();
2064 HoverState.X = MousePosition.X;
2065 HoverState.Y = MousePosition.Y;
2074 #if DriverDebugExtra
2075 Console.WriteLine("GetMessage(): non-client area {0:X} MotionNotify x={1} y={2}",
2076 client ? hwnd.ClientWindow.ToInt32() : hwnd.WholeWindow.ToInt32(),
2077 xevent.MotionEvent.x, xevent.MotionEvent.y);
2079 msg.message = Msg.WM_NCMOUSEMOVE;
2081 if (!hwnd.Enabled) {
2082 RedirectMsgToEnabledAncestor (hwnd, msg, xevent.AnyEvent.window,
2083 ref xevent.MotionEvent.x, ref xevent.MotionEvent.y);
2086 // The hit test is sent in screen coordinates
2087 Xlib.XTranslateCoordinates (display, xevent.AnyEvent.window, RootWindow.Handle,
2088 xevent.MotionEvent.x, xevent.MotionEvent.y,
2089 out screen_x, out screen_y, out dummy);
2091 msg.lParam = (IntPtr) (screen_y << 16 | screen_x & 0xFFFF);
2092 ht = (HitTest)NativeWindow.WndProc (hwnd.ClientWindow, Msg.WM_NCHITTEST,
2093 IntPtr.Zero, msg.lParam).ToInt32 ();
2094 NativeWindow.WndProc(hwnd.ClientWindow, Msg.WM_SETCURSOR, msg.hwnd, (IntPtr)ht);
2096 MousePosition.X = xevent.MotionEvent.x;
2097 MousePosition.Y = xevent.MotionEvent.y;
2102 case XEventName.EnterNotify:
2104 goto ProcessNextMessage;
2106 if (xevent.CrossingEvent.mode != NotifyMode.NotifyNormal)
2107 goto ProcessNextMessage;
2109 msg.message = Msg.WM_MOUSE_ENTER;
2110 HoverState.X = xevent.CrossingEvent.x;
2111 HoverState.Y = xevent.CrossingEvent.y;
2112 HoverState.Timer.Enabled = true;
2113 HoverState.Window = xevent.CrossingEvent.window;
2117 case XEventName.LeaveNotify:
2119 goto ProcessNextMessage;
2121 if ((xevent.CrossingEvent.mode != NotifyMode.NotifyNormal) ||
2122 (xevent.CrossingEvent.window != hwnd.ClientWindow))
2123 goto ProcessNextMessage;
2125 msg.message=Msg.WM_MOUSE_LEAVE;
2126 HoverState.Timer.Enabled = false;
2127 HoverState.Window = IntPtr.Zero;
2131 case XEventName.ReparentNotify:
2132 if (hwnd.parent == null) { // Toplevel
2133 if ((xevent.ReparentEvent.parent != IntPtr.Zero) && (xevent.ReparentEvent.window == hwnd.WholeWindow)) {
2134 // We need to adjust x/y
2135 // This sucks ass, part 2
2136 // Every WM does the reparenting of toplevel windows different, so there's
2137 // no standard way of getting our adjustment considering frames/decorations
2138 // The code below is needed for metacity. KDE doesn't works just fine without this
2146 hwnd.Reparented = true;
2148 Xlib.XGetGeometry(display, XGetParent(hwnd.WholeWindow),
2149 out dummy_ptr, out new_x, out new_y,
2150 out dummy_int, out dummy_int, out dummy_int, out dummy_int);
2151 hwnd.FrameExtents(out frame_left, out frame_top);
2152 if ((frame_left != 0) && (frame_top != 0) && (new_x != frame_left) && (new_y != frame_top)) {
2155 hwnd.whacky_wm = true;
2158 if (hwnd.opacity != 0xffffffff) {
2161 opacity = (IntPtr)(Int32)hwnd.opacity;
2162 Xlib.XChangeProperty (display, XGetParent(hwnd.WholeWindow),
2163 Atoms._NET_WM_WINDOW_OPACITY, Atoms.XA_CARDINAL, 32,
2164 PropertyMode.Replace, ref opacity, 1);
2166 SendMessage(msg.hwnd, Msg.WM_WINDOWPOSCHANGED, msg.wParam, msg.lParam);
2167 goto ProcessNextMessage;
2169 hwnd.Reparented = false;
2170 goto ProcessNextMessage;
2173 goto ProcessNextMessage;
2175 case XEventName.ConfigureNotify:
2176 hwnd.HandleConfigureNotify (xevent);
2177 goto ProcessNextMessage;
2179 case XEventName.FocusIn:
2180 // We received focus. We use X11 focus only to know if the app window does or does not have focus
2181 // We do not track the actual focussed window via it. Instead, this is done via FocusWindow internally
2182 // Receiving focus means we've gotten activated and therefore we need to let the actual FocusWindow know
2183 // about it having focus again
2184 if (xevent.FocusChangeEvent.detail != NotifyDetail.NotifyNonlinear)
2185 goto ProcessNextMessage;
2187 if (FocusWindow == null) {
2188 Control c = Control.FromHandle (hwnd.ClientWindow);
2190 goto ProcessNextMessage;
2191 Form form = c.FindForm ();
2193 goto ProcessNextMessage;
2194 X11Hwnd new_active = (X11Hwnd)Hwnd.ObjectFromHandle (form.Handle);
2195 if (ActiveWindow != new_active) {
2196 ActiveWindow = new_active;
2197 SendMessage (ActiveWindow.Handle, Msg.WM_ACTIVATE, (IntPtr) WindowActiveFlags.WA_ACTIVE, IntPtr.Zero);
2199 goto ProcessNextMessage;
2201 Keyboard.FocusIn(FocusWindow.Handle);
2202 SendMessage(FocusWindow.Handle, Msg.WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero);
2203 goto ProcessNextMessage;
2205 case XEventName.FocusOut:
2206 // Se the comment for our FocusIn handler
2207 if (xevent.FocusChangeEvent.detail != NotifyDetail.NotifyNonlinear)
2208 goto ProcessNextMessage;
2210 if (FocusWindow == null)
2211 goto ProcessNextMessage;
2213 Keyboard.FocusOut(FocusWindow.Handle);
2215 while (Keyboard.ResetKeyState(FocusWindow.Handle, ref msg))
2216 SendMessage(FocusWindow.Handle, msg.message, msg.wParam, msg.lParam);
2218 SendMessage(FocusWindow.Handle, Msg.WM_KILLFOCUS, IntPtr.Zero, IntPtr.Zero);
2219 goto ProcessNextMessage;
2221 case XEventName.Expose:
2222 if (queue.PostQuitState || !hwnd.Mapped) {
2223 hwnd.PendingExpose = hwnd.PendingNCExpose = false;
2227 msg.hwnd = hwnd.Handle;
2230 #if DriverDebugExtra
2231 Console.WriteLine("GetMessage(): Window {0:X} Exposed area {1},{2} {3}x{4}",
2232 hwnd.client_window.ToInt32(),
2233 xevent.ExposeEvent.x, xevent.ExposeEvent.y,
2234 xevent.ExposeEvent.width, xevent.ExposeEvent.height);
2236 msg.message = Msg.WM_PAINT;
2241 switch (hwnd.border_style) {
2242 case FormBorderStyle.Fixed3D:
2243 g = Graphics.FromHwnd(hwnd.WholeWindow);
2244 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height),
2245 Border3DStyle.Sunken);
2249 case FormBorderStyle.FixedSingle:
2250 g = Graphics.FromHwnd(hwnd.WholeWindow);
2251 ControlPaint.DrawBorder(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height),
2252 Color.Black, ButtonBorderStyle.Solid);
2256 #if DriverDebugExtra
2257 Console.WriteLine("GetMessage(): Window {0:X} Exposed non-client area {1},{2} {3}x{4}",
2258 hwnd.ClientWindow.ToInt32(),
2259 xevent.ExposeEvent.x, xevent.ExposeEvent.y,
2260 xevent.ExposeEvent.width, xevent.ExposeEvent.height);
2263 Rectangle rect = new Rectangle (xevent.ExposeEvent.x, xevent.ExposeEvent.y,
2264 xevent.ExposeEvent.width, xevent.ExposeEvent.height);
2265 Region region = new Region (rect);
2266 IntPtr hrgn = region.GetHrgn (null); // Graphics object isn't needed
2267 msg.message = Msg.WM_NCPAINT;
2268 msg.wParam = hrgn == IntPtr.Zero ? (IntPtr)1 : hrgn;
2269 msg.refobject = region;
2274 case XEventName.DestroyNotify:
2276 // This is a bit tricky, we don't receive our own DestroyNotify, we only get those for our children
2277 hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(xevent.DestroyWindowEvent.window);
2279 // We may get multiple for the same window, act only one the first (when Hwnd still knows about it)
2280 if ((hwnd != null) && (hwnd.ClientWindow == xevent.DestroyWindowEvent.window)) {
2281 CleanupCachedWindows (hwnd);
2283 #if DriverDebugDestroy
2284 Console.WriteLine("Received X11 Destroy Notification for {0}", XplatUI.Window(hwnd.ClientWindow));
2287 msg.hwnd = hwnd.ClientWindow;
2288 msg.message=Msg.WM_DESTROY;
2292 goto ProcessNextMessage;
2296 case XEventName.ClientMessage:
2297 if (Dnd.HandleClientMessage (ref xevent))
2298 goto ProcessNextMessage;
2300 if (xevent.ClientMessageEvent.message_type == Atoms.AsyncAtom) {
2301 XplatUIDriverSupport.ExecuteClientMessage((GCHandle)xevent.ClientMessageEvent.ptr1);
2302 goto ProcessNextMessage;
2305 if (xevent.ClientMessageEvent.message_type == HoverState.Atom) {
2306 msg.message = Msg.WM_MOUSEHOVER;
2307 msg.wParam = GetMousewParam(0);
2308 msg.lParam = (IntPtr) (xevent.ClientMessageEvent.ptr1);
2312 if (xevent.ClientMessageEvent.message_type == Atoms.PostAtom) {
2313 msg.hwnd = xevent.ClientMessageEvent.ptr1;
2314 msg.message = (Msg) xevent.ClientMessageEvent.ptr2.ToInt32 ();
2315 msg.wParam = xevent.ClientMessageEvent.ptr3;
2316 msg.lParam = xevent.ClientMessageEvent.ptr4;
2320 if (xevent.ClientMessageEvent.message_type == Atoms._XEMBED) {
2321 #if DriverDebugXEmbed
2322 Console.WriteLine("GOT EMBED MESSAGE {0:X}, detail {1:X}",
2323 xevent.ClientMessageEvent.ptr2.ToInt32(), xevent.ClientMessageEvent.ptr3.ToInt32());
2326 if (xevent.ClientMessageEvent.ptr2.ToInt32() == (int)XEmbedMessage.EmbeddedNotify) {
2327 XSizeHints hints = new XSizeHints();
2330 Xlib.XGetWMNormalHints (display, hwnd.WholeWindow, ref hints, out dummy);
2332 hwnd.width = hints.max_width;
2333 hwnd.height = hints.max_height;
2334 hwnd.ClientRect = Rectangle.Empty;
2335 SendMessage(msg.hwnd, Msg.WM_WINDOWPOSCHANGED, IntPtr.Zero, IntPtr.Zero);
2339 if (xevent.ClientMessageEvent.message_type == Atoms.WM_PROTOCOLS) {
2340 if (xevent.ClientMessageEvent.ptr1 == Atoms.WM_DELETE_WINDOW) {
2341 msg.message = Msg.WM_CLOSE;
2345 // We should not get this, but I'll leave the code in case we need it in the future
2346 if (xevent.ClientMessageEvent.ptr1 == Atoms.WM_TAKE_FOCUS) {
2347 goto ProcessNextMessage;
2350 goto ProcessNextMessage;
2352 case XEventName.PropertyNotify:
2353 // The Hwnd's themselves handle this
2354 hwnd.PropertyChanged (xevent);
2355 goto ProcessNextMessage;
2360 // XXX need to figure out how to post
2361 // WM_ENTERIDLE only in specific conditions
2362 // (we've handled some input events)
2363 if (!queue.PostQuitState) {
2364 msg.hwnd= IntPtr.Zero;
2365 msg.message = Msg.WM_ENTERIDLE;
2369 goto ProcessNextMessage;
2372 // We reset ourselves so GetMessage can be called again
2373 queue.PostQuitState = false;
2378 [MonoTODO("Implement filtering and PM_NOREMOVE")]
2379 public bool PeekMessage (object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags)
2381 X11ThreadQueue queue = (X11ThreadQueue) queue_id;
2384 if ((flags & (uint)PeekMessageFlags.PM_REMOVE) == 0) {
2385 throw new NotImplementedException("PeekMessage PM_NOREMOVE is not implemented yet"); // FIXME - Implement PM_NOREMOVE flag
2391 if (queue.CountUnlocked > 0)
2401 queue.CheckTimers ();
2406 return GetMessage(queue_id, ref msg, hWnd, wFilterMin, wFilterMax);
2409 public void DoEvents (X11ThreadQueue queue)
2411 MSG msg = new MSG ();
2413 if (OverrideCursorHandle != IntPtr.Zero)
2414 OverrideCursorHandle = IntPtr.Zero;
2416 queue.DispatchIdle = false;
2418 while (PeekMessage(queue, ref msg, IntPtr.Zero, 0, 0, (uint)PeekMessageFlags.PM_REMOVE)) {
2419 TranslateMessage (ref msg);
2420 DispatchMessage (ref msg);
2423 queue.DispatchIdle = true;
2426 // double buffering support
2427 public void CreateOffscreenDrawable (IntPtr handle,
2428 int width, int height,
2429 out object offscreen_drawable)
2432 int x_out, y_out, width_out, height_out, border_width_out, depth_out;
2434 Xlib.XGetGeometry (display, handle,
2436 out x_out, out y_out,
2437 out width_out, out height_out,
2438 out border_width_out, out depth_out);
2440 IntPtr pixmap = Xlib.XCreatePixmap (display, handle, width, height, depth_out);
2442 offscreen_drawable = pixmap;
2445 public void DestroyOffscreenDrawable (object offscreen_drawable)
2447 Xlib.XFreePixmap (display, (IntPtr)offscreen_drawable);
2450 public Graphics GetOffscreenGraphics (object offscreen_drawable)
2452 return Graphics.FromHwnd ((IntPtr) offscreen_drawable);
2455 public void BlitFromOffscreen (IntPtr dest_handle,
2457 object offscreen_drawable,
2458 Graphics offscreen_dc,
2461 XGCValues gc_values;
2464 gc_values = new XGCValues();
2466 gc = Xlib.XCreateGC (display, dest_handle, IntPtr.Zero, ref gc_values);
2468 Xlib.XCopyArea (display, (IntPtr)offscreen_drawable, dest_handle,
2469 gc, r.X, r.Y, r.Width, r.Height, r.X, r.Y);
2471 Xlib.XFreeGC (display, gc);
2475 // reversible screen-level drawing
2476 IntPtr GetReversibleScreenGC (Color backColor)
2478 XGCValues gc_values;
2482 XColor xcolor = new XColor();
2483 xcolor.red = (ushort)(backColor.R * 257);
2484 xcolor.green = (ushort)(backColor.G * 257);
2485 xcolor.blue = (ushort)(backColor.B * 257);
2486 Xlib.XAllocColor (display, DefaultColormap, ref xcolor);
2487 pixel = (uint)xcolor.pixel.ToInt32();
2490 gc_values = new XGCValues();
2492 gc_values.subwindow_mode = GCSubwindowMode.IncludeInferiors;
2493 gc_values.foreground = (IntPtr)pixel;
2495 gc = Xlib.XCreateGC (display, RootWindow.Handle, new IntPtr ((int) (GCFunction.GCSubwindowMode | GCFunction.GCForeground)), ref gc_values);
2496 Xlib.XSetForeground (display, gc, (UIntPtr)pixel);
2497 Xlib.XSetFunction (display, gc, GXFunction.GXxor);
2502 public void DrawReversibleLine (Point start, Point end, Color backColor)
2504 IntPtr gc = GetReversibleScreenGC (backColor);
2506 Xlib.XDrawLine (display, RootWindow.Handle, gc, start.X, start.Y, end.X, end.Y);
2508 Xlib.XFreeGC (display, gc);
2511 public void FillReversibleRectangle (Rectangle rectangle, Color backColor)
2513 IntPtr gc = GetReversibleScreenGC (backColor);
2515 if (rectangle.Width < 0) {
2516 rectangle.X += rectangle.Width;
2517 rectangle.Width = -rectangle.Width;
2519 if (rectangle.Height < 0) {
2520 rectangle.Y += rectangle.Height;
2521 rectangle.Height = -rectangle.Height;
2524 Xlib.XFillRectangle (display, RootWindow.Handle, gc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
2526 Xlib.XFreeGC (display, gc);
2529 public void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style)
2531 IntPtr gc = GetReversibleScreenGC (backColor);
2533 if (rectangle.Width < 0) {
2534 rectangle.X += rectangle.Width;
2535 rectangle.Width = -rectangle.Width;
2537 if (rectangle.Height < 0) {
2538 rectangle.Y += rectangle.Height;
2539 rectangle.Height = -rectangle.Height;
2543 GCLineStyle line_style = GCLineStyle.LineSolid;
2544 GCCapStyle cap_style = GCCapStyle.CapButt;
2545 GCJoinStyle join_style = GCJoinStyle.JoinMiter;
2548 case FrameStyle.Dashed:
2549 line_style = GCLineStyle.LineOnOffDash;
2551 case FrameStyle.Thick:
2556 Xlib.XSetLineAttributes (display, gc, line_width, line_style, cap_style, join_style);
2558 Xlib.XDrawRectangle (display, RootWindow.Handle, gc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
2560 Xlib.XFreeGC (display, gc);