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