~alan-griffiths/mir/fix-1654023

« back to all changes in this revision

Viewing changes to examples/server_example_tiling_window_manager.cpp

  • Committer: Daniel van Vugt
  • Date: 2015-02-22 07:46:25 UTC
  • mfrom: (2332 development-branch)
  • mto: This revision was merged to the branch mainline in revision 2673.
  • Revision ID: daniel.van.vugt@canonical.com-20150222074625-8t23m2sif3dc0j3t
Merge latest trunk and fix conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored By: Alan Griffiths <alan@octopull.co.uk>
 
17
 */
 
18
 
 
19
#include "server_example_tiling_window_manager.h"
 
20
 
 
21
#include "mir/scene/surface.h"
 
22
#include "mir/geometry/displacement.h"
 
23
 
 
24
#include <linux/input.h>
 
25
#include <csignal>
 
26
 
 
27
namespace me = mir::examples;
 
28
namespace ms = mir::scene;
 
29
using namespace mir::geometry;
 
30
 
 
31
///\example server_example_tiling_window_manager.cpp
 
32
/// Demonstrate implementing a simple tiling algorithm
 
33
 
 
34
me::TilingSurfaceInfo::TilingSurfaceInfo(
 
35
    std::shared_ptr<scene::Session> const& session,
 
36
    std::shared_ptr<scene::Surface> const& surface) :
 
37
    session{session},
 
38
    state{mir_surface_state_restored},
 
39
    restore_rect{surface->top_left(), surface->size()}
 
40
{
 
41
}
 
42
 
 
43
me::TilingWindowManagerPolicy::TilingWindowManagerPolicy(Tools* const tools) :
 
44
    tools{tools}
 
45
{
 
46
}
 
47
 
 
48
void me::TilingWindowManagerPolicy::click(Point cursor)
 
49
{
 
50
    if (const auto session = session_under(cursor))
 
51
        tools->set_focus_to(session);
 
52
    old_cursor = cursor;
 
53
}
 
54
 
 
55
void me::TilingWindowManagerPolicy::handle_session_info_updated(TilingSessionInfoMap& session_info, Rectangles const& displays)
 
56
{
 
57
    update_tiles(session_info, displays);
 
58
}
 
59
 
 
60
void me::TilingWindowManagerPolicy::handle_displays_updated(TilingSessionInfoMap& session_info, Rectangles const& displays)
 
61
{
 
62
    update_tiles(session_info, displays);
 
63
}
 
64
 
 
65
void me::TilingWindowManagerPolicy::resize(Point cursor)
 
66
{
 
67
    if (auto const session = session_under(cursor))
 
68
    {
 
69
        if (session == session_under(old_cursor))
 
70
        {
 
71
            auto const& info = tools->info_for(session);
 
72
 
 
73
            if (resize(old_surface.lock(), cursor, old_cursor, info.tile))
 
74
            {
 
75
                // Still dragging the same old_surface
 
76
            }
 
77
            else if (resize(session->default_surface(), cursor, old_cursor, info.tile))
 
78
            {
 
79
                old_surface = session->default_surface();
 
80
            }
 
81
            else
 
82
            {
 
83
                for (auto const& ps : info.surfaces)
 
84
                {
 
85
                    auto const new_surface = ps.lock();
 
86
 
 
87
                    if (resize(new_surface, cursor, old_cursor, info.tile))
 
88
                    {
 
89
                        old_surface = new_surface;
 
90
                        break;
 
91
                    }
 
92
                }
 
93
            }
 
94
        }
 
95
    }
 
96
    old_cursor = cursor;
 
97
}
 
98
 
 
99
auto me::TilingWindowManagerPolicy::handle_place_new_surface(
 
100
    std::shared_ptr<ms::Session> const& session,
 
101
    ms::SurfaceCreationParameters const& request_parameters)
 
102
-> ms::SurfaceCreationParameters
 
103
{
 
104
    auto parameters = request_parameters;
 
105
 
 
106
    Rectangle const& tile = tools->info_for(session).tile;
 
107
    parameters.top_left = parameters.top_left + (tile.top_left - Point{0, 0});
 
108
 
 
109
    clip_to_tile(parameters, tile);
 
110
    return parameters;
 
111
}
 
