3
'''Darwin (OS X) support.
5
Appropriated from pygame.macosx
8
__docformat__ = 'restructuredtext'
14
# SDL-ctypes on OS X requires PyObjC
15
from Foundation import *
20
import cocos.audio.SDL.dll
21
import cocos.audio.SDL.events
26
# Need to do this if not running with a nib
27
def setupAppleMenu(app):
28
appleMenuController = NSAppleMenuController.alloc().init()
29
appleMenuController.retain()
30
appleMenu = NSMenu.alloc().initWithTitle_('')
31
appleMenuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('', None, '')
32
appleMenuItem.setSubmenu_(appleMenu)
33
app.mainMenu().addItem_(appleMenuItem)
34
appleMenuController.controlMenu_(appleMenu)
35
app.mainMenu().removeItem_(appleMenuItem)
37
# Need to do this if not running with a nib
38
def setupWindowMenu(app):
39
windowMenu = NSMenu.alloc().initWithTitle_('Window')
41
menuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Minimize', 'performMiniaturize:', 'm')
42
windowMenu.addItem_(menuItem)
43
windowMenuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Window', None, '')
44
windowMenuItem.setSubmenu_(windowMenu)
45
app.mainMenu().addItem_(windowMenuItem)
46
app.setWindowsMenu_(windowMenu)
48
# Used to cleanly terminate
49
class SDLAppDelegate(NSObject):
50
def applicationShouldTerminate_(self, app):
51
event = SDL.events.SDL_Event()
53
SDL.events.SDL_PushEvent(event)
54
return NSTerminateLater
56
def windowUpdateNotification_(self, notification):
57
win = notification.object()
58
if not SDL.dll.version_compatible((1, 2, 8)) and isinstance(win, objc.lookUpClass('SDL_QuartzWindow')):
59
# Seems to be a retain count bug in SDL.. workaround!
61
NSNotificationCenter.defaultCenter().removeObserver_name_object_(
62
self, NSWindowDidUpdateNotification, None)
65
def setIcon(app, icon_data):
66
data = NSData.dataWithBytes_length_(icon_data, len(icon_data))
69
img = NSImage.alloc().initWithData_(data)
72
app.setApplicationIconImage_(img)
75
app = NSApplication.sharedApplication()
76
appDelegate = SDLAppDelegate.alloc().init()
77
app.setDelegate_(appDelegate)
79
NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(
81
'windowUpdateNotification:',
82
NSWindowDidUpdateNotification,
84
if not app.mainMenu():
85
mainMenu = NSMenu.alloc().init()
86
app.setMainMenu_(mainMenu)
91
app.activateIgnoringOtherApps_(True)
97
OUTPSN = 'o^{ProcessSerialNumber=LL}'
98
INPSN = 'n^{ProcessSerialNumber=LL}'
101
# These two are public API
102
( u'GetCurrentProcess', S(OSErr, OUTPSN) ),
103
( u'SetFrontProcess', S(OSErr, INPSN) ),
104
# This is undocumented SPI
105
( u'CPSSetProcessName', S(OSErr, INPSN, objc._C_CHARPTR) ),
106
( u'CPSEnableForegroundOperation', S(OSErr, INPSN) ),
109
def WMEnable(name=None):
111
name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
112
if isinstance(name, unicode):
113
name = name.encode('utf-8')
114
if not hasattr(objc, 'loadBundleFunctions'):
116
bndl = NSBundle.bundleWithPath_(objc.pathForFramework('/System/Library/Frameworks/ApplicationServices.framework'))
118
print >>sys.stderr, 'ApplicationServices missing'
121
app = NSApplication.sharedApplication()
122
objc.loadBundleFunctions(bndl, d, FUNCTIONS)
123
for (fn, sig) in FUNCTIONS:
125
print >>sys.stderr, 'Missing', fn
127
err, psn = d['GetCurrentProcess']()
129
print >>sys.stderr, 'GetCurrentProcess', (err, psn)
131
err = d['CPSSetProcessName'](psn, name)
133
print >>sys.stderr, 'CPSSetProcessName', (err, psn)
135
err = d['CPSEnableForegroundOperation'](psn)
137
print >>sys.stderr, 'CPSEnableForegroundOperation', (err, psn)
139
err = d['SetFrontProcess'](psn)
141
print >>sys.stderr, 'SetFrontProcess', (err, psn)
146
if not (MacOS.WMAvailable() or WMEnable()):
147
raise ImportError, "Can not access the window manager. Use py2app or execute with the pythonw script."
149
# running outside of a bundle
151
# running inside a bundle, change dir
152
if (os.getcwd() == '/') and len(sys.argv) > 1:
153
os.chdir(os.path.dirname(sys.argv[0]))