3 * \brief fb usage demo, with C and C++
4 * \author Adam Lackorzynski <adam@os.inf.tu-dresden.de>
7 /* (c) 2010, Adam Lackorzynski
8 * economic rights: Technische Universität Dresden (Germany)
9 * This file is part of the con package, which is distributed under
10 * the terms of the GNU General Public License 2. Please see the
11 * COPYING file for details. */
14 * Event handling in C mode has not been done.
20 #include <l4/re/util/video/goos_fb>
21 #include <l4/re/util/event>
22 #include <l4/event/event>
24 #include <l4/re/c/util/video/goos_fb.h>
27 #include <l4/re/event_enums.h>
28 #include <l4/util/keymap.h>
35 static L4Re::Util::Video::Goos_fb gfb;
36 static L4Re::Video::View::Info fbi;
37 static L4Re::Util::Event event;
39 static l4re_util_video_goos_fb_t gfb;
40 static l4re_video_view_info_t fbi;
43 static void *fbmem_vaddr;
46 static void put_pixel(int x, int y, int fullval)
50 unsigned long offset = (unsigned long)fbmem_vaddr + y * fbi.bytes_per_line + x * fbi.pixel_info.bytes_per_pixel();
51 v = ((fullval >> (8 - fbi.pixel_info.r().size())) & ((1 << fbi.pixel_info.r().size()) - 1)) << fbi.pixel_info.r().shift();
52 v |= ((fullval >> (16 - fbi.pixel_info.g().size())) & ((1 << fbi.pixel_info.g().size()) - 1)) << fbi.pixel_info.g().shift();
53 v |= ((fullval >> (24 - fbi.pixel_info.b().size())) & ((1 << fbi.pixel_info.b().size()) - 1)) << fbi.pixel_info.b().shift();
55 unsigned bpp = l4re_video_bits_per_pixel(&fbi.pixel_info);
56 unsigned long offset = (unsigned long)fbmem_vaddr + y * fbi.bytes_per_line + x * fbi.pixel_info.bytes_per_pixel;
57 v = ((fullval >> (8 - fbi.pixel_info.r.size)) & ((1 << fbi.pixel_info.r.size) - 1)) << fbi.pixel_info.r.shift;
58 v |= ((fullval >> (16 - fbi.pixel_info.g.size)) & ((1 << fbi.pixel_info.g.size) - 1)) << fbi.pixel_info.g.shift;
59 v |= ((fullval >> (24 - fbi.pixel_info.b.size)) & ((1 << fbi.pixel_info.b.size) - 1)) << fbi.pixel_info.b.shift;
64 case 8: *(unsigned char *)offset = v; break;
65 case 14: case 15: case 16: *(unsigned short *)offset = v; break;
66 case 24: case 32: *(unsigned int *)offset = v; break;
68 printf("unhandled bitsperpixel %d\n", bpp);
72 static void update_rect(int x, int y, int w, int h)
75 gfb.refresh(x, y, w, h);
77 l4re_util_video_goos_fb_refresh(&gfb, x, y, w, h);
81 static inline unsigned color_val(unsigned w, unsigned peak_point, unsigned val)
83 unsigned third = w / 3;
85 unsigned a = abs(val - peak_point);
87 a = peak_point + w - val;
91 return ((third - a) * 255) / third;
95 static void ev_hdl_func(void *data)
98 while (L4Re::Event_buffer::Event *e = event.buffer().next())
101 printf("Event: %16lld: %d %d %d\n",
102 e->time, e->payload.type, e->payload.code, e->payload.value);
103 if (e->payload.type == L4RE_EV_KEY
104 && ((k = l4util_map_event_to_keymap(e->payload.code, 0)) >= 32))
105 printf(" key: %c\n", k);
106 // proper mouse and keyboard handling code comes here
115 try { gfb.setup("fb"); } catch (...) { return 1; }
116 if (gfb.view_info(&fbi))
119 if (!(fbmem_vaddr = gfb.attach_buffer()))
122 bpp = fbi.pixel_info.bits_per_pixel();
124 if (event.init(L4::cap_dynamic_cast<L4Re::Event>(gfb.goos())))
127 Event::Event event_hdl(event.irq(), ev_hdl_func, NULL, 4);
128 if (!event_hdl.attached())
131 if (l4re_util_video_goos_fb_setup_name(&gfb, "fb"))
134 if (l4re_util_video_goos_fb_view_info(&gfb, &fbi))
137 if (!(fbmem_vaddr = l4re_util_video_goos_fb_attach_buffer(&gfb)))
140 bpp = l4re_video_bits_per_pixel(&fbi.pixel_info);
143 printf("x:%ld y:%ld bit/pixel:%d bytes/line:%ld\n",
144 fbi.width, fbi.width, bpp, fbi.bytes_per_line);
146 // now some fancy stuff
147 unsigned w = fbi.width;
148 unsigned h = fbi.height;
151 for (unsigned cnt = 0; ; cnt += 2)
153 for (unsigned y = 0; y < h; ++y)
154 for (unsigned x = 0; x < w; ++x)
156 unsigned r = color_val(w, 0 * t, (x + cnt) % w);
157 unsigned g = color_val(h, 1 * t, (y + (cnt >> 1)) % h);
158 unsigned b = color_val(w, 2 * t, (w - x + cnt) % w);
160 //printf("%3d: %3d:%3d:%3d\n", x, r, g, b);
161 put_pixel(x, y, (r << 0) | (g << 8) | (b << 16));
164 update_rect(0, 0, fbi.width, fbi.height);