2 * Copyright (c) 2014 Martin Decky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup barber
42 #include <fibril_synch.h>
44 #include <device/led_dev.h>
48 #include <codec/tga.gz.h>
58 #define MIN_LOAD (LOAD_UNIT / 4)
59 #define MAX_LOAD (LOAD_UNIT / 3)
61 #define FRAME_WIDTH 59
62 #define FRAME_HEIGHT 192
64 #define LED_PERIOD 1000000
73 static char *winreg
= NULL
;
75 static fibril_timer_t
*led_timer
= NULL
;
76 static list_t led_devs
;
77 static unsigned int led_color
= 0;
79 static 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)
89 static fibril_timer_t
*frame_timer
= NULL
;
90 static canvas_t
*frame_canvas
;
91 static surface_t
*frames
[FRAMES
];
93 static unsigned int frame
= 0;
94 static unsigned int fps
= MIN_FPS
;
96 static void led_timer_callback(void *);
97 static void frame_timer_callback(void *);
99 static bool decode_frames(void)
101 for (unsigned int i
= 0; i
< FRAMES
; i
++) {
102 frames
[i
] = decode_tga_gz(images
[i
].addr
, images
[i
].size
, 0);
103 if (frames
[i
] == NULL
) {
104 printf("Unable to decode frame %u.\n", i
);
112 static void plan_led_timer(void)
114 fibril_timer_set(led_timer
, LED_PERIOD
, led_timer_callback
, NULL
);
117 static load_t
get_load(void)
120 load_t
*load
= stats_get_load(&count
);
123 if ((load
!= NULL
) && (count
> 0)) {
132 static void plan_frame_timer(usec_t render_time
)
135 * Crank up the FPS unless we lack
136 * behind with the rendering and
137 * unless the load is not above
141 usec_t delta
= 1000000 / fps
;
142 load_t load
= get_load();
144 if ((delta
>= render_time
) && (load
< MIN_LOAD
))
151 * If we lack behind then immediately
152 * go to the lowest FPS.
155 if (delta
< render_time
)
159 * Crank down the FPS if the current
160 * load is above an upper threshold.
169 delta
= 1000000 / fps
;
171 fibril_timer_set(frame_timer
, delta
, frame_timer_callback
, NULL
);
174 static void led_timer_callback(void *data
)
176 pixel_t next_led_color
= led_colors
[led_color
];
179 if (led_color
>= LED_COLORS
)
182 list_foreach(led_devs
, link
, led_dev_t
, dev
) {
184 led_dev_color_set(dev
->sess
, next_led_color
);
190 static void frame_timer_callback(void *data
)
192 struct timespec prev
;
199 update_canvas(frame_canvas
, frames
[frame
]);
204 plan_frame_timer(NSEC2USEC(ts_sub_diff(&cur
, &prev
)));
207 static void loc_callback(void *arg
)
209 category_id_t led_cat
;
210 errno_t rc
= loc_category_get_id("led", &led_cat
, IPC_FLAG_BLOCKING
);
216 rc
= loc_category_get_svcs(led_cat
, &svcs
, &count
);
220 for (size_t i
= 0; i
< count
; i
++) {
223 /* Determine whether we already know this device. */
224 list_foreach(led_devs
, link
, led_dev_t
, dev
) {
225 if (dev
->svc_id
== svcs
[i
]) {
232 led_dev_t
*dev
= (led_dev_t
*) calloc(1, sizeof(led_dev_t
));
236 link_initialize(&dev
->link
);
237 dev
->svc_id
= svcs
[i
];
238 dev
->sess
= loc_service_connect(svcs
[i
], INTERFACE_DDF
, 0);
240 list_append(&dev
->link
, &led_devs
);
244 // FIXME: Handle LED device removal
249 int main(int argc
, char *argv
[])
252 printf("Compositor server not specified.\n");
256 list_initialize(&led_devs
);
257 errno_t rc
= loc_register_cat_change_cb(loc_callback
, NULL
);
259 printf("Unable to register callback for device discovery.\n");
263 led_timer
= fibril_timer_create(NULL
);
265 printf("Unable to create LED timer.\n");
269 frame_timer
= fibril_timer_create(NULL
);
271 printf("Unable to create frame timer.\n");
275 if (!decode_frames())
279 window_t
*main_window
= window_open(argv
[1], NULL
,
280 WINDOW_MAIN
| WINDOW_DECORATED
, "barber");
282 printf("Cannot open main window.\n");
286 frame_canvas
= create_canvas(window_root(main_window
), NULL
,
287 FRAME_WIDTH
, FRAME_HEIGHT
, frames
[frame
]);
290 window_close(main_window
);
291 printf("Cannot create widgets.\n");
295 window_resize(main_window
, 0, 0, FRAME_WIDTH
+ 8, FRAME_HEIGHT
+ 28,
296 WINDOW_PLACEMENT_RIGHT
| WINDOW_PLACEMENT_BOTTOM
);
297 window_exec(main_window
);