]> Eric's Git Repo - ericdock.git/commitdiff
Moved battery and brightness control to own file
authorEric Wertz <ericdwertz@gmail.com>
Fri, 17 Nov 2017 14:05:12 +0000 (09:05 -0500)
committerEric Wertz <ericdwertz@gmail.com>
Fri, 17 Nov 2017 14:05:12 +0000 (09:05 -0500)
Makefile
battery.c [deleted file]
battery.h [deleted file]
control.c [new file with mode: 0644]
control.h [new file with mode: 0644]
main.c

index 887684f0e8decc70509bf78f2d694d0f9414aabb..08de8b7fb266f3dbae6bb3ba5ea20aa5028846fb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
 CC = gcc
 CFLAGS = -Wunused-result -g -Wall -lm -lX11 -lXext `pkg-config --cflags gtk+-3.0 libwnck-3.0`
 LDFLAGS = `pkg-config --libs gtk+-3.0 libwnck-3.0`
-DEPS = dock_icon.h ericdock.h clock.h drawing.h eric_window.h pager_item.h tooltip_window.h battery.h xutils.h
-OBJ = dock_icon.o tooltip_window.o eric_window.o clock.o main.o pager_item.o drawing.o battery.o xutils.o
+DEPS = dock_icon.h ericdock.h clock.h drawing.h eric_window.h pager_item.h tooltip_window.h control.h xutils.h
+OBJ = dock_icon.o tooltip_window.o eric_window.o clock.o main.o pager_item.o drawing.o control.o xutils.o
 
 %.o: %.c $(DEPS)
        $(CC) $(CFLAGS) -c -o $@ $<
