src/video/mir/SDL_mirwindow.c
author bschaefer
Sun, 21 Feb 2016 15:19:35 -0800
changeset 10089 25fda20d0173
parent 9998 f67cf37e9cd4
child 10095 0897c3accd52
permissions -rw-r--r--
Fix API/ABI breakage in Mir 0.13/0.14.
icculus@8153
     1
/*
icculus@8153
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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
int
icculus@8153
    50
MIR_CreateWindow(_THIS, SDL_Window* window)
icculus@8153
    51
{
icculus@8153
    52
    MIR_Window* mir_window;
icculus@8153
    53
    MIR_Data* mir_data;
bschaefer@10089
    54
    MirPixelFormat pixel_format;
bschaefer@10089
    55
    MirBufferUsage buffer_usage;
icculus@8153
    56
bschaefer@10089
    57
    MirSurfaceSpec* spec;
icculus@8153
    58
icculus@8153
    59
    mir_window = SDL_calloc(1, sizeof(MIR_Window));
icculus@8153
    60
    if (!mir_window)
icculus@8153
    61
        return SDL_OutOfMemory();
icculus@8153
    62
icculus@8153
    63
    mir_data = _this->driverdata;
icculus@8153
    64
    window->driverdata = mir_window;
icculus@8153
    65
icculus@8153
    66
    if (window->x == SDL_WINDOWPOS_UNDEFINED)
icculus@8153
    67
        window->x = 0;
icculus@8153
    68
icculus@8153
    69
    if (window->y == SDL_WINDOWPOS_UNDEFINED)
icculus@8153
    70
        window->y = 0;
icculus@8153
    71
icculus@8153
    72
    mir_window->mir_data = mir_data;
icculus@8153
    73
    mir_window->sdl_window = window;
icculus@8153
    74
bschaefer@10089
    75
    pixel_format = MIR_mir_connection_get_egl_pixel_format(mir_data->connection,
bschaefer@10089
    76
                                                           _this->egl_data->egl_display,
bschaefer@10089
    77
                                                           _this->egl_data->egl_config);
bschaefer@10089
    78
bschaefer@10089
    79
    mir_data->pixel_format = pixel_format;
bschaefer@10089
    80
    if (pixel_format == mir_pixel_format_invalid) {
icculus@8153
    81
        return SDL_SetError("Failed to find a valid pixel format.");
icculus@8153
    82
    }
icculus@8153
    83
bschaefer@10089
    84
    buffer_usage = mir_buffer_usage_hardware;
bschaefer@10089
    85
    if (mir_data->software)
bschaefer@10089
    86
        buffer_usage = mir_buffer_usage_software;
bschaefer@10089
    87
bschaefer@10089
    88
    spec = MIR_mir_connection_create_spec_for_normal_surface(mir_data->connection,
bschaefer@10089
    89
                                                             window->w,
bschaefer@10089
    90
                                                             window->h,
bschaefer@10089
    91
                                                             pixel_format);
bschaefer@10089
    92
bschaefer@10089
    93
    MIR_mir_surface_spec_set_buffer_usage(spec, buffer_usage);
bschaefer@10089
    94
    MIR_mir_surface_spec_set_name(spec, "Mir surface");
bschaefer@10089
    95
bschaefer@10089
    96
    mir_window->surface = MIR_mir_surface_create_sync(spec);
bschaefer@10089
    97
    MIR_mir_surface_set_event_handler(mir_window->surface, MIR_HandleEvent, window);
bschaefer@10089
    98
bschaefer@10089
    99
    MIR_mir_surface_spec_release(spec);
bschaefer@10089
   100
icculus@8159
   101
    if (!MIR_mir_surface_is_valid(mir_window->surface)) {
icculus@8159
   102
        const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
icculus@8153
   103
        return SDL_SetError("Failed to created a mir surface: %s", error);
icculus@8153
   104
    }
icculus@8153
   105
icculus@8153
   106
    if (window->flags & SDL_WINDOW_OPENGL) {
icculus@8153
   107
        EGLNativeWindowType egl_native_window =
bschaefer@10089
   108
                        (EGLNativeWindowType)MIR_mir_buffer_stream_get_egl_native_window(
bschaefer@10089
   109
                                                       MIR_mir_surface_get_buffer_stream(mir_window->surface));
icculus@8153
   110
icculus@8153
   111
        mir_window->egl_surface = SDL_EGL_CreateSurface(_this, egl_native_window);
icculus@8153
   112
icculus@8153
   113
        if (mir_window->egl_surface == EGL_NO_SURFACE) {
icculus@8153
   114
            return SDL_SetError("Failed to created a window surface %p",
icculus@8153
   115
                                _this->egl_data->egl_display);
icculus@8153
   116
        }
icculus@8153
   117
    }
icculus@8153
   118
    else {
icculus@8153
   119
        mir_window->egl_surface = EGL_NO_SURFACE;
icculus@8153
   120
    }
icculus@8153
   121
bschaefer@10089
   122
    mir_data->current_window = mir_window;
icculus@8153
   123
icculus@8153
   124
    return 0;
icculus@8153
   125
}
icculus@8153
   126
icculus@8153
   127
void
icculus@8153
   128
MIR_DestroyWindow(_THIS, SDL_Window* window)
icculus@8153
   129
{
bschaefer@10089
   130
    MIR_Data* mir_data     = _this->driverdata;
icculus@8153
   131
    MIR_Window* mir_window = window->driverdata;
icculus@8153
   132
icculus@8153
   133
    if (mir_data) {
icculus@8153
   134
        SDL_EGL_DestroySurface(_this, mir_window->egl_surface);
icculus@8159
   135
        MIR_mir_surface_release_sync(mir_window->surface);
icculus@8153
   136
bschaefer@10089
   137
        mir_data->current_window = NULL;
bschaefer@10089
   138
icculus@8153
   139
        SDL_free(mir_window);
icculus@8153
   140
    }
slouken@8978
   141
    window->driverdata = NULL;
icculus@8153
   142
}
icculus@8153
   143
icculus@8153
   144
SDL_bool
icculus@8153
   145
MIR_GetWindowWMInfo(_THIS, SDL_Window* window, SDL_SysWMinfo* info)
icculus@8153
   146
{
icculus@8153
   147
    if (info->version.major == SDL_MAJOR_VERSION &&
icculus@8153
   148
        info->version.minor == SDL_MINOR_VERSION) {
icculus@8155
   149
        MIR_Window* mir_window = window->driverdata;
icculus@8153
   150
icculus@8153
   151
        info->subsystem = SDL_SYSWM_MIR;
icculus@8155
   152
        info->info.mir.connection = mir_window->mir_data->connection;
icculus@8155
   153
        info->info.mir.surface = mir_window->surface;
icculus@8155
   154
icculus@8153
   155
        return SDL_TRUE;
icculus@8153
   156
    }
icculus@8153
   157
icculus@8153
   158
    return SDL_FALSE;
icculus@8153
   159
}
icculus@8153
   160
icculus@8153
   161
void
icculus@8153
   162
MIR_SetWindowFullscreen(_THIS, SDL_Window* window,
icculus@8153
   163
                        SDL_VideoDisplay* display,
icculus@8153
   164
                        SDL_bool fullscreen)
icculus@8153
   165
{
bschaefer@10089
   166
    MIR_Data*   mir_data   = _this->driverdata;
icculus@8153
   167
    MIR_Window* mir_window = window->driverdata;
bschaefer@10089
   168
    MirSurfaceSpec* spec;
bschaefer@10089
   169
    MirSurfaceState state;
icculus@8153
   170
icculus@8153
   171
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   172
        return;
icculus@8153
   173
icculus@8153
   174
    if (fullscreen) {
bschaefer@10089
   175
        state = mir_surface_state_fullscreen;
icculus@8159
   176
    } else {
bschaefer@10089
   177
        state = mir_surface_state_restored;
icculus@8153
   178
    }
bschaefer@10089
   179
bschaefer@10089
   180
    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
bschaefer@10089
   181
    MIR_mir_surface_spec_set_state(spec, state);
bschaefer@10089
   182
bschaefer@10089
   183
    MIR_mir_surface_apply_spec(mir_window->surface, spec);
bschaefer@10089
   184
    MIR_mir_surface_spec_release(spec);
icculus@8153
   185
}
icculus@8153
   186
icculus@8153
   187
void
icculus@8153
   188
MIR_MaximizeWindow(_THIS, SDL_Window* window)
icculus@8153
   189
{
bschaefer@10089
   190
    MIR_Data*   mir_data   = _this->driverdata;
icculus@8153
   191
    MIR_Window* mir_window = window->driverdata;
bschaefer@10089
   192
    MirSurfaceSpec* spec;
icculus@8153
   193
icculus@8153
   194
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   195
        return;
icculus@8153
   196
bschaefer@10089
   197
    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
bschaefer@10089
   198
    MIR_mir_surface_spec_set_state(spec, mir_surface_state_maximized);
bschaefer@10089
   199
bschaefer@10089
   200
    MIR_mir_surface_apply_spec(mir_window->surface, spec);
bschaefer@10089
   201
    MIR_mir_surface_spec_release(spec);
icculus@8153
   202
}
icculus@8153
   203
icculus@8153
   204
void
icculus@8153
   205
MIR_MinimizeWindow(_THIS, SDL_Window* window)
icculus@8153
   206
{
bschaefer@10089
   207
    MIR_Data*   mir_data   = _this->driverdata;
icculus@8153
   208
    MIR_Window* mir_window = window->driverdata;
bschaefer@10089
   209
    MirSurfaceSpec* spec;
icculus@8153
   210
icculus@8153
   211
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   212
        return;
icculus@8153
   213
bschaefer@10089
   214
    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
bschaefer@10089
   215
    MIR_mir_surface_spec_set_state(spec, mir_surface_state_minimized);
bschaefer@10089
   216
bschaefer@10089
   217
    MIR_mir_surface_apply_spec(mir_window->surface, spec);
bschaefer@10089
   218
    MIR_mir_surface_spec_release(spec);
icculus@8153
   219
}
icculus@8153
   220
icculus@8153
   221
void
icculus@8153
   222
MIR_RestoreWindow(_THIS, SDL_Window * window)
icculus@8153
   223
{
bschaefer@10089
   224
    MIR_Data*   mir_data   = _this->driverdata;
icculus@8153
   225
    MIR_Window* mir_window = window->driverdata;
bschaefer@10089
   226
    MirSurfaceSpec* spec;
icculus@8153
   227
icculus@8153
   228
    if (IsSurfaceValid(mir_window) < 0)
icculus@8153
   229
        return;
icculus@8153
   230
bschaefer@10089
   231
    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
bschaefer@10089
   232
    MIR_mir_surface_spec_set_state(spec, mir_surface_state_restored);
bschaefer@10089
   233
bschaefer@10089
   234
    MIR_mir_surface_apply_spec(mir_window->surface, spec);
bschaefer@10089
   235
    MIR_mir_surface_spec_release(spec);
bschaefer@10089
   236
}
bschaefer@10089
   237
bschaefer@10089
   238
void
bschaefer@10089
   239
MIR_HideWindow(_THIS, SDL_Window* window)
bschaefer@10089
   240
{
bschaefer@10089
   241
    MIR_Data*   mir_data   = _this->driverdata;
bschaefer@10089
   242
    MIR_Window* mir_window = window->driverdata;
bschaefer@10089
   243
    MirSurfaceSpec* spec;
bschaefer@10089
   244
bschaefer@10089
   245
    if (IsSurfaceValid(mir_window) < 0)
bschaefer@10089
   246
        return;
bschaefer@10089
   247
bschaefer@10089
   248
    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
bschaefer@10089
   249
    MIR_mir_surface_spec_set_state(spec, mir_surface_state_hidden);
bschaefer@10089
   250
bschaefer@10089
   251
    MIR_mir_surface_apply_spec(mir_window->surface, spec);
bschaefer@10089
   252
    MIR_mir_surface_spec_release(spec);
bschaefer@10089
   253
}
bschaefer@10089
   254
bschaefer@10089
   255
void
bschaefer@10089
   256
MIR_SetWindowSize(_THIS, SDL_Window* window)
bschaefer@10089
   257
{
bschaefer@10089
   258
    MIR_Data*   mir_data   = _this->driverdata;
bschaefer@10089
   259
    MIR_Window* mir_window = window->driverdata;
bschaefer@10089
   260
    MirSurfaceSpec* spec;
bschaefer@10089
   261
bschaefer@10089
   262
    if (IsSurfaceValid(mir_window) < 0)
bschaefer@10089
   263
        return;
bschaefer@10089
   264
bschaefer@10089
   265
    /* You cannot set the x/y of a mir window! So only update w/h */
