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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
|
/*
* Cocoa's event loop must be in main thread.
*
* Unless otherwise stated, all coordinate systems
* are bottom-left-based.
*/
#define Cursor OSXCursor
#define Point OSXPoint
#define Rect OSXRect
#import <Cocoa/Cocoa.h>
#undef Cursor
#undef Point
#undef Rect
#include <u.h>
#include <libc.h>
#include "cocoa-thread.h"
#include <draw.h>
#include <memdraw.h>
#include <keyboard.h>
#include <cursor.h>
#include "cocoa-screen.h"
#include "osx-keycodes.h"
#include "devdraw.h"
#include "bigarrow.h"
#include "glendapng.h"
// Use non-deprecated names.
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
#define NSKeyDown NSEventTypeKeyDown
#define NSShiftKeyMask NSEventModifierFlagShift
#define NSAlternateKeyMask NSEventModifierFlagOption
#define NSCommandKeyMask NSEventModifierFlagCommand
#define NSResizableWindowMask NSWindowStyleMaskResizable
#define NSLeftMouseDown NSEventTypeLeftMouseDown
#define NSLeftMouseUp NSEventTypeLeftMouseUp
#define NSRightMouseDown NSEventTypeRightMouseDown
#define NSRightMouseUp NSEventTypeRightMouseUp
#define NSOtherMouseDown NSEventTypeOtherMouseDown
#define NSOtherMouseUp NSEventTypeOtherMouseUp
#define NSScrollWheel NSEventTypeScrollWheel
#define NSMouseMoved NSEventTypeMouseMoved
#define NSLeftMouseDragged NSEventTypeLeftMouseDragged
#define NSRightMouseDragged NSEventTypeRightMouseDragged
#define NSOtherMouseDragged NSEventTypeOtherMouseDragged
#define NSCompositeCopy NSCompositingOperationCopy
#define NSCompositeSourceIn NSCompositingOperationSourceIn
#define NSFlagsChanged NSEventTypeFlagsChanged
#define NSTitledWindowMask NSWindowStyleMaskTitled
#define NSClosableWindowMask NSWindowStyleMaskClosable
#define NSMiniaturizableWindowMask NSWindowStyleMaskMiniaturizable
#define NSBorderlessWindowMask NSWindowStyleMaskBorderless
#endif
AUTOFRAMEWORK(Cocoa)
#define LOG if(0)NSLog
#define panic sysfatal
int usegestures = 0;
int useliveresizing = 0;
int useoldfullscreen = 0;
int usebigarrow = 0;
static void setprocname(const char*);
/*
* By default, devdraw uses retina displays.
* Set devdrawretina=0 in the environment to override.
*/
int devdrawretina = 1;
void
usage(void)
{
fprint(2, "usage: devdraw (don't run directly)\n");
threadexitsall("usage");
}
@interface appdelegate : NSObject<NSApplicationDelegate,NSWindowDelegate> @end
NSObject<NSApplicationDelegate,NSWindowDelegate> *myApp;
void
threadmain(int argc, char **argv)
{
char *envvar;
/*
* Move the protocol off stdin/stdout so that
* any inadvertent prints don't screw things up.
*/
dup(0,3);
dup(1,4);
close(0);
close(1);
open("/dev/null", OREAD);
open("/dev/null", OWRITE);
ARGBEGIN{
case 'D': /* for good ps -a listings */
break;
case 'f':
useoldfullscreen = 1;
break;
case 'g':
usegestures = 1;
break;
case 'b':
usebigarrow = 1;
break;
default:
usage();
}ARGEND
setprocname(argv0);
if (envvar = getenv("devdrawretina"))
devdrawretina = atoi(envvar) > 0;
if(OSX_VERSION < 100700)
[NSAutoreleasePool new];
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
myApp = [appdelegate new];
[NSApp setDelegate:myApp];
[NSApp run];
}
#define WIN win.ofs[win.isofs]
struct
{
NSWindow *ofs[2]; /* ofs[1] for old fullscreen; ofs[0] else */
int isofs;
int isnfs;
NSView *content;
NSBitmapImageRep *img;
int needimg;
int deferflush;
NSCursor *cursor;
CGFloat topointscale;
CGFloat topixelscale;
} win;
struct
{
NSCursor *bigarrow;
int kbuttons;
int mbuttons;
NSPoint mpos;
int mscroll;
int willactivate;
} in;
static void hidebars(int);
static void flushimg(NSRect);
static void autoflushwin(int);
static void flushwin(void);
static void followzoombutton(NSRect);
static void getmousepos(void);
static void makeicon(void);
static void makemenu(void);
static void makewin(char*);
static void sendmouse(void);
static void kicklabel0(char*);
static void setcursor0(Cursor*);
static void togglefs(void);
static void acceptresizing(int);
static NSCursor* makecursor(Cursor*);
static NSSize winsizepixels();
static NSSize winsizepoints();
static NSRect scalerect(NSRect, CGFloat);
static NSPoint scalepoint(NSPoint, CGFloat);
static NSRect dilate(NSRect);
@implementation appdelegate
- (void)applicationDidFinishLaunching:(id)arg
{
in.bigarrow = makecursor(&bigarrow);
makeicon();
makemenu();
[NSApplication
detachDrawingThread:@selector(callservep9p:)
toTarget:[self class] withObject:nil];
}
- (void)windowDidBecomeKey:(id)arg
{
getmousepos();
sendmouse();
}
- (void)windowDidResize:(id)arg
{
getmousepos();
sendmouse();
}
- (void)windowWillStartLiveResize:(id)arg
{
if(useliveresizing == 0)
[win.content setHidden:YES];
}
- (void)windowDidEndLiveResize:(id)arg
{
if(useliveresizing == 0)
[win.content setHidden:NO];
}
- (void)windowDidChangeScreen:(id)arg
{
if(win.isnfs || win.isofs)
hidebars(1);
[win.ofs[1] setFrame:[[WIN screen] frame] display:YES];
}
- (BOOL)windowShouldZoom:(id)arg toFrame:(NSRect)r
{
followzoombutton(r);
return YES;
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(id)arg
{
return YES;
}
- (void)applicationDidBecomeActive:(id)arg{ in.willactivate = 0;}
- (void)windowWillEnterFullScreen:(id)arg{ acceptresizing(1);}
- (void)windowDidEnterFullScreen:(id)arg{ win.isnfs = 1; hidebars(1);}
- (void)windowWillExitFullScreen:(id)arg{ win.isnfs = 0; hidebars(0);}
- (void)windowDidExitFullScreen:(id)arg
{
NSButton *b;
b = [WIN standardWindowButton:NSWindowMiniaturizeButton];
if([b isEnabled] == 0){
[b setEnabled:YES];
hidebars(0);
}
}
- (void)windowWillClose:(id)arg
{
autoflushwin(0); /* can crash otherwise */
}
+ (void)callservep9p:(id)arg
{
servep9p();
[NSApp terminate:self];
}
- (void)plumbmanual:(id)arg
{
if(fork() != 0)
return;
execl("plumb", "plumb", "devdraw(1)", nil);
}
+ (void)callflushwin:(id)arg{ flushwin();}
- (void)calltogglefs:(id)arg{ togglefs();}
+ (void)callflushimg:(NSValue*)v{ flushimg([v rectValue]);}
+ (void)callmakewin:(NSValue*)v{ makewin([v pointerValue]);}
+ (void)callsetcursor0:(NSValue*)v{ setcursor0([v pointerValue]);}
+ (void)callkicklabel0:(NSValue*)v{ kicklabel0([v pointerValue]);}
@end
static Memimage* initimg(void);
Memimage*
attachscreen(char *label, char *winsize)
{
static int first = 1;
if(first)
first = 0;
else
panic("attachscreen called twice");
if(label == nil)
label = "gnot a label";
if(strcmp(label, "page") == 0)
useliveresizing = 1;
/*
* Create window in main thread, else no cursor
* change while resizing.
*/
[appdelegate
performSelectorOnMainThread:@selector(callmakewin:)
withObject:[NSValue valueWithPointer:winsize]
waitUntilDone:YES];
// makewin(winsize);
kicklabel(label);
return initimg();
}
@interface appwin : NSWindow @end
@interface contentview : NSView @end
@implementation appwin
- (NSTimeInterval)animationResizeTime:(NSRect)r
{
return 0;
}
- (BOOL)canBecomeKeyWindow
{
return YES; /* else no keyboard for old fullscreen */
}
- (void)makeKeyAndOrderFront:(id)arg
{
LOG(@"makeKeyAndOrderFront");
autoflushwin(1);
[win.content setHidden:NO];
[super makeKeyAndOrderFront:arg];
}
- (void)miniaturize:(id)arg
{
[super miniaturize:arg];
[NSApp hide:nil];
[win.content setHidden:YES];
autoflushwin(0);
}
- (void)deminiaturize:(id)arg
{
autoflushwin(1);
[win.content setHidden:NO];
[super deminiaturize:arg];
}
- (NSDragOperation)draggingEntered:(id)arg
{
NSPasteboard *b;
NSDragOperation op;
op = [arg draggingSourceOperationMask];
b = [arg draggingPasteboard];
if([[b types] containsObject:NSFilenamesPboardType])
if(op&NSDragOperationLink)
return NSDragOperationLink;
return NSDragOperationNone;
}
- (BOOL)performDragOperation:(id)arg
{
NSPasteboard *b;
NSArray *files;
int i, n;
b = [arg draggingPasteboard];
if(![[b types] containsObject:NSFilenamesPboardType])
return NO;
files = [b propertyListForType:NSFilenamesPboardType];
n = [files count];
for(i=0; i<n; i++)
if(fork() == 0)
execl("macedit", "macedit", [[files objectAtIndex:i] UTF8String], nil);
return YES;
}
@end
double
min(double a, double b)
{
return a<b? a : b;
}
enum
{
Winstyle = NSTitledWindowMask
| NSClosableWindowMask
| NSMiniaturizableWindowMask
| NSResizableWindowMask
};
static void
makewin(char *s)
{
NSRect r, sr;
NSWindow *w;
Rectangle wr;
int i, set;
sr = [[NSScreen mainScreen] frame];
r = [[NSScreen mainScreen] visibleFrame];
if(s && *s){
if(parsewinsize(s, &wr, &set) < 0)
sysfatal("%r");
}else{
wr = Rect(0, 0, sr.size.width*2/3, sr.size.height*2/3);
set = 0;
}
r.origin.x = wr.min.x;
r.origin.y = sr.size.height-wr.max.y; /* winsize is top-left-based */
r.size.width = min(Dx(wr), r.size.width);
r.size.height = min(Dy(wr), r.size.height);
r = [NSWindow contentRectForFrameRect:r
styleMask:Winstyle];
w = [[appwin alloc]
initWithContentRect:r
styleMask:Winstyle
backing:NSBackingStoreBuffered defer:NO];
[w setTitle:@"devdraw"];
if(!set)
[w center];
#if OSX_VERSION >= 100700
[w setCollectionBehavior:
NSWindowCollectionBehaviorFullScreenPrimary];
#endif
[w setContentMinSize:NSMakeSize(128,128)];
[w registerForDraggedTypes:[NSArray arrayWithObjects:
NSFilenamesPboardType, nil]];
win.ofs[0] = w;
win.ofs[1] = [[appwin alloc]
initWithContentRect:sr
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered defer:YES];
for(i=0; i<2; i++){
[win.ofs[i] setAcceptsMouseMovedEvents:YES];
[win.ofs[i] setDelegate:myApp];
[win.ofs[i] setDisplaysWhenScreenProfileChanges:NO];
}
win.isofs = 0;
win.content = [contentview new];
[WIN setContentView:win.content];
topwin();
}
static Memimage*
initimg(void)
{
Memimage *i;
NSSize size, ptsize;
Rectangle r;
size = winsizepixels();
LOG(@"initimg %.0f %.0f", size.width, size.height);
r = Rect(0, 0, size.width, size.height);
i = allocmemimage(r, XBGR32);
if(i == nil)
panic("allocmemimage: %r");
if(i->data == nil)
panic("i->data == nil");
win.img = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:&i->data->bdata
pixelsWide:Dx(r)
pixelsHigh:Dy(r)
bitsPerSample:8
samplesPerPixel:3
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:bytesperline(r, 32)
bitsPerPixel:32];
ptsize = winsizepoints();
[win.img setSize: ptsize];
win.topixelscale = size.width / ptsize.width;
win.topointscale = 1.0f / win.topixelscale;
// NOTE: This is not really the display DPI.
// On retina, topixelscale is 2; otherwise it is 1.
// This formula gives us 220 for retina, 110 otherwise.
// That's not quite right but it's close to correct.
// http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density#Apple
displaydpi = win.topixelscale * 110;
return i;
}
void
resizeimg(void)
{
[win.img release];
_drawreplacescreenimage(initimg());
mouseresized = 1;
sendmouse();
}
static void
waitimg(int msec)
{
NSDate *limit;
int n;
win.needimg = 1;
win.deferflush = 0;
n = 0;
limit = [NSDate dateWithTimeIntervalSinceNow:msec/1000.0];
do{
[[NSRunLoop currentRunLoop]
runMode:@"waiting image"
beforeDate:limit];
n++;
}while(win.needimg && [(NSDate*)[NSDate date] compare:limit]<0);
win.deferflush = win.needimg;
LOG(@"waitimg %s (%d loop)", win.needimg?"defer":"ok", n);
}
void
_flushmemscreen(Rectangle r)
{
static int n;
NSRect rect;
LOG(@"_flushmemscreen");
if(n==0){
n++;
return; /* to skip useless white init rect */
}else
if(n==1){
[WIN performSelectorOnMainThread:
@selector(makeKeyAndOrderFront:)
withObject:nil
waitUntilDone:NO];
n++;
}else
if([win.content canDraw] == 0)
return;
rect = NSMakeRect(r.min.x, r.min.y, Dx(r), Dy(r));
// This can get blocked behind responding to mouse events,
// which need to acquire the zlock, so let go of it during
// the flush. Perhaps the waitUntilDone:YES is wrong?
zunlock();
[appdelegate
performSelectorOnMainThread:@selector(callflushimg:)
withObject:[NSValue valueWithRect:rect]
waitUntilDone:YES
modes:[NSArray arrayWithObjects:
NSRunLoopCommonModes,
@"waiting image", nil]];
zlock();
}
static void drawimg(NSRect, uint);
static void drawresizehandle(void);
enum
{
Pixel = 1,
Barsize = 4*Pixel,
Cornersize = 3*Pixel,
Handlesize = 3*Barsize + 1*Pixel,
};
/*
* |rect| is in pixel coordinates.
*/
static void
flushimg(NSRect rect)
{
NSRect dr, r;
if([win.content lockFocusIfCanDraw] == 0)
return;
if(win.needimg){
if(!NSEqualSizes(scalerect(rect, win.topointscale).size, [win.img size])){
LOG(@"flushimg reject %.0f %.0f",
rect.size.width, rect.size.height);
[win.content unlockFocus];
return;
}
win.needimg = 0;
}else
win.deferflush = 1;
LOG(@"flushimg ok %.0f %.0f", rect.size.width, rect.size.height);
/*
* Unless we are inside "drawRect", we have to round
* the corners ourselves, if this is the custom.
* "NSCompositeSourceIn" can do that, but we don't
* apply it to the whole rectangle, because this
* slows down trackpad scrolling considerably in
* Acme.
*/
r = [win.content bounds];
rect = dilate(scalerect(rect, win.topointscale));
r.size.height -= Cornersize;
dr = NSIntersectionRect(r, rect);
LOG(@"r %.0f %.0f %.0f %.0f", r.origin.x, r.origin.y, rect.size.width, rect.size.height);
LOG(@"rect in points %f %f %.0f %.0f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
LOG(@"dr in points %f %f %.0f %.0f", dr.origin.x, dr.origin.y, dr.size.width, dr.size.height);
drawimg(dr, NSCompositeCopy);
r.origin.y = r.size.height;
r.size = NSMakeSize(Cornersize, Cornersize);
dr = NSIntersectionRect(r, rect);
drawimg(dr, NSCompositeSourceIn);
r.origin.x = [win.img size].width - Cornersize;
dr = NSIntersectionRect(r, rect);
drawimg(dr, NSCompositeSourceIn);
r.size.width = r.origin.x - Cornersize;
r.origin.x -= r.size.width;
dr = NSIntersectionRect(r, rect);
drawimg(dr, NSCompositeCopy);
if(OSX_VERSION<100700 && win.isofs==0){
r.origin.x = [win.img size].width - Handlesize;
r.origin.y = [win.img size].height - Handlesize;
r.size = NSMakeSize(Handlesize, Handlesize);
if(NSIntersectsRect(r, rect))
drawresizehandle();
}
[win.content unlockFocus];
}
static void
autoflushwin(int set)
{
static NSTimer *t;
if(set){
if(t)
return;
/*
* We need "NSRunLoopCommonModes", otherwise the
* timer will not fire during live resizing.
*/
t = [NSTimer
timerWithTimeInterval:0.033
target:[appdelegate class]
selector:@selector(callflushwin:) userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:t
forMode:NSRunLoopCommonModes];
}else{
[t invalidate];
t = nil;
win.deferflush = 0;
}
}
static void
flushwin(void)
{
if(win.deferflush && win.needimg==0){
[WIN flushWindow];
win.deferflush = 0;
}
}
/*
* |dr| is sized in points. What if I make it pixels?
*/
static void
drawimg(NSRect dr, uint op)
{
CGContextRef c;
CGImageRef i;
NSRect sr;
if(NSIsEmptyRect(dr))
return;
sr = [win.content convertRect:dr fromView:nil];
LOG(@"before dr: %f %f %f %f\n", dr.origin.x, dr.origin.y, dr.size.width, dr.size.height);
LOG(@"before sr: %f %f %f %f\n", sr.origin.x, sr.origin.y, sr.size.width, sr.size.height);
dr = scalerect(dr, win.topixelscale);
sr = scalerect(sr, win.topixelscale);
LOG(@"dr: %f %f %f %f\n", dr.origin.x, dr.origin.y, dr.size.width, dr.size.height);
LOG(@"sr: %f %f %f %f\n", sr.origin.x, sr.origin.y, sr.size.width, sr.size.height);
if(OSX_VERSION >= 100800){
i = CGImageCreateWithImageInRect([win.img CGImage], NSRectToCGRect(dr));
c = [[WIN graphicsContext] graphicsPort];
CGContextSaveGState(c);
if(op == NSCompositeSourceIn)
CGContextSetBlendMode(c, kCGBlendModeSourceIn);
LOG(@"wim.img size %f %f\n", [win.img size].width, [win.img size].height);
CGContextTranslateCTM(c, 0, [win.img size].height);
CGContextScaleCTM(c, win.topointscale, -win.topointscale);
CGContextDrawImage(c, NSRectToCGRect(sr), i);
CGContextRestoreGState(c);
CGImageRelease(i);
}else{
[win.img drawInRect:dr fromRect:sr
operation:op fraction:1
respectFlipped:YES hints:nil];
}
// NSFrameRect(dr);
}
static void
drawresizehandle(void)
{
NSColor *color[Barsize];
NSPoint a,b;
Point c;
int i,j;
c = Pt([win.img size].width, [win.img size].height);
[[WIN graphicsContext] setShouldAntialias:NO];
color[0] = [NSColor clearColor];
color[1] = [NSColor darkGrayColor];
color[2] = [NSColor lightGrayColor];
color[3] = [NSColor whiteColor];
for(i=1; i+Barsize <= Handlesize; )
for(j=0; j<Barsize; j++){
[color[j] setStroke];
i++;
a = NSMakePoint(c.x-i, c.y-1);
b = NSMakePoint(c.x-2, c.y+1-i);
[NSBezierPath strokeLineFromPoint:a toPoint:b];
}
}
static void getgesture(NSEvent*);
static void getkeyboard(NSEvent*);
static void getmouse(NSEvent*);
static void gettouch(NSEvent*, int);
static void updatecursor(void);
@implementation contentview
/*
* "drawRect" is called each time Cocoa needs an
* image, and each time we call "display". It is
* preceded by background painting, and followed by
* "flushWindow".
*/
- (void)drawRect:(NSRect)r
{
static int first = 1;
LOG(@"drawrect %.0f %.0f %.0f %.0f",
r.origin.x, r.origin.y, r.size.width, r.size.height);
if(first)
first = 0;
else
resizeimg();
if([WIN inLiveResize])
waitimg(100);
else
waitimg(500);
}
- (BOOL)isFlipped
{
return YES; /* to make the content's origin top left */
}
- (BOOL)acceptsFirstResponder
{
return YES; /* else no keyboard */
}
- (id)initWithFrame:(NSRect)r
{
[super initWithFrame:r];
[self setAcceptsTouchEvents:YES];
[self setHidden:YES]; /* to avoid early "drawRect" call */
return self;
}
- (void)setHidden:(BOOL)set
{
if(!set)
[WIN makeFirstResponder:self]; /* for keyboard focus */
[super setHidden:set];
}
- (void)cursorUpdate:(NSEvent*)e{ updatecursor();}
- (void)mouseMoved:(NSEvent*)e{ getmouse(e);}
- (void)mouseDown:(NSEvent*)e{ getmouse(e);}
- (void)mouseDragged:(NSEvent*)e{ getmouse(e);}
- (void)mouseUp:(NSEvent*)e{ getmouse(e);}
- (void)otherMouseDown:(NSEvent*)e{ getmouse(e);}
- (void)otherMouseDragged:(NSEvent*)e{ getmouse(e);}
- (void)otherMouseUp:(NSEvent*)e{ getmouse(e);}
- (void)rightMouseDown:(NSEvent*)e{ getmouse(e);}
- (void)rightMouseDragged:(NSEvent*)e{ getmouse(e);}
- (void)rightMouseUp:(NSEvent*)e{ getmouse(e);}
- (void)scrollWheel:(NSEvent*)e{ getmouse(e);}
- (void)keyDown:(NSEvent*)e{ getkeyboard(e);}
- (void)flagsChanged:(NSEvent*)e{ getkeyboard(e);}
- (void)magnifyWithEvent:(NSEvent*)e{ getgesture(e);}
- (void)touchesBeganWithEvent:(NSEvent*)e
{
gettouch(e, NSTouchPhaseBegan);
}
- (void)touchesMovedWithEvent:(NSEvent*)e
{
gettouch(e, NSTouchPhaseMoved);
}
- (void)touchesEndedWithEvent:(NSEvent*)e
{
gettouch(e, NSTouchPhaseEnded);
}
- (void)touchesCancelledWithEvent:(NSEvent*)e
{
gettouch(e, NSTouchPhaseCancelled);
}
@end
static int keycvt[] =
{
[QZ_IBOOK_ENTER]= '\n',
[QZ_RETURN]= '\n',
[QZ_ESCAPE]= 27,
[QZ_BACKSPACE]= '\b',
[QZ_LALT]= Kalt,
[QZ_LCTRL]= Kctl,
[QZ_LSHIFT]= Kshift,
[QZ_F1]= KF+1,
[QZ_F2]= KF+2,
[QZ_F3]= KF+3,
[QZ_F4]= KF+4,
[QZ_F5]= KF+5,
[QZ_F6]= KF+6,
[QZ_F7]= KF+7,
[QZ_F8]= KF+8,
[QZ_F9]= KF+9,
[QZ_F10]= KF+10,
[QZ_F11]= KF+11,
[QZ_F12]= KF+12,
[QZ_INSERT]= Kins,
[QZ_DELETE]= 0x7F,
[QZ_HOME]= Khome,
[QZ_END]= Kend,
[QZ_KP_PLUS]= '+',
[QZ_KP_MINUS]= '-',
[QZ_TAB]= '\t',
[QZ_PAGEUP]= Kpgup,
[QZ_PAGEDOWN]= Kpgdown,
[QZ_UP]= Kup,
[QZ_DOWN]= Kdown,
[QZ_LEFT]= Kleft,
[QZ_RIGHT]= Kright,
[QZ_KP_MULTIPLY]= '*',
[QZ_KP_DIVIDE]= '/',
[QZ_KP_ENTER]= '\n',
[QZ_KP_PERIOD]= '.',
[QZ_KP0]= '0',
[QZ_KP1]= '1',
[QZ_KP2]= '2',
[QZ_KP3]= '3',
[QZ_KP4]= '4',
[QZ_KP5]= '5',
[QZ_KP6]= '6',
[QZ_KP7]= '7',
[QZ_KP8]= '8',
[QZ_KP9]= '9',
};
@interface apptext : NSTextView @end
@implementation apptext
- (void)doCommandBySelector:(SEL)s{} /* Esc key beeps otherwise */
- (void)insertText:(id)arg{} /* to avoid a latency after some time */
@end
static void
interpretdeadkey(NSEvent *e)
{
static apptext *t;
if(t == nil)
t = [apptext new];
[t interpretKeyEvents:[NSArray arrayWithObject:e]];
}
static void
getkeyboard(NSEvent *e)
{
static int omod;
NSString *s;
char c;
int k, m;
uint code;
m = [e modifierFlags];
switch([e type]){
case NSKeyDown:
s = [e characters];
c = [s UTF8String][0];
interpretdeadkey(e);
if(m & NSCommandKeyMask){
if((m & NSShiftKeyMask) && 'a' <= c && c <= 'z')
c += 'A' - 'a';
if(' '<=c && c<='~')
keystroke(Kcmd+c);
break;
}
k = c;
code = [e keyCode];
if(code<nelem(keycvt) && keycvt[code])
k = keycvt[code];
if(k==0)
break;
if(k>0)
keystroke(k);
else
keystroke([s characterAtIndex:0]);
break;
case NSFlagsChanged:
if(in.mbuttons || in.kbuttons){
in.kbuttons = 0;
if(m & NSControlKeyMask)
in.kbuttons |= 1;
if(m & NSAlternateKeyMask)
in.kbuttons |= 2;
if(m & NSCommandKeyMask)
in.kbuttons |= 4;
sendmouse();
}else
if(m&NSAlternateKeyMask && (omod&NSAlternateKeyMask)==0)
keystroke(Kalt);
break;
default:
panic("getkey: unexpected event type");
}
omod = m;
}
/*
* Devdraw does not use NSTrackingArea, that often
* forgets to update the cursor on entering and on
* leaving the area, and that sometimes stops sending
* us MouseMove events, at least on OS X Lion.
*/
static void
updatecursor(void)
{
NSCursor *c;
int isdown, isinside;
isinside = NSPointInRect(in.mpos, [win.content bounds]);
isdown = (in.mbuttons || in.kbuttons);
if(win.cursor && (isinside || isdown))
c = win.cursor;
else if(isinside && usebigarrow)
c = in.bigarrow;
else
c = [NSCursor arrowCursor];
[c set];
/*
* Without this trick, we can come back from the dock
* with a resize cursor.
*/
if(OSX_VERSION >= 100700)
[NSCursor unhide];
}
static void
acceptresizing(int set)
{
uint old, style;
old = [WIN styleMask];
if((old | NSResizableWindowMask) != Winstyle)
return; /* when entering new fullscreen */
if(set)
style = Winstyle;
else
style = Winstyle & ~NSResizableWindowMask;
if(style != old)
[WIN setStyleMask:style];
}
static void
getmousepos(void)
{
NSPoint p, q;
p = [WIN mouseLocationOutsideOfEventStream];
q = [win.content convertPoint:p fromView:nil];
/* q is in point coordinates. in.mpos is in pixels. */
q = scalepoint(q, win.topixelscale);
in.mpos.x = round(q.x);
in.mpos.y = round(q.y);
updatecursor();
if(win.isnfs || win.isofs)
hidebars(1);
else if(OSX_VERSION>=100700 && [WIN inLiveResize]==0){
if(p.x<12 && p.y<12 && p.x>2 && p.y>2)
acceptresizing(0);
else
acceptresizing(1);
}
}
static void
getmouse(NSEvent *e)
{
float d;
int b, m;
if([WIN isKeyWindow] == 0)
return;
getmousepos();
switch([e type]){
case NSLeftMouseDown:
case NSLeftMouseUp:
case NSOtherMouseDown:
case NSOtherMouseUp:
case NSRightMouseDown:
case NSRightMouseUp:
b = [NSEvent pressedMouseButtons];
b = b&~6 | (b&4)>>1 | (b&2)<<1;
b = mouseswap(b);
if(b == 1){
m = [e modifierFlags];
if(m & NSAlternateKeyMask){
abortcompose();
b = 2;
}else
if(m & NSCommandKeyMask)
b = 4;
}
in.mbuttons = b;
break;
case NSScrollWheel:
#if OSX_VERSION >= 100700
d = [e scrollingDeltaY];
#else
d = [e deltaY];
#endif
if(d>0)
in.mscroll = 8;
else
if(d<0)
in.mscroll = 16;
break;
case NSMouseMoved:
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged:
break;
default:
panic("getmouse: unexpected event type");
}
sendmouse();
}
#define Minpinch 0.02
static void
getgesture(NSEvent *e)
{
switch([e type]){
case NSEventTypeMagnify:
if(fabs([e magnification]) > Minpinch)
togglefs();
break;
}
}
static void sendclick(int);
static uint
msec(void)
{
return nsec()/1000000;
}
static void
gettouch(NSEvent *e, int type)
{
static int tapping;
static uint taptime;
NSSet *set;
int p;
switch(type){
case NSTouchPhaseBegan:
p = NSTouchPhaseTouching;
set = [e touchesMatchingPhase:p inView:nil];
if(set.count == 3){
tapping = 1;
taptime = msec();
}else
if(set.count > 3)
tapping = 0;
break;
case NSTouchPhaseMoved:
tapping = 0;
break;
case NSTouchPhaseEnded:
p = NSTouchPhaseTouching;
set = [e touchesMatchingPhase:p inView:nil];
if(set.count == 0){
if(tapping && msec()-taptime<400)
sendclick(2);
tapping = 0;
}
break;
case NSTouchPhaseCancelled:
break;
default:
panic("gettouch: unexpected event type");
}
}
static void
sendclick(int b)
{
in.mbuttons = b;
sendmouse();
in.mbuttons = 0;
sendmouse();
}
static void
sendmouse(void)
{
NSSize size;
int b;
size = winsizepixels();
mouserect = Rect(0, 0, size.width, size.height);
b = in.kbuttons | in.mbuttons | in.mscroll;
mousetrack(in.mpos.x, in.mpos.y, b, msec());
in.mscroll = 0;
}
/*
* |p| is in pixels.
*/
void
setmouse(Point p)
{
NSPoint q;
NSRect r;
if([NSApp isActive]==0 && in.willactivate==0)
return;
if([WIN inLiveResize])
return;
in.mpos = scalepoint(NSMakePoint(p.x, p.y), win.topointscale); // race condition
q = [win.content convertPoint:in.mpos toView:nil];
q = [WIN convertRectToScreen:NSMakeRect(q.x, q.y, 0, 0)].origin;
r = [[[NSScreen screens] objectAtIndex:0] frame];
q.y = r.size.height - q.y; /* Quartz is top-left-based here */
CGWarpMouseCursorPosition(NSPointToCGPoint(q));
CGAssociateMouseAndMouseCursorPosition(true);
}
/*
* |r| is in points.
*/
static void
followzoombutton(NSRect r)
{
NSRect wr;
Point p;
NSPoint pt;
wr = [WIN frame];
wr.origin.y += wr.size.height;
r.origin.y += r.size.height;
getmousepos();
pt.x = in.mpos.x;
pt.y = in.mpos.y;
pt = scalepoint(pt, win.topointscale);
pt.x = (r.origin.x - wr.origin.x) + pt.x;
pt.y = -(r.origin.y - wr.origin.y) + pt.y;
pt = scalepoint(pt, win.topixelscale);
p.x = pt.x;
p.y = pt.y;
setmouse(p);
}
static void
togglefs(void)
{
uint opt, tmp;
#if OSX_VERSION >= 100700
NSScreen *s, *s0;
s = [WIN screen];
s0 = [[NSScreen screens] objectAtIndex:0];
if((s==s0 && useoldfullscreen==0) || win.isnfs) {
[WIN toggleFullScreen:nil];
return;
}
#endif
[win.content retain];
[WIN orderOut:nil];
[WIN setContentView:nil];
win.isofs = ! win.isofs;
hidebars(win.isofs);
/*
* If we move the window from one space to another,
* ofs[0] and ofs[1] can be on different spaces.
* This "setCollectionBehavior" trick moves the
* window to the active space.
*/
opt = [WIN collectionBehavior];
tmp = opt | NSWindowCollectionBehaviorCanJoinAllSpaces;
[WIN setContentView:win.content];
[WIN setCollectionBehavior:tmp];
[WIN makeKeyAndOrderFront:nil];
[WIN setCollectionBehavior:opt];
[win.content release];
}
enum
{
Autohiddenbars = NSApplicationPresentationAutoHideDock
| NSApplicationPresentationAutoHideMenuBar,
Hiddenbars = NSApplicationPresentationHideDock
| NSApplicationPresentationHideMenuBar,
};
static void
hidebars(int set)
{
NSScreen *s,*s0;
uint old, opt;
s = [WIN screen];
s0 = [[NSScreen screens] objectAtIndex:0];
old = [NSApp presentationOptions];
#if OSX_VERSION >= 100700
/* This bit can get lost, resulting in dreadful bugs. */
if(win.isnfs)
old |= NSApplicationPresentationFullScreen;
#endif
if(set && s==s0)
opt = (old & ~Autohiddenbars) | Hiddenbars;
else
opt = old & ~(Autohiddenbars | Hiddenbars);
if(opt != old)
[NSApp setPresentationOptions:opt];
}
static void
makemenu(void)
{
NSMenu *m;
NSMenuItem *i0,*i1;
m = [NSMenu new];
i0 = [m addItemWithTitle:@"app" action:NULL keyEquivalent:@""];
i1 = [m addItemWithTitle:@"help" action:NULL keyEquivalent:@""];
[NSApp setMainMenu:m];
[m release];
m = [[NSMenu alloc] initWithTitle:@"app"];
[m addItemWithTitle:@"Full Screen"
action:@selector(calltogglefs:)
keyEquivalent:@"f"];
[m addItemWithTitle:@"Hide"
action:@selector(hide:)
keyEquivalent:@"h"];
[m addItemWithTitle:@"Quit"
action:@selector(terminate:)
keyEquivalent:@"q"];
[i0 setSubmenu:m];
[m release];
m = [[NSMenu alloc] initWithTitle:@"help"];
[m addItemWithTitle:@"Plumb devdraw(1)"
action:@selector(plumbmanual:)
keyEquivalent:@""];
[i1 setSubmenu:m];
[m release];
}
// FIXME: Introduce a high-resolution Glenda image.
static void
makeicon(void)
{
NSData *d;
NSImage *i;
d = [[NSData alloc]
initWithBytes:glenda_png
length:(sizeof glenda_png)];
i = [[NSImage alloc] initWithData:d];
[NSApp setApplicationIconImage:i];
[[NSApp dockTile] display];
[i release];
[d release];
}
QLock snarfl;
char*
getsnarf(void)
{
NSPasteboard *pb;
NSString *s;
pb = [NSPasteboard generalPasteboard];
qlock(&snarfl);
s = [pb stringForType:NSPasteboardTypeString];
qunlock(&snarfl);
if(s)
return strdup((char*)[s UTF8String]);
else
return nil;
}
void
putsnarf(char *s)
{
NSArray *t;
NSPasteboard *pb;
NSString *str;
if(strlen(s) >= SnarfSize)
return;
t = [NSArray arrayWithObject:NSPasteboardTypeString];
pb = [NSPasteboard generalPasteboard];
str = [[NSString alloc] initWithUTF8String:s];
qlock(&snarfl);
[pb declareTypes:t owner:nil];
[pb setString:str forType:NSPasteboardTypeString];
qunlock(&snarfl);
[str release];
}
void
kicklabel(char *label)
{
if(label == nil)
return;
[appdelegate
performSelectorOnMainThread:@selector(callkicklabel0:)
withObject:[NSValue valueWithPointer:label]
waitUntilDone:YES];
}
static void
kicklabel0(char *label) {
NSString *s;
s = [[NSString alloc] initWithUTF8String:label];
[win.ofs[0] setTitle:s];
[win.ofs[1] setTitle:s];
[[NSApp dockTile] setBadgeLabel:s];
[s release];
}
void
setcursor(Cursor *c, Cursor2 *c2)
{
USED(c2);
/*
* No cursor change unless in main thread.
*/
[appdelegate
performSelectorOnMainThread:@selector(callsetcursor0:)
withObject:[NSValue valueWithPointer:c]
waitUntilDone:YES];
}
static void
setcursor0(Cursor *c)
{
NSCursor *d;
d = win.cursor;
if(c)
win.cursor = makecursor(c);
else
win.cursor = nil;
updatecursor();
if(d)
[d release];
}
/*
* Cursors will be scaled on retina display.
*/
static NSCursor*
makecursor(Cursor *c)
{
NSBitmapImageRep *r;
NSCursor *d;
NSImage *i;
NSPoint p;
int b;
uchar *plane[5];
r = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:nil
pixelsWide:16
pixelsHigh:16
bitsPerSample:1
samplesPerPixel:2
hasAlpha:YES
isPlanar:YES
colorSpaceName:NSDeviceWhiteColorSpace
bytesPerRow:2
bitsPerPixel:1];
[r getBitmapDataPlanes:plane];
for(b=0; b<2*16; b++){
plane[0][b] = ~c->set[b];
plane[1][b] = c->clr[b];
}
p = NSMakePoint(-c->offset.x, -c->offset.y);
i = [NSImage new];
[i addRepresentation:r];
[r release];
d = [[NSCursor alloc] initWithImage:i hotSpot:p];
[i release];
return d;
}
void
topwin(void)
{
[WIN performSelectorOnMainThread:
@selector(makeKeyAndOrderFront:)
withObject:nil
waitUntilDone:NO];
in.willactivate = 1;
[NSApp activateIgnoringOtherApps:YES];
}
static NSSize
winsizepoints()
{
return [win.content bounds].size;
}
static NSSize
winsizepixels()
{
#if OSX_VERSION >= 100700
if (OSX_VERSION >= 100700 && devdrawretina)
return [win.content convertSizeToBacking: winsizepoints()];
else
#endif
return winsizepoints();
}
static NSRect
scalerect(NSRect r, CGFloat scale)
{
r.origin.x *= scale;
r.origin.y *= scale;
r.size.width *= scale;
r.size.height *= scale;
return r;
}
/*
* Expands rectangle |r|'s bounds to more inclusive integer bounds to
* eliminate 1 pixel gaps.
*/
static NSRect
dilate(NSRect r)
{
if(win.topixelscale > 1.0f){
r.origin.x = floorf(r.origin.x);
r.origin.y = floorf(r.origin.y);
r.size.width = ceilf(r.size.width + 0.5);
r.size.height = ceilf(r.size.height + 0.5);
}
return r;
}
static NSPoint
scalepoint(NSPoint pt, CGFloat scale)
{
pt.x *= scale;
pt.y *= scale;
return pt;
}
static void
setprocname(const char *s)
{
CFStringRef process_name;
process_name = CFStringCreateWithBytes(nil, (uchar*)s, strlen(s), kCFStringEncodingUTF8, false);
// Adapted from Chrome's mac_util.mm.
// http://src.chromium.org/viewvc/chrome/trunk/src/base/mac/mac_util.mm
//
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Warning: here be dragons! This is SPI reverse-engineered from WebKit's
// plugin host, and could break at any time (although realistically it's only
// likely to break in a new major release).
// When 10.7 is available, check that this still works, and update this
// comment for 10.8.
// Private CFType used in these LaunchServices calls.
typedef CFTypeRef PrivateLSASN;
typedef PrivateLSASN (*LSGetCurrentApplicationASNType)();
typedef OSStatus (*LSSetApplicationInformationItemType)(int, PrivateLSASN,
CFStringRef,
CFStringRef,
CFDictionaryRef*);
static LSGetCurrentApplicationASNType ls_get_current_application_asn_func =
NULL;
static LSSetApplicationInformationItemType
ls_set_application_information_item_func = NULL;
static CFStringRef ls_display_name_key = NULL;
static bool did_symbol_lookup = false;
if (!did_symbol_lookup) {
did_symbol_lookup = true;
CFBundleRef launch_services_bundle =
CFBundleGetBundleWithIdentifier(CFSTR("com.apple.LaunchServices"));
if (!launch_services_bundle) {
fprint(2, "Failed to look up LaunchServices bundle\n");
return;
}
ls_get_current_application_asn_func =
(LSGetCurrentApplicationASNType)(
CFBundleGetFunctionPointerForName(
launch_services_bundle, CFSTR("_LSGetCurrentApplicationASN")));
if (!ls_get_current_application_asn_func)
fprint(2, "Could not find _LSGetCurrentApplicationASN\n");
ls_set_application_information_item_func =
(LSSetApplicationInformationItemType)(
CFBundleGetFunctionPointerForName(
launch_services_bundle,
CFSTR("_LSSetApplicationInformationItem")));
if (!ls_set_application_information_item_func)
fprint(2, "Could not find _LSSetApplicationInformationItem\n");
CFStringRef* key_pointer = (CFStringRef*)(
CFBundleGetDataPointerForName(launch_services_bundle,
CFSTR("_kLSDisplayNameKey")));
ls_display_name_key = key_pointer ? *key_pointer : NULL;
if (!ls_display_name_key)
fprint(2, "Could not find _kLSDisplayNameKey\n");
// Internally, this call relies on the Mach ports that are started up by the
// Carbon Process Manager. In debug builds this usually happens due to how
// the logging layers are started up; but in release, it isn't started in as
// much of a defined order. So if the symbols had to be loaded, go ahead
// and force a call to make sure the manager has been initialized and hence
// the ports are opened.
ProcessSerialNumber psn;
GetCurrentProcess(&psn);
}
if (!ls_get_current_application_asn_func ||
!ls_set_application_information_item_func ||
!ls_display_name_key) {
return;
}
PrivateLSASN asn = ls_get_current_application_asn_func();
// Constant used by WebKit; what exactly it means is unknown.
const int magic_session_constant = -2;
OSErr err =
ls_set_application_information_item_func(magic_session_constant, asn,
ls_display_name_key,
process_name,
NULL /* optional out param */);
if(err != noErr)
fprint(2, "Call to set process name failed\n");
}
void
resizewindow(Rectangle r)
{
USED(r);
}
|