6447N/A/*
6447N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6447N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6447N/A *
6447N/A * This code is free software; you can redistribute it and/or modify it
6447N/A * under the terms of the GNU General Public License version 2 only, as
6447N/A * published by the Free Software Foundation. Oracle designates this
6447N/A * particular file as subject to the "Classpath" exception as provided
6447N/A * by Oracle in the LICENSE file that accompanied this code.
6447N/A *
6447N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6447N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6447N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6447N/A * version 2 for more details (a copy is included in the LICENSE file that
6447N/A * accompanied this code).
6447N/A *
6447N/A * You should have received a copy of the GNU General Public License version
6447N/A * 2 along with this work; if not, write to the Free Software Foundation,
6447N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6447N/A *
6447N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6447N/A * or visit www.oracle.com if you need additional information or have any
6447N/A * questions.
6447N/A */
6447N/Apackage sun.awt.X11;
6447N/A
6447N/Aimport java.security.AccessController;
6447N/Aimport sun.awt.SunToolkit;
6447N/Aimport sun.security.action.GetBooleanAction;
6447N/Aimport sun.util.logging.PlatformLogger;
6447N/A
6447N/A/**
6447N/A * This class contains code of the global toolkit error handler, exposes static
6447N/A * methods which allow to set and unset synthetic error handlers.
6447N/A */
6447N/Apublic final class XErrorHandlerUtil {
6447N/A private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XErrorHandlerUtil");
6447N/A
6447N/A /**
6447N/A * The connection to X11 window server.
6447N/A */
6447N/A private static long display;
6447N/A
6447N/A /**
6447N/A * Error handler at the moment of {@code XErrorHandlerUtil} initialization.
6447N/A */
6447N/A private static long saved_error_handler;
6447N/A
6447N/A /**
6447N/A * XErrorEvent being handled.
6447N/A */
6447N/A static volatile XErrorEvent saved_error;
6447N/A
6447N/A /**
6447N/A * Current error handler or null if no error handler is set.
6447N/A */
6447N/A private static XErrorHandler current_error_handler;
6447N/A
6447N/A /**
6447N/A * Value of sun.awt.noisyerrorhandler system property.
6447N/A */
6447N/A private static boolean noisyAwtHandler = AccessController.doPrivileged(
6447N/A new GetBooleanAction("sun.awt.noisyerrorhandler"));
6447N/A
6447N/A /**
6447N/A * The flag indicating that {@code init} was called already.
6447N/A */
6447N/A private static boolean initPassed;
6447N/A
6447N/A /**
6447N/A * Guarantees that no instance of this class can be created.
6447N/A */
6447N/A private XErrorHandlerUtil() {}
6447N/A
6447N/A /**
6447N/A * Sets the toolkit global error handler, stores the connection to X11 server,
6447N/A * which will be used during an error handling process. This method is called
6447N/A * once from {@code awt_init_Display} function defined in {@code awt_GraphicsEnv.c}
6447N/A * file immediately after the connection to X11 window server is opened.
6447N/A * @param display the connection to X11 server which should be stored
6447N/A */
6447N/A private static void init(long display) {
6447N/A SunToolkit.awtLock();
6447N/A try {
6447N/A if (!initPassed) {
6447N/A XErrorHandlerUtil.display = display;
6447N/A saved_error_handler = XlibWrapper.SetToolkitErrorHandler();
6447N/A initPassed = true;
6447N/A }
6447N/A } finally {
6447N/A SunToolkit.awtUnlock();
6447N/A }
6447N/A }
6447N/A
6447N/A /**
6447N/A * Sets a synthetic error handler. Must be called with the acquired AWT lock.
6447N/A * @param handler the synthetic error handler to set
6447N/A */
6447N/A public static void WITH_XERROR_HANDLER(XErrorHandler handler) {
6447N/A saved_error = null;
6447N/A current_error_handler = handler;
6447N/A }
6447N/A
6447N/A /**
6447N/A * Unsets a current synthetic error handler. Must be called with the acquired AWT lock.
6447N/A */
6447N/A public static void RESTORE_XERROR_HANDLER() {
6447N/A RESTORE_XERROR_HANDLER(true);
6447N/A }
6447N/A
6447N/A private static void RESTORE_XERROR_HANDLER(boolean doXSync) {
6447N/A if (doXSync) {
6447N/A // Wait until all requests are processed by the X server
6447N/A // and only then uninstall the error handler.
6447N/A XSync();
6447N/A }
6447N/A current_error_handler = null;
6447N/A }
6447N/A
6447N/A /**
6447N/A * Should be called under LOCK.
6447N/A */
6447N/A public static int SAVED_XERROR_HANDLER(long display, XErrorEvent error) {
6447N/A if (saved_error_handler != 0) {
6447N/A // Default XErrorHandler may just terminate the process. Don't call it.
6447N/A // return XlibWrapper.CallErrorHandler(saved_error_handler, display, error.pData);
6447N/A }
6447N/A if (log.isLoggable(PlatformLogger.FINE)) {
6447N/A log.fine("Unhandled XErrorEvent: " +
6447N/A "id=" + error.get_resourceid() + ", " +
6447N/A "serial=" + error.get_serial() + ", " +
6447N/A "ec=" + error.get_error_code() + ", " +
6447N/A "rc=" + error.get_request_code() + ", " +
6447N/A "mc=" + error.get_minor_code());
6447N/A }
6447N/A return 0;
6447N/A }
6447N/A
6447N/A /**
6447N/A * Called from the native code when an error occurs.
6447N/A */
6447N/A private static int globalErrorHandler(long display, long event_ptr) {
6447N/A if (noisyAwtHandler) {
6447N/A XlibWrapper.PrintXErrorEvent(display, event_ptr);
6447N/A }
6447N/A XErrorEvent event = new XErrorEvent(event_ptr);
6447N/A saved_error = event;
6447N/A try {
6447N/A if (current_error_handler != null) {
6447N/A return current_error_handler.handleError(display, event);
6447N/A } else {
6447N/A return SAVED_XERROR_HANDLER(display, event);
6447N/A }
6447N/A } catch (Throwable z) {
6447N/A log.fine("Error in GlobalErrorHandler", z);
6447N/A }
6447N/A return 0;
6447N/A }
6447N/A
6447N/A private static void XSync() {
6447N/A SunToolkit.awtLock();
6447N/A try {
6447N/A XlibWrapper.XSync(display, 0);
6447N/A } finally {
6447N/A SunToolkit.awtUnlock();
6447N/A }
6447N/A }
6447N/A}