]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/windows/display.cpp
Subclass NSWindow to eat keyDown events so they won't beep
[libs/gui.git] / source / graphics / windows / display.cpp
1 #include <windows.h>
2 #include "display.h"
3 #include "display_private.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace Graphics {
9
10 Display::Display(const string &):
11         primary_monitor(0),
12         priv(new Private)
13 {
14         static ErrorDialog err_dlg(0);
15
16         for(unsigned i=0;; ++i)
17         {
18                 DISPLAY_DEVICE adapter_dev;
19                 adapter_dev.cb = sizeof(adapter_dev);
20                 if(!EnumDisplayDevices(0, i, &adapter_dev, 0))
21                         break;
22
23                 if(adapter_dev.StateFlags&DISPLAY_DEVICE_MIRRORING_DRIVER)
24                         continue;
25
26                 monitors.push_back(Monitor());
27                 Monitor &monitor = monitors.back();
28                 monitor.index = monitors.size()-1;
29                 priv->monitors.push_back(adapter_dev.DeviceName);
30
31                 if(adapter_dev.StateFlags&DISPLAY_DEVICE_PRIMARY_DEVICE)
32                         primary_monitor = &monitor;
33
34                 DEVMODE current;
35                 bool have_current = EnumDisplaySettings(adapter_dev.DeviceName, ENUM_CURRENT_SETTINGS, &current);
36
37                 for(unsigned j=0;; ++j)
38                 {
39                         DEVMODE info;
40                         if(!EnumDisplaySettings(adapter_dev.DeviceName, j, &info))
41                                 break;
42
43                         VideoMode mode(info.dmPelsWidth, info.dmPelsHeight);
44                         mode.index = modes.size();
45                         mode.monitor = &monitor;
46                         mode.rate = info.dmDisplayFrequency;
47                         if(find_mode(mode))
48                                 continue;
49
50                         modes.push_back(mode);
51                         monitor.video_modes.push_back(&modes.back());
52
53                         if(have_current && info.dmPelsWidth==current.dmPelsWidth && info.dmPelsHeight==current.dmPelsHeight && info.dmDisplayFrequency==current.dmDisplayFrequency)
54                                 monitor.desktop_mode = &modes.back();
55                 }
56         }
57 }
58
59 Display::~Display()
60 {
61 }
62
63 void Display::set_mode(const VideoMode &requested_mode, bool)
64 {
65         const VideoMode *mode = find_mode(requested_mode);
66         if(!mode)
67                 throw unsupported_video_mode(requested_mode);
68
69         DEVMODE info;
70         info.dmDeviceName[0] = 0;
71         info.dmSpecVersion = DM_SPECVERSION;
72         info.dmDriverVersion = 0;
73         info.dmSize = sizeof(DEVMODE);
74         info.dmDriverExtra = 0;
75         info.dmFields = DM_PELSWIDTH|DM_PELSHEIGHT;
76         info.dmPelsWidth = mode->width;
77         info.dmPelsHeight = mode->height;
78         if(requested_mode.rate)
79         {
80                 info.dmFields |= DM_DISPLAYFREQUENCY;
81                 info.dmDisplayFrequency = mode->rate;
82         }
83
84         LONG ret = ChangeDisplaySettingsEx(priv->monitors[mode->monitor->index].c_str(), &info, NULL, CDS_FULLSCREEN, NULL);
85         if(ret!=DISP_CHANGE_SUCCESSFUL)
86                 throw unsupported_video_mode(requested_mode);
87
88         for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
89                 if(&*i==mode->monitor)
90                         i->current_mode = mode;
91 }
92
93 bool Display::process_events()
94 {
95         MSG msg;
96         if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
97         {
98                 DispatchMessage(&msg);
99                 return true;
100         }
101         else
102                 return false;
103 }
104
105 void Display::check_error()
106 {
107 }
108
109 } // namespace Msp
110 } // namespace Graphics