From 347c54dbd3495d7414c6149c828d276efd232f7d Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 24 Jun 2008 17:50:12 +0000 Subject: [PATCH] better handling of quit events (underspecified@gmail) --- gui-osx/screen.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/gui-osx/screen.c b/gui-osx/screen.c index 1fe5a2d..876bfb9 100644 --- a/gui-osx/screen.c +++ b/gui-osx/screen.c @@ -111,6 +111,8 @@ void screeninit(void) ksleep(&rend, isready, 0); } +// No wonder Apple sells so many wide displays! +static OSStatus ApplicationQuitEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData); static OSStatus MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData); static OSStatus MainWindowCommandHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData); @@ -165,6 +167,9 @@ void winproc(void *a) if(PasteboardCreate(kPasteboardClipboard, &appleclip) != noErr) sysfatal("pasteboard create failed"); + const EventTypeSpec quit_events[] = { + { kEventClassApplication, kEventAppQuit } + }; const EventTypeSpec commands[] = { { kEventClassWindow, kEventWindowClosed }, { kEventClassWindow, kEventWindowBoundsChanged }, @@ -181,6 +186,13 @@ void winproc(void *a) { kEventClassMouse, kEventMouseWheelMoved }, }; + InstallApplicationEventHandler ( + NewEventHandlerUPP (ApplicationQuitEventHandler), + GetEventTypeCount(quit_events), + quit_events, + NULL, + NULL); + InstallApplicationEventHandler ( NewEventHandlerUPP (MainWindowEventHandler), GetEventTypeCount(events), @@ -328,6 +340,14 @@ full_screen() } } +// catch quit events to handle quits from menu, Cmd+Q, applescript, and task switcher +static OSStatus ApplicationQuitEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData) +{ + exit(0); +// QuitApplicationEventLoop(); + return noErr; +} + static OSStatus MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData) { OSStatus result = noErr; @@ -517,9 +537,21 @@ static OSStatus MainWindowCommandHandler(EventHandlerCallRef nextHandler, switch (kind) { case kEventWindowClosed: - theWindow = NULL; - exit(0); // only one window - break; + // send a quit carbon event instead of directly calling cleanexit + // so that all quits are done in ApplicationQuitEventHandler + { + EventRef quitEvent; + CreateEvent(NULL, + kEventClassApplication, + kEventAppQuit, + 0, + kEventAttributeNone, + &quitEvent); + EventTargetRef target; + target = GetApplicationEventTarget(); + SendEventToEventTarget(quitEvent, target); + } + break; //resize window case kEventWindowBoundsChanged: