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

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

cstyle
(no change in functionality)

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