aboutsummaryrefslogtreecommitdiff
path: root/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkEvents.c
blob: cdcd72e187cbc08e2ff29fb7b358d09169942f9e (plain)
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
/* gtkevents.c -- GDK/GTK event handlers
   Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


#include "gtkpeer.h"
#include <X11/Xlib.h>
#include <gdk/gdkkeysyms.h>
#include <stdarg.h>
#include <assert.h>

/* A widget can be composed of multipled windows, so we need to hook
   events on all of them. */
struct event_hook_info
{
  jobject *peer_obj;
  int nwindows;
  /* array of pointers to (GdkWindow *) */
  GdkWindow ***windows;
};

static jint
button_to_awt_mods (int button)
{
  switch (button)
    {
    case 1:
      return AWT_BUTTON1_MASK;
    case 2:
      return AWT_BUTTON2_MASK;
    case 3:
      return AWT_BUTTON3_MASK;
    }

  return 0;
}

static jint
state_to_awt_mods (guint state)
{
  jint result = 0;

  if (state & GDK_SHIFT_MASK)
    result |= AWT_SHIFT_MASK;
  if (state & GDK_CONTROL_MASK)
    result |= AWT_CTRL_MASK;
  if (state & GDK_MOD1_MASK)
    result |= AWT_ALT_MASK;

  return result;
}

/* Modifier key events need special treatment.  In Sun's peer
   implementation, when a modifier key is pressed, the KEY_PRESSED
   event has that modifier in its modifiers list.  The corresponding
   KEY_RELEASED event's modifier list does not contain the modifier.
   For example, pressing and releasing the shift key will produce a
   key press event with modifiers=Shift, and a key release event with
   no modifiers.  GDK's key events behave in the exact opposite way,
   so this translation code is needed. */
static jint
keyevent_state_to_awt_mods (GdkEvent *event)
{
  jint result = 0;
  guint state;

  if (event->type == GDK_KEY_PRESS)
    {
      state = event->key.state;

      if (event->key.keyval == GDK_Shift_L
          || event->key.keyval == GDK_Shift_R)
        result |= AWT_SHIFT_MASK;
      else
        {
          if (state & GDK_SHIFT_MASK)
            result |= AWT_SHIFT_MASK;
        }

      if (event->key.keyval == GDK_Control_L
          || event->key.keyval == GDK_Control_R)
        result |= AWT_CTRL_MASK;
      else
        {
          if (state & GDK_CONTROL_MASK)
            result |= AWT_CTRL_MASK;
        }

      if (event->key.keyval == GDK_Alt_L
          || event->key.keyval == GDK_Alt_R)
        result |= AWT_ALT_MASK;
      else
        {
          if (state & GDK_MOD1_MASK)
            result |= AWT_ALT_MASK;
        }
    }
  else if (event->type == GDK_KEY_RELEASE)
    {
      state = event->key.state;

      if (event->key.keyval != GDK_Shift_L
          && event->key.keyval != GDK_Shift_R)
        {
          if (state & GDK_SHIFT_MASK)
            result |= AWT_SHIFT_MASK;
        }
      if (event->key.keyval != GDK_Control_L
          && event->key.keyval != GDK_Control_R)
        {
          if (state & GDK_CONTROL_MASK)
            result |= AWT_CTRL_MASK;
        }

      if (event->key.keyval != GDK_Alt_L
          && event->key.keyval != GDK_Alt_R)
        {
          if (state & GDK_MOD1_MASK)
            result |= AWT_ALT_MASK;
        }
    }

  return result;
}

/* Get the first keyval in the keymap for this event's keycode.  The
   first keyval corresponds roughly to Java's notion of a virtual
   key.  Returns the uppercase version of the first keyval. */
static guint
get_first_keyval_from_keymap (GdkEvent *event)
{
  guint keyval;
  guint *keyvals;
  gint n_entries;

  if (!gdk_keymap_get_entries_for_keycode (NULL,
                                           event->key.hardware_keycode,
                                           NULL,
                                           &keyvals,
                                           &n_entries))
    {
      g_warning ("No keyval found for hardware keycode %d\n",
                 event->key.hardware_keycode);
      /* Try to recover by using the keyval in the event structure. */
      keyvals = &(event->key.keyval);
    }
  keyval = keyvals[0];
  g_free (keyvals);

  return gdk_keyval_to_upper (keyval);
}