112
 
 
113
void me::TilingWindowManagerPolicy::handle_new_surface(std::shared_ptr<ms::Session> const& session, std::shared_ptr<ms::Surface> const& surface)
 
114
{
 
115
    tools->info_for(session).surfaces.push_back(surface);
 
116
}
 
117
 
 
118
void me::TilingWindowManagerPolicy::handle_delete_surface(std::shared_ptr<ms::Session> const& session, std::weak_ptr<ms::Surface> const& surface)
 
119
{
 
120
    auto& surfaces = tools->info_for(session).surfaces;
 
121
 
 
122
    for (auto i = begin(surfaces); i != end(surfaces); ++i)
 
123
    {
 
124
        if (surface.lock() == i->lock())
 
125
        {
 
126
            surfaces.erase(i);
 
127
            break;
 
128
        }
 
129
    }
 
130
 
 
131
    if (surfaces.empty() && session == tools->focussed_application().lock())
 
132
    {
 
133
        tools->focus_next();
 
134
    }
 
135
}
 
136
 
 
137
int me::TilingWindowManagerPolicy::handle_set_state(std::shared_ptr<ms::Surface> const& surface, MirSurfaceState value)
 
138
{
 
139
    auto& info = tools->info_for(surface);
 
140
 
 
141
    switch (value)
 
142
    {
 
143
    case mir_surface_state_restored:
 
144
    case mir_surface_state_maximized:
 
145
    case mir_surface_state_vertmaximized:
 
146
    case mir_surface_state_horizmaximized:
 
147
        break;
 
148
 
 
149
    default:
 
150
        return info.state;
 
151
    }
 
152
 
 
153
    if (info.state == mir_surface_state_restored)
 
154
    {
 
155
        info.restore_rect = {surface->top_left(), surface->size()};
 
156
    }
 
157
 
 
158
    if (info.state == value)
 
159
    {
 
160
        return info.state;
 
161
    }
 
162
 
 
163
    auto const& tile = tools->info_for(info.session).tile;
 
164
 
 
165
    switch (value)
 
166
    {
 
167
    case mir_surface_state_restored:
 
168
        surface->move_to(info.restore_rect.top_left);
 
169
        surface->resize(info.restore_rect.size);
 
170
        break;
 
171
 
 
172
    case mir_surface_state_maximized:
 
173
        surface->move_to(tile.top_left);
 
174
        surface->resize(tile.size);
 
175
        break;
 
176
 
 
177
    case mir_surface_state_horizmaximized:
 
178
        surface->move_to({tile.top_left.x, info.restore_rect.top_left.y});
 
179
        surface->resize({tile.size.width, info.restore_rect.size.height});
 
180
        break;
 
181
 
 
182
    case mir_surface_state_vertmaximized:
 
183
        surface->move_to({info.restore_rect.top_left.x, tile.top_left.y});
 
184
        surface->resize({info.restore_rect.size.width, tile.size.height});
 
185
        break;
 
186
 
 
187
    default:
 
188
        break;
 
189
    }
 
190
 
 
191
    return info.state = value;
 
192
}
 
193
 
 
194
void me::TilingWindowManagerPolicy::drag(Point cursor)
 
195
{
 
196
    if (const auto session = session_under(cursor))
 
197
    {
 
198
        if (session == session_under(old_cursor))
 
199
        {
 
200
            const auto& info = tools->info_for(session);
 
201
            if (drag(old_surface.lock(), cursor, old_cursor, info.tile))
 
202
            {
 
203
                // Still dragging the same old_surface
 
204
            }
 
205
            else if (drag(session->default_surface(), cursor, old_cursor, info.tile))
 
206
            {
 
207
                old_surface = session->default_surface();
 
208
            }
 
209
            else
 
210
            {
 
211
                for (const auto& ps : info.surfaces)
 
212
                {
 
213
                    const auto new_surface = ps.lock();
 
214
                    if (drag(new_surface, cursor, old_cursor, info.tile))
 
215
                    {
 
216
                        old_surface = new_surface;
 
217
                        break;
 
218
                    }
 
219
                }
 
220
            }
 
221
        }
 
222
    }
 
223
    old_cursor = cursor;
 
224
}
 
