Blame SOURCES/motif-2.3.4-motifzone_1564-88bdce1.patch

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