2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
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.
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.
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/>.
18 #include "anbox/platform/sdl/window.h"
19 #include "anbox/wm/window_state.h"
20 #include "anbox/graphics/density.h"
21 #include "anbox/logger.h"
23 #include <boost/throw_exception.hpp>
25 #if defined(MIR_SUPPORT)
26 #include <mir_toolkit/mir_client_library.h>
30 constexpr const int window_resize_border{10};
31 constexpr const int top_drag_area{42};
32 constexpr const int button_size{32};
33 constexpr const int button_margin{5};
34 constexpr const int button_padding{0};
40 Window::Id Window::Invalid{-1};
42 Window::Observer::~Observer() {}
44 Window::Window(const std::shared_ptr<Renderer> &renderer,
45 const Id &id, const wm::Task::Id &task,
46 const std::shared_ptr<Observer> &observer,
47 const graphics::Rect &frame,
48 const std::string &title,
51 : wm::Window(renderer, task, frame, title),
56 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
58 // NOTE: We don't furce GL initialization of the window as this will
59 // be take care of by the Renderer when we attach to it. On EGL
60 // initializing GL here will cause a surface to be created and the
61 // renderer will attempt to create one too which will not work as
62 // only a single surface per EGLNativeWindowType is supported.
63 std::uint32_t flags = 0;
65 flags |= SDL_WINDOW_BORDERLESS;
67 flags |= SDL_WINDOW_RESIZABLE;
69 window_ = SDL_CreateWindow(title.c_str(),
70 frame.left(), frame.top(),
71 frame.width(), frame.height(),
74 const auto message = utils::string_format("Failed to create window: %s", SDL_GetError());
75 BOOST_THROW_EXCEPTION(std::runtime_error(message));
78 // If we create a window with border (server-side decoration), We
79 // should not set hit test handler beacuse we don't need to simulate
80 // the behavior of the title bar and resize area.
81 if (borderless && utils::get_env_value("ANBOX_NO_SDL_WINDOW_HIT_TEST", "false") == "false")
82 if (SDL_SetWindowHitTest(window_, &Window::on_window_hit, this) < 0)
83 BOOST_THROW_EXCEPTION(std::runtime_error("Failed to register for window hit test"));
86 SDL_VERSION(&info.version);
87 SDL_GetWindowWMInfo(window_, &info);
88 switch (info.subsystem) {
89 #if defined(X11_SUPPORT)
91 native_display_ = static_cast<EGLNativeDisplayType>(info.info.x11.display);
92 native_window_ = static_cast<EGLNativeWindowType>(info.info.x11.window);
95 #if defined(WAYLAND_SUPPORT)
96 case SDL_SYSWM_WAYLAND:
97 native_display_ = reinterpret_cast<EGLNativeDisplayType>(info.info.wl.display);
98 native_window_ = reinterpret_cast<EGLNativeWindowType>(info.info.wl.surface);
101 #if defined(MIR_SUPPORT)
102 case SDL_SYSWM_MIR: {
103 native_display_ = static_cast<EGLNativeDisplayType>(mir_connection_get_egl_native_display(info.info.mir.connection));
104 auto buffer_stream = mir_surface_get_buffer_stream(info.info.mir.surface);
105 native_window_ = reinterpret_cast<EGLNativeWindowType>(mir_buffer_stream_get_egl_native_window(buffer_stream));
110 ERROR("Unknown subsystem (%d)", info.subsystem);
111 BOOST_THROW_EXCEPTION(std::runtime_error("SDL subsystem not supported"));
114 SDL_ShowWindow(window_);
118 if (window_) SDL_DestroyWindow(window_);
121 SDL_HitTestResult Window::on_window_hit(SDL_Window *window, const SDL_Point *pt, void *data) {
122 auto platform_window = reinterpret_cast<Window*>(data);
125 SDL_GetWindowSize(window, &w, &h);
127 const auto border_size = graphics::dp_to_pixel(window_resize_border);
128 const auto top_drag_area_height = graphics::dp_to_pixel(top_drag_area);
129 const auto button_area_width = graphics::dp_to_pixel(button_size + button_padding * 2 + button_margin * 2);
130 const auto flags = SDL_GetWindowFlags(window);
132 if (flags & SDL_WINDOW_FULLSCREEN)
133 return SDL_HITTEST_NORMAL;
135 if (!(flags & SDL_WINDOW_RESIZABLE)) {
136 if (pt->y < border_size)
137 return SDL_HITTEST_DRAGGABLE;
139 return SDL_HITTEST_NORMAL;
142 if (pt->y < top_drag_area_height) {
143 if (pt->x > w - button_area_width && pt->x < w) {
144 platform_window->close();
145 return SDL_HITTEST_NORMAL;
146 } else if (pt->x > w - button_area_width * 2 && pt->x < w - button_area_width) {
147 platform_window->switch_window_state();
148 return SDL_HITTEST_NORMAL;
150 return SDL_HITTEST_DRAGGABLE;
153 if (flags & SDL_WINDOW_MAXIMIZED)
154 return SDL_HITTEST_NORMAL;
156 if (pt->x < border_size && pt->y < border_size)
157 return SDL_HITTEST_RESIZE_TOPLEFT;
158 else if (pt->x > border_size && pt->x < w - border_size && pt->y < border_size)
159 return SDL_HITTEST_RESIZE_TOP;
160 else if (pt->x > w - border_size && pt->y < border_size)
161 return SDL_HITTEST_RESIZE_TOPRIGHT;
162 else if (pt->x > w - border_size && pt->y > border_size && pt->y < h - border_size)
163 return SDL_HITTEST_RESIZE_RIGHT;
164 else if (pt->x > w - border_size && pt->y > h - border_size)
165 return SDL_HITTEST_RESIZE_BOTTOMRIGHT;
166 else if (pt->x < w - border_size && pt->x > border_size && pt->y > h - border_size)
167 return SDL_HITTEST_RESIZE_BOTTOM;
168 else if (pt->x < border_size && pt->y > h - border_size)
169 return SDL_HITTEST_RESIZE_BOTTOMLEFT;
170 else if (pt->x < border_size && pt->y < h - border_size && pt->y > border_size)
171 return SDL_HITTEST_RESIZE_LEFT;
173 return SDL_HITTEST_NORMAL;
176 void Window::close() {
178 observer_->window_deleted(id_);
181 void Window::switch_window_state() {
182 const auto flags = SDL_GetWindowFlags(window_);
183 if (flags & SDL_WINDOW_MAXIMIZED)
184 SDL_RestoreWindow(window_);
186 SDL_MaximizeWindow(window_);
189 void Window::process_event(const SDL_Event &event) {
190 switch (event.window.event) {
191 case SDL_WINDOWEVENT_FOCUS_GAINED:
192 if (observer_) observer_->window_wants_focus(id_);
194 case SDL_WINDOWEVENT_FOCUS_LOST:
196 // Not need to listen for SDL_WINDOWEVENT_RESIZED here as the
197 // SDL_WINDOWEVENT_SIZE_CHANGED is always sent.
198 case SDL_WINDOWEVENT_SIZE_CHANGED:
200 observer_->window_resized(id_, event.window.data1, event.window.data2);
202 case SDL_WINDOWEVENT_MOVED:
204 observer_->window_moved(id_, event.window.data1, event.window.data2);
206 case SDL_WINDOWEVENT_SHOWN:
208 case SDL_WINDOWEVENT_HIDDEN:
210 case SDL_WINDOWEVENT_CLOSE:
218 EGLNativeWindowType Window::native_handle() const { return native_window_; }
220 Window::Id Window::id() const { return id_; }
222 std::uint32_t Window::window_id() const { return SDL_GetWindowID(window_); }
224 } // namespace platform