better handling of quit events (underspecified@gmail)

This commit is contained in:
Russ Cox 2008-06-24 17:50:12 +00:00
parent 6c519f3309
commit 347c54dbd3

View File

@ -111,6 +111,8 @@ void screeninit(void)
ksleep(&rend, isready, 0); 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 MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
static OSStatus MainWindowCommandHandler(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) if(PasteboardCreate(kPasteboardClipboard, &appleclip) != noErr)
sysfatal("pasteboard create failed"); sysfatal("pasteboard create failed");
const EventTypeSpec quit_events[] = {
{ kEventClassApplication, kEventAppQuit }
};
const EventTypeSpec commands[] = { const EventTypeSpec commands[] = {
{ kEventClassWindow, kEventWindowClosed }, { kEventClassWindow, kEventWindowClosed },
{ kEventClassWindow, kEventWindowBoundsChanged }, { kEventClassWindow, kEventWindowBoundsChanged },
@ -181,6 +186,13 @@ void winproc(void *a)
{ kEventClassMouse, kEventMouseWheelMoved }, { kEventClassMouse, kEventMouseWheelMoved },
}; };
InstallApplicationEventHandler (
NewEventHandlerUPP (ApplicationQuitEventHandler),
GetEventTypeCount(quit_events),
quit_events,
NULL,
NULL);
InstallApplicationEventHandler ( InstallApplicationEventHandler (
NewEventHandlerUPP (MainWindowEventHandler), NewEventHandlerUPP (MainWindowEventHandler),
GetEventTypeCount(events), 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) static OSStatus MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
{ {
OSStatus result = noErr; OSStatus result = noErr;
@ -517,9 +537,21 @@ static OSStatus MainWindowCommandHandler(EventHandlerCallRef nextHandler,
switch (kind) switch (kind)
{ {
case kEventWindowClosed: case kEventWindowClosed:
theWindow = NULL; // send a quit carbon event instead of directly calling cleanexit
exit(0); // only one window // so that all quits are done in ApplicationQuitEventHandler
break; {
EventRef quitEvent;
CreateEvent(NULL,
kEventClassApplication,
kEventAppQuit,
0,
kEventAttributeNone,
&quitEvent);
EventTargetRef target;
target = GetApplicationEventTarget();
SendEventToEventTarget(quitEvent, target);
}
break;
//resize window //resize window
case kEventWindowBoundsChanged: case kEventWindowBoundsChanged: