xref: /aoo41x/main/vcl/unx/generic/window/FWS.cxx (revision c82f2877)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
34 #include <X11/Xatom.h>
35 #include "FWS.hxx"
36 
37 static Atom fwsIconAtom;
38 
39 static Atom FWS_CLIENT;
40 static Atom FWS_COMM_WINDOW;
41 static Atom FWS_PROTOCOLS;
42 static Atom FWS_STACK_UNDER;
43 static Atom FWS_PARK_ICONS;
44 static Atom FWS_PASS_ALL_INPUT;
45 static Atom FWS_PASSES_INPUT;
46 static Atom FWS_HANDLES_FOCUS;
47 
48 static Atom FWS_REGISTER_WINDOW;
49 static Atom FWS_STATE_CHANGE;
50 static Atom FWS_UNSEEN_STATE;
51 static Atom FWS_NORMAL_STATE;
52 static Atom WM_PROTOCOLS;
53 static Atom WM_CHANGE_STATE;
54 
55 static Bool fwsStackUnder;
56 static Bool fwsParkIcons;
57 static Bool fwsPassesInput;
58 static Bool fwsHandlesFocus;
59 
60 static Window fwsCommWindow;
61 
62 /*************************************<->***********************************
63  *
64  * WMSupportsFWS() -
65  *
66  * Initialize our atoms and determine if the current window manager is
67  * providing FWS extension support.
68  *
69  *************************************<->***********************************/
70 
71 Bool
WMSupportsFWS(Display * display,int screen)72 WMSupportsFWS (Display *display, int screen)
73 {
74 	unsigned int    i;
75 	Atom            protocol;
76 	Atom            propType;
77 	int             propFormat;
78 	unsigned long   propItems;
79 	unsigned long   propBytesAfter;
80 	unsigned char   *propData;
81 	char            propName[64];
82 
83 	FWS_CLIENT         = XInternAtom(display, "_SUN_FWS_CLIENT",         False);
84 	FWS_COMM_WINDOW    = XInternAtom(display, "_SUN_FWS_COMM_WINDOW",    False);
85 	FWS_PROTOCOLS      = XInternAtom(display, "_SUN_FWS_PROTOCOLS",      False);
86 	FWS_STACK_UNDER    = XInternAtom(display, "_SUN_FWS_STACK_UNDER",    False);
87 	FWS_PARK_ICONS     = XInternAtom(display, "_SUN_FWS_PARK_ICONS",     False);
88 	FWS_PASS_ALL_INPUT = XInternAtom(display, "_SUN_FWS_PASS_ALL_INPUT", False);
89 	FWS_PASSES_INPUT   = XInternAtom(display, "_SUN_FWS_PASSES_INPUT",   False);
90 	FWS_HANDLES_FOCUS  = XInternAtom(display, "_SUN_FWS_HANDLES_FOCUS",  False);
91 	FWS_REGISTER_WINDOW= XInternAtom(display, "_SUN_FWS_REGISTER_WINDOW",False);
92 	FWS_STATE_CHANGE   = XInternAtom(display, "_SUN_FWS_STATE_CHANGE",   False);
93 	FWS_UNSEEN_STATE   = XInternAtom(display, "_SUN_FWS_UNSEEN_STATE",   False);
94 	FWS_NORMAL_STATE   = XInternAtom(display, "_SUN_FWS_NORMAL_STATE",   False);
95 	WM_PROTOCOLS       = XInternAtom(display, "WM_PROTOCOLS",            False);
96 	WM_CHANGE_STATE    = XInternAtom(display, "WM_CHANGE_STATE",         False);
97 
98 	snprintf (propName, sizeof(propName), "_SUN_FWS_NEXT_ICON_%d", screen);
99 	fwsIconAtom        = XInternAtom(display, propName, False);
100 
101 	if (XGetWindowProperty (display, DefaultRootWindow (display),
102 				FWS_COMM_WINDOW, 0, 1,
103 				False, AnyPropertyType, &propType,
104 				&propFormat, &propItems,
105 				&propBytesAfter, &propData) != Success)
106 		return False;
107 
108 	if (propFormat != 32 ||
109 	    propItems != 1 ||
110 	    propBytesAfter != 0)
111 	{
112         #if OSL_DEBUG_LEVEL > 1
113 		fprintf (stderr, "Bad FWS_COMM_WINDOW property on root window.\n");
114 		#endif
115 		XFree (propData);
116 		return False;
117 	}
118 
119 	fwsCommWindow = *(Window *) propData;
120     #if OSL_DEBUG_LEVEL > 1
121 	fprintf (stderr, "Using fwsCommWindow = 0x%lx.\n", fwsCommWindow);
122 	#endif
123 	XFree (propData);
124 
125 
126 	if (XGetWindowProperty (display, DefaultRootWindow (display),
127 			FWS_PROTOCOLS, 0, 10,
128 			False, AnyPropertyType, &propType,
129 			&propFormat, &propItems,
130 			&propBytesAfter, &propData) != Success)
131 	{
132 		return False;
133 	}
134 
135 	if (propFormat     != 32 ||
136 	    propBytesAfter != 0)
137 	{
138         #if OSL_DEBUG_LEVEL > 1
139 		fprintf (stderr, "Bad FWS_PROTOCOLS property on root window.\n");
140 		#endif
141 		XFree (propData);
142 		return False;
143 	}
144 
145 	for (i = 0; i < propItems; ++i)
146 	{
147 		protocol = ((Atom *) propData)[i];
148 		if (protocol == FWS_STACK_UNDER)
149 		{
150 			fwsStackUnder = True;
151             #if OSL_DEBUG_LEVEL > 1
152 			fprintf (stderr, "Using fwsStackUnder.\n");
153 			#endif
154 		}
155 		else
156 		if (protocol == FWS_PARK_ICONS)
157 		{
158 			fwsParkIcons = True;
159             #if OSL_DEBUG_LEVEL > 1
160 			fprintf (stderr, "Using fwsParkIcons.\n");
161 			#endif
162 		}
163 		else
164 		if (protocol == FWS_PASSES_INPUT)
165 		{
166 			fwsPassesInput = True;
167             #if OSL_DEBUG_LEVEL > 1
168 			fprintf (stderr, "Using fwsPassesInput.\n");
169 			#endif
170 		}
171 		else
172 		if (protocol == FWS_HANDLES_FOCUS)
173 		{
174 			fwsHandlesFocus = True;
175             #if OSL_DEBUG_LEVEL > 1
176 			fprintf (stderr, "Using fwsHandlesFocus.\n");
177 			#endif
178 		}
179 	}
180 
181 	XFree (propData);
182 	return True;
183 }
184 
185 /*************************************<->***********************************
186  *
187  * newHandler() -
188  *
189  * Handle X errors (temporarily) to record the occurance of BadWindow
190  * errors without crashing.  Used to detect the FWS_COMM_WINDOW root window
191  * property containing an old or obsolete window id.
192  *
193  *************************************<->***********************************/
194 
195 extern "C" {
196 
197 static Bool badWindowFound;
198 static int (* oldHandler) (Display *, XErrorEvent *);
199 
200 static int
newHandler(Display * display,XErrorEvent * xerror)201 newHandler (Display *display, XErrorEvent *xerror)
202 {
203 	if (xerror->error_code != BadWindow)
204 		(*oldHandler)(display, xerror);
205 	else
206 		badWindowFound = True;
207 
208 	return 0;
209 }
210 
211 }
212 
213 /*************************************<->***********************************
214  *
215  * RegisterFwsWindow() -
216  *
217  * Send a client message to the FWS_COMM_WINDOW indicating the existance
218  * of a new FWS client window.  Be careful to avoid BadWindow errors on
219  * the XSendEvent in case the FWS_COMM_WINDOW root window property had
220  * old/obsolete junk in it.
221  *
222  *************************************<->***********************************/
223 
224 Bool
RegisterFwsWindow(Display * display,Window window)225 RegisterFwsWindow (Display *display, Window window)
226 {
227 	XClientMessageEvent  msg;
228 
229 	msg.type         = ClientMessage;
230 	msg.window       = fwsCommWindow;
231 	msg.message_type = FWS_REGISTER_WINDOW;
232 	msg.format       = 32;
233 	msg.data.l[0]    = window;
234 
235 	XSync (display, False);
236 	badWindowFound = False;
237 	oldHandler = XSetErrorHandler (newHandler);
238 
239 	XSendEvent (display, fwsCommWindow, False, NoEventMask,
240 		    (XEvent *) &msg);
241 	XSync (display, False);
242 
243 	XSetErrorHandler (oldHandler);
244     #if OSL_DEBUG_LEVEL > 1
245 	if (badWindowFound)
246 		fprintf (stderr, "No FWS client window to register with.\n");
247 	#endif
248 
249 	return !badWindowFound;
250 }
251 
252 /*************************************<->***********************************
253  *
254  * AddFwsProtocols -
255  *
256  * Add the FWS protocol atoms to the WMProtocols property for the window.
257  *
258  *************************************<->***********************************/
259 
260 void
AddFwsProtocols(Display * display,Window window)261 AddFwsProtocols (Display *display, Window window)
262 {
263 	#define MAX_FWS_PROTOS 10
264 
265 	Atom fwsProtocols[ MAX_FWS_PROTOS ];
266 	int  nProtos = 0;
267 
268 	fwsProtocols[ nProtos++ ] = FWS_CLIENT;
269 	fwsProtocols[ nProtos++ ] = FWS_STACK_UNDER;
270 	fwsProtocols[ nProtos++ ] = FWS_STATE_CHANGE;
271 	fwsProtocols[ nProtos++ ] = FWS_PASS_ALL_INPUT;
272 	XChangeProperty (display, window, WM_PROTOCOLS,
273 			XA_ATOM, 32, PropModeAppend,
274 			(unsigned char *) fwsProtocols, nProtos);
275 }
276 
277