2 #include "display_private.h"
11 Display::Display(const string &):
14 static ErrorDialog err_dlg(nullptr);
16 int primary_index = -1;
17 vector<unsigned> mode_monitor_indices;
18 vector<int> desktop_mode_indices;
19 for(unsigned i=0;; ++i)
21 DISPLAY_DEVICE adapter_dev;
22 adapter_dev.cb = sizeof(adapter_dev);
23 if(!EnumDisplayDevices(nullptr, i, &adapter_dev, 0))
26 if(adapter_dev.StateFlags&DISPLAY_DEVICE_MIRRORING_DRIVER)
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);
35 if(adapter_dev.StateFlags&DISPLAY_DEVICE_PRIMARY_DEVICE)
39 bool have_current = EnumDisplaySettings(adapter_dev.DeviceName, ENUM_CURRENT_SETTINGS, ¤t);
41 unsigned first_mode = modes.size();
42 int desktop_mode_index = -1;
43 for(unsigned j=0;; ++j)
46 if(!EnumDisplaySettings(adapter_dev.DeviceName, j, &info))
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);
54 VideoMode mode(info.dmPelsWidth, info.dmPelsHeight);
55 mode.index = modes.size();
56 mode.rate = info.dmDisplayFrequency;
58 modes.push_back(mode);
59 mode_monitor_indices.push_back(monitor.index);
61 if(have_current && info.dmPelsWidth==current.dmPelsWidth && info.dmPelsHeight==current.dmPelsHeight && info.dmDisplayFrequency==current.dmDisplayFrequency)
62 desktop_mode_index = mode.index;
64 desktop_mode_indices.push_back(desktop_mode_index);
67 for(unsigned i=0; i<monitors.size(); ++i)
69 int j = desktop_mode_indices[i];
71 monitors[i].desktop_settings.mode = &modes[j];
72 monitors[i].current_settings = monitors[i].desktop_settings;
75 for(unsigned i=0; i<modes.size(); ++i)
77 Monitor &monitor = monitors[mode_monitor_indices[i]];
78 modes[i].monitor = &monitor;
79 monitor.video_modes.push_back(&modes[i]);
83 primary_monitor = &monitors[primary_index];
91 void Display::set_mode(const VideoMode &requested_mode, bool)
93 const VideoMode *mode = find_mode(requested_mode);
95 throw unsupported_video_mode(requested_mode);
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)
108 info.dmFields |= DM_DISPLAYFREQUENCY;
109 info.dmDisplayFrequency = mode->rate;
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);
116 for(Monitor &m: monitors)
117 if(&m==mode->monitor)
118 m.current_settings.mode = mode;
121 bool Display::process_events()
124 if(PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
126 TranslateMessage(&msg);
127 DispatchMessage(&msg);
134 void Display::check_error()
139 } // namespace Graphics