aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/devdraw/osx-delegate.m
blob: c4a5f5bb5086b4bc6009fe0dfc6597f248516b03 (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#define Point OSXPoint
#define Rect OSXRect
#define Cursor OSXCursor
#import "osx-delegate.h"
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#undef Cursor
#undef Rect
#undef Point

#include <u.h>
#include <errno.h>
#include <sys/select.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>
#include <memlayer.h>
#include <keyboard.h>
#include <mouse.h>
#include <cursor.h>
#include <drawfcall.h>

AUTOFRAMEWORK(Foundation)
AUTOFRAMEWORK(AppKit)

extern int trace;

extern void fullscreen(int);
extern void kbdevent(NSEvent *event);
extern void mouseevent(NSEvent *event);
extern void eresized(int);

extern void runmsg(Wsysmsg *m);
extern void seticon();

@implementation DevdrawDelegate
+(void)populateMainMenu
{
	NSMenu *mainMenu = [[NSMenu alloc] initWithTitle:@"MainMenu"];
	NSMenuItem *menuItem;
	NSMenu *submenu;

	menuItem = [mainMenu addItemWithTitle:@"Apple" action:NULL keyEquivalent:@""];
	submenu = [[NSMenu alloc] initWithTitle:@"Apple"];
	[NSApp performSelector:@selector(setAppleMenu:) withObject:submenu];
	[self populateApplicationMenu:submenu];
	[mainMenu setSubmenu:submenu forItem:menuItem];

	menuItem = [mainMenu addItemWithTitle:@"View" action:NULL keyEquivalent:@""];
	submenu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"View", "@The View menu")];
	[self populateViewMenu:submenu];
	[mainMenu setSubmenu:submenu forItem:menuItem];

	menuItem = [mainMenu addItemWithTitle:@"Window" action:NULL keyEquivalent:@""];
	submenu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Window", @"The Window menu")];
	[self populateWindowMenu:submenu];
	[mainMenu setSubmenu:submenu forItem:menuItem];
	[NSApp setWindowsMenu:submenu];

	menuItem = [mainMenu addItemWithTitle:@"Help" action:NULL keyEquivalent:@""];
	submenu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Help", @"The Help menu")];
	[self populateHelpMenu:submenu];
	[mainMenu setSubmenu:submenu forItem:menuItem];

	[NSApp setMainMenu:mainMenu];
}

+(void)populateApplicationMenu:(NSMenu *)aMenu
{
	NSString *applicationName = [[NSProcessInfo processInfo] processName];
	NSMenuItem *menuItem;
	
	menuItem = [aMenu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"About", nil), applicationName]
								action:@selector(orderFrontStandardAboutPanel:)
						 keyEquivalent:@""];
	[menuItem setTarget:NSApp];
	
	[aMenu addItem:[NSMenuItem separatorItem]];
	
	menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Preferences...", nil)
								action:NULL
						 keyEquivalent:@","];
	
	[aMenu addItem:[NSMenuItem separatorItem]];
	
	menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Services", nil)
								action:NULL
						 keyEquivalent:@""];
	NSMenu * servicesMenu = [[NSMenu alloc] initWithTitle:@"Services"];
	[aMenu setSubmenu:servicesMenu forItem:menuItem];
	[NSApp setServicesMenu:servicesMenu];
	
	[aMenu addItem:[NSMenuItem separatorItem]];
	
	menuItem = [aMenu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Hide", nil), applicationName]
								action:@selector(hide:)
						 keyEquivalent:@"h"];
	[menuItem setTarget:NSApp];
	
	menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Hide Others", nil)
								action:@selector(hideOtherApplications:)
						 keyEquivalent:@"h"];
	[menuItem setKeyEquivalentModifierMask:NSCommandKeyMask | NSAlternateKeyMask];
	[menuItem setTarget:NSApp];
	
	menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Show All", nil)
								action:@selector(unhideAllApplications:)
						 keyEquivalent:@""];
	[menuItem setTarget:NSApp];
	
	[aMenu addItem:[NSMenuItem separatorItem]];
	
	menuItem = [aMenu addItemWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Quit", nil), applicationName]
								action:@selector(terminate:)
						 keyEquivalent:@"q"];
	[menuItem setTarget:NSApp];
}

