util  Check-in [4dc779457a]

Overview
Comment:initial commit; add safekill
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4dc779457a45c84fd327742efec3e9d2d96879ae14bb33e9a7bc217c9ca6ef79
User & Date: lexi on 2019-04-08 02:07:07
Other Links: manifest | tags
Context
2019-04-08
02:15
add licensing info check-in: e3a5a8ccea user: lexi tags: trunk
02:07
initial commit; add safekill check-in: 4dc779457a user: lexi tags: trunk
02:01
initial empty check-in check-in: 3b99e03c29 user: lexi tags: trunk
Changes

Added readme.md version [c82e74fe1c].









>
>
>
>
1
2
3
4
# util
various odds and ends

* **safekill.c**: utility to help keep from accidentally killing important windows; compile with `cc -Ofast safekill.c -lX11 -lc -osafekill`

Added safekill.c version [77306fdca0].

















































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef enum { false, true } bool;
char* isvital(char* str) {
	start: // tail calls for dipshits
		if (*str != '{') {
			if (*str == 0) return 0;
			++str; goto start;
		} else {
			if (str[1] == 'V' && str[2] == '}')
				return str;
			else { ++str; goto start; }
		}
}
int main(const int argc, const char** argv) {
	Display *display;
	Window focus;
	int revert;

	bool active;
	enum { vitalize, devitalize, hardkill, softkill } op;

	if (argc > 3) goto usage;
	if (argc == 1)
		op = softkill, active = true;
	else {
		if (argc == 2) active = true;
		else           active = false;

		if (argv[1][0] == '-' && argv[1][2] == 0) { // opt is short switch
			switch (argv[1][1]) { // opt char

				// make window mission-critical
				case 'v': op = vitalize; break;

				// make window acceptable loss
				case 'c': op = devitalize; break;

				// liquidate by any means necessary
				case 'q': op = hardkill; break;

				// with all due respect sir what the fuck
				default: goto usage;
			}
		} else goto usage;
	}

	display = XOpenDisplay(NULL);

	if (active) {
		XGetInputFocus(display, &focus, &revert);
	} else {
		unsigned long temp = strtoul(argv[2], NULL, 0);
		if (errno == EINVAL || errno == ERANGE) goto usage;
		if (temp == 0) goto usage;
		focus = (Window)temp;
	}

	if(op == hardkill) {
		XDestroyWindow(display,focus);
		XSync(display,false);
		return 0;
	}

	XClassHint xclh;
	if (XGetClassHint(display,focus,&xclh)) {
		char* v;
		if(v = isvital(xclh.res_class)) {

			if (op == vitalize) return 0; // nothing need be done
			else if (op == devitalize) {
				strcpy(v, v+3);
				XSetClassHint(display,focus,&xclh);
				XSync(display,false);
			} else if (op == softkill) {
				printf("softkill forbidden\n");
				return 1; // ACCESS DENIED
			}
		} else {
			if (op == vitalize) {
				size_t sz=strlen(xclh.res_class); // TODO: remove double-walk
				xclh.res_class=realloc(xclh.res_class, sz+4);
				xclh.res_class[sz]='{';
				xclh.res_class[sz+1]='V';
				xclh.res_class[sz+2]='}';
				xclh.res_class[sz+3]=0;
				XSetClassHint(display,focus,&xclh);
				XSync(display,false);
			} else if (op == devitalize) return 0;
			else if (op == softkill) {
				// ice that motherfucker
				XDestroyWindow(display,focus);
				XSync(display,false);
				return 3;
			}
		}
	} else {
		printf("\x1b[1merror:\x1b[m bad window\n");
		goto usage;
	}

	return 0;

usage:
	#define info  "\x1b[34m-- "
	#define param "\x1b[32m"
	#define eol   "\x1b[m\n"
	#define bold  "\x1b[1m"
	#define nl    "       "
	printf(bold "usage:\x1b[0m %s    "       "     " info "kill active window if non-vital" eol
						 nl "%1$s -v " param "[id] " info "make [id] or active window vital" eol
						 nl "%1$s -c " param "[id] " info "clear vital flag on [id] or active window" eol
						 nl "%1$s -q " param "[id] " info "'emergency' kill w/o reference to vital flag" eol,
				argv[0]);
	return 1;
}