bschaefer@10089
   266
    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
bschaefer@10089
   267
    MIR_mir_surface_spec_set_width (spec, window->w);
bschaefer@10089
   268
    MIR_mir_surface_spec_set_height(spec, window->h);
bschaefer@10089
   269
bschaefer@10089
   270
    MIR_mir_surface_apply_spec(mir_window->surface, spec);
bschaefer@10089
   271
    MIR_mir_surface_spec_release(spec);
bschaefer@10089
   272
}
bschaefer@10089
   273
bschaefer@10089
   274
void
bschaefer@10089
   275
MIR_SetWindowMinimumSize(_THIS, SDL_Window* window)
bschaefer@10089
   276
{
bschaefer@10089
   277
    MIR_Data*   mir_data   = _this->driverdata;
bschaefer@10089
   278
    MIR_Window* mir_window = window->driverdata;
bschaefer@10089
   279
    MirSurfaceSpec* spec;
bschaefer@10089
   280
bschaefer@10089
   281
    if (IsSurfaceValid(mir_window) < 0)
bschaefer@10089
   282
        return;
bschaefer@10089
   283
bschaefer@10089
   284
    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
bschaefer@10089
   285
    MIR_mir_surface_spec_set_min_width (spec, window->min_w);
bschaefer@10089
   286
    MIR_mir_surface_spec_set_min_height(spec, window->min_h);
bschaefer@10089
   287
bschaefer@10089
   288
    MIR_mir_surface_apply_spec(mir_window->surface, spec);
bschaefer@10089
   289
    MIR_mir_surface_spec_release(spec);
bschaefer@10089
   290
}
bschaefer@10089
   291
