Repository navigation
Clock Overlay Shows in the top-right corner of the screen when you press the Guide button on a controller) #1188
hamidzizo30
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
// ==WindhawkMod==
// @id xbox-guide-clock-overlay
// @name Xbox Guide Button Clock Overlay
// @description Shows a clock overlay in the top-right corner of the screen when you press the Guide (Xbox) button on a controller
// @Version 1.1
// @author LeoGFX
// @include explorer.exe
// @compilerOptions -lgdi32 -luser32 -ldwmapi
// @license MIT
// ==/WindhawkMod==
// ==WindhawkModReadme==
/*
Xbox Guide Button Clock Overlay
Press the Guide (Xbox logo) button on any connected XInput controller and a
small clock overlay appears in the top-right corner of your primary
monitor for a few seconds.
Features
Important notes
every time you press it: Settings → Gaming → Game Bar → turn off
"Open Game Bar using this button on a controller".
and borderless/windowed games, but not over games running in
exclusive fullscreen.
its fast "independent flip" mode to slower desktop composition while the
overlay is visible. This can cause a brief hitch or a small FPS drop. The
mod keeps this as small as possible (opaque window, redraw only when the
time changes), but a window drawn on top of a game can't avoid it
completely.
can change that in Steam → Settings → Controller → Guide Button Chord
Layout.
XInputGetStateEx(ordinal 100 of
xinput1_4.dll), which is the standard way to read it.*/
// ==/WindhawkModReadme==
// ==WindhawkModSettings==
/*
$name: Display duration (seconds)
$description: How long the overlay stays visible after pressing Guide.
$name: Press Guide again to hide
$description: If enabled, pressing Guide while the overlay is shown hides it.
$name: 24-hour clock
$name: Show seconds
$name: Show date
$name: Time font size (pt)
$name: Distance from screen edge (px)
$name: Opacity (%)
$description: >-
10 - 100. Keep it at 100 for the least impact on games: below 100 the
overlay becomes a semi-transparent (layered) window, which costs more
to draw on top of a game.
*/
// ==/WindhawkModSettings==
#include <windows.h>
#include <dwmapi.h>
// ---------------------------------------------------------------------------
// XInput (loaded dynamically, Guide button needs XInputGetStateEx)
// ---------------------------------------------------------------------------
struct XINPUT_GAMEPAD_EX {
WORD wButtons;
BYTE bLeftTrigger;
BYTE bRightTrigger;
SHORT sThumbLX;
SHORT sThumbLY;
SHORT sThumbRX;
SHORT sThumbRY;
};
struct XINPUT_STATE_EX {
DWORD dwPacketNumber;
XINPUT_GAMEPAD_EX Gamepad;
DWORD dwPadding[4]; // extra room, the Ex struct is larger than XINPUT_STATE
};
using XInputGetStateEx_t = DWORD(WINAPI*)(DWORD, XINPUT_STATE_EX*);
constexpr WORD XINPUT_GAMEPAD_GUIDE = 0x0400;
constexpr int kMaxControllers = 4;
// ---------------------------------------------------------------------------
// Settings
// ---------------------------------------------------------------------------
struct {
int durationSec;
bool toggleMode;
bool use24Hour;
bool showSeconds;
bool showDate;
int fontSize;
int margin;
int opacity;
} settings;
void LoadSettings() {
settings.durationSec = Wh_GetIntSetting(L"duration");
settings.toggleMode = Wh_GetIntSetting(L"toggleMode");
settings.use24Hour = Wh_GetIntSetting(L"use24Hour");
settings.showSeconds = Wh_GetIntSetting(L"showSeconds");
settings.showDate = Wh_GetIntSetting(L"showDate");
settings.fontSize = Wh_GetIntSetting(L"fontSize");
settings.margin = Wh_GetIntSetting(L"margin");
settings.opacity = Wh_GetIntSetting(L"opacity");
}
// ---------------------------------------------------------------------------
// Globals
// ---------------------------------------------------------------------------
constexpr wchar_t kClassName[] = L"WhXboxGuideClockOverlay";
constexpr wchar_t kMutexName[] = L"Local\WhXboxGuideClockOverlay";
constexpr UINT_PTR TIMER_POLL = 1;
constexpr UINT_PTR TIMER_HIDE = 2;
constexpr UINT_PTR TIMER_TICK = 3;
constexpr UINT WM_APP_RELOAD = WM_APP + 1;
HANDLE g_thread = nullptr;
HANDLE g_stopEvent = nullptr;
HANDLE g_mutex = nullptr;
HWND volatile g_hwnd = nullptr;
HMODULE g_xinputModule = nullptr;
XInputGetStateEx_t g_XInputGetStateEx = nullptr;
HFONT g_timeFont = nullptr;
HFONT g_dateFont = nullptr;
int g_pad = 0;
int g_gap = 0;
int g_timeH = 0;
int g_width = 0;
int g_height = 0;
bool g_visible = false;
wchar_t g_lastTime[64] = L"";
wchar_t g_lastDate[128] = L"";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
bool LoadXInput() {
const wchar_t* dlls[] = {L"xinput1_4.dll", L"xinput1_3.dll"};
for (auto dll : dlls) {
HMODULE mod = LoadLibraryW(dll);
if (!mod) continue;
auto fn = (XInputGetStateEx_t)GetProcAddress(mod, (LPCSTR)100);
if (fn) {
g_xinputModule = mod;
g_XInputGetStateEx = fn;
Wh_Log(L"Using XInputGetStateEx from %s", dll);
return true;
}
FreeLibrary(mod);
}
Wh_Log(L"XInputGetStateEx not found");
return false;
}
void FormatTexts(wchar_t* timeBuf, int timeLen, wchar_t* dateBuf, int dateLen) {
SYSTEMTIME st;
GetLocalTime(&st);
}
void FreeFonts() {
if (g_timeFont) DeleteObject(g_timeFont);
if (g_dateFont) DeleteObject(g_dateFont);
g_timeFont = nullptr;
g_dateFont = nullptr;
}
void CreateFonts(int dpi) {
FreeFonts();
int timeSize = -MulDiv(settings.fontSize, dpi, 72);
int dateSize = -MulDiv(settings.fontSize * 45 / 100 + 1, dpi, 72);
}
// Measures the text, sizes and positions the window in the top-right corner.
void UpdateLayout(HWND hwnd) {
HDC screen = GetDC(nullptr);
int dpi = GetDeviceCaps(screen, LOGPIXELSY);
CreateFonts(dpi);
}
void ShowOverlay(HWND hwnd) {
UpdateLayout(hwnd);
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
g_lastTime[0] = L'\0';
g_lastDate[0] = L'\0';
InvalidateRect(hwnd, nullptr, FALSE);
g_visible = true;
}
void HideOverlay(HWND hwnd) {
KillTimer(hwnd, TIMER_HIDE);
KillTimer(hwnd, TIMER_TICK);
ShowWindow(hwnd, SW_HIDE);
g_visible = false;
}
void Paint(HWND hwnd) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
}
void PollControllers(HWND hwnd) {
static bool prevGuide[kMaxControllers] = {};
static bool connected[kMaxControllers] = {};
static ULONGLONG nextCheck[kMaxControllers] = {};
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_TIMER:
if (wParam == TIMER_POLL) {
PollControllers(hwnd);
} else if (wParam == TIMER_HIDE) {
HideOverlay(hwnd);
} else if (wParam == TIMER_TICK) {
// Repaint only when the displayed text actually changes.
wchar_t t[64], d[128];
FormatTexts(t, 64, d, 128);
if (lstrcmpW(t, g_lastTime) != 0 ||
(settings.showDate && lstrcmpW(d, g_lastDate) != 0)) {
InvalidateRect(hwnd, nullptr, FALSE);
}
}
return 0;
}
// ---------------------------------------------------------------------------
// Worker thread
// ---------------------------------------------------------------------------
DWORD WINAPI OverlayThread(LPVOID) {
// Explorer can run in several processes; only one of them should show
// the overlay. Wait until we own the mutex (or until the mod unloads).
HANDLE waits[2] = {g_stopEvent, g_mutex};
DWORD r = WaitForMultipleObjects(2, waits, FALSE, INFINITE);
if (r != WAIT_OBJECT_0 + 1 && r != WAIT_ABANDONED_0 + 1) {
return 0;
}
}
// ---------------------------------------------------------------------------
// Windhawk entry points
// ---------------------------------------------------------------------------
BOOL Wh_ModInit() {
Wh_Log(L"Init");
}
void Wh_ModUninit() {
Wh_Log(L"Uninit");
}
void Wh_ModSettingsChanged() {
Wh_Log(L"SettingsChanged");
}
All reactions