refactor plat.h, add gamma
authornotaz <notasas@gmail.com>
Sun, 4 Nov 2012 22:02:03 +0000 (00:02 +0200)
committernotaz <notasas@gmail.com>
Sun, 4 Nov 2012 22:02:03 +0000 (00:02 +0200)
pandora/plat.c
plat.h

index 4fff936..f97cb83 100644 (file)
@@ -105,88 +105,112 @@ static void scan_for_filters(void)
        }
        closedir(dir);
 
-       plat_target.filters = mfilters;
+       plat_target.hwfilters = mfilters;
 }
 
-static void set_lcdrate(int is_pal)
+static int do_system(const char *cmd)
+{
+       int ret;
+
+       ret = system(cmd);
+       if (ret >= 0)
+               ret = 0;
+
+       return ret;
+}
+
+static int read_int_from_file(const char *fname)
+{
+       int ret = -1;
+       FILE *f;
+
+       f = fopen(fname, "r");
+       if (f) {
+               fscanf(f, "%d", &ret);
+               fclose(f);
+       }
+       return ret;
+}
+
+static int lcdrate_set(int is_pal)
 {
        static int old_pal = -1;
        char buf[128];
 
        if (is_pal == old_pal)
-               return;
+               return 0;
+       old_pal = is_pal;
 
        snprintf(buf, sizeof(buf), "%s/op_lcdrate.sh %d",
                        pnd_script_base, is_pal ? 50 : 60);
-       system(buf);
-       old_pal = is_pal;
+       return do_system(buf);
 }
 
-static void set_filter(int which)
+static int hwfilter_set(int which)
 {
        static int old_filter = -1;
        char buf[128];
        int i;
 
-       if (plat_target.filters == NULL || which == old_filter)
-               return;
+       if (plat_target.hwfilters == NULL)
+               return -1;
 
-       for (i = 0; i < which; i++)
-               if (plat_target.filters[i] == NULL)
-                       return;
+       if (which == old_filter)
+               return 0;
 
-       if (plat_target.filters[i] == NULL)
-               return;
+       for (i = 0; i <= which; i++)
+               if (plat_target.hwfilters[i] == NULL)
+                       return -1;
 
-       snprintf(buf, sizeof(buf), "%s/op_videofir.sh %s",
-               pnd_script_base, plat_target.filters[i]);
-       system(buf);
        old_filter = which;
+
+       snprintf(buf, sizeof(buf), "%s/op_videofir.sh %s",
+               pnd_script_base, plat_target.hwfilters[which]);
+       return do_system(buf);
 }
 
 static int cpu_clock_get(void)
 {
-       FILE *f;
-       int ret = 0;
-       f = fopen("/proc/pandora/cpu_mhz_max", "r");
-       if (f) {
-               fscanf(f, "%d", &ret);
-               fclose(f);
-       }
-       return ret;
+       return read_int_from_file("/proc/pandora/cpu_mhz_max");
 }
 
 static int cpu_clock_set(int cpu_clock)
 {
        char buf[128];
 
-       if (cpu_clock != 0 && cpu_clock != cpu_clock_get()) {
-               snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
-                        pnd_script_base, cpu_clock);
-               system(buf);
-       }
-       return 0;
+       if (cpu_clock < 14)
+               return -1;
+
+       if (cpu_clock == cpu_clock_get())
+               return 0;
+
+       snprintf(buf, sizeof(buf),
+                "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
+                pnd_script_base, cpu_clock);
+       return do_system(buf);
 }
 
-static int get_bat_capacity(void)
+static int bat_capacity_get(void)
 {
-       FILE *f;
-       int ret = 0;
-       f = fopen("/sys/class/power_supply/bq27500-0/capacity", "r");
-       if (f) {
-               fscanf(f, "%d", &ret);
-               fclose(f);
-       }
-       return ret;
+       return read_int_from_file("/sys/class/power_supply/bq27500-0/capacity");
+}
+
+static int gamma_set(int val, int black_level)
+{
+       char buf[128];
+
+       snprintf(buf, sizeof(buf), "%s/op_gamma.sh -b %d %.2f",
+                pnd_script_base, black_level, (float)val / 100.0f);
+       return do_system(buf);
 }
 
 struct plat_target plat_target = {
        cpu_clock_get,
        cpu_clock_set,
-       get_bat_capacity,
-       set_filter,
-       NULL,
-       set_lcdrate,
+       bat_capacity_get,
+       hwfilter_set,
+       lcdrate_set,
+       gamma_set,
 };
 
 int plat_target_init(void)
diff --git a/plat.h b/plat.h
index f502a40..139ea47 100644 (file)
--- a/plat.h
+++ b/plat.h
@@ -9,11 +9,12 @@ extern "C" {
 struct plat_target {
        int (*cpu_clock_get)(void);
        int (*cpu_clock_set)(int clock);
-       int (*get_bat_capacity)(void);
-       void (*set_filter)(int which);
-       char **filters;
-       void (*set_lcdrate)(int is_pal);
-       void (*step_volume)(int is_up);
+       int (*bat_capacity_get)(void);
+       int (*hwfilter_set)(int which);
+       int (*lcdrate_set)(int is_pal);
+       int (*gamma_set)(int val, int black_level);
+       int (*step_volume)(int is_up);
+       char **hwfilters;
 };
 
 extern struct plat_target plat_target;
@@ -21,16 +22,59 @@ int  plat_target_init(void);
 void plat_target_finish(void);
 void plat_target_setup_input(void);
 
-static __inline void plat_target_set_filter(int which)
+/* CPU clock in MHz */
+static __inline int plat_target_cpu_clock_get(void)
 {
-       if (plat_target.set_filter)
-               plat_target.set_filter(which);
+       if (plat_target.cpu_clock_get)
+               return plat_target.cpu_clock_get();
+       return -1;
 }
 
-static __inline void plat_target_set_lcdrate(int is_pal)
+static __inline int plat_target_cpu_clock_set(int mhz)
 {
-       if (plat_target.set_lcdrate)
-               plat_target.set_lcdrate(is_pal);
+       if (plat_target.cpu_clock_set)
+               return plat_target.cpu_clock_set(mhz);
+       return -1;
+}
+
+/* battery capacity (0-100) */
+static __inline int plat_target_bat_capacity_get(void)
+{
+       if (plat_target.bat_capacity_get)
+               return plat_target.bat_capacity_get();
+       return -1;
+}
+
+/* set some hardware-specific video filter, 0 for none */
+static __inline int plat_target_hwfilter_set(int which)
+{
+       if (plat_target.hwfilter_set)
+               return plat_target.hwfilter_set(which);
+       return -1;
+}
+
+/* set device LCD rate, is_pal 0 for NTSC, 1 for PAL compatible */
+static __inline int plat_target_lcdrate_set(int is_pal)
+{
+       if (plat_target.lcdrate_set)
+               return plat_target.lcdrate_set(is_pal);
+       return -1;
+}
+
+/* set device LCD rate, is_pal 0 for NTSC, 1 for PAL compatible */
+static __inline int plat_target_gamma_set(int val, int black_level)
+{
+       if (plat_target.gamma_set)
+               return plat_target.gamma_set(val, black_level);
+       return -1;
+}
+
+/* step sound volume up or down */
+static __inline int plat_target_step_volume(int is_up)
+{
+       if (plat_target.step_volume)
+               return plat_target.step_volume(is_up);
+       return -1;
 }
 
 /* menu: enter (switch bpp, etc), begin/end drawing */