diff --git a/battery.c b/battery.c
deleted file mode 100644 (file)
index fe09940..0000000
--- a/battery.c
+++ /dev/null
@@ -1,57 +0,0 @@
-#include <stdlib.h>
-
-#include "ericdock.h"
-#include "battery.h"
-
-//Battery stuff
-typedef struct {
-    double charge_percent;
-    int status;
-} Battery;
-
-void getbatteryinfo( Battery* bat )
-{
-    FILE* fp_charge_full;
-    FILE* fp_charge_now;
-    FILE* fp_status;
-    int charge_full, charge_now;
-    char buffer[256];
-
-    fp_charge_full = fopen( "/sys/class/power_supply/BAT0/charge_full", "r" );
-    fp_charge_now = fopen( "/sys/class/power_supply/BAT0/charge_now", "r" );
-    fp_status = fopen( "/sys/class/power_supply/BAT0/status", "r" );
-
-    fgets( buffer, 255, fp_charge_full );
-    charge_full = atoi( buffer );
-    fgets( buffer, 255, fp_charge_now );
-    charge_now = atoi( buffer );
-    fgets( buffer, 255, fp_status );
-
-    bat->charge_percent = (double)charge_now / (double)charge_full;
-    if( strcmp( "Charging\n", buffer ) == 0 ) bat->status = 1;
-    if( strcmp( "Discharging\n", buffer ) == 0 ) bat->status = 0;
-
-    fclose( fp_charge_full );
-    fclose( fp_charge_now );
-    fclose( fp_status );
-}
-
-void draw_battery_icon( cairo_t* cr, double x, double y, double height )
-{
-    Battery bat;
-    getbatteryinfo( &bat );
-    //bat.charge_percent * 100.0
-    //bat.status == 1 means charging
-
-    cairo_set_line_width( cr, 2.0 );
-    cairo_rectangle( cr, x+height*0.375, y+height*0.125, height*0.25, height*0.125 );
-    cairo_rectangle( cr, x+height*0.25, y+height*0.25, height*0.5, height*0.75 );
-    cairo_stroke( cr );
-
-    if( bat.charge_percent <= 0.15 )
-        cairo_set_source_rgb( cr, 0.9, 0.0, 0.0 );
-
-    cairo_rectangle( cr, x+height*0.25+2.0, y+height*0.25+(height*0.75*(1.0-bat.charge_percent))+2.0, height*0.5-4.0, height*0.75*bat.charge_percent-4.0  );
-    cairo_fill( cr );
-}
-
diff --git a/battery.h b/battery.h
deleted file mode 100644 (file)
index 5be4640..0000000
--- a/battery.h
+++ /dev/null
@@ -1,3 +0,0 @@
-#pragma once
-
-void draw_battery_icon( cairo_t* cr, double x, double y, double height );
diff --git a/control.c b/control.c
new file mode 100644 (file)
index 0000000..7f3197b
--- /dev/null
+++ b/control.c
@@ -0,0 +1,187 @@
+/*
+ * System control functions
+ *
+ * Battery Icon
+ * Volume
+ * Screen brightness
+ */
+#include <stdlib.h>
+
+#include "ericdock.h"
+#include "control.h"
+#include <math.h>
+
+#define BRIGHTNESS_INTENSITY_FILE "/sys/bus/iio/devices/iio:device0/in_illuminance0_input"
+#define BATTERY_CHARGE_FILE "/sys/class/power_supply/BAT0/charge_full"
+#define KEYBOARD_BACKLIGHT_FILE "/sys/class/backlight/keyboard_backlight/brightness"
+
+int brightness_support;
+
+//Battery stuff
+typedef struct {
+    double charge_percent;
+    int status;
+    double dt;
+    double dc;
+} Battery;
+
+//Brightness stuff
+typedef struct {
+    double scaled;
+    double readings[10];
+    double out;
+    double target;
+    double db;
+    int moving;
+} Brightness;
+
+Brightness brightness;
+
+void getbatteryinfo( Battery* bat )
+{
+    FILE* fp_charge_full;
+    FILE* fp_charge_now;
+    FILE* fp_status;
+    int charge_full, charge_now;
+    char buffer[256];
+
+    fp_charge_full = fopen( "/sys/class/power_supply/BAT0/charge_full", "r" );
+    fp_charge_now = fopen( "/sys/class/power_supply/BAT0/charge_now", "r" );
+    fp_status = fopen( "/sys/class/power_supply/BAT0/status", "r" );
+
+    fgets( buffer, 255, fp_charge_full );
+    charge_full = atoi( buffer );
+    fgets( buffer, 255, fp_charge_now );
+    charge_now = atoi( buffer );
+    fgets( buffer, 255, fp_status );
+
+    bat->charge_percent = (double)charge_now / (double)charge_full;
+    if( strcmp( "Full\n", buffer ) == 0 ) bat->status = 2;
+    if( strcmp( "Charging\n", buffer ) == 0 ) bat->status = 1;
+    if( strcmp( "Discharging\n", buffer ) == 0 ) bat->status = 0;
+
+    fclose( fp_charge_full );
+    fclose( fp_charge_now );
+    fclose( fp_status );
+}
+
+void draw_battery_icon( cairo_t* cr, double x, double y, double height )
+{
+    static Battery bat;
+    getbatteryinfo( &bat );
+    //bat.charge_percent * 100.0
+    //bat.status == 1 means charging
+
+    cairo_set_line_width( cr, 2.0 );
+    cairo_rectangle( cr, x+height*0.375, y+height*0.125, height*0.25, height*0.125 );
+    cairo_rectangle( cr, x+height*0.25, y+height*0.25, height*0.5, height*0.75 );
+    cairo_stroke( cr );
+
+    if( bat.charge_percent <= 0.20 ) //Low battery
+        cairo_set_source_rgb( cr, 0.9, 0.0, 0.0 );
+    if( bat.status == 1 ) //Charging
+        cairo_set_source_rgb( cr, 0.0, 0.5, 1.0 );
+    if( bat.status == 2 ) //Full
+        cairo_set_source_rgb( cr, 0.0, 0.7, 0.0 );
+
+    cairo_rectangle( cr, x+height*0.25+2.0, y+height*0.25+(height*0.75*(1.0-bat.charge_percent))+2.0, height*0.5-4.0, height*0.75*bat.charge_percent-4.0  );
+    cairo_fill( cr );
+}
+
+void add_brightness_reading( double r )
+{
+    int i;
+    double avg;
+
+    for( i = 9; i > 0; i-- )
+        brightness.readings[i] = brightness.readings[i-1];
+    brightness.readings[0] = r;
+
+    avg = 0.0;
+    for( i = 0; i < 10; i++ )
+        avg += brightness.readings[i];
+
+    brightness.scaled = avg/10.0;
+    if( brightness.scaled < 0.15 )
+        brightness.scaled = 0.15;
+}
+
+gboolean fade_brightness(gpointer data)
+{
+    brightness.out+=brightness.db;
+
+    if( abs( brightness.out - brightness.target ) < 0.75 )
+        brightness.out = brightness.target;
+    brightness.moving = brightness.out != brightness.target;
+
+    char buffer[80];
+    sprintf( buffer, "xbacklight -set %f", brightness.out>2?brightness.out:2 );
+    system( buffer );
+
+    //printf( "current: %f, target: %f\n", brightness.out, brightness.target );
+
+    return brightness.moving;
+}
+
+void update_auto_brightness()
+{
+    char buffer[256];
+    FILE* f = fopen( BRIGHTNESS_INTENSITY_FILE, "r" );
+
+    fgets( buffer, 255, f );
+    add_brightness_reading( atof( buffer ) );
+
+    //9.9323 ln(x) + 27.059
+    brightness.target = (9.9323*log(brightness.scaled) + 27.059);
+
+    if( brightness.target != brightness.out )
+    {
+        brightness.db = (brightness.target-brightness.out)/8;
+        if( !brightness.moving )
+        {
+            brightness.moving = 1;
+            g_timeout_add( 250, fade_brightness, NULL);
+        }
+    }
+
+    fclose( f );
+}
+
+gboolean control_update(gpointer data)
+{
+    if(brightness_support)
+        update_auto_brightness();
+
+    return TRUE;
+}
+
+void control_init( int* battery, int* auto_brightness )
+{
+    FILE* f;
+
+    //Check if there's a battery
+    f = fopen( BATTERY_CHARGE_FILE, "r" );
+    *battery = f != NULL;
+    if( f )
+        fclose(f);
+
+    //Check for light sensor
+    f = fopen( BRIGHTNESS_INTENSITY_FILE, "r" );
+    brightness_support = f != NULL;
+    if( auto_brightness )
+        *auto_brightness = brightness_support;
+    if( f )
+        fclose(f);
+
+    if( brightness_support )
+    {
+        int i;
+        for( i = 0; i < 10; i++ )
+            brightness.readings[i] = 5.0;
+        brightness.moving = 0;
+        brightness.out = 50;
+    }
+
+    g_timeout_add_seconds( 2, control_update, NULL);
+}
+
diff --git a/control.h b/control.h
new file mode 100644 (file)
index 0000000..f871161
--- /dev/null
+++ b/control.h
@@ -0,0 +1,12 @@
+/*
+ * System control functions
+ *
+ * Battery Icon
+ * Volume
+ * Screen brightness
+ */
+#pragma once
+
+void control_init( int* battery, int* auto_brightness );
+//void control_update();
+void draw_battery_icon( cairo_t* cr, double x, double y, double height );
diff --git a/main.c b/main.c
index f8b08f824f121c45945d76e477a138a0d6ca2a0c..158cf0b3eb3187dac90562482b198d183308ffc0 100644 (file)
--- a/main.c
+++ b/main.c
@@ -13,9 +13,10 @@ eric_window* dock_window = NULL;
 #include "clock.h"
 #include "xutils.h"
 