#ifdef __GNUC__
__inline
#endif
static jint
keysym_to_awt_keycode (GdkEvent *event)
{
  guint ukeyval;
  guint state;

  ukeyval = get_first_keyval_from_keymap (event);
  state = event->key.state;

  /* VK_A through VK_Z */
  if (ukeyval >= GDK_A && ukeyval <= GDK_Z)
    return ukeyval;

  /* VK_0 through VK_9 */
  if (ukeyval >= GDK_0 && ukeyval <= GDK_9)
    return ukeyval;

  switch (ukeyval)
    {
    case GDK_Return:
    case GDK_KP_Enter:
      return VK_ENTER;
    case GDK_BackSpace:
      return VK_BACK_SPACE;
    case GDK_Tab:
      return VK_TAB;
    case GDK_Cancel:
      return VK_CANCEL;
    case GDK_Clear:
      return VK_CLEAR;
    case GDK_Shift_L:
    case GDK_Shift_R:
      return VK_SHIFT;
    case GDK_Control_L:
    case GDK_Control_R:
      return VK_CONTROL;
    case GDK_Alt_L:
    case GDK_Alt_R:
      return VK_ALT;
    case GDK_Pause:
      return VK_PAUSE;
    case GDK_Caps_Lock:
      return VK_CAPS_LOCK;
    case GDK_Escape:
      return VK_ESCAPE;
    case GDK_space:
      return VK_SPACE;
    case GDK_KP_Page_Up:
      /* For keys on the numeric keypad, the JVM produces one of two
         virtual keys, depending on the num lock state. */
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD9;
      else
        return VK_PAGE_UP;
    case GDK_Page_Up:
      return VK_PAGE_UP;
    case GDK_KP_Page_Down:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD3;
      else
        return VK_PAGE_DOWN;
    case GDK_Page_Down:
      return VK_PAGE_DOWN;
    case GDK_KP_End:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD1;
      else
        return VK_END;
    case GDK_End:
      return VK_END;
    case GDK_KP_Home:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD7;
      else
        return VK_HOME;
    case GDK_Home:
      return VK_HOME;
    case GDK_KP_Begin:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD5;
      else
        return VK_UNDEFINED;
    case GDK_Left:
      return VK_LEFT;
    case GDK_Up:
      return VK_UP;
    case GDK_Right:
      return VK_RIGHT;
    case GDK_Down:
      return VK_DOWN;
    case GDK_comma:
      return VK_COMMA;
    case GDK_minus:
      return VK_MINUS;
    case GDK_period:
      return VK_PERIOD;
    case GDK_slash:
      return VK_SLASH;
      /*
      return VK_0;
      return VK_1;
      return VK_2;
      return VK_3;
      return VK_4;
      return VK_5;
      return VK_6;
      return VK_7;
      return VK_8;
      return VK_9;
      */
    case GDK_semicolon:
      return VK_SEMICOLON;
    case GDK_equal:
      return VK_EQUALS;
      /*
      return VK_A;
      return VK_B;
      return VK_C;
      return VK_D;
      return VK_E;
      return VK_F;
      return VK_G;
      return VK_H;
      return VK_I;
      return VK_J;
      return VK_K;
      return VK_L;
      return VK_M;
      return VK_N;
      return VK_O;
      return VK_P;
      return VK_Q;
      return VK_R;
      return VK_S;
      return VK_T;
      return VK_U;
      return VK_V;
      return VK_W;
      return VK_X;
      return VK_Y;
      return VK_Z;
      */
    case GDK_bracketleft:
      return VK_OPEN_BRACKET;
    case GDK_backslash:
      return VK_BACK_SLASH;
    case GDK_bracketright:
      return VK_CLOSE_BRACKET;
    case GDK_KP_0:
      return VK_NUMPAD0;
    case GDK_KP_1:
      return VK_NUMPAD1;
    case GDK_KP_2:
      return VK_NUMPAD2;
    case GDK_KP_3:
      return VK_NUMPAD3;
    case GDK_KP_4:
      return VK_NUMPAD4;
    case GDK_KP_5:
      return VK_NUMPAD5;
    case GDK_KP_6:
      return VK_NUMPAD6;
    case GDK_KP_7:
      return VK_NUMPAD7;
    case GDK_KP_8:
      return VK_NUMPAD8;
    case GDK_KP_9:
      return VK_NUMPAD9;
    case GDK_KP_Multiply:
      return VK_MULTIPLY;
    case GDK_KP_Add:
      return VK_ADD;
      /*
      return VK_SEPARATER;
      */
    case GDK_KP_Separator:
      return VK_SEPARATOR;
    case GDK_KP_Subtract:
      return VK_SUBTRACT;
    case GDK_KP_Decimal:
      return VK_DECIMAL;
    case GDK_KP_Divide:
      return VK_DIVIDE;
    case GDK_KP_Delete:
      if (state & GDK_MOD2_MASK)
        return VK_DECIMAL;
      else
        return VK_DELETE;
    case GDK_Delete:
      return VK_DELETE;
    case GDK_Num_Lock:
      return VK_NUM_LOCK;
    case GDK_Scroll_Lock:
      return VK_SCROLL_LOCK;
    case GDK_F1:
      return VK_F1;
    case GDK_F2:
      return VK_F2;
    case GDK_F3:
      return VK_F3;
    case GDK_F4:
      return VK_F4;
    case GDK_F5:
      return VK_F5;
    case GDK_F6:
      return VK_F6;
    case GDK_F7:
      return VK_F7;
    case GDK_F8:
      return VK_F8;
    case GDK_F9:
      return VK_F9;
    case GDK_F10:
      return VK_F10;
    case GDK_F11:
      return VK_F11;
    case GDK_F12:
      return VK_F12;
    case GDK_F13:
      return VK_F13;
    case GDK_F14:
      return VK_F14;
    case GDK_F15:
      return VK_F15;
    case GDK_F16:
      return VK_F16;
    case GDK_F17:
      return VK_F17;
    case GDK_F18:
      return VK_F18;
    case GDK_F19:
      return VK_F19;
    case GDK_F20:
      return VK_F20;
    case GDK_F21:
      return VK_F21;
    case GDK_F22:
      return VK_F22;
    case GDK_F23:
      return VK_F23;
    case GDK_F24:
      return VK_F24;
    case GDK_Print:
      return VK_PRINTSCREEN;
    case GDK_KP_Insert:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD0;
      else
        return VK_INSERT;
    case GDK_Insert:
      return VK_INSERT;
    case GDK_Help:
      return VK_HELP;
    case GDK_Meta_L:
    case GDK_Meta_R:
      return VK_META;
    case GDK_grave:
      return VK_BACK_QUOTE;
    case GDK_apostrophe:
      return VK_QUOTE;
    case GDK_KP_Up:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD8;
      else
        return VK_KP_UP;
    case GDK_KP_Down:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD2;
      else
        return VK_KP_DOWN;
    case GDK_KP_Left:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD4;
      else
        return VK_KP_LEFT;
    case GDK_KP_Right:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD6;
      else
        return VK_KP_RIGHT;
    case GDK_dead_grave:
      return VK_DEAD_GRAVE;
    case GDK_dead_acute:
      return VK_DEAD_ACUTE;
    case GDK_dead_circumflex:
      return VK_DEAD_CIRCUMFLEX;
    case GDK_dead_tilde:
      return VK_DEAD_TILDE;
    case GDK_dead_macron:
      return VK_DEAD_MACRON;
    case GDK_dead_breve:
      return VK_DEAD_BREVE;
    case GDK_dead_abovedot:
      return VK_DEAD_ABOVEDOT;
    case GDK_dead_diaeresis:
      return VK_DEAD_DIAERESIS;
    case GDK_dead_abovering:
      return VK_DEAD_ABOVERING;
    case GDK_dead_doubleacute:
      return VK_DEAD_DOUBLEACUTE;
    case GDK_dead_caron:
      return VK_DEAD_CARON;
    case GDK_dead_cedilla:
      return VK_DEAD_CEDILLA;
    case GDK_dead_ogonek:
      return VK_DEAD_OGONEK;
    case GDK_dead_iota:
      return VK_DEAD_IOTA;
    case GDK_dead_voiced_sound:
      return VK_DEAD_VOICED_SOUND;
    case GDK_dead_semivoiced_sound:
      return VK_DEAD_SEMIVOICED_SOUND;
    case GDK_ampersand:
      return VK_AMPERSAND;
    case GDK_asterisk:
      return VK_ASTERISK;
    case GDK_quotedbl:
      return VK_QUOTEDBL;
    case GDK_less:
      return VK_LESS;
    case GDK_greater:
      return VK_GREATER;
    case GDK_braceleft:
      return VK_BRACELEFT;
    case GDK_braceright:
      return VK_BRACERIGHT;
    case GDK_at:
      return VK_AT;
    case GDK_colon:
      return VK_COLON;
    case GDK_asciicircum:
      return VK_CIRCUMFLEX;
    case GDK_dollar:
      return VK_DOLLAR;
    case GDK_EuroSign:
      return VK_EURO_SIGN;
    case GDK_exclam:
      return VK_EXCLAMATION_MARK;
    case GDK_exclamdown:
      return VK_INVERTED_EXCLAMATION_MARK;
    case GDK_parenleft:
      return VK_LEFT_PARENTHESIS;
    case GDK_numbersign:
      return VK_NUMBER_SIGN;
    case GDK_plus:
      return VK_PLUS;
    case GDK_parenright:
      return VK_RIGHT_PARENTHESIS;
    case GDK_underscore:
      return VK_UNDERSCORE;
      /*
      return VK_FINAL;
      return VK_CONVERT;
      return VK_NONCONVERT;
      return VK_ACCEPT;
      */
    case GDK_Mode_switch:
      return VK_MODECHANGE;
      /*
      return VK_KANA;
      */
    case GDK_Kanji:
      return VK_KANJI;
      /*
      return VK_ALPHANUMERIC;
      */
    case GDK_Katakana:
      return VK_KATAKANA;
    case GDK_Hiragana:
      return VK_HIRAGANA;
      /*
      return VK_FULL_WIDTH;
      return VK_HALF_WIDTH;
      return VK_ROMAN_CHARACTERS;
      return VK_ALL_CANDIDATES;
      */
    case GDK_PreviousCandidate:
      return VK_PREVIOUS_CANDIDATE;
    case GDK_Codeinput:
      return VK_CODE_INPUT;
      /*
      return VK_JAPANESE_KATAKANA;
      return VK_JAPANESE_HIRAGANA;
      return VK_JAPANESE_ROMAN;
      */
    case GDK_Kana_Lock:
      return VK_KANA_LOCK;
      /*
      return VK_INPUT_METHOD_ON_OFF;
      return VK_CUT;
      return VK_COPY;
      return VK_PASTE;
      return VK_UNDO;
      return VK_AGAIN;
      return VK_FIND;
      return VK_PROPS;
      return VK_STOP;
      return VK_COMPOSE;
      return VK_ALT_GRAPH;
      */
    default:
      return VK_UNDEFINED;
    }
}

