]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/windows/display.cpp
09ca2e9edab55530bdc796dedc796141e6fa01ef
[libs/gui.git] / source / graphics / windows / display.cpp
1 #include "display.h"
2 #include "display_private.h"
3 #include <algorithm>
4 #include <windows.h>
5
6 using namespace std;
7
8 namespace Msp {
9 namespace Graphics {
10
11 Display::Display(const string &):
12         priv(new Private)
13 {
14         static ErrorDialog err_dlg(nullptr);
15
16         int primary_index = -1;
17         vector<unsigned> mode_monitor_indices;
18         vector<int> desktop_mode_indices;
19         for(unsigned i=0;; ++i)
20         {
21                 DISPLAY_DEVICE adapter_dev;
22                 adapter_dev.cb = sizeof(adapter_dev);
23                 if(!EnumDisplayDevices(nullptr, i, &adapter_dev, 0))
24                         break;
25
26                 if(adapter_dev.StateFlags&DISPLAY_DEVICE_MIRRORING_DRIVER)
27                         continue;
28
29                 monitors.push_back(Monitor());
30                 Monitor &monitor = monitors.back();
31                 monitor.index = monitors.size()-1;
32                 monitor.name = adapter_dev.DeviceString;
33                 priv->monitors.push_back(adapter_dev.DeviceName);
34
35                 if(adapter_dev.StateFlags&DISPLAY_DEVICE_PRIMARY_DEVICE)
36                         primary_index = i;
37
38                 DEVMODE current;
39                 bool have_current = EnumDisplaySettings(adapter_dev.DeviceName, ENUM_CURRENT_SETTINGS, &current);
40
41                 unsigned first_mode = modes.size();
42                 int desktop_mode_index = -1;
43                 for(unsigned j=0;; ++j)
44                 {
45                         DEVMODE info;
46                         if(!EnumDisplaySettings(adapter_dev.DeviceName, j, &info))
47                                 break;
48
49                         if(any_of(modes.begin()+first_mode, modes.end(), [&info](const VideoMode &m){
50                                         return (m.width==info.dmPelsWidth && m.height==info.dmPelsHeight && m.rate==info.dmDisplayFrequency);
51                                 }))
52                                 continue;
53
54                         VideoMode mode(info.dmPelsWidth, info.dmPelsHeight);
55                         mode.index = modes.size();
56                         mode.rate = info.dmDisplayFrequency;
57
58                         modes.push_back(mode);
59                         mode_monitor_indices.push_back(monitor.index);
60
61                         if(have_current && info.dmPelsWidth==current.dmPelsWidth && info.dmPelsHeight==current.dmPelsHeight && info.dmDisplayFrequency==current.dmDisplayFrequency)
62                                 desktop_mode_index = mode.index;
63                 }
64                 desktop_mode_indices.push_back(desktop_mode_index);
65         }
66
67         for(unsigned i=0; i<monitors.size(); ++i)
68         {
69                 int j = desktop_mode_indices[i];
70                 if(j>=0)
71                         monitors[i].desktop_settings.mode = &modes[j];
72                 monitors[i].current_settings = monitors[i].desktop_settings;
73         }
74
75         for(unsigned i=0; i<modes.size(); ++i)
76         {
77                 Monitor &monitor = monitors[mode_monitor_indices[i]];
78                 modes[i].monitor = &monitor;
79                 monitor.video_modes.push_back(&modes[i]);
80         }
81
82         if(primary_index>=0)
83                 primary_monitor = &monitors[primary_index];
84 }
85
86 Display::~Display()
87 {
88         delete priv;
89 }
90
91 void Display::set_mode(const VideoMode &requested_mode, bool)
92 {
93         const VideoMode *mode = find_mode(requested_mode);
94         if(!mode)
95                 throw unsupported_video_mode(requested_mode);
96
97         DEVMODE info;
98         info.dmDeviceName[0] = 0;
99         info.dmSpecVersion = DM_SPECVERSION;
100         info.dmDriverVersion = 0;
101         info.dmSize = sizeof(DEVMODE);
102         info.dmDriverExtra = 0;
103         info.dmFields = DM_PELSWIDTH|DM_PELSHEIGHT;
104         info.dmPelsWidth = mode->width;
105         info.dmPelsHeight = mode->height;
106         if(requested_mode.rate)
107         {
108                 info.dmFields |= DM_DISPLAYFREQUENCY;
109                 info.dmDisplayFrequency = mode->rate;
110         }
111
112         LONG ret = ChangeDisplaySettingsEx(priv->monitors[mode->monitor->index].c_str(), &info, NULL, CDS_FULLSCREEN, NULL);
113         if(ret!=DISP_CHANGE_SUCCESSFUL)
114                 throw unsupported_video_mode(requested_mode);
115
116         for(Monitor &m: monitors)
117                 if(&m==mode->monitor)
118                         m.current_settings.mode = mode;
119 }
120
121 bool Display::process_events()
122 {
123         MSG msg;
124         if(PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
125         {
126                 TranslateMessage(&msg);
127                 DispatchMessage(&msg);
128                 return true;
129         }
130         else
131                 return false;
132 }
133
134 void Display::check_error()
135 {
136 }
137
138 } // namespace Msp
139 } // namespace Graphics