: nply/dwhkd

commit c60cd269ba0a7b2df30c88459e0ef1dcaa9a874f
Author: theSoberSobber <akshatshrivastava0010@gmail.com>
Date: Fri, 1 Dec 2023 20:56:02 +0530

basic
10 files changed, 160 insertions(+), 0 deletions(-)
A
README.md
|
6
++++++
A
actions.cpp
|
10
++++++++++
A
actions.h
|
2
++
A
clean.bat
|
2
++
A
compile.bat
|
1
+
A
config.h
|
0
A
debug.h
|
8
++++++++
A
hotKeyTry.c
|
128
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
kill.bat
|
2
++
A
run.bat
|
1
+
diff --git a/README.md b/README.md
@@ -0,0 +1,6 @@
+# sxhkd-win32
+sxhkd impl for windows, cus ahk sucks.
+
+# TODO
+- structure correctly
+- decide on config and see how similar can it be to sxhkd
diff --git a/actions.cpp b/actions.cpp
@@ -0,0 +1,9 @@
+#ifdef DEBUG
+#include "debug.h"
+#else
+#define dbg
+#endif
+
+void test() {
+	dbg("Test");
+}+
\ No newline at end of file
diff --git a/actions.h b/actions.h
@@ -0,0 +1 @@
+static void test();+
\ No newline at end of file
diff --git a/clean.bat b/clean.bat
@@ -0,0 +1,2 @@
+@echo off
+del a.exe
diff --git a/compile.bat b/compile.bat
@@ -0,0 +1 @@
+gcc ./hotKeyTry.c -DDEBUG -mwindows
diff --git a/config.h b/config.h
diff --git a/debug.h b/debug.h
@@ -0,0 +1,7 @@
+void dbg(const char *errstr, ...) {
+	va_list ap;
+	va_start(ap, errstr);
+	vfprintf(stdout, errstr, ap);
+	fflush(stdout);
+	va_end(ap);
+}+
\ No newline at end of file
diff --git a/hotKeyTry.c b/hotKeyTry.c
@@ -0,0 +1,128 @@
+#include <windows.h>
+#include <winuser.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <shellapi.h>
+#include <stdbool.h>
+
+#define NAME   "sxhkd-win32" 	/* Used for window name/class */
+
+// options: MOD_CTRL | MOD_SHIFT | MOD_ALT
+#define MODKEY MOD_WIN
+#define HOTKEY_ID 1
+
+void eprint(const char *errstr, ...) {
+	va_list ap;
+	va_start(ap, errstr);
+	vfprintf(stdout, errstr, ap);
+	fflush(stdout);
+	va_end(ap);
+}
+
+HWND dwmhwnd;
+
+#ifdef DEBUG
+#define dbg eprint
+#else
+#define dbg
+#endif
+
+void quit(const Arg *arg) {
+	PostMessage(dwmhwnd, WM_CLOSE, 0, 0);
+}
+
+void test() {
+	dbg("Toggling Explorer!");
+}
+
+void cleanup() {
+	UnregisterHotKey(dwmhwnd, HOTKEY_ID);
+	DestroyWindow(dwmhwnd);
+}
+
+void die(const char *errstr, ...) {
+	va_list ap;
+	va_start(ap, errstr);
+	vfprintf(stdout, errstr, ap);
+	fflush(stdout);
+	va_end(ap);
+	cleanup();
+	exit(EXIT_FAILURE);
+}
+
+LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
+	switch (msg) {
+	case WM_CREATE:
+		break;
+	case WM_CLOSE:
+		cleanup();
+		break;
+	case WM_DESTROY:
+		PostQuitMessage(0);
+		break;
+	case WM_HOTKEY:
+		if (wParam == HOTKEY_ID) {
+			dbg("\nHotkey was pressed!\n");
+			test();
+		}
+		break;
+	default:
+		return DefWindowProc(hwnd, msg, wParam, lParam);
+	}
+	return 0;
+}
+
+
+void setup(HINSTANCE hInstance) {
+	WNDCLASSEX winClass;
+
+	winClass.cbSize = sizeof(WNDCLASSEX);
+	winClass.style = 0;
+	winClass.lpfnWndProc = WndProc;
+	winClass.cbClsExtra = 0;
+	winClass.cbWndExtra = 0;
+	winClass.hInstance = hInstance;
+	winClass.hIcon = NULL;
+	winClass.hIconSm = NULL;
+	winClass.hCursor = NULL;
+	winClass.hbrBackground = NULL;
+	winClass.lpszMenuName = NULL;
+	winClass.lpszClassName = NAME;
+
+	if (!RegisterClassEx(&winClass)) die("Error registering window class\n");
+	dwmhwnd = CreateWindowEx(0, NAME, NAME, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL);
+	if (!dwmhwnd) die("Error creating window\n");
+
+	RegisterHotKey(dwmhwnd, HOTKEY_ID, MODKEY, (unsigned int)'4');
+}
+
+
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
+
+#ifdef DEBUG
+	extern char ** __argv;
+	extern int __argc;
+
+	// This attaches a console to the parent process if it has a console
+	if (AttachConsole(ATTACH_PARENT_PROCESS)) {
+		// reopen stout handle as console window output
+		freopen("CONOUT$", "wb", stdout);
+		// reopen stderr handle as console window output
+		freopen("CONOUT$", "wb", stderr);
+	}
+#endif
+
+	dbg("Hello!! :))\n");
+
+	MSG msg;
+
+	setup(hInstance);
+
+	while (GetMessage(&msg, NULL, 0, 0) > 0) {
+		TranslateMessage(&msg);
+		DispatchMessage(&msg);
+	}
+	cleanup();
+
+	return msg.wParam;
+}
diff --git a/kill.bat b/kill.bat
@@ -0,0 +1,2 @@
+@echo off
+taskkill /f /im a.exe
diff --git a/run.bat b/run.bat
@@ -0,0 +1 @@
+start /w a.exe