summaryrefslogtreecommitdiffstats
path: root/tigervnc/build/patches/fltk-1_v6.3.x-keyboard-osx.patch
blob: cf13aad74becb3471292666c116109a85b7de7fd (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
diff -ur fltk-1.3.0r9619.org/configure.in fltk-1.3.0r9619/configure.in
--- fltk-1.3.0r9619.org/configure.in	2012-04-22 04:45:09.000000000 +0200
+++ fltk-1.3.0r9619/configure.in	2012-06-18 13:47:33.290447462 +0200
@@ -865,6 +865,8 @@
     Darwin*)
         # MacOS X uses Cocoa for graphics.
         LIBS="$LIBS -framework Cocoa"
+        # And some Carbon for keyboard handling
+        LIBS="$LIBS -framework Carbon"
 
 	if test x$have_pthread = xyes; then
 	    AC_DEFINE(HAVE_PTHREAD)
diff -ur fltk-1.3.0r9619.org/src/Fl_cocoa.mm fltk-1.3.0r9619/src/Fl_cocoa.mm
--- fltk-1.3.0r9619.org/src/Fl_cocoa.mm	2012-06-16 10:49:52.000000000 +0200
+++ fltk-1.3.0r9619/src/Fl_cocoa.mm	2012-06-18 13:47:42.944910782 +0200
@@ -53,6 +53,7 @@
 #include <math.h>
 
 #import <Cocoa/Cocoa.h>
+#import <Carbon/Carbon.h>
 
 #ifndef NSINTEGER_DEFINED // appears with 10.5 in NSObjCRuntime.h
 #if defined(__LP64__) && __LP64__
@@ -114,6 +115,8 @@
 extern Fl_Window* fl_xmousewin;
 #endif
 
+bool use_simple_keyboard = false;
+
 enum { FLTKTimerEvent = 1, FLTKDataReadyEvent };
 
 
@@ -130,6 +133,39 @@
 {
 }
 
+// Undocumented voodoo. Taken from Mozilla.
+#define ENABLE_ROMAN_KYBDS_ONLY -23
+
+void fl_update_focus(void)
+{
+  Fl_Widget *focus;
+
+  focus = Fl::grab();
+  if (!focus)
+    focus = Fl::focus();
+  if (!focus)
+    return;
+
+  if (focus->simple_keyboard())
+    use_simple_keyboard = true;
+  else
+    use_simple_keyboard = false;
+
+  // Force a "Roman" or "ASCII" keyboard, which both the Mozilla and
+  // Safari people seem to think implies turning off advanced IME stuff
+  // (see nsTSMManager::SyncKeyScript in Mozilla and enableSecureTextInput
+  // in Safari/Webcore). Should be good enough for us then...
+#if (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
+  CFArrayRef inputSources = TISCreateASCIICapableInputSourceList();
+  TSMSetDocumentProperty(TSMGetActiveDocument(),
+                         kTSMDocumentEnabledInputSourcesPropertyTag,
+                         sizeof(CFArrayRef), &inputSources);
+  CFRelease(inputSources);
+#else
+  KeyScript(use_simple_keyboard ? ENABLE_ROMAN_KYBDS_ONLY : smKeyEnableKybds);
+#endif  
+}
+
 /*
  * Mac keyboard lookup table
  */
@@ -908,6 +944,25 @@
 }
 @end
 
