-
-
Notifications
You must be signed in to change notification settings - Fork 487
/
compositor.c
3074 lines (2751 loc) · 95.7 KB
/
compositor.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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @brief Yutani - The ToaruOS Window Compositor.
*
* Yutani is a canvas-based window compositor and manager.
* It employs shared memory to provide clients access to
* canvases in which they may render, while using a packet-based
* socket interface to communicate actions between the server
* and client such as keyboard activity, mouse movement, responses
* to client events, etc., as well as to communicate requests from
* the client to the server, such as creation of new windows,
* movement, resizing, and display updates.
*
* @copyright
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2018 K. Lange
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#include <getopt.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/fswait.h>
#include <sys/sysfunc.h>
#include <sys/shm.h>
#include <pthread.h>
#include <dlfcn.h>
#include <toaru/graphics.h>
#include <toaru/mouse.h>
#include <toaru/kbd.h>
#include <toaru/pex.h>
#include <toaru/yutani.h>
#include <toaru/yutani-internal.h>
#include <toaru/yutani-server.h>
#include <toaru/hashmap.h>
#include <toaru/list.h>
#include <toaru/text.h>
#define _DEBUG_YUTANI
#ifdef _DEBUG_YUTANI
#include <toaru/trace.h>
#define TRACE_APP_NAME "yutani"
#else
#define TRACE(msg,...)
#endif
/* Early definitions */
static void mark_window(yutani_globals_t * yg, yutani_server_window_t * window);
static void window_actually_close(yutani_globals_t * yg, yutani_server_window_t * w);
static void notify_subscribers(yutani_globals_t * yg);
static void mouse_stop_drag(yutani_globals_t * yg);
static void window_move(yutani_globals_t * yg, yutani_server_window_t * window, int x, int y);
static yutani_server_window_t * top_at(yutani_globals_t * yg, uint16_t x, uint16_t y);
static void window_unminimize(yutani_globals_t * yg, yutani_server_window_t * window);
static void window_finish_minimize(yutani_globals_t * yg, yutani_server_window_t * w);
#define ENABLE_BLUR_BEHIND
#ifdef ENABLE_BLUR_BEHIND
#define BLUR_CLIP_MAX 20
#define BLUR_KERNEL 10
static char * blur_texture = NULL;
static gfx_context_t * blur_ctx = NULL;
static gfx_context_t * clip_ctx = NULL;
#endif
/**
* Print usage information.
*/
static int usage(char * argv[]) {
fprintf(stderr,
"Yutani - Window Compositor\n"
"\n"
"usage: %s [-n [-g WxH]] [-h]\n"
"\n"
" -n --nested \033[3mRun in a window.\033[0m\n"
" -h --help \033[3mShow this help message.\033[0m\n"
" -g --geometry \033[3mSet the size of the server framebuffer.\033[0m\n"
"\n"
" Yutani is the standard system compositor.\n"
"\n",
argv[0]);
return 1;
}
/**
* Parse arguments
*/
static int parse_args(int argc, char * argv[], int * out) {
static struct option long_opts[] = {
{"nested", no_argument, 0, 'n'},
{"geometry", required_argument, 0, 'g'},
{"help", no_argument, 0, 'h'},
{0,0,0,0}
};
int index, c;
while ((c = getopt_long(argc, argv, "hg:n", long_opts, &index)) != -1) {
if (!c) {
if (long_opts[index].flag == 0) {
c = long_opts[index].val;
}
}
switch (c) {
case 'h':
return usage(argv);
case 'n':
yutani_options.nested = 1;
break;
case 'g':
{
char * c = strstr(optarg, "x");
if (c) {
*c = '\0';
c++;
yutani_options.nest_width = atoi(optarg);
yutani_options.nest_height = atoi(c);
}
}
break;
default:
fprintf(stderr, "Unrecognized option: %c\n", c);
break;
}
}
*out = optind;
return 0;
}
static int32_t min(int32_t a, int32_t b) {
return (a < b) ? a : b;
}
static int32_t max(int32_t a, int32_t b) {
return (a > b) ? a : b;
}
static int next_buf_id(void) {
static int _next = 1;
return _next++;
}
static int next_wid(void) {
static int _next = 1;
return _next++;
}
uint64_t yutani_current_time(yutani_globals_t * yg) {
struct timeval t;
gettimeofday(&t, NULL);
time_t sec_diff = t.tv_sec - yg->start_time;
suseconds_t usec_diff = t.tv_usec - yg->start_subtime;
if (t.tv_usec < yg->start_subtime) {
sec_diff -= 1;
usec_diff = (1000000 + t.tv_usec) - yg->start_subtime;
}
return (uint64_t)(sec_diff * 1000 + usec_diff / 1000);
}
uint64_t yutani_time_since(yutani_globals_t * yg, uint64_t start_time) {
uint64_t now = yutani_current_time(yg);
uint64_t diff = now - start_time; /* Milliseconds */
return diff;
}
/**
* Translate and transform coordinate from screen-relative to window-relative.
*/
void yutani_device_to_window(yutani_server_window_t * window, int32_t x, int32_t y, int32_t * out_x, int32_t * out_y) {
if (!window) {
*out_x = 0;
*out_y = 0;
return;
}
*out_x = x - window->x;
*out_y = y - window->y;
if (!window->rotation) return;
double t_x = (double)*out_x - ((double)window->width / 2);
double t_y = (double)*out_y - ((double)window->height / 2);
double s = sin(-M_PI * (window->rotation/ 180.0));
double c = cos(-M_PI * (window->rotation/ 180.0));
double n_x = t_x * c - t_y * s;
double n_y = t_x * s + t_y * c;
*out_x = (int32_t)(n_x + ((double)window->width / 2));
*out_y = (int32_t)(n_y + ((double)window->height / 2));
}
/**
* Translate and transform coordinate from window-relative to screen-relative.
*/
void yutani_window_to_device(yutani_server_window_t * window, int32_t x, int32_t y, int32_t * out_x, int32_t * out_y) {
if (!window->rotation) {
*out_x = window->x + x;
*out_y = window->y + y;
return;
}
double t_x = (double)x - ((double)window->width / 2);
double t_y = (double)y - ((double)window->height / 2);
double s = sin((double)window->rotation * M_PI / 180.0);
double c = cos((double)window->rotation * M_PI / 180.0);
double n_x = t_x * c - t_y * s;
double n_y = t_x * s + t_y * c;
*out_x = (int32_t)(n_x + ((double)window->width / 2) + (double)window->x);
*out_y = (int32_t)(n_y + ((double)window->height / 2) + (double)window->y);
}
static list_t * window_zorder_owner(yutani_globals_t * yg, unsigned short index) {
switch (index) {
case YUTANI_ZORDER_BOTTOM:
case YUTANI_ZORDER_TOP:
return NULL;
case YUTANI_ZORDER_MENU:
return yg->menu_zs;
case YUTANI_ZORDER_OVERLAY:
return yg->overlay_zs;
default:
return yg->mid_zs;
}
}
/**
* Remove a window from the z stack.
*/
static void unorder_window(yutani_globals_t * yg, yutani_server_window_t * w) {
unsigned short index = w->z;
w->z = -1;
if (index == YUTANI_ZORDER_BOTTOM && yg->bottom_z == w) {
yg->bottom_z = NULL;
return;
}
if (index == YUTANI_ZORDER_TOP && yg->top_z == w) {
yg->top_z = NULL;
return;
}
list_t * zorder_owner = window_zorder_owner(yg, index);
if (!zorder_owner) return;
node_t * n = list_find(zorder_owner, w);
if (!n) return;
list_delete(zorder_owner, n);
free(n);
}
/**
* Move a window to a new stack order.
*/
static void reorder_window(yutani_globals_t * yg, yutani_server_window_t * window, uint16_t new_zed) {
if (!window) {
return;
}
unorder_window(yg, window);
window->z = new_zed;
list_t * zorder_owner = window_zorder_owner(yg, new_zed);
if (zorder_owner) {
list_insert(zorder_owner, window);
return;
}
if (new_zed == YUTANI_ZORDER_TOP) {
if (yg->top_z) {
unorder_window(yg, yg->top_z);
}
yg->top_z = window;
return;
}
if (new_zed == YUTANI_ZORDER_BOTTOM) {
if (yg->bottom_z) {
unorder_window(yg, yg->bottom_z);
}
yg->bottom_z = window;
return;
}
}
/**
* Move a window to the top of if its z stack.
*/
static void make_top(yutani_globals_t * yg, yutani_server_window_t * w) {
unsigned short index = w->z;
list_t * zorder_owner = window_zorder_owner(yg, index);
if (!zorder_owner) return;
node_t * n = list_find(zorder_owner, w);
if (!n) return; /* wat */
list_delete(zorder_owner, n);
list_append(zorder_owner, n);
}
/**
* Set a window as the focused window.
*
* Currently, we only support one focused window.
* In the future, we should support multiple windows as "focused" to account
* for multiple "seats" on a single display.
*/
static void set_focused_window(yutani_globals_t * yg, yutani_server_window_t * w) {
if (w == yg->focused_window) {
return; /* Already focused */
}
if (w && w->minimized) {
window_unminimize(yg,w);
}
if (yg->focused_window) {
/* Send focus change to old focused window */
yutani_msg_buildx_window_focus_change_alloc(response);
yutani_msg_buildx_window_focus_change(response, yg->focused_window->wid, 0);
pex_send(yg->server, yg->focused_window->owner, response->size, (char *)response);
}
yg->focused_window = w;
if (w) {
/* Send focus change to new focused window */
yutani_msg_buildx_window_focus_change_alloc(response);
yutani_msg_buildx_window_focus_change(response, w->wid, 1);
pex_send(yg->server, w->owner, response->size, (char *)response);
make_top(yg, w);
mark_window(yg, w);
} else {
/*
* There is no window to focus (we're unsetting focus);
* default to the bottom window (background)
*/
yg->focused_window = yg->bottom_z;
}
/* Notify all subscribers of window changes */
notify_subscribers(yg);
}
/**
* Get the focused window.
*
* In case there is no focused window, we return the bottom window.
*/
static yutani_server_window_t * get_focused(yutani_globals_t * yg) {
if (yg->focused_window) return yg->focused_window;
return yg->bottom_z;
}
static int yutani_pick_animation(uint32_t flags, int direction) {
if (flags & YUTANI_WINDOW_FLAG_DIALOG_ANIMATION) {
return (direction == 0) ? YUTANI_EFFECT_SQUEEZE_IN : YUTANI_EFFECT_SQUEEZE_OUT;
}
if (flags & YUTANI_WINDOW_FLAG_NO_ANIMATION) {
return (direction == 0) ? YUTANI_EFFECT_NONE : YUTANI_EFFECT_DISAPPEAR;
}
return (direction == 0) ? YUTANI_EFFECT_FADE_IN : YUTANI_EFFECT_FADE_OUT;
}
/**
* Create a server window object.
*
* Initializes a window of the particular size for a given client.
*/
static yutani_server_window_t * server_window_create(yutani_globals_t * yg, int width, int height, uintptr_t owner, uint32_t flags) {
yutani_server_window_t * win = malloc(sizeof(yutani_server_window_t));
win->wid = next_wid();
win->owner = owner;
list_insert(yg->windows, win);
hashmap_set(yg->wids_to_windows, (void*)(uintptr_t)win->wid, win);
list_t * client_list = hashmap_get(yg->clients_to_windows, (void *)owner);
list_insert(client_list, win);
win->x = 0;
win->y = 0;
win->z = 1;
win->width = width;
win->height = height;
win->bufid = next_buf_id();
win->rotation = 0;
win->newbufid = 0;
win->client_flags = 0;
win->client_icon = 0;
win->client_length = 0;
win->client_strings = NULL;
win->anim_mode = 0;
win->anim_start = 0;
win->alpha_threshold = 0;
win->show_mouse = 1;
win->tiled = 0;
win->untiled_width = 0;
win->untiled_height = 0;
win->default_mouse = 1;
win->server_flags = flags;
win->opacity = 255;
win->hidden = 1;
win->minimized = 0;
char key[1024];
YUTANI_SHMKEY(yg->server_ident, key, 1024, win);
size_t size = (width * height * 4);
win->buffer = shm_obtain(key, &size);
memset(win->buffer, 0, size);
list_insert(yg->mid_zs, win);
return win;
}
/**
* Update the shape threshold for a window.
*
* A shaping threshold is a byte representing the minimum
* required alpha for a window to be considered "solid".
* Eg., a value of 0 says all windows are solid, while a value of
* 1 requires a window to have at least some opacity to it,
* and a value of 255 requires fully opaque pixels.
*
* Not actually stored as a byte, so a value over 255 can be used.
* This results in a window that passes through all clicks.
*/
static void server_window_update_shape(yutani_globals_t * yg, yutani_server_window_t * window, int set) {
window->alpha_threshold = set;
}
/**
* Start resizing a window.
*
* Resizing a multi-stage process.
* The client and server agree on a size and the server prepares a buffer.
* The client then needs to accept the resize, fill the buffer, and then
* inform the server that it is ready, at which point we'll swap the
* buffer we are rendering from.
*/
static uint32_t server_window_resize(yutani_globals_t * yg, yutani_server_window_t * win, int width, int height) {
/* A client has accepted our offer, let's make a buffer for them */
if (win->newbufid) {
/* Already in the middle of an accept/done, bail */
return win->newbufid;
}
win->newbufid = next_buf_id();
{
char key[1024];
YUTANI_SHMKEY_EXP(yg->server_ident, key, 1024, win->newbufid);
size_t size = (width * height * 4);
win->newbuffer = shm_obtain(key, &size);
}
return win->newbufid;
}
/**
* Finish the resize process.
*
* We delete the unlink the old buffer and then swap the pointers
* for the new buffer.
*/
static void server_window_resize_finish(yutani_globals_t * yg, yutani_server_window_t * win, int width, int height) {
if (!win->newbufid) {
return;
}
int oldbufid = win->bufid;
mark_window(yg, win);
if (yg->resizing_window == win) {
yg->resize_release_time = 0;
if (yg->mouse_state == YUTANI_MOUSE_STATE_NORMAL) {
int32_t x, y;
if (yg->resizing_window->rotation) {
/* If the window is rotated, we need to move the center to be where the new center should be, but x/y are based on the unrotated upper left corner. */
/* The center always moves by one-half the resize dimensions */
int32_t center_x, center_y;
yutani_server_window_t fake_window = {
.width = yg->resizing_init_w,
.height = yg->resizing_init_h,
.x = yg->resizing_window->x,
.y = yg->resizing_window->y,
.rotation = yg->resizing_window->rotation,
};
yutani_window_to_device(&fake_window, yg->resizing_offset_x + yg->resizing_w / 2, yg->resizing_offset_y + yg->resizing_h / 2, ¢er_x, ¢er_y);
x = center_x - yg->resizing_w / 2;
y = center_y - yg->resizing_h / 2;
} else {
yutani_window_to_device(yg->resizing_window, yg->resizing_offset_x, yg->resizing_offset_y, &x, &y);
}
TRACE("resize complete, now %d x %d", yg->resizing_w, yg->resizing_h);
window_move(yg, yg->resizing_window, x,y);
yg->resizing_window = NULL;
yg->mouse_window = NULL;
}
}
win->width = width;
win->height = height;
win->bufid = win->newbufid;
win->buffer = win->newbuffer;
win->newbuffer = NULL;
win->newbufid = 0;
{
char key[1024];
YUTANI_SHMKEY_EXP(yg->server_ident, key, 1024, oldbufid);
shm_release(key);
}
mark_window(yg, win);
}
/**
* Mark a screen region as damaged.
*/
static void mark_screen(yutani_globals_t * yg, int32_t x, int32_t y, int32_t width, int32_t height) {
yutani_damage_rect_t * rect = malloc(sizeof(yutani_damage_rect_t));
rect->x = x;
rect->y = y;
rect->width = width;
rect->height = height;
list_insert(yg->update_list, rect);
}
/**
* Draw the cursor sprite.
*/
static void draw_cursor(yutani_globals_t * yg, int x, int y, int cursor) {
sprite_t * sprite = &yg->mouse_sprite;
static sprite_t * previous = NULL;
if (yg->resizing_window) {
switch (yg->resizing_direction) {
case SCALE_UP:
case SCALE_DOWN:
sprite = &yg->mouse_sprite_resize_v;
break;
case SCALE_LEFT:
case SCALE_RIGHT:
sprite = &yg->mouse_sprite_resize_h;
break;
case SCALE_DOWN_RIGHT:
case SCALE_UP_LEFT:
sprite = &yg->mouse_sprite_resize_da;
break;
case SCALE_DOWN_LEFT:
case SCALE_UP_RIGHT:
sprite = &yg->mouse_sprite_resize_db;
break;
default:
break;
}
} else if (yg->mouse_state == YUTANI_MOUSE_STATE_MOVING) {
sprite = &yg->mouse_sprite_drag;
} else {
switch (cursor) {
case YUTANI_CURSOR_TYPE_DRAG: sprite = &yg->mouse_sprite_drag; break;
case YUTANI_CURSOR_TYPE_RESIZE_VERTICAL: sprite = &yg->mouse_sprite_resize_v; break;
case YUTANI_CURSOR_TYPE_RESIZE_HORIZONTAL: sprite = &yg->mouse_sprite_resize_h; break;
case YUTANI_CURSOR_TYPE_RESIZE_UP_DOWN: sprite = &yg->mouse_sprite_resize_da; break;
case YUTANI_CURSOR_TYPE_RESIZE_DOWN_UP: sprite = &yg->mouse_sprite_resize_db; break;
case YUTANI_CURSOR_TYPE_POINT: sprite = &yg->mouse_sprite_point; break;
case YUTANI_CURSOR_TYPE_IBEAM: sprite = &yg->mouse_sprite_ibeam; break;
}
}
if (sprite != previous) {
mark_screen(yg, x / MOUSE_SCALE - MOUSE_OFFSET_X, y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
previous = sprite;
}
if (yg->vbox_pointer > 0) {
if (write(yg->vbox_pointer, sprite->bitmap, 48*48*4) > 0) {
/* if that was successful, we don't need to draw the cursor */
return;
}
}
yutani_server_window_t * cursor_window = yg->resizing_window ? yg->resizing_window :
top_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
int16_t rotation = cursor_window ? cursor_window->rotation : 0;
if (rotation) {
draw_sprite_rotate(yg->backend_ctx, sprite, x / MOUSE_SCALE - MOUSE_OFFSET_X, y / MOUSE_SCALE - MOUSE_OFFSET_Y, (double)rotation * M_PI / 180.0, 1.0);
} else {
draw_sprite(yg->backend_ctx, sprite, x / MOUSE_SCALE - MOUSE_OFFSET_X, y / MOUSE_SCALE - MOUSE_OFFSET_Y);
}
}
/**
* Determine if a window has a solid pixel at a given screen-space coordinate.
*
* This is where we evaluate alpha thresholds. We only do this underneath
* the cursor, and only when we move the cursor. It's reasonably fast
* in those circumstances, but shouldn't be used for large regions. We
* do have one debug method that indicates the top window in a box
* around the cursor, but it is relatively slow.
*/
static yutani_server_window_t * check_top_at(yutani_globals_t * yg, yutani_server_window_t * w, uint16_t x, uint16_t y){
if (!w || w->hidden || w->minimized) return NULL;
int32_t _x = -1, _y = -1;
yutani_device_to_window(w, x, y, &_x, &_y);
if (_x < 0 || _x >= w->width || _y < 0 || _y >= w->height) return NULL;
uint32_t c = ((uint32_t *)w->buffer)[(w->width * _y + _x)];
uint8_t a = _ALP(c);
if (a >= w->alpha_threshold) {
return w;
}
return NULL;
}
/**
* Find the window that is at the top at a particular screen-space coordinate.
*
* This walks through each window from top to bottom (foreachr - reverse foreach)
* until it finds one with a pixel at this coordinate. Again, we only call this
* at the cursor coordinates, and it is not particularly fast, so don't use it
* anywhere that needs to hit a lot of coordinates.
*/
static yutani_server_window_t * top_at(yutani_globals_t * yg, uint16_t x, uint16_t y) {
if (check_top_at(yg, yg->top_z, x, y)) return yg->top_z;
foreachr(node, yg->menu_zs) {
yutani_server_window_t * w = node->value;
if (check_top_at(yg, w, x, y)) return w;
}
foreachr(node, yg->overlay_zs) {
yutani_server_window_t * w = node->value;
if (check_top_at(yg, w, x, y)) return w;
}
foreachr(node, yg->mid_zs) {
yutani_server_window_t * w = node->value;
if (check_top_at(yg, w, x, y)) return w;
}
if (check_top_at(yg, yg->bottom_z, x, y)) return yg->bottom_z;
return NULL;
}
/**
* Get the window at a coordinate and focus it.
*
* See the docs for the proceeding functions, but added to this
* focusing windows is also not particular fast as the reshuffle
* is complicated.
*/
static void set_focused_at(yutani_globals_t * yg, int x, int y) {
yutani_server_window_t * n_focused = top_at(yg, x, y);
set_focused_window(yg, n_focused);
}
/*
* Convenience functions for checking if a window is in the top/bottom stack.
*
* In the future, these single-item "stacks" will be replaced with dedicated stacks
* so we can have multiple background windows and multiple panels / always-top windows.
*/
int yutani_window_is_top(yutani_globals_t * yg, yutani_server_window_t * window) {
/* For now, just use simple z-order */
return window->z == YUTANI_ZORDER_TOP;
}
int yutani_window_is_bottom(yutani_globals_t * yg, yutani_server_window_t * window) {
/* For now, just use simple z-order */
return window->z == YUTANI_ZORDER_BOTTOM;
}
/**
* Get a color for a wid for debugging.
*
* Makes a pretty rainbow pattern.
*/
uint32_t yutani_color_for_wid(yutani_wid_t wid) {
static uint32_t colors[] = {
0xFF19aeff,
0xFFff4141,
0xFFffff3e,
0xFFff6600,
0xFF9ade00,
0xFFd76cff,
0xFF364e59,
0xFF0084c8,
0xFFdc0000,
0xFFff9900,
0xFF009100,
0xFFba00ff,
0xFFb88100,
0xFF9eabb0
};
int i = wid % (sizeof(colors) / sizeof(uint32_t));
return colors[i];
}
/**
* Determine if a matrix has an identity transformation for its linear component.
*/
static inline int matrix_is_translation(gfx_matrix_t m) {
return (m[0][0] == 1.0 && m[0][1] == 0.0 && m[1][0] == 0.0 && m[1][1] == 1.0);
}
static void apply_rotation(yutani_globals_t * yg, yutani_server_window_t * window, gfx_matrix_t m, int16_t r) {
if (window == yg->resizing_window) {
if (r) {
gfx_matrix_translate(m, yg->resizing_init_w / 2, yg->resizing_init_h / 2);
gfx_matrix_rotate(m, (double)r * M_PI / 180.0);
gfx_matrix_translate(m, -yg->resizing_init_w / 2, -yg->resizing_init_h / 2);
}
double x_scale = (double)yg->resizing_w / (double)yg->resizing_window->width;
double y_scale = (double)yg->resizing_h / (double)yg->resizing_window->height;
if (x_scale < 0.00001) {
x_scale = 0.00001;
}
if (y_scale < 0.00001) {
y_scale = 0.00001;
}
gfx_matrix_translate(m, (int)yg->resizing_offset_x, (int)yg->resizing_offset_y);
gfx_matrix_scale(m, x_scale, y_scale);
} else if (r) {
gfx_matrix_translate(m, window->width / 2, window->height / 2);
gfx_matrix_rotate(m, (double)r * M_PI / 180.0);
gfx_matrix_translate(m, -window->width / 2, -window->height / 2);
}
}
/**
* Blit a window to the framebuffer.
*
* Applies transformations (rotation, animations) and then renders
* the window through alpha blitting.
*/
static int yutani_blit_window(yutani_globals_t * yg, yutani_server_window_t * window, int x, int y) {
if (window->hidden || window->minimized) {
return 0;
}
sprite_t _win_sprite;
_win_sprite.width = window->width;
_win_sprite.height = window->height;
_win_sprite.bitmap = (uint32_t *)window->buffer;
_win_sprite.masks = NULL;
_win_sprite.blank = 0;
_win_sprite.alpha = ALPHA_EMBEDDED;
double opacity = (double)(window->opacity) / 255.0;
if (window->rotation || window == yg->resizing_window || window->anim_mode || (window->server_flags & YUTANI_WINDOW_FLAG_BLUR_BEHIND)) {
double m[2][3];
gfx_matrix_identity(m);
gfx_matrix_translate(m,x,y);
if (window->anim_mode) {
int frame = yutani_time_since(yg, window->anim_start);
if (frame >= yutani_animation_lengths[window->anim_mode]) {
/* XXX handle animation-end things like cleanup of closing windows */
if (yutani_is_closing_animation[window->anim_mode]) {
list_insert(yg->windows_to_remove, window);
return 0;
}
if (yutani_is_minimizing_animation[window->anim_mode]) {
list_insert(yg->windows_to_minimize, window);
return 0;
}
window->anim_mode = 0;
window->anim_start = 0;
apply_rotation(yg, window, m, window->rotation);
} else {
switch (window->anim_mode) {
case YUTANI_EFFECT_SQUEEZE_OUT:
case YUTANI_EFFECT_FADE_OUT:
{
frame = yutani_animation_lengths[window->anim_mode] - frame;
} /* fallthrough */
case YUTANI_EFFECT_SQUEEZE_IN:
case YUTANI_EFFECT_FADE_IN:
{
double time_diff = ((double)frame / (float)yutani_animation_lengths[window->anim_mode]);
apply_rotation(yg, window, m, window->rotation);
if (window->server_flags & YUTANI_WINDOW_FLAG_DIALOG_ANIMATION) {
double x = time_diff;
int t_y = (window->height * (1.0 -x)) / 2;
gfx_matrix_translate(m, 0, t_y);
gfx_matrix_scale(m, 1.0, x);
} else {
double x = 0.75 + time_diff * 0.25;
opacity *= time_diff;
if (!(window->server_flags & YUTANI_WINDOW_FLAG_ALT_ANIMATION)) {
int t_x = (window->width * (1.0 - x)) / 2;
int t_y = (window->height * (1.0 - x)) / 2;
gfx_matrix_translate(m, t_x, t_y);
gfx_matrix_scale(m, x, x);
}
}
}
break;
case YUTANI_EFFECT_MINIMIZE:
{
frame = yutani_animation_lengths[window->anim_mode] - frame;
} /* fallthrough */
case YUTANI_EFFECT_UNMINIMIZE:
{
double time_diff = ((double)frame / (float)yutani_animation_lengths[window->anim_mode]);
opacity *= time_diff;
double t_x = -(window->x - window->icon_x) * (1.0 - time_diff);
double t_y = -(window->y - window->icon_y) * (1.0 - time_diff);
double s_x = 1.0 + (((float)window->icon_w / (float)(window->width ?: 1.0)) - 1.0) * (1.0 - time_diff);
double s_y = 1.0 + (((float)window->icon_h / (float)(window->height ?: 1.0)) - 1.0) * (1.0 - time_diff);
gfx_matrix_translate(m, t_x, t_y);
gfx_matrix_scale(m, s_x, s_y);
apply_rotation(yg, window, m, window->rotation * time_diff);
}
break;
default:
apply_rotation(yg, window, m, window->rotation);
break;
}
}
} else {
apply_rotation(yg, window, m, window->rotation);
}
#ifdef ENABLE_BLUR_BEHIND
if (window->server_flags & YUTANI_WINDOW_FLAG_BLUR_BEHIND) {
extern void draw_sprite_transform_blur(gfx_context_t * ctx, gfx_context_t * blur_ctx, const sprite_t * sprite, gfx_matrix_t matrix, float alpha, uint8_t threshold);
draw_sprite_transform_blur(yg->backend_ctx, blur_ctx, &_win_sprite, m, opacity, window->alpha_threshold);
} else
#endif
if (matrix_is_translation(m)) {
draw_sprite_alpha(yg->backend_ctx, &_win_sprite, m[0][2], m[1][2], opacity);
} else {
draw_sprite_transform(yg->backend_ctx, &_win_sprite, m, opacity);
}
} else if (window->opacity != 255) {
draw_sprite_alpha(yg->backend_ctx, &_win_sprite, window->x, window->y, opacity);
} else {
draw_sprite(yg->backend_ctx, &_win_sprite, window->x, window->y);
}
#if YUTANI_DEBUG_WINDOW_BOUNDS
if (yg->debug_bounds) {
int32_t t_x, t_y;
int32_t s_x, s_y;
int32_t r_x, r_y;
int32_t q_x, q_y;
yutani_window_to_device(window, 0, 0, &t_x, &t_y);
yutani_window_to_device(window, window->width, window->height, &s_x, &s_y);
yutani_window_to_device(window, 0, window->height, &r_x, &r_y);
yutani_window_to_device(window, window->width, 0, &q_x, &q_y);
uint32_t x = alpha_blend(rgba(0,0,0,0), yutani_color_for_wid(window->wid), rgb(178,0,0));
struct TT_Contour * contour = tt_contour_start(t_x,t_y);
contour = tt_contour_line_to(contour, r_x,r_y);
contour = tt_contour_line_to(contour, s_x,s_y);
contour = tt_contour_line_to(contour, q_x,q_y);
struct TT_Shape * shape = tt_contour_finish(contour);
free(contour);
tt_path_paint(yg->backend_ctx, shape, x);
free(shape);
}
#endif
return 0;
}
/**
* VirtualBox Seamless desktop driver.
*
* Sends rectangles describing all the non-background windows
* to the VirtualBox Guest Additions driver for use with the
* seamless desktop mode.
*/
static void yutani_post_vbox_rects(yutani_globals_t * yg) {
if (yg->vbox_rects <= 0) return;
char tmp[4096];
uint32_t * count = (uint32_t *)tmp;
*count = 0;
struct Rect {
int32_t x;
int32_t y;
int32_t xe;
int32_t ye;
} __attribute__((packed));
struct Rect * rects = (struct Rect *)(tmp+sizeof(int32_t));
#define DO_WINDOW(win) if (win && !win->hidden && !win->minimized && *count < 255 ) { \
rects->x = (win)->x; \
rects->y = (win)->y; \
rects->xe = (win)->x + (win)->width; \
rects->ye = (win)->y + (win)->height; \
rects++; \
(*count)++; \
}
/* Add top window if it exists */
DO_WINDOW(yg->top_z);
/* Add regular windows */
foreach (node, yg->mid_zs) {
yutani_server_window_t * w = node->value;
DO_WINDOW(w);
}
/* Add overlay windows */
foreach (node, yg->overlay_zs) {
yutani_server_window_t * w = node->value;
DO_WINDOW(w);
}
/* Add menu windows */
foreach (node, yg->menu_zs) {
yutani_server_window_t * w = node->value;
DO_WINDOW(w);
}
/*
* If there were no windows, show the whole desktop
* so we can see, eg., the login screen.
*/
if (*count == 0) {
*count = 1;
rects->x = 0;
rects->y = 0;
rects->xe = yg->width;
rects->ye = yg->height;
}
/* Post rectangle data to driver */
write(yg->vbox_rects, tmp, sizeof(tmp));
}
/**
* Blit all windows into the given context.
*
* This is called for rendering and for screenshots.
*/
static void yutani_blit_windows(yutani_globals_t * yg) {
if (!yg->bottom_z || yg->bottom_z->anim_mode) {
draw_fill(yg->backend_ctx, rgb(0,0,0));
}
if (yg->bottom_z) yutani_blit_window(yg, yg->bottom_z, yg->bottom_z->x, yg->bottom_z->y);
foreach (node, yg->mid_zs) {
yutani_server_window_t * w = node->value;
if (w) yutani_blit_window(yg, w, w->x, w->y);
}
foreach (node, yg->overlay_zs) {
yutani_server_window_t * w = node->value;
if (w) yutani_blit_window(yg, w, w->x, w->y);
}
foreach (node, yg->menu_zs) {
yutani_server_window_t * w = node->value;
if (w) yutani_blit_window(yg, w, w->x, w->y);
}
if (yg->top_z) yutani_blit_window(yg, yg->top_z, yg->top_z->x, yg->top_z->y);
}
/**
* Take a screenshot
*/
static void yutani_screenshot(yutani_globals_t * yg) {
int task = yg->screenshot_frame;
yg->screenshot_frame = 0;
/* raw screenshots */
char fname[1024];