forked from cbdevnet/xecho
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx11.c
More file actions
616 lines (527 loc) · 17.1 KB
/
x11.c
File metadata and controls
616 lines (527 loc) · 17.1 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
bool xfd_add(X_FDS* set, int fd){
unsigned i;
if(!set->fds){
set->fds=malloc(sizeof(int));
if(!set->fds){
fprintf(stderr, "xfd_add: Initial alloc failed\n");
return false;
}
set->size=1;
set->fds[0]=fd;
return true;
}
for(i=0;i<set->size;i++){
if(set->fds[i]==fd){
fprintf(stderr, "xfd_add: Not pushing duplicate entry\n");
return false;
}
}
set->fds=realloc(set->fds, (set->size+1)*sizeof(int));
if(!set->fds){
fprintf(stderr, "xfd_add: Failed to realloc fd set\n");
return false;
}
set->fds[set->size]=fd;
set->size++;
return true;
}
bool xfd_remove(X_FDS* set, int fd){
unsigned i, c;
for(i=0;i<set->size;i++){
if(set->fds[i]==fd){
for(c=i;c<set->size-1;c++){
set->fds[c]=set->fds[c+1];
}
set->size--;
set->fds=realloc(set->fds, set->size*sizeof(int));
if(!set->fds&&set->size>0){
fprintf(stderr, "xfd_remove: Failed to realloc\n");
return false;
}
return true;
}
}
fprintf(stderr, "xfd_remove: FD not in set\n");
return false;
}
void xfd_free(X_FDS* set){
if(set->fds){
free(set->fds);
set->fds=NULL;
}
set->size=0;
}
void xconn_watch(Display* dpy, XPointer client_data, int fd, Bool opening, XPointer* watch_data){
if(opening){
fprintf(stderr, "xconn_watch: Internal connection registered\n");
xfd_add((X_FDS*)client_data, fd);
}
else{
fprintf(stderr, "xconn_watch: Internal connection closed\n");
xfd_remove((X_FDS*)client_data, fd);
}
}
bool x11_init(XRESOURCES* res, CFG* config){
Window root;
XSetWindowAttributes window_attributes;
unsigned width, height;
Atom wm_state_fullscreen;
int xdbe_major, xdbe_minor;
//allocate some structures
XSizeHints* size_hints=XAllocSizeHints();
XWMHints* wm_hints=XAllocWMHints();
XClassHint* class_hints=XAllocClassHint();
if(!size_hints||!wm_hints||!class_hints){
fprintf(stderr, "Failed to allocate X data structures\n");
return false;
}
//x data initialization
res->display=XOpenDisplay(NULL);
if(!(res->display)){
fprintf(stderr, "Failed to open display\n");
XFree(size_hints);
XFree(wm_hints);
XFree(class_hints);
return false;
}
if(config->double_buffer){
config->double_buffer=(XdbeQueryExtension(res->display, &xdbe_major, &xdbe_minor)!=0);
}
else{
config->double_buffer=false;
}
errlog(config, LOG_INFO, "Double buffering %s\n", config->double_buffer?"enabled":"disabled");
res->screen=DefaultScreen(res->display);
root=RootWindow(res->display, res->screen);
//start xft
if(!XftInit(NULL)){
fprintf(stderr, "Failed to initialize Xft\n");
XFree(size_hints);
XFree(wm_hints);
XFree(class_hints);
return false;
}
//set up colors
res->text_color=colorspec_parse(config->text_color, res->display, res->screen);
res->bg_color=colorspec_parse(config->bg_color, res->display, res->screen);
res->debug_color=colorspec_parse(config->debug_color, res->display, res->screen);
//set up window params
window_attributes.background_pixel=res->bg_color.pixel;
window_attributes.cursor=None;
window_attributes.event_mask=ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask;
width=DisplayWidth(res->display, res->screen);
height=DisplayHeight(res->display, res->screen);
//create window
res->main=XCreateWindow(res->display,
root,
0,
0,
width,
height,
0,
CopyFromParent,
InputOutput,
CopyFromParent,
CWBackPixel | CWCursor | CWEventMask,
&window_attributes);
//set window properties (TODO XSetWMProperties)
class_hints->res_name="xecho-binary";
class_hints->res_class="xecho";
//XSetWMProperties(RESOURCES.display, RESOURCES.main_window, "xecho", NULL, argv, argc, size_hints, wm_hints, class_hints);
XFree(size_hints);
XFree(wm_hints);
XFree(class_hints);
//set fullscreen mode
wm_state_fullscreen=XInternAtom(res->display, "_NET_WM_STATE_FULLSCREEN", False);
XChangeProperty(res->display, res->main, XInternAtom(res->display, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char*) &wm_state_fullscreen, 1);
//allocate back drawing buffer
if(config->double_buffer){
res->back_buffer=XdbeAllocateBackBufferName(res->display, res->main, XdbeBackground);
}
//make xft drawable from window
res->drawable=XftDrawCreate(res->display, (config->double_buffer?res->back_buffer:res->main), DefaultVisual(res->display, res->screen), DefaultColormap(res->display, res->screen));
if(!res->drawable){
fprintf(stderr, "Failed to allocate drawable\n");
return false;
}
//map window
XMapRaised(res->display, res->main);
//get x socket fds
if(!xfd_add(&(res->xfds), XConnectionNumber(res->display))){
fprintf(stderr, "Failed to allocate xfd memory\n");
return false;
}
if(XAddConnectionWatch(res->display, xconn_watch, (void*)(&(res->xfds)))==0){
fprintf(stderr, "Failed to register connection watch procedure\n");
return false;
}
return true;
}
void x11_cleanup(XRESOURCES* xres, CFG* config){
if(!(xres->display)){
return;
}
XftColorFree(xres->display, DefaultVisual(xres->display, xres->screen), DefaultColormap(xres->display, xres->screen), &(xres->text_color));
XftColorFree(xres->display, DefaultVisual(xres->display, xres->screen), DefaultColormap(xres->display, xres->screen), &(xres->bg_color));
XftColorFree(xres->display, DefaultVisual(xres->display, xres->screen), DefaultColormap(xres->display, xres->screen), &(xres->debug_color));
if(xres->drawable){
XftDrawDestroy(xres->drawable);
}
if(config->double_buffer){
XdbeDeallocateBackBufferName(xres->display, xres->back_buffer);
}
XCloseDisplay(xres->display);
xfd_free(&(xres->xfds));
}
bool x11_draw_blocks(CFG* config, XRESOURCES* xres, TEXTBLOCK** blocks){
unsigned i;
double current_size;
XftFont* font=NULL;
//early exit
if(!blocks||!blocks[0]){
return true;
}
//draw debug blocks if requested
if(config->debug_boxes){
for(i=0;blocks[i]&&blocks[i]->active;i++){
XftDrawRect(xres->drawable, &(xres->debug_color), blocks[i]->layout_x, blocks[i]->layout_y, blocks[i]->extents.width, blocks[i]->extents.height);
}
}
//draw boxes only
if(config->disable_text){
return true;
}
//draw all blocks
for(i=0;blocks[i]&&blocks[i]->active;i++){
//load font
if(!font||(font&¤t_size!=blocks[i]->size)){
if(font){
XftFontClose(xres->display, font);
}
font=XftFontOpen(xres->display, xres->screen,
XFT_FAMILY, XftTypeString, config->font_name,
XFT_PIXEL_SIZE, XftTypeDouble, blocks[i]->size,
NULL
);
current_size=blocks[i]->size;
if(!font){
fprintf(stderr, "Failed to load block font (%s, %d)\n", config->font_name, (int)current_size);
return false;
}
}
//draw text
errlog(config, LOG_DEBUG, "Drawing block %d (%s) at layoutcoords %d|%d size %d\n", i, blocks[i]->text,
blocks[i]->layout_x+blocks[i]->extents.x,
blocks[i]->layout_y+blocks[i]->extents.y,
(int)blocks[i]->size);
XftDrawStringUtf8(xres->drawable,
&(xres->text_color),
font,
blocks[i]->layout_x+blocks[i]->extents.x,
blocks[i]->layout_y+blocks[i]->extents.y,
(FcChar8*)blocks[i]->text,
strlen(blocks[i]->text));
}
//clean up the mess
if(font){
XftFontClose(xres->display, font);
}
return true;
}
bool x11_blocks_resize(XRESOURCES* xres, CFG* config, TEXTBLOCK** blocks, XGlyphInfo* bounding_box, double size){
XftFont* font=NULL;
unsigned bounding_width=0, bounding_height=0;
unsigned i;
//FIXME is config needed here?
//load font with at supplied size
font=XftFontOpen(xres->display, xres->screen,
XFT_FAMILY, XftTypeString, config->font_name,
XFT_PIXEL_SIZE, XftTypeDouble, size,
NULL
);
if(!font){
fprintf(stderr, "Could not load font\n");
return false;
}
//fprintf(stderr, "Block \"%s\" extents: width %d, height %d, x %d, y %d, xOff %d, yOff %d\n",
// block->text, block->extents.width, block->extents.height, block->extents.x, block->extents.y,
// block->extents.xOff, block->extents.yOff);
//bounds calculation
for(i=0;blocks[i]&&blocks[i]->active;i++){
//update only not yet calculated blocks
if(!(blocks[i]->calculated)){
XftTextExtentsUtf8(xres->display, font, (FcChar8*)blocks[i]->text, strlen(blocks[i]->text), &(blocks[i]->extents));
errlog(config, LOG_DEBUG, "Recalculated block %d (%s) extents: %dx%d\n", i, blocks[i]->text, blocks[i]->extents.width, blocks[i]->extents.height);
blocks[i]->size=size;
}
//calculate bounding box over all
bounding_height+=blocks[i]->extents.height;
if(blocks[i]->extents.width>bounding_width){
bounding_width=blocks[i]->extents.width;
}
}
if(bounding_box){
bounding_box->width=bounding_width;
bounding_box->height=bounding_height;
}
XftFontClose(xres->display, font);
return true;
}
bool x11_maximize_blocks(XRESOURCES* xres, CFG* config, TEXTBLOCK** blocks, unsigned width, unsigned height){
unsigned i, num_blocks=0;
double current_size=1;
unsigned bound_low, bound_high, bound_delta;
unsigned done_block, longest_block;
XGlyphInfo bbox;
bool break_loop=false;
int bounds_delta=4; //initial secondary bound delta
//count blocks
for(i=0;blocks[i]&&blocks[i]->active;i++){
if(!blocks[i]->calculated){
num_blocks++;
}
}
//no blocks, bail out
if(num_blocks<1||width<1||height<1){
errlog(config, LOG_DEBUG, "Maximizer bailing out, nothing to do\n");
return true;
}
errlog(config, LOG_DEBUG, "Maximizer running for %dx%d bounds\n", width, height);
//guess primary bound
//sizes in sets to be maximized are always the same,
//since any pass modifies all active blocks to the same size
longest_block=string_block_longest(blocks);
if(blocks[longest_block]->size==0){
if(config->max_size>0){
//use max size as primary bound
current_size=config->max_size;
}
else{
//educated guess
current_size=fabs(width/((strlen(blocks[longest_block]->text)>0)?strlen(blocks[longest_block]->text):1));
}
}
else{
//use last known size as primary bound
current_size=blocks[longest_block]->size;
}
errlog(config, LOG_DEBUG, "Guessing primary bound %d\n", (int)current_size);
//find secondary bound for binary search
if(!x11_blocks_resize(xres, config, blocks, &bbox, current_size)){
fprintf(stderr, "Failed to resize blocks to primary bound\n");
}
if(bbox.height>height||bbox.width>width){
//primary bound is upper bound, search down
bounds_delta*=-1;
}
errlog(config, LOG_DEBUG, "Primary bound is %s than bounding box\n", (bounds_delta<0)?"bigger":"smaller");
do{
bounds_delta*=2;
if(current_size+bounds_delta<1){
errlog(config, LOG_DEBUG, "Search went out of permissible range\n");
bounds_delta=-current_size; //FIXME this might fail when the condition is met with an overflow
break;
}
if(!x11_blocks_resize(xres, config, blocks, &bbox, current_size+bounds_delta)){
fprintf(stderr, "Failed to resize blocks to size %d\n", (int)current_size+bounds_delta);
return false;
}
if(bbox.width<1||bbox.height<1){
errlog(config, LOG_DEBUG, "Bounding box was empty\n");
return true;
}
errlog(config, LOG_DEBUG, "With bounds_delta %d bounding box is %dx%d\n", bounds_delta, bbox.width, bbox.height);
}
//loop until direction needs to be reversed
while( ((bounds_delta<0)&&(bbox.width>width||bbox.height>height)) //searching lower bound, break if within bounds
|| ((bounds_delta>0)&&(bbox.width<=width&&bbox.height<=height))); //searching upper bound, break if out of bounds
errlog(config, LOG_DEBUG, "Calculated secondary bound %d via offset %d\n", (int)current_size+bounds_delta, bounds_delta);
//prepare bounds for binary search
if(bounds_delta<0){
bound_low=current_size+bounds_delta;
bound_high=current_size; //cant optimize here if starting bound matches exactly
}
else{
bound_high=current_size+bounds_delta;
bound_low=current_size; //cant optimize here if starting bound matches exactly
}
if(config->max_size>0&&bound_high>config->max_size){
errlog(config, LOG_DEBUG, "Enforcing size constraint\n");
bound_high=config->max_size;
}
if(config->max_size>0&&bound_low>config->max_size){
bound_low=1;
}
//binary search for final size
do{
bound_delta=bound_high-bound_low;
current_size=bound_low+((double)bound_delta/(double)2);
//stupid tiebreaker implementation
if(bound_delta/2==0){
if(break_loop){
current_size=bound_high;
}
else{
break_loop=true;
}
}
errlog(config, LOG_DEBUG, "Binary search testing size %d, hi %d, lo %d, delta %d\n", (int)current_size, bound_high, bound_low, bound_delta);
if(!x11_blocks_resize(xres, config, blocks, &bbox, current_size)){
fprintf(stderr, "Failed to resize blocks to test size %d\n", (int)current_size);
return false;
}
if(bbox.width<1||bbox.height<1){
errlog(config, LOG_DEBUG, "Bounding box is 0, bailing out\n");
break;
}
if(bbox.width>width||bbox.height>height){
//out of bounds
bound_high=current_size;
errlog(config, LOG_DEBUG, "-> OOB\n");
}
else{
//inside bounds
bound_low=current_size;
errlog(config, LOG_DEBUG, "-> OK\n");
}
}while(bound_delta>0);
errlog(config, LOG_DEBUG, "Final size is %d\n", (int)current_size);
//set active to false for longest
//FIXME find longest by actual extents
done_block=string_block_longest(blocks);
blocks[done_block]->calculated=true;
errlog(config, LOG_DEBUG, "Marked block %d as done\n", done_block);
return true;
}
bool x11_align_blocks(XRESOURCES* xres, CFG* config, TEXTBLOCK** blocks, unsigned width, unsigned height){
//align blocks within bounding rectangle according to configured alignment
unsigned i, total_height=0, current_height=0;
for(i=0;blocks[i]&&blocks[i]->active;i++){
total_height+=blocks[i]->extents.height;
}
if(i>0){
total_height+=config->line_spacing*(i-1);
}
//FIXME this might underflow in some cases
for(i=0;blocks[i]&&blocks[i]->active;i++){
//align x axis
switch(config->alignment){
case ALIGN_NORTH:
case ALIGN_SOUTH:
case ALIGN_CENTER:
//centered
blocks[i]->layout_x=(width-(blocks[i]->extents.width))/2;
break;
case ALIGN_NORTHWEST:
case ALIGN_WEST:
case ALIGN_SOUTHWEST:
//left
blocks[i]->layout_x=config->padding;
break;
case ALIGN_NORTHEAST:
case ALIGN_EAST:
case ALIGN_SOUTHEAST:
//right
blocks[i]->layout_x=width-(blocks[i]->extents.width)-config->padding;
break;
}
//align y axis
switch(config->alignment){
case ALIGN_WEST:
case ALIGN_EAST:
case ALIGN_CENTER:
//centered
blocks[i]->layout_y=((height-total_height)/2)
+current_height
+((current_height>0)?config->line_spacing:0);
current_height+=blocks[i]->extents.height
+((current_height>0)?config->line_spacing:0);
break;
case ALIGN_NORTHWEST:
case ALIGN_NORTH:
case ALIGN_NORTHEAST:
//top
blocks[i]->layout_y=(config->padding)
+current_height
+((current_height>0)?config->line_spacing:0);
current_height+=blocks[i]->extents.height
+((current_height>0)?config->line_spacing:0);
break;
case ALIGN_SOUTHWEST:
case ALIGN_SOUTH:
case ALIGN_SOUTHEAST:
//bottom
blocks[i]->layout_y=height-total_height-(config->padding)
+((current_height>0)?config->line_spacing:0);
total_height-=(blocks[i]->extents.height
+((current_height>0)?config->line_spacing:0));
current_height+=blocks[i]->extents.height
+((current_height>0)?config->line_spacing:0);
break;
}
}
return true;
}
bool x11_recalculate_blocks(CFG* config, XRESOURCES* xres, TEXTBLOCK** blocks, unsigned width, unsigned height){
unsigned i, num_blocks=0;
unsigned layout_width=width, layout_height=height;
//early exit.
if(!blocks||!blocks[0]){
return true;
}
//initialize calculation set
for(i=0;blocks[i]&&blocks[i]->active;i++){
errlog(config, LOG_INFO, "Block %d: %s\n", i, blocks[i]->text);
if(blocks[i]->text[0]){
blocks[i]->calculated=false;
}
else{
//disable obviously empty blocks before running maximizer
errlog(config, LOG_DEBUG, "Disabling empty block %d\n", i);
blocks[i]->calculated=true;
blocks[i]->extents.width=0;
blocks[i]->extents.height=0;
blocks[i]->extents.x=0;
blocks[i]->extents.y=0;
}
num_blocks++;
}
//calculate layout volume
if(width>(2*config->padding)){
layout_width-=2*config->padding;
}
if(height>(2*config->padding)){
errlog(config, LOG_DEBUG, "Subtracting %d pixels for height padding\n", config->padding);
layout_height-=2*config->padding;
}
if(num_blocks>1&&(((num_blocks-1)*(config->line_spacing)<layout_height))){
errlog(config, LOG_DEBUG, "Subtracting %d pixels for linespacing\n", (num_blocks-1)*config->line_spacing);
layout_height-=(num_blocks-1)*config->line_spacing;
}
errlog(config, LOG_INFO, "Window volume %dx%d, layout volume %dx%d\n", width, height, layout_width, layout_height);
if(config->force_size==0){
//do binary search for match size
i=0;
do{
errlog(config, LOG_DEBUG, "Running maximizer for pass %d (%d blocks)\n", i, num_blocks);
if(!x11_maximize_blocks(xres, config, blocks, layout_width, layout_height)){
return false;
}
i++;
}
//do multiple passes if flag is set
while(config->independent_resize&&i<num_blocks);
}
else{
//render with forced size
if(!x11_blocks_resize(xres, config, blocks, NULL, config->force_size)){
fprintf(stderr, "Failed to resize blocks\n");
return false;
}
}
//do alignment pass
if(!x11_align_blocks(xres, config, blocks, width, height)){
return false;
}
return true;
}