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

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

Merge from lp:~werkov/helenos/various-fixes

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