commit 681acbf5ce3969fdd01a11a026d5d2fabb496af7 Author: klein panic Date: Sun Sep 29 02:33:35 2024 -0400 initial commit diff --git a/build/Makefile b/build/Makefile new file mode 100644 index 0000000..a2b1c08 --- /dev/null +++ b/build/Makefile @@ -0,0 +1,23 @@ +# Variables +CC = gcc +CFLAGS = -Wall -I../include +SRC = ../src +OBJ = ../obj +BIN = battery_saver +OBJS = $(OBJ)/main.o $(OBJ)/cpu_control.o $(OBJ)/brightness_control.o $(OBJ)/service_control.o $(OBJ)/utils.o +DEPS = ../include/cpu_control.h ../include/brightness_control.h ../include/service_control.h ../include/utils.h + +# Targets +$(BIN): $(OBJS) + $(CC) $(CFLAGS) -o $(BIN) $(OBJS) + +$(OBJ)/%.o: $(SRC)/%.c $(DEPS) + mkdir -p $(OBJ) + $(CC) $(CFLAGS) -c $< -o $@ + +# Clean up build +clean: + rm -rf $(OBJ)/*.o $(BIN) + +# Phony targets +.PHONY: clean diff --git a/build/battery_saver b/build/battery_saver new file mode 100755 index 0000000..5318efc Binary files /dev/null and b/build/battery_saver differ diff --git a/include/brightness_control.h b/include/brightness_control.h new file mode 100644 index 0000000..3480c8c --- /dev/null +++ b/include/brightness_control.h @@ -0,0 +1,6 @@ +#ifndef BRIGHTNESS_CONTROL_H +#define BRIGHTNESS_CONTROL_H + +int adjust_brightness(int value); + +#endif diff --git a/include/cpu_control.h b/include/cpu_control.h new file mode 100644 index 0000000..4d54652 --- /dev/null +++ b/include/cpu_control.h @@ -0,0 +1,6 @@ +#ifndef CPU_CONTROL_H +#define CPU_CONTROL_H + +void set_cpu_governor(const char *governor); + +#endif diff --git a/include/service_control.h b/include/service_control.h new file mode 100644 index 0000000..3ae2dc5 --- /dev/null +++ b/include/service_control.h @@ -0,0 +1,7 @@ +#ifndef SERVICE_CONTROL_H +#define SERVICE_CONTROL_H + +int disable_services(); +int enable_services(); + +#endif diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..c8def63 --- /dev/null +++ b/include/utils.h @@ -0,0 +1,8 @@ +#ifndef UTILS_H +#define UTILS_H + +void log_status(const char *message); +void print_status(); + +#endif + diff --git a/obj/brightness_control.o b/obj/brightness_control.o new file mode 100644 index 0000000..78939d8 Binary files /dev/null and b/obj/brightness_control.o differ diff --git a/obj/cpu_control.o b/obj/cpu_control.o new file mode 100644 index 0000000..9580200 Binary files /dev/null and b/obj/cpu_control.o differ diff --git a/obj/main.o b/obj/main.o new file mode 100644 index 0000000..6d3e56c Binary files /dev/null and b/obj/main.o differ diff --git a/obj/service_control.o b/obj/service_control.o new file mode 100644 index 0000000..381317a Binary files /dev/null and b/obj/service_control.o differ diff --git a/obj/utils.o b/obj/utils.o new file mode 100644 index 0000000..e7f9725 Binary files /dev/null and b/obj/utils.o differ diff --git a/src/brightness_control.c b/src/brightness_control.c new file mode 100644 index 0000000..d894803 --- /dev/null +++ b/src/brightness_control.c @@ -0,0 +1,16 @@ +#include +#include +#include "brightness_control.h" + +int adjust_brightness(int value) { + FILE *brightness = fopen("/sys/class/backlight/intel_backlight/brightness", "w"); + if (brightness) { + fprintf(brightness, "%d", value); + fclose(brightness); + return 0; + } else { + perror("Failed to adjust brightness"); + return 1; + } +} + diff --git a/src/cpu_control.c b/src/cpu_control.c new file mode 100644 index 0000000..45fb9ad --- /dev/null +++ b/src/cpu_control.c @@ -0,0 +1,19 @@ +#include +#include +#include // Add this for sysconf() +#include "cpu_control.h" + +void set_cpu_governor(const char *governor) { + int cpu_count = sysconf(_SC_NPROCESSORS_ONLN); + for (int i = 0; i < cpu_count; i++) { + char path[64]; + sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", i); + FILE *f = fopen(path, "w"); + if (f) { + fprintf(f, "%s", governor); + fclose(f); + } else { + perror("Failed to set CPU governor"); + } + } +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..09f9acc --- /dev/null +++ b/src/main.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include "cpu_control.h" +#include "brightness_control.h" +#include "service_control.h" +#include "utils.h" + +void print_usage() { + printf("Usage: battery_saver --enable | --disable | --status\n"); + printf("--enable : Enable battery saving mode\n"); + printf("--disable : Disable battery saving mode and restore settings\n"); + printf("--status : Display current battery saving mode status\n"); +} + +int main(int argc, char *argv[]) { + int opt; + int enable = 0, disable = 0, status = 0; + + struct option long_options[] = { + {"enable", no_argument, &enable, 1}, + {"disable", no_argument, &disable, 1}, + {"status", no_argument, &status, 1}, + {0, 0, 0, 0} + }; + + if (argc < 2) { + print_usage(); + return 1; + } + + while ((opt = getopt_long(argc, argv, "", long_options, NULL)) != -1) { + if (opt == '?') { + print_usage(); + return 1; + } + } + + if (enable) { + set_cpu_governor("powersave"); + adjust_brightness(20); // Set to 20% brightness + disable_services(); + log_status("Battery saving mode enabled."); + printf("Battery saving mode enabled.\n"); + } else if (disable) { + set_cpu_governor("ondemand"); + adjust_brightness(100); // Restore brightness to 100% + enable_services(); + log_status("Battery saving mode disabled."); + printf("Battery saving mode disabled.\n"); + } else if (status) { + print_status(); + } else { + print_usage(); // Fallback to usage message if no valid option + return 1; + } + + return 0; +} diff --git a/src/service_control.c b/src/service_control.c new file mode 100644 index 0000000..c3b3791 --- /dev/null +++ b/src/service_control.c @@ -0,0 +1,11 @@ +#include +#include +#include "service_control.h" + +int disable_services() { + return system("nmcli radio wifi off && rfkill block bluetooth"); +} + +int enable_services() { + return system("nmcli radio wifi on && rfkill unblock bluetooth"); +} diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..2edf385 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,24 @@ +#include +#include +#include "utils.h" + +void log_status(const char *message) { + FILE *logfile = fopen("/var/log/battery_saver.log", "a"); + if (logfile) { + fprintf(logfile, "%s\n", message); + fclose(logfile); + } else { + perror("Failed to write to log"); + } +} + +void print_status() { + printf("CPU Governor: "); + system("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"); + printf("Brightness: "); + system("cat /sys/class/backlight/intel_backlight/brightness"); + printf("WiFi Status: "); + system("nmcli radio wifi"); +} + +