medfall

A super great game engine
Log | Files | Refs

wl_monitor.cc (5659B)


      1 //========================================================================
      2 // GLFW 3.3 Wayland - www.glfw.org
      3 //------------------------------------------------------------------------
      4 // Copyright (c) 2014 Jonas Ã…dahl <jadahl@gmail.com>
      5 //
      6 // This software is provided 'as-is', without any express or implied
      7 // warranty. In no event will the authors be held liable for any damages
      8 // arising from the use of this software.
      9 //
     10 // Permission is granted to anyone to use this software for any purpose,
     11 // including commercial applications, and to alter it and redistribute it
     12 // freely, subject to the following restrictions:
     13 //
     14 // 1. The origin of this software must not be misrepresented; you must not
     15 //    claim that you wrote the original software. If you use this software
     16 //    in a product, an acknowledgment in the product documentation would
     17 //    be appreciated but is not required.
     18 //
     19 // 2. Altered source versions must be plainly marked as such, and must not
     20 //    be misrepresented as being the original software.
     21 //
     22 // 3. This notice may not be removed or altered from any source
     23 //    distribution.
     24 //
     25 //========================================================================
     26 
     27 #include "internal.h"
     28 
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 #include <errno.h>
     33 
     34 
     35 static void geometry(void* data,
     36                      struct wl_output* output,
     37                      int32_t x,
     38                      int32_t y,
     39                      int32_t physicalWidth,
     40                      int32_t physicalHeight,
     41                      int32_t subpixel,
     42                      const char* make,
     43                      const char* model,
     44                      int32_t transform)
     45 {
     46     struct _GLFWmonitor *monitor = data;
     47     char name[1024];
     48 
     49     monitor->wl.x = x;
     50     monitor->wl.y = y;
     51     monitor->widthMM = physicalWidth;
     52     monitor->heightMM = physicalHeight;
     53 
     54     snprintf(name, sizeof(name), "%s %s", make, model);
     55     monitor->name = strdup(name);
     56 }
     57 
     58 static void mode(void* data,
     59                  struct wl_output* output,
     60                  uint32_t flags,
     61                  int32_t width,
     62                  int32_t height,
     63                  int32_t refresh)
     64 {
     65     struct _GLFWmonitor *monitor = data;
     66     GLFWvidmode mode;
     67 
     68     mode.width = width;
     69     mode.height = height;
     70     mode.redBits = 8;
     71     mode.greenBits = 8;
     72     mode.blueBits = 8;
     73     mode.refreshRate = refresh / 1000;
     74 
     75     monitor->modeCount++;
     76     monitor->modes =
     77         realloc(monitor->modes, monitor->modeCount * sizeof(GLFWvidmode));
     78     monitor->modes[monitor->modeCount - 1] = mode;
     79 
     80     if (flags & WL_OUTPUT_MODE_CURRENT)
     81         monitor->wl.currentMode = monitor->modeCount - 1;
     82 }
     83 
     84 static void done(void* data, struct wl_output* output)
     85 {
     86     struct _GLFWmonitor *monitor = data;
     87 
     88     _glfwInputMonitor(monitor, GLFW_CONNECTED, _GLFW_INSERT_LAST);
     89 }
     90 
     91 static void scale(void* data,
     92                   struct wl_output* output,
     93                   int32_t factor)
     94 {
     95     struct _GLFWmonitor *monitor = data;
     96 
     97     monitor->wl.scale = factor;
     98 }
     99 
    100 static const struct wl_output_listener outputListener = {
    101     geometry,
    102     mode,
    103     done,
    104     scale,
    105 };
    106 
    107 
    108 //////////////////////////////////////////////////////////////////////////
    109 //////                       GLFW internal API                      //////
    110 //////////////////////////////////////////////////////////////////////////
    111 
    112 void _glfwAddOutputWayland(uint32_t name, uint32_t version)
    113 {
    114     _GLFWmonitor *monitor;
    115     struct wl_output *output;
    116 
    117     if (version < 2)
    118     {
    119         _glfwInputError(GLFW_PLATFORM_ERROR,
    120                         "Wayland: Unsupported output interface version");
    121         return;
    122     }
    123 
    124     // The actual name of this output will be set in the geometry handler.
    125     monitor = _glfwAllocMonitor(NULL, 0, 0);
    126 
    127     output = wl_registry_bind(_glfw.wl.registry,
    128                               name,
    129                               &wl_output_interface,
    130                               2);
    131     if (!output)
    132     {
    133         _glfwFreeMonitor(monitor);
    134         return;
    135     }
    136 
    137     monitor->wl.scale = 1;
    138     monitor->wl.output = output;
    139 
    140     wl_output_add_listener(output, &outputListener, monitor);
    141 }
    142 
    143 
    144 //////////////////////////////////////////////////////////////////////////
    145 //////                       GLFW platform API                      //////
    146 //////////////////////////////////////////////////////////////////////////
    147 
    148 void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos)
    149 {
    150     if (xpos)
    151         *xpos = monitor->wl.x;
    152     if (ypos)
    153         *ypos = monitor->wl.y;
    154 }
    155 
    156 GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found)
    157 {
    158     *found = monitor->modeCount;
    159     return monitor->modes;
    160 }
    161 
    162 void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
    163 {
    164     *mode = monitor->modes[monitor->wl.currentMode];
    165 }
    166 
    167 void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
    168 {
    169     // TODO
    170     _glfwInputError(GLFW_PLATFORM_ERROR,
    171                     "Wayland: Gamma ramp getting not supported yet");
    172 }
    173 
    174 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)
    175 {
    176     // TODO
    177     _glfwInputError(GLFW_PLATFORM_ERROR,
    178                     "Wayland: Gamma ramp setting not supported yet");
    179 }
    180 
    181 
    182 //////////////////////////////////////////////////////////////////////////
    183 //////                        GLFW native API                       //////
    184 //////////////////////////////////////////////////////////////////////////
    185 
    186 GLFWAPI struct wl_output* glfwGetWaylandMonitor(GLFWmonitor* handle)
    187 {
    188     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    189     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    190     return monitor->wl.output;
    191 }
    192