ref: 69094e255d18e4ad55302705a0144b8df4a0321f
author: pra <pra@cloud9p.org>
date: Thu Nov 23 22:59:33 EST 2023
initial commit
--- /dev/null
+++ b/.gitignore
@@ -1,0 +1,2 @@
+*.o
+keylock
--- /dev/null
+++ b/Makefile
@@ -1,0 +1,25 @@
+include config.mk
+
+SRC = keylock.c
+OBJ = ${SRC:.c=.o}
+
+all: keylock
+
+.c.o:
+ ${CC} -c ${CFLAGS} $<
+
+${OBJ}: config.mk
+
+keylock: ${OBJ}
+ ${CC} -o $@ ${LIBS} ${OBJ}
+
+install: all
+ mkdir -p ${PREFIX}
+ install -m 0755 ${PROGNAME} ${PREFIX}/bin
+uninstall:
+ rm ${PREFIX}/bin/${PROGNAME}
+
+clean:
+ rm *.o ${PROGNAME}
+
+.PHONY: all install uninstall clean
--- /dev/null
+++ b/README.md
@@ -1,0 +1,18 @@
+Keylock
+
+Keylock is a simple keyboard/mice locker.
+It uses Xlib to lock the keyboard and mice/mouse.
+Quit/Unlock, with pressing CTRL+ALT+u.
+Keys like audio lower/raise, play,pause and such, are not locked.
+CTRL+ALT+{F1,F2,F3,F4,F5,F6}
+
+Installation
+
+make install
+or
+make PREFIX=/usr/local install
+Note: `keylock` will be copied to `~/.local/bin` directory by default.
+
+Usage
+
+Simply execute/run `keylock` on a terminal emulator, or from `dmenu`.
--- /dev/null
+++ b/config.mk
@@ -1,0 +1,12 @@
+PREFIX = ~/.local
+PROGNAME = keylock
+
+# OpenBSD
+X11INC = /usr/X11R6/include
+X11LIB = /usr/X11R6/lib
+
+
+INCS = -I${X11INC}
+LIBS = -L${X11LIB} -l X11
+CFLAGS = -Wall -Wno-deprecated-declarations ${INCS}
+
--- /dev/null
+++ b/keylock.c
@@ -1,0 +1,68 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include <X11/keysym.h>
+
+int main(void)
+{
+ Display *xdisplay = XOpenDisplay(NULL);
+ if (xdisplay == NULL) {
+ fprintf(stderr, "%s\n", "Failed to open X display");
+ exit(1);
+ }
+
+ Window root_win = DefaultRootWindow(xdisplay);
+
+ int rc = 1;
+ rc = XGrabKeyboard(xdisplay, root_win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
+ if (rc != GrabSuccess) {
+ perror("XGrabKeyboard");
+ return (EXIT_FAILURE);
+ }
+ rc = XGrabPointer(xdisplay, root_win, False, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
+ if (rc != GrabSuccess) {
+ perror("XGrabPointer");
+ return (EXIT_FAILURE);
+ }
+
+ KeySym quit_key = XK_u;
+ XEvent event;
+ unsigned int cntrl_mask = ControlMask;
+ unsigned int alt_mask = Mod1Mask;
+
+ for (;;) {
+ XNextEvent(xdisplay, &event);
+
+ if (event.type == KeyPress) {
+ if (event.xkey.keycode == XKeysymToKeycode(xdisplay, quit_key) &&
+ event.xkey.state & cntrl_mask &&
+ event.xkey.state & alt_mask) {
+
+ break;
+
+ } else {
+
+ continue;
+ }
+ }
+
+ /* You could quit/unlock with mouse/mice as well.
+ * Uncomment the following lines and re-compile.
+ * The unlock/quit sequence with mouse/mice is a quick left-click then right-click.
+ * It needs to be really quick.
+ */
+
+ /* else if (event.type == ButtonPress) {
+ if (event.xbutton.state & Button1Mask) {
+
+ break;
+ } else {
+
+ continue;
+ }
+ } */
+ }
+
+ XCloseDisplay(xdisplay);
+ return (EXIT_SUCCESS);
+}