-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxwobf.c
297 lines (248 loc) · 8.4 KB
/
xwobf.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Gustaf Lindstedt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <getopt.h>
#include <err.h>
#include <xcb/xcb.h>
#include <MagickWand/MagickWand.h>
#include "xwobf.h"
MagickWand *wand = NULL;
MagickWand *obs_wand = NULL;
xcb_connection_t *xcb_c = NULL;
xcb_screen_t *xcb_scr = NULL;
// Used to store the position/width/height of all visible windows
rectangle_t **rect = NULL;
size_t rect_size = 0;
void print_usage()
{
printf("Usage: xwobf [OPTION]... DEST\n");
printf(" -h --help\t\tprint this message and exit\n");
printf(" -s pixel_size\t\tadjust the obfuscation strength (default=9)\n");
printf(" --size=pixel_size\n");
printf(" -f --fuzzy\t\tadd a blur effect\n");
}
int main(int argc, char **argv)
{
char *file;
int pixel_size = 9;
int fuzzy = 0;
char *optstring = "hs:f";
struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"size", required_argument, NULL, 's' },
{"fuzzy", no_argument, NULL, 'f' },
{NULL, no_argument, NULL, 0}
};
int c;
while ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {
switch(c) {
case 's':
if (sscanf (optarg, "%i", &pixel_size)!=1) {
printf("Size argument should be an integer.\n");
exit(EXIT_FAILURE);
}
if (pixel_size < 1) {
printf("Size argument must be superior or equal to 1. Continuing with 1...\n");
pixel_size = 1;
}
break;
case 'f':
fuzzy = 1;
break;
default:
print_usage();
exit(EXIT_SUCCESS);
}
}
// Read the destination file
if (optind < argc) {
file = argv[optind];
} else {
printf("No output file given.\n");
print_usage();
exit(EXIT_FAILURE);
}
init();
(void)MagickReadImage(wand,"x:root");
obscure_image(pixel_size, fuzzy);
(void)MagickWriteImage(wand,file);
cleanup();
return EXIT_SUCCESS;
}
void init()
{
int screen_num;
// Connect to the x server
xcb_c = xcb_connect(NULL, &screen_num);
check_xcb_error(xcb_c);
// Find the root window
xcb_screen_iterator_t s_it = xcb_setup_roots_iterator(xcb_get_setup(xcb_c));
for (; s_it.rem; --screen_num, xcb_screen_next (&s_it)) {
if (screen_num == 0) {
xcb_scr = s_it.data;
break;
}
}
MagickWandGenesis();
wand = NewMagickWand();
find_rectangles();
}
// Check if the xcb_connection has encountered an error.
// Exit if an error has occurred.
void check_xcb_error(xcb_connection_t *xcb_connection)
{
int xcb_error = xcb_connection_has_error(xcb_connection);
if (xcb_error > 0) {
switch(xcb_error) {
case XCB_CONN_ERROR:
errx(EXIT_FAILURE,"[XCB_CONN_ERROR]");
break;
case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
errx(EXIT_FAILURE,"[XCB_CONN_CLOSED_EXT_NOTSUPPORTED]");
break;
case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
errx(EXIT_FAILURE,"[XCB_CONN_CLOSED_MEM_INSUFFICIENT]");
break;
case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
errx(EXIT_FAILURE,"[XCB_CONN_CLOSED_REQ_LEN_EXCEED]");
break;
case XCB_CONN_CLOSED_PARSE_ERR:
errx(EXIT_FAILURE,"[XCB_CONN_CLOSED_PARSE_ERR]");
break;
case XCB_CONN_CLOSED_INVALID_SCREEN:
errx(EXIT_FAILURE,"[XCB_CONN_CLOSED_INVALID_SCREEN]");
break;
default:
printf("[UNKNOWN_ERROR] %d\n", xcb_error);
exit(EXIT_FAILURE);
}
}
}
void cleanup()
{
xcb_disconnect(xcb_c);
if (wand) wand = DestroyMagickWand(wand);
if (obs_wand) obs_wand = DestroyMagickWand(obs_wand);
MagickWandTerminus();
free_rectangles();
}
// Obscure the image!
void obscure_image(int pixel_size, int fuzzy)
{
for(size_t i = 0; i < rect_size; ++i) {
obscure_rectangle(rect[i], pixel_size, fuzzy);
}
}
// Obscure the area within the given rectangle
void obscure_rectangle(rectangle_t *rec, int pixel_size, int fuzzy)
{
if ((obs_wand = CloneMagickWand(wand))) {
(void)MagickCropImage(obs_wand, rec->w, rec->h, rec->x, rec->y);
// This is where the magick happens
(void)MagickResizeImage(obs_wand, (rec->w)/pixel_size, (rec->h)/pixel_size,
PointFilter);
if (fuzzy) {
(void)MagickBlurImage(obs_wand, 0, 1);
}
(void)MagickResizeImage(obs_wand, rec->w, rec->h,
PointFilter);
(void)MagickCompositeImage(wand, obs_wand, OverCompositeOp, MagickTrue, rec->x, rec->y);
obs_wand = DestroyMagickWand(obs_wand);
}
}
// Check if a window is visible
// Return 1 if visible, 0 otherwise
int window_is_visible(xcb_window_t win)
{
xcb_get_window_attributes_cookie_t cookie;
xcb_get_window_attributes_reply_t *reply;
cookie = xcb_get_window_attributes(xcb_c, win);
int retval = 0;
if ((reply = xcb_get_window_attributes_reply(xcb_c, cookie, NULL))) {
if (XCB_MAP_STATE_VIEWABLE == (*reply).map_state)
retval = 1;
free(reply);
}
return retval;
}
// Populate an array of rectangle_t pointers with all the visible windows
void find_rectangles()
{
xcb_query_tree_cookie_t cookie = xcb_query_tree(xcb_c, xcb_scr->root);
xcb_query_tree_reply_t *reply;
if ((reply = xcb_query_tree_reply(xcb_c, cookie, NULL))) {
xcb_window_t *children = xcb_query_tree_children(reply);
int num_children = xcb_query_tree_children_length(reply);
// At most there are num_children visible windows
rect = malloc(sizeof(rectangle_t*) * num_children);
// Use rect_size to count the number of visible rectangles
rect_size = 0;
for (int i = 0; i < xcb_query_tree_children_length(reply); i++) {
if (window_is_visible(children[i])) {
rect[rect_size++] = get_rectangle(children[i]);
}
}
free(reply);
}
}
// Get the position, width and height of an xcb_window
rectangle_t *get_rectangle(xcb_window_t win)
{
xcb_get_geometry_cookie_t cookie;
xcb_get_geometry_reply_t *reply;
cookie = xcb_get_geometry(xcb_c, win);
if ((reply = xcb_get_geometry_reply(xcb_c, cookie, NULL))) {
rectangle_t *rectangle = malloc(sizeof(rectangle_t));
rectangle->x = (size_t) reply->x;
rectangle->y = (size_t) reply->y;
rectangle->w = (size_t) reply->width;
rectangle->h = (size_t) reply->height;
free(reply);
return rectangle;
}
return NULL;
}
// Free every rectangle in the array, including the array itself
void free_rectangles()
{
for(size_t i = 0; i < rect_size; ++i) {
free(rect[i]);
}
free(rect);
}
// DEBUG FUNCTIONS
void print_rectangle(rectangle_t *rec)
{
printf("Rec { x: %*zu y: %*zu w: %*zu h: %*zu }\n",
4,rec->x,4,rec->y,4,rec->w,4,rec->h);
}
void print_rectangle_array(rectangle_t **rec_arr, size_t size)
{
printf("RecArray {\n");
for(size_t i = 0; i < size; ++i) {
printf(" ");
print_rectangle(rec_arr[i]);
}
printf("}\n");
}