Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dpi fix #78

Merged
merged 5 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <windows.h>
#endif

#define MONITOR_NAME_MAX 100
#define MONITOR_NAME_MAX 128
#define WALLPAPER_NAME_MAX 100

#define DEFAULT_LINUX_PREFIX "/usr/local"
Expand Down Expand Up @@ -52,14 +52,16 @@ typedef struct

typedef struct
{
char displayName[MONITOR_NAME_MAX];
char name[MONITOR_NAME_MAX];
Bounds bounds;
Bounds pixelBounds;
Bounds clientBounds;
Bounds virtualBounds;
MonitorConfig config;
} MonitorInfo;

typedef struct
{
int drawOnRootWindow;
int targetFps;
char renderQuality[8];
} AppConfig;
Expand Down
11 changes: 0 additions & 11 deletions src/common/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ int loadMonitorConfig(const char *name, MonitorConfig *mc)

static void useDefaultAppCfg(AppConfig *ac)
{
#ifdef __LINUX
ac->drawOnRootWindow = 0;
#endif
ac->targetFps = 60;
strcpy(ac->renderQuality, "best");
}
Expand All @@ -104,10 +101,6 @@ void saveAppConfig(AppConfig *ac)
config_init(&cfg);
root = config_root_setting(&cfg);

#ifdef __LINUX
setting = config_setting_add(root, "draw_on_rootwindow", CONFIG_TYPE_INT);
config_setting_set_int(setting, ac->drawOnRootWindow);
#endif
setting = config_setting_add(root, "target_fps", CONFIG_TYPE_INT);
config_setting_set_int(setting, ac->targetFps);
setting = config_setting_add(root, "render_quality", CONFIG_TYPE_STRING);
Expand Down Expand Up @@ -141,10 +134,6 @@ int loadAppConfig(AppConfig *ac)
}
root = config_root_setting(&cfg);

#ifdef __LINUX
setting = config_setting_get_member(root, "draw_on_rootwindow");
ac->drawOnRootWindow = config_setting_get_int(setting);
#endif
setting = config_setting_get_member(root, "target_fps");
ac->targetFps = config_setting_get_int(setting);
setting = config_setting_get_member(root, "render_quality");
Expand Down
137 changes: 116 additions & 21 deletions src/common/monitorScanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,118 @@
#include "../common.h"

#ifdef __WIN32
#include <windows.h>
#include <Windows.h>
#include <shellscalingapi.h>
#else
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
#include <string.h>
#endif

#ifdef __WIN32

int monitorEnumIndex = 0;

static BOOL monitorenumproc(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM param)
int primaryX = 0;
int primaryY = 0;

static float getScalingFactor(HMONITOR monitor)
{
DEVICE_SCALE_FACTOR rawScaleFactor;
HRESULT hr = GetScaleFactorForMonitor(monitor, &rawScaleFactor);

return rawScaleFactor / 100;
}

static void getMonitorName(int index, char *name, char *displayName)
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(DISPLAY_DEVICE);
EnumDisplayDevices(NULL, index, &dd, EDD_GET_DEVICE_INTERFACE_NAME);
DISPLAY_DEVICE dd2;
dd2.cb = sizeof(DISPLAY_DEVICE);
EnumDisplayDevices(dd.DeviceName, 0, &dd2, EDD_GET_DEVICE_INTERFACE_NAME);

char *ptr = dd2.DeviceID;
while (*ptr != '#') ptr++;
ptr++;

char *ptrEnd = ptr;
int hashes = 0;
while (hashes < 2)
{
ptrEnd++;
if (*ptrEnd == '#') hashes++;
}
*ptrEnd = '\0';

strncpy(displayName, dd2.DeviceString, MONITOR_NAME_MAX);
strncpy(name, ptr, MONITOR_NAME_MAX);
}

