2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
23 Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
26 #include "../../SDL_internal.h"
28 #if SDL_VIDEO_DRIVER_MIR
30 #include "../SDL_egl_c.h"
31 #include "../SDL_sysvideo.h"
33 #include "SDL_mirevents.h"
34 #include "SDL_mirwindow.h"
36 #include "SDL_mirdyn.h"
39 IsSurfaceValid(MIR_Window* mir_window)
41 if (!MIR_mir_surface_is_valid(mir_window->surface)) {
42 const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
43 return SDL_SetError("Failed to created a mir surface: %s", error);
50 FindValidPixelFormat(MIR_Data* mir_data)
52 unsigned int pf_size = 32;
53 unsigned int valid_formats;
56 MirPixelFormat formats[pf_size];
57 MIR_mir_connection_get_available_surface_formats(mir_data->connection, formats,
58 pf_size, &valid_formats);
60 for (f = 0; f < valid_formats; f++) {
61 MirPixelFormat cur_pf = formats[f];
63 if (cur_pf == mir_pixel_format_abgr_8888 ||
64 cur_pf == mir_pixel_format_xbgr_8888 ||
65 cur_pf == mir_pixel_format_argb_8888 ||
66 cur_pf == mir_pixel_format_xrgb_8888) {
72 return mir_pixel_format_invalid;
76 MIR_CreateWindow(_THIS, SDL_Window* window)
78 MIR_Window* mir_window;
81 MirSurfaceParameters surfaceparm =
86 .pixel_format = mir_pixel_format_invalid,
87 .buffer_usage = mir_buffer_usage_hardware,
88 .output_id = mir_display_output_id_invalid
91 MirEventDelegate delegate = {
96 mir_window = SDL_calloc(1, sizeof(MIR_Window));
98 return SDL_OutOfMemory();
100 mir_data = _this->driverdata;
101 window->driverdata = mir_window;
103 if (mir_data->software)
104 surfaceparm.buffer_usage = mir_buffer_usage_software;
106 if (window->x == SDL_WINDOWPOS_UNDEFINED)
109 if (window->y == SDL_WINDOWPOS_UNDEFINED)
112 mir_window->mir_data = mir_data;
113 mir_window->sdl_window = window;
115 surfaceparm.pixel_format = FindValidPixelFormat(mir_data);
116 if (surfaceparm.pixel_format == mir_pixel_format_invalid) {
117 return SDL_SetError("Failed to find a valid pixel format.");
120 mir_window->surface = MIR_mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
121 if (!MIR_mir_surface_is_valid(mir_window->surface)) {
122 const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
123 return SDL_SetError("Failed to created a mir surface: %s", error);
126 if (window->flags & SDL_WINDOW_OPENGL) {
127 EGLNativeWindowType egl_native_window =
128 (EGLNativeWindowType)MIR_mir_surface_get_egl_native_window(mir_window->surface);
130 mir_window->egl_surface = SDL_EGL_CreateSurface(_this, egl_native_window);
132 if (mir_window->egl_surface == EGL_NO_SURFACE) {
133 return SDL_SetError("Failed to created a window surface %p",
134 _this->egl_data->egl_display);
138 mir_window->egl_surface = EGL_NO_SURFACE;
141 MIR_mir_surface_set_event_handler(mir_window->surface, &delegate);
147 MIR_DestroyWindow(_THIS, SDL_Window* window)
149 MIR_Data* mir_data = _this->driverdata;
150 MIR_Window* mir_window = window->driverdata;
153 SDL_EGL_DestroySurface(_this, mir_window->egl_surface);
154 MIR_mir_surface_release_sync(mir_window->surface);
156 SDL_free(mir_window);
158 window->driverdata = NULL;
162 MIR_GetWindowWMInfo(_THIS, SDL_Window* window, SDL_SysWMinfo* info)
164 if (info->version.major == SDL_MAJOR_VERSION &&
165 info->version.minor == SDL_MINOR_VERSION) {
166 MIR_Window* mir_window = window->driverdata;
168 info->subsystem = SDL_SYSWM_MIR;
169 info->info.mir.connection = mir_window->mir_data->connection;
170 info->info.mir.surface = mir_window->surface;
179 MIR_SetWindowFullscreen(_THIS, SDL_Window* window,
180 SDL_VideoDisplay* display,
183 MIR_Window* mir_window = window->driverdata;
185 if (IsSurfaceValid(mir_window) < 0)
189 MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_fullscreen);
191 MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_restored);
196 MIR_MaximizeWindow(_THIS, SDL_Window* window)
198 MIR_Window* mir_window = window->driverdata;
200 if (IsSurfaceValid(mir_window) < 0)
203 MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_maximized);
207 MIR_MinimizeWindow(_THIS, SDL_Window* window)
209 MIR_Window* mir_window = window->driverdata;
211 if (IsSurfaceValid(mir_window) < 0)
214 MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_minimized);
218 MIR_RestoreWindow(_THIS, SDL_Window * window)
220 MIR_Window* mir_window = window->driverdata;
222 if (IsSurfaceValid(mir_window) < 0)
225 MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_restored);
228 #endif /* SDL_VIDEO_DRIVER_MIR */
230 /* vi: set ts=4 sw=4 expandtab: */