source: mainline/uspace/srv/hid/compositor/compositor.c@ 071fefec

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

full support for affine transformation with rotation
(given the trigonometric functions are properly implemented for the target platform)
transformation matrix code cleanup

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