source: mainline/uspace/srv/hid/compositor/compositor.c@ 3083c74

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

Do not answer call twice

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

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