File: x11_keyboard_impl.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (126 lines) | stat: -rw-r--r-- 3,587 bytes parent folder | download
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/linux/x11_keyboard_impl.h"

#include <X11/extensions/XInput.h>
#include <X11/extensions/XTest.h>
#include <X11/XKBlib.h>

#include "base/strings/stringprintf.h"
#include "remoting/host/linux/unicode_to_keysym.h"
#include "ui/gfx/x/x11_types.h"

namespace {

bool FindKeycodeForKeySym(Display* display,
                          KeySym key_sym,
                          uint32_t* keycode,
                          uint32_t* modifiers) {
  uint32_t found_keycode = XKeysymToKeycode(display, key_sym);

  const uint32_t kModifiersToTry[] = {
      0,
      ShiftMask,
      Mod2Mask,
      Mod3Mask,
      Mod4Mask,
      ShiftMask | Mod2Mask,
      ShiftMask | Mod3Mask,
      ShiftMask | Mod4Mask,
  };

  // TODO(sergeyu): Is there a better way to find modifiers state?
  for (size_t i = 0; i < arraysize(kModifiersToTry); ++i) {
    unsigned long key_sym_with_mods;
    if (XkbLookupKeySym(display, found_keycode, kModifiersToTry[i], nullptr,
                        &key_sym_with_mods) &&
        key_sym_with_mods == key_sym) {
      *modifiers = kModifiersToTry[i];
      *keycode = found_keycode;
      return true;
    }
  }
  return false;
}

}  // namespace

namespace remoting {

X11KeyboardImpl::X11KeyboardImpl(Display* display) : display_(display) {}

X11KeyboardImpl::~X11KeyboardImpl() {}

std::vector<uint32_t> X11KeyboardImpl::GetUnusedKeycodes() {
  std::vector<uint32_t> unused_keycodes_;
  int min_keycode;
  int max_keycode;
  XDisplayKeycodes(display_, &min_keycode, &max_keycode);
  uint32_t keycode_count = max_keycode - min_keycode + 1;

  int sym_per_key;
  gfx::XScopedPtr<KeySym> mapping(
      XGetKeyboardMapping(display_, min_keycode, keycode_count, &sym_per_key));
  for (int keycode = max_keycode; keycode >= min_keycode; keycode--) {
    bool used = false;
    int offset = (keycode - min_keycode) * sym_per_key;
    for (int level = 0; level < sym_per_key; level++) {
      if (mapping.get()[offset + level]) {
        used = true;
        break;
      }
    }
    if (!used) {
      unused_keycodes_.push_back(keycode);
    }
  }
  return unused_keycodes_;
}

void X11KeyboardImpl::PressKey(uint32_t keycode, uint32_t modifiers) {
  XkbLockModifiers(display_, XkbUseCoreKbd, modifiers, modifiers);

  XTestFakeKeyEvent(display_, keycode, True, CurrentTime);
  XTestFakeKeyEvent(display_, keycode, False, CurrentTime);

  XkbLockModifiers(display_, XkbUseCoreKbd, modifiers, 0);
}

bool X11KeyboardImpl::FindKeycode(uint32_t code_point,
                                  uint32_t* keycode,
                                  uint32_t* modifiers) {
  for (uint32_t keysym : GetKeySymsForUnicode(code_point)) {
    if (FindKeycodeForKeySym(display_, keysym, keycode, modifiers)) {
      return true;
    }
  }
  return false;
}

bool X11KeyboardImpl::ChangeKeyMapping(uint32_t keycode, uint32_t code_point) {
  KeySym sym = NoSymbol;
  if (code_point > 0) {
    std::string sym_hex = base::StringPrintf("U%x", code_point);
    sym = XStringToKeysym(sym_hex.c_str());
    if (sym == NoSymbol) {
      // The server may not support Unicode-to-KeySym translation.
      return false;
    }
  }

  KeySym syms[2]{sym, sym};  // {lower-case, upper-case}
  XChangeKeyboardMapping(display_, keycode, 2, syms, 1);
  return true;
}

void X11KeyboardImpl::Flush() {
  XFlush(display_);
}

void X11KeyboardImpl::Sync() {
  XSync(display_, false);
}

}  // namespace remoting