src/video/mir/SDL_mirwindow.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 02 Feb 2014 23:41:46 -0500
changeset 8153 3e43c73ac79b
child 8154 ea6b25de67b6
permissions -rw-r--r--
Added Mir video target (thanks, Brandon!).
icculus@8153
     1
/*
icculus@8153
     2
  Simple DirectMedia Layer
icculus@8153
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
icculus@8153
     4
icculus@8153
     5
  This software is provided 'as-is', without any express or implied
icculus@8153
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@8153
     7
  arising from the use of this software.
icculus@8153
     8
icculus@8153
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@8153
    10
  including commercial applications, and to alter it and redistribute it
icculus@8153
    11
  freely, subject to the following restrictions:
icculus@8153
    12
icculus@8153
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@8153
    14
     claim that you wrote the original software. If you use this software
icculus@8153
    15
     in a product, an acknowledgment in the product documentation would be
icculus@8153
    16
     appreciated but is not required.
icculus@8153
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@8153
    18
     misrepresented as being the original software.
icculus@8153
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@8153
    20
*/
icculus@8153
    21
icculus@8153
    22
/*
icculus@8153
    23
  Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
icculus@8153
    24
*/
icculus@8153
    25
icculus@8153
    26
#include "SDL_config.h"
icculus@8153
    27
icculus@8153
    28
#include "../SDL_egl_c.h"
icculus@8153
    29
#include "../SDL_sysvideo.h"
icculus@8153
    30
icculus@8153
    31
#include "SDL_mirevents.h"
icculus@8153
    32
#include "SDL_mirwindow.h"
icculus@8153
    33
icculus@8153
    34
int
icculus@8153
    35
IsSurfaceValid(MIR_Window* mir_window)
icculus@8153
    36
{
icculus@8153
    37
    if (!mir_surface_is_valid(mir_window->surface)) {
icculus@8153
    38
        const char* error = mir_surface_get_error_message(mir_window->surface);
icculus@8153
    39
        return SDL_SetError("Failed to created a mir surface: %s", error);
icculus@8153
    40
    }
icculus@8153
    41
icculus@8153
    42
    return 0;
icculus@8153
    43
}
icculus@8153
    44
icculus@8153
    45
MirPixelFormat
icculus@8153
    46
FindValidPixelFormat(MIR_Data* mir_data)
icculus@8153
    47
{
icculus@8153
    48
    unsigned int pf_size = 32;
icculus@8153
    49
    unsigned int valid_formats;
icculus@8153
    50
    unsigned int f;
icculus@8153
    51
icculus@8153
    52
    MirPixelFormat formats[pf_size];
icculus@8153
    53
    mir_connection_get_available_surface_formats(mir_data->connection, formats,
icculus@8153
    54
                                                 pf_size, &valid_formats);
icculus@8153
    55
icculus@8153
    56
    for (f = 0; f < valid_formats; f++) {
icculus@8153
    57
        MirPixelFormat cur_pf = formats[f];
icculus@8153
    58
icculus@8153
    59
        if (cur_pf == mir_pixel_format_abgr_8888 ||
icculus@8153
    60
            cur_pf == mir_pixel_format_xbgr_8888 ||
icculus@8153
    61
            cur_pf == mir_pixel_format_argb_8888 ||
icculus@8153
    62
            cur_pf == mir_pixel_format_xrgb_8888) {
icculus@8153
    63
icculus@8153
    64
            return cur_pf;
icculus@8153
    65
        }
icculus@8153
    66
    }
icculus@8153
    67
icculus@8153
    68
    return mir_pixel_format_invalid;
icculus@8153
    69
}
icculus@8153
    70
icculus@8153
    71
int
icculus@8153
    72
