19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__APPLE__) && defined(__MACH__)
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Carbon/Carbon.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Carbon.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Script.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <LowMem.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Devices.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <DiskInit.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <ToolUtils.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_events.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_syswm.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_sysevents.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_cursor_c.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_macevents_c.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mackeys.h"
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_macmouse_c.h"
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Define this to be able to collapse SDL windows.
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define USE_APPEARANCE_MANAGER
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Macintosh resource constants */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define mApple	128			/* Apple menu resource */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define iAbout	1			/* About menu item */
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions to handle the About menu */
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_DoAppleMenu(_THIS, long item);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The translation table from a macintosh key scancode to a SDL keysym */
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDLKey MAC_keymap[256];
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_keysym *TranslateKey(int scancode, int modifiers,
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                SDL_keysym *keysym, int pressed);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Handle activation and deactivation  -- returns whether an event was posted */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Mac_HandleActivate(int activate)
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( activate ) {
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Show the current SDL application cursor */
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetCursor(NULL);
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* put our mask back case it changed during context switch */
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetEventMask(everyEvent & ~autoKeyMask);
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if TARGET_API_MAC_CARBON
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{ Cursor cursor;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetCursor(GetQDGlobalsArrow(&cursor));
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetCursor(&theQD->arrow);
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! Mac_cursor_showing ) {
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ShowCursor();
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Mac_cursor_showing = 1;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_PrivateAppActive(activate, SDL_APPINPUTFOCUS));
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void myGlobalToLocal(_THIS, Point *pt)
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_VideoSurface && !(SDL_VideoSurface->flags&SDL_FULLSCREEN) ) {
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GrafPtr saveport;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GetPort(&saveport);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if TARGET_API_MAC_CARBON
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetPort(GetWindowPort(SDL_Window));
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetPort(SDL_Window);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GlobalToLocal(pt);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetPort(saveport);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The main MacOS event handler */
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Mac_HandleEvents(_THIS, int wait4it)
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static int mouse_button = 1;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	EventRecord event;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if TARGET_API_MAC_CARBON
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* There's no GetOSEvent() in the Carbon API. *sigh* */
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define cooperative_multitasking 1
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int cooperative_multitasking;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we're running fullscreen, we can hog the MacOS events,
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   otherwise we had better play nicely with the other apps.
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->screen && (this->screen->flags & SDL_FULLSCREEN) ) {
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cooperative_multitasking = 0;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cooperative_multitasking = 1;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we call WaitNextEvent(), MacOS will check other processes
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * and allow them to run, and perform other high-level processing.
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cooperative_multitasking || wait4it ) {
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		UInt32 wait_time;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Are we polling or not? */
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( wait4it ) {
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wait_time = 2147483647;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wait_time = 0;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		WaitNextEvent(everyEvent, &event, wait_time, nil);
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if ! TARGET_API_MAC_CARBON
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GetOSEvent(everyEvent, &event);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if TARGET_API_MAC_CARBON
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* for some reason, event.where isn't set ? */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GetGlobalMouse ( &event.where );
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check for mouse motion */
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (event.where.h != last_where.h) ||
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (event.where.v != last_where.v) ) {
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Point pt;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pt = last_where = event.where;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		myGlobalToLocal(this, &pt);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_PrivateMouseMotion(0, 0, pt.h, pt.v);
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check the current state of the keyboard */
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		KeyMap keys;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const Uint32 *keysptr = (Uint32 *) &keys;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const Uint32 *last_keysptr = (Uint32 *) &last_keys;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Check for special non-event keys */
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( event.modifiers != last_mods ) {
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			static struct {
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				EventModifiers mask;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDLKey key;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} mods[] = {
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{ alphaLock,		SDLK_CAPSLOCK },
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0 /* These are handled below in the GetKeys() code */
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{ cmdKey,		SDLK_LMETA },
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{ shiftKey,		SDLK_LSHIFT },
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{ rightShiftKey,	SDLK_RSHIFT },
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{ optionKey,		SDLK_LALT },
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{ rightOptionKey,	SDLK_RALT },
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{ controlKey,		SDLK_LCTRL },
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{ rightControlKey,	SDLK_RCTRL },
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* 0 */
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{ 0,			0 }
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			};
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_keysym keysym;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint8 mode;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			EventModifiers mod, mask;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Set up the keyboard event */
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.scancode = 0;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.sym = SDLK_UNKNOWN;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.mod = KMOD_NONE;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.unicode = 0;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* See what has changed, and generate events */
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mod = event.modifiers;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; mods[i].mask; ++i ) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mask = mods[i].mask;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( (mod&mask) != (last_mods&mask) ) {
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					keysym.sym = mods[i].key;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( (mod&mask) ||
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					     (mods[i].key == SDLK_CAPSLOCK) ) {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						mode = SDL_PRESSED;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					} else {
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						mode = SDL_RELEASED;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_PrivateKeyboard(mode, &keysym);
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Save state for next time */
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			last_mods = mod;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Check for normal event keys, but we have to scan the
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   actual keyboard state because on Mac OS X a keydown event
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   is immediately followed by a keyup event.
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GetKeys(keys);
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (keysptr[0] != last_keysptr[0]) ||
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (keysptr[1] != last_keysptr[1]) ||
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (keysptr[2] != last_keysptr[2]) ||
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (keysptr[3] != last_keysptr[3]) ) {
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_keysym keysym;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int old_bit, new_bit;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_KEYBOARD
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(sterr, "New keys: 0x%x 0x%x 0x%x 0x%x\n",
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				new_keys[0], new_keys[1],
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				new_keys[2], new_keys[3]);
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; i<128; ++i ) {
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				old_bit = (((Uint8 *)last_keys)[i/8]>>(i%8)) & 0x01;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				new_bit = (((Uint8 *)keys)[i/8]>>(i%8)) & 0x01;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( old_bit != new_bit ) {
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Post the keyboard event */
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_KEYBOARD
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					fprintf(stderr,"Scancode: 0x%2.2X\n",i);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_PrivateKeyboard(new_bit,
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				            TranslateKey(i, event.modifiers,
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				                         &keysym, new_bit));
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Save state for next time */
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			last_keys[0] = keys[0];
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			last_keys[1] = keys[1];
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			last_keys[2] = keys[2];
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			last_keys[3] = keys[3];
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Handle normal events */
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (event.what) {
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  case mouseDown: {
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		WindowRef win;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		short area;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		area = FindWindow(event.where, &win);
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Support switching between the SIOUX console
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   and SDL_Window by clicking in the window.
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( win && (win != FrontWindow()) ) {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SelectWindow(win);
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (area) {
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case inMenuBar: /* Only the apple menu exists */
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Mac_DoAppleMenu(this, MenuSelect(event.where));
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HiliteMenu(0);
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case inDrag:
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if TARGET_API_MAC_CARBON
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DragWindow(win, event.where, NULL);
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DragWindow(win, event.where, &theQD->screenBits.bounds);
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case inGoAway:
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( TrackGoAway(win, event.where) ) {
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateQuit();
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case inContent:
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			myGlobalToLocal(this, &event.where);
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Treat command-click as right mouse button */
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( event.modifiers & optionKey ) {
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mouse_button = 2;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else if ( event.modifiers & cmdKey ) {
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mouse_button = 3;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mouse_button = 1;
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateMouseButton(SDL_PRESSED,
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mouse_button, event.where.h, event.where.v);
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case inGrow: {
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int newSize;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Don't allow resize if video mode isn't resizable */
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( ! SDL_PublicSurface ||
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     ! (SDL_PublicSurface->flags & SDL_RESIZABLE) ) {
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if TARGET_API_MAC_CARBON
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			newSize = GrowWindow(win, event.where, NULL);
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			newSize = GrowWindow(win, event.where, &theQD->screenBits.bounds);
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( newSize ) {
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !TARGET_API_MAC_CARBON
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				EraseRect ( &theQD->screenBits.bounds );
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SizeWindow ( win, LoWord (newSize), HiWord (newSize), 1 );
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateResize ( LoWord (newSize), HiWord (newSize) );
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  	} break;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case inZoomIn:
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case inZoomOut:
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( TrackBox (win, event.where, area )) {
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Rect rect;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !TARGET_API_MAC_CARBON
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				EraseRect ( &theQD->screenBits.bounds );
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				ZoomWindow ( win, area, 0);
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( area == inZoomIn ) {
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GetWindowUserState(SDL_Window, &rect);
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GetWindowStandardState(SDL_Window, &rect);
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateResize (rect.right-rect.left,
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				                   rect.bottom-rect.top);
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if TARGET_API_MAC_CARBON
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case inCollapseBox:
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( TrackBox (win, event.where, area )) {
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( IsWindowCollapsable(win) ) {
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					CollapseWindow (win, !IsWindowCollapsed(win));
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* There should be something done like in inGrow case, but... */
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* TARGET_API_MAC_CARBON */
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case inSysWindow:
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if TARGET_API_MAC_CARBON
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Never happens in Carbon? */
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SystemClick(&event, win);
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  default:
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  break;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  case mouseUp: {
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		myGlobalToLocal(this, &event.where);
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Release the mouse button we simulated in the last press.
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   The drawback of this methos is we cannot press more than
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   one button. However, this doesn't matter, since there is
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   only a single logical mouse button, even if you have a
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   multi-button mouse, this doesn't matter at all.
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_PrivateMouseButton(SDL_RELEASED,
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_button, event.where.h, event.where.v);
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  break;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0 /* Handled above the switch statement */
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  case keyDown: {
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_keysym keysym;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_PrivateKeyboard(SDL_PRESSED,
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			TranslateKey((event.message&keyCodeMask)>>8
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                     event.modifiers, &keysym, 1));
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  break;
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  case keyUp: {
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_keysym keysym;
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_PrivateKeyboard(SDL_RELEASED,
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			TranslateKey((event.message&keyCodeMask)>>8
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                     event.modifiers, &keysym, 0));
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  break;
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  case updateEvt: {
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BeginUpdate(SDL_Window);
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	#if SDL_VIDEO_OPENGL
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (SDL_VideoSurface->flags & SDL_OPENGL)
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_GL_SwapBuffers();
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	#endif
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (SDL_VideoSurface->flags & SDL_HWSURFACE) ==
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						SDL_SWSURFACE ) {
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_UpdateRect(SDL_VideoSurface, 0, 0, 0, 0);
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		EndUpdate(SDL_Window);
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  /* If this was an update event for the SIOUX console, we return 0
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             in order to stop an endless series of updates being triggered.
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  */
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  if ( (WindowRef) event.message != SDL_Window ) {
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  break;
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  case activateEvt: {
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Mac_HandleActivate(!!(event.modifiers & activeFlag));
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  break;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  case diskEvt: {
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if TARGET_API_MAC_CARBON
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* What are we supposed to do? */;
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ((event.message>>16)&0xFFFF) != noErr ) {
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Point spot;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetPt(&spot, 0x0070, 0x0050);
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DIBadMount(spot, event.message);
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  break;
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  case osEvt: {
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch ((event.message>>24) & 0xFF) {
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0 /* Handled above the switch statement */
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case mouseMovedMessage: {
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			myGlobalToLocal(this, &event.where);
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateMouseMotion(0, 0,
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					event.where.h, event.where.v);
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  }
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  break;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* 0 */
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  case suspendResumeMessage: {
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Mac_HandleActivate(!!(event.message & resumeFlag));
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  }
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  break;
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  break;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  default: {
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		;
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  break;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (event.what != nullEvent);
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Mac_PumpEvents(_THIS)
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Process pending MacOS events */
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( Mac_HandleEvents(this, 0) ) {
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Loop and check again */;
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Mac_InitOSKeymap(_THIS)
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const void *KCHRPtr;
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UInt32 state;
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UInt32 value;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int world = SDLK_WORLD_0;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Map the MAC keysyms */
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_arraysize(MAC_keymap); ++i )
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		MAC_keymap[i] = SDLK_UNKNOWN;
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Defined MAC_* constants */
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_ESCAPE] = SDLK_ESCAPE;
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F1] = SDLK_F1;
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F2] = SDLK_F2;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F3] = SDLK_F3;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F4] = SDLK_F4;
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F5] = SDLK_F5;
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F6] = SDLK_F6;
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F7] = SDLK_F7;
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F8] = SDLK_F8;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F9] = SDLK_F9;
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F10] = SDLK_F10;
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F11] = SDLK_F11;
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_F12] = SDLK_F12;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_PRINT] = SDLK_PRINT;
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_SCROLLOCK] = SDLK_SCROLLOCK;
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_PAUSE] = SDLK_PAUSE;
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_POWER] = SDLK_POWER;
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_BACKQUOTE] = SDLK_BACKQUOTE;
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_1] = SDLK_1;
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_2] = SDLK_2;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_3] = SDLK_3;
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_4] = SDLK_4;
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_5] = SDLK_5;
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_6] = SDLK_6;
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_7] = SDLK_7;
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_8] = SDLK_8;
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_9] = SDLK_9;
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_0] = SDLK_0;
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_MINUS] = SDLK_MINUS;
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_EQUALS] = SDLK_EQUALS;
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_BACKSPACE] = SDLK_BACKSPACE;
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_INSERT] = SDLK_INSERT;
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_HOME] = SDLK_HOME;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_PAGEUP] = SDLK_PAGEUP;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_NUMLOCK] = SDLK_NUMLOCK;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_EQUALS] = SDLK_KP_EQUALS;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_DIVIDE] = SDLK_KP_DIVIDE;
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_MULTIPLY] = SDLK_KP_MULTIPLY;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_TAB] = SDLK_TAB;
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_q] = SDLK_q;
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_w] = SDLK_w;
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_e] = SDLK_e;
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_r] = SDLK_r;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_t] = SDLK_t;
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_y] = SDLK_y;
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_u] = SDLK_u;
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_i] = SDLK_i;
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_o] = SDLK_o;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_p] = SDLK_p;
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_LEFTBRACKET] = SDLK_LEFTBRACKET;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_RIGHTBRACKET] = SDLK_RIGHTBRACKET;
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_BACKSLASH] = SDLK_BACKSLASH;
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_DELETE] = SDLK_DELETE;
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_END] = SDLK_END;
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_PAGEDOWN] = SDLK_PAGEDOWN;
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP7] = SDLK_KP7;
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP8] = SDLK_KP8;
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP9] = SDLK_KP9;
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_MINUS] = SDLK_KP_MINUS;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_CAPSLOCK] = SDLK_CAPSLOCK;
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_a] = SDLK_a;
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_s] = SDLK_s;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_d] = SDLK_d;
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_f] = SDLK_f;
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_g] = SDLK_g;
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_h] = SDLK_h;
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_j] = SDLK_j;
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_k] = SDLK_k;
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_l] = SDLK_l;
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_SEMICOLON] = SDLK_SEMICOLON;
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_QUOTE] = SDLK_QUOTE;
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_RETURN] = SDLK_RETURN;
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP4] = SDLK_KP4;
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP5] = SDLK_KP5;
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP6] = SDLK_KP6;
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_PLUS] = SDLK_KP_PLUS;
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_LSHIFT] = SDLK_LSHIFT;
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_z] = SDLK_z;
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_x] = SDLK_x;
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_c] = SDLK_c;
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_v] = SDLK_v;
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_b] = SDLK_b;
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_n] = SDLK_n;
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_m] = SDLK_m;
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_COMMA] = SDLK_COMMA;
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_PERIOD] = SDLK_PERIOD;
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_SLASH] = SDLK_SLASH;
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0	/* These are the same as the left versions - use left by default */
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_RSHIFT] = SDLK_RSHIFT;
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_UP] = SDLK_UP;
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP1] = SDLK_KP1;
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP2] = SDLK_KP2;
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP3] = SDLK_KP3;
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_ENTER] = SDLK_KP_ENTER;
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_LCTRL] = SDLK_LCTRL;
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_LALT] = SDLK_LALT;
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_LMETA] = SDLK_LMETA;
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_SPACE] = SDLK_SPACE;
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0	/* These are the same as the left versions - use left by default */
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_RMETA] = SDLK_RMETA;
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_RALT] = SDLK_RALT;
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_RCTRL] = SDLK_RCTRL;
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_LEFT] = SDLK_LEFT;
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_DOWN] = SDLK_DOWN;
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_RIGHT] = SDLK_RIGHT;
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP0] = SDLK_KP0;
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_PERIOD] = SDLK_KP_PERIOD;
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__APPLE__) && defined(__MACH__)
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wierd, these keys are on my iBook under Mac OS X
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   Note that the left arrow keysym is the same as left ctrl!?
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_IBOOK_ENTER] = SDLK_KP_ENTER;
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_IBOOK_RIGHT] = SDLK_RIGHT;
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_IBOOK_DOWN] = SDLK_DOWN;
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_IBOOK_UP] = SDLK_UP;
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_IBOOK_LEFT] = SDLK_LEFT;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* Mac OS X */
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Up there we setup a static scancode->keysym map. However, it will not
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * work very well on international keyboard. Hence we now query MacOS
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * for its own keymap to adjust our own mapping table. However, this is
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * bascially only useful for ascii char keys. This is also the reason
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * why we keep the static table, too.
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get a pointer to the systems cached KCHR */
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	KCHRPtr = (void *)GetScriptManagerVariable(smKCHRCache);
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (KCHRPtr)
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Loop over all 127 possible scan codes */
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (i = 0; i < 0x7F; i++)
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* We pretend a clean start to begin with (i.e. no dead keys active */
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			state = 0;
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Now translate the key code to a key value */
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			value = KeyTranslate(KCHRPtr, i, &state) & 0xff;
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* If the state become 0, it was a dead key. We need to translate again,
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			passing in the new state, to get the actual key value */
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (state != 0)
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				value = KeyTranslate(KCHRPtr, i, &state) & 0xff;
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Now we should have an ascii value, or 0. Try to figure out to which SDL symbol it maps */
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (value >= 128)	 /* Some non-ASCII char, map it to SDLK_WORLD_* */
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				MAC_keymap[i] = world++;
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else if (value >= 32)	 /* non-control ASCII char */
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				MAC_keymap[i] = value;
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The keypad codes are re-setup here, because the loop above cannot
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * distinguish between a key on the keypad and a regular key. We maybe
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * could get around this problem in another fashion: NSEvent's flags
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * include a "NSNumericPadKeyMask" bit; we could check that and modify
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * the symbol we return on the fly. However, this flag seems to exhibit
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * some weird behaviour related to the num lock key
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP0] = SDLK_KP0;
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP1] = SDLK_KP1;
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP2] = SDLK_KP2;
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP3] = SDLK_KP3;
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP4] = SDLK_KP4;
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP5] = SDLK_KP5;
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP6] = SDLK_KP6;
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP7] = SDLK_KP7;
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP8] = SDLK_KP8;
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP9] = SDLK_KP9;
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_MINUS] = SDLK_KP_MINUS;
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_PLUS] = SDLK_KP_PLUS;
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_PERIOD] = SDLK_KP_PERIOD;
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_EQUALS] = SDLK_KP_EQUALS;
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_DIVIDE] = SDLK_KP_DIVIDE;
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_MULTIPLY] = SDLK_KP_MULTIPLY;
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MAC_keymap[MK_KP_ENTER] = SDLK_KP_ENTER;
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_keysym *TranslateKey(int scancode, int modifiers,
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                SDL_keysym *keysym, int pressed)
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the keysym information */
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	keysym->scancode = scancode;
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	keysym->sym = MAC_keymap[keysym->scancode];
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	keysym->mod = KMOD_NONE;
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	keysym->unicode = 0;
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( pressed && SDL_TranslateUNICODE ) {
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		static unsigned long state = 0;
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		static Ptr keymap = nil;
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Ptr new_keymap;
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Get the current keyboard map resource */
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		new_keymap = (Ptr)GetScriptManagerVariable(smKCHRCache);
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( new_keymap != keymap ) {
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keymap = new_keymap;
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			state = 0;
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		keysym->unicode = KeyTranslate(keymap,
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym->scancode|modifiers, &state) & 0xFFFF;
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(keysym);
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Mac_InitEvents(_THIS)
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create apple menu bar */
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	apple_menu = GetMenu(mApple);
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( apple_menu != nil ) {
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		AppendResMenu(apple_menu, 'DRVR');
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		InsertMenu(apple_menu, 0);
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DrawMenuBar();
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get rid of spurious events at startup */
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FlushEvents(everyEvent, 0);
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allow every event but keyrepeat */
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SetEventMask(everyEvent & ~autoKeyMask);
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Mac_QuitEvents(_THIS)
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ClearMenuBar();
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( apple_menu != nil ) {
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ReleaseResource((char **)apple_menu);
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clean up pending events */
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FlushEvents(everyEvent, 0);
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_DoAppleMenu(_THIS, long choice)
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !TARGET_API_MAC_CARBON  /* No Apple menu in OS X */
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short menu, item;
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	item = (choice&0xFFFF);
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	choice >>= 16;
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	menu = (choice&0xFFFF);
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (menu) {
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case mApple: {
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch (item) {
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				case iAbout: {
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Run the about box */;
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				default: {
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Str255 name;
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GetMenuItemText(apple_menu, item, name);
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					OpenDeskAcc(name);
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default: {
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Ignore other menus */;
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !TARGET_API_MAC_CARBON */
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !TARGET_API_MAC_CARBON
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Since we don't initialize QuickDraw, we need to get a pointer to qd */
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct QDGlobals *theQD = NULL;
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Exported to the macmain code */
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_InitQuickDraw(struct QDGlobals *the_qd)
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !TARGET_API_MAC_CARBON
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	theQD = the_qd;
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
747