TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / platform / sdl / platform.h
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #ifndef ANBOX_PLATFORM_SDL_PLATFORM_H_
19 #define ANBOX_PLATFORM_SDL_PLATFORM_H_
20
21 #include "anbox/platform/sdl/window.h"
22 #include "anbox/platform/sdl/sdl_wrapper.h"
23 #include "anbox/platform/base_platform.h"
24 #include "anbox/graphics/emugl/DisplayManager.h"
25 #include "anbox/input/device.h"
26
27 #include <map>
28 #include <thread>
29
30 class Renderer;
31
32 namespace anbox {
33 namespace input {
34 class Device;
35 class Manager;
36 }  // namespace input
37 namespace wm {
38 class Manager;
39 } // namespace wm
40 namespace platform {
41 namespace sdl {
42 class Platform : public std::enable_shared_from_this<Platform>,
43                        public platform::BasePlatform,
44                        public Window::Observer {
45  public:
46   Platform(const std::shared_ptr<input::Manager> &input_manager,
47            const Configuration &config);
48   ~Platform();
49
50   std::shared_ptr<wm::Window> create_window(
51       const anbox::wm::Task::Id &task,
52       const anbox::graphics::Rect &frame,
53       const std::string &title) override;
54
55   void window_deleted(const Window::Id &id) override;
56   void window_wants_focus(const Window::Id &id) override;
57   void window_moved(const Window::Id &id, const std::int32_t &x,
58                     const std::int32_t &y) override;
59   void window_resized(const Window::Id &id, const std::int32_t &width,
60                       const std::int32_t &height) override;
61
62   void set_renderer(const std::shared_ptr<Renderer> &renderer) override;
63   void set_window_manager(const std::shared_ptr<wm::Manager> &window_manager) override;
64
65   void set_clipboard_data(const ClipboardData &data) override;
66   ClipboardData get_clipboard_data() override;
67
68   std::shared_ptr<audio::Sink> create_audio_sink() override;
69   std::shared_ptr<audio::Source> create_audio_source() override;
70
71   bool supports_multi_window() const override;
72
73  private:
74   void process_events();
75   void process_input_event(const SDL_Event &event);
76
77   bool adjust_coordinates(std::int32_t &x, std::int32_t &y);
78   bool adjust_coordinates(SDL_Window *window, std::int32_t &x, std::int32_t &y);
79   bool calculate_touch_coordinates(const SDL_Event &event, std::int32_t &x,
80                                    std::int32_t &y);
81
82   static Window::Id next_window_id();
83   static constexpr std::uint32_t emulated_touch_id_ = 0;
84
85   std::shared_ptr<Renderer> renderer_;
86   std::shared_ptr<input::Manager> input_manager_;
87   std::shared_ptr<wm::Manager> window_manager_;
88   // We don't own the windows anymore after the got created by us so we
89   // need to be careful once we try to use them again.
90   std::map<Window::Id, std::weak_ptr<Window>> windows_;
91   std::shared_ptr<Window> current_window_;
92   std::thread event_thread_;
93   bool event_thread_running_;
94   std::shared_ptr<input::Device> pointer_;
95   std::shared_ptr<input::Device> keyboard_;
96   std::shared_ptr<input::Device> touch_;
97   graphics::Rect display_frame_;
98   bool window_size_immutable_ = false;
99   std::uint32_t focused_sdl_window_id_ = 0;
100   Configuration config_;
101
102   static const int MAX_FINGERS = 10;
103   static const int MAX_TRACKING_ID = 10;
104   int touch_slots[MAX_FINGERS];
105   int last_slot = -1;
106
107   int find_touch_slot(int id);
108   void push_slot(std::vector<input::Event> &touch_events, int slot);
109   void push_finger_down(int x, int y, int finger_id, std::vector<input::Event> &touch_events);
110   void push_finger_up(int finger_id, std::vector<input::Event> &touch_events);
111   void push_finger_motion(int x, int y, int finger_id, std::vector<input::Event> &touch_events);
112 };
113 } // namespace sdl
114 } // namespace platform
115 } // namespace anbox
116
117 #endif