Skip to content

Instantly share code, notes, and snippets.

@ctrlcctrlv
Last active July 25, 2023 03:23
Show Gist options
  • Save ctrlcctrlv/9fd8493aa88ed00693521e4ff6e0735f to your computer and use it in GitHub Desktop.
Save ctrlcctrlv/9fd8493aa88ed00693521e4ff6e0735f to your computer and use it in GitHub Desktop.
The VISUAL BELL tolls for you, Elon Musk (download all files. run `make run` which needs Xephyr, if u still use Xorg maybe will work w/o, just run a.out)
file a.out
set $count = 0
set pagination off
break _main
commands
set $count = $count + 1
printf "Loop iteration: %d\n", $count
print event
continue
end
#ifndef DIEIFEOF_H
#define DIEIFEOF_H
#include <stdio.h>
#include <sys/select.h>
// Function to check for EOF without blocking
int die_if_eof() {
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(STDIN_FILENO, &read_fds);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int select_result = select(STDIN_FILENO + 1, &read_fds, NULL, NULL, &timeout);
if (select_result == -1) {
perror("select");
return 0;
}
if (FD_ISSET(STDIN_FILENO, &read_fds)) {
int ch = getchar();
if (ch == EOF) {
printf("EOF received. Exiting...\n");
return 1;
}
}
return 0;
}
#endif /* DIEIFEOF_H */
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
a.out:
/usr/bin/gcc -g -lX11 -lglib -std=c2x x.c
.PHONY: run
run:
$(MAKE) a.out
[[ $$(/usr/bin/wc -l "`/usr/bin/ps axu|/usr/bin/grep 'Xephyr :3'|/usr/bin/grep -v grep`") -ge 1 ]] || ( nohup /usr/bin/Xephyr :3 & )
DISPLAY=:3 /usr/bin/gdb a.out -ex 'run 50'
#ifndef VISUALBELL_H
#define VISUALBELL_H
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <string.h>
#define WINDOW_WIDTH 200
#define WINDOW_HEIGHT 100
char *lines[] = {
"The VISUAL BELL",
"tolls for you,",
"Elon Musk"
};
void draw_text(Display *display, Window window, GC gc, int is_visible) {
XClearWindow(display, window);
if (is_visible) {
const char *fontname = "-*-helvetica-*-r-*-*-18-*-*-*-*-*-*-*";
XFontStruct *font = XLoadQueryFont(display, fontname);
if (font == NULL) {
fprintf(stderr, "Unable to load font: %s\n", fontname);
return;
}
XSetFont(display, gc, font->fid);
int num_lines = sizeof(lines) / sizeof(lines[0]);
int line_height = font->ascent + font->descent;
for (int i = 0; i < num_lines; i++) {
XDrawString(display, window, gc, 10, (i+1)*line_height, lines[i], strlen(lines[i]));
}
XFreeFont(display, font);
}
XFlush(display);
}
void visual_bell() {
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Cannot open display\n");
return;
}
int screen = DefaultScreen(display);
Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 1,
WhitePixel(display, screen), BlackPixel(display, screen));
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
GC gc = XCreateGC(display, window, 0, NULL);
XSetForeground(display, gc, WhitePixel(display, screen));
XSetBackground(display, gc, BlackPixel(display, screen));
XSetLineAttributes(display, gc, 1, LineSolid, CapButt, JoinBevel);
int is_visible = 1;
draw_text(display, window, gc, is_visible);
sleep(1); // display "Bell" for 1 second
is_visible = 0;
draw_text(display, window, gc, is_visible);
XCloseDisplay(display);
}
#endif // VISUALBELL_H
// Stupid quick program by Fredrick R. Brennan <[email protected]>.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include "dieifeof.h"
#include "visualbell.h"
XEvent event;
Display *display = NULL;
XKeyboardControl *keyboard_control = NULL;
int volume = -1;
void sigterm_handler(int signum) {
// Clean up X display before exiting
XAutoRepeatOff(display);
XCloseDisplay(display);
exit(0);
}
// Lookup table for key names
static const char *key_names[] = {
"KBBellPercent", "KBKeyClickPercent", "KBBellPitch",
"KBBellDuration", "KBLed", "KBLedMode",
"KBKey", "KBAutoRepeatMode"
};
int print_xkb_controls() {
if (XGetKeyboardControl(display, keyboard_control)) {
printf("Current XKeyboardControl settings:\n");
printf("Key click percent: %d\n", keyboard_control->key_click_percent);
printf("Bell percent: %d\n", keyboard_control->bell_percent);
printf("Bell pitch: %d\n", keyboard_control->bell_pitch);
printf("Bell duration: %d\n", keyboard_control->bell_duration);
printf("LED: %d\n", keyboard_control->led);
printf("LED mode: %d\n", keyboard_control->led_mode);
printf("Key: %s\n", key_names[keyboard_control->key & 0x7]);
printf("Auto repeat mode: %d\n", keyboard_control->auto_repeat_mode);
return EXIT_SUCCESS;
} else {
fprintf(stderr, "Error: Could not get keyboard control values.\n");
XCloseDisplay(display);
return EXIT_FAILURE;
}
}
int _main() {
// Try to ring the bell with the specified volume
XBell(display, volume);
// Also do a visual bell
visual_bell();
sleep((unsigned int)1);
while (XPending(display) >= 1) {
XNextEvent(display, &event);
}
return die_if_eof();
}
int main(int argc, char *argv[]) {
keyboard_control = calloc(1, sizeof(XKeyboardControl));
if (keyboard_control == NULL) exit(EXIT_FAILURE);
// Check if the volume argument is provided
if (argc != 2) {
fprintf(stderr, "Usage: %s <volume (0-100)>\n", argv[0]);
return 1;
}
// Convert the volume argument to an integer
volume = atoi(argv[1]);
if (volume < 0 || volume > 100) {
fprintf(stderr, "Error: Volume must be in the range 0 to 100.\n");
return 1;
}
// Set up SIGTERM signal handler
signal(SIGTERM, sigterm_handler);
// Open X display
display = XOpenDisplay(NULL);
if (!display) {
fprintf(stderr, "Error: Could not open X display.\n");
return 1;
}
// Check if the X server supports the XKB extension
int opcode, event, error, major, minor;
if (!XkbQueryExtension(display, &opcode, &event, &error, &major, &minor)) {
fprintf(stderr, "Error: XKB extension not supported by the X server.\n");
XCloseDisplay(display);
return 1;
}
// Check if the keyboard has a bell
int has_bell = XkbBell(display, 0, 0, 0);
if (!has_bell) {
fprintf(stderr, "Error: The keyboard does not have a bell.\n");
XCloseDisplay(display);
return 1;
}
if (print_xkb_controls()) return EXIT_FAILURE;
// Set the new bell volume
keyboard_control->bell_percent = volume;
XChangeKeyboardControl(display, KBBellPercent, keyboard_control);
if (print_xkb_controls()) return EXIT_FAILURE;
// Turn on auto-repeat for the keyboard
XAutoRepeatOn(display);
// Loop to ring the keyboard bell every second
while (1) {
if (_main()) return EXIT_FAILURE;
}
return 0;
}
@ctrlcctrlv
Copy link
Author

Just flashes this and if you have a hardware bell uses it:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment