]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/mag/server/src/plugin.cc
update
[l4.git] / l4 / pkg / mag / server / src / plugin.cc
1 /*
2  * (c) 2010 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 #include "plugin"
11 #include "lua"
12
13 #include <lua.h>
14 #include <lauxlib.h>
15
16 #include <l4/cxx/string>
17 #include <l4/mag/server/session>
18 #include <l4/re/error_helper>
19
20 #include <algorithm>
21 #include <cstring>
22
23
24 namespace Mag_server {
25
26 Plugin *Plugin::_first;
27
28 Core_api::Core_api(lua_State *lua, User_state *u,
29                    L4::Cap<void> rcvc, L4::Cap<L4Re::Video::Goos> fb,
30                    Mag_gfx::Font const *label_font)
31 : _ust(u), _rcv_cap(rcvc), _fb(fb), _lua(lua), _label_font(label_font)
32 {
33   lua_pushlightuserdata(_lua, this);
34   lua_newtable(_lua);
35   lua_rawset(_lua, LUA_REGISTRYINDEX);
36 }
37
38 void
39 Core_api::get_refs_table() const
40 {
41   lua_pushlightuserdata(_lua, const_cast<Core_api*>(this));
42   lua_rawget(_lua, LUA_REGISTRYINDEX);
43 }
44
45 namespace {
46   Session::Property_handler const _default_session_props[] =
47     { { "l",     true, &Session::set_label_prop },
48       { "label", true, &Session::set_label_prop },
49       { "col",   true, &Session::set_color_prop },
50       { 0, 0, 0 }
51     };
52
53   static bool handle_option(Session *s, Session::Property_handler const *p, cxx::String const &a)
54   {
55     for (; p && p->tag; ++p)
56       {
57         cxx::String::Index v = a.starts_with(p->tag);
58         if (v && (!p->value_property || a[v] == '='))
59           {
60             p->handler(s, p, a.substr(v + 1));
61             return true;
62           }
63       }
64     return false;
65   }
66 }
67
68 void
69 Core_api::set_session_options(Session *s, L4::Ipc::Istream &ios,
70                               Session::Property_handler const *extra) const
71 {
72   L4::Ipc::Varg opt;
73   while (ios.get(&opt))
74     {
75       if (!opt.is_of<char const *>())
76         {
77           printf("skipping non string argument for session!\n");
78           continue;
79         }
80
81       cxx::String a(opt.value<char const *>(), opt.length());
82
83       if (!handle_option(s, _default_session_props, a)
84           && !handle_option(s, extra, a))
85         {
86           printf("unknown session option '%.*s'\n", a.len(), a.start());
87           L4Re::chksys(-L4_EINVAL, "parsing session options");
88         }
89     }
90
91   if (!s->label())
92     s->set_label_prop(s, 0, "<empty>");
93 }
94
95 }
96