src/video/mir/SDL_mirvideo.c
changeset 12391 ca6e41ade79a
parent 12390 60f322c5984f
child 12392 046dcbdbaa74
     1.1 --- a/src/video/mir/SDL_mirvideo.c	Fri Nov 02 18:07:11 2018 -0700
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,423 +0,0 @@
     1.4 -/*
     1.5 -  Simple DirectMedia Layer
     1.6 -  Copyright (C) 1997-2018 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_internal.h"
    1.30 -
    1.31 -#if SDL_VIDEO_DRIVER_MIR
    1.32 -
    1.33 -#include "SDL_assert.h"
    1.34 -#include "SDL_log.h"
    1.35 -
    1.36 -#include "SDL_mirwindow.h"
    1.37 -#include "SDL_video.h"
    1.38 -
    1.39 -#include "SDL_mirframebuffer.h"
    1.40 -#include "SDL_mirmouse.h"
    1.41 -#include "SDL_miropengl.h"
    1.42 -#include "SDL_mirvideo.h"
    1.43 -#include "SDL_mirvulkan.h"
    1.44 -
    1.45 -#include "SDL_mirdyn.h"
    1.46 -
    1.47 -#define MIR_DRIVER_NAME "mir"
    1.48 -
    1.49 -static const Uint32 mir_pixel_format_to_sdl_format[] = {
    1.50 -    SDL_PIXELFORMAT_UNKNOWN,  /* mir_pixel_format_invalid   */
    1.51 -    SDL_PIXELFORMAT_ABGR8888, /* mir_pixel_format_abgr_8888 */
    1.52 -    SDL_PIXELFORMAT_BGR888,   /* mir_pixel_format_xbgr_8888 */
    1.53 -    SDL_PIXELFORMAT_ARGB8888, /* mir_pixel_format_argb_8888 */
    1.54 -    SDL_PIXELFORMAT_RGB888,   /* mir_pixel_format_xrgb_8888 */
    1.55 -    SDL_PIXELFORMAT_BGR24,    /* mir_pixel_format_bgr_888   */
    1.56 -    SDL_PIXELFORMAT_RGB24,    /* mir_pixel_format_rgb_888   */
    1.57 -    SDL_PIXELFORMAT_RGB565,   /* mir_pixel_format_rgb_565   */
    1.58 -    SDL_PIXELFORMAT_RGBA5551, /* mir_pixel_format_rgba_5551 */
    1.59 -    SDL_PIXELFORMAT_RGBA4444  /* mir_pixel_format_rgba_4444 */
    1.60 -};
    1.61 -
    1.62 -Uint32
    1.63 -MIR_GetSDLPixelFormat(MirPixelFormat format)
    1.64 -{
    1.65 -    return mir_pixel_format_to_sdl_format[format];
    1.66 -}
    1.67 -
    1.68 -static int
    1.69 -MIR_VideoInit(_THIS);
    1.70 -
    1.71 -static void
    1.72 -MIR_VideoQuit(_THIS);
    1.73 -
    1.74 -static int
    1.75 -MIR_GetDisplayBounds(_THIS, SDL_VideoDisplay* display, SDL_Rect* rect);
    1.76 -
    1.77 -static void
    1.78 -MIR_GetDisplayModes(_THIS, SDL_VideoDisplay* sdl_display);
    1.79 -
    1.80 -static int
    1.81 -MIR_SetDisplayMode(_THIS, SDL_VideoDisplay* sdl_display, SDL_DisplayMode* mode);
    1.82 -
    1.83 -static SDL_WindowShaper*
    1.84 -MIR_CreateShaper(SDL_Window* window)
    1.85 -{
    1.86 -    /* FIXME Im not sure if mir support this atm, will have to come back to this */
    1.87 -    return NULL;
    1.88 -}
    1.89 -
    1.90 -static int
    1.91 -MIR_SetWindowShape(SDL_WindowShaper* shaper, SDL_Surface* shape, SDL_WindowShapeMode* shape_mode)
    1.92 -{
    1.93 -    return SDL_Unsupported();
    1.94 -}
    1.95 -
    1.96 -static int
    1.97 -MIR_ResizeWindowShape(SDL_Window* window)
    1.98 -{
    1.99 -    return SDL_Unsupported();
   1.100 -}
   1.101 -
   1.102 -static int
   1.103 -MIR_Available()
   1.104 -{
   1.105 -    int available = 0;
   1.106 -
   1.107 -    if (SDL_MIR_LoadSymbols()) {
   1.108 -
   1.109 -        /* Lets ensure we can connect to the mir server */
   1.110 -        MirConnection* connection = MIR_mir_connect_sync(NULL, SDL_FUNCTION);
   1.111 -
   1.112 -        if (!MIR_mir_connection_is_valid(connection)) {
   1.113 -            SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Unable to connect to the mir server %s",
   1.114 -                MIR_mir_connection_get_error_message(connection));
   1.115 -
   1.116 -            return available;
   1.117 -        }
   1.118 -
   1.119 -        MIR_mir_connection_release(connection);
   1.120 -
   1.121 -        available = 1;
   1.122 -        SDL_MIR_UnloadSymbols();
   1.123 -    }
   1.124 -
   1.125 -    return available;
   1.126 -}
   1.127 -
   1.128 -static void
   1.129 -MIR_DeleteDevice(SDL_VideoDevice* device)
   1.130 -{
   1.131 -    SDL_free(device);
   1.132 -    SDL_MIR_UnloadSymbols();
   1.133 -}
   1.134 -
   1.135 -static void
   1.136 -MIR_PumpEvents(_THIS)
   1.137 -{
   1.138 -}
   1.139 -
   1.140 -static SDL_VideoDevice*
   1.141 -MIR_CreateDevice(int device_index)
   1.142 -{
   1.143 -    MIR_Data* mir_data;
   1.144 -    SDL_VideoDevice* device = NULL;
   1.145 -
   1.146 -    if (!SDL_MIR_LoadSymbols()) {
   1.147 -        return NULL;
   1.148 -    }
   1.149 -
   1.150 -    device = SDL_calloc(1, sizeof(SDL_VideoDevice));
   1.151 -    if (!device) {
   1.152 -        SDL_MIR_UnloadSymbols();
   1.153 -        SDL_OutOfMemory();
   1.154 -        return NULL;
   1.155 -    }
   1.156 -
   1.157 -    mir_data = SDL_calloc(1, sizeof(MIR_Data));
   1.158 -    if (!mir_data) {
   1.159 -        SDL_free(device);
   1.160 -        SDL_MIR_UnloadSymbols();
   1.161 -        SDL_OutOfMemory();
   1.162 -        return NULL;
   1.163 -    }
   1.164 -
   1.165 -    device->driverdata = mir_data;
   1.166 -
   1.167 -    /* mirvideo */
   1.168 -    device->VideoInit        = MIR_VideoInit;
   1.169 -    device->VideoQuit        = MIR_VideoQuit;
   1.170 -    device->GetDisplayBounds = MIR_GetDisplayBounds;
   1.171 -    device->GetDisplayModes  = MIR_GetDisplayModes;
   1.172 -    device->SetDisplayMode   = MIR_SetDisplayMode;
   1.173 -    device->free             = MIR_DeleteDevice;
   1.174 -
   1.175 -    /* miropengles */
   1.176 -    device->GL_SwapWindow      = MIR_GL_SwapWindow;
   1.177 -    device->GL_MakeCurrent     = MIR_GL_MakeCurrent;
   1.178 -    device->GL_CreateContext   = MIR_GL_CreateContext;
   1.179 -    device->GL_DeleteContext   = MIR_GL_DeleteContext;
   1.180 -    device->GL_LoadLibrary     = MIR_GL_LoadLibrary;
   1.181 -    device->GL_UnloadLibrary   = MIR_GL_UnloadLibrary;
   1.182 -    device->GL_GetSwapInterval = MIR_GL_GetSwapInterval;
   1.183 -    device->GL_SetSwapInterval = MIR_GL_SetSwapInterval;
   1.184 -    device->GL_GetProcAddress  = MIR_GL_GetProcAddress;
   1.185 -
   1.186 -    /* mirwindow */
   1.187 -    device->CreateSDLWindow         = MIR_CreateWindow;
   1.188 -    device->DestroyWindow        = MIR_DestroyWindow;
   1.189 -    device->GetWindowWMInfo      = MIR_GetWindowWMInfo;
   1.190 -    device->SetWindowFullscreen  = MIR_SetWindowFullscreen;
   1.191 -    device->MaximizeWindow       = MIR_MaximizeWindow;
   1.192 -    device->MinimizeWindow       = MIR_MinimizeWindow;
   1.193 -    device->RestoreWindow        = MIR_RestoreWindow;
   1.194 -    device->ShowWindow           = MIR_RestoreWindow;
   1.195 -    device->HideWindow           = MIR_HideWindow;
   1.196 -    device->SetWindowSize        = MIR_SetWindowSize;
   1.197 -    device->SetWindowMinimumSize = MIR_SetWindowMinimumSize;
   1.198 -    device->SetWindowMaximumSize = MIR_SetWindowMaximumSize;
   1.199 -    device->SetWindowTitle       = MIR_SetWindowTitle;
   1.200 -    device->SetWindowGrab        = MIR_SetWindowGrab;
   1.201 -    device->SetWindowGammaRamp   = MIR_SetWindowGammaRamp;
   1.202 -    device->GetWindowGammaRamp   = MIR_GetWindowGammaRamp;
   1.203 -
   1.204 -    device->CreateSDLWindowFrom     = NULL;
   1.205 -    device->SetWindowIcon        = NULL;
   1.206 -    device->RaiseWindow          = NULL;
   1.207 -    device->SetWindowBordered    = NULL;
   1.208 -    device->SetWindowResizable   = NULL;
   1.209 -    device->OnWindowEnter        = NULL;
   1.210 -    device->SetWindowPosition    = NULL;
   1.211 -
   1.212 -    /* mirframebuffer */
   1.213 -    device->CreateWindowFramebuffer  = MIR_CreateWindowFramebuffer;
   1.214 -    device->UpdateWindowFramebuffer  = MIR_UpdateWindowFramebuffer;
   1.215 -    device->DestroyWindowFramebuffer = MIR_DestroyWindowFramebuffer;
   1.216 -
   1.217 -    device->shape_driver.CreateShaper      = MIR_CreateShaper;
   1.218 -    device->shape_driver.SetWindowShape    = MIR_SetWindowShape;
   1.219 -    device->shape_driver.ResizeWindowShape = MIR_ResizeWindowShape;
   1.220 -
   1.221 -    device->PumpEvents = MIR_PumpEvents;
   1.222 -
   1.223 -    device->SuspendScreenSaver = NULL;
   1.224 -
   1.225 -    device->StartTextInput   = NULL;
   1.226 -    device->StopTextInput    = NULL;
   1.227 -    device->SetTextInputRect = NULL;
   1.228 -
   1.229 -    device->HasScreenKeyboardSupport = NULL;
   1.230 -    device->ShowScreenKeyboard       = NULL;
   1.231 -    device->HideScreenKeyboard       = NULL;
   1.232 -    device->IsScreenKeyboardShown    = NULL;
   1.233 -
   1.234 -    device->SetClipboardText = NULL;
   1.235 -    device->GetClipboardText = NULL;
   1.236 -    device->HasClipboardText = NULL;
   1.237 -
   1.238 -    device->ShowMessageBox = NULL;
   1.239 -
   1.240 -#if SDL_VIDEO_VULKAN
   1.241 -    device->Vulkan_LoadLibrary = MIR_Vulkan_LoadLibrary;
   1.242 -    device->Vulkan_UnloadLibrary = MIR_Vulkan_UnloadLibrary;
   1.243 -    device->Vulkan_GetInstanceExtensions = MIR_Vulkan_GetInstanceExtensions;
   1.244 -    device->Vulkan_CreateSurface = MIR_Vulkan_CreateSurface;
   1.245 -#endif
   1.246 -
   1.247 -    return device;
   1.248 -}
   1.249 -
   1.250 -VideoBootStrap MIR_bootstrap = {
   1.251 -    MIR_DRIVER_NAME, "SDL Mir video driver",
   1.252 -    MIR_Available, MIR_CreateDevice
   1.253 -};
   1.254 -
   1.255 -static SDL_DisplayMode
   1.256 -MIR_ConvertModeToSDLMode(MirOutputMode const* mode, MirPixelFormat format)
   1.257 -{
   1.258 -    SDL_DisplayMode sdl_mode  = {
   1.259 -        .format = MIR_GetSDLPixelFormat(format),
   1.260 -        .w      = MIR_mir_output_mode_get_width(mode),
   1.261 -        .h      = MIR_mir_output_mode_get_height(mode),
   1.262 -        .refresh_rate = MIR_mir_output_mode_get_refresh_rate(mode),
   1.263 -        .driverdata   = NULL
   1.264 -    };
   1.265 -
   1.266 -    return sdl_mode;
   1.267 -}
   1.268 -
   1.269 -static void
   1.270 -MIR_AddModeToDisplay(SDL_VideoDisplay* display, MirOutputMode const* mode, MirPixelFormat format)
   1.271 -{
   1.272 -    SDL_DisplayMode sdl_mode = MIR_ConvertModeToSDLMode(mode, format);
   1.273 -    SDL_AddDisplayMode(display, &sdl_mode);
   1.274 -}
   1.275 -
   1.276 -static void
   1.277 -MIR_InitDisplayFromOutput(_THIS, MirOutput* output)
   1.278 -{
   1.279 -    SDL_VideoDisplay display;
   1.280 -    int m;
   1.281 -
   1.282 -    MirPixelFormat format = MIR_mir_output_get_current_pixel_format(output);
   1.283 -    int num_modes         = MIR_mir_output_get_num_modes(output);
   1.284 -    SDL_DisplayMode current_mode = MIR_ConvertModeToSDLMode(MIR_mir_output_get_current_mode(output), format);
   1.285 -
   1.286 -    SDL_zero(display);
   1.287 -
   1.288 -    // Unfortunate cast, but SDL_AddVideoDisplay will strdup this pointer so its read-only in this case.
   1.289 -    display.name = (char*)MIR_mir_output_type_name(MIR_mir_output_get_type(output));
   1.290 -
   1.291 -    for (m = 0; m < num_modes; m++) {
   1.292 -        MirOutputMode const* mode = MIR_mir_output_get_mode(output, m);
   1.293 -        MIR_AddModeToDisplay(&display, mode, format);
   1.294 -    }
   1.295 -
   1.296 -    display.desktop_mode = current_mode;
   1.297 -    display.current_mode = current_mode;
   1.298 -
   1.299 -    display.driverdata = output;
   1.300 -    SDL_AddVideoDisplay(&display);
   1.301 -}
   1.302 -
   1.303 -static void
   1.304 -MIR_InitDisplays(_THIS)
   1.305 -{
   1.306 -    MIR_Data* mir_data = _this->driverdata;
   1.307 -    int num_outputs    = MIR_mir_display_config_get_num_outputs(mir_data->display_config);
   1.308 -    int d;
   1.309 -
   1.310 -    for (d = 0; d < num_outputs; d++) {
   1.311 -        MirOutput* output = MIR_mir_display_config_get_mutable_output(mir_data->display_config, d);
   1.312 -        SDL_bool enabled  = MIR_mir_output_is_enabled(output);
   1.313 -        MirOutputConnectionState state = MIR_mir_output_get_connection_state(output);
   1.314 -
   1.315 -        if (enabled && state == mir_output_connection_state_connected) {
   1.316 -            MIR_InitDisplayFromOutput(_this, output);
   1.317 -        }
   1.318 -    }
   1.319 -}
   1.320 -
   1.321 -static int
   1.322 -MIR_VideoInit(_THIS)
   1.323 -{
   1.324 -    MIR_Data* mir_data = _this->driverdata;
   1.325 -
   1.326 -    mir_data->connection     = MIR_mir_connect_sync(NULL, SDL_FUNCTION);
   1.327 -    mir_data->current_window = NULL;
   1.328 -    mir_data->software       = SDL_FALSE;
   1.329 -    mir_data->pixel_format   = mir_pixel_format_invalid;
   1.330 -
   1.331 -    if (!MIR_mir_connection_is_valid(mir_data->connection)) {
   1.332 -        return SDL_SetError("Failed to connect to the mir server: %s",
   1.333 -            MIR_mir_connection_get_error_message(mir_data->connection));
   1.334 -    }
   1.335 -
   1.336 -    mir_data->display_config = MIR_mir_connection_create_display_configuration(mir_data->connection);
   1.337 -
   1.338 -    MIR_InitDisplays(_this);
   1.339 -    MIR_InitMouse();
   1.340 -
   1.341 -    return 0;
   1.342 -}
   1.343 -
   1.344 -static void
   1.345 -MIR_CleanUpDisplayConfig(_THIS)
   1.346 -{
   1.347 -    MIR_Data* mir_data = _this->driverdata;
   1.348 -    int i;
   1.349 -
   1.350 -    // SDL_VideoQuit frees the display driverdata, we own it not them
   1.351 -    for (i = 0; i < _this->num_displays; ++i) {
   1.352 -        _this->displays[i].driverdata = NULL;
   1.353 -    }
   1.354 -
   1.355 -    MIR_mir_display_config_release(mir_data->display_config);
   1.356 -}
   1.357 -
   1.358 -static void
   1.359 -MIR_VideoQuit(_THIS)
   1.360 -{
   1.361 -    MIR_Data* mir_data = _this->driverdata;
   1.362 -
   1.363 -    MIR_CleanUpDisplayConfig(_this);
   1.364 -
   1.365 -    MIR_FiniMouse();
   1.366 -
   1.367 -    MIR_GL_DeleteContext(_this, NULL);
   1.368 -    MIR_GL_UnloadLibrary(_this);
   1.369 -
   1.370 -    MIR_mir_connection_release(mir_data->connection);
   1.371 -
   1.372 -    SDL_free(mir_data);
   1.373 -    _this->driverdata = NULL;
   1.374 -}
   1.375 -
   1.376 -static int
   1.377 -MIR_GetDisplayBounds(_THIS, SDL_VideoDisplay* display, SDL_Rect* rect)
   1.378 -{
   1.379 -    MirOutput const* output = display->driverdata;
   1.380 -
   1.381 -    rect->x = MIR_mir_output_get_position_x(output);
   1.382 -    rect->y = MIR_mir_output_get_position_y(output);
   1.383 -    rect->w = display->current_mode.w;
   1.384 -    rect->h = display->current_mode.h;
   1.385 -
   1.386 -    return 0;
   1.387 -}
   1.388 -
   1.389 -static void
   1.390 -MIR_GetDisplayModes(_THIS, SDL_VideoDisplay* display)
   1.391 -{
   1.392 -}
   1.393 -
   1.394 -static int
   1.395 -MIR_SetDisplayMode(_THIS, SDL_VideoDisplay* display, SDL_DisplayMode* mode)
   1.396 -{
   1.397 -    int m;
   1.398 -    MirOutput* output = display->driverdata;
   1.399 -    int num_modes     = MIR_mir_output_get_num_modes(output);
   1.400 -    Uint32 sdl_format = MIR_GetSDLPixelFormat(
   1.401 -                            MIR_mir_output_get_current_pixel_format(output));
   1.402 -
   1.403 -    for (m = 0; m < num_modes; m++) {
   1.404 -        MirOutputMode const* mir_mode = MIR_mir_output_get_mode(output, m);
   1.405 -        int width  = MIR_mir_output_mode_get_width(mir_mode);
   1.406 -        int height = MIR_mir_output_mode_get_height(mir_mode);
   1.407 -        double refresh_rate = MIR_mir_output_mode_get_refresh_rate(mir_mode);
   1.408 -
   1.409 -        if (mode->format == sdl_format &&
   1.410 -            mode->w      == width &&
   1.411 -            mode->h      == height &&
   1.412 -            mode->refresh_rate == refresh_rate) {
   1.413 -
   1.414 -            // FIXME Currently wont actually *set* anything. Need to wait for applying display changes
   1.415 -            MIR_mir_output_set_current_mode(output, mir_mode);
   1.416 -            return 0;
   1.417 -        }
   1.418 -    }
   1.419 -
   1.420 -    return -1;
   1.421 -}
   1.422 -
   1.423 -#endif /* SDL_VIDEO_DRIVER_MIR */
   1.424 -
   1.425 -/* vi: set ts=4 sw=4 expandtab: */
   1.426 -