1 //========================================================================
2 // GLFW 3.2 Mir - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2014-2015 Brandon Schaefer <brandon.schaefer@canonical.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 <stdlib.h>
30
31
32 //////////////////////////////////////////////////////////////////////////
33 ////// GLFW platform API //////
34 //////////////////////////////////////////////////////////////////////////
35
_glfwPlatformGetMonitors(int * count)36 _GLFWmonitor** _glfwPlatformGetMonitors(int* count)
37 {
38 int i, found = 0;
39 _GLFWmonitor** monitors = NULL;
40 MirDisplayConfiguration* displayConfig =
41 mir_connection_create_display_config(_glfw.mir.connection);
42
43 *count = 0;
44
45 for (i = 0; i < displayConfig->num_outputs; i++)
46 {
47 const MirDisplayOutput* out = displayConfig->outputs + i;
48
49 if (out->used &&
50 out->connected &&
51 out->num_modes &&
52 out->current_mode < out->num_modes)
53 {
54 found++;
55 monitors = realloc(monitors, sizeof(_GLFWmonitor*) * found);
56 monitors[i] = _glfwAllocMonitor("Unknown",
57 out->physical_width_mm,
58 out->physical_height_mm);
59
60 monitors[i]->mir.x = out->position_x;
61 monitors[i]->mir.y = out->position_y;
62 monitors[i]->mir.output_id = out->output_id;
63 monitors[i]->mir.cur_mode = out->current_mode;
64
65 monitors[i]->modes = _glfwPlatformGetVideoModes(monitors[i],
66 &monitors[i]->modeCount);
67 }
68 }
69
70 mir_display_config_destroy(displayConfig);
71
72 *count = found;
73 return monitors;
74 }
75
_glfwPlatformIsSameMonitor(_GLFWmonitor * first,_GLFWmonitor * second)76 GLFWbool _glfwPlatformIsSameMonitor(_GLFWmonitor* first, _GLFWmonitor* second)
77 {
78 return first->mir.output_id == second->mir.output_id;
79 }
80
_glfwPlatformGetMonitorPos(_GLFWmonitor * monitor,int * xpos,int * ypos)81 void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos)
82 {
83 if (xpos)
84 *xpos = monitor->mir.x;
85 if (ypos)
86 *ypos = monitor->mir.y;
87 }
88
FillInRGBBitsFromPixelFormat(GLFWvidmode * mode,const MirPixelFormat pf)89 void FillInRGBBitsFromPixelFormat(GLFWvidmode* mode, const MirPixelFormat pf)
90 {
91 switch (pf)
92 {
93 case mir_pixel_format_rgb_565:
94 mode->redBits = 5;
95 mode->greenBits = 6;
96 mode->blueBits = 5;
97 break;
98 case mir_pixel_format_rgba_5551:
99 mode->redBits = 5;
100 mode->greenBits = 5;
101 mode->blueBits = 5;
102 break;
103 case mir_pixel_format_rgba_4444:
104 mode->redBits = 4;
105 mode->greenBits = 4;
106 mode->blueBits = 4;
107 break;
108 case mir_pixel_format_abgr_8888:
109 case mir_pixel_format_xbgr_8888:
110 case mir_pixel_format_argb_8888:
111 case mir_pixel_format_xrgb_8888:
112 case mir_pixel_format_bgr_888:
113 case mir_pixel_format_rgb_888:
114 default:
115 mode->redBits = 8;
116 mode->greenBits = 8;
117 mode->blueBits = 8;
118 break;
119 }
120 }
121
_glfwPlatformGetVideoModes(_GLFWmonitor * monitor,int * found)122 GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found)
123 {
124 int i;
125 GLFWvidmode* modes = NULL;
126 MirDisplayConfiguration* displayConfig =
127 mir_connection_create_display_config(_glfw.mir.connection);
128
129 for (i = 0; i < displayConfig->num_outputs; i++)
130 {
131 const MirDisplayOutput* out = displayConfig->outputs + i;
132 if (out->output_id != monitor->mir.output_id)
133 continue;
134
135 modes = calloc(out->num_modes, sizeof(GLFWvidmode));
136
137 for (*found = 0; *found < out->num_modes; (*found)++)
138 {
139 modes[*found].width = out->modes[*found].horizontal_resolution;
140 modes[*found].height = out->modes[*found].vertical_resolution;
141 modes[*found].refreshRate = out->modes[*found].refresh_rate;
142
143 FillInRGBBitsFromPixelFormat(&modes[*found], out->output_formats[*found]);
144 }
145
146 break;
147 }
148
149 mir_display_config_destroy(displayConfig);
150
151 return modes;
152 }
153
_glfwPlatformGetVideoMode(_GLFWmonitor * monitor,GLFWvidmode * mode)154 void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
155 {
156 *mode = monitor->modes[monitor->mir.cur_mode];
157 }
158
_glfwPlatformGetGammaRamp(_GLFWmonitor * monitor,GLFWgammaramp * ramp)159 void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
160 {
161 _glfwInputError(GLFW_PLATFORM_ERROR,
162 "Mir: Unsupported function %s", __PRETTY_FUNCTION__);
163 }
164
_glfwPlatformSetGammaRamp(_GLFWmonitor * monitor,const GLFWgammaramp * ramp)165 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)
166 {
167 _glfwInputError(GLFW_PLATFORM_ERROR,
168 "Mir: Unsupported function %s", __PRETTY_FUNCTION__);
169 }
170
171
172 //////////////////////////////////////////////////////////////////////////
173 ////// GLFW native API //////
174 //////////////////////////////////////////////////////////////////////////
175
glfwGetMirMonitor(GLFWmonitor * handle)176 GLFWAPI int glfwGetMirMonitor(GLFWmonitor* handle)
177 {
178 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
179 _GLFW_REQUIRE_INIT_OR_RETURN(0);
180 return monitor->mir.output_id;
181 }
182
183