mir_monitor.cc (7223B)
1 //======================================================================== 2 // GLFW 3.3 Mir - www.glfw.org 3 //------------------------------------------------------------------------ 4 // Copyright (c) 2014-2017 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 internal API ////// 34 ////////////////////////////////////////////////////////////////////////// 35 36 // Poll for changes in the set of connected monitors 37 // 38 void _glfwPollMonitorsMir(void) 39 { 40 int i; 41 MirDisplayConfig* displayConfig = 42 mir_connection_create_display_configuration(_glfw.mir.connection); 43 44 int numOutputs = mir_display_config_get_num_outputs(displayConfig); 45 46 for (i = 0; i < numOutputs; i++) 47 { 48 const MirOutput* output = mir_display_config_get_output(displayConfig, i); 49 MirOutputConnectionState state = mir_output_get_connection_state(output); 50 bool enabled = mir_output_is_enabled(output); 51 52 if (enabled && state == mir_output_connection_state_connected) 53 { 54 int widthMM = mir_output_get_physical_width_mm(output); 55 int heightMM = mir_output_get_physical_height_mm(output); 56 int x = mir_output_get_position_x(output); 57 int y = mir_output_get_position_y(output); 58 int id = mir_output_get_id(output); 59 size_t currentMode = mir_output_get_current_mode_index(output); 60 const char* name = mir_output_type_name(mir_output_get_type(output)); 61 62 _GLFWmonitor* monitor = _glfwAllocMonitor(name, 63 widthMM, 64 heightMM); 65 monitor->mir.x = x; 66 monitor->mir.y = y; 67 monitor->mir.outputId = id; 68 monitor->mir.curMode = currentMode; 69 monitor->modes = _glfwPlatformGetVideoModes(monitor, &monitor->modeCount); 70 71 _glfwInputMonitor(monitor, GLFW_CONNECTED, _GLFW_INSERT_LAST); 72 } 73 } 74 75 mir_display_config_release(displayConfig); 76 } 77 78 79 ////////////////////////////////////////////////////////////////////////// 80 ////// GLFW platform API ////// 81 ////////////////////////////////////////////////////////////////////////// 82 83 void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos) 84 { 85 if (xpos) 86 *xpos = monitor->mir.x; 87 if (ypos) 88 *ypos = monitor->mir.y; 89 } 90 91 static void FillInRGBBitsFromPixelFormat(GLFWvidmode* mode, const MirPixelFormat pf) 92 { 93 switch (pf) 94 { 95 case mir_pixel_format_rgb_565: 96 mode->redBits = 5; 97 mode->greenBits = 6; 98 mode->blueBits = 5; 99 break; 100 case mir_pixel_format_rgba_5551: 101 mode->redBits = 5; 102 mode->greenBits = 5; 103 mode->blueBits = 5; 104 break; 105 case mir_pixel_format_rgba_4444: 106 mode->redBits = 4; 107 mode->greenBits = 4; 108 mode->blueBits = 4; 109 break; 110 case mir_pixel_format_abgr_8888: 111 case mir_pixel_format_xbgr_8888: 112 case mir_pixel_format_argb_8888: 113 case mir_pixel_format_xrgb_8888: 114 case mir_pixel_format_bgr_888: 115 case mir_pixel_format_rgb_888: 116 default: 117 mode->redBits = 8; 118 mode->greenBits = 8; 119 mode->blueBits = 8; 120 break; 121 } 122 } 123 124 GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found) 125 { 126 int i; 127 GLFWvidmode* modes = NULL; 128 MirDisplayConfig* displayConfig = 129 mir_connection_create_display_configuration(_glfw.mir.connection); 130 131 int numOutputs = mir_display_config_get_num_outputs(displayConfig); 132 133 for (i = 0; i < numOutputs; i++) 134 { 135 const MirOutput* output = mir_display_config_get_output(displayConfig, i); 136 int id = mir_output_get_id(output); 137 138 if (id != monitor->mir.outputId) 139 continue; 140 141 MirOutputConnectionState state = mir_output_get_connection_state(output); 142 bool enabled = mir_output_is_enabled(output); 143 144 // We must have been disconnected 145 if (!enabled || state != mir_output_connection_state_connected) 146 { 147 _glfwInputError(GLFW_PLATFORM_ERROR, 148 "Mir: Monitor no longer connected"); 149 return NULL; 150 } 151 152 int numModes = mir_output_get_num_modes(output); 153 modes = calloc(numModes, sizeof(GLFWvidmode)); 154 155 for (*found = 0; *found < numModes; (*found)++) 156 { 157 const MirOutputMode* mode = mir_output_get_mode(output, *found); 158 int width = mir_output_mode_get_width(mode); 159 int height = mir_output_mode_get_height(mode); 160 double refreshRate = mir_output_mode_get_refresh_rate(mode); 161 MirPixelFormat currentFormat = mir_output_get_current_pixel_format(output); 162 163 modes[*found].width = width; 164 modes[*found].height = height; 165 modes[*found].refreshRate = refreshRate; 166 167 FillInRGBBitsFromPixelFormat(&modes[*found], currentFormat); 168 } 169 170 break; 171 } 172 173 mir_display_config_release(displayConfig); 174 175 return modes; 176 } 177 178 void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) 179 { 180 *mode = monitor->modes[monitor->mir.curMode]; 181 } 182 183 void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp) 184 { 185 _glfwInputError(GLFW_PLATFORM_ERROR, 186 "Mir: Unsupported function %s", __PRETTY_FUNCTION__); 187 } 188 189 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp) 190 { 191 _glfwInputError(GLFW_PLATFORM_ERROR, 192 "Mir: Unsupported function %s", __PRETTY_FUNCTION__); 193 } 194 195 196 ////////////////////////////////////////////////////////////////////////// 197 ////// GLFW native API ////// 198 ////////////////////////////////////////////////////////////////////////// 199 200 GLFWAPI int glfwGetMirMonitor(GLFWmonitor* handle) 201 { 202 _GLFWmonitor* monitor = (_GLFWmonitor*) handle; 203 _GLFW_REQUIRE_INIT_OR_RETURN(0); 204 return monitor->mir.outputId; 205 }