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>
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>
23 113867b8 2009-09-29 jas AUTOFRAMEWORK(Foundation)
24 113867b8 2009-09-29 jas AUTOFRAMEWORK(AppKit)
26 113867b8 2009-09-29 jas extern int trace;
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);
33 113867b8 2009-09-29 jas extern void runmsg(Wsysmsg *m);
34 113867b8 2009-09-29 jas extern void seticon();
36 113867b8 2009-09-29 jas @implementation DevdrawDelegate
37 113867b8 2009-09-29 jas +(void)populateMainMenu
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;
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];
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];
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];
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];
65 113867b8 2009-09-29 jas [NSApp setMainMenu:mainMenu];
68 113867b8 2009-09-29 jas +(void)populateApplicationMenu:(NSMenu *)aMenu
70 113867b8 2009-09-29 jas NSString *applicationName = [[NSProcessInfo processInfo] processName];
71 113867b8 2009-09-29 jas NSMenuItem *menuItem;
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];
78 113867b8 2009-09-29 jas [aMenu addItem:[NSMenuItem separatorItem]];
80 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Preferences...", nil)
82 113867b8 2009-09-29 jas keyEquivalent:@","];
84 113867b8 2009-09-29 jas [aMenu addItem:[NSMenuItem separatorItem]];
86 113867b8 2009-09-29 jas menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Services", nil)
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];
93 113867b8 2009-09-29 jas [aMenu addItem:[NSMenuItem separatorItem]];
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];
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];
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];
111 113867b8 2009-09-29 jas [aMenu addItem:[NSMenuItem separatorItem]];
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];
119 113867b8 2009-09-29 jas +(void)populateViewMenu:(NSMenu *)aMenu
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];
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:@""];
130 113867b8 2009-09-29 jas +(void)populateWindowMenu:(NSMenu *)aMenu
134 113867b8 2009-09-29 jas +(void)populateHelpMenu:(NSMenu *)aMenu
138 113867b8 2009-09-29 jas - (void)applicationWillFinishLaunching:(NSNotification *)notification
143 113867b8 2009-09-29 jas - (void)applicationDidFinishLaunching:(NSNotification *)notification
145 113867b8 2009-09-29 jas [DevdrawDelegate populateMainMenu];
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];
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];
167 113867b8 2009-09-29 jas return self;
170 113867b8 2009-09-29 jas - (void)dealloc
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];
177 113867b8 2009-09-29 jas - (void)devdrawMain:(NSNotification *)notification
179 113867b8 2009-09-29 jas uchar buf[4], *mbuf;
180 113867b8 2009-09-29 jas int nmbuf, n, nn;
182 113867b8 2009-09-29 jas NSData *data;
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){
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");
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");
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);
211 113867b8 2009-09-29 jas [NSApp terminate:self];
213 113867b8 2009-09-29 jas [readHandle waitForDataInBackgroundAndNotify];
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){
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");
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");
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);
239 113867b8 2009-09-29 jas #pragma mark Notifications
241 113867b8 2009-09-29 jas - (void)fullscreen:(NSNotification *)notification
243 113867b8 2009-09-29 jas fullscreen(1);
246 113867b8 2009-09-29 jas - (void)windowWillClose:(NSNotification *)notification
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];
254 113867b8 2009-09-29 jas - (void)windowDidResize:(NSNotification *)notification
256 113867b8 2009-09-29 jas // if(osx.window == [notification object]) {
257 113867b8 2009-09-29 jas eresized(1);
261 113867b8 2009-09-29 jas - (void)receiveWake:(NSNotification *)notification
263 113867b8 2009-09-29 jas if(trace) NSLog(@"%s:%d %@", __FILE__, __LINE__, notification);
267 113867b8 2009-09-29 jas - (void)mouseDown:(NSEvent *)anEvent
269 113867b8 2009-09-29 jas mouseevent(anEvent);
272 113867b8 2009-09-29 jas - (void)mouseDragged:(NSEvent *)anEvent
274 113867b8 2009-09-29 jas mouseevent(anEvent);
277 113867b8 2009-09-29 jas - (void)keydown:(NSEvent *)anEvent
279 113867b8 2009-09-29 jas kbdevent(anEvent);