Blame


1 113867b8 2009-09-29 jas #define Point OSXPoint
2 113867b8 2009-09-29 jas #define Rect OSXRect
3 113867b8 2009-09-29 jas #define Cursor OSXCursor
4 113867b8 2009-09-29 jas #import "osx-delegate.h"
5 113867b8 2009-09-29 jas #import <Foundation/Foundation.h>
6 113867b8 2009-09-29 jas #import <AppKit/AppKit.h>
7 113867b8 2009-09-29 jas #undef Cursor
8 113867b8 2009-09-29 jas #undef Rect
9 113867b8 2009-09-29 jas #undef Point
10 113867b8 2009-09-29 jas
11 113867b8 2009-09-29 jas #include <u.h>
12 113867b8 2009-09-29 jas #include <errno.h>
13 113867b8 2009-09-29 jas #include <sys/select.h>
14 113867b8 2009-09-29 jas #include <libc.h>
15 113867b8 2009-09-29 jas #include <draw.h>
16 113867b8 2009-09-29 jas #include <memdraw.h>
17 113867b8 2009-09-29 jas #include <memlayer.h>
18 113867b8 2009-09-29 jas #include <keyboard.h>
19 113867b8 2009-09-29 jas #include <mouse.h>
20 113867b8 2009-09-29 jas #include <cursor.h>
21 113867b8 2009-09-29 jas #include <drawfcall.h>
22 113867b8 2009-09-29 jas
23 113867b8 2009-09-29 jas AUTOFRAMEWORK(Foundation)
24 113867b8 2009-09-29 jas AUTOFRAMEWORK(AppKit)
25 113867b8 2009-09-29 jas
26 113867b8 2009-09-29 jas extern int trace;
27 113867b8 2009-09-29 jas
28 113867b8 2009-09-29 jas extern void fullscreen(int);
29 113867b8 2009-09-29 jas extern void kbdevent(NSEvent *event);
30 113867b8 2009-09-29 jas extern void mouseevent(NSEvent *event);
31 113867b8 2009-09-29 jas extern void eresized(int);
32 113867b8 2009-09-29 jas
33 113867b8 2009-09-29 jas extern void runmsg(Wsysmsg *m);
34 113867b8 2009-09-29 jas extern void seticon();
35 113867b8 2009-09-29 jas
36 113867b8 2009-09-29 jas @implementation DevdrawDelegate
37 113867b8 2009-09-29 jas +(void)populateMainMenu
38 113867b8 2009-09-29 jas {
39 113867b8 2009-09-29 jas NSMenu *mainMenu = [[NSMenu alloc] initWithTitle:@"MainMenu"];
40 113867b8 2009-09-29 jas NSMenuItem *menuItem;
41 113867b8 2009-09-29 jas NSMenu *submenu;
42 113867b8 2009-09-29 jas
43 113867b8 2009-09-29 jas menuItem = [mainMenu addItemWithTitle:@"Apple" action:NULL keyEquivalent:@""];
44 113867b8 2009-09-29 jas submenu = [[NSMenu alloc] initWithTitle:@"Apple"];
45 113867b8 2009-09-29 jas [NSApp performSelector:@selector(setAppleMenu:) withObject:submenu];
46 113867b8 2009-09-29 jas [self populateApplicationMenu:submenu];
47 113867b8 2009-09-29 jas [mainMenu setSubmenu:submenu forItem:menuItem];
48 113867b8 2009-09-29 jas
49 113867b8 2009-09-29 jas menuItem = [mainMenu addItemWithTitle:@"View" action:NULL keyEquivalent:@""];
50 113867b8 2009-09-29 jas submenu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"View", "@The View menu")];
51 113867b8 2009-09-29 jas [self populateViewMenu:submenu];
52 113867b8 2009-09-29 jas [mainMenu setSubmenu:submenu forItem:menuItem];
53 113867b8 2009-09-29 jas
54 113867b8 2009-09-29 jas menuItem = [mainMenu addItemWithTitle:@"Window" action:NULL keyEquivalent:@""];
55 113867b8 2009-09-29 jas submenu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Window", @"The Window menu")];
56 113867b8 2009-09-29 jas [self populateWindowMenu:submenu];
57 113867b8 2009-09-29 jas [mainMenu setSubmenu:submenu forItem:menuItem];
58 113867b8 2009-09-29 jas [NSApp setWindowsMenu:submenu];
59 113867b8 2009-09-29 jas
60 113867b8 2009-09-29 jas menuItem = [mainMenu addItemWithTitle:@"Help" action:NULL keyEquivalent:@""];
61 113867b8 2009-09-29 jas submenu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Help", @"The Help menu")];
62 113867b8 2009-09-29 jas [self populateHelpMenu:submenu];
63 113867b8 2009-09-29 jas [mainMenu setSubmenu:submenu forItem:menuItem];
64 113867b8 2009-09-29 jas
65 113867b8 2009-09-29 jas [NSApp setMainMenu:mainMenu];
66 113867b8 2009-09-29 jas }
67 113867b8 2009-09-29 jas
68 113867b8 2009-09-29 jas +(void)populateApplicationMenu:(NSMenu *)aMenu
69 113867b8 2009-09-29 jas {
70 113867b8 2009-09-29 jas NSString *applicationName = [[NSProcessInfo processInfo] processName];
71 113867b8 2009-09-29 jas NSMenuItem *menuItem;
72 113867b8 2009-09-29 jas
73 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"About", nil), applicationName]
74 113867b8 2009-09-29 jas action:@selector(orderFrontStandardAboutPanel:)
75 113867b8 2009-09-29 jas keyEquivalent:@""];
76 113867b8 2009-09-29 jas [menuItem setTarget:NSApp];
77 113867b8 2009-09-29 jas
78 113867b8 2009-09-29 jas [aMenu addItem:[NSMenuItem separatorItem]];
79 113867b8 2009-09-29 jas
80 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Preferences...", nil)
81 113867b8 2009-09-29 jas action:NULL
82 113867b8 2009-09-29 jas keyEquivalent:@","];
83 113867b8 2009-09-29 jas
84 113867b8 2009-09-29 jas [aMenu addItem:[NSMenuItem separatorItem]];
85 113867b8 2009-09-29 jas
86 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Services", nil)
87 113867b8 2009-09-29 jas action:NULL
88 113867b8 2009-09-29 jas keyEquivalent:@""];
89 113867b8 2009-09-29 jas NSMenu * servicesMenu = [[NSMenu alloc] initWithTitle:@"Services"];
90 113867b8 2009-09-29 jas [aMenu setSubmenu:servicesMenu forItem:menuItem];
91 113867b8 2009-09-29 jas [NSApp setServicesMenu:servicesMenu];
92 113867b8 2009-09-29 jas
93 113867b8 2009-09-29 jas [aMenu addItem:[NSMenuItem separatorItem]];
94 113867b8 2009-09-29 jas
95 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Hide", nil), applicationName]
96 113867b8 2009-09-29 jas action:@selector(hide:)
97 113867b8 2009-09-29 jas keyEquivalent:@"h"];
98 113867b8 2009-09-29 jas [menuItem setTarget:NSApp];
99 113867b8 2009-09-29 jas
100 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Hide Others", nil)
101 113867b8 2009-09-29 jas action:@selector(hideOtherApplications:)
102 113867b8 2009-09-29 jas keyEquivalent:@"h"];
103 113867b8 2009-09-29 jas [menuItem setKeyEquivalentModifierMask:NSCommandKeyMask | NSAlternateKeyMask];
104 113867b8 2009-09-29 jas [menuItem setTarget:NSApp];
105 113867b8 2009-09-29 jas
106 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Show All", nil)
107 113867b8 2009-09-29 jas action:@selector(unhideAllApplications:)
108 113867b8 2009-09-29 jas keyEquivalent:@""];
109 113867b8 2009-09-29 jas [menuItem setTarget:NSApp];
110 113867b8 2009-09-29 jas
111 113867b8 2009-09-29 jas [aMenu addItem:[NSMenuItem separatorItem]];
112 113867b8 2009-09-29 jas
113 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Quit", nil), applicationName]
114 113867b8 2009-09-29 jas action:@selector(terminate:)
115 113867b8 2009-09-29 jas keyEquivalent:@"q"];
116 113867b8 2009-09-29 jas [menuItem setTarget:NSApp];
117 113867b8 2009-09-29 jas }
118 113867b8 2009-09-29 jas
119 113867b8 2009-09-29 jas +(void)populateViewMenu:(NSMenu *)aMenu
120 113867b8 2009-09-29 jas {
121 113867b8 2009-09-29 jas NSMenuItem *menuItem;
122 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Full Screen", nil)
123 113867b8 2009-09-29 jas action:@selector(fullscreen:) keyEquivalent:@"F"];
124 113867b8 2009-09-29 jas [menuItem setTarget:NSApp];
125 113867b8 2009-09-29 jas
126 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Cmd-F exits full screen", nil)
127 113867b8 2009-09-29 jas action:NULL keyEquivalent:@""];
128 113867b8 2009-09-29 jas }
129 113867b8 2009-09-29 jas
130 113867b8 2009-09-29 jas +(void)populateWindowMenu:(NSMenu *)aMenu
131 113867b8 2009-09-29 jas {
132 113867b8 2009-09-29 jas }
133 113867b8 2009-09-29 jas
134 113867b8 2009-09-29 jas +(void)populateHelpMenu:(NSMenu *)aMenu
135 113867b8 2009-09-29 jas {
136 113867b8 2009-09-29 jas }
137 113867b8 2009-09-29 jas
138 113867b8 2009-09-29 jas - (void)applicationWillFinishLaunching:(NSNotification *)notification
139 113867b8 2009-09-29 jas {
140 113867b8 2009-09-29 jas seticon();
141 113867b8 2009-09-29 jas }
142 113867b8 2009-09-29 jas
143 113867b8 2009-09-29 jas - (void)applicationDidFinishLaunching:(NSNotification *)notification
144 113867b8 2009-09-29 jas {
145 113867b8 2009-09-29 jas [DevdrawDelegate populateMainMenu];
146 113867b8 2009-09-29 jas
147 113867b8 2009-09-29 jas // [NSThread detachNewThreadSelector:@selector(devdrawMain)
148 113867b8 2009-09-29 jas // toTarget:self withObject:nil];
149 113867b8 2009-09-29 jas // [NSApplication detachDrawingThread:@selector(devdrawMain)
150 113867b8 2009-09-29 jas // toTarget:self withObject:nil];
151 113867b8 2009-09-29 jas [readHandle waitForDataInBackgroundAndNotify];
152 113867b8 2009-09-29 jas }
153 113867b8 2009-09-29 jas
154 113867b8 2009-09-29 jas - (id)init
155 113867b8 2009-09-29 jas {
156 113867b8 2009-09-29 jas if(self = [super init]){
157 113867b8 2009-09-29 jas readHandle = [[NSFileHandle alloc] initWithFileDescriptor:3 closeOnDealloc:YES];
158 113867b8 2009-09-29 jas [[NSNotificationCenter defaultCenter] addObserver:self
159 113867b8 2009-09-29 jas selector:@selector(devdrawMain:)
160 113867b8 2009-09-29 jas name:NSFileHandleDataAvailableNotification
161 113867b8 2009-09-29 jas object:readHandle];
162 113867b8 2009-09-29 jas [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
163 113867b8 2009-09-29 jas selector:@selector(receiveWake:)
164 113867b8 2009-09-29 jas name:NSWorkspaceDidWakeNotification
165 113867b8 2009-09-29 jas object:NULL];
166 113867b8 2009-09-29 jas }
167 113867b8 2009-09-29 jas return self;
168 113867b8 2009-09-29 jas }
169 113867b8 2009-09-29 jas
170 113867b8 2009-09-29 jas - (void)dealloc
171 113867b8 2009-09-29 jas {
172 113867b8 2009-09-29 jas [[NSNotificationCenter defaultCenter] removeObserver:self];
173 113867b8 2009-09-29 jas [readHandle release];
174 113867b8 2009-09-29 jas return [super dealloc];
175 113867b8 2009-09-29 jas }
176 113867b8 2009-09-29 jas
177 113867b8 2009-09-29 jas - (void)devdrawMain:(NSNotification *)notification
178 113867b8 2009-09-29 jas {
179 113867b8 2009-09-29 jas uchar buf[4], *mbuf;
180 113867b8 2009-09-29 jas int nmbuf, n, nn;
181 113867b8 2009-09-29 jas Wsysmsg m;
182 113867b8 2009-09-29 jas NSData *data;
183 113867b8 2009-09-29 jas
184 113867b8 2009-09-29 jas mbuf = nil;
185 113867b8 2009-09-29 jas nmbuf = 0;
186 113867b8 2009-09-29 jas
187 113867b8 2009-09-29 jas data = [readHandle readDataOfLength:4];
188 113867b8 2009-09-29 jas if([data length] == 4){
189 113867b8 2009-09-29 jas [data getBytes:buf length:4];
190 113867b8 2009-09-29 jas GET(buf, n);
191 113867b8 2009-09-29 jas if(n > nmbuf){
192 113867b8 2009-09-29 jas free(mbuf);
193 113867b8 2009-09-29 jas mbuf = malloc(4+n);
194 113867b8 2009-09-29 jas if(mbuf == nil)
195 113867b8 2009-09-29 jas sysfatal("malloc: %r");
196 113867b8 2009-09-29 jas nmbuf = n;
197 113867b8 2009-09-29 jas }
198 113867b8 2009-09-29 jas memmove(mbuf, buf, 4);
199 113867b8 2009-09-29 jas data = [readHandle readDataOfLength:(n-4)];
200 113867b8 2009-09-29 jas [data getBytes:(mbuf+4)];
201 113867b8 2009-09-29 jas nn = [data length];
202 113867b8 2009-09-29 jas if(nn != n-4)
203 113867b8 2009-09-29 jas sysfatal("eof during message");
204 113867b8 2009-09-29 jas
205 113867b8 2009-09-29 jas /* pick off messages one by one */
206 113867b8 2009-09-29 jas if(convM2W(mbuf, nn+4, &m) <= 0)
207 113867b8 2009-09-29 jas sysfatal("cannot convert message");
208 113867b8 2009-09-29 jas if(trace) fprint(2, "<- %W\n", &m);
209 113867b8 2009-09-29 jas runmsg(&m);
210 113867b8 2009-09-29 jas } else {
211 113867b8 2009-09-29 jas [NSApp terminate:self];
212 113867b8 2009-09-29 jas }
213 113867b8 2009-09-29 jas [readHandle waitForDataInBackgroundAndNotify];
214 113867b8 2009-09-29 jas
215 113867b8 2009-09-29 jas return;
216 113867b8 2009-09-29 jas
217 113867b8 2009-09-29 jas while((n = read(3, buf, 4)) == 4){
218 113867b8 2009-09-29 jas GET(buf, n);
219 113867b8 2009-09-29 jas if(n > nmbuf){
220 113867b8 2009-09-29 jas free(mbuf);
221 113867b8 2009-09-29 jas mbuf = malloc(4+n);
222 113867b8 2009-09-29 jas if(mbuf == nil)
223 113867b8 2009-09-29 jas sysfatal("malloc: %r");
224 113867b8 2009-09-29 jas nmbuf = n;
225 113867b8 2009-09-29 jas }
226 113867b8 2009-09-29 jas memmove(mbuf, buf, 4);
227 113867b8 2009-09-29 jas nn = readn(3, mbuf+4, n-4);
228 113867b8 2009-09-29 jas if(nn != n-4)
229 113867b8 2009-09-29 jas sysfatal("eof during message");
230 113867b8 2009-09-29 jas
231 113867b8 2009-09-29 jas /* pick off messages one by one */
232 113867b8 2009-09-29 jas if(convM2W(mbuf, nn+4, &m) <= 0)
233 113867b8 2009-09-29 jas sysfatal("cannot convert message");
234 113867b8 2009-09-29 jas if(trace) fprint(2, "<- %W\n", &m);
235 113867b8 2009-09-29 jas runmsg(&m);
236 113867b8 2009-09-29 jas }
237 113867b8 2009-09-29 jas }
238 113867b8 2009-09-29 jas
239 113867b8 2009-09-29 jas #pragma mark Notifications
240 113867b8 2009-09-29 jas
241 113867b8 2009-09-29 jas - (void)fullscreen:(NSNotification *)notification
242 113867b8 2009-09-29 jas {
243 113867b8 2009-09-29 jas fullscreen(1);
244 113867b8 2009-09-29 jas }
245 113867b8 2009-09-29 jas
246 113867b8 2009-09-29 jas - (void)windowWillClose:(NSNotification *)notification
247 113867b8 2009-09-29 jas {
248 113867b8 2009-09-29 jas // if(osx.window == [notification object]){
249 113867b8 2009-09-29 jas [[NSNotificationCenter defaultCenter] removeObserver:self];
250 113867b8 2009-09-29 jas [NSApp terminate:self];
251 113867b8 2009-09-29 jas // }
252 113867b8 2009-09-29 jas }
253 113867b8 2009-09-29 jas
254 113867b8 2009-09-29 jas - (void)windowDidResize:(NSNotification *)notification
255 113867b8 2009-09-29 jas {
256 113867b8 2009-09-29 jas // if(osx.window == [notification object]) {
257 113867b8 2009-09-29 jas eresized(1);
258 113867b8 2009-09-29 jas // }
259 113867b8 2009-09-29 jas }
260 113867b8 2009-09-29 jas
261 113867b8 2009-09-29 jas - (void)receiveWake:(NSNotification *)notification
262 113867b8 2009-09-29 jas {
263 113867b8 2009-09-29 jas if(trace) NSLog(@"%s:%d %@", __FILE__, __LINE__, notification);
264 113867b8 2009-09-29 jas // redraw
265 113867b8 2009-09-29 jas }
266 113867b8 2009-09-29 jas
267 113867b8 2009-09-29 jas - (void)mouseDown:(NSEvent *)anEvent
268 113867b8 2009-09-29 jas {
269 113867b8 2009-09-29 jas mouseevent(anEvent);
270 113867b8 2009-09-29 jas }
271 113867b8 2009-09-29 jas
272 113867b8 2009-09-29 jas - (void)mouseDragged:(NSEvent *)anEvent
273 113867b8 2009-09-29 jas {
274 113867b8 2009-09-29 jas mouseevent(anEvent);
275 113867b8 2009-09-29 jas }
276 113867b8 2009-09-29 jas
277 113867b8 2009-09-29 jas - (void)keydown:(NSEvent *)anEvent
278 113867b8 2009-09-29 jas {
279 113867b8 2009-09-29 jas kbdevent(anEvent);
280 113867b8 2009-09-29 jas }
281 113867b8 2009-09-29 jas
282 113867b8 2009-09-29 jas @end