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
|
/*
* OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
* Original Xvnc code Copyright (C) 1999 AT&T Laboratories Cambridge.
* All Rights Reserved.
*
* Cut in two parts by Johannes Schindelin (2001): libvncserver and OSXvnc.
*
*
* This file implements every system specific function for Mac OS X.
*
* It includes the keyboard functions:
*
void KbdAddEvent(down, keySym, cl)
rfbBool down;
rfbKeySym keySym;
rfbClientPtr cl;
void KbdReleaseAllKeys()
*
* the mouse functions:
*
void PtrAddEvent(buttonMask, x, y, cl)
int buttonMask;
int x;
int y;
rfbClientPtr cl;
*
*/
#include <unistd.h>
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>
/* zlib doesn't like Byte already defined */
#undef Byte
#undef TRUE
#undef rfbBool
#include <rfb/rfb.h>
#include <rfb/keysym.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/pwr_mgt/IOPM.h>
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
rfbBool rfbNoDimming = FALSE;
rfbBool rfbNoSleep = TRUE;
static pthread_mutex_t dimming_mutex;
static unsigned long dim_time;
static unsigned long sleep_time;
static mach_port_t master_dev_port;
static io_connect_t power_mgt;
static rfbBool initialized = FALSE;
static rfbBool dim_time_saved = FALSE;
static rfbBool sleep_time_saved = FALSE;
static int
saveDimSettings(void)
{
if (IOPMGetAggressiveness(power_mgt,
kPMMinutesToDim,
&dim_time) != kIOReturnSuccess)
return -1;
dim_time_saved = TRUE;
return 0;
}
static int
restoreDimSettings(void)
{
if (!dim_time_saved)
return -1;
if (IOPMSetAggressiveness(power_mgt,
kPMMinutesToDim,
dim_time) != kIOReturnSuccess)
return -1;
dim_time_saved = FALSE;
dim_time = 0;
return 0;
}
static int
saveSleepSettings(void)
{
if (IOPMGetAggressiveness(power_mgt,
kPMMinutesToSleep,
&sleep_time) != kIOReturnSuccess)
return -1;
sleep_time_saved = TRUE;
return 0;
}
static int
restoreSleepSettings(void)
{
if (!sleep_time_saved)
return -1;
if (IOPMSetAggressiveness(power_mgt,
kPMMinutesToSleep,
sleep_time) != kIOReturnSuccess)
return -1;
sleep_time_saved = FALSE;
sleep_time = 0;
return 0;
}
int
rfbDimmingInit(void)
{
pthread_mutex_init(&dimming_mutex, NULL);
if (IOMasterPort(bootstrap_port, &master_dev_port) != kIOReturnSuccess)
return -1;
if (!(power_mgt = IOPMFindPowerManagement(master_dev_port)))
return -1;
if (rfbNoDimming) {
if (saveDimSettings() < 0)
return -1;
if (IOPMSetAggressiveness(power_mgt,
kPMMinutesToDim, 0) != kIOReturnSuccess)
return -1;
}
if (rfbNoSleep) {
if (saveSleepSettings() < 0)
return -1;
if (IOPMSetAggressiveness(power_mgt,
kPMMinutesToSleep, 0) != kIOReturnSuccess)
return -1;
}
initialized = TRUE;
return 0;
}
int
rfbUndim(void)
{
int result = -1;
pthread_mutex_lock(&dimming_mutex);
if (!initialized)
goto DONE;
if (!rfbNoDimming) {
if (saveDimSettings() < 0)
goto DONE;
if (IOPMSetAggressiveness(power_mgt, kPMMinutesToDim, 0) != kIOReturnSuccess)
goto DONE;
if (restoreDimSettings() < 0)
goto DONE;
}
if (!rfbNoSleep) {
if (saveSleepSettings() < 0)
goto DONE;
if (IOPMSetAggressiveness(power_mgt, kPMMinutesToSleep, 0) != kIOReturnSuccess)
goto DONE;
if (restoreSleepSettings() < 0)
goto DONE;
}
result = 0;
DONE:
pthread_mutex_unlock(&dimming_mutex);
return result;
}
int
rfbDimmingShutdown(void)
{
int result = -1;
if (!initialized)
goto DONE;
pthread_mutex_lock(&dimming_mutex);
if (dim_time_saved)
if (restoreDimSettings() < 0)
goto DONE;
if (sleep_time_saved)
if (restoreSleepSettings() < 0)
goto DONE;
result = 0;
DONE:
pthread_mutex_unlock(&dimming_mutex);
return result;
}
rfbScreenInfoPtr rfbScreen;
void rfbShutdown(rfbClientPtr cl);
/* some variables to enable special behaviour */
int startTime = -1, maxSecsToConnect = 0;
rfbBool disconnectAfterFirstClient = TRUE;
/* Where do I get the "official" list of Mac key codes?
Ripped these out of a Mac II emulator called Basilisk II
that I found on the net. */
static int keyTable[] = {
/* The alphabet */
XK_A, 0, /* A */
XK_B, 11, /* B */
XK_C, 8, /* C */
XK_D, 2, /* D */
XK_E, 14, /* E */
XK_F, 3, /* F */
XK_G, 5, /* G */
XK_H, 4, /* H */
XK_I, 34, /* I */
XK_J, 38, /* J */
XK_K, 40, /* K */
XK_L, 37, /* L */
XK_M, 46, /* M */
XK_N, 45, /* N */
XK_O, 31, /* O */
XK_P, 35, /* P */
XK_Q, 12, /* Q */
XK_R, 15, /* R */
XK_S, 1, /* S */
XK_T, 17, /* T */
XK_U, 32, /* U */
XK_V, 9, /* V */
XK_W, 13, /* W */
XK_X, 7, /* X */
XK_Y, 16, /* Y */
XK_Z, 6, /* Z */
XK_a, 0, /* a */
XK_b, 11, /* b */
XK_c, 8, /* c */
XK_d, 2, /* d */
XK_e, 14, /* e */
XK_f, 3, /* f */
XK_g, 5, /* g */
XK_h, 4, /* h */
XK_i, 34, /* i */
XK_j, 38, /* j */
XK_k, 40, /* k */
XK_l, 37, /* l */
XK_m, 46, /* m */
XK_n, 45, /* n */
XK_o, 31, /* o */
XK_p, 35, /* p */
XK_q, 12, /* q */
XK_r, 15, /* r */
XK_s, 1, /* s */
XK_t, 17, /* t */
XK_u, 32, /* u */
XK_v, 9, /* v */
XK_w, 13, /* w */
XK_x, 7, /* x */
XK_y, 16, /* y */
XK_z, 6, /* z */
/* Numbers */
XK_0, 29, /* 0 */
XK_1, 18, /* 1 */
XK_2, 19, /* 2 */
XK_3, 20, /* 3 */
XK_4, 21, /* 4 */
XK_5, 23, /* 5 */
XK_6, 22, /* 6 */
XK_7, 26, /* 7 */
XK_8, 28, /* 8 */
XK_9, 25, /* 9 */
/* Symbols */
XK_exclam, 18, /* ! */
XK_at, 19, /* @ */
XK_numbersign, 20, /* # */
XK_dollar, 21, /* $ */
XK_percent, 23, /* % */
XK_asciicircum, 22, /* ^ */
XK_ampersand, 26, /* & */
XK_asterisk, 28, /* * */
XK_parenleft, 25, /* ( */
XK_parenright, 29, /* ) */
XK_minus, 27, /* - */
XK_underscore, 27, /* _ */
XK_equal, 24, /* = */
XK_plus, 24, /* + */
XK_grave, 10, /* ` */ /* XXX ? */
XK_asciitilde, 10, /* ~ */
XK_bracketleft, 33, /* [ */
XK_braceleft, 33, /* { */
XK_bracketright, 30, /* ] */
XK_braceright, 30, /* } */
XK_semicolon, 41, /* ; */
XK_colon, 41, /* : */
XK_apostrophe, 39, /* ' */
XK_quotedbl, 39, /* " */
XK_comma, 43, /* , */
XK_less, 43, /* < */
XK_period, 47, /* . */
XK_greater, 47, /* > */
XK_slash, 44, /* / */
XK_question, 44, /* ? */
XK_backslash, 42, /* \ */
XK_bar, 42, /* | */
/* "Special" keys */
XK_space, 49, /* Space */
XK_Return, 36, /* Return */
XK_Delete, 117, /* Delete */
XK_Tab, 48, /* Tab */
XK_Escape, 53, /* Esc */
XK_Caps_Lock, 57, /* Caps Lock */
XK_Num_Lock, 71, /* Num Lock */
XK_Scroll_Lock, 107, /* Scroll Lock */
XK_Pause, 113, /* Pause */
XK_BackSpace, 51, /* Backspace */
XK_Insert, 114, /* Insert */
/* Cursor movement */
XK_Up, 126, /* Cursor Up */
XK_Down, 125, /* Cursor Down */
XK_Left, 123, /* Cursor Left */
XK_Right, 124, /* Cursor Right */
XK_Page_Up, 116, /* Page Up */
XK_Page_Down, 121, /* Page Down */
XK_Home, 115, /* Home */
XK_End, 119, /* End */
/* Numeric keypad */
XK_KP_0, 82, /* KP 0 */
XK_KP_1, 83, /* KP 1 */
XK_KP_2, 84, /* KP 2 */
XK_KP_3, 85, /* KP 3 */
XK_KP_4, 86, /* KP 4 */
XK_KP_5, 87, /* KP 5 */
XK_KP_6, 88, /* KP 6 */
XK_KP_7, 89, /* KP 7 */
XK_KP_8, 91, /* KP 8 */
XK_KP_9, 92, /* KP 9 */
XK_KP_Enter, 76, /* KP Enter */
XK_KP_Decimal, 65, /* KP . */
XK_KP_Add, 69, /* KP + */
XK_KP_Subtract, 78, /* KP - */
XK_KP_Multiply, 67, /* KP * */
XK_KP_Divide, 75, /* KP / */
/* Function keys */
XK_F1, 122, /* F1 */
XK_F2, 120, /* F2 */
XK_F3, 99, /* F3 */
XK_F4, 118, /* F4 */
XK_F5, 96, /* F5 */
XK_F6, 97, /* F6 */
XK_F7, 98, /* F7 */
XK_F8, 100, /* F8 */
XK_F9, 101, /* F9 */
XK_F10, 109, /* F10 */
XK_F11, 103, /* F11 */
XK_F12, 111, /* F12 */
/* Modifier keys */
XK_Shift_L, 56, /* Shift Left */
XK_Shift_R, 56, /* Shift Right */
XK_Control_L, 59, /* Ctrl Left */
XK_Control_R, 59, /* Ctrl Right */
XK_Meta_L, 58, /* Logo Left (-> Option) */
XK_Meta_R, 58, /* Logo Right (-> Option) */
XK_Alt_L, 55, /* Alt Left (-> Command) */
XK_Alt_R, 55, /* Alt Right (-> Command) */
/* Weirdness I can't figure out */
#if 0
XK_3270_PrintScreen, 105, /* PrintScrn */
??? 94, 50, /* International */
XK_Menu, 50, /* Menu (-> International) */
#endif
};
void
KbdAddEvent(rfbBool down, rfbKeySym keySym, struct _rfbClientRec* cl)
{
int i;
CGKeyCode keyCode = -1;
int found = 0;
if(((int)cl->clientData)==-1) return; /* viewOnly */
rfbUndim();
for (i = 0; i < (sizeof(keyTable) / sizeof(int)); i += 2) {
if (keyTable[i] == keySym) {
keyCode = keyTable[i+1];
found = 1;
break;
}
}
if (!found) {
rfbErr("warning: couldn't figure out keycode for X keysym %d (0x%x)\n",
(int)keySym, (int)keySym);
} else {
/* Hopefully I can get away with not specifying a CGCharCode.
(Why would you need both?) */
CGPostKeyboardEvent((CGCharCode)0, keyCode, down);
}
}
void
PtrAddEvent(buttonMask, x, y, cl)
int buttonMask;
int x;
int y;
rfbClientPtr cl;
{
CGPoint position;
if(((int)cl->clientData)==-1) return; /* viewOnly */
rfbUndim();
position.x = x;
position.y = y;
CGPostMouseEvent(position, TRUE, 8,
(buttonMask & (1 << 0)) ? TRUE : FALSE,
(buttonMask & (1 << 1)) ? TRUE : FALSE,
(buttonMask & (1 << 2)) ? TRUE : FALSE,
(buttonMask & (1 << 3)) ? TRUE : FALSE,
(buttonMask & (1 << 4)) ? TRUE : FALSE,
(buttonMask & (1 << 5)) ? TRUE : FALSE,
(buttonMask & (1 << 6)) ? TRUE : FALSE,
(buttonMask & (1 << 7)) ? TRUE : FALSE);
}
rfbBool viewOnly = FALSE, sharedMode = FALSE;
void
ScreenInit(int argc, char**argv)
{
int bitsPerSample=CGDisplayBitsPerSample(kCGDirectMainDisplay);
rfbScreen = rfbGetScreen(&argc,argv,
CGDisplayPixelsWide(kCGDirectMainDisplay),
CGDisplayPixelsHigh(kCGDirectMainDisplay),
bitsPerSample,
CGDisplaySamplesPerPixel(kCGDirectMainDisplay),4);
if(!rfbScreen)
exit(0);
rfbScreen->serverFormat.redShift = bitsPerSample*2;
rfbScreen->serverFormat.greenShift = bitsPerSample*1;
rfbScreen->serverFormat.blueShift = 0;
gethostname(rfbScreen->thisHost, 255);
rfbScreen->paddedWidthInBytes = CGDisplayBytesPerRow(kCGDirectMainDisplay);
rfbScreen->frameBuffer =
(char *)CGDisplayBaseAddress(kCGDirectMainDisplay);
/* we cannot write to the frame buffer */
rfbScreen->cursor = NULL;
rfbScreen->ptrAddEvent = PtrAddEvent;
rfbScreen->kbdAddEvent = KbdAddEvent;
if(sharedMode) {
rfbScreen->alwaysShared = TRUE;
}
rfbInitServer(rfbScreen);
}
static void
refreshCallback(CGRectCount count, const CGRect *rectArray, void *ignore)
{
int i;
if(startTime>0 && time(0)>startTime+maxSecsToConnect)
rfbShutdown(0);
for (i = 0; i < count; i++)
rfbMarkRectAsModified(rfbScreen,
rectArray[i].origin.x,rectArray[i].origin.y,
rectArray[i].origin.x + rectArray[i].size.width,
rectArray[i].origin.y + rectArray[i].size.height);
}
void clientGone(rfbClientPtr cl)
{
rfbShutdown(cl);
}
enum rfbNewClientAction newClient(rfbClientPtr cl)
{
if(startTime>0 && time(0)>startTime+maxSecsToConnect)
rfbShutdown(cl);
if(disconnectAfterFirstClient)
cl->clientGoneHook = clientGone;
cl->clientData=(void*)((viewOnly)?-1:0);
return(RFB_CLIENT_ACCEPT);
}
int main(int argc,char *argv[])
{
int i;
for(i=argc-1;i>0;i--)
if(i<argc-1 && strcmp(argv[i],"-wait4client")==0) {
maxSecsToConnect = atoi(argv[i+1])/1000;
startTime = time(0);
} else if(strcmp(argv[i],"-runforever")==0) {
disconnectAfterFirstClient = FALSE;
} else if(strcmp(argv[i],"-viewonly")==0) {
viewOnly=TRUE;
} else if(strcmp(argv[i],"-shared")==0) {
sharedMode=TRUE;
}
rfbDimmingInit();
ScreenInit(argc,argv);
rfbScreen->newClientHook = newClient;
/* enter background event loop */
rfbRunEventLoop(rfbScreen,40,TRUE);
/* enter OS X loop */
CGRegisterScreenRefreshCallback(refreshCallback, NULL);
RunApplicationEventLoop();
rfbDimmingShutdown();
return(0); /* never ... */
}
void rfbShutdown(rfbClientPtr cl)
{
rfbScreenCleanup(rfbScreen);
rfbDimmingShutdown();
exit(0);
}
|