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

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