OSDN Git Service

first
[psychlops/cpp.git] / psychlops / platform / win32 / psychlops_io_display_Win32.cpp
1 /*
2  *  psychlops_io_display_Win32.cpp
3  *  Psychlops Standard Library (Win32)
4  *
5  *  Last Modified 2009/03/02 by Kenchi HOSOKAWA
6  *  (C) 2009 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9
10 #include <string>
11 #include <windows.h>
12 #include "../../core/graphic/psychlops_g_canvas.h"
13 #include "psychlops_io_display_Win32.h"
14
15
16
17 namespace Psychlops {
18
19         Displays APIDisplayProperties::displays;
20
21         BOOL CALLBACK APIDisplayProperties::DisplayEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
22                 APIDisplayProperties *api = new APIDisplayProperties;
23                 api->monitor_ = hMonitor;
24                 //api.display_ = CreateDC("DISPLAY".ptr, hMonitor, null, null);
25                 Display monitor(api);
26                 displays.push_back(monitor);
27                 return TRUE;
28         }
29
30
31         APIDisplayProperties::APIDisplayProperties() :
32                 display_(0)
33 //              calibration_mode_(Color.CalibrationMode.NONE);
34         {}
35
36         APIDisplayProperties::~APIDisplayProperties() {
37                 if(display_ != 0) DeleteDC(display_);
38         }
39
40         Displays APIDisplayProperties::emumDisplays() {
41                 displays.clear();
42                 EnumDisplayMonitors(NULL, NULL, &DisplayEnumProc, 0);
43                 return displays;
44         }
45
46
47         void APIDisplayProperties::getInfo(int &width, int &height, int &color_depth, double &refresh_rate, std::string& name, Rectangle& area) {
48                 char info_[sizeof(MONITORINFOEXA)];
49                 LPMONITORINFOEXA info = (LPMONITORINFOEXA)&info_;
50                 info->cbSize = (DWORD)sizeof(MONITORINFOEXA);
51                 GetMonitorInfoA(monitor_, info);
52                 width = info->rcMonitor.right - info->rcMonitor.left;
53                 height = info->rcMonitor.bottom - info->rcMonitor.top;
54                 area.set(info->rcWork.left, info->rcWork.top, info->rcWork.right, info->rcWork.bottom);
55
56                 primary_ = info->dwFlags;
57                 name = info->szDevice;
58                 display_ = CreateDC("DISPLAY", (char*)info->szDevice, (LPCTSTR)0, (const DEVMODE*)0);
59                 color_depth  = GetDeviceCaps(display_, BITSPIXEL);
60                 refresh_rate = GetDeviceCaps(display_, VREFRESH);
61         }
62 /*
63         // Hardware Gamma Settings : requires CGDirectDisplayID(target_display_)
64         void APIDisplayProperties::setGammaValue(const double gamma_r, const double gamma_g, const double gamma_b) {
65                 OSErr err;
66                 saveGammaValue();
67                 err = CGSetDisplayTransferByFormula( did,
68                                                                                         0.0, 1.0, (1.0/gamma_r),
69                                                                                         0.0, 1.0, (1.0/gamma_g),
70                                                                                         0.0, 1.0, (1.0/gamma_b));
71                 //                              0.0, 1.0, (1.0/gamma_r)*savedRedGamma_,
72                 //                              0.0, 1.0, (1.0/gamma_g)*savedGreenGamma_,
73                 //                              0.0, 1.0, (1.0/gamma_b)*savedBlueGamma_);
74                 if(err!=kCGErrorSuccess) throw Exception(typeid(*this), "API ERROR", "Failed to set color calibration table for the video renderer.");
75                 gamma_mode_ = Color::GAMMA_VALUE;
76         }
77         void APIDisplayProperties::setGammaTable(const std::vector<double> &table_r, const std::vector<double> &table_g, const std::vector<double> &table_b) {
78                 OSErr err;
79                 if(table_r.size()!=256 || table_g.size()!=256 || table_b.size()!=256)
80                         throw Exception(typeid(*this), "Gamma Table Error", "Table size is out of order (not 256).");
81                 int num_steps = table_r.size();
82                 saveGammaValue();
83                 CGGammaValue *(table[3]);
84                 for(int i=0; i<3; i++) table[i] = new CGGammaValue[num_steps];
85                 for(int j=0; j<num_steps; j++) {
86                         table[0][j] = (CGGammaValue)table_r[j];
87                         table[1][j] = (CGGammaValue)table_g[j];
88                         table[2][j] = (CGGammaValue)table_b[j];
89                 }
90                 err = CGSetDisplayTransferByTable(did, num_steps, table[0], table[1], table[2]);
91                 if(err!=kCGErrorSuccess) throw Exception(typeid(*this), "API ERROR", "Failed to set color calibration table for the video renderer.");
92                 gamma_mode_ = Color::TABLE;
93                 for(int i=0; i<3; i++) delete [] table[i];
94         }
95         void APIDisplayProperties::setGammaTable(const CGGammaValue * const table_r, const CGGammaValue * const table_g, const CGGammaValue * const table_b, const int num_steps) {
96                 OSErr err;
97                 saveGammaValue();
98                 err = CGSetDisplayTransferByTable(did, num_steps, table_r, table_g, table_b);
99                 gamma_mode_ = Color::TABLE;
100         }
101         void APIDisplayProperties::saveGammaValue() {
102                 OSErr err;
103                 if(gamma_mode_==Color::NOTHING) {
104                         err = CGGetDisplayTransferByFormula( did,
105                                                                                                 &savedRedMin_, &savedRedMax_, &savedRedGamma_,
106                                                                                                 &savedGreenMin_, &savedGreenMax_, &savedGreenGamma_,
107                                                                                                 &savedBlueMin_, &savedBlueMax_, &savedBlueGamma_);
108                 }
109         }
110         void APIDisplayProperties::destroyGammaSettings() {
111                 OSErr err;
112                 switch(gamma_mode_) {
113                         case Color::GAMMA_VALUE:
114                         case Color::TABLE:
115                         default:
116                                 err = CGSetDisplayTransferByFormula( did,
117                                                                                                         savedRedMin_, savedRedMax_, savedRedGamma_,
118                                                                                                         savedGreenMin_, savedGreenMax_, savedGreenGamma_,
119                                                                                                         savedBlueMin_, savedBlueMax_, savedBlueGamma_);
120                                 break;
121                 }
122                 gamma_mode_ = Color::NOTHING;
123         }
124
125 */
126         const std::vector<Display> Display::list() {
127                 return APIDisplayProperties::emumDisplays();
128         }
129 #ifndef MONITORINFOF_PRIMARY
130 #define MONITORINFOF_PRIMARY 1
131 #endif
132 /*
133         const Display Display::main() {
134                 int width, height, color_depth;
135                 double refresh_rate;
136                 std::string name;
137                 Rectangle area;
138
139                 Displays d = APIDisplayProperties::emumDisplays();
140                 int i=0;
141                 for(i=0; i<d.size(); i++) {
142                         d[i].api_->getInfo(width, height, color_depth, refresh_rate, name, area);
143                         if(d[i].api_->primary_ == MONITORINFOF_PRIMARY) break;
144                 }
145                 if(i>=d.size()) i=0;
146                 return d[i];
147         }
148 */
149
150         Display::Display(APIDisplayProperties *apid) : api_(apid) {
151                 apid->getInfo(width, height, color_depth, refresh_rate, name, area);
152         }\r
153         void Display::refreshInfo()\r
154         {\r
155                 api_->getInfo(width, height, color_depth, refresh_rate, name, area);\r
156         }\r
157
158
159
160
161
162
163 }       /*      <- namespace Psycholops         */
164
165
166