source: mainline/uspace/srv/hid/compositor/compositor.c@ 6abdef37

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6abdef37 was 790f3a3, checked in by Jakub Jermar <jakub@…>, 7 years ago

Do not answer call twice

Answering the same call handle twice is an error because the call handle
might have been allocated for another call in a different fibril.

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