layer position config, r2 release
[pandora_liveinfo.git] / main.c
diff --git a/main.c b/main.c
index 010f63c..4a8b952 100644 (file)
--- a/main.c
+++ b/main.c
@@ -10,6 +10,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
+#include <ctype.h>
 #include <pthread.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #define SCREEN_HEIGHT 480
 #define WIDTH 80
 #define HEIGHT 480
-#define MEM_SIZE (((WIDTH * HEIGHT * 2 * 2) + 0xfff) & ~0xfff)
 #define Y_STEP 9
 
 static struct fb_var_screeninfo g_vi;
 static uint16_t *g_screen_base, *g_screen;
+static struct {
+  int x, y, w, h;
+} g_layer;
+static unsigned int g_mem_size;
 static unsigned int g_old_mem;
 static int g_flip_id;
 static int g_exit, g_hide;
 static pthread_cond_t g_cond;
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+#define PAGE_ALIGN(x) (((x) + 0xfff) & ~0xfff)
 #define IS_START(buf, str) \
   !strncmp(buf, str, sizeof(str) - 1)
 
@@ -78,8 +83,9 @@ static int setup_layer(int fd)
   }
 
   g_old_mem = mi.size;
-  if (mi.size < MEM_SIZE) {
-    mi.size = MEM_SIZE;
+  g_mem_size = PAGE_ALIGN(g_layer.w * g_layer.h * 2 * 2);
+  if (mi.size < g_mem_size) {
+    mi.size = g_mem_size;
     ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
     if (ret < 0) {
       perror("ioctl SETUP_MEM");
@@ -87,10 +93,13 @@ static int setup_layer(int fd)
     }
   }
 
-  pi.pos_x = SCREEN_WIDTH - WIDTH;
-  pi.pos_y = 0;
-  pi.out_width = WIDTH;
-  pi.out_height = HEIGHT;
+  printf("layer: %d %d %d %d\n",
+    g_layer.x, g_layer.y, g_layer.w, g_layer.h);
+
+  pi.pos_x = g_layer.x;
+  pi.pos_y = g_layer.y;
+  pi.out_width = g_layer.w;
+  pi.out_height = g_layer.h;
   ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
   if (ret < 0) {
     perror("ioctl OMAPFB_SETUP_PLANE (e1)");
@@ -103,9 +112,9 @@ static int setup_layer(int fd)
     return 1;
   }
 
-  g_vi.xres = g_vi.xres_virtual = WIDTH;
-  g_vi.yres = HEIGHT;
-  g_vi.yres_virtual = HEIGHT * 2;
+  g_vi.xres = g_vi.xres_virtual = g_layer.w;
+  g_vi.yres = g_layer.h;
+  g_vi.yres_virtual = g_layer.h * 2;
   g_vi.bits_per_pixel = 16;
 
   ret = ioctl(fd, FBIOPUT_VSCREENINFO, &g_vi);
@@ -131,14 +140,14 @@ static int setup_layer(int fd)
     return 1;
   }
 
-  g_screen_base = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE,
+  g_screen_base = mmap(NULL, g_mem_size, PROT_READ | PROT_WRITE,
                   MAP_SHARED, fd, 0);
   if (g_screen_base == MAP_FAILED) {
     perror("mmap");
     g_screen = g_screen_base = NULL;
     return 1;
   }
-  clear_bytes(g_screen_base, MEM_SIZE);
+  clear_bytes(g_screen_base, g_mem_size);
   g_screen = g_screen_base;
   g_flip_id = 0;
 
@@ -171,7 +180,7 @@ static void remove_layer(int fd)
   int ret;
 
   if (g_screen_base != NULL) {
-    munmap(g_screen_base, MEM_SIZE);
+    munmap(g_screen_base, g_mem_size);
     g_screen = g_screen_base = NULL;
   }
 
@@ -201,10 +210,10 @@ out:;
 
 static void flip_fb(int fd)
 {
-  g_vi.yoffset = g_flip_id * HEIGHT;
+  g_vi.yoffset = g_flip_id * g_layer.h;
   ioctl(fd, FBIOPAN_DISPLAY, &g_vi);
   g_flip_id ^= 1;
-  g_screen = g_screen_base + g_flip_id * WIDTH * HEIGHT;
+  g_screen = g_screen_base + g_flip_id * g_layer.w * g_layer.h;
 }
 
 static void handle_term(int num)
@@ -283,8 +292,65 @@ static int handle_socket(int sock)
   return 0;
 }
 
-#define s_printf(x, y, fmt, ...) \
-  basic_text_out16(g_screen, WIDTH, x, y, fmt, ##__VA_ARGS__);
+static void default_config(void)
+{
+  g_layer.x = SCREEN_WIDTH - WIDTH;
+  g_layer.y = 0;
+  g_layer.w = WIDTH;
+  g_layer.h = HEIGHT;
+}
+
+static void handle_config(void)
+{
+  char buf[256], *p;
+  int x, y, v;
+  FILE *f;
+
+  default_config();
+
+  f = fopen("config.cfg", "r");
+  if (f == NULL)
+    return;
+
+  while ((p = fgets(buf, sizeof(buf), f))) {
+    while (isspace(*p))
+      p++;
+    if (*p == '#' || *p == ';' || *p == 0)
+      continue;
+
+    if (sscanf(p, "position = %d %d", &x, &y) == 2) {
+      g_layer.x = x;
+      g_layer.y = y;
+    }
+    else {
+      printf("unhandled config entry: '%s'\n", p);
+    }
+  }
+  fclose(f);
+
+  v = SCREEN_WIDTH - g_layer.x;
+  if (v <= 0) {
+    printf("bad x position in config: %d\n", g_layer.x);
+    default_config();
+    return;
+  }
+  if (v < WIDTH)
+    g_layer.w = v;
+
+  v = SCREEN_HEIGHT - g_layer.y;
+  if (v <= 0) {
+    printf("bad y position in config: %d\n", g_layer.y);
+    default_config();
+    return;
+  }
+  if (v < HEIGHT)
+    g_layer.h = v;
+}
+
+#define s_printf(x, y, fmt, ...) do { \
+  if ((y) <= g_layer.h - 8) \
+    basic_text_out16(g_screen, g_layer.w, x, y, fmt, ##__VA_ARGS__); \
+} while (0)
 
 static int read_int_file(const char *fn, int *val)
 {
@@ -316,7 +382,10 @@ static void clear_bytes(void *mem, int bytes)
 
 static void clear(int h)
 {
-  clear_bytes(g_screen, WIDTH * h * 2);
+  if (h > g_layer.h)
+    h = g_layer.h;
+
+  clear_bytes(g_screen, g_layer.w * h * 2);
 }
 
 struct proc_stat {
@@ -766,6 +835,9 @@ int main(int argc, char *argv[])
     return ret == 4 ? 0 : 1;
   }
 
+  // load the config
+  handle_config();
+
   fd = open(fbname, O_RDWR);
   if (fd == -1) {
     fprintf(stderr, "open %s: ", fbname);