static void getMonitorBounds(
HMONITOR monitor, Bounds *original, Bounds *client, Bounds *virtual
)
{
MONITORINFOEX info;
info.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(monitor, (LPMONITORINFO)&info);

if (original)
{
original->x = info.rcMonitor.left;
original->y = info.rcMonitor.top;
original->w = info.rcMonitor.right - info.rcMonitor.left;
original->h = info.rcMonitor.bottom - info.rcMonitor.top;
}

DEVMODE devmode;
devmode.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(info.szDevice, ENUM_CURRENT_SETTINGS, &devmode);

if (client)
{
client->x = devmode.dmPosition.x;
client->y = devmode.dmPosition.y;
client->w = devmode.dmPelsWidth;
client->h = devmode.dmPelsHeight;
}

float scaleFactor = getScalingFactor(monitor);

if (virtual)
{
virtual->x = devmode.dmPosition.x * scaleFactor - primaryX;
virtual->y = devmode.dmPosition.y * scaleFactor - primaryY;
virtual->w = devmode.dmPelsWidth * scaleFactor;
virtual->h = devmode.dmPelsHeight * scaleFactor;
}
}

static BOOL monitorenumproc_FindPrimaryCoords(
HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM param
)
{
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
Bounds bounds;
getMonitorBounds(monitor, NULL, &bounds, NULL);

if (bounds.x < primaryX) primaryX = bounds.x;
if (bounds.y < primaryY) primaryY = bounds.y;

GetMonitorInfo(monitor, &info);
return TRUE;
}

static BOOL monitorenumproc_GetInfo(
HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM param
)
{
MonitorInfo *mi = (MonitorInfo *)param + monitorEnumIndex;

snprintf(
mi->name,
MONITOR_NAME_MAX,
"Monitor %d%s",
monitorEnumIndex + 1,
info.dwFlags == MONITORINFOF_PRIMARY ? " (main)" : ""
getMonitorBounds(
monitor, &mi->pixelBounds, &mi->clientBounds, &mi->virtualBounds
);
mi->bounds.x = info.rcWork.left;
mi->bounds.y = info.rcWork.top;
mi->bounds.w = info.rcWork.right - info.rcWork.left;
mi->bounds.h = info.rcWork.bottom - info.rcWork.top;
getMonitorName(monitorEnumIndex, mi->name, mi->displayName);

mi->config.loaded = 0;

monitorEnumIndex++;
Expand All @@ -45,11 +128,15 @@ MonitorInfo *scanMonitors(int *count)
MonitorInfo *m = NULL;

#ifdef __WIN32
EnumDisplayMonitors(
NULL, NULL, &monitorenumproc_FindPrimaryCoords, (LPARAM)NULL
);

monitorEnumIndex = 0;
*count = GetSystemMetrics(SM_CMONITORS);
m = malloc(sizeof(MonitorInfo) * (*count));

EnumDisplayMonitors(NULL, NULL, &monitorenumproc, (LPARAM)m);
EnumDisplayMonitors(NULL, NULL, &monitorenumproc_GetInfo, (LPARAM)m);

#else
int monitorCount;
Expand All @@ -63,11 +150,19 @@ MonitorInfo *scanMonitors(int *count)
int i = 0;
while (i < monitorCount)
{
snprintf(m[i].name, MONITOR_NAME_MAX, "%s", XGetAtomName(display, info->name));
m[i].bounds.x = info->x;
m[i].bounds.y = info->y;
m[i].bounds.w = info->width;
m[i].bounds.h = info->height;
snprintf(
m[i].name, MONITOR_NAME_MAX, "%s", XGetAtomName(display, info->name)
);
strcpy(m[i].displayName, m[i].name);

m[i].virtualBounds.x = info->x;
m[i].virtualBounds.y = info->y;
m[i].virtualBounds.w = info->width;
m[i].virtualBounds.h = info->height;

memcpy(&m[i].clientBounds, &m[i].virtualBounds, sizeof(Bounds));
memcpy(&m[i].pixelBounds, &m[i].virtualBounds, sizeof(Bounds));

m[i].config.loaded = 0;

info++;
Expand Down
5 changes: 5 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ pkg_check_modules (libconfig REQUIRED libconfig)
list(APPEND _INCLUDE_DIRS ${libconfig_INCLUDE_DIRS})
list(APPEND _LIBS ${libconfig_LINK_LIBRARIES})

if (_UNAME STREQUAL "WIN32")
# Shcore dependency
list(APPEND _LIBS "-lShcore")
endif()

# Main executable
if(_UNAME STREQUAL "WIN32")
add_executable(lwp WIN32 ${_SOURCE_FILES})
Expand Down
Loading
Loading