-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgr_mac.m
More file actions
254 lines (222 loc) · 7.49 KB
/
Copy pathgr_mac.m
File metadata and controls
254 lines (222 loc) · 7.49 KB
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
#import <Cocoa/Cocoa.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
extern int gr_mode;
extern void sysres(int ac, char *av[]);
// X11 global variables exported by sysres
int c0, c1, bx, by, bw, bh, cw, ch, xpix, ypix, ytop, ybot, xmax, ymax;
int keyevent = 0;
static struct {
short int r, g, b;
} ct[16] = {
{0, 0, 0}, /* black */
{0, 0, 170}, /* blue */
{0, 170, 0}, /* green */
{0, 170, 170}, /* cyan */
{170, 0, 0}, /* red */
{170, 0, 170}, /* magenta */
{170, 85, 0}, /* brown */
{170, 170, 170}, /* light gray */
{85, 85, 85}, /* dark gray */
{85, 85, 255}, /* light blue */
{85, 255, 85}, /* light green*/
{85, 255, 255}, /* light cyan */
{255, 85, 85}, /* light red */
{255, 85, 255}, /* light magenta*/
{255, 255, 85}, /* yellow */
{255, 255, 255}, /* white */
};
static NSColor *cocoaColors[16];
static NSWindow *window;
static NSImage *bufferImage;
static NSMutableArray<NSNumber *> *keyQueue;
static pthread_mutex_t keyMutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t keyCond = PTHREAD_COND_INITIALIZER;
static int current_color = 15; // white default
@interface SysResView : NSView
@end
@implementation SysResView
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)drawRect:(NSRect)dirtyRect {
if (bufferImage) {
[bufferImage drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositingOperationCopy fraction:1.0];
}
}
- (void)keyDown:(NSEvent *)event {
NSString *chars = [event characters];
if ([chars length] > 0) {
unichar ch = [chars characterAtIndex:0];
// Handle special keys mapping (like arrows) if needed.
// For simplicity, just enqueue the raw char.
if (ch == NSUpArrowFunctionKey) ch = '8'; // Map arrow keys if needed or let them fall through
else if (ch == NSDownArrowFunctionKey) ch = '2';
else if (ch == NSLeftArrowFunctionKey) ch = '4';
else if (ch == NSRightArrowFunctionKey) ch = '6';
pthread_mutex_lock(&keyMutex);
[keyQueue addObject:@(ch)];
keyevent = (int)[keyQueue count];
pthread_cond_signal(&keyCond);
pthread_mutex_unlock(&keyMutex);
}
}
@end
// SYSRES X11 interface implementation
int set_color(int c) {
int old = current_color;
if (c >= 0 && c < 16) {
current_color = c;
}
return old;
}
void xw_init(void) {
// This is called from the background thread. We must do window creation on the main thread.
dispatch_sync(dispatch_get_main_queue(), ^{
for (int i=0; i<16; i++) {
cocoaColors[i] = [NSColor colorWithCalibratedRed:ct[i].r/255.0 green:ct[i].g/255.0 blue:ct[i].b/255.0 alpha:1.0];
}
NSRect frame = NSMakeRect(0, 0, 1024, 768);
window = [[NSWindow alloc] initWithContentRect:frame
styleMask:(NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable)
backing:NSBackingStoreBuffered
defer:NO];
[window setTitle:@"SYSRES"];
[window center];
SysResView *view = [[SysResView alloc] initWithFrame:frame];
[window setContentView:view];
[window makeFirstResponder:view];
bufferImage = [[NSImage alloc] initWithSize:NSMakeSize(1024, 768)];
[bufferImage lockFocus];
[cocoaColors[15] set];
NSRectFill(frame);
[bufferImage unlockFocus];
[window makeKeyAndOrderFront:nil];
});
keyQueue = [[NSMutableArray alloc] init];
xpix = 1024;
ypix = 768;
c0 = 15;
c1 = 0;
cw = 8;
ch = 14;
bx = 0;
by = 0;
bw = xpix;
bh = ypix;
ytop = 0;
ybot = ypix;
xmax = xpix;
ymax = ypix;
}
void xw_close(void) {
dispatch_async(dispatch_get_main_queue(), ^{
[window close];
[NSApp terminate:nil];
});
}
void xw_clrb(int x1, int y1, int x2, int y2) {
dispatch_sync(dispatch_get_main_queue(), ^{
[bufferImage lockFocus];
[cocoaColors[c0] setFill];
NSRectFill(NSMakeRect(x1, 768 - y2, x2 - x1, y2 - y1));
[bufferImage unlockFocus];
[[window contentView] setNeedsDisplay:YES];
});
}
void xw_clrs(void) {
xw_clrb(0, 0, 1024, 768);
}
void xw_pixel(int x, int y) {
dispatch_sync(dispatch_get_main_queue(), ^{
[bufferImage lockFocus];
[cocoaColors[current_color] setFill];
NSRectFill(NSMakeRect(x, 768 - y - 1, 1, 1));
[bufferImage unlockFocus];
[[window contentView] setNeedsDisplay:YES];
});
}
void xw_white(void) {
set_color(15);
}
void xw_line(int x1, int y1, int x2, int y2, int c) {
dispatch_sync(dispatch_get_main_queue(), ^{
[bufferImage lockFocus];
NSBezierPath *path = [NSBezierPath bezierPath];
[path moveToPoint:NSMakePoint(x1, 768 - y1)];
[path lineToPoint:NSMakePoint(x2, 768 - y2)];
[cocoaColors[c] setStroke];
[path stroke];
[bufferImage unlockFocus];
[[window contentView] setNeedsDisplay:YES];
});
}
void xw_text(int x, int y, char *s, int fgc, int bgc) {
dispatch_sync(dispatch_get_main_queue(), ^{
[bufferImage lockFocus];
NSString *text = [NSString stringWithUTF8String:s];
NSDictionary *attrs = @{
NSForegroundColorAttributeName: cocoaColors[fgc],
NSBackgroundColorAttributeName: cocoaColors[bgc],
NSFontAttributeName: [NSFont userFixedPitchFontOfSize:12.0]
};
// Y-axis inversion and rough adjustment for font baseline
[text drawAtPoint:NSMakePoint(x, 768 - y - 14) withAttributes:attrs];
[bufferImage unlockFocus];
[[window contentView] setNeedsDisplay:YES];
});
}
void xw_flush(void) {
// setNeedsDisplay:YES is already called in drawing routines.
}
int xw_kbhit(void) {
int count = 0;
pthread_mutex_lock(&keyMutex);
count = (int)[keyQueue count];
pthread_mutex_unlock(&keyMutex);
return count;
}
int xw_getch(void) {
int c = 0;
pthread_mutex_lock(&keyMutex);
while ([keyQueue count] == 0) {
pthread_cond_wait(&keyCond, &keyMutex);
}
c = [[keyQueue objectAtIndex:0] intValue];
[keyQueue removeObjectAtIndex:0];
keyevent = (int)[keyQueue count];
pthread_mutex_unlock(&keyMutex);
return c;
}
// macOS Application Delegate
@interface SysResAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) int argc;
@property (assign) char **argv;
@end
@implementation SysResAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[NSThread detachNewThreadSelector:@selector(runProgram) toTarget:self withObject:nil];
}
- (void)runProgram {
gr_mode = 2; // Force X11 mode in gr_con.c which we now intercept
sysres(self.argc, self.argv);
// When the C program finishes, terminate the app
dispatch_async(dispatch_get_main_queue(), ^{
[NSApp terminate:nil];
});
}
@end
int main(int argc, char **argv) {
@autoreleasepool {
NSApplication *app = [NSApplication sharedApplication];
SysResAppDelegate *delegate = [[SysResAppDelegate alloc] init];
delegate.argc = argc;
delegate.argv = argv;
[app setDelegate:delegate];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
[app run];
}
return 0;
}