author | yan |
Mon, 06 Oct 2008 16:45:00 +0400 | |
changeset 1966 | 12a51fb0db0d |
parent 1189 | ee54b5ce2113 |
child 2451 | 597df8e1d786 |
permissions | -rw-r--r-- |
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); |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
107
diff
changeset
|
102 |
req.set_type((int)XConstants.ClientMessage); |
2 | 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, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
107
diff
changeset
|
112 |
XConstants.SubstructureRedirectMask | XConstants.SubstructureNotifyMask, |
2 | 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 { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
107
diff
changeset
|
186 |
req.set_type((int)XConstants.ClientMessage); |
2 | 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()); |
|
1189
ee54b5ce2113
6735584: XNETProtocol:setStateHelper() produces bad _NET_WM_STATE messages
art
parents:
439
diff
changeset
|
192 |
// Fix for 6735584: req.data[2] must be set to 0 when only one property is changed |
ee54b5ce2113
6735584: XNETProtocol:setStateHelper() produces bad _NET_WM_STATE messages
art
parents:
439
diff
changeset
|
193 |
req.set_data(2, 0); |
2 | 194 |
log.log(Level.FINE, "Setting _NET_STATE atom {0} on {1} for {2}", new Object[] {state, window, Boolean.valueOf(set)}); |
195 |
XToolkit.awtLock(); |
|
196 |
try { |
|
197 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(), |
|
198 |
XlibWrapper.RootWindow(XToolkit.getDisplay(), window.getScreenNumber()), |
|
199 |
false, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
107
diff
changeset
|
200 |
XConstants.SubstructureRedirectMask | XConstants.SubstructureNotifyMask, |
2 | 201 |
req.pData); |
202 |
} |
|
203 |
finally { |
|
204 |
XToolkit.awtUnlock(); |
|
205 |
} |
|
206 |
} finally { |
|
207 |
req.dispose(); |
|
208 |
} |
|
209 |
} else { |
|
210 |
XAtomList net_wm_state = window.getNETWMState(); |
|
211 |
log.log(Level.FINE, "Current state on {0} is {1}", new Object[] {window, net_wm_state}); |
|
212 |
if (!set) { |
|
213 |
net_wm_state.remove(state); |
|
214 |
} else { |
|
215 |
net_wm_state.add(state); |
|
216 |
} |
|
217 |
log.log(Level.FINE, "Setting states on {0} to {1}", new Object[] {window, net_wm_state}); |
|
218 |
window.setNETWMState(net_wm_state); |
|
219 |
} |
|
220 |
XToolkit.XSync(); |
|
221 |
} |
|
222 |
||
223 |
public void setLayer(XWindowPeer window, int layer) { |
|
224 |
setStateHelper(window, XA_NET_WM_STATE_ABOVE, layer == LAYER_ALWAYS_ON_TOP); |
|
225 |
} |
|
226 |
||
227 |
/* New "netwm" spec from www.freedesktop.org */ |
|
228 |
XAtom XA_UTF8_STRING = XAtom.get("UTF8_STRING"); /* like STRING but encoding is UTF-8 */ |
|
229 |
XAtom XA_NET_SUPPORTING_WM_CHECK = XAtom.get("_NET_SUPPORTING_WM_CHECK"); |
|
230 |
XAtom XA_NET_SUPPORTED = XAtom.get("_NET_SUPPORTED"); /* list of protocols (property of root) */ |
|
231 |
XAtom XA_NET_WM_NAME = XAtom.get("_NET_WM_NAME"); /* window property */ |
|
232 |
XAtom XA_NET_WM_STATE = XAtom.get("_NET_WM_STATE");/* both window property and request */ |
|
233 |
||
234 |
/* |
|
235 |
* _NET_WM_STATE is a list of atoms. |
|
236 |
* NB: Standard spelling is "HORZ" (yes, without an 'I'), but KDE2 |
|
237 |
* uses misspelled "HORIZ" (see KDE bug #20229). This was fixed in |
|
238 |
* KDE 2.2. Under earlier versions of KDE2 horizontal and full |
|
239 |
* maximization doesn't work . |
|
240 |
*/ |
|
241 |
XAtom XA_NET_WM_STATE_MAXIMIZED_HORZ = XAtom.get("_NET_WM_STATE_MAXIMIZED_HORZ"); |
|
242 |
XAtom XA_NET_WM_STATE_MAXIMIZED_VERT = XAtom.get("_NET_WM_STATE_MAXIMIZED_VERT"); |
|
243 |
XAtom XA_NET_WM_STATE_SHADED = XAtom.get("_NET_WM_STATE_SHADED"); |
|
244 |
XAtom XA_NET_WM_STATE_ABOVE = XAtom.get("_NET_WM_STATE_ABOVE"); |
|
245 |
XAtom XA_NET_WM_STATE_MODAL = XAtom.get("_NET_WM_STATE_MODAL"); |
|
246 |
XAtom XA_NET_WM_STATE_FULLSCREEN = XAtom.get("_NET_WM_STATE_FULLSCREEN"); |
|
247 |
XAtom XA_NET_WM_STATE_BELOW = XAtom.get("_NET_WM_STATE_BELOW"); |
|
248 |
XAtom XA_NET_WM_STATE_HIDDEN = XAtom.get("_NET_WM_STATE_HIDDEN"); |
|
249 |
XAtom XA_NET_WM_STATE_SKIP_TASKBAR = XAtom.get("_NET_WM_STATE_SKIP_TASKBAR"); |
|
250 |
XAtom XA_NET_WM_STATE_SKIP_PAGER = XAtom.get("_NET_WM_STATE_SKIP_PAGER"); |
|
251 |
||
252 |
XAtom XA_NET_WM_WINDOW_TYPE = XAtom.get("_NET_WM_WINDOW_TYPE"); |
|
253 |
XAtom XA_NET_WM_WINDOW_TYPE_DIALOG = XAtom.get("_NET_WM_WINDOW_TYPE_DIALOG"); |
|
254 |
||
255 |
/* For _NET_WM_STATE ClientMessage requests */ |
|
256 |
final static int _NET_WM_STATE_REMOVE =0; /* remove/unset property */ |
|
257 |
final static int _NET_WM_STATE_ADD =1; /* add/set property */ |
|
258 |
final static int _NET_WM_STATE_TOGGLE =2; /* toggle property */ |
|
259 |
||
260 |
boolean supportChecked = false; |
|
261 |
long NetWindow = 0; |
|
262 |
void detect() { |
|
263 |
if (supportChecked) { |
|
264 |
// TODO: How about detecting WM-restart or exit? |
|
265 |
return; |
|
266 |
} |
|
267 |
NetWindow = checkAnchor(XA_NET_SUPPORTING_WM_CHECK, XAtom.XA_WINDOW); |
|
268 |
supportChecked = true; |
|
269 |
if (log.isLoggable(Level.FINE)) log.fine("### " + this + " is active: " + (NetWindow != 0)); |
|
270 |
} |
|
271 |
||
272 |
boolean active() { |
|
273 |
detect(); |
|
274 |
return NetWindow != 0; |
|
275 |
} |
|
276 |
||
277 |
boolean doStateProtocol() { |
|
278 |
boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE); |
|
107 | 279 |
stateLog.finer("doStateProtocol() returns " + res); |
2 | 280 |
return res; |
281 |
} |
|
282 |
||
283 |
boolean doLayerProtocol() { |
|
284 |
boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_ABOVE); |
|
285 |
return res; |
|
286 |
} |
|
287 |
||
288 |
boolean doModalityProtocol() { |
|
289 |
boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_MODAL); |
|
290 |
return res; |
|
291 |
} |
|
292 |
boolean isWMName(String name) { |
|
293 |
if (!active()) { |
|
294 |
return false; |
|
295 |
} |
|
296 |
String net_wm_name_string = getWMName(); |
|
297 |
if (net_wm_name_string == null) { |
|
298 |
return false; |
|
299 |
} |
|
300 |
if (log.isLoggable(Level.FINE)) log.fine("### WM_NAME = " + net_wm_name_string); |
|
301 |
return net_wm_name_string.startsWith(name); |
|
302 |
} |
|
303 |
||
304 |
String net_wm_name_cache; |
|
305 |
public String getWMName() { |
|
306 |
if (!active()) { |
|
307 |
return null; |
|
308 |
} |
|
309 |
||
310 |
if (net_wm_name_cache != null) { |
|
311 |
return net_wm_name_cache; |
|
312 |
} |
|
313 |
||
314 |
/* |
|
315 |
* Check both UTF8_STRING and STRING. We only call this function |
|
316 |
* with ASCII names and UTF8 preserves ASCII bit-wise. wm-spec |
|
317 |
* mandates UTF8_STRING for _NET_WM_NAME but at least sawfish-1.0 |
|
318 |
* still uses STRING. (mmm, moving targets...). |
|
319 |
*/ |
|
320 |
String charSet = "UTF8"; |
|
321 |
byte[] net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XA_UTF8_STRING.getAtom()); |
|
322 |
if (net_wm_name == null) { |
|
323 |
net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XAtom.XA_STRING); |
|
324 |
charSet = "ASCII"; |
|
325 |
} |
|
326 |
||
327 |
if (net_wm_name == null) { |
|
328 |
return null; |
|
329 |
} |
|
330 |
try { |
|
331 |
net_wm_name_cache = new String(net_wm_name, charSet); |
|
332 |
return net_wm_name_cache; |
|
333 |
} catch (java.io.UnsupportedEncodingException uex) { |
|
334 |
return null; |
|
335 |
} |
|
336 |
} |
|
337 |
||
338 |
/** |
|
339 |
* Sets _NET_WM_ICON property on the window using the List of XIconInfo |
|
340 |
* If icons is null or empty list, removes _NET_WM_ICON property |
|
341 |
*/ |
|
342 |
public void setWMIcons(XWindowPeer window, java.util.List<XIconInfo> icons) { |
|
343 |
if (window == null) return; |
|
344 |
||
345 |
XAtom iconsAtom = XAtom.get("_NET_WM_ICON"); |
|
346 |
if (icons == null) { |
|
347 |
iconsAtom.DeleteProperty(window); |
|
348 |
return; |
|
349 |
} |
|
350 |
||
351 |
int length = 0; |
|
352 |
for (XIconInfo ii : icons) { |
|
353 |
length += ii.getRawLength(); |
|
354 |
} |
|
355 |
int cardinalSize = (XlibWrapper.dataModel == 32) ? 4 : 8; |
|
356 |
int bufferSize = length * cardinalSize; |
|
357 |
||
358 |
if (bufferSize != 0) { |
|
359 |
long buffer = XlibWrapper.unsafe.allocateMemory(bufferSize); |
|
360 |
try { |
|
361 |
long ptr = buffer; |
|
362 |
for (XIconInfo ii : icons) { |
|
363 |
int size = ii.getRawLength() * cardinalSize; |
|
364 |
if (XlibWrapper.dataModel == 32) { |
|
365 |
XlibWrapper.copyIntArray(ptr, ii.getIntData(), size); |
|
366 |
} else { |
|
367 |
XlibWrapper.copyLongArray(ptr, ii.getLongData(), size); |
|
368 |
} |
|
369 |
ptr += size; |
|
370 |
} |
|
371 |
iconsAtom.setAtomData(window.getWindow(), XAtom.XA_CARDINAL, buffer, bufferSize/Native.getCard32Size()); |
|
372 |
} finally { |
|
373 |
XlibWrapper.unsafe.freeMemory(buffer); |
|
374 |
} |
|
375 |
} else { |
|
376 |
iconsAtom.DeleteProperty(window); |
|
377 |
} |
|
378 |
} |
|
379 |
||
380 |
public boolean isWMStateNetHidden(XWindowPeer window) { |
|
381 |
if (!doStateProtocol()) { |
|
382 |
return false; |
|
383 |
} |
|
384 |
XAtomList state = window.getNETWMState(); |
|
385 |
return (state != null && state.size() != 0 && state.contains(XA_NET_WM_STATE_HIDDEN)); |
|
386 |
} |
|
387 |
} |