From a80b5e520e7b8e0f8ff1c9c841492a332ac353b7 Mon Sep 17 00:00:00 2001 From: Eric Wertz Date: Fri, 17 Nov 2017 09:05:12 -0500 Subject: [PATCH] Moved battery and brightness control to own file --- Makefile | 4 +- battery.c | 57 ----------------- battery.h | 3 - control.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ control.h | 12 ++++ main.c | 14 ++-- 6 files changed, 209 insertions(+), 68 deletions(-) delete mode 100644 battery.c delete mode 100644 battery.h create mode 100644 control.c create mode 100644 control.h diff --git a/Makefile b/Makefile index 887684f..08de8b7 100644 --- 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 index fe09940..0000000 --- a/battery.c +++ /dev/null @@ -1,57 +0,0 @@ -#include - -#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 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 index 0000000..7f3197b --- /dev/null +++ b/control.c @@ -0,0 +1,187 @@ +/* + * System control functions + * + * Battery Icon + * Volume + * Screen brightness + */ +#include + +#include "ericdock.h" +#include "control.h" +#include + +#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 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 f8b08f8..158cf0b 100644 --- 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 ); -- 2.47.0