source: mainline/uspace/app/barber/barber.c@ 867b375

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 867b375 was 582a0b8, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove unistd.h

  • Rename usleep() and sleep() to thread_usleep() and thread_sleep() and move to thread.[hc].
  • Include stddef.h in order to provide NULL.
  • Move getpagesize() to libposix.
  • Sync uspace/dist/src/c/demos with originals.
  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*
2 * Copyright (c) 2014 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup barber
30 * @{
31 */
32/** @file
33 */
34
35#include <stdbool.h>
36#include <errno.h>
37#include <stdio.h>
38#include <malloc.h>
39#include <task.h>
40#include <loc.h>
41#include <stats.h>
42#include <fibril_synch.h>
43#include <io/pixel.h>
44#include <device/led_dev.h>
45#include <window.h>
46#include <canvas.h>
47#include <surface.h>
48#include <codec/tga.gz.h>
49#include "images.h"
50
51#define NAME "barber"
52
53#define FRAMES 30
54
55#define MIN_FPS 1
56#define MAX_FPS 25
57
58#define MIN_LOAD (LOAD_UNIT / 4)
59#define MAX_LOAD (LOAD_UNIT / 3)
60
61#define FRAME_WIDTH 59
62#define FRAME_HEIGHT 192
63
64#define LED_PERIOD 1000000
65#define LED_COLORS 7
66
67typedef struct {
68 link_t link;
69 service_id_t svc_id;
70 async_sess_t *sess;
71} led_dev_t;
72
73static char *winreg = NULL;
74
75static fibril_timer_t *led_timer = NULL;
76static list_t led_devs;
77static unsigned int led_color = 0;
78
79static pixel_t led_colors[LED_COLORS] = {
80 PIXEL(0xff, 0xff, 0x00, 0x00),
81 PIXEL(0xff, 0x00, 0xff, 0x00),
82 PIXEL(0xff, 0x00, 0x00, 0xff),
83 PIXEL(0xff, 0xff, 0xff, 0x00),
84 PIXEL(0xff, 0xff, 0x00, 0xff),
85 PIXEL(0xff, 0x00, 0xff, 0xff),
86 PIXEL(0xff, 0xff, 0xff, 0xff)
87};
88
89static fibril_timer_t *frame_timer = NULL;
90static canvas_t *frame_canvas;
91static surface_t *frames[FRAMES];
92
93static unsigned int frame = 0;
94static unsigned int fps = MIN_FPS;
95
96static void led_timer_callback(void *);
97static void frame_timer_callback(void *);
98
99static bool decode_frames(void)
100{
101 frames[0] = decode_tga_gz((void *) frame01_tga_gz, frame01_tga_gz_size, 0);
102 frames[1] = decode_tga_gz((void *) frame02_tga_gz, frame02_tga_gz_size, 0);
103 frames[2] = decode_tga_gz((void *) frame03_tga_gz, frame03_tga_gz_size, 0);
104 frames[3] = decode_tga_gz((void *) frame04_tga_gz, frame04_tga_gz_size, 0);
105 frames[4] = decode_tga_gz((void *) frame05_tga_gz, frame05_tga_gz_size, 0);
106 frames[5] = decode_tga_gz((void *) frame06_tga_gz, frame06_tga_gz_size, 0);
107 frames[6] = decode_tga_gz((void *) frame07_tga_gz, frame07_tga_gz_size, 0);
108 frames[7] = decode_tga_gz((void *) frame08_tga_gz, frame08_tga_gz_size, 0);
109 frames[8] = decode_tga_gz((void *) frame09_tga_gz, frame09_tga_gz_size, 0);
110 frames[9] = decode_tga_gz((void *) frame10_tga_gz, frame10_tga_gz_size, 0);
111 frames[10] = decode_tga_gz((void *) frame11_tga_gz, frame11_tga_gz_size, 0);
112 frames[11] = decode_tga_gz((void *) frame12_tga_gz, frame12_tga_gz_size, 0);
113 frames[12] = decode_tga_gz((void *) frame13_tga_gz, frame13_tga_gz_size, 0);
114 frames[13] = decode_tga_gz((void *) frame14_tga_gz, frame14_tga_gz_size, 0);
115 frames[14] = decode_tga_gz((void *) frame15_tga_gz, frame15_tga_gz_size, 0);
116 frames[15] = decode_tga_gz((void *) frame16_tga_gz, frame16_tga_gz_size, 0);
117 frames[16] = decode_tga_gz((void *) frame17_tga_gz, frame17_tga_gz_size, 0);
118 frames[17] = decode_tga_gz((void *) frame18_tga_gz, frame18_tga_gz_size, 0);
119 frames[18] = decode_tga_gz((void *) frame19_tga_gz, frame19_tga_gz_size, 0);
120 frames[19] = decode_tga_gz((void *) frame20_tga_gz, frame20_tga_gz_size, 0);
121 frames[20] = decode_tga_gz((void *) frame21_tga_gz, frame21_tga_gz_size, 0);
122 frames[21] = decode_tga_gz((void *) frame22_tga_gz, frame22_tga_gz_size, 0);
123 frames[22] = decode_tga_gz((void *) frame23_tga_gz, frame23_tga_gz_size, 0);
124 frames[23] = decode_tga_gz((void *) frame24_tga_gz, frame24_tga_gz_size, 0);
125 frames[24] = decode_tga_gz((void *) frame25_tga_gz, frame25_tga_gz_size, 0);
126 frames[25] = decode_tga_gz((void *) frame26_tga_gz, frame26_tga_gz_size, 0);
127 frames[26] = decode_tga_gz((void *) frame27_tga_gz, frame27_tga_gz_size, 0);
128 frames[27] = decode_tga_gz((void *) frame28_tga_gz, frame28_tga_gz_size, 0);
129 frames[28] = decode_tga_gz((void *) frame29_tga_gz, frame29_tga_gz_size, 0);
130 frames[29] = decode_tga_gz((void *) frame30_tga_gz, frame30_tga_gz_size, 0);
131
132 for (unsigned int frame = 0; frame < FRAMES; frame++) {
133 if (frames[frame] == NULL) {
134 printf("Unable to decode frame %u.\n", frame);
135 return false;
136 }
137 }
138
139 return true;
140}
141
142static void plan_led_timer(void)
143{
144 fibril_timer_set(led_timer, LED_PERIOD, led_timer_callback, NULL);
145}
146
147static load_t get_load(void)
148{
149 size_t count;
150 load_t *load = stats_get_load(&count);
151 load_t load_val;
152
153 if ((load != NULL) && (count > 0)) {
154 load_val = load[0];
155 free(load);
156 } else
157 load_val = 0;
158
159 return load_val;
160}
161
162static void plan_frame_timer(suseconds_t render_time)
163{
164 /*
165 * Crank up the FPS unless we lack
166 * behind with the rendering and
167 * unless the load is not above
168 * a lower threshold.
169 */
170
171 suseconds_t delta = 1000000 / fps;
172 load_t load = get_load();
173
174 if ((delta >= render_time) && (load < MIN_LOAD))
175 fps++;
176
177 if (fps > MAX_FPS)
178 fps = MAX_FPS;
179
180 /*
181 * If we lack behind then immediately
182 * go to the lowest FPS.
183 */
184
185 if (delta < render_time)
186 fps = MIN_FPS;
187
188 /*
189 * Crank down the FPS if the current
190 * load is above an upper threshold.
191 */
192
193 if (load > MAX_LOAD)
194 fps--;
195
196 if (fps < MIN_FPS)
197 fps = MIN_FPS;
198
199 delta = 1000000 / fps;
200
201 fibril_timer_set(frame_timer, delta, frame_timer_callback, NULL);
202}
203
204static void led_timer_callback(void *data)
205{
206 pixel_t next_led_color = led_colors[led_color];
207
208 led_color++;
209 if (led_color >= LED_COLORS)
210 led_color = 0;
211
212 list_foreach(led_devs, link, led_dev_t, dev) {
213 if (dev->sess)
214 led_dev_color_set(dev->sess, next_led_color);
215 }
216
217 plan_led_timer();
218}
219
220static void frame_timer_callback(void *data)
221{
222 struct timeval prev;
223 getuptime(&prev);
224
225 frame++;
226 if (frame >= FRAMES)
227 frame = 0;
228
229 update_canvas(frame_canvas, frames[frame]);
230
231 struct timeval cur;
232 getuptime(&cur);
233
234 plan_frame_timer(tv_sub_diff(&cur, &prev));
235}
236
237static void loc_callback(void)
238{
239 category_id_t led_cat;
240 int rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
241 if (rc != EOK)
242 return;
243
244 service_id_t *svcs;
245 size_t count;
246 rc = loc_category_get_svcs(led_cat, &svcs, &count);
247 if (rc != EOK)
248 return;
249
250 for (size_t i = 0; i < count; i++) {
251 bool known = false;
252
253 /* Determine whether we already know this device. */
254 list_foreach(led_devs, link, led_dev_t, dev) {
255 if (dev->svc_id == svcs[i]) {
256 known = true;
257 break;
258 }
259 }
260
261 if (!known) {
262 led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
263 if (!dev)
264 continue;
265
266 link_initialize(&dev->link);
267 dev->svc_id = svcs[i];
268 dev->sess = loc_service_connect(svcs[i], INTERFACE_DDF, 0);
269
270 list_append(&dev->link, &led_devs);
271 }
272 }
273
274 // FIXME: Handle LED device removal
275
276 free(svcs);
277}
278
279int main(int argc, char *argv[])
280{
281 if (argc < 2) {
282 printf("Compositor server not specified.\n");
283 return 1;
284 }
285
286 list_initialize(&led_devs);
287 int rc = loc_register_cat_change_cb(loc_callback);
288 if (rc != EOK) {
289 printf("Unable to register callback for device discovery.\n");
290 return 1;
291 }
292
293 led_timer = fibril_timer_create(NULL);
294 if (!led_timer) {
295 printf("Unable to create LED timer.\n");
296 return 1;
297 }
298
299 frame_timer = fibril_timer_create(NULL);
300 if (!frame_timer) {
301 printf("Unable to create frame timer.\n");
302 return 1;
303 }
304
305 if (!decode_frames())
306 return 1;
307
308 winreg = argv[1];
309 window_t *main_window = window_open(argv[1], NULL,
310 WINDOW_MAIN | WINDOW_DECORATED, "barber");
311 if (!main_window) {
312 printf("Cannot open main window.\n");
313 return 1;
314 }
315
316 frame_canvas = create_canvas(window_root(main_window), NULL,
317 FRAME_WIDTH, FRAME_HEIGHT, frames[frame]);
318
319 if (!frame_canvas) {
320 window_close(main_window);
321 printf("Cannot create widgets.\n");
322 return 1;
323 }
324
325 window_resize(main_window, 0, 0, FRAME_WIDTH + 8, FRAME_HEIGHT + 28,
326 WINDOW_PLACEMENT_RIGHT | WINDOW_PLACEMENT_BOTTOM);
327 window_exec(main_window);
328
329 plan_led_timer();
330 plan_frame_timer(0);
331
332 task_retval(0);
333 async_manager();
334
335 return 0;
336}
337
338/** @}
339 */
Note: See TracBrowser for help on using the repository browser.