src/video/mir/SDL_mirwindow.c
changeset 8153 3e43c73ac79b
child 8154 ea6b25de67b6
equal deleted inserted replaced
8152:8f8e4958651c 8153:3e43c73ac79b
       
     1 /*
       
     2   Simple DirectMedia Layer
       
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
       
     4 
       
     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.
       
     8 
       
     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:
       
    12 
       
    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.
       
    20 */
       
    21 
       
    22 /*
       
    23   Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
       
    24 */
       
    25 
       
    26 #include "SDL_config.h"
       
    27 
       
    28 #include "../SDL_egl_c.h"
       
    29 #include "../SDL_sysvideo.h"
       
    30 
       
    31 #include "SDL_mirevents.h"
       
    32 #include "SDL_mirwindow.h"
       
    33 
       
    34 int
       
    35 IsSurfaceValid(MIR_Window* mir_window)
       
    36 {
       
    37     if (!mir_surface_is_valid(mir_window->surface)) {
       
    38         const char* error = mir_surface_get_error_message(mir_window->surface);
       
    39         return SDL_SetError("Failed to created a mir surface: %s", error);
       
    40     }
       
    41 
       
    42     return 0;
       
    43 }
       
    44 
       
    45 MirPixelFormat
       
    46 FindValidPixelFormat(MIR_Data* mir_data)
       
    47 {
       
    48     unsigned int pf_size = 32;
       
    49     unsigned int valid_formats;
       
    50     unsigned int f;
       
    51 
       
    52     MirPixelFormat formats[pf_size];
       
    53     mir_connection_get_available_surface_formats(mir_data->connection, formats,
       
    54                                                  pf_size, &valid_formats);
       
    55 
       
    56     for (f = 0; f < valid_formats; f++) {
       
    57         MirPixelFormat cur_pf = formats[f];
       
    58 
       
    59         if (cur_pf == mir_pixel_format_abgr_8888 ||
       
    60             cur_pf == mir_pixel_format_xbgr_8888 ||
       
    61             cur_pf == mir_pixel_format_argb_8888 ||
       
    62             cur_pf == mir_pixel_format_xrgb_8888) {
       
    63 
       
    64             return cur_pf;
       
    65         }
       
    66     }
       
    67 
       
    68     return mir_pixel_format_invalid;
       
    69 }
       
    70 
       
    71 int
       
    72 MIR_CreateWindow(_THIS, SDL_Window* window)
       
    73 {
       
    74     MIR_Window* mir_window;
       
    75     MIR_Data* mir_data;
       
    76 
       
    77     MirSurfaceParameters surfaceparm =
       
    78     {
       
    79         .name = "MirSurface",
       
    80         .width = window->w,
       
    81         .height = window->h,
       
    82         .pixel_format = mir_pixel_format_invalid,
       
    83         .buffer_usage = mir_buffer_usage_hardware
       
    84     };
       
    85 
       
    86     MirEventDelegate delegate = {
       
    87         MIR_HandleInput,
       
    88         window
       
    89     };
       
    90 
       
    91     mir_window = SDL_calloc(1, sizeof(MIR_Window));
       
    92     if (!mir_window)
       
    93         return SDL_OutOfMemory();
       
    94 
       
    95     mir_data = _this->driverdata;
       
    96     window->driverdata = mir_window;
       
    97 
       
    98     if (window->x == SDL_WINDOWPOS_UNDEFINED)
       
    99         window->x = 0;
       
   100 
       
   101     if (window->y == SDL_WINDOWPOS_UNDEFINED)
       
   102         window->y = 0;
       
   103 
       
   104     mir_window->mir_data = mir_data;
       
   105     mir_window->sdl_window = window;
       
   106 
       
   107     surfaceparm.pixel_format = FindValidPixelFormat(mir_data);
       
   108     if (surfaceparm.pixel_format == mir_pixel_format_invalid) {
       
   109         return SDL_SetError("Failed to find a valid pixel format.");
       
   110     }
       
   111 
       
   112     mir_window->surface = mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
       
   113     if (!mir_surface_is_valid(mir_window->surface)) {
       
   114         const char* error = mir_surface_get_error_message(mir_window->surface);
       
   115         return SDL_SetError("Failed to created a mir surface: %s", error);
       
   116     }
       
   117 
       
   118     if (window->flags & SDL_WINDOW_OPENGL) {
       
   119         EGLNativeWindowType egl_native_window =
       
   120                         (EGLNativeWindowType)mir_surface_get_egl_native_window(mir_window->surface);
       
   121 
       
   122         mir_window->egl_surface = SDL_EGL_CreateSurface(_this, egl_native_window);
       
   123 
       
   124         if (mir_window->egl_surface == EGL_NO_SURFACE) {
       
   125             return SDL_SetError("Failed to created a window surface %p",
       
   126                                 _this->egl_data->egl_display);
       
   127         }
       
   128     }
       
   129     else {
       
   130         mir_window->egl_surface = EGL_NO_SURFACE;
       
   131     }
       
   132 
       
   133     mir_surface_set_event_handler(mir_window->surface, &delegate);
       
   134 
       
   135     return 0;
       
   136 }
       
   137 
       
   138 void
       
   139 MIR_DestroyWindow(_THIS, SDL_Window* window)
       
   140 {
       
   141     MIR_Data* mir_data = _this->driverdata;
       
   142     MIR_Window* mir_window = window->driverdata;
       
   143 
       
   144     window->driverdata = NULL;
       
   145 
       
   146     if (mir_data) {
       
   147         SDL_EGL_DestroySurface(_this, mir_window->egl_surface);
       
   148         mir_surface_release_sync(mir_window->surface);
       
   149 
       
   150         SDL_free(mir_window);
       
   151     }
       
   152 }
       
   153 
       
   154 SDL_bool
       
   155 MIR_GetWindowWMInfo(_THIS, SDL_Window* window, SDL_SysWMinfo* info)
       
   156 {
       
   157     if (info->version.major == SDL_MAJOR_VERSION &&
       
   158         info->version.minor == SDL_MINOR_VERSION) {
       
   159 
       
   160         info->subsystem = SDL_SYSWM_MIR;
       
   161         return SDL_TRUE;
       
   162     }
       
   163 
       
   164     return SDL_FALSE;
       
   165 }
       
   166 
       
   167 void
       
   168 MIR_SetWindowFullscreen(_THIS, SDL_Window* window,
       
   169                         SDL_VideoDisplay* display,
       
   170                         SDL_bool fullscreen)
       
   171 {
       
   172     MIR_Window* mir_window = window->driverdata;
       
   173 
       
   174     if (IsSurfaceValid(mir_window) < 0)
       
   175         return;
       
   176 
       
   177     if (fullscreen) {
       
   178         mir_surface_set_type(mir_window->surface, mir_surface_state_fullscreen);
       
   179     }
       
   180     else {
       
   181         mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
       
   182     }
       
   183 }
       
   184 
       
   185 void
       
   186 MIR_MaximizeWindow(_THIS, SDL_Window* window)
       
   187 {
       
   188     MIR_Window* mir_window = window->driverdata;
       
   189 
       
   190     if (IsSurfaceValid(mir_window) < 0)
       
   191         return;
       
   192 
       
   193     mir_surface_set_type(mir_window->surface, mir_surface_state_maximized);
       
   194 }
       
   195 
       
   196 void
       
   197 MIR_MinimizeWindow(_THIS, SDL_Window* window)
       
   198 {
       
   199     MIR_Window* mir_window = window->driverdata;
       
   200 
       
   201     if (IsSurfaceValid(mir_window) < 0)
       
   202         return;
       
   203 
       
   204     mir_surface_set_type(mir_window->surface, mir_surface_state_minimized);
       
   205 }
       
   206 
       
   207 void
       
   208 MIR_RestoreWindow(_THIS, SDL_Window * window)
       
   209 {
       
   210     MIR_Window* mir_window = window->driverdata;
       
   211 
       
   212     if (IsSurfaceValid(mir_window) < 0)
       
   213         return;
       
   214 
       
   215     mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
       
   216 }