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