+#include "control.h"
+
 //Structure to hold actual pager items
 #include "pager_item.h"
-#include "battery.h"
 #include "ericdock.h"
 
 //Stucture to hold class groups... which are icons on the dock
@@ -194,7 +195,7 @@ static gboolean draw_dock_window( GtkWidget* widget, cairo_t* cr, eric_window* w
     }
     double clock_width = clock_draw( cr, (double)screen_width-SCALE_VALUE(10), ( BAR_HEIGHT * UI_SCALE ) / 2.0, w );
     if( show_battery_icon )
-        draw_battery_icon( cr, clock_width-SCALE_VALUE( 48.0 ), (( BAR_HEIGHT * UI_SCALE ) / 2.0)-SCALE_VALUE( 12 ), SCALE_VALUE( 24 ) );
+        draw_battery_icon( cr, clock_width-SCALE_VALUE( 52.0 ), (( BAR_HEIGHT * UI_SCALE ) / 2.0)-SCALE_VALUE( 12 ), SCALE_VALUE( 24 ) );
 
     return FALSE;
 }
@@ -298,10 +299,11 @@ void setup_dock_window()
     clock_init( dock_window->window );
 
     //Check if there's a battery
-    FILE* f = fopen( "/sys/class/power_supply/BAT0/charge_full", "r" );
-    show_battery_icon = f != NULL;
-    if( f )
-        fclose(f);
+    control_init( &show_battery_icon, NULL );
+    //FILE* f = fopen( "/sys/class/power_supply/BAT0/charge_full", "r" );
+    //show_battery_icon = f != NULL;
+    //if( f )
+    //    fclose(f);
 
     //Set window struts
     xutils_set_strut( gtk_widget_get_window( dock_window->window ), GTK_POS_BOTTOM, SCALE_VALUE( BAR_HEIGHT ), 0, screen_width );