summaryrefslogtreecommitdiffstats
path: root/widget/gtk/NativeKeyBindings.cpp
blob: f5b15367a6020f43cd00da8d405ad5d24ff9834a (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/ArrayUtils.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/Maybe.h"
#include "mozilla/NativeKeyBindingsType.h"
#include "mozilla/StaticPrefs_ui.h"
#include "mozilla/TextEvents.h"
#include "mozilla/WritingModes.h"

#include "NativeKeyBindings.h"
#include "nsString.h"
#include "nsGtkKeyUtils.h"

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <gdk/gdkkeysyms-compat.h>
#include <gdk/gdk.h>

namespace mozilla {
namespace widget {

static nsTArray<CommandInt>* gCurrentCommands = nullptr;
static bool gHandled = false;

inline void AddCommand(Command aCommand) {
  MOZ_ASSERT(gCurrentCommands);
  gCurrentCommands->AppendElement(static_cast<CommandInt>(aCommand));
}

// Common GtkEntry and GtkTextView signals
static void copy_clipboard_cb(GtkWidget* w, gpointer user_data) {
  AddCommand(Command::Copy);
  g_signal_stop_emission_by_name(w, "copy_clipboard");
  gHandled = true;
}

static void cut_clipboard_cb(GtkWidget* w, gpointer user_data) {
  AddCommand(Command::Cut);
  g_signal_stop_emission_by_name(w, "cut_clipboard");
  gHandled = true;
}

// GTK distinguishes between display lines (wrapped, as they appear on the
// screen) and paragraphs, which are runs of text terminated by a newline.
// We don't have this distinction, so we always use editor's notion of
// lines, which are newline-terminated.

static const Command sDeleteCommands[][2] = {
    // backward, forward
    // CHARS
    {Command::DeleteCharBackward, Command::DeleteCharForward},
    // WORD_ENDS
    {Command::DeleteWordBackward, Command::DeleteWordForward},
    // WORDS
    {Command::DeleteWordBackward, Command::DeleteWordForward},
    // LINES
    {Command::DeleteToBeginningOfLine, Command::DeleteToEndOfLine},
    // LINE_ENDS
    {Command::DeleteToBeginningOfLine, Command::DeleteToEndOfLine},
    // PARAGRAPH_ENDS
    {Command::DeleteToBeginningOfLine, Command::DeleteToEndOfLine},
    // PARAGRAPHS
    {Command::DeleteToBeginningOfLine, Command::DeleteToEndOfLine},
    // This deletes from the end of the previous word to the beginning of the
    // next word, but only if the caret is not in a word.
    // XXX need to implement in editor
    {Command::DoNothing, Command::DoNothing}  // WHITESPACE
};

static void delete_from_cursor_cb(GtkWidget* w, GtkDeleteType del_type,
                                  gint count, gpointer user_data) {
  g_signal_stop_emission_by_name(w, "delete_from_cursor");
  if (count == 0) {
    // Nothing to do.
    return;
  }

  bool forward = count > 0;

  // Ignore GTK's Ctrl-K keybinding introduced in GTK 3.14 and removed in
  // 3.18 if the user has custom bindings set. See bug 1176929.
  if (del_type == GTK_DELETE_PARAGRAPH_ENDS && forward && GTK_IS_ENTRY(w) &&
      !gtk_check_version(3, 14, 1) && gtk_check_version(3, 17, 9)) {
    GtkStyleContext* context = gtk_widget_get_style_context(w);
    GtkStateFlags flags = gtk_widget_get_state_flags(w);

    GPtrArray* array;
    gtk_style_context_get(context, flags, "gtk-key-bindings", &array, nullptr);
    if (!array) return;
    g_ptr_array_unref(array);
  }

  gHandled = true;
  if (uint32_t(del_type) >= ArrayLength(sDeleteCommands)) {
    // unsupported deletion type
    return;
  }

  if (del_type == GTK_DELETE_WORDS) {
    // This works like word_ends, except we first move the caret to the
    // beginning/end of the current word.
    if (forward) {
      AddCommand(Command::WordNext);
      AddCommand(Command::WordPrevious);
    } else {
      AddCommand(Command::WordPrevious);
      AddCommand(Command::WordNext);
    }
  } else if (del_type == GTK_DELETE_DISPLAY_LINES ||
             del_type == GTK_DELETE_PARAGRAPHS) {
    // This works like display_line_ends, except we first move the caret to the
    // beginning/end of the current line.
    if (forward) {
      AddCommand(Command::BeginLine);
    } else {
      AddCommand(Command::EndLine);
    }
  }

  Command command = sDeleteCommands[del_type][forward];
  if (command == Command::DoNothing) {
    return;
  }

  unsigned int absCount = Abs(count);
  for (unsigned int i = 0; i < absCount; ++i) {
    AddCommand(command);
  }
}

static const Command sMoveCommands[][2][2] = {
    // non-extend { backward, forward }, extend { backward, forward }
    // GTK differentiates between logical position, which is prev/next,
    // and visual position, which is always left/right.
    // We should fix this to work the same way for RTL text input.
    {// LOGICAL_POSITIONS
     {Command::CharPrevious, Command::CharNext},
     {Command::SelectCharPrevious, Command::SelectCharNext}},
    {// VISUAL_POSITIONS
     {Command::CharPrevious, Command::CharNext},
     {Command::SelectCharPrevious, Command::SelectCharNext}},
    {// WORDS
     {Command::WordPrevious, Command::WordNext},
     {Command::SelectWordPrevious, Command::SelectWordNext}},
    {// DISPLAY_LINES
     {Command::LinePrevious, Command::LineNext},
     {Command::SelectLinePrevious, Command::SelectLineNext}},
    {// DISPLAY_LINE_ENDS
     {Command::BeginLine, Command::EndLine},
     {Command::SelectBeginLine, Command::SelectEndLine}},
    {// PARAGRAPHS
     {Command::LinePrevious, Command::LineNext},
     {Command::SelectLinePrevious, Command::SelectLineNext}},
    {// PARAGRAPH_ENDS
     {Command::BeginLine, Command::EndLine},
     {Command::SelectBeginLine, Command::SelectEndLine}},
    {// PAGES
     {Command::MovePageUp, Command::MovePageDown},
     {Command::SelectPageUp, Command::SelectPageDown}},
    {// BUFFER_ENDS
     {Command::MoveTop, Command::MoveBottom},
     {Command::SelectTop, Command::SelectBottom}},
    {// HORIZONTAL_PAGES (unsupported)
     {Command::DoNothing, Command::DoNothing},
     {Command::DoNothing, Command::DoNothing}}};

static void move_cursor_cb(GtkWidget* w, GtkMovementStep step, gint count,
                           gboolean extend_selection, gpointer user_data) {
  g_signal_stop_emission_by_name(w, "move_cursor");
  if (count == 0) {
    // Nothing to do.
    return;
  }

  gHandled = true;
  bool forward = count > 0;
  if (uint32_t(step) >= ArrayLength(sMoveCommands)) {
    // unsupported movement type
    return;
  }

  Command command = sMoveCommands[step][extend_selection][forward];
  if (command == Command::DoNothing) {
    return;
  }

  unsigned int absCount = Abs(count);
  for (unsigned int i = 0; i < absCount; ++i) {
    AddCommand(command);
  }
}

static void paste_clipboard_cb(GtkWidget* w, gpointer user_data) {
  AddCommand(Command::Paste);
  g_signal_stop_emission_by_name(w, "paste_clipboard");
  gHandled = true;
}

// GtkTextView-only signals
static void select_all_cb(GtkWidget* aWidget, gboolean aSelect,
                          gpointer aUserData) {
  // We don't support "Unselect All" command.
  // Note that if we'd support it, `Ctrl-Shift-a` will be mapped to it and
  // overrides open `about:addons` shortcut.
  if (aSelect) {
    AddCommand(Command::SelectAll);
  }
  g_signal_stop_emission_by_name(aWidget, "select_all");
  // Although we prevent the default of `GtkTExtView` with
  // `g_signal_stop_emission_by_name`, but `gHandled` is used for asserting
  // if it does not match with the emptiness of the command array.
  // Therefore, we should not set it to `true` if we don't add a command.
  gHandled |= aSelect;
}

NativeKeyBindings* NativeKeyBindings::sInstanceForSingleLineEditor = nullptr;
NativeKeyBindings* NativeKeyBindings::sInstanceForMultiLineEditor = nullptr;

// static
NativeKeyBindings* NativeKeyBindings::GetInstance(NativeKeyBindingsType aType) {
  switch (aType) {
    case NativeKeyBindingsType::SingleLineEditor:
      if (!sInstanceForSingleLineEditor) {
        sInstanceForSingleLineEditor = new NativeKeyBindings();
        sInstanceForSingleLineEditor->Init(aType);
      }
      return sInstanceForSingleLineEditor;

    default:
      // fallback to multiline editor case in release build
      MOZ_FALLTHROUGH_ASSERT("aType is invalid or not yet implemented");
    case NativeKeyBindingsType::MultiLineEditor:
    case NativeKeyBindingsType::RichTextEditor:
      if (!sInstanceForMultiLineEditor) {
        sInstanceForMultiLineEditor = new NativeKeyBindings();
        sInstanceForMultiLineEditor->Init(aType);
      }
      return sInstanceForMultiLineEditor;
  }
}

// static
void NativeKeyBindings::Shutdown() {
  delete sInstanceForSingleLineEditor;
  sInstanceForSingleLineEditor = nullptr;
  delete sInstanceForMultiLineEditor;
  sInstanceForMultiLineEditor = nullptr;
}

void NativeKeyBindings::Init(NativeKeyBindingsType aType) {
  switch (aType) {
    case NativeKeyBindingsType::SingleLineEditor:
      mNativeTarget = gtk_entry_new();
      break;
    default:
      mNativeTarget = gtk_text_view_new();
      g_signal_connect(mNativeTarget, "select_all", G_CALLBACK(select_all_cb),
                       this);
      break;
  }

  g_object_ref_sink(mNativeTarget);

  g_signal_connect(mNativeTarget, "copy_clipboard",
                   G_CALLBACK(copy_clipboard_cb), this);
  g_signal_connect(mNativeTarget, "cut_clipboard", G_CALLBACK(cut_clipboard_cb),
                   this);
  g_signal_connect(mNativeTarget, "delete_from_cursor",
                   G_CALLBACK(delete_from_cursor_cb), this);
  g_signal_connect(mNativeTarget, "move_cursor", G_CALLBACK(move_cursor_cb),
                   this);
  g_signal_connect(mNativeTarget, "paste_clipboard",
                   G_CALLBACK(paste_clipboard_cb), this);
}

NativeKeyBindings::~NativeKeyBindings() {
  gtk_widget_destroy(mNativeTarget);
  g_object_unref(mNativeTarget);
}

void NativeKeyBindings::GetEditCommands(const WidgetKeyboardEvent& aEvent,
                                        const Maybe<WritingMode>& aWritingMode,
                                        nsTArray<CommandInt>& aCommands) {
  MOZ_ASSERT(!aEvent.mFlags.mIsSynthesizedForTests);
  MOZ_ASSERT(aCommands.IsEmpty());

  // It must be a DOM event dispached by chrome script.
  if (!aEvent.mNativeKeyEvent) {
    return;
  }

  guint keyval;
  if (aEvent.mCharCode) {
    keyval = gdk_unicode_to_keyval(aEvent.mCharCode);
  } else if (aWritingMode.isSome() && aEvent.NeedsToRemapNavigationKey() &&
             aWritingMode.ref().IsVertical()) {
    // TODO: Use KeyNameIndex rather than legacy keyCode.
    uint32_t remappedGeckoKeyCode =
        aEvent.GetRemappedKeyCode(aWritingMode.ref());
    switch (remappedGeckoKeyCode) {
      case NS_VK_UP:
        keyval = GDK_Up;
        break;
      case NS_VK_DOWN:
        keyval = GDK_Down;
        break;
      case NS_VK_LEFT:
        keyval = GDK_Left;
        break;
      case NS_VK_RIGHT:
        keyval = GDK_Right;
        break;
      default:
        MOZ_ASSERT_UNREACHABLE("Add a case for the new remapped key");
        return;
    }
  } else {
    keyval = static_cast<GdkEventKey*>(aEvent.mNativeKeyEvent)->keyval;
  }

  if (GetEditCommandsInternal(aEvent, aCommands, keyval)) {
    return;
  }

  for (uint32_t i = 0; i < aEvent.mAlternativeCharCodes.Length(); ++i) {
    uint32_t ch = aEvent.IsShift()
                      ? aEvent.mAlternativeCharCodes[i].mShiftedCharCode
                      : aEvent.mAlternativeCharCodes[i].mUnshiftedCharCode;
    if (ch && ch != aEvent.mCharCode) {
      keyval = gdk_unicode_to_keyval(ch);
      if (GetEditCommandsInternal(aEvent, aCommands, keyval)) {
        return;
      }
    }
  }

  // If the key event does not cause any commands, and we're for single line
  // editor, let's check whether the key combination is for "select-all" in
  // GtkTextView because the signal is not supported by GtkEntry.
  if (aCommands.IsEmpty() && this == sInstanceForSingleLineEditor &&
      StaticPrefs::ui_key_use_select_all_in_single_line_editor()) {
    if (NativeKeyBindings* bindingsForMultilineEditor =
            GetInstance(NativeKeyBindingsType::MultiLineEditor)) {
      bindingsForMultilineEditor->GetEditCommands(aEvent, aWritingMode,
                                                  aCommands);
      if (aCommands.Length() == 1u &&
          aCommands[0u] == static_cast<CommandInt>(Command::SelectAll)) {
        return;
      }
      aCommands.Clear();
    }
  }

  /*
  gtk_bindings_activate_event is preferable, but it has unresolved bug:
  http://bugzilla.gnome.org/show_bug.cgi?id=162726
  The bug was already marked as FIXED.  However, somebody reports that the
  bug still exists.
  Also gtk_bindings_activate may work with some non-shortcuts operations
  (todo: check it). See bug 411005 and bug 406407.

  Code, which should be used after fixing GNOME bug 162726:

    gtk_bindings_activate_event(GTK_OBJECT(mNativeTarget),
      static_cast<GdkEventKey*>(aEvent.mNativeKeyEvent));
  */
}

bool NativeKeyBindings::GetEditCommandsInternal(
    const WidgetKeyboardEvent& aEvent, nsTArray<CommandInt>& aCommands,
    guint aKeyval) {
  guint modifiers = static_cast<GdkEventKey*>(aEvent.mNativeKeyEvent)->state;

  gCurrentCommands = &aCommands;

  gHandled = false;
  gtk_bindings_activate(G_OBJECT(mNativeTarget), aKeyval,
                        GdkModifierType(modifiers));

  gCurrentCommands = nullptr;

  return gHandled;
}

// static
void NativeKeyBindings::GetEditCommandsForTests(
    NativeKeyBindingsType aType, const WidgetKeyboardEvent& aEvent,
    const Maybe<WritingMode>& aWritingMode, nsTArray<CommandInt>& aCommands) {
  MOZ_DIAGNOSTIC_ASSERT(aEvent.IsTrusted());

  if (aEvent.IsAlt() || aEvent.IsMeta() || aEvent.IsOS()) {
    return;
  }

  static const size_t kBackward = 0;
  static const size_t kForward = 1;
  const size_t extentSelection = aEvent.IsShift() ? 1 : 0;
  // https://github.com/GNOME/gtk/blob/1f141c19533f4b3f397c3959ade673ce243b6138/gtk/gtktext.c#L1289
  // https://github.com/GNOME/gtk/blob/c5dd34344f0c660ceffffb3bf9da43c263db16e1/gtk/gtktextview.c#L1534
  Command command = Command::DoNothing;
  const KeyNameIndex remappedKeyNameIndex =
      aWritingMode.isSome() ? aEvent.GetRemappedKeyNameIndex(aWritingMode.ref())
                            : aEvent.mKeyNameIndex;
  switch (remappedKeyNameIndex) {
    case KEY_NAME_INDEX_USE_STRING:
      switch (aEvent.PseudoCharCode()) {
        case 'a':
        case 'A':
          if (aEvent.IsControl()) {
            command = Command::SelectAll;
          }
          break;
        case 'c':
        case 'C':
          if (aEvent.IsControl() && !aEvent.IsShift()) {
            command = Command::Copy;
          }
          break;
        case 'u':
        case 'U':
          if (aType == NativeKeyBindingsType::SingleLineEditor &&
              aEvent.IsControl() && !aEvent.IsShift()) {
            command = sDeleteCommands[GTK_DELETE_PARAGRAPH_ENDS][kBackward];
          }
          break;
        case 'v':
        case 'V':
          if (aEvent.IsControl() && !aEvent.IsShift()) {
            command = Command::Paste;
          }
          break;
        case 'x':
        case 'X':
          if (aEvent.IsControl() && !aEvent.IsShift()) {
            command = Command::Cut;
          }
          break;
        case '/':
          if (aEvent.IsControl() && !aEvent.IsShift()) {
            command = Command::SelectAll;
          }
          break;
        default:
          break;
      }
      break;
    case KEY_NAME_INDEX_Insert:
      if (aEvent.IsControl() && !aEvent.IsShift()) {
        command = Command::Copy;
      } else if (aEvent.IsShift() && !aEvent.IsControl()) {
        command = Command::Paste;
      }
      break;
    case KEY_NAME_INDEX_Delete:
      if (aEvent.IsShift()) {
        command = Command::Cut;
        break;
      }
      [[fallthrough]];
    case KEY_NAME_INDEX_Backspace: {
      const size_t direction =
          remappedKeyNameIndex == KEY_NAME_INDEX_Delete ? kForward : kBackward;
      const GtkDeleteType amount =
          aEvent.IsControl() && aEvent.IsShift()
              ? GTK_DELETE_PARAGRAPH_ENDS
              // FYI: Shift key for Backspace is ignored to help mis-typing.
              : (aEvent.IsControl() ? GTK_DELETE_WORD_ENDS : GTK_DELETE_CHARS);
      command = sDeleteCommands[amount][direction];
      break;
    }
    case KEY_NAME_INDEX_ArrowLeft:
    case KEY_NAME_INDEX_ArrowRight: {
      const size_t direction = remappedKeyNameIndex == KEY_NAME_INDEX_ArrowRight
                                   ? kForward
                                   : kBackward;
      const GtkMovementStep amount = aEvent.IsControl()
                                         ? GTK_MOVEMENT_WORDS
                                         : GTK_MOVEMENT_VISUAL_POSITIONS;
      command = sMoveCommands[amount][extentSelection][direction];
      break;
    }
    case KEY_NAME_INDEX_ArrowUp:
    case KEY_NAME_INDEX_ArrowDown: {
      const size_t direction = remappedKeyNameIndex == KEY_NAME_INDEX_ArrowDown
                                   ? kForward
                                   : kBackward;
      const GtkMovementStep amount = aEvent.IsControl()
                                         ? GTK_MOVEMENT_PARAGRAPHS
                                         : GTK_MOVEMENT_DISPLAY_LINES;
      command = sMoveCommands[amount][extentSelection][direction];
      break;
    }
    case KEY_NAME_INDEX_Home:
    case KEY_NAME_INDEX_End: {
      const size_t direction =
          remappedKeyNameIndex == KEY_NAME_INDEX_End ? kForward : kBackward;
      const GtkMovementStep amount = aEvent.IsControl()
                                         ? GTK_MOVEMENT_BUFFER_ENDS
                                         : GTK_MOVEMENT_DISPLAY_LINE_ENDS;
      command = sMoveCommands[amount][extentSelection][direction];
      break;
    }
    case KEY_NAME_INDEX_PageUp:
    case KEY_NAME_INDEX_PageDown: {
      const size_t direction = remappedKeyNameIndex == KEY_NAME_INDEX_PageDown
                                   ? kForward
                                   : kBackward;
      const GtkMovementStep amount = aEvent.IsControl()
                                         ? GTK_MOVEMENT_HORIZONTAL_PAGES
                                         : GTK_MOVEMENT_PAGES;
      command = sMoveCommands[amount][extentSelection][direction];
      break;
    }
    default:
      break;
  }
  if (command != Command::DoNothing) {
    aCommands.AppendElement(static_cast<CommandInt>(command));
  }
}

}  // namespace widget
}  // namespace mozilla