225
 
 
226
bool me::TilingWindowManagerPolicy::handle_key_event(MirKeyInputEvent const* event)
 
227
{
 
228
    auto const action = mir_key_input_event_get_action(event);
 
229
    auto const scan_code = mir_key_input_event_get_scan_code(event);
 
230
    auto const modifiers = mir_key_input_event_get_modifiers(event) & modifier_mask;
 
231
 
 
232
    if (action == mir_key_input_event_action_down && scan_code == KEY_F11)
 
233
    {
 
234
        switch (modifiers & modifier_mask)
 
235
        {
 
236
        case mir_input_event_modifier_alt:
 
237
            toggle(mir_surface_state_maximized);
 
238
            return true;
 
239
 
 
240
        case mir_input_event_modifier_shift:
 
241
            toggle(mir_surface_state_vertmaximized);
 
242
            return true;
 
243
 
 
244
        case mir_input_event_modifier_ctrl:
 
245
            toggle(mir_surface_state_horizmaximized);
 
246
            return true;
 
247
 
 
248
        default:
 
249
            break;
 
250
        }
 
251
    }
 
252
    else if (action == mir_key_input_event_action_down && scan_code == KEY_F4)
 
253
    {
 
254
        if (auto const session = tools->focussed_application().lock())
 
255
        {
 
256
            switch (modifiers & modifier_mask)
 
257
            {
 
258
            case mir_input_event_modifier_alt:
 
259
                kill(session->process_id(), SIGTERM);
 
260
                return true;
 
261
 
 
262
            case mir_input_event_modifier_ctrl:
 
263
                if (auto const surf = session->default_surface())
 
264
                {
 
265
                    surf->request_client_surface_close();
 
266
                    return true;
 
267
                }
 
268
 
 
269
            default:
 
270
                break;
 
271
            }
 
272
        }
 
273
    }
 
274
 
 
275
    return false;
 
276
}
 
277
 
 
278
bool me::TilingWindowManagerPolicy::handle_touch_event(MirTouchInputEvent const* event)
 
279
{
 
280
    auto const count = mir_touch_input_event_get_touch_count(event);
 
281
 
 
282
    long total_x = 0;
 
283
    long total_y = 0;
 
284
 
 
285
    for (auto i = 0U; i != count; ++i)
 
286
    {
 
287
        total_x += mir_touch_input_event_get_touch_axis_value(event, i, mir_touch_input_axis_x);
 
288
        total_y += mir_touch_input_event_get_touch_axis_value(event, i, mir_touch_input_axis_y);
 
289
    }
 
290
 
 
291
    Point const cursor{total_x/count, total_y/count};
 
292
 
 
293
    bool is_drag = true;
 
294
    for (auto i = 0U; i != count; ++i)
 
295
    {
 
296
        switch (mir_touch_input_event_get_touch_action(event, i))
 
297
        {
 
298
        case mir_touch_input_event_action_up:
 
299
            return false;
 
300
 
 
301
        case mir_touch_input_event_action_down:
 
302
            is_drag = false;
 
303
 
 
304
        case mir_touch_input_event_action_change:
 
305
            continue;
 
306
        }
 
307
    }
 
308
 
 
309
    if (is_drag && count == 3)
 
310
    {
 
311
        drag(cursor);
 
312
        return true;
 
313
    }
 
314
    else
 
315
    {
 
316
        click(cursor);
 
317
        return false;
 
318
    }
 
319
}
 
320
 
 
321
bool me::TilingWindowManagerPolicy::handle_pointer_event(MirPointerInputEvent const* event)
 
