Path: blob/master/src/java.desktop/macosx/classes/com/apple/eawt/_AppEventHandler.java
41153 views
/*1* Copyright (c) 2011, 2016, 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.EventQueue;28import java.awt.Frame;29import java.awt.desktop.AboutEvent;30import java.awt.desktop.AboutHandler;31import java.awt.desktop.AppForegroundEvent;32import java.awt.desktop.AppForegroundListener;33import java.awt.desktop.AppHiddenEvent;34import java.awt.desktop.AppHiddenListener;35import java.awt.desktop.AppReopenedEvent;36import java.awt.desktop.AppReopenedListener;37import java.awt.desktop.OpenFilesEvent;38import java.awt.desktop.OpenFilesHandler;39import java.awt.desktop.OpenURIEvent;40import java.awt.desktop.OpenURIHandler;41import java.awt.desktop.PreferencesEvent;42import java.awt.desktop.PreferencesHandler;43import java.awt.desktop.PrintFilesEvent;44import java.awt.desktop.PrintFilesHandler;45import java.awt.desktop.QuitEvent;46import java.awt.desktop.QuitHandler;47import java.awt.desktop.QuitStrategy;48import java.awt.desktop.ScreenSleepEvent;49import java.awt.desktop.ScreenSleepListener;50import java.awt.desktop.SystemEventListener;51import java.awt.desktop.SystemSleepEvent;52import java.awt.desktop.SystemSleepListener;53import java.awt.desktop.UserSessionEvent;54import java.awt.desktop.UserSessionEvent.Reason;55import java.awt.desktop.UserSessionListener;56import java.awt.event.WindowEvent;57import java.io.File;58import java.net.URI;59import java.net.URISyntaxException;60import java.util.ArrayList;61import java.util.IdentityHashMap;62import java.util.LinkedList;63import java.util.List;64import java.util.Map;65import sun.awt.AppContext;66import sun.awt.SunToolkit;6768class _AppEventHandler {69private static final int NOTIFY_ABOUT = 1;70private static final int NOTIFY_PREFS = 2;71private static final int NOTIFY_OPEN_APP = 3;72private static final int NOTIFY_REOPEN_APP = 4;73private static final int NOTIFY_QUIT = 5;74private static final int NOTIFY_SHUTDOWN = 6;75private static final int NOTIFY_ACTIVE_APP_GAINED = 7;76private static final int NOTIFY_ACTIVE_APP_LOST = 8;77private static final int NOTIFY_APP_HIDDEN = 9;78private static final int NOTIFY_APP_SHOWN = 10;79private static final int NOTIFY_USER_SESSION_ACTIVE = 11;80private static final int NOTIFY_USER_SESSION_INACTIVE = 12;81private static final int NOTIFY_SCREEN_SLEEP = 13;82private static final int NOTIFY_SCREEN_WAKE = 14;83private static final int NOTIFY_SYSTEM_SLEEP = 15;84private static final int NOTIFY_SYSTEM_WAKE = 16;8586private static final int REGISTER_USER_SESSION = 1;87private static final int REGISTER_SCREEN_SLEEP = 2;88private static final int REGISTER_SYSTEM_SLEEP = 3;8990private static native void nativeOpenCocoaAboutWindow();91private static native void nativeReplyToAppShouldTerminate(final boolean shouldTerminate);92private static native void nativeRegisterForNotification(final int notification);9394static final _AppEventHandler instance = new _AppEventHandler();95static _AppEventHandler getInstance() {96return instance;97}9899// single shot dispatchers (some queuing, others not)100final _AboutDispatcher aboutDispatcher = new _AboutDispatcher();101final _PreferencesDispatcher preferencesDispatcher = new _PreferencesDispatcher();102final _OpenFileDispatcher openFilesDispatcher = new _OpenFileDispatcher();103final _PrintFileDispatcher printFilesDispatcher = new _PrintFileDispatcher();104final _OpenURIDispatcher openURIDispatcher = new _OpenURIDispatcher();105final _QuitDispatcher quitDispatcher = new _QuitDispatcher();106final _OpenAppDispatcher openAppDispatcher = new _OpenAppDispatcher();107108// multiplexing dispatchers (contains listener lists)109final _AppReOpenedDispatcher reOpenAppDispatcher = new _AppReOpenedDispatcher();110final _AppForegroundDispatcher foregroundAppDispatcher = new _AppForegroundDispatcher();111final _HiddenAppDispatcher hiddenAppDispatcher = new _HiddenAppDispatcher();112final _UserSessionDispatcher userSessionDispatcher = new _UserSessionDispatcher();113final _ScreenSleepDispatcher screenSleepDispatcher = new _ScreenSleepDispatcher();114final _SystemSleepDispatcher systemSleepDispatcher = new _SystemSleepDispatcher();115116QuitStrategy defaultQuitAction = QuitStrategy.NORMAL_EXIT;117118_AppEventHandler() {119final String strategyProp = System.getProperty("apple.eawt.quitStrategy");120if (strategyProp == null) return;121122if ("CLOSE_ALL_WINDOWS".equals(strategyProp)) {123setDefaultQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);124} else if ("SYSTEM_EXIT_O".equals(strategyProp)125|| "NORMAL_EXIT".equals(strategyProp)) {126setDefaultQuitStrategy(QuitStrategy.NORMAL_EXIT);127} else {128System.err.println("unrecognized apple.eawt.quitStrategy: " + strategyProp);129}130}131132void addListener(final SystemEventListener listener) {133if (listener instanceof AppReopenedListener) reOpenAppDispatcher.addListener((AppReopenedListener)listener);134if (listener instanceof AppForegroundListener) foregroundAppDispatcher.addListener((AppForegroundListener)listener);135if (listener instanceof AppHiddenListener) hiddenAppDispatcher.addListener((AppHiddenListener)listener);136if (listener instanceof UserSessionListener) userSessionDispatcher.addListener((UserSessionListener)listener);137if (listener instanceof ScreenSleepListener) screenSleepDispatcher.addListener((ScreenSleepListener)listener);138if (listener instanceof SystemSleepListener) systemSleepDispatcher.addListener((SystemSleepListener)listener);139}140141void removeListener(final SystemEventListener listener) {142if (listener instanceof AppReopenedListener) reOpenAppDispatcher.removeListener((AppReopenedListener)listener);143if (listener instanceof AppForegroundListener) foregroundAppDispatcher.removeListener((AppForegroundListener)listener);144if (listener instanceof AppHiddenListener) hiddenAppDispatcher.removeListener((AppHiddenListener)listener);145if (listener instanceof UserSessionListener) userSessionDispatcher.removeListener((UserSessionListener)listener);146if (listener instanceof ScreenSleepListener) screenSleepDispatcher.removeListener((ScreenSleepListener)listener);147if (listener instanceof SystemSleepListener) systemSleepDispatcher.removeListener((SystemSleepListener)listener);148}149150void openCocoaAboutWindow() {151nativeOpenCocoaAboutWindow();152}153154void setDefaultQuitStrategy(final QuitStrategy defaultQuitAction) {155this.defaultQuitAction = defaultQuitAction;156}157158MacQuitResponse currentQuitResponse;159synchronized MacQuitResponse obtainQuitResponse() {160if (currentQuitResponse != null) return currentQuitResponse;161return currentQuitResponse = new MacQuitResponse(this);162}163164synchronized void cancelQuit() {165currentQuitResponse = null;166nativeReplyToAppShouldTerminate(false);167}168169synchronized void performQuit() {170currentQuitResponse = null;171172try {173if (defaultQuitAction == QuitStrategy.NORMAL_EXIT174|| _AppMiscHandlers.isSuddenTerminationEnbaled()) System.exit(0);175176if (defaultQuitAction != QuitStrategy.CLOSE_ALL_WINDOWS) {177throw new RuntimeException("Unknown quit action");178}179180EventQueue.invokeLater(new Runnable() {181public void run() {182// walk frames from back to front183final Frame[] allFrames = Frame.getFrames();184for (int i = allFrames.length - 1; i >= 0; i--) {185final Frame frame = allFrames[i];186frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));187}188}189});190} finally {191// Either we've just called System.exit(), or the app will call192// it when processing a WINDOW_CLOSING event. Either way, we reply193// to Cocoa that we don't want to exit the event loop yet.194nativeReplyToAppShouldTerminate(false);195}196}197198/*199* callbacks from native delegate200*/201private static void handlePrintFiles(final List<String> filenames) {202instance.printFilesDispatcher.dispatch(new _NativeEvent(filenames));203}204205private static void handleOpenFiles(final List<String> filenames, final String searchTerm) {206instance.openFilesDispatcher.dispatch(new _NativeEvent(filenames, searchTerm));207}208209private static void handleOpenURI(final String uri) {210instance.openURIDispatcher.dispatch(new _NativeEvent(uri));211}212213// default funnel for non-complex events214private static void handleNativeNotification(final int code) {215// System.out.println(code);216217switch (code) {218case NOTIFY_ABOUT:219instance.aboutDispatcher.dispatch(new _NativeEvent());220break;221case NOTIFY_PREFS:222instance.preferencesDispatcher.dispatch(new _NativeEvent());223break;224case NOTIFY_OPEN_APP:225instance.openAppDispatcher.dispatch(new _NativeEvent());226break;227case NOTIFY_REOPEN_APP:228instance.reOpenAppDispatcher.dispatch(new _NativeEvent());229break;230case NOTIFY_QUIT:231instance.quitDispatcher.dispatch(new _NativeEvent());232break;233case NOTIFY_SHUTDOWN:234// do nothing for now235break;236case NOTIFY_ACTIVE_APP_GAINED:237instance.foregroundAppDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));238break;239case NOTIFY_ACTIVE_APP_LOST:240instance.foregroundAppDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));241break;242case NOTIFY_APP_HIDDEN:243instance.hiddenAppDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));244break;245case NOTIFY_APP_SHOWN:246instance.hiddenAppDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));247break;248case NOTIFY_USER_SESSION_ACTIVE:249instance.userSessionDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));250break;251case NOTIFY_USER_SESSION_INACTIVE:252instance.userSessionDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));253break;254case NOTIFY_SCREEN_SLEEP:255instance.screenSleepDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));256break;257case NOTIFY_SCREEN_WAKE:258instance.screenSleepDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));259break;260case NOTIFY_SYSTEM_SLEEP:261instance.systemSleepDispatcher.dispatch(new _NativeEvent(Boolean.TRUE));262break;263case NOTIFY_SYSTEM_WAKE:264instance.systemSleepDispatcher.dispatch(new _NativeEvent(Boolean.FALSE));265break;266default:267System.err.println("EAWT unknown native notification: " + code);268break;269}270}271272273class _AboutDispatcher extends _AppEventDispatcher<AboutHandler> {274void performDefaultAction(final _NativeEvent event) {275openCocoaAboutWindow(); // if the handler is null, fall back to showing the Cocoa default276}277278void performUsing(final AboutHandler handler, final _NativeEvent event) {279handler.handleAbout(new AboutEvent());280}281}282283class _PreferencesDispatcher extends _AppEventDispatcher<PreferencesHandler> {284synchronized void setHandler(final PreferencesHandler handler) {285super.setHandler(handler);286287_AppMenuBarHandler.getInstance().setPreferencesMenuItemVisible(handler != null);288_AppMenuBarHandler.getInstance().setPreferencesMenuItemEnabled(handler != null);289}290291void performUsing(final PreferencesHandler handler, final _NativeEvent event) {292handler.handlePreferences(new PreferencesEvent());293}294}295296class _OpenAppDispatcher extends _QueuingAppEventDispatcher<com.apple.eawt._OpenAppHandler> {297void performUsing(com.apple.eawt._OpenAppHandler handler, _NativeEvent event) {298handler.handleOpenApp();299}300}301302class _AppReOpenedDispatcher extends _AppEventMultiplexor<AppReopenedListener> {303void performOnListener(AppReopenedListener listener, final _NativeEvent event) {304final AppReopenedEvent e = new AppReopenedEvent();305listener.appReopened(e);306}307}308309class _AppForegroundDispatcher extends _BooleanAppEventMultiplexor<AppForegroundListener, AppForegroundEvent> {310AppForegroundEvent createEvent(final boolean isTrue) { return new AppForegroundEvent(); }311312void performFalseEventOn(final AppForegroundListener listener, final AppForegroundEvent e) {313listener.appMovedToBackground(e);314}315316void performTrueEventOn(final AppForegroundListener listener, final AppForegroundEvent e) {317listener.appRaisedToForeground(e);318}319}320321class _HiddenAppDispatcher extends _BooleanAppEventMultiplexor<AppHiddenListener, AppHiddenEvent> {322AppHiddenEvent createEvent(final boolean isTrue) { return new AppHiddenEvent(); }323324void performFalseEventOn(final AppHiddenListener listener, final AppHiddenEvent e) {325listener.appUnhidden(e);326}327328void performTrueEventOn(final AppHiddenListener listener, final AppHiddenEvent e) {329listener.appHidden(e);330}331}332333class _UserSessionDispatcher extends _BooleanAppEventMultiplexor<UserSessionListener, UserSessionEvent> {334UserSessionEvent createEvent(final boolean isTrue) {335return new UserSessionEvent(Reason.UNSPECIFIED);336}337338void performFalseEventOn(final UserSessionListener listener, final UserSessionEvent e) {339listener.userSessionDeactivated(e);340}341342void performTrueEventOn(final UserSessionListener listener, final UserSessionEvent e) {343listener.userSessionActivated(e);344}345346void registerNativeListener() {347nativeRegisterForNotification(REGISTER_USER_SESSION);348}349}350351class _ScreenSleepDispatcher extends _BooleanAppEventMultiplexor<ScreenSleepListener, ScreenSleepEvent> {352ScreenSleepEvent createEvent(final boolean isTrue) { return new ScreenSleepEvent(); }353354void performFalseEventOn(final ScreenSleepListener listener, final ScreenSleepEvent e) {355listener.screenAwoke(e);356}357358void performTrueEventOn(final ScreenSleepListener listener, final ScreenSleepEvent e) {359listener.screenAboutToSleep(e);360}361362void registerNativeListener() {363nativeRegisterForNotification(REGISTER_SCREEN_SLEEP);364}365}366367class _SystemSleepDispatcher extends _BooleanAppEventMultiplexor<SystemSleepListener, SystemSleepEvent> {368SystemSleepEvent createEvent(final boolean isTrue) { return new SystemSleepEvent(); }369370void performFalseEventOn(final SystemSleepListener listener, final SystemSleepEvent e) {371listener.systemAwoke(e);372}373374void performTrueEventOn(final SystemSleepListener listener, final SystemSleepEvent e) {375listener.systemAboutToSleep(e);376}377378void registerNativeListener() {379nativeRegisterForNotification(REGISTER_SYSTEM_SLEEP);380}381}382383class _OpenFileDispatcher extends _QueuingAppEventDispatcher<OpenFilesHandler> {384void performUsing(final OpenFilesHandler handler, final _NativeEvent event) {385// create file list from fileNames386final List<String> fileNameList = event.get(0);387final ArrayList<File> files = new ArrayList<File>(fileNameList.size());388for (final String fileName : fileNameList) files.add(new File(fileName));389390// populate the properties map391final String searchTerm = event.get(1);392handler.openFiles(new OpenFilesEvent(files, searchTerm));393}394}395396class _PrintFileDispatcher extends _QueuingAppEventDispatcher<PrintFilesHandler> {397void performUsing(final PrintFilesHandler handler, final _NativeEvent event) {398// create file list from fileNames399final List<String> fileNameList = event.get(0);400final ArrayList<File> files = new ArrayList<File>(fileNameList.size());401for (final String fileName : fileNameList) files.add(new File(fileName));402403handler.printFiles(new PrintFilesEvent(files));404}405}406407// Java URLs can't handle unknown protocol types, which is why we use URIs408class _OpenURIDispatcher extends _QueuingAppEventDispatcher<OpenURIHandler> {409void performUsing(final OpenURIHandler handler, final _NativeEvent event) {410final String urlString = event.get(0);411try {412handler.openURI(new OpenURIEvent(new URI(urlString)));413} catch (final URISyntaxException e) {414throw new RuntimeException(e);415}416}417}418419class _QuitDispatcher extends _AppEventDispatcher<QuitHandler> {420void performDefaultAction(final _NativeEvent event) {421obtainQuitResponse().performQuit();422}423424void performUsing(final QuitHandler handler, final _NativeEvent event) {425if (_AppMiscHandlers.isSuddenTerminationEnbaled()) {426performDefaultAction(event);427return;428}429final MacQuitResponse response = obtainQuitResponse(); // obtains the "current" quit response430handler.handleQuitRequestWith(new QuitEvent(), response);431}432}433434435// -- ABSTRACT QUEUE/EVENT/LISTENER HELPERS --436437// generic little "raw event" that's constructed easily from the native callbacks438static class _NativeEvent {439Object[] args;440441public _NativeEvent(final Object... args) {442this.args = args;443}444445@SuppressWarnings("unchecked")446<T> T get(final int i) {447if (args == null) return null;448return (T)args[i];449}450}451452abstract class _AppEventMultiplexor<L> {453private final Map<L, AppContext> listenerToAppContext =454new IdentityHashMap<L, AppContext>();455boolean nativeListenerRegistered;456457// called from AppKit Thread-0458void dispatch(final _NativeEvent event, final Object... args) {459// grab a local ref to the listeners and its contexts as an array of the map's entries460final ArrayList<Map.Entry<L, AppContext>> localEntries;461synchronized (this) {462if (listenerToAppContext.size() == 0) {463return;464}465localEntries = new ArrayList<Map.Entry<L, AppContext>>(listenerToAppContext.size());466localEntries.addAll(listenerToAppContext.entrySet());467}468469for (final Map.Entry<L, AppContext> e : localEntries) {470final L listener = e.getKey();471final AppContext listenerContext = e.getValue();472SunToolkit.invokeLaterOnAppContext(listenerContext, new Runnable() {473public void run() {474performOnListener(listener, event);475}476});477}478}479480synchronized void addListener(final L listener) {481setListenerContext(listener, AppContext.getAppContext());482483if (!nativeListenerRegistered) {484registerNativeListener();485nativeListenerRegistered = true;486}487}488489synchronized void removeListener(final L listener) {490listenerToAppContext.remove(listener);491}492493abstract void performOnListener(L listener, final _NativeEvent event);494void registerNativeListener() { }495496private void setListenerContext(L listener, AppContext listenerContext) {497if (listenerContext == null) {498throw new RuntimeException(499"Attempting to add a listener from a thread group without AppContext");500}501listenerToAppContext.put(listener, AppContext.getAppContext());502}503}504505abstract class _BooleanAppEventMultiplexor<L, E> extends _AppEventMultiplexor<L> {506@Override507void performOnListener(L listener, final _NativeEvent event) {508final boolean isTrue = Boolean.TRUE.equals(event.get(0));509final E e = createEvent(isTrue);510if (isTrue) {511performTrueEventOn(listener, e);512} else {513performFalseEventOn(listener, e);514}515}516517abstract E createEvent(final boolean isTrue);518abstract void performTrueEventOn(final L listener, final E e);519abstract void performFalseEventOn(final L listener, final E e);520}521522/*523* Ensures that setting and obtaining an app event handler is done in524* both a thread-safe manner, and that user code is performed on the525* AWT EventQueue thread.526*527* Allows native to blindly lob new events into the dispatcher,528* knowing that they will only be dispatched once a handler is set.529*530* User code is not (and should not be) run under any synchronized lock.531*/532abstract class _AppEventDispatcher<H> {533H _handler;534AppContext handlerContext;535536// called from AppKit Thread-0537void dispatch(final _NativeEvent event) {538// grab a local ref to the handler539final H localHandler;540final AppContext localHandlerContext;541synchronized (_AppEventDispatcher.this) {542localHandler = _handler;543localHandlerContext = handlerContext;544}545546if (localHandler == null) {547performDefaultAction(event);548} else {549SunToolkit.invokeLaterOnAppContext(localHandlerContext, new Runnable() {550public void run() {551performUsing(localHandler, event);552}553});554}555}556557synchronized void setHandler(final H handler) {558this._handler = handler;559560setHandlerContext(AppContext.getAppContext());561562}563564void performDefaultAction(final _NativeEvent event) { } // by default, do nothing565abstract void performUsing(final H handler, final _NativeEvent event);566567protected void setHandlerContext(AppContext ctx) {568if (ctx == null) {569throw new RuntimeException(570"Attempting to set a handler from a thread group without AppContext");571}572573handlerContext = ctx;574}575}576577abstract class _QueuingAppEventDispatcher<H> extends _AppEventDispatcher<H> {578List<_NativeEvent> queuedEvents = new LinkedList<_NativeEvent>();579580@Override581void dispatch(final _NativeEvent event) {582synchronized (this) {583// dispatcher hasn't started yet584if (queuedEvents != null) {585queuedEvents.add(event);586return;587}588}589590super.dispatch(event);591}592593synchronized void setHandler(final H handler) {594this._handler = handler;595596setHandlerContext(AppContext.getAppContext());597598// dispatch any events in the queue599if (queuedEvents != null) {600// grab a local ref to the queue, so the real one can be nulled out601final java.util.List<_NativeEvent> localQueuedEvents = queuedEvents;602queuedEvents = null;603if (localQueuedEvents.size() != 0) {604for (final _NativeEvent arg : localQueuedEvents) {605dispatch(arg);606}607}608}609}610}611}612613614