static jint
keysym_to_awt_keylocation (GdkEvent *event)
{
  guint ukeyval;

  ukeyval = get_first_keyval_from_keymap (event);

  /* VK_A through VK_Z */
  if (ukeyval >= GDK_A && ukeyval <= GDK_Z)
    return AWT_KEY_LOCATION_STANDARD;

  /* VK_0 through VK_9 */
  if (ukeyval >= GDK_0 && ukeyval <= GDK_9)
    return AWT_KEY_LOCATION_STANDARD;

  switch (ukeyval)
    {
    case GDK_Shift_L:
    case GDK_Control_L:
    case GDK_Alt_L:
    case GDK_Meta_L:
      return AWT_KEY_LOCATION_LEFT;

    case GDK_Shift_R:
    case GDK_Control_R:
    case GDK_Alt_R:
    case GDK_Meta_R:
      return AWT_KEY_LOCATION_RIGHT;

    case GDK_Return:
    case GDK_BackSpace:
    case GDK_Tab:
    case GDK_Cancel:
    case GDK_Clear:
    case GDK_Pause:
    case GDK_Caps_Lock:
    case GDK_Escape:
    case GDK_space:
    case GDK_Page_Up:
    case GDK_Page_Down:
    case GDK_End:
    case GDK_Home:
    case GDK_Left:
    case GDK_Up:
    case GDK_Right:
    case GDK_Down:
    case GDK_comma:
    case GDK_minus:
    case GDK_period:
    case GDK_slash:
    case GDK_semicolon:
    case GDK_equal:
    case GDK_bracketleft:
    case GDK_backslash:
    case GDK_bracketright:
    case GDK_Delete:
    case GDK_Scroll_Lock:
    case GDK_F1:
    case GDK_F2:
    case GDK_F3:
    case GDK_F4:
    case GDK_F5:
    case GDK_F6:
    case GDK_F7:
    case GDK_F8:
    case GDK_F9:
    case GDK_F10:
    case GDK_F11:
    case GDK_F12:
    case GDK_F13:
    case GDK_F14:
    case GDK_F15:
    case GDK_F16:
    case GDK_F17:
    case GDK_F18:
    case GDK_F19:
    case GDK_F20:
    case GDK_F21:
    case GDK_F22:
    case GDK_F23:
    case GDK_F24:
    case GDK_Print:
    case GDK_Insert:
    case GDK_Help:
    case GDK_grave:
    case GDK_apostrophe:
    case GDK_dead_grave:
    case GDK_dead_acute:
    case GDK_dead_circumflex:
    case GDK_dead_tilde:
    case GDK_dead_macron:
    case GDK_dead_breve:
    case GDK_dead_abovedot:
    case GDK_dead_diaeresis:
    case GDK_dead_abovering:
    case GDK_dead_doubleacute:
    case GDK_dead_caron:
    case GDK_dead_cedilla:
    case GDK_dead_ogonek:
    case GDK_dead_iota:
    case GDK_dead_voiced_sound:
    case GDK_dead_semivoiced_sound:
    case GDK_ampersand:
    case GDK_asterisk:
    case GDK_quotedbl:
    case GDK_less:
    case GDK_greater:
    case GDK_braceleft:
    case GDK_braceright:
    case GDK_at:
    case GDK_colon:
    case GDK_asciicircum:
    case GDK_dollar:
    case GDK_EuroSign:
    case GDK_exclam:
    case GDK_exclamdown:
    case GDK_parenleft:
    case GDK_numbersign:
    case GDK_plus:
    case GDK_parenright:
    case GDK_underscore:
    case GDK_Mode_switch:
    case GDK_Kanji:
    case GDK_Katakana:
    case GDK_Hiragana:
    case GDK_PreviousCandidate:
    case GDK_Codeinput:
    case GDK_Kana_Lock:
      return AWT_KEY_LOCATION_STANDARD;

    case GDK_KP_Enter:
    case GDK_KP_Page_Up:
    case GDK_KP_Page_Down:
    case GDK_KP_End:
    case GDK_KP_Home:
    case GDK_KP_Begin:
    case GDK_KP_0:
    case GDK_KP_1:
    case GDK_KP_2:
    case GDK_KP_3:
    case GDK_KP_4:
    case GDK_KP_5:
    case GDK_KP_6:
    case GDK_KP_7:
    case GDK_KP_8:
    case GDK_KP_9:
    case GDK_KP_Multiply:
    case GDK_KP_Add:
    case GDK_KP_Separator:
    case GDK_KP_Subtract:
    case GDK_KP_Decimal:
    case GDK_KP_Divide:
    case GDK_KP_Delete:
    case GDK_Num_Lock:
    case GDK_KP_Insert:
    case GDK_KP_Up:
    case GDK_KP_Down:
    case GDK_KP_Left:
    case GDK_KP_Right:
      return AWT_KEY_LOCATION_NUMPAD;

    default:
      return AWT_KEY_LOCATION_UNKNOWN;
    }
}

