-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdim.c
175 lines (134 loc) · 3.75 KB
/
dim.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "data.h"
#define OPAQUE 0xffffffff
#define OPACITY "_NET_WM_WINDOW_OPACITY"
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define BUFFER 16
#define DIMCL "#2a4d58"
static char *fifo = "/tmp/blur_fifo";
static double dimopacity = 0.4;
static const char *dimcolor = "#2a4d58";
static char *dimname = "blur";
static char *class = "Blur";
volatile sig_atomic_t stop;
void inthand()
{
stop = 1;
}
int catcher( Display *disp, XErrorEvent *xe )
{
return 0;
}
void create(Container *cont)
{
int screen = DefaultScreen(cont->disp);
Screen *defScreen = DefaultScreenOfDisplay(cont->disp);
int dimx, dimy, dimw, dimh;
Window root = RootWindow(cont->disp, screen);
XSetWindowAttributes swa;
dimx = 0;
dimy = 0;
dimw = WidthOfScreen(defScreen);
dimh = HeightOfScreen(defScreen);
swa.override_redirect = True;
swa.background_pixel = cont->dimcol;
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
cont->win = XCreateWindow(cont->disp, root, dimx, dimy, dimw, dimh, 0,
DefaultDepth(cont->disp, screen), CopyFromParent,
DefaultVisual(cont->disp, screen),
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
XClassHint dimhint = { .res_name = dimname, .res_class = class };
XSetClassHint(cont->disp, cont->win, &dimhint);
dimopacity = MIN(MAX(dimopacity, 0), 1);
unsigned int dimopacity_set = (unsigned int)(dimopacity * OPAQUE);
XChangeProperty(cont->disp, cont->win, XInternAtom(cont->disp, OPACITY, False),
XA_CARDINAL, 32, PropModeReplace,
(unsigned char *) &dimopacity_set, 1L);
XMapRaised(cont->disp, cont->win);
}
void handle_create(Container *cont)
{
/* check if a window exists and close it */
XDestroyWindow(cont->disp, cont->win);
/* check for color */
char *cl = strtok(NULL, "|");
if(cl)
dimcolor = cl;
else
dimcolor = DIMCL;
/* check for opacity */
char *op = strtok(NULL, "|");
if(op) {
dimopacity = atof(op);
}
else
dimopacity = 0.4;
/* initialize the color */
cont->cmap = DefaultColormap(cont->disp, DefaultScreen(cont->disp));
XAllocNamedColor(cont->disp, cont->cmap, dimcolor, &cont->color, &cont->color);
cont->dimcol = cont->color.pixel;
create(cont);
}
int main(int argc, char *argv[])
{
/* register handler */
signal(SIGINT, inthand);
XSetErrorHandler( catcher );
/* local vars */
XColor color;
Colormap cmap;
unsigned long dimcol;
XEvent ev;
Container *cont = malloc(sizeof(Container));
/* parse parameters */
for(int i = 1; i < argc; i++) {
if(!strcmp(argv[i], "-up"))
dimopacity = atof(argv[++i]);
}
/* get display*/
cont->disp = XOpenDisplay(NULL);
/* initialize the color */
cont->cmap = DefaultColormap(cont->disp, DefaultScreen(cont->disp));
XAllocNamedColor(cont->disp, cmap, dimcolor, &color, &color);
cont->dimcol = color.pixel;
/* FIFO */
mkfifo(fifo, 0666);
char in[BUFFER];
/* loop, so the window stays open, wait for the signal */
while(!stop) {
/* prepare buffer */
memset(in, '\0', BUFFER);
/*open fifo and read */
int in_fd = open(fifo, O_RDONLY);
read(in_fd, in, BUFFER);
close(in_fd);
char *cmd = strtok(in,"|");
if(!cmd)
continue;
/* (ex)it (cr)eate (cl)ose */
if(!strcmp("ex",cmd))
break;
else if(!strcmp("cr",cmd))
handle_create(cont);
else if(!strcmp("cl",in))
XDestroyWindow(cont->disp, cont->win);
while(XPending(cont->disp) > 0) {
XNextEvent(cont->disp, &ev);
}
}
XDestroyWindow(cont->disp, cont->win);
free(cont);
exit(EXIT_SUCCESS);
}