]> git.sesse.net Git - pistorm/blob - raylib_pi4_test/external/glfw/src/wl_monitor.c
Update raylib files and Makefile for Pi 4 testing
[pistorm] / raylib_pi4_test / external / glfw / src / wl_monitor.c
1 //========================================================================
2 // GLFW 3.4 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* data,
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 = data;
50     char name[1024];
51
52     monitor->wl.x = x;
53     monitor->wl.y = y;
54     monitor->widthMM = physicalWidth;
55     monitor->heightMM = physicalHeight;
56
57     snprintf(name, sizeof(name), "%s %s", make, model);
58     monitor->name = _glfw_strdup(name);
59 }
60
61 static void outputHandleMode(void* data,
62                              struct wl_output* output,
63                              uint32_t flags,
64                              int32_t width,
65                              int32_t height,
66                              int32_t refresh)
67 {
68     struct _GLFWmonitor *monitor = data;
69     GLFWvidmode mode;
70
71     mode.width = width;
72     mode.height = height;
73     mode.redBits = 8;
74     mode.greenBits = 8;
75     mode.blueBits = 8;
76     mode.refreshRate = (int) round(refresh / 1000.0);
77
78     monitor->modeCount++;
79     monitor->modes =
80         realloc(monitor->modes, monitor->modeCount * sizeof(GLFWvidmode));
81     monitor->modes[monitor->modeCount - 1] = mode;
82
83     if (flags & WL_OUTPUT_MODE_CURRENT)
84         monitor->wl.currentMode = monitor->modeCount - 1;
85 }
86
87 static void outputHandleDone(void* data, struct wl_output* output)
88 {
89     struct _GLFWmonitor *monitor = data;
90
91     _glfwInputMonitor(monitor, GLFW_CONNECTED, _GLFW_INSERT_LAST);
92 }
93
94 static void outputHandleScale(void* data,
95                               struct wl_output* output,
96                               int32_t factor)
97 {
98     struct _GLFWmonitor *monitor = data;
99
100     monitor->wl.scale = factor;
101 }
102
103 static const struct wl_output_listener outputListener = {
104     outputHandleGeometry,
105     outputHandleMode,
106     outputHandleDone,
107     outputHandleScale,
108 };
109
110
111 //////////////////////////////////////////////////////////////////////////
112 //////                       GLFW internal API                      //////
113 //////////////////////////////////////////////////////////////////////////
114
115 void _glfwAddOutputWayland(uint32_t name, uint32_t version)
116 {
117     _GLFWmonitor *monitor;
118     struct wl_output *output;
119
120     if (version < 2)
121     {
122         _glfwInputError(GLFW_PLATFORM_ERROR,
123                         "Wayland: Unsupported output interface version");
124         return;
125     }
126
127     // The actual name of this output will be set in the geometry handler.
128     monitor = _glfwAllocMonitor(NULL, 0, 0);
129
130     output = wl_registry_bind(_glfw.wl.registry,
131                               name,
132                               &wl_output_interface,
133                               2);
134     if (!output)
135     {
136         _glfwFreeMonitor(monitor);
137         return;
138     }
139
140     monitor->wl.scale = 1;
141     monitor->wl.output = output;
142     monitor->wl.name = name;
143
144     wl_output_add_listener(output, &outputListener, monitor);
145 }
146
147
148 //////////////////////////////////////////////////////////////////////////
149 //////                       GLFW platform API                      //////
150 //////////////////////////////////////////////////////////////////////////
151
152 void _glfwPlatformFreeMonitor(_GLFWmonitor* monitor)
153 {
154     if (monitor->wl.output)
155         wl_output_destroy(monitor->wl.output);
156 }
157
158 void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos)
159 {
160     if (xpos)
161         *xpos = monitor->wl.x;
162     if (ypos)
163         *ypos = monitor->wl.y;
164 }
165
166 void _glfwPlatformGetMonitorContentScale(_GLFWmonitor* monitor,
167                                          float* xscale, float* yscale)
168 {
169     if (xscale)
170         *xscale = (float) monitor->wl.scale;
171     if (yscale)
172         *yscale = (float) monitor->wl.scale;
173 }
174
175 void _glfwPlatformGetMonitorWorkarea(_GLFWmonitor* monitor,
176                                      int* xpos, int* ypos,
177                                      int* width, int* height)
178 {
179     if (xpos)
180         *xpos = monitor->wl.x;
181     if (ypos)
182         *ypos = monitor->wl.y;
183     if (width)
184         *width = monitor->modes[monitor->wl.currentMode].width;
185     if (height)
186         *height = monitor->modes[monitor->wl.currentMode].height;
187 }
188
189 GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found)
190 {
191     *found = monitor->modeCount;
192     return monitor->modes;
193 }
194
195 void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
196 {
197     *mode = monitor->modes[monitor->wl.currentMode];
198 }
199
200 GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
201 {
202     _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
203                     "Wayland: Gamma ramp access is not available");
204     return GLFW_FALSE;
205 }
206
207 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor,
208                                const GLFWgammaramp* ramp)
209 {
210     _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
211                     "Wayland: Gamma ramp access is not available");
212 }
213
214
215 //////////////////////////////////////////////////////////////////////////
216 //////                        GLFW native API                       //////
217 //////////////////////////////////////////////////////////////////////////
218
219 GLFWAPI struct wl_output* glfwGetWaylandMonitor(GLFWmonitor* handle)
220 {
221     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
222     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
223     return monitor->wl.output;
224 }
225