src/video/mir/SDL_mirvideo.c
changeset 8153 3e43c73ac79b
child 8154 ea6b25de67b6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/mir/SDL_mirvideo.c	Sun Feb 02 23:41:46 2014 -0500
     1.3 @@ -0,0 +1,319 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/*
    1.26 +  Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
    1.27 +*/
    1.28 +
    1.29 +#include "SDL_config.h"
    1.30 +#include "SDL_video.h"
    1.31 +
    1.32 +#include "SDL_mirframebuffer.h"
    1.33 +#include "SDL_mirmouse.h"
    1.34 +#include "SDL_miropengl.h"
    1.35 +#include "SDL_mirvideo.h"
    1.36 +#include "SDL_mirwindow.h"
    1.37 +
    1.38 +#define MIR_DRIVER_NAME "mir"
    1.39 +
    1.40 +static int
    1.41 +MIR_VideoInit(_THIS);
    1.42 +
    1.43 +static void
    1.44 +MIR_VideoQuit(_THIS);
    1.45 +
    1.46 +static int
    1.47 +MIR_GetDisplayBounds(_THIS, SDL_VideoDisplay* display, SDL_Rect* rect);
    1.48 +
    1.49 +static void
    1.50 +MIR_GetDisplayModes(_THIS, SDL_VideoDisplay* sdl_display);
    1.51 +
    1.52 +static int
    1.53 +MIR_SetDisplayMode(_THIS, SDL_VideoDisplay* sdl_display, SDL_DisplayMode* mode);
    1.54 +
    1.55 +static SDL_WindowShaper*
    1.56 +MIR_CreateShaper(SDL_Window* window)
    1.57 +{
    1.58 +    /* FIXME Im not sure if mir support this atm, will have to come back to this */
    1.59 +    return NULL;
    1.60 +}
    1.61 +
    1.62 +static int
    1.63 +MIR_SetWindowShape(SDL_WindowShaper* shaper, SDL_Surface* shape, SDL_WindowShapeMode* shape_mode)
    1.64 +{
    1.65 +    return SDL_Unsupported();
    1.66 +}
    1.67 +
    1.68 +static int
    1.69 +MIR_ResizeWindowShape(SDL_Window* window)
    1.70 +{
    1.71 +    return SDL_Unsupported();
    1.72 +}
    1.73 +
    1.74 +static int
    1.75 +MIR_Available()
    1.76 +{
    1.77 +    return 1;
    1.78 +}
    1.79 +
    1.80 +static void
    1.81 +MIR_DeleteDevice(SDL_VideoDevice* device)
    1.82 +{
    1.83 +    SDL_free(device);
    1.84 +}
    1.85 +
    1.86 +void
    1.87 +MIR_PumpEvents(_THIS)
    1.88 +{
    1.89 +}
    1.90 +
    1.91 +static SDL_VideoDevice*
    1.92 +MIR_CreateDevice(int device_index)
    1.93 +{
    1.94 +    MIR_Data* mir_data;
    1.95 +    SDL_VideoDevice* device = SDL_calloc(1, sizeof(SDL_VideoDevice));
    1.96 +    if (!device) {
    1.97 +        SDL_OutOfMemory();
    1.98 +        return NULL;
    1.99 +    }
   1.100 +
   1.101 +    mir_data = SDL_calloc(1, sizeof(MIR_Data));
   1.102 +    if (!mir_data) {
   1.103 +        SDL_OutOfMemory();
   1.104 +        SDL_free(device);
   1.105 +        return NULL;
   1.106 +    }
   1.107 +
   1.108 +    device->driverdata = mir_data;
   1.109 +
   1.110 +    /* mirvideo */
   1.111 +    device->VideoInit        = MIR_VideoInit;
   1.112 +    device->VideoQuit        = MIR_VideoQuit;
   1.113 +    device->GetDisplayBounds = MIR_GetDisplayBounds;
   1.114 +    device->GetDisplayModes  = MIR_GetDisplayModes;
   1.115 +    device->SetDisplayMode   = MIR_SetDisplayMode;
   1.116 +    device->free             = MIR_DeleteDevice;
   1.117 +
   1.118 +    /* miropengles */
   1.119 +    device->GL_SwapWindow      = MIR_GL_SwapWindow;
   1.120 +    device->GL_MakeCurrent     = MIR_GL_MakeCurrent;
   1.121 +    device->GL_CreateContext   = MIR_GL_CreateContext;
   1.122 +    device->GL_DeleteContext   = MIR_GL_DeleteContext;
   1.123 +    device->GL_LoadLibrary     = MIR_GL_LoadLibrary;
   1.124 +    device->GL_UnloadLibrary   = MIR_GL_UnloadLibrary;
   1.125 +    device->GL_GetSwapInterval = MIR_GL_GetSwapInterval;
   1.126 +    device->GL_SetSwapInterval = MIR_GL_SetSwapInterval;
   1.127 +    device->GL_GetProcAddress  = MIR_GL_GetProcAddress;
   1.128 +
   1.129 +    /* mirwindow */
   1.130 +    device->CreateWindow        = MIR_CreateWindow;
   1.131 +    device->DestroyWindow       = MIR_DestroyWindow;
   1.132 +    device->GetWindowWMInfo     = MIR_GetWindowWMInfo;
   1.133 +    device->SetWindowFullscreen = MIR_SetWindowFullscreen;
   1.134 +    device->MaximizeWindow      = MIR_MaximizeWindow;
   1.135 +    device->MinimizeWindow      = MIR_MinimizeWindow;
   1.136 +    device->RestoreWindow       = MIR_RestoreWindow;
   1.137 +
   1.138 +    device->CreateWindowFrom     = NULL;
   1.139 +    device->SetWindowTitle       = NULL;
   1.140 +    device->SetWindowIcon        = NULL;
   1.141 +    device->SetWindowPosition    = NULL;
   1.142 +    device->SetWindowSize        = NULL;
   1.143 +    device->SetWindowMinimumSize = NULL;
   1.144 +    device->SetWindowMaximumSize = NULL;
   1.145 +    device->ShowWindow           = NULL;
   1.146 +    device->HideWindow           = NULL;
   1.147 +    device->RaiseWindow          = NULL;
   1.148 +    device->SetWindowBordered    = NULL;
   1.149 +    device->SetWindowGammaRamp   = NULL;
   1.150 +    device->GetWindowGammaRamp   = NULL;
   1.151 +    device->SetWindowGrab        = NULL;
   1.152 +    device->OnWindowEnter        = NULL;
   1.153 +
   1.154 +    /* mirframebuffer */
   1.155 +    device->CreateWindowFramebuffer  = MIR_CreateWindowFramebuffer;
   1.156 +    device->UpdateWindowFramebuffer  = MIR_UpdateWindowFramebuffer;
   1.157 +    device->DestroyWindowFramebuffer = MIR_DestroyWindowFramebuffer;
   1.158 +
   1.159 +    device->shape_driver.CreateShaper      = MIR_CreateShaper;
   1.160 +    device->shape_driver.SetWindowShape    = MIR_SetWindowShape;
   1.161 +    device->shape_driver.ResizeWindowShape = MIR_ResizeWindowShape;
   1.162 +
   1.163 +    device->PumpEvents = MIR_PumpEvents;
   1.164 +
   1.165 +    device->SuspendScreenSaver = NULL;
   1.166 +
   1.167 +    device->StartTextInput   = NULL;
   1.168 +    device->StopTextInput    = NULL;
   1.169 +    device->SetTextInputRect = NULL;
   1.170 +
   1.171 +    device->HasScreenKeyboardSupport = NULL;
   1.172 +    device->ShowScreenKeyboard       = NULL;
   1.173 +    device->HideScreenKeyboard       = NULL;
   1.174 +    device->IsScreenKeyboardShown    = NULL;
   1.175 +
   1.176 +    device->SetClipboardText = NULL;
   1.177 +    device->GetClipboardText = NULL;
   1.178 +    device->HasClipboardText = NULL;
   1.179 +
   1.180 +    device->ShowMessageBox = NULL;
   1.181 +
   1.182 +    return device;
   1.183 +}
   1.184 +
   1.185 +VideoBootStrap MIR_bootstrap = {
   1.186 +    MIR_DRIVER_NAME, "SDL Mir video driver",
   1.187 +    MIR_Available, MIR_CreateDevice
   1.188 +};
   1.189 +
   1.190 +static void
   1.191 +MIR_SetCurrentDisplayMode(MirDisplayOutput const* out, SDL_VideoDisplay* display)
   1.192 +{
   1.193 +    SDL_DisplayMode mode = {
   1.194 +        .format = SDL_PIXELFORMAT_RGB888,
   1.195 +        .w = out->modes[out->current_mode].horizontal_resolution,
   1.196 +        .h = out->modes[out->current_mode].vertical_resolution,
   1.197 +        .refresh_rate = out->modes[out->current_mode].refresh_rate,
   1.198 +        .driverdata = NULL
   1.199 +    };
   1.200 +
   1.201 +    display->desktop_mode = mode;
   1.202 +    display->current_mode = mode;
   1.203 +}
   1.204 +
   1.205 +static void
   1.206 +MIR_AddAllModesFromDisplay(MirDisplayOutput const* out, SDL_VideoDisplay* display)
   1.207 +{
   1.208 +    int n_mode;
   1.209 +    for (n_mode = 0; n_mode < out->num_modes; ++n_mode) {
   1.210 +        SDL_DisplayMode mode = {
   1.211 +            .format = SDL_PIXELFORMAT_RGB888,
   1.212 +            .w = out->modes[n_mode].horizontal_resolution,
   1.213 +            .h = out->modes[n_mode].vertical_resolution,
   1.214 +            .refresh_rate = out->modes[n_mode].refresh_rate,
   1.215 +            .driverdata = NULL
   1.216 +        };
   1.217 +
   1.218 +        SDL_AddDisplayMode(display, &mode);
   1.219 +    }
   1.220 +}
   1.221 +
   1.222 +static void
   1.223 +MIR_InitDisplays(_THIS)
   1.224 +{
   1.225 +    MIR_Data* mir_data = _this->driverdata;
   1.226 +    int d;
   1.227 +
   1.228 +    MirDisplayConfiguration* display_config =
   1.229 +            mir_connection_create_display_config(mir_data->connection);
   1.230 +
   1.231 +    for (d = 0; d < display_config->num_outputs; d++) {
   1.232 +        MirDisplayOutput const* out = display_config->outputs + d;
   1.233 +
   1.234 +        SDL_VideoDisplay display;
   1.235 +        SDL_zero(display);
   1.236 +
   1.237 +        if (out->used &&
   1.238 +            out->connected &&
   1.239 +            out->num_modes &&
   1.240 +            out->current_mode < out->num_modes) {
   1.241 +
   1.242 +            MIR_SetCurrentDisplayMode(out, &display);
   1.243 +            MIR_AddAllModesFromDisplay(out, &display);
   1.244 +
   1.245 +            SDL_AddVideoDisplay(&display);
   1.246 +        }
   1.247 +    }
   1.248 +
   1.249 +    mir_display_config_destroy(display_config);
   1.250 +}
   1.251 +
   1.252 +int
   1.253 +MIR_VideoInit(_THIS)
   1.254 +{
   1.255 +    MIR_Data* mir_data = _this->driverdata;
   1.256 +
   1.257 +    mir_data->connection = mir_connect_sync(NULL, __PRETTY_FUNCTION__);
   1.258 +
   1.259 +    if (!mir_connection_is_valid(mir_data->connection))
   1.260 +        return SDL_SetError("Failed to connect to the Mir Server");
   1.261 +
   1.262 +    MIR_InitDisplays(_this);
   1.263 +    MIR_InitMouse();
   1.264 +
   1.265 +    return 0;
   1.266 +}
   1.267 +
   1.268 +void
   1.269 +MIR_VideoQuit(_THIS)
   1.270 +{
   1.271 +    MIR_Data* mir_data = _this->driverdata;
   1.272 +
   1.273 +    MIR_FiniMouse();
   1.274 +
   1.275 +    MIR_GL_DeleteContext(_this, NULL);
   1.276 +    MIR_GL_UnloadLibrary(_this);
   1.277 +
   1.278 +    mir_connection_release(mir_data->connection);
   1.279 +
   1.280 +    SDL_free(mir_data);
   1.281 +    _this->driverdata = NULL;
   1.282 +}
   1.283 +
   1.284 +static int
   1.285 +MIR_GetDisplayBounds(_THIS, SDL_VideoDisplay* display, SDL_Rect* rect)
   1.286 +{
   1.287 +    MIR_Data* mir_data = _this->driverdata;
   1.288 +    int d;
   1.289 +
   1.290 +    MirDisplayConfiguration* display_config =
   1.291 +            mir_connection_create_display_config(mir_data->connection);
   1.292 +
   1.293 +    for (d = 0; d < display_config->num_outputs; d++) {
   1.294 +        MirDisplayOutput const* out = display_config->outputs + d;
   1.295 +
   1.296 +        if (out->used &&
   1.297 +            out->connected &&
   1.298 +            out->num_modes &&
   1.299 +            out->current_mode < out->num_modes) {
   1.300 +
   1.301 +            rect->x = out->position_x;
   1.302 +            rect->y = out->position_y;
   1.303 +            rect->w = out->modes->horizontal_resolution;
   1.304 +            rect->h = out->modes->vertical_resolution;
   1.305 +        }
   1.306 +    }
   1.307 +
   1.308 +    mir_display_config_destroy(display_config);
   1.309 +
   1.310 +    return 0;
   1.311 +}
   1.312 +
   1.313 +static void
   1.314 +MIR_GetDisplayModes(_THIS, SDL_VideoDisplay* sdl_display)
   1.315 +{
   1.316 +}
   1.317 +
   1.318 +static int
   1.319 +MIR_SetDisplayMode(_THIS, SDL_VideoDisplay* sdl_display, SDL_DisplayMode* mode)
   1.320 +{
   1.321 +    return 0;
   1.322 +}