static jchar
keyevent_to_awt_keychar (GdkEvent *event)
{
  if (event->key.length > 0)
    {
      /* Translate GDK carriage return to Java linefeed. */
      if (event->key.string[0] == 13)
        return VK_ENTER;
      else
        return event->key.string[0];
    }
  else
    {
      switch (event->key.keyval)
        {
        case GDK_BackSpace:
          return VK_BACK_SPACE;
        case GDK_Tab:
          return VK_TAB;
        case GDK_Delete:
        case GDK_KP_Delete:
          return VK_DELETE;
        default:
          return AWT_KEY_CHAR_UNDEFINED;
        }
    }
}

/* Checks if keyval triggers a KEY_TYPED event on the source widget.
   This function identifies special keyvals that don't trigger
   GtkIMContext "commit" signals, but that do trigger Java KEY_TYPED
   events. */
static int
generates_key_typed_event (GdkEvent *event, GtkWidget *source)
{
  guint keyval;

  if (!GTK_IS_ENTRY (source)
      && !GTK_IS_TEXT_VIEW (source))
    return event->key.length ? 1 : 0;

  keyval = event->key.keyval;

  return (keyval == GDK_Escape
          || keyval == GDK_BackSpace
          || keyval == GDK_Delete
          || keyval == GDK_KP_Delete
          || keyval == GDK_Return
          || keyval == GDK_KP_Enter
          || (keyval == GDK_Tab
              && GTK_IS_TEXT_VIEW(source))) ? 1 : 0;
}

