Merge branch 'merges'
[sxemacs] / src / ui / X11 / ExternalClient-Xlib.c
1 /* External client, raw Xlib version.
2    Copyright (C) 1993, 1994 Sun Microsystems, Inc.
3
4 This file is part of SXEmacs
5
6 SXEmacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 SXEmacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19
20 /* Synched up with: Not in FSF. */
21
22 /* Written by Ben Wing, February 1994. */
23
24 #include <X11/Xlib.h>
25 #include <X11/Xresource.h>
26 #include <X11/Xutil.h>
27 #include "ui/X11/extw-Xlib.h"
28
29 /* this is not a perfect solution, but otherwise we have to include all
30    of the Xt junk */
31
32 #define XtGeometryNo 1
33
34 #if (XlibSpecificationRelease < 5)
35 # define XPointer char *
36 #endif
37
38 static int context_inited;
39 static XContext focus_context;
40
41 /* does the specified window have the focus, given that the pointer just
42    entered (or left) the window (according to enter_p)?  This question
43    does not have an obvious answer in X.  (Basically, X sucks.) */
44
45 static int window_has_focus_p(Display * display, Window win, int enter_p)
46 {
47         Window focuswin;
48         int dummy;
49
50         XGetInputFocus(display, &focuswin, &dummy);
51         if (focuswin == PointerRoot)
52                 return enter_p;
53         if (focuswin == win)
54                 return True;
55         if (!enter_p)
56                 return False;
57         do {
58                 Status st;
59                 Window root_win, parent_win;
60                 Window *child_win;
61                 int nchild;
62
63                 st = XQueryTree(display, win, &root_win, &parent_win,
64                                 &child_win, &nchild);
65                 if (!st)
66                         return False;
67                 XFree((XPointer) child_win);
68                 if (parent_win == focuswin)
69                         return True;
70                 if (parent_win == root_win)
71                         return False;
72                 win = parent_win;
73         }
74         while (1);
75 }
76
77 /* External entry points when using XLib directly */
78
79 void ExternalClientInitialize(Display * display, Window win);
80 void ExternalClientInitialize(Display * display, Window win)
81 {
82         extw_initialize_atoms(display);
83         extw_which_side = extw_client_send;
84         if (!context_inited) {
85                 focus_context = XUniqueContext();
86                 context_inited = 1;
87         }
88         XSaveContext(display, win, focus_context, 0);
89         XSelectInput(display, win, EnterWindowMask | LeaveWindowMask |
90                      FocusChangeMask);
91 }
92
93 void ExternalClientEventHandler(Display * display, Window win, XEvent * event);
94 void ExternalClientEventHandler(Display * display, Window win, XEvent * event)
95 {
96         if (win != event->xany.window)
97                 return;
98
99         if (event->type == ClientMessage &&
100             event->xclient.message_type == a_EXTW_NOTIFY &&
101             event->xclient.data.l[0] == extw_shell_send)
102                 switch (event->xclient.data.l[1]) {
103                 case extw_notify_gm:
104                         /* for the moment, just refuse geometry requests. */
105                         extw_send_notify_3(display, win, extw_notify_gm,
106                                            XtGeometryNo, 0, 0);
107                         break;
108
109                 case extw_notify_init:
110                         extw_send_notify_3(display, win, extw_notify_init,
111                                            EXTW_TYPE_XLIB, 0, 0);
112                         break;
113
114                 case extw_notify_end:
115                         XClearArea(display, win, 0, 0, 0, 0, True);
116                         break;
117         } else {
118                 int focus_status;
119                 XPointer current_focus;
120
121                 if (event->type == FocusIn)
122                         focus_status = 1;
123                 else if (event->type == FocusOut)
124                         focus_status = 0;
125                 else if (event->type == EnterNotify &&
126                          event->xcrossing.detail != NotifyInferior)
127                         focus_status = window_has_focus_p(display, win, 1);
128                 else if (event->type == LeaveNotify &&
129                          event->xcrossing.detail != NotifyInferior)
130                         focus_status = window_has_focus_p(display, win, 0);
131                 else
132                         return;
133                 XFindContext(display, win, focus_context, &current_focus);
134                 if (focus_status != (int)current_focus) {
135                         XSaveContext(display, win, focus_context,
136                                      (XPointer) focus_status);
137                         extw_send_notify_3(display, win,
138                                            focus_status ? extw_notify_focus_in :
139                                            extw_notify_focus_out, 0, 0, 0);
140                 }
141         }
142 }