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