void
awt_event_handler (GdkEvent *event)
{
  jobject *obj_ptr;
  static guint32 button_click_time = 0;
  static GdkWindow *button_window = NULL;
  static guint button_number = -1;
  static jint click_count = 1;

  /* keep synthetic AWT events from being processed recursively */
  if (event->type & SYNTHETIC_EVENT_MASK && event->type != GDK_NOTHING)
    {
      event->type ^= SYNTHETIC_EVENT_MASK;
      gtk_main_do_event (event);
      return;
    }

  /* keep track of clickCount ourselves, since the AWT allows more
     than a triple click to occur */
  if (event->type == GDK_BUTTON_PRESS)
    {
      if ((event->button.time < (button_click_time + MULTI_CLICK_TIME))
	  && (event->button.window == button_window)
	  && (event->button.button == button_number))
	click_count++;
      else
	click_count = 1;
      
      button_click_time = event->button.time;
      button_window = event->button.window;
      button_number = event->button.button;
    }

  /* for all input events, which have a window with a jobject attached,
     send the input event off to Java before GTK has a chance to process
     the event */
  if ((event->type == GDK_BUTTON_PRESS
       || event->type == GDK_BUTTON_RELEASE
       || event->type == GDK_ENTER_NOTIFY
       || event->type == GDK_LEAVE_NOTIFY
       || event->type == GDK_CONFIGURE
       || event->type == GDK_EXPOSE
       || event->type == GDK_KEY_PRESS
       || event->type == GDK_KEY_RELEASE
       || event->type == GDK_FOCUS_CHANGE
       || event->type == GDK_MOTION_NOTIFY)
      && gdk_property_get (event->any.window,
			   gdk_atom_intern ("_GNU_GTKAWT_ADDR", FALSE),
			   gdk_atom_intern ("CARDINAL", FALSE),
			   0,
			   sizeof (jobject),
			   FALSE,
			   NULL,
			   NULL,
			   NULL,
			   (guchar **)&obj_ptr))
    {
      switch (event->type)
	{
	case GDK_BUTTON_PRESS:
	  (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postMouseEventID,
				      AWT_MOUSE_PRESSED, 
				      (jlong)event->button.time,
				    state_to_awt_mods (event->button.state) |
				    button_to_awt_mods (event->button.button), 
				      (jint)event->button.x,
				      (jint)event->button.y, 
				      click_count, 
				      (event->button.button == 3) ? JNI_TRUE :
				                                    JNI_FALSE);

	  /*	  grab_counter++;
	  gdk_pointer_grab (event->any.window,
			    FALSE,
			    GDK_POINTER_MOTION_MASK |
			    GDK_BUTTON_MOTION_MASK |
			    GDK_BUTTON_PRESS_MASK |
			    GDK_BUTTON_RELEASE_MASK |
			    GDK_ENTER_NOTIFY_MASK |
			    GDK_LEAVE_NOTIFY_MASK,
			    NULL,
			    NULL,
			    event->button.time);*/
	  break;
	case GDK_BUTTON_RELEASE:
	  {
	    int width, height;

	    /* only ungrab if no other buttons are pressed down */
	    /*	    if (--grab_counter == 0)
	      gdk_pointer_ungrab (event->button.time);
	    */
	    (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postMouseEventID,
					AWT_MOUSE_RELEASED, 
					(jlong)event->button.time,
				    state_to_awt_mods (event->button.state) |
				    button_to_awt_mods (event->button.button), 
					(jint)event->button.x,
					(jint)event->button.y, 
					click_count, JNI_FALSE);

	    /* check to see if the release occured in the window it was pressed
	       in, and if so, generate an AWT click event */
	    gdk_window_get_size (event->any.window, &width, &height);
	    if (event->button.x >= 0
		&& event->button.y >= 0
		&& event->button.x <= width 
		&& event->button.y <= height)
	      (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postMouseEventID,
					  AWT_MOUSE_CLICKED, 
					  (jlong)event->button.time,
				   state_to_awt_mods (event->button.state) |
				  button_to_awt_mods (event->button.button), 
					  (jint)event->button.x,
					  (jint)event->button.y, 
					  click_count, JNI_FALSE);
	    
	  }
	  break;
	case GDK_MOTION_NOTIFY:
	  (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postMouseEventID,
				      AWT_MOUSE_MOVED,
				      (jlong)event->motion.time,
				      state_to_awt_mods (event->motion.state),
				      (jint)event->motion.x,
				      (jint)event->motion.y,
				      0, JNI_FALSE);

	  if (event->motion.state & (GDK_BUTTON1_MASK
				     | GDK_BUTTON2_MASK
				     | GDK_BUTTON3_MASK
				     | GDK_BUTTON4_MASK
				     | GDK_BUTTON5_MASK))
	    {
	      (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postMouseEventID,
					  AWT_MOUSE_DRAGGED,
					  (jlong)event->motion.time,
				      state_to_awt_mods (event->motion.state),
					  (jint)event->motion.x,
					  (jint)event->motion.y,
					  0, JNI_FALSE);
	    }
	  break;
	case GDK_ENTER_NOTIFY:
	  (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postMouseEventID,
				      AWT_MOUSE_ENTERED, 
				      (jlong)event->crossing.time,
				    state_to_awt_mods (event->crossing.state), 
				      (jint)event->crossing.x,
				      (jint)event->crossing.y, 
				      0, JNI_FALSE);
	  break;
	case GDK_LEAVE_NOTIFY:
	  if (event->crossing.mode == GDK_CROSSING_NORMAL)
	    (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, postMouseEventID,
					AWT_MOUSE_EXITED, 
					(jlong)event->crossing.time,
				    state_to_awt_mods (event->crossing.state),
					(jint)event->crossing.x,
					(jint)event->crossing.y, 
					0, JNI_FALSE);
	  break;
	case GDK_CONFIGURE:
	  {
	    GtkWidget *widget;

	    gdk_window_get_user_data (event->any.window, (void **) &widget);
	    
	    if (widget && GTK_WIDGET_TOPLEVEL (widget))
	      {
		gint top, left, right, bottom;
		gint x, y, w, h, wb, d;

		/* calculate our insets */
		gdk_window_get_root_geometry (event->any.window, 
					      &x, &y, &w, &h, &wb, &d);

		/* We used to compute these based on the configure
		   event's fields.  However, that gives strange and
		   apparently incorrect results.  */
		top = left = bottom = right = 0;

		/* configure events are not posted to the AWT event queue,
		   and as such, gdk/gtk will be called back before
		   postConfigureEvent returns */
		gdk_threads_leave ();
		(*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr, 
					    postConfigureEventID,
					    (jint)event->configure.x,
					    (jint)event->configure.y,
					    (jint)event->configure.width,
					    (jint)event->configure.height,
					    (jint)top,
					    (jint)left,
					    (jint)bottom,
					    (jint)right);
		gdk_threads_enter ();
	      }
	  }
	  break;
	case GDK_EXPOSE:
	  {
	    (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr,
					postExposeEventID,
					(jint)event->expose.area.x,
					(jint)event->expose.area.y,
					(jint)event->expose.area.width,
					(jint)event->expose.area.height);
	  }
	  break;

	case GDK_KEY_PRESS:
	  {
	    GtkWidget *widget;
	    GtkWindow *window;
            /* The window to which the Java peer is attached. */
            GdkWindow *obj_window;

	    gdk_window_get_user_data (event->any.window, (void **) &widget);

	    window = GTK_WINDOW (gtk_widget_get_ancestor (widget, 
							  GTK_TYPE_WINDOW));
	    if (window
		&& GTK_WIDGET_IS_SENSITIVE (window)
		&& window->focus_widget
		&& GTK_WIDGET_IS_SENSITIVE (window->focus_widget)
		&& window->focus_widget->window)
	      {
		gtk_widget_activate (window->focus_widget);

                /* TextArea peers are attached to the scrolled window
                   that contains the GtkTextView, not to the text view
                   itself. */
                if (GTK_IS_TEXT_VIEW (window->focus_widget))
                  obj_window = gtk_widget_get_parent (window->focus_widget)->window;
                else
                  obj_window = window->focus_widget->window;

		gdk_property_get (obj_window,
				  gdk_atom_intern ("_GNU_GTKAWT_ADDR", FALSE),
				  gdk_atom_intern ("CARDINAL", FALSE),
				  0,
				  sizeof (jobject),
				  FALSE,
				  NULL,
				  NULL,
				  NULL,
				  (guchar **)&obj_ptr);

		(*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr,
					    postKeyEventID,
					    (jint) AWT_KEY_PRESSED,
					    (jlong) event->key.time,
                             keyevent_state_to_awt_mods (event),
                             keysym_to_awt_keycode (event),
                             keyevent_to_awt_keychar (event),
                             keysym_to_awt_keylocation (event));

                if (generates_key_typed_event (event, window->focus_widget))
                  (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr,
                                              postKeyEventID,
                                              (jint) AWT_KEY_TYPED,
                                              (jlong) event->key.time,
                                              state_to_awt_mods (event->key.state),
                                              VK_UNDEFINED,
                                              keyevent_to_awt_keychar (event),
                                              AWT_KEY_LOCATION_UNKNOWN);
	      }
	  }
	  break;
        case GDK_KEY_RELEASE:
	  {
	    GtkWidget *widget;
	    GtkWindow *window;
            GdkWindow *obj_window;

	    gdk_window_get_user_data (event->any.window, (void **) &widget);

	    window = GTK_WINDOW (gtk_widget_get_ancestor (widget, 
							  GTK_TYPE_WINDOW));
	    if (window
		&& GTK_WIDGET_IS_SENSITIVE (window) 
		&& window->focus_widget
		&& GTK_WIDGET_IS_SENSITIVE (window->focus_widget)
		&& window->focus_widget->window)
	      {
		gtk_widget_activate (window->focus_widget);

                if (GTK_IS_TEXT_VIEW (window->focus_widget))
                  obj_window = gtk_widget_get_parent (window->focus_widget)->window;
                else
                  obj_window = window->focus_widget->window;

		gdk_property_get (obj_window,
				  gdk_atom_intern ("_GNU_GTKAWT_ADDR", FALSE),
				  gdk_atom_intern ("CARDINAL", FALSE),
				  0,
				  sizeof (jobject),
				  FALSE,
				  NULL,
				  NULL,
				  NULL,
				  (guchar **)&obj_ptr);

		(*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr,
					    postKeyEventID,
					    (jint) AWT_KEY_RELEASED,
					    (jlong) event->key.time,
			     keyevent_state_to_awt_mods (event),
			     keysym_to_awt_keycode (event),
                             keyevent_to_awt_keychar (event),
                             keysym_to_awt_keylocation (event));
              }
          }
          break;
	case GDK_FOCUS_CHANGE:
	  (*gdk_env)->CallVoidMethod (gdk_env, *obj_ptr,
				      postFocusEventID,
				      (jint) (event->focus_change.in) ? 
				      AWT_FOCUS_GAINED : AWT_FOCUS_LOST,
				      JNI_FALSE);
	  break;
        default:
	}
      g_free (obj_ptr);
    }

  gtk_main_do_event (event);
}

