source: mainline/uspace/srv/hid/compositor/compositor.c@ 8d3512f1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8d3512f1 was 8d3512f1, checked in by Martin Sucha <sucha14@…>, 11 years ago

Implement a bilinear filter and use it when compositing windows.

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