+static const char* cocoaDead2FLTK(const char *in)
+{
+  if (strcmp(in, "\140") == 0)      // GRAVE ACCENT
+    return "\314\200";              // COMBINING GRAVE ACCENT
+  if (strcmp(in, "\302\264") == 0)  // ACUTE ACCENT
+    return "\314\201";              // COMBINING ACUTE ACCENT
+  if (strcmp(in, "\136") == 0)      // CIRCUMFLEX ACCENT
+    return "\314\202";              // COMBINING CIRCUMFLEX ACCENT
+  if (strcmp(in, "\176") == 0)      // TILDE
+    return "\314\203";              // COMBINING TILDE
+  if (strcmp(in, "\302\250") == 0)  // DIAERESIS
+    return "\314\210";              // COMBINING DIAERESIS
+  // FIXME: OS X dead key behaviour isn't documented and I don't have
+  //        any more keyboards to test with...
+
+  // hope that OS X gave us something proper to begin with
+  return in;
+}
+
 /*
 Handle cocoa keyboard events
 Events during a character composition sequence:
@@ -1648,6 +1703,7 @@
 - (void)rightMouseDragged:(NSEvent *)theEvent;
 - (void)otherMouseDragged:(NSEvent *)theEvent;
 - (void)scrollWheel:(NSEvent *)theEvent;
++ (NSString *)keyTranslate:(UInt16)keyCode withModifierFlags:(UInt32)modifierFlags;
 - (BOOL)handleKeyDown:(NSEvent *)theEvent;
 - (void)keyDown:(NSEvent *)theEvent;
 - (void)keyUp:(NSEvent *)theEvent;
@@ -1726,6 +1782,130 @@
 - (void)scrollWheel:(NSEvent *)theEvent {
   cocoaMouseWheelHandler(theEvent);
 }
++ (NSString *)keyTranslate:(UInt16)keyCode withModifierFlags:(UInt32)modifierFlags {
+  const UCKeyboardLayout *layout;
+  OSStatus err;
+
+  layout = NULL;
+
+#if (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
+  TISInputSourceRef keyboard;
+  CFDataRef uchr;
+
+  keyboard = TISCopyCurrentKeyboardInputSource();
+  uchr = (CFDataRef)TISGetInputSourceProperty(keyboard,
+                                              kTISPropertyUnicodeKeyLayoutData);
+  if (uchr == NULL)
+    return nil;
+
+  layout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
+#else
+  KeyboardLayoutRef old_layout;
+  int kind;
+
+  err = KLGetCurrentKeyboardLayout(&old_layout);
+  if (err != noErr)
+    return nil;
+
+  err = KLGetKeyboardLayoutProperty(old_layout, kKLKind,
+                                    (const void**)&kind);
+  if (err != noErr)
+    return nil;
+
+  // Old, crufty layout format?
+  if (kind == kKLKCHRKind) {
+    void *kchr_layout;
+
+    UInt32 chars, state;
+    char buf[3];
+
+    unichar result[16];
+    ByteCount in_len, out_len;
+
+    err = KLGetKeyboardLayoutProperty(old_layout, kKLKCHRData,
+                                      (const void**)&kchr_layout);
+    if (err != noErr)
+      return nil;
+
+    state = 0;
+
+    keyCode &= 0x7f;
+    modifierFlags &= 0xff00;
+
+    chars = KeyTranslate(kchr_layout, keyCode | modifierFlags, &state);
+
+    buf[0] = (chars >> 16) & 0xff;
+    buf[1] = chars & 0xff;
+    buf[2] = '\0';
+
+    if (buf[0] == '\0') {
+      buf[0] = buf[1];
+      buf[1] = '\0';
+    }
+
+    // The data is now in some layout specific encoding. Need to convert
+    // this to unicode.
+
+    ScriptCode script;
+    TextEncoding encoding;
+    TECObjectRef converter;
+
+    script = (ScriptCode)GetScriptManagerVariable(smKeyScript);
+
+    err = UpgradeScriptInfoToTextEncoding(script, kTextLanguageDontCare,
+                                          kTextRegionDontCare, NULL,
+                                          &encoding);
+    if (err != noErr)
+      return nil;
+
+    err = TECCreateConverter(&converter, encoding, kTextEncodingUnicodeV4_0);
+    if (err != noErr)
+      return nil;
+
+    in_len = strlen(buf);
+    out_len = sizeof(result);
+
+    err = TECConvertText(converter, (ConstTextPtr)buf, in_len, &in_len,
+                         (TextPtr)result, out_len, &out_len);
+
+    TECDisposeConverter(converter);
+
+    if (err != noErr)
+      return nil;
+
+    return [NSString stringWithCharacters:result
+                     length:(out_len / sizeof(unichar))];
+  }
+
+  if ((kind != kKLKCHRuchrKind) && (kind != kKLuchrKind))
+    return nil;
+
+  err = KLGetKeyboardLayoutProperty(old_layout, kKLuchrData,
+                                    (const void**)&layout);
+  if (err != noErr)
+    return nil;
+#endif
+ 
+  if (layout == NULL)
+    return nil;
+
+  UInt32 dead_state;
+  UniCharCount max_len, actual_len;
+  UniChar string[255];
+
+  dead_state = 0;
+  max_len = sizeof(string)/sizeof(*string);
+
+  modifierFlags = (modifierFlags >> 8) & 0xff;
+
+  err = UCKeyTranslate(layout, keyCode, kUCKeyActionDown, modifierFlags,
+                       LMGetKbdType(), 0, &dead_state, max_len, &actual_len,
+                       string);
+  if (err != noErr)
+    return nil;
+
+  return [NSString stringWithCharacters:string length:actual_len];
+}
 - (BOOL)handleKeyDown:(NSEvent *)theEvent {
   //NSLog(@"handleKeyDown");
   fl_lock_function();
@@ -1752,14 +1932,47 @@
       break;
     }
   }
-  if (!no_text_key && !(Fl::e_state & FL_META) ) {
-    // Don't send cmd-<key> to interpretKeyEvents because it beeps.
+  if (!no_text_key) {
+    // The simple keyboard model will ignore insertText, so we need to grab
+    // the symbol directly from the event. Note that we still use setMarkedText.
+    if (use_simple_keyboard) {
+      NSString *simple_chars;
+      UInt32 modifiers;
+
+      // We want a "normal" symbol out of the event, which basically means
+      // we only respect the shift and alt/altgr modifiers. Cocoa can help
+      // us if we only wanted shift, but as we also want alt/altgr, we'll
+      // have to do some lookup ourselves. This matches our behaviour on
+      // other platforms.
+
+      modifiers = 0;
+      if ([theEvent modifierFlags] & NSAlphaShiftKeyMask)
+        modifiers |= alphaLock;
+      if ([theEvent modifierFlags] & NSShiftKeyMask)
+        modifiers |= shiftKey;
+      if ([theEvent modifierFlags] & NSAlternateKeyMask)
+        modifiers |= optionKey;
+
+      simple_chars = [FLView keyTranslate:[theEvent keyCode]
+                             withModifierFlags:modifiers];
+      if (simple_chars == nil) {
+        // Something went wrong. Fall back to what Cocoa gave us...
+        simple_chars = [theEvent charactersIgnoringModifiers];
+      }
+
+      [FLView prepareEtext:simple_chars];
+    }
+
     // Then we can let the OS have a stab at it and see if it thinks it
     // should result in some text
-    NSText *edit = [[theEvent window]  fieldEditor:YES forObject:nil];
-    in_key_event = true;
-    [edit interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
-    in_key_event = false;
+
+    // Don't send cmd-<key> to interpretKeyEvents because it beeps.
+    if (!(Fl::e_state & FL_META)) {
+      NSText *edit = [[theEvent window]  fieldEditor:YES forObject:nil];
+      in_key_event = true;
+      [edit interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
+      in_key_event = false;
+    }
   }
   //NSLog(@"to text=%@ l=%d", [NSString stringWithUTF8String:Fl::e_text], Fl::e_length);
   int handled = Fl::handle(FL_KEYDOWN, window);
@@ -1937,21 +2150,30 @@
   //NSLog(@"insertText: received=%@",received);
 
   if (!in_key_event) fl_lock_function();
+
+  // Simple keyboard widgets do not want these side channel inputs.
+  if (use_simple_keyboard)
+    goto end;
+
   [FLView prepareEtext:received];
+
   // We can get called outside of key events (e.g. from the character
-  // palette). Transform such actions to FL_PASTE events.
+  // palette). We need to fake our own key event at that point.
   if (!in_key_event) {
     Fl_Window *target = [(FLWindow*)[self window] getFl_Window];
-    Fl::handle(FL_PASTE, target);
+    Fl::e_keysym = Fl::e_original_keysym = 0;
+    Fl::handle(FL_KEYDOWN, target);
     // for some reason, the window does not redraw until the next mouse move or button push
     // sending a 'redraw()' or 'awake()' does not solve the issue!
     Fl::flush();
   }
+
+end:
   if (!in_key_event) fl_unlock_function();
 }
 
 - (void)setMarkedText:(id)aString selectedRange:(NSRange)newSelection  {
-  NSString *received;
+  NSString *received, *current, *aggregate;
   if (newSelection.location == 0) {
     [self unmarkText];
     return;
@@ -1962,11 +2184,47 @@
     received = (NSString*)aString;
   }
   //NSLog(@"setMarkedText: %@ %d %d",received,newSelection.location,newSelection.length);
+
+  fl_lock_function();
+
+  // Simple keyboard widgets generally do not want these side channel
+  // inputs, but we have no other way of getting dead keys so we make
+  // an exception in that case.
+  if (use_simple_keyboard) {
+    if (in_key_event && (Fl::e_length == 0)) {
+      [FLView prepareEtext:received];
+
+      Fl::e_text = (char*)cocoaDead2FLTK(Fl::e_text);
+      Fl::e_length = strlen(Fl::e_text);
+    }
+    goto end;
+  }
+
   // This code creates the OS X behaviour of seeing dead keys as things
   // are being composed.
+  //
+  // Note: The concatenation thing is because of how OS X deals with
+  //       invalid sequences. At that point it will spit out one call
+  //       to insertText with the now aborted sequence, and one new
+  //       call to setMarkedText with the new sequence. Since we want
+  //       both to be visible, we need to concatenate.
   next_compose_length = newSelection.location;
-  [FLView prepareEtext:received];
-  //NSLog(@"Fl::e_text=%@ Fl::e_length=%d next_compose_length=%d", received, Fl::e_length, next_compose_length);
+  current = [NSString stringWithUTF8String:Fl::e_text];
+  aggregate = [current stringByAppendingString:received];
+
+  [FLView prepareEtext:aggregate];
+  //NSLog(@"Fl::e_text=%@ Fl::e_length=%d next_compose_length=%d", aggregate, Fl::e_length, next_compose_length);
+
+  // We can get called outside of key events (e.g. from the character
+  // palette). We need to fake our own key event at that point.
+  if (!in_key_event) {
+    Fl_Window *target = [(FLWindow*)[self window] getFl_Window];
+    Fl::e_keysym = Fl::e_original_keysym = 0;
+    Fl::handle(FL_KEYDOWN, target);
+  }
+
+end:
+  fl_unlock_function();
 }
 
 - (void)unmarkText {