177784
commit 88bdce139baf89839b6e13d698576fc56211e845
177784
Author: Oleksiy Chernyavskyy <ochern@ics.com>
177784
Date:   Wed Mar 16 00:46:49 2016 +0200
177784
177784
    Reimplemented bugfix 1565
177784
    
177784
    Signed-off-by: Oleksiy Chernyavskyy <ochern@ics.com>
177784
177784
diff --git a/lib/Xm/ComboBox.c b/lib/Xm/ComboBox.c
177784
index 1472e45..cf507da 100644
177784
--- a/lib/Xm/ComboBox.c
177784
+++ b/lib/Xm/ComboBox.c
177784
@@ -3164,6 +3164,9 @@ CreatePulldown(Widget    parent,
177784
   Arg      args[4];
177784
   ArgList  merged_args;
177784
   Cardinal n;
177784
+#ifdef FIX_1565
177784
+  XmGrabShellWidget grabsh;
177784
+#endif
177784
 
177784
   n = 0;
177784
   XtSetArg(args[n], XmNlayoutDirection, LayoutM(parent)), n++;
177784
@@ -3175,6 +3178,11 @@ CreatePulldown(Widget    parent,
177784
 			     merged_args, n + *num_args);
177784
   XtFree((char*)merged_args);
177784
 
177784
+#ifdef FIX_1565
177784
+  grabsh = (XmGrabShellWidget) shell;
177784
+  grabsh->grab_shell.set_input_focus = False;
177784
+#endif
177784
+
177784
   return shell;
177784
 }
177784
 
177784
diff --git a/lib/Xm/DropDown.c b/lib/Xm/DropDown.c
177784
index 37fec03..5cd15ca 100644
177784
--- a/lib/Xm/DropDown.c
177784
+++ b/lib/Xm/DropDown.c
177784
@@ -2027,6 +2027,9 @@ CreatePopup(Widget w, ArgList args, Cardinal num_args)
177784
     Arg *new_list, largs[10];
177784
     Cardinal num_largs;
177784
     Widget sb;
177784
+#ifdef FIX_1565
177784
+    XmGrabShellWidget grabsh;
177784
+#endif
177784
 
177784
     num_largs = 0;
177784
     XtSetArg(largs[num_largs], XmNoverrideRedirect, True); num_largs++;
177784
@@ -2040,6 +2043,10 @@ CreatePopup(Widget w, ArgList args, Cardinal num_args)
177784
 						xmGrabShellWidgetClass, w,
177784
 						new_list,
177784
 						num_largs + num_args);
177784
+#ifdef FIX_1565
177784
+    grabsh = (XmGrabShellWidget) XmDropDown_popup_shell(cbw);
177784
+    grabsh->grab_shell.set_input_focus = False;
177784
+#endif
177784
     XtFree((char *) new_list);
177784
 
177784
 #ifdef FIX_1446
177784
diff --git a/lib/Xm/GrabShell.c b/lib/Xm/GrabShell.c
177784
index 88f3154..af13e0b 100644
177784
--- a/lib/Xm/GrabShell.c
177784
+++ b/lib/Xm/GrabShell.c
177784
@@ -283,6 +283,10 @@ Initialize(Widget req,		/* unused */
177784
 
177784
   /* CR 9920:  Popdown may be requested before MapNotify. */
177784
   grabsh->grab_shell.mapped = False;
177784
+
177784
+#ifdef FIX_1565
177784
+  grabsh->grab_shell.set_input_focus = True;
177784
+#endif
177784
 }
177784
 
