Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/eawt/_AppEventHandler.java
38831 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.eawt;2627import java.awt.*;28import java.awt.event.WindowEvent;29import java.io.File;30import java.net.*;31import java.util.*;32import java.util.List;33import sun.awt.AppContext;34import sun.awt.SunToolkit;3536import com.apple.eawt.AppEvent.*;3738class _AppEventHandler {39private static final int NOTIFY_ABOUT = 1;40private static final int NOTIFY_PREFS = 2;41private static final int NOTIFY_OPEN_APP = 3;42private static final int NOTIFY_REOPEN_APP = 4;43private static final int NOTIFY_QUIT = 5;44private static final int NOTIFY_SHUTDOWN = 6;45private static final int NOTIFY_ACTIVE_APP_GAINED = 7;46private static final int NOTIFY_ACTIVE_APP_LOST = 8;47private static final int NOTIFY_APP_HIDDEN = 9;48private static final int NOTIFY_APP_SHOWN = 10;49private static final int NOTIFY_USER_SESSION_ACTIVE = 11;50private static final int NOTIFY_USER_SESSION_INACTIVE = 12;51private static final int NOTIFY_SCREEN_SLEEP = 13;52private static final int NOTIFY_SCREEN_WAKE = 14;53private static final int NOTIFY_SYSTEM_SLEEP = 15;54private static final int NOTIFY_SYSTEM_WAKE = 16;5556private static final int REGISTER_USER_SESSION = 1;57private static final int REGISTER_SCREEN_SLEEP = 2;58private static final int REGISTER_SYSTEM_SLEEP = 3;5960private static native void nativeOpenCocoaAboutWindow();61private static native void nativeReplyToAppShouldTerminate(final boolean shouldTerminate);62private static native void nativeRegisterForNotification(final int notification);6364final static _AppEventHandler instance = new _AppEventHandler();65static _AppEventHandler getInstance() {66return instance;67}6869// single shot dispatchers (some queuing, others not)70final _AboutDispatcher aboutDispatcher = new _AboutDispatcher();71final _PreferencesDispatcher preferencesDispatcher = new _PreferencesDispatcher();72final _OpenFileDispatcher openFilesDispatcher = new _OpenFileDispatcher();73final _PrintFileDispatcher printFilesDispatcher = new _PrintFileDispatcher();74final _OpenURIDispatcher openURIDispatcher = new _OpenURIDispatcher();75final _QuitDispatcher quitDispatcher = new _QuitDispatcher();76final _OpenAppDispatcher openAppDispatcher = new _OpenAppDispatcher();7778// multiplexing dispatchers (contains listener lists)79final _AppReOpenedDispatcher reOpenAppDispatcher = new _AppReOpenedDispatcher();80final _AppForegroundDispatcher foregroundAppDispatcher = new _AppForegroundDispatcher();81final _HiddenAppDispatcher hiddenAppDispatcher = new _HiddenAppDispatcher();82final _UserSessionDispatcher userSessionDispatcher = new _UserSessionDispatcher();83final _ScreenSleepDispatcher screenSleepDispatcher = new _ScreenSleepDispatcher();84final _SystemSleepDispatcher systemSleepDispatcher = new _SystemSleepDispatcher();8586final _AppEventLegacyHandler legacyHandler = new _AppEventLegacyHandler(this);8788QuitStrategy defaultQuitAction = QuitStrategy.SYSTEM_EXIT_0;8990_AppEventHandler() {91final String strategyProp = System.getProperty("apple.eawt.quitStrategy");92if (strategyProp == null) return;9394if ("CLOSE_ALL_WINDOWS".equals(strategyProp)) {95setDefaultQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);96} else if ("SYSTEM_EXIT_O".equals(strategyProp)) {97setDefaultQuitStrategy(QuitStrategy.SYSTEM_EXIT_0);98} else {99System.err.println("unrecognized apple.eawt.quitStrategy: " + strategyProp);100}101}102103void addListener(final AppEventListener listener) {104if (listener instanceof AppReOpenedListener) reOpenAppDispatcher.addListener((AppReOpenedListener)listener);105if (listener instanceof AppForegroundListener) foregroundAppDispatcher.addListener((AppForegroundListener)listener);106if (listener instanceof AppHiddenListener) hiddenAppDispatcher.addListener((AppHiddenListener)listener);107if (listener instanceof UserSessionListener) userSessionDispatcher.addListener((UserSessionListener)listener);108if (listener instanceof ScreenSleepListener) screenSleepDispatcher.addListener((ScreenSleepListener)listener);109if (listener instanceof SystemSleepListener) systemSleepDispatcher.addListener((SystemSleepListener)listener);110}111112void removeListener(final AppEventListener listener) {113if (listener instanceof AppReOpenedListener) reOpenAppDispatcher.removeListener((AppReOpenedListener)listener);114if (listener instanceof AppForegroundListener) foregroundAppDispatcher.removeListener((AppForegroundListener)listener);115if (listener instanceof AppHiddenListener) hiddenAppDispatcher.removeListener((AppHiddenListener)listener);116if (listener instanceof UserSessionListener) userSessionDispatcher.removeListener((UserSessionListener)listener);117if (listener instanceof ScreenSleepListener) screenSleepDispatcher.removeListener((ScreenSleepListener)listener);118if (listener instanceof SystemSleepListener) systemSleepDispatcher.removeListener((SystemSleepListener)listener);119}120121void openCocoaAboutWindow() {122nativeOpenCocoaAboutWindow();123}124125void setDefaultQuitStrategy(final QuitStrategy defaultQuitAction) {126this.defaultQuitAction = defaultQuitAction;127}128129QuitResponse currentQuitResponse;130synchronized QuitResponse obtainQuitResponse() {131if (currentQuitResponse != null) return currentQuitResponse;132return currentQuitResponse = new QuitResponse(this);133}134135synchronized void cancelQuit() {136currentQuitResponse = null;137nativeReplyToAppShouldTerminate(false);138}139140synchronized void performQuit() {141currentQuitResponse = null;142143try {144if (defaultQuitAction == QuitStrategy.SYSTEM_EXIT_0) System.exit(0);145146if (defaultQuitAction != QuitStrategy.CLOSE_ALL_WINDOWS) {147throw new RuntimeException("Unknown quit action");148}149150EventQueue.invokeLater(new Runnable() {151public void run() {152// walk frames from back to front153final Frame[] allFrames = Frame.getFrames();154for (int i = allFrames.length - 1; i >= 0; i--) {155final Frame frame = allFrames[i];156frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));157}158}159});160} finally {161// Either we've just called System.exit(), or the app will call162// it when processing a WINDOW_CLOSING event. Either way, we reply163// to Cocoa that we don't want to exit the event loop yet.164nativeReplyToAppShouldTerminate(false);165}166}167168/*169* callbacks from native delegate170*/171private static void handlePrintFiles(final List<String> filenames) {172instance.printFilesDispatcher.dispatch(new _NativeEvent(filenames));173}174175private static void handleOpenFiles(final List<String> filenames, final String searchTerm) {176instance.openFilesDispatcher.dispatch(new _NativeEvent(filenames, searchTerm));177}178179private static void handleOpenURI(final String uri) {180instance.openURIDispatcher.dispatch(new _NativeEvent(uri));181}182183// default funnel for non-complex events184private static void handleNativeNotification(final int code) {185// System.out.println(code);186187switch (code) {188case NOTIFY_ABOUT:189instance.aboutDispatcher.dispatch(new _NativeEvent());190break;191case NOTIFY_PREFS:192instance.preferencesDispatcher.dispatch(new _NativeEvent());193break;194case NOTIFY_OPEN_APP:195instance.openAppDispatcher.dispatch(new _NativeEvent());196break;197case NOTIFY_REOPEN_APP:198instance.reOpenAppDispatcher.dispatch(new _NativeEvent());199break;200case NOTIFY_QUIT:201instance.quitDispatcher.dispatch(new _NativeEvent());202break;203case NOTIFY_SHUTDOWN:204// do nothing for now205break;206case NOTIFY_ACTIVE_APP_GAINED:207instance.foregroundAppDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));208break;209case NOTIFY_ACTIVE_APP_LOST:210instance.foregroundAppDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));211break;212case NOTIFY_APP_HIDDEN:213instance.hiddenAppDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));214break;215case NOTIFY_APP_SHOWN:216instance.hiddenAppDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));217break;218case NOTIFY_USER_SESSION_ACTIVE:219instance.userSessionDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));220break;221case NOTIFY_USER_SESSION_INACTIVE:222instance.userSessionDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));223break;224case NOTIFY_SCREEN_SLEEP:225instance.screenSleepDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));226break;227case NOTIFY_SCREEN_WAKE:228instance.screenSleepDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));229break;230case NOTIFY_SYSTEM_SLEEP:231instance.systemSleepDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));232break;233case NOTIFY_SYSTEM_WAKE:234instance.systemSleepDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));235break;236default:237System.err.println("EAWT unknown native notification: " + code);238break;239}240}241242243class _AboutDispatcher extends _AppEventDispatcher<AboutHandler> {244void performDefaultAction(final _NativeEvent event) {245openCocoaAboutWindow(); // if the handler is null, fall back to showing the Cocoa default246}247248void performUsing(final AboutHandler handler, final _NativeEvent event) {249handler.handleAbout(new AboutEvent());250}251}252253class _PreferencesDispatcher extends _AppEventDispatcher<PreferencesHandler> {254synchronized void setHandler(final PreferencesHandler handler) {255super.setHandler(handler);256257_AppMenuBarHandler.getInstance().setPreferencesMenuItemVisible(handler != null);258_AppMenuBarHandler.getInstance().setPreferencesMenuItemEnabled(handler != null);259}260261void performUsing(final PreferencesHandler handler, final _NativeEvent event) {262handler.handlePreferences(new PreferencesEvent());263}264}265266class _OpenAppDispatcher extends _QueuingAppEventDispatcher<com.apple.eawt._OpenAppHandler> {267void performUsing(com.apple.eawt._OpenAppHandler handler, _NativeEvent event) {268handler.handleOpenApp();269}270}271272class _AppReOpenedDispatcher extends _AppEventMultiplexor<AppReOpenedListener> {273void performOnListener(AppReOpenedListener listener, final _NativeEvent event) {274final AppReOpenedEvent e = new AppReOpenedEvent();275listener.appReOpened(e);276}277}278279class _AppForegroundDispatcher extends _BooleanAppEventMultiplexor<AppForegroundListener, AppForegroundEvent> {280AppForegroundEvent createEvent(final boolean isTrue) { return new AppForegroundEvent(); }281282void performFalseEventOn(final AppForegroundListener listener, final AppForegroundEvent e) {283listener.appMovedToBackground(e);284}285286void performTrueEventOn(final AppForegroundListener listener, final AppForegroundEvent e) {287listener.appRaisedToForeground(e);288}289}290291class _HiddenAppDispatcher extends _BooleanAppEventMultiplexor<AppHiddenListener, AppHiddenEvent> {292AppHiddenEvent createEvent(final boolean isTrue) { return new AppHiddenEvent(); }293294void performFalseEventOn(final AppHiddenListener listener, final AppHiddenEvent e) {295listener.appUnhidden(e);296}297298void performTrueEventOn(final AppHiddenListener listener, final AppHiddenEvent e) {299listener.appHidden(e);300}301}302303class _UserSessionDispatcher extends _BooleanAppEventMultiplexor<UserSessionListener, UserSessionEvent> {304UserSessionEvent createEvent(final boolean isTrue) { return new UserSessionEvent(); }305306void performFalseEventOn(final UserSessionListener listener, final UserSessionEvent e) {307listener.userSessionDeactivated(e);308}309310void performTrueEventOn(final UserSessionListener listener, final UserSessionEvent e) {311listener.userSessionActivated(e);312}313314void registerNativeListener() {315nativeRegisterForNotification(REGISTER_USER_SESSION);316}317}318319class _ScreenSleepDispatcher extends _BooleanAppEventMultiplexor<ScreenSleepListener, ScreenSleepEvent> {320ScreenSleepEvent createEvent(final boolean isTrue) { return new ScreenSleepEvent(); }321322void performFalseEventOn(final ScreenSleepListener listener, final ScreenSleepEvent e) {323listener.screenAwoke(e);324}325326void performTrueEventOn(final ScreenSleepListener listener, final ScreenSleepEvent e) {327listener.screenAboutToSleep(e);328}329330void registerNativeListener() {331nativeRegisterForNotification(REGISTER_SCREEN_SLEEP);332}333}334335class _SystemSleepDispatcher extends _BooleanAppEventMultiplexor<SystemSleepListener, SystemSleepEvent> {336SystemSleepEvent createEvent(final boolean isTrue) { return new SystemSleepEvent(); }337338void performFalseEventOn(final SystemSleepListener listener, final SystemSleepEvent e) {339listener.systemAwoke(e);340}341342void performTrueEventOn(final SystemSleepListener listener, final SystemSleepEvent e) {343listener.systemAboutToSleep(e);344}345346void registerNativeListener() {347nativeRegisterForNotification(REGISTER_SYSTEM_SLEEP);348}349}350351class _OpenFileDispatcher extends _QueuingAppEventDispatcher<OpenFilesHandler> {352void performUsing(final OpenFilesHandler handler, final _NativeEvent event) {353// create file list from fileNames354final List<String> fileNameList = event.get(0);355final ArrayList<File> files = new ArrayList<File>(fileNameList.size());356for (final String fileName : fileNameList) files.add(new File(fileName));357358// populate the properties map359final String searchTerm = event.get(1);360handler.openFiles(new OpenFilesEvent(files, searchTerm));361}362}363364class _PrintFileDispatcher extends _QueuingAppEventDispatcher<PrintFilesHandler> {365void performUsing(final PrintFilesHandler handler, final _NativeEvent event) {366// create file list from fileNames367final List<String> fileNameList = event.get(0);368final ArrayList<File> files = new ArrayList<File>(fileNameList.size());369for (final String fileName : fileNameList) files.add(new File(fileName));370371handler.printFiles(new PrintFilesEvent(files));372}373}374375// Java URLs can't handle unknown protocol types, which is why we use URIs376class _OpenURIDispatcher extends _QueuingAppEventDispatcher<OpenURIHandler> {377void performUsing(final OpenURIHandler handler, final _NativeEvent event) {378final String urlString = event.get(0);379try {380handler.openURI(new OpenURIEvent(new URI(urlString)));381} catch (final URISyntaxException e) {382throw new RuntimeException(e);383}384}385}386387class _QuitDispatcher extends _AppEventDispatcher<QuitHandler> {388void performDefaultAction(final _NativeEvent event) {389obtainQuitResponse().performQuit();390}391392void performUsing(final QuitHandler handler, final _NativeEvent event) {393final QuitResponse response = obtainQuitResponse(); // obtains the "current" quit response394handler.handleQuitRequestWith(new QuitEvent(), response);395}396}397398399// -- ABSTRACT QUEUE/EVENT/LISTENER HELPERS --400401// generic little "raw event" that's constructed easily from the native callbacks402static class _NativeEvent {403Object[] args;404405public _NativeEvent(final Object... args) {406this.args = args;407}408409@SuppressWarnings("unchecked")410<T> T get(final int i) {411if (args == null) return null;412return (T)args[i];413}414}415416abstract class _AppEventMultiplexor<L> {417private final Map<L, AppContext> listenerToAppContext =418new IdentityHashMap<L, AppContext>();419boolean nativeListenerRegistered;420421// called from AppKit Thread-0422void dispatch(final _NativeEvent event, final Object... args) {423// grab a local ref to the listeners and its contexts as an array of the map's entries424final ArrayList<Map.Entry<L, AppContext>> localEntries;425synchronized (this) {426if (listenerToAppContext.size() == 0) {427return;428}429localEntries = new ArrayList<Map.Entry<L, AppContext>>(listenerToAppContext.size());430localEntries.addAll(listenerToAppContext.entrySet());431}432433for (final Map.Entry<L, AppContext> e : localEntries) {434final L listener = e.getKey();435final AppContext listenerContext = e.getValue();436SunToolkit.invokeLaterOnAppContext(listenerContext, new Runnable() {437public void run() {438performOnListener(listener, event);439}440});441}442}443444synchronized void addListener(final L listener) {445setListenerContext(listener, AppContext.getAppContext());446447if (!nativeListenerRegistered) {448registerNativeListener();449nativeListenerRegistered = true;450}451}452453synchronized void removeListener(final L listener) {454listenerToAppContext.remove(listener);455}456457abstract void performOnListener(L listener, final _NativeEvent event);458void registerNativeListener() { }459460private void setListenerContext(L listener, AppContext listenerContext) {461if (listenerContext == null) {462throw new RuntimeException(463"Attempting to add a listener from a thread group without AppContext");464}465listenerToAppContext.put(listener, AppContext.getAppContext());466}467}468469abstract class _BooleanAppEventMultiplexor<L, E> extends _AppEventMultiplexor<L> {470@Override471void performOnListener(L listener, final _NativeEvent event) {472final boolean isTrue = Boolean.TRUE.equals(event.get(0));473final E e = createEvent(isTrue);474if (isTrue) {475performTrueEventOn(listener, e);476} else {477performFalseEventOn(listener, e);478}479}480481abstract E createEvent(final boolean isTrue);482abstract void performTrueEventOn(final L listener, final E e);483abstract void performFalseEventOn(final L listener, final E e);484}485486/*487* Ensures that setting and obtaining an app event handler is done in488* both a thread-safe manner, and that user code is performed on the489* AWT EventQueue thread.490*491* Allows native to blindly lob new events into the dispatcher,492* knowing that they will only be dispatched once a handler is set.493*494* User code is not (and should not be) run under any synchronized lock.495*/496abstract class _AppEventDispatcher<H> {497H _handler;498AppContext handlerContext;499500// called from AppKit Thread-0501void dispatch(final _NativeEvent event) {502// grab a local ref to the handler503final H localHandler;504final AppContext localHandlerContext;505synchronized (_AppEventDispatcher.this) {506localHandler = _handler;507localHandlerContext = handlerContext;508}509510if (localHandler == null) {511performDefaultAction(event);512} else {513SunToolkit.invokeLaterOnAppContext(localHandlerContext, new Runnable() {514public void run() {515performUsing(localHandler, event);516}517});518}519}520521synchronized void setHandler(final H handler) {522this._handler = handler;523524setHandlerContext(AppContext.getAppContext());525526// if a new handler is installed, block addition of legacy ApplicationListeners527if (handler == legacyHandler) return;528legacyHandler.blockLegacyAPI();529}530531void performDefaultAction(final _NativeEvent event) { } // by default, do nothing532abstract void performUsing(final H handler, final _NativeEvent event);533534protected void setHandlerContext(AppContext ctx) {535if (ctx == null) {536throw new RuntimeException(537"Attempting to set a handler from a thread group without AppContext");538}539540handlerContext = ctx;541}542}543544abstract class _QueuingAppEventDispatcher<H> extends _AppEventDispatcher<H> {545List<_NativeEvent> queuedEvents = new LinkedList<_NativeEvent>();546547@Override548void dispatch(final _NativeEvent event) {549synchronized (this) {550// dispatcher hasn't started yet551if (queuedEvents != null) {552queuedEvents.add(event);553return;554}555}556557super.dispatch(event);558}559560synchronized void setHandler(final H handler) {561this._handler = handler;562563setHandlerContext(AppContext.getAppContext());564565// dispatch any events in the queue566if (queuedEvents != null) {567// grab a local ref to the queue, so the real one can be nulled out568final java.util.List<_NativeEvent> localQueuedEvents = queuedEvents;569queuedEvents = null;570if (localQueuedEvents.size() != 0) {571for (final _NativeEvent arg : localQueuedEvents) {572dispatch(arg);573}574}575}576577// if a new handler is installed, block addition of legacy ApplicationListeners578if (handler == legacyHandler) return;579legacyHandler.blockLegacyAPI();580}581}582}583584585