static void
attach_jobject (GdkWindow *window, jobject *obj)
{
  GdkAtom addr_atom = gdk_atom_intern ("_GNU_GTKAWT_ADDR", FALSE);
  GdkAtom type_atom = gdk_atom_intern ("CARDINAL", FALSE);

  gdk_window_set_events (window, 
			 gdk_window_get_events (window)
			 | GDK_POINTER_MOTION_MASK
			 | GDK_BUTTON_MOTION_MASK
			 | GDK_BUTTON_PRESS_MASK
			 | GDK_BUTTON_RELEASE_MASK
			 | GDK_KEY_PRESS_MASK
			 | GDK_KEY_RELEASE_MASK
			 | GDK_ENTER_NOTIFY_MASK
			 | GDK_LEAVE_NOTIFY_MASK
			 | GDK_STRUCTURE_MASK
			 | GDK_KEY_PRESS_MASK
			 | GDK_FOCUS_CHANGE_MASK);

  //  g_print("storing obj %p property on window %p\n", obj, window);
  gdk_property_change (window,
		       addr_atom,
		       type_atom,
		       8,
		       GDK_PROP_MODE_REPLACE,
		       (guchar *)obj,
		       sizeof (jobject));
}

void
connect_awt_hook (JNIEnv *env, jobject peer_obj, int nwindows, ...)
{
  va_list ap;
  jobject *obj;

  obj = (jobject *) malloc (sizeof (jobject));
  *obj = (*env)->NewGlobalRef (env, peer_obj);
  //g_print("Connection obj %p\n", peer_obj);

  va_start (ap, nwindows);
  {
  int i;
  for (i = 0; i < nwindows; i++)
    {
      GdkWindow* attach = (va_arg (ap, GdkWindow *));
      //g_print("attach peer obj %p and %p\n", peer_obj, attach);
      attach_jobject(attach, obj);
    }
  }
  va_end (ap);
}