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
|
/**************************************************************
*
* Shared code for the Darwin X Server
* running with Quartz or the IOKit
*
**************************************************************/
/* $XFree86: xc/programs/Xserver/hw/darwin/darwin.c,v 1.21 2001/04/25 02:23:47 torrey Exp $ */
#include "X.h"
#include "Xproto.h"
#include "os.h"
#include "servermd.h"
#include "inputstr.h"
#include "scrnintstr.h"
#include "mi.h"
#include "mibstore.h"
#include "mipointer.h"
#include "micmap.h"
#include "site.h"
#include "globals.h"
#include "xf86Version.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/syslimits.h>
#include <stdio.h>
#include <fcntl.h>
#define NO_CFPLUGIN
#include <IOKit/IOKitLib.h>
#include <IOKit/hidsystem/IOHIDLib.h>
#include <IOKit/hidsystem/ev_keymap.h>
#include "darwin.h"
#include "darwinClut8.h"
#include "bundle/quartz.h"
#include "xfIOKit.h"
// X server shared global variables
DarwinFramebufferRec dfb;
int darwinEventFD = -1;
Bool quartz = FALSE;
int quartzEventWriteFD = -1;
int quartzStartClients = 1;
int quartzUseSysBeep = 0;
int darwinFakeButtons = 0;
UInt32 darwinDesiredWidth = 0, darwinDesiredHeight = 0;
IOIndex darwinDesiredDepth = -1;
SInt32 darwinDesiredRefresh = -1;
UInt32 darwinScreenNumber = 0;
char *darwinKeymapFile = NULL;
// Quit after this many seconds if no quartz event poster is found.
// Leave undefined for no safety quit.
#undef QUARTZ_SAFETY_DELAY
/* Fake button press/release for scroll wheel move. */
#define SCROLLWHEELUPFAKE 4
#define SCROLLWHEELDOWNFAKE 5
static DeviceIntPtr darwinPointer;
static DeviceIntPtr darwinKeyboard;
static unsigned char darwinKeyCommandL = 0, darwinKeyOptionL = 0;
// Common pixmap formats
static PixmapFormatRec formats[] = {
{ 1, 1, BITMAP_SCANLINE_PAD },
{ 4, 8, BITMAP_SCANLINE_PAD },
{ 8, 8, BITMAP_SCANLINE_PAD },
{ 15, 16, BITMAP_SCANLINE_PAD },
{ 16, 16, BITMAP_SCANLINE_PAD },
{ 24, 32, BITMAP_SCANLINE_PAD }
};
const int NUMFORMATS = sizeof(formats)/sizeof(formats[0]);
#ifndef OSNAME
#define OSNAME " Darwin"
#endif
#ifndef OSVENDOR
#define OSVENDOR ""
#endif
#ifndef PRE_RELEASE
#define PRE_RELEASE XF86_VERSION_SNAP
#endif
static void
DarwinPrintBanner()
{
#if PRE_RELEASE
ErrorF("\n"
"This is a pre-release version of XFree86, and is not supported in any\n"
"way. Bugs may be reported to XFree86@XFree86.Org and patches submitted\n"
"to fixes@XFree86.Org. Before reporting bugs in pre-release versions,\n"
"please check the latest version in the XFree86 CVS repository\n"
"(http://www.XFree86.Org/cvs)\n");
#endif
ErrorF("\nXFree86 Version %d.%d.%d", XF86_VERSION_MAJOR, XF86_VERSION_MINOR,
XF86_VERSION_PATCH);
#if XF86_VERSION_SNAP > 0
ErrorF(".%d", XF86_VERSION_SNAP);
#endif
#ifdef XF86_CUSTOM_VERSION
ErrorF(" (%s)", XF86_CUSTOM_VERSION);
#endif
ErrorF(" / X Window System\n");
ErrorF("(protocol Version %d, revision %d, vendor release %d)\n",
X_PROTOCOL, X_PROTOCOL_REVISION, VENDOR_RELEASE );
ErrorF("Release Date: %s\n", XF86_DATE);
ErrorF("\tIf the server is older than 6-12 months, or if your hardware is\n"
"\tnewer than the above date, look for a newer version before\n"
"\treporting problems. (See http://www.XFree86.Org/FAQ)\n");
ErrorF("Operating System:%s%s\n", OSNAME, OSVENDOR);
#if defined(BUILDERSTRING)
ErrorF("%s \n",BUILDERSTRING);
#endif
}
/*
* DarwinSaveScreen
* X screensaver support. Not implemented.
*/
static Bool DarwinSaveScreen(ScreenPtr pScreen, int on)
{
// FIXME
if (on == SCREEN_SAVER_FORCER) {
} else if (on == SCREEN_SAVER_ON) {
} else {
}
return TRUE;
}
/*
* DarwinAddScreen
* This is a callback from X during AddScreen() from InitOutput()
*/
static Bool DarwinAddScreen(
int index,
ScreenPtr pScreen,
int argc,
char **argv )
{
int bitsPerRGB, i, dpi;
VisualPtr visual;
ColormapPtr pmap;
/* Communicate the information about our initialized screen back to X. */
bitsPerRGB = dfb.pixelInfo.bitsPerComponent;
// reset the visual list
miClearVisualTypes();
// setup a single visual appropriate for our pixel type
// Note: Darwin kIORGBDirectPixels = X window TrueColor, not DirectColor
if (dfb.pixelInfo.pixelType == kIORGBDirectPixels) {
if (!miSetVisualTypes( dfb.colorBitsPerPixel, TrueColorMask,
bitsPerRGB, TrueColor )) {
return FALSE;
}
} else if (dfb.pixelInfo.pixelType == kIOCLUTPixels) {
if (!miSetVisualTypes( dfb.colorBitsPerPixel, PseudoColorMask,
bitsPerRGB, PseudoColor )) {
return FALSE;
}
} else if (dfb.pixelInfo.pixelType == kIOFixedCLUTPixels) {
if (!miSetVisualTypes( dfb.colorBitsPerPixel, StaticColorMask,
bitsPerRGB, StaticColor )) {
return FALSE;
}
} else {
return FALSE;
}
// machine independent screen init
// setup _Screen structure in pScreen
if (monitorResolution)
dpi = monitorResolution;
else
dpi = 75;
if ( dfb.bitsPerPixel == 32 ) {
if (!cfb32ScreenInit(pScreen,
dfb.framebuffer,
dfb.width, dfb.height,
dpi, dpi,
dfb.pitch / (dfb.bitsPerPixel/8))) {
return FALSE;
}
} else if ( dfb.bitsPerPixel == 16 ) {
if (!cfb16ScreenInit(pScreen,
dfb.framebuffer,
dfb.width, dfb.height,
dpi, dpi,
dfb.pitch / (dfb.bitsPerPixel/8))) {
return FALSE;
}
} else if ( dfb.bitsPerPixel == 8 ) {
if (!cfbScreenInit(pScreen,
dfb.framebuffer,
dfb.width, dfb.height,
dpi, dpi,
dfb.pitch / (dfb.bitsPerPixel/8))) {
return FALSE;
}
} else {
return FALSE;
}
// set the RGB order correctly for TrueColor
if (dfb.bitsPerPixel > 8) {
for (i = 0, visual = pScreen->visuals; // someday we may have more than 1
i < pScreen->numVisuals; i++, visual++) {
if (visual->class == TrueColor) {
visual->offsetRed = bitsPerRGB * 2;
visual->offsetGreen = bitsPerRGB;
visual->offsetBlue = 0;
#if TRUE
visual->redMask = ((1<<bitsPerRGB)-1) << visual->offsetRed;
visual->greenMask = ((1<<bitsPerRGB)-1) << visual->offsetGreen;
visual->blueMask = ((1<<bitsPerRGB)-1) << visual->offsetBlue;
#else
visual->redMask = dfb.pixelInfo.componentMasks[0];
visual->greenMask = dfb.pixelInfo.componentMasks[1];
visual->blueMask = dfb.pixelInfo.componentMasks[2];
#endif
}
}
}
#ifdef MITSHM
ShmRegisterFbFuncs(pScreen);
#endif
// this must be initialized (why doesn't X have a default?)
pScreen->SaveScreen = DarwinSaveScreen;
// Perform operations specific to the screen interface
if (quartz) {
if (! QuartzAddScreen(pScreen)) {
return FALSE;
}
} else {
if (! XFIOKitAddScreen(pScreen)) {
return FALSE;
}
}
// create and install the default colormap and
// set pScreen->blackPixel / pScreen->white
if (!miCreateDefColormap( pScreen )) {
return FALSE;
}
/* Set the colormap to the statically defined one if we're in 8 bit
* mode and we're using a fixed color map. Essentially this translates
* to Darwin/x86 in 8-bit mode.
*/
if( (dfb.colorBitsPerPixel == 8) &&
(dfb.pixelInfo.pixelType == kIOFixedCLUTPixels) ) {
pmap = miInstalledMaps[pScreen->myNum];
visual = pmap->pVisual;
for( i = 0; i < visual->ColormapEntries; i++ ) {
pmap->red[i].co.local.red = darwinClut8[i].red;
pmap->red[i].co.local.green = darwinClut8[i].green;
pmap->red[i].co.local.blue = darwinClut8[i].blue;
}
}
return TRUE;
}
/*
=============================================================================
mouse and keyboard callbacks
=============================================================================
*/
/*
* DarwinChangePointerControl
* Set mouse acceleration and thresholding
* FIXME: We currently ignore the threshold in ctrl->threshold.
*/
static void DarwinChangePointerControl(
DeviceIntPtr device,
PtrCtrl *ctrl )
{
kern_return_t kr;
double acceleration;
acceleration = ctrl->num / ctrl->den;
kr = IOHIDSetMouseAcceleration( dfb.hidParam, acceleration );
if (kr != KERN_SUCCESS)
ErrorF( "Could not set mouse acceleration with kernel return = 0x%x.\n", kr );
}
/*
* DarwinMouseProc
* Handle the initialization, etc. of a mouse
*/
static int DarwinMouseProc(
DeviceIntPtr pPointer,
int what )
{
char map[6];
switch (what) {
case DEVICE_INIT:
pPointer->public.on = FALSE;
// Set button map. Darwin uses 2 for right and 3 for center.
// Reverse these to correspond to typical X usage.
map[1] = 1;
map[2] = 3;
map[3] = 2;
map[4] = 4;
map[5] = 5;
InitPointerDeviceStruct( (DevicePtr)pPointer,
map,
5, // numbuttons (4 & 5 are scroll wheel)
miPointerGetMotionEvents,
DarwinChangePointerControl,
0 );
break;
case DEVICE_ON:
pPointer->public.on = TRUE;
AddEnabledDevice( darwinEventFD );
return Success;
case DEVICE_CLOSE:
case DEVICE_OFF:
pPointer->public.on = FALSE;
RemoveEnabledDevice( darwinEventFD );
return Success;
}
return Success;
}
/*
* DarwinKeybdProc
* Callback from X
*/
static int DarwinKeybdProc( DeviceIntPtr pDev, int onoff )
{
switch ( onoff ) {
case DEVICE_INIT:
DarwinKeyboardInit( pDev );
darwinKeyCommandL = DarwinModifierKeycode(NX_MODIFIERKEY_COMMAND, 0);
darwinKeyOptionL = DarwinModifierKeycode(NX_MODIFIERKEY_ALTERNATE, 0);
break;
case DEVICE_ON:
pDev->public.on = TRUE;
AddEnabledDevice( darwinEventFD );
break;
case DEVICE_OFF:
pDev->public.on = FALSE;
RemoveEnabledDevice( darwinEventFD );
break;
case DEVICE_CLOSE:
break;
}
return Success;
}
/*
===========================================================================
Utility routines
===========================================================================
*/
/*
* DarwinFindLibraryFile
* Search for a file in the standard Library paths, which are (in order):
*
* ~/Library/ user specific
* /Library/ host specific
* /Network/Library/ LAN specific
* /System/Library/ OS specific
*
* A sub-path can be specified to search in below the various Library
* directories. Returns a new character string (owned by the caller)
* containing the full path to the first file found.
*/
static char * DarwinFindLibraryFile(
const char *file,
const char *pathext )
{
// Library search paths
char *pathList[] = {
"",
"/Network",
"/System",
NULL
};
char *home;
char *fullPath;
int i = 0;
// Return the file name as is if it is already a fully qualified path.
if (!access(file, F_OK)) {
fullPath = xalloc(strlen(file)+1);
strcpy(fullPath, file);
return fullPath;
}
fullPath = xalloc(PATH_MAX);
home = getenv("HOME");
if (home) {
snprintf(fullPath, PATH_MAX, "%s/Library/%s/%s", home, pathext, file);
if (!access(fullPath, F_OK))
return fullPath;
}
while (pathList[i]) {
snprintf(fullPath, PATH_MAX, "%s/Library/%s/%s", pathList[i++],
pathext, file);
if (!access(fullPath, F_OK))
return fullPath;
}
xfree(fullPath);
return NULL;
}
/*
* DarwinSimulateMouseClick
* Send a mouse click to X when multiple mouse buttons are simulated
* with modifier-clicks, such as command-click for button 2. The dix
* layer is told that the previously pressed modifier key(s) are
* released, the simulated click event is sent, and the modifier keys
* are reverted to their actual (pressed) state. This is usually
* closest to what the user wants. Ie. the user typically wants to
* simulate a button 2 press instead of Command-button 2.
*/
static void DarwinSimulateMouseClick(
xEvent xe, // event template with time and
// mouse position filled in
int whichButton, // mouse button to be pressed
int whichEvent, // ButtonPress or ButtonRelease
int keycodesUsed[], // list of keycodes of the modifiers used
// to create the fake click + MIN_KEYCODE
int numKeycodes ) // number of keycodes in list
{
int i;
// first fool X into forgetting about the keys
for (i = 0; i < numKeycodes; i++) {
xe.u.u.type = KeyRelease;
xe.u.u.detail = keycodesUsed[i];
(darwinKeyboard->public.processInputProc)
( &xe, darwinKeyboard, 1 );
}
// push the mouse button
xe.u.u.type = whichEvent;
xe.u.u.detail = whichButton;
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
// reset the keys
for (i = 0; i < numKeycodes; i++) {
xe.u.u.type = KeyPress;
xe.u.u.detail = keycodesUsed[i];
(darwinKeyboard->public.processInputProc)
( &xe, darwinKeyboard, 1 );
}
}
/*
* DarwinUpdateModifiers
* Send events to update the modifier state.
*/
static void DarwinUpdateModifiers(
xEvent xe, // event template with time, mouse position,
// and KeyPress or KeyRelease filled in
int flags ) // modifier flags that have changed
{
if (flags & NX_ALPHASHIFTMASK) {
xe.u.u.detail = DarwinModifierKeycode(NX_MODIFIERKEY_ALPHALOCK, 0);
(darwinKeyboard->public.processInputProc)
(&xe, darwinKeyboard, 1);
}
if (flags & NX_COMMANDMASK) {
xe.u.u.detail = DarwinModifierKeycode(NX_MODIFIERKEY_COMMAND, 0);
(darwinKeyboard->public.processInputProc)
(&xe, darwinKeyboard, 1);
}
if (flags & NX_CONTROLMASK) {
xe.u.u.detail = DarwinModifierKeycode(NX_MODIFIERKEY_CONTROL, 0);
(darwinKeyboard->public.processInputProc)
(&xe, darwinKeyboard, 1);
}
if (flags & NX_ALTERNATEMASK) {
xe.u.u.detail = DarwinModifierKeycode(NX_MODIFIERKEY_ALTERNATE, 0);
(darwinKeyboard->public.processInputProc)
(&xe, darwinKeyboard, 1);
}
if (flags & NX_SHIFTMASK) {
xe.u.u.detail = DarwinModifierKeycode(NX_MODIFIERKEY_SHIFT, 0);
(darwinKeyboard->public.processInputProc)
(&xe, darwinKeyboard, 1);
}
}
/*
===========================================================================
Functions needed to link against device independent X
===========================================================================
*/
/*
* ProcessInputEvents
* Read and process events from the event pipe until it is empty.
*/
void ProcessInputEvents(void)
{
xEvent xe;
NXEvent ev;
int r;
struct timeval tv;
struct timezone tz;
static int startsec = 0;
static Bool gotread = false;
static int old_state = 0;
#if defined(DARWIN_WITH_QUARTZ) && defined(QUARTZ_SAFETY_DELAY)
// Quartz safety quit. Bail if we don't get any events from the event pipe.
// If the event writer fails to find us, we will have captured the screen
// but not be seeing any events and be unkillable from the console.
if (quartz && ! gotread) {
gettimeofday(&tv, &tz);
if (startsec == 0) startsec = tv.tv_sec;
if (startsec + QUARTZ_SAFETY_DELAY< tv.tv_sec) {
QuartzGiveUp();
FatalError("%d second safety quit\n", QUARTZ_SAFETY_DELAY);
}
}
#endif
while (TRUE) {
// try to read from our pipe
r = read( darwinEventFD, &ev, sizeof(ev));
if ((r == -1) && (errno == EAGAIN)) {
// no event available
break;
} else if (r == -1) {
// FIXME: FatalError here? Reading may be permanently broken.
ErrorF( "Event pipe read failed, errno=%d: %s\n",
errno, strerror(errno) );
break;
} else if ( r == 0 ) {
FatalError("Darwin ProcessInputEvents: EOF on event pipe!\n");
} else if ( r != sizeof( ev ) ) {
ErrorF( "Only read %i bytes from event pipe!\n", r );
break;
}
gotread = true;
gettimeofday(&tv, &tz);
// translate it to an X event and post it
memset(&xe, 0, sizeof(xe));
xe.u.keyButtonPointer.rootX = ev.location.x;
xe.u.keyButtonPointer.rootY = ev.location.y;
//xe.u.keyButtonPointer.time = ev.time;
xe.u.keyButtonPointer.time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
/* A newer kernel generates multi-button events by NX_SYSDEFINED.
See iokit/Families/IOHIDSystem/IOHIDSystem.cpp version 1.1.1.7,
2000/08/10 00:23:37 or later. */
switch( ev.type ) {
case NX_MOUSEMOVED:
xe.u.u.type = MotionNotify;
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
break;
#ifdef __i386__
// x86 drivers currently reverse mouse up and down
case NX_LMOUSEUP:
#else
case NX_LMOUSEDOWN:
#endif
// Mimic multi-button mouse with Command and Option
if (darwinFakeButtons &&
ev.flags & (NX_COMMANDMASK | NX_ALTERNATEMASK)) {
int button;
int keycode;
if (ev.flags & NX_COMMANDMASK) {
button = 2;
keycode = darwinKeyCommandL;
} else {
button = 3;
keycode = darwinKeyOptionL;
}
DarwinSimulateMouseClick(xe, button, ButtonPress,
&keycode, 1);
} else {
xe.u.u.detail = 1;
xe.u.u.type = ButtonPress;
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
}
break;
#ifdef __i386__
case NX_LMOUSEDOWN:
#else
case NX_LMOUSEUP:
#endif
// Mimic multi-button mouse with Command and Option
if (darwinFakeButtons &&
ev.flags & (NX_COMMANDMASK | NX_ALTERNATEMASK)) {
int button;
int keycode;
if (ev.flags & NX_COMMANDMASK) {
button = 2;
keycode = darwinKeyCommandL;
} else {
button = 3;
keycode = darwinKeyOptionL;
}
DarwinSimulateMouseClick(xe, button, ButtonRelease,
&keycode, 1);
} else {
xe.u.u.detail = 1;
xe.u.u.type = ButtonRelease;
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
}
break;
// Button 2 isn't handled correctly by older kernels anyway.
// Just let NX_SYSDEFINED events handle these.
case NX_RMOUSEDOWN:
#if 0
xe.u.u.detail = 2;
xe.u.u.type = ButtonPress;
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
#endif
break;
case NX_RMOUSEUP:
#if 0
xe.u.u.detail = 2;
xe.u.u.type = ButtonRelease;
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
#endif
break;
case NX_KEYDOWN:
xe.u.u.type = KeyPress;
xe.u.u.detail = ev.data.key.keyCode + MIN_KEYCODE;
(darwinKeyboard->public.processInputProc)
( &xe, darwinKeyboard, 1 );
break;
case NX_KEYUP:
xe.u.u.type = KeyRelease;
xe.u.u.detail = ev.data.key.keyCode + MIN_KEYCODE;
(darwinKeyboard->public.processInputProc)
(&xe, darwinKeyboard, 1);
break;
case NX_FLAGSCHANGED:
{
// Assumes only one flag has changed. In Quartz mode, this
// restriction must be enforced by the Quartz event feeder.
int new_on_flags = ~old_state & ev.flags;
int new_off_flags = old_state & ~ev.flags;
old_state = ev.flags;
xe.u.u.detail = ev.data.key.keyCode + MIN_KEYCODE;
// Alphalock is toggled rather than held on,
// so we have to handle it differently.
if (new_on_flags & NX_ALPHASHIFTMASK ||
new_off_flags & NX_ALPHASHIFTMASK) {
xe.u.u.type = KeyPress;
(darwinKeyboard->public.processInputProc)
(&xe, darwinKeyboard, 1);
xe.u.u.type = KeyRelease;
(darwinKeyboard->public.processInputProc)
(&xe, darwinKeyboard, 1);
break;
}
if (new_on_flags) {
xe.u.u.type = KeyPress;
} else if (new_off_flags) {
xe.u.u.type = KeyRelease;
} else {
break;
}
(darwinKeyboard->public.processInputProc)
(&xe, darwinKeyboard, 1);
break;
}
case NX_SYSDEFINED:
if (ev.data.compound.subType == 7) {
long hwDelta = ev.data.compound.misc.L[0];
long hwButtons = ev.data.compound.misc.L[1];
int i;
for (i = 1; i < 4; i++) {
if (hwDelta & (1 << i)) {
xe.u.u.detail = i + 1;
if (hwButtons & (1 << i)) {
#ifdef __i386__
xe.u.u.type = ButtonRelease;
} else {
xe.u.u.type = ButtonPress;
#else
xe.u.u.type = ButtonPress;
} else {
xe.u.u.type = ButtonRelease;
#endif
}
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
}
}
}
break;
case NX_SCROLLWHEELMOVED:
{
short count = ev.data.scrollWheel.deltaAxis1;
if (count > 0) {
xe.u.u.detail = SCROLLWHEELUPFAKE;
} else {
xe.u.u.detail = SCROLLWHEELDOWNFAKE;
count = -count;
}
for (; count; --count) {
xe.u.u.type = ButtonPress;
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
xe.u.u.type = ButtonRelease;
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
}
break;
}
// Special events for Quartz support
case NX_APPDEFINED:
if (quartz) {
switch (ev.data.compound.subType) {
// Update modifier state. As opposed to NX_FLAGSCHANGED,
// in this case any amount of modifiers may have changed.
case kXDarwinUpdateModifiers:
xe.u.u.type = KeyRelease;
DarwinUpdateModifiers(xe, old_state & ~ev.flags);
xe.u.u.type = KeyPress;
DarwinUpdateModifiers(xe, ~old_state & ev.flags);
old_state = ev.flags;
break;
case kXDarwinShow:
QuartzShow(ev.location.x, ev.location.y);
// The mouse location will have moved; track it.
xe.u.u.type = MotionNotify;
(darwinPointer->public.processInputProc)
( &xe, darwinPointer, 1 );
break;
case kXDarwinHide:
QuartzHide();
break;
case kXDarwinQuit:
GiveUp(0);
break;
case kXDarwinReadPasteboard:
QuartzReadPasteboard();
break;
case kXDarwinWritePasteboard:
QuartzWritePasteboard();
break;
default:
ErrorF("Unknown application defined event.\n");
} // switch (ev.data.compound.subType)
} // if (quartz)
break;
default:
ErrorF("Unknown event caught: %d\n", ev.type);
ErrorF("\tev.type = %d\n", ev.type);
ErrorF("\tev.location.x,y = %d,%d\n", ev.location.x, ev.location.y);
ErrorF("\tev.time = %ld\n", ev.time);
ErrorF("\tev.flags = 0x%x\n", ev.flags);
ErrorF("\tev.window = %d\n", ev.window);
ErrorF("\tev.data.key.origCharSet = %d\n", ev.data.key.origCharSet);
ErrorF("\tev.data.key.charSet = %d\n", ev.data.key.charSet);
ErrorF("\tev.data.key.charCode = %d\n", ev.data.key.charCode);
ErrorF("\tev.data.key.keyCode = %d\n", ev.data.key.keyCode);
ErrorF("\tev.data.key.origCharCode = %d\n", ev.data.key.origCharCode);
break;
}
// why isn't this handled automatically by X???
//miPointerAbsoluteCursor( ev.location.x, ev.location.y, ev.time );
miPointerAbsoluteCursor( ev.location.x, ev.location.y,
tv.tv_sec * 1000 + tv.tv_usec / 1000 );
}
miPointerUpdate();
}
/*
* InitInput
* Register the keyboard and mouse devices
*/
void InitInput( int argc, char **argv )
{
if (serverGeneration == 1) {
darwinPointer = AddInputDevice(DarwinMouseProc, TRUE);
RegisterPointerDevice( darwinPointer );
darwinKeyboard = AddInputDevice(DarwinKeybdProc, TRUE);
RegisterKeyboardDevice( darwinKeyboard );
}
}
/*
* InitOutput
* Initialize screenInfo for all actually accessible framebuffers.
*/
void InitOutput( ScreenInfo *pScreenInfo, int argc, char **argv )
{
int i;
pScreenInfo->imageByteOrder = IMAGE_BYTE_ORDER;
pScreenInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
pScreenInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
// list how we want common pixmap formats to be padded
pScreenInfo->numPixmapFormats = NUMFORMATS;
for (i = 0; i < NUMFORMATS; i++)
pScreenInfo->formats[i] = formats[i];
AddScreen( DarwinAddScreen, argc, argv );
}
/*
* OsVendorFataError
*/
void OsVendorFatalError( void )
{
ErrorF( " OsVendorFatalError\n" );
}
/*
* OsVendorInit
* Initialization of Darwin support.
* Initialize display and event handling.
*/
void OsVendorInit(void)
{
DarwinPrintBanner();
if (quartz) {
QuartzOsVendorInit();
} else {
XFIOKitOsVendorInit();
}
}
/*
* ddxProcessArgument --
* Process device-dependent command line args. Returns 0 if argument is
* not device dependent, otherwise Count of number of elements of argv
* that are part of a device dependent commandline option.
*/
int ddxProcessArgument( int argc, char *argv[], int i )
{
if ( !strcmp( argv[i], "-screen" ) ) {
if ( i == argc-1 ) {
FatalError( "-screen must be followed by a number\n" );
}
darwinScreenNumber = atoi( argv[i+1] );
ErrorF( "Attempting to use screen number %i\n", darwinScreenNumber );
return 2;
}
if ( !strcmp( argv[i], "-fakebuttons" ) ) {
darwinFakeButtons = TRUE;
ErrorF( "Faking a three button mouse\n" );
return 1;
}
if ( !strcmp( argv[i], "-nofakebuttons" ) ) {
darwinFakeButtons = FALSE;
ErrorF( "Not faking a three button mouse\n" );
return 1;
}
if ( !strcmp( argv[i], "-keymap" ) ) {
if ( i == argc-1 ) {
FatalError( "-keymap must be followed by a filename\n" );
}
darwinKeymapFile = DarwinFindLibraryFile(argv[i+1], "Keyboards");
if ( !darwinKeymapFile )
FatalError( "Could not find keymapping file %s.\n", argv[i+1] );
ErrorF( "Using keymapping provided in %s.\n", darwinKeymapFile );
return 2;
}
#ifdef DARWIN_WITH_QUARTZ
if ( !strcmp( argv[i], "-quartz" ) ) {
quartz = TRUE;
ErrorF( "Running in parallel with Mac OS X Quartz window server.\n" );
#ifdef QUARTZ_SAFETY_DELAY
ErrorF( "Quitting in %d seconds if no controller is found.\n",
QUARTZ_SAFETY_DELAY );
#endif
return 1;
}
// The Mac OS X front end uses this argument, which we just ignore here.
if ( !strcmp( argv[i], "-nostartx" ) ) {
return 1;
}
// This command line arg is passed when launched from the Aqua GUI.
if ( !strncmp( argv[i], "-psn_", 5 ) ) {
return 1;
}
#endif
if ( !strcmp( argv[i], "-size" ) ) {
if ( i >= argc-2 ) {
FatalError( "-size must be followed by two numbers\n" );
}
#ifdef OLD_POWERBOOK_G3
ErrorF( "Ignoring unsupported -size option on old PowerBook G3\n" );
#else
darwinDesiredWidth = atoi( argv[i+1] );
darwinDesiredHeight = atoi( argv[i+2] );
ErrorF( "Attempting to use width x height = %i x %i\n",
darwinDesiredWidth, darwinDesiredHeight );
#endif
return 3;
}
if ( !strcmp( argv[i], "-depth" ) ) {
int bitDepth;
if ( i == argc-1 ) {
FatalError( "-depth must be followed by a number\n" );
}
#ifdef OLD_POWERBOOK_G3
ErrorF( "Ignoring unsupported -depth option on old PowerBook G3\n");
#else
bitDepth = atoi( argv[i+1] );
if (bitDepth == 8)
darwinDesiredDepth = 0;
else if (bitDepth == 15)
darwinDesiredDepth = 1;
else if (bitDepth == 24)
darwinDesiredDepth = 2;
else
FatalError( "Unsupported pixel depth. Use 8, 15, or 24 bits\n" );
ErrorF( "Attempting to use pixel depth of %i\n", bitDepth );
#endif
return 2;
}
if ( !strcmp( argv[i], "-refresh" ) ) {
if ( i == argc-1 ) {
FatalError( "-refresh must be followed by a number\n" );
}
#ifdef OLD_POWERBOOK_G3
ErrorF( "Ignoring unsupported -refresh option on old PowerBook G3\n");
#else
darwinDesiredRefresh = atoi( argv[i+1] );
ErrorF( "Attempting to use refresh rate of %i\n", darwinDesiredRefresh );
#endif
return 2;
}
if (!strcmp( argv[i], "-showconfig" ) || !strcmp( argv[i], "-version" )) {
DarwinPrintBanner();
exit(0);
}
return 0;
}
/*
* ddxUseMsg --
* Print out correct use of device dependent commandline options.
* Maybe the user now knows what really to do ...
*/
void ddxUseMsg( void )
{
ErrorF("\n");
ErrorF("\n");
ErrorF("Device Dependent Usage:\n");
ErrorF("\n");
ErrorF("-fakebuttons : fake a three button mouse with Command and Option keys.\n");
ErrorF("-nofakebuttons : don't fake a three button mouse.\n");
ErrorF("-keymap <file> : read the keymapping from a file instead of the kernel.\n");
ErrorF("-version : show the server version.\n");
#ifdef DARWIN_WITH_QUARTZ
ErrorF("-quartz : run in parallel with Mac OS X Quartz window server.\n");
ErrorF("\n");
ErrorF("IOKit specific options (ignored with -quartz):\n");
#endif
ErrorF("-screen <0,1,...> : use this screen number.\n");
ErrorF("-size <height> <width> : use a screen resolution of <height> x <width>.\n");
ErrorF("-depth <8,15,24> : use this bit depth.\n");
ErrorF("-refresh <rate> : use a monitor refresh rate of <rate> Hz.\n");
ErrorF("\n");
}
/*
* ddxGiveUp --
* Device dependent cleanup. Called by dix before normal server death.
*/
void ddxGiveUp( void )
{
ErrorF( " ddxGiveUp\n" );
close(darwinEventFD);
if (quartz) {
QuartzGiveUp();
} else {
XFIOKitGiveUp();
}
}
/*
* AbortDDX --
* DDX - specific abort routine. Called by AbortServer(). The attempt is
* made to restore all original setting of the displays. Also all devices
* are closed.
*/
void AbortDDX( void )
{
ErrorF( " AbortDDX\n" );
/*
* This is needed for a abnormal server exit, since the normal exit stuff
* MUST also be performed (i.e. the vt must be left in a defined state)
*/
ddxGiveUp();
}
Bool DPMSSupported(void)
{ return 0;
}
void DPMSSet(void)
{ return;
}
#include "mivalidate.h" // for union _Validate used by windowstr.h
#include "windowstr.h" // for struct _Window
#include "scrnintstr.h" // for struct _Screen
// This is copied from Xserver/hw/xfree86/common/xf86Helper.c.
// Quartz mode uses this when switching in and out of Quartz.
// Quartz or IOKit can use this when waking from sleep.
// Copyright (c) 1997-1998 by The XFree86 Project, Inc.
/*
* xf86SetRootClip --
* Enable or disable rendering to the screen by
* setting the root clip list and revalidating
* all of the windows
*/
void
xf86SetRootClip (ScreenPtr pScreen, BOOL enable)
{
WindowPtr pWin = WindowTable[pScreen->myNum];
WindowPtr pChild;
Bool WasViewable = (Bool)(pWin->viewable);
Bool anyMarked;
RegionPtr pOldClip, bsExposed;
#ifdef DO_SAVE_UNDERS
Bool dosave = FALSE;
#endif
WindowPtr pLayerWin;
BoxRec box;
if (WasViewable)
{
for (pChild = pWin->firstChild; pChild; pChild = pChild->nextSib)
{
(void) (*pScreen->MarkOverlappedWindows)(pChild,
pChild,
&pLayerWin);
}
(*pScreen->MarkWindow) (pWin);
anyMarked = TRUE;
if (pWin->valdata)
{
if (HasBorder (pWin))
{
RegionPtr borderVisible;
borderVisible = REGION_CREATE(pScreen, NullBox, 1);
REGION_SUBTRACT(pScreen, borderVisible,
&pWin->borderClip, &pWin->winSize);
pWin->valdata->before.borderVisible = borderVisible;
}
pWin->valdata->before.resized = TRUE;
}
}
/*
* Use REGION_BREAK to avoid optimizations in ValidateTree
* that assume the root borderClip can't change well, normally
* it doesn't...)
*/
if (enable)
{
box.x1 = 0;
box.y1 = 0;
box.x2 = pScreen->width;
box.y2 = pScreen->height;
REGION_RESET(pScreen, &pWin->borderClip, &box);
REGION_BREAK (pWin->drawable.pScreen, &pWin->clipList);
}
else
{
REGION_EMPTY(pScreen, &pWin->borderClip);
REGION_BREAK (pWin->drawable.pScreen, &pWin->clipList);
}
ResizeChildrenWinSize (pWin, 0, 0, 0, 0);
if (WasViewable)
{
if (pWin->backStorage)
{
pOldClip = REGION_CREATE(pScreen, NullBox, 1);
REGION_COPY(pScreen, pOldClip, &pWin->clipList);
}
if (pWin->firstChild)
{
anyMarked |= (*pScreen->MarkOverlappedWindows)(pWin->firstChild,
pWin->firstChild,
(WindowPtr *)NULL);
}
else
{
(*pScreen->MarkWindow) (pWin);
anyMarked = TRUE;
}
#ifdef DO_SAVE_UNDERS
if (DO_SAVE_UNDERS(pWin))
{
dosave = (*pScreen->ChangeSaveUnder)(pLayerWin, pLayerWin);
}
#endif /* DO_SAVE_UNDERS */
if (anyMarked)
(*pScreen->ValidateTree)(pWin, NullWindow, VTOther);
}
if (pWin->backStorage &&
((pWin->backingStore == Always) || WasViewable))
{
if (!WasViewable)
pOldClip = &pWin->clipList; /* a convenient empty region */
bsExposed = (*pScreen->TranslateBackingStore)
(pWin, 0, 0, pOldClip,
pWin->drawable.x, pWin->drawable.y);
if (WasViewable)
REGION_DESTROY(pScreen, pOldClip);
if (bsExposed)
{
RegionPtr valExposed = NullRegion;
if (pWin->valdata)
valExposed = &pWin->valdata->after.exposed;
(*pScreen->WindowExposures) (pWin, valExposed, bsExposed);
if (valExposed)
REGION_EMPTY(pScreen, valExposed);
REGION_DESTROY(pScreen, bsExposed);
}
}
if (WasViewable)
{
if (anyMarked)
(*pScreen->HandleExposures)(pWin);
#ifdef DO_SAVE_UNDERS
if (dosave)
(*pScreen->PostChangeSaveUnder)(pLayerWin, pLayerWin);
#endif /* DO_SAVE_UNDERS */
if (anyMarked && pScreen->PostValidateTree)
(*pScreen->PostValidateTree)(pWin, NullWindow, VTOther);
}
if (pWin->realized)
WindowsRestructured ();
FlushAllOutput ();
}
|