Index: uspace/app/barber/barber.c
===================================================================
--- uspace/app/barber/barber.c	(revision a047aaa4124e2925a3f142f3c586465498393142)
+++ uspace/app/barber/barber.c	(revision 1ab853969f4ab4bd3b894a43444959889f64873d)
@@ -40,4 +40,5 @@
 #include <task.h>
 #include <loc.h>
+#include <stats.h>
 #include <fibril_synch.h>
 #include <io/pixel.h>
@@ -53,18 +54,29 @@
 #define FRAMES  30
 
+#define MIN_FPS  1
+#define MAX_FPS  25
+
+#define MIN_LOAD  (LOAD_UNIT / 4)
+#define MAX_LOAD  (LOAD_UNIT / 3)
+
 #define FRAME_WIDTH   59
 #define FRAME_HEIGHT  192
 
-#define PERIOD  1000000
-#define COLORS  7
+#define LED_PERIOD  1000000
+#define LED_COLORS  7
+
+typedef struct {
+	link_t link;
+	service_id_t svc_id;
+	async_sess_t *sess;
+} led_dev_t;
 
 static char *winreg = NULL;
-static fibril_timer_t *timer = NULL;
+
+static fibril_timer_t *led_timer = NULL;
 static list_t led_devs;
-
-static canvas_t *frame_canvas;
-static surface_t *frames[FRAMES];
-
-static pixel_t colors[COLORS] = {
+static unsigned int led_color = 0;
+
+static pixel_t led_colors[LED_COLORS] = {
 	PIXEL(0xff, 0xff, 0x00, 0x00),
 	PIXEL(0xff, 0x00, 0xff, 0x00),
@@ -76,76 +88,13 @@
 };
 
+static fibril_timer_t *frame_timer = NULL;
+static canvas_t *frame_canvas;
+static surface_t *frames[FRAMES];
+
 static unsigned int frame = 0;
-static unsigned int color = 0;
-
-typedef struct {
-	link_t link;
-	service_id_t svc_id;
-	async_sess_t *sess;
-} led_dev_t;
-
-static void timer_callback(void *data)
-{
-	pixel_t next_color = colors[color];
-	
-	color++;
-	if (color >= COLORS)
-		color = 0;
-	
-	list_foreach(led_devs, link, led_dev_t, dev) {
-		if (dev->sess)
-			led_dev_color_set(dev->sess, next_color);
-	}
-	
-	frame++;
-	if (frame >= FRAMES)
-		frame = 0;
-	
-	update_canvas(frame_canvas, frames[frame]);
-	
-	fibril_timer_set(timer, PERIOD, timer_callback, NULL);
-}
-
-static void loc_callback(void)
-{
-	category_id_t led_cat;
-	int rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
-	if (rc != EOK)
-		return;
-	
-	service_id_t *svcs;
-	size_t count;
-	rc = loc_category_get_svcs(led_cat, &svcs, &count);
-	if (rc != EOK)
-		return;
-	
-	for (size_t i = 0; i < count; i++) {
-		bool known = false;
-		
-		/* Determine whether we already know this device. */
-		list_foreach(led_devs, link, led_dev_t, dev) {
-			if (dev->svc_id == svcs[i]) {
-				known = true;
-				break;
-			}
-		}
-		
-		if (!known) {
-			led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
-			if (!dev)
-				continue;
-			
-			link_initialize(&dev->link);
-			dev->svc_id = svcs[i];
-			dev->sess = loc_service_connect(EXCHANGE_SERIALIZE, svcs[i], 0);
-			
-			list_append(&dev->link, &led_devs);
-		}
-	}
-	
-	// FIXME: Handle LED device removal
-	
-	free(svcs);
-}
+static unsigned int fps = MIN_FPS;
+
+static void led_timer_callback(void *);
+static void frame_timer_callback(void *);
 
 static bool decode_frames(void)
@@ -192,4 +141,141 @@
 }
 
