source: mainline/uspace/srv/hid/compositor/compositor.c@ fafb8e5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fafb8e5 was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • Property mode set to 100644
File size: 62.0 KB
Line 
1/*
2 * Copyright (c) 2012 Petr Koupy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup compositor
30 * @{
31 */
32/** @file
33 */
34
35#include <assert.h>
36#include <stddef.h>
37#include <stdint.h>
38#include <stdbool.h>
39#include <errno.h>
40#include <str_error.h>
41#include <byteorder.h>
42#include <stdio.h>
43#include <libc.h>
44
45#include <align.h>
46#include <as.h>
47#include <stdlib.h>
48
49#include <refcount.h>
50#include <fibril_synch.h>
51#include <adt/prodcons.h>
52#include <adt/list.h>
53#include <io/input.h>
54#include <ipc/graph.h>
55#include <ipc/window.h>
56
57#include <async.h>
58#include <loc.h>
59#include <task.h>
60
61#include <io/keycode.h>
62#include <io/mode.h>
63#include <io/visualizer.h>
64#include <io/window.h>
65#include <io/console.h>
66
67#include <transform.h>
68#include <rectangle.h>
69#include <surface.h>
70#include <cursor.h>
71#include <source.h>
72#include <drawctx.h>
73#include <codec/tga.h>
74
75#include "compositor.h"
76
77#define NAME "compositor"
78#define NAMESPACE "comp"
79
80/*
81 * Until there is blitter support and some further optimizations, window
82 * animations are too slow to be practically usable.
83 */
84#ifndef ANIMATE_WINDOW_TRANSFORMS
85#define ANIMATE_WINDOW_TRANSFORMS 0
86#endif
87
88static char *server_name;
89static sysarg_t coord_origin;
90static pixel_t bg_color;
91static filter_t filter = filter_bilinear;
92static unsigned int filter_index = 1;
93
94typedef struct {
95 link_t link;
96 atomic_refcount_t ref_cnt;
97 window_flags_t flags;
98 service_id_t in_dsid;
99 service_id_t out_dsid;
100 prodcons_t queue;
101 transform_t transform;
102 double dx;
103 double dy;
104 double fx;
105 double fy;
106 double angle;
107 uint8_t opacity;
108 surface_t *surface;
109} window_t;
110
111static service_id_t winreg_id;
112static sysarg_t window_id = 0;
113static FIBRIL_MUTEX_INITIALIZE(window_list_mtx);
114static LIST_INITIALIZE(window_list);
115static double scale_back_x;
116static double scale_back_y;
117
118typedef struct {
119 link_t link;
120 sysarg_t id;
121 uint8_t state;
122 desktop_point_t pos;
123 sysarg_t btn_num;
124 desktop_point_t btn_pos;
125 desktop_vector_t accum;
126 sysarg_t grab_flags;
127 bool pressed;
128 cursor_t cursor;
129 window_t ghost;
130 desktop_vector_t accum_ghost;
131} pointer_t;
132
133static sysarg_t pointer_id = 0;
134static FIBRIL_MUTEX_INITIALIZE(pointer_list_mtx);
135static LIST_INITIALIZE(pointer_list);
136
137typedef struct {
138 link_t link;
139 service_id_t dsid;
140 vslmode_t mode;
141 async_sess_t *sess;
142 desktop_point_t pos;
143 surface_t *surface;
144} viewport_t;
145
146static desktop_rect_t viewport_bound_rect;
147static FIBRIL_MUTEX_INITIALIZE(viewport_list_mtx);
148static LIST_INITIALIZE(viewport_list);
149
150static FIBRIL_MUTEX_INITIALIZE(discovery_mtx);
151
152/** Input server proxy */
153static input_t *input;
154static bool active = false;
155
156static errno_t comp_active(input_t *);
157static errno_t comp_deactive(input_t *);
158static errno_t comp_key_press(input_t *, kbd_event_type_t, keycode_t, keymod_t, wchar_t);
159static errno_t comp_mouse_move(input_t *, int, int);
160static errno_t comp_abs_move(input_t *, unsigned, unsigned, unsigned, unsigned);
161static errno_t comp_mouse_button(input_t *, int, int);
162
163static input_ev_ops_t input_ev_ops = {
164 .active = comp_active,
165 .deactive = comp_deactive,
166 .key = comp_key_press,
167 .move = comp_mouse_move,
168 .abs_move = comp_abs_move,
169 .button = comp_mouse_button
170};
171
172static pointer_t *input_pointer(input_t *input)
173{
174 return input->user;
175}
176
177static pointer_t *pointer_create(void)
178{
179 pointer_t *p = (pointer_t *) malloc(sizeof(pointer_t));
180 if (!p)
181 return NULL;
182
183 link_initialize(&p->link);
184 p->pos.x = coord_origin;
185 p->pos.y = coord_origin;
186 p->btn_num = 1;
187 p->btn_pos = p->pos;
188 p->accum.x = 0;
189 p->accum.y = 0;
190 p->grab_flags = GF_EMPTY;
191 p->pressed = false;
192 p->state = 0;
193 cursor_init(&p->cursor, CURSOR_DECODER_EMBEDDED, NULL);
194
195 /* Ghost window for transformation animation. */
196 transform_identity(&p->ghost.transform);
197 transform_translate(&p->ghost.transform, coord_origin, coord_origin);
198 p->ghost.dx = coord_origin;
199 p->ghost.dy = coord_origin;
200 p->ghost.fx = 1;
201 p->ghost.fy = 1;
202 p->ghost.angle = 0;
203 p->ghost.opacity = 255;
204 p->ghost.surface = NULL;
205 p->accum_ghost.x = 0;
206 p->accum_ghost.y = 0;
207
208 return p;
209}
210
211static void pointer_destroy(pointer_t *p)
212{
213 if (p) {
214 cursor_release(&p->cursor);
215 free(p);
216 }
217}
218
219static window_t *window_create(void)
220{
221 window_t *win = (window_t *) malloc(sizeof(window_t));
222 if (!win)
223 return NULL;
224
225 link_initialize(&win->link);
226 /* One initial reference will be for being in the window list */
227 refcount_init(&win->ref_cnt);
228 prodcons_initialize(&win->queue);
229 transform_identity(&win->transform);
230 transform_translate(&win->transform, coord_origin, coord_origin);
231 win->dx = coord_origin;
232 win->dy = coord_origin;
233 win->fx = 1;
234 win->fy = 1;
235 win->angle = 0;
236 win->opacity = 255;
237 win->surface = NULL;
238
239 return win;
240}
241
242static void window_destroy(window_t *win)
243{
244 if (win == NULL)
245 return;
246
247 if (!refcount_down(&win->ref_cnt))
248 return;
249
250 while (!list_empty(&win->queue.list)) {
251 window_event_t *event = (window_event_t *) list_first(&win->queue.list);
252 list_remove(&event->link);
253 free(event);
254 }
255
256 if (win->surface)
257 surface_destroy(win->surface);
258
259 free(win);
260}
261
262static bool comp_coord_to_client(sysarg_t x_in, sysarg_t y_in, transform_t win_trans,
263 sysarg_t x_lim, sysarg_t y_lim, sysarg_t *x_out, sysarg_t *y_out)
264{
265 double x = x_in;
266 double y = y_in;
267 transform_invert(&win_trans);
268 transform_apply_affine(&win_trans, &x, &y);
269
270 /*
271 * Since client coordinate origin is (0, 0), it is necessary to check
272 * coordinates to avoid underflow. Moreover, it is convenient to also
273 * check against provided upper limits to determine whether the converted
274 * coordinates are within the client window.
275 */
276 if ((x < 0) || (y < 0))
277 return false;
278
279 (*x_out) = (sysarg_t) (x + 0.5);
280 (*y_out) = (sysarg_t) (y + 0.5);
281
282 if (((*x_out) >= x_lim) || ((*y_out) >= y_lim))
283 return false;
284
285 return true;
286}
287
288static void comp_coord_from_client(double x_in, double y_in, transform_t win_trans,
289 sysarg_t *x_out, sysarg_t *y_out)
290{
291 double x = x_in;
292 double y = y_in;
293 transform_apply_affine(&win_trans, &x, &y);
294
295 /*
296 * It is assumed that compositor coordinate origin is chosen in such way,
297 * that underflow/overflow here would be unlikely.
298 */
299 (*x_out) = (sysarg_t) (x + 0.5);
300 (*y_out) = (sysarg_t) (y + 0.5);
301}
302
303static void comp_coord_bounding_rect(double x_in, double y_in,
304 double w_in, double h_in, transform_t win_trans,
305 sysarg_t *x_out, sysarg_t *y_out, sysarg_t *w_out, sysarg_t *h_out)
306{
307 if ((w_in > 0) && (h_in > 0)) {
308 sysarg_t x[4];
309 sysarg_t y[4];
310
311 comp_coord_from_client(x_in, y_in, win_trans, &x[0], &y[0]);
312 comp_coord_from_client(x_in + w_in - 1, y_in, win_trans, &x[1], &y[1]);
313 comp_coord_from_client(x_in + w_in - 1, y_in + h_in - 1, win_trans, &x[2], &y[2]);
314 comp_coord_from_client(x_in, y_in + h_in - 1, win_trans, &x[3], &y[3]);
315
316 (*x_out) = x[0];
317 (*y_out) = y[0];
318 (*w_out) = x[0];
319 (*h_out) = y[0];
320
321 for (unsigned int i = 1; i < 4; ++i) {
322 (*x_out) = (x[i] < (*x_out)) ? x[i] : (*x_out);
323 (*y_out) = (y[i] < (*y_out)) ? y[i] : (*y_out);
324 (*w_out) = (x[i] > (*w_out)) ? x[i] : (*w_out);
325 (*h_out) = (y[i] > (*h_out)) ? y[i] : (*h_out);
326 }
327
328 (*w_out) = (*w_out) - (*x_out) + 1;
329 (*h_out) = (*h_out) - (*y_out) + 1;
330 } else {
331 (*x_out) = 0;
332 (*y_out) = 0;
333 (*w_out) = 0;
334 (*h_out) = 0;
335 }
336}
337
338static void comp_update_viewport_bound_rect(void)
339{
340 fibril_mutex_lock(&viewport_list_mtx);
341
342 sysarg_t x_res = coord_origin;
343 sysarg_t y_res = coord_origin;
344 sysarg_t w_res = 0;
345 sysarg_t h_res = 0;
346
347 if (!list_empty(&viewport_list)) {
348 viewport_t *vp = (viewport_t *) list_first(&viewport_list);
349 x_res = vp->pos.x;
350 y_res = vp->pos.y;
351 surface_get_resolution(vp->surface, &w_res, &h_res);
352 }
353
354 list_foreach(viewport_list, link, viewport_t, vp) {
355 sysarg_t w_vp, h_vp;
356 surface_get_resolution(vp->surface, &w_vp, &h_vp);
357 rectangle_union(x_res, y_res, w_res, h_res,
358 vp->pos.x, vp->pos.y, w_vp, h_vp,
359 &x_res, &y_res, &w_res, &h_res);
360 }
361
362 viewport_bound_rect.x = x_res;
363 viewport_bound_rect.y = y_res;
364 viewport_bound_rect.w = w_res;
365 viewport_bound_rect.h = h_res;
366
367 fibril_mutex_unlock(&viewport_list_mtx);
368}
369
370static void comp_restrict_pointers(void)
371{
372 comp_update_viewport_bound_rect();
373
374 fibril_mutex_lock(&pointer_list_mtx);
375
376 list_foreach(pointer_list, link, pointer_t, ptr) {
377 ptr->pos.x = ptr->pos.x > viewport_bound_rect.x ? ptr->pos.x : viewport_bound_rect.x;
378 ptr->pos.y = ptr->pos.y > viewport_bound_rect.y ? ptr->pos.y : viewport_bound_rect.y;
379 ptr->pos.x = ptr->pos.x < viewport_bound_rect.x + viewport_bound_rect.w ?
380 ptr->pos.x : viewport_bound_rect.x + viewport_bound_rect.w;
381 ptr->pos.y = ptr->pos.y < viewport_bound_rect.y + viewport_bound_rect.h ?
382 ptr->pos.y : viewport_bound_rect.y + viewport_bound_rect.h;
383 }
384
385 fibril_mutex_unlock(&pointer_list_mtx);
386}
387
388static void comp_damage(sysarg_t x_dmg_glob, sysarg_t y_dmg_glob,
389 sysarg_t w_dmg_glob, sysarg_t h_dmg_glob)
390{
391 fibril_mutex_lock(&viewport_list_mtx);
392 fibril_mutex_lock(&window_list_mtx);
393 fibril_mutex_lock(&pointer_list_mtx);
394
395 list_foreach(viewport_list, link, viewport_t, vp) {
396 /* Determine what part of the viewport must be updated. */
397 sysarg_t x_dmg_vp, y_dmg_vp, w_dmg_vp, h_dmg_vp;
398 surface_get_resolution(vp->surface, &w_dmg_vp, &h_dmg_vp);
399 bool isec_vp = rectangle_intersect(
400 x_dmg_glob, y_dmg_glob, w_dmg_glob, h_dmg_glob,
401 vp->pos.x, vp->pos.y, w_dmg_vp, h_dmg_vp,
402 &x_dmg_vp, &y_dmg_vp, &w_dmg_vp, &h_dmg_vp);
403
404 if (isec_vp) {
405
406 /* Paint background color. */
407 for (sysarg_t y = y_dmg_vp - vp->pos.y; y < y_dmg_vp - vp->pos.y + h_dmg_vp; ++y) {
408 pixel_t *dst = pixelmap_pixel_at(
409 surface_pixmap_access(vp->surface), x_dmg_vp - vp->pos.x, y);
410 sysarg_t count = w_dmg_vp;
411 while (count-- != 0) {
412 *dst++ = bg_color;
413 }
414 }
415 surface_add_damaged_region(vp->surface,
416 x_dmg_vp - vp->pos.x, y_dmg_vp - vp->pos.y, w_dmg_vp, h_dmg_vp);
417
418 transform_t transform;
419 source_t source;
420 drawctx_t context;
421
422 source_init(&source);
423 source_set_filter(&source, filter);
424 drawctx_init(&context, vp->surface);
425 drawctx_set_compose(&context, compose_over);
426 drawctx_set_source(&context, &source);
427
428 /* For each window. */
429 for (link_t *link = window_list.head.prev;
430 link != &window_list.head; link = link->prev) {
431
432 /*
433 * Determine what part of the window intersects with the
434 * updated area of the current viewport.
435 */
436 window_t *win = list_get_instance(link, window_t, link);
437 if (!win->surface) {
438 continue;
439 }
440 sysarg_t x_dmg_win, y_dmg_win, w_dmg_win, h_dmg_win;
441 surface_get_resolution(win->surface, &w_dmg_win, &h_dmg_win);
442 comp_coord_bounding_rect(0, 0, w_dmg_win, h_dmg_win, win->transform,
443 &x_dmg_win, &y_dmg_win, &w_dmg_win, &h_dmg_win);
444 bool isec_win = rectangle_intersect(
445 x_dmg_vp, y_dmg_vp, w_dmg_vp, h_dmg_vp,
446 x_dmg_win, y_dmg_win, w_dmg_win, h_dmg_win,
447 &x_dmg_win, &y_dmg_win, &w_dmg_win, &h_dmg_win);
448
449 if (isec_win) {
450 /*
451 * Prepare conversion from global coordinates to viewport
452 * coordinates.
453 */
454 transform = win->transform;
455 double_point_t pos;
456 pos.x = vp->pos.x;
457 pos.y = vp->pos.y;
458 transform_translate(&transform, -pos.x, -pos.y);
459
460 source_set_transform(&source, transform);
461 source_set_texture(&source, win->surface,
462 PIXELMAP_EXTEND_TRANSPARENT_SIDES);
463 source_set_alpha(&source, PIXEL(win->opacity, 0, 0, 0));
464
465 drawctx_transfer(&context,
466 x_dmg_win - vp->pos.x, y_dmg_win - vp->pos.y, w_dmg_win, h_dmg_win);
467 }
468 }
469
470 list_foreach(pointer_list, link, pointer_t, ptr) {
471 if (ptr->ghost.surface) {
472
473 sysarg_t x_bnd_ghost, y_bnd_ghost, w_bnd_ghost, h_bnd_ghost;
474 sysarg_t x_dmg_ghost, y_dmg_ghost, w_dmg_ghost, h_dmg_ghost;
475 surface_get_resolution(ptr->ghost.surface, &w_bnd_ghost, &h_bnd_ghost);
476 comp_coord_bounding_rect(0, 0, w_bnd_ghost, h_bnd_ghost, ptr->ghost.transform,
477 &x_bnd_ghost, &y_bnd_ghost, &w_bnd_ghost, &h_bnd_ghost);
478 bool isec_ghost = rectangle_intersect(
479 x_dmg_vp, y_dmg_vp, w_dmg_vp, h_dmg_vp,
480 x_bnd_ghost, y_bnd_ghost, w_bnd_ghost, h_bnd_ghost,
481 &x_dmg_ghost, &y_dmg_ghost, &w_dmg_ghost, &h_dmg_ghost);
482
483 if (isec_ghost) {
484 /*
485 * FIXME: Ghost is currently drawn based on the bounding
486 * rectangle of the window, which is sufficient as long
487 * as the windows can be rotated only by 90 degrees.
488 * For ghost to be compatible with arbitrary-angle
489 * rotation, it should be drawn as four lines adjusted
490 * by the transformation matrix. That would however
491 * require to equip libdraw with line drawing functionality.
492 */
493
494 transform_t transform = ptr->ghost.transform;
495 double_point_t pos;
496 pos.x = vp->pos.x;
497 pos.y = vp->pos.y;
498 transform_translate(&transform, -pos.x, -pos.y);
499
500 pixel_t ghost_color;
501
502 if (y_bnd_ghost == y_dmg_ghost) {
503 for (sysarg_t x = x_dmg_ghost - vp->pos.x;
504 x < x_dmg_ghost - vp->pos.x + w_dmg_ghost; ++x) {
505 ghost_color = surface_get_pixel(vp->surface,
506 x, y_dmg_ghost - vp->pos.y);
507 surface_put_pixel(vp->surface,
508 x, y_dmg_ghost - vp->pos.y, INVERT(ghost_color));
509 }
510 }
511
512 if (y_bnd_ghost + h_bnd_ghost == y_dmg_ghost + h_dmg_ghost) {
513 for (sysarg_t x = x_dmg_ghost - vp->pos.x;
514 x < x_dmg_ghost - vp->pos.x + w_dmg_ghost; ++x) {
515 ghost_color = surface_get_pixel(vp->surface,
516 x, y_dmg_ghost - vp->pos.y + h_dmg_ghost - 1);
517 surface_put_pixel(vp->surface,
518 x, y_dmg_ghost - vp->pos.y + h_dmg_ghost - 1, INVERT(ghost_color));
519 }
520 }
521
522 if (x_bnd_ghost == x_dmg_ghost) {
523 for (sysarg_t y = y_dmg_ghost - vp->pos.y;
524 y < y_dmg_ghost - vp->pos.y + h_dmg_ghost; ++y) {
525 ghost_color = surface_get_pixel(vp->surface,
526 x_dmg_ghost - vp->pos.x, y);
527 surface_put_pixel(vp->surface,
528 x_dmg_ghost - vp->pos.x, y, INVERT(ghost_color));
529 }
530 }
531
532 if (x_bnd_ghost + w_bnd_ghost == x_dmg_ghost + w_dmg_ghost) {
533 for (sysarg_t y = y_dmg_ghost - vp->pos.y;
534 y < y_dmg_ghost - vp->pos.y + h_dmg_ghost; ++y) {
535 ghost_color = surface_get_pixel(vp->surface,
536 x_dmg_ghost - vp->pos.x + w_dmg_ghost - 1, y);
537 surface_put_pixel(vp->surface,
538 x_dmg_ghost - vp->pos.x + w_dmg_ghost - 1, y, INVERT(ghost_color));
539 }
540 }
541 }
542
543 }
544 }
545
546 list_foreach(pointer_list, link, pointer_t, ptr) {
547
548 /*
549 * Determine what part of the pointer intersects with the
550 * updated area of the current viewport.
551 */
552 sysarg_t x_dmg_ptr, y_dmg_ptr, w_dmg_ptr, h_dmg_ptr;
553 surface_t *sf_ptr = ptr->cursor.states[ptr->state];
554 surface_get_resolution(sf_ptr, &w_dmg_ptr, &h_dmg_ptr);
555 bool isec_ptr = rectangle_intersect(
556 x_dmg_vp, y_dmg_vp, w_dmg_vp, h_dmg_vp,
557 ptr->pos.x, ptr->pos.y, w_dmg_ptr, h_dmg_ptr,
558 &x_dmg_ptr, &y_dmg_ptr, &w_dmg_ptr, &h_dmg_ptr);
559
560 if (isec_ptr) {
561 /*
562 * Pointer is currently painted directly by copying pixels.
563 * However, it is possible to draw the pointer similarly
564 * as window by using drawctx_transfer. It would allow
565 * more sophisticated control over drawing, but would also
566 * cost more regarding the performance.
567 */
568
569 sysarg_t x_vp = x_dmg_ptr - vp->pos.x;
570 sysarg_t y_vp = y_dmg_ptr - vp->pos.y;
571 sysarg_t x_ptr = x_dmg_ptr - ptr->pos.x;
572 sysarg_t y_ptr = y_dmg_ptr - ptr->pos.y;
573
574 for (sysarg_t y = 0; y < h_dmg_ptr; ++y) {
575 pixel_t *src = pixelmap_pixel_at(
576 surface_pixmap_access(sf_ptr), x_ptr, y_ptr + y);
577 pixel_t *dst = pixelmap_pixel_at(
578 surface_pixmap_access(vp->surface), x_vp, y_vp + y);
579 sysarg_t count = w_dmg_ptr;
580 while (count-- != 0) {
581 *dst = (*src & 0xff000000) ? *src : *dst;
582 ++dst;
583 ++src;
584 }
585 }
586 surface_add_damaged_region(vp->surface, x_vp, y_vp, w_dmg_ptr, h_dmg_ptr);
587 }
588
589 }
590 }
591 }
592
593 fibril_mutex_unlock(&pointer_list_mtx);
594 fibril_mutex_unlock(&window_list_mtx);
595
596 /* Notify visualizers about updated regions. */
597 if (active) {
598 list_foreach(viewport_list, link, viewport_t, vp) {
599 sysarg_t x_dmg_vp, y_dmg_vp, w_dmg_vp, h_dmg_vp;
600 surface_get_damaged_region(vp->surface, &x_dmg_vp, &y_dmg_vp, &w_dmg_vp, &h_dmg_vp);
601 surface_reset_damaged_region(vp->surface);
602 visualizer_update_damaged_region(vp->sess,
603 x_dmg_vp, y_dmg_vp, w_dmg_vp, h_dmg_vp, 0, 0);
604 }
605 }
606
607 fibril_mutex_unlock(&viewport_list_mtx);
608}
609
610static void comp_window_get_event(window_t *win, ipc_call_t *icall)
611{
612 window_event_t *event = (window_event_t *) prodcons_consume(&win->queue);
613
614 ipc_call_t call;
615 size_t len;
616
617 if (!async_data_read_receive(&call, &len)) {
618 async_answer_0(icall, EINVAL);
619 free(event);
620 return;
621 }
622
623 errno_t rc = async_data_read_finalize(&call, event, len);
624 if (rc != EOK) {
625 async_answer_0(icall, ENOMEM);
626 free(event);
627 return;
628 }
629
630 async_answer_0(icall, EOK);
631 free(event);
632}
633
634static void comp_window_damage(window_t *win, ipc_call_t *icall)
635{
636 double x = ipc_get_arg1(icall);
637 double y = ipc_get_arg2(icall);
638 double width = ipc_get_arg3(icall);
639 double height = ipc_get_arg4(icall);
640
641 if ((width == 0) || (height == 0)) {
642 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
643 } else {
644 fibril_mutex_lock(&window_list_mtx);
645 sysarg_t x_dmg_glob, y_dmg_glob, w_dmg_glob, h_dmg_glob;
646 comp_coord_bounding_rect(x - 1, y - 1, width + 2, height + 2,
647 win->transform, &x_dmg_glob, &y_dmg_glob, &w_dmg_glob, &h_dmg_glob);
648 fibril_mutex_unlock(&window_list_mtx);
649 comp_damage(x_dmg_glob, y_dmg_glob, w_dmg_glob, h_dmg_glob);
650 }
651
652 async_answer_0(icall, EOK);
653}
654
655static void comp_window_grab(window_t *win, ipc_call_t *icall)
656{
657 sysarg_t pos_id = ipc_get_arg1(icall);
658 sysarg_t grab_flags = ipc_get_arg2(icall);
659
660 /*
661 * Filter out resize grab flags if the window
662 * is not resizeable.
663 */
664 if ((win->flags & WINDOW_RESIZEABLE) != WINDOW_RESIZEABLE)
665 grab_flags &= ~(GF_RESIZE_X | GF_RESIZE_Y);
666
667 fibril_mutex_lock(&pointer_list_mtx);
668 list_foreach(pointer_list, link, pointer_t, pointer) {
669 if (pointer->id == pos_id) {
670 pointer->grab_flags = pointer->pressed ? grab_flags : GF_EMPTY;
671 // TODO change pointer->state according to grab_flags
672 break;
673 }
674 }
675 fibril_mutex_unlock(&pointer_list_mtx);
676
677 if ((grab_flags & GF_RESIZE_X) || (grab_flags & GF_RESIZE_Y)) {
678 scale_back_x = 1;
679 scale_back_y = 1;
680 }
681
682 async_answer_0(icall, EOK);
683}
684
685static void comp_recalc_transform(window_t *win)
686{
687 transform_t translate;
688 transform_identity(&translate);
689 transform_translate(&translate, win->dx, win->dy);
690
691 transform_t scale;
692 transform_identity(&scale);
693 if ((win->fx != 1) || (win->fy != 1))
694 transform_scale(&scale, win->fx, win->fy);
695
696 transform_t rotate;
697 transform_identity(&rotate);
698 if (win->angle != 0)
699 transform_rotate(&rotate, win->angle);
700
701 transform_t transform;
702 transform_t temp;
703 transform_identity(&transform);
704 temp = transform;
705 transform_product(&transform, &temp, &translate);
706 temp = transform;
707 transform_product(&transform, &temp, &rotate);
708 temp = transform;
709 transform_product(&transform, &temp, &scale);
710
711 win->transform = transform;
712}
713
714static void comp_window_resize(window_t *win, ipc_call_t *icall)
715{
716 ipc_call_t call;
717 size_t size;
718 unsigned int flags;
719
720 /* Start sharing resized window with client. */
721 if (!async_share_out_receive(&call, &size, &flags)) {
722 async_answer_0(icall, EINVAL);
723 return;
724 }
725
726 void *new_cell_storage;
727 errno_t rc = async_share_out_finalize(&call, &new_cell_storage);
728 if ((rc != EOK) || (new_cell_storage == AS_MAP_FAILED)) {
729 async_answer_0(icall, ENOMEM);
730 return;
731 }
732
733 /* Create new surface for the resized window. */
734 surface_t *new_surface = surface_create(ipc_get_arg3(icall),
735 ipc_get_arg4(icall), new_cell_storage, SURFACE_FLAG_SHARED);
736 if (!new_surface) {
737 as_area_destroy(new_cell_storage);
738 async_answer_0(icall, ENOMEM);
739 return;
740 }
741
742 sysarg_t offset_x = ipc_get_arg1(icall);
743 sysarg_t offset_y = ipc_get_arg2(icall);
744 window_placement_flags_t placement_flags =
745 (window_placement_flags_t) ipc_get_arg5(icall);
746
747 comp_update_viewport_bound_rect();
748
749 /* Switch new surface with old surface and calculate damage. */
750 fibril_mutex_lock(&window_list_mtx);
751
752 sysarg_t old_width = 0;
753 sysarg_t old_height = 0;
754
755 if (win->surface) {
756 surface_get_resolution(win->surface, &old_width, &old_height);
757 surface_destroy(win->surface);
758 }
759
760 win->surface = new_surface;
761
762 sysarg_t new_width = 0;
763 sysarg_t new_height = 0;
764 surface_get_resolution(win->surface, &new_width, &new_height);
765
766 if (placement_flags & WINDOW_PLACEMENT_CENTER_X)
767 win->dx = viewport_bound_rect.x + viewport_bound_rect.w / 2 -
768 new_width / 2;
769
770 if (placement_flags & WINDOW_PLACEMENT_CENTER_Y)
771 win->dy = viewport_bound_rect.y + viewport_bound_rect.h / 2 -
772 new_height / 2;
773
774 if (placement_flags & WINDOW_PLACEMENT_LEFT)
775 win->dx = viewport_bound_rect.x;
776
777 if (placement_flags & WINDOW_PLACEMENT_TOP)
778 win->dy = viewport_bound_rect.y;
779
780 if (placement_flags & WINDOW_PLACEMENT_RIGHT)
781 win->dx = viewport_bound_rect.x + viewport_bound_rect.w -
782 new_width;
783
784 if (placement_flags & WINDOW_PLACEMENT_BOTTOM)
785 win->dy = viewport_bound_rect.y + viewport_bound_rect.h -
786 new_height;
787
788 if (placement_flags & WINDOW_PLACEMENT_ABSOLUTE_X)
789 win->dx = coord_origin + offset_x;
790
791 if (placement_flags & WINDOW_PLACEMENT_ABSOLUTE_Y)
792 win->dy = coord_origin + offset_y;
793
794 /* Transform the window and calculate damage. */
795 sysarg_t x1;
796 sysarg_t y1;
797 sysarg_t width1;
798 sysarg_t height1;
799
800 comp_coord_bounding_rect(0, 0, old_width, old_height, win->transform,
801 &x1, &y1, &width1, &height1);
802
803 comp_recalc_transform(win);
804
805 sysarg_t x2;
806 sysarg_t y2;
807 sysarg_t width2;
808 sysarg_t height2;
809
810 comp_coord_bounding_rect(0, 0, new_width, new_height, win->transform,
811 &x2, &y2, &width2, &height2);
812
813 sysarg_t x;
814 sysarg_t y;
815 sysarg_t width;
816 sysarg_t height;
817
818 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
819 &x, &y, &width, &height);
820
821 fibril_mutex_unlock(&window_list_mtx);
822
823 comp_damage(x, y, width, height);
824
825 async_answer_0(icall, EOK);
826}
827
828static void comp_post_event_win(window_event_t *event, window_t *target)
829{
830 fibril_mutex_lock(&window_list_mtx);
831
832 list_foreach(window_list, link, window_t, window) {
833 if (window == target) {
834 prodcons_produce(&window->queue, &event->link);
835 fibril_mutex_unlock(&window_list_mtx);
836 return;
837 }
838 }
839
840 fibril_mutex_unlock(&window_list_mtx);
841 free(event);
842}
843
844static void comp_post_event_top(window_event_t *event)
845{
846 fibril_mutex_lock(&window_list_mtx);
847
848 window_t *win = (window_t *) list_first(&window_list);
849 if (win)
850 prodcons_produce(&win->queue, &event->link);
851 else
852 free(event);
853
854 fibril_mutex_unlock(&window_list_mtx);
855}
856
857static void comp_window_close(window_t *win, ipc_call_t *icall)
858{
859 /* Stop managing the window. */
860 fibril_mutex_lock(&window_list_mtx);
861 list_remove(&win->link);
862 window_t *win_focus = (window_t *) list_first(&window_list);
863 window_event_t *event_focus = (window_event_t *) malloc(sizeof(window_event_t));
864 if (event_focus) {
865 link_initialize(&event_focus->link);
866 event_focus->type = ET_WINDOW_FOCUS;
867 }
868 fibril_mutex_unlock(&window_list_mtx);
869
870 if (event_focus && win_focus) {
871 comp_post_event_win(event_focus, win_focus);
872 }
873
874 loc_service_unregister(win->in_dsid);
875 loc_service_unregister(win->out_dsid);
876
877 /*
878 * In case the client was killed, input fibril of the window might be
879 * still blocked on the condition within comp_window_get_event.
880 */
881 window_event_t *event_dummy = (window_event_t *) malloc(sizeof(window_event_t));
882 if (event_dummy) {
883 link_initialize(&event_dummy->link);
884 prodcons_produce(&win->queue, &event_dummy->link);
885 }
886
887 /* Calculate damage. */
888 sysarg_t x = 0;
889 sysarg_t y = 0;
890 sysarg_t width = 0;
891 sysarg_t height = 0;
892 if (win->surface) {
893 surface_get_resolution(win->surface, &width, &height);
894 comp_coord_bounding_rect(
895 0, 0, width, height, win->transform, &x, &y, &width, &height);
896 }
897
898 /* Down refcount for removing from the window list */
899 window_destroy(win);
900
901 comp_damage(x, y, width, height);
902 async_answer_0(icall, EOK);
903}
904
905static void comp_window_close_request(window_t *win, ipc_call_t *icall)
906{
907 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
908 if (event == NULL) {
909 async_answer_0(icall, ENOMEM);
910 return;
911 }
912
913 link_initialize(&event->link);
914 event->type = ET_WINDOW_CLOSE;
915
916 prodcons_produce(&win->queue, &event->link);
917 async_answer_0(icall, EOK);
918}
919
920static void client_connection(ipc_call_t *icall, void *arg)
921{
922 ipc_call_t call;
923 service_id_t service_id = (service_id_t) ipc_get_arg2(icall);
924
925 /* Allocate resources for new window and register it to the location service. */
926 if (service_id == winreg_id) {
927 async_accept_0(icall);
928
929 async_get_call(&call);
930 if (ipc_get_imethod(&call) == WINDOW_REGISTER) {
931 fibril_mutex_lock(&window_list_mtx);
932
933 window_t *win = window_create();
934 if (!win) {
935 async_answer_0(&call, EHANGUP);
936 fibril_mutex_unlock(&window_list_mtx);
937 return;
938 }
939
940 win->flags = ipc_get_arg1(&call);
941
942 char name_in[LOC_NAME_MAXLEN + 1];
943 snprintf(name_in, LOC_NAME_MAXLEN, "%s%s/win%zuin", NAMESPACE,
944 server_name, window_id);
945
946 char name_out[LOC_NAME_MAXLEN + 1];
947 snprintf(name_out, LOC_NAME_MAXLEN, "%s%s/win%zuout", NAMESPACE,
948 server_name, window_id);
949
950 ++window_id;
951
952 if (loc_service_register(name_in, &win->in_dsid) != EOK) {
953 window_destroy(win);
954 async_answer_0(&call, EHANGUP);
955 fibril_mutex_unlock(&window_list_mtx);
956 return;
957 }
958
959 if (loc_service_register(name_out, &win->out_dsid) != EOK) {
960 loc_service_unregister(win->in_dsid);
961 window_destroy(win);
962 async_answer_0(&call, EHANGUP);
963 fibril_mutex_unlock(&window_list_mtx);
964 return;
965 }
966
967 window_t *win_unfocus = (window_t *) list_first(&window_list);
968 list_prepend(&win->link, &window_list);
969
970 window_event_t *event_unfocus = (window_event_t *) malloc(sizeof(window_event_t));
971 if (event_unfocus) {
972 link_initialize(&event_unfocus->link);
973 event_unfocus->type = ET_WINDOW_UNFOCUS;
974 }
975
976 async_answer_2(&call, EOK, win->in_dsid, win->out_dsid);
977 fibril_mutex_unlock(&window_list_mtx);
978
979 if (event_unfocus && win_unfocus) {
980 comp_post_event_win(event_unfocus, win_unfocus);
981 }
982
983 async_get_call(&call);
984 }
985 async_answer_0(&call, EHANGUP);
986 return;
987 }
988
989 /* Match the client with pre-allocated window. */
990 window_t *win = NULL;
991 fibril_mutex_lock(&window_list_mtx);
992 list_foreach(window_list, link, window_t, cur) {
993 if (cur->in_dsid == service_id || cur->out_dsid == service_id) {
994 win = cur;
995 break;
996 }
997 }
998
999 if (win)
1000 refcount_up(&win->ref_cnt);
1001
1002 fibril_mutex_unlock(&window_list_mtx);
1003
1004 if (win) {
1005 async_accept_0(icall);
1006 } else {
1007 async_answer_0(icall, EINVAL);
1008 return;
1009 }
1010
1011 /* Each client establishes two separate connections. */
1012 if (win->in_dsid == service_id) {
1013 while (true) {
1014 async_get_call(&call);
1015
1016 if (!ipc_get_imethod(&call)) {
1017 async_answer_0(&call, EOK);
1018 window_destroy(win);
1019 return;
1020 }
1021
1022 switch (ipc_get_imethod(&call)) {
1023 case WINDOW_GET_EVENT:
1024 comp_window_get_event(win, &call);
1025 break;
1026 default:
1027 async_answer_0(&call, EINVAL);
1028 }
1029 }
1030 } else if (win->out_dsid == service_id) {
1031 while (true) {
1032 async_get_call(&call);
1033
1034 if (!ipc_get_imethod(&call)) {
1035 comp_window_close(win, &call);
1036 window_destroy(win);
1037 return;
1038 }
1039
1040 switch (ipc_get_imethod(&call)) {
1041 case WINDOW_DAMAGE:
1042 comp_window_damage(win, &call);
1043 break;
1044 case WINDOW_GRAB:
1045 comp_window_grab(win, &call);
1046 break;
1047 case WINDOW_RESIZE:
1048 comp_window_resize(win, &call);
1049 break;
1050 case WINDOW_CLOSE:
1051 /*
1052 * Postpone the closing until the phone is hung up to cover
1053 * the case when the client is killed abruptly.
1054 */
1055 async_answer_0(&call, EOK);
1056 break;
1057 case WINDOW_CLOSE_REQUEST:
1058 comp_window_close_request(win, &call);
1059 break;
1060 default:
1061 async_answer_0(&call, EINVAL);
1062 }
1063 }
1064 }
1065}
1066
1067static void comp_mode_change(viewport_t *vp, ipc_call_t *icall)
1068{
1069 sysarg_t mode_idx = ipc_get_arg2(icall);
1070 fibril_mutex_lock(&viewport_list_mtx);
1071
1072 /* Retrieve the mode that shall be set. */
1073 vslmode_t new_mode;
1074 errno_t rc = visualizer_get_mode(vp->sess, &new_mode, mode_idx);
1075 if (rc != EOK) {
1076 fibril_mutex_unlock(&viewport_list_mtx);
1077 async_answer_0(icall, EINVAL);
1078 return;
1079 }
1080
1081 /* Create surface with respect to the retrieved mode. */
1082 surface_t *new_surface = surface_create(new_mode.screen_width,
1083 new_mode.screen_height, NULL, SURFACE_FLAG_SHARED);
1084 if (!new_surface) {
1085 fibril_mutex_unlock(&viewport_list_mtx);
1086 async_answer_0(icall, ENOMEM);
1087 return;
1088 }
1089
1090 /* Try to set the mode and share out the surface. */
1091 rc = visualizer_set_mode(vp->sess,
1092 new_mode.index, new_mode.version, surface_direct_access(new_surface));
1093 if (rc != EOK) {
1094 surface_destroy(new_surface);
1095 fibril_mutex_unlock(&viewport_list_mtx);
1096 async_answer_0(icall, rc);
1097 return;
1098 }
1099
1100 /* Destroy old surface and update viewport. */
1101 surface_destroy(vp->surface);
1102 vp->mode = new_mode;
1103 vp->surface = new_surface;
1104
1105 fibril_mutex_unlock(&viewport_list_mtx);
1106 async_answer_0(icall, EOK);
1107
1108 comp_restrict_pointers();
1109 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
1110}
1111
1112static void viewport_destroy(viewport_t *vp)
1113{
1114 if (vp) {
1115 visualizer_yield(vp->sess);
1116 surface_destroy(vp->surface);
1117 async_hangup(vp->sess);
1118 free(vp);
1119 }
1120}
1121
1122#if 0
1123static void comp_shutdown(void)
1124{
1125 loc_service_unregister(winreg_id);
1126 input_disconnect();
1127
1128 /* Close all clients and their windows. */
1129 fibril_mutex_lock(&window_list_mtx);
1130 list_foreach(window_list, link, window_t, win) {
1131 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
1132 if (event) {
1133 link_initialize(&event->link);
1134 event->type = WINDOW_CLOSE;
1135 prodcons_produce(&win->queue, &event->link);
1136 }
1137 }
1138 fibril_mutex_unlock(&window_list_mtx);
1139
1140 async_answer_0(icall, EOK);
1141
1142 /* All fibrils of the compositor will terminate soon. */
1143}
1144#endif
1145
1146static void comp_visualizer_disconnect(viewport_t *vp, ipc_call_t *icall)
1147{
1148 /* Release viewport resources. */
1149 fibril_mutex_lock(&viewport_list_mtx);
1150
1151 list_remove(&vp->link);
1152 viewport_destroy(vp);
1153
1154 fibril_mutex_unlock(&viewport_list_mtx);
1155
1156 async_answer_0(icall, EOK);
1157
1158 comp_restrict_pointers();
1159 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
1160}
1161
1162static void vsl_notifications(ipc_call_t *icall, void *arg)
1163{
1164 viewport_t *vp = NULL;
1165 fibril_mutex_lock(&viewport_list_mtx);
1166 list_foreach(viewport_list, link, viewport_t, cur) {
1167 if (cur->dsid == (service_id_t) ipc_get_arg1(icall)) {
1168 vp = cur;
1169 break;
1170 }
1171 }
1172 fibril_mutex_unlock(&viewport_list_mtx);
1173
1174 if (!vp)
1175 return;
1176
1177 /* Ignore parameters, the connection is already opened. */
1178 while (true) {
1179 ipc_call_t call;
1180 async_get_call(&call);
1181
1182 if (!ipc_get_imethod(&call)) {
1183 async_hangup(vp->sess);
1184 return;
1185 }
1186
1187 switch (ipc_get_imethod(&call)) {
1188 case VISUALIZER_MODE_CHANGE:
1189 comp_mode_change(vp, &call);
1190 break;
1191 case VISUALIZER_DISCONNECT:
1192 comp_visualizer_disconnect(vp, &call);
1193 return;
1194 default:
1195 async_answer_0(&call, EINVAL);
1196 }
1197 }
1198}
1199
1200static async_sess_t *vsl_connect(service_id_t sid, const char *svc)
1201{
1202 errno_t rc;
1203 async_sess_t *sess;
1204
1205 sess = loc_service_connect(sid, INTERFACE_DDF, 0);
1206 if (sess == NULL) {
1207 printf("%s: Unable to connect to visualizer %s\n", NAME, svc);
1208 return NULL;
1209 }
1210
1211 async_exch_t *exch = async_exchange_begin(sess);
1212
1213 port_id_t port;
1214 rc = async_create_callback_port(exch, INTERFACE_VISUALIZER_CB, 0, 0,
1215 vsl_notifications, NULL, &port);
1216
1217 async_exchange_end(exch);
1218
1219 if (rc != EOK) {
1220 async_hangup(sess);
1221 printf("%s: Unable to create callback connection to service %s (%s)\n",
1222 NAME, svc, str_error(rc));
1223 return NULL;
1224 }
1225
1226 return sess;
1227}
1228
1229static viewport_t *viewport_create(service_id_t sid)
1230{
1231 errno_t rc;
1232 char *vsl_name = NULL;
1233 viewport_t *vp = NULL;
1234 bool claimed = false;
1235
1236 rc = loc_service_get_name(sid, &vsl_name);
1237 if (rc != EOK)
1238 goto error;
1239
1240 vp = (viewport_t *) calloc(1, sizeof(viewport_t));
1241 if (!vp)
1242 goto error;
1243
1244 link_initialize(&vp->link);
1245 vp->pos.x = coord_origin;
1246 vp->pos.y = coord_origin;
1247
1248 /* Establish output bidirectional connection. */
1249 vp->dsid = sid;
1250 vp->sess = vsl_connect(sid, vsl_name);
1251 if (vp->sess == NULL)
1252 goto error;
1253
1254 /* Claim the given visualizer. */
1255 rc = visualizer_claim(vp->sess, 0);
1256 if (rc != EOK) {
1257 printf("%s: Unable to claim visualizer (%s)\n", NAME, str_error(rc));
1258 goto error;
1259 }
1260
1261 claimed = true;
1262
1263 /* Retrieve the default mode. */
1264 rc = visualizer_get_default_mode(vp->sess, &vp->mode);
1265 if (rc != EOK) {
1266 printf("%s: Unable to retrieve mode (%s)\n", NAME, str_error(rc));
1267 goto error;
1268 }
1269
1270 /* Create surface with respect to the retrieved mode. */
1271 vp->surface = surface_create(vp->mode.screen_width, vp->mode.screen_height,
1272 NULL, SURFACE_FLAG_SHARED);
1273 if (vp->surface == NULL) {
1274 printf("%s: Unable to create surface (%s)\n", NAME, str_error(rc));
1275 goto error;
1276 }
1277
1278 /* Try to set the mode and share out the surface. */
1279 rc = visualizer_set_mode(vp->sess,
1280 vp->mode.index, vp->mode.version, surface_direct_access(vp->surface));
1281 if (rc != EOK) {
1282 printf("%s: Unable to set mode (%s)\n", NAME, str_error(rc));
1283 goto error;
1284 }
1285
1286 return vp;
1287error:
1288 if (claimed)
1289 visualizer_yield(vp->sess);
1290
1291 if (vp->sess != NULL)
1292 async_hangup(vp->sess);
1293
1294 free(vp);
1295 free(vsl_name);
1296 return NULL;
1297}
1298
1299static void comp_window_animate(pointer_t *pointer, window_t *win,
1300 sysarg_t *dmg_x, sysarg_t *dmg_y, sysarg_t *dmg_width, sysarg_t *dmg_height)
1301{
1302 /* window_list_mtx locked by caller */
1303 /* pointer_list_mtx locked by caller */
1304
1305 int dx = pointer->accum.x;
1306 int dy = pointer->accum.y;
1307 pointer->accum.x = 0;
1308 pointer->accum.y = 0;
1309
1310 bool move = (pointer->grab_flags & GF_MOVE_X) || (pointer->grab_flags & GF_MOVE_Y);
1311 bool scale = (pointer->grab_flags & GF_SCALE_X) || (pointer->grab_flags & GF_SCALE_Y);
1312 bool resize = (pointer->grab_flags & GF_RESIZE_X) || (pointer->grab_flags & GF_RESIZE_Y);
1313
1314 sysarg_t width, height;
1315 surface_get_resolution(win->surface, &width, &height);
1316
1317 if (move) {
1318 double cx = 0;
1319 double cy = 0;
1320
1321 if (pointer->grab_flags & GF_MOVE_X)
1322 cx = 1;
1323
1324 if (pointer->grab_flags & GF_MOVE_Y)
1325 cy = 1;
1326
1327 if (((scale) || (resize)) && (win->angle != 0)) {
1328 transform_t rotate;
1329 transform_identity(&rotate);
1330
1331 transform_rotate(&rotate, win->angle);
1332 transform_apply_linear(&rotate, &cx, &cy);
1333 }
1334
1335 cx = (cx < 0) ? (-1 * cx) : cx;
1336 cy = (cy < 0) ? (-1 * cy) : cy;
1337
1338 win->dx += (cx * dx);
1339 win->dy += (cy * dy);
1340 }
1341
1342 if ((scale) || (resize)) {
1343 double _dx = dx;
1344 double _dy = dy;
1345 if (win->angle != 0) {
1346 transform_t unrotate;
1347 transform_identity(&unrotate);
1348 transform_rotate(&unrotate, -win->angle);
1349 transform_apply_linear(&unrotate, &_dx, &_dy);
1350 }
1351 _dx = (pointer->grab_flags & GF_MOVE_X) ? -_dx : _dx;
1352 _dy = (pointer->grab_flags & GF_MOVE_Y) ? -_dy : _dy;
1353
1354 if ((pointer->grab_flags & GF_SCALE_X) || (pointer->grab_flags & GF_RESIZE_X)) {
1355 double fx = 1.0 + (_dx / ((width - 1) * win->fx));
1356 if (fx > 0) {
1357#if ANIMATE_WINDOW_TRANSFORMS == 0
1358 if (scale)
1359 win->fx *= fx;
1360#endif
1361#if ANIMATE_WINDOW_TRANSFORMS == 1
1362 win->fx *= fx;
1363#endif
1364 scale_back_x *= fx;
1365 }
1366 }
1367
1368 if ((pointer->grab_flags & GF_SCALE_Y) || (pointer->grab_flags & GF_RESIZE_Y)) {
1369 double fy = 1.0 + (_dy / ((height - 1) * win->fy));
1370 if (fy > 0) {
1371#if ANIMATE_WINDOW_TRANSFORMS == 0
1372 if (scale)
1373 win->fy *= fy;
1374#endif
1375#if ANIMATE_WINDOW_TRANSFORMS == 1
1376 win->fy *= fy;
1377#endif
1378 scale_back_y *= fy;
1379 }
1380 }
1381 }
1382
1383 sysarg_t x1, y1, width1, height1;
1384 sysarg_t x2, y2, width2, height2;
1385 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1386 &x1, &y1, &width1, &height1);
1387 comp_recalc_transform(win);
1388 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1389 &x2, &y2, &width2, &height2);
1390 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
1391 dmg_x, dmg_y, dmg_width, dmg_height);
1392}
1393
1394#if ANIMATE_WINDOW_TRANSFORMS == 0
1395static void comp_ghost_animate(pointer_t *pointer,
1396 desktop_rect_t *rect1, desktop_rect_t *rect2, desktop_rect_t *rect3, desktop_rect_t *rect4)
1397{
1398 /* window_list_mtx locked by caller */
1399 /* pointer_list_mtx locked by caller */
1400
1401 int dx = pointer->accum_ghost.x;
1402 int dy = pointer->accum_ghost.y;
1403 pointer->accum_ghost.x = 0;
1404 pointer->accum_ghost.y = 0;
1405
1406 bool move = (pointer->grab_flags & GF_MOVE_X) || (pointer->grab_flags & GF_MOVE_Y);
1407 bool scale = (pointer->grab_flags & GF_SCALE_X) || (pointer->grab_flags & GF_SCALE_Y);
1408 bool resize = (pointer->grab_flags & GF_RESIZE_X) || (pointer->grab_flags & GF_RESIZE_Y);
1409
1410 sysarg_t width, height;
1411 surface_get_resolution(pointer->ghost.surface, &width, &height);
1412
1413 if (move) {
1414 double cx = 0;
1415 double cy = 0;
1416 if (pointer->grab_flags & GF_MOVE_X) {
1417 cx = 1;
1418 }
1419 if (pointer->grab_flags & GF_MOVE_Y) {
1420 cy = 1;
1421 }
1422
1423 if (scale || resize) {
1424 transform_t rotate;
1425 transform_identity(&rotate);
1426 transform_rotate(&rotate, pointer->ghost.angle);
1427 transform_apply_linear(&rotate, &cx, &cy);
1428 }
1429
1430 cx = (cx < 0) ? (-1 * cx) : cx;
1431 cy = (cy < 0) ? (-1 * cy) : cy;
1432
1433 pointer->ghost.dx += (cx * dx);
1434 pointer->ghost.dy += (cy * dy);
1435 }
1436
1437 if (scale || resize) {
1438 double _dx = dx;
1439 double _dy = dy;
1440 transform_t unrotate;
1441 transform_identity(&unrotate);
1442 transform_rotate(&unrotate, -pointer->ghost.angle);
1443 transform_apply_linear(&unrotate, &_dx, &_dy);
1444 _dx = (pointer->grab_flags & GF_MOVE_X) ? -_dx : _dx;
1445 _dy = (pointer->grab_flags & GF_MOVE_Y) ? -_dy : _dy;
1446
1447 if ((pointer->grab_flags & GF_SCALE_X) || (pointer->grab_flags & GF_RESIZE_X)) {
1448 double fx = 1.0 + (_dx / ((width - 1) * pointer->ghost.fx));
1449 pointer->ghost.fx *= fx;
1450 }
1451
1452 if ((pointer->grab_flags & GF_SCALE_Y) || (pointer->grab_flags & GF_RESIZE_Y)) {
1453 double fy = 1.0 + (_dy / ((height - 1) * pointer->ghost.fy));
1454 pointer->ghost.fy *= fy;
1455 }
1456 }
1457
1458 sysarg_t x1, y1, width1, height1;
1459 sysarg_t x2, y2, width2, height2;
1460 comp_coord_bounding_rect(0, 0, width, height, pointer->ghost.transform,
1461 &x1, &y1, &width1, &height1);
1462 comp_recalc_transform(&pointer->ghost);
1463 comp_coord_bounding_rect(0, 0, width, height, pointer->ghost.transform,
1464 &x2, &y2, &width2, &height2);
1465
1466 sysarg_t x_u, y_u, w_u, h_u;
1467 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
1468 &x_u, &y_u, &w_u, &h_u);
1469
1470 sysarg_t x_i, y_i, w_i, h_i;
1471 rectangle_intersect(x1, y1, width1, height1, x2, y2, width2, height2,
1472 &x_i, &y_i, &w_i, &h_i);
1473
1474 if (w_i == 0 || h_i == 0) {
1475 rect1->x = x_u;
1476 rect2->x = 0;
1477 rect3->x = 0;
1478 rect4->x = 0;
1479
1480 rect1->y = y_u;
1481 rect2->y = 0;
1482 rect3->y = 0;
1483 rect4->y = 0;
1484
1485 rect1->w = w_u;
1486 rect2->w = 0;
1487 rect3->w = 0;
1488 rect4->w = 0;
1489
1490 rect1->h = h_u;
1491 rect2->h = 0;
1492 rect3->h = 0;
1493 rect4->h = 0;
1494 } else {
1495 rect1->x = x_u;
1496 rect1->y = y_u;
1497 rect1->w = x_i - x_u + 1;
1498 rect1->h = h_u;
1499
1500 rect2->x = x_u;
1501 rect2->y = y_u;
1502 rect2->w = w_u;
1503 rect2->h = y_i - y_u + 1;
1504
1505 rect3->x = x_i + w_i - 1;
1506 rect3->y = y_u;
1507 rect3->w = w_u - w_i - x_i + x_u + 1;
1508 rect3->h = h_u;
1509
1510 rect4->x = x_u;
1511 rect4->y = y_i + h_i - 1;
1512 rect4->w = w_u;
1513 rect4->h = h_u - h_i - y_i + y_u + 1;
1514 }
1515}
1516#endif
1517
1518static errno_t comp_abs_move(input_t *input, unsigned x, unsigned y,
1519 unsigned max_x, unsigned max_y)
1520{
1521 /* XXX TODO Use absolute coordinates directly */
1522
1523 pointer_t *pointer = input_pointer(input);
1524
1525 sysarg_t width, height;
1526
1527 fibril_mutex_lock(&viewport_list_mtx);
1528 if (list_empty(&viewport_list)) {
1529 printf("No viewport found\n");
1530 fibril_mutex_unlock(&viewport_list_mtx);
1531 return EOK; /* XXX */
1532 }
1533 link_t *link = list_first(&viewport_list);
1534 viewport_t *vp = list_get_instance(link, viewport_t, link);
1535 surface_get_resolution(vp->surface, &width, &height);
1536 desktop_point_t vp_pos = vp->pos;
1537 fibril_mutex_unlock(&viewport_list_mtx);
1538
1539 desktop_point_t pos_in_viewport;
1540 pos_in_viewport.x = x * width / max_x;
1541 pos_in_viewport.y = y * height / max_y;
1542
1543 /* Calculate offset from pointer */
1544 fibril_mutex_lock(&pointer_list_mtx);
1545 desktop_vector_t delta;
1546 delta.x = (vp_pos.x + pos_in_viewport.x) - pointer->pos.x;
1547 delta.y = (vp_pos.y + pos_in_viewport.y) - pointer->pos.y;
1548 fibril_mutex_unlock(&pointer_list_mtx);
1549
1550 return comp_mouse_move(input, delta.x, delta.y);
1551}
1552
1553static errno_t comp_mouse_move(input_t *input, int dx, int dy)
1554{
1555 pointer_t *pointer = input_pointer(input);
1556
1557 comp_update_viewport_bound_rect();
1558
1559 /* Update pointer position. */
1560 fibril_mutex_lock(&pointer_list_mtx);
1561
1562 desktop_point_t old_pos = pointer->pos;
1563
1564 sysarg_t cursor_width;
1565 sysarg_t cursor_height;
1566 surface_get_resolution(pointer->cursor.states[pointer->state],
1567 &cursor_width, &cursor_height);
1568
1569 if (pointer->pos.x + dx < viewport_bound_rect.x)
1570 dx = -1 * (pointer->pos.x - viewport_bound_rect.x);
1571
1572 if (pointer->pos.y + dy < viewport_bound_rect.y)
1573 dy = -1 * (pointer->pos.y - viewport_bound_rect.y);
1574
1575 if (pointer->pos.x + dx > viewport_bound_rect.x + viewport_bound_rect.w)
1576 dx = (viewport_bound_rect.x + viewport_bound_rect.w - pointer->pos.x);
1577
1578 if (pointer->pos.y + dy > viewport_bound_rect.y + viewport_bound_rect.h)
1579 dy = (viewport_bound_rect.y + viewport_bound_rect.h - pointer->pos.y);
1580
1581 pointer->pos.x += dx;
1582 pointer->pos.y += dy;
1583 fibril_mutex_unlock(&pointer_list_mtx);
1584 comp_damage(old_pos.x, old_pos.y, cursor_width, cursor_height);
1585 comp_damage(old_pos.x + dx, old_pos.y + dy, cursor_width, cursor_height);
1586
1587 fibril_mutex_lock(&window_list_mtx);
1588 fibril_mutex_lock(&pointer_list_mtx);
1589 window_t *top = (window_t *) list_first(&window_list);
1590 if (top && top->surface) {
1591
1592 if (pointer->grab_flags == GF_EMPTY) {
1593 /* Notify top-level window about move event. */
1594 bool within_client = false;
1595 sysarg_t point_x, point_y;
1596 sysarg_t width, height;
1597 surface_get_resolution(top->surface, &width, &height);
1598 within_client = comp_coord_to_client(pointer->pos.x, pointer->pos.y,
1599 top->transform, width, height, &point_x, &point_y);
1600
1601 window_event_t *event = NULL;
1602 if (within_client) {
1603 event = (window_event_t *) malloc(sizeof(window_event_t));
1604 if (event) {
1605 link_initialize(&event->link);
1606 event->type = ET_POSITION_EVENT;
1607 event->data.pos.pos_id = pointer->id;
1608 event->data.pos.type = POS_UPDATE;
1609 event->data.pos.btn_num = pointer->btn_num;
1610 event->data.pos.hpos = point_x;
1611 event->data.pos.vpos = point_y;
1612 }
1613 }
1614
1615 fibril_mutex_unlock(&pointer_list_mtx);
1616 fibril_mutex_unlock(&window_list_mtx);
1617
1618 if (event) {
1619 comp_post_event_top(event);
1620 }
1621
1622 } else {
1623 /* Pointer is grabbed by top-level window action. */
1624 pointer->accum.x += dx;
1625 pointer->accum.y += dy;
1626 pointer->accum_ghost.x += dx;
1627 pointer->accum_ghost.y += dy;
1628#if ANIMATE_WINDOW_TRANSFORMS == 0
1629 if (pointer->ghost.surface == NULL) {
1630 pointer->ghost.surface = top->surface;
1631 pointer->ghost.dx = top->dx;
1632 pointer->ghost.dy = top->dy;
1633 pointer->ghost.fx = top->fx;
1634 pointer->ghost.fy = top->fy;
1635 pointer->ghost.angle = top->angle;
1636 pointer->ghost.transform = top->transform;
1637 }
1638 desktop_rect_t dmg_rect1, dmg_rect2, dmg_rect3, dmg_rect4;
1639 comp_ghost_animate(pointer, &dmg_rect1, &dmg_rect2, &dmg_rect3, &dmg_rect4);
1640#endif
1641#if ANIMATE_WINDOW_TRANSFORMS == 1
1642 sysarg_t x, y, width, height;
1643 comp_window_animate(pointer, top, &x, &y, &width, &height);
1644#endif
1645 fibril_mutex_unlock(&pointer_list_mtx);
1646 fibril_mutex_unlock(&window_list_mtx);
1647#if ANIMATE_WINDOW_TRANSFORMS == 0
1648 comp_damage(dmg_rect1.x, dmg_rect1.y, dmg_rect1.w, dmg_rect1.h);
1649 comp_damage(dmg_rect2.x, dmg_rect2.y, dmg_rect2.w, dmg_rect2.h);
1650 comp_damage(dmg_rect3.x, dmg_rect3.y, dmg_rect3.w, dmg_rect3.h);
1651 comp_damage(dmg_rect4.x, dmg_rect4.y, dmg_rect4.w, dmg_rect4.h);
1652#endif
1653#if ANIMATE_WINDOW_TRANSFORMS == 1
1654 comp_damage(x, y, width, height);
1655#endif
1656 }
1657 } else {
1658 fibril_mutex_unlock(&pointer_list_mtx);
1659 fibril_mutex_unlock(&window_list_mtx);
1660 }
1661
1662 return EOK;
1663}
1664
1665static errno_t comp_mouse_button(input_t *input, int bnum, int bpress)
1666{
1667 pointer_t *pointer = input_pointer(input);
1668
1669 fibril_mutex_lock(&window_list_mtx);
1670 fibril_mutex_lock(&pointer_list_mtx);
1671 window_t *win = NULL;
1672 sysarg_t point_x = 0;
1673 sysarg_t point_y = 0;
1674 sysarg_t width, height;
1675 bool within_client = false;
1676
1677 /* Determine the window which the mouse click belongs to. */
1678 list_foreach(window_list, link, window_t, cw) {
1679 win = cw;
1680 if (win->surface) {
1681 surface_get_resolution(win->surface, &width, &height);
1682 within_client = comp_coord_to_client(pointer->pos.x, pointer->pos.y,
1683 win->transform, width, height, &point_x, &point_y);
1684 }
1685 if (within_client) {
1686 break;
1687 }
1688 }
1689
1690 /* Check whether the window is top-level window. */
1691 window_t *top = (window_t *) list_first(&window_list);
1692 if (!win || !top) {
1693 fibril_mutex_unlock(&pointer_list_mtx);
1694 fibril_mutex_unlock(&window_list_mtx);
1695 return EOK;
1696 }
1697
1698 window_event_t *event_top = NULL;
1699 window_event_t *event_unfocus = NULL;
1700 window_t *win_unfocus = NULL;
1701 sysarg_t dmg_x, dmg_y;
1702 sysarg_t dmg_width = 0;
1703 sysarg_t dmg_height = 0;
1704
1705#if ANIMATE_WINDOW_TRANSFORMS == 0
1706 desktop_rect_t dmg_rect1, dmg_rect2, dmg_rect3, dmg_rect4;
1707#endif
1708
1709 if (bpress && !pointer->pressed) {
1710 pointer->btn_pos = pointer->pos;
1711 pointer->btn_num = bnum;
1712 pointer->pressed = true;
1713
1714 /* Bring the window to the foreground. */
1715 if ((win != top) && within_client) {
1716 win_unfocus = (window_t *) list_first(&window_list);
1717 list_remove(&win->link);
1718 list_prepend(&win->link, &window_list);
1719 event_unfocus = (window_event_t *) malloc(sizeof(window_event_t));
1720 if (event_unfocus) {
1721 link_initialize(&event_unfocus->link);
1722 event_unfocus->type = ET_WINDOW_UNFOCUS;
1723 }
1724 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1725 &dmg_x, &dmg_y, &dmg_width, &dmg_height);
1726 }
1727
1728 /* Notify top-level window about mouse press. */
1729 if (within_client) {
1730 event_top = (window_event_t *) malloc(sizeof(window_event_t));
1731 if (event_top) {
1732 link_initialize(&event_top->link);
1733 event_top->type = ET_POSITION_EVENT;
1734 event_top->data.pos.pos_id = pointer->id;
1735 event_top->data.pos.type = POS_PRESS;
1736 event_top->data.pos.btn_num = bnum;
1737 event_top->data.pos.hpos = point_x;
1738 event_top->data.pos.vpos = point_y;
1739 }
1740 pointer->grab_flags = GF_EMPTY;
1741 }
1742
1743 } else if (pointer->pressed && pointer->btn_num == (unsigned)bnum) {
1744 pointer->pressed = false;
1745
1746#if ANIMATE_WINDOW_TRANSFORMS == 0
1747 sysarg_t pre_x = 0;
1748 sysarg_t pre_y = 0;
1749 sysarg_t pre_width = 0;
1750 sysarg_t pre_height = 0;
1751
1752 if (pointer->grab_flags != GF_EMPTY) {
1753 if (pointer->ghost.surface) {
1754 comp_ghost_animate(pointer, &dmg_rect1, &dmg_rect2, &dmg_rect3, &dmg_rect4);
1755 pointer->ghost.surface = NULL;
1756 }
1757 comp_window_animate(pointer, top, &pre_x, &pre_y, &pre_width, &pre_height);
1758 dmg_x = pre_x;
1759 dmg_y = pre_y;
1760 dmg_width = pre_width;
1761 dmg_height = pre_height;
1762 }
1763#endif
1764
1765 if ((pointer->grab_flags & GF_RESIZE_X) || (pointer->grab_flags & GF_RESIZE_Y)) {
1766
1767 surface_get_resolution(top->surface, &width, &height);
1768#if ANIMATE_WINDOW_TRANSFORMS == 1
1769 top->fx *= (1.0 / scale_back_x);
1770 top->fy *= (1.0 / scale_back_y);
1771 comp_recalc_transform(top);
1772#endif
1773
1774 /* Commit proper resize action. */
1775 event_top = (window_event_t *) malloc(sizeof(window_event_t));
1776 if (event_top) {
1777 link_initialize(&event_top->link);
1778 event_top->type = ET_WINDOW_RESIZE;
1779
1780 event_top->data.resize.offset_x = 0;
1781 event_top->data.resize.offset_y = 0;
1782
1783 int dx = (int) (((double) width) * (scale_back_x - 1.0));
1784 int dy = (int) (((double) height) * (scale_back_y - 1.0));
1785
1786 if (pointer->grab_flags & GF_RESIZE_X)
1787 event_top->data.resize.width =
1788 ((((int) width) + dx) >= 0) ? (width + dx) : 0;
1789 else
1790 event_top->data.resize.width = width;
1791
1792 if (pointer->grab_flags & GF_RESIZE_Y)
1793 event_top->data.resize.height =
1794 ((((int) height) + dy) >= 0) ? (height + dy) : 0;
1795 else
1796 event_top->data.resize.height = height;
1797
1798 event_top->data.resize.placement_flags =
1799 WINDOW_PLACEMENT_ANY;
1800 }
1801
1802 pointer->grab_flags = GF_EMPTY;
1803
1804 } else if (within_client && (pointer->grab_flags == GF_EMPTY) && (top == win)) {
1805
1806 /* Notify top-level window about mouse release. */
1807 event_top = (window_event_t *) malloc(sizeof(window_event_t));
1808 if (event_top) {
1809 link_initialize(&event_top->link);
1810 event_top->type = ET_POSITION_EVENT;
1811 event_top->data.pos.pos_id = pointer->id;
1812 event_top->data.pos.type = POS_RELEASE;
1813 event_top->data.pos.btn_num = bnum;
1814 event_top->data.pos.hpos = point_x;
1815 event_top->data.pos.vpos = point_y;
1816 }
1817 pointer->grab_flags = GF_EMPTY;
1818
1819 } else {
1820 pointer->grab_flags = GF_EMPTY;
1821 }
1822
1823 }
1824
1825 fibril_mutex_unlock(&pointer_list_mtx);
1826 fibril_mutex_unlock(&window_list_mtx);
1827
1828#if ANIMATE_WINDOW_TRANSFORMS == 0
1829 comp_damage(dmg_rect1.x, dmg_rect1.y, dmg_rect1.w, dmg_rect1.h);
1830 comp_damage(dmg_rect2.x, dmg_rect2.y, dmg_rect2.w, dmg_rect2.h);
1831 comp_damage(dmg_rect3.x, dmg_rect3.y, dmg_rect3.w, dmg_rect3.h);
1832 comp_damage(dmg_rect4.x, dmg_rect4.y, dmg_rect4.w, dmg_rect4.h);
1833#endif
1834
1835 if (dmg_width > 0 && dmg_height > 0) {
1836 comp_damage(dmg_x, dmg_y, dmg_width, dmg_height);
1837 }
1838
1839 if (event_unfocus && win_unfocus) {
1840 comp_post_event_win(event_unfocus, win_unfocus);
1841 }
1842
1843 if (event_top) {
1844 comp_post_event_top(event_top);
1845 }
1846
1847 return EOK;
1848}
1849
1850static errno_t comp_active(input_t *input)
1851{
1852 active = true;
1853 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
1854
1855 return EOK;
1856}
1857
1858static errno_t comp_deactive(input_t *input)
1859{
1860 active = false;
1861 return EOK;
1862}
1863
1864static errno_t comp_key_press(input_t *input, kbd_event_type_t type, keycode_t key,
1865 keymod_t mods, wchar_t c)
1866{
1867 bool win_transform = (mods & KM_ALT) &&
1868 (key == KC_W || key == KC_S || key == KC_A || key == KC_D ||
1869 key == KC_Q || key == KC_E || key == KC_R || key == KC_F);
1870 bool win_resize = (mods & KM_ALT) &&
1871 (key == KC_T || key == KC_G || key == KC_B || key == KC_N);
1872 bool win_opacity = (mods & KM_ALT) && (key == KC_C || key == KC_V);
1873 bool win_close = (mods & KM_ALT) && (key == KC_X);
1874 bool win_switch = (mods & KM_ALT) && (key == KC_TAB);
1875 bool viewport_move = (mods & KM_ALT) &&
1876 (key == KC_I || key == KC_K || key == KC_J || key == KC_L);
1877 bool viewport_change = (mods & KM_ALT) && (key == KC_O || key == KC_P);
1878 bool kconsole_switch = (key == KC_PAUSE) || (key == KC_BREAK);
1879 bool filter_switch = (mods & KM_ALT) && (key == KC_Y);
1880
1881 bool key_filter = (type == KEY_RELEASE) && (win_transform || win_resize ||
1882 win_opacity || win_close || win_switch || viewport_move ||
1883 viewport_change || kconsole_switch || filter_switch);
1884
1885 if (key_filter) {
1886 /* no-op */
1887 } else if (win_transform) {
1888 fibril_mutex_lock(&window_list_mtx);
1889 window_t *win = (window_t *) list_first(&window_list);
1890 if (win && win->surface) {
1891 switch (key) {
1892 case KC_W:
1893 win->dy += -20;
1894 break;
1895 case KC_S:
1896 win->dy += 20;
1897 break;
1898 case KC_A:
1899 win->dx += -20;
1900 break;
1901 case KC_D:
1902 win->dx += 20;
1903 break;
1904 case KC_Q:
1905 win->angle += 0.1;
1906 break;
1907 case KC_E:
1908 win->angle -= 0.1;
1909 break;
1910 case KC_R:
1911 win->fx *= 0.95;
1912 win->fy *= 0.95;
1913 break;
1914 case KC_F:
1915 win->fx *= 1.05;
1916 win->fy *= 1.05;
1917 break;
1918 default:
1919 break;
1920 }
1921
1922 /* Transform the window and calculate damage. */
1923 sysarg_t x, y, width, height;
1924 surface_get_resolution(win->surface, &width, &height);
1925 sysarg_t x1, y1, width1, height1;
1926 sysarg_t x2, y2, width2, height2;
1927 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1928 &x1, &y1, &width1, &height1);
1929 comp_recalc_transform(win);
1930 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1931 &x2, &y2, &width2, &height2);
1932 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
1933 &x, &y, &width, &height);
1934 fibril_mutex_unlock(&window_list_mtx);
1935
1936 comp_damage(x, y, width, height);
1937 } else {
1938 fibril_mutex_unlock(&window_list_mtx);
1939 }
1940 } else if (win_resize) {
1941 fibril_mutex_lock(&window_list_mtx);
1942 window_t *win = (window_t *) list_first(&window_list);
1943 if ((win) && (win->surface) && (win->flags & WINDOW_RESIZEABLE)) {
1944 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
1945 if (event == NULL) {
1946 fibril_mutex_unlock(&window_list_mtx);
1947 return ENOMEM;
1948 }
1949
1950 sysarg_t width, height;
1951 surface_get_resolution(win->surface, &width, &height);
1952
1953 link_initialize(&event->link);
1954 event->type = ET_WINDOW_RESIZE;
1955
1956 event->data.resize.offset_x = 0;
1957 event->data.resize.offset_y = 0;
1958
1959 switch (key) {
1960 case KC_T:
1961 event->data.resize.width = width;
1962 event->data.resize.height = (height >= 20) ? height - 20 : 0;
1963 break;
1964 case KC_G:
1965 event->data.resize.width = width;
1966 event->data.resize.height = height + 20;
1967 break;
1968 case KC_B:
1969 event->data.resize.width = (width >= 20) ? width - 20 : 0;
1970 event->data.resize.height = height;
1971 break;
1972 case KC_N:
1973 event->data.resize.width = width + 20;
1974 event->data.resize.height = height;
1975 break;
1976 default:
1977 event->data.resize.width = 0;
1978 event->data.resize.height = 0;
1979 break;
1980 }
1981
1982 event->data.resize.placement_flags = WINDOW_PLACEMENT_ANY;
1983
1984 fibril_mutex_unlock(&window_list_mtx);
1985 comp_post_event_top(event);
1986 } else {
1987 fibril_mutex_unlock(&window_list_mtx);
1988 }
1989 } else if (win_opacity) {
1990 fibril_mutex_lock(&window_list_mtx);
1991 window_t *win = (window_t *) list_first(&window_list);
1992 if (win && win->surface) {
1993 switch (key) {
1994 case KC_C:
1995 if (win->opacity > 0) {
1996 win->opacity -= 5;
1997 }
1998 break;
1999 case KC_V:
2000 if (win->opacity < 255) {
2001 win->opacity += 5;
2002 }
2003 break;
2004 default:
2005 break;
2006 }
2007
2008 /* Calculate damage. */
2009 sysarg_t x, y, width, height;
2010 surface_get_resolution(win->surface, &width, &height);
2011 comp_coord_bounding_rect(0, 0, width, height, win->transform,
2012 &x, &y, &width, &height);
2013 fibril_mutex_unlock(&window_list_mtx);
2014
2015 comp_damage(x, y, width, height);
2016 } else {
2017 fibril_mutex_unlock(&window_list_mtx);
2018 }
2019 } else if (win_close) {
2020 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
2021 if (event == NULL)
2022 return ENOMEM;
2023
2024 link_initialize(&event->link);
2025 event->type = ET_WINDOW_CLOSE;
2026
2027 comp_post_event_top(event);
2028 } else if (win_switch) {
2029 fibril_mutex_lock(&window_list_mtx);
2030 if (!list_empty(&window_list)) {
2031 window_t *win1 = (window_t *) list_first(&window_list);
2032 list_remove(&win1->link);
2033 list_append(&win1->link, &window_list);
2034 window_t *win2 = (window_t *) list_first(&window_list);
2035
2036 window_event_t *event1 = (window_event_t *) malloc(sizeof(window_event_t));
2037 if (event1) {
2038 link_initialize(&event1->link);
2039 event1->type = ET_WINDOW_UNFOCUS;
2040 }
2041
2042 window_event_t *event2 = (window_event_t *) malloc(sizeof(window_event_t));
2043 if (event2) {
2044 link_initialize(&event2->link);
2045 event2->type = ET_WINDOW_FOCUS;
2046 }
2047
2048 sysarg_t x1 = 0;
2049 sysarg_t y1 = 0;
2050 sysarg_t width1 = 0;
2051 sysarg_t height1 = 0;
2052 if (win1->surface) {
2053 sysarg_t width, height;
2054 surface_get_resolution(win1->surface, &width, &height);
2055 comp_coord_bounding_rect(0, 0, width, height, win1->transform,
2056 &x1, &y1, &width1, &height1);
2057 }
2058
2059 sysarg_t x2 = 0;
2060 sysarg_t y2 = 0;
2061 sysarg_t width2 = 0;
2062 sysarg_t height2 = 0;
2063 if (win2->surface) {
2064 sysarg_t width, height;
2065 surface_get_resolution(win2->surface, &width, &height);
2066 comp_coord_bounding_rect(0, 0, width, height, win2->transform,
2067 &x2, &y2, &width2, &height2);
2068 }
2069
2070 sysarg_t x, y, width, height;
2071 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
2072 &x, &y, &width, &height);
2073
2074 fibril_mutex_unlock(&window_list_mtx);
2075
2076 if (event1 && win1) {
2077 comp_post_event_win(event1, win1);
2078 }
2079
2080 if (event2 && win2) {
2081 comp_post_event_win(event2, win2);
2082 }
2083
2084 comp_damage(x, y, width, height);
2085 } else {
2086 fibril_mutex_unlock(&window_list_mtx);
2087 }
2088 } else if (viewport_move) {
2089 fibril_mutex_lock(&viewport_list_mtx);
2090 viewport_t *vp = (viewport_t *) list_first(&viewport_list);
2091 if (vp) {
2092 switch (key) {
2093 case KC_I:
2094 vp->pos.x += 0;
2095 vp->pos.y += -20;
2096 break;
2097 case KC_K:
2098 vp->pos.x += 0;
2099 vp->pos.y += 20;
2100 break;
2101 case KC_J:
2102 vp->pos.x += -20;
2103 vp->pos.y += 0;
2104 break;
2105 case KC_L:
2106 vp->pos.x += 20;
2107 vp->pos.y += 0;
2108 break;
2109 default:
2110 vp->pos.x += 0;
2111 vp->pos.y += 0;
2112 break;
2113 }
2114
2115 sysarg_t x = vp->pos.x;
2116 sysarg_t y = vp->pos.y;
2117 sysarg_t width, height;
2118 surface_get_resolution(vp->surface, &width, &height);
2119 fibril_mutex_unlock(&viewport_list_mtx);
2120
2121 comp_restrict_pointers();
2122 comp_damage(x, y, width, height);
2123 } else {
2124 fibril_mutex_unlock(&viewport_list_mtx);
2125 }
2126 } else if (viewport_change) {
2127 fibril_mutex_lock(&viewport_list_mtx);
2128
2129 viewport_t *vp;
2130 switch (key) {
2131 case KC_O:
2132 vp = (viewport_t *) list_first(&viewport_list);
2133 if (vp) {
2134 list_remove(&vp->link);
2135 list_append(&vp->link, &viewport_list);
2136 }
2137 break;
2138 case KC_P:
2139 vp = (viewport_t *) list_last(&viewport_list);
2140 if (vp) {
2141 list_remove(&vp->link);
2142 list_prepend(&vp->link, &viewport_list);
2143 }
2144 break;
2145 default:
2146 break;
2147 }
2148
2149 fibril_mutex_unlock(&viewport_list_mtx);
2150 } else if (kconsole_switch) {
2151 if (console_kcon())
2152 active = false;
2153 } else if (filter_switch) {
2154 filter_index++;
2155 if (filter_index > 1)
2156 filter_index = 0;
2157 if (filter_index == 0) {
2158 filter = filter_nearest;
2159 } else {
2160 filter = filter_bilinear;
2161 }
2162 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
2163 } else {
2164 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
2165 if (event == NULL)
2166 return ENOMEM;
2167
2168 link_initialize(&event->link);
2169 event->type = ET_KEYBOARD_EVENT;
2170 event->data.kbd.type = type;
2171 event->data.kbd.key = key;
2172 event->data.kbd.mods = mods;
2173 event->data.kbd.c = c;
2174
2175 comp_post_event_top(event);
2176 }
2177
2178 return EOK;
2179}
2180
2181static errno_t input_connect(const char *svc)
2182{
2183 async_sess_t *sess;
2184 service_id_t dsid;
2185
2186 errno_t rc = loc_service_get_id(svc, &dsid, 0);
2187 if (rc != EOK) {
2188 printf("%s: Input service %s not found\n", NAME, svc);
2189 return rc;
2190 }
2191
2192 sess = loc_service_connect(dsid, INTERFACE_INPUT, 0);
2193 if (sess == NULL) {
2194 printf("%s: Unable to connect to input service %s\n", NAME,
2195 svc);
2196 return EIO;
2197 }
2198
2199 fibril_mutex_lock(&pointer_list_mtx);
2200 pointer_t *pointer = pointer_create();
2201 if (pointer != NULL) {
2202 pointer->id = pointer_id++;
2203 list_append(&pointer->link, &pointer_list);
2204 }
2205 fibril_mutex_unlock(&pointer_list_mtx);
2206
2207 if (pointer == NULL) {
2208 printf("%s: Cannot create pointer.\n", NAME);
2209 async_hangup(sess);
2210 return ENOMEM;
2211 }
2212
2213 rc = input_open(sess, &input_ev_ops, pointer, &input);
2214 if (rc != EOK) {
2215 async_hangup(sess);
2216 printf("%s: Unable to communicate with service %s (%s)\n",
2217 NAME, svc, str_error(rc));
2218 return rc;
2219 }
2220
2221 return EOK;
2222}
2223
2224static void input_disconnect(void)
2225{
2226 pointer_t *pointer = input->user;
2227 input_close(input);
2228 pointer_destroy(pointer);
2229}
2230
2231static void discover_viewports(void)
2232{
2233 fibril_mutex_lock(&discovery_mtx);
2234
2235 /* Create viewports and connect them to visualizers. */
2236 category_id_t cat_id;
2237 errno_t rc = loc_category_get_id("visualizer", &cat_id, IPC_FLAG_BLOCKING);
2238 if (rc != EOK)
2239 goto ret;
2240
2241 service_id_t *svcs;
2242 size_t svcs_cnt = 0;
2243 rc = loc_category_get_svcs(cat_id, &svcs, &svcs_cnt);
2244 if (rc != EOK)
2245 goto ret;
2246
2247 fibril_mutex_lock(&viewport_list_mtx);
2248 for (size_t i = 0; i < svcs_cnt; ++i) {
2249 bool exists = false;
2250 list_foreach(viewport_list, link, viewport_t, vp) {
2251 if (vp->dsid == svcs[i]) {
2252 exists = true;
2253 break;
2254 }
2255 }
2256
2257 if (exists)
2258 continue;
2259
2260 viewport_t *vp = viewport_create(svcs[i]);
2261 if (vp != NULL)
2262 list_append(&vp->link, &viewport_list);
2263 }
2264 fibril_mutex_unlock(&viewport_list_mtx);
2265
2266 if (!list_empty(&viewport_list))
2267 input_activate(input);
2268
2269ret:
2270 fibril_mutex_unlock(&discovery_mtx);
2271}
2272
2273static void category_change_cb(void *arg)
2274{
2275 discover_viewports();
2276}
2277
2278static errno_t compositor_srv_init(char *input_svc, char *name)
2279{
2280 /* Coordinates of the central pixel. */
2281 coord_origin = UINT32_MAX / 4;
2282
2283 /* Color of the viewport background. Must be opaque. */
2284 bg_color = PIXEL(255, 69, 51, 103);
2285
2286 /* Register compositor server. */
2287 async_set_fallback_port_handler(client_connection, NULL);
2288
2289 errno_t rc = loc_server_register(NAME);
2290 if (rc != EOK) {
2291 printf("%s: Unable to register server (%s)\n", NAME, str_error(rc));
2292 return -1;
2293 }
2294
2295 server_name = name;
2296
2297 char svc[LOC_NAME_MAXLEN + 1];
2298 snprintf(svc, LOC_NAME_MAXLEN, "%s/%s", NAMESPACE, server_name);
2299
2300 service_id_t service_id;
2301 rc = loc_service_register(svc, &service_id);
2302 if (rc != EOK) {
2303 printf("%s: Unable to register service %s\n", NAME, svc);
2304 return rc;
2305 }
2306
2307 /* Prepare window registrator (entrypoint for clients). */
2308 char winreg[LOC_NAME_MAXLEN + 1];
2309 snprintf(winreg, LOC_NAME_MAXLEN, "%s%s/winreg", NAMESPACE, server_name);
2310 if (loc_service_register(winreg, &winreg_id) != EOK) {
2311 printf("%s: Unable to register service %s\n", NAME, winreg);
2312 return -1;
2313 }
2314
2315 /* Establish input bidirectional connection. */
2316 rc = input_connect(input_svc);
2317 if (rc != EOK) {
2318 printf("%s: Failed to connect to input service.\n", NAME);
2319 return rc;
2320 }
2321
2322 rc = loc_register_cat_change_cb(category_change_cb, NULL);
2323 if (rc != EOK) {
2324 printf("%s: Failed to register category change callback\n", NAME);
2325 input_disconnect();
2326 return rc;
2327 }
2328
2329 discover_viewports();
2330
2331 comp_restrict_pointers();
2332 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
2333
2334 return EOK;
2335}
2336
2337static void usage(char *name)
2338{
2339 printf("Usage: %s <input_dev> <server_name>\n", name);
2340}
2341
2342int main(int argc, char *argv[])
2343{
2344 if (argc < 3) {
2345 usage(argv[0]);
2346 return 1;
2347 }
2348
2349 printf("%s: HelenOS Compositor server\n", NAME);
2350
2351 errno_t rc = compositor_srv_init(argv[1], argv[2]);
2352 if (rc != EOK)
2353 return rc;
2354
2355 printf("%s: Accepting connections\n", NAME);
2356 task_retval(0);
2357 async_manager();
2358
2359 /* Never reached */
2360 return 0;
2361}
2362
2363/** @}
2364 */
Note: See TracBrowser for help on using the repository browser.