initial commit
This commit is contained in:
23
build/Makefile
Normal file
23
build/Makefile
Normal file
@@ -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
|
||||
BIN
build/battery_saver
Executable file
BIN
build/battery_saver
Executable file
Binary file not shown.
6
include/brightness_control.h
Normal file
6
include/brightness_control.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef BRIGHTNESS_CONTROL_H
|
||||
#define BRIGHTNESS_CONTROL_H
|
||||
|
||||
int adjust_brightness(int value);
|
||||
|
||||
#endif
|
||||
6
include/cpu_control.h
Normal file
6
include/cpu_control.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef CPU_CONTROL_H
|
||||
#define CPU_CONTROL_H
|
||||
|
||||
void set_cpu_governor(const char *governor);
|
||||
|
||||
#endif
|
||||
7
include/service_control.h
Normal file
7
include/service_control.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef SERVICE_CONTROL_H
|
||||
#define SERVICE_CONTROL_H
|
||||
|
||||
int disable_services();
|
||||
int enable_services();
|
||||
|
||||
#endif
|
||||
8
include/utils.h
Normal file
8
include/utils.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
void log_status(const char *message);
|
||||
void print_status();
|
||||
|
||||
#endif
|
||||
|
||||
BIN
obj/brightness_control.o
Normal file
BIN
obj/brightness_control.o
Normal file
Binary file not shown.
BIN
obj/cpu_control.o
Normal file
BIN
obj/cpu_control.o
Normal file
Binary file not shown.
BIN
obj/main.o
Normal file
BIN
obj/main.o
Normal file
Binary file not shown.
BIN
obj/service_control.o
Normal file
BIN
obj/service_control.o
Normal file
Binary file not shown.
BIN
obj/utils.o
Normal file
BIN
obj/utils.o
Normal file
Binary file not shown.
16
src/brightness_control.c
Normal file
16
src/brightness_control.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
||||
19
src/cpu_control.c
Normal file
19
src/cpu_control.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h> // 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
59
src/main.c
Normal file
59
src/main.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <getopt.h>
|
||||
#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;
|
||||
}
|
||||
11
src/service_control.c
Normal file
11
src/service_control.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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");
|
||||
}
|
||||
24
src/utils.c
Normal file
24
src/utils.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user