322
{
 
323
    auto const action = mir_pointer_input_event_get_action(event);
 
324
    auto const modifiers = mir_pointer_input_event_get_modifiers(event) & modifier_mask;
 
325
    Point const cursor{
 
326
        mir_pointer_input_event_get_axis_value(event, mir_pointer_input_axis_x),
 
327
        mir_pointer_input_event_get_axis_value(event, mir_pointer_input_axis_y)};
 
328
 
 
329
    if (action == mir_pointer_input_event_action_button_down)
 
330
    {
 
331
        click(cursor);
 
332
        return false;
 
333
    }
 
334
    else if (action == mir_pointer_input_event_action_motion &&
 
335
             modifiers == mir_input_event_modifier_alt)
 
336
    {
 
337
        if (mir_pointer_input_event_get_button_state(event, mir_pointer_input_button_primary))
 
338
        {
 
339
            drag(cursor);
 
340
            return true;
 
341
        }
 
342
 
 
343
        if (mir_pointer_input_event_get_button_state(event, mir_pointer_input_button_tertiary))
 
344
        {
 
345
            resize(cursor);
 
346
            return true;
 
347
        }
 
348
    }
 
349
 
 
350
    return false;
 
351
}
 
352
 
 
353
void me::TilingWindowManagerPolicy::toggle(MirSurfaceState state)
 
354
{
 
355
    if (auto const session = tools->focussed_application().lock())
 
356
    {
 
357
        if (auto const surface = session->default_surface())
 
358
        {
 
359
            if (surface->state() == state)
 
360
                state = mir_surface_state_restored;
 
361
 
 
362
            auto const value = handle_set_state(surface, MirSurfaceState(state));
 
363
            surface->configure(mir_surface_attrib_state, value);
 
364
        }
 
365
    }
 
366
}
 
367
 
 
368
std::shared_ptr<ms::Session> me::TilingWindowManagerPolicy::session_under(Point position)
 
369
{
 
370
    return tools->find_session([&](TilingSessionInfo const& info) { return info.tile.contains(position);});
 
371
}
 
372
 
 
373
void me::TilingWindowManagerPolicy::update_tiles(
 
374
    TilingSessionInfoMap& session_info,
 
375
    Rectangles const& displays)
 
376
{
 
377
    if (session_info.size() < 1 || displays.size() < 1) return;
 
378
 
 
379
    auto const sessions = session_info.size();
 
380
 
 
381
    auto const bounding_rect = displays.bounding_rectangle();
 
382
 
 
383
    auto const total_width  = bounding_rect.size.width.as_int();
 
384
    auto const total_height = bounding_rect.size.height.as_int();
 
385
 
 
386
    auto index = 0;
 
387
 
 
388
    for (auto& info : session_info)
 
389
    {
 
390
        auto const x = (total_width*index)/sessions;
 
391
        ++index;
 
392
        auto const dx = (total_width*index)/sessions - x;
 
393
 
 
394
        auto const old_tile = info.second.tile;
 
395
        Rectangle const new_tile{{x, 0}, {dx, total_height}};
 
396
 
 
397
        update_surfaces(info.first, old_tile, new_tile);
 
398
 
 
399
        info.second.tile = new_tile;
 
400
    }
 
401
}
 
402
 
 
403
void me::TilingWindowManagerPolicy::update_surfaces(std::weak_ptr<ms::Session> const& session, Rectangle const& old_tile, Rectangle const& new_tile)
 
404
{
 
405
    auto displacement = new_tile.top_left - old_tile.top_left;
 
406
    auto& info = tools->info_for(session);
 
407
 
 
408
    for (auto const& ps : info.surfaces)
 
409
    {
 
410
        if (auto const surface = ps.lock())
 
411
        {
 
412
            auto const old_pos = surface->top_left();
 
413
            surface->move_to(old_pos + displacement);
 
414
 
 
415
            fit_to_new_tile(*surface, old_tile, new_tile);
 
416
        }
 
417
    }
 
418
}
 
419
 
 
420
void me::TilingWindowManagerPolicy::clip_to_tile(ms::SurfaceCreationParameters& parameters, Rectangle const& tile)
 
