Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XplatUIX11.cs
index d1a5990883508d8af0e13e19acbbbc7be39f59b8..c903781a906bd7a2c565fecb64218f64432376d3 100644 (file)
@@ -499,6 +499,12 @@ namespace System.Windows.Forms {
 
                                wake = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                                wake.Connect(listen.LocalEndPoint);
+
+                               // Make this non-blocking, so it doesn't
+                               // deadlock if too many wakes are sent
+                               // before the wake_receive end is polled
+                               wake.Blocking = false;
+
                                wake_receive = listen.Accept();
 
                                #if __MonoCS__
@@ -1230,7 +1236,13 @@ namespace System.Windows.Forms {
                }
 
                void WakeupMain () {
-                       wake.Send (new byte [] { 0xFF });
+                       try {
+                               wake.Send (new byte [] { 0xFF });
+                       } catch (SocketException ex) {
+                               if (ex.SocketErrorCode != SocketError.WouldBlock) {
+                                       throw;
+                               }
+                       }
                }
 
                XEventQueue ThreadQueue(Thread thread) {
@@ -3756,6 +3768,10 @@ namespace System.Windows.Forms {
                        if (((long)nitems > 0) && (prop != IntPtr.Zero)) {
                                active = (IntPtr)Marshal.ReadInt32(prop);
                                XFree(prop);
+                       } else {
+                               // The window manager does not support _NET_ACTIVE_WINDOW.  Fall back to XGetInputFocus.
+                               IntPtr  revert_to = IntPtr.Zero;
+                               XGetInputFocus(DisplayHandle, out active, out revert_to);
                        }
 
                        if (active != IntPtr.Zero) {
@@ -6199,19 +6215,15 @@ namespace System.Windows.Forms {
                                tt.Dispose();
                                tt = null;
                        }
-#if NET_2_0
                        // Close any balloon window *we* fired.
                        ThemeEngine.Current.HideBalloonWindow (handle);
-#endif
                }
 
-#if NET_2_0
                internal override void SystrayBalloon(IntPtr handle, int timeout, string title, string text, ToolTipIcon icon)
                {
                        ThemeEngine.Current.ShowBalloonWindow (handle, timeout, title, text, icon);
                        SendMessage(handle, Msg.WM_USER, IntPtr.Zero, (IntPtr) Msg.NIN_BALLOONSHOW);    
                }
-#endif
 
                internal override bool Text(IntPtr handle, string text)
 {
@@ -7518,6 +7530,9 @@ namespace System.Windows.Forms {
 
                [DllImport ("libX11", EntryPoint="XIfEvent")]
                internal extern static void XIfEvent (IntPtr display, ref XEvent xevent, Delegate event_predicate, IntPtr arg);
+
+               [DllImport ("libX11", EntryPoint="XGetInputFocus")]
+               internal extern static void XGetInputFocus (IntPtr display, out IntPtr focus, out IntPtr revert_to);
                #endregion
 #endif
        }