MIR_CreateWindow(_THIS, SDL_Window* window)
icculus@8153
    73
{
icculus@8153
    74
    MIR_Window* mir_window;
icculus@8153
    75
    MIR_Data* mir_data;
icculus@8153
    76
icculus@8153
    77
    MirSurfaceParameters surfaceparm =
icculus@8153
    78
    {
icculus@8153
    79
        .name = "MirSurface",
icculus@8153
    80
        .width = window->w,
icculus@8153
    81
        .height = window->h,
icculus@8153
    82
        .pixel_format = mir_pixel_format_invalid,
icculus@8153
    83
        .buffer_usage = mir_buffer_usage_hardware
icculus@8153
    84
    };
icculus@8153
    85
icculus@8153
    86
    MirEventDelegate delegate = {
icculus@8153
    87
        MIR_HandleInput,
icculus@8153
    88
        window
icculus@8153
    89
    };
icculus@8153
    90
icculus@8153
    91
    mir_window = SDL_calloc(1, sizeof(MIR_Window));
icculus@8153
    92
    if (!mir_window)
icculus@8153
    93
        return SDL_OutOfMemory();
icculus@8153
    94
icculus@8153
    95
    mir_data = _this->driverdata;
icculus@8153
    96
    window->driverdata = mir_window;
icculus@8153
    97
icculus@8153
    98
    if (window->x == SDL_WINDOWPOS_UNDEFINED)
icculus@8153
    99
        window->x = 0;
icculus@8153
   100
icculus@8153
   101
    if (window->y == SDL_WINDOWPOS_UNDEFINED)
icculus@8153
   102
        window->y = 0;
icculus@8153
   103
icculus@8153
   104
    mir_window->mir_data = mir_data;
icculus@8153
   105
    mir_window->sdl_window = window;
icculus@8153
   106
icculus@8153
   107
    surfaceparm.pixel_format = FindValidPixelFormat(mir_data);
icculus@8153
   108
    if (surfaceparm.pixel_format == mir_pixel_format_invalid) {
icculus@8153
   109
        return SDL_SetError("Failed to find a valid pixel format.");
icculus@8153
   110
    }
icculus@8153
   111
icculus@8153
   112
    mir_window->surface = mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
icculus@8153
   113
    if (!mir_surface_is_valid(mir_window->surface)) {
icculus@8153
   114
        const char* error = mir_surface_get_error_message(mir_window->surface);
icculus@8153
   115
        return SDL_SetError("Failed to created a mir surface: %s", error);
icculus@8153
   116
    }
icculus@8153
   117
icculus@8153
   118
    if (window->flags & SDL_WINDOW_OPENGL) {
icculus@8153
   119
        EGLNativeWindowType egl_native_window =
icculus@8153
   120
                        (EGLNativeWindowType)mir_surface_get_egl_native_window(mir_window->surface);
icculus@8153
   121
icculus@8153
   122
        mir_window->egl_surface = SDL_EGL_CreateSurface(_this, egl_native_window);
icculus@8153
   123
icculus@8153
   124
        if (mir_window->egl_surface == EGL_NO_SURFACE) {
icculus@8153
   125
            return SDL_SetError("Failed to created a window surface %p",
icculus@8153
   126
                                _this->egl_data->egl_display);
icculus@8153
   127
        }
icculus@8153
   128
    }
icculus@8153
   129
    else {
icculus@8153
   130
        mir_window->egl_surface = EGL_NO_SURFACE;
icculus@8153
   131
    }
icculus@8153
   132
icculus@8153
   133
    mir_surface_set_event_handler(mir_window->surface, &delegate);
icculus@8153
   134
icculus@8153
   135
    return 0;
icculus@8153
   136
}
icculus@8153
   137
icculus@8153
   138
void
icculus@8153
   139
MIR_DestroyWindow(_THIS, SDL_Window* window)
icculus@8153
   140
{
icculus@8153
   141
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
   142
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   143
icculus@8153
   144
    window->driverdata = NULL;
icculus@8153
   145
icculus@8153
   146
    if (mir_data) {
icculus@8153
   147
        SDL_EGL_DestroySurface(_this, mir_window->egl_surface);
icculus@8153
   148
        mir_surface_release_sync(mir_window->surface);
icculus@8153
   149
icculus@8153
   150
        SDL_free(mir_window);
icculus@8153
   151
    }
icculus@8153
   152
}
icculus@8153
   153
icculus@8153
   154
SDL_bool
icculus@8153
   155
MIR_GetWindowWMInfo(_THIS, SDL_Window* window, SDL_SysWMinfo* info)
icculus@8153
   156
{
icculus@8153
   157
    if (info->version.major == SDL_MAJOR_VERSION &&
icculus@8153
   158
        info->version.minor == SDL_MINOR_VERSION) {
icculus@8153
   159
icculus@8153
   160
        info->subsystem = SDL_SYSWM_MIR;
icculus@8153
   161
        return SDL_TRUE;
icculus@8153
   162
    }
icculus@8153
   163
icculus@8153
   164
    return SDL_FALSE;
icculus@8153
   165
}
icculus@8153
   166
icculus@8153
   167
void
icculus@8153
   168
MIR_SetWindowFullscreen(_THIS, SDL_Window* window,
icculus@8153
   169
                        SDL_VideoDisplay* display,
icculus@8153
   170
                        SDL_bool fullscreen)
icculus@8153
   171
{
icculus@8153
   172
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   173
icculus@8153
   174
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   175
        return;
icculus@8153
   176
icculus@8153
   177
    if (fullscreen) {
icculus@8153
   178
        mir_surface_set_type(mir_window->surface, mir_surface_state_fullscreen);
icculus@8153
   179
    }
icculus@8153
   180
    else {
icculus@8153
   181
        mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
icculus@8153
   182
    }
icculus@8153
   183
}
icculus@8153
   184
icculus@8153
   185
void
icculus@8153
   186
MIR_MaximizeWindow(_THIS, SDL_Window* window)
icculus@8153
   187
{
icculus@8153
   188
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   189
icculus@8153
   190
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   191
        return;
icculus@8153
   192
icculus@8153
   193
    mir_surface_set_type(mir_window->surface, mir_surface_state_maximized);
icculus@8153
   194
}
icculus@8153
   195
icculus@8153
   196
void
icculus@8153
   197
MIR_MinimizeWindow(_THIS, SDL_Window* window)
icculus@8153
   198
{
icculus@8153
   199
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   200
icculus@8153
   201
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   202
        return;
icculus@8153
   203
icculus@8153
   204
    mir_surface_set_type(mir_window->surface, mir_surface_state_minimized);
icculus@8153
   205
}
icculus@8153
   206
icculus@8153
   207
void
icculus@8153
   208
MIR_RestoreWindow(_THIS, SDL_Window * window)
icculus@8153
   209
{
icculus@8153
   210
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   211
icculus@8153
   212
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   213
        return;
icculus@8153
   214
icculus@8153
   215
    mir_surface_set_type(mir_window->surface, mir_surface_state_restored);
icculus@8153
   216
}