Skip to content

Commit 6d745b4

Browse files
committed
Cleanup for Windows monitor code.
1 parent fdc61ae commit 6d745b4

File tree

1 file changed

+15
-101
lines changed

1 file changed

+15
-101
lines changed

src/windows/system_properties.c

Lines changed: 15 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -32,133 +32,47 @@ HINSTANCE hInst = NULL;
3232
//TODO: structure should have the count as well to avoid the global. s_info[ s_array, s_count ]
3333
screen_data *screens = NULL;
3434

35-
typedef BOOL (WINAPI *SetProcessDPIAware_t)(void);
36-
37-
//http://msdn.microsoft.com/en-us/library/windows/desktop/dn280512%28v=vs.85%29.aspx
38-
typedef enum _Process_DPI_Awareness {
39-
Process_DPI_Unaware = 0,
40-
Process_System_DPI_Aware,
41-
Process_Per_Monitor_DPI_Aware
42-
} Process_DPI_Awareness;
43-
44-
typedef Process_DPI_Awareness PROCESS_DPI_AWARENESS;
45-
46-
// Very new SetProcessDpiAwareness() function prototype.
47-
// http://msdn.microsoft.com/en-us/library/windows/desktop/dn302216%28v=vs.85%29.aspx
48-
typedef HRESULT (WINAPI *SetProcessDpiAwareness_t)(PROCESS_DPI_AWARENESS);
49-
50-
static BOOL windows8xDPIAwareness() {
51-
BOOL res = FALSE;
52-
HMODULE lib_shcore = LoadLibrary("Shcore.dll");
53-
if (lib_shcore) {
54-
SetProcessDpiAwareness_t pSetProcessDpiAwareness =
55-
(SetProcessDpiAwareness_t) GetProcAddress(lib_shcore, "SetProcessDpiAwareness" );
56-
57-
if (pSetProcessDpiAwareness) {
58-
// http://msdn.microsoft.com/en-us/library/windows/desktop/dn469266(v=vs.85).aspx
59-
// Process_Per_Monitor_DPI_Aware only 8.1 I think
60-
HRESULT hres = pSetProcessDpiAwareness(Process_Per_Monitor_DPI_Aware);
61-
if( hres != S_OK )
62-
hres = pSetProcessDpiAwareness( Process_System_DPI_Aware );
63-
64-
( hres == S_OK ) ? (res = TRUE) : (res = FALSE);
65-
66-
logger(LOG_LEVEL_INFO, "%s [%u]: windows8xDPIAwareness: "
67-
"SetProcessDpiAwareness: %d.\n", __FUNCTION__, __LINE__, res );
68-
}
69-
70-
FreeLibrary(lib_shcore);
71-
}
72-
73-
return res;
74-
}
75-
76-
static BOOL windows7VistaDPIAwareness() {
77-
BOOL status = FALSE;
78-
79-
HMODULE currLib = LoadLibrary("user32.dll");
80-
if (currLib) {
81-
SetProcessDPIAware_t pSetProcessDPIAware =
82-
(SetProcessDPIAware_t) GetProcAddress(currLib, "SetProcessDPIAware");
83-
84-
if (pSetProcessDPIAware) {
85-
status = pSetProcessDPIAware();
86-
logger(LOG_LEVEL_INFO, "%s [%u]: windows7VistaDPIAwareness: "
87-
"SetProcessDPIAware: %d.\n", __FUNCTION__, __LINE__, status);
88-
}
89-
FreeLibrary( currLib );
90-
}
91-
92-
return status;
93-
}
94-
95-
void enableDPIAwareness(){
96-
// TODO: Windows XP support?
97-
if (!windows8xDPIAwareness()) {
98-
windows7VistaDPIAwareness();
99-
}
100-
}
10135