bschaefer@10089
   292
void
bschaefer@10089
   293
MIR_SetWindowMaximumSize(_THIS, SDL_Window* window)
bschaefer@10089
   294
{
bschaefer@10089
   295
    MIR_Data*   mir_data   = _this->driverdata;
bschaefer@10089
   296
    MIR_Window* mir_window = window->driverdata;
bschaefer@10089
   297
    MirSurfaceSpec* spec;
bschaefer@10089
   298
bschaefer@10089
   299
    if (IsSurfaceValid(mir_window) < 0)
bschaefer@10089
   300
        return;
bschaefer@10089
   301
bschaefer@10089
   302
    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
bschaefer@10089
   303
    MIR_mir_surface_spec_set_max_width (spec, window->max_w);
bschaefer@10089
   304
    MIR_mir_surface_spec_set_max_height(spec, window->max_h);
bschaefer@10089
   305
bschaefer@10089
   306
    MIR_mir_surface_apply_spec(mir_window->surface, spec);
bschaefer@10089
   307
    MIR_mir_surface_spec_release(spec);
bschaefer@10089
   308
}
bschaefer@10089
   309
bschaefer@10089
   310
void
bschaefer@10089
   311
MIR_SetWindowTitle(_THIS, SDL_Window* window)
bschaefer@10089
   312
{
bschaefer@10089
   313
    MIR_Data*   mir_data   = _this->driverdata;
bschaefer@10089
   314
    MIR_Window* mir_window = window->driverdata;
bschaefer@10089
   315
    char const* title = window->title ? window->title : "";
bschaefer@10089
   316
    MirSurfaceSpec* spec;
bschaefer@10089
   317
bschaefer@10089
   318
    if (IsSurfaceValid(mir_window) < 0)
bschaefer@10089
   319
        return;
bschaefer@10089
   320
bschaefer@10089
   321
    spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
bschaefer@10089
   322
    MIR_mir_surface_spec_set_name(spec, title);
bschaefer@10089
   323
bschaefer@10089
   324
    MIR_mir_surface_apply_spec(mir_window->surface, spec);
bschaefer@10089
   325
    MIR_mir_surface_spec_release(spec);
icculus@8153
   326
}
icculus@8154
   327
icculus@8154
   328
#endif /* SDL_VIDEO_DRIVER_MIR */
icculus@8154
   329
icculus@8154
   330
/* vi: set ts=4 sw=4 expandtab: */