src/video/mir/SDL_mirvideo.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 10:10:34 -0800
changeset 9998 f67cf37e9cd4
parent 9619 b94b6d0bff0f
child 10089 25fda20d0173
permissions -rw-r--r--
Updated copyright to 2016
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@8154
    29
icculus@8153
    30
#include "SDL_video.h"
icculus@8153
    31
icculus@8153
    32
#include "SDL_mirframebuffer.h"
icculus@8153
    33
#include "SDL_mirmouse.h"
icculus@8153
    34
#include "SDL_miropengl.h"
icculus@8153
    35
#include "SDL_mirvideo.h"
icculus@8153
    36
#include "SDL_mirwindow.h"
icculus@8153
    37
icculus@8159
    38
#include "SDL_mirdyn.h"
icculus@8159
    39
icculus@8153
    40
#define MIR_DRIVER_NAME "mir"
icculus@8153
    41
icculus@8153
    42
static int
icculus@8153
    43
MIR_VideoInit(_THIS);
icculus@8153
    44
icculus@8153
    45
static void
icculus@8153
    46
MIR_VideoQuit(_THIS);
icculus@8153
    47
icculus@8153
    48
static int
icculus@8153
    49
MIR_GetDisplayBounds(_THIS, SDL_VideoDisplay* display, SDL_Rect* rect);
icculus@8153
    50
icculus@8153
    51
static void
icculus@8153
    52
MIR_GetDisplayModes(_THIS, SDL_VideoDisplay* sdl_display);
icculus@8153
    53
icculus@8153
    54
static int
icculus@8153
    55
MIR_SetDisplayMode(_THIS, SDL_VideoDisplay* sdl_display, SDL_DisplayMode* mode);
icculus@8153
    56
icculus@8153
    57
static SDL_WindowShaper*
icculus@8153
    58
MIR_CreateShaper(SDL_Window* window)
icculus@8153
    59
{
icculus@8153
    60
    /* FIXME Im not sure if mir support this atm, will have to come back to this */
icculus@8153
    61
    return NULL;
icculus@8153
    62
}
icculus@8153
    63
icculus@8153
    64
static int
icculus@8153
    65
MIR_SetWindowShape(SDL_WindowShaper* shaper, SDL_Surface* shape, SDL_WindowShapeMode* shape_mode)
icculus@8153
    66
{
icculus@8153
    67
    return SDL_Unsupported();
icculus@8153
    68
}
icculus@8153
    69
icculus@8153
    70
static int
icculus@8153
    71
MIR_ResizeWindowShape(SDL_Window* window)
icculus@8153
    72
{
icculus@8153
    73
    return SDL_Unsupported();
icculus@8153
    74
}
icculus@8153
    75
icculus@8153
    76
static int
icculus@8153
    77
MIR_Available()
icculus@8153
    78
{
icculus@8159
    79
    int available = 0;
icculus@8159
    80
icculus@8159
    81
    if (SDL_MIR_LoadSymbols()) {
icculus@8159
    82
        /* !!! FIXME: try to make a MirConnection here. */
icculus@8159
    83
        available = 1;
icculus@8159
    84
        SDL_MIR_UnloadSymbols();
icculus@8159
    85
    }
icculus@8159
    86
icculus@8159
    87
    return available;
icculus@8153
    88
}
icculus@8153
    89
icculus@8153
    90
static void
icculus@8153
    91
MIR_DeleteDevice(SDL_VideoDevice* device)
icculus@8153
    92
{
icculus@8153
    93
    SDL_free(device);
icculus@8159
    94
    SDL_MIR_UnloadSymbols();
icculus@8153
    95
}
icculus@8153
    96
icculus@8153
    97
void
icculus@8153
    98
MIR_PumpEvents(_THIS)
icculus@8153
    99
{
icculus@8153
   100
}
icculus@8153
   101
icculus@8153
   102
static SDL_VideoDevice*
icculus@8153
   103
