source: mainline/uspace/srv/hid/compositor/compositor.c@ 00ddb40

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

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