]> 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(L4Re::Event_buffer::Event const &e, Point const &mouse);
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(L4Re::Event_buffer::Event const &e,
94                                    Point const &mouse)
95 {
96   Session *s = _m.handle_event(e, mouse - p1());
97
98   if (s)
99     {
100       s->ignore(!s->ignore());
101       _core->user_state()->vstack()->update_all_views();
102     }
103 }
104
105 void
106 Session_manager_view::draw(Canvas *canvas, View_stack const *, Mode) const
107 {
108   _m.draw(canvas, p1());
109 }
110
111 void
112 Session_manager_view::calc_geometry()
113 {
114   enum {
115     BORDER = 10,
116   };
117   Area cs = _core->user_state()->vstack()->canvas()->size();
118   cs = cs - Area(BORDER * 2, BORDER * 2);
119   Area sz =_m.calc_geometry(cs);
120
121   Point p = Point(20, 20).min(Point(BORDER, BORDER) + Point(cs) - Point(sz));
122   set_geometry(Rect(p, sz));
123 }
124
125 void
126 Session_manager_view::sessions_changed()
127 {
128   if (!_visible)
129     return;
130
131   calc_geometry();
132   _core->user_state()->vstack()->refresh_view(this, 0, *this);
133 }
134
135 void
136 Session_manager_view::notify()
137 {
138   View_stack *vs = _core->user_state()->vstack();
139   Mode m = vs->mode();
140   if (_visible && !m.kill())
141     {
142       _visible = false;
143       View::l_remove();
144       vs->refresh_view(0, 0, *this);
145     }
146   else if (!_visible && m.kill())
147     {
148       _visible = true;
149       calc_geometry();
150       vs->push_top(this, true);
151     }
152 }
153
154 }}
155