src/video/mir/SDL_mirvideo.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 03 Feb 2014 11:52:54 -0500
changeset 8159 427e8fdf6f69
parent 8157 f5d262613ed2
child 8723 cfc134c9c369
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@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
icculus@8159
    88
    return available;
icculus@8153
    89
}
icculus@8153
    90
icculus@8153
    91
static void
icculus@8153
    92
MIR_DeleteDevice(SDL_VideoDevice* device)
icculus@8153
    93
{
icculus@8153
    94
    SDL_free(device);
icculus@8159
    95
    SDL_MIR_UnloadSymbols();
icculus@8153
    96
}
icculus@8153
    97
icculus@8153
    98
void
icculus@8153
    99
MIR_PumpEvents(_THIS)
icculus@8153
   100
{
icculus@8153
   101
}
icculus@8153
   102
icculus@8153
   103
static SDL_VideoDevice*
icculus@8153
   104
MIR_CreateDevice(int device_index)
icculus@8153
   105
{
icculus@8153
   106
    MIR_Data* mir_data;
icculus@8159
   107
    SDL_VideoDevice* device = NULL;
icculus@8159
   108
icculus@8159
   109
    if (!SDL_MIR_LoadSymbols()) {
icculus@8159
   110
        return NULL;
icculus@8159
   111
    }
icculus@8159
   112
icculus@8159
   113
    device = SDL_calloc(1, sizeof(SDL_VideoDevice));
icculus@8153
   114
    if (!device) {
icculus@8159
   115
        SDL_MIR_UnloadSymbols();
icculus@8153
   116
        SDL_OutOfMemory();
icculus@8153
   117
        return NULL;
icculus@8153
   118
    }
icculus@8153
   119
icculus@8153
   120
    mir_data = SDL_calloc(1, sizeof(MIR_Data));
icculus@8153
   121
    if (!mir_data) {
icculus@8159
   122
        SDL_free(device);
icculus@8159
   123
        SDL_MIR_UnloadSymbols();
icculus@8153
   124
        SDL_OutOfMemory();
icculus@8153
   125
        return NULL;
icculus@8153
   126
    }
icculus@8153
   127
icculus@8153
   128
    device->driverdata = mir_data;
icculus@8153
   129
icculus@8153
   130
    /* mirvideo */
icculus@8153
   131
    device->VideoInit        = MIR_VideoInit;
icculus@8153
   132
    device->VideoQuit        = MIR_VideoQuit;
icculus@8153
   133
    device->GetDisplayBounds = MIR_GetDisplayBounds;
icculus@8153
   134
    device->GetDisplayModes  = MIR_GetDisplayModes;
icculus@8153
   135
    device->SetDisplayMode   = MIR_SetDisplayMode;
icculus@8153
   136
    device->free             = MIR_DeleteDevice;
icculus@8153
   137
icculus@8153
   138
    /* miropengles */
icculus@8153
   139
    device->GL_SwapWindow      = MIR_GL_SwapWindow;
icculus@8153
   140
    device->GL_MakeCurrent     = MIR_GL_MakeCurrent;
icculus@8153
   141
    device->GL_CreateContext   = MIR_GL_CreateContext;
icculus@8153
   142
    device->GL_DeleteContext   = MIR_GL_DeleteContext;
icculus@8153
   143
    device->GL_LoadLibrary     = MIR_GL_LoadLibrary;
icculus@8153
   144
    device->GL_UnloadLibrary   = MIR_GL_UnloadLibrary;
icculus@8153
   145
    device->GL_GetSwapInterval = MIR_GL_GetSwapInterval;
icculus@8153
   146
    device->GL_SetSwapInterval = MIR_GL_SetSwapInterval;
icculus@8153
   147
    device->GL_GetProcAddress  = MIR_GL_GetProcAddress;
icculus@8153
   148
icculus@8153
   149
    /* mirwindow */
icculus@8153
   150
    device->CreateWindow        = MIR_CreateWindow;
icculus@8153
   151
    device->DestroyWindow       = MIR_DestroyWindow;
icculus@8153
   152
    device->GetWindowWMInfo     = MIR_GetWindowWMInfo;
icculus@8153
   153
    device->SetWindowFullscreen = MIR_SetWindowFullscreen;
icculus@8153
   154
    device->MaximizeWindow      = MIR_MaximizeWindow;
icculus@8153
   155
    device->MinimizeWindow      = MIR_MinimizeWindow;
icculus@8153
   156
    device->RestoreWindow       = MIR_RestoreWindow;
icculus@8153
   157
icculus@8153
   158
    device->CreateWindowFrom     = NULL;
icculus@8153
   159
    device->SetWindowTitle       = NULL;
icculus@8153
   160
    device->SetWindowIcon        = NULL;
icculus@8153
   161
    device->SetWindowPosition    = NULL;
icculus@8153
   162
    device->SetWindowSize        = NULL;
icculus@8153
   163
    device->SetWindowMinimumSize = NULL;
icculus@8153
   164
    device->SetWindowMaximumSize = NULL;
icculus@8153
   165
    device->ShowWindow           = NULL;
icculus@8153
   166
    device->HideWindow           = NULL;
icculus@8153
   167
    device->RaiseWindow          = NULL;
icculus@8153
   168
    device->SetWindowBordered    = NULL;
icculus@8153
   169
    device->SetWindowGammaRamp   = NULL;
icculus@8153
   170
    device->GetWindowGammaRamp   = NULL;
icculus@8153
   171
    device->SetWindowGrab        = NULL;
icculus@8153
   172
    device->OnWindowEnter        = NULL;
icculus@8153
   173
icculus@8153
   174
    /* mirframebuffer */
icculus@8153
   175
    device->CreateWindowFramebuffer  = MIR_CreateWindowFramebuffer;
icculus@8153
   176
    device->UpdateWindowFramebuffer  = MIR_UpdateWindowFramebuffer;
icculus@8153
   177
    device->DestroyWindowFramebuffer = MIR_DestroyWindowFramebuffer;
icculus@8153
   178
icculus@8153
   179
    device->shape_driver.CreateShaper      = MIR_CreateShaper;
icculus@8153
   180
    device->shape_driver.SetWindowShape    = MIR_SetWindowShape;
icculus@8153
   181
    device->shape_driver.ResizeWindowShape = MIR_ResizeWindowShape;
icculus@8153
   182
icculus@8153
   183
    device->PumpEvents = MIR_PumpEvents;
icculus@8153
   184
icculus@8153
   185
    device->SuspendScreenSaver = NULL;
icculus@8153
   186
icculus@8153
   187
    device->StartTextInput   = NULL;
icculus@8153
   188
    device->StopTextInput    = NULL;
icculus@8153
   189
    device->SetTextInputRect = NULL;
icculus@8153
   190
icculus@8153
   191
    device->HasScreenKeyboardSupport = NULL;
icculus@8153
   192
    device->ShowScreenKeyboard       = NULL;
icculus@8153
   193
    device->HideScreenKeyboard       = NULL;
icculus@8153
   194
    device->IsScreenKeyboardShown    = NULL;
icculus@8153
   195
icculus@8153
   196
    device->SetClipboardText = NULL;
icculus@8153
   197
    device->GetClipboardText = NULL;
icculus@8153
   198
    device->HasClipboardText = NULL;
icculus@8153
   199
icculus@8153
   200
    device->ShowMessageBox = NULL;
icculus@8153
   201
icculus@8153
   202
    return device;
icculus@8153
   203
}
icculus@8153
   204
