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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a1b9f63 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
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
[498ced1]35#include <assert.h>
[8d2dd7f2]36#include <stddef.h>
37#include <stdint.h>
[3e6a98c5]38#include <stdbool.h>
[6d5e378]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>
[38d150e]47#include <stdlib.h>
[6d5e378]48
[498ced1]49#include <refcount.h>
[6d5e378]50#include <fibril_synch.h>
51#include <adt/prodcons.h>
52#include <adt/list.h>
[111d2d6]53#include <io/input.h>
[6d5e378]54#include <ipc/graph.h>
55#include <ipc/window.h>
56
57#include <async.h>
58#include <loc.h>
[2f6ad06]59#include <task.h>
[6d5e378]60
61#include <io/keycode.h>
62#include <io/mode.h>
63#include <io/visualizer.h>
64#include <io/window.h>
[593e023]65#include <io/console.h>
[6d5e378]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
[7c3fb9b]80/*
81 * Until there is blitter support and some further optimizations, window
82 * animations are too slow to be practically usable.
83 */
[6d5e378]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;
[8d3512f1]91static filter_t filter = filter_bilinear;
92static unsigned int filter_index = 1;
[6d5e378]93
94typedef struct {
95 link_t link;
[498ced1]96 atomic_refcount_t ref_cnt;
[2c7fdaa]97 window_flags_t flags;
[6d5e378]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
[563573b]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
[6d5e378]137typedef struct {
138 link_t link;
139 service_id_t dsid;
140 vslmode_t mode;
141 async_sess_t *sess;
[b5416c3]142 desktop_point_t pos;
[6d5e378]143 surface_t *surface;
144} viewport_t;
145
[3b98311]146static desktop_rect_t viewport_bound_rect;
[6d5e378]147static FIBRIL_MUTEX_INITIALIZE(viewport_list_mtx);
148static LIST_INITIALIZE(viewport_list);
149
[d08ba7fc]150static FIBRIL_MUTEX_INITIALIZE(discovery_mtx);
151
[111d2d6]152/** Input server proxy */
153static input_t *input;
[593e023]154static bool active = false;
[111d2d6]155
[b7fd2a0]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);
[111d2d6]162
163static input_ev_ops_t input_ev_ops = {
[593e023]164 .active = comp_active,
165 .deactive = comp_deactive,
[111d2d6]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}
[6d5e378]176
[62fbb7e]177static pointer_t *pointer_create(void)
[6d5e378]178{
179 pointer_t *p = (pointer_t *) malloc(sizeof(pointer_t));
[ba02baa]180 if (!p)
[6d5e378]181 return NULL;
[a35b458]182
[6d5e378]183 link_initialize(&p->link);
[b5416c3]184 p->pos.x = coord_origin;
185 p->pos.y = coord_origin;
[6d5e378]186 p->btn_num = 1;
[b5416c3]187 p->btn_pos = p->pos;
188 p->accum.x = 0;
189 p->accum.y = 0;
[6d5e378]190 p->grab_flags = GF_EMPTY;
191 p->pressed = false;
192 p->state = 0;
193 cursor_init(&p->cursor, CURSOR_DECODER_EMBEDDED, NULL);
[a35b458]194
[563573b]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;
[a35b458]207
[6d5e378]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
[62fbb7e]219static window_t *window_create(void)
[6d5e378]220{
221 window_t *win = (window_t *) malloc(sizeof(window_t));
[ba02baa]222 if (!win)
[6d5e378]223 return NULL;
[a35b458]224
[6d5e378]225 link_initialize(&win->link);
[498ced1]226 refcount_init(&win->ref_cnt);
[6d5e378]227 prodcons_initialize(&win->queue);
228 transform_identity(&win->transform);
[62fbb7e]229 transform_translate(&win->transform, coord_origin, coord_origin);
230 win->dx = coord_origin;
231 win->dy = coord_origin;
[6d5e378]232 win->fx = 1;
233 win->fy = 1;
234 win->angle = 0;
235 win->opacity = 255;
236 win->surface = NULL;
[a35b458]237
[6d5e378]238 return win;
239}
240
241static void window_destroy(window_t *win)
242{
[498ced1]243 if (!win || !refcount_down(&win->ref_cnt))
244 return;
[a35b458]245
[498ced1]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);
[6d5e378]250 }
[498ced1]251
252 if (win->surface)
253 surface_destroy(win->surface);
254
255 free(win);
[6d5e378]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);
[a35b458]265
[ce3efa0]266 /*
267 * Since client coordinate origin is (0, 0), it is necessary to check
[6d5e378]268 * coordinates to avoid underflow. Moreover, it is convenient to also
269 * check against provided upper limits to determine whether the converted
[ce3efa0]270 * coordinates are within the client window.
271 */
272 if ((x < 0) || (y < 0))
[6d5e378]273 return false;
[a35b458]274
[ce3efa0]275 (*x_out) = (sysarg_t) (x + 0.5);
276 (*y_out) = (sysarg_t) (y + 0.5);
[a35b458]277
[ce3efa0]278 if (((*x_out) >= x_lim) || ((*y_out) >= y_lim))
279 return false;
[a35b458]280
[ce3efa0]281 return true;
[6d5e378]282}
283
[e80d8f8]284static void comp_coord_from_client(double x_in, double y_in, transform_t win_trans,
[6d5e378]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);
[a35b458]290
[ce3efa0]291 /*
292 * It is assumed that compositor coordinate origin is chosen in such way,
293 * that underflow/overflow here would be unlikely.
294 */
[6d5e378]295 (*x_out) = (sysarg_t) (x + 0.5);
296 (*y_out) = (sysarg_t) (y + 0.5);
297}
298
[e80d8f8]299static void comp_coord_bounding_rect(double x_in, double y_in,
300 double w_in, double h_in, transform_t win_trans,
[6d5e378]301 sysarg_t *x_out, sysarg_t *y_out, sysarg_t *w_out, sysarg_t *h_out)
302{
[dace86a]303 if ((w_in > 0) && (h_in > 0)) {
[03f0b02]304 sysarg_t x[4];
305 sysarg_t y[4];
[a35b458]306
[03f0b02]307 comp_coord_from_client(x_in, y_in, win_trans, &x[0], &y[0]);
[c8e2ac5]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]);
[a35b458]311
[03f0b02]312 (*x_out) = x[0];
313 (*y_out) = y[0];
314 (*w_out) = x[0];
315 (*h_out) = y[0];
[a35b458]316
[dace86a]317 for (unsigned int i = 1; i < 4; ++i) {
[03f0b02]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 }
[a35b458]323
[03f0b02]324 (*w_out) = (*w_out) - (*x_out) + 1;
325 (*h_out) = (*h_out) - (*y_out) + 1;
326 } else {
[e80d8f8]327 (*x_out) = 0;
328 (*y_out) = 0;
[03f0b02]329 (*w_out) = 0;
330 (*h_out) = 0;
[6d5e378]331 }
332}
333
[62fbb7e]334static void comp_update_viewport_bound_rect(void)
[3b98311]335{
336 fibril_mutex_lock(&viewport_list_mtx);
[a35b458]337
[3b98311]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;
[a35b458]342
[3b98311]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 }
[a35b458]349
[feeac0d]350 list_foreach(viewport_list, link, viewport_t, vp) {
[3b98311]351 sysarg_t w_vp, h_vp;
352 surface_get_resolution(vp->surface, &w_vp, &h_vp);
[62fbb7e]353 rectangle_union(x_res, y_res, w_res, h_res,
[3b98311]354 vp->pos.x, vp->pos.y, w_vp, h_vp,
355 &x_res, &y_res, &w_res, &h_res);
356 }
[a35b458]357
[3b98311]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;
[a35b458]362
[3b98311]363 fibril_mutex_unlock(&viewport_list_mtx);
[62fbb7e]364}
[3b98311]365
[62fbb7e]366static void comp_restrict_pointers(void)
367{
368 comp_update_viewport_bound_rect();
[a35b458]369
[3b98311]370 fibril_mutex_lock(&pointer_list_mtx);
[a35b458]371
[feeac0d]372 list_foreach(pointer_list, link, pointer_t, ptr) {
[3b98311]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 }
[a35b458]380
[3b98311]381 fibril_mutex_unlock(&pointer_list_mtx);
382}
383
[6d5e378]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
[feeac0d]391 list_foreach(viewport_list, link, viewport_t, vp) {
[6d5e378]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,
[b5416c3]397 vp->pos.x, vp->pos.y, w_dmg_vp, h_dmg_vp,
[6d5e378]398 &x_dmg_vp, &y_dmg_vp, &w_dmg_vp, &h_dmg_vp);
399
400 if (isec_vp) {
401
402 /* Paint background color. */
[b5416c3]403 for (sysarg_t y = y_dmg_vp - vp->pos.y; y < y_dmg_vp - vp->pos.y + h_dmg_vp; ++y) {
[7dba813]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;
[6d5e378]409 }
410 }
[7dba813]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);
[6d5e378]413
414 transform_t transform;
415 source_t source;
416 drawctx_t context;
417
418 source_init(&source);
[8d3512f1]419 source_set_filter(&source, filter);
[6d5e378]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
[7c3fb9b]428 /*
429 * Determine what part of the window intersects with the
430 * updated area of the current viewport.
431 */
[6d5e378]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) {
[7c3fb9b]446 /*
447 * Prepare conversion from global coordinates to viewport
448 * coordinates.
449 */
[6d5e378]450 transform = win->transform;
[b5416c3]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);
[6d5e378]455
[ce3efa0]456 source_set_transform(&source, transform);
[00ddb40]457 source_set_texture(&source, win->surface,
458 PIXELMAP_EXTEND_TRANSPARENT_SIDES);
[6d5e378]459 source_set_alpha(&source, PIXEL(win->opacity, 0, 0, 0));
460
461 drawctx_transfer(&context,
[b5416c3]462 x_dmg_win - vp->pos.x, y_dmg_win - vp->pos.y, w_dmg_win, h_dmg_win);
[6d5e378]463 }
464 }
465
[feeac0d]466 list_foreach(pointer_list, link, pointer_t, ptr) {
[563573b]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) {
[7c3fb9b]480 /*
481 * FIXME: Ghost is currently drawn based on the bounding
[563573b]482 * rectangle of the window, which is sufficient as long
[523b17a]483 * as the windows can be rotated only by 90 degrees.
[563573b]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
[7c3fb9b]487 * require to equip libdraw with line drawing functionality.
488 */
[563573b]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;
[18b6a88]500 x < x_dmg_ghost - vp->pos.x + w_dmg_ghost; ++x) {
[563573b]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;
[18b6a88]510 x < x_dmg_ghost - vp->pos.x + w_dmg_ghost; ++x) {
[563573b]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;
[18b6a88]520 y < y_dmg_ghost - vp->pos.y + h_dmg_ghost; ++y) {
[563573b]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;
[18b6a88]530 y < y_dmg_ghost - vp->pos.y + h_dmg_ghost; ++y) {
[563573b]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
[feeac0d]542 list_foreach(pointer_list, link, pointer_t, ptr) {
[6d5e378]543
[7c3fb9b]544 /*
545 * Determine what part of the pointer intersects with the
546 * updated area of the current viewport.
547 */
[6d5e378]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,
[b5416c3]553 ptr->pos.x, ptr->pos.y, w_dmg_ptr, h_dmg_ptr,
[6d5e378]554 &x_dmg_ptr, &y_dmg_ptr, &w_dmg_ptr, &h_dmg_ptr);
555
556 if (isec_ptr) {
[7c3fb9b]557 /*
558 * Pointer is currently painted directly by copying pixels.
[7dba813]559 * However, it is possible to draw the pointer similarly
[6d5e378]560 * as window by using drawctx_transfer. It would allow
561 * more sophisticated control over drawing, but would also
[7c3fb9b]562 * cost more regarding the performance.
563 */
[6d5e378]564
[b5416c3]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;
[6d5e378]569
570 for (sysarg_t y = 0; y < h_dmg_ptr; ++y) {
[7dba813]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;
[18b6a88]578 ++dst;
579 ++src;
[6d5e378]580 }
581 }
[7dba813]582 surface_add_damaged_region(vp->surface, x_vp, y_vp, w_dmg_ptr, h_dmg_ptr);
[6d5e378]583 }
[563573b]584
[6d5e378]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. */
[593e023]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 }
[6d5e378]601 }
[a35b458]602
[6d5e378]603 fibril_mutex_unlock(&viewport_list_mtx);
604}
605
[984a9ba]606static void comp_window_get_event(window_t *win, ipc_call_t *icall)
[6d5e378]607{
608 window_event_t *event = (window_event_t *) prodcons_consume(&win->queue);
609
[984a9ba]610 ipc_call_t call;
[6d5e378]611 size_t len;
612
[984a9ba]613 if (!async_data_read_receive(&call, &len)) {
614 async_answer_0(icall, EINVAL);
[6d5e378]615 free(event);
616 return;
617 }
[a35b458]618
[984a9ba]619 errno_t rc = async_data_read_finalize(&call, event, len);
[6d5e378]620 if (rc != EOK) {
[984a9ba]621 async_answer_0(icall, ENOMEM);
[6d5e378]622 free(event);
623 return;
624 }
[a35b458]625
[984a9ba]626 async_answer_0(icall, EOK);
[6d5e378]627 free(event);
628}
629
[984a9ba]630static void comp_window_damage(window_t *win, ipc_call_t *icall)
[6d5e378]631{
[e80d8f8]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);
[6d5e378]636
[ce3efa0]637 if ((width == 0) || (height == 0)) {
[6d5e378]638 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
639 } else {
640 fibril_mutex_lock(&window_list_mtx);
[e80d8f8]641 sysarg_t x_dmg_glob, y_dmg_glob, w_dmg_glob, h_dmg_glob;
[c8e2ac5]642 comp_coord_bounding_rect(x - 1, y - 1, width + 2, height + 2,
[e80d8f8]643 win->transform, &x_dmg_glob, &y_dmg_glob, &w_dmg_glob, &h_dmg_glob);
[6d5e378]644 fibril_mutex_unlock(&window_list_mtx);
[e80d8f8]645 comp_damage(x_dmg_glob, y_dmg_glob, w_dmg_glob, h_dmg_glob);
[6d5e378]646 }
647
[984a9ba]648 async_answer_0(icall, EOK);
[6d5e378]649}
650
[984a9ba]651static void comp_window_grab(window_t *win, ipc_call_t *icall)
[6d5e378]652{
653 sysarg_t pos_id = IPC_GET_ARG1(*icall);
654 sysarg_t grab_flags = IPC_GET_ARG2(*icall);
[a35b458]655
[2c7fdaa]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);
[6d5e378]662
663 fibril_mutex_lock(&pointer_list_mtx);
[feeac0d]664 list_foreach(pointer_list, link, pointer_t, pointer) {
[6d5e378]665 if (pointer->id == pos_id) {
[290a0f0]666 pointer->grab_flags = pointer->pressed ? grab_flags : GF_EMPTY;
[6d5e378]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
[984a9ba]678 async_answer_0(icall, EOK);
[6d5e378]679}
680
[62fbb7e]681static void comp_recalc_transform(window_t *win)
[6d5e378]682{
[62fbb7e]683 transform_t translate;
684 transform_identity(&translate);
685 transform_translate(&translate, win->dx, win->dy);
[a35b458]686
[62fbb7e]687 transform_t scale;
688 transform_identity(&scale);
689 if ((win->fx != 1) || (win->fy != 1))
690 transform_scale(&scale, win->fx, win->fy);
[a35b458]691
[62fbb7e]692 transform_t rotate;
693 transform_identity(&rotate);
694 if (win->angle != 0)
695 transform_rotate(&rotate, win->angle);
[a35b458]696
[62fbb7e]697 transform_t transform;
698 transform_t temp;
699 transform_identity(&transform);
700 temp = transform;
[071fefec]701 transform_product(&transform, &temp, &translate);
[62fbb7e]702 temp = transform;
[071fefec]703 transform_product(&transform, &temp, &rotate);
[62fbb7e]704 temp = transform;
[071fefec]705 transform_product(&transform, &temp, &scale);
[a35b458]706
[62fbb7e]707 win->transform = transform;
708}
[6d5e378]709
[984a9ba]710static void comp_window_resize(window_t *win, ipc_call_t *icall)
[6d5e378]711{
[984a9ba]712 ipc_call_t call;
[6d5e378]713 size_t size;
714 unsigned int flags;
[a35b458]715
[6d5e378]716 /* Start sharing resized window with client. */
[984a9ba]717 if (!async_share_out_receive(&call, &size, &flags)) {
718 async_answer_0(icall, EINVAL);
[6d5e378]719 return;
720 }
[a35b458]721
[6d5e378]722 void *new_cell_storage;
[984a9ba]723 errno_t rc = async_share_out_finalize(&call, &new_cell_storage);
[6d5e378]724 if ((rc != EOK) || (new_cell_storage == AS_MAP_FAILED)) {
[984a9ba]725 async_answer_0(icall, ENOMEM);
[6d5e378]726 return;
727 }
[a35b458]728
[6d5e378]729 /* Create new surface for the resized window. */
[62fbb7e]730 surface_t *new_surface = surface_create(IPC_GET_ARG3(*icall),
731 IPC_GET_ARG4(*icall), new_cell_storage, SURFACE_FLAG_SHARED);
[6d5e378]732 if (!new_surface) {
733 as_area_destroy(new_cell_storage);
[984a9ba]734 async_answer_0(icall, ENOMEM);
[6d5e378]735 return;
736 }
[a35b458]737
[62fbb7e]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);
[a35b458]742
[62fbb7e]743 comp_update_viewport_bound_rect();
[a35b458]744
[6d5e378]745 /* Switch new surface with old surface and calculate damage. */
746 fibril_mutex_lock(&window_list_mtx);
[a35b458]747
[6d5e378]748 sysarg_t old_width = 0;
749 sysarg_t old_height = 0;
[a35b458]750
[6d5e378]751 if (win->surface) {
752 surface_get_resolution(win->surface, &old_width, &old_height);
753 surface_destroy(win->surface);
754 }
[a35b458]755
[6d5e378]756 win->surface = new_surface;
[a35b458]757
[6d5e378]758 sysarg_t new_width = 0;
759 sysarg_t new_height = 0;
760 surface_get_resolution(win->surface, &new_width, &new_height);
[a35b458]761
[62fbb7e]762 if (placement_flags & WINDOW_PLACEMENT_CENTER_X)
763 win->dx = viewport_bound_rect.x + viewport_bound_rect.w / 2 -
764 new_width / 2;
[a35b458]765
[62fbb7e]766 if (placement_flags & WINDOW_PLACEMENT_CENTER_Y)
767 win->dy = viewport_bound_rect.y + viewport_bound_rect.h / 2 -
768 new_height / 2;
[a35b458]769
[62fbb7e]770 if (placement_flags & WINDOW_PLACEMENT_LEFT)
771 win->dx = viewport_bound_rect.x;
[a35b458]772
[62fbb7e]773 if (placement_flags & WINDOW_PLACEMENT_TOP)
774 win->dy = viewport_bound_rect.y;
[a35b458]775
[62fbb7e]776 if (placement_flags & WINDOW_PLACEMENT_RIGHT)
777 win->dx = viewport_bound_rect.x + viewport_bound_rect.w -
778 new_width;
[a35b458]779
[62fbb7e]780 if (placement_flags & WINDOW_PLACEMENT_BOTTOM)
781 win->dy = viewport_bound_rect.y + viewport_bound_rect.h -
782 new_height;
[a35b458]783
[62fbb7e]784 if (placement_flags & WINDOW_PLACEMENT_ABSOLUTE_X)
785 win->dx = coord_origin + offset_x;
[a35b458]786
[62fbb7e]787 if (placement_flags & WINDOW_PLACEMENT_ABSOLUTE_Y)
788 win->dy = coord_origin + offset_y;
[a35b458]789
[62fbb7e]790 /* Transform the window and calculate damage. */
791 sysarg_t x1;
792 sysarg_t y1;
793 sysarg_t width1;
794 sysarg_t height1;
[a35b458]795
[62fbb7e]796 comp_coord_bounding_rect(0, 0, old_width, old_height, win->transform,
797 &x1, &y1, &width1, &height1);
[a35b458]798
[62fbb7e]799 comp_recalc_transform(win);
[a35b458]800
[62fbb7e]801 sysarg_t x2;
802 sysarg_t y2;
803 sysarg_t width2;
804 sysarg_t height2;
[a35b458]805
[62fbb7e]806 comp_coord_bounding_rect(0, 0, new_width, new_height, win->transform,
807 &x2, &y2, &width2, &height2);
[a35b458]808
[dace86a]809 sysarg_t x;
810 sysarg_t y;
[62fbb7e]811 sysarg_t width;
812 sysarg_t height;
[a35b458]813
[62fbb7e]814 rectangle_union(x1, y1, width1, height1, x2, y2, width2, height2,
815 &x, &y, &width, &height);
[a35b458]816
[6d5e378]817 fibril_mutex_unlock(&window_list_mtx);
[a35b458]818
[6d5e378]819 comp_damage(x, y, width, height);
[a35b458]820
[984a9ba]821 async_answer_0(icall, EOK);
[6d5e378]822}
823
[290a0f0]824static void comp_post_event_win(window_event_t *event, window_t *target)
825{
826 fibril_mutex_lock(&window_list_mtx);
[a35b458]827
[feeac0d]828 list_foreach(window_list, link, window_t, window) {
[290a0f0]829 if (window == target) {
830 prodcons_produce(&window->queue, &event->link);
[feeac0d]831 fibril_mutex_unlock(&window_list_mtx);
832 return;
[290a0f0]833 }
834 }
[a35b458]835
[290a0f0]836 fibril_mutex_unlock(&window_list_mtx);
[feeac0d]837 free(event);
[290a0f0]838}
839
840static void comp_post_event_top(window_event_t *event)
841{
842 fibril_mutex_lock(&window_list_mtx);
[a35b458]843
[290a0f0]844 window_t *win = (window_t *) list_first(&window_list);
[ba02baa]845 if (win)
[290a0f0]846 prodcons_produce(&win->queue, &event->link);
[ba02baa]847 else
[290a0f0]848 free(event);
[a35b458]849
[290a0f0]850 fibril_mutex_unlock(&window_list_mtx);
851}
852
[984a9ba]853static void comp_window_close(window_t *win, ipc_call_t *icall)
[6d5e378]854{
855 /* Stop managing the window. */
856 fibril_mutex_lock(&window_list_mtx);
857 list_remove(&win->link);
[290a0f0]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 }
[6d5e378]864 fibril_mutex_unlock(&window_list_mtx);
865
[290a0f0]866 if (event_focus && win_focus) {
867 comp_post_event_win(event_focus, win_focus);
868 }
869
[1e3ea91]870 loc_service_unregister(win->in_dsid);
871 loc_service_unregister(win->out_dsid);
872
[7c3fb9b]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 */
[1e3ea91]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
[6d5e378]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);
[984a9ba]895 async_answer_0(icall, EOK);
[6d5e378]896}
897
[984a9ba]898static void comp_window_close_request(window_t *win, ipc_call_t *icall)
[6d5e378]899{
[ce3efa0]900 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
[6d5e378]901 if (event == NULL) {
[984a9ba]902 async_answer_0(icall, ENOMEM);
[6d5e378]903 return;
904 }
905
906 link_initialize(&event->link);
907 event->type = ET_WINDOW_CLOSE;
908
909 prodcons_produce(&win->queue, &event->link);
[984a9ba]910 async_answer_0(icall, EOK);
[6d5e378]911}
912
[984a9ba]913static void client_connection(ipc_call_t *icall, void *arg)
[6d5e378]914{
915 ipc_call_t call;
[f9b2cb4c]916 service_id_t service_id = (service_id_t) IPC_GET_ARG2(*icall);
[6d5e378]917
918 /* Allocate resources for new window and register it to the location service. */
919 if (service_id == winreg_id) {
[984a9ba]920 async_answer_0(icall, EOK);
[6d5e378]921
[984a9ba]922 async_get_call(&call);
[6d5e378]923 if (IPC_GET_IMETHOD(call) == WINDOW_REGISTER) {
924 fibril_mutex_lock(&window_list_mtx);
925
[62fbb7e]926 window_t *win = window_create();
[6d5e378]927 if (!win) {
[984a9ba]928 async_answer_2(&call, ENOMEM, 0, 0);
[523b17a]929 fibril_mutex_unlock(&window_list_mtx);
[6d5e378]930 return;
931 }
[a35b458]932
[2c7fdaa]933 win->flags = IPC_GET_ARG1(call);
[6d5e378]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);
[984a9ba]947 async_answer_2(&call, EINVAL, 0, 0);
[523b17a]948 fibril_mutex_unlock(&window_list_mtx);
[6d5e378]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);
[984a9ba]955 async_answer_2(&call, EINVAL, 0, 0);
[523b17a]956 fibril_mutex_unlock(&window_list_mtx);
[6d5e378]957 return;
958 }
959
[290a0f0]960 window_t *win_unfocus = (window_t *) list_first(&window_list);
[6d5e378]961 list_prepend(&win->link, &window_list);
[290a0f0]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 }
[a35b458]968
[984a9ba]969 async_answer_2(&call, EOK, win->in_dsid, win->out_dsid);
[6d5e378]970 fibril_mutex_unlock(&window_list_mtx);
[a35b458]971
[290a0f0]972 if (event_unfocus && win_unfocus) {
973 comp_post_event_win(event_unfocus, win_unfocus);
974 }
975
[6d5e378]976 return;
977 } else {
[984a9ba]978 async_answer_0(&call, EINVAL);
[6d5e378]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);
[feeac0d]986 list_foreach(window_list, link, window_t, cur) {
[6d5e378]987 if (cur->in_dsid == service_id || cur->out_dsid == service_id) {
988 win = cur;
989 break;
990 }
991 }
[498ced1]992
993 if (win)
994 refcount_up(&win->ref_cnt);
995
[6d5e378]996 fibril_mutex_unlock(&window_list_mtx);
997
998 if (win) {
[984a9ba]999 async_answer_0(icall, EOK);
[6d5e378]1000 } else {
[984a9ba]1001 async_answer_0(icall, EINVAL);
[6d5e378]1002 return;
1003 }
1004
1005 /* Each client establishes two separate connections. */
1006 if (win->in_dsid == service_id) {
1007 while (true) {
[984a9ba]1008 async_get_call(&call);
[6d5e378]1009
1010 if (!IPC_GET_IMETHOD(call)) {
[984a9ba]1011 async_answer_0(&call, EOK);
[1e3ea91]1012 window_destroy(win);
[6d5e378]1013 return;
1014 }
1015
1016 switch (IPC_GET_IMETHOD(call)) {
1017 case WINDOW_GET_EVENT:
[984a9ba]1018 comp_window_get_event(win, &call);
[6d5e378]1019 break;
1020 default:
[984a9ba]1021 async_answer_0(&call, EINVAL);
[6d5e378]1022 }
1023 }
1024 } else if (win->out_dsid == service_id) {
1025 while (true) {
[984a9ba]1026 async_get_call(&call);
[6d5e378]1027
1028 if (!IPC_GET_IMETHOD(call)) {
[984a9ba]1029 comp_window_close(win, &call);
[1e3ea91]1030 window_destroy(win);
[6d5e378]1031 return;
1032 }
1033
1034 switch (IPC_GET_IMETHOD(call)) {
1035 case WINDOW_DAMAGE:
[984a9ba]1036 comp_window_damage(win, &call);
[6d5e378]1037 break;
1038 case WINDOW_GRAB:
[984a9ba]1039 comp_window_grab(win, &call);
[6d5e378]1040 break;
1041 case WINDOW_RESIZE:
[984a9ba]1042 comp_window_resize(win, &call);
[6d5e378]1043 break;
1044 case WINDOW_CLOSE:
[ce3efa0]1045 /*
1046 * Postpone the closing until the phone is hung up to cover
1047 * the case when the client is killed abruptly.
1048 */
[984a9ba]1049 async_answer_0(&call, EOK);
[6d5e378]1050 break;
1051 case WINDOW_CLOSE_REQUEST:
[984a9ba]1052 comp_window_close_request(win, &call);
[6d5e378]1053 break;
1054 default:
[984a9ba]1055 async_answer_0(&call, EINVAL);
[6d5e378]1056 }
1057 }
1058 }
1059}
1060
[984a9ba]1061static void comp_mode_change(viewport_t *vp, ipc_call_t *icall)
[6d5e378]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;
[b7fd2a0]1068 errno_t rc = visualizer_get_mode(vp->sess, &new_mode, mode_idx);
[6d5e378]1069 if (rc != EOK) {
1070 fibril_mutex_unlock(&viewport_list_mtx);
[984a9ba]1071 async_answer_0(icall, EINVAL);
[6d5e378]1072 return;
1073 }
1074
1075 /* Create surface with respect to the retrieved mode. */
[523b17a]1076 surface_t *new_surface = surface_create(new_mode.screen_width,
[6d5e378]1077 new_mode.screen_height, NULL, SURFACE_FLAG_SHARED);
1078 if (!new_surface) {
1079 fibril_mutex_unlock(&viewport_list_mtx);
[984a9ba]1080 async_answer_0(icall, ENOMEM);
[6d5e378]1081 return;
1082 }
1083
1084 /* Try to set the mode and share out the surface. */
1085 rc = visualizer_set_mode(vp->sess,
[ce3efa0]1086 new_mode.index, new_mode.version, surface_direct_access(new_surface));
[6d5e378]1087 if (rc != EOK) {
1088 surface_destroy(new_surface);
1089 fibril_mutex_unlock(&viewport_list_mtx);
[984a9ba]1090 async_answer_0(icall, rc);
[6d5e378]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);
[984a9ba]1100 async_answer_0(icall, EOK);
[6d5e378]1101
[3b98311]1102 comp_restrict_pointers();
[6d5e378]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
[593e023]1116#if 0
1117static void comp_shutdown(void)
1118{
1119 loc_service_unregister(winreg_id);
1120 input_disconnect();
[a35b458]1121
[593e023]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);
[a35b458]1133
[984a9ba]1134 async_answer_0(icall, EOK);
[a35b458]1135
[593e023]1136 /* All fibrils of the compositor will terminate soon. */
1137}
1138#endif
1139
[984a9ba]1140static void comp_visualizer_disconnect(viewport_t *vp, ipc_call_t *icall)
[6d5e378]1141{
1142 /* Release viewport resources. */
1143 fibril_mutex_lock(&viewport_list_mtx);
[a35b458]1144
[6d5e378]1145 list_remove(&vp->link);
1146 viewport_destroy(vp);
[a35b458]1147
[593e023]1148 fibril_mutex_unlock(&viewport_list_mtx);
[a35b458]1149
[984a9ba]1150 async_answer_0(icall, EOK);
[a35b458]1151
[593e023]1152 comp_restrict_pointers();
1153 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
[6d5e378]1154}
1155
[984a9ba]1156static void vsl_notifications(ipc_call_t *icall, void *arg)
[6d5e378]1157{
1158 viewport_t *vp = NULL;
1159 fibril_mutex_lock(&viewport_list_mtx);
[feeac0d]1160 list_foreach(viewport_list, link, viewport_t, cur) {
[6d5e378]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
[ce3efa0]1168 if (!vp)
[6d5e378]1169 return;
1170
1171 /* Ignore parameters, the connection is already opened. */
1172 while (true) {
1173 ipc_call_t call;
[984a9ba]1174 async_get_call(&call);
[6d5e378]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:
[984a9ba]1183 comp_mode_change(vp, &call);
[6d5e378]1184 break;
1185 case VISUALIZER_DISCONNECT:
[984a9ba]1186 comp_visualizer_disconnect(vp, &call);
[6d5e378]1187 return;
1188 default:
[984a9ba]1189 async_answer_0(&call, EINVAL);
[6d5e378]1190 }
1191 }
1192}
1193
[0ca15eb7]1194static async_sess_t *vsl_connect(service_id_t sid, const char *svc)
[6d5e378]1195{
[b7fd2a0]1196 errno_t rc;
[6d5e378]1197 async_sess_t *sess;
1198
[f9b2cb4c]1199 sess = loc_service_connect(sid, INTERFACE_DDF, 0);
[0ca15eb7]1200 if (sess == NULL) {
1201 printf("%s: Unable to connect to visualizer %s\n", NAME, svc);
[6d5e378]1202 return NULL;
1203 }
1204
1205 async_exch_t *exch = async_exchange_begin(sess);
[a35b458]1206
[f9b2cb4c]1207 port_id_t port;
1208 rc = async_create_callback_port(exch, INTERFACE_VISUALIZER_CB, 0, 0,
1209 vsl_notifications, NULL, &port);
[a35b458]1210
[6d5e378]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
[0ca15eb7]1223static viewport_t *viewport_create(service_id_t sid)
[6d5e378]1224{
[b7fd2a0]1225 errno_t rc;
[0ca15eb7]1226 char *vsl_name = NULL;
1227 viewport_t *vp = NULL;
1228 bool claimed = false;
[6d5e378]1229
[0ca15eb7]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;
[6d5e378]1237
1238 link_initialize(&vp->link);
[b5416c3]1239 vp->pos.x = coord_origin;
1240 vp->pos.y = coord_origin;
[6d5e378]1241
1242 /* Establish output bidirectional connection. */
[0ca15eb7]1243 vp->dsid = sid;
1244 vp->sess = vsl_connect(sid, vsl_name);
1245 if (vp->sess == NULL)
1246 goto error;
[6d5e378]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));
[0ca15eb7]1252 goto error;
[6d5e378]1253 }
1254
[0ca15eb7]1255 claimed = true;
1256
[6d5e378]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));
[0ca15eb7]1261 goto error;
[6d5e378]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));
[0ca15eb7]1269 goto error;
[6d5e378]1270 }
1271
1272 /* Try to set the mode and share out the surface. */
1273 rc = visualizer_set_mode(vp->sess,
[ce3efa0]1274 vp->mode.index, vp->mode.version, surface_direct_access(vp->surface));
[6d5e378]1275 if (rc != EOK) {
1276 printf("%s: Unable to set mode (%s)\n", NAME, str_error(rc));
[0ca15eb7]1277 goto error;
[6d5e378]1278 }
1279
1280 return vp;
[0ca15eb7]1281error:
1282 if (claimed)
1283 visualizer_yield(vp->sess);
[a35b458]1284
[0ca15eb7]1285 if (vp->sess != NULL)
1286 async_hangup(vp->sess);
[a35b458]1287
[0ca15eb7]1288 free(vp);
1289 free(vsl_name);
1290 return NULL;
[6d5e378]1291}
1292
1293static void comp_window_animate(pointer_t *pointer, window_t *win,
[563573b]1294 sysarg_t *dmg_x, sysarg_t *dmg_y, sysarg_t *dmg_width, sysarg_t *dmg_height)
[6d5e378]1295{
1296 /* window_list_mtx locked by caller */
[290a0f0]1297 /* pointer_list_mtx locked by caller */
[6d5e378]1298
[b5416c3]1299 int dx = pointer->accum.x;
1300 int dy = pointer->accum.y;
1301 pointer->accum.x = 0;
1302 pointer->accum.y = 0;
[6d5e378]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;
[a35b458]1314
[dace86a]1315 if (pointer->grab_flags & GF_MOVE_X)
[6d5e378]1316 cx = 1;
[a35b458]1317
[dace86a]1318 if (pointer->grab_flags & GF_MOVE_Y)
[6d5e378]1319 cy = 1;
[a35b458]1320
[dace86a]1321 if (((scale) || (resize)) && (win->angle != 0)) {
[6d5e378]1322 transform_t rotate;
1323 transform_identity(&rotate);
[a35b458]1324
[6d5e378]1325 transform_rotate(&rotate, win->angle);
1326 transform_apply_linear(&rotate, &cx, &cy);
1327 }
[a35b458]1328
[dace86a]1329 cx = (cx < 0) ? (-1 * cx) : cx;
[6d5e378]1330 cy = (cy < 0) ? (-1 * cy) : cy;
[a35b458]1331
[6d5e378]1332 win->dx += (cx * dx);
1333 win->dy += (cy * dy);
1334 }
1335
[62fbb7e]1336 if ((scale) || (resize)) {
[6d5e378]1337 double _dx = dx;
1338 double _dy = dy;
[82edef2]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 }
[6d5e378]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)) {
[c8e2ac5]1349 double fx = 1.0 + (_dx / ((width - 1) * win->fx));
[6d5e378]1350 if (fx > 0) {
[82edef2]1351#if ANIMATE_WINDOW_TRANSFORMS == 0
[ba02baa]1352 if (scale)
1353 win->fx *= fx;
[82edef2]1354#endif
1355#if ANIMATE_WINDOW_TRANSFORMS == 1
[6d5e378]1356 win->fx *= fx;
[82edef2]1357#endif
[6d5e378]1358 scale_back_x *= fx;
1359 }
1360 }
1361
1362 if ((pointer->grab_flags & GF_SCALE_Y) || (pointer->grab_flags & GF_RESIZE_Y)) {
[c8e2ac5]1363 double fy = 1.0 + (_dy / ((height - 1) * win->fy));
[6d5e378]1364 if (fy > 0) {
[82edef2]1365#if ANIMATE_WINDOW_TRANSFORMS == 0
[18b6a88]1366 if (scale)
1367 win->fy *= fy;
[82edef2]1368#endif
1369#if ANIMATE_WINDOW_TRANSFORMS == 1
[6d5e378]1370 win->fy *= fy;
[82edef2]1371#endif
[6d5e378]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
[563573b]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{
[290a0f0]1392 /* window_list_mtx locked by caller */
1393 /* pointer_list_mtx locked by caller */
1394
[563573b]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)) {
[c8e2ac5]1442 double fx = 1.0 + (_dx / ((width - 1) * pointer->ghost.fx));
[563573b]1443 pointer->ghost.fx *= fx;
1444 }
1445
1446 if ((pointer->grab_flags & GF_SCALE_Y) || (pointer->grab_flags & GF_RESIZE_Y)) {
[c8e2ac5]1447 double fy = 1.0 + (_dy / ((height - 1) * pointer->ghost.fy));
[563573b]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) {
[18b6a88]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;
[563573b]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;
[a35b458]1503
[563573b]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
[18b6a88]1512static errno_t comp_abs_move(input_t *input, unsigned x, unsigned y,
[111d2d6]1513 unsigned max_x, unsigned max_y)
1514{
[d17a4a9]1515 /* XXX TODO Use absolute coordinates directly */
[a35b458]1516
[d17a4a9]1517 pointer_t *pointer = input_pointer(input);
[a35b458]1518
[d17a4a9]1519 sysarg_t width, height;
[a35b458]1520
[d17a4a9]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;
[a35b458]1536
[d17a4a9]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);
[a35b458]1543
[d17a4a9]1544 return comp_mouse_move(input, delta.x, delta.y);
[111d2d6]1545}
1546
[b7fd2a0]1547static errno_t comp_mouse_move(input_t *input, int dx, int dy)
[6d5e378]1548{
[111d2d6]1549 pointer_t *pointer = input_pointer(input);
[a35b458]1550
[62fbb7e]1551 comp_update_viewport_bound_rect();
[a35b458]1552
[6d5e378]1553 /* Update pointer position. */
1554 fibril_mutex_lock(&pointer_list_mtx);
[a35b458]1555
[b5416c3]1556 desktop_point_t old_pos = pointer->pos;
[a35b458]1557
[6d5e378]1558 sysarg_t cursor_width;
1559 sysarg_t cursor_height;
[62fbb7e]1560 surface_get_resolution(pointer->cursor.states[pointer->state],
[18b6a88]1561 &cursor_width, &cursor_height);
[a35b458]1562
[62fbb7e]1563 if (pointer->pos.x + dx < viewport_bound_rect.x)
[3b98311]1564 dx = -1 * (pointer->pos.x - viewport_bound_rect.x);
[a35b458]1565
[62fbb7e]1566 if (pointer->pos.y + dy < viewport_bound_rect.y)
[3b98311]1567 dy = -1 * (pointer->pos.y - viewport_bound_rect.y);
[a35b458]1568
[62fbb7e]1569 if (pointer->pos.x + dx > viewport_bound_rect.x + viewport_bound_rect.w)
[3b98311]1570 dx = (viewport_bound_rect.x + viewport_bound_rect.w - pointer->pos.x);
[a35b458]1571
[62fbb7e]1572 if (pointer->pos.y + dy > viewport_bound_rect.y + viewport_bound_rect.h)
[3b98311]1573 dy = (viewport_bound_rect.y + viewport_bound_rect.h - pointer->pos.y);
[a35b458]1574
[b5416c3]1575 pointer->pos.x += dx;
1576 pointer->pos.y += dy;
[6d5e378]1577 fibril_mutex_unlock(&pointer_list_mtx);
[b5416c3]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);
[a35b458]1580
[6d5e378]1581 fibril_mutex_lock(&window_list_mtx);
[290a0f0]1582 fibril_mutex_lock(&pointer_list_mtx);
[6d5e378]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);
[b5416c3]1592 within_client = comp_coord_to_client(pointer->pos.x, pointer->pos.y,
[6d5e378]1593 top->transform, width, height, &point_x, &point_y);
1594
[290a0f0]1595 window_event_t *event = NULL;
[6d5e378]1596 if (within_client) {
[290a0f0]1597 event = (window_event_t *) malloc(sizeof(window_event_t));
[6d5e378]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 }
[290a0f0]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
[6d5e378]1616 } else {
1617 /* Pointer is grabbed by top-level window action. */
[b5416c3]1618 pointer->accum.x += dx;
1619 pointer->accum.y += dy;
[563573b]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
[6d5e378]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
[290a0f0]1639 fibril_mutex_unlock(&pointer_list_mtx);
[6d5e378]1640 fibril_mutex_unlock(&window_list_mtx);
[563573b]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
[6d5e378]1647#if ANIMATE_WINDOW_TRANSFORMS == 1
1648 comp_damage(x, y, width, height);
1649#endif
1650 }
1651 } else {
[290a0f0]1652 fibril_mutex_unlock(&pointer_list_mtx);
[6d5e378]1653 fibril_mutex_unlock(&window_list_mtx);
1654 }
1655
[111d2d6]1656 return EOK;
[6d5e378]1657}
1658
[b7fd2a0]1659static errno_t comp_mouse_button(input_t *input, int bnum, int bpress)
[6d5e378]1660{
[111d2d6]1661 pointer_t *pointer = input_pointer(input);
[6d5e378]1662
[8e6ec6e]1663 fibril_mutex_lock(&window_list_mtx);
[290a0f0]1664 fibril_mutex_lock(&pointer_list_mtx);
[8e6ec6e]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. */
[feeac0d]1672 list_foreach(window_list, link, window_t, cw) {
1673 win = cw;
[8e6ec6e]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) {
[290a0f0]1687 fibril_mutex_unlock(&pointer_list_mtx);
[8e6ec6e]1688 fibril_mutex_unlock(&window_list_mtx);
1689 return EOK;
1690 }
1691
[290a0f0]1692 window_event_t *event_top = NULL;
1693 window_event_t *event_unfocus = NULL;
1694 window_t *win_unfocus = NULL;
[8e6ec6e]1695 sysarg_t dmg_x, dmg_y;
1696 sysarg_t dmg_width = 0;
1697 sysarg_t dmg_height = 0;
[a35b458]1698
[563573b]1699#if ANIMATE_WINDOW_TRANSFORMS == 0
1700 desktop_rect_t dmg_rect1, dmg_rect2, dmg_rect3, dmg_rect4;
1701#endif
[8e6ec6e]1702
[111d2d6]1703 if (bpress) {
[b5416c3]1704 pointer->btn_pos = pointer->pos;
[111d2d6]1705 pointer->btn_num = bnum;
[6d5e378]1706 pointer->pressed = true;
1707
[8e6ec6e]1708 /* Bring the window to the foreground. */
[c4ebe02]1709 if ((win != top) && within_client) {
[290a0f0]1710 win_unfocus = (window_t *) list_first(&window_list);
[8e6ec6e]1711 list_remove(&win->link);
1712 list_prepend(&win->link, &window_list);
[290a0f0]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 }
[8e6ec6e]1718 comp_coord_bounding_rect(0, 0, width, height, win->transform,
1719 &dmg_x, &dmg_y, &dmg_width, &dmg_height);
[6d5e378]1720 }
1721
[8e6ec6e]1722 /* Notify top-level window about mouse press. */
[6d5e378]1723 if (within_client) {
[290a0f0]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;
[6d5e378]1733 }
1734 pointer->grab_flags = GF_EMPTY;
1735 }
1736
[8e6ec6e]1737 } else if (pointer->pressed && pointer->btn_num == (unsigned)bnum) {
1738 pointer->pressed = false;
[6d5e378]1739
[563573b]1740#if ANIMATE_WINDOW_TRANSFORMS == 0
[62fbb7e]1741 sysarg_t pre_x = 0;
[6d5e378]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) {
[563573b]1747 if (pointer->ghost.surface) {
1748 comp_ghost_animate(pointer, &dmg_rect1, &dmg_rect2, &dmg_rect3, &dmg_rect4);
1749 pointer->ghost.surface = NULL;
1750 }
[6d5e378]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);
[82edef2]1762#if ANIMATE_WINDOW_TRANSFORMS == 1
[6d5e378]1763 top->fx *= (1.0 / scale_back_x);
1764 top->fy *= (1.0 / scale_back_y);
1765 comp_recalc_transform(top);
[82edef2]1766#endif
[6d5e378]1767
1768 /* Commit proper resize action. */
[290a0f0]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;
[a35b458]1773
[62fbb7e]1774 event_top->data.resize.offset_x = 0;
1775 event_top->data.resize.offset_y = 0;
[a35b458]1776
[6d5e378]1777 int dx = (int) (((double) width) * (scale_back_x - 1.0));
1778 int dy = (int) (((double) height) * (scale_back_y - 1.0));
[a35b458]1779
[62fbb7e]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;
[a35b458]1785
[62fbb7e]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;
[a35b458]1791
[62fbb7e]1792 event_top->data.resize.placement_flags =
1793 WINDOW_PLACEMENT_ANY;
[6d5e378]1794 }
1795
1796 pointer->grab_flags = GF_EMPTY;
1797
1798 } else if (within_client && (pointer->grab_flags == GF_EMPTY) && (top == win)) {
[a35b458]1799
[6d5e378]1800 /* Notify top-level window about mouse release. */
[290a0f0]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;
[6d5e378]1810 }
1811 pointer->grab_flags = GF_EMPTY;
[a35b458]1812
[6d5e378]1813 } else {
1814 pointer->grab_flags = GF_EMPTY;
1815 }
1816
[8e6ec6e]1817 }
[6d5e378]1818
[290a0f0]1819 fibril_mutex_unlock(&pointer_list_mtx);
[8e6ec6e]1820 fibril_mutex_unlock(&window_list_mtx);
[6d5e378]1821
[563573b]1822#if ANIMATE_WINDOW_TRANSFORMS == 0
[18b6a88]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);
[563573b]1827#endif
1828
[8e6ec6e]1829 if (dmg_width > 0 && dmg_height > 0) {
1830 comp_damage(dmg_x, dmg_y, dmg_width, dmg_height);
1831 }
1832
[290a0f0]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);
[6d5e378]1839 }
1840
[111d2d6]1841 return EOK;
[6d5e378]1842}
1843
[b7fd2a0]1844static errno_t comp_active(input_t *input)
[593e023]1845{
1846 active = true;
1847 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
[a35b458]1848
[593e023]1849 return EOK;
1850}
1851
[b7fd2a0]1852static errno_t comp_deactive(input_t *input)
[593e023]1853{
1854 active = false;
1855 return EOK;
1856}
1857
[b7fd2a0]1858static errno_t comp_key_press(input_t *input, kbd_event_type_t type, keycode_t key,
[111d2d6]1859 keymod_t mods, wchar_t c)
[6d5e378]1860{
[18b6a88]1861 bool win_transform = (mods & KM_ALT) &&
1862 (key == KC_W || key == KC_S || key == KC_A || key == KC_D ||
[6d5e378]1863 key == KC_Q || key == KC_E || key == KC_R || key == KC_F);
[18b6a88]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);
[6d5e378]1867 bool win_close = (mods & KM_ALT) && (key == KC_X);
1868 bool win_switch = (mods & KM_ALT) && (key == KC_TAB);
[18b6a88]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);
[c072a29]1872 bool kconsole_switch = (key == KC_PAUSE) || (key == KC_BREAK);
[8d3512f1]1873 bool filter_switch = (mods & KM_ALT) && (key == KC_Y);
[6d5e378]1874
[8d3512f1]1875 bool key_filter = (type == KEY_RELEASE) && (win_transform || win_resize ||
[6d5e378]1876 win_opacity || win_close || win_switch || viewport_move ||
[8d3512f1]1877 viewport_change || kconsole_switch || filter_switch);
[6d5e378]1878
[8d3512f1]1879 if (key_filter) {
[6d5e378]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:
[071fefec]1899 win->angle += 0.1;
[6d5e378]1900 break;
1901 case KC_E:
[071fefec]1902 win->angle -= 0.1;
[6d5e378]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);
[2c7fdaa]1937 if ((win) && (win->surface) && (win->flags & WINDOW_RESIZEABLE)) {
[6d5e378]1938 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
1939 if (event == NULL) {
1940 fibril_mutex_unlock(&window_list_mtx);
[111d2d6]1941 return ENOMEM;
[6d5e378]1942 }
[a35b458]1943
[6d5e378]1944 sysarg_t width, height;
1945 surface_get_resolution(win->surface, &width, &height);
[a35b458]1946
[6d5e378]1947 link_initialize(&event->link);
1948 event->type = ET_WINDOW_RESIZE;
[a35b458]1949
[62fbb7e]1950 event->data.resize.offset_x = 0;
1951 event->data.resize.offset_y = 0;
[a35b458]1952
[6d5e378]1953 switch (key) {
1954 case KC_T:
[62fbb7e]1955 event->data.resize.width = width;
1956 event->data.resize.height = (height >= 20) ? height - 20 : 0;
[6d5e378]1957 break;
1958 case KC_G:
[62fbb7e]1959 event->data.resize.width = width;
1960 event->data.resize.height = height + 20;
[6d5e378]1961 break;
1962 case KC_B:
[850fd32]1963 event->data.resize.width = (width >= 20) ? width - 20 : 0;
[62fbb7e]1964 event->data.resize.height = height;
[6d5e378]1965 break;
1966 case KC_N:
[62fbb7e]1967 event->data.resize.width = width + 20;
1968 event->data.resize.height = height;
[6d5e378]1969 break;
1970 default:
[62fbb7e]1971 event->data.resize.width = 0;
1972 event->data.resize.height = 0;
[6d5e378]1973 break;
1974 }
[a35b458]1975
[62fbb7e]1976 event->data.resize.placement_flags = WINDOW_PLACEMENT_ANY;
[a35b458]1977
[6d5e378]1978 fibril_mutex_unlock(&window_list_mtx);
[290a0f0]1979 comp_post_event_top(event);
[6d5e378]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));
[111d2d6]2015 if (event == NULL)
2016 return ENOMEM;
[6d5e378]2017
2018 link_initialize(&event->link);
2019 event->type = ET_WINDOW_CLOSE;
2020
[290a0f0]2021 comp_post_event_top(event);
[6d5e378]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
[290a0f0]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
[6d5e378]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);
[290a0f0]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
[6d5e378]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:
[b5416c3]2088 vp->pos.x += 0;
2089 vp->pos.y += -20;
[6d5e378]2090 break;
2091 case KC_K:
[b5416c3]2092 vp->pos.x += 0;
2093 vp->pos.y += 20;
[6d5e378]2094 break;
2095 case KC_J:
[b5416c3]2096 vp->pos.x += -20;
2097 vp->pos.y += 0;
[6d5e378]2098 break;
2099 case KC_L:
[b5416c3]2100 vp->pos.x += 20;
2101 vp->pos.y += 0;
[6d5e378]2102 break;
2103 default:
[b5416c3]2104 vp->pos.x += 0;
2105 vp->pos.y += 0;
[6d5e378]2106 break;
2107 }
[a35b458]2108
[b5416c3]2109 sysarg_t x = vp->pos.x;
2110 sysarg_t y = vp->pos.y;
[6d5e378]2111 sysarg_t width, height;
2112 surface_get_resolution(vp->surface, &width, &height);
2113 fibril_mutex_unlock(&viewport_list_mtx);
2114
[3b98311]2115 comp_restrict_pointers();
[6d5e378]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) {
[593e023]2145 if (console_kcon())
2146 active = false;
[8d3512f1]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;
[18b6a88]2153 } else {
[8d3512f1]2154 filter = filter_bilinear;
2155 }
2156 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
[6d5e378]2157 } else {
2158 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
[111d2d6]2159 if (event == NULL)
2160 return ENOMEM;
[6d5e378]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
[290a0f0]2169 comp_post_event_top(event);
[6d5e378]2170 }
2171
[111d2d6]2172 return EOK;
[6d5e378]2173}
2174
[b7fd2a0]2175static errno_t input_connect(const char *svc)
[6d5e378]2176{
[111d2d6]2177 async_sess_t *sess;
2178 service_id_t dsid;
2179
[b7fd2a0]2180 errno_t rc = loc_service_get_id(svc, &dsid, 0);
[111d2d6]2181 if (rc != EOK) {
2182 printf("%s: Input service %s not found\n", NAME, svc);
2183 return rc;
2184 }
2185
[f9b2cb4c]2186 sess = loc_service_connect(dsid, INTERFACE_INPUT, 0);
[111d2d6]2187 if (sess == NULL) {
2188 printf("%s: Unable to connect to input service %s\n", NAME,
2189 svc);
2190 return EIO;
2191 }
2192
[6d5e378]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
[111d2d6]2201 if (pointer == NULL) {
2202 printf("%s: Cannot create pointer.\n", NAME);
2203 async_hangup(sess);
2204 return ENOMEM;
[6d5e378]2205 }
2206
[111d2d6]2207 rc = input_open(sess, &input_ev_ops, pointer, &input);
[6d5e378]2208 if (rc != EOK) {
2209 async_hangup(sess);
[111d2d6]2210 printf("%s: Unable to communicate with service %s (%s)\n",
[6d5e378]2211 NAME, svc, str_error(rc));
[111d2d6]2212 return rc;
[6d5e378]2213 }
2214
[111d2d6]2215 return EOK;
2216}
2217
2218static void input_disconnect(void)
2219{
[290a0f0]2220 pointer_t *pointer = input->user;
[111d2d6]2221 input_close(input);
2222 pointer_destroy(pointer);
[6d5e378]2223}
2224
[593e023]2225static void discover_viewports(void)
[d08ba7fc]2226{
[593e023]2227 fibril_mutex_lock(&discovery_mtx);
[a35b458]2228
[d08ba7fc]2229 /* Create viewports and connect them to visualizers. */
2230 category_id_t cat_id;
[b7fd2a0]2231 errno_t rc = loc_category_get_id("visualizer", &cat_id, IPC_FLAG_BLOCKING);
[593e023]2232 if (rc != EOK)
2233 goto ret;
[a35b458]2234
[d08ba7fc]2235 service_id_t *svcs;
2236 size_t svcs_cnt = 0;
2237 rc = loc_category_get_svcs(cat_id, &svcs, &svcs_cnt);
[593e023]2238 if (rc != EOK)
2239 goto ret;
[a35b458]2240
[593e023]2241 fibril_mutex_lock(&viewport_list_mtx);
[d08ba7fc]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 }
[a35b458]2250
[d08ba7fc]2251 if (exists)
2252 continue;
[a35b458]2253
[0ca15eb7]2254 viewport_t *vp = viewport_create(svcs[i]);
2255 if (vp != NULL)
2256 list_append(&vp->link, &viewport_list);
[d08ba7fc]2257 }
2258 fibril_mutex_unlock(&viewport_list_mtx);
[a35b458]2259
[593e023]2260 if (!list_empty(&viewport_list))
2261 input_activate(input);
[a35b458]2262
[593e023]2263ret:
2264 fibril_mutex_unlock(&discovery_mtx);
[d08ba7fc]2265}
2266
[e89a06a]2267static void category_change_cb(void *arg)
[d08ba7fc]2268{
2269 discover_viewports();
2270}
2271
[b7fd2a0]2272static errno_t compositor_srv_init(char *input_svc, char *name)
[6d5e378]2273{
2274 /* Coordinates of the central pixel. */
[563573b]2275 coord_origin = UINT32_MAX / 4;
[a35b458]2276
[6d5e378]2277 /* Color of the viewport background. Must be opaque. */
[fbb2d0d]2278 bg_color = PIXEL(255, 69, 51, 103);
[a35b458]2279
[6d5e378]2280 /* Register compositor server. */
[b688fd8]2281 async_set_fallback_port_handler(client_connection, NULL);
[a35b458]2282
[b7fd2a0]2283 errno_t rc = loc_server_register(NAME);
[6d5e378]2284 if (rc != EOK) {
2285 printf("%s: Unable to register server (%s)\n", NAME, str_error(rc));
2286 return -1;
2287 }
[a35b458]2288
[6d5e378]2289 server_name = name;
[a35b458]2290
[6d5e378]2291 char svc[LOC_NAME_MAXLEN + 1];
2292 snprintf(svc, LOC_NAME_MAXLEN, "%s/%s", NAMESPACE, server_name);
[a35b458]2293
[6d5e378]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 }
[a35b458]2300
[6d5e378]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 }
[a35b458]2308
[6d5e378]2309 /* Establish input bidirectional connection. */
[111d2d6]2310 rc = input_connect(input_svc);
[428bd07]2311 if (rc != EOK) {
2312 printf("%s: Failed to connect to input service.\n", NAME);
[111d2d6]2313 return rc;
[428bd07]2314 }
[a35b458]2315
[e89a06a]2316 rc = loc_register_cat_change_cb(category_change_cb, NULL);
[6d5e378]2317 if (rc != EOK) {
[d08ba7fc]2318 printf("%s: Failed to register category change callback\n", NAME);
[111d2d6]2319 input_disconnect();
[d08ba7fc]2320 return rc;
[6d5e378]2321 }
[a35b458]2322
[593e023]2323 discover_viewports();
[a35b458]2324
[3b98311]2325 comp_restrict_pointers();
[6d5e378]2326 comp_damage(0, 0, UINT32_MAX, UINT32_MAX);
[a35b458]2327
[6d5e378]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 }
[a35b458]2342
[6d5e378]2343 printf("%s: HelenOS Compositor server\n", NAME);
[a35b458]2344
[b7fd2a0]2345 errno_t rc = compositor_srv_init(argv[1], argv[2]);
[6d5e378]2346 if (rc != EOK)
2347 return rc;
[a35b458]2348
[6d5e378]2349 printf("%s: Accepting connections\n", NAME);
2350 task_retval(0);
2351 async_manager();
[a35b458]2352
[6d5e378]2353 /* Never reached */
2354 return 0;
2355}
2356
2357/** @}
2358 */
Note: See TracBrowser for help on using the repository browser.