source: mainline/uspace/srv/hid/compositor/compositor.c@ 290a0f0

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

Resolved Ticket #482 (Cannot see which window is active).

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