MIR_CreateDevice(int device_index)
icculus@8153
   104
{
icculus@8153
   105
    MIR_Data* mir_data;
icculus@8159
   106
    SDL_VideoDevice* device = NULL;
icculus@8159
   107
icculus@8159
   108
    if (!SDL_MIR_LoadSymbols()) {
icculus@8159
   109
        return NULL;
icculus@8159
   110
    }
icculus@8159
   111
icculus@8159
   112
    device = SDL_calloc(1, sizeof(SDL_VideoDevice));
icculus@8153
   113
    if (!device) {
icculus@8159
   114
        SDL_MIR_UnloadSymbols();
icculus@8153
   115
        SDL_OutOfMemory();
icculus@8153
   116
        return NULL;
icculus@8153
   117
    }
icculus@8153
   118
icculus@8153
   119
    mir_data = SDL_calloc(1, sizeof(MIR_Data));
icculus@8153
   120
    if (!mir_data) {
icculus@8159
   121
        SDL_free(device);
icculus@8159
   122
        SDL_MIR_UnloadSymbols();
icculus@8153
   123
        SDL_OutOfMemory();
icculus@8153
   124
        return NULL;
icculus@8153
   125
    }
icculus@8153
   126
icculus@8153
   127
    device->driverdata = mir_data;
icculus@8153
   128
icculus@8153
   129
    /* mirvideo */
icculus@8153
   130
    device->VideoInit        = MIR_VideoInit;
icculus@8153
   131
    device->VideoQuit        = MIR_VideoQuit;
icculus@8153
   132
    device->GetDisplayBounds = MIR_GetDisplayBounds;
icculus@8153
   133
    device->GetDisplayModes  = MIR_GetDisplayModes;
icculus@8153
   134
    device->SetDisplayMode   = MIR_SetDisplayMode;
icculus@8153
   135
    device->free             = MIR_DeleteDevice;
icculus@8153
   136
icculus@8153
   137
    /* miropengles */
icculus@8153
   138
    device->GL_SwapWindow      = MIR_GL_SwapWindow;
icculus@8153
   139
    device->GL_MakeCurrent     = MIR_GL_MakeCurrent;
icculus@8153
   140
    device->GL_CreateContext   = MIR_GL_CreateContext;
icculus@8153
   141
    device->GL_DeleteContext   = MIR_GL_DeleteContext;
icculus@8153
   142
    device->GL_LoadLibrary     = MIR_GL_LoadLibrary;
icculus@8153
   143
    device->GL_UnloadLibrary   = MIR_GL_UnloadLibrary;
icculus@8153
   144
    device->GL_GetSwapInterval = MIR_GL_GetSwapInterval;
icculus@8153
   145
    device->GL_SetSwapInterval = MIR_GL_SetSwapInterval;
icculus@8153
   146
    device->GL_GetProcAddress  = MIR_GL_GetProcAddress;
icculus@8153
   147
icculus@8153
   148
    /* mirwindow */
icculus@8153
   149
    device->CreateWindow        = MIR_CreateWindow;
icculus@8153
   150
    device->DestroyWindow       = MIR_DestroyWindow;
icculus@8153
   151
    device->GetWindowWMInfo     = MIR_GetWindowWMInfo;
icculus@8153
   152
    device->SetWindowFullscreen = MIR_SetWindowFullscreen;
icculus@8153
   153
    device->MaximizeWindow      = MIR_MaximizeWindow;
icculus@8153
   154
    device->MinimizeWindow      = MIR_MinimizeWindow;
icculus@8153
   155
    device->RestoreWindow       = MIR_RestoreWindow;
icculus@8153
   156
icculus@8153
   157
    device->CreateWindowFrom     = NULL;
icculus@8153
   158
    device->SetWindowTitle       = NULL;
icculus@8153
   159
    device->SetWindowIcon        = NULL;
icculus@8153
   160
    device->SetWindowPosition    = NULL;
icculus@8153
   161
    device->SetWindowSize        = NULL;
icculus@8153
   162
    device->SetWindowMinimumSize = NULL;
icculus@8153
   163
    device->SetWindowMaximumSize = NULL;
icculus@8153
   164
    device->ShowWindow           = NULL;
icculus@8153
   165
    device->HideWindow           = NULL;
icculus@8153
   166
    device->RaiseWindow          = NULL;
icculus@8153
   167
    device->SetWindowBordered    = NULL;
icculus@8153
   168
    device->SetWindowGammaRamp   = NULL;
icculus@8153
   169
    device->GetWindowGammaRamp   = NULL;
icculus@8153
   170
    device->SetWindowGrab        = NULL;
icculus@8153
   171
    device->OnWindowEnter        = NULL;
icculus@8153
   172
icculus@8153
   173
    /* mirframebuffer */
icculus@8153
   174
    device->CreateWindowFramebuffer  = MIR_CreateWindowFramebuffer;
icculus@8153
   175
    device->UpdateWindowFramebuffer  = MIR_UpdateWindowFramebuffer;
icculus@8153
   176
    device->DestroyWindowFramebuffer = MIR_DestroyWindowFramebuffer;
icculus@8153
   177
icculus@8153
   178
    device->shape_driver.CreateShaper      = MIR_CreateShaper;
icculus@8153
   179
    device->shape_driver.SetWindowShape    = MIR_SetWindowShape;
icculus@8153
   180
    device->shape_driver.ResizeWindowShape = MIR_ResizeWindowShape;
icculus@8153
   181
icculus@8153
   182
    device->PumpEvents = MIR_PumpEvents;
icculus@8153
   183
icculus@8153
   184
    device->SuspendScreenSaver = NULL;
icculus@8153
   185
icculus@8153
   186
    device->StartTextInput   = NULL;
icculus@8153
   187
    device->StopTextInput    = NULL;
icculus@8153
   188
    device->SetTextInputRect = NULL;
icculus@8153
   189
icculus@8153
   190
    device->HasScreenKeyboardSupport = NULL;
icculus@8153
   191
    device->ShowScreenKeyboard       = NULL;
icculus@8153
   192
    device->HideScreenKeyboard       = NULL;
icculus@8153
   193
    device->IsScreenKeyboardShown    = NULL;
icculus@8153
   194
icculus@8153
   195
    device->SetClipboardText = NULL;
icculus@8153
   196
    device->GetClipboardText = NULL;
icculus@8153
   197
    device->HasClipboardText = NULL;
icculus@8153
   198
icculus@8153
   199
    device->ShowMessageBox = NULL;
icculus@8153
   200
icculus@8153
   201
    return device;
icculus@8153
   202
}
icculus@8153
   203
