Mir
event.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2016 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU 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  * Authored by: Brandon Schaefer <brandon.schaefer@canonical.com>
17  */
18 
19 #ifndef MIR_COMMON_EVENT_H_
20 #define MIR_COMMON_EVENT_H_
21 
22 #include "mir_toolkit/event.h"
24 
25 #include <cstring>
26 
27 // Only MirPointerEvent and MirTouchEvent are typedefed in the public API
29 
30 struct MirEvent
31 {
32  MirEventType type() const;
33 
35  MirInputEvent const* to_input() const;
36 
39 
41  MirSurfaceEvent const* to_surface() const;
42 
44  MirResizeEvent const* to_resize() const;
45 
48 
50  MirOrientationEvent const* to_orientation() const;
51 
54 
56  MirKeymapEvent const* to_keymap() const;
57 
60 
63 
64  MirEvent* clone() const;
65 
66  static mir::EventUPtr deserialize(std::string const& bytes);
67  static std::string serialize(MirEvent const* event);
68 
69 protected:
70  MirEvent() = default;
71  explicit MirEvent(MirEventType type);
72  MirEvent(MirEvent const& event) = default;
73  MirEvent& operator=(MirEvent const& event) = default;
74 
76 };
77 
78 namespace mir
79 {
80 namespace event
81 {
82 template<typename Data>
83 char const* consume(char const* pos, Data& data)
84 {
85  std::memcpy(&data, pos, sizeof data);
86  return pos + sizeof data;
87 }
88 
89 template<typename T>
91 {
92 #if __GNUC__ >= 5
93  static_assert(std::is_trivially_copyable<T>::value, "");
94 #else
95  static_assert(__has_trivial_copy(T), "");
96 #endif
97 }
98 
99 // T needs to be trivially copyable
100 // vivid wont allow a std::is_trivially_copyable
101 template<typename T>
102 mir::EventUPtr deserialize_from(std::string const& bytes)
103 {
104  assert_type_is_trivially_copyable<T>();
105 
106  T* t = new T;
107  memcpy(t, bytes.data(), bytes.size());
108 
109  return mir::EventUPtr(t, [](MirEvent* e) { delete e; });
110 }
111 
112 template<typename T>
113 std::string serialize_from(MirEvent const* ev)
114 {
115  assert_type_is_trivially_copyable<T>();
116 
117  std::string encoded_bytes;
118  encoded_bytes.append(reinterpret_cast<char const*>(ev), sizeof(T));
119 
120  return encoded_bytes;
121 }
122 
123 template<typename T>
125 {
126  assert_type_is_trivially_copyable<T>();
127 
128  T* t = new T;
129  memcpy(t, ev, sizeof(T));
130 
131  return t;
132 }
133 }
134 }
135 
136 #endif /* MIR_COMMON_EVENT_H_ */
All things Mir.
Definition: atomic_callback.h:25
MirResizeEvent * to_resize()
Definition: event.cpp:183
mir::EventUPtr deserialize_from(std::string const &bytes)
Definition: event.h:102
Definition: keymap_event.h:27
Definition: surface_event.h:24
MirEventType type() const
Definition: event.cpp:148
void assert_type_is_trivially_copyable()
Definition: event.h:90
MirEventType type_
Definition: event.h:75
MirSurfaceOutputEvent * to_surface_output()
Definition: event.cpp:233
std::string serialize_from(MirEvent const *ev)
Definition: event.h:113
MirInputDeviceStateEvent * to_input_device_state()
Definition: event.cpp:243
Definition: prompt_session_event.h:24
static mir::EventUPtr deserialize(std::string const &bytes)
Definition: event.cpp:74
Definition: input_device_state_event.h:27
MirEvent & operator=(MirEvent const &event)=default
MirInputConfigurationEvent * to_input_configuration()
Definition: event.cpp:163
MirKeymapEvent * to_keymap()
Definition: event.cpp:223
static std::string serialize(MirEvent const *event)
Definition: event.cpp:115
MirEvent * deep_copy(MirEvent const *ev)
Definition: event.h:124
MirEvent * clone() const
Definition: event.cpp:41
Definition: surface_output_event.h:27
std::unique_ptr< MirEvent, void(*)(MirEvent *)> EventUPtr
Definition: seat.h:32
MirEventType
Definition: event.h:34
MirEvent()=default
Definition: resize_event.h:24
Definition: close_surface_event.h:24
MirCloseSurfaceEvent * to_close_surface()
Definition: event.cpp:213
Definition: input_event.h:24
MirOrientationEvent * to_orientation()
Definition: event.cpp:203
MirPromptSessionEvent * to_prompt_session()
Definition: event.cpp:193
Definition: input_configuration_event.h:27
Definition: motion_event.h:52
MirSurfaceEvent * to_surface()
Definition: event.cpp:173
Definition: event.h:30
MirInputEvent * to_input()
Definition: event.cpp:153
char const * consume(char const *pos, Data &data)
Definition: event.h:83
Definition: orientation_event.h:24

Copyright © 2012-2015 Canonical Ltd.
Generated on Thu Sep 8 14:50:19 UTC 2016