icculus@8153
   205
VideoBootStrap MIR_bootstrap = {
icculus@8153
   206
    MIR_DRIVER_NAME, "SDL Mir video driver",
icculus@8153
   207
    MIR_Available, MIR_CreateDevice
icculus@8153
   208
};
icculus@8153
   209
icculus@8153
   210
static void
icculus@8153
   211
MIR_SetCurrentDisplayMode(MirDisplayOutput const* out, SDL_VideoDisplay* display)
icculus@8153
   212
{
icculus@8153
   213
    SDL_DisplayMode mode = {
icculus@8153
   214
        .format = SDL_PIXELFORMAT_RGB888,
icculus@8153
   215
        .w = out->modes[out->current_mode].horizontal_resolution,
icculus@8153
   216
        .h = out->modes[out->current_mode].vertical_resolution,
icculus@8153
   217
        .refresh_rate = out->modes[out->current_mode].refresh_rate,
icculus@8153
   218
        .driverdata = NULL
icculus@8153
   219
    };
icculus@8153
   220
icculus@8153
   221
    display->desktop_mode = mode;
icculus@8153
   222
    display->current_mode = mode;
icculus@8153
   223
}
icculus@8153
   224
icculus@8153
   225
static void
icculus@8153
   226
MIR_AddAllModesFromDisplay(MirDisplayOutput const* out, SDL_VideoDisplay* display)
icculus@8153
   227
{
icculus@8153
   228
    int n_mode;
icculus@8153
   229
    for (n_mode = 0; n_mode < out->num_modes; ++n_mode) {
icculus@8153
   230
        SDL_DisplayMode mode = {
icculus@8153
   231
            .format = SDL_PIXELFORMAT_RGB888,
icculus@8153
   232
            .w = out->modes[n_mode].horizontal_resolution,
icculus@8153
   233
            .h = out->modes[n_mode].vertical_resolution,
icculus@8153
   234
            .refresh_rate = out->modes[n_mode].refresh_rate,
icculus@8153
   235
            .driverdata = NULL
icculus@8153
   236
        };
icculus@8153
   237
icculus@8153
   238
        SDL_AddDisplayMode(display, &mode);
icculus@8153
   239
    }
icculus@8153
   240
}
icculus@8153
   241
icculus@8153
   242
static void
icculus@8153
   243
MIR_InitDisplays(_THIS)
icculus@8153
   244
{
icculus@8153
   245
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
   246
    int d;
icculus@8153
   247
icculus@8159
   248
    MirDisplayConfiguration* display_config = MIR_mir_connection_create_display_config(mir_data->connection);
icculus@8153
   249
icculus@8153
   250
    for (d = 0; d < display_config->num_outputs; d++) {
icculus@8153
   251
        MirDisplayOutput const* out = display_config->outputs + d;
icculus@8153
   252
icculus@8153
   253
        SDL_VideoDisplay display;
icculus@8153
   254
        SDL_zero(display);
icculus@8153
   255
icculus@8153
   256
        if (out->used &&
icculus@8153
   257
            out->connected &&
icculus@8153
   258
            out->num_modes &&
icculus@8153
   259
            out->current_mode < out->num_modes) {
icculus@8153
   260
icculus@8153
   261
            MIR_SetCurrentDisplayMode(out, &display);
icculus@8153
   262
            MIR_AddAllModesFromDisplay(out, &display);
icculus@8153
   263
icculus@8153
   264
            SDL_AddVideoDisplay(&display);
icculus@8153
   265
        }
icculus@8153
   266
    }
icculus@8153
   267
icculus@8159
   268
    MIR_mir_display_config_destroy(display_config);
icculus@8153
   269
}
icculus@8153
   270
icculus@8153
   271
int
icculus@8153
   272
MIR_VideoInit(_THIS)
icculus@8153
   273
{
icculus@8153
   274
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
   275
icculus@8159
   276
    mir_data->connection = MIR_mir_connect_sync(NULL, __PRETTY_FUNCTION__);
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