]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/mag/plugins/client_fb/service.cc
update
[l4.git] / l4 / pkg / mag / plugins / client_fb / service.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/cxx/ipc_server>
11 #include <l4/re/util/object_registry>
12 #include <l4/re/env>
13 #include <l4/re/protocols>
14 #include <l4/re/console>
15 #include <l4/re/util/cap_alloc>
16 #include <l4/re/error_helper>
17 #include <l4/re/rm>
18 #include <l4/re/dataspace>
19 #include <l4/re/mem_alloc>
20 #include <l4/re/util/meta>
21
22 #include <cstdio>
23 #include <cstring>
24 #include <algorithm>
25
26 #include "client_fb.h"
27 #include <l4/mag-gfx/canvas>
28 #include <l4/mag/server/user_state>
29 #include <l4/mag/server/factory>
30
31 #include "service.h"
32 #include <l4/sys/kdebug.h>
33
34 namespace Mag_server {
35
36 using L4Re::Util::Auto_cap;
37
38 void Service::start(Core_api *core)
39 {
40   _core = core;
41   if (!reg()->register_obj(cxx::Ref_ptr<Service>(this), "svc").is_valid())
42     printf("Service registration failed.\n");
43   else
44     printf("Plugin: Frame-buffer service started\n");
45 }
46
47
48 int
49 Service::create(char const *_msg, L4::Ipc_iostream &ios)
50 {
51   int w, h;
52
53   if (sscanf(_msg, "%dx%d", &w, &h) != 2)
54     return -L4_EINVAL;
55
56   if (w <= 0 || h <= 0 || h >= 10000 || w >= 10000)
57     return -L4_ERANGE;
58
59   int px = 50, py = 50;
60
61   if (char *a = strstr(_msg, "pos="))
62     {
63       char *endp;
64       px = strtol(a + 4, &endp, 0);
65       if (*endp == ',')
66         py = strtol(endp + 1, 0, 0);
67     }
68
69   if (px < 10 - w)
70     px = 10 - w;
71   if (px >= ust()->vstack()->canvas()->size().w())
72     px = ust()->vstack()->canvas()->size().w() - 10;
73   if (py < 0)
74     py = 0;
75   if (py >= ust()->vstack()->canvas()->size().h())
76     py = ust()->vstack()->canvas()->size().h() - 10;
77
78   Area res(w, h);
79   Auto_cap<L4Re::Dataspace>::Cap ds(
80       L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>());
81
82   Screen_factory *sf = dynamic_cast<Screen_factory*>(ust()->vstack()->canvas()->type()->factory);
83
84   L4Re::chksys(L4Re::Env::env()->mem_alloc()->alloc(sf->get_texture_size(res), ds.get()));
85
86   L4Re::Rm::Auto_region<void *> dsa;
87   L4Re::chksys(L4Re::Env::env()->rm()->attach(&dsa, ds->size(), L4Re::Rm::Search_addr, ds.get(), 0, L4_SUPERPAGESHIFT));
88
89   Texture *smpl = sf->create_texture(res, dsa.get());
90
91   cxx::Ref_ptr<Client_fb> x(new Client_fb(_core, Rect(Point(px, py), Area(res.w(), res.h() + 16)), Point(0, 0), smpl, ds.get()));
92
93   reg()->register_obj(x);
94   x->obj_cap()->dec_refcnt(1);
95
96   ust()->vstack()->push_top(x.ptr());
97
98   ds.release();
99   dsa.release();
100   ios << x->obj_cap();
101   return 0;
102 }
103
104 int
105 Service::dispatch(l4_umword_t, L4::Ipc_iostream &ios)
106 {
107   l4_msgtag_t tag;
108   ios >> tag;
109
110   switch (tag.label())
111     {
112     case L4::Meta::Protocol:
113       return L4Re::Util::handle_meta_request<L4::Factory>(ios);
114     case L4::Factory::Protocol:
115       if (L4::kobject_typeid<L4Re::Console>()->
116             has_proto(L4::Ipc::read<L4::Factory::Proto>(ios)))
117         {
118           L4::Ipc::Varg opt;
119           ios.get(&opt);
120
121           if (!opt.is_of<char const *>())
122             return -L4_EINVAL;
123
124           char _msg[50];
125           int _l = sizeof(_msg) - 1;
126
127           _l =  std::min(_l, opt.length());
128           strncpy(_msg, opt.value<char const *>(), _l);
129           _msg[_l] = 0;
130
131           return create(_msg, ios);
132         }
133       return -L4_ENODEV;
134     default:
135       return -L4_EBADPROTO;
136     }
137 }
138
139 void
140 Service::destroy()
141 {
142   printf("MAG: destroying the fb_client_service, holla\n");
143 }
144
145 Service::~Service()
146 {
147   printf("MAG: destroy FB svc\n");
148 }
149
150 static Service _fb_service("frame-buffer");
151
152 }