File: keyboard.cpp

package info (click to toggle)
ares 134%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 34,680 kB
  • sloc: cpp: 338,717; ansic: 89,036; sh: 52; makefile: 27
file content (67 lines) | stat: -rw-r--r-- 1,483 bytes parent folder | download | duplicates (2)
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
#if defined(Hiro_Keyboard)

namespace hiro {

auto pKeyboard::poll() -> vector<bool> {
  if(Application::state().quit) return {};

  vector<bool> result;
  char state[256];

  #if defined(DISPLAY_XORG)
  XQueryKeymap(pApplication::state().display, state);
  #endif

  for(auto& code : settings.keycodes) {
    result.append(_pressed(state, code));
  }

  return result;
}

auto pKeyboard::pressed(unsigned code) -> bool {
  char state[256];

  #if defined(DISPLAY_XORG)
  XQueryKeymap(pApplication::state().display, state);
  #endif

  return _pressed(state, code);
}

auto pKeyboard::_pressed(const char* state, u16 code) -> bool {
  u8 lo = code >> 0;
  u8 hi = code >> 8;

  #if defined(DISPLAY_XORG)
  if(lo && state[lo >> 3] & (1 << (lo & 7))) return true;
  if(hi && state[hi >> 3] & (1 << (hi & 7))) return true;
  #endif

  return false;
}

auto pKeyboard::initialize() -> void {
  auto append = [](u32 lo, u32 hi = 0) {
    #if defined(DISPLAY_XORG)
    lo = lo ? (u8)XKeysymToKeycode(pApplication::state().display, lo) : 0;
    hi = hi ? (u8)XKeysymToKeycode(pApplication::state().display, hi) : 0;
    #endif
    settings.keycodes.append(lo << 0 | hi << 8);
  };

  #define map(name, ...) if(key == name) { append(__VA_ARGS__); continue; }
  for(auto& key : Keyboard::keys) {
    #if defined(DISPLAY_XORG)
      #include <hiro/platform/xorg/keyboard.hpp>
    #endif

  //print("[hiro/qt] warning: unhandled key: ", key, "\n");
    append(0);
  }
  #undef map
}

}

#endif