421
{
 
422
    auto const displacement = parameters.top_left - tile.top_left;
 
423
 
 
424
    auto width = std::min(tile.size.width.as_int()-displacement.dx.as_int(), parameters.size.width.as_int());
 
425
    auto height = std::min(tile.size.height.as_int()-displacement.dy.as_int(), parameters.size.height.as_int());
 
426
 
 
427
    parameters.size = Size{width, height};
 
428
}
 
429
 
 
430
void me::TilingWindowManagerPolicy::fit_to_new_tile(ms::Surface& surface, Rectangle const& old_tile, Rectangle const& new_tile)
 
431
{
 
432
    auto const displacement = surface.top_left() - new_tile.top_left;
 
433
 
 
434
    // For now just scale if was filling width/height of tile
 
435
    auto const old_size = surface.size();
 
436
    auto const scaled_width = old_size.width == old_tile.size.width ? new_tile.size.width : old_size.width;
 
437
    auto const scaled_height = old_size.height == old_tile.size.height ? new_tile.size.height : old_size.height;
 
438
 
 
439
    auto width = std::min(new_tile.size.width.as_int()-displacement.dx.as_int(), scaled_width.as_int());
 
440
    auto height = std::min(new_tile.size.height.as_int()-displacement.dy.as_int(), scaled_height.as_int());
 
441
 
 
442
    surface.resize({width, height});
 
443
}
 
444
 
 
445
bool me::TilingWindowManagerPolicy::drag(std::shared_ptr<ms::Surface> surface, Point to, Point from, Rectangle bounds)
 
446
{
 
447
    if (surface && surface->input_area_contains(from))
 
448
    {
 
449
        auto const top_left = surface->top_left();
 
450
        auto const surface_size = surface->size();
 
451
        auto const bottom_right = top_left + as_displacement(surface_size);
 
452
 
 
453
        auto movement = to - from;
 
454
 
 
455
        if (movement.dx < DeltaX{0})
 
456
            movement.dx = std::max(movement.dx, (bounds.top_left - top_left).dx);
 
457
 
 
458
        if (movement.dy < DeltaY{0})
 
459
            movement.dy = std::max(movement.dy, (bounds.top_left - top_left).dy);
 
460
 
 
461
        if (movement.dx > DeltaX{0})
 
462
            movement.dx = std::min(movement.dx, (bounds.bottom_right() - bottom_right).dx);
 
463
 
 
464
        if (movement.dy > DeltaY{0})
 
465
            movement.dy = std::min(movement.dy, (bounds.bottom_right() - bottom_right).dy);
 
466
 
 
467
        auto new_pos = surface->top_left() + movement;
 
468
 
 
469
        surface->move_to(new_pos);
 
470
        return true;
 
471
    }
 
472
 
 
473
    return false;
 
474
}
 
475
 
 
476
bool me::TilingWindowManagerPolicy::resize(std::shared_ptr<ms::Surface> surface, Point cursor, Point old_cursor, Rectangle bounds)
 
477
{
 
478
    if (surface && surface->input_area_contains(old_cursor))
 
479
    {
 
480
        auto const top_left = surface->top_left();
 
481
 
 
482
        auto const old_displacement = old_cursor - top_left;
 
483
        auto const new_displacement = cursor - top_left;
 
484
 
 
485
        auto const scale_x = new_displacement.dx.as_float()/std::max(1.0f, old_displacement.dx.as_float());
 
486
        auto const scale_y = new_displacement.dy.as_float()/std::max(1.0f, old_displacement.dy.as_float());
 
487
 
 
488
        if (scale_x <= 0.0f || scale_y <= 0.0f) return false;
 
489
 
 
490
        auto const old_size = surface->size();
 
491
        Size new_size{scale_x*old_size.width, scale_y*old_size.height};
 
492
 
 
493
        auto const size_limits = as_size(bounds.bottom_right() - top_left);
 
494
 
 
495
        if (new_size.width > size_limits.width)
 
496
            new_size.width = size_limits.width;
 
497
 
 
498
        if (new_size.height > size_limits.height)
 
499
            new_size.height = size_limits.height;
 
500
 
 
501
        surface->resize(new_size);
 
502
 
 
503
        return true;
 
504
    }
 
505
 
 
506
    return false;
 
507
}