src/video/mir/SDL_mirwindow.c
changeset 8153 3e43c73ac79b
child 8154 ea6b25de67b6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/mir/SDL_mirwindow.c	Sun Feb 02 23:41:46 2014 -0500
     1.3 @@ -0,0 +1,216 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/*
    1.26 +  Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
    1.27 +*/
    1.28 +
    1.29 +#include "SDL_config.h"
    1.30 +
    1.31 +#include "../SDL_egl_c.h"
    1.32 +#include "../SDL_sysvideo.h"
    1.33 +
    1.34 +#include "SDL_mirevents.h"
    1.35 +#include "SDL_mirwindow.h"
    1.36 +
    1.37 +int
    1.38 +IsSurfaceValid(MIR_Window* mir_window)
    1.39 +{
    1.40 +    if (!mir_surface_is_valid(mir_window->surface)) {
    1.41 +        const char* error = mir_surface_get_error_message(mir_window->surface);
    1.42 +        return SDL_SetError("Failed to created a mir surface: %s", error);
    1.43 +    }
    1.44 +
    1.45 +    return 0;
    1.46 +}
    1.47 +
    1.48 +MirPixelFormat
    1.49 +FindValidPixelFormat(MIR_Data* mir_data)
    1.50 +{
    1.51 +    unsigned int pf_size = 32;
    1.52 +    unsigned int valid_formats;
    1.53 +    unsigned int f;
    1.54 +
    1.55 +    MirPixelFormat formats[pf_size];
    1.56 +    mir_connection_get_available_surface_formats(mir_data->connection, formats,
    1.57 +                                                 pf_size, &valid_formats);
    1.58 +
    1.59 +    for (f = 0; f < valid_formats; f++) {
    1.60 +        MirPixelFormat cur_pf = formats[f];
    1.61 +
    1.62 +        if (cur_pf == mir_pixel_format_abgr_8888 ||
    1.63 +            cur_pf == mir_pixel_format_xbgr_8888 ||
    1.64 +            cur_pf == mir_pixel_format_argb_8888 ||
    1.65 +            cur_pf == mir_pixel_format_xrgb_8888) {
    1.66 +
    1.67 +            return cur_pf;
    1.68 +        }
    1.69 +    }
    1.70 +
    1.71 +    return mir_pixel_format_invalid;
    1.72 +}
    1.73 +
    1.74 +int
    1.75 +MIR_CreateWindow(_THIS, SDL_Window* window)
    1.76 +{
    1.77 +    MIR_Window* mir_window;
    1.78 +    MIR_Data* mir_data;
    1.79 +
    1.80 +    MirSurfaceParameters surfaceparm =
    1.81 +    {
    1.82 +        .name = "MirSurface",
    1.83 +        .width = window->w,
    1.84 +        .height = window->h,
    1.85 +        .pixel_format = mir_pixel_format_invalid,
    1.86 +        .buffer_usage = mir_buffer_usage_hardware
    1.87 +    };
    1.88 +
    1.89 +    MirEventDelegate delegate = {
    1.90 +        MIR_HandleInput,
    1.91 +        window
    1.92 +    };
    1.93 +
    1.94 +    mir_window = SDL_calloc(1, sizeof(MIR_Window));
    1.95 +    if (!mir_window)
    1.96 +        return SDL_OutOfMemory();
    1.97 +
    1.98 +    mir_data = _this->driverdata;
    1.99 +    window->driverdata = mir_window;
   1.100 +
   1.101 +    if (window->x == SDL_WINDOWPOS_UNDEFINED)
   1.102 +        window->x = 0;
   1.103 +
   1.104 +    if (window->y == SDL_WINDOWPOS_UNDEFINED)
   1.105 +        window->y = 0;
   1.106 +
   1.107 +    mir_window->mir_data = mir_data;
   1.108 +    mir_window->sdl_window = window;
   1.109 +
   1.110 +    surfaceparm.pixel_format = FindValidPixelFormat(mir_data);
   1.111 +    if (surfaceparm.pixel_format == mir_pixel_format_invalid) {
   1.112 +        return SDL_SetError("Failed to find a valid pixel format.");
   1.113 +    }
   1.114 +
   1.115 +    mir_window->surface = mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
   1.116 +    if (!mir_surface_is_valid(mir_window->surface)) {
   1.117 +        const char* error = mir_surface_get_error_message(mir_window->surface);
   1.118 +        return SDL_SetError("Failed to created a mir surface: %s", error);
   1.119 +    }
   1.120 +
   1.121 +    if (window->flags & SDL_WINDOW_OPENGL) {
   1.122 +        EGLNativeWindowType egl_native_window =
   1.123 +                        (EGLNativeWindowType)mir_surface_get_egl_native_window(mir_window->surface);
   1.124 +
   1.125 +        mir_window->egl_surface = SDL_EGL_CreateSurface(_this, egl_native_window);
   1.126 +
   1.127 +        if (mir_window->egl_surface == EGL_NO_SURFACE) {
   1.128 +            return SDL_SetError("Failed to created a window surface %p",
   1.129 +                                _this->egl_data->egl_display);
   1.130 +        }
   1.131 +    }
   1.132 +    else {
   1.133 +        mir_window->egl_surface = EGL_NO_SURFACE;
   1.134 +    }
   1.135 +
   1.136 +    mir_surface_set_event_handler(mir_window->surface, &delegate);
   1.137 +
   1.138 +    return 0;
   1.139 +}
   1.140 +
   1.141 +void
   1.142 +MIR_DestroyWindow(_THIS, SDL_Window* window)
   1.143 +{
   1.144 +    MIR_Data* mir_data = _this->driverdata;
   1.145 +    MIR_Window* mir_window = window->driverdata;
   1.146 +
   1.147 +    window->driverdata = NULL;
   1.148 +
   1.149 +    if (mir_data) {
   1.150 +        SDL_EGL_DestroySurface(_this, mir_window->egl_surface);
   1.151 +        mir_surface_release_sync(mir_window->surface);
   1.152 +
   1.153 +        SDL_free(mir_window);
   1.154 +    }
   1.155 +}
   1.156 +
   1.157 +SDL_bool
   1.158 +MIR_GetWindowWMInfo(_THIS, SDL_Window* window, SDL_SysWMinfo* info)
   1.159 +{
   1.160 +    if (info->version.major == SDL_MAJOR_VERSION &&
   1.161 +        info->version.minor == SDL_MINOR_VERSION) {
   1.162 +
   1.163 +        info->subsystem = SDL_SYSWM_MIR;
   1.164 +        return SDL_TRUE;
   1.165 +    }
   1.166 +
   1.167 +    return SDL_FALSE;
   1.168 +}
   1.169 +
   1.170 +void
   1.171 +MIR_SetWindowFullscreen(_THIS, SDL_Window* window,
   1.172 +                        SDL_VideoDisplay* display,
   1.173 +                        SDL_bool fullscreen)
   1.174 +{
   1.175 +    MIR_Window* mir_window = window->driverdata;
   1.176 +
   1.177 +    if (IsSurfaceValid(mir_window) < 0)
   1.178 +        return;
   1.179 +
   1.180 +    if (fullscreen) {
   1.181 +        mir_surface_set_type(mir_window->surface, mir_surface_state_fullscreen);
   1.182 +    }
   1.183 +    else {
   1.184 +        mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
   1.185 +    }
   1.186 +}
   1.187 +
   1.188 +void
   1.189 +MIR_MaximizeWindow(_THIS, SDL_Window* window)
   1.190 +{
   1.191 +    MIR_Window* mir_window = window->driverdata;
   1.192 +
   1.193 +    if (IsSurfaceValid(mir_window) < 0)
   1.194 +        return;
   1.195 +
   1.196 +    mir_surface_set_type(mir_window->surface, mir_surface_state_maximized);
   1.197 +}
   1.198 +
   1.199 +void
   1.200 +MIR_MinimizeWindow(_THIS, SDL_Window* window)
   1.201 +{
   1.202 +    MIR_Window* mir_window = window->driverdata;
   1.203 +
   1.204 +    if (IsSurfaceValid(mir_window) < 0)
   1.205 +        return;
   1.206 +
   1.207 +    mir_surface_set_type(mir_window->surface, mir_surface_state_minimized);
   1.208 +}
   1.209 +
   1.210 +void
   1.211 +MIR_RestoreWindow(_THIS, SDL_Window * window)
   1.212 +{
   1.213 +    MIR_Window* mir_window = window->driverdata;
   1.214 +
   1.215 +    if (IsSurfaceValid(mir_window) < 0)
   1.216 +        return;
   1.217 +
   1.218 +    mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
   1.219 +}