wl_monitor.c (8515B)
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 // It is fine to use C99 in this file because it will not be built with VS 27 //======================================================================== 28 29 #include "internal.h" 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <errno.h> 35 #include <math.h> 36 37 38 static void outputHandleGeometry(void* userData, 39 struct wl_output* output, 40 int32_t x, 41 int32_t y, 42 int32_t physicalWidth, 43 int32_t physicalHeight, 44 int32_t subpixel, 45 const char* make, 46 const char* model, 47 int32_t transform) 48 { 49 struct _GLFWmonitor* monitor = userData; 50 51 monitor->wl.x = x; 52 monitor->wl.y = y; 53 monitor->widthMM = physicalWidth; 54 monitor->heightMM = physicalHeight; 55 56 if (strlen(monitor->name) == 0) 57 snprintf(monitor->name, sizeof(monitor->name), "%s %s", make, model); 58 } 59 60 static void outputHandleMode(void* userData, 61 struct wl_output* output, 62 uint32_t flags, 63 int32_t width, 64 int32_t height, 65 int32_t refresh) 66 { 67 struct _GLFWmonitor* monitor = userData; 68 GLFWvidmode mode; 69 70 mode.width = width; 71 mode.height = height; 72 mode.redBits = 8; 73 mode.greenBits = 8; 74 mode.blueBits = 8; 75 mode.refreshRate = (int) round(refresh / 1000.0); 76 77 monitor->modeCount++; 78 monitor->modes = 79 realloc(monitor->modes, monitor->modeCount * sizeof(GLFWvidmode)); 80 monitor->modes[monitor->modeCount - 1] = mode; 81 82 if (flags & WL_OUTPUT_MODE_CURRENT) 83 monitor->wl.currentMode = monitor->modeCount - 1; 84 } 85 86 static void outputHandleDone(void* userData, struct wl_output* output) 87 { 88 struct _GLFWmonitor* monitor = userData; 89 90 if (monitor->widthMM <= 0 || monitor->heightMM <= 0) 91 { 92 // If Wayland does not provide a physical size, assume the default 96 DPI 93 const GLFWvidmode* mode = &monitor->modes[monitor->wl.currentMode]; 94 monitor->widthMM = (int) (mode->width * 25.4f / 96.f); 95 monitor->heightMM = (int) (mode->height * 25.4f / 96.f); 96 } 97 98 for (int i = 0; i < _glfw.monitorCount; i++) 99 { 100 if (_glfw.monitors[i] == monitor) 101 return; 102 } 103 104 _glfwInputMonitor(monitor, GLFW_CONNECTED, _GLFW_INSERT_LAST); 105 } 106 107 static void outputHandleScale(void* userData, 108 struct wl_output* output, 109 int32_t factor) 110 { 111 struct _GLFWmonitor* monitor = userData; 112 113 monitor->wl.scale = factor; 114 115 for (_GLFWwindow* window = _glfw.windowListHead; window; window = window->next) 116 { 117 for (int i = 0; i < window->wl.monitorsCount; i++) 118 { 119 if (window->wl.monitors[i] == monitor) 120 { 121 _glfwUpdateContentScaleWayland(window); 122 break; 123 } 124 } 125 } 126 } 127 128 #ifdef WL_OUTPUT_NAME_SINCE_VERSION 129 130 void outputHandleName(void* userData, struct wl_output* wl_output, const char* name) 131 { 132 struct _GLFWmonitor* monitor = userData; 133 134 strncpy(monitor->name, name, sizeof(monitor->name) - 1); 135 } 136 137 void outputHandleDescription(void* userData, 138 struct wl_output* wl_output, 139 const char* description) 140 { 141 } 142 143 #endif // WL_OUTPUT_NAME_SINCE_VERSION 144 145 static const struct wl_output_listener outputListener = 146 { 147 outputHandleGeometry, 148 outputHandleMode, 149 outputHandleDone, 150 outputHandleScale, 151 #ifdef WL_OUTPUT_NAME_SINCE_VERSION 152 outputHandleName, 153 outputHandleDescription, 154 #endif 155 }; 156 157 158 ////////////////////////////////////////////////////////////////////////// 159 ////// GLFW internal API ////// 160 ////////////////////////////////////////////////////////////////////////// 161 162 void _glfwAddOutputWayland(uint32_t name, uint32_t version) 163 { 164 if (version < 2) 165 { 166 _glfwInputError(GLFW_PLATFORM_ERROR, 167 "Wayland: Unsupported output interface version"); 168 return; 169 } 170 171 #ifdef WL_OUTPUT_NAME_SINCE_VERSION 172 version = _glfw_min(version, WL_OUTPUT_NAME_SINCE_VERSION); 173 #else 174 version = 2; 175 #endif 176 177 struct wl_output* output = wl_registry_bind(_glfw.wl.registry, 178 name, 179 &wl_output_interface, 180 version); 181 if (!output) 182 return; 183 184 // The actual name of this output will be set in the geometry handler 185 _GLFWmonitor* monitor = _glfwAllocMonitor("", 0, 0); 186 monitor->wl.scale = 1; 187 monitor->wl.output = output; 188 monitor->wl.name = name; 189 190 wl_output_add_listener(output, &outputListener, monitor); 191 } 192 193 194 ////////////////////////////////////////////////////////////////////////// 195 ////// GLFW platform API ////// 196 ////////////////////////////////////////////////////////////////////////// 197 198 void _glfwPlatformFreeMonitor(_GLFWmonitor* monitor) 199 { 200 if (monitor->wl.output) 201 wl_output_destroy(monitor->wl.output); 202 } 203 204 void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos) 205 { 206 if (xpos) 207 *xpos = monitor->wl.x; 208 if (ypos) 209 *ypos = monitor->wl.y; 210 } 211 212 void _glfwPlatformGetMonitorContentScale(_GLFWmonitor* monitor, 213 float* xscale, float* yscale) 214 { 215 if (xscale) 216 *xscale = (float) monitor->wl.scale; 217 if (yscale) 218 *yscale = (float) monitor->wl.scale; 219 } 220 221 void _glfwPlatformGetMonitorWorkarea(_GLFWmonitor* monitor, 222 int* xpos, int* ypos, 223 int* width, int* height) 224 { 225 if (xpos) 226 *xpos = monitor->wl.x; 227 if (ypos) 228 *ypos = monitor->wl.y; 229 if (width) 230 *width = monitor->modes[monitor->wl.currentMode].width; 231 if (height) 232 *height = monitor->modes[monitor->wl.currentMode].height; 233 } 234 235 GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found) 236 { 237 *found = monitor->modeCount; 238 return monitor->modes; 239 } 240 241 void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) 242 { 243 *mode = monitor->modes[monitor->wl.currentMode]; 244 } 245 246 GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp) 247 { 248 _glfwInputError(GLFW_PLATFORM_ERROR, 249 "Wayland: Gamma ramp access is not available"); 250 return GLFW_FALSE; 251 } 252 253 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, 254 const GLFWgammaramp* ramp) 255 { 256 _glfwInputError(GLFW_PLATFORM_ERROR, 257 "Wayland: Gamma ramp access is not available"); 258 } 259 260 261 ////////////////////////////////////////////////////////////////////////// 262 ////// GLFW native API ////// 263 ////////////////////////////////////////////////////////////////////////// 264 265 GLFWAPI struct wl_output* glfwGetWaylandMonitor(GLFWmonitor* handle) 266 { 267 _GLFWmonitor* monitor = (_GLFWmonitor*) handle; 268 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 269 return monitor->wl.output; 270 } 271