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)
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.
10 #include <l4/mag-gfx/geometry>
11 #include <l4/mag-gfx/canvas>
14 #include <l4/util/util.h>
16 #include <l4/cxx/iostream>
17 #include <l4/cxx/exceptions>
18 #include <l4/re/util/cap_alloc>
19 #include <l4/re/error_helper>
22 #include <l4/re/video/goos>
23 #include <l4/re/util/video/goos_fb>
35 #include "background.h"
36 #include "big_mouse.h"
37 #include "input_driver"
38 #include "object_gc.h"
44 using namespace Mag_server;
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[];
51 namespace Mag_server {
56 static void start_plugins(Core_api *core)
58 for (Plugin *p = Plugin::_first; p; p = p->_next)
69 class My_reg : public Registry, private Object_gc
72 class Del_handler : public L4::Server_object
78 explicit Del_handler(Object_gc *gc) : gc(gc) {}
80 int dispatch(l4_umword_t, L4::Ipc_iostream &)
87 L4::Cap<L4::Irq> _del_irq;
92 _del_irq = register_irq_obj(new Del_handler(this));
94 _server->register_del_irq(_del_irq);
97 void add_gc_obj(Object *o)
99 L4::Cap<L4::Kobject> c(o->obj_cap());
100 Object_gc::add_obj(o);
106 poll_input(Core_api_impl *core)
108 for (Input_source *i = core->input_sources(); i; i = i->next())
112 static L4::Cap<void> rcv_cap;
114 class Loop_hooks : public L4::Ipc_svr::Ignore_errors
117 l4_kernel_clock_t to;
120 to = l4re_kip()->clock + 40000;
123 l4_timeout_t timeout()
124 { return l4_timeout(L4_IPC_TIMEOUT_0, l4_timeout_abs(to, 8)); }
126 void setup_wait(L4::Ipc_istream &istr, L4::Ipc_svr::Reply_mode reply_mode)
128 if (to < l4re_kip()->clock
129 && reply_mode == L4::Ipc_svr::Reply_separate)
131 poll_input(_core_api);
132 _core_api->user_state()->vstack()->flush();
135 while (to - 10000 < l4re_kip()->clock)
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);
145 L4::Ipc_svr::Reply_mode before_reply(long, L4::Ipc_iostream &)
147 if (to < l4re_kip()->clock)
148 return L4::Ipc_svr::Reply_separate;
149 return L4::Ipc_svr::Reply_compound;
153 static My_reg registry;
154 static L4::Server<Loop_hooks> server(l4_utcb());
156 using L4Re::Util::Auto_cap;
160 static void test_texture(Texture *t)
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)
166 t->type()->set(tb, Pixel_info::Col(x*400, y*300, x*y, 0));
167 tb += t->type()->bytes;
172 int load_lua_plugin(Core_api *core_api, char const *name)
174 char const *n = name;
176 if (access(n, F_OK) != 0)
179 printf("loading '%s'\n", n);
181 lua_State *_l = core_api->lua_state();
182 int err = luaL_dofile(_l, n);
185 printf("ERROR: loading '%s': %s\n", n, lua_tostring(_l, -1));
186 lua_pop(_l, lua_gettop(_l));
190 lua_pop(_l, lua_gettop(_l));
194 int load_so_plugin(Core_api *core_api, char const *name)
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];
200 strcpy(n + strlen(pfx), name);
201 strcpy(n + strlen(pfx) + strlen(name), sfx);
203 printf("loading '%s'\n", n);
205 void *pl = dlopen(n, RTLD_LAZY);
209 printf("ERROR: loading '%s': %s\n", n, dlerror());
214 void (*ini)(Core_api*) = (void (*)(Core_api*))dlsym(pl, "init_plugin");
221 static const luaL_Reg libs[] =
223 { "", luaopen_base },
224 // { LUA_IOLIBNAME, luaopen_io },
225 { LUA_STRLIBNAME, luaopen_string },
226 {LUA_LOADLIBNAME, luaopen_package},
227 {LUA_DBLIBNAME, luaopen_debug},
231 int run(int argc, char const *argv[])
233 printf("Hello from MAG\n");
234 L4Re::Env const *env = L4Re::Env::env();
236 L4::Cap<L4Re::Video::Goos> fb
237 = chkcap(env->get_cap<L4Re::Video::Goos>("fb"), "requesting frame-buffer", 0);
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");
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));
247 printf("mapped frame buffer at %p\n", fb_addr.get());
249 Screen_factory *f = dynamic_cast<Screen_factory*>(Screen_factory::set.find(view_i.pixel_info));
252 printf("ERROR: could not start screen driver for given video mode.\n"
253 " Maybe unsupoported pixel format... exiting\n");
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);
260 view_i.dump(L4::cout)
261 << " memory " << (void*)fb_addr.get()
262 << '-' << (void*)(fb_addr.get() + goos_fb.buffer()->size()) << '\n';
266 printf("ERROR: could not start screen driver for given video mode.\n"
267 " Maybe unsupoported pixel format... exiting\n");
271 rcv_cap = L4Re::Util::cap_alloc.alloc<void>();
272 if (!rcv_cap.is_valid())
274 printf("ERROR: Out of caps\n");
278 View *cursor = f->create_cursor(big_mouse);
279 Background bg(screen->size());
281 L4Re::Video::View *screen_view = 0;
284 L4Re::Video::Goos::Info i;
285 goos_fb.goos()->info(&i);
286 if (!i.auto_refresh())
287 screen_view = goos_fb.view();
290 lua_State *lua = luaL_newstate();
294 printf("ERROR: cannot allocate Lua state\n");
298 for (int i = 0; libs[i].func; ++i)
300 lua_pushcfunction(lua, libs[i].func);
301 lua_pushstring(lua,libs[i].name);
306 if ((err = luaL_loadbuffer(lua, _binary_mag_lua_start, _binary_mag_lua_end - _binary_mag_lua_start, "@mag.lua")))
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));
313 throw L4::Out_of_memory(lua_tostring(lua, -1));
316 if ((err = lua_pcall(lua, 0, 1, 0)))
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));
323 throw L4::Out_of_memory(lua_tostring(lua, -1));
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(®istry, lua, &user_state, rcv_cap, fb, &label_font);
332 Plugin_manager::start_plugins(&core_api);
334 for (int i = 1; i < argc; ++i)
336 if (load_lua_plugin(&core_api, argv[i]) == 1)
337 load_so_plugin(&core_api, argv[i]);
340 _core_api = &core_api;
342 server.loop(registry);
348 int main(int argc, char const *argv[])
352 return run(argc, argv);
354 catch (L4::Runtime_error const &e)
356 L4::cerr << "Error: " << e << '\n';
358 catch (L4::Base_exception const &e)
360 L4::cerr << "Error: " << e << '\n';
362 catch (std::exception const &e)
364 L4::cerr << "Error: " << e.what() << '\n';