177784
 /*
177784
@@ -395,8 +399,16 @@ MapNotifyHandler(Widget shell, XtPointer client_data,
177784
   XGetInputFocus(XtDisplay(shell), &grabshell->grab_shell.old_focus,
177784
 		 &grabshell->grab_shell.old_revert_to);
177784
   old_handler = XSetErrorHandler(IgnoreXErrors);
177784
-  XSetInputFocus(XtDisplay(shell), XtWindow(shell), RevertToParent, time);
177784
-  XSync(XtDisplay(shell), False);
177784
+#ifdef FIX_1565
177784
+  if (! grabshell->grab_shell.set_input_focus) {
177784
+    XmForceGrabKeyboard(shell, time);
177784
+  } else {
177784
+#endif
177784
+    XSetInputFocus(XtDisplay(shell), XtWindow(shell), RevertToParent, time);
177784
+    XSync(XtDisplay(shell), False);
177784
+#ifdef FIX_1565
177784
+  }
177784
+#endif
177784
   XSetErrorHandler(old_handler);
177784
 }
177784
 
177784
diff --git a/lib/Xm/GrabShellP.h b/lib/Xm/GrabShellP.h
177784
index 92fe508..025f001 100644
177784
--- a/lib/Xm/GrabShellP.h
177784
+++ b/lib/Xm/GrabShellP.h
177784
@@ -30,6 +30,7 @@
177784
 #include <Xm/GrabShell.h>
177784
 #include <Xm/XmP.h>
177784
 #include <X11/ShellP.h>
177784
+#include "XmI.h"
177784
 
177784
 #ifdef __cplusplus
177784
 extern "C" {
177784
@@ -55,6 +56,9 @@ typedef struct
177784
   Boolean	mapped;
177784
   Window	old_focus;
177784
   int		old_revert_to;
177784
+#ifdef FIX_1565
177784
+  Boolean	set_input_focus;
177784
+#endif
177784
 } XmGrabShellPart;
177784
 
177784
 
177784
diff --git a/lib/Xm/MenuShell.c b/lib/Xm/MenuShell.c
177784
index 2ed3dd8..9887087 100644
177784
--- a/lib/Xm/MenuShell.c
177784
+++ b/lib/Xm/MenuShell.c
177784
@@ -1514,9 +1514,7 @@ ChangeManaged(
177784
 
177784
 	       /** the real grab ***/
177784
          _XmMenuGrabKeyboardAndPointer((Widget)rowcol, _time);
177784
-#ifndef FIX_1565
177784
 	 _XmMenuFocus(XtParent(rowcol), XmMENU_BEGIN, _time);
177784
-#endif
177784
 
177784
 	 /* To support menu replay, keep the pointer in sync mode */
177784
 	 XAllowEvents(XtDisplay(rowcol), SyncPointer, CurrentTime);
