]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/mag/server/src/main.cc
bdc372399ec68ec60a2f648be78baf39411b2f05
[l4.git] / l4 / pkg / mag / server / src / main.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 <l4/mag-gfx/geometry>
11 #include <l4/mag-gfx/canvas>
12 #include "factory"
13
14 #include <l4/util/util.h>
15
16 #include <l4/cxx/iostream>
17 #include <l4/cxx/exceptions>
18 #include <l4/re/util/cap_alloc>
19 #include <l4/re/error_helper>
20 #include <l4/re/env>
21 #include <l4/re/rm>
22 #include <l4/re/video/goos>
23 #include <l4/re/util/video/goos_fb>
24
25 #include <lua.h>
26 #include <lauxlib.h>
27 #include <lualib.h>
28
29 #include <cassert>
30 #include <cstdio>
31 #include <cstring>
32
33 #include <unistd.h>
34
35 #include "background.h"
36 #include "big_mouse.h"
37 #include "input_driver"
38 #include "object_gc.h"
39
40 #include "plugin"
41
42 #include <dlfcn.h>
43
44 using namespace Mag_server;
45
46 static Core_api *_core_api;
47 extern char const _binary_mag_lua_start[];
48 extern char const _binary_mag_lua_end[];
49
50 namespace Mag_server {
51
52 class Plugin_manager
53 {
54 public:
55   static void start_plugins(Core_api *core)
56   {
57     for (Plugin *p = Plugin::_first; p; p = p->_next)
58       if (!p->started())
59         p->start(core);
60   }
61 };
62
63 }
64
65 namespace {
66
67
68 class My_reg : public Registry, private Object_gc
69 {
70 private:
71   class Del_handler : public L4::Server_object
72   {
73   private:
74     Object_gc *gc;
75
76   public:
77     explicit Del_handler(Object_gc *gc) : gc(gc) {}
78
79     int dispatch(l4_umword_t, L4::Ipc_iostream &s)
80     {
81       l4_msgtag_t t;
82       s >> t;
83       if (t.label() != L4_PROTO_IRQ)
84         return -L4_EBADPROTO;
85
86       gc->gc_step();
87       return 0;
88     }
89   };
90
91   L4::Cap<L4::Irq> _del_irq;
92
93 public:
94   My_reg() : Registry()
95   {
96     _del_irq = register_irq_obj(new Del_handler(this));
97     assert (_del_irq);
98     _server->register_del_irq(_del_irq);
99   };
100
101   void add_gc_obj(Object *o)
102   {
103     L4::Cap<L4::Kobject> c(o->obj_cap());
104     Object_gc::add_obj(o);
105   }
106 };
107
108
109 static void
110 poll_input(Core_api *core)
111 {
112   for (Input_source *i = core->input_sources(); i; i = i->next())
113       i->poll_events();
114 }
115
116 static L4::Cap<void> rcv_cap;
117
118 class Loop_hooks : public L4::Ipc_svr::Ignore_errors
119 {
120 public:
121   l4_kernel_clock_t to;
122   Loop_hooks()
123   {
124     to = l4re_kip()->clock + 40000;
125   }
126
127   l4_timeout_t timeout()
128   { return l4_timeout(L4_IPC_TIMEOUT_0, l4_timeout_abs(to, 8)); }
129
130   void setup_wait(L4::Ipc_istream &istr, L4::Ipc_svr::Reply_mode reply_mode)
131   {
132     if (to < l4re_kip()->clock
133         && reply_mode == L4::Ipc_svr::Reply_separate)
134     {
135       poll_input(_core_api);
136       _core_api->user_state()->vstack()->flush();
137       to += 40000;
138       while (to - 10000 < l4re_kip()->clock)
139         to += 20000;
140     }
141
142     istr.reset();
143     istr << L4::Small_buf(rcv_cap.cap(), L4_RCV_ITEM_LOCAL_ID);
144     l4_utcb_br_u(istr.utcb())->bdr = 0;
145     l4_timeout_abs(to, 8);
146   }
147
148   L4::Ipc_svr::Reply_mode before_reply(long, L4::Ipc_iostream &)
149   {
150     if (to < l4re_kip()->clock)
151       return L4::Ipc_svr::Reply_separate;
152     return L4::Ipc_svr::Reply_compound;
153   }
154 };
155
156 static My_reg registry;
157 static L4::Server<Loop_hooks> server(l4_utcb());
158
159 using L4Re::Util::Auto_cap;
160 using L4Re::chksys;
161 using L4Re::chkcap;
162 #if 0
163 static void test_texture(Texture *t)
164 {
165   char *tb = (char *)t->pixels();
166   for (int y = 0; y < t->size().h(); ++y)
167     for (int x = 0; x < t->size().w(); ++x)
168       {
169         t->type()->set(tb, Pixel_info::Col(x*400, y*300, x*y, 0));
170         tb += t->type()->bytes;
171       }
172 }
173 #endif
174
175 int load_lua_plugin(Core_api *core_api, char const *name)
176 {
177   char const *n = name;
178
179   if (access(n, F_OK) != 0)
180     return 1;
181
182   printf("loading '%s'\n", n);
183
184   lua_State *_l = core_api->lua_state();
185   int err = luaL_dofile(_l, n);
186   if (err)
187     {
188       printf("ERROR: loading '%s': %s\n", n, lua_tostring(_l, -1));
189       lua_pop(_l, lua_gettop(_l));
190       return -1;
191     }
192
193   lua_pop(_l, lua_gettop(_l));
194   return 0;
195 }
196
197 int load_so_plugin(Core_api *core_api, char const *name)
198 {
199   static char const *const pfx = "libmag-";
200   static char const *const sfx = ".so";
201   char *n = new char [strlen(name) + strlen(pfx) + strlen(sfx) + 1];
202   strcpy(n, pfx);
203   strcpy(n + strlen(pfx), name);
204   strcpy(n + strlen(pfx) + strlen(name), sfx);
205
206   printf("loading '%s'\n", n);
207
208   void *pl = dlopen(n, RTLD_LAZY);
209   if (!pl)
210     {
211       delete [] n;
212       printf("ERROR: loading '%s': %s\n", n, dlerror());
213       return -1;
214     }
215   else
216     {
217       void (*ini)(Core_api*) = (void (*)(Core_api*))dlsym(pl, "init_plugin");
218       ini(core_api);
219     }
220   delete [] n;
221   return 0;
222 }
223
224 static const luaL_Reg libs[] =
225 {
226   { "", luaopen_base },
227 // { LUA_IOLIBNAME, luaopen_io },
228   { LUA_STRLIBNAME, luaopen_string },
229   {LUA_LOADLIBNAME, luaopen_package},
230   {LUA_DBLIBNAME, luaopen_debug},
231   { NULL, NULL }
232 };
233
234 int run(int argc, char const *argv[])
235 {
236   printf("Hello from MAG\n");
237   L4Re::Env const *env = L4Re::Env::env();
238
239   L4::Cap<L4Re::Video::Goos> fb
240     = chkcap(env->get_cap<L4Re::Video::Goos>("fb"), "requesting frame-buffer", 0);
241
242   L4Re::Util::Video::Goos_fb goos_fb(fb);
243   L4Re::Video::View::Info view_i;
244   chksys(goos_fb.view_info(&view_i), "requesting frame-buffer info");
245
246   L4Re::Rm::Auto_region<char *> fb_addr;
247   chksys(env->rm()->attach(&fb_addr, goos_fb.buffer()->size(),
248         L4Re::Rm::Search_addr, goos_fb.buffer(), 0, L4_SUPERPAGESHIFT));
249
250   printf("mapped frame buffer at %p\n", fb_addr.get());
251
252   Screen_factory *f = dynamic_cast<Screen_factory*>(Screen_factory::set.find(view_i.pixel_info));
253   if (!f)
254     {
255       printf("ERROR: could not start screen driver for given video mode.\n"
256              "       Maybe unsupoported pixel format... exiting\n");
257       exit(1);
258     }
259
260   Canvas *screen = f->create_canvas(fb_addr.get() + view_i.buffer_offset,
261       Area(view_i.width, view_i.height), view_i.bytes_per_line);
262
263   view_i.dump(L4::cout)
264     << "  memory " << (void*)fb_addr.get()
265     << '-' << (void*)(fb_addr.get() + goos_fb.buffer()->size()) << '\n';
266
267   if (!screen)
268     {
269       printf("ERROR: could not start screen driver for given video mode.\n"
270              "       Maybe unsupoported pixel format... exiting\n");
271       exit(1);
272     }
273
274   rcv_cap = L4Re::Util::cap_alloc.alloc<void>();
275   if (!rcv_cap.is_valid())
276     {
277       printf("ERROR: Out of caps\n");
278       exit(1);
279     }
280
281   View *cursor = f->create_cursor(big_mouse);
282   Background bg(screen->size());
283
284   L4Re::Video::View *screen_view = 0;
285
286     {
287       L4Re::Video::Goos::Info i;
288       goos_fb.goos()->info(&i);
289       if (!i.auto_refresh())
290         screen_view = goos_fb.view();
291     }
292
293   lua_State *lua = luaL_newstate();
294
295   if (!lua)
296     {
297       printf("ERROR: cannot allocate Lua state\n");
298       exit(1);
299     }
300
301   for (int i = 0; libs[i].func; ++i)
302     {
303       lua_pushcfunction(lua, libs[i].func);
304       lua_pushstring(lua,libs[i].name);
305       lua_call(lua, 1, 0);
306     }
307
308   int err;
309   if ((err = luaL_loadbuffer(lua, _binary_mag_lua_start, _binary_mag_lua_end - _binary_mag_lua_start, "@mag.lua")))
310     {
311       fprintf(stderr, "lua error: %s.\n", lua_tostring(lua, -1));
312       lua_pop(lua, lua_gettop(lua));
313       if (err == LUA_ERRSYNTAX)
314         throw L4::Runtime_error(L4_EINVAL, lua_tostring(lua, -1));
315       else
316         throw L4::Out_of_memory(lua_tostring(lua, -1));
317     }
318
319   if ((err = lua_pcall(lua, 0, 1, 0)))
320     {
321       fprintf(stderr, "lua error: %s.\n", lua_tostring(lua, -1));
322       lua_pop(lua, lua_gettop(lua));
323       if (err == LUA_ERRSYNTAX)
324         throw L4::Runtime_error(L4_EINVAL, lua_tostring(lua, -1));
325       else
326         throw L4::Out_of_memory(lua_tostring(lua, -1));
327     }
328
329   lua_pop(lua, lua_gettop(lua));
330
331   static View_stack vstack(screen, screen_view, &bg);
332   static User_state user_state(lua, &vstack, cursor);
333   static Core_api core_api(&registry, lua, &user_state, rcv_cap, fb);
334
335   Plugin_manager::start_plugins(&core_api);
336
337   for (int i = 1; i < argc; ++i)
338     {
339       if (load_lua_plugin(&core_api, argv[i]) == 1)
340         load_so_plugin(&core_api, argv[i]);
341     }
342
343   _core_api = &core_api;
344
345   server.loop(registry);
346
347   return 0;
348 }
349 }
350
351 int main(int argc, char const *argv[])
352 {
353   try
354     {
355       return run(argc, argv);
356     }
357   catch (L4::Runtime_error const &e)
358     {
359       L4::cerr << "Error: " << e << '\n';
360     }
361   catch (L4::Base_exception const &e)
362     {
363       L4::cerr << "Error: " << e << '\n';
364     }
365   catch (std::exception const &e)
366     {
367       L4::cerr << "Error: " << e.what() << '\n';
368     }
369
370   return -1;
371 }