merge the formfield patch from ooo-build
[ooovba.git] / vcl / unx / source / window / FWS.cxx
blobed24c5a8cb072e8f0bd975364e7f3c87aec2234b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FWS.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include <X11/Xlib.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
42 #include "FWS.hxx"
44 static Atom fwsIconAtom;
46 static Atom FWS_CLIENT;
47 static Atom FWS_COMM_WINDOW;
48 static Atom FWS_PROTOCOLS;
49 static Atom FWS_STACK_UNDER;
50 static Atom FWS_PARK_ICONS;
51 static Atom FWS_PASS_ALL_INPUT;
52 static Atom FWS_PASSES_INPUT;
53 static Atom FWS_HANDLES_FOCUS;
55 static Atom FWS_REGISTER_WINDOW;
56 static Atom FWS_STATE_CHANGE;
57 static Atom FWS_UNSEEN_STATE;
58 static Atom FWS_NORMAL_STATE;
59 static Atom WM_PROTOCOLS;
60 static Atom WM_CHANGE_STATE;
62 static Bool fwsStackUnder;
63 static Bool fwsParkIcons;
64 static Bool fwsPassesInput;
65 static Bool fwsHandlesFocus;
67 static Window fwsCommWindow;
69 /*************************************<->***********************************
71 * WMSupportsFWS() -
73 * Initialize our atoms and determine if the current window manager is
74 * providing FWS extension support.
76 *************************************<->***********************************/
78 Bool
79 WMSupportsFWS (Display *display, int screen)
81 unsigned int i;
82 Atom protocol;
83 Atom propType;
84 int propFormat;
85 unsigned long propItems;
86 unsigned long propBytesAfter;
87 unsigned char *propData;
88 char propName[64];
90 FWS_CLIENT = XInternAtom(display, "_SUN_FWS_CLIENT", False);
91 FWS_COMM_WINDOW = XInternAtom(display, "_SUN_FWS_COMM_WINDOW", False);
92 FWS_PROTOCOLS = XInternAtom(display, "_SUN_FWS_PROTOCOLS", False);
93 FWS_STACK_UNDER = XInternAtom(display, "_SUN_FWS_STACK_UNDER", False);
94 FWS_PARK_ICONS = XInternAtom(display, "_SUN_FWS_PARK_ICONS", False);
95 FWS_PASS_ALL_INPUT = XInternAtom(display, "_SUN_FWS_PASS_ALL_INPUT", False);
96 FWS_PASSES_INPUT = XInternAtom(display, "_SUN_FWS_PASSES_INPUT", False);
97 FWS_HANDLES_FOCUS = XInternAtom(display, "_SUN_FWS_HANDLES_FOCUS", False);
98 FWS_REGISTER_WINDOW= XInternAtom(display, "_SUN_FWS_REGISTER_WINDOW",False);
99 FWS_STATE_CHANGE = XInternAtom(display, "_SUN_FWS_STATE_CHANGE", False);
100 FWS_UNSEEN_STATE = XInternAtom(display, "_SUN_FWS_UNSEEN_STATE", False);
101 FWS_NORMAL_STATE = XInternAtom(display, "_SUN_FWS_NORMAL_STATE", False);
102 WM_PROTOCOLS = XInternAtom(display, "WM_PROTOCOLS", False);
103 WM_CHANGE_STATE = XInternAtom(display, "WM_CHANGE_STATE", False);
105 snprintf (propName, sizeof(propName), "_SUN_FWS_NEXT_ICON_%d", screen);
106 fwsIconAtom = XInternAtom(display, propName, False);
108 if (XGetWindowProperty (display, DefaultRootWindow (display),
109 FWS_COMM_WINDOW, 0, 1,
110 False, AnyPropertyType, &propType,
111 &propFormat, &propItems,
112 &propBytesAfter, &propData) != Success)
113 return False;
115 if (propFormat != 32 ||
116 propItems != 1 ||
117 propBytesAfter != 0)
119 #if OSL_DEBUG_LEVEL > 1
120 fprintf (stderr, "Bad FWS_COMM_WINDOW property on root window.\n");
121 #endif
122 XFree (propData);
123 return False;
126 fwsCommWindow = *(Window *) propData;
127 #if OSL_DEBUG_LEVEL > 1
128 fprintf (stderr, "Using fwsCommWindow = 0x%lx.\n", fwsCommWindow);
129 #endif
130 XFree (propData);
133 if (XGetWindowProperty (display, DefaultRootWindow (display),
134 FWS_PROTOCOLS, 0, 10,
135 False, AnyPropertyType, &propType,
136 &propFormat, &propItems,
137 &propBytesAfter, &propData) != Success)
139 return False;
142 if (propFormat != 32 ||
143 propBytesAfter != 0)
145 #if OSL_DEBUG_LEVEL > 1
146 fprintf (stderr, "Bad FWS_PROTOCOLS property on root window.\n");
147 #endif
148 XFree (propData);
149 return False;
152 for (i = 0; i < propItems; ++i)
154 protocol = ((Atom *) propData)[i];
155 if (protocol == FWS_STACK_UNDER)
157 fwsStackUnder = True;
158 #if OSL_DEBUG_LEVEL > 1
159 fprintf (stderr, "Using fwsStackUnder.\n");
160 #endif
162 else
163 if (protocol == FWS_PARK_ICONS)
165 fwsParkIcons = True;
166 #if OSL_DEBUG_LEVEL > 1
167 fprintf (stderr, "Using fwsParkIcons.\n");
168 #endif
170 else
171 if (protocol == FWS_PASSES_INPUT)
173 fwsPassesInput = True;
174 #if OSL_DEBUG_LEVEL > 1
175 fprintf (stderr, "Using fwsPassesInput.\n");
176 #endif
178 else
179 if (protocol == FWS_HANDLES_FOCUS)
181 fwsHandlesFocus = True;
182 #if OSL_DEBUG_LEVEL > 1
183 fprintf (stderr, "Using fwsHandlesFocus.\n");
184 #endif
188 XFree (propData);
189 return True;
192 /*************************************<->***********************************
194 * newHandler() -
196 * Handle X errors (temporarily) to record the occurance of BadWindow
197 * errors without crashing. Used to detect the FWS_COMM_WINDOW root window
198 * property containing an old or obsolete window id.
200 *************************************<->***********************************/
202 extern "C" {
204 static Bool badWindowFound;
205 static int (* oldHandler) (Display *, XErrorEvent *);
207 static int
208 newHandler (Display *display, XErrorEvent *xerror)
210 if (xerror->error_code != BadWindow)
211 (*oldHandler)(display, xerror);
212 else
213 badWindowFound = True;
215 return 0;
220 /*************************************<->***********************************
222 * RegisterFwsWindow() -
224 * Send a client message to the FWS_COMM_WINDOW indicating the existance
225 * of a new FWS client window. Be careful to avoid BadWindow errors on
226 * the XSendEvent in case the FWS_COMM_WINDOW root window property had
227 * old/obsolete junk in it.
229 *************************************<->***********************************/
231 Bool
232 RegisterFwsWindow (Display *display, Window window)
234 XClientMessageEvent msg;
236 msg.type = ClientMessage;
237 msg.window = fwsCommWindow;
238 msg.message_type = FWS_REGISTER_WINDOW;
239 msg.format = 32;
240 msg.data.l[0] = window;
242 XSync (display, False);
243 badWindowFound = False;
244 oldHandler = XSetErrorHandler (newHandler);
246 XSendEvent (display, fwsCommWindow, False, NoEventMask,
247 (XEvent *) &msg);
248 XSync (display, False);
250 XSetErrorHandler (oldHandler);
251 #if OSL_DEBUG_LEVEL > 1
252 if (badWindowFound)
253 fprintf (stderr, "No FWS client window to register with.\n");
254 #endif
256 return !badWindowFound;
259 /*************************************<->***********************************
261 * AddFwsProtocols -
263 * Add the FWS protocol atoms to the WMProtocols property for the window.
265 *************************************<->***********************************/
267 void
268 AddFwsProtocols (Display *display, Window window)
270 #define MAX_FWS_PROTOS 10
272 Atom fwsProtocols[ MAX_FWS_PROTOS ];
273 int nProtos = 0;
275 fwsProtocols[ nProtos++ ] = FWS_CLIENT;
276 fwsProtocols[ nProtos++ ] = FWS_STACK_UNDER;
277 fwsProtocols[ nProtos++ ] = FWS_STATE_CHANGE;
278 fwsProtocols[ nProtos++ ] = FWS_PASS_ALL_INPUT;
279 XChangeProperty (display, window, WM_PROTOCOLS,
280 XA_ATOM, 32, PropModeAppend,
281 (unsigned char *) fwsProtocols, nProtos);