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