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

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