1 /*
   2  * Copyright (c) 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 package sun.awt.X11;
  26 
  27 import java.security.AccessController;
  28 import sun.awt.SunToolkit;
  29 import sun.security.action.GetBooleanAction;
  30 import sun.util.logging.PlatformLogger;
  31 
  32 /**
  33  * This class contains code of the global toolkit error handler, exposes static
  34  * methods which allow to set and unset synthetic error handlers.
  35  */
  36 public final class XErrorHandlerUtil {
  37     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XErrorHandlerUtil");
  38 
  39     /**
  40      * The connection to X11 window server.
  41      */
  42     private static long display;
  43 
  44     /**
  45      * Error handler at the moment of <code>XErrorHandlerUtil</code> initialization.
  46      */
  47     private static long saved_error_handler;
  48 
  49     /**
  50      * XErrorEvent being handled.
  51      */
  52     static volatile XErrorEvent saved_error;
  53 
  54     /**
  55      * Current error handler or null if no error handler is set.
  56      */
  57     private static XErrorHandler current_error_handler;
  58 
  59     /**
  60      * Value of sun.awt.noisyerrorhandler system property.
  61      */
  62     private static boolean noisyAwtHandler = AccessController.doPrivileged(
  63         new GetBooleanAction("sun.awt.noisyerrorhandler"));
  64 
  65     /**
  66      * The flag indicating that <code>init</code> was called already.
  67      */
  68     private static boolean initPassed;
  69 
  70     /**
  71      * Guarantees that no instance of this class can be created.
  72      */
  73     private XErrorHandlerUtil() {}
  74 
  75     /**
  76      * Sets the toolkit global error handler, stores the connection to X11 server, which
  77      * will be used during an error handling process. This method is called once from
  78      * <code>awt_init_Display</code> function defined in <code>awt_GraphicsEnv.c</code>
  79      * file immediately after the connection to X11 window server is opened.
  80      * @param display the connection to X11 server which should be stored
  81      */
  82     private static void init(long display) {
  83         SunToolkit.awtLock();
  84         try {
  85             if (!initPassed) {
  86                 XErrorHandlerUtil.display = display;
  87                 saved_error_handler = XlibWrapper.SetToolkitErrorHandler();
  88                 initPassed = true;
  89             }
  90         } finally {
  91             SunToolkit.awtUnlock();
  92         }
  93     }
  94 
  95     /**
  96      * Sets a synthetic error handler. Must be called with the acquired AWT lock.
  97      * @param handler the synthetic error handler to set
  98      */
  99     public static void WITH_XERROR_HANDLER(XErrorHandler handler) {
 100         saved_error = null;
 101         current_error_handler = handler;
 102     }
 103 
 104     /**
 105      * Unsets a current synthetic error handler. Must be called with the acquired AWT lock.
 106      */
 107     public static void RESTORE_XERROR_HANDLER() {
 108         // Wait until all requests are processed by the X server
 109         // and only then uninstall the error handler.
 110         XSync();
 111         current_error_handler = null;
 112     }
 113 
 114     /**
 115      * Should be called under LOCK.
 116      */
 117     public static int SAVED_XERROR_HANDLER(long display, XErrorEvent error) {
 118         if (saved_error_handler != 0) {
 119             // Default XErrorHandler may just terminate the process. Don't call it.
 120             // return XlibWrapper.CallErrorHandler(saved_error_handler, display, error.pData);
 121         }
 122         if (log.isLoggable(PlatformLogger.FINE)) {
 123             log.fine("Unhandled XErrorEvent: " +
 124                 "id=" + error.get_resourceid() + ", " +
 125                 "serial=" + error.get_serial() + ", " +
 126                 "ec=" + error.get_error_code() + ", " +
 127                 "rc=" + error.get_request_code() + ", " +
 128                 "mc=" + error.get_minor_code());
 129         }
 130         return 0;
 131     }
 132 
 133     /**
 134      * Called from the native code when an error occurs.
 135      */
 136     private static int globalErrorHandler(long display, long event_ptr) {
 137         if (noisyAwtHandler) {
 138             XlibWrapper.PrintXErrorEvent(display, event_ptr);
 139         }
 140         XErrorEvent event = new XErrorEvent(event_ptr);
 141         saved_error = event;
 142         try {
 143             if (current_error_handler != null) {
 144                 return current_error_handler.handleError(display, event);
 145             } else {
 146                 return SAVED_XERROR_HANDLER(display, event);
 147             }
 148         } catch (Throwable z) {
 149             log.fine("Error in GlobalErrorHandler", z);
 150         }
 151         return 0;
 152     }
 153 
 154     private static void XSync() {
 155         SunToolkit.awtLock();
 156         try {
 157             XlibWrapper.XSync(getDisplay(), 0);
 158         } finally {
 159             SunToolkit.awtUnlock();
 160         }
 161     }
 162 
 163     private static long getDisplay() {
 164         return display;
 165     }
 166 }