+static void plan_led_timer(void)
+{
+	fibril_timer_set(led_timer, LED_PERIOD, led_timer_callback, NULL);
+}
+
+static load_t get_load(void)
+{
+	size_t count;
+	load_t *load = stats_get_load(&count);
+	load_t load_val;
+	
+	if ((load != NULL) && (count > 0)) {
+		load_val = load[0];
+		free(load);
+	} else
+		load_val = 0;
+	
+	return load_val;
+}
+
+static void plan_frame_timer(suseconds_t render_time)
+{
+	/*
+	 * Crank up the FPS unless we lack
+	 * behind with the rendering and
+	 * unless the load is not above
+	 * a lower threshold.
+	 */
+	
+	suseconds_t delta = 1000000 / fps;
+	load_t load = get_load();
+	
+	if ((delta >= render_time) && (load < MIN_LOAD))
+		fps++;
+	
+	if (fps > MAX_FPS)
+		fps = MAX_FPS;
+	
+	/*
+	 * If we lack behind then immediately
+	 * go to the lowest FPS.
+	 */
+	
+	if (delta < render_time)
+		fps = MIN_FPS;
+	
+	/*
+	 * Crank down the FPS if the current
+	 * load is above an upper threshold.
+	 */
+	
+	if (load > MAX_LOAD)
+		fps--;
+	
+	if (fps < MIN_FPS)
+		fps = MIN_FPS;
+	
+	delta = 1000000 / fps;
+	
+	fibril_timer_set(frame_timer, delta, frame_timer_callback, NULL);
+}
+
+static void led_timer_callback(void *data)
+{
+	pixel_t next_led_color = led_colors[led_color];
+	
+	led_color++;
+	if (led_color >= LED_COLORS)
+		led_color = 0;
+	
+	list_foreach(led_devs, link, led_dev_t, dev) {
+		if (dev->sess)
+			led_dev_color_set(dev->sess, next_led_color);
+	}
+	
+	plan_led_timer();
+}
+
+static void frame_timer_callback(void *data)
+{
+	struct timeval prev;
+	getuptime(&prev);
+	
+	frame++;
+	if (frame >= FRAMES)
+		frame = 0;
+	
+	update_canvas(frame_canvas, frames[frame]);
+	
+	struct timeval cur;
+	getuptime(&cur);
+	
+	plan_frame_timer(tv_sub(&cur, &prev));
+}
+
+static void loc_callback(void)
+{
+	category_id_t led_cat;
+	int rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
+	if (rc != EOK)
+		return;
+	
+	service_id_t *svcs;
+	size_t count;
+	rc = loc_category_get_svcs(led_cat, &svcs, &count);
+	if (rc != EOK)
+		return;
+	
+	for (size_t i = 0; i < count; i++) {
+		bool known = false;
+		
+		/* Determine whether we already know this device. */
+		list_foreach(led_devs, link, led_dev_t, dev) {
+			if (dev->svc_id == svcs[i]) {
+				known = true;
+				break;
+			}
+		}
+		
+		if (!known) {
+			led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
+			if (!dev)
+				continue;
+			
+			link_initialize(&dev->link);
+			dev->svc_id = svcs[i];
+			dev->sess = loc_service_connect(EXCHANGE_SERIALIZE, svcs[i], 0);
+			
+			list_append(&dev->link, &led_devs);
+		}
+	}
+	
+	// FIXME: Handle LED device removal
+	
+	free(svcs);
+}
+
 int main(int argc, char *argv[])
 {
@@ -206,7 +292,13 @@
 	}
 	
-	timer = fibril_timer_create(NULL);
-	if (!timer) {
-		printf("Unable to create timer.\n");
+	led_timer = fibril_timer_create(NULL);
+	if (!led_timer) {
+		printf("Unable to create LED timer.\n");
+		return 1;
+	}
+	
+	frame_timer = fibril_timer_create(NULL);
+	if (!frame_timer) {
+		printf("Unable to create frame timer.\n");
 		return 1;
 	}
@@ -235,5 +327,6 @@
 	window_exec(main_window);
 	
-	fibril_timer_set(timer, PERIOD, timer_callback, NULL);
+	plan_led_timer();
+	plan_frame_timer(0);
 	
 	task_retval(0);
