source: mainline/uspace/srv/hid/compositor/compositor.c@ 498ced1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 498ced1 was 498ced1, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Unify reference counting and remove some unnecessary instances of <atomic.h>

  • 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_answer_0(icall, EOK);
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_2(&call, ENOMEM, 0, 0);
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_2(&call, EINVAL, 0, 0);
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_2(&call, EINVAL, 0, 0);
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 return;
977 } else {
978 async_answer_0(&call, EINVAL);
979 return;
980 }
981 }
982
983 /* Match the client with pre-allocated window. */
984 window_t *win = NULL;
985 fibril_mutex_lock(&window_list_mtx);
986 list_foreach(window_list, link, window_t, cur) {
987 if (cur->in_dsid == service_id || cur->out_dsid == service_id) {
988 win = cur;
989 break;
990 }
991 }
992
993 if (win)
994 refcount_up(&win->ref_cnt);
995
996 fibril_mutex_unlock(&window_list_mtx);
997
998 if (win) {
999 async_answer_0(icall, EOK);
1000 } else {
1001 async_answer_0(icall, EINVAL);
1002 return;
1003 }
1004
1005 /* Each client establishes two separate connections. */
1006 if (win->in_dsid == service_id) {
1007 while (true) {
1008 async_get_call(&call);
1009
1010 if (!IPC_GET_IMETHOD(call)) {
1011 async_answer_0(&call, EOK);
1012 window_destroy(win);
1013 return;
1014 }
1015
1016 switch (IPC_GET_IMETHOD(call)) {
1017 case WINDOW_GET_EVENT:
1018 comp_window_get_event(win, &call);
1019 break;
1020 default:
1021 async_answer_0(&call, EINVAL);
1022 }
1023 }
1024 } else if (win->out_dsid == service_id) {
1025 while (true) {
1026 async_get_call(&call);
1027
1028 if (!IPC_GET_IMETHOD(call)) {
1029 comp_window_close(win, &call);
1030 window_destroy(win);
1031 return;
1032 }
1033
1034 switch (IPC_GET_IMETHOD(call)) {
1035 case WINDOW_DAMAGE:
1036 comp_window_damage(win, &call);
1037 break;
1038 case WINDOW_GRAB:
1039 comp_window_grab(win, &call);
1040 break;
1041 case WINDOW_RESIZE:
1042 comp_window_resize(win, &call);
1043 break;
1044 case WINDOW_CLOSE:
1045 /*
1046 * Postpone the closing until the phone is hung up to cover
1047 * the case when the client is killed abruptly.
1048 */
1049 async_answer_0(&call, EOK);
1050 break;
1051 case WINDOW_CLOSE_REQUEST:
1052 comp_window_close_request(win, &call);
1053 break;
1054 default:
1055 async_answer_0(&call, EINVAL);
1056 }
1057 }
1058 }
1059}
1060
1061static void comp_mode_change(viewport_t *vp, ipc_call_t *icall)
1062{
1063 sysarg_t mode_idx = IPC_GET_ARG2(*icall);
1064 fibril_mutex_lock(&viewport_list_mtx);
1065
1066 /* Retrieve the mode that shall be set. */
1067 vslmode_t new_mode;
1068 errno_t rc = visualizer_get_mode(vp->sess, &new_mode, mode_idx);
1069 if (rc != EOK) {
1070 fibril_mutex_unlock(&viewport_list_mtx);
1071 async_answer_0(icall, EINVAL);
1072 return;
1073 }
1074
1075 /* Create surface with respect to the retrieved mode. */
1076 surface_t *new_surface = surface_create(new_mode.screen_width,
1077 new_mode.screen_height, NULL, SURFACE_FLAG_SHARED);
1078 if (!new_surface) {
1079 fibril_mutex_unlock(&viewport_list_mtx);
1080 async_answer_0(icall, ENOMEM);
1081 return;
1082 }
1083
1084 /* Try to set the mode and share out the surface. */
1085 rc = visualizer_set_mode(vp->sess,
1086 new_mode.index, new_mode.version, surface_direct_access(new_surface));
1087 if (rc != EOK) {
1088 surface_destroy(new_surface);
1089 fibril_mutex_unlock(&viewport_list_mtx);
1090 async_answer_0(icall, rc);
1091 return;
1092 }
1093
1094 /* Destroy old surface and update viewport. */
1095 surface_destroy(vp->surface);
1096 vp->mode = new_mode;
1097 vp->surface = new_surface;
1098
1099 fibril_mutex_unlock(&viewport_list_mtx);
1100 async_answer_0(icall, EOK);
1101
1102 comp_restrict_pointers();
1103 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
1104}
1105
1106static void viewport_destroy(viewport_t *vp)
1107{
1108 if (vp) {
1109 visualizer_yield(vp->sess);
1110 surface_destroy(vp->surface);
1111 async_hangup(vp->sess);
1112 free(vp);
1113 }
1114}
1115
1116#if 0
1117static void comp_shutdown(void)
1118{
1119 loc_service_unregister(winreg_id);
1120 input_disconnect();
1121
1122 /* Close all clients and their windows. */
1123 fibril_mutex_lock(&window_list_mtx);
1124 list_foreach(window_list, link, window_t, win) {
1125 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
1126 if (event) {
1127 link_initialize(&event->link);
1128 event->type = WINDOW_CLOSE;
1129 prodcons_produce(&win->queue, &event->link);
1130 }
1131 }
1132 fibril_mutex_unlock(&window_list_mtx);
1133
1134 async_answer_0(icall, EOK);
1135
1136 /* All fibrils of the compositor will terminate soon. */
1137}
1138#endif
1139
1140static void comp_visualizer_disconnect(viewport_t *vp, ipc_call_t *icall)
1141{
1142 /* Release viewport resources. */
1143 fibril_mutex_lock(&viewport_list_mtx);
1144
1145 list_remove(&vp->link);
1146 viewport_destroy(vp);
1147
1148 fibril_mutex_unlock(&viewport_list_mtx);
1149
1150 async_answer_0(icall, EOK);
1151
1152 comp_restrict_pointers();
1153 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
1154}
1155
1156static void vsl_notifications(ipc_call_t *icall, void *arg)
1157{
1158 viewport_t *vp = NULL;
1159 fibril_mutex_lock(&viewport_list_mtx);
1160 list_foreach(viewport_list, link, viewport_t, cur) {
1161 if (cur->dsid == (service_id_t) IPC_GET_ARG1(*icall)) {
1162 vp = cur;
1163 break;
1164 }
1165 }
1166 fibril_mutex_unlock(&viewport_list_mtx);
1167
1168 if (!vp)
1169 return;
1170
1171 /* Ignore parameters, the connection is already opened. */
1172 while (true) {
1173 ipc_call_t call;
1174 async_get_call(&call);
1175
1176 if (!IPC_GET_IMETHOD(call)) {
1177 async_hangup(vp->sess);
1178 return;
1179 }
1180
1181 switch (IPC_GET_IMETHOD(call)) {
1182 case VISUALIZER_MODE_CHANGE:
1183 comp_mode_change(vp, &call);
1184 break;
1185 case VISUALIZER_DISCONNECT:
1186 comp_visualizer_disconnect(vp, &call);
1187 return;
1188 default:
1189 async_answer_0(&call, EINVAL);
1190 }
1191 }
1192}
1193
1194static async_sess_t *vsl_connect(service_id_t sid, const char *svc)
1195{
1196 errno_t rc;
1197 async_sess_t *sess;
1198
1199 sess = loc_service_connect(sid, INTERFACE_DDF, 0);
1200 if (sess == NULL) {
1201 printf("%s: Unable to connect to visualizer %s\n", NAME, svc);
1202 return NULL;
1203 }
1204
1205 async_exch_t *exch = async_exchange_begin(sess);
1206
1207 port_id_t port;
1208 rc = async_create_callback_port(exch, INTERFACE_VISUALIZER_CB, 0, 0,
1209 vsl_notifications, NULL, &port);
1210
1211 async_exchange_end(exch);
1212
1213 if (rc != EOK) {
1214 async_hangup(sess);
1215 printf("%s: Unable to create callback connection to service %s (%s)\n",
1216 NAME, svc, str_error(rc));
1217 return NULL;
1218 }
1219
1220 return sess;
1221}
1222
1223static viewport_t *viewport_create(service_id_t sid)
1224{
1225 errno_t rc;
1226 char *vsl_name = NULL;
1227 viewport_t *vp = NULL;
1228 bool claimed = false;
1229
1230 rc = loc_service_get_name(sid, &vsl_name);
1231 if (rc != EOK)
1232 goto error;
1233
1234 vp = (viewport_t *) calloc(1, sizeof(viewport_t));
1235 if (!vp)
1236 goto error;
1237
1238 link_initialize(&vp->link);
1239 vp->pos.x = coord_origin;
1240 vp->pos.y = coord_origin;
1241
1242 /* Establish output bidirectional connection. */
1243 vp->dsid = sid;
1244 vp->sess = vsl_connect(sid, vsl_name);
1245 if (vp->sess == NULL)
1246 goto error;
1247
1248 /* Claim the given visualizer. */
1249 rc = visualizer_claim(vp->sess, 0);
1250 if (rc != EOK) {
1251 printf("%s: Unable to claim visualizer (%s)\n", NAME, str_error(rc));
1252 goto error;
1253 }
1254
1255 claimed = true;
1256
1257 /* Retrieve the default mode. */
1258 rc = visualizer_get_default_mode(vp->sess, &vp->mode);
1259 if (rc != EOK) {
1260 printf("%s: Unable to retrieve mode (%s)\n", NAME, str_error(rc));
1261 goto error;
1262 }
1263
1264 /* Create surface with respect to the retrieved mode. */
1265 vp->surface = surface_create(vp->mode.screen_width, vp->mode.screen_height,
1266 NULL, SURFACE_FLAG_SHARED);
1267 if (vp->surface == NULL) {
1268 printf("%s: Unable to create surface (%s)\n", NAME, str_error(rc));
1269 goto error;
1270 }
1271
1272 /* Try to set the mode and share out the surface. */
1273 rc = visualizer_set_mode(vp->sess,
1274 vp->mode.index, vp->mode.version, surface_direct_access(vp->surface));
1275 if (rc != EOK) {
1276 printf("%s: Unable to set mode (%s)\n", NAME, str_error(rc));
1277 goto error;
1278 }
1279
1280 return vp;
1281error:
1282 if (claimed)
1283 visualizer_yield(vp->sess);
1284
1285 if (vp->sess != NULL)
1286 async_hangup(vp->sess);
1287
1288 free(vp);
1289 free(vsl_name);
1290 return NULL;
1291}
1292
1293static void comp_window_animate(pointer_t *pointer, window_t *win,
1294 sysarg_t *dmg_x, sysarg_t *dmg_y, sysarg_t *dmg_width, sysarg_t *dmg_height)
1295{
1296 /* window_list_mtx locked by caller */
1297 /* pointer_list_mtx locked by caller */
1298
1299 int dx = pointer->accum.x;
1300 int dy = pointer->accum.y;
1301 pointer->accum.x = 0;
1302 pointer->accum.y = 0;
1303
1304 bool move = (pointer->grab_flags & GF_MOVE_X) || (pointer->grab_flags & GF_MOVE_Y);
1305 bool scale = (pointer->grab_flags & GF_SCALE_X) || (pointer->grab_flags & GF_SCALE_Y);
1306 bool resize = (pointer->grab_flags & GF_RESIZE_X) || (pointer->grab_flags & GF_RESIZE_Y);
1307
1308 sysarg_t width, height;
1309 surface_get_resolution(win->surface, &width, &height);
1310
1311 if (move) {
1312 double cx = 0;
1313 double cy = 0;
1314
1315 if (pointer->grab_flags & GF_MOVE_X)
1316 cx = 1;
1317
1318 if (pointer->grab_flags & GF_MOVE_Y)
1319 cy = 1;
1320
1321 if (((scale) || (resize)) && (win->angle != 0)) {
1322 transform_t rotate;
1323 transform_identity(&rotate);
1324
1325 transform_rotate(&rotate, win->angle);
1326 transform_apply_linear(&rotate, &cx, &cy);
1327 }
1328
1329 cx = (cx < 0) ? (-1 * cx) : cx;
1330 cy = (cy < 0) ? (-1 * cy) : cy;
1331
1332 win->dx += (cx * dx);
1333 win->dy += (cy * dy);
1334 }
1335
1336 if ((scale) || (resize)) {
1337 double _dx = dx;
1338 double _dy = dy;
1339 if (win->angle != 0) {
1340 transform_t unrotate;
1341 transform_identity(&unrotate);
1342 transform_rotate(&unrotate, -win->angle);
1343 transform_apply_linear(&unrotate, &_dx, &_dy);
1344 }
1345 _dx = (pointer->grab_flags & GF_MOVE_X) ? -_dx : _dx;
1346 _dy = (pointer->grab_flags & GF_MOVE_Y) ? -_dy : _dy;
1347
1348 if ((pointer->grab_flags & GF_SCALE_X) || (pointer->grab_flags & GF_RESIZE_X)) {
1349 double fx = 1.0 + (_dx / ((width - 1) * win->fx));
1350 if (fx > 0) {
1351#if ANIMATE_WINDOW_TRANSFORMS == 0
1352 if (scale)
1353 win->fx *= fx;
1354#endif
1355#if ANIMATE_WINDOW_TRANSFORMS == 1
1356 win->fx *= fx;
1357#endif
1358 scale_back_x *= fx;
1359 }
1360 }
1361
1362 if ((pointer->grab_flags & GF_SCALE_Y) || (pointer->grab_flags & GF_RESIZE_Y)) {
1363 double fy = 1.0 + (_dy / ((height - 1) * win->fy));
1364 if (fy > 0) {
1365#if ANIMATE_WINDOW_TRANSFORMS == 0
1366 if (scale)
1367 win->fy *= fy;
1368#endif
1369#if ANIMATE_WINDOW_TRANSFORMS == 1
1370 win->fy *= fy;
1371#endif
1372 scale_back_y *= fy;
1373 }
1374 }
1375 }
1376
1377 sysarg_t x1, y1, width1, height1;
1378 sysarg_t x2, y2, width2, height2;
1379 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1380 &x1, &y1, &width1, &height1);
1381 comp_recalc_transform(win);
1382 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1383 &x2, &y2, &width2, &height2);
1384 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
1385 dmg_x, dmg_y, dmg_width, dmg_height);
1386}
1387
1388#if ANIMATE_WINDOW_TRANSFORMS == 0
1389static void comp_ghost_animate(pointer_t *pointer,
1390 desktop_rect_t *rect1, desktop_rect_t *rect2, desktop_rect_t *rect3, desktop_rect_t *rect4)
1391{
1392 /* window_list_mtx locked by caller */
1393 /* pointer_list_mtx locked by caller */
1394
1395 int dx = pointer->accum_ghost.x;
1396 int dy = pointer->accum_ghost.y;
1397 pointer->accum_ghost.x = 0;
1398 pointer->accum_ghost.y = 0;
1399
1400 bool move = (pointer->grab_flags & GF_MOVE_X) || (pointer->grab_flags & GF_MOVE_Y);
1401 bool scale = (pointer->grab_flags & GF_SCALE_X) || (pointer->grab_flags & GF_SCALE_Y);
1402 bool resize = (pointer->grab_flags & GF_RESIZE_X) || (pointer->grab_flags & GF_RESIZE_Y);
1403
1404 sysarg_t width, height;
1405 surface_get_resolution(pointer->ghost.surface, &width, &height);
1406
1407 if (move) {
1408 double cx = 0;
1409 double cy = 0;
1410 if (pointer->grab_flags & GF_MOVE_X) {
1411 cx = 1;
1412 }
1413 if (pointer->grab_flags & GF_MOVE_Y) {
1414 cy = 1;
1415 }
1416
1417 if (scale || resize) {
1418 transform_t rotate;
1419 transform_identity(&rotate);
1420 transform_rotate(&rotate, pointer->ghost.angle);
1421 transform_apply_linear(&rotate, &cx, &cy);
1422 }
1423
1424 cx = (cx < 0) ? (-1 * cx) : cx;
1425 cy = (cy < 0) ? (-1 * cy) : cy;
1426
1427 pointer->ghost.dx += (cx * dx);
1428 pointer->ghost.dy += (cy * dy);
1429 }
1430
1431 if (scale || resize) {
1432 double _dx = dx;
1433 double _dy = dy;
1434 transform_t unrotate;
1435 transform_identity(&unrotate);
1436 transform_rotate(&unrotate, -pointer->ghost.angle);
1437 transform_apply_linear(&unrotate, &_dx, &_dy);
1438 _dx = (pointer->grab_flags & GF_MOVE_X) ? -_dx : _dx;
1439 _dy = (pointer->grab_flags & GF_MOVE_Y) ? -_dy : _dy;
1440
1441 if ((pointer->grab_flags & GF_SCALE_X) || (pointer->grab_flags & GF_RESIZE_X)) {
1442 double fx = 1.0 + (_dx / ((width - 1) * pointer->ghost.fx));
1443 pointer->ghost.fx *= fx;
1444 }
1445
1446 if ((pointer->grab_flags & GF_SCALE_Y) || (pointer->grab_flags & GF_RESIZE_Y)) {
1447 double fy = 1.0 + (_dy / ((height - 1) * pointer->ghost.fy));
1448 pointer->ghost.fy *= fy;
1449 }
1450 }
1451
1452 sysarg_t x1, y1, width1, height1;
1453 sysarg_t x2, y2, width2, height2;
1454 comp_coord_bounding_rect(0, 0, width, height, pointer->ghost.transform,
1455 &x1, &y1, &width1, &height1);
1456 comp_recalc_transform(&pointer->ghost);
1457 comp_coord_bounding_rect(0, 0, width, height, pointer->ghost.transform,
1458 &x2, &y2, &width2, &height2);
1459
1460 sysarg_t x_u, y_u, w_u, h_u;
1461 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
1462 &x_u, &y_u, &w_u, &h_u);
1463
1464 sysarg_t x_i, y_i, w_i, h_i;
1465 rectangle_intersect(x1, y1, width1, height1, x2, y2, width2, height2,
1466 &x_i, &y_i, &w_i, &h_i);
1467
1468 if (w_i == 0 || h_i == 0) {
1469 rect1->x = x_u;
1470 rect2->x = 0;
1471 rect3->x = 0;
1472 rect4->x = 0;
1473
1474 rect1->y = y_u;
1475 rect2->y = 0;
1476 rect3->y = 0;
1477 rect4->y = 0;
1478
1479 rect1->w = w_u;
1480 rect2->w = 0;
1481 rect3->w = 0;
1482 rect4->w = 0;
1483
1484 rect1->h = h_u;
1485 rect2->h = 0;
1486 rect3->h = 0;
1487 rect4->h = 0;
1488 } else {
1489 rect1->x = x_u;
1490 rect1->y = y_u;
1491 rect1->w = x_i - x_u + 1;
1492 rect1->h = h_u;
1493
1494 rect2->x = x_u;
1495 rect2->y = y_u;
1496 rect2->w = w_u;
1497 rect2->h = y_i - y_u + 1;
1498
1499 rect3->x = x_i + w_i - 1;
1500 rect3->y = y_u;
1501 rect3->w = w_u - w_i - x_i + x_u + 1;
1502 rect3->h = h_u;
1503
1504 rect4->x = x_u;
1505 rect4->y = y_i + h_i - 1;
1506 rect4->w = w_u;
1507 rect4->h = h_u - h_i - y_i + y_u + 1;
1508 }
1509}
1510#endif
1511
1512static errno_t comp_abs_move(input_t *input, unsigned x, unsigned y,
1513 unsigned max_x, unsigned max_y)
1514{
1515 /* XXX TODO Use absolute coordinates directly */
1516
1517 pointer_t *pointer = input_pointer(input);
1518
1519 sysarg_t width, height;
1520
1521 fibril_mutex_lock(&viewport_list_mtx);
1522 if (list_empty(&viewport_list)) {
1523 printf("No viewport found\n");
1524 fibril_mutex_unlock(&viewport_list_mtx);
1525 return EOK; /* XXX */
1526 }
1527 link_t *link = list_first(&viewport_list);
1528 viewport_t *vp = list_get_instance(link, viewport_t, link);
1529 surface_get_resolution(vp->surface, &width, &height);
1530 desktop_point_t vp_pos = vp->pos;
1531 fibril_mutex_unlock(&viewport_list_mtx);
1532
1533 desktop_point_t pos_in_viewport;
1534 pos_in_viewport.x = x * width / max_x;
1535 pos_in_viewport.y = y * height / max_y;
1536
1537 /* Calculate offset from pointer */
1538 fibril_mutex_lock(&pointer_list_mtx);
1539 desktop_vector_t delta;
1540 delta.x = (vp_pos.x + pos_in_viewport.x) - pointer->pos.x;
1541 delta.y = (vp_pos.y + pos_in_viewport.y) - pointer->pos.y;
1542 fibril_mutex_unlock(&pointer_list_mtx);
1543
1544 return comp_mouse_move(input, delta.x, delta.y);
1545}
1546
1547static errno_t comp_mouse_move(input_t *input, int dx, int dy)
1548{
1549 pointer_t *pointer = input_pointer(input);
1550
1551 comp_update_viewport_bound_rect();
1552
1553 /* Update pointer position. */
1554 fibril_mutex_lock(&pointer_list_mtx);
1555
1556 desktop_point_t old_pos = pointer->pos;
1557
1558 sysarg_t cursor_width;
1559 sysarg_t cursor_height;
1560 surface_get_resolution(pointer->cursor.states[pointer->state],
1561 &cursor_width, &cursor_height);
1562
1563 if (pointer->pos.x + dx < viewport_bound_rect.x)
1564 dx = -1 * (pointer->pos.x - viewport_bound_rect.x);
1565
1566 if (pointer->pos.y + dy < viewport_bound_rect.y)
1567 dy = -1 * (pointer->pos.y - viewport_bound_rect.y);
1568
1569 if (pointer->pos.x + dx > viewport_bound_rect.x + viewport_bound_rect.w)
1570 dx = (viewport_bound_rect.x + viewport_bound_rect.w - pointer->pos.x);
1571
1572 if (pointer->pos.y + dy > viewport_bound_rect.y + viewport_bound_rect.h)
1573 dy = (viewport_bound_rect.y + viewport_bound_rect.h - pointer->pos.y);
1574
1575 pointer->pos.x += dx;
1576 pointer->pos.y += dy;
1577 fibril_mutex_unlock(&pointer_list_mtx);
1578 comp_damage(old_pos.x, old_pos.y, cursor_width, cursor_height);
1579 comp_damage(old_pos.x + dx, old_pos.y + dy, cursor_width, cursor_height);
1580
1581 fibril_mutex_lock(&window_list_mtx);
1582 fibril_mutex_lock(&pointer_list_mtx);
1583 window_t *top = (window_t *) list_first(&window_list);
1584 if (top && top->surface) {
1585
1586 if (pointer->grab_flags == GF_EMPTY) {
1587 /* Notify top-level window about move event. */
1588 bool within_client = false;
1589 sysarg_t point_x, point_y;
1590 sysarg_t width, height;
1591 surface_get_resolution(top->surface, &width, &height);
1592 within_client = comp_coord_to_client(pointer->pos.x, pointer->pos.y,
1593 top->transform, width, height, &point_x, &point_y);
1594
1595 window_event_t *event = NULL;
1596 if (within_client) {
1597 event = (window_event_t *) malloc(sizeof(window_event_t));
1598 if (event) {
1599 link_initialize(&event->link);
1600 event->type = ET_POSITION_EVENT;
1601 event->data.pos.pos_id = pointer->id;
1602 event->data.pos.type = POS_UPDATE;
1603 event->data.pos.btn_num = pointer->btn_num;
1604 event->data.pos.hpos = point_x;
1605 event->data.pos.vpos = point_y;
1606 }
1607 }
1608
1609 fibril_mutex_unlock(&pointer_list_mtx);
1610 fibril_mutex_unlock(&window_list_mtx);
1611
1612 if (event) {
1613 comp_post_event_top(event);
1614 }
1615
1616 } else {
1617 /* Pointer is grabbed by top-level window action. */
1618 pointer->accum.x += dx;
1619 pointer->accum.y += dy;
1620 pointer->accum_ghost.x += dx;
1621 pointer->accum_ghost.y += dy;
1622#if ANIMATE_WINDOW_TRANSFORMS == 0
1623 if (pointer->ghost.surface == NULL) {
1624 pointer->ghost.surface = top->surface;
1625 pointer->ghost.dx = top->dx;
1626 pointer->ghost.dy = top->dy;
1627 pointer->ghost.fx = top->fx;
1628 pointer->ghost.fy = top->fy;
1629 pointer->ghost.angle = top->angle;
1630 pointer->ghost.transform = top->transform;
1631 }
1632 desktop_rect_t dmg_rect1, dmg_rect2, dmg_rect3, dmg_rect4;
1633 comp_ghost_animate(pointer, &dmg_rect1, &dmg_rect2, &dmg_rect3, &dmg_rect4);
1634#endif
1635#if ANIMATE_WINDOW_TRANSFORMS == 1
1636 sysarg_t x, y, width, height;
1637 comp_window_animate(pointer, top, &x, &y, &width, &height);
1638#endif
1639 fibril_mutex_unlock(&pointer_list_mtx);
1640 fibril_mutex_unlock(&window_list_mtx);
1641#if ANIMATE_WINDOW_TRANSFORMS == 0
1642 comp_damage(dmg_rect1.x, dmg_rect1.y, dmg_rect1.w, dmg_rect1.h);
1643 comp_damage(dmg_rect2.x, dmg_rect2.y, dmg_rect2.w, dmg_rect2.h);
1644 comp_damage(dmg_rect3.x, dmg_rect3.y, dmg_rect3.w, dmg_rect3.h);
1645 comp_damage(dmg_rect4.x, dmg_rect4.y, dmg_rect4.w, dmg_rect4.h);
1646#endif
1647#if ANIMATE_WINDOW_TRANSFORMS == 1
1648 comp_damage(x, y, width, height);
1649#endif
1650 }
1651 } else {
1652 fibril_mutex_unlock(&pointer_list_mtx);
1653 fibril_mutex_unlock(&window_list_mtx);
1654 }
1655
1656 return EOK;
1657}
1658
1659static errno_t comp_mouse_button(input_t *input, int bnum, int bpress)
1660{
1661 pointer_t *pointer = input_pointer(input);
1662
1663 fibril_mutex_lock(&window_list_mtx);
1664 fibril_mutex_lock(&pointer_list_mtx);
1665 window_t *win = NULL;
1666 sysarg_t point_x = 0;
1667 sysarg_t point_y = 0;
1668 sysarg_t width, height;
1669 bool within_client = false;
1670
1671 /* Determine the window which the mouse click belongs to. */
1672 list_foreach(window_list, link, window_t, cw) {
1673 win = cw;
1674 if (win->surface) {
1675 surface_get_resolution(win->surface, &width, &height);
1676 within_client = comp_coord_to_client(pointer->pos.x, pointer->pos.y,
1677 win->transform, width, height, &point_x, &point_y);
1678 }
1679 if (within_client) {
1680 break;
1681 }
1682 }
1683
1684 /* Check whether the window is top-level window. */
1685 window_t *top = (window_t *) list_first(&window_list);
1686 if (!win || !top) {
1687 fibril_mutex_unlock(&pointer_list_mtx);
1688 fibril_mutex_unlock(&window_list_mtx);
1689 return EOK;
1690 }
1691
1692 window_event_t *event_top = NULL;
1693 window_event_t *event_unfocus = NULL;
1694 window_t *win_unfocus = NULL;
1695 sysarg_t dmg_x, dmg_y;
1696 sysarg_t dmg_width = 0;
1697 sysarg_t dmg_height = 0;
1698
1699#if ANIMATE_WINDOW_TRANSFORMS == 0
1700 desktop_rect_t dmg_rect1, dmg_rect2, dmg_rect3, dmg_rect4;
1701#endif
1702
1703 if (bpress) {
1704 pointer->btn_pos = pointer->pos;
1705 pointer->btn_num = bnum;
1706 pointer->pressed = true;
1707
1708 /* Bring the window to the foreground. */
1709 if ((win != top) && within_client) {
1710 win_unfocus = (window_t *) list_first(&window_list);
1711 list_remove(&win->link);
1712 list_prepend(&win->link, &window_list);
1713 event_unfocus = (window_event_t *) malloc(sizeof(window_event_t));
1714 if (event_unfocus) {
1715 link_initialize(&event_unfocus->link);
1716 event_unfocus->type = ET_WINDOW_UNFOCUS;
1717 }
1718 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1719 &dmg_x, &dmg_y, &dmg_width, &dmg_height);
1720 }
1721
1722 /* Notify top-level window about mouse press. */
1723 if (within_client) {
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_POSITION_EVENT;
1728 event_top->data.pos.pos_id = pointer->id;
1729 event_top->data.pos.type = POS_PRESS;
1730 event_top->data.pos.btn_num = bnum;
1731 event_top->data.pos.hpos = point_x;
1732 event_top->data.pos.vpos = point_y;
1733 }
1734 pointer->grab_flags = GF_EMPTY;
1735 }
1736
1737 } else if (pointer->pressed && pointer->btn_num == (unsigned)bnum) {
1738 pointer->pressed = false;
1739
1740#if ANIMATE_WINDOW_TRANSFORMS == 0
1741 sysarg_t pre_x = 0;
1742 sysarg_t pre_y = 0;
1743 sysarg_t pre_width = 0;
1744 sysarg_t pre_height = 0;
1745
1746 if (pointer->grab_flags != GF_EMPTY) {
1747 if (pointer->ghost.surface) {
1748 comp_ghost_animate(pointer, &dmg_rect1, &dmg_rect2, &dmg_rect3, &dmg_rect4);
1749 pointer->ghost.surface = NULL;
1750 }
1751 comp_window_animate(pointer, top, &pre_x, &pre_y, &pre_width, &pre_height);
1752 dmg_x = pre_x;
1753 dmg_y = pre_y;
1754 dmg_width = pre_width;
1755 dmg_height = pre_height;
1756 }
1757#endif
1758
1759 if ((pointer->grab_flags & GF_RESIZE_X) || (pointer->grab_flags & GF_RESIZE_Y)) {
1760
1761 surface_get_resolution(top->surface, &width, &height);
1762#if ANIMATE_WINDOW_TRANSFORMS == 1
1763 top->fx *= (1.0 / scale_back_x);
1764 top->fy *= (1.0 / scale_back_y);
1765 comp_recalc_transform(top);
1766#endif
1767
1768 /* Commit proper resize action. */
1769 event_top = (window_event_t *) malloc(sizeof(window_event_t));
1770 if (event_top) {
1771 link_initialize(&event_top->link);
1772 event_top->type = ET_WINDOW_RESIZE;
1773
1774 event_top->data.resize.offset_x = 0;
1775 event_top->data.resize.offset_y = 0;
1776
1777 int dx = (int) (((double) width) * (scale_back_x - 1.0));
1778 int dy = (int) (((double) height) * (scale_back_y - 1.0));
1779
1780 if (pointer->grab_flags & GF_RESIZE_X)
1781 event_top->data.resize.width =
1782 ((((int) width) + dx) >= 0) ? (width + dx) : 0;
1783 else
1784 event_top->data.resize.width = width;
1785
1786 if (pointer->grab_flags & GF_RESIZE_Y)
1787 event_top->data.resize.height =
1788 ((((int) height) + dy) >= 0) ? (height + dy) : 0;
1789 else
1790 event_top->data.resize.height = height;
1791
1792 event_top->data.resize.placement_flags =
1793 WINDOW_PLACEMENT_ANY;
1794 }
1795
1796 pointer->grab_flags = GF_EMPTY;
1797
1798 } else if (within_client && (pointer->grab_flags == GF_EMPTY) && (top == win)) {
1799
1800 /* Notify top-level window about mouse release. */
1801 event_top = (window_event_t *) malloc(sizeof(window_event_t));
1802 if (event_top) {
1803 link_initialize(&event_top->link);
1804 event_top->type = ET_POSITION_EVENT;
1805 event_top->data.pos.pos_id = pointer->id;
1806 event_top->data.pos.type = POS_RELEASE;
1807 event_top->data.pos.btn_num = bnum;
1808 event_top->data.pos.hpos = point_x;
1809 event_top->data.pos.vpos = point_y;
1810 }
1811 pointer->grab_flags = GF_EMPTY;
1812
1813 } else {
1814 pointer->grab_flags = GF_EMPTY;
1815 }
1816
1817 }
1818
1819 fibril_mutex_unlock(&pointer_list_mtx);
1820 fibril_mutex_unlock(&window_list_mtx);
1821
1822#if ANIMATE_WINDOW_TRANSFORMS == 0
1823 comp_damage(dmg_rect1.x, dmg_rect1.y, dmg_rect1.w, dmg_rect1.h);
1824 comp_damage(dmg_rect2.x, dmg_rect2.y, dmg_rect2.w, dmg_rect2.h);
1825 comp_damage(dmg_rect3.x, dmg_rect3.y, dmg_rect3.w, dmg_rect3.h);
1826 comp_damage(dmg_rect4.x, dmg_rect4.y, dmg_rect4.w, dmg_rect4.h);
1827#endif
1828
1829 if (dmg_width > 0 && dmg_height > 0) {
1830 comp_damage(dmg_x, dmg_y, dmg_width, dmg_height);
1831 }
1832
1833 if (event_unfocus && win_unfocus) {
1834 comp_post_event_win(event_unfocus, win_unfocus);
1835 }
1836
1837 if (event_top) {
1838 comp_post_event_top(event_top);
1839 }
1840
1841 return EOK;
1842}
1843
1844static errno_t comp_active(input_t *input)
1845{
1846 active = true;
1847 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
1848
1849 return EOK;
1850}
1851
1852static errno_t comp_deactive(input_t *input)
1853{
1854 active = false;
1855 return EOK;
1856}
1857
1858static errno_t comp_key_press(input_t *input, kbd_event_type_t type, keycode_t key,
1859 keymod_t mods, wchar_t c)
1860{
1861 bool win_transform = (mods & KM_ALT) &&
1862 (key == KC_W || key == KC_S || key == KC_A || key == KC_D ||
1863 key == KC_Q || key == KC_E || key == KC_R || key == KC_F);
1864 bool win_resize = (mods & KM_ALT) &&
1865 (key == KC_T || key == KC_G || key == KC_B || key == KC_N);
1866 bool win_opacity = (mods & KM_ALT) && (key == KC_C || key == KC_V);
1867 bool win_close = (mods & KM_ALT) && (key == KC_X);
1868 bool win_switch = (mods & KM_ALT) && (key == KC_TAB);
1869 bool viewport_move = (mods & KM_ALT) &&
1870 (key == KC_I || key == KC_K || key == KC_J || key == KC_L);
1871 bool viewport_change = (mods & KM_ALT) && (key == KC_O || key == KC_P);
1872 bool kconsole_switch = (key == KC_PAUSE) || (key == KC_BREAK);
1873 bool filter_switch = (mods & KM_ALT) && (key == KC_Y);
1874
1875 bool key_filter = (type == KEY_RELEASE) && (win_transform || win_resize ||
1876 win_opacity || win_close || win_switch || viewport_move ||
1877 viewport_change || kconsole_switch || filter_switch);
1878
1879 if (key_filter) {
1880 /* no-op */
1881 } else if (win_transform) {
1882 fibril_mutex_lock(&window_list_mtx);
1883 window_t *win = (window_t *) list_first(&window_list);
1884 if (win && win->surface) {
1885 switch (key) {
1886 case KC_W:
1887 win->dy += -20;
1888 break;
1889 case KC_S:
1890 win->dy += 20;
1891 break;
1892 case KC_A:
1893 win->dx += -20;
1894 break;
1895 case KC_D:
1896 win->dx += 20;
1897 break;
1898 case KC_Q:
1899 win->angle += 0.1;
1900 break;
1901 case KC_E:
1902 win->angle -= 0.1;
1903 break;
1904 case KC_R:
1905 win->fx *= 0.95;
1906 win->fy *= 0.95;
1907 break;
1908 case KC_F:
1909 win->fx *= 1.05;
1910 win->fy *= 1.05;
1911 break;
1912 default:
1913 break;
1914 }
1915
1916 /* Transform the window and calculate damage. */
1917 sysarg_t x, y, width, height;
1918 surface_get_resolution(win->surface, &width, &height);
1919 sysarg_t x1, y1, width1, height1;
1920 sysarg_t x2, y2, width2, height2;
1921 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1922 &x1, &y1, &width1, &height1);
1923 comp_recalc_transform(win);
1924 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1925 &x2, &y2, &width2, &height2);
1926 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
1927 &x, &y, &width, &height);
1928 fibril_mutex_unlock(&window_list_mtx);
1929
1930 comp_damage(x, y, width, height);
1931 } else {
1932 fibril_mutex_unlock(&window_list_mtx);
1933 }
1934 } else if (win_resize) {
1935 fibril_mutex_lock(&window_list_mtx);
1936 window_t *win = (window_t *) list_first(&window_list);
1937 if ((win) && (win->surface) && (win->flags & WINDOW_RESIZEABLE)) {
1938 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
1939 if (event == NULL) {
1940 fibril_mutex_unlock(&window_list_mtx);
1941 return ENOMEM;
1942 }
1943
1944 sysarg_t width, height;
1945 surface_get_resolution(win->surface, &width, &height);
1946
1947 link_initialize(&event->link);
1948 event->type = ET_WINDOW_RESIZE;
1949
1950 event->data.resize.offset_x = 0;
1951 event->data.resize.offset_y = 0;
1952
1953 switch (key) {
1954 case KC_T:
1955 event->data.resize.width = width;
1956 event->data.resize.height = (height >= 20) ? height - 20 : 0;
1957 break;
1958 case KC_G:
1959 event->data.resize.width = width;
1960 event->data.resize.height = height + 20;
1961 break;
1962 case KC_B:
1963 event->data.resize.width = (width >= 20) ? width - 20 : 0;
1964 event->data.resize.height = height;
1965 break;
1966 case KC_N:
1967 event->data.resize.width = width + 20;
1968 event->data.resize.height = height;
1969 break;
1970 default:
1971 event->data.resize.width = 0;
1972 event->data.resize.height = 0;
1973 break;
1974 }
1975
1976 event->data.resize.placement_flags = WINDOW_PLACEMENT_ANY;
1977
1978 fibril_mutex_unlock(&window_list_mtx);
1979 comp_post_event_top(event);
1980 } else {
1981 fibril_mutex_unlock(&window_list_mtx);
1982 }
1983 } else if (win_opacity) {
1984 fibril_mutex_lock(&window_list_mtx);
1985 window_t *win = (window_t *) list_first(&window_list);
1986 if (win && win->surface) {
1987 switch (key) {
1988 case KC_C:
1989 if (win->opacity > 0) {
1990 win->opacity -= 5;
1991 }
1992 break;
1993 case KC_V:
1994 if (win->opacity < 255) {
1995 win->opacity += 5;
1996 }
1997 break;
1998 default:
1999 break;
2000 }
2001
2002 /* Calculate damage. */
2003 sysarg_t x, y, width, height;
2004 surface_get_resolution(win->surface, &width, &height);
2005 comp_coord_bounding_rect(0, 0, width, height, win->transform,
2006 &x, &y, &width, &height);
2007 fibril_mutex_unlock(&window_list_mtx);
2008
2009 comp_damage(x, y, width, height);
2010 } else {
2011 fibril_mutex_unlock(&window_list_mtx);
2012 }
2013 } else if (win_close) {
2014 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
2015 if (event == NULL)
2016 return ENOMEM;
2017
2018 link_initialize(&event->link);
2019 event->type = ET_WINDOW_CLOSE;
2020
2021 comp_post_event_top(event);
2022 } else if (win_switch) {
2023 fibril_mutex_lock(&window_list_mtx);
2024 if (!list_empty(&window_list)) {
2025 window_t *win1 = (window_t *) list_first(&window_list);
2026 list_remove(&win1->link);
2027 list_append(&win1->link, &window_list);
2028 window_t *win2 = (window_t *) list_first(&window_list);
2029
2030 window_event_t *event1 = (window_event_t *) malloc(sizeof(window_event_t));
2031 if (event1) {
2032 link_initialize(&event1->link);
2033 event1->type = ET_WINDOW_UNFOCUS;
2034 }
2035
2036 window_event_t *event2 = (window_event_t *) malloc(sizeof(window_event_t));
2037 if (event2) {
2038 link_initialize(&event2->link);
2039 event2->type = ET_WINDOW_FOCUS;
2040 }
2041
2042 sysarg_t x1 = 0;
2043 sysarg_t y1 = 0;
2044 sysarg_t width1 = 0;
2045 sysarg_t height1 = 0;
2046 if (win1->surface) {
2047 sysarg_t width, height;
2048 surface_get_resolution(win1->surface, &width, &height);
2049 comp_coord_bounding_rect(0, 0, width, height, win1->transform,
2050 &x1, &y1, &width1, &height1);
2051 }
2052
2053 sysarg_t x2 = 0;
2054 sysarg_t y2 = 0;
2055 sysarg_t width2 = 0;
2056 sysarg_t height2 = 0;
2057 if (win2->surface) {
2058 sysarg_t width, height;
2059 surface_get_resolution(win2->surface, &width, &height);
2060 comp_coord_bounding_rect(0, 0, width, height, win2->transform,
2061 &x2, &y2, &width2, &height2);
2062 }
2063
2064 sysarg_t x, y, width, height;
2065 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
2066 &x, &y, &width, &height);
2067
2068 fibril_mutex_unlock(&window_list_mtx);
2069
2070 if (event1 && win1) {
2071 comp_post_event_win(event1, win1);
2072 }
2073
2074 if (event2 && win2) {
2075 comp_post_event_win(event2, win2);
2076 }
2077
2078 comp_damage(x, y, width, height);
2079 } else {
2080 fibril_mutex_unlock(&window_list_mtx);
2081 }
2082 } else if (viewport_move) {
2083 fibril_mutex_lock(&viewport_list_mtx);
2084 viewport_t *vp = (viewport_t *) list_first(&viewport_list);
2085 if (vp) {
2086 switch (key) {
2087 case KC_I:
2088 vp->pos.x += 0;
2089 vp->pos.y += -20;
2090 break;
2091 case KC_K:
2092 vp->pos.x += 0;
2093 vp->pos.y += 20;
2094 break;
2095 case KC_J:
2096 vp->pos.x += -20;
2097 vp->pos.y += 0;
2098 break;
2099 case KC_L:
2100 vp->pos.x += 20;
2101 vp->pos.y += 0;
2102 break;
2103 default:
2104 vp->pos.x += 0;
2105 vp->pos.y += 0;
2106 break;
2107 }
2108
2109 sysarg_t x = vp->pos.x;
2110 sysarg_t y = vp->pos.y;
2111 sysarg_t width, height;
2112 surface_get_resolution(vp->surface, &width, &height);
2113 fibril_mutex_unlock(&viewport_list_mtx);
2114
2115 comp_restrict_pointers();
2116 comp_damage(x, y, width, height);
2117 } else {
2118 fibril_mutex_unlock(&viewport_list_mtx);
2119 }
2120 } else if (viewport_change) {
2121 fibril_mutex_lock(&viewport_list_mtx);
2122
2123 viewport_t *vp;
2124 switch (key) {
2125 case KC_O:
2126 vp = (viewport_t *) list_first(&viewport_list);
2127 if (vp) {
2128 list_remove(&vp->link);
2129 list_append(&vp->link, &viewport_list);
2130 }
2131 break;
2132 case KC_P:
2133 vp = (viewport_t *) list_last(&viewport_list);
2134 if (vp) {
2135 list_remove(&vp->link);
2136 list_prepend(&vp->link, &viewport_list);
2137 }
2138 break;
2139 default:
2140 break;
2141 }
2142
2143 fibril_mutex_unlock(&viewport_list_mtx);
2144 } else if (kconsole_switch) {
2145 if (console_kcon())
2146 active = false;
2147 } else if (filter_switch) {
2148 filter_index++;
2149 if (filter_index > 1)
2150 filter_index = 0;
2151 if (filter_index == 0) {
2152 filter = filter_nearest;
2153 } else {
2154 filter = filter_bilinear;
2155 }
2156 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
2157 } else {
2158 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
2159 if (event == NULL)
2160 return ENOMEM;
2161
2162 link_initialize(&event->link);
2163 event->type = ET_KEYBOARD_EVENT;
2164 event->data.kbd.type = type;
2165 event->data.kbd.key = key;
2166 event->data.kbd.mods = mods;
2167 event->data.kbd.c = c;
2168
2169 comp_post_event_top(event);
2170 }
2171
2172 return EOK;
2173}
2174
2175static errno_t input_connect(const char *svc)
2176{
2177 async_sess_t *sess;
2178 service_id_t dsid;
2179
2180 errno_t rc = loc_service_get_id(svc, &dsid, 0);
2181 if (rc != EOK) {
2182 printf("%s: Input service %s not found\n", NAME, svc);
2183 return rc;
2184 }
2185
2186 sess = loc_service_connect(dsid, INTERFACE_INPUT, 0);
2187 if (sess == NULL) {
2188 printf("%s: Unable to connect to input service %s\n", NAME,
2189 svc);
2190 return EIO;
2191 }
2192
2193 fibril_mutex_lock(&pointer_list_mtx);
2194 pointer_t *pointer = pointer_create();
2195 if (pointer != NULL) {
2196 pointer->id = pointer_id++;
2197 list_append(&pointer->link, &pointer_list);
2198 }
2199 fibril_mutex_unlock(&pointer_list_mtx);
2200
2201 if (pointer == NULL) {
2202 printf("%s: Cannot create pointer.\n", NAME);
2203 async_hangup(sess);
2204 return ENOMEM;
2205 }
2206
2207 rc = input_open(sess, &input_ev_ops, pointer, &input);
2208 if (rc != EOK) {
2209 async_hangup(sess);
2210 printf("%s: Unable to communicate with service %s (%s)\n",
2211 NAME, svc, str_error(rc));
2212 return rc;
2213 }
2214
2215 return EOK;
2216}
2217
2218static void input_disconnect(void)
2219{
2220 pointer_t *pointer = input->user;
2221 input_close(input);
2222 pointer_destroy(pointer);
2223}
2224
2225static void discover_viewports(void)
2226{
2227 fibril_mutex_lock(&discovery_mtx);
2228
2229 /* Create viewports and connect them to visualizers. */
2230 category_id_t cat_id;
2231 errno_t rc = loc_category_get_id("visualizer", &cat_id, IPC_FLAG_BLOCKING);
2232 if (rc != EOK)
2233 goto ret;
2234
2235 service_id_t *svcs;
2236 size_t svcs_cnt = 0;
2237 rc = loc_category_get_svcs(cat_id, &svcs, &svcs_cnt);
2238 if (rc != EOK)
2239 goto ret;
2240
2241 fibril_mutex_lock(&viewport_list_mtx);
2242 for (size_t i = 0; i < svcs_cnt; ++i) {
2243 bool exists = false;
2244 list_foreach(viewport_list, link, viewport_t, vp) {
2245 if (vp->dsid == svcs[i]) {
2246 exists = true;
2247 break;
2248 }
2249 }
2250
2251 if (exists)
2252 continue;
2253
2254 viewport_t *vp = viewport_create(svcs[i]);
2255 if (vp != NULL)
2256 list_append(&vp->link, &viewport_list);
2257 }
2258 fibril_mutex_unlock(&viewport_list_mtx);
2259
2260 if (!list_empty(&viewport_list))
2261 input_activate(input);
2262
2263ret:
2264 fibril_mutex_unlock(&discovery_mtx);
2265}
2266
2267static void category_change_cb(void *arg)
2268{
2269 discover_viewports();
2270}
2271
2272static errno_t compositor_srv_init(char *input_svc, char *name)
2273{
2274 /* Coordinates of the central pixel. */
2275 coord_origin = UINT32_MAX / 4;
2276
2277 /* Color of the viewport background. Must be opaque. */
2278 bg_color = PIXEL(255, 69, 51, 103);
2279
2280 /* Register compositor server. */
2281 async_set_fallback_port_handler(client_connection, NULL);
2282
2283 errno_t rc = loc_server_register(NAME);
2284 if (rc != EOK) {
2285 printf("%s: Unable to register server (%s)\n", NAME, str_error(rc));
2286 return -1;
2287 }
2288
2289 server_name = name;
2290
2291 char svc[LOC_NAME_MAXLEN + 1];
2292 snprintf(svc, LOC_NAME_MAXLEN, "%s/%s", NAMESPACE, server_name);
2293
2294 service_id_t service_id;
2295 rc = loc_service_register(svc, &service_id);
2296 if (rc != EOK) {
2297 printf("%s: Unable to register service %s\n", NAME, svc);
2298 return rc;
2299 }
2300
2301 /* Prepare window registrator (entrypoint for clients). */
2302 char winreg[LOC_NAME_MAXLEN + 1];
2303 snprintf(winreg, LOC_NAME_MAXLEN, "%s%s/winreg", NAMESPACE, server_name);
2304 if (loc_service_register(winreg, &winreg_id) != EOK) {
2305 printf("%s: Unable to register service %s\n", NAME, winreg);
2306 return -1;
2307 }
2308
2309 /* Establish input bidirectional connection. */
2310 rc = input_connect(input_svc);
2311 if (rc != EOK) {
2312 printf("%s: Failed to connect to input service.\n", NAME);
2313 return rc;
2314 }
2315
2316 rc = loc_register_cat_change_cb(category_change_cb, NULL);
2317 if (rc != EOK) {
2318 printf("%s: Failed to register category change callback\n", NAME);
2319 input_disconnect();
2320 return rc;
2321 }
2322
2323 discover_viewports();
2324
2325 comp_restrict_pointers();
2326 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
2327
2328 return EOK;
2329}
2330
2331static void usage(char *name)
2332{
2333 printf("Usage: %s <input_dev> <server_name>\n", name);
2334}
2335
2336int main(int argc, char *argv[])
2337{
2338 if (argc < 3) {
2339 usage(argv[0]);
2340 return 1;
2341 }
2342
2343 printf("%s: HelenOS Compositor server\n", NAME);
2344
2345 errno_t rc = compositor_srv_init(argv[1], argv[2]);
2346 if (rc != EOK)
2347 return rc;
2348
2349 printf("%s: Accepting connections\n", NAME);
2350 task_retval(0);
2351 async_manager();
2352
2353 /* Never reached */
2354 return 0;
2355}
2356
2357/** @}
2358 */
Note: See TracBrowser for help on using the repository browser.