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