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