summaryrefslogtreecommitdiffstats
path: root/xbmc/windowing/wayland/InputProcessorKeyboard.h
blob: 73a37cee7f28ef339494c0499bbe3985397cafa2 (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
/*
 *  Copyright (C) 2017-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "Seat.h"
#include "XkbcommonKeymap.h"
#include "input/XBMC_keysym.h"
#include "threads/Timer.h"
#include "windowing/XBMC_events.h"

#include <atomic>
#include <cstdint>
#include <memory>

namespace KODI
{
namespace WINDOWING
{
namespace WAYLAND
{

class IInputHandlerKeyboard
{
public:
  virtual void OnKeyboardEnter() {}
  virtual void OnKeyboardLeave() {}
  virtual void OnKeyboardEvent(XBMC_Event& event) = 0;
  virtual ~IInputHandlerKeyboard() = default;
};

class CInputProcessorKeyboard final : public IRawInputHandlerKeyboard
{
public:
  CInputProcessorKeyboard(IInputHandlerKeyboard& handler);

  void OnKeyboardKeymap(CSeat* seat, wayland::keyboard_keymap_format format, std::string const& keymap) override;
  void OnKeyboardEnter(CSeat* seat,
                       std::uint32_t serial,
                       const wayland::surface_t& surface,
                       const wayland::array_t& keys) override;
  void OnKeyboardLeave(CSeat* seat,
                       std::uint32_t serial,
                       const wayland::surface_t& surface) override;
  void OnKeyboardKey(CSeat* seat, std::uint32_t serial, std::uint32_t time, std::uint32_t key, wayland::keyboard_key_state state) override;
  void OnKeyboardModifiers(CSeat* seat, std::uint32_t serial, std::uint32_t modsDepressed, std::uint32_t modsLatched, std::uint32_t modsLocked, std::uint32_t group) override;
  void OnKeyboardRepeatInfo(CSeat* seat, std::int32_t rate, std::int32_t delay) override;

private:
  CInputProcessorKeyboard(CInputProcessorKeyboard const& other) = delete;
  CInputProcessorKeyboard& operator=(CInputProcessorKeyboard const& other) = delete;

  void ConvertAndSendKey(std::uint32_t scancode, bool pressed);
  XBMC_Event SendKey(unsigned char scancode, XBMCKey key, std::uint16_t unicodeCodepoint, bool pressed);
  void KeyRepeatTimeout();

  IInputHandlerKeyboard& m_handler;

  std::unique_ptr<CXkbcommonContext> m_xkbContext;
  std::unique_ptr<CXkbcommonKeymap> m_keymap;
  // Default values are used if compositor does not send any
  std::atomic<int> m_keyRepeatDelay{1000};
  std::atomic<int> m_keyRepeatInterval{50};
  // Save complete XBMC_Event so no keymap lookups which might not be thread-safe
  // are needed in the repeat callback
  XBMC_Event m_keyToRepeat;

  CTimer m_keyRepeatTimer;
};

}
}
}