Mir
event_builder.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2021 Canonical, Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 3, as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10  * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MIROIL_EVENT_BUILDER_H
18 #define MIROIL_EVENT_BUILDER_H
19 
20 #include <mir_toolkit/mir_input_device_types.h>
21 #include <miral/version.h>
22 #if MIRAL_VERSION >= MIR_VERSION_NUMBER(3, 0, 0)
23 #include <miral/toolkit_event.h>
24 #else
25 #include <mir_toolkit/event.h>
26 #endif
27 
28 #include <chrono>
29 #include <memory>
30 #include <sys/types.h>
31 #include <vector>
32 
33 struct MirEvent;
34 struct MirInputEvent;
35 
36 namespace mir { typedef std::unique_ptr<MirEvent, void(*)(MirEvent*)> EventUPtr; }
37 
38 namespace miroil
39 {
40 
41 /*
42  Creates Mir input events out of Qt input events
43 
44  The class is splitt into miroil::EventBuilder which does the internal mir stuff,
45  and qtmir::EventBuilder which handles the qt stuff.
46 
47  One important feature is that it's able to match a QInputEvent with the MirInputEvent that originated it, so
48  it can make a MirInputEvent version of a QInputEvent containing also information that the latter does not carry,
49  such as relative axis movement for pointer devices.
50 */
51 
52 class EventBuilder {
53 
54 public:
55  class EventInfo {
56  public:
57  void store(const MirInputEvent *mirInputEvent, ulong qtTimestamp);
58 
59  ulong timestamp;
62  float relative_x{0};
63  float relative_y{0};
64  };
65 
66 public:
68  virtual ~EventBuilder();
69 
70  // add Touch event
71  void add_touch(MirEvent &event, MirTouchId touch_id, MirTouchAction action,
72  MirTouchTooltype tooltype, float x_axis_value, float y_axis_value,
73  float pressure_value, float touch_major_value, float touch_minor_value, float size_value);
74 
75  // Key event
76  mir::EventUPtr make_key_event(MirInputDeviceId device_id, std::chrono::nanoseconds timestamp,
77  std::vector<uint8_t> const& cookie, MirKeyboardAction action, xkb_keysym_t keysym,
78  int scan_code, MirInputEventModifiers modifiers);
79 
80  // Touch event
81  mir::EventUPtr make_touch_event(MirInputDeviceId device_id, std::chrono::nanoseconds timestamp,
82  std::vector<uint8_t> const& mac, MirInputEventModifiers modifiers);
83 
84  // Pointer event
85  mir::EventUPtr make_pointer_event(MirInputDeviceId device_id, std::chrono::nanoseconds timestamp,
86  std::vector<uint8_t> const& mac, MirInputEventModifiers modifiers, MirPointerAction action,
87  MirPointerButtons buttons_pressed,
88  float x_axis_value, float y_axis_value,
89  float hscroll_value, float vscroll_value,
90  float relative_x_value, float relative_y_value);
91 
92  EventInfo * find_info(ulong qtTimestamp);
93 
94  /* Stores information that cannot be carried by QInputEvents so that it can be fully
95  reconstructed later given the same qtTimestamp */
96  void store(const MirInputEvent *mirInputEvent, ulong qtTimestamp);
97 
98 private:
99  /*
100  Ring buffer that stores information on recent MirInputEvents that cannot be carried by QInputEvents.
101 
102  When MirInputEvents are dispatched through a QML scene, not all of its information can be carried
103  by QInputEvents. Some information is lost. Thus further on, if we want to transform a QInputEvent back into
104  its original MirInputEvent so that it can be consumed by a mir::scene::Surface and properly handled by mir clients
105  we have to reach out to this EventRegistry to get the missing bits.
106 
107  Given the objective of this EventRegistry (MirInputEvent reconstruction after having gone through QQuickWindow input dispatch
108  as a QInputEvent), it stores information only about the most recent MirInputEvents.
109  */
110  std::vector<EventInfo> event_info_vector;
111  size_t next_index{0};
112  size_t event_info_count{0};
113 };
114 
115 }
116 
117 #endif // MIROIL_EVENT_BUILDER_H

Copyright © 2012-2022 Canonical Ltd.
Generated on Thu Sep 8 12:37:23 UTC 2022
This documentation is licensed under the GPL version 2 or 3.