-
Notifications
You must be signed in to change notification settings - Fork 247
/
gui_session.cc
513 lines (387 loc) · 12.2 KB
/
gui_session.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* \brief GUI session component
* \author Norman Feske
* \date 2017-11-16
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#include "view_stack.h"
using namespace Nitpicker;
bool Gui_session::_views_are_equal(View_handle v1, View_handle v2)
{
if (!v1.valid() || !v2.valid())
return false;
Weak_ptr<View> v1_ptr = _view_handle_registry.lookup(v1);
Weak_ptr<View> v2_ptr = _view_handle_registry.lookup(v2);
return v1_ptr == v2_ptr;
}
View_owner &Gui_session::forwarded_focus()
{
Gui_session *next_focus = this;
/* helper used for detecting cycles */
Gui_session *next_focus_slow = next_focus;
for (bool odd = false; ; odd = !odd) {
/* we found the final focus once the forwarding stops */
if (!next_focus->_forwarded_focus)
break;
next_focus = next_focus->_forwarded_focus;
/* advance 'next_focus_slow' every odd iteration only */
if (odd)
next_focus_slow = next_focus_slow->_forwarded_focus;
/* a cycle is detected if 'next_focus' laps 'next_focus_slow' */
if (next_focus == next_focus_slow) {
error("cyclic focus forwarding by ", next_focus->label());
break;
}
}
return *next_focus;
}
void Gui_session::_execute_command(Command const &command)
{
switch (command.opcode) {
case Command::OP_GEOMETRY:
{
Command::Geometry const &cmd = command.geometry;
Locked_ptr<View> view(_view_handle_registry.lookup(cmd.view));
if (!view.valid())
return;
Point pos = cmd.rect.p1();
/* transpose position of top-level views by vertical session offset */
if (view->top_level())
pos = _phys_pos(pos, _view_stack.size());
if (view.valid())
_view_stack.geometry(*view, Rect(pos, cmd.rect.area()));
return;
}
case Command::OP_OFFSET:
{
Command::Offset const &cmd = command.offset;
Locked_ptr<View> view(_view_handle_registry.lookup(cmd.view));
if (view.valid())
_view_stack.buffer_offset(*view, cmd.offset);
return;
}
case Command::OP_TO_FRONT:
{
Command::To_front const &cmd = command.to_front;
if (_views_are_equal(cmd.view, cmd.neighbor))
return;
Locked_ptr<View> view(_view_handle_registry.lookup(cmd.view));
if (!view.valid())
return;
/* bring to front if no neighbor is specified */
if (!cmd.neighbor.valid()) {
_view_stack.stack(*view, nullptr, true);
return;
}
/* stack view relative to neighbor */
Locked_ptr<View> neighbor(_view_handle_registry.lookup(cmd.neighbor));
if (neighbor.valid())
_view_stack.stack(*view, &(*neighbor), false);
return;
}
case Command::OP_TO_BACK:
{
Command::To_back const &cmd = command.to_back;
if (_views_are_equal(cmd.view, cmd.neighbor))
return;
Locked_ptr<View> view(_view_handle_registry.lookup(cmd.view));
if (!view.valid())
return;
/* bring to front if no neighbor is specified */
if (!cmd.neighbor.valid()) {
_view_stack.stack(*view, nullptr, false);
return;
}
/* stack view relative to neighbor */
Locked_ptr<View> neighbor(_view_handle_registry.lookup(cmd.neighbor));
if (neighbor.valid())
_view_stack.stack(*view, &(*neighbor), true);
return;
}
case Command::OP_BACKGROUND:
{
Command::Background const &cmd = command.background;
if (_provides_default_bg) {
Locked_ptr<View> view(_view_handle_registry.lookup(cmd.view));
if (!view.valid())
return;
view->background(true);
_view_stack.default_background(*view);
return;
}
/* revert old background view to normal mode */
if (_background)
_background->background(false);
/* assign session background */
Locked_ptr<View> view(_view_handle_registry.lookup(cmd.view));
if (!view.valid())
return;
_background = &(*view);
/* switch background view to background mode */
if (background())
view->background(true);
return;
}
case Command::OP_TITLE:
{
Command::Title const &cmd = command.title;
Locked_ptr<View> view(_view_handle_registry.lookup(cmd.view));
if (view.valid())
_view_stack.title(*view, cmd.title.string());
return;
}
case Command::OP_NOP:
return;
}
}
void Gui_session::_destroy_view(View &view)
{
if (_background == &view)
_background = nullptr;
/* reset background if view was used as default background */
if (_view_stack.is_default_background(view))
_view_stack.default_background(_builtin_background);
_view_stack.remove_view(view);
_env.ep().dissolve(view);
_view_list.remove(&view);
destroy(_view_alloc, &view);
}
void Gui_session::destroy_all_views()
{
while (Session_view_list_elem *v = _view_list.first())
_destroy_view(*static_cast<View *>(v));
}
void Gui_session::submit_input_event(Input::Event e)
{
using namespace Input;
Point const origin_offset = _phys_pos(Point(0, 0), _view_stack.size());
/*
* Transpose absolute coordinates by session-specific vertical offset.
*/
e.handle_absolute_motion([&] (int x, int y) {
e = Absolute_motion{max(0, x - origin_offset.x()),
max(0, y - origin_offset.y())}; });
e.handle_touch([&] (Touch_id id, float x, float y) {
e = Touch{ id, max(0.0f, x - (float)origin_offset.x()),
max(0.0f, y - (float)origin_offset.y())}; });
_input_session_component.submit(&e);
}
Gui_session::View_handle Gui_session::create_view(View_handle parent_handle)
{
View *view = nullptr;
/*
* Create child view
*/
if (parent_handle.valid()) {
try {
Locked_ptr<View> parent(_view_handle_registry.lookup(parent_handle));
if (!parent.valid())
return View_handle();
view = new (_view_alloc)
View(*this, _texture, View::NOT_TRANSPARENT, View::NOT_BACKGROUND, &(*parent));
parent->add_child(*view);
}
catch (View_handle_registry::Lookup_failed) { return View_handle(); }
catch (View_handle_registry::Out_of_memory) { throw Out_of_ram(); }
}
/*
* Create top-level view
*/
else {
try {
view = new (_view_alloc)
View(*this, _texture, View::NOT_TRANSPARENT, View::NOT_BACKGROUND, nullptr);
}
catch (Allocator::Out_of_memory) { throw Out_of_ram(); }
}
_view_stack.title(*view, "");
view->apply_origin_policy(_pointer_origin);
_view_list.insert(view);
_env.ep().manage(*view);
try {
return _view_handle_registry.alloc(*view);
}
catch (View_handle_registry::Out_of_memory) { throw Out_of_ram(); }
}
void Gui_session::apply_session_policy(Xml_node config,
Domain_registry const &domain_registry)
{
reset_domain();
try {
Session_policy policy(_label, config);
/* read domain attribute */
if (!policy.has_attribute("domain")) {
error("policy for label \"", _label, "\" lacks domain declaration");
return;
}
typedef Domain_registry::Entry::Name Name;
Name const name = policy.attribute_value("domain", Name());
_domain = domain_registry.lookup(name);
if (!_domain)
error("policy for label \"", _label,
"\" specifies nonexistent domain \"", name, "\"");
} catch (...) {
error("no policy matching label \"", _label, "\""); }
}
void Gui_session::destroy_view(View_handle handle)
{
/*
* Search view object given the handle
*
* We cannot look up the view directly from the
* '_view_handle_registry' because we would obtain a weak
* pointer to the view object. If we called the object's
* destructor from the corresponding locked pointer, the
* call of 'lock_for_destruction' in the view's destructor
* would attempt to take the lock again.
*/
for (Session_view_list_elem *v = _view_list.first(); v; v = v->next()) {
auto handle_matches = [&] (View const &view)
{
try { return _view_handle_registry.has_handle(view, handle); }
/* 'Handle_registry::has_handle' may throw */
catch (...) { return false; };
};
View &view = *static_cast<View *>(v);
if (handle_matches(view)) {
_destroy_view(view);
_view_handle_registry.free(handle);
break;
}
}
_hover_updater.update_hover();
}
Gui_session::View_handle
Gui_session::view_handle(View_capability view_cap, View_handle handle)
{
auto lambda = [&] (View *view)
{
return (view) ? _view_handle_registry.alloc(*view, handle)
: View_handle();
};
try {
return _env.ep().rpc_ep().apply(view_cap, lambda);
}
catch (View_handle_registry::Out_of_memory) { throw Out_of_ram(); }
}
View_capability Gui_session::view_capability(View_handle handle)
{
try {
Locked_ptr<View> view(_view_handle_registry.lookup(handle));
return view.valid() ? view->cap() : View_capability();
}
catch (View_handle_registry::Lookup_failed) { return View_capability(); }
}
void Gui_session::release_view_handle(View_handle handle)
{
try {
_view_handle_registry.free(handle); }
catch (View_handle_registry::Lookup_failed) {
warning("view lookup failed while releasing view handle");
return;
}
}
void Gui_session::execute()
{
for (unsigned i = 0; i < _command_buffer.num(); i++) {
try {
_execute_command(_command_buffer.get(i)); }
catch (View_handle_registry::Lookup_failed) {
warning("view lookup failed during command execution"); }
}
_hover_updater.update_hover();
}
Framebuffer::Mode Gui_session::mode()
{
Area const screen = screen_area(_view_stack.size());
/*
* Return at least a size of 1x1 to spare the clients the need to handle
* the special case of 0x0, which can happen at boot time before the
* framebuffer driver is running.
*/
return { .area = { max(screen.w(), 1u), max(screen.h(), 1u) } };
}
void Gui_session::buffer(Framebuffer::Mode mode, bool use_alpha)
{
/* check if the session quota suffices for the specified mode */
if (_buffer_size + _ram_quota_guard().avail().value < ram_quota(mode, use_alpha))
throw Out_of_ram();
/* buffer re-allocation may consume new dataspace capability if buffer is new */
if (_cap_quota_guard().avail().value < 1)
throw Out_of_caps();
_framebuffer_session_component.notify_mode_change(mode, use_alpha);
}
void Gui_session::focus(Capability<Gui::Session> session_cap)
{
if (this->cap() == session_cap)
return;
_forwarded_focus = nullptr;
_env.ep().rpc_ep().apply(session_cap, [&] (Gui_session *session) {
if (session)
_forwarded_focus = session; });
_focus_updater.update_focus();
}
void Gui_session::session_control(Label suffix, Session_control control)
{
switch (control) {
case SESSION_CONTROL_HIDE:
_visibility_controller.hide_matching_sessions(label(), suffix);
break;
case SESSION_CONTROL_SHOW:
_visibility_controller.show_matching_sessions(label(), suffix);
break;
case SESSION_CONTROL_TO_FRONT:
_view_stack.to_front(Label(label(), suffix).string());
break;
}
_hover_updater.update_hover();
}
Dataspace_capability Gui_session::realloc_buffer(Framebuffer::Mode mode, bool use_alpha)
{
Ram_quota const next_buffer_size { Chunky_texture<Pixel>::calc_num_bytes(mode.area, use_alpha) };
Ram_quota const orig_buffer_size { _buffer_size };
/*
* Preserve the content of the original buffer if nitpicker has
* enough slack memory to temporarily keep the original pixels.
*/
enum { PRESERVED_RAM = 128*1024 };
bool const preserve_content =
(_env.pd().avail_ram().value > next_buffer_size.value + PRESERVED_RAM);
if (!preserve_content) {
warning("not enough RAM to preserve buffer content during resize");
_texture.release_current();
replenish(orig_buffer_size);
}
_buffer_size = 0;
_uses_alpha = false;
_input_mask = nullptr;
Ram_quota const temporary_ram_upgrade = _texture.valid()
? next_buffer_size : Ram_quota{0};
_ram_quota_guard().upgrade(temporary_ram_upgrade);
if (!_texture.try_construct_next(_env.ram(), _env.rm(), mode.area, use_alpha)) {
_texture.release_current();
replenish(orig_buffer_size);
_ram_quota_guard().try_downgrade(temporary_ram_upgrade);
return Dataspace_capability();
}
_texture.switch_to_next();
/* 'switch_to_next' has released the current texture */
if (preserve_content)
replenish(orig_buffer_size);
if (!_ram_quota_guard().try_downgrade(temporary_ram_upgrade))
warning("accounting error during framebuffer realloc");
try { withdraw(next_buffer_size); }
catch (...) {
_texture.release_current();
return Dataspace_capability();
}
_buffer_size = next_buffer_size.value;
_uses_alpha = use_alpha;
_input_mask = _texture.input_mask_buffer();
return _texture.dataspace();
}