source: mainline/uspace/srv/hid/compositor/compositor.c@ 5cd6c84

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5cd6c84 was 3b98311, checked in by Petr Koupy <petr.koupy@…>, 13 years ago

Restrict mouse movement to the bounding rectangle of all viewports.

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