2
|
1 |
/*
|
107
|
2 |
* Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
package sun.awt.X11;
|
|
28 |
|
107
|
29 |
import java.awt.Frame;
|
2
|
30 |
import java.util.logging.Level;
|
|
31 |
import java.util.logging.Logger;
|
|
32 |
|
107
|
33 |
final class XNETProtocol extends XProtocol implements XStateProtocol, XLayerProtocol
|
|
34 |
{
|
|
35 |
private final static Logger log = Logger.getLogger("sun.awt.X11.XNETProtocol");
|
2
|
36 |
private final static Logger iconLog = Logger.getLogger("sun.awt.X11.icon.XNETProtocol");
|
107
|
37 |
private static Logger stateLog = Logger.getLogger("sun.awt.X11.states.XNETProtocol");
|
2
|
38 |
|
|
39 |
/**
|
|
40 |
* XStateProtocol
|
|
41 |
*/
|
|
42 |
public boolean supportsState(int state) {
|
|
43 |
return doStateProtocol() ; // TODO - check for Frame constants
|
|
44 |
}
|
|
45 |
|
|
46 |
public void setState(XWindowPeer window, int state) {
|
|
47 |
if (log.isLoggable(Level.FINE)) log.fine("Setting state of " + window + " to " + state);
|
|
48 |
if (window.isShowing()) {
|
|
49 |
requestState(window, state);
|
|
50 |
} else {
|
|
51 |
setInitialState(window, state);
|
|
52 |
}
|
|
53 |
}
|
|
54 |
|
|
55 |
private void setInitialState(XWindowPeer window, int state) {
|
|
56 |
XAtomList old_state = window.getNETWMState();
|
|
57 |
log.log(Level.FINE, "Current state of the window {0} is {1}", new Object[] {window, old_state});
|
|
58 |
if ((state & Frame.MAXIMIZED_VERT) != 0) {
|
|
59 |
old_state.add(XA_NET_WM_STATE_MAXIMIZED_VERT);
|
|
60 |
} else {
|
|
61 |
old_state.remove(XA_NET_WM_STATE_MAXIMIZED_VERT);
|
|
62 |
}
|
|
63 |
if ((state & Frame.MAXIMIZED_HORIZ) != 0) {
|
|
64 |
old_state.add(XA_NET_WM_STATE_MAXIMIZED_HORZ);
|
|
65 |
} else {
|
|
66 |
old_state.remove(XA_NET_WM_STATE_MAXIMIZED_HORZ);
|
|
67 |
}
|
|
68 |
log.log(Level.FINE, "Setting initial state of the window {0} to {1}", new Object[] {window, old_state});
|
|
69 |
window.setNETWMState(old_state);
|
|
70 |
}
|
|
71 |
|
|
72 |
private void requestState(XWindowPeer window, int state) {
|
|
73 |
/*
|
|
74 |
* We have to use toggle for maximization because of transitions
|
|
75 |
* from maximization in one direction only to maximization in the
|
|
76 |
* other direction only.
|
|
77 |
*/
|
|
78 |
int old_net_state = getState(window);
|
|
79 |
int max_changed = (state ^ old_net_state) & (Frame.MAXIMIZED_BOTH);
|
|
80 |
|
|
81 |
XClientMessageEvent req = new XClientMessageEvent();
|
|
82 |
try {
|
|
83 |
switch(max_changed) {
|
|
84 |
case 0:
|
|
85 |
return;
|
|
86 |
case Frame.MAXIMIZED_HORIZ:
|
|
87 |
req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_HORZ.getAtom());
|
|
88 |
req.set_data(2, 0);
|
|
89 |
break;
|
|
90 |
case Frame.MAXIMIZED_VERT:
|
|
91 |
req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_VERT.getAtom());
|
|
92 |
req.set_data(2, 0);
|
|
93 |
break;
|
|
94 |
case Frame.MAXIMIZED_BOTH:
|
|
95 |
req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_HORZ.getAtom());
|
|
96 |
req.set_data(2, XA_NET_WM_STATE_MAXIMIZED_VERT.getAtom());
|
|
97 |
break;
|
|
98 |
default:
|
|
99 |
return;
|
|
100 |
}
|
|
101 |
if (log.isLoggable(Level.FINE)) log.fine("Requesting state on " + window + " for " + state);
|
|
102 |
req.set_type((int)XlibWrapper.ClientMessage);
|
|
103 |
req.set_window(window.getWindow());
|
|
104 |
req.set_message_type(XA_NET_WM_STATE.getAtom());
|
|
105 |
req.set_format(32);
|
|
106 |
req.set_data(0, _NET_WM_STATE_TOGGLE);
|
|
107 |
XToolkit.awtLock();
|
|
108 |
try {
|
|
109 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(),
|
|
110 |
XlibWrapper.RootWindow(XToolkit.getDisplay(), window.getScreenNumber()),
|
|
111 |
false,
|
|
112 |
XlibWrapper.SubstructureRedirectMask | XlibWrapper.SubstructureNotifyMask,
|
|
113 |
req.pData);
|
|
114 |
}
|
|
115 |
finally {
|
|
116 |
XToolkit.awtUnlock();
|
|
117 |
}
|
|
118 |
} finally {
|
|
119 |
req.dispose();
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
public int getState(XWindowPeer window) {
|
|
124 |
return getStateImpl(window);
|
|
125 |
}
|
|
126 |
|
|
127 |
/*
|
|
128 |
* New "NET" WM spec: _NET_WM_STATE/Atom[]
|
|
129 |
*/
|
|
130 |
int getStateImpl(XWindowPeer window) {
|
|
131 |
XAtomList net_wm_state = window.getNETWMState();
|
|
132 |
if (net_wm_state.size() == 0) {
|
|
133 |
return Frame.NORMAL;
|
|
134 |
}
|
|
135 |
int java_state = Frame.NORMAL;
|
|
136 |
if (net_wm_state.contains(XA_NET_WM_STATE_MAXIMIZED_VERT)) {
|
|
137 |
java_state |= Frame.MAXIMIZED_VERT;
|
|
138 |
}
|
|
139 |
if (net_wm_state.contains(XA_NET_WM_STATE_MAXIMIZED_HORZ)) {
|
|
140 |
java_state |= Frame.MAXIMIZED_HORIZ;
|
|
141 |
}
|
|
142 |
return java_state;
|
|
143 |
}
|
|
144 |
|
|
145 |
public boolean isStateChange(XPropertyEvent e) {
|
|
146 |
boolean res = doStateProtocol() && (e.get_atom() == XA_NET_WM_STATE.getAtom()) ;
|
|
147 |
|
|
148 |
if (res) {
|
|
149 |
// Since state change happened, reset our cached state. It will be re-read by getState
|
|
150 |
XWindowPeer wpeer = (XWindowPeer)XToolkit.windowToXWindow(e.get_window());
|
|
151 |
wpeer.setNETWMState(null);
|
|
152 |
}
|
|
153 |
return res;
|
|
154 |
}
|
|
155 |
|
|
156 |
/*
|
|
157 |
* Work around for 4775545.
|
|
158 |
*/
|
|
159 |
public void unshadeKludge(XWindowPeer window) {
|
|
160 |
XAtomList net_wm_state = window.getNETWMState();
|
|
161 |
net_wm_state.remove(XA_NET_WM_STATE_SHADED);
|
|
162 |
window.setNETWMState(net_wm_state);
|
|
163 |
}
|
|
164 |
|
|
165 |
/**
|
|
166 |
* XLayerProtocol
|
|
167 |
*/
|
|
168 |
public boolean supportsLayer(int layer) {
|
|
169 |
return ((layer == LAYER_ALWAYS_ON_TOP) || (layer == LAYER_NORMAL)) && doLayerProtocol();
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Helper function to set/reset one state in NET_WM_STATE
|
|
174 |
* If window is showing then it uses ClientMessage, otherwise adjusts NET_WM_STATE list
|
|
175 |
* @param window Window which NET_WM_STATE property is being modified
|
|
176 |
* @param state State atom to be set/reset
|
|
177 |
* @param reset Indicates operation, 'set' if false, 'reset' if true
|
|
178 |
*/
|
|
179 |
private void setStateHelper(XWindowPeer window, XAtom state, boolean set) {
|
|
180 |
log.log(Level.FINER, "Window visibility is: withdrawn={0}, visible={1}, mapped={2} showing={3}",
|
|
181 |
new Object[] {Boolean.valueOf(window.isWithdrawn()), Boolean.valueOf(window.isVisible()),
|
|
182 |
Boolean.valueOf(window.isMapped()), Boolean.valueOf(window.isShowing())});
|
|
183 |
if (window.isShowing()) {
|
|
184 |
XClientMessageEvent req = new XClientMessageEvent();
|
|
185 |
try {
|
|
186 |
req.set_type((int)XlibWrapper.ClientMessage);
|
|
187 |
req.set_window(window.getWindow());
|
|
188 |
req.set_message_type(XA_NET_WM_STATE.getAtom());
|
|
189 |
req.set_format(32);
|
|
190 |
req.set_data(0, (!set) ? _NET_WM_STATE_REMOVE : _NET_WM_STATE_ADD);
|
|
191 |
req.set_data(1, state.getAtom());
|
|
192 |
log.log(Level.FINE, "Setting _NET_STATE atom {0} on {1} for {2}", new Object[] {state, window, Boolean.valueOf(set)});
|
|
193 |
XToolkit.awtLock();
|
|
194 |
try {
|
|
195 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(),
|
|
196 |
XlibWrapper.RootWindow(XToolkit.getDisplay(), window.getScreenNumber()),
|
|
197 |
false,
|
|
198 |
XlibWrapper.SubstructureRedirectMask | XlibWrapper.SubstructureNotifyMask,
|
|
199 |
req.pData);
|
|
200 |
}
|
|
201 |
finally {
|
|
202 |
XToolkit.awtUnlock();
|
|
203 |
}
|
|
204 |
} finally {
|
|
205 |
req.dispose();
|
|
206 |
}
|
|
207 |
} else {
|
|
208 |
XAtomList net_wm_state = window.getNETWMState();
|
|
209 |
log.log(Level.FINE, "Current state on {0} is {1}", new Object[] {window, net_wm_state});
|
|
210 |
if (!set) {
|
|
211 |
net_wm_state.remove(state);
|
|
212 |
} else {
|
|
213 |
net_wm_state.add(state);
|
|
214 |
}
|
|
215 |
log.log(Level.FINE, "Setting states on {0} to {1}", new Object[] {window, net_wm_state});
|
|
216 |
window.setNETWMState(net_wm_state);
|
|
217 |
}
|
|
218 |
XToolkit.XSync();
|
|
219 |
}
|
|
220 |
|
|
221 |
public void setLayer(XWindowPeer window, int layer) {
|
|
222 |
setStateHelper(window, XA_NET_WM_STATE_ABOVE, layer == LAYER_ALWAYS_ON_TOP);
|
|
223 |
}
|
|
224 |
|
|
225 |
/* New "netwm" spec from www.freedesktop.org */
|
|
226 |
XAtom XA_UTF8_STRING = XAtom.get("UTF8_STRING"); /* like STRING but encoding is UTF-8 */
|
|
227 |
XAtom XA_NET_SUPPORTING_WM_CHECK = XAtom.get("_NET_SUPPORTING_WM_CHECK");
|
|
228 |
XAtom XA_NET_SUPPORTED = XAtom.get("_NET_SUPPORTED"); /* list of protocols (property of root) */
|
|
229 |
XAtom XA_NET_WM_NAME = XAtom.get("_NET_WM_NAME"); /* window property */
|
|
230 |
XAtom XA_NET_WM_STATE = XAtom.get("_NET_WM_STATE");/* both window property and request */
|
|
231 |
|
|
232 |
/*
|
|
233 |
* _NET_WM_STATE is a list of atoms.
|
|
234 |
* NB: Standard spelling is "HORZ" (yes, without an 'I'), but KDE2
|
|
235 |
* uses misspelled "HORIZ" (see KDE bug #20229). This was fixed in
|
|
236 |
* KDE 2.2. Under earlier versions of KDE2 horizontal and full
|
|
237 |
* maximization doesn't work .
|
|
238 |
*/
|
|
239 |
XAtom XA_NET_WM_STATE_MAXIMIZED_HORZ = XAtom.get("_NET_WM_STATE_MAXIMIZED_HORZ");
|
|
240 |
XAtom XA_NET_WM_STATE_MAXIMIZED_VERT = XAtom.get("_NET_WM_STATE_MAXIMIZED_VERT");
|
|
241 |
XAtom XA_NET_WM_STATE_SHADED = XAtom.get("_NET_WM_STATE_SHADED");
|
|
242 |
XAtom XA_NET_WM_STATE_ABOVE = XAtom.get("_NET_WM_STATE_ABOVE");
|
|
243 |
XAtom XA_NET_WM_STATE_MODAL = XAtom.get("_NET_WM_STATE_MODAL");
|
|
244 |
XAtom XA_NET_WM_STATE_FULLSCREEN = XAtom.get("_NET_WM_STATE_FULLSCREEN");
|
|
245 |
XAtom XA_NET_WM_STATE_BELOW = XAtom.get("_NET_WM_STATE_BELOW");
|
|
246 |
XAtom XA_NET_WM_STATE_HIDDEN = XAtom.get("_NET_WM_STATE_HIDDEN");
|
|
247 |
XAtom XA_NET_WM_STATE_SKIP_TASKBAR = XAtom.get("_NET_WM_STATE_SKIP_TASKBAR");
|
|
248 |
XAtom XA_NET_WM_STATE_SKIP_PAGER = XAtom.get("_NET_WM_STATE_SKIP_PAGER");
|
|
249 |
|
|
250 |
XAtom XA_NET_WM_WINDOW_TYPE = XAtom.get("_NET_WM_WINDOW_TYPE");
|
|
251 |
XAtom XA_NET_WM_WINDOW_TYPE_DIALOG = XAtom.get("_NET_WM_WINDOW_TYPE_DIALOG");
|
|
252 |
|
|
253 |
/* For _NET_WM_STATE ClientMessage requests */
|
|
254 |
final static int _NET_WM_STATE_REMOVE =0; /* remove/unset property */
|
|
255 |
final static int _NET_WM_STATE_ADD =1; /* add/set property */
|
|
256 |
final static int _NET_WM_STATE_TOGGLE =2; /* toggle property */
|
|
257 |
|
|
258 |
boolean supportChecked = false;
|
|
259 |
long NetWindow = 0;
|
|
260 |
void detect() {
|
|
261 |
if (supportChecked) {
|
|
262 |
// TODO: How about detecting WM-restart or exit?
|
|
263 |
return;
|
|
264 |
}
|
|
265 |
NetWindow = checkAnchor(XA_NET_SUPPORTING_WM_CHECK, XAtom.XA_WINDOW);
|
|
266 |
supportChecked = true;
|
|
267 |
if (log.isLoggable(Level.FINE)) log.fine("### " + this + " is active: " + (NetWindow != 0));
|
|
268 |
}
|
|
269 |
|
|
270 |
boolean active() {
|
|
271 |
detect();
|
|
272 |
return NetWindow != 0;
|
|
273 |
}
|
|
274 |
|
|
275 |
boolean doStateProtocol() {
|
|
276 |
boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE);
|
107
|
277 |
stateLog.finer("doStateProtocol() returns " + res);
|
2
|
278 |
return res;
|
|
279 |
}
|
|
280 |
|
|
281 |
boolean doLayerProtocol() {
|
|
282 |
boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_ABOVE);
|
|
283 |
return res;
|
|
284 |
}
|
|
285 |
|
|
286 |
boolean doModalityProtocol() {
|
|
287 |
boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_MODAL);
|
|
288 |
return res;
|
|
289 |
}
|
|
290 |
boolean isWMName(String name) {
|
|
291 |
if (!active()) {
|
|
292 |
return false;
|
|
293 |
}
|
|
294 |
String net_wm_name_string = getWMName();
|
|
295 |
if (net_wm_name_string == null) {
|
|
296 |
return false;
|
|
297 |
}
|
|
298 |
if (log.isLoggable(Level.FINE)) log.fine("### WM_NAME = " + net_wm_name_string);
|
|
299 |
return net_wm_name_string.startsWith(name);
|
|
300 |
}
|
|
301 |
|
|
302 |
String net_wm_name_cache;
|
|
303 |
public String getWMName() {
|
|
304 |
if (!active()) {
|
|
305 |
return null;
|
|
306 |
}
|
|
307 |
|
|
308 |
if (net_wm_name_cache != null) {
|
|
309 |
return net_wm_name_cache;
|
|
310 |
}
|
|
311 |
|
|
312 |
/*
|
|
313 |
* Check both UTF8_STRING and STRING. We only call this function
|
|
314 |
* with ASCII names and UTF8 preserves ASCII bit-wise. wm-spec
|
|
315 |
* mandates UTF8_STRING for _NET_WM_NAME but at least sawfish-1.0
|
|
316 |
* still uses STRING. (mmm, moving targets...).
|
|
317 |
*/
|
|
318 |
String charSet = "UTF8";
|
|
319 |
byte[] net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XA_UTF8_STRING.getAtom());
|
|
320 |
if (net_wm_name == null) {
|
|
321 |
net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XAtom.XA_STRING);
|
|
322 |
charSet = "ASCII";
|
|
323 |
}
|
|
324 |
|
|
325 |
if (net_wm_name == null) {
|
|
326 |
return null;
|
|
327 |
}
|
|
328 |
try {
|
|
329 |
net_wm_name_cache = new String(net_wm_name, charSet);
|
|
330 |
return net_wm_name_cache;
|
|
331 |
} catch (java.io.UnsupportedEncodingException uex) {
|
|
332 |
return null;
|
|
333 |
}
|
|
334 |
}
|
|
335 |
|
|
336 |
/**
|
|
337 |
* Sets _NET_WM_ICON property on the window using the List of XIconInfo
|
|
338 |
* If icons is null or empty list, removes _NET_WM_ICON property
|
|
339 |
*/
|
|
340 |
public void setWMIcons(XWindowPeer window, java.util.List<XIconInfo> icons) {
|
|
341 |
if (window == null) return;
|
|
342 |
|
|
343 |
XAtom iconsAtom = XAtom.get("_NET_WM_ICON");
|
|
344 |
if (icons == null) {
|
|
345 |
iconsAtom.DeleteProperty(window);
|
|
346 |
return;
|
|
347 |
}
|
|
348 |
|
|
349 |
int length = 0;
|
|
350 |
for (XIconInfo ii : icons) {
|
|
351 |
length += ii.getRawLength();
|
|
352 |
}
|
|
353 |
int cardinalSize = (XlibWrapper.dataModel == 32) ? 4 : 8;
|
|
354 |
int bufferSize = length * cardinalSize;
|
|
355 |
|
|
356 |
if (bufferSize != 0) {
|
|
357 |
long buffer = XlibWrapper.unsafe.allocateMemory(bufferSize);
|
|
358 |
try {
|
|
359 |
long ptr = buffer;
|
|
360 |
for (XIconInfo ii : icons) {
|
|
361 |
int size = ii.getRawLength() * cardinalSize;
|
|
362 |
if (XlibWrapper.dataModel == 32) {
|
|
363 |
XlibWrapper.copyIntArray(ptr, ii.getIntData(), size);
|
|
364 |
} else {
|
|
365 |
XlibWrapper.copyLongArray(ptr, ii.getLongData(), size);
|
|
366 |
}
|
|
367 |
ptr += size;
|
|
368 |
}
|
|
369 |
iconsAtom.setAtomData(window.getWindow(), XAtom.XA_CARDINAL, buffer, bufferSize/Native.getCard32Size());
|
|
370 |
} finally {
|
|
371 |
XlibWrapper.unsafe.freeMemory(buffer);
|
|
372 |
}
|
|
373 |
} else {
|
|
374 |
iconsAtom.DeleteProperty(window);
|
|
375 |
}
|
|
376 |
}
|
|
377 |
|
|
378 |
public boolean isWMStateNetHidden(XWindowPeer window) {
|
|
379 |
if (!doStateProtocol()) {
|
|
380 |
return false;
|
|
381 |
}
|
|
382 |
XAtomList state = window.getNETWMState();
|
|
383 |
return (state != null && state.size() != 0 && state.contains(XA_NET_WM_STATE_HIDDEN));
|
|
384 |
}
|
|
385 |
}
|