+(void)populateViewMenu:(NSMenu *)aMenu
{
	NSMenuItem *menuItem;
	menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Full Screen", nil)
				action:@selector(fullscreen:) keyEquivalent:@"F"];
	[menuItem setTarget:NSApp];

	menuItem = [aMenu addItemWithTitle:NSLocalizedString(@"Cmd-F exits full screen", nil)
				action:NULL keyEquivalent:@""];
}

+(void)populateWindowMenu:(NSMenu *)aMenu
{
}

+(void)populateHelpMenu:(NSMenu *)aMenu
{
}

- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
	seticon();
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
	[DevdrawDelegate populateMainMenu];

//	[NSThread detachNewThreadSelector:@selector(devdrawMain)
//		toTarget:self withObject:nil];
//	[NSApplication detachDrawingThread:@selector(devdrawMain)
//		toTarget:self withObject:nil];
	[readHandle waitForDataInBackgroundAndNotify];
}

- (id)init
{
	if(self = [super init]){
		readHandle = [[NSFileHandle alloc] initWithFileDescriptor:3 closeOnDealloc:YES];
		[[NSNotificationCenter defaultCenter] addObserver:self
			selector:@selector(devdrawMain:)
			name:NSFileHandleDataAvailableNotification
			object:readHandle];
		[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
			selector:@selector(receiveWake:)
			name:NSWorkspaceDidWakeNotification
			object:NULL];
	}
	return self;
}

- (void)dealloc
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[readHandle release];
	return [super dealloc];
}

- (void)devdrawMain:(NSNotification *)notification
{
	uchar buf[4], *mbuf;
	int nmbuf, n, nn;
	Wsysmsg m;
	NSData *data;

	mbuf = nil;
	nmbuf = 0;

	data = [readHandle readDataOfLength:4];
	if([data length] == 4){
		[data getBytes:buf length:4];
		GET(buf, n);
		if(n > nmbuf){
			free(mbuf);
			mbuf = malloc(4+n);
			if(mbuf == nil)
			sysfatal("malloc: %r");
			nmbuf = n;
		}
		memmove(mbuf, buf, 4);
		data = [readHandle readDataOfLength:(n-4)];
		[data getBytes:(mbuf+4)];
		nn = [data length];
		if(nn != n-4)
			sysfatal("eof during message");

		/* pick off messages one by one */
		if(convM2W(mbuf, nn+4, &m) <= 0)
			sysfatal("cannot convert message");
		if(trace) fprint(2, "<- %W\n", &m);
		runmsg(&m);
	} else {
		[NSApp terminate:self];
	}
	[readHandle waitForDataInBackgroundAndNotify];

return;

	while((n = read(3, buf, 4)) == 4){
		GET(buf, n);
		if(n > nmbuf){
			free(mbuf);
			mbuf = malloc(4+n);
			if(mbuf == nil)
				sysfatal("malloc: %r");
			nmbuf = n;
		}
		memmove(mbuf, buf, 4);
		nn = readn(3, mbuf+4, n-4);
		if(nn != n-4)
			sysfatal("eof during message");

		/* pick off messages one by one */
		if(convM2W(mbuf, nn+4, &m) <= 0)
			sysfatal("cannot convert message");
		if(trace) fprint(2, "<- %W\n", &m);
		runmsg(&m);
	}
}

#pragma mark Notifications

- (void)fullscreen:(NSNotification *)notification
{
	fullscreen(1);
}

- (void)windowWillClose:(NSNotification *)notification
{
//	if(osx.window == [notification object]){
		[[NSNotificationCenter defaultCenter] removeObserver:self];
		[NSApp terminate:self];
//	}
}

- (void)windowDidResize:(NSNotification *)notification
{
//	if(osx.window == [notification object]) {
		eresized(1);
//	}
}

- (void)receiveWake:(NSNotification *)notification
{
	if(trace) NSLog(@"%s:%d %@", __FILE__, __LINE__, notification);
	// redraw
}

- (void)mouseDown:(NSEvent *)anEvent
{
	mouseevent(anEvent);
}

- (void)mouseDragged:(NSEvent *)anEvent
{
	mouseevent(anEvent);
}

- (void)keydown:(NSEvent *)anEvent
{
	kbdevent(anEvent);
}

@end