+#ifdef MAC_OSX
+void
+mac_check_bundle()
+{
+ extern int inhibit_window_system;
+ extern int noninteractive;
+ CFBundleRef appsBundle;
+ pid_t child;
+
+ /* No need to test if already -nw*/
+ if (inhibit_window_system || noninteractive)
+ return;
+
+ appsBundle = CFBundleGetMainBundle();
+ if (appsBundle != NULL)
+ {
+ CFStringRef cfBI = CFSTR("CFBundleIdentifier");
+ CFTypeRef res = CFBundleGetValueForInfoDictionaryKey(appsBundle, cfBI);
+ /* We found the bundle identifier, now we know we are valid. */
+ if (res != NULL)
+ {
+ CFRelease(res);
+ return;
+ }
+ }
+ /* MAC_TODO: Have this start the bundled executable */
+
+ /* For now, prevent the fatal error by bringing it up in the terminal */
+ inhibit_window_system = 1;
+}
+
+void
+MakeMeTheFrontProcess ()
+{
+ ProcessSerialNumber psn;
+ OSErr err;
+
+ err = GetCurrentProcess (&psn);
+ if (err == noErr)
+ (void) SetFrontProcess (&psn);
+}
+
+/***** Code to handle C-g testing *****/
+
+/* Contains the Mac modifier formed from quit_char */
+static mac_quit_char_modifiers = 0;
+static mac_quit_char_keycode;
+extern int quit_char;
+
+static void
+mac_determine_quit_char_modifiers()
+{
+ /* Todo: Determine modifiers from quit_char. */
+ UInt32 qc_modifiers = ctrl_modifier;
+
+ /* Map modifiers */
+ mac_quit_char_modifiers = 0;
+ if (qc_modifiers & ctrl_modifier) mac_quit_char_modifiers |= macCtrlKey;
+ if (qc_modifiers & shift_modifier) mac_quit_char_modifiers |= macShiftKey;
+ if (qc_modifiers & meta_modifier) mac_quit_char_modifiers |= macMetaKey;
+ if (qc_modifiers & alt_modifier) mac_quit_char_modifiers |= macAltKey;
+}
+
+static void
+init_quit_char_handler ()
+{
+ /* TODO: Let this support keys other the 'g' */
+ mac_quit_char_keycode = 5;
+ /* Look at <architecture/adb_kb_map.h> for details */
+ /* http://gemma.apple.com/techpubs/mac/Toolbox/Toolbox-40.html#MARKER-9-184*/
+
+ mac_determine_quit_char_modifiers();
+}
+
+static Boolean
+quit_char_comp (EventRef inEvent, void *inCompData)
+{
+ if (GetEventClass(inEvent) != kEventClassKeyboard)
+ return false;
+ if (GetEventKind(inEvent) != kEventRawKeyDown)
+ return false;
+ {
+ UInt32 keyCode;
+ UInt32 keyModifiers;
+ GetEventParameter(inEvent, kEventParamKeyCode,
+ typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode);
+ if (keyCode != mac_quit_char_keycode)
+ return false;
+ GetEventParameter(inEvent, kEventParamKeyModifiers,
+ typeUInt32, NULL, sizeof(UInt32), NULL, &keyModifiers);
+ if (keyModifiers != mac_quit_char_modifiers)
+ return false;
+ }
+ return true;
+}
+
+void
+mac_check_for_quit_char()
+{
+ EventRef event;
+ /* If windows are not initialized, return immediately (keep it bouncin')*/
+ if (!mac_quit_char_modifiers)
+ return;
+
+ /* Redetermine modifiers because they are based on lisp variables */
+ mac_determine_quit_char_modifiers();
+
+ /* Fill the queue with events */
+ ReceiveNextEvent (0, NULL, kEventDurationNoWait, false, &event);
+ event = FindSpecificEventInQueue (GetMainEventQueue(), quit_char_comp, NULL);
+ if (event)
+ {
+ struct input_event e;
+ struct mac_output *mwp = (mac_output*) GetWRefCon (FrontNonFloatingWindow ());
+ /* Use an input_event to emulate what the interrupt handler does. */
+ e.kind = ASCII_KEYSTROKE_EVENT;
+ e.code = quit_char;
+ e.arg = NULL;
+ e.modifiers = NULL;
+ e.timestamp = EventTimeToTicks(GetEventTime(event))*(1000/60);
+ XSETFRAME(e.frame_or_window, mwp->mFP);
+ /* Remove event from queue to prevent looping. */
+ RemoveEventFromQueue(GetMainEventQueue(), event);
+ ReleaseEvent(event);
+ kbd_buffer_store_event(&e);
+ }
+}
+
+#endif /* MAC_OSX */
+