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
|
/* X11Application.m -- subclass of NSApplication to multiplex events
Copyright (c) 2002-2008 Apple Inc.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name(s) of the above
copyright holders shall not be used in advertising or otherwise to
promote the sale, use or other dealings in this Software without
prior written authorization. */
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
#include "quartzCommon.h"
#include "quartzForeground.h"
#import "X11Application.h"
#include <Carbon/Carbon.h>
/* ouch! */
#define BOOL X_BOOL
# include "darwin.h"
# include "darwinEvents.h"
# include "quartz.h"
# define _APPLEWM_SERVER_
# include "X11/extensions/applewm.h"
# include "micmap.h"
#undef BOOL
#include <mach/mach.h>
#include <unistd.h>
#include <pthread.h>
#include "rootlessCommon.h"
WindowPtr xprGetXWindowFromAppKit(int windowNumber); // xpr/xprFrame.c
#define DEFAULTS_FILE "/usr/X11/lib/X11/xserver/Xquartz.plist"
int X11EnableKeyEquivalents = TRUE;
int quartzHasRoot = FALSE, quartzEnableRootless = TRUE;
extern int darwinFakeButtons, input_check_flag;
extern Bool enable_stereo;
extern xEvent *darwinEvents;
X11Application *X11App;
#define ALL_KEY_MASKS (NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask)
@implementation X11Application
typedef struct message_struct message;
struct message_struct {
mach_msg_header_t hdr;
SEL selector;
NSObject *arg;
};
static mach_port_t _port;
static void send_nsevent (NSEventType type, NSEvent *e);
/* Quartz mode initialization routine. This is often dynamically loaded
but is statically linked into this X server. */
Bool QuartzModeBundleInit(void);
static void init_ports (void) {
kern_return_t r;
NSPort *p;
if (_port != MACH_PORT_NULL) return;
r = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &_port);
if (r != KERN_SUCCESS) return;
p = [NSMachPort portWithMachPort:_port];
[p setDelegate:NSApp];
[p scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
static void message_kit_thread (SEL selector, NSObject *arg) {
message msg;
kern_return_t r;
msg.hdr.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MAKE_SEND, 0);
msg.hdr.msgh_size = sizeof (msg);
msg.hdr.msgh_remote_port = _port;
msg.hdr.msgh_local_port = MACH_PORT_NULL;
msg.hdr.msgh_reserved = 0;
msg.hdr.msgh_id = 0;
msg.selector = selector;
msg.arg = [arg retain];
r = mach_msg (&msg.hdr, MACH_SEND_MSG, msg.hdr.msgh_size,
0, MACH_PORT_NULL, 0, MACH_PORT_NULL);
if (r != KERN_SUCCESS)
ErrorF("%s: mach_msg failed: %x\n", __FUNCTION__, r);
}
- (void) handleMachMessage:(void *)_msg {
message *msg = _msg;
[self performSelector:msg->selector withObject:msg->arg];
[msg->arg release];
}
- (void) set_controller:obj {
if (_controller == nil) _controller = [obj retain];
}
- (void) dealloc {
if (_controller != nil) [_controller release];
if (_port != MACH_PORT_NULL)
mach_port_deallocate (mach_task_self (), _port);
[super dealloc];
}
- (void) orderFrontStandardAboutPanel: (id) sender {
NSMutableDictionary *dict;
NSDictionary *infoDict;
NSString *tem;
dict = [NSMutableDictionary dictionaryWithCapacity:2];
infoDict = [[NSBundle mainBundle] infoDictionary];
[dict setObject: NSLocalizedString (@"The X Window System", @"About panel")
forKey:@"ApplicationName"];
tem = [infoDict objectForKey:@"CFBundleShortVersionString"];
[dict setObject:[NSString stringWithFormat:@"XQuartz %@ - (xorg-server %s)", tem, XSERVER_VERSION]
forKey:@"ApplicationVersion"];
[self orderFrontStandardAboutPanelWithOptions: dict];
}
- (void) activateX:(BOOL)state {
/* Create a TSM document that supports full Unicode input, and
have it activated while X is active (unless using the old
keymapping files) */
static TSMDocumentID x11_document;
DEBUG_LOG("state=%d, _x_active=%d, \n", state, _x_active)
if (state) {
DarwinSendDDXEvent(kXquartzActivate, 0);
if (!_x_active) {
if (x11_document == 0 && darwinKeymapFile == NULL) {
OSType types[1];
types[0] = kUnicodeDocument;
NewTSMDocument (1, types, &x11_document, 0);
}
if (x11_document != 0) ActivateTSMDocument (x11_document);
}
} else {
DarwinSendDDXEvent(kXquartzDeactivate, 0);
if (_x_active && x11_document != 0)
DeactivateTSMDocument (x11_document);
}
_x_active = state;
}
- (void) became_key:(NSWindow *)win {
[self activateX:NO];
}
- (void) sendEvent:(NSEvent *)e {
NSEventType type;
BOOL for_appkit, for_x;
type = [e type];
/* By default pass down the responder chain and to X. */
for_appkit = YES;
for_x = YES;
switch (type) {
case NSLeftMouseDown: case NSRightMouseDown: case NSOtherMouseDown:
case NSLeftMouseUp: case NSRightMouseUp: case NSOtherMouseUp:
if ([e window] != nil) {
/* Pointer event has an (AppKit) window. Probably something for the kit. */
for_x = NO;
if (_x_active) [self activateX:NO];
} else if ([self modalWindow] == nil) {
/* Must be an X window. Tell appkit it doesn't have focus. */
WindowPtr pWin = xprGetXWindowFromAppKit([e windowNumber]);
if (pWin) RootlessReorderWindow(pWin);
for_appkit = NO;
if ([self isActive]) {
[self deactivate];
if (!_x_active && quartzProcs->IsX11Window([e window],
[e windowNumber]))
[self activateX:YES];
}
}
break;
case NSKeyDown: case NSKeyUp:
if (_x_active) {
static int swallow_up;
/* No kit window is focused, so send it to X. */
for_appkit = NO;
if (type == NSKeyDown) {
/* Before that though, see if there are any global
shortcuts bound to it. */
if (X11EnableKeyEquivalents
&& [[self mainMenu] performKeyEquivalent:e]) {
swallow_up = [e keyCode];
for_x = NO;
} else if (!quartzEnableRootless
&& ([e modifierFlags] & ALL_KEY_MASKS)
== (NSCommandKeyMask | NSAlternateKeyMask)
&& ([e keyCode] == 0 /*a*/
|| [e keyCode] == 53 /*Esc*/)) {
swallow_up = 0;
for_x = NO;
#ifdef DARWIN_DDX_MISSING
DarwinSendDDXEvent(kXquartzToggleFullscreen, 0);
#endif
}
} else {
/* If we saw a key equivalent on the down, don't pass
the up through to X. */
if (swallow_up != 0 && [e keyCode] == swallow_up) {
swallow_up = 0;
for_x = NO;
}
}
} else for_x = NO;
break;
case NSFlagsChanged:
/* For the l33t X users who remap modifier keys to normal keysyms. */
if (!_x_active) for_x = NO;
break;
case NSAppKitDefined:
switch ([e subtype]) {
case NSApplicationActivatedEventType:
for_x = NO;
if ([self modalWindow] == nil) {
for_appkit = NO;
/* FIXME: hack to avoid having to pass the event to appkit,
which would cause it to raise one of its windows. */
_appFlags._active = YES;
[self activateX:YES];
if ([e data2] & 0x10) X11ApplicationSetFrontProcess();
}
break;
case 18: /* ApplicationDidReactivate */
if (quartzHasRoot) for_appkit = NO;
break;
case NSApplicationDeactivatedEventType:
for_x = NO;
[self activateX:NO];
break;
}
break;
default: break; /* for gcc */
}
if (for_appkit) [super sendEvent:e];
if (for_x) send_nsevent (type, e);
}
- (void) set_window_menu:(NSArray *)list {
[_controller set_window_menu:list];
}
- (void) set_window_menu_check:(NSNumber *)n {
[_controller set_window_menu_check:n];
}
- (void) set_apps_menu:(NSArray *)list {
[_controller set_apps_menu:list];
}
- (void) set_front_process:unused {
[NSApp activateIgnoringOtherApps:YES];
if ([self modalWindow] == nil)
[self activateX:YES];
}
- (void) set_can_quit:(NSNumber *)state {
[_controller set_can_quit:[state boolValue]];
}
- (void) server_ready:unused {
[_controller server_ready];
}
- (void) show_hide_menubar:(NSNumber *)state {
if ([state boolValue]) ShowMenuBar ();
else HideMenuBar ();
}
/* user preferences */
/* Note that these functions only work for arrays whose elements
can be toll-free-bridged between NS and CF worlds. */
static const void *cfretain (CFAllocatorRef a, const void *b) {
return CFRetain (b);
}
static void cfrelease (CFAllocatorRef a, const void *b) {
CFRelease (b);
}
static CFMutableArrayRef nsarray_to_cfarray (NSArray *in) {
CFMutableArrayRef out;
CFArrayCallBacks cb;
NSObject *ns;
const CFTypeRef *cf;
int i, count;
memset (&cb, 0, sizeof (cb));
cb.version = 0;
cb.retain = cfretain;
cb.release = cfrelease;
count = [in count];
out = CFArrayCreateMutable (NULL, count, &cb);
for (i = 0; i < count; i++) {
ns = [in objectAtIndex:i];
if ([ns isKindOfClass:[NSArray class]])
cf = (CFTypeRef) nsarray_to_cfarray ((NSArray *) ns);
else
cf = CFRetain ((CFTypeRef) ns);
CFArrayAppendValue (out, cf);
CFRelease (cf);
}
return out;
}
static NSMutableArray * cfarray_to_nsarray (CFArrayRef in) {
NSMutableArray *out;
const CFTypeRef *cf;
NSObject *ns;
int i, count;
count = CFArrayGetCount (in);
out = [[NSMutableArray alloc] initWithCapacity:count];
for (i = 0; i < count; i++) {
cf = CFArrayGetValueAtIndex (in, i);
if (CFGetTypeID (cf) == CFArrayGetTypeID ())
ns = cfarray_to_nsarray ((CFArrayRef) cf);
else
ns = [(id)cf retain];
[out addObject:ns];
[ns release];
}
return out;
}
- (CFPropertyListRef) prefs_get:(NSString *)key {
CFPropertyListRef value;
value = CFPreferencesCopyAppValue ((CFStringRef) key, CFSTR (APP_PREFS));
if (value == NULL) {
static CFDictionaryRef defaults;
if (defaults == NULL) {
CFStringRef error = NULL;
CFDataRef data;
CFURLRef url;
SInt32 error_code;
url = (CFURLCreateFromFileSystemRepresentation
(NULL, (unsigned char *)DEFAULTS_FILE, strlen (DEFAULTS_FILE), false));
if (CFURLCreateDataAndPropertiesFromResource (NULL, url, &data,
NULL, NULL, &error_code)) {
defaults = (CFPropertyListCreateFromXMLData
(NULL, data, kCFPropertyListMutableContainersAndLeaves, &error));
if (error != NULL) CFRelease (error);
CFRelease (data);
}
CFRelease (url);
if (defaults != NULL) {
NSMutableArray *apps, *elt;
int count, i;
NSString *name, *nname;
/* Localize the names in the default apps menu. */
apps = [(NSDictionary *)defaults objectForKey:@PREFS_APPSMENU];
if (apps != nil) {
count = [apps count];
for (i = 0; i < count; i++) {
elt = [apps objectAtIndex:i];
if (elt != nil && [elt isKindOfClass:[NSArray class]]) {
name = [elt objectAtIndex:0];
if (name != nil) {
nname = NSLocalizedString (name, nil);
if (nname != nil && nname != name)
[elt replaceObjectAtIndex:0 withObject:nname];
}
}
}
}
}
}
if (defaults != NULL) value = CFDictionaryGetValue (defaults, key);
if (value != NULL) CFRetain (value);
}
return value;
}
- (int) prefs_get_integer:(NSString *)key default:(int)def {
CFPropertyListRef value;
int ret;
value = [self prefs_get:key];
if (value != NULL && CFGetTypeID (value) == CFNumberGetTypeID ())
CFNumberGetValue (value, kCFNumberIntType, &ret);
else if (value != NULL && CFGetTypeID (value) == CFStringGetTypeID ())
ret = CFStringGetIntValue (value);
else
ret = def;
if (value != NULL) CFRelease (value);
return ret;
}
- (const char *) prefs_get_string:(NSString *)key default:(const char *)def {
CFPropertyListRef value;
const char *ret = NULL;
value = [self prefs_get:key];
if (value != NULL && CFGetTypeID (value) == CFStringGetTypeID ()) {
NSString *s = (NSString *) value;
ret = [s UTF8String];
}
if (value != NULL) CFRelease (value);
return ret != NULL ? ret : def;
}
- (float) prefs_get_float:(NSString *)key default:(float)def {
CFPropertyListRef value;
float ret = def;
value = [self prefs_get:key];
if (value != NULL
&& CFGetTypeID (value) == CFNumberGetTypeID ()
&& CFNumberIsFloatType (value))
CFNumberGetValue (value, kCFNumberFloatType, &ret);
else if (value != NULL && CFGetTypeID (value) == CFStringGetTypeID ())
ret = CFStringGetDoubleValue (value);
if (value != NULL) CFRelease (value);
return ret;
}
- (int) prefs_get_boolean:(NSString *)key default:(int)def {
CFPropertyListRef value;
int ret = def;
value = [self prefs_get:key];
if (value != NULL) {
if (CFGetTypeID (value) == CFNumberGetTypeID ())
CFNumberGetValue (value, kCFNumberIntType, &ret);
else if (CFGetTypeID (value) == CFBooleanGetTypeID ())
ret = CFBooleanGetValue (value);
else if (CFGetTypeID (value) == CFStringGetTypeID ()) {
const char *tem = [(NSString *) value UTF8String];
if (strcasecmp (tem, "true") == 0 || strcasecmp (tem, "yes") == 0)
ret = YES;
else
ret = NO;
}
CFRelease (value);
}
return ret;
}
- (NSArray *) prefs_get_array:(NSString *)key {
NSArray *ret = nil;
CFPropertyListRef value;
value = [self prefs_get:key];
if (value != NULL) {
if (CFGetTypeID (value) == CFArrayGetTypeID ())
ret = [cfarray_to_nsarray (value) autorelease];
CFRelease (value);
}
return ret;
}
- (void) prefs_set_integer:(NSString *)key value:(int)value {
CFNumberRef x;
x = CFNumberCreate (NULL, kCFNumberIntType, &value);
CFPreferencesSetValue ((CFStringRef) key, (CFTypeRef) x, CFSTR (APP_PREFS),
kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFRelease (x);
}
- (void) prefs_set_float:(NSString *)key value:(float)value {
CFNumberRef x;
x = CFNumberCreate (NULL, kCFNumberFloatType, &value);
CFPreferencesSetValue ((CFStringRef) key, (CFTypeRef) x, CFSTR (APP_PREFS),
kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFRelease (x);
}
- (void) prefs_set_boolean:(NSString *)key value:(int)value {
CFPreferencesSetValue ((CFStringRef) key,
(CFTypeRef) value ? kCFBooleanTrue
: kCFBooleanFalse, CFSTR (APP_PREFS),
kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
}
- (void) prefs_set_array:(NSString *)key value:(NSArray *)value {
CFArrayRef cfarray;
cfarray = nsarray_to_cfarray (value);
CFPreferencesSetValue ((CFStringRef) key,
(CFTypeRef) cfarray,
CFSTR (APP_PREFS),
kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFRelease (cfarray);
}
- (void) prefs_set_string:(NSString *)key value:(NSString *)value {
CFPreferencesSetValue ((CFStringRef) key, (CFTypeRef) value,
CFSTR (APP_PREFS), kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
}
- (void) prefs_synchronize {
CFPreferencesAppSynchronize (kCFPreferencesCurrentApplication);
}
- (void) read_defaults
{
const char *tem;
quartzUseSysBeep = [self prefs_get_boolean:@PREFS_SYSBEEP
default:quartzUseSysBeep];
// TODO: Add fullscreen support
//quartzEnableRootless = [self prefs_get_boolean:@PREFS_ROOTLESS
// default:quartzEnableRootless];
#ifdef DARWIN_DDX_MISSING
quartzFullscreenDisableHotkeys = ![self prefs_get_boolean:
@PREFS_FULLSCREEN_HOTKEYS default:
!quartzFullscreenDisableHotkeys];
quartzXpluginOptions = [self prefs_get_integer:@PREFS_XP_OPTIONS
default:quartzXpluginOptions];
#endif
darwinFakeButtons = [self prefs_get_boolean:@PREFS_FAKEBUTTONS
default:darwinFakeButtons];
if (darwinFakeButtons) {
const char *fake2, *fake3;
fake2 = [self prefs_get_string:@PREFS_FAKE_BUTTON2 default:NULL];
fake3 = [self prefs_get_string:@PREFS_FAKE_BUTTON3 default:NULL];
if (fake2 != NULL) darwinFakeMouse2Mask = DarwinParseModifierList(fake2);
if (fake3 != NULL) darwinFakeMouse3Mask = DarwinParseModifierList(fake3);
}
X11EnableKeyEquivalents = [self prefs_get_boolean:@PREFS_KEYEQUIVS
default:X11EnableKeyEquivalents];
darwinSyncKeymap = [self prefs_get_boolean:@PREFS_SYNC_KEYMAP
default:darwinSyncKeymap];
tem = [self prefs_get_string:@PREFS_KEYMAP_FILE default:NULL];
if (tem != NULL) darwinKeymapFile = strdup (tem);
else darwinKeymapFile = NULL;
darwinDesiredDepth = [self prefs_get_integer:@PREFS_DEPTH
default:darwinDesiredDepth];
// TODO: Add 256 color support
if(darwinDesiredDepth == 8)
darwinDesiredDepth = -1;
enable_stereo = [self prefs_get_boolean:@PREFS_ENABLE_STEREO
default:false];
}
/* This will end up at the end of the responder chain. */
- (void) copy:sender {
DarwinSendDDXEvent(kXquartzPasteboardNotify, 1,
AppleWMCopyToPasteboard);
}
- (BOOL) x_active {
return _x_active;
}
@end
static NSArray *
array_with_strings_and_numbers (int nitems, const char **items,
const char *numbers) {
NSMutableArray *array, *subarray;
NSString *string, *number;
int i;
/* (Can't autorelease on the X server thread) */
array = [[NSMutableArray alloc] initWithCapacity:nitems];
for (i = 0; i < nitems; i++) {
subarray = [[NSMutableArray alloc] initWithCapacity:2];
string = [[NSString alloc] initWithUTF8String:items[i]];
[subarray addObject:string];
[string release];
if (numbers[i] != 0) {
number = [[NSString alloc] initWithFormat:@"%d", numbers[i]];
[subarray addObject:number];
[number release];
} else
[subarray addObject:@""];
[array addObject:subarray];
[subarray release];
}
return array;
}
void X11ApplicationSetWindowMenu (int nitems, const char **items,
const char *shortcuts) {
NSArray *array;
array = array_with_strings_and_numbers (nitems, items, shortcuts);
/* Send the array of strings over to the appkit thread */
message_kit_thread (@selector (set_window_menu:), array);
[array release];
}
void X11ApplicationSetWindowMenuCheck (int idx) {
NSNumber *n;
n = [[NSNumber alloc] initWithInt:idx];
message_kit_thread (@selector (set_window_menu_check:), n);
[n release];
}
void X11ApplicationSetFrontProcess (void) {
message_kit_thread (@selector (set_front_process:), nil);
}
void X11ApplicationSetCanQuit (int state) {
NSNumber *n;
n = [[NSNumber alloc] initWithBool:state];
message_kit_thread (@selector (set_can_quit:), n);
[n release];
}
void X11ApplicationServerReady (void) {
message_kit_thread (@selector (server_ready:), nil);
}
void X11ApplicationShowHideMenubar (int state) {
NSNumber *n;
n = [[NSNumber alloc] initWithBool:state];
message_kit_thread (@selector (show_hide_menubar:), n);
[n release];
}
static void * create_thread (void *func, void *arg) {
pthread_attr_t attr;
pthread_t tid;
pthread_attr_init (&attr);
pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
pthread_create (&tid, &attr, func, arg);
pthread_attr_destroy (&attr);
return (void *) tid;
}
static void check_xinitrc (void) {
char *tem, buf[1024];
NSString *msg;
if ([X11App prefs_get_boolean:@PREFS_DONE_XINIT_CHECK default:NO])
return;
tem = getenv ("HOME");
if (tem == NULL) goto done;
snprintf (buf, sizeof (buf), "%s/.xinitrc", tem);
if (access (buf, F_OK) != 0)
goto done;
/* FIXME: put localized strings into Resources/English.lproj */
msg = NSLocalizedString (@"You have an existing ~/.xinitrc file.\n\n\
Windows displayed by X11 applications may not have titlebars, or may look \
different to windows displayed by native applications.\n\n\
Would you like to move aside the existing file and use the standard X11 \
environment?", @"Startup xinitrc dialog");
if (NSRunAlertPanel (nil, msg, NSLocalizedString (@"Yes", @""),
NSLocalizedString (@"No", @""), nil)
== NSAlertDefaultReturn) {
char buf2[1024];
int i = -1;
snprintf (buf2, sizeof (buf2), "%s.old", buf);
for (i = 1; access (buf2, F_OK) == 0; i++)
snprintf (buf2, sizeof (buf2), "%s.old.%d", buf, i);
rename (buf, buf2);
}
done:
[X11App prefs_set_boolean:@PREFS_DONE_XINIT_CHECK value:YES];
[X11App prefs_synchronize];
}
void X11ApplicationMain (int argc, const char **argv, void (*server_thread) (void *), void *server_arg) {
NSAutoreleasePool *pool;
#ifdef DEBUG
while (access ("/tmp/x11-block", F_OK) == 0) sleep (1);
#endif
pool = [[NSAutoreleasePool alloc] init];
X11App = (X11Application *) [X11Application sharedApplication];
init_ports ();
[NSApp read_defaults];
[NSBundle loadNibNamed:@"main" owner:NSApp];
[[NSNotificationCenter defaultCenter] addObserver:NSApp
selector:@selector (became_key:)
name:NSWindowDidBecomeKeyNotification object:nil];
check_xinitrc ();
/*
* The xpr Quartz mode is statically linked into this server.
* Initialize all the Quartz functions.
*/
QuartzModeBundleInit();
/* Calculate the height of the menubar so we can avoid it. */
aquaMenuBarHeight = NSHeight([[NSScreen mainScreen] frame]) -
NSMaxY([[NSScreen mainScreen] visibleFrame]);
if (!create_thread (server_thread, server_arg)) {
ErrorF("can't create secondary thread\n");
exit (1);
}
QuartzMoveToForeground();
[NSApp run];
/* not reached */
}
/* event conversion */
static inline unsigned short
convert_flags (unsigned int nsflags) {
unsigned int xflags = 0;
if (nsflags == ~0) return 0xffff;
if (nsflags & NSAlphaShiftKeyMask) xflags |= LockMask;
if (nsflags & NSShiftKeyMask) xflags |= ShiftMask;
if (nsflags & NSControlKeyMask) xflags |= ControlMask;
if (nsflags & NSAlternateKeyMask) xflags |= Mod1Mask;
if (nsflags & NSCommandKeyMask) xflags |= Mod2Mask;
/* FIXME: secondaryfn? */
return xflags;
}
static void send_nsevent (NSEventType type, NSEvent *e) {
NSRect screen;
NSPoint location;
NSWindow *window;
int pointer_x, pointer_y, ev_button, ev_type;
float pressure, tilt_x, tilt_y;
/* convert location to global top-left coordinates */
location = [e locationInWindow];
window = [e window];
screen = [[[NSScreen screens] objectAtIndex:0] frame];
if (window != nil) {
NSRect frame = [window frame];
pointer_x = location.x + frame.origin.x;
pointer_y = (((screen.origin.y + screen.size.height)
- location.y) - frame.origin.y);
} else {
pointer_x = location.x;
pointer_y = (screen.origin.y + screen.size.height) - location.y;
}
pointer_y -= aquaMenuBarHeight;
pressure = 0; // for tablets
tilt_x = 0;
tilt_y = 0;
switch (type) {
case NSLeftMouseDown: ev_button=1; ev_type=ButtonPress; goto handle_mouse;
case NSOtherMouseDown: ev_button=2; ev_type=ButtonPress; goto handle_mouse;
case NSRightMouseDown: ev_button=3; ev_type=ButtonPress; goto handle_mouse;
case NSLeftMouseUp: ev_button=1; ev_type=ButtonRelease; goto handle_mouse;
case NSOtherMouseUp: ev_button=2; ev_type=ButtonRelease; goto handle_mouse;
case NSRightMouseUp: ev_button=3; ev_type=ButtonRelease; goto handle_mouse;
case NSLeftMouseDragged: ev_button=1; ev_type=MotionNotify; goto handle_mouse;
case NSOtherMouseDragged: ev_button=2; ev_type=MotionNotify; goto handle_mouse;
case NSRightMouseDragged: ev_button=3; ev_type=MotionNotify; goto handle_mouse;
case NSTabletPoint:
pressure = [e pressure];
tilt_x = [e tilt].x;
tilt_y = [e tilt].y; // fall through
case NSMouseMoved: ev_button=0; ev_type=MotionNotify; goto handle_mouse;
handle_mouse:
// if ([e subtype] == NSTabletPointEventSubtype) pressure = [e pressure];
DarwinSendPointerEvents(ev_type, ev_button, pointer_x, pointer_y,
pressure, tilt_x, tilt_y);
break;
case NSScrollWheel:
DarwinSendScrollEvents([e deltaY], pointer_x, pointer_y,
pressure, tilt_x, tilt_y);
break;
case NSKeyDown: case NSKeyUp:
DarwinSendKeyboardEvents((type == NSKeyDown)?KeyPress:KeyRelease, [e keyCode]);
break;
case NSFlagsChanged:
DarwinUpdateModKeys([e modifierFlags]);
break;
default: break; /* for gcc */
}
}
|