]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/mag/plugins/session_manager/session_manager.cc
update
[l4.git] / l4 / pkg / mag / plugins / session_manager / session_manager.cc
1 /*
2  * (c) 2011 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3  *          Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10
11 #include <l4/mag/server/session>
12 #include <l4/mag/server/plugin>
13 #include <l4/mag/server/view>
14 #include <l4/mag/server/user_state>
15 #include <l4/mag/server/menu>
16
17 #include <l4/mag-gfx/canvas>
18 #include <l4/mag-gfx/clip_guard>
19
20 #include <l4/re/event_enums.h>
21
22 namespace Mag_server { namespace {
23
24 typedef Menu<Session_list> Session_menu;
25
26 class Session_manager_view : public View, private cxx::Observer
27 {
28 private:
29   Core_api const *_core;
30   Session_menu _m;
31   bool _visible;
32
33   struct So : public Observer
34   {
35     explicit So(Session_manager_view *v) : v(v) {}
36     Session_manager_view *v;
37     void notify() { v->sessions_changed(); }
38   };
39
40   So _session_observer;
41
42 public:
43   explicit Session_manager_view(Core_api const *core)
44   : View(Rect(), View::F_super_view /*| F_transparent*/), _core(core),
45     _m(core, this, core->sessions()),
46     _visible(false), _session_observer(this)
47   { }
48
49   Observer *mode_observer() { return this; }
50   Observer *session_observer() { return &_session_observer; }
51
52   void handle_event(Hid_report *e, Point const &mouse, bool core_dev);
53   void draw(Canvas *canvas, View_stack const *, Mode mode) const;
54   void notify();
55   void sessions_changed();
56   void calc_geometry();
57 };
58
59 class Session_manager : public Plugin
60 {
61 private:
62   Session_manager_view *_v;
63
64 public:
65   Session_manager() : Plugin("Session manager") {}
66   char const *type() const { return "Session manager"; }
67   void start(Core_api *core);
68   void stop();
69 };
70
71
72 void
73 Session_manager::start(Core_api *core)
74 {
75   _v = new Session_manager_view(core);
76   core->user_state()->vstack()->add_mode_observer(_v->mode_observer());
77   core->add_session_observer(_v->session_observer());
78 }
79
80 void
81 Session_manager::stop()
82 {
83   if (_v)
84     delete _v;
85
86   _v = 0;
87 }
88
89 static Session_manager session_manager;
90
91
92 void
93 Session_manager_view::handle_event(Hid_report *e, Point const &mouse, bool)
94 {
95   Session_list::Const_iterator s = _m.handle_event(e, mouse - p1());
96
97   if (s != _core->sessions()->end())
98     {
99       s->ignore(!s->ignore());
100       _core->user_state()->vstack()->update_all_views();
101     }
102 }
103
104 void
105 Session_manager_view::draw(Canvas *canvas, View_stack const *, Mode) const
106 {
107   _m.draw(canvas, p1());
108 }
109
110 void
111 Session_manager_view::calc_geometry()
112 {
113   enum {
114     BORDER = 10,
115   };
116   Area cs = _core->user_state()->vstack()->canvas()->size();
117   cs = cs - Area(BORDER * 2, BORDER * 2);
118   Area sz =_m.calc_geometry(cs);
119
120   Point p = Point(20, 20).min(Point(BORDER, BORDER) + Point(cs) - Point(sz));
121   set_geometry(Rect(p, sz));
122 }
123
124 void
125 Session_manager_view::sessions_changed()
126 {
127   if (!_visible)
128     return;
129
130   calc_geometry();
131   _core->user_state()->vstack()->refresh_view(this, 0, *this);
132 }
133
134 void
135 Session_manager_view::notify()
136 {
137   View_stack *vs = _core->user_state()->vstack();
138   Mode m = vs->mode();
139   if (_visible && !m.kill())
140     {
141       _visible = false;
142       vs->remove(this);
143       vs->refresh_view(0, 0, *this);
144     }
145   else if (!_visible && m.kill())
146     {
147       _visible = true;
148       calc_geometry();
149       vs->push_top(this, true);
150     }
151 }
152
153 }}
154