3 #define Cursor OSXCursor
4 #import <Cocoa/Cocoa.h>
5 #import <AppKit/AppKit.h>
19 #include "osx-screen.h"
20 #include "osx-keycodes.h"
22 #include "glendapng.h"
26 #define panic sysfatal
28 extern Rectangle mouserect;
35 Rectangle fullscreenr;
37 Memimage *screenimage;
45 CGDataProviderRef provider;
48 CGContextRef windowctx;
54 int kalting; // last keystroke was Kalt
59 WindowAttrs = NSClosableWindowMask |
61 NSMiniaturizableWindowMask |
65 static void screenproc(void*);
69 static void activated(int);
76 @interface P9View : NSView
80 @implementation P9View
81 - (BOOL)acceptsFirstResponder
87 void screeninit(void);
88 void _flushmemscreen(Rectangle r);
91 attachscreen(char *label, char *winsize)
94 label = "gnot a label";
95 osx.label = strdup(label);
96 osx.winsize = winsize;
97 if(osx.screenimage == nil){
99 if(osx.screenimage == nil)
100 panic("cannot create OS X screen");
102 return osx.screenimage;
115 cgr = CGDisplayBounds(CGMainDisplayID());
116 osx.fullscreenr = Rect(0, 0, cgr.size.width, cgr.size.height);
118 // Create the window.
119 r = Rect(0, 0, Dx(osx.fullscreenr)*2/3, Dy(osx.fullscreenr)*2/3);
121 if(osx.winsize && osx.winsize[0]){
122 if(parsewinsize(osx.winsize, &r, &havemin) < 0)
126 r = rectaddpt(r, Pt((Dx(osx.fullscreenr)-Dx(r))/2, (Dy(osx.fullscreenr)-Dy(r))/2));
127 or = NSMakeRect(r.min.x, r.min.y, r.max.x, r.max.y);
128 osx.window = [[NSWindow alloc] initWithContentRect:or styleMask:WindowAttrs
129 backing:NSBackingStoreBuffered defer:NO screen:[NSScreen mainScreen]];
130 [osx.window setDelegate:[NSApp delegate]];
131 [osx.window setAcceptsMouseMovedEvents:YES];
133 P9View *view = [[P9View alloc] initWithFrame:or];
134 [osx.window setContentView:view];
140 // Finally, put the window on the screen.
142 [osx.window makeKeyAndOrderFront:nil];
155 // proccreate(screenproc, nil, 256*1024);
157 while(osx.window == nil)
174 return nsec()/1000000;
179 mouseevent(NSEvent *event)
184 op = [event locationInWindow];
186 osx.xy = subpt(Pt(op.x, op.y), osx.screenr.min);
189 switch([event type]){
191 CGFloat delta = [event deltaY];
198 case NSLeftMouseDown:
199 case NSRightMouseDown:
200 case NSOtherMouseDown:
203 case NSOtherMouseUp:;
206 but = [event buttonNumber];
207 mod = [event modifierFlags];
209 // OS X swaps button 2 and 3
210 but = (but & ~6) | ((but & 4)>>1) | ((but&2)<<1);
211 but = mouseswap(but);
213 // Apply keyboard modifiers and pretend it was a real mouse button.
214 // (Modifiers typed while holding the button go into kbuttons,
215 // but this one does not.)
217 if(mod & NSAlternateKeyMask) {
218 // Take the ALT away from the keyboard handler.
225 else if(mod & NSCommandKeyMask)
232 case NSLeftMouseDragged:
233 case NSRightMouseDragged:
234 case NSOtherMouseDragged:
241 mousetrack(osx.xy.x, osx.xy.y, osx.buttons|osx.kbuttons|wheel, msec());
244 static int keycvt[] =
246 [QZ_IBOOK_ENTER] '\n',
273 [QZ_PAGEDOWN] Kpgdown,
278 [QZ_KP_MULTIPLY] '*',
296 kbdevent(NSEvent *event)
303 ch = [[event characters] characterAtIndex:0];
304 code = [event keyCode];
305 mod = [event modifierFlags];
307 switch([event type]){
310 if(mod == NSCommandKeyMask){
311 if(ch == 'F' || ch == 'f'){
312 if(osx.isfullscreen && msec() - osx.fullscreentime > 500)
317 // Pass most Cmd keys through as Kcmd + ch.
318 // OS X interprets a few no matter what we do,
319 // so it is useless to pass them through as keystrokes too.
321 case 'm': // minimize window
322 case 'h': // hide window
323 case 'H': // hide others
327 if(' ' <= ch && ch <= '~') {
328 keystroke(Kcmd + ch);
334 if(code < nelem(keycvt) && keycvt[code])
344 if(!osx.buttons && !osx.kbuttons){
345 if(mod == NSAlternateKeyMask) {
352 // If the mouse button is being held down, treat
353 // changes in the keyboard modifiers as changes
354 // in the mouse buttons.
356 if(mod & NSAlternateKeyMask)
358 if(mod & NSCommandKeyMask)
360 mousetrack(osx.xy.x, osx.xy.y, osx.buttons|osx.kbuttons, msec());
375 CGDataProviderRef provider;
377 CGColorSpaceRef cspace;
379 or = [[osx.window contentView] bounds];
380 r = Rect(or.origin.x, or.origin.y, or.size.width, or.size.height);
381 if(Dx(r) == Dx(osx.screenr) && Dy(r) == Dy(osx.screenr)){
382 // No need to make new image.
388 m = allocmemimage(Rect(0, 0, Dx(r), Dy(r)), chan);
390 panic("allocmemimage: %r");
392 panic("m->data == nil");
393 bpl = bytesperline(r, 32);
394 provider = CGDataProviderCreateWithData(0,
395 m->data->bdata, Dy(r)*bpl, 0);
396 //cspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
397 cspace = CGColorSpaceCreateDeviceRGB();
398 image = CGImageCreate(Dx(r), Dy(r), 8, 32, bpl,
400 kCGImageAlphaNoneSkipLast,
401 provider, 0, 0, kCGRenderingIntentDefault);
402 CGColorSpaceRelease(cspace);
403 CGDataProviderRelease(provider); // CGImageCreate did incref
408 mousetrack(osx.xy.x, osx.xy.y, osx.buttons|osx.kbuttons, msec());
410 // termreplacescreenimage(m);
411 _drawreplacescreenimage(m); // frees old osx.screenimage if any
413 CGImageRelease(osx.image);
423 if(osx.needflush && osx.windowctx && canqlock(&osx.flushlock)){
425 CGContextFlush(osx.windowctx);
428 qunlock(&osx.flushlock);
435 _flushmemscreen(Rectangle r)
440 qlock(&osx.flushlock);
441 if(osx.windowctx == nil){
442 osx.windowctx = [[osx.window graphicsContext] graphicsPort];
443 // [osx.window flushWindow];
444 // proccreate(flushproc, nil, 256*1024);
447 cgr.origin.x = r.min.x;
448 cgr.origin.y = r.min.y;
449 cgr.size.width = Dx(r);
450 cgr.size.height = Dy(r);
451 subimg = CGImageCreateWithImageInRect(osx.image, cgr);
452 cgr.origin.y = Dy(osx.screenr) - r.max.y; // XXX how does this make any sense?
453 CGContextDrawImage(osx.windowctx, cgr, subimg);
455 qunlock(&osx.flushlock);
456 CGImageRelease(subimg);
460 activated(int active)
466 fullscreen(int wascmd)
468 NSView *view = [osx.window contentView];
470 if(osx.isfullscreen){
471 [view exitFullScreenModeWithOptions:nil];
472 osx.isfullscreen = 0;
474 [view enterFullScreenMode:[osx.window screen] withOptions:nil];
475 osx.isfullscreen = 1;
476 osx.fullscreentime = msec();
486 cgp.x = p.x + osx.screenr.min.x;
487 cgp.y = p.y + osx.screenr.min.y;
488 CGWarpMouseCursorPosition(cgp);
495 NSBitmapImageRep *bitmap;
497 unsigned char *planes[5];
505 image = [[NSImage alloc] initWithSize:NSMakeSize(16.0, 16.0)];
506 bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
513 colorSpaceName:NSCalibratedWhiteColorSpace
517 [bitmap getBitmapDataPlanes:planes];
520 planes[0][i] = ((ushort*)c->set)[i];
521 planes[1][i] = planes[0][i] | ((ushort*)c->clr)[i];
524 [image addRepresentation:bitmap];
526 nsc = [[NSCursor alloc] initWithImage:image
527 hotSpot:NSMakePoint(c->offset.x, c->offset.y)];
536 getcolor(ulong i, ulong *r, ulong *g, ulong *b)
547 setcolor(ulong i, ulong r, ulong g, ulong b)
555 hwdraw(Memdrawparam *p)
563 Rune rbuf[SnarfSize];
576 /* fprint(2, "applegetsnarf\n"); */
579 clip.apple = [NSPasteboard generalPasteboard];
580 types = [clip.apple types];
582 string = [clip.apple stringForType:NSStringPboardType];
584 fprint(2, "apple pasteboard get item type failed\n");
589 data = [string dataUsingEncoding:NSUnicodeStringEncoding];
591 ndata = [data length];
593 s = smprint("%.*S", ndata/2, (Rune*)[data bytes]);
607 NSArray *pboardTypes;
610 /* fprint(2, "appleputsnarf\n"); */
612 if(strlen(s) >= SnarfSize)
616 runesnprint(clip.rbuf, nelem(clip.rbuf), "%s", s);
618 pboardTypes = [NSArray arrayWithObject:NSStringPboardType];
620 clip.apple = [NSPasteboard generalPasteboard];
621 [clip.apple declareTypes:pboardTypes owner:nil];
623 assert(sizeof(clip.rbuf[0]) == 2);
624 string = [NSString stringWithCharacters:clip.rbuf length:runestrlen(clip.rbuf)*2];
626 fprint(2, "apple pasteboard data create failed\n");
630 if(![clip.apple setString:string forType:NSStringPboardType]){
631 fprint(2, "apple pasteboard putitem failed\n");
639 setlabel(char *label)
642 cs = CFStringCreateWithBytes(nil, (uchar*)label, strlen(osx.label), kCFStringEncodingUTF8, false);
643 [osx.window setTitle:(NSString*)cs];
648 kicklabel(char *label)
655 qlock(&osx.labellock);
658 qunlock(&osx.labellock);
670 d = [[NSData alloc] initWithBytes:glenda_png length:(sizeof glenda_png)];
671 im = [[NSImage alloc] initWithData:d];
674 [NSApp setApplicationIconImage:im];
675 [[NSApp dockTile] setShowsApplicationBadge:YES];
676 [[NSApp dockTile] display];