10236
//http://msdn.microsoft.com/en-us/library/windows/desktop/dd162610(v=vs.85).aspx
10337
//http://msdn.microsoft.com/en-us/library/dd162610%28VS.85%29.aspx
10438
//http://msdn.microsoft.com/en-us/library/dd145061%28VS.85%29.aspx
10539
//http://msdn.microsoft.com/en-us/library/dd144901(v=vs.85).aspx
10640
// callback function called by EnumDisplayMonitors for each enabled monitor
10741
static BOOL CALLBACK monitor_enum_proc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
108-
// Screen counter, will be passed to the next calls
109-
uint8_t *screen_count = (uint8_t*) dwData;
110-
int width = 0, height = 0, origin_x, origin_y;
111-
112-
if (hdcMonitor != NULL) {
113-
MONITORINFO info;
114-
if (GetMonitorInfo(hMonitor, &info)) {
115-
width = info.rcMonitor.right - info.rcMonitor.left;
116-
height = info.rcMonitor.bottom - info.rcMonitor.top;
117-
origin_x = info.rcMonitor.left;
118-
origin_y = info.rcMonitor.top;
119-
}
120-
else {
121-
// FIXME Produce an error.
122-
}
123-
}
124-
else {
125-
//if the RECT structure becomes unreliable, use GetMonitorInfo
126-
width = lprcMonitor->right - lprcMonitor->left;
127-
height = lprcMonitor->bottom - lprcMonitor->top;
128-
origin_x = lprcMonitor->left;
129-
origin_y = lprcMonitor->top;
130-
}
42+
int width = lprcMonitor->right - lprcMonitor->left;
43+
int height = lprcMonitor->bottom - lprcMonitor->top;
44+
int origin_x = lprcMonitor->left;
45+
int origin_y = lprcMonitor->top;
13146

13247
if (width > 0 && height > 0) {
48+
uint8_t *screen_count = (uint8_t *) dwData;
49+
13350
// FIXME Figure out memory management strategy.
13451
if (screens == NULL) {
13552
screens = malloc(sizeof(screen_data));
13653
}
137-
else{
138-
screens = realloc(screens, sizeof(screen_data) * (*screen_count));
54+
else {
55+
screens = realloc(screens, sizeof(screen_data) * (*(uint8_t *) dwData));
13956
}
14057

141-
screens[*screen_count++] = (screen_data) {
58+
screens[(*screen_count)++] = (screen_data) {
59+
// Should monitor count start @ zero? Currently it starts at 1.
14260
.number = *screen_count,
14361
.x = origin_x,
14462
.y = origin_y,
14563
.width = width,
14664
.height = height
14765
};
148-
149-
logger(LOG_LEVEL_INFO, "%s [%u]: Monitor %d: %ldx%ld (%ld, %ld)\n",
150-
__FUNCTION__, __LINE__, *screen_count, width, height, origin_x, origin_y);
151-
}
15266

67+
logger(LOG_LEVEL_INFO, "%s [%u]: Monitor %d: %ldx%ld (%ld, %ld)\n",
68+
__FUNCTION__, __LINE__, *screen_count, width, height, origin_x, origin_y);
69+
}
70+
15371
return TRUE;
15472
}
15573

15674

15775
UIOHOOK_API screen_data* hook_get_screen_info(uint8_t *count) {
158-
// TODO This sounds a lot like it should be called on library load...
159-
// or possibly offloaded to the library implementer!
160-
//enableDPIAwareness();
161-
16276
// Initialize count to zero.
16377
*count = 0;
16478

@@ -170,7 +84,7 @@ UIOHOOK_API screen_data* hook_get_screen_info(uint8_t *count) {
17084
if (!status) {
17185
// Fallback in case EnumDisplayMonitors fails.
17286
logger(LOG_LEVEL_INFO, "%s [%u]: EnumDisplayMonitors failed. Fallback.\n",
173-
__FUNCTION__, __LINE__);
87+
__FUNCTION__, __LINE__);
17488

17589
int width = GetSystemMetrics(SM_CXSCREEN);
17690
int height = GetSystemMetrics(SM_CYSCREEN);

0 commit comments

Comments
 (0)