icculus@8153
   204
VideoBootStrap MIR_bootstrap = {
icculus@8153
   205
    MIR_DRIVER_NAME, "SDL Mir video driver",
icculus@8153
   206
    MIR_Available, MIR_CreateDevice
icculus@8153
   207
};
icculus@8153
   208
icculus@8153
   209
static void
icculus@8153
   210
MIR_SetCurrentDisplayMode(MirDisplayOutput const* out, SDL_VideoDisplay* display)
icculus@8153
   211
{
icculus@8153
   212
    SDL_DisplayMode mode = {
icculus@8153
   213
        .format = SDL_PIXELFORMAT_RGB888,
icculus@8153
   214
        .w = out->modes[out->current_mode].horizontal_resolution,
icculus@8153
   215
        .h = out->modes[out->current_mode].vertical_resolution,
icculus@8153
   216
        .refresh_rate = out->modes[out->current_mode].refresh_rate,
icculus@8153
   217
        .driverdata = NULL
icculus@8153
   218
    };
icculus@8153
   219
icculus@8153
   220
    display->desktop_mode = mode;
icculus@8153
   221
    display->current_mode = mode;
icculus@8153
   222
}
icculus@8153
   223
icculus@8153
   224
static void
icculus@8153
   225
MIR_AddAllModesFromDisplay(MirDisplayOutput const* out, SDL_VideoDisplay* display)
icculus@8153
   226
{
icculus@8153
   227
    int n_mode;
icculus@8153
   228
    for (n_mode = 0; n_mode < out->num_modes; ++n_mode) {
icculus@8153
   229
        SDL_DisplayMode mode = {
icculus@8153
   230
            .format = SDL_PIXELFORMAT_RGB888,
icculus@8153
   231
            .w = out->modes[n_mode].horizontal_resolution,
icculus@8153
   232
            .h = out->modes[n_mode].vertical_resolution,
icculus@8153
   233
            .refresh_rate = out->modes[n_mode].refresh_rate,
icculus@8153
   234
            .driverdata = NULL
icculus@8153
   235
        };
icculus@8153
   236
icculus@8153
   237
        SDL_AddDisplayMode(display, &mode);
icculus@8153
   238
    }
icculus@8153
   239
}
icculus@8153
   240
icculus@8153
   241
static void
icculus@8153
   242
