0001: /*
0002: * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved.
0003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0004: *
0005: * This code is free software; you can redistribute it and/or modify it
0006: * under the terms of the GNU General Public License version 2 only, as
0007: * published by the Free Software Foundation. Sun designates this
0008: * particular file as subject to the "Classpath" exception as provided
0009: * by Sun in the LICENSE file that accompanied this code.
0010: *
0011: * This code is distributed in the hope that it will be useful, but WITHOUT
0012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0014: * version 2 for more details (a copy is included in the LICENSE file that
0015: * accompanied this code).
0016: *
0017: * You should have received a copy of the GNU General Public License version
0018: * 2 along with this work; if not, write to the Free Software Foundation,
0019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0020: *
0021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0022: * CA 95054 USA or visit www.sun.com if you need additional information or
0023: * have any questions.
0024: */
0025: package sun.awt.X11;
0026:
0027: import java.awt.*;
0028: import java.awt.event.*;
0029: import java.awt.peer.*;
0030: import java.beans.PropertyChangeListener;
0031: import sun.awt.*;
0032: import java.util.*;
0033: import java.awt.dnd.DragSource;
0034: import java.awt.dnd.DragGestureListener;
0035: import java.awt.dnd.DragGestureEvent;
0036: import java.awt.dnd.DragGestureRecognizer;
0037: import java.awt.dnd.MouseDragGestureRecognizer;
0038: import java.awt.dnd.InvalidDnDOperationException;
0039: import java.awt.dnd.peer.DragSourceContextPeer;
0040: import java.awt.image.*;
0041: import java.security.*;
0042: import java.awt.im.InputMethodHighlight;
0043: import java.awt.im.spi.InputMethodDescriptor;
0044: import java.awt.datatransfer.Clipboard;
0045: import javax.swing.LookAndFeel;
0046: import javax.swing.UIDefaults;
0047: import java.util.logging.*;
0048: import sun.font.FontManager;
0049: import sun.misc.PerformanceLogger;
0050: import sun.print.PrintJob2D;
0051: import java.lang.reflect.*;
0052:
0053: public class XToolkit extends UNIXToolkit implements Runnable,
0054: XConstants {
0055: private static Logger log = Logger
0056: .getLogger("sun.awt.X11.XToolkit");
0057: private static Logger eventLog = Logger
0058: .getLogger("sun.awt.X11.event.XToolkit");
0059: private static final Logger timeoutTaskLog = Logger
0060: .getLogger("sun.awt.X11.timeoutTask.XToolkit");
0061: private static Logger keyEventLog = Logger
0062: .getLogger("sun.awt.X11.kye.XToolkit");
0063: private static final Logger backingStoreLog = Logger
0064: .getLogger("sun.awt.X11.backingStore.XToolkit");
0065:
0066: static final boolean PRIMARY_LOOP = false;
0067: static final boolean SECONDARY_LOOP = true;
0068:
0069: private static String awtAppClassName = null;
0070:
0071: // the system clipboard - CLIPBOARD selection
0072: XClipboard clipboard;
0073: // the system selection - PRIMARY selection
0074: XClipboard selection;
0075:
0076: // Dynamic Layout Resize client code setting
0077: protected static boolean dynamicLayoutSetting = false;
0078:
0079: /**
0080: * True when the x settings have been loaded.
0081: */
0082: private boolean loadedXSettings;
0083:
0084: /**
0085: * XSETTINGS for the default screen.
0086: * <p>
0087: */
0088: private XSettings xs;
0089:
0090: static int arrowCursor;
0091: static TreeMap winMap = new TreeMap();
0092: static HashMap specialPeerMap = new HashMap();
0093: static HashMap winToDispatcher = new HashMap();
0094: private static long _display;
0095: static UIDefaults uidefaults;
0096: static X11GraphicsEnvironment localEnv;
0097: static X11GraphicsDevice device;
0098: static final X11GraphicsConfig config;
0099: static int awt_multiclick_time;
0100: static boolean securityWarningEnabled;
0101:
0102: private static int screenWidth = -1, screenHeight = -1; // Dimensions of default screen
0103: static long awt_defaultFg; // Pixel
0104: private static XMouseInfoPeer xPeer;
0105: private static Method m_removeSourceEvents;
0106:
0107: static {
0108: initSecurityWarning();
0109: if (GraphicsEnvironment.isHeadless()) {
0110: config = null;
0111: } else {
0112: localEnv = (X11GraphicsEnvironment) GraphicsEnvironment
0113: .getLocalGraphicsEnvironment();
0114: device = (X11GraphicsDevice) localEnv
0115: .getDefaultScreenDevice();
0116: config = (X11GraphicsConfig) (device
0117: .getDefaultConfiguration());
0118: if (device != null) {
0119: _display = device.getDisplay();
0120: }
0121: setupModifierMap();
0122: initIDs();
0123: setBackingStoreType();
0124: }
0125: m_removeSourceEvents = SunToolkit.getMethod(EventQueue.class,
0126: "removeSourceEvents", new Class[] { Object.class,
0127: Boolean.TYPE });
0128: }
0129:
0130: // Error handler stuff
0131: static XErrorEvent saved_error;
0132: static long saved_error_handler;
0133: static XErrorHandler curErrorHandler;
0134:
0135: // Should be called under LOCK, before releasing LOCK RESTORE_XERROR_HANDLER should be called
0136: static void WITH_XERROR_HANDLER(XErrorHandler handler) {
0137: saved_error = null;
0138: curErrorHandler = handler;
0139: XSync();
0140: saved_error_handler = XlibWrapper.SetToolkitErrorHandler();
0141: }
0142:
0143: static void XERROR_SAVE(XErrorEvent event) {
0144: saved_error = event;
0145: }
0146:
0147: // Should be called under LOCK
0148: static void RESTORE_XERROR_HANDLER() {
0149: XSync();
0150: XlibWrapper.XSetErrorHandler(saved_error_handler);
0151: curErrorHandler = null;
0152: }
0153:
0154: // Should be called under LOCK
0155: static int SAVED_ERROR_HANDLER(long display, XErrorEvent error) {
0156: return XlibWrapper.CallErrorHandler(saved_error_handler,
0157: display, error.pData);
0158: }
0159:
0160: interface XErrorHandler {
0161: int handleError(long display, XErrorEvent err);
0162: }
0163:
0164: static int GlobalErrorHandler(long display, long event_ptr) {
0165: XErrorEvent event = new XErrorEvent(event_ptr);
0166: try {
0167: if (curErrorHandler != null) {
0168: return curErrorHandler.handleError(display, event);
0169: } else {
0170: return SAVED_ERROR_HANDLER(display, event);
0171: }
0172: } finally {
0173: }
0174: }
0175:
0176: /*
0177: * Instead of validating window id, we simply call XGetWindowProperty,
0178: * but temporary install this function as the error handler to ignore
0179: * BadWindow error.
0180: */
0181: static XErrorHandler IgnoreBadWindowHandler = new XErrorHandler() {
0182: public int handleError(long display, XErrorEvent err) {
0183: XERROR_SAVE(err);
0184: if (err.get_error_code() == BadWindow) {
0185: return 0;
0186: } else {
0187: return SAVED_ERROR_HANDLER(display, err);
0188: }
0189: }
0190: };
0191:
0192: private native static void initIDs();
0193:
0194: native static void waitForEvents(long nextTaskTime);
0195:
0196: static Thread toolkitThread;
0197:
0198: static boolean isToolkitThread() {
0199: return Thread.currentThread() == toolkitThread;
0200: }
0201:
0202: static void initSecurityWarning() {
0203: // Enable warning only for internal builds
0204: String runtime = getSystemProperty("java.runtime.version");
0205: securityWarningEnabled = (runtime != null && runtime
0206: .contains("internal"));
0207: }
0208:
0209: static boolean isSecurityWarningEnabled() {
0210: return securityWarningEnabled;
0211: }
0212:
0213: static native void awt_output_flush();
0214:
0215: static final void awtFUnlock() {
0216: awtUnlock();
0217: awt_output_flush();
0218: }
0219:
0220: public native void nativeLoadSystemColors(int[] systemColors);
0221:
0222: static UIDefaults getUIDefaults() {
0223: if (uidefaults == null) {
0224: initUIDefaults();
0225: }
0226: return uidefaults;
0227: }
0228:
0229: public void loadSystemColors(int[] systemColors) {
0230: nativeLoadSystemColors(systemColors);
0231: MotifColorUtilities.loadSystemColors(systemColors);
0232: }
0233:
0234: static void initUIDefaults() {
0235: try {
0236: // Load Defaults from MotifLookAndFeel
0237:
0238: // This dummy load is necessary to get SystemColor initialized. !!!!!!
0239: Color c = SystemColor.text;
0240:
0241: LookAndFeel lnf = new XAWTLookAndFeel();
0242: uidefaults = lnf.getDefaults();
0243: } catch (Exception e) {
0244: e.printStackTrace();
0245: }
0246: }
0247:
0248: static Object displayLock = new Object();
0249:
0250: public static long getDisplay() {
0251: return _display;
0252: }
0253:
0254: public static long getDefaultRootWindow() {
0255: awtLock();
0256: try {
0257: long res = XlibWrapper.RootWindow(XToolkit.getDisplay(),
0258: XlibWrapper.DefaultScreen(XToolkit.getDisplay()));
0259:
0260: if (res == 0) {
0261: throw new IllegalStateException(
0262: "Root window must not be null");
0263: }
0264: return res;
0265: } finally {
0266: awtUnlock();
0267: }
0268: }
0269:
0270: void init() {
0271: awtLock();
0272: try {
0273: XlibWrapper.XSupportsLocale();
0274: if (XlibWrapper.XSetLocaleModifiers("") == null) {
0275: log
0276: .finer("X locale modifiers are not supported, using default");
0277: }
0278:
0279: AwtScreenData defaultScreen = new AwtScreenData(XToolkit
0280: .getDefaultScreenData());
0281: awt_defaultFg = defaultScreen.get_blackpixel();
0282:
0283: arrowCursor = XlibWrapper.XCreateFontCursor(XToolkit
0284: .getDisplay(), XCursorFontConstants.XC_arrow);
0285: } finally {
0286: awtUnlock();
0287: }
0288:
0289: if (log.isLoggable(Level.FINE)) {
0290: Runtime.getRuntime().addShutdownHook(new Thread() {
0291: public void run() {
0292: dumpPeers();
0293: }
0294: });
0295: }
0296: }
0297:
0298: static String getCorrectXIDString(String val) {
0299: if (val != null) {
0300: return val.replace('.', '-');
0301: } else {
0302: return val;
0303: }
0304: }
0305:
0306: static native String getEnv(String key);
0307:
0308: static String getAWTAppClassName() {
0309: return awtAppClassName;
0310: }
0311:
0312: static final String DATA_TRANSFERER_CLASS_NAME = "sun.awt.X11.XDataTransferer";
0313:
0314: public XToolkit() {
0315: super ();
0316: if (PerformanceLogger.loggingEnabled()) {
0317: PerformanceLogger.setTime("XToolkit construction");
0318: }
0319:
0320: if (!GraphicsEnvironment.isHeadless()) {
0321: String mainClassName = null;
0322:
0323: StackTraceElement trace[] = (new Throwable())
0324: .getStackTrace();
0325: int bottom = trace.length - 1;
0326: if (bottom >= 0) {
0327: mainClassName = trace[bottom].getClassName();
0328: }
0329: if (mainClassName == null || mainClassName.equals("")) {
0330: mainClassName = "AWT";
0331: }
0332: awtAppClassName = getCorrectXIDString(mainClassName);
0333:
0334: init();
0335: XWM.init();
0336: SunToolkit
0337: .setDataTransfererClassName(DATA_TRANSFERER_CLASS_NAME);
0338: toolkitThread = new Thread(this , "AWT-XAWT");
0339: toolkitThread.setPriority(Thread.NORM_PRIORITY + 1);
0340: toolkitThread.setDaemon(true);
0341: ThreadGroup mainTG = (ThreadGroup) AccessController
0342: .doPrivileged(new PrivilegedAction() {
0343: public Object run() {
0344: ThreadGroup currentTG = Thread
0345: .currentThread().getThreadGroup();
0346: ThreadGroup parentTG = currentTG
0347: .getParent();
0348: while (parentTG != null) {
0349: currentTG = parentTG;
0350: parentTG = currentTG.getParent();
0351: }
0352: return currentTG;
0353: }
0354: });
0355: toolkitThread.start();
0356: }
0357: }
0358:
0359: public ButtonPeer createButton(Button target) {
0360: ButtonPeer peer = new XButtonPeer(target);
0361: targetCreatedPeer(target, peer);
0362: return peer;
0363: }
0364:
0365: public FramePeer createFrame(Frame target) {
0366: FramePeer peer = new XFramePeer(target);
0367: targetCreatedPeer(target, peer);
0368: return peer;
0369: }
0370:
0371: static void addToWinMap(long window, XBaseWindow xwin) {
0372: synchronized (winMap) {
0373: winMap.put(Long.valueOf(window), xwin);
0374: }
0375: }
0376:
0377: static void removeFromWinMap(long window, XBaseWindow xwin) {
0378: synchronized (winMap) {
0379: winMap.remove(Long.valueOf(window));
0380: }
0381: }
0382:
0383: static XBaseWindow windowToXWindow(long window) {
0384: synchronized (winMap) {
0385: return (XBaseWindow) winMap.get(Long.valueOf(window));
0386: }
0387: }
0388:
0389: static void addEventDispatcher(long window,
0390: XEventDispatcher dispatcher) {
0391: synchronized (winToDispatcher) {
0392: Long key = Long.valueOf(window);
0393: Collection dispatchers = (Collection) winToDispatcher
0394: .get(key);
0395: if (dispatchers == null) {
0396: dispatchers = new Vector();
0397: winToDispatcher.put(key, dispatchers);
0398: }
0399: dispatchers.add(dispatcher);
0400: }
0401: }
0402:
0403: static void removeEventDispatcher(long window,
0404: XEventDispatcher dispatcher) {
0405: synchronized (winToDispatcher) {
0406: Long key = Long.valueOf(window);
0407: Collection dispatchers = (Collection) winToDispatcher
0408: .get(key);
0409: if (dispatchers != null) {
0410: dispatchers.remove(dispatcher);
0411: }
0412: }
0413: }
0414:
0415: private Point lastCursorPos;
0416:
0417: /**
0418: * Returns whether there is last remembered cursor position. The
0419: * position is remembered from X mouse events on our peers. The
0420: * position is stored in <code>p</code>.
0421: * @return true, if there is remembered last cursor position,
0422: * false otherwise
0423: */
0424: boolean getLastCursorPos(Point p) {
0425: awtLock();
0426: try {
0427: if (lastCursorPos == null) {
0428: return false;
0429: }
0430: p.setLocation(lastCursorPos);
0431: return true;
0432: } finally {
0433: awtUnlock();
0434: }
0435: }
0436:
0437: private void processGlobalMotionEvent(XEvent e) {
0438: // Only our windows guaranteely generate MotionNotify, so we
0439: // should track enter/leave, to catch the moment when to
0440: // switch to XQueryPointer
0441: if (e.get_type() == MotionNotify) {
0442: XMotionEvent ev = e.get_xmotion();
0443: awtLock();
0444: try {
0445: if (lastCursorPos == null) {
0446: lastCursorPos = new Point(ev.get_x_root(), ev
0447: .get_y_root());
0448: } else {
0449: lastCursorPos.setLocation(ev.get_x_root(), ev
0450: .get_y_root());
0451: }
0452: } finally {
0453: awtUnlock();
0454: }
0455: } else if (e.get_type() == LeaveNotify) {
0456: // Leave from our window
0457: awtLock();
0458: try {
0459: lastCursorPos = null;
0460: } finally {
0461: awtUnlock();
0462: }
0463: } else if (e.get_type() == EnterNotify) {
0464: // Entrance into our window
0465: XCrossingEvent ev = e.get_xcrossing();
0466: awtLock();
0467: try {
0468: if (lastCursorPos == null) {
0469: lastCursorPos = new Point(ev.get_x_root(), ev
0470: .get_y_root());
0471: } else {
0472: lastCursorPos.setLocation(ev.get_x_root(), ev
0473: .get_y_root());
0474: }
0475: } finally {
0476: awtUnlock();
0477: }
0478: }
0479: }
0480:
0481: public interface XEventListener {
0482: public void eventProcessed(XEvent e);
0483: }
0484:
0485: private Collection<XEventListener> listeners = new LinkedList<XEventListener>();
0486:
0487: public void addXEventListener(XEventListener listener) {
0488: synchronized (listeners) {
0489: listeners.add(listener);
0490: }
0491: }
0492:
0493: private void notifyListeners(XEvent xev) {
0494: synchronized (listeners) {
0495: if (listeners.size() == 0)
0496: return;
0497:
0498: XEvent copy = xev.clone();
0499: try {
0500: for (XEventListener listener : listeners) {
0501: listener.eventProcessed(copy);
0502: }
0503: } finally {
0504: copy.dispose();
0505: }
0506: }
0507: }
0508:
0509: private void dispatchEvent(XEvent ev) {
0510: final XAnyEvent xany = ev.get_xany();
0511:
0512: if (windowToXWindow(xany.get_window()) != null
0513: && (ev.get_type() == MotionNotify
0514: || ev.get_type() == EnterNotify || ev
0515: .get_type() == LeaveNotify)) {
0516: processGlobalMotionEvent(ev);
0517: }
0518:
0519: XBaseWindow.dispatchToWindow(ev);
0520:
0521: Collection dispatchers = null;
0522: synchronized (winToDispatcher) {
0523: Long key = Long.valueOf(xany.get_window());
0524: dispatchers = (Collection) winToDispatcher.get(key);
0525: if (dispatchers != null) { // Clone it to avoid synchronization during dispatching
0526: dispatchers = new Vector(dispatchers);
0527: }
0528: }
0529: if (dispatchers != null) {
0530: Iterator iter = dispatchers.iterator();
0531: while (iter.hasNext()) {
0532: XEventDispatcher disp = (XEventDispatcher) iter.next();
0533: disp.dispatchEvent(ev);
0534: }
0535: }
0536: notifyListeners(ev);
0537: }
0538:
0539: static void processException(Throwable thr) {
0540: if (log.isLoggable(Level.WARNING)) {
0541: log.log(Level.WARNING, "Exception on Toolkit thread", thr);
0542: }
0543: }
0544:
0545: static native void awt_toolkit_init();
0546:
0547: public void run() {
0548: awt_toolkit_init();
0549: run(PRIMARY_LOOP);
0550: }
0551:
0552: public void run(boolean loop) {
0553: XEvent ev = new XEvent();
0554: while (true) {
0555: awtLock();
0556: try {
0557: if (loop == SECONDARY_LOOP) {
0558: // In the secondary loop we may have already aquired awt_lock
0559: // several times, so waitForEvents() might be unable to release
0560: // the awt_lock and this causes lock up.
0561: // For now, we just avoid waitForEvents in the secondary loop.
0562: if (!XlibWrapper.XNextSecondaryLoopEvent(
0563: getDisplay(), ev.pData)) {
0564: break;
0565: }
0566: } else {
0567: callTimeoutTasks();
0568: // If no events are queued, waitForEvents() causes calls to
0569: // awtUnlock(), awtJNI_ThreadYield, poll, awtLock(),
0570: // so it spends most of its time in poll, without holding the lock.
0571: while ((XlibWrapper.XEventsQueued(getDisplay(),
0572: XlibWrapper.QueuedAfterReading) == 0)
0573: && (XlibWrapper.XEventsQueued(getDisplay(),
0574: XlibWrapper.QueuedAfterFlush) == 0)) {
0575: callTimeoutTasks();
0576: waitForEvents(getNextTaskTime());
0577: }
0578: XlibWrapper.XNextEvent(getDisplay(), ev.pData);
0579: }
0580:
0581: if (ev.get_type() != NoExpose) {
0582: eventNumber++;
0583: }
0584:
0585: if (XDropTargetEventProcessor.processEvent(ev)
0586: || XDragSourceContextPeer.processEvent(ev)) {
0587: continue;
0588: }
0589:
0590: if (eventLog.isLoggable(Level.FINER)) {
0591: eventLog.log(Level.FINER, "{0}", ev);
0592: }
0593:
0594: // Check if input method consumes the event
0595: long w = 0;
0596: if (windowToXWindow(ev.get_xany().get_window()) != null) {
0597: Component owner = XKeyboardFocusManagerPeer
0598: .getCurrentNativeFocusOwner();
0599: if (owner != null) {
0600: XWindow ownerWindow = (XWindow) ComponentAccessor
0601: .getPeer(owner);
0602: if (ownerWindow != null) {
0603: w = ownerWindow.getContentWindow();
0604: }
0605: }
0606: }
0607: if (keyEventLog.isLoggable(Level.FINE)
0608: && (ev.get_type() == KeyPress || ev.get_type() == KeyRelease)) {
0609: keyEventLog.fine("before XFilterEvent:" + ev);
0610: }
0611: if (XlibWrapper.XFilterEvent(ev.getPData(), w)) {
0612: continue;
0613: }
0614: if (keyEventLog.isLoggable(Level.FINE)
0615: && (ev.get_type() == KeyPress || ev.get_type() == KeyRelease)) {
0616: keyEventLog.fine("after XFilterEvent:" + ev); // IS THIS CORRECT?
0617: }
0618:
0619: dispatchEvent(ev);
0620: } catch (ThreadDeath td) {
0621: XBaseWindow.ungrabInput();
0622: return;
0623: } catch (Throwable thr) {
0624: XBaseWindow.ungrabInput();
0625: processException(thr);
0626: } finally {
0627: awtUnlock();
0628: }
0629: }
0630: }
0631:
0632: static int getDefaultScreenWidth() {
0633: if (screenWidth == -1) {
0634: long display = getDisplay();
0635: awtLock();
0636: try {
0637: screenWidth = (int) XlibWrapper.DisplayWidth(display,
0638: XlibWrapper.DefaultScreen(display));
0639: } finally {
0640: awtUnlock();
0641: }
0642: }
0643: return screenWidth;
0644: }
0645:
0646: static int getDefaultScreenHeight() {
0647: if (screenHeight == -1) {
0648: long display = getDisplay();
0649: awtLock();
0650: try {
0651: screenHeight = (int) XlibWrapper.DisplayHeight(display,
0652: XlibWrapper.DefaultScreen(display));
0653: } finally {
0654: awtUnlock();
0655: }
0656: }
0657: return screenHeight;
0658: }
0659:
0660: protected int getScreenWidth() {
0661: return getDefaultScreenWidth();
0662: }
0663:
0664: protected int getScreenHeight() {
0665: return getDefaultScreenHeight();
0666: }
0667:
0668: private static Rectangle getWorkArea(long root) {
0669: XAtom XA_NET_WORKAREA = XAtom.get("_NET_WORKAREA");
0670:
0671: long native_ptr = Native.allocateLongArray(4);
0672: try {
0673: boolean workareaPresent = XA_NET_WORKAREA.getAtomData(root,
0674: XAtom.XA_CARDINAL, native_ptr, 4);
0675: if (workareaPresent) {
0676: int rootX = (int) Native.getLong(native_ptr, 0);
0677: int rootY = (int) Native.getLong(native_ptr, 1);
0678: int rootWidth = (int) Native.getLong(native_ptr, 2);
0679: int rootHeight = (int) Native.getLong(native_ptr, 3);
0680:
0681: return new Rectangle(rootX, rootY, rootWidth,
0682: rootHeight);
0683: }
0684: } finally {
0685: XlibWrapper.unsafe.freeMemory(native_ptr);
0686: }
0687:
0688: return null;
0689: }
0690:
0691: /*
0692: * If we're running in non-Xinerama environment and the current
0693: * window manager supports _NET protocol then the screen insets
0694: * are calculated using _NET_WM_WORKAREA property of the root
0695: * window.
0696: * Otherwise, i. e. if Xinerama is on or _NET_WM_WORKAREA is
0697: * not set, we try to calculate the insets ourselves using
0698: * getScreenInsetsManually method.
0699: */
0700: public Insets getScreenInsets(GraphicsConfiguration gc) {
0701: XNETProtocol netProto = XWM.getWM().getNETProtocol();
0702: if ((netProto == null) || !netProto.active()) {
0703: return super .getScreenInsets(gc);
0704: }
0705:
0706: XToolkit.awtLock();
0707: try {
0708: X11GraphicsConfig x11gc = (X11GraphicsConfig) gc;
0709: X11GraphicsDevice x11gd = (X11GraphicsDevice) x11gc
0710: .getDevice();
0711: long root = XlibUtil.getRootWindow(x11gd.getScreen());
0712: Rectangle rootBounds = XlibUtil.getWindowGeometry(root);
0713:
0714: X11GraphicsEnvironment x11ge = (X11GraphicsEnvironment) GraphicsEnvironment
0715: .getLocalGraphicsEnvironment();
0716: if (!x11ge.runningXinerama()) {
0717: Rectangle workArea = XToolkit.getWorkArea(root);
0718: if (workArea != null) {
0719: return new Insets(workArea.y, workArea.x,
0720: rootBounds.height - workArea.height
0721: - workArea.y, rootBounds.width
0722: - workArea.width - workArea.x);
0723: }
0724: }
0725:
0726: return getScreenInsetsManually(root, rootBounds, gc
0727: .getBounds());
0728: } finally {
0729: XToolkit.awtUnlock();
0730: }
0731: }
0732:
0733: /*
0734: * Manual calculation of screen insets: get all the windows with
0735: * _NET_WM_STRUT/_NET_WM_STRUT_PARTIAL hints and add these
0736: * hints' values to screen insets.
0737: *
0738: * This method should be called under XToolkit.awtLock()
0739: */
0740: private Insets getScreenInsetsManually(long root,
0741: Rectangle rootBounds, Rectangle screenBounds) {
0742: /*
0743: * During the manual calculation of screen insets we iterate
0744: * all the X windows hierarchy starting from root window. This
0745: * constant is the max level inspected in this hierarchy.
0746: * 3 is a heuristic value: I suppose any the toolbar-like
0747: * window is a child of either root or desktop window.
0748: */
0749: final int MAX_NESTED_LEVEL = 3;
0750:
0751: XAtom XA_NET_WM_STRUT = XAtom.get("_NET_WM_STRUT");
0752: XAtom XA_NET_WM_STRUT_PARTIAL = XAtom
0753: .get("_NET_WM_STRUT_PARTIAL");
0754:
0755: Insets insets = new Insets(0, 0, 0, 0);
0756:
0757: java.util.List search = new LinkedList();
0758: search.add(root);
0759: search.add(0);
0760: while (!search.isEmpty()) {
0761: long window = (Long) search.remove(0);
0762: int windowLevel = (Integer) search.remove(0);
0763:
0764: /*
0765: * Note that most of the modern window managers unmap
0766: * application window if it is iconified. Thus, any
0767: * _NET_WM_STRUT[_PARTIAL] hints for iconified windows
0768: * are not included to the screen insets.
0769: */
0770: if (XlibUtil.getWindowMapState(window) == XlibWrapper.IsUnmapped) {
0771: continue;
0772: }
0773:
0774: long native_ptr = Native.allocateLongArray(4);
0775: try {
0776: // first, check if _NET_WM_STRUT or _NET_WM_STRUT_PARTIAL are present
0777: // if both are set on the window, _NET_WM_STRUT_PARTIAL is used (see _NET spec)
0778: boolean strutPresent = XA_NET_WM_STRUT_PARTIAL
0779: .getAtomData(window, XAtom.XA_CARDINAL,
0780: native_ptr, 4);
0781: if (!strutPresent) {
0782: strutPresent = XA_NET_WM_STRUT.getAtomData(window,
0783: XAtom.XA_CARDINAL, native_ptr, 4);
0784: }
0785: if (strutPresent) {
0786: // second, verify that window is located on the proper screen
0787: Rectangle windowBounds = XlibUtil
0788: .getWindowGeometry(window);
0789: if (windowLevel > 1) {
0790: windowBounds = XlibUtil.translateCoordinates(
0791: window, root, windowBounds);
0792: }
0793: // if _NET_WM_STRUT_PARTIAL is present, we should use its values to detect
0794: // if the struts area intersects with screenBounds, however some window
0795: // managers don't set this hint correctly, so we just get intersection with windowBounds
0796: if (windowBounds.intersects(screenBounds)) {
0797: insets.left = Math.max((int) Native.getLong(
0798: native_ptr, 0), insets.left);
0799: insets.right = Math.max((int) Native.getLong(
0800: native_ptr, 1), insets.right);
0801: insets.top = Math.max((int) Native.getLong(
0802: native_ptr, 2), insets.top);
0803: insets.bottom = Math.max((int) Native.getLong(
0804: native_ptr, 3), insets.bottom);
0805: }
0806: }
0807: } finally {
0808: XlibWrapper.unsafe.freeMemory(native_ptr);
0809: }
0810:
0811: if (windowLevel < MAX_NESTED_LEVEL) {
0812: Set<Long> children = XlibUtil.getChildWindows(window);
0813: for (long child : children) {
0814: search.add(child);
0815: search.add(windowLevel + 1);
0816: }
0817: }
0818: }
0819:
0820: return insets;
0821: }
0822:
0823: /*
0824: * The current implementation of disabling background erasing for
0825: * canvases is that we don't set any native background color
0826: * (with XSetWindowBackground) for the canvas window. However,
0827: * this color is set in the peer constructor - see
0828: * XWindow.postInit() for details. That's why this method from
0829: * SunToolkit is not overridden in XToolkit: it's too late to
0830: * disable background erasing :(
0831: */
0832: /*
0833: @Override
0834: public void disableBackgroundErase(Canvas canvas) {
0835: XCanvasPeer peer = (XCanvasPeer)canvas.getPeer();
0836: if (peer == null) {
0837: throw new IllegalStateException("Canvas must have a valid peer");
0838: }
0839: peer.disableBackgroundErase();
0840: }
0841: */
0842:
0843: // Need this for XMenuItemPeer.
0844: protected static final Object targetToPeer(Object target) {
0845: Object p = null;
0846: if (target != null && !GraphicsEnvironment.isHeadless()) {
0847: p = specialPeerMap.get(target);
0848: }
0849: if (p != null)
0850: return p;
0851: else
0852: return SunToolkit.targetToPeer(target);
0853: }
0854:
0855: // Need this for XMenuItemPeer.
0856: protected static final void targetDisposedPeer(Object target,
0857: Object peer) {
0858: SunToolkit.targetDisposedPeer(target, peer);
0859: }
0860:
0861: public RobotPeer createRobot(Robot target, GraphicsDevice screen) {
0862: return new XRobotPeer(screen.getDefaultConfiguration());
0863: }
0864:
0865: /*
0866: * On X, support for dynamic layout on resizing is governed by the
0867: * window manager. If the window manager supports it, it happens
0868: * automatically. The setter method for this property is
0869: * irrelevant on X.
0870: */
0871: public void setDynamicLayout(boolean b) {
0872: dynamicLayoutSetting = b;
0873: }
0874:
0875: protected boolean isDynamicLayoutSet() {
0876: return dynamicLayoutSetting;
0877: }
0878:
0879: /* Called from isDynamicLayoutActive() and from
0880: * lazilyLoadDynamicLayoutSupportedProperty()
0881: */
0882: protected boolean isDynamicLayoutSupported() {
0883: return XWM.getWM().supportsDynamicLayout();
0884: }
0885:
0886: public boolean isDynamicLayoutActive() {
0887: return isDynamicLayoutSupported();
0888: }
0889:
0890: public FontPeer getFontPeer(String name, int style) {
0891: return new XFontPeer(name, style);
0892: }
0893:
0894: public DragSourceContextPeer createDragSourceContextPeer(
0895: DragGestureEvent dge) throws InvalidDnDOperationException {
0896: return XDragSourceContextPeer.createDragSourceContextPeer(dge);
0897: }
0898:
0899: public <T extends DragGestureRecognizer> T createDragGestureRecognizer(
0900: Class<T> recognizerClass, DragSource ds, Component c,
0901: int srcActions, DragGestureListener dgl) {
0902: if (MouseDragGestureRecognizer.class.equals(recognizerClass))
0903: return (T) new XMouseDragGestureRecognizer(ds, c,
0904: srcActions, dgl);
0905: else
0906: return null;
0907: }
0908:
0909: public CheckboxMenuItemPeer createCheckboxMenuItem(
0910: CheckboxMenuItem target) {
0911: XCheckboxMenuItemPeer peer = new XCheckboxMenuItemPeer(target);
0912: //vb157120: looks like we don't need to map menu items
0913: //in new menus implementation
0914: //targetCreatedPeer(target, peer);
0915: return peer;
0916: }
0917:
0918: public MenuItemPeer createMenuItem(MenuItem target) {
0919: XMenuItemPeer peer = new XMenuItemPeer(target);
0920: //vb157120: looks like we don't need to map menu items
0921: //in new menus implementation
0922: //targetCreatedPeer(target, peer);
0923: return peer;
0924: }
0925:
0926: public TextFieldPeer createTextField(TextField target) {
0927: TextFieldPeer peer = new XTextFieldPeer(target);
0928: targetCreatedPeer(target, peer);
0929: return peer;
0930: }
0931:
0932: public LabelPeer createLabel(Label target) {
0933: LabelPeer peer = new XLabelPeer(target);
0934: targetCreatedPeer(target, peer);
0935: return peer;
0936: }
0937:
0938: public ListPeer createList(java.awt.List target) {
0939: ListPeer peer = new XListPeer(target);
0940: targetCreatedPeer(target, peer);
0941: return peer;
0942: }
0943:
0944: public CheckboxPeer createCheckbox(Checkbox target) {
0945: CheckboxPeer peer = new XCheckboxPeer(target);
0946: targetCreatedPeer(target, peer);
0947: return peer;
0948: }
0949:
0950: public ScrollbarPeer createScrollbar(Scrollbar target) {
0951: XScrollbarPeer peer = new XScrollbarPeer(target);
0952: targetCreatedPeer(target, peer);
0953: return peer;
0954: }
0955:
0956: public ScrollPanePeer createScrollPane(ScrollPane target) {
0957: XScrollPanePeer peer = new XScrollPanePeer(target);
0958: targetCreatedPeer(target, peer);
0959: return peer;
0960: }
0961:
0962: public TextAreaPeer createTextArea(TextArea target) {
0963: TextAreaPeer peer = new XTextAreaPeer(target);
0964: targetCreatedPeer(target, peer);
0965: return peer;
0966: }
0967:
0968: public ChoicePeer createChoice(Choice target) {
0969: XChoicePeer peer = new XChoicePeer(target);
0970: targetCreatedPeer(target, peer);
0971: return peer;
0972: }
0973:
0974: public CanvasPeer createCanvas(Canvas target) {
0975: XCanvasPeer peer = (isXEmbedServerRequested() ? new XEmbedCanvasPeer(
0976: target)
0977: : new XCanvasPeer(target));
0978: targetCreatedPeer(target, peer);
0979: return peer;
0980: }
0981:
0982: public PanelPeer createPanel(Panel target) {
0983: PanelPeer peer = new XPanelPeer(target);
0984: targetCreatedPeer(target, peer);
0985: return peer;
0986: }
0987:
0988: public WindowPeer createWindow(Window target) {
0989: WindowPeer peer = new XWindowPeer(target);
0990: targetCreatedPeer(target, peer);
0991: return peer;
0992: }
0993:
0994: public DialogPeer createDialog(Dialog target) {
0995: DialogPeer peer = new XDialogPeer(target);
0996: targetCreatedPeer(target, peer);
0997: return peer;
0998: }
0999:
1000: public FileDialogPeer createFileDialog(FileDialog target) {
1001: FileDialogPeer peer = new XFileDialogPeer(target);
1002: targetCreatedPeer(target, peer);
1003: return peer;
1004: }
1005:
1006: public MenuBarPeer createMenuBar(MenuBar target) {
1007: XMenuBarPeer peer = new XMenuBarPeer(target);
1008: targetCreatedPeer(target, peer);
1009: return peer;
1010: }
1011:
1012: public MenuPeer createMenu(Menu target) {
1013: XMenuPeer peer = new XMenuPeer(target);
1014: //vb157120: looks like we don't need to map menu items
1015: //in new menus implementation
1016: //targetCreatedPeer(target, peer);
1017: return peer;
1018: }
1019:
1020: public PopupMenuPeer createPopupMenu(PopupMenu target) {
1021: XPopupMenuPeer peer = new XPopupMenuPeer(target);
1022: targetCreatedPeer(target, peer);
1023: return peer;
1024: }
1025:
1026: public synchronized MouseInfoPeer getMouseInfoPeer() {
1027: if (xPeer == null) {
1028: xPeer = new XMouseInfoPeer();
1029: }
1030: return xPeer;
1031: }
1032:
1033: public XEmbeddedFramePeer createEmbeddedFrame(XEmbeddedFrame target) {
1034: XEmbeddedFramePeer peer = new XEmbeddedFramePeer(target);
1035: targetCreatedPeer(target, peer);
1036: return peer;
1037: }
1038:
1039: XEmbedChildProxyPeer createEmbedProxy(XEmbedChildProxy target) {
1040: XEmbedChildProxyPeer peer = new XEmbedChildProxyPeer(target);
1041: targetCreatedPeer(target, peer);
1042: return peer;
1043: }
1044:
1045: public KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(
1046: KeyboardFocusManager manager) throws HeadlessException {
1047: XKeyboardFocusManagerPeer peer = new XKeyboardFocusManagerPeer(
1048: manager);
1049: return peer;
1050: }
1051:
1052: /**
1053: * Returns a new custom cursor.
1054: */
1055: public Cursor createCustomCursor(Image cursor, Point hotSpot,
1056: String name) throws IndexOutOfBoundsException {
1057: return new XCustomCursor(cursor, hotSpot, name);
1058: }
1059:
1060: public TrayIconPeer createTrayIcon(TrayIcon target)
1061: throws HeadlessException, AWTException {
1062: TrayIconPeer peer = new XTrayIconPeer(target);
1063: targetCreatedPeer(target, peer);
1064: return peer;
1065: }
1066:
1067: public SystemTrayPeer createSystemTray(SystemTray target)
1068: throws HeadlessException {
1069: SystemTrayPeer peer = new XSystemTrayPeer(target);
1070: return peer;
1071: }
1072:
1073: public boolean isTraySupported() {
1074: int wm = XWM.getWMID();
1075: if (wm == XWM.METACITY_WM || wm == XWM.KDE2_WM) {
1076: return true;
1077: }
1078: return false;
1079: }
1080:
1081: /**
1082: * Returns the supported cursor size
1083: */
1084: public Dimension getBestCursorSize(int preferredWidth,
1085: int preferredHeight) {
1086: return XCustomCursor
1087: .getBestCursorSize(java.lang.Math
1088: .max(1, preferredWidth), java.lang.Math.max(1,
1089: preferredHeight));
1090: }
1091:
1092: public int getMaximumCursorColors() {
1093: return 2; // Black and white.
1094: }
1095:
1096: public Map mapInputMethodHighlight(InputMethodHighlight highlight) {
1097: return XInputMethod.mapInputMethodHighlight(highlight);
1098: }
1099:
1100: public Clipboard getSystemClipboard() {
1101: SecurityManager security = System.getSecurityManager();
1102: if (security != null) {
1103: security.checkSystemClipboardAccess();
1104: }
1105: synchronized (this ) {
1106: if (clipboard == null) {
1107: clipboard = new XClipboard("System", "CLIPBOARD");
1108: }
1109: }
1110: return clipboard;
1111: }
1112:
1113: public Clipboard getSystemSelection() {
1114: SecurityManager security = System.getSecurityManager();
1115: if (security != null) {
1116: security.checkSystemClipboardAccess();
1117: }
1118: synchronized (this ) {
1119: if (selection == null) {
1120: selection = new XClipboard("Selection", "PRIMARY");
1121: }
1122: }
1123: return selection;
1124: }
1125:
1126: public void beep() {
1127: awtLock();
1128: try {
1129: XlibWrapper.XBell(getDisplay(), 0);
1130: } finally {
1131: awtUnlock();
1132: }
1133: }
1134:
1135: static String getSystemProperty(final String name) {
1136: return (String) AccessController
1137: .doPrivileged(new PrivilegedAction() {
1138: public Object run() {
1139: return System.getProperty(name);
1140: }
1141: });
1142: }
1143:
1144: public PrintJob getPrintJob(final Frame frame,
1145: final String doctitle, final Properties props) {
1146:
1147: if (GraphicsEnvironment.isHeadless()) {
1148: throw new IllegalArgumentException();
1149: }
1150:
1151: PrintJob2D printJob = new PrintJob2D(frame, doctitle, props);
1152:
1153: if (printJob.printDialog() == false) {
1154: printJob = null;
1155: }
1156: return printJob;
1157: }
1158:
1159: public PrintJob getPrintJob(final Frame frame,
1160: final String doctitle, final JobAttributes jobAttributes,
1161: final PageAttributes pageAttributes) {
1162:
1163: if (GraphicsEnvironment.isHeadless()) {
1164: throw new IllegalArgumentException();
1165: }
1166:
1167: PrintJob2D printJob = new PrintJob2D(frame, doctitle,
1168: jobAttributes, pageAttributes);
1169:
1170: if (printJob.printDialog() == false) {
1171: printJob = null;
1172: }
1173:
1174: return printJob;
1175: }
1176:
1177: static void XSync() {
1178: awtLock();
1179: try {
1180: XlibWrapper.XSync(getDisplay(), 0);
1181: } finally {
1182: awtUnlock();
1183: }
1184: }
1185:
1186: public int getScreenResolution() {
1187: long display = getDisplay();
1188: awtLock();
1189: try {
1190: return (int) ((XlibWrapper.DisplayWidth(display,
1191: XlibWrapper.DefaultScreen(display)) * 25.4) / XlibWrapper
1192: .DisplayWidthMM(display, XlibWrapper
1193: .DefaultScreen(display)));
1194: } finally {
1195: awtUnlock();
1196: }
1197: }
1198:
1199: static native long getDefaultXColormap();
1200:
1201: static native long getDefaultScreenData();
1202:
1203: static ColorModel screenmodel;
1204:
1205: static ColorModel getStaticColorModel() {
1206: if (screenmodel == null) {
1207: screenmodel = config.getColorModel();
1208: }
1209: return screenmodel;
1210: }
1211:
1212: public ColorModel getColorModel() {
1213: return getStaticColorModel();
1214: }
1215:
1216: /**
1217: * Returns a new input method adapter descriptor for native input methods.
1218: */
1219: public InputMethodDescriptor getInputMethodAdapterDescriptor()
1220: throws AWTException {
1221: return new XInputMethodDescriptor();
1222: }
1223:
1224: static int getMultiClickTime() {
1225: if (awt_multiclick_time == 0) {
1226: initializeMultiClickTime();
1227: }
1228: return awt_multiclick_time;
1229: }
1230:
1231: static void initializeMultiClickTime() {
1232: awtLock();
1233: try {
1234: try {
1235: String multiclick_time_query = XlibWrapper.XGetDefault(
1236: XToolkit.getDisplay(), "*", "multiClickTime");
1237: if (multiclick_time_query != null) {
1238: awt_multiclick_time = (int) Long
1239: .parseLong(multiclick_time_query);
1240: // awt_multiclick_time = XtGetMultiClickTime(awt_display);
1241: } else {
1242: multiclick_time_query = XlibWrapper.XGetDefault(
1243: XToolkit.getDisplay(), "OpenWindows",
1244: "MultiClickTimeout");
1245: if (multiclick_time_query != null) {
1246: /* Note: OpenWindows.MultiClickTimeout is in tenths of
1247: a second, so we need to multiply by 100 to convert to
1248: milliseconds */
1249: awt_multiclick_time = (int) Long
1250: .parseLong(multiclick_time_query) * 100;
1251: } else {
1252: awt_multiclick_time = 200;
1253: // awt_multiclick_time = XtGetMultiClickTime(awt_display);
1254: }
1255: }
1256: } catch (NumberFormatException nf) {
1257: awt_multiclick_time = 200;
1258: } catch (NullPointerException npe) {
1259: awt_multiclick_time = 200;
1260: }
1261: } finally {
1262: awtUnlock();
1263: }
1264: if (awt_multiclick_time == 0) {
1265: awt_multiclick_time = 200;
1266: }
1267: }
1268:
1269: public boolean isFrameStateSupported(int state)
1270: throws HeadlessException {
1271: if (state == Frame.NORMAL || state == Frame.ICONIFIED) {
1272: return true;
1273: } else {
1274: return XWM.getWM().supportsExtendedState(state);
1275: }
1276: }
1277:
1278: static void dumpPeers() {
1279: if (log.isLoggable(Level.FINE)) {
1280: log.fine("Mapped windows:");
1281: Iterator iter = winMap.entrySet().iterator();
1282: while (iter.hasNext()) {
1283: Map.Entry entry = (Map.Entry) iter.next();
1284: log.fine(entry.getKey() + "->" + entry.getValue());
1285: if (entry.getValue() instanceof XComponentPeer) {
1286: Component target = (Component) ((XComponentPeer) entry
1287: .getValue()).getTarget();
1288: log.fine("\ttarget: " + target);
1289: }
1290: }
1291:
1292: SunToolkit.dumpPeers(log);
1293:
1294: log.fine("Mapped special peers:");
1295: iter = specialPeerMap.entrySet().iterator();
1296: while (iter.hasNext()) {
1297: Map.Entry entry = (Map.Entry) iter.next();
1298: log.fine(entry.getKey() + "->" + entry.getValue());
1299: }
1300:
1301: log.fine("Mapped dispatchers:");
1302: iter = winToDispatcher.entrySet().iterator();
1303: while (iter.hasNext()) {
1304: Map.Entry entry = (Map.Entry) iter.next();
1305: log.fine(entry.getKey() + "->" + entry.getValue());
1306: }
1307: }
1308: }
1309:
1310: /* Protected with awt_lock. */
1311: private static boolean initialized;
1312: private static boolean timeStampUpdated;
1313: private static long timeStamp;
1314:
1315: private static final XEventDispatcher timeFetcher = new XEventDispatcher() {
1316: public void dispatchEvent(XEvent ev) {
1317: switch (ev.get_type()) {
1318: case PropertyNotify:
1319: XPropertyEvent xpe = ev.get_xproperty();
1320:
1321: awtLock();
1322: try {
1323: timeStamp = xpe.get_time();
1324: timeStampUpdated = true;
1325: awtLockNotifyAll();
1326: } finally {
1327: awtUnlock();
1328: }
1329:
1330: break;
1331: }
1332: }
1333: };
1334:
1335: private static XAtom _XA_JAVA_TIME_PROPERTY_ATOM;
1336:
1337: static long getCurrentServerTime() {
1338: awtLock();
1339: try {
1340: try {
1341: if (!initialized) {
1342: XToolkit.addEventDispatcher(XBaseWindow
1343: .getXAWTRootWindow().getWindow(),
1344: timeFetcher);
1345: _XA_JAVA_TIME_PROPERTY_ATOM = XAtom
1346: .get("_SUNW_JAVA_AWT_TIME");
1347: initialized = true;
1348: }
1349: timeStampUpdated = false;
1350: XlibWrapper.XChangeProperty(XToolkit.getDisplay(),
1351: XBaseWindow.getXAWTRootWindow().getWindow(),
1352: _XA_JAVA_TIME_PROPERTY_ATOM.getAtom(),
1353: XAtom.XA_ATOM, 32, PropModeAppend, 0, 0);
1354: XlibWrapper.XFlush(XToolkit.getDisplay());
1355:
1356: if (isToolkitThread()) {
1357: XEvent event = new XEvent();
1358: try {
1359: XlibWrapper.XWindowEvent(XToolkit.getDisplay(),
1360: XBaseWindow.getXAWTRootWindow()
1361: .getWindow(),
1362: XConstants.PropertyChangeMask,
1363: event.pData);
1364: timeFetcher.dispatchEvent(event);
1365: } finally {
1366: event.dispose();
1367: }
1368: } else {
1369: while (!timeStampUpdated) {
1370: awtLockWait();
1371: }
1372: }
1373: } catch (InterruptedException ie) {
1374: // Note: the returned timeStamp can be incorrect in this case.
1375: if (log.isLoggable(Level.FINE))
1376: log
1377: .fine("Catched exception, timeStamp may not be correct (ie = "
1378: + ie + ")");
1379: }
1380: } finally {
1381: awtUnlock();
1382: }
1383: return timeStamp;
1384: }
1385:
1386: protected void initializeDesktopProperties() {
1387: desktopProperties.put("DnD.Autoscroll.initialDelay", Integer
1388: .valueOf(50));
1389: desktopProperties.put("DnD.Autoscroll.interval", Integer
1390: .valueOf(50));
1391: desktopProperties.put("DnD.Autoscroll.cursorHysteresis",
1392: Integer.valueOf(5));
1393: // Don't want to call getMultiClickTime() if we are headless
1394: if (!GraphicsEnvironment.isHeadless()) {
1395: desktopProperties.put("awt.multiClickInterval", Integer
1396: .valueOf(getMultiClickTime()));
1397: desktopProperties.put("awt.mouse.numButtons", Integer
1398: .valueOf(getNumMouseButtons()));
1399: }
1400: }
1401:
1402: private int getNumMouseButtons() {
1403: awtLock();
1404: try {
1405: return XlibWrapper.XGetPointerMapping(
1406: XToolkit.getDisplay(), 0, 0);
1407: } finally {
1408: awtUnlock();
1409: }
1410: }
1411:
1412: private final static String prefix = "DnD.Cursor.";
1413: private final static String postfix = ".32x32";
1414: private static final String dndPrefix = "DnD.";
1415:
1416: protected Object lazilyLoadDesktopProperty(String name) {
1417: if (name.startsWith(prefix)) {
1418: String cursorName = name.substring(prefix.length(), name
1419: .length())
1420: + postfix;
1421:
1422: try {
1423: return Cursor.getSystemCustomCursor(cursorName);
1424: } catch (AWTException awte) {
1425: throw new RuntimeException(
1426: "cannot load system cursor: " + cursorName,
1427: awte);
1428: }
1429: }
1430:
1431: if (name.equals("awt.dynamicLayoutSupported")) {
1432: return Boolean.valueOf(isDynamicLayoutSupported());
1433: }
1434:
1435: if (initXSettingsIfNeeded(name)) {
1436: return desktopProperties.get(name);
1437: }
1438:
1439: return super .lazilyLoadDesktopProperty(name);
1440: }
1441:
1442: public synchronized void addPropertyChangeListener(String name,
1443: PropertyChangeListener pcl) {
1444: initXSettingsIfNeeded(name);
1445: super .addPropertyChangeListener(name, pcl);
1446: }
1447:
1448: /**
1449: * Initializes XAWTXSettings if a property for a given property name is provided by
1450: * XSettings and they are not initialized yet.
1451: *
1452: * @return true if the method has initialized XAWTXSettings.
1453: */
1454: private boolean initXSettingsIfNeeded(final String propName) {
1455: if (!loadedXSettings
1456: && (propName.startsWith("gnome.")
1457: || propName.equals(SunToolkit.DESKTOPFONTHINTS) || propName
1458: .startsWith(dndPrefix))) {
1459: loadedXSettings = true;
1460: if (!GraphicsEnvironment.isHeadless()) {
1461: loadXSettings();
1462: /* If no desktop font hint could be retrieved, check for
1463: * KDE running KWin and retrieve settings from fontconfig.
1464: * If that isn't found let SunToolkit will see if there's a
1465: * system property set by a user.
1466: */
1467: if (desktopProperties.get(SunToolkit.DESKTOPFONTHINTS) == null) {
1468: if (XWM.isKDE2()) {
1469: Object hint = FontManager.getFontConfigAAHint();
1470: if (hint != null) {
1471: /* set the fontconfig/KDE property so that
1472: * getDesktopHints() below will see it
1473: * and set the public property.
1474: */
1475: desktopProperties.put(
1476: UNIXToolkit.FONTCONFIGAAHINT, hint);
1477: }
1478: }
1479: desktopProperties.put(SunToolkit.DESKTOPFONTHINTS,
1480: SunToolkit.getDesktopFontHints());
1481: }
1482:
1483: return true;
1484: }
1485: }
1486: return false;
1487: }
1488:
1489: private void loadXSettings() {
1490: xs = new XAWTXSettings();
1491: }
1492:
1493: /**
1494: * Callback from the native side indicating some, or all, of the
1495: * desktop properties have changed and need to be reloaded.
1496: * <code>data</code> is the byte array directly from the x server and
1497: * may be in little endian format.
1498: * <p>
1499: * NB: This could be called from any thread if triggered by
1500: * <code>loadXSettings</code>. It is called from the System EDT
1501: * if triggered by an XSETTINGS change.
1502: */
1503: void parseXSettings(int screen_XXX_ignored, Map updatedSettings) {
1504:
1505: if (updatedSettings == null || updatedSettings.isEmpty()) {
1506: return;
1507: }
1508:
1509: Iterator i = updatedSettings.entrySet().iterator();
1510: while (i.hasNext()) {
1511: Map.Entry e = (Map.Entry) i.next();
1512: String name = (String) e.getKey();
1513:
1514: name = "gnome." + name;
1515: setDesktopProperty(name, e.getValue());
1516: log.fine("name = " + name + " value = " + e.getValue());
1517:
1518: // XXX: we probably want to do something smarter. In
1519: // particular, "Net" properties are of interest to the
1520: // "core" AWT itself. E.g.
1521: //
1522: // Net/DndDragThreshold -> ???
1523: // Net/DoubleClickTime -> awt.multiClickInterval
1524: }
1525:
1526: setDesktopProperty(SunToolkit.DESKTOPFONTHINTS, SunToolkit
1527: .getDesktopFontHints());
1528:
1529: Integer dragThreshold = null;
1530: synchronized (this ) {
1531: dragThreshold = (Integer) desktopProperties
1532: .get("gnome.Net/DndDragThreshold");
1533: }
1534: if (dragThreshold != null) {
1535: setDesktopProperty("DnD.gestureMotionThreshold",
1536: dragThreshold);
1537: }
1538:
1539: }
1540:
1541: static int altMask;
1542: static int metaMask;
1543: static int numLockMask;
1544: static int modeSwitchMask;
1545: static int modLockIsShiftLock;
1546:
1547: /* Like XKeysymToKeycode, but ensures that keysym is the primary
1548: * symbol on the keycode returned. Returns zero otherwise.
1549: */
1550: static int keysymToPrimaryKeycode(long sym) {
1551: awtLock();
1552: try {
1553: int code = XlibWrapper.XKeysymToKeycode(getDisplay(), sym);
1554: if (code == 0) {
1555: return 0;
1556: }
1557: long primary = XlibWrapper.XKeycodeToKeysym(getDisplay(),
1558: code, 0);
1559: if (sym != primary) {
1560: return 0;
1561: }
1562: return code;
1563: } finally {
1564: awtUnlock();
1565: }
1566: }
1567:
1568: /* Assign meaning - alt, meta, etc. - to X modifiers mod1 ... mod5.
1569: * Only consider primary symbols on keycodes attached to modifiers.
1570: */
1571: static void setupModifierMap() {
1572: final int metaL = keysymToPrimaryKeycode(XKeySymConstants.XK_Meta_L);
1573: final int metaR = keysymToPrimaryKeycode(XKeySymConstants.XK_Meta_R);
1574: final int altL = keysymToPrimaryKeycode(XKeySymConstants.XK_Alt_L);
1575: final int altR = keysymToPrimaryKeycode(XKeySymConstants.XK_Alt_R);
1576: final int numLock = keysymToPrimaryKeycode(XKeySymConstants.XK_Num_Lock);
1577: final int modeSwitch = keysymToPrimaryKeycode(XKeySymConstants.XK_Mode_switch);
1578: final int shiftLock = keysymToPrimaryKeycode(XKeySymConstants.XK_Shift_Lock);
1579: final int capsLock = keysymToPrimaryKeycode(XKeySymConstants.XK_Caps_Lock);
1580:
1581: final int modmask[] = { ShiftMask, LockMask, ControlMask,
1582: Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask };
1583:
1584: log.fine("In setupModifierMap");
1585: awtLock();
1586: try {
1587: XModifierKeymap modmap = new XModifierKeymap(XlibWrapper
1588: .XGetModifierMapping(getDisplay()));
1589:
1590: int nkeys = modmap.get_max_keypermod();
1591:
1592: long map_ptr = modmap.get_modifiermap();
1593:
1594: for (int modn = XConstants.Mod1MapIndex; modn <= XConstants.Mod5MapIndex; ++modn) {
1595: for (int i = 0; i < nkeys; ++i) {
1596: /* for each keycode attached to this modifier */
1597: int keycode = Native.getUByte(map_ptr, modn * nkeys
1598: + i);
1599:
1600: if (keycode == 0) {
1601: break;
1602: }
1603: if (metaMask == 0
1604: && (keycode == metaL || keycode == metaR)) {
1605: metaMask = modmask[modn];
1606: break;
1607: }
1608: if (altMask == 0
1609: && (keycode == altL || keycode == altR)) {
1610: altMask = modmask[modn];
1611: break;
1612: }
1613: if (numLockMask == 0 && keycode == numLock) {
1614: numLockMask = modmask[modn];
1615: break;
1616: }
1617: if (modeSwitchMask == 0 && keycode == modeSwitch) {
1618: modeSwitchMask = modmask[modn];
1619: break;
1620: }
1621: continue;
1622: }
1623: }
1624: modLockIsShiftLock = 0;
1625: for (int j = 0; j < nkeys; ++j) {
1626: int keycode = Native.getUByte(map_ptr,
1627: XConstants.LockMapIndex * nkeys + j);
1628: if (keycode == 0) {
1629: break;
1630: }
1631: if (keycode == shiftLock) {
1632: modLockIsShiftLock = 1;
1633: break;
1634: }
1635: if (keycode == capsLock) {
1636: break;
1637: }
1638: }
1639: XlibWrapper.XFreeModifiermap(modmap.pData);
1640: } finally {
1641: awtUnlock();
1642: }
1643: if (log.isLoggable(Level.FINE)) {
1644: log.fine("metaMask = " + metaMask);
1645: log.fine("altMask = " + altMask);
1646: log.fine("numLockMask = " + numLockMask);
1647: log.fine("modeSwitchMask = " + modeSwitchMask);
1648: log.fine("modLockIsShiftLock = " + modLockIsShiftLock);
1649: }
1650: }
1651:
1652: private static SortedMap timeoutTasks;
1653:
1654: /**
1655: * Removed the task from the list of waiting-to-be called tasks.
1656: * If the task has been scheduled several times removes only first one.
1657: */
1658: static void remove(Runnable task) {
1659: if (task == null) {
1660: throw new NullPointerException("task is null");
1661: }
1662: awtLock();
1663: try {
1664: if (timeoutTaskLog.isLoggable(Level.FINER)) {
1665: timeoutTaskLog.finer("Removing task " + task);
1666: }
1667: if (timeoutTasks == null) {
1668: if (timeoutTaskLog.isLoggable(Level.FINER)) {
1669: timeoutTaskLog.finer("Task is not scheduled");
1670: }
1671: return;
1672: }
1673: Collection values = timeoutTasks.values();
1674: Iterator iter = values.iterator();
1675: while (iter.hasNext()) {
1676: java.util.List list = (java.util.List) iter.next();
1677: boolean removed = false;
1678: if (list.contains(task)) {
1679: list.remove(task);
1680: if (list.isEmpty()) {
1681: iter.remove();
1682: }
1683: break;
1684: }
1685: }
1686: } finally {
1687: awtUnlock();
1688: }
1689: }
1690:
1691: static native void wakeup_poll();
1692:
1693: /**
1694: * Registers a Runnable which <code>run()</code> method will be called
1695: * once on the toolkit thread when a specified interval of time elapses.
1696: *
1697: * @param task a Runnable which <code>run</code> method will be called
1698: * on the toolkit thread when <code>interval</code> milliseconds
1699: * elapse
1700: * @param interval an interal in milliseconds
1701: *
1702: * @throws NullPointerException if <code>task</code> is <code>null</code>
1703: * @throws IllegalArgumentException if <code>interval</code> is not positive
1704: */
1705: static void schedule(Runnable task, long interval) {
1706: if (task == null) {
1707: throw new NullPointerException("task is null");
1708: }
1709: if (interval <= 0) {
1710: throw new IllegalArgumentException("interval " + interval
1711: + " is not positive");
1712: }
1713:
1714: awtLock();
1715: try {
1716: if (timeoutTaskLog.isLoggable(Level.FINER)) {
1717: timeoutTaskLog.log(Level.FINER,
1718: "XToolkit.schedule(): current time={0}"
1719: + "; interval={1}"
1720: + "; task being added={2}"
1721: + "; tasks before addition={3}",
1722: new Object[] {
1723: Long
1724: .valueOf(System
1725: .currentTimeMillis()),
1726: Long.valueOf(interval), task,
1727: timeoutTasks });
1728: }
1729:
1730: if (timeoutTasks == null) {
1731: timeoutTasks = new TreeMap();
1732: }
1733:
1734: Long time = Long.valueOf(System.currentTimeMillis()
1735: + interval);
1736: java.util.List tasks = (java.util.List) timeoutTasks
1737: .get(time);
1738: if (tasks == null) {
1739: tasks = new ArrayList(1);
1740: timeoutTasks.put(time, tasks);
1741: }
1742: tasks.add(task);
1743:
1744: if (timeoutTasks.get(timeoutTasks.firstKey()) == tasks
1745: && tasks.size() == 1) {
1746: // Added task became first task - poll won't know
1747: // about it so we need to wake it up
1748: wakeup_poll();
1749: }
1750: } finally {
1751: awtUnlock();
1752: }
1753: }
1754:
1755: private long getNextTaskTime() {
1756: awtLock();
1757: try {
1758: if (timeoutTasks == null || timeoutTasks.isEmpty()) {
1759: return -1L;
1760: }
1761: return (Long) timeoutTasks.firstKey();
1762: } finally {
1763: awtUnlock();
1764: }
1765: }
1766:
1767: /**
1768: * Executes mature timeout tasks registered with schedule().
1769: * Called from run() under awtLock.
1770: */
1771: private static void callTimeoutTasks() {
1772: if (timeoutTaskLog.isLoggable(Level.FINER)) {
1773: timeoutTaskLog.log(Level.FINER,
1774: "XToolkit.callTimeoutTasks(): current time={0}"
1775: + "; tasks={1}", new Object[] {
1776: Long.valueOf(System.currentTimeMillis()),
1777: timeoutTasks });
1778: }
1779:
1780: if (timeoutTasks == null || timeoutTasks.isEmpty()) {
1781: return;
1782: }
1783:
1784: Long currentTime = Long.valueOf(System.currentTimeMillis());
1785: Long time = (Long) timeoutTasks.firstKey();
1786:
1787: while (time.compareTo(currentTime) <= 0) {
1788: java.util.List tasks = (java.util.List) timeoutTasks
1789: .remove(time);
1790:
1791: for (Iterator iter = tasks.iterator(); iter.hasNext();) {
1792: Runnable task = (Runnable) iter.next();
1793:
1794: if (timeoutTaskLog.isLoggable(Level.FINER)) {
1795: timeoutTaskLog.log(Level.FINER,
1796: "XToolkit.callTimeoutTasks(): current time={0}"
1797: + "; about to run task={1}",
1798: new Object[] { Long.valueOf(currentTime),
1799: task });
1800: }
1801:
1802: try {
1803: task.run();
1804: } catch (ThreadDeath td) {
1805: throw td;
1806: } catch (Throwable thr) {
1807: processException(thr);
1808: }
1809: }
1810:
1811: if (timeoutTasks.isEmpty()) {
1812: break;
1813: }
1814: time = (Long) timeoutTasks.firstKey();
1815: }
1816: }
1817:
1818: static long getAwtDefaultFg() {
1819: return awt_defaultFg;
1820: }
1821:
1822: static boolean isLeftMouseButton(MouseEvent me) {
1823: switch (me.getID()) {
1824: case MouseEvent.MOUSE_PRESSED:
1825: case MouseEvent.MOUSE_RELEASED:
1826: return (me.getButton() == MouseEvent.BUTTON1);
1827: case MouseEvent.MOUSE_ENTERED:
1828: case MouseEvent.MOUSE_EXITED:
1829: case MouseEvent.MOUSE_CLICKED:
1830: case MouseEvent.MOUSE_DRAGGED:
1831: return ((me.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0);
1832: }
1833: return false;
1834: }
1835:
1836: static boolean isRightMouseButton(MouseEvent me) {
1837: int numButtons = ((Integer) getDefaultToolkit()
1838: .getDesktopProperty("awt.mouse.numButtons")).intValue();
1839: switch (me.getID()) {
1840: case MouseEvent.MOUSE_PRESSED:
1841: case MouseEvent.MOUSE_RELEASED:
1842: return ((numButtons == 2 && me.getButton() == MouseEvent.BUTTON2) || (numButtons > 2 && me
1843: .getButton() == MouseEvent.BUTTON3));
1844: case MouseEvent.MOUSE_ENTERED:
1845: case MouseEvent.MOUSE_EXITED:
1846: case MouseEvent.MOUSE_CLICKED:
1847: case MouseEvent.MOUSE_DRAGGED:
1848: return ((numButtons == 2 && (me.getModifiersEx() & InputEvent.BUTTON2_DOWN_MASK) != 0) || (numButtons > 2 && (me
1849: .getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0));
1850: }
1851: return false;
1852: }
1853:
1854: static long reset_time_utc;
1855: static final long WRAP_TIME_MILLIS = Integer.MAX_VALUE;
1856:
1857: /*
1858: * This function converts between the X server time (number of milliseconds
1859: * since the last server reset) and the UTC time for the 'when' field of an
1860: * InputEvent (or another event type with a timestamp).
1861: */
1862: static long nowMillisUTC_offset(long server_offset) {
1863: // ported from awt_util.c
1864: /*
1865: * Because Time is of type 'unsigned long', it is possible that Time will
1866: * never wrap when using 64-bit Xlib. However, if a 64-bit client
1867: * connects to a 32-bit server, I suspect the values will still wrap. So
1868: * we should not attempt to remove the wrap checking even if _LP64 is
1869: * true.
1870: */
1871:
1872: long current_time_utc = System.currentTimeMillis();
1873: if (log.isLoggable(Level.FINER)) {
1874: log.finer("reset_time=" + reset_time_utc
1875: + ", current_time=" + current_time_utc
1876: + ", server_offset=" + server_offset
1877: + ", wrap_time=" + WRAP_TIME_MILLIS);
1878: }
1879:
1880: if ((current_time_utc - reset_time_utc) > WRAP_TIME_MILLIS) {
1881: reset_time_utc = System.currentTimeMillis()
1882: - getCurrentServerTime();
1883: }
1884:
1885: if (log.isLoggable(Level.FINER)) {
1886: log.finer("result = " + (reset_time_utc + server_offset));
1887: }
1888: return reset_time_utc + server_offset;
1889: }
1890:
1891: /**
1892: * @see sun.awt.SunToolkit#needsXEmbedImpl
1893: */
1894: protected boolean needsXEmbedImpl() {
1895: // XToolkit implements supports for XEmbed-client protocol and
1896: // requires the supports from the embedding host for it to work.
1897: return true;
1898: }
1899:
1900: public boolean isModalityTypeSupported(
1901: Dialog.ModalityType modalityType) {
1902: return (modalityType == null)
1903: || (modalityType == Dialog.ModalityType.MODELESS)
1904: || (modalityType == Dialog.ModalityType.DOCUMENT_MODAL)
1905: || (modalityType == Dialog.ModalityType.APPLICATION_MODAL)
1906: || (modalityType == Dialog.ModalityType.TOOLKIT_MODAL);
1907: }
1908:
1909: public boolean isModalExclusionTypeSupported(
1910: Dialog.ModalExclusionType exclusionType) {
1911: return (exclusionType == null)
1912: || (exclusionType == Dialog.ModalExclusionType.NO_EXCLUDE)
1913: || (exclusionType == Dialog.ModalExclusionType.APPLICATION_EXCLUDE)
1914: || (exclusionType == Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
1915: }
1916:
1917: static EventQueue getEventQueue(Object target) {
1918: AppContext appContext = targetToAppContext(target);
1919: if (appContext != null) {
1920: return (EventQueue) appContext
1921: .get(AppContext.EVENT_QUEUE_KEY);
1922: }
1923: return null;
1924: }
1925:
1926: static void removeSourceEvents(EventQueue queue, Object source,
1927: boolean removeAllEvents) {
1928: try {
1929: m_removeSourceEvents.invoke(queue, source, removeAllEvents);
1930: } catch (IllegalAccessException e) {
1931: e.printStackTrace();
1932: } catch (InvocationTargetException e) {
1933: e.printStackTrace();
1934: }
1935: }
1936:
1937: public boolean isAlwaysOnTopSupported() {
1938: Iterator iter = XWM.getWM().getProtocols(XLayerProtocol.class)
1939: .iterator();
1940: while (iter.hasNext()) {
1941: XLayerProtocol proto = (XLayerProtocol) iter.next();
1942: if (proto.supportsLayer(XLayerProtocol.LAYER_ALWAYS_ON_TOP)) {
1943: return true;
1944: }
1945: }
1946: return false;
1947: }
1948:
1949: public boolean useBufferPerWindow() {
1950: return XToolkit.getBackingStoreType() == XConstants.NotUseful;
1951: }
1952:
1953: /**
1954: * Returns one of XConstants: NotUseful, WhenMapped or Always.
1955: * If backing store is not available on at least one screen, or
1956: * java2d uses DGA(which conflicts with backing store) on at least one screen,
1957: * or the string system property "sun.awt.backingStore" is neither "Always"
1958: * nor "WhenMapped", then the method returns XConstants.NotUseful.
1959: * Otherwise, if the system property "sun.awt.backingStore" is "WhenMapped",
1960: * then the method returns XConstants.WhenMapped.
1961: * Otherwise (i.e., if the system property "sun.awt.backingStore" is "Always"),
1962: * the method returns XConstants.Always.
1963: */
1964: static int getBackingStoreType() {
1965: return backingStoreType;
1966: }
1967:
1968: private static void setBackingStoreType() {
1969: String prop = (String) AccessController
1970: .doPrivileged(new sun.security.action.GetPropertyAction(
1971: "sun.awt.backingStore"));
1972:
1973: if (prop == null) {
1974: backingStoreType = XConstants.NotUseful;
1975: if (backingStoreLog.isLoggable(Level.CONFIG)) {
1976: backingStoreLog
1977: .config("The system property sun.awt.backingStore is not set"
1978: + ", by default backingStore=NotUseful");
1979: }
1980: return;
1981: }
1982:
1983: if (backingStoreLog.isLoggable(Level.CONFIG)) {
1984: backingStoreLog
1985: .config("The system property sun.awt.backingStore is "
1986: + prop);
1987: }
1988: prop = prop.toLowerCase();
1989: if (prop.equals("always")) {
1990: backingStoreType = XConstants.Always;
1991: } else if (prop.equals("whenmapped")) {
1992: backingStoreType = XConstants.WhenMapped;
1993: } else {
1994: backingStoreType = XConstants.NotUseful;
1995: }
1996:
1997: if (backingStoreLog.isLoggable(Level.CONFIG)) {
1998: backingStoreLog
1999: .config("backingStore(as provided by the system property)="
2000: + (backingStoreType == XConstants.NotUseful ? "NotUseful"
2001: : backingStoreType == XConstants.WhenMapped ? "WhenMapped"
2002: : "Always"));
2003: }
2004:
2005: if (sun.java2d.x11.X11SurfaceData.isDgaAvailable()) {
2006: backingStoreType = XConstants.NotUseful;
2007:
2008: if (backingStoreLog.isLoggable(Level.CONFIG)) {
2009: backingStoreLog
2010: .config("DGA is available, backingStore=NotUseful");
2011: }
2012:
2013: return;
2014: }
2015:
2016: awtLock();
2017: try {
2018: int screenCount = XlibWrapper.ScreenCount(getDisplay());
2019: for (int i = 0; i < screenCount; i++) {
2020: if (XlibWrapper.DoesBackingStore(XlibWrapper
2021: .ScreenOfDisplay(getDisplay(), i)) == XConstants.NotUseful) {
2022: backingStoreType = XConstants.NotUseful;
2023:
2024: if (backingStoreLog.isLoggable(Level.CONFIG)) {
2025: backingStoreLog
2026: .config("Backing store is not available on the screen "
2027: + i
2028: + ", backingStore=NotUseful");
2029: }
2030:
2031: return;
2032: }
2033: }
2034: } finally {
2035: awtUnlock();
2036: }
2037: }
2038:
2039: /**
2040: * One of XConstants: NotUseful, WhenMapped or Always.
2041: */
2042: private static int backingStoreType;
2043:
2044: static boolean awt_ServerInquired = false;
2045: static boolean awt_IsXsunServer = false;
2046: static boolean awt_XKBInquired = false;
2047: static boolean awt_UseXKB = false;
2048:
2049: /**
2050: Try to understand if it is Xsun server.
2051: By now (2005) Sun is vendor of Xsun and Xorg servers; we only return true if Xsun is running.
2052: */
2053: static boolean isXsunServer() {
2054: awtLock();
2055: try {
2056: if (awt_ServerInquired) {
2057: return awt_IsXsunServer;
2058: }
2059: if (!XlibWrapper.ServerVendor(getDisplay()).startsWith(
2060: "Sun Microsystems")) {
2061: awt_ServerInquired = true;
2062: awt_IsXsunServer = false;
2063: return false;
2064: }
2065: // Now, it's Sun. It still may be Xorg though, eg on Solaris 10, x86.
2066: // Today (2005), VendorRelease of Xorg is a Big Number unlike Xsun.
2067: if (XlibWrapper.VendorRelease(getDisplay()) > 10000) {
2068: awt_ServerInquired = true;
2069: awt_IsXsunServer = false;
2070: return false;
2071: }
2072: awt_ServerInquired = true;
2073: awt_IsXsunServer = true;
2074: return true;
2075: } finally {
2076: awtUnlock();
2077: }
2078: }
2079:
2080: /**
2081: Query XKEYBOARD extension.
2082: */
2083: static boolean isXKBenabled() {
2084: awtLock();
2085: try {
2086: if (awt_XKBInquired) {
2087: return awt_UseXKB;
2088: }
2089: awt_XKBInquired = true;
2090: String name = "XKEYBOARD";
2091: awt_UseXKB = XlibWrapper.XQueryExtension(getDisplay(),
2092: name, XlibWrapper.larg1, XlibWrapper.larg2,
2093: XlibWrapper.larg3);
2094: return awt_UseXKB;
2095: } finally {
2096: awtUnlock();
2097: }
2098: }
2099:
2100: private static long eventNumber;
2101:
2102: public static long getEventNumber() {
2103: awtLock();
2104: try {
2105: return eventNumber;
2106: } finally {
2107: awtUnlock();
2108: }
2109: }
2110:
2111: private static XEventDispatcher oops_waiter;
2112: private static boolean oops_updated;
2113: private static boolean oops_failed;
2114: private XAtom oops;
2115: private static final long WORKAROUND_SLEEP = 100;
2116:
2117: /**
2118: * @inheritDoc
2119: */
2120: protected boolean syncNativeQueue(final long timeout) {
2121: XBaseWindow win = XBaseWindow.getXAWTRootWindow();
2122:
2123: if (oops_waiter == null) {
2124: oops_waiter = new XEventDispatcher() {
2125: public void dispatchEvent(XEvent e) {
2126: if (e.get_type() == SelectionNotify) {
2127: XSelectionEvent pe = e.get_xselection();
2128: if (pe.get_property() == oops.getAtom()) {
2129: oops_updated = true;
2130: awtLockNotifyAll();
2131: } else if (pe.get_selection() == XAtom.get(
2132: "WM_S0").getAtom()
2133: && pe.get_target() == XAtom.get(
2134: "VERSION").getAtom()
2135: && pe.get_property() == 0
2136: && XlibWrapper
2137: .XGetSelectionOwner(
2138: getDisplay(), XAtom
2139: .get("WM_S0")
2140: .getAtom()) == 0) {
2141: // WM forgot to acquire selection or there is no WM
2142: oops_failed = true;
2143: awtLockNotifyAll();
2144: }
2145:
2146: }
2147: }
2148: };
2149: }
2150:
2151: if (oops == null) {
2152: oops = XAtom.get("OOPS");
2153: }
2154:
2155: awtLock();
2156: try {
2157: addEventDispatcher(win.getWindow(), oops_waiter);
2158:
2159: oops_updated = false;
2160: oops_failed = false;
2161: // Wait for selection notify for oops on win
2162: long event_number = getEventNumber();
2163: XAtom atom = XAtom.get("WM_S0");
2164: eventLog.log(Level.FINER, "WM_S0 selection owner {0}",
2165: new Object[] { XlibWrapper.XGetSelectionOwner(
2166: getDisplay(), atom.getAtom()) });
2167: XlibWrapper.XConvertSelection(getDisplay(), atom.getAtom(),
2168: XAtom.get("VERSION").getAtom(), oops.getAtom(), win
2169: .getWindow(), XlibWrapper.CurrentTime);
2170: XSync();
2171:
2172: eventLog.finer("Requested OOPS");
2173:
2174: long start = System.currentTimeMillis();
2175: while (!oops_updated && !oops_failed) {
2176: try {
2177: awtLockWait(timeout);
2178: } catch (InterruptedException e) {
2179: throw new RuntimeException(e);
2180: }
2181: // This "while" is a protection from spurious
2182: // wake-ups. However, we shouldn't wait for too long
2183: if ((System.currentTimeMillis() - start > timeout)
2184: && timeout >= 0) {
2185: throw new OperationTimedOut(Long.toString(System
2186: .currentTimeMillis()
2187: - start));
2188: }
2189: }
2190: if (oops_failed && getEventNumber() - event_number == 1) {
2191: // If selection update failed we can simply wait some time
2192: // hoping some events will arrive
2193: awtUnlock();
2194: eventLog.log(Level.FINEST, "Emergency sleep");
2195: try {
2196: Thread.sleep(WORKAROUND_SLEEP);
2197: } catch (InterruptedException ie) {
2198: throw new RuntimeException(ie);
2199: } finally {
2200: awtLock();
2201: }
2202: }
2203: return getEventNumber() - event_number > 2;
2204: } finally {
2205: removeEventDispatcher(win.getWindow(), oops_waiter);
2206: eventLog.log(Level.FINER, "Exiting syncNativeQueue");
2207: awtUnlock();
2208: }
2209: }
2210:
2211: public void grab(Window w) {
2212: if (w.getPeer() != null) {
2213: ((XWindowPeer) w.getPeer()).setGrab(true);
2214: }
2215: }
2216:
2217: public void ungrab(Window w) {
2218: if (w.getPeer() != null) {
2219: ((XWindowPeer) w.getPeer()).setGrab(false);
2220: }
2221: }
2222:
2223: /**
2224: * Returns if the java.awt.Desktop class is supported on the current
2225: * desktop.
2226: * <p>
2227: * The methods of java.awt.Desktop class are supported on the Gnome desktop.
2228: * Check if the running desktop is Gnome by checking the window manager.
2229: */
2230: public boolean isDesktopSupported() {
2231: return XDesktopPeer.isDesktopSupported();
2232: }
2233:
2234: public DesktopPeer createDesktopPeer(Desktop target) {
2235: return new XDesktopPeer();
2236: }
2237:
2238: public static native void setNoisyXErrorHandler();
2239: }
|