~ubuntu-branches/debian/lenny/italc/lenny

« back to all changes in this revision

Viewing changes to ica/x11/x11vnc/macosxCG.c

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Winnertz
  • Date: 2008-06-17 13:46:54 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080617134654-cl0gi4u524cv1ici
Tags: 1:1.0.9~rc3-1
* Package new upstream version
  - upstream ported the code to qt4.4 (Closes: #481974)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -- macosxCG.c -- */
 
2
 
 
3
/*
 
4
 * We need to keep this separate from nearly everything else, e.g. rfb.h
 
5
 * and the other stuff, otherwise it does not work properly, mouse drags
 
6
 * will not work!!
 
7
 */
 
8
 
 
9
void macosxCG_dummy(void) {}
 
10
 
 
11
#if (defined(__MACH__) && defined(__APPLE__))
 
12
 
 
13
#include <ApplicationServices/ApplicationServices.h>
 
14
#include <Cocoa/Cocoa.h>
 
15
#include <Carbon/Carbon.h>
 
16
 
 
17
void macosxCG_init(void);
 
18
void macosxCG_fini(void);
 
19
void macosxCG_event_loop(void);
 
20
char *macosxCG_get_fb_addr(void);
 
21
 
 
22
int macosxCG_CGDisplayPixelsWide(void);
 
23
int macosxCG_CGDisplayPixelsHigh(void);
 
24
int macosxCG_CGDisplayBitsPerPixel(void);
 
25
int macosxCG_CGDisplayBitsPerSample(void);
 
26
int macosxCG_CGDisplaySamplesPerPixel(void);
 
27
int macosxCG_CGDisplayBytesPerRow(void);
 
28
 
 
29
void macosxCG_pointer_inject(int mask, int x, int y);
 
30
int macosxCG_get_cursor_pos(int *x, int *y);
 
31
int macosxCG_get_cursor(void);
 
32
void macosxCG_init_key_table(void);
 
33
void macosxCG_key_inject(int down, unsigned int keysym);
 
34
 
 
35
CGDirectDisplayID displayID = 0;
 
36
 
 
37
extern void macosx_log(char *);
 
38
extern int collect_non_X_xdamage(int x_in, int y_in, int w_in, int h_in, int call);
 
39
 
 
40
static void macosxCG_callback(CGRectCount n, const CGRect *rects, void *dum) {
 
41
        int i, db = 0;
 
42
        if (db) fprintf(stderr, "macosx_callback: n=%d\n", (int) n);
 
43
        if (!dum) {}
 
44
        for (i=0; i < (int) n; i++) {
 
45
                if (db > 1) fprintf(stderr, "               : %g %g - %g %g\n", rects[i].origin.x, rects[i].origin.y, rects[i].size.width, rects[i].size.height);
 
46
                collect_non_X_xdamage( (int) rects[i].origin.x, (int) rects[i].origin.y,
 
47
                    (int) rects[i].size.width, (int) rects[i].size.height, 1);
 
48
        }
 
49
}
 
50
 
 
51
int dragum(void) {
 
52
#if 0
 
53
        int x =200, y = 150, dy = 10, i;
 
54
        CGPoint loc;
 
55
 
 
56
        CGDirectDisplayID displayID2 = kCGDirectMainDisplay;
 
57
        (void) GetMainDevice();
 
58
 
 
59
        for (i=0; i< 50; i++) {
 
60
                usleep(1000*100);
 
61
                loc.x = x;
 
62
                loc.y = y + i*dy;
 
63
                CGPostMouseEvent(loc, TRUE, 1, TRUE);
 
64
        }
 
65
        CGPostMouseEvent(loc, TRUE, 1, FALSE);
 
66
        usleep(4*1000*1000);
 
67
#endif
 
68
        return 0;
 
69
}
 
70
 
 
71
static int callback_set = 0;
 
72
extern int nofb;
 
73
 
 
74
void macosxCG_refresh_callback_on(void) {
 
75
        if (nofb) {
 
76
                return;
 
77
        }
 
78
 
 
79
        if (! callback_set) {
 
80
                if (1) macosx_log("macosxCG_callback: register\n");
 
81
                CGRegisterScreenRefreshCallback(macosxCG_callback, NULL);
 
82
        }
 
83
        callback_set = 1;
 
84
}
 
85
 
 
86
void macosxCG_refresh_callback_off(void) {
 
87
        if (callback_set) {
 
88
                if (1) macosx_log("macosxCG_callback: unregister\n");
 
89
                CGUnregisterScreenRefreshCallback(macosxCG_callback, NULL);
 
90
        }
 
91
        callback_set = 0;
 
92
}
 
93
 
 
94
extern int macosx_noscreensaver;
 
95
extern void macosxGCS_initpb(void);
 
96
extern int macosxCGP_init_dimming(void);
 
97
extern int macosxCGP_undim(void);
 
98
extern int macosxCGP_dim_shutdown(void);
 
99
extern void macosxCGP_screensaver_timer_off(void);
 
100
extern void macosxCGP_screensaver_timer_on(void);
 
101
 
 
102
void macosxCG_init(void) {
 
103
        if (displayID == 0) {
 
104
                macosx_log("macosxCG_init: initializing display.\n");
 
105
#if 0
 
106
                dragum();
 
107
#endif
 
108
 
 
109
                displayID = kCGDirectMainDisplay;
 
110
                (void) GetMainDevice();
 
111
 
 
112
                CGSetLocalEventsSuppressionInterval(0.0);
 
113
                CGSetLocalEventsFilterDuringSupressionState(
 
114
                    kCGEventFilterMaskPermitAllEvents,
 
115
                    kCGEventSupressionStateSupressionInterval);
 
116
                CGSetLocalEventsFilterDuringSupressionState(
 
117
                    kCGEventFilterMaskPermitAllEvents,
 
118
                    kCGEventSupressionStateRemoteMouseDrag);
 
119
 
 
120
                macosxCGP_init_dimming();
 
121
                if (macosx_noscreensaver) {
 
122
                        macosxCGP_screensaver_timer_on();
 
123
                }
 
124
                macosxGCS_initpb();
 
125
        }
 
126
}
 
127
 
 
128
void macosxCG_fini(void) {
 
129
        macosxCGP_dim_shutdown();
 
130
        if (macosx_noscreensaver) {
 
131
                macosxCGP_screensaver_timer_off();
 
132
        }
 
133
        macosxCG_refresh_callback_off();
 
134
}
 
135
 
 
136
extern int dpy_x, dpy_y, bpp, wdpy_x, wdpy_y;
 
137
extern int client_count, nofb;
 
138
extern void do_new_fb(int);
 
139
extern int macosx_wait_for_switch, macosx_resize;
 
140
 
 
141
extern void macosxGCS_poll_pb(void);
 
142
#if 0
 
143
extern void usleep(unsigned long usec);
 
144
#else
 
145
extern int usleep(useconds_t usec);
 
146
#endif
 
147
extern unsigned int sleep(unsigned int seconds);
 
148
extern void clean_up_exit (int ret);
 
149
 
 
150
void macosxCG_event_loop(void) {
 
151
        OSStatus rc;
 
152
        int nbpp;
 
153
 
 
154
        macosxGCS_poll_pb();
 
155
        if (nofb) {
 
156
                return;
 
157
        }
 
158
 
 
159
        rc = RunCurrentEventLoop(kEventDurationSecond/30);
 
160
 
 
161
        if (client_count) {
 
162
                macosxCG_refresh_callback_on();
 
163
        } else {
 
164
                macosxCG_refresh_callback_off();
 
165
        }
 
166
 
 
167
        nbpp = macosxCG_CGDisplayBitsPerPixel();
 
168
 
 
169
                
 
170
        if (nbpp > 0 && nbpp != bpp) {
 
171
                if (macosx_resize) {
 
172
                        do_new_fb(1);
 
173
                }
 
174
        } else if (wdpy_x != (int) CGDisplayPixelsWide(displayID)) {
 
175
            if (wdpy_y != (int) CGDisplayPixelsHigh(displayID)) {
 
176
                if (macosx_wait_for_switch) {
 
177
                        int cnt = 0;
 
178
                        while (1) {
 
179
                                if(CGDisplayPixelsWide(displayID) > 0) {
 
180
                                        if(CGDisplayPixelsHigh(displayID) > 0) {
 
181
                                                usleep(500*1000);
 
182
                                                break;
 
183
                                        }
 
184
                                }
 
185
                                if ((cnt++ % 120) == 0) {
 
186
                                        macosx_log("waiting for user to "
 
187
                                            "switch back..\n");
 
188
                                }
 
189
                                sleep(1);
 
190
                        }
 
191
                        if (wdpy_x == (int) CGDisplayPixelsWide(displayID)) {
 
192
                                if (wdpy_y == (int) CGDisplayPixelsHigh(displayID)) {
 
193
                                        macosx_log("we're back...\n");
 
194
                                        return;
 
195
                                }
 
196
                        }
 
197
                }
 
198
                if (macosx_resize) {
 
199
                        do_new_fb(1);
 
200
                }
 
201
            }
 
202
        }
 
203
}
 
204
 
 
205
char *macosxCG_get_fb_addr(void) {
 
206
        macosxCG_init();
 
207
        return (char *) CGDisplayBaseAddress(displayID);
 
208
}
 
209
 
 
210
int macosxCG_CGDisplayPixelsWide(void) {
 
211
        return (int) CGDisplayPixelsWide(displayID);
 
212
}
 
213
int macosxCG_CGDisplayPixelsHigh(void) {
 
214
        return (int) CGDisplayPixelsHigh(displayID);
 
215
}
 
216
int macosxCG_CGDisplayBitsPerPixel(void) {
 
217
        return (int) CGDisplayBitsPerPixel(displayID);
 
218
}
 
219
int macosxCG_CGDisplayBitsPerSample(void) {
 
220
        return (int) CGDisplayBitsPerSample(displayID);
 
221
}
 
222
int macosxCG_CGDisplaySamplesPerPixel(void) {
 
223
        return (int) CGDisplaySamplesPerPixel(displayID);
 
224
}
 
225
int macosxCG_CGDisplayBytesPerRow(void) {
 
226
        return (int) CGDisplayBytesPerRow(displayID);;
 
227
}
 
228
 
 
229
typedef int CGSConnectionRef;
 
230
static CGSConnectionRef conn = 0;
 
231
extern CGError CGSNewConnection(void*, CGSConnectionRef*);
 
232
extern CGError CGSReleaseConnection(CGSConnectionRef);
 
233
extern CGError CGSGetGlobalCursorDataSize(CGSConnectionRef, int*);
 
234
extern CGError CGSGetGlobalCursorData(CGSConnectionRef, unsigned char*,
 
235
    int*, int*, CGRect*, CGPoint*, int*, int*, int*);
 
236
extern CGError CGSGetCurrentCursorLocation(CGSConnectionRef, CGPoint*);
 
237
extern int CGSCurrentCursorSeed(void);
 
238
extern int CGSHardwareCursorActive(); 
 
239
 
 
240
static unsigned int last_local_button_mask = 0;
 
241
static unsigned int last_local_mod_mask = 0;
 
242
static int last_local_x = 0;
 
243
static int last_local_y = 0;
 
244
 
 
245
extern unsigned int display_button_mask;
 
246
extern unsigned int display_mod_mask;
 
247
extern int got_local_pointer_input;
 
248
extern time_t last_local_input;
 
249
 
 
250
static CGPoint current_cursor_pos(void) {
 
251
        CGPoint pos;
 
252
        pos.x = 0;
 
253
        pos.y = 0;
 
254
        if (! conn) {
 
255
                if (CGSNewConnection(NULL, &conn) != kCGErrorSuccess) {
 
256
                        macosx_log("CGSNewConnection error.\n");
 
257
                        if (!dpy_x || !dpy_y || !wdpy_x || !wdpy_y) {
 
258
                                clean_up_exit(1);
 
259
                        }
 
260
                }
 
261
        }
 
262
        if (CGSGetCurrentCursorLocation(conn, &pos) != kCGErrorSuccess) {
 
263
                macosx_log("CGSGetCurrentCursorLocation error\n");
 
264
        }
 
265
 
 
266
        display_button_mask = GetCurrentButtonState();
 
267
#if 0
 
268
/* not used yet */
 
269
        display_mod_mask = GetCurrentKeyModifiers();
 
270
#endif
 
271
 
 
272
        if (last_local_button_mask != display_button_mask) {
 
273
                got_local_pointer_input++;
 
274
                last_local_input = time(NULL);
 
275
        } else if (pos.x != last_local_x || pos.y != last_local_y) {
 
276
                got_local_pointer_input++;
 
277
                last_local_input = time(NULL);
 
278
        }
 
279
        last_local_button_mask = display_button_mask;
 
280
        last_local_mod_mask = display_mod_mask;
 
281
        last_local_x = pos.x;
 
282
        last_local_y = pos.y;
 
283
 
 
284
        return pos;
 
285
}
 
286
 
 
287
int macosxCG_get_cursor_pos(int *x, int *y) {
 
288
        CGPoint pos = current_cursor_pos();
 
289
        *x = pos.x;
 
290
        *y = pos.y;
 
291
        return 1;
 
292
}
 
293
 
 
294
extern int get_cursor_serial(int);
 
295
extern int store_cursor(int serial, unsigned long *data, int w, int h, int cbpp, int xhot, int yhot);
 
296
 
 
297
int macosxCG_get_cursor(void) {
 
298
        int last_idx = (int) get_cursor_serial(1);
 
299
        int which = 1;
 
300
        CGError err;
 
301
        int datasize, row_bytes, cdepth, comps, bpcomp;
 
302
        CGRect rect;
 
303
        CGPoint hot;
 
304
        unsigned char *data;
 
305
        int cursor_seed;
 
306
        static int last_cursor_seed = -1;
 
307
        static time_t last_fetch = 0;
 
308
        time_t now = time(NULL);
 
309
 
 
310
        if (last_idx) {
 
311
                which = last_idx;
 
312
        }
 
313
 
 
314
        if (! conn) {
 
315
                if (CGSNewConnection(NULL, &conn) != kCGErrorSuccess) {
 
316
                        macosx_log("CGSNewConnection error.\n");
 
317
                        if (!dpy_x || !dpy_y || !wdpy_x || !wdpy_y) {
 
318
                                clean_up_exit(1);
 
319
                        }
 
320
                        return which;
 
321
                }
 
322
        }
 
323
 
 
324
        cursor_seed = CGSCurrentCursorSeed();
 
325
        if (last_idx && cursor_seed == last_cursor_seed) {
 
326
                if (now < last_fetch + 2) {
 
327
                        return which;
 
328
                }
 
329
        }
 
330
        last_cursor_seed = cursor_seed;
 
331
        last_fetch = now;
 
332
 
 
333
        if (CGSGetGlobalCursorDataSize(conn, &datasize) != kCGErrorSuccess) {
 
334
                macosx_log("CGSGetGlobalCursorDataSize error\n");
 
335
                return which;
 
336
        }
 
337
 
 
338
        data = (unsigned char*) malloc(datasize);
 
339
 
 
340
        err = CGSGetGlobalCursorData(conn, data, &datasize, &row_bytes,
 
341
            &rect, &hot, &cdepth, &comps, &bpcomp);
 
342
        if (err != kCGErrorSuccess) {
 
343
                macosx_log("CGSGetGlobalCursorData error\n");
 
344
                return which;
 
345
        }
 
346
 
 
347
        if (cdepth == 24) {
 
348
                cdepth = 32;
 
349
        }
 
350
 
 
351
        which = store_cursor(cursor_seed, (unsigned long*) data,
 
352
            (int) rect.size.width, (int) rect.size.height, cdepth, (int) hot.x, (int) hot.y);
 
353
 
 
354
        free(data);
 
355
        return(which);
 
356
}
 
357
 
 
358
extern int macosx_mouse_wheel_speed;
 
359
extern int macosx_swap23;
 
360
extern int off_x, coff_x, off_y, coff_y;
 
361
 
 
362
void macosxCG_pointer_inject(int mask, int x, int y) {
 
363
        int swap23 = macosx_swap23;
 
364
        int s1 = 0, s2 = 1, s3 = 2, s4 = 3, s5 = 4;
 
365
        CGPoint loc;
 
366
        int wheel_distance = macosx_mouse_wheel_speed;
 
367
        static int cnt = 0;
 
368
 
 
369
        if (swap23) {
 
370
                s2 = 2;
 
371
                s3 = 1;
 
372
        }
 
373
 
 
374
        loc.x = x + off_x + coff_x;
 
375
        loc.y = y + off_y + coff_y;
 
376
 
 
377
        if ((cnt++ % 10) == 0) {
 
378
                macosxCGP_undim();
 
379
        }
 
380
 
 
381
        if ((mask & (1 << s4))) {
 
382
                CGPostScrollWheelEvent(1,  wheel_distance);
 
383
        }
 
384
        if ((mask & (1 << s5))) {
 
385
                CGPostScrollWheelEvent(1, -wheel_distance);
 
386
        }
 
387
        
 
388
        CGPostMouseEvent(loc, TRUE, 3,
 
389
            (mask & (1 << s1)) ? TRUE : FALSE,
 
390
            (mask & (1 << s2)) ? TRUE : FALSE,
 
391
            (mask & (1 << s3)) ? TRUE : FALSE
 
392
        );
 
393
}
 
394
 
 
395
#define keyTableSize 0xFFFF
 
396
 
 
397
#include <rfb/keysym.h>
 
398
 
 
399
static int USKeyCodes[] = {
 
400
    /* The alphabet */
 
401
    XK_A,                  0,      /* A */
 
402
    XK_B,                 11,      /* B */
 
403
    XK_C,                  8,      /* C */
 
404
    XK_D,                  2,      /* D */
 
405
    XK_E,                 14,      /* E */
 
406
    XK_F,                  3,      /* F */
 
407
    XK_G,                  5,      /* G */
 
408
    XK_H,                  4,      /* H */
 
409
    XK_I,                 34,      /* I */
 
410
    XK_J,                 38,      /* J */
 
411
    XK_K,                 40,      /* K */
 
412
    XK_L,                 37,      /* L */
 
413
    XK_M,                 46,      /* M */
 
414
    XK_N,                 45,      /* N */
 
415
    XK_O,                 31,      /* O */
 
416
    XK_P,                 35,      /* P */
 
417
    XK_Q,                 12,      /* Q */
 
418
    XK_R,                 15,      /* R */
 
419
    XK_S,                  1,      /* S */
 
420
    XK_T,                 17,      /* T */
 
421
    XK_U,                 32,      /* U */
 
422
    XK_V,                  9,      /* V */
 
423
    XK_W,                 13,      /* W */
 
424
    XK_X,                  7,      /* X */
 
425
    XK_Y,                 16,      /* Y */
 
426
    XK_Z,                  6,      /* Z */
 
427
    XK_a,                  0,      /* a */
 
428
    XK_b,                 11,      /* b */
 
429
    XK_c,                  8,      /* c */
 
430
    XK_d,                  2,      /* d */
 
431
    XK_e,                 14,      /* e */
 
432
    XK_f,                  3,      /* f */
 
433
    XK_g,                  5,      /* g */
 
434
    XK_h,                  4,      /* h */
 
435
    XK_i,                 34,      /* i */
 
436
    XK_j,                 38,      /* j */
 
437
    XK_k,                 40,      /* k */
 
438
    XK_l,                 37,      /* l */
 
439
    XK_m,                 46,      /* m */
 
440
    XK_n,                 45,      /* n */
 
441
    XK_o,                 31,      /* o */
 
442
    XK_p,                 35,      /* p */
 
443
    XK_q,                 12,      /* q */
 
444
    XK_r,                 15,      /* r */
 
445
    XK_s,                  1,      /* s */
 
446
    XK_t,                 17,      /* t */
 
447
    XK_u,                 32,      /* u */
 
448
    XK_v,                  9,      /* v */
 
449
    XK_w,                 13,      /* w */
 
450
    XK_x,                  7,      /* x */
 
451
    XK_y,                 16,      /* y */
 
452
    XK_z,                  6,      /* z */
 
453
 
 
454
    /* Numbers */
 
455
    XK_0,                 29,      /* 0 */
 
456
    XK_1,                 18,      /* 1 */
 
457
    XK_2,                 19,      /* 2 */
 
458
    XK_3,                 20,      /* 3 */
 
459
    XK_4,                 21,      /* 4 */
 
460
    XK_5,                 23,      /* 5 */
 
461
    XK_6,                 22,      /* 6 */
 
462
    XK_7,                 26,      /* 7 */
 
463
    XK_8,                 28,      /* 8 */
 
464
    XK_9,                 25,      /* 9 */
 
465
 
 
466
    /* Symbols */
 
467
    XK_exclam,            18,      /* ! */
 
468
    XK_at,                19,      /* @ */
 
469
    XK_numbersign,        20,      /* # */
 
470
    XK_dollar,            21,      /* $ */
 
471
    XK_percent,           23,      /* % */
 
472
    XK_asciicircum,       22,      /* ^ */
 
473
    XK_ampersand,         26,      /* & */
 
474
    XK_asterisk,          28,      /* * */
 
475
    XK_parenleft,         25,      /* ( */
 
476
    XK_parenright,        29,      /* ) */
 
477
    XK_minus,             27,      /* - */
 
478
    XK_underscore,        27,      /* _ */
 
479
    XK_equal,             24,      /* = */
 
480
    XK_plus,              24,      /* + */
 
481
    XK_grave,             50,      /* ` */  /* XXX ? */
 
482
    XK_asciitilde,        50,      /* ~ */
 
483
    XK_bracketleft,       33,      /* [ */
 
484
    XK_braceleft,         33,      /* { */
 
485
    XK_bracketright,      30,      /* ] */
 
486
    XK_braceright,        30,      /* } */
 
487
    XK_semicolon,         41,      /* ; */
 
488
    XK_colon,             41,      /* : */
 
489
    XK_apostrophe,        39,      /* ' */
 
490
    XK_quotedbl,          39,      /* " */
 
491
    XK_comma,             43,      /* , */
 
492
    XK_less,              43,      /* < */
 
493
    XK_period,            47,      /* . */
 
494
    XK_greater,           47,      /* > */
 
495
    XK_slash,             44,      /* / */
 
496
    XK_question,          44,      /* ? */
 
497
    XK_backslash,         42,      /* \ */
 
498
    XK_bar,               42,      /* | */
 
499
    /* OS X Sends this (END OF MEDIUM) for Shift-Tab (with US Keyboard) */
 
500
    0x0019,               48,      /* Tab */
 
501
    XK_space,             49,      /* Space */
 
502
};
 
503
 
 
504
static int SpecialKeyCodes[] = {
 
505
    /* "Special" keys */
 
506
    XK_Return,            36,      /* Return */
 
507
    XK_Delete,           117,      /* Delete */
 
508
    XK_Tab,               48,      /* Tab */
 
509
    XK_Escape,            53,      /* Esc */
 
510
    XK_Caps_Lock,         57,      /* Caps Lock */
 
511
    XK_Num_Lock,          71,      /* Num Lock */
 
512
    XK_Scroll_Lock,      107,      /* Scroll Lock */
 
513
    XK_Pause,            113,      /* Pause */
 
514
    XK_BackSpace,         51,      /* Backspace */
 
515
    XK_Insert,           114,      /* Insert */
 
516
 
 
517
    /* Cursor movement */
 
518
    XK_Up,               126,      /* Cursor Up */
 
519
    XK_Down,             125,      /* Cursor Down */
 
520
    XK_Left,             123,      /* Cursor Left */
 
521
    XK_Right,            124,      /* Cursor Right */
 
522
    XK_Page_Up,          116,      /* Page Up */
 
523
    XK_Page_Down,        121,      /* Page Down */
 
524
    XK_Home,             115,      /* Home */
 
525
    XK_End,              119,      /* End */
 
526
 
 
527
    /* Numeric keypad */
 
528
    XK_KP_0,              82,      /* KP 0 */
 
529
    XK_KP_1,              83,      /* KP 1 */
 
530
    XK_KP_2,              84,      /* KP 2 */
 
531
    XK_KP_3,              85,      /* KP 3 */
 
532
    XK_KP_4,              86,      /* KP 4 */
 
533
    XK_KP_5,              87,      /* KP 5 */
 
534
    XK_KP_6,              88,      /* KP 6 */
 
535
    XK_KP_7,              89,      /* KP 7 */
 
536
    XK_KP_8,              91,      /* KP 8 */
 
537
    XK_KP_9,              92,      /* KP 9 */
 
538
    XK_KP_Enter,          76,      /* KP Enter */
 
539
    XK_KP_Decimal,        65,      /* KP . */
 
540
    XK_KP_Add,            69,      /* KP + */
 
541
    XK_KP_Subtract,       78,      /* KP - */
 
542
    XK_KP_Multiply,       67,      /* KP * */
 
543
    XK_KP_Divide,         75,      /* KP / */
 
544
 
 
545
    /* Function keys */
 
546
    XK_F1,               122,      /* F1 */
 
547
    XK_F2,               120,      /* F2 */
 
548
    XK_F3,                99,      /* F3 */
 
549
    XK_F4,               118,      /* F4 */
 
550
    XK_F5,                96,      /* F5 */
 
551
    XK_F6,                97,      /* F6 */
 
552
    XK_F7,                98,      /* F7 */
 
553
    XK_F8,               100,      /* F8 */
 
554
    XK_F9,               101,      /* F9 */
 
555
    XK_F10,              109,      /* F10 */
 
556
    XK_F11,              103,      /* F11 */
 
557
    XK_F12,              111,      /* F12 */
 
558
 
 
559
    /* Modifier keys */
 
560
    XK_Alt_L,             55,      /* Alt Left (-> Command) */
 
561
    XK_Alt_R,             55,      /* Alt Right (-> Command) */
 
562
    XK_Shift_L,           56,      /* Shift Left */
 
563
    XK_Shift_R,           56,      /* Shift Right */
 
564
    XK_Meta_L,            58,      /* Option Left (-> Option) */
 
565
    XK_Meta_R,            58,      /* Option Right (-> Option) */
 
566
    XK_Super_L,           58,      /* Option Left (-> Option) */
 
567
    XK_Super_R,           58,      /* Option Right (-> Option) */
 
568
    XK_Control_L,         59,      /* Ctrl Left */
 
569
    XK_Control_R,         59,      /* Ctrl Right */
 
570
};
 
571
 
 
572
CGKeyCode keyTable[keyTableSize];
 
573
unsigned char keyTableMods[keyTableSize];
 
574
 
 
575
void macosxCG_init_key_table(void) {
 
576
        static int init = 0;
 
577
        int i;
 
578
        if (init) {
 
579
                return;
 
580
        }
 
581
        init = 1;
 
582
 
 
583
        for (i=0; i < keyTableSize; i++) {
 
584
                keyTable[i] = 0xFFFF;
 
585
                keyTableMods[i] = 0;
 
586
        }
 
587
        for (i=0; i< (int) (sizeof(USKeyCodes) / sizeof(int)); i += 2) {
 
588
                int j = USKeyCodes[i];
 
589
                keyTable[(unsigned short) j] = (CGKeyCode) USKeyCodes[i+1];
 
590
        }
 
591
        for (i=0; i< (int) (sizeof(SpecialKeyCodes) / sizeof(int)); i += 2) {
 
592
                int j = SpecialKeyCodes[i];
 
593
                keyTable[(unsigned short) j] = (CGKeyCode) SpecialKeyCodes[i+1];
 
594
        }
 
595
}
 
596
 
 
597
extern void init_key_table(void);
 
598
 
 
599
void macosxCG_key_inject(int down, unsigned int keysym) {
 
600
        CGKeyCode keyCode = keyTable[(unsigned short)keysym];
 
601
        CGCharCode keyChar = 0;
 
602
#if 0
 
603
        int pressModsForKeys = FALSE;
 
604
        UInt32 modsForKey = keyTableMods[keysym] << 8;
 
605
#endif
 
606
 
 
607
        init_key_table();
 
608
 
 
609
        if (keysym < 0xFF) {
 
610
                keyChar = (CGCharCode) keysym;
 
611
        }
 
612
        if (keyCode == 0xFFFF) {
 
613
                return;
 
614
        }
 
615
        macosxCGP_undim();
 
616
        CGPostKeyboardEvent(keyChar, keyCode, down);
 
617
}
 
618
 
 
619
#endif  /* __APPLE__ */
 
620
 
 
621