author | yan |
Mon, 06 Oct 2008 16:45:00 +0400 | |
changeset 1966 | 12a51fb0db0d |
parent 439 | 3488710b02f8 |
child 3938 | ef327bd847c0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
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 |
* This code is ported to XAWT from MAWT based on awt_mgrsel.c |
|
28 |
* code written originally by Valeriy Ushakov |
|
29 |
* Author : Bino George |
|
30 |
*/ |
|
31 |
||
32 |
package sun.awt.X11; |
|
33 |
||
34 |
import java.util.*; |
|
35 |
import java.util.logging.*; |
|
36 |
||
37 |
||
38 |
public class XMSelection { |
|
39 |
||
40 |
/* |
|
41 |
* A method for a subsytem to express its interest in a certain |
|
42 |
* manager selection. |
|
43 |
* |
|
44 |
* If owner changes, the ownerChanged of the XMSelectionListener |
|
45 |
* will be called with the screen |
|
46 |
* number and the new owning window when onwership is established, or |
|
47 |
* None if the owner is gone. |
|
48 |
* |
|
49 |
* Events in extra_mask are selected for on owning windows (exsiting |
|
50 |
* ones and on new owners when established) and otherEvent of the |
|
51 |
* XMWSelectionListener will be called with the screen number and an event. |
|
52 |
* |
|
53 |
* The function returns an array of current owners. The size of the |
|
54 |
* array is ScreenCount(awt_display). The array is "owned" by this |
|
55 |
* module and should be considered by the caller as read-only. |
|
56 |
*/ |
|
57 |
||
58 |
||
59 |
private static Logger log = Logger.getLogger("sun.awt.X11.XMSelection"); |
|
60 |
/* Name of the selection */ |
|
61 |
String selectionName; |
|
62 |
||
63 |
/* list of listeners to be called for events */ |
|
64 |
Vector listeners; |
|
65 |
||
66 |
/* X atom array (one per screen) for this selection */ |
|
67 |
XAtom atoms[]; |
|
68 |
||
69 |
/* Window ids of selection owners */ |
|
70 |
long owners[]; |
|
71 |
||
72 |
/* event mask to set */ |
|
73 |
long eventMask; |
|
74 |
||
75 |
static int numScreens; |
|
76 |
||
77 |
static XAtom XA_MANAGER; |
|
78 |
||
79 |
static HashMap selectionMap; |
|
80 |
||
81 |
static { |
|
82 |
long display = XToolkit.getDisplay(); |
|
83 |
XToolkit.awtLock(); |
|
84 |
try { |
|
85 |
numScreens = XlibWrapper.ScreenCount(display); |
|
86 |
} finally { |
|
87 |
XToolkit.awtUnlock(); |
|
88 |
} |
|
89 |
XA_MANAGER = XAtom.get("MANAGER"); |
|
90 |
for (int screen = 0; screen < numScreens ; screen ++) { |
|
91 |
initScreen(display,screen); |
|
92 |
} |
|
93 |
||
94 |
selectionMap = new HashMap(); |
|
95 |
} |
|
96 |
||
97 |
static void initScreen(long display, final int screen) { |
|
98 |
XToolkit.awtLock(); |
|
99 |
try { |
|
100 |
long root = XlibWrapper.RootWindow(display,screen); |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
101 |
XlibWrapper.XSelectInput(display, root, XConstants.StructureNotifyMask); |
2 | 102 |
XToolkit.addEventDispatcher(root, |
103 |
new XEventDispatcher() { |
|
104 |
public void dispatchEvent(XEvent ev) { |
|
105 |
processRootEvent(ev, screen); |
|
106 |
} |
|
107 |
}); |
|
108 |
||
109 |
} finally { |
|
110 |
XToolkit.awtUnlock(); |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
||
115 |
public int getNumberOfScreens() { |
|
116 |
return numScreens; |
|
117 |
} |
|
118 |
||
119 |
void select(long extra_mask) { |
|
120 |
eventMask = extra_mask; |
|
121 |
for (int screen = 0; screen < numScreens ; screen ++) { |
|
122 |
selectPerScreen(screen,extra_mask); |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
void resetOwner(long owner, final int screen) { |
|
127 |
XToolkit.awtLock(); |
|
128 |
try { |
|
129 |
long display = XToolkit.getDisplay(); |
|
130 |
synchronized(this) { |
|
131 |
setOwner(owner, screen); |
|
132 |
if (log.isLoggable(Level.FINE)) log.fine("New Selection Owner for screen " + screen + " = " + owner ); |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
133 |
XlibWrapper.XSelectInput(display, owner, XConstants.StructureNotifyMask | eventMask); |
2 | 134 |
XToolkit.addEventDispatcher(owner, |
135 |
new XEventDispatcher() { |
|
136 |
public void dispatchEvent(XEvent ev) { |
|
137 |
dispatchSelectionEvent(ev, screen); |
|
138 |
} |
|
139 |
}); |
|
140 |
||
141 |
} |
|
142 |
} finally { |
|
143 |
XToolkit.awtUnlock(); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
void selectPerScreen(final int screen, long extra_mask) { |
|
148 |
XToolkit.awtLock(); |
|
149 |
try { |
|
150 |
try { |
|
151 |
long display = XToolkit.getDisplay(); |
|
152 |
if (log.isLoggable(Level.FINE)) log.fine("Grabbing XServer"); |
|
153 |
XlibWrapper.XGrabServer(display); |
|
154 |
||
155 |
synchronized(this) { |
|
156 |
String selection_name = getName()+"_S"+screen; |
|
157 |
if (log.isLoggable(Level.FINE)) log.fine("Screen = " + screen + " selection name = " + selection_name); |
|
158 |
XAtom atom = XAtom.get(selection_name); |
|
159 |
selectionMap.put(Long.valueOf(atom.getAtom()),this); // add mapping from atom to the instance of XMSelection |
|
160 |
setAtom(atom,screen); |
|
161 |
long owner = XlibWrapper.XGetSelectionOwner(display, atom.getAtom()); |
|
162 |
if (owner != 0) { |
|
163 |
setOwner(owner, screen); |
|
164 |
if (log.isLoggable(Level.FINE)) log.fine("Selection Owner for screen " + screen + " = " + owner ); |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
165 |
XlibWrapper.XSelectInput(display, owner, XConstants.StructureNotifyMask | extra_mask); |
2 | 166 |
XToolkit.addEventDispatcher(owner, |
167 |
new XEventDispatcher() { |
|
168 |
public void dispatchEvent(XEvent ev) { |
|
169 |
dispatchSelectionEvent(ev, screen); |
|
170 |
} |
|
171 |
}); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
catch (Exception e) { |
|
176 |
e.printStackTrace(); |
|
177 |
} |
|
178 |
finally { |
|
179 |
if (log.isLoggable(Level.FINE)) log.fine("UnGrabbing XServer"); |
|
180 |
XlibWrapper.XUngrabServer(XToolkit.getDisplay()); |
|
181 |
} |
|
182 |
} finally { |
|
183 |
XToolkit.awtUnlock(); |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
||
188 |
static boolean processClientMessage(XEvent xev, int screen) { |
|
189 |
XClientMessageEvent xce = xev.get_xclient(); |
|
190 |
if (xce.get_message_type() == XA_MANAGER.getAtom()) { |
|
191 |
if (log.isLoggable(Level.FINE)) log.fine("client messags = " + xce); |
|
192 |
long timestamp = xce.get_data(0); |
|
193 |
long atom = xce.get_data(1); |
|
194 |
long owner = xce.get_data(2); |
|
195 |
long data = xce.get_data(3); |
|
196 |
||
197 |
XMSelection sel = getInstance(atom); |
|
198 |
if (sel != null) { |
|
199 |
sel.resetOwner(owner,screen); |
|
200 |
sel.dispatchOwnerChangedEvent(xev,screen,owner,data, timestamp); |
|
201 |
} |
|
202 |
} |
|
203 |
return false; |
|
204 |
} |
|
205 |
||
206 |
static boolean processRootEvent(XEvent xev, int screen) { |
|
207 |
switch (xev.get_type()) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
208 |
case XConstants.ClientMessage: { |
2 | 209 |
return processClientMessage(xev, screen); |
210 |
} |
|
211 |
} |
|
212 |
||
213 |
return false; |
|
214 |
||
215 |
} |
|
216 |
||
217 |
||
218 |
static XMSelection getInstance(long selection) { |
|
219 |
return (XMSelection) selectionMap.get(Long.valueOf(selection)); |
|
220 |
} |
|
221 |
||
222 |
||
223 |
/* |
|
224 |
* Default constructor specifies PropertyChangeMask as well |
|
225 |
*/ |
|
226 |
||
227 |
public XMSelection (String selname) { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
228 |
this(selname, XConstants.PropertyChangeMask); |
2 | 229 |
} |
230 |
||
231 |
||
232 |
/* |
|
233 |
* Some users may not need to know about selection changes, |
|
234 |
* just owner ship changes, They would specify a zero extra mask. |
|
235 |
*/ |
|
236 |
||
237 |
public XMSelection (String selname, long extraMask) { |
|
238 |
||
239 |
synchronized (this) { |
|
240 |
selectionName = selname; |
|
241 |
atoms = new XAtom[getNumberOfScreens()]; |
|
242 |
owners = new long[getNumberOfScreens()]; |
|
243 |
} |
|
244 |
select(extraMask); |
|
245 |
} |
|
246 |
||
247 |
||
248 |
||
249 |
public synchronized void addSelectionListener(XMSelectionListener listener) { |
|
250 |
if (listeners == null) { |
|
251 |
listeners = new Vector(); |
|
252 |
} |
|
253 |
listeners.add(listener); |
|
254 |
} |
|
255 |
||
256 |
public synchronized void removeSelectionListener(XMSelectionListener listener) { |
|
257 |
if (listeners != null) { |
|
258 |
listeners.remove(listener); |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
synchronized Collection getListeners() { |
|
263 |
return listeners; |
|
264 |
} |
|
265 |
||
266 |
synchronized XAtom getAtom(int screen) { |
|
267 |
if (atoms != null) { |
|
268 |
return atoms[screen]; |
|
269 |
} |
|
270 |
return null; |
|
271 |
} |
|
272 |
||
273 |
synchronized void setAtom(XAtom a, int screen) { |
|
274 |
if (atoms != null) { |
|
275 |
atoms[screen] = a; |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
synchronized long getOwner(int screen) { |
|
280 |
if (owners != null) { |
|
281 |
return owners[screen]; |
|
282 |
} |
|
283 |
return 0; |
|
284 |
} |
|
285 |
||
286 |
synchronized void setOwner(long owner, int screen) { |
|
287 |
if (owners != null) { |
|
288 |
owners[screen] = owner; |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
synchronized String getName() { |
|
293 |
return selectionName; |
|
294 |
} |
|
295 |
||
296 |
||
297 |
synchronized void dispatchSelectionChanged( XPropertyEvent ev, int screen) { |
|
298 |
if (log.isLoggable(Level.FINE)) log.fine("Selection Changed : Screen = " + screen + "Event =" + ev); |
|
299 |
if (listeners != null) { |
|
300 |
Iterator iter = listeners.iterator(); |
|
301 |
while (iter.hasNext()) { |
|
302 |
XMSelectionListener disp = (XMSelectionListener) iter.next(); |
|
303 |
disp.selectionChanged(screen, this, ev.get_window(), ev); |
|
304 |
} |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
synchronized void dispatchOwnerDeath(XDestroyWindowEvent de, int screen) { |
|
309 |
if (log.isLoggable(Level.FINE)) log.fine("Owner dead : Screen = " + screen + "Event =" + de); |
|
310 |
if (listeners != null) { |
|
311 |
Iterator iter = listeners.iterator(); |
|
312 |
while (iter.hasNext()) { |
|
313 |
XMSelectionListener disp = (XMSelectionListener) iter.next(); |
|
314 |
disp.ownerDeath(screen, this, de.get_window()); |
|
315 |
||
316 |
} |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
void dispatchSelectionEvent(XEvent xev, int screen) { |
|
321 |
if (log.isLoggable(Level.FINE)) log.fine("Event =" + xev); |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
322 |
if (xev.get_type() == XConstants.DestroyNotify) { |
2 | 323 |
XDestroyWindowEvent de = xev.get_xdestroywindow(); |
324 |
dispatchOwnerDeath( de, screen); |
|
325 |
} |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
326 |
else if (xev.get_type() == XConstants.PropertyNotify) { |
2 | 327 |
XPropertyEvent xpe = xev.get_xproperty(); |
328 |
dispatchSelectionChanged( xpe, screen); |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
||
333 |
synchronized void dispatchOwnerChangedEvent(XEvent ev, int screen, long owner, long data, long timestamp) { |
|
334 |
if (listeners != null) { |
|
335 |
Iterator iter = listeners.iterator(); |
|
336 |
while (iter.hasNext()) { |
|
337 |
XMSelectionListener disp = (XMSelectionListener) iter.next(); |
|
338 |
disp.ownerChanged(screen,this, owner, data, timestamp); |
|
339 |
} |
|
340 |
} |
|
341 |
} |
|
342 |
||
343 |
||
344 |
} |