Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/X11/XBaseWindow.java
32288 views
1
/*
2
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.awt.X11;
27
28
import java.awt.*;
29
import sun.awt.*;
30
import java.util.*;
31
import sun.util.logging.PlatformLogger;
32
33
public class XBaseWindow {
34
private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XBaseWindow");
35
private static final PlatformLogger insLog = PlatformLogger.getLogger("sun.awt.X11.insets.XBaseWindow");
36
private static final PlatformLogger eventLog = PlatformLogger.getLogger("sun.awt.X11.event.XBaseWindow");
37
private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.X11.focus.XBaseWindow");
38
private static final PlatformLogger grabLog = PlatformLogger.getLogger("sun.awt.X11.grab.XBaseWindow");
39
40
public static final String
41
PARENT_WINDOW = "parent window", // parent window, Long
42
BOUNDS = "bounds", // bounds of the window, Rectangle
43
OVERRIDE_REDIRECT = "overrideRedirect", // override_redirect setting, Boolean
44
EVENT_MASK = "event mask", // event mask, Integer
45
VALUE_MASK = "value mask", // value mask, Long
46
BORDER_PIXEL = "border pixel", // border pixel value, Integer
47
COLORMAP = "color map", // color map, Long
48
DEPTH = "visual depth", // depth, Integer
49
VISUAL_CLASS = "visual class", // visual class, Integer
50
VISUAL = "visual", // visual, Long
51
EMBEDDED = "embedded", // is embedded?, Boolean
52
DELAYED = "delayed", // is creation delayed?, Boolean
53
PARENT = "parent", // parent peer
54
BACKGROUND_PIXMAP = "pixmap", // background pixmap
55
VISIBLE = "visible", // whether it is visible by default
56
SAVE_UNDER = "save under", // save content under this window
57
BACKING_STORE = "backing store", // enables double buffering
58
BIT_GRAVITY = "bit gravity"; // copy old content on geometry change
59
private XCreateWindowParams delayedParams;
60
61
Set<Long> children = new HashSet<Long>();
62
long window;
63
boolean visible;
64
boolean mapped;
65
boolean embedded;
66
Rectangle maxBounds;
67
volatile XBaseWindow parentWindow;
68
69
private boolean disposed;
70
71
private long screen;
72
private XSizeHints hints;
73
private XWMHints wmHints;
74
75
final static int MIN_SIZE = 1;
76
final static int DEF_LOCATION = 1;
77
78
private static XAtom wm_client_leader;
79
80
static enum InitialiseState {
81
INITIALISING,
82
INITIALISED,
83
FAILED_INITIALISATION
84
};
85
86
private InitialiseState initialising;
87
88
int x;
89
int y;
90
int width;
91
int height;
92
93
void awtLock() {
94
XToolkit.awtLock();
95
}
96
97
void awtUnlock() {
98
XToolkit.awtUnlock();
99
}
100
101
void awtLockNotifyAll() {
102
XToolkit.awtLockNotifyAll();
103
}
104
105
void awtLockWait() throws InterruptedException {
106
XToolkit.awtLockWait();
107
}
108
109
// To prevent errors from overriding obsolete methods
110
protected final void init(long parentWindow, Rectangle bounds) {}
111
protected final void preInit() {}
112
protected final void postInit() {}
113
114
// internal lock for synchronizing state changes and paint calls, initialized in preInit.
115
// the order with other locks: AWTLock -> stateLock
116
static class StateLock extends Object { }
117
protected StateLock state_lock;
118
119
/**
120
* Called for delayed inits during construction
121
*/
122
void instantPreInit(XCreateWindowParams params) {
123
state_lock = new StateLock();
124
}
125
126
/**
127
* Called before window creation, descendants should override to initialize the data,
128
* initialize params.
129
*/
130
void preInit(XCreateWindowParams params) {
131
state_lock = new StateLock();
132
embedded = Boolean.TRUE.equals(params.get(EMBEDDED));
133
visible = Boolean.TRUE.equals(params.get(VISIBLE));
134
135
Object parent = params.get(PARENT);
136
if (parent instanceof XBaseWindow) {
137
parentWindow = (XBaseWindow)parent;
138
} else {
139
Long parentWindowID = (Long)params.get(PARENT_WINDOW);
140
if (parentWindowID != null) {
141
parentWindow = XToolkit.windowToXWindow(parentWindowID);
142
}
143
}
144
145
Long eventMask = (Long)params.get(EVENT_MASK);
146
if (eventMask != null) {
147
long mask = eventMask.longValue();
148
mask |= XConstants.SubstructureNotifyMask;
149
params.put(EVENT_MASK, mask);
150
}
151
152
screen = -1;
153
}
154
155
/**
156
* Called after window creation, descendants should override to initialize Window
157
* with class-specific values and perform post-initialization actions.
158
*/
159
void postInit(XCreateWindowParams params) {
160
if (log.isLoggable(PlatformLogger.Level.FINE)) {
161
log.fine("WM name is " + getWMName());
162
}
163
updateWMName();
164
165
// Set WM_CLIENT_LEADER property
166
initClientLeader();
167
}
168
169
/**
170
* Creates window using parameters <code>params</code>
171
* If params contain flag DELAYED doesn't do anything.
172
* Note: Descendants can call this method to create the window
173
* at the time different to instance construction.
174
*/
175
protected final void init(XCreateWindowParams params) {
176
awtLock();
177
initialising = InitialiseState.INITIALISING;
178
awtUnlock();
179
180
try {
181
if (!Boolean.TRUE.equals(params.get(DELAYED))) {
182
preInit(params);
183
create(params);
184
postInit(params);
185
} else {
186
instantPreInit(params);
187
delayedParams = params;
188
}
189
awtLock();
190
initialising = InitialiseState.INITIALISED;
191
awtLockNotifyAll();
192
awtUnlock();
193
} catch (RuntimeException re) {
194
awtLock();
195
initialising = InitialiseState.FAILED_INITIALISATION;
196
awtLockNotifyAll();
197
awtUnlock();
198
throw re;
199
} catch (Throwable t) {
200
log.warning("Exception during peer initialization", t);
201
awtLock();
202
initialising = InitialiseState.FAILED_INITIALISATION;
203
awtLockNotifyAll();
204
awtUnlock();
205
}
206
}
207
208
public boolean checkInitialised() {
209
awtLock();
210
try {
211
switch (initialising) {
212
case INITIALISED:
213
return true;
214
case INITIALISING:
215
try {
216
while (initialising != InitialiseState.INITIALISED) {
217
awtLockWait();
218
}
219
} catch (InterruptedException ie) {
220
return false;
221
}
222
return true;
223
case FAILED_INITIALISATION:
224
return false;
225
default:
226
return false;
227
}
228
} finally {
229
awtUnlock();
230
}
231
}
232
233
/*
234
* Creates an invisible InputOnly window without an associated Component.
235
*/
236
XBaseWindow() {
237
this(new XCreateWindowParams());
238
}
239
240
/**
241
* Creates normal child window
242
*/
243
XBaseWindow(long parentWindow, Rectangle bounds) {
244
this(new XCreateWindowParams(new Object[] {
245
BOUNDS, bounds,
246
PARENT_WINDOW, Long.valueOf(parentWindow)}));
247
}
248
249
/**
250
* Creates top-level window
251
*/
252
XBaseWindow(Rectangle bounds) {
253
this(new XCreateWindowParams(new Object[] {
254
BOUNDS, bounds
255
}));
256
}
257
258
public XBaseWindow (XCreateWindowParams params) {
259
init(params);
260
}
261
262
/* This create is used by the XEmbeddedFramePeer since it has to create the window
263
as a child of the netscape window. This netscape window is passed in as wid */
264
XBaseWindow(long parentWindow) {
265
this(new XCreateWindowParams(new Object[] {
266
PARENT_WINDOW, Long.valueOf(parentWindow),
267
EMBEDDED, Boolean.TRUE
268
}));
269
}
270
271
/**
272
* Verifies that all required parameters are set. If not, sets them to default values.
273
* Verifies values of critical parameters, adjust their values when needed.
274
* @throws IllegalArgumentException if params is null
275
*/
276
protected void checkParams(XCreateWindowParams params) {
277
if (params == null) {
278
throw new IllegalArgumentException("Window creation parameters are null");
279
}
280
params.putIfNull(PARENT_WINDOW, Long.valueOf(XToolkit.getDefaultRootWindow()));
281
params.putIfNull(BOUNDS, new Rectangle(DEF_LOCATION, DEF_LOCATION, MIN_SIZE, MIN_SIZE));
282
params.putIfNull(DEPTH, Integer.valueOf((int)XConstants.CopyFromParent));
283
params.putIfNull(VISUAL, Long.valueOf(XConstants.CopyFromParent));
284
params.putIfNull(VISUAL_CLASS, Integer.valueOf((int)XConstants.InputOnly));
285
params.putIfNull(VALUE_MASK, Long.valueOf(XConstants.CWEventMask));
286
Rectangle bounds = (Rectangle)params.get(BOUNDS);
287
bounds.width = Math.max(MIN_SIZE, bounds.width);
288
bounds.height = Math.max(MIN_SIZE, bounds.height);
289
290
Long eventMaskObj = (Long)params.get(EVENT_MASK);
291
long eventMask = eventMaskObj != null ? eventMaskObj.longValue() : 0;
292
// We use our own synthetic grab see XAwtState.getGrabWindow()
293
// (see X vol. 1, 8.3.3.2)
294
eventMask |= XConstants.PropertyChangeMask | XConstants.OwnerGrabButtonMask;
295
params.put(EVENT_MASK, Long.valueOf(eventMask));
296
}
297
298
/**
299
* Creates window with parameters specified by <code>params</code>
300
* @see #init
301
*/
302
private final void create(XCreateWindowParams params) {
303
XToolkit.awtLock();
304
try {
305
XSetWindowAttributes xattr = new XSetWindowAttributes();
306
try {
307
checkParams(params);
308
309
long value_mask = ((Long)params.get(VALUE_MASK)).longValue();
310
311
Long eventMask = (Long)params.get(EVENT_MASK);
312
xattr.set_event_mask(eventMask.longValue());
313
value_mask |= XConstants.CWEventMask;
314
315
Long border_pixel = (Long)params.get(BORDER_PIXEL);
316
if (border_pixel != null) {
317
xattr.set_border_pixel(border_pixel.longValue());
318
value_mask |= XConstants.CWBorderPixel;
319
}
320
321
Long colormap = (Long)params.get(COLORMAP);
322
if (colormap != null) {
323
xattr.set_colormap(colormap.longValue());
324
value_mask |= XConstants.CWColormap;
325
}
326
Long background_pixmap = (Long)params.get(BACKGROUND_PIXMAP);
327
if (background_pixmap != null) {
328
xattr.set_background_pixmap(background_pixmap.longValue());
329
value_mask |= XConstants.CWBackPixmap;
330
}
331
332
Long parentWindow = (Long)params.get(PARENT_WINDOW);
333
Rectangle bounds = (Rectangle)params.get(BOUNDS);
334
Integer depth = (Integer)params.get(DEPTH);
335
Integer visual_class = (Integer)params.get(VISUAL_CLASS);
336
Long visual = (Long)params.get(VISUAL);
337
Boolean overrideRedirect = (Boolean)params.get(OVERRIDE_REDIRECT);
338
if (overrideRedirect != null) {
339
xattr.set_override_redirect(overrideRedirect.booleanValue());
340
value_mask |= XConstants.CWOverrideRedirect;
341
}
342
343
Boolean saveUnder = (Boolean)params.get(SAVE_UNDER);
344
if (saveUnder != null) {
345
xattr.set_save_under(saveUnder.booleanValue());
346
value_mask |= XConstants.CWSaveUnder;
347
}
348
349
Integer backingStore = (Integer)params.get(BACKING_STORE);
350
if (backingStore != null) {
351
xattr.set_backing_store(backingStore.intValue());
352
value_mask |= XConstants.CWBackingStore;
353
}
354
355
Integer bitGravity = (Integer)params.get(BIT_GRAVITY);
356
if (bitGravity != null) {
357
xattr.set_bit_gravity(bitGravity.intValue());
358
value_mask |= XConstants.CWBitGravity;
359
}
360
361
if (log.isLoggable(PlatformLogger.Level.FINE)) {
362
log.fine("Creating window for " + this + " with the following attributes: \n" + params);
363
}
364
window = XlibWrapper.XCreateWindow(XToolkit.getDisplay(),
365
parentWindow.longValue(),
366
bounds.x, bounds.y, // location
367
bounds.width, bounds.height, // size
368
0, // border
369
depth.intValue(), // depth
370
visual_class.intValue(), // class
371
visual.longValue(), // visual
372
value_mask, // value mask
373
xattr.pData); // attributes
374
375
if (window == 0) {
376
throw new IllegalStateException("Couldn't create window because of wrong parameters. Run with NOISY_AWT to see details");
377
}
378
XToolkit.addToWinMap(window, this);
379
} finally {
380
xattr.dispose();
381
}
382
} finally {
383
XToolkit.awtUnlock();
384
}
385
}
386
387
public XCreateWindowParams getDelayedParams() {
388
return delayedParams;
389
}
390
391
protected String getWMName() {
392
return XToolkit.getCorrectXIDString(getClass().getName());
393
}
394
395
protected void initClientLeader() {
396
XToolkit.awtLock();
397
try {
398
if (wm_client_leader == null) {
399
wm_client_leader = XAtom.get("WM_CLIENT_LEADER");
400
}
401
wm_client_leader.setWindowProperty(this, getXAWTRootWindow());
402
} finally {
403
XToolkit.awtUnlock();
404
}
405
}
406
407
static XRootWindow getXAWTRootWindow() {
408
return XRootWindow.getInstance();
409
}
410
411
void destroy() {
412
XToolkit.awtLock();
413
try {
414
if (hints != null) {
415
XlibWrapper.XFree(hints.pData);
416
hints = null;
417
}
418
XToolkit.removeFromWinMap(getWindow(), this);
419
XlibWrapper.XDestroyWindow(XToolkit.getDisplay(), getWindow());
420
if (XPropertyCache.isCachingSupported()) {
421
XPropertyCache.clearCache(window);
422
}
423
window = -1;
424
if( !isDisposed() ) {
425
setDisposed( true );
426
}
427
428
XAwtState.getGrabWindow(); // Magic - getGrabWindow clear state if grabbing window is disposed of.
429
} finally {
430
XToolkit.awtUnlock();
431
}
432
}
433
434
void flush() {
435
XToolkit.awtLock();
436
try {
437
XlibWrapper.XFlush(XToolkit.getDisplay());
438
} finally {
439
XToolkit.awtUnlock();
440
}
441
}
442
443
/**
444
* Helper function to set W
445
*/
446
public final void setWMHints(XWMHints hints) {
447
XToolkit.awtLock();
448
try {
449
XlibWrapper.XSetWMHints(XToolkit.getDisplay(), getWindow(), hints.pData);
450
} finally {
451
XToolkit.awtUnlock();
452
}
453
}
454
455
public XWMHints getWMHints() {
456
if (wmHints == null) {
457
wmHints = new XWMHints(XlibWrapper.XAllocWMHints());
458
// XlibWrapper.XGetWMHints(XToolkit.getDisplay(),
459
// getWindow(),
460
// wmHints.pData);
461
}
462
return wmHints;
463
}
464
465
466
/*
467
* Call this method under AWTLock.
468
* The lock should be acquired untill all operations with XSizeHints are completed.
469
*/
470
public XSizeHints getHints() {
471
if (hints == null) {
472
long p_hints = XlibWrapper.XAllocSizeHints();
473
hints = new XSizeHints(p_hints);
474
// XlibWrapper.XGetWMNormalHints(XToolkit.getDisplay(), getWindow(), p_hints, XlibWrapper.larg1);
475
// TODO: Shouldn't we listen for WM updates on this property?
476
}
477
return hints;
478
}
479
480
public void setSizeHints(long flags, int x, int y, int width, int height) {
481
if (insLog.isLoggable(PlatformLogger.Level.FINER)) {
482
insLog.finer("Setting hints, flags " + XlibWrapper.hintsToString(flags));
483
}
484
XToolkit.awtLock();
485
try {
486
XSizeHints hints = getHints();
487
// Note: if PPosition is not set in flags this means that
488
// we want to reset PPosition in hints. This is necessary
489
// for locationByPlatform functionality
490
if ((flags & XUtilConstants.PPosition) != 0) {
491
hints.set_x(x);
492
hints.set_y(y);
493
}
494
if ((flags & XUtilConstants.PSize) != 0) {
495
hints.set_width(width);
496
hints.set_height(height);
497
} else if ((hints.get_flags() & XUtilConstants.PSize) != 0) {
498
flags |= XUtilConstants.PSize;
499
}
500
if ((flags & XUtilConstants.PMinSize) != 0) {
501
hints.set_min_width(width);
502
hints.set_min_height(height);
503
} else if ((hints.get_flags() & XUtilConstants.PMinSize) != 0) {
504
flags |= XUtilConstants.PMinSize;
505
//Fix for 4320050: Minimum size for java.awt.Frame is not being enforced.
506
//We don't need to reset minimum size if it's already set
507
}
508
if ((flags & XUtilConstants.PMaxSize) != 0) {
509
if (maxBounds != null) {
510
if (maxBounds.width != Integer.MAX_VALUE) {
511
hints.set_max_width(maxBounds.width);
512
} else {
513
hints.set_max_width(XToolkit.getDefaultScreenWidth());
514
}
515
if (maxBounds.height != Integer.MAX_VALUE) {
516
hints.set_max_height(maxBounds.height);
517
} else {
518
hints.set_max_height(XToolkit.getDefaultScreenHeight());
519
}
520
} else {
521
hints.set_max_width(width);
522
hints.set_max_height(height);
523
}
524
} else if ((hints.get_flags() & XUtilConstants.PMaxSize) != 0) {
525
flags |= XUtilConstants.PMaxSize;
526
if (maxBounds != null) {
527
if (maxBounds.width != Integer.MAX_VALUE) {
528
hints.set_max_width(maxBounds.width);
529
} else {
530
hints.set_max_width(XToolkit.getDefaultScreenWidth());
531
}
532
if (maxBounds.height != Integer.MAX_VALUE) {
533
hints.set_max_height(maxBounds.height);
534
} else {
535
hints.set_max_height(XToolkit.getDefaultScreenHeight());
536
}
537
} else {
538
// Leave intact
539
}
540
}
541
flags |= XUtilConstants.PWinGravity;
542
hints.set_flags(flags);
543
hints.set_win_gravity((int)XConstants.NorthWestGravity);
544
if (insLog.isLoggable(PlatformLogger.Level.FINER)) {
545
insLog.finer("Setting hints, resulted flags " + XlibWrapper.hintsToString(flags) +
546
", values " + hints);
547
}
548
XlibWrapper.XSetWMNormalHints(XToolkit.getDisplay(), getWindow(), hints.pData);
549
} finally {
550
XToolkit.awtUnlock();
551
}
552
}
553
554
public boolean isMinSizeSet() {
555
XSizeHints hints = getHints();
556
long flags = hints.get_flags();
557
return ((flags & XUtilConstants.PMinSize) == XUtilConstants.PMinSize);
558
}
559
560
/**
561
* This lock object can be used to protect instance data from concurrent access
562
* by two threads. If both state lock and AWT lock are taken, AWT Lock should be taken first.
563
*/
564
Object getStateLock() {
565
return state_lock;
566
}
567
568
public long getWindow() {
569
return window;
570
}
571
public long getContentWindow() {
572
return window;
573
}
574
575
public XBaseWindow getContentXWindow() {
576
return XToolkit.windowToXWindow(getContentWindow());
577
}
578
579
public Rectangle getBounds() {
580
return new Rectangle(x, y, width, height);
581
}
582
public Dimension getSize() {
583
return new Dimension(width, height);
584
}
585
586
587
public void toFront() {
588
XToolkit.awtLock();
589
try {
590
XlibWrapper.XRaiseWindow(XToolkit.getDisplay(), getWindow());
591
} finally {
592
XToolkit.awtUnlock();
593
}
594
}
595
public void xRequestFocus(long time) {
596
XToolkit.awtLock();
597
try {
598
if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
599
focusLog.finer("XSetInputFocus on " + Long.toHexString(getWindow()) + " with time " + time);
600
}
601
XlibWrapper.XSetInputFocus2(XToolkit.getDisplay(), getWindow(), time);
602
} finally {
603
XToolkit.awtUnlock();
604
}
605
}
606
public void xRequestFocus() {
607
XToolkit.awtLock();
608
try {
609
if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
610
focusLog.finer("XSetInputFocus on " + Long.toHexString(getWindow()));
611
}
612
XlibWrapper.XSetInputFocus(XToolkit.getDisplay(), getWindow());
613
} finally {
614
XToolkit.awtUnlock();
615
}
616
}
617
618
public static long xGetInputFocus() {
619
XToolkit.awtLock();
620
try {
621
return XlibWrapper.XGetInputFocus(XToolkit.getDisplay());
622
} finally {
623
XToolkit.awtUnlock();
624
}
625
}
626
627
public void xSetVisible(boolean visible) {
628
if (log.isLoggable(PlatformLogger.Level.FINE)) {
629
log.fine("Setting visible on " + this + " to " + visible);
630
}
631
XToolkit.awtLock();
632
try {
633
this.visible = visible;
634
if (visible) {
635
XlibWrapper.XMapWindow(XToolkit.getDisplay(), getWindow());
636
}
637
else {
638
XlibWrapper.XUnmapWindow(XToolkit.getDisplay(), getWindow());
639
}
640
XlibWrapper.XFlush(XToolkit.getDisplay());
641
} finally {
642
XToolkit.awtUnlock();
643
}
644
}
645
646
boolean isMapped() {
647
return mapped;
648
}
649
650
void updateWMName() {
651
String name = getWMName();
652
XToolkit.awtLock();
653
try {
654
if (name == null) {
655
name = " ";
656
}
657
XAtom nameAtom = XAtom.get(XAtom.XA_WM_NAME);
658
nameAtom.setProperty(getWindow(), name);
659
XAtom netNameAtom = XAtom.get("_NET_WM_NAME");
660
netNameAtom.setPropertyUTF8(getWindow(), name);
661
} finally {
662
XToolkit.awtUnlock();
663
}
664
}
665
void setWMClass(String[] cl) {
666
if (cl.length != 2) {
667
throw new IllegalArgumentException("WM_CLASS_NAME consists of exactly two strings");
668
}
669
XToolkit.awtLock();
670
try {
671
XAtom xa = XAtom.get(XAtom.XA_WM_CLASS);
672
xa.setProperty8(getWindow(), cl[0] + '\0' + cl[1] + '\0');
673
} finally {
674
XToolkit.awtUnlock();
675
}
676
}
677
678
boolean isVisible() {
679
return visible;
680
}
681
682
static long getScreenOfWindow(long window) {
683
XToolkit.awtLock();
684
try {
685
return XlibWrapper.getScreenOfWindow(XToolkit.getDisplay(), window);
686
} finally {
687
XToolkit.awtUnlock();
688
}
689
}
690
long getScreenNumber() {
691
XToolkit.awtLock();
692
try {
693
return XlibWrapper.XScreenNumberOfScreen(getScreen());
694
} finally {
695
XToolkit.awtUnlock();
696
}
697
}
698
699
long getScreen() {
700
if (screen == -1) { // Not initialized
701
screen = getScreenOfWindow(window);
702
}
703
return screen;
704
}
705
706
public void xSetBounds(Rectangle bounds) {
707
xSetBounds(bounds.x, bounds.y, bounds.width, bounds.height);
708
}
709
710
public void xSetBounds(int x, int y, int width, int height) {
711
if (getWindow() == 0) {
712
insLog.warning("Attempt to resize uncreated window");
713
throw new IllegalStateException("Attempt to resize uncreated window");
714
}
715
if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
716
insLog.fine("Setting bounds on " + this + " to (" + x + ", " + y + "), " + width + "x" + height);
717
}
718
width = Math.max(MIN_SIZE, width);
719
height = Math.max(MIN_SIZE, height);
720
XToolkit.awtLock();
721
try {
722
XlibWrapper.XMoveResizeWindow(XToolkit.getDisplay(), getWindow(), x,y,width,height);
723
} finally {
724
XToolkit.awtUnlock();
725
}
726
}
727
728
/**
729
* Translate coordinates from one window into another. Optimized
730
* for XAWT - uses cached data when possible. Preferable over
731
* pure XTranslateCoordinates.
732
* @return coordinates relative to dst, or null if error happened
733
*/
734
static Point toOtherWindow(long src, long dst, int x, int y) {
735
Point rpt = new Point(0, 0);
736
737
// Check if both windows belong to XAWT - then no X calls are necessary
738
739
XBaseWindow srcPeer = XToolkit.windowToXWindow(src);
740
XBaseWindow dstPeer = XToolkit.windowToXWindow(dst);
741
742
if (srcPeer != null && dstPeer != null) {
743
// (x, y) is relative to src
744
rpt.x = x + srcPeer.getAbsoluteX() - dstPeer.getAbsoluteX();
745
rpt.y = y + srcPeer.getAbsoluteY() - dstPeer.getAbsoluteY();
746
} else if (dstPeer != null && XlibUtil.isRoot(src, dstPeer.getScreenNumber())) {
747
// from root into peer
748
rpt.x = x - dstPeer.getAbsoluteX();
749
rpt.y = y - dstPeer.getAbsoluteY();
750
} else if (srcPeer != null && XlibUtil.isRoot(dst, srcPeer.getScreenNumber())) {
751
// from peer into root
752
rpt.x = x + srcPeer.getAbsoluteX();
753
rpt.y = y + srcPeer.getAbsoluteY();
754
} else {
755
rpt = XlibUtil.translateCoordinates(src, dst, new Point(x, y));
756
}
757
return rpt;
758
}
759
760
/*
761
* Convert to global coordinates.
762
*/
763
Rectangle toGlobal(Rectangle rec) {
764
Point p = toGlobal(rec.getLocation());
765
Rectangle newRec = new Rectangle(rec);
766
if (p != null) {
767
newRec.setLocation(p);
768
}
769
return newRec;
770
}
771
772
Point toGlobal(Point pt) {
773
Point p = toGlobal(pt.x, pt.y);
774
if (p != null) {
775
return p;
776
} else {
777
return new Point(pt);
778
}
779
}
780
781
Point toGlobal(int x, int y) {
782
long root;
783
XToolkit.awtLock();
784
try {
785
root = XlibWrapper.RootWindow(XToolkit.getDisplay(),
786
getScreenNumber());
787
} finally {
788
XToolkit.awtUnlock();
789
}
790
Point p = toOtherWindow(getContentWindow(), root, x, y);
791
if (p != null) {
792
return p;
793
} else {
794
return new Point(x, y);
795
}
796
}
797
798
/*
799
* Convert to local coordinates.
800
*/
801
Point toLocal(Point pt) {
802
Point p = toLocal(pt.x, pt.y);
803
if (p != null) {
804
return p;
805
} else {
806
return new Point(pt);
807
}
808
}
809
810
Point toLocal(int x, int y) {
811
long root;
812
XToolkit.awtLock();
813
try {
814
root = XlibWrapper.RootWindow(XToolkit.getDisplay(),
815
getScreenNumber());
816
} finally {
817
XToolkit.awtUnlock();
818
}
819
Point p = toOtherWindow(root, getContentWindow(), x, y);
820
if (p != null) {
821
return p;
822
} else {
823
return new Point(x, y);
824
}
825
}
826
827
/**
828
* We should always grab both keyboard and pointer to control event flow
829
* on popups. This also simplifies synthetic grab implementation.
830
* The active grab overrides activated automatic grab.
831
*/
832
public boolean grabInput() {
833
if (grabLog.isLoggable(PlatformLogger.Level.FINE)) {
834
grabLog.fine("Grab input on {0}", this);
835
}
836
837
XToolkit.awtLock();
838
try {
839
if (XAwtState.getGrabWindow() == this &&
840
XAwtState.isManualGrab())
841
{
842
grabLog.fine(" Already Grabbed");
843
return true;
844
}
845
//6273031: PIT. Choice drop down does not close once it is right clicked to show a popup menu
846
//remember previous window having grab and if it's not null ungrab it.
847
XBaseWindow prevGrabWindow = XAwtState.getGrabWindow();
848
final int eventMask = (int) (XConstants.ButtonPressMask | XConstants.ButtonReleaseMask
849
| XConstants.EnterWindowMask | XConstants.LeaveWindowMask | XConstants.PointerMotionMask
850
| XConstants.ButtonMotionMask);
851
final int ownerEvents = 1;
852
853
854
//6714678: IDE (Netbeans, Eclipse, JDeveloper) Debugger hangs
855
//process on Linux
856
//The user must pass the sun.awt.disablegrab property to disable
857
//taking grabs. This prevents hanging of the GUI when a breakpoint
858
//is hit while a popup window taking the grab is open.
859
if (!XToolkit.getSunAwtDisableGrab()) {
860
int ptrGrab = XlibWrapper.XGrabPointer(XToolkit.getDisplay(),
861
getContentWindow(), ownerEvents, eventMask, XConstants.GrabModeAsync,
862
XConstants.GrabModeAsync, XConstants.None, (XWM.isMotif() ? XToolkit.arrowCursor : XConstants.None),
863
XConstants.CurrentTime);
864
// Check grab results to be consistent with X server grab
865
if (ptrGrab != XConstants.GrabSuccess) {
866
XlibWrapper.XUngrabPointer(XToolkit.getDisplay(), XConstants.CurrentTime);
867
XAwtState.setGrabWindow(null);
868
grabLog.fine(" Grab Failure - mouse");
869
return false;
870
}
871
872
int keyGrab = XlibWrapper.XGrabKeyboard(XToolkit.getDisplay(),
873
getContentWindow(), ownerEvents, XConstants.GrabModeAsync, XConstants.GrabModeAsync,
874
XConstants.CurrentTime);
875
if (keyGrab != XConstants.GrabSuccess) {
876
XlibWrapper.XUngrabPointer(XToolkit.getDisplay(), XConstants.CurrentTime);
877
XlibWrapper.XUngrabKeyboard(XToolkit.getDisplay(), XConstants.CurrentTime);
878
XAwtState.setGrabWindow(null);
879
grabLog.fine(" Grab Failure - keyboard");
880
return false;
881
}
882
}
883
if (prevGrabWindow != null) {
884
prevGrabWindow.ungrabInputImpl();
885
}
886
XAwtState.setGrabWindow(this);
887
grabLog.fine(" Grab - success");
888
return true;
889
} finally {
890
XToolkit.awtUnlock();
891
}
892
}
893
894
static void ungrabInput() {
895
XToolkit.awtLock();
896
try {
897
XBaseWindow grabWindow = XAwtState.getGrabWindow();
898
if (grabLog.isLoggable(PlatformLogger.Level.FINE)) {
899
grabLog.fine("UnGrab input on {0}", grabWindow);
900
}
901
if (grabWindow != null) {
902
grabWindow.ungrabInputImpl();
903
if (!XToolkit.getSunAwtDisableGrab()) {
904
XlibWrapper.XUngrabPointer(XToolkit.getDisplay(), XConstants.CurrentTime);
905
XlibWrapper.XUngrabKeyboard(XToolkit.getDisplay(), XConstants.CurrentTime);
906
}
907
XAwtState.setGrabWindow(null);
908
// we need to call XFlush() here to force ungrab
909
// see 6384219 for details
910
XlibWrapper.XFlush(XToolkit.getDisplay());
911
}
912
} finally {
913
XToolkit.awtUnlock();
914
}
915
}
916
917
// called from ungrabInput, used in popup windows to hide theirselfs in ungrabbing
918
void ungrabInputImpl() {
919
}
920
921
static void checkSecurity() {
922
if (XToolkit.isSecurityWarningEnabled() && XToolkit.isToolkitThread()) {
923
StackTraceElement stack[] = (new Throwable()).getStackTrace();
924
log.warning(stack[1] + ": Security violation: calling user code on toolkit thread");
925
}
926
}
927
928
public Set<Long> getChildren() {
929
synchronized (getStateLock()) {
930
return new HashSet<Long>(children);
931
}
932
}
933
934
// -------------- Event handling ----------------
935
public void handleMapNotifyEvent(XEvent xev) {
936
mapped = true;
937
}
938
public void handleUnmapNotifyEvent(XEvent xev) {
939
mapped = false;
940
}
941
public void handleReparentNotifyEvent(XEvent xev) {
942
if (eventLog.isLoggable(PlatformLogger.Level.FINER)) {
943
XReparentEvent msg = xev.get_xreparent();
944
eventLog.finer(msg.toString());
945
}
946
}
947
public void handlePropertyNotify(XEvent xev) {
948
XPropertyEvent msg = xev.get_xproperty();
949
if (XPropertyCache.isCachingSupported()) {
950
XPropertyCache.clearCache(window, XAtom.get(msg.get_atom()));
951
}
952
if (eventLog.isLoggable(PlatformLogger.Level.FINER)) {
953
eventLog.finer("{0}", msg);
954
}
955
}
956
957
public void handleDestroyNotify(XEvent xev) {
958
XAnyEvent xany = xev.get_xany();
959
if (xany.get_window() == getWindow()) {
960
XToolkit.removeFromWinMap(getWindow(), this);
961
if (XPropertyCache.isCachingSupported()) {
962
XPropertyCache.clearCache(getWindow());
963
}
964
}
965
if (xany.get_window() != getWindow()) {
966
synchronized (getStateLock()) {
967
children.remove(xany.get_window());
968
}
969
}
970
}
971
972
public void handleCreateNotify(XEvent xev) {
973
XAnyEvent xany = xev.get_xany();
974
if (xany.get_window() != getWindow()) {
975
synchronized (getStateLock()) {
976
children.add(xany.get_window());
977
}
978
}
979
}
980
981
public void handleClientMessage(XEvent xev) {
982
if (eventLog.isLoggable(PlatformLogger.Level.FINER)) {
983
XClientMessageEvent msg = xev.get_xclient();
984
eventLog.finer(msg.toString());
985
}
986
}
987
988
public void handleVisibilityEvent(XEvent xev) {
989
}
990
public void handleKeyPress(XEvent xev) {
991
}
992
public void handleKeyRelease(XEvent xev) {
993
}
994
public void handleExposeEvent(XEvent xev) {
995
}
996
/**
997
* Activate automatic grab on first ButtonPress,
998
* deactivate on full mouse release
999
*/
1000
public void handleButtonPressRelease(XEvent xev) {
1001
XButtonEvent xbe = xev.get_xbutton();
1002
/*
1003
* Ignore the buttons above 20 due to the bit limit for
1004
* InputEvent.BUTTON_DOWN_MASK.
1005
* One more bit is reserved for FIRST_HIGH_BIT.
1006
*/
1007
int theButton = xbe.get_button();
1008
if (theButton > SunToolkit.MAX_BUTTONS_SUPPORTED) {
1009
return;
1010
}
1011
int buttonState = 0;
1012
buttonState = xbe.get_state() & XConstants.ALL_BUTTONS_MASK;
1013
1014
boolean isWheel = (theButton == XConstants.MouseWheelUp ||
1015
theButton == XConstants.MouseWheelDown);
1016
1017
// don't give focus if it's just the mouse wheel turning
1018
if (!isWheel) {
1019
switch (xev.get_type()) {
1020
case XConstants.ButtonPress:
1021
if (buttonState == 0) {
1022
XWindowPeer parent = getToplevelXWindow();
1023
// See 6385277, 6981400.
1024
if (parent != null && parent.isFocusableWindow()) {
1025
// A click in a client area drops the actual focused window retaining.
1026
parent.setActualFocusedWindow(null);
1027
parent.requestWindowFocus(xbe.get_time(), true);
1028
}
1029
XAwtState.setAutoGrabWindow(this);
1030
}
1031
break;
1032
case XConstants.ButtonRelease:
1033
if (isFullRelease(buttonState, xbe.get_button())) {
1034
XAwtState.setAutoGrabWindow(null);
1035
}
1036
break;
1037
}
1038
}
1039
}
1040
public void handleMotionNotify(XEvent xev) {
1041
}
1042
public void handleXCrossingEvent(XEvent xev) {
1043
}
1044
public void handleConfigureNotifyEvent(XEvent xev) {
1045
XConfigureEvent xe = xev.get_xconfigure();
1046
if (insLog.isLoggable(PlatformLogger.Level.FINER)) {
1047
insLog.finer("Configure, {0}", xe);
1048
}
1049
x = xe.get_x();
1050
y = xe.get_y();
1051
width = xe.get_width();
1052
height = xe.get_height();
1053
}
1054
/**
1055
* Checks ButtonRelease released all Mouse buttons
1056
*/
1057
static boolean isFullRelease(int buttonState, int button) {
1058
final int buttonsNumber = XToolkit.getNumberOfButtonsForMask();
1059
1060
if (button < 0 || button > buttonsNumber) {
1061
return buttonState == 0;
1062
} else {
1063
return buttonState == XlibUtil.getButtonMask(button);
1064
}
1065
}
1066
1067
static boolean isGrabbedEvent(XEvent ev, XBaseWindow target) {
1068
switch (ev.get_type()) {
1069
case XConstants.ButtonPress:
1070
case XConstants.ButtonRelease:
1071
case XConstants.MotionNotify:
1072
case XConstants.KeyPress:
1073
case XConstants.KeyRelease:
1074
return true;
1075
case XConstants.LeaveNotify:
1076
case XConstants.EnterNotify:
1077
// We shouldn't dispatch this events to the grabbed components (see 6317481)
1078
// But this logic is important if the grabbed component is top-level (see realSync)
1079
return (target instanceof XWindowPeer);
1080
default:
1081
return false;
1082
}
1083
}
1084
/**
1085
* Dispatches event to the grab Window or event source window depending
1086
* on whether the grab is active and on the event type
1087
*/
1088
static void dispatchToWindow(XEvent ev) {
1089
XBaseWindow target = XAwtState.getGrabWindow();
1090
if (target == null || !isGrabbedEvent(ev, target)) {
1091
target = XToolkit.windowToXWindow(ev.get_xany().get_window());
1092
}
1093
if (target != null && target.checkInitialised()) {
1094
target.dispatchEvent(ev);
1095
}
1096
}
1097
1098
public void dispatchEvent(XEvent xev) {
1099
if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
1100
eventLog.finest(xev.toString());
1101
}
1102
int type = xev.get_type();
1103
1104
if (isDisposed()) {
1105
return;
1106
}
1107
1108
switch (type)
1109
{
1110
case XConstants.VisibilityNotify:
1111
handleVisibilityEvent(xev);
1112
break;
1113
case XConstants.ClientMessage:
1114
handleClientMessage(xev);
1115
break;
1116
case XConstants.Expose :
1117
case XConstants.GraphicsExpose :
1118
handleExposeEvent(xev);
1119
break;
1120
case XConstants.ButtonPress:
1121
case XConstants.ButtonRelease:
1122
handleButtonPressRelease(xev);
1123
break;
1124
1125
case XConstants.MotionNotify:
1126
handleMotionNotify(xev);
1127
break;
1128
case XConstants.KeyPress:
1129
handleKeyPress(xev);
1130
break;
1131
case XConstants.KeyRelease:
1132
handleKeyRelease(xev);
1133
break;
1134
case XConstants.EnterNotify:
1135
case XConstants.LeaveNotify:
1136
handleXCrossingEvent(xev);
1137
break;
1138
case XConstants.ConfigureNotify:
1139
handleConfigureNotifyEvent(xev);
1140
break;
1141
case XConstants.MapNotify:
1142
handleMapNotifyEvent(xev);
1143
break;
1144
case XConstants.UnmapNotify:
1145
handleUnmapNotifyEvent(xev);
1146
break;
1147
case XConstants.ReparentNotify:
1148
handleReparentNotifyEvent(xev);
1149
break;
1150
case XConstants.PropertyNotify:
1151
handlePropertyNotify(xev);
1152
break;
1153
case XConstants.DestroyNotify:
1154
handleDestroyNotify(xev);
1155
break;
1156
case XConstants.CreateNotify:
1157
handleCreateNotify(xev);
1158
break;
1159
}
1160
}
1161
protected boolean isEventDisabled(XEvent e) {
1162
return false;
1163
}
1164
1165
int getX() {
1166
return x;
1167
}
1168
1169
int getY() {
1170
return y;
1171
}
1172
1173
int getWidth() {
1174
return width;
1175
}
1176
1177
int getHeight() {
1178
return height;
1179
}
1180
1181
void setDisposed(boolean d) {
1182
disposed = d;
1183
}
1184
1185
boolean isDisposed() {
1186
return disposed;
1187
}
1188
1189
public int getAbsoluteX() {
1190
XBaseWindow pw = getParentWindow();
1191
if (pw != null) {
1192
return pw.getAbsoluteX() + getX();
1193
} else {
1194
// Overridden for top-levels as their (x,y) is Java (x, y), not native location
1195
return getX();
1196
}
1197
}
1198
1199
public int getAbsoluteY() {
1200
XBaseWindow pw = getParentWindow();
1201
if (pw != null) {
1202
return pw.getAbsoluteY() + getY();
1203
} else {
1204
return getY();
1205
}
1206
}
1207
1208
public XBaseWindow getParentWindow() {
1209
return parentWindow;
1210
}
1211
1212
public XWindowPeer getToplevelXWindow() {
1213
XBaseWindow bw = this;
1214
while (bw != null && !(bw instanceof XWindowPeer)) {
1215
bw = bw.getParentWindow();
1216
}
1217
return (XWindowPeer)bw;
1218
}
1219
public String toString() {
1220
return super.toString() + "(" + Long.toString(getWindow(), 16) + ")";
1221
}
1222
1223
/**
1224
* Returns whether the given point is inside of the window. Coordinates are local.
1225
*/
1226
public boolean contains(int x, int y) {
1227
return x >= 0 && y >= 0 && x < getWidth() && y < getHeight();
1228
}
1229
1230
/**
1231
* Returns whether the given point is inside of the window. Coordinates are global.
1232
*/
1233
public boolean containsGlobal(int x, int y) {
1234
return x >= getAbsoluteX() && y >= getAbsoluteY() && x < (getAbsoluteX()+getWidth()) && y < (getAbsoluteY()+getHeight());
1235
}
1236
1237
}
1238
1239