MIR_InitDisplays(_THIS)
icculus@8153
   243
{
icculus@8153
   244
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
   245
    int d;
icculus@8153
   246
icculus@8159
   247
    MirDisplayConfiguration* display_config = MIR_mir_connection_create_display_config(mir_data->connection);
icculus@8153
   248
icculus@8153
   249
    for (d = 0; d < display_config->num_outputs; d++) {
icculus@8153
   250
        MirDisplayOutput const* out = display_config->outputs + d;
icculus@8153
   251
icculus@8153
   252
        SDL_VideoDisplay display;
icculus@8153
   253
        SDL_zero(display);
icculus@8153
   254
icculus@8153
   255
        if (out->used &&
icculus@8153
   256
            out->connected &&
icculus@8153
   257
            out->num_modes &&
icculus@8153
   258
            out->current_mode < out->num_modes) {
icculus@8153
   259
icculus@8153
   260
            MIR_SetCurrentDisplayMode(out, &display);
icculus@8153
   261
            MIR_AddAllModesFromDisplay(out, &display);
icculus@8153
   262
icculus@8153
   263
            SDL_AddVideoDisplay(&display);
icculus@8153
   264
        }
icculus@8153
   265
    }
icculus@8153
   266
icculus@8159
   267
    MIR_mir_display_config_destroy(display_config);
icculus@8153
   268
}
icculus@8153
   269
icculus@8153
   270
int
icculus@8153
   271
MIR_VideoInit(_THIS)
icculus@8153
   272
{
icculus@8153
   273
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
   274
icculus@8159
   275
    mir_data->connection = MIR_mir_connect_sync(NULL, __PRETTY_FUNCTION__);
brandon@8749
   276
    mir_data->software = SDL_FALSE;
icculus@8153
   277
icculus@8159
   278
    if (!MIR_mir_connection_is_valid(mir_data->connection))
icculus@8153
   279
        return SDL_SetError("Failed to connect to the Mir Server");
icculus@8153
   280
icculus@8153
   281
    MIR_InitDisplays(_this);
icculus@8153
   282
    MIR_InitMouse();
icculus@8153
   283
icculus@8153
   284
    return 0;
icculus@8153
   285
}
icculus@8153
   286
icculus@8153
   287
void
icculus@8153
   288
MIR_VideoQuit(_THIS)
icculus@8153
   289
{
icculus@8153
   290
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
   291
icculus@8153
   292
    MIR_FiniMouse();
icculus@8153
   293
icculus@8153
   294
    MIR_GL_DeleteContext(_this, NULL);
icculus@8153
   295
    MIR_GL_UnloadLibrary(_this);
icculus@8153
   296
icculus@8159
   297
    MIR_mir_connection_release(mir_data->connection);
icculus@8153
   298
icculus@8153
   299
    SDL_free(mir_data);
icculus@8153
   300
    _this->driverdata = NULL;
icculus@8153
   301
}
icculus@8153
   302
icculus@8153
   303
static int
icculus@8153
   304
MIR_GetDisplayBounds(_THIS, SDL_VideoDisplay* display, SDL_Rect* rect)
icculus@8153
   305
{
icculus@8153
   306
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
   307
    int d;
icculus@8153
   308
icculus@8159
   309
    MirDisplayConfiguration* display_config = MIR_mir_connection_create_display_config(mir_data->connection);
icculus@8153
   310
icculus@8153
   311
    for (d = 0; d < display_config->num_outputs; d++) {
icculus@8153
   312
        MirDisplayOutput const* out = display_config->outputs + d;
icculus@8153
   313
icculus@8153
   314
        if (out->used &&
icculus@8153
   315
            out->connected &&
icculus@8153
   316
            out->num_modes &&
icculus@8153
   317
            out->current_mode < out->num_modes) {
icculus@8153
   318
icculus@8153
   319
            rect->x = out->position_x;
icculus@8153
   320
            rect->y = out->position_y;
icculus@8153
   321
            rect->w = out->modes->horizontal_resolution;
icculus@8153
   322
            rect->h = out->modes->vertical_resolution;
icculus@8153
   323
        }
icculus@8153
   324
    }
icculus@8153
   325
icculus@8159
   326
    MIR_mir_display_config_destroy(display_config);
icculus@8153
   327
icculus@8153
   328
    return 0;
icculus@8153
   329
}
icculus@8153
   330
icculus@8153
   331
static void
icculus@8153
   332
MIR_GetDisplayModes(_THIS, SDL_VideoDisplay* sdl_display)
icculus@8153
   333
{
icculus@8153
   334
}
icculus@8153
   335
icculus@8153
   336
static int
icculus@8153
   337
MIR_SetDisplayMode(_THIS, SDL_VideoDisplay* sdl_display, SDL_DisplayMode* mode)
icculus@8153
   338
{
icculus@8153
   339
    return 0;
icculus@8153
   340
}
icculus@8154
   341
icculus@8154
   342
#endif /* SDL_VIDEO_DRIVER_MIR */
icculus@8154
   343
icculus@8154
   344
/* vi: set ts=4 sw=4 expandtab: */
icculus@8154
   345