-
Notifications
You must be signed in to change notification settings - Fork 1
/
chuck_ui_mac.mm
235 lines (168 loc) · 6.09 KB
/
chuck_ui_mac.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*----------------------------------------------------------------------------
ChucK Concurrent, On-the-fly Audio Programming Language
Compiler and Virtual Machine
Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
http://chuck.cs.princeton.edu/
http://soundlab.cs.princeton.edu/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
U.S.A.
-----------------------------------------------------------------------------*/
//-----------------------------------------------------------------------------
// file: chuck_ui_mac.mm
// desc: ui boilerplate for mac os x
//
// author: Spencer Salazar ([email protected])
// date: October 2011
//-----------------------------------------------------------------------------
#include "chuck_ui.h"
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
#import "chuck_vm.h"
#import "chuck_globals.h"
#import "chuck_errmsg.h"
#import "util_icon.h"
@class Chuck_UI_Controller;
static Chuck_UI_Manager * g_ui_manager = NULL;
static Chuck_UI_Controller * g_ui_controller = nil;
@interface Chuck_UI_Controller : NSObject<NSApplicationDelegate>
{
@private
Chuck_UI_Manager * uiManager;
}
- (void)quit;
@end
struct Chuck_UI_Manager::platform_data
{
platform_data() : m_app(nil) { }
NSApplication * m_app;
};
Chuck_UI_Manager * Chuck_UI_Manager::instance()
{
if(g_ui_manager == NULL)
g_ui_manager = new Chuck_UI_Manager;
return g_ui_manager;
}
Chuck_UI_Manager::Chuck_UI_Manager() : m_platformData(new platform_data)
{
init();
g_ui_controller = [Chuck_UI_Controller new];
}
void Chuck_UI_Manager::init()
{
m_hasStarted = false;
m_doStart = false;
m_doShutdown = false;
m_hasMainThreadInit = false;
m_hookActivated = false;
m_hook = NULL;
}
void Chuck_UI_Manager::main_thread_init()
{
if(![NSThread isMainThread])
{
EM_error3("chuck_ui: attempt to call main_thread_init from non-main thread!");
return;
}
if(m_hasMainThreadInit)
return;
EM_log(CK_LOG_INFO, "chuck_ui: initializing on main-thread");
m_hasMainThreadInit = true;
NSAutoreleasePool * temp_pool = [NSAutoreleasePool new];
NSApplication * app = [NSApplication sharedApplication];
[app setDelegate:g_ui_controller];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
NSImage * application_image = [[[NSImage alloc] initWithData:[NSData dataWithBytesNoCopy:(void *)miniAudicle_png
length:miniAudicle_png_length
freeWhenDone:NO]]
autorelease];
[app setApplicationIconImage:application_image];
NSMenu * mainMenu = [[[NSMenu alloc] initWithTitle:@"main menu"] autorelease];
NSMenu * chuckMenu = [[[NSMenu alloc] initWithTitle:@"chuck"] autorelease];
NSMenuItem * menuItem;
menuItem = [[[NSMenuItem new] initWithTitle:@"chuck"
action:NULL
keyEquivalent:@""]
autorelease];
[menuItem setSubmenu:chuckMenu];
[mainMenu addItem:menuItem];
menuItem = [[[NSMenuItem new] initWithTitle:@"About chuck"
action:NULL
keyEquivalent:@""]
autorelease];
[chuckMenu addItem:menuItem];
[chuckMenu addItem:[NSMenuItem separatorItem]];
menuItem = [[[NSMenuItem new] initWithTitle:@"Quit"
action:NULL
keyEquivalent:@"q"]
autorelease];
[menuItem setTarget:g_ui_controller];
[menuItem setAction:@selector(quit)];
[chuckMenu addItem:menuItem];
[app setMainMenu:mainMenu];
[app activateIgnoringOtherApps:YES];
m_platformData->m_app = app;
[temp_pool release];
}
void Chuck_UI_Manager::run()
{
while( !m_doStart && !m_doShutdown )
{
// TODO: semaphore?
usleep(10000);
}
if( m_doShutdown )
return;
// TODO: handle case where VM shuts down before this function finishes,
// which seems to cause crashes
NSAutoreleasePool * arpool = [NSAutoreleasePool new];
main_thread_init();
m_hasStarted = true;
m_doStart = false;
EM_log(CK_LOG_INFO, "running chuck_ui");
[[NSApplication sharedApplication] run];
[arpool release];
m_hook->deactivate(m_hook);
}
void Chuck_UI_Manager::start()
{
if( !m_hookActivated )
{
m_hook->activate(m_hook);
m_hookActivated = true;
}
if( !m_hasStarted )
{
// if main thread
if([NSThread isMainThread])
main_thread_init();
m_doStart = true;
}
}
void Chuck_UI_Manager::shutdown()
{
m_doShutdown = true;
if( m_hasStarted || m_doStart )
{
NSAutoreleasePool * arpool = [NSAutoreleasePool new];
[[NSApplication sharedApplication] performSelectorOnMainThread:@selector(terminate:)
withObject:NSApp
waitUntilDone:NO];
[arpool release];
}
}
@implementation Chuck_UI_Controller
- (void)quit
{
//if(g_vm) g_vm->shutdown(); // termination of vm will cause application exit
}
@end