~facundo/enjuewemela/trunk

« back to all changes in this revision

Viewing changes to enjuewemela/cocos/cocos/audio/SDL/darwin.py

  • Committer: facundo at com
  • Date: 2010-11-20 01:42:31 UTC
  • mfrom: (62.1.3 lint-issues)
  • Revision ID: facundo@taniquetil.com.ar-20101120014231-b2tkyc3mwr84ggcc
Project reorder and lint issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
'''Darwin (OS X) support.
4
 
 
5
 
Appropriated from pygame.macosx
6
 
'''
7
 
 
8
 
__docformat__ = 'restructuredtext'
9
 
__version__ = '$Id: $'
10
 
 
11
 
import os
12
 
import sys
13
 
 
14
 
# SDL-ctypes on OS X requires PyObjC
15
 
from Foundation import *
16
 
from AppKit import *
17
 
import objc
18
 
import MacOS
19
 
 
20
 
import cocos.audio.SDL.dll
21
 
import cocos.audio.SDL.events
22
 
SDL = cocos.audio.SDL
23
 
 
24
 
__all__ = ['init']
25
 
 
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)
36
 
    
37
 
# Need to do this if not running with a nib
38
 
def setupWindowMenu(app):
39
 
    windowMenu = NSMenu.alloc().initWithTitle_('Window')
40
 
    windowMenu.retain()
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)
47
 
 
48
 
# Used to cleanly terminate
49
 
class SDLAppDelegate(NSObject):
50
 
    def applicationShouldTerminate_(self, app):
51
 
        event = SDL.events.SDL_Event()
52
 
        event.type = SDL_QUIT
53
 
        SDL.events.SDL_PushEvent(event)
54
 
        return NSTerminateLater
55
 
 
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!
60
 
            win.retain()
61
 
        NSNotificationCenter.defaultCenter().removeObserver_name_object_(
62
 
            self, NSWindowDidUpdateNotification, None)
63
 
        self.release()
64
 
 
65
 
def setIcon(app, icon_data):
66
 
    data = NSData.dataWithBytes_length_(icon_data, len(icon_data))
67
 
    if data is None:
68
 
        return
69
 
    img = NSImage.alloc().initWithData_(data)
70
 
    if img is None:
71
 
        return
72
 
    app.setApplicationIconImage_(img)
73
 
 
74
 
def install():
75
 
    app = NSApplication.sharedApplication()
76
 
    appDelegate = SDLAppDelegate.alloc().init()
77
 
    app.setDelegate_(appDelegate)
78
 
    appDelegate.retain()
79
 
    NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(
80
 
        appDelegate,
81
 
        'windowUpdateNotification:',
82
 
        NSWindowDidUpdateNotification,
83
 
        None)
84
 
    if not app.mainMenu():
85
 
        mainMenu = NSMenu.alloc().init()
86
 
        app.setMainMenu_(mainMenu)
87
 
        setupAppleMenu(app)
88
 
        setupWindowMenu(app)
89
 
    app.finishLaunching()
90
 
    app.updateWindows()
91
 
    app.activateIgnoringOtherApps_(True)
92
 
 
93
 
def S(*args):
94
 
    return ''.join(args)
95
 
 
96
 
OSErr = objc._C_SHT
97
 
OUTPSN = 'o^{ProcessSerialNumber=LL}'
98
 
INPSN = 'n^{ProcessSerialNumber=LL}'
99
 
 
100
 
FUNCTIONS=[
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) ),
107
 
]
108
 
 
109
 
def WMEnable(name=None):
110
 
    if name is 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'):
115
 
        return False
116
 
    bndl = NSBundle.bundleWithPath_(objc.pathForFramework('/System/Library/Frameworks/ApplicationServices.framework'))
117
 
    if bndl is None:
118
 
        print >>sys.stderr, 'ApplicationServices missing'
119
 
        return False
120
 
    d = {}
121
 
    app = NSApplication.sharedApplication()
122
 
    objc.loadBundleFunctions(bndl, d, FUNCTIONS)
123
 
    for (fn, sig) in FUNCTIONS:
124
 
        if fn not in d:
125
 
            print >>sys.stderr, 'Missing', fn
126
 
            return False
127
 
    err, psn = d['GetCurrentProcess']()
128
 
    if err:
129
 
        print >>sys.stderr, 'GetCurrentProcess', (err, psn)
130
 
        return False
131
 
    err = d['CPSSetProcessName'](psn, name)
132
 
    if err:
133
 
        print >>sys.stderr, 'CPSSetProcessName', (err, psn)
134
 
        return False
135
 
    err = d['CPSEnableForegroundOperation'](psn)
136
 
    if err:
137
 
        print >>sys.stderr, 'CPSEnableForegroundOperation', (err, psn)
138
 
        return False
139
 
    err = d['SetFrontProcess'](psn)
140
 
    if err:
141
 
        print >>sys.stderr, 'SetFrontProcess', (err, psn)
142
 
        return False
143
 
    return True
144
 
 
145
 
def init():
146
 
    if not (MacOS.WMAvailable() or WMEnable()):
147
 
        raise ImportError, "Can not access the window manager.  Use py2app or execute with the pythonw script."
148
 
    if not NSApp():
149
 
        # running outside of a bundle
150
 
        install()
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]))
154
 
    return True