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