177784
diff --git a/lib/Xm/MenuUtil.c b/lib/Xm/MenuUtil.c
177784
index 1d88390..2fb6a27 100644
177784
--- a/lib/Xm/MenuUtil.c
177784
+++ b/lib/Xm/MenuUtil.c
177784
@@ -1053,11 +1053,7 @@ _XmMenuGrabKeyboardAndPointer(
177784
 
177784
    register int status =
177784
            (_XmGrabKeyboard(widget,
177784
-#ifdef FIX_1565
177784
-                            False,
177784
-#else
177784
                             True,
177784
-#endif
177784
                             GrabModeSync,
177784
                             GrabModeAsync,
177784
                             time) != GrabSuccess);
177784
diff --git a/lib/Xm/RCMenu.c b/lib/Xm/RCMenu.c
177784
index 2c698d4..8b156da 100644
177784
--- a/lib/Xm/RCMenu.c
177784
+++ b/lib/Xm/RCMenu.c
177784
@@ -85,6 +85,9 @@ static char *rcsid = "$TOG: RCMenu.c /main/25 1999/05/24 18:06:57 samborn $";
177784
 #include "TraversalI.h"
177784
 #include "UniqueEvnI.h"
177784
 #include "VendorSI.h"
177784
+#ifdef FIX_1565
177784
+#include <Xm/GrabShell.h>
177784
+#endif
177784
 
177784
 #define FIX_1535
177784
 
177784
@@ -943,6 +946,13 @@ _XmMenuFocus(
177784
    XmMenuState mst = _XmGetMenuState((Widget)w);
177784
    Window tmpWindow;
177784
    int tmpRevert;
177784
+#ifdef FIX_1565
177784
+   Widget shell;
177784
+
177784
+   shell = w;
177784
+   while (! XtIsSubclass(shell, shellWidgetClass))
177784
+     shell = XtParent(shell);
177784
+#endif
177784
  
177784
    if (_time == CurrentTime) 
177784
      _time = XtLastTimestampProcessed(XtDisplay(w));
177784
@@ -983,6 +993,11 @@ _XmMenuFocus(
177784
 			  shell.popped_up))
177784
 **/
177784
 		     {
177784
+#ifdef FIX_1565
177784
+               if (XtIsSubclass(shell, xmGrabShellWidgetClass) || XtIsSubclass(shell, xmMenuShellWidgetClass))
177784
+                 XmForceGrabKeyboard(w, _time);
177784
+               else
177784
+#endif
177784
 		       SetInputFocus(XtDisplay(w), mst->RC_menuFocus.oldFocus,
177784
 				     mst->RC_menuFocus.oldRevert,
177784
 				     mst->RC_menuFocus.oldTime);
177784
@@ -996,6 +1011,11 @@ _XmMenuFocus(
177784
  	      */
177784
  	     else
177784
  	       {
177784
+#ifdef FIX_1565
177784
+               if (XtIsSubclass(shell, xmGrabShellWidgetClass) || XtIsSubclass(shell, xmMenuShellWidgetClass))
177784
+                 XmForceGrabKeyboard(w, _time);
177784
+               else
177784
+#endif
177784
  		  SetInputFocus(XtDisplay(w), mst->RC_menuFocus.oldFocus,
177784
 				mst->RC_menuFocus.oldRevert,
177784
 				mst->RC_menuFocus.oldTime);
177784
@@ -1014,6 +1034,11 @@ _XmMenuFocus(
177784
 	     RC_menuFocus.oldFocus);
177784
 	  mst->RC_menuFocus.oldTime = _time - 1;
177784
 
177784
+#ifdef FIX_1565
177784
+      if (XtIsSubclass(shell, xmGrabShellWidgetClass) || XtIsSubclass(shell, xmMenuShellWidgetClass))
177784
+        XmForceGrabKeyboard(w, _time);
177784
+      else
177784
+#endif
177784
 	  SetInputFocus(XtDisplay(w), XtWindow(w), mst->RC_menuFocus.oldRevert,
177784
 			mst->RC_menuFocus.oldTime);
177784
 
177784
@@ -1027,6 +1052,11 @@ _XmMenuFocus(
177784
 	  XGetInputFocus(XtDisplay(w), &tmpWindow, &tmpRevert);
177784
 	  if (tmpWindow != XtWindow(w))
177784
 	  {
177784
+#ifdef FIX_1565
177784
+        if (XtIsSubclass(shell, xmGrabShellWidgetClass) || XtIsSubclass(shell, xmMenuShellWidgetClass))
177784
+          XmForceGrabKeyboard(w, _time);
177784
+        else
177784
+#endif
177784
 	    SetInputFocus(XtDisplay(w), XtWindow(w), tmpRevert, _time);
177784
 
177784
 	    mst->RC_menuFocus.oldRevert = tmpRevert;
177784
@@ -1048,6 +1078,11 @@ _XmMenuFocus(
177784
 
177784
 	  break;
177784
 	case XmMENU_MIDDLE:
177784
+#ifdef FIX_1565
177784
+      if (XtIsSubclass(shell, xmGrabShellWidgetClass) || XtIsSubclass(shell, xmMenuShellWidgetClass))
177784
+        XmForceGrabKeyboard(w, _time);
177784
+      else
177784
+#endif
177784
 	  SetInputFocus(XtDisplay(w), XtWindow(w),
177784
 			mst->RC_menuFocus.oldRevert,
177784
 			mst->RC_menuFocus.oldTime);
177784
@@ -1062,6 +1097,11 @@ _XmMenuFocus(
177784
 	  if ((tmpWindow != XtWindow(w)) &&
177784
 	      (_time > mst->RC_menuFocus.oldTime))
177784
 	  {
177784
+#ifdef FIX_1565
177784
+        if (XtIsSubclass(shell, xmGrabShellWidgetClass) || XtIsSubclass(shell, xmMenuShellWidgetClass))
177784
+          XmForceGrabKeyboard(w, _time);
177784
+        else
177784
+#endif
177784
 	    SetInputFocus(XtDisplay(w), XtWindow(w), tmpRevert, _time);
177784
 
177784
 	    mst->RC_menuFocus.oldRevert = tmpRevert;
177784
diff --git a/lib/Xm/Xm.c b/lib/Xm/Xm.c
177784
index 3dfd794..45d48b6 100644
177784
--- a/lib/Xm/Xm.c
177784
+++ b/lib/Xm/Xm.c
177784
@@ -40,6 +40,10 @@
177784
 #ifdef FIX_345
177784
 #include <X11/keysym.h>
177784
 #endif
177784
+#ifdef FIX_1565
177784
+#include <Xm/GrabShell.h>
177784
+#include <Xm/MenuShell.h>
177784
+#endif
177784
 
177784
 
177784
 /**************************************************************************
177784
@@ -530,3 +534,173 @@ _XmAssignInsensitiveColor(Widget w)
177784
 	return p;
177784
 }
177784
 #endif
177784
+
177784
+#ifdef FIX_1565
177784
+
177784
+typedef struct _GrabData GrabData;
177784
+struct _GrabData {
177784
+  Widget w;
177784
+  GrabData *next;
177784
+};
177784
+
177784
+static void _XmSendFocusEvent(Widget child, int type);
177784
+static void _XmStartDispatcher(Display *display);
177784
+static Boolean _XmEventDispatcher(XEvent *event);
177784
+static void UnmapHandler(Widget w, XtPointer client_data, XEvent *event, Boolean *cont);
177784
+static Boolean _UngrabKeyboard(Widget w);
177784
+
177784
+static GrabData *grabw_top = NULL;
177784
+static int xm_dispatcher_on = 0;
177784
+static XtEventDispatchProc saved_dispatcher_proc = NULL;
177784
+static XtEventDispatchProc xt_dispatcher_proc = NULL;
177784
+
177784
+/*
177784
+   XmForceGrabKeyboard function is defined to be a substitutor of XSetInputFocus calls
177784
+   for popup and pulldown menus that should grab keyboard focus yet main window at the
177784
+   same time should visually stay in focus for window manager. This resolves focus flip
177784
+   issue when popup or pulldown menu is raised. ~ochern
177784
+ */
177784
+void XmForceGrabKeyboard(Widget w, Time time)
177784
+{
177784
+  GrabData *grabw;
177784
+
177784
+  if (!w)
177784
+    return;
177784
+
177784
+  while (! XtIsSubclass(w, shellWidgetClass))
177784
+    w = XtParent(w);
177784
+
177784
+  if (! (XtIsSubclass(w, xmGrabShellWidgetClass) || XtIsSubclass(w, xmMenuShellWidgetClass)))
177784
+    return;
177784
+
177784
+  _XmStartDispatcher(XtDisplay(w));
177784
+
177784
+  _UngrabKeyboard(w);
177784
+
177784
+  grabw = (GrabData *) XtMalloc(sizeof(GrabData));
177784
+  grabw->w = w;
177784
+  _XmProcessLock();
177784
+  grabw->next = grabw_top;
177784
+  grabw_top = grabw;
177784
+  _XmProcessUnlock();
177784
+
177784
+  XtInsertEventHandler(w, StructureNotifyMask, False, UnmapHandler, NULL, XtListHead);
177784
+
177784
+  _XmSendFocusEvent(w, FocusIn);
177784
+
177784
+  /* Following the XSetInputFocus behaviour we force sending FocusOut (see XGrabKeyboard(3))
177784
+     event to a previous keyboard holder */
177784
+  XtGrabKeyboard(w, True, GrabModeAsync, GrabModeAsync, time);
177784
+}
177784
+
177784
+static void _XmStartDispatcher(Display *display)
177784
+{
177784
+  if (!display)
177784
+    return;
177784
+
177784
+  _XmProcessLock();
177784
+
177784
+  if (xm_dispatcher_on) {
177784
+    _XmProcessUnlock();
177784
+    return;
177784
+  }
177784
+
177784
+  saved_dispatcher_proc = XtSetEventDispatcher(display, KeyPress, _XmEventDispatcher);
177784
+  if (! xt_dispatcher_proc)
177784
+    xt_dispatcher_proc = saved_dispatcher_proc;
177784
+  XtSetEventDispatcher(display, KeyRelease, _XmEventDispatcher);
177784
+  xm_dispatcher_on = 1;
177784
+
177784
+  _XmProcessUnlock();
177784
+}
177784
+
177784
+static Boolean _XmEventDispatcher(XEvent *event)
177784
+{
177784
+  _XmProcessLock();
177784
+  if (grabw_top) {
177784
+    if (event->type == KeyPress || event->type == KeyRelease)
177784
+      event->xany.window = XtWindow(grabw_top->w);
177784
+  }
177784
+  _XmProcessUnlock();
177784
+
177784
+  if (saved_dispatcher_proc) {
177784
+    return (*saved_dispatcher_proc)(event);
177784
+  } else if (xt_dispatcher_proc) {
177784
+    return (*xt_dispatcher_proc)(event);
177784
+  } else {
177784
+    if (grabw_top)
177784
+      XtSetEventDispatcher(XtDisplay(grabw_top->w), event->type, NULL);
177784
+    return XtDispatchEvent(event);
177784
+  }
177784
+}
177784
+
177784
+static void UnmapHandler(Widget w, XtPointer client_data, XEvent *event, Boolean *cont)
177784
+{
177784
+  if (event->type == UnmapNotify)
177784
+    _UngrabKeyboard(w);
177784
+  if (! grabw_top) {
177784
+    XtSetEventDispatcher(XtDisplay(w), KeyPress, saved_dispatcher_proc);
177784
+    XtSetEventDispatcher(XtDisplay(w), KeyRelease, saved_dispatcher_proc);
177784
+    xm_dispatcher_on = 0;
177784
+  }
177784
+
177784
+  /* we do not call XtUngrabKeyboard since X server automatically performs an
177784
+     UngrabKeyboard request if the event window for an active keyboard grab becomes
177784
+     not viewable. ~ochern */
177784
+}
177784
+
177784
+static Boolean _UngrabKeyboard(Widget w)
177784
+{
177784
+  GrabData *grabw, *grabw_prev;
177784
+
177784
+  _XmProcessLock();
177784
+  if (! grabw_top) {
177784
+    _XmProcessUnlock();
177784
+    return False;
177784
+  }
177784
+
177784
+  grabw = grabw_top;
177784
+  grabw_prev = NULL;
177784
+  while(grabw && grabw->w != w) {
177784
+    grabw_prev = grabw;
177784
+    grabw = grabw->next;
177784
+  }
177784
+  if (grabw) {
177784
+    if (grabw_prev)
177784
+      grabw_prev->next = grabw->next;
177784
+    else
177784
+      grabw_top = grabw->next;
177784
+    XtFree((char*) grabw);
177784
+
177784
+    _XmProcessUnlock();
177784
+    return True;
177784
+  }
177784
+
177784
+  _XmProcessUnlock();
177784
+  return False;
177784
+}
177784
+
177784
+static void _XmSendFocusEvent(Widget child, int type)
177784
+{
177784
+  child = XtIsWidget(child) ? child : _XtWindowedAncestor(child);
177784
+  if (XtIsSensitive(child) && !child->core.being_destroyed
177784
+      && XtIsRealized(child) && (XtBuildEventMask(child) & FocusChangeMask))
177784
+  {
177784
+    XFocusChangeEvent event;
177784
+    Display* dpy = XtDisplay (child);
177784
+
177784
+    event.type = type;
177784
+    event.serial = LastKnownRequestProcessed(dpy);
177784
+    event.send_event = True;
177784
+    event.display = dpy;
177784
+    event.window = XtWindow(child);
177784
+    event.mode = NotifyNormal;
177784
+    event.detail = NotifyAncestor;
177784
+    if (XFilterEvent((XEvent*)&event, XtWindow(child)))
177784
+      return;
177784
+    XtDispatchEventToWidget(child, (XEvent*)&event);
177784
+  }
177784
+}
177784
+
177784
+#endif
177784
+
177784
diff --git a/lib/Xm/XmI.h b/lib/Xm/XmI.h
177784
index b4420d3..c2b819e 100644
177784
--- a/lib/Xm/XmI.h
177784
+++ b/lib/Xm/XmI.h
177784
@@ -242,7 +242,9 @@ extern Boolean _XmIsISO10646(Display *dpy,
177784
 extern XChar2b* _XmUtf8ToUcs2(char *draw_text,
177784
                               size_t seg_len,
177784
 			      size_t *ret_str_len);
177784
-
177784
+#ifdef FIX_1565
177784
+extern void XmForceGrabKeyboard(Widget w, Time time);
177784
+#endif
177784
 
177784
 /********    End Private Function Declarations    ********/
177784