2
pygame - Python Game Library
3
Copyright (C) 2009 Brian Fisher
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with this library; if not, write to the Free
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
/* Mac OS X functions to accommodate the fact SDLMain.m is not included */
24
#include <Carbon/Carbon.h>
25
#include <Foundation/Foundation.h>
26
#include <AppKit/NSApplication.h>
27
#include <AppKit/NSMenuItem.h>
28
#include <AppKit/NSMenu.h>
29
#include <AppKit/NSEvent.h>
30
#include <Foundation/NSData.h>
31
#include <AppKit/NSImage.h>
35
struct CPSProcessSerNum
40
typedef struct CPSProcessSerNum CPSProcessSerNum;
42
extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn);
43
extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
44
extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn);
45
extern OSErr CPSSetProcessName ( CPSProcessSerNum *psn, const char *processname );
47
static bool HasInstalledApplication = 0;
49
static NSString *getApplicationName(void)
51
const NSDictionary *dict;
52
NSString *appName = 0;
54
/* Determine the application name */
55
dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
57
appName = [dict objectForKey: @"CFBundleName"];
59
if (![appName length])
60
appName = [[NSProcessInfo processInfo] processName];
66
_WMEnable(PyObject* self)
70
const char* nameString;
71
NSString* nameNSString;
73
err = CPSGetCurrentProcess(&psn);
76
nameNSString = getApplicationName();
77
nameString = [nameNSString UTF8String];
78
CPSSetProcessName(&psn, nameString);
80
err = CPSEnableForegroundOperation(&psn,0x03,0x3C,0x2C,0x1103);
83
err = CPSSetFrontProcess(&psn);
86
return RAISE (PyExc_SDLError, "CPSSetFrontProcess failed");
91
return RAISE (PyExc_SDLError, "CPSEnableForegroundOperation failed");
96
return RAISE (PyExc_SDLError, "CPSGetCurrentProcess failed");
103
_RunningFromBundleWithNSApplication(PyObject* self)
105
if (HasInstalledApplication)
109
CFBundleRef MainBundle = CFBundleGetMainBundle();
110
if (MainBundle != NULL)
112
if (CFBundleGetDataPointerForName(MainBundle, CFSTR("NSApp")) != NULL)
120
//#############################################################################
121
// Defining the NSApplication class we will use
122
//#############################################################################
123
@interface SDLApplication : NSApplication
126
/* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
127
but the method still is there and works. To avoid warnings, we declare
128
it ourselves here. */
129
@interface NSApplication(SDL_Missing_Methods)
130
- (void)setAppleMenu:(NSMenu *)menu;
133
@implementation SDLApplication
134
/* Invoked from the Quit menu item */
135
- (void)terminate:(id)sender
138
event.type = SDL_QUIT;
139
SDL_PushEvent(&event);
143
static void setApplicationMenu(void)
146
NSMenuItem *menuItem;
150
appName = getApplicationName();
151
appleMenu = [[NSMenu alloc] initWithTitle:@""];
154
title = [@"About " stringByAppendingString:appName];
155
[appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
157
[appleMenu addItem:[NSMenuItem separatorItem]];
159
title = [@"Hide " stringByAppendingString:appName];
160
[appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
162
menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
163
[menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
165
[appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
167
[appleMenu addItem:[NSMenuItem separatorItem]];
169
title = [@"Quit " stringByAppendingString:appName];
170
[appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
173
menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
174
[menuItem setSubmenu:appleMenu];
175
[[NSApp mainMenu] addItem:menuItem];
177
[NSApp setAppleMenu:appleMenu];
183
static void setupWindowMenu(void)
186
NSMenuItem *windowMenuItem;
187
NSMenuItem *menuItem;
189
windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
191
menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
192
[windowMenu addItem:menuItem];
195
windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
196
[windowMenuItem setSubmenu:windowMenu];
197
[[NSApp mainMenu] addItem:windowMenuItem];
199
[NSApp setWindowsMenu:windowMenu];
201
[windowMenu release];
202
[windowMenuItem release];
206
_InstallNSApplication(PyObject* self, PyObject* arg)
208
char* icon_data = NULL;
211
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
213
[SDLApplication sharedApplication];
215
if (PyArg_ParseTuple (arg, "|z#", &icon_data, &data_len))
217
NSData *image_data = [NSData dataWithBytes:icon_data length:data_len];
218
NSImage *icon_img = [[NSImage alloc] initWithData:image_data];
219
if (icon_img != NULL)
221
[NSApp setApplicationIconImage:icon_img];
225
[NSApp setMainMenu:[[NSMenu alloc] init]];
226
setApplicationMenu();
229
[NSApp finishLaunching];
230
[NSApp updateWindows];
231
[NSApp activateIgnoringOtherApps:true];
233
HasInstalledApplication = 1;
238
static PyMethodDef macosx_builtins[] =
240
{ "WMEnable", (PyCFunction) _WMEnable, METH_NOARGS, "Enables Foreground Operation when Window Manager is not available" },
241
{ "RunningFromBundleWithNSApplication", (PyCFunction) _RunningFromBundleWithNSApplication, METH_NOARGS, "Returns true if we are running from an AppBundle with a variable named NSApp" },
242
{ "InstallNSApplication", _InstallNSApplication, METH_VARARGS, "Creates an NSApplication with the right behaviors for SDL" },
243
{ NULL, NULL, 0, NULL}
248
MODINIT_DEFINE (sdlmain_osx)
252
/* create the module */
255
static struct PyModuleDef _module = {
256
PyModuleDef_HEAD_INIT,
257
MODPREFIX "sdlmain_osx",
261
NULL, NULL, NULL, NULL
267
module = PyModule_Create (&_module);
269
module = Py_InitModule3 (MODPREFIX "sdlmain_osx", macosx_builtins, NULL);
273
/*imported needed apis*/
274
import_pygame_base ();
275
if (PyErr_Occurred ()) {
280
MODINIT_RETURN (module);