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 <Cocoa/Cocoa.h>
5 113867b8 2009-09-29 jas #import <AppKit/AppKit.h>
9 113867b8 2009-09-29 jas #undef offsetof
12 113867b8 2009-09-29 jas #include <u.h>
13 113867b8 2009-09-29 jas #include <libc.h>
14 113867b8 2009-09-29 jas #include <draw.h>
15 113867b8 2009-09-29 jas #include <memdraw.h>
16 113867b8 2009-09-29 jas #include <keyboard.h>
17 113867b8 2009-09-29 jas #include <mouse.h>
18 113867b8 2009-09-29 jas #include <cursor.h>
19 113867b8 2009-09-29 jas #include "osx-screen.h"
20 113867b8 2009-09-29 jas #include "osx-keycodes.h"
21 113867b8 2009-09-29 jas #include "devdraw.h"
22 113867b8 2009-09-29 jas #include "glendapng.h"
24 113867b8 2009-09-29 jas AUTOFRAMEWORK(Cocoa)
26 113867b8 2009-09-29 jas #define panic sysfatal
28 113867b8 2009-09-29 jas extern Rectangle mouserect;
32 113867b8 2009-09-29 jas char *winsize;
33 113867b8 2009-09-29 jas QLock labellock;
35 113867b8 2009-09-29 jas Rectangle fullscreenr;
36 113867b8 2009-09-29 jas Rectangle screenr;
37 113867b8 2009-09-29 jas Memimage *screenimage;
38 113867b8 2009-09-29 jas int isfullscreen;
39 113867b8 2009-09-29 jas ulong fullscreentime;
43 113867b8 2009-09-29 jas int kbuttons;
45 113867b8 2009-09-29 jas CGDataProviderRef provider;
46 113867b8 2009-09-29 jas NSWindow *window;
47 113867b8 2009-09-29 jas CGImageRef image;
48 113867b8 2009-09-29 jas CGContextRef windowctx;
50 113867b8 2009-09-29 jas int needflush;
51 113867b8 2009-09-29 jas QLock flushlock;
53 113867b8 2009-09-29 jas int infullscreen;
54 113867b8 2009-09-29 jas int kalting; // last keystroke was Kalt
59 113867b8 2009-09-29 jas WindowAttrs = NSClosableWindowMask |
60 113867b8 2009-09-29 jas NSTitledWindowMask |
61 113867b8 2009-09-29 jas NSMiniaturizableWindowMask |
62 113867b8 2009-09-29 jas NSResizableWindowMask
65 113867b8 2009-09-29 jas static void screenproc(void*);
66 113867b8 2009-09-29 jas void eresized(int);
67 113867b8 2009-09-29 jas void fullscreen(int);
68 113867b8 2009-09-29 jas void seticon(void);
69 113867b8 2009-09-29 jas static void activated(int);
73 113867b8 2009-09-29 jas CmdFullScreen = 1,
76 113867b8 2009-09-29 jas @interface P9View : NSView
80 113867b8 2009-09-29 jas @implementation P9View
81 113867b8 2009-09-29 jas - (BOOL)acceptsFirstResponder
87 113867b8 2009-09-29 jas void screeninit(void);
88 113867b8 2009-09-29 jas void _flushmemscreen(Rectangle r);
91 113867b8 2009-09-29 jas attachscreen(char *label, char *winsize)
93 113867b8 2009-09-29 jas if(label == nil)
94 113867b8 2009-09-29 jas label = "gnot a label";
95 113867b8 2009-09-29 jas osx.label = strdup(label);
96 113867b8 2009-09-29 jas osx.winsize = winsize;
97 113867b8 2009-09-29 jas if(osx.screenimage == nil){
98 113867b8 2009-09-29 jas screeninit();
99 113867b8 2009-09-29 jas if(osx.screenimage == nil)
100 113867b8 2009-09-29 jas panic("cannot create OS X screen");
102 113867b8 2009-09-29 jas return osx.screenimage;
106 113867b8 2009-09-29 jas _screeninit(void)
110 113867b8 2009-09-29 jas Rectangle r;
111 113867b8 2009-09-29 jas int havemin;
113 113867b8 2009-09-29 jas memimageinit();
115 113867b8 2009-09-29 jas cgr = CGDisplayBounds(CGMainDisplayID());
116 113867b8 2009-09-29 jas osx.fullscreenr = Rect(0, 0, cgr.size.width, cgr.size.height);
118 113867b8 2009-09-29 jas // Create the window.
119 113867b8 2009-09-29 jas r = Rect(0, 0, Dx(osx.fullscreenr)*2/3, Dy(osx.fullscreenr)*2/3);
120 113867b8 2009-09-29 jas havemin = 0;
121 113867b8 2009-09-29 jas if(osx.winsize && osx.winsize[0]){
122 113867b8 2009-09-29 jas if(parsewinsize(osx.winsize, &r, &havemin) < 0)
123 113867b8 2009-09-29 jas sysfatal("%r");
125 113867b8 2009-09-29 jas if(!havemin)
126 113867b8 2009-09-29 jas r = rectaddpt(r, Pt((Dx(osx.fullscreenr)-Dx(r))/2, (Dy(osx.fullscreenr)-Dy(r))/2));
127 113867b8 2009-09-29 jas or = NSMakeRect(r.min.x, r.min.y, r.max.x, r.max.y);
128 113867b8 2009-09-29 jas osx.window = [[NSWindow alloc] initWithContentRect:or styleMask:WindowAttrs
129 113867b8 2009-09-29 jas backing:NSBackingStoreBuffered defer:NO screen:[NSScreen mainScreen]];
130 113867b8 2009-09-29 jas [osx.window setDelegate:[NSApp delegate]];
131 113867b8 2009-09-29 jas [osx.window setAcceptsMouseMovedEvents:YES];
133 113867b8 2009-09-29 jas P9View *view = [[P9View alloc] initWithFrame:or];
134 113867b8 2009-09-29 jas [osx.window setContentView:view];
135 113867b8 2009-09-29 jas [view release];
137 113867b8 2009-09-29 jas setlabel(osx.label);
140 113867b8 2009-09-29 jas // Finally, put the window on the screen.
141 113867b8 2009-09-29 jas eresized(0);
142 113867b8 2009-09-29 jas [osx.window makeKeyAndOrderFront:nil];
144 113867b8 2009-09-29 jas [NSCursor unhide];
147 113867b8 2009-09-29 jas static Rendez scr;
148 113867b8 2009-09-29 jas static QLock slock;
151 113867b8 2009-09-29 jas screeninit(void)
153 113867b8 2009-09-29 jas scr.l = &slock;
154 113867b8 2009-09-29 jas qlock(scr.l);
155 113867b8 2009-09-29 jas // proccreate(screenproc, nil, 256*1024);
156 113867b8 2009-09-29 jas screenproc(NULL);
157 113867b8 2009-09-29 jas while(osx.window == nil)
158 113867b8 2009-09-29 jas rsleep(&scr);
159 113867b8 2009-09-29 jas qunlock(scr.l);
163 113867b8 2009-09-29 jas screenproc(void *v)
165 113867b8 2009-09-29 jas qlock(scr.l);
166 113867b8 2009-09-29 jas _screeninit();
167 113867b8 2009-09-29 jas rwakeup(&scr);
168 113867b8 2009-09-29 jas qunlock(scr.l);
171 113867b8 2009-09-29 jas static ulong
174 113867b8 2009-09-29 jas return nsec()/1000000;
177 113867b8 2009-09-29 jas //static void
179 113867b8 2009-09-29 jas mouseevent(NSEvent *event)
184 113867b8 2009-09-29 jas op = [event locationInWindow];
186 113867b8 2009-09-29 jas osx.xy = subpt(Pt(op.x, op.y), osx.screenr.min);
189 113867b8 2009-09-29 jas switch([event type]){
190 113867b8 2009-09-29 jas case NSScrollWheel:;
191 113867b8 2009-09-29 jas CGFloat delta = [event deltaY];
192 113867b8 2009-09-29 jas if(delta > 0)
198 113867b8 2009-09-29 jas case NSLeftMouseDown:
199 113867b8 2009-09-29 jas case NSRightMouseDown:
200 113867b8 2009-09-29 jas case NSOtherMouseDown:
201 113867b8 2009-09-29 jas case NSLeftMouseUp:
202 113867b8 2009-09-29 jas case NSRightMouseUp:
203 113867b8 2009-09-29 jas case NSOtherMouseUp:;
204 113867b8 2009-09-29 jas NSInteger but;
205 113867b8 2009-09-29 jas NSUInteger mod;
206 113867b8 2009-09-29 jas but = [event buttonNumber];
207 113867b8 2009-09-29 jas mod = [event modifierFlags];
209 113867b8 2009-09-29 jas // OS X swaps button 2 and 3
210 113867b8 2009-09-29 jas but = (but & ~6) | ((but & 4)>>1) | ((but&2)<<1);
211 113867b8 2009-09-29 jas but = mouseswap(but);
213 113867b8 2009-09-29 jas // Apply keyboard modifiers and pretend it was a real mouse button.
214 113867b8 2009-09-29 jas // (Modifiers typed while holding the button go into kbuttons,
215 113867b8 2009-09-29 jas // but this one does not.)
216 113867b8 2009-09-29 jas if(but == 1){
217 113867b8 2009-09-29 jas if(mod & NSAlternateKeyMask) {
218 113867b8 2009-09-29 jas // Take the ALT away from the keyboard handler.
219 113867b8 2009-09-29 jas if(osx.kalting) {
220 113867b8 2009-09-29 jas osx.kalting = 0;
221 113867b8 2009-09-29 jas keystroke(Kalt);
225 113867b8 2009-09-29 jas else if(mod & NSCommandKeyMask)
228 113867b8 2009-09-29 jas osx.buttons = but;
231 113867b8 2009-09-29 jas case NSMouseMoved:
232 113867b8 2009-09-29 jas case NSLeftMouseDragged:
233 113867b8 2009-09-29 jas case NSRightMouseDragged:
234 113867b8 2009-09-29 jas case NSOtherMouseDragged:
241 113867b8 2009-09-29 jas mousetrack(osx.xy.x, osx.xy.y, osx.buttons|osx.kbuttons|wheel, msec());
244 113867b8 2009-09-29 jas static int keycvt[] =
246 113867b8 2009-09-29 jas [QZ_IBOOK_ENTER] '\n',
247 113867b8 2009-09-29 jas [QZ_RETURN] '\n',
248 113867b8 2009-09-29 jas [QZ_ESCAPE] 27,
249 113867b8 2009-09-29 jas [QZ_BACKSPACE] '\b',
250 113867b8 2009-09-29 jas [QZ_LALT] Kalt,
251 113867b8 2009-09-29 jas [QZ_LCTRL] Kctl,
252 113867b8 2009-09-29 jas [QZ_LSHIFT] Kshift,
253 113867b8 2009-09-29 jas [QZ_F1] KF+1,
254 113867b8 2009-09-29 jas [QZ_F2] KF+2,
255 113867b8 2009-09-29 jas [QZ_F3] KF+3,
256 113867b8 2009-09-29 jas [QZ_F4] KF+4,
257 113867b8 2009-09-29 jas [QZ_F5] KF+5,
258 113867b8 2009-09-29 jas [QZ_F6] KF+6,
259 113867b8 2009-09-29 jas [QZ_F7] KF+7,
260 113867b8 2009-09-29 jas [QZ_F8] KF+8,
261 113867b8 2009-09-29 jas [QZ_F9] KF+9,
262 113867b8 2009-09-29 jas [QZ_F10] KF+10,
263 113867b8 2009-09-29 jas [QZ_F11] KF+11,
264 113867b8 2009-09-29 jas [QZ_F12] KF+12,
265 113867b8 2009-09-29 jas [QZ_INSERT] Kins,
266 113867b8 2009-09-29 jas [QZ_DELETE] 0x7F,
267 113867b8 2009-09-29 jas [QZ_HOME] Khome,
268 113867b8 2009-09-29 jas [QZ_END] Kend,
269 113867b8 2009-09-29 jas [QZ_KP_PLUS] '+',
270 113867b8 2009-09-29 jas [QZ_KP_MINUS] '-',
271 113867b8 2009-09-29 jas [QZ_TAB] '\t',
272 113867b8 2009-09-29 jas [QZ_PAGEUP] Kpgup,
273 113867b8 2009-09-29 jas [QZ_PAGEDOWN] Kpgdown,
274 113867b8 2009-09-29 jas [QZ_UP] Kup,
275 113867b8 2009-09-29 jas [QZ_DOWN] Kdown,
276 113867b8 2009-09-29 jas [QZ_LEFT] Kleft,
277 113867b8 2009-09-29 jas [QZ_RIGHT] Kright,
278 113867b8 2009-09-29 jas [QZ_KP_MULTIPLY] '*',
279 113867b8 2009-09-29 jas [QZ_KP_DIVIDE] '/',
280 113867b8 2009-09-29 jas [QZ_KP_ENTER] '\n',
281 113867b8 2009-09-29 jas [QZ_KP_PERIOD] '.',
282 113867b8 2009-09-29 jas [QZ_KP0] '0',
283 113867b8 2009-09-29 jas [QZ_KP1] '1',
284 113867b8 2009-09-29 jas [QZ_KP2] '2',
285 113867b8 2009-09-29 jas [QZ_KP3] '3',
286 113867b8 2009-09-29 jas [QZ_KP4] '4',
287 113867b8 2009-09-29 jas [QZ_KP5] '5',
288 113867b8 2009-09-29 jas [QZ_KP6] '6',
289 113867b8 2009-09-29 jas [QZ_KP7] '7',
290 113867b8 2009-09-29 jas [QZ_KP8] '8',
291 113867b8 2009-09-29 jas [QZ_KP9] '9',
294 113867b8 2009-09-29 jas //static void
296 113867b8 2009-09-29 jas kbdevent(NSEvent *event)
299 113867b8 2009-09-29 jas UInt32 code;
303 113867b8 2009-09-29 jas ch = [[event characters] characterAtIndex:0];
304 113867b8 2009-09-29 jas code = [event keyCode];
305 113867b8 2009-09-29 jas mod = [event modifierFlags];
307 113867b8 2009-09-29 jas switch([event type]){
308 113867b8 2009-09-29 jas case NSKeyDown:
309 113867b8 2009-09-29 jas osx.kalting = 0;
310 113867b8 2009-09-29 jas if(mod == NSCommandKeyMask){
311 113867b8 2009-09-29 jas if(ch == 'F' || ch == 'f'){
312 113867b8 2009-09-29 jas if(osx.isfullscreen && msec() - osx.fullscreentime > 500)
313 113867b8 2009-09-29 jas fullscreen(0);
317 113867b8 2009-09-29 jas // Pass most Cmd keys through as Kcmd + ch.
318 113867b8 2009-09-29 jas // OS X interprets a few no matter what we do,
319 113867b8 2009-09-29 jas // so it is useless to pass them through as keystrokes too.
320 113867b8 2009-09-29 jas switch(ch) {
321 113867b8 2009-09-29 jas case 'm': // minimize window
322 113867b8 2009-09-29 jas case 'h': // hide window
323 113867b8 2009-09-29 jas case 'H': // hide others
324 113867b8 2009-09-29 jas case 'q': // quit
327 113867b8 2009-09-29 jas if(' ' <= ch && ch <= '~') {
328 113867b8 2009-09-29 jas keystroke(Kcmd + ch);
334 113867b8 2009-09-29 jas if(code < nelem(keycvt) && keycvt[code])
335 113867b8 2009-09-29 jas k = keycvt[code];
337 113867b8 2009-09-29 jas keystroke(k);
339 113867b8 2009-09-29 jas keystroke(ch);
343 113867b8 2009-09-29 jas case NSFlagsChanged:
344 113867b8 2009-09-29 jas if(!osx.buttons && !osx.kbuttons){
345 113867b8 2009-09-29 jas if(mod == NSAlternateKeyMask) {
346 113867b8 2009-09-29 jas osx.kalting = 1;
347 113867b8 2009-09-29 jas keystroke(Kalt);
352 113867b8 2009-09-29 jas // If the mouse button is being held down, treat
353 113867b8 2009-09-29 jas // changes in the keyboard modifiers as changes
354 113867b8 2009-09-29 jas // in the mouse buttons.
355 113867b8 2009-09-29 jas osx.kbuttons = 0;
356 113867b8 2009-09-29 jas if(mod & NSAlternateKeyMask)
357 113867b8 2009-09-29 jas osx.kbuttons |= 2;
358 113867b8 2009-09-29 jas if(mod & NSCommandKeyMask)
359 113867b8 2009-09-29 jas osx.kbuttons |= 4;
360 113867b8 2009-09-29 jas mousetrack(osx.xy.x, osx.xy.y, osx.buttons|osx.kbuttons, msec());
366 113867b8 2009-09-29 jas //static void
368 113867b8 2009-09-29 jas eresized(int new)
370 113867b8 2009-09-29 jas Memimage *m;
373 113867b8 2009-09-29 jas Rectangle r;
375 113867b8 2009-09-29 jas CGDataProviderRef provider;
376 113867b8 2009-09-29 jas CGImageRef image;
377 113867b8 2009-09-29 jas CGColorSpaceRef cspace;
379 113867b8 2009-09-29 jas or = [[osx.window contentView] bounds];
380 113867b8 2009-09-29 jas r = Rect(or.origin.x, or.origin.y, or.size.width, or.size.height);
381 113867b8 2009-09-29 jas if(Dx(r) == Dx(osx.screenr) && Dy(r) == Dy(osx.screenr)){
382 113867b8 2009-09-29 jas // No need to make new image.
383 113867b8 2009-09-29 jas osx.screenr = r;
387 113867b8 2009-09-29 jas chan = XBGR32;
388 113867b8 2009-09-29 jas m = allocmemimage(Rect(0, 0, Dx(r), Dy(r)), chan);
389 113867b8 2009-09-29 jas if(m == nil)
390 113867b8 2009-09-29 jas panic("allocmemimage: %r");
391 113867b8 2009-09-29 jas if(m->data == nil)
392 113867b8 2009-09-29 jas panic("m->data == nil");
393 113867b8 2009-09-29 jas bpl = bytesperline(r, 32);
394 113867b8 2009-09-29 jas provider = CGDataProviderCreateWithData(0,
395 113867b8 2009-09-29 jas m->data->bdata, Dy(r)*bpl, 0);
396 113867b8 2009-09-29 jas //cspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
397 113867b8 2009-09-29 jas cspace = CGColorSpaceCreateDeviceRGB();
398 113867b8 2009-09-29 jas image = CGImageCreate(Dx(r), Dy(r), 8, 32, bpl,
400 113867b8 2009-09-29 jas kCGImageAlphaNoneSkipLast,
401 113867b8 2009-09-29 jas provider, 0, 0, kCGRenderingIntentDefault);
402 113867b8 2009-09-29 jas CGColorSpaceRelease(cspace);
403 113867b8 2009-09-29 jas CGDataProviderRelease(provider); // CGImageCreate did incref
405 113867b8 2009-09-29 jas mouserect = m->r;
407 113867b8 2009-09-29 jas mouseresized = 1;
408 113867b8 2009-09-29 jas mousetrack(osx.xy.x, osx.xy.y, osx.buttons|osx.kbuttons, msec());
410 113867b8 2009-09-29 jas // termreplacescreenimage(m);
411 113867b8 2009-09-29 jas _drawreplacescreenimage(m); // frees old osx.screenimage if any
412 113867b8 2009-09-29 jas if(osx.image)
413 113867b8 2009-09-29 jas CGImageRelease(osx.image);
414 113867b8 2009-09-29 jas osx.image = image;
415 113867b8 2009-09-29 jas osx.screenimage = m;
416 113867b8 2009-09-29 jas osx.screenr = r;
420 113867b8 2009-09-29 jas flushproc(void *v)
423 113867b8 2009-09-29 jas if(osx.needflush && osx.windowctx && canqlock(&osx.flushlock)){
424 113867b8 2009-09-29 jas if(osx.windowctx){
425 113867b8 2009-09-29 jas CGContextFlush(osx.windowctx);
426 113867b8 2009-09-29 jas osx.needflush = 0;
428 113867b8 2009-09-29 jas qunlock(&osx.flushlock);
430 113867b8 2009-09-29 jas usleep(33333);
435 113867b8 2009-09-29 jas _flushmemscreen(Rectangle r)
438 113867b8 2009-09-29 jas CGImageRef subimg;
440 113867b8 2009-09-29 jas qlock(&osx.flushlock);
441 113867b8 2009-09-29 jas if(osx.windowctx == nil){
442 113867b8 2009-09-29 jas osx.windowctx = [[osx.window graphicsContext] graphicsPort];
443 113867b8 2009-09-29 jas // [osx.window flushWindow];
444 113867b8 2009-09-29 jas // proccreate(flushproc, nil, 256*1024);
447 113867b8 2009-09-29 jas cgr.origin.x = r.min.x;
448 113867b8 2009-09-29 jas cgr.origin.y = r.min.y;
449 113867b8 2009-09-29 jas cgr.size.width = Dx(r);
450 113867b8 2009-09-29 jas cgr.size.height = Dy(r);
451 113867b8 2009-09-29 jas subimg = CGImageCreateWithImageInRect(osx.image, cgr);
452 113867b8 2009-09-29 jas cgr.origin.y = Dy(osx.screenr) - r.max.y; // XXX how does this make any sense?
453 113867b8 2009-09-29 jas CGContextDrawImage(osx.windowctx, cgr, subimg);
454 113867b8 2009-09-29 jas osx.needflush = 1;
455 113867b8 2009-09-29 jas qunlock(&osx.flushlock);
456 113867b8 2009-09-29 jas CGImageRelease(subimg);
460 113867b8 2009-09-29 jas activated(int active)
462 113867b8 2009-09-29 jas osx.active = active;
466 113867b8 2009-09-29 jas fullscreen(int wascmd)
468 113867b8 2009-09-29 jas NSView *view = [osx.window contentView];
470 113867b8 2009-09-29 jas if(osx.isfullscreen){
471 113867b8 2009-09-29 jas [view exitFullScreenModeWithOptions:nil];
472 113867b8 2009-09-29 jas osx.isfullscreen = 0;
474 113867b8 2009-09-29 jas [view enterFullScreenMode:[osx.window screen] withOptions:nil];
475 113867b8 2009-09-29 jas osx.isfullscreen = 1;
476 113867b8 2009-09-29 jas osx.fullscreentime = msec();
478 113867b8 2009-09-29 jas eresized(1);
482 113867b8 2009-09-29 jas setmouse(Point p)
484 113867b8 2009-09-29 jas CGPoint cgp;
486 113867b8 2009-09-29 jas cgp.x = p.x + osx.screenr.min.x;
487 113867b8 2009-09-29 jas cgp.y = p.y + osx.screenr.min.y;
488 113867b8 2009-09-29 jas CGWarpMouseCursorPosition(cgp);
492 113867b8 2009-09-29 jas setcursor(Cursor *c)
494 113867b8 2009-09-29 jas NSImage *image;
495 113867b8 2009-09-29 jas NSBitmapImageRep *bitmap;
496 113867b8 2009-09-29 jas NSCursor *nsc;
497 113867b8 2009-09-29 jas unsigned char *planes[5];
500 113867b8 2009-09-29 jas if(c == nil){
501 113867b8 2009-09-29 jas [NSCursor pop];
505 113867b8 2009-09-29 jas image = [[NSImage alloc] initWithSize:NSMakeSize(16.0, 16.0)];
506 113867b8 2009-09-29 jas bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
507 113867b8 2009-09-29 jas pixelsWide:16
508 113867b8 2009-09-29 jas pixelsHigh:16
509 113867b8 2009-09-29 jas bitsPerSample:1
510 113867b8 2009-09-29 jas samplesPerPixel:2
511 113867b8 2009-09-29 jas hasAlpha:YES
512 113867b8 2009-09-29 jas isPlanar:YES
513 113867b8 2009-09-29 jas colorSpaceName:NSCalibratedWhiteColorSpace
514 113867b8 2009-09-29 jas bytesPerRow:2
515 113867b8 2009-09-29 jas bitsPerPixel:1];
517 113867b8 2009-09-29 jas [bitmap getBitmapDataPlanes:planes];
519 113867b8 2009-09-29 jas for(i=0; i<16; i++){
520 113867b8 2009-09-29 jas planes[0][i] = ((ushort*)c->set)[i];
521 113867b8 2009-09-29 jas planes[1][i] = planes[0][i] | ((ushort*)c->clr)[i];
524 113867b8 2009-09-29 jas [image addRepresentation:bitmap];
526 113867b8 2009-09-29 jas nsc = [[NSCursor alloc] initWithImage:image
527 113867b8 2009-09-29 jas hotSpot:NSMakePoint(c->offset.x, c->offset.y)];
530 113867b8 2009-09-29 jas [image release];
531 113867b8 2009-09-29 jas [bitmap release];
532 113867b8 2009-09-29 jas [nsc release];
536 113867b8 2009-09-29 jas getcolor(ulong i, ulong *r, ulong *g, ulong *b)
541 113867b8 2009-09-29 jas *r = (v>>16)&0xFF;
542 113867b8 2009-09-29 jas *g = (v>>8)&0xFF;
543 113867b8 2009-09-29 jas *b = v&0xFF;
547 113867b8 2009-09-29 jas setcolor(ulong i, ulong r, ulong g, ulong b)
555 113867b8 2009-09-29 jas hwdraw(Memdrawparam *p)
562 113867b8 2009-09-29 jas char buf[SnarfSize];
563 113867b8 2009-09-29 jas Rune rbuf[SnarfSize];
564 113867b8 2009-09-29 jas NSPasteboard *apple;
568 113867b8 2009-09-29 jas getsnarf(void)
570 113867b8 2009-09-29 jas char *s, *t;
571 113867b8 2009-09-29 jas NSArray *types;
572 113867b8 2009-09-29 jas NSString *string;
573 113867b8 2009-09-29 jas NSData * data;
574 113867b8 2009-09-29 jas NSUInteger ndata;
576 113867b8 2009-09-29 jas /* fprint(2, "applegetsnarf\n"); */
577 113867b8 2009-09-29 jas qlock(&clip.lk);
579 113867b8 2009-09-29 jas clip.apple = [NSPasteboard generalPasteboard];
580 113867b8 2009-09-29 jas types = [clip.apple types];
582 113867b8 2009-09-29 jas string = [clip.apple stringForType:NSStringPboardType];
583 113867b8 2009-09-29 jas if(string == nil){
584 113867b8 2009-09-29 jas fprint(2, "apple pasteboard get item type failed\n");
585 113867b8 2009-09-29 jas qunlock(&clip.lk);
589 113867b8 2009-09-29 jas data = [string dataUsingEncoding:NSUnicodeStringEncoding];
590 113867b8 2009-09-29 jas if(data != nil){
591 113867b8 2009-09-29 jas ndata = [data length];
592 113867b8 2009-09-29 jas qunlock(&clip.lk);
593 113867b8 2009-09-29 jas s = smprint("%.*S", ndata/2, (Rune*)[data bytes]);
594 113867b8 2009-09-29 jas for(t=s; *t; t++)
595 113867b8 2009-09-29 jas if(*t == '\r')
600 113867b8 2009-09-29 jas qunlock(&clip.lk);
605 113867b8 2009-09-29 jas putsnarf(char *s)
607 113867b8 2009-09-29 jas NSArray *pboardTypes;
608 113867b8 2009-09-29 jas NSString *string;
610 113867b8 2009-09-29 jas /* fprint(2, "appleputsnarf\n"); */
612 113867b8 2009-09-29 jas if(strlen(s) >= SnarfSize)
614 113867b8 2009-09-29 jas qlock(&clip.lk);
615 113867b8 2009-09-29 jas strcpy(clip.buf, s);
616 113867b8 2009-09-29 jas runesnprint(clip.rbuf, nelem(clip.rbuf), "%s", s);
618 113867b8 2009-09-29 jas pboardTypes = [NSArray arrayWithObject:NSStringPboardType];
620 113867b8 2009-09-29 jas clip.apple = [NSPasteboard generalPasteboard];
621 113867b8 2009-09-29 jas [clip.apple declareTypes:pboardTypes owner:nil];
623 113867b8 2009-09-29 jas assert(sizeof(clip.rbuf[0]) == 2);
624 113867b8 2009-09-29 jas string = [NSString stringWithCharacters:clip.rbuf length:runestrlen(clip.rbuf)*2];
625 113867b8 2009-09-29 jas if(string == nil){
626 113867b8 2009-09-29 jas fprint(2, "apple pasteboard data create failed\n");
627 113867b8 2009-09-29 jas qunlock(&clip.lk);
630 113867b8 2009-09-29 jas if(![clip.apple setString:string forType:NSStringPboardType]){
631 113867b8 2009-09-29 jas fprint(2, "apple pasteboard putitem failed\n");
632 113867b8 2009-09-29 jas qunlock(&clip.lk);
635 113867b8 2009-09-29 jas qunlock(&clip.lk);
639 113867b8 2009-09-29 jas setlabel(char *label)
641 113867b8 2009-09-29 jas CFStringRef cs;
642 113867b8 2009-09-29 jas cs = CFStringCreateWithBytes(nil, (uchar*)label, strlen(osx.label), kCFStringEncodingUTF8, false);
643 113867b8 2009-09-29 jas [osx.window setTitle:(NSString*)cs];
644 113867b8 2009-09-29 jas CFRelease(cs);
648 113867b8 2009-09-29 jas kicklabel(char *label)
652 113867b8 2009-09-29 jas p = strdup(label);
653 113867b8 2009-09-29 jas if(p == nil)
655 113867b8 2009-09-29 jas qlock(&osx.labellock);
656 113867b8 2009-09-29 jas free(osx.label);
657 113867b8 2009-09-29 jas osx.label = p;
658 113867b8 2009-09-29 jas qunlock(&osx.labellock);
660 113867b8 2009-09-29 jas setlabel(label);
663 113867b8 2009-09-29 jas // static void
665 113867b8 2009-09-29 jas seticon(void)
667 113867b8 2009-09-29 jas NSImage *im;
670 113867b8 2009-09-29 jas d = [[NSData alloc] initWithBytes:glenda_png length:(sizeof glenda_png)];
671 113867b8 2009-09-29 jas im = [[NSImage alloc] initWithData:d];
673 113867b8 2009-09-29 jas NSLog(@"here");
674 113867b8 2009-09-29 jas [NSApp setApplicationIconImage:im];
675 113867b8 2009-09-29 jas [[NSApp dockTile] setShowsApplicationBadge:YES];
676 113867b8 2009-09-29 jas [[NSApp dockTile] display];
678 113867b8 2009-09-29 jas [d release];
679 113867b8 2009-09-29 jas [im release];