Workaround for light title bar in Windows

Use undocumented APIs to make Windows display title bar in dark mode
This commit is contained in:
Pratyush Nair 2021-05-17 16:16:52 +05:30
parent 38bd9b3326
commit bd8b525faa
1 changed files with 209 additions and 145 deletions

View File

@ -1,145 +1,209 @@
#include <stdio.h> #include <stdio.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "api/api.h" #include "api/api.h"
#include "renderer.h" #include "renderer.h"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#elif __linux__ #include <dwmapi.h>
#include <unistd.h> #include <SDL2/SDL_syswm.h>
#elif __APPLE__
#include <mach-o/dyld.h> enum WINDOWCOMPOSITIONATTRIB {
#endif WCA_USEDARKMODECOLORS = 26
};
SDL_Window *window; struct WINDOWCOMPOSITIONATTRIBDATA {
enum WINDOWCOMPOSITIONATTRIB Attrib;
PVOID pvData;
static double get_scale(void) { SIZE_T cbData;
float dpi; };
SDL_GetDisplayDPI(0, NULL, &dpi, NULL);
#if _WIN32 enum PreferredAppMode { Default, AllowDark, ForceDark, ForceLight, Max };
return dpi / 96.0;
#else typedef BOOL(WINAPI *fnSetWindowCompositionAttribute)(
return 1.0; HWND hWnd, struct WINDOWCOMPOSITIONATTRIBDATA *);
#endif typedef VOID(WINAPI *fnRefreshImmersiveColorPolicyState)(); // ordinal 104
} typedef BOOL(WINAPI *fnShouldAppsUseDarkMode)(); // ordinal 132
typedef enum PreferredAppMode(WINAPI *fnSetPreferredAppMode)(
enum PreferredAppMode appMode); // ordinal 135
static void get_exe_filename(char *buf, int sz) {
#if _WIN32 fnRefreshImmersiveColorPolicyState _RefreshImmersiveColorPolicyState = NULL;
int len = GetModuleFileName(NULL, buf, sz - 1); fnSetWindowCompositionAttribute _SetWindowCompositionAttribute = NULL;
buf[len] = '\0'; fnShouldAppsUseDarkMode _ShouldAppsUseDarkMode = NULL;
#elif __linux__ fnSetPreferredAppMode _SetPreferredAppMode = NULL;
char path[512];
sprintf(path, "/proc/%d/exe", getpid()); HWND getWinHandle(SDL_Window *win) {
int len = readlink(path, buf, sz - 1); SDL_SysWMinfo infoWindow;
buf[len] = '\0'; SDL_VERSION(&infoWindow.version);
#elif __APPLE__ SDL_GetWindowWMInfo(win, &infoWindow);
unsigned size = sz; return (infoWindow.info.win.window);
_NSGetExecutablePath(buf, &size); }
#else
strcpy(buf, "./lite"); void get_dark_titlebar(SDL_Window *window, int dark) {
#endif HWND hwnd;
} hwnd = getWinHandle(window);
HMODULE hUxtheme =
LoadLibraryExW(L"uxtheme.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
static void init_window_icon(void) { if (hUxtheme) {
#ifndef _WIN32 _SetWindowCompositionAttribute =
#include "../icon.inl" (fnSetWindowCompositionAttribute)(GetProcAddress(
(void) icon_rgba_len; /* unused */ GetModuleHandleW(L"user32.dll"), "SetWindowCompositionAttribute"));
SDL_Surface *surf = SDL_CreateRGBSurfaceFrom( _RefreshImmersiveColorPolicyState = (fnRefreshImmersiveColorPolicyState)(
icon_rgba, 64, 64, GetProcAddress(hUxtheme, MAKEINTRESOURCEA(104)));
32, 64 * 4, _ShouldAppsUseDarkMode = (fnShouldAppsUseDarkMode)(
0x000000ff, GetProcAddress(hUxtheme, MAKEINTRESOURCEA(132)));
0x0000ff00, _SetPreferredAppMode = (fnSetPreferredAppMode)(
0x00ff0000, GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135)));
0xff000000);
SDL_SetWindowIcon(window, surf); if (_RefreshImmersiveColorPolicyState && _ShouldAppsUseDarkMode &&
SDL_FreeSurface(surf); _SetPreferredAppMode) {
#endif _SetPreferredAppMode(AllowDark);
} _RefreshImmersiveColorPolicyState();
LONG ldark = dark;
struct WINDOWCOMPOSITIONATTRIBDATA data = {WCA_USEDARKMODECOLORS, &ldark,
int main(int argc, char **argv) { sizeof ldark};
#ifdef _WIN32 _SetWindowCompositionAttribute(hwnd, &data);
HINSTANCE lib = LoadLibrary("user32.dll"); }
int (*SetProcessDPIAware)() = (void*) GetProcAddress(lib, "SetProcessDPIAware"); }
SetProcessDPIAware(); }
#endif #elif __linux__
#include <unistd.h>
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); #elif __APPLE__
SDL_EnableScreenSaver(); #include <mach-o/dyld.h>
SDL_EventState(SDL_DROPFILE, SDL_ENABLE); #endif
atexit(SDL_Quit);
#ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* Available since 2.0.8 */ SDL_Window *window;
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
#endif
#if SDL_VERSION_ATLEAST(2, 0, 5) static double get_scale(void) {
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); float dpi;
#endif SDL_GetDisplayDPI(0, NULL, &dpi, NULL);
#if _WIN32
SDL_DisplayMode dm; return dpi / 96.0;
SDL_GetCurrentDisplayMode(0, &dm); #else
return 1.0;
window = SDL_CreateWindow( #endif
"", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, dm.w * 0.8, dm.h * 0.8, }
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);
init_window_icon();
ren_init(window); static void get_exe_filename(char *buf, int sz) {
#if _WIN32
int len = GetModuleFileName(NULL, buf, sz - 1);
lua_State *L = luaL_newstate(); buf[len] = '\0';
luaL_openlibs(L); #elif __linux__
api_load_libs(L); char path[512];
sprintf(path, "/proc/%d/exe", getpid());
int len = readlink(path, buf, sz - 1);
lua_newtable(L); buf[len] = '\0';
for (int i = 0; i < argc; i++) { #elif __APPLE__
lua_pushstring(L, argv[i]); unsigned size = sz;
lua_rawseti(L, -2, i + 1); _NSGetExecutablePath(buf, &size);
} #else
lua_setglobal(L, "ARGS"); strcpy(buf, "./lite");
#endif
lua_pushstring(L, "1.11"); }
lua_setglobal(L, "VERSION");
lua_pushstring(L, SDL_GetPlatform()); static void init_window_icon(void) {
lua_setglobal(L, "PLATFORM"); #ifndef _WIN32
#include "../icon.inl"
lua_pushnumber(L, get_scale()); (void) icon_rgba_len; /* unused */
lua_setglobal(L, "SCALE"); SDL_Surface *surf = SDL_CreateRGBSurfaceFrom(
icon_rgba, 64, 64,
char exename[2048]; 32, 64 * 4,
get_exe_filename(exename, sizeof(exename)); 0x000000ff,
lua_pushstring(L, exename); 0x0000ff00,
lua_setglobal(L, "EXEFILE"); 0x00ff0000,
0xff000000);
SDL_SetWindowIcon(window, surf);
(void) luaL_dostring(L, SDL_FreeSurface(surf);
"local core\n" #endif
"xpcall(function()\n" }
" SCALE = tonumber(os.getenv(\"LITE_SCALE\")) or SCALE\n"
" PATHSEP = package.config:sub(1, 1)\n"
" EXEDIR = EXEFILE:match(\"^(.+)[/\\\\].*$\")\n" int main(int argc, char **argv) {
" package.path = EXEDIR .. '/data/?.lua;' .. package.path\n" #ifdef _WIN32
" package.path = EXEDIR .. '/data/?/init.lua;' .. package.path\n" HINSTANCE lib = LoadLibrary("user32.dll");
" core = require('core')\n" int (*SetProcessDPIAware)() = (void*) GetProcAddress(lib, "SetProcessDPIAware");
" core.init()\n" SetProcessDPIAware();
" core.run()\n" #endif
"end, function(err)\n"
" print('Error: ' .. tostring(err))\n" SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
" print(debug.traceback(nil, 2))\n" SDL_EnableScreenSaver();
" if core and core.on_error then\n" SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
" pcall(core.on_error, err)\n" atexit(SDL_Quit);
" end\n"
" os.exit(1)\n" #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* Available since 2.0.8 */
"end)"); SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
#endif
#if SDL_VERSION_ATLEAST(2, 0, 5)
lua_close(L); SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
SDL_DestroyWindow(window); #endif
return EXIT_SUCCESS; SDL_DisplayMode dm;
} SDL_GetCurrentDisplayMode(0, &dm);
window = SDL_CreateWindow(
"", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, dm.w * 0.8, dm.h * 0.8,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);
#if _WIN32
get_dark_titlebar(window, 1);
#endif
init_window_icon();
ren_init(window);
lua_State *L = luaL_newstate();
luaL_openlibs(L);
api_load_libs(L);
lua_newtable(L);
for (int i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i + 1);
}
lua_setglobal(L, "ARGS");
lua_pushstring(L, "1.11");
lua_setglobal(L, "VERSION");
lua_pushstring(L, SDL_GetPlatform());
lua_setglobal(L, "PLATFORM");
lua_pushnumber(L, get_scale());
lua_setglobal(L, "SCALE");
char exename[2048];
get_exe_filename(exename, sizeof(exename));
lua_pushstring(L, exename);
lua_setglobal(L, "EXEFILE");
(void) luaL_dostring(L,
"local core\n"
"xpcall(function()\n"
" SCALE = tonumber(os.getenv(\"LITE_SCALE\")) or SCALE\n"
" PATHSEP = package.config:sub(1, 1)\n"
" EXEDIR = EXEFILE:match(\"^(.+)[/\\\\].*$\")\n"
" package.path = EXEDIR .. '/data/?.lua;' .. package.path\n"
" package.path = EXEDIR .. '/data/?/init.lua;' .. package.path\n"
" core = require('core')\n"
" core.init()\n"
" core.run()\n"
"end, function(err)\n"
" print('Error: ' .. tostring(err))\n"
" print(debug.traceback(nil, 2))\n"
" if core and core.on_error then\n"
" pcall(core.on_error, err)\n"
" end\n"
" os.exit(1)\n"
"end)");
lua_close(L);
SDL_DestroyWindow(window);
return EXIT_SUCCESS;
}