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

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