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

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

rename sys_debug_activate_console to sys_debug_console in order to anticipate more generic use of the syscall in the near future

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