commit e1db7f59c8428f6ceccae98a53c8e28eeddab707 Author: klein panic Date: Sun Sep 29 02:33:45 2024 -0400 intiial commit diff --git a/Obj/brightness.o b/Obj/brightness.o new file mode 100644 index 0000000..f265845 Binary files /dev/null and b/Obj/brightness.o differ diff --git a/Obj/main.o b/Obj/main.o new file mode 100644 index 0000000..e6c1781 Binary files /dev/null and b/Obj/main.o differ diff --git a/build/Makefile b/build/Makefile new file mode 100644 index 0000000..5192352 --- /dev/null +++ b/build/Makefile @@ -0,0 +1,62 @@ +# Makefile located in build/ directory + +# Compiler +CC = gcc + +# Compiler Flags +CFLAGS = -I../include -Wall + +# Linker Flags +LDFLAGS = -lX11 -lm + +# Directories +SRC_DIR = ../src +OBJ_DIR = ../Obj +BUILD_DIR = . +INCLUDE_DIR = ../include + +# Source Files +SRC = $(SRC_DIR)/main.c $(SRC_DIR)/brightness.c + +# Object Files +OBJ = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC)) + +# Target Binary +TARGET = brightness + +# Installation Directory +PREFIX ?= /usr/local +BIN_DIR = $(PREFIX)/bin + +# Default Target +all: $(TARGET) + +# Link Object Files to Create Binary +$(TARGET): $(OBJ) + $(CC) -o $(BUILD_DIR)/$@ $^ $(LDFLAGS) + +# Compile Source Files to Object Files +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) + $(CC) $(CFLAGS) -c $< -o $@ + +# Create Object Directory if it Doesn't Exist +$(OBJ_DIR): + mkdir -p $(OBJ_DIR) + +# Install the Binary to /usr/local/bin/ +install: $(TARGET) + @echo "Installing $(TARGET) to $(BIN_DIR)/$(TARGET)" + mkdir -p $(BIN_DIR) + install -m 755 $(BUILD_DIR)/$(TARGET) $(BIN_DIR)/$(TARGET) + +# Uninstall the Binary from /usr/local/bin/ +uninstall: + @echo "Uninstalling $(TARGET) from $(BIN_DIR)/$(TARGET)" + rm -f $(BIN_DIR)/$(TARGET) + +# Clean Build Artifacts +clean: + rm -f $(OBJ) $(BUILD_DIR)/$(TARGET) + +# Phony Targets +.PHONY: all clean install uninstall diff --git a/build/brightness b/build/brightness new file mode 100755 index 0000000..0f98f71 Binary files /dev/null and b/build/brightness differ diff --git a/docs/brightness.1 b/docs/brightness.1 new file mode 100644 index 0000000..e69de29 diff --git a/include/brightness.h b/include/brightness.h new file mode 100644 index 0000000..f0a3e40 --- /dev/null +++ b/include/brightness.h @@ -0,0 +1,14 @@ +// brightness.h +#ifndef BRIGHTNESS_H +#define BRIGHTNESS_H + +#define BRIGHTNESS_PATH "/sys/class/backlight/intel_backlight/brightness" +#define MAX_BRIGHTNESS_PATH "/sys/class/backlight/intel_backlight/max_brightness" + +int get_max_brightness(); +int get_current_brightness(); +void set_brightness(int value, int raw, int no_display); +void adjust_brightness(const char *direction, int no_display); +void display_brightness(int brightness); + +#endif // BRIGHTNESS_H diff --git a/include/brightness.h.bk b/include/brightness.h.bk new file mode 100644 index 0000000..7799f99 --- /dev/null +++ b/include/brightness.h.bk @@ -0,0 +1,16 @@ +#ifndef BRIGHTNESS_H +#define BRIGHTNESS_H + +#define BRIGHTNESS_PATH "/sys/class/backlight/intel_backlight/brightness" +#define MAX_BRIGHTNESS_PATH "/sys/class/backlight/intel_backlight/max_brightness" + +void set_brightness(int value, int raw); +void adjust_brightness(const char *direction); +void display_brightness(int brightness); +int get_max_brightness(); +int get_current_brightness(); +void list_brightness_devices(const char *class_filter); +int check_brightness_paths(); +void set_device(const char *device, const char *class); + +#endif // BRIGHTNESS_H diff --git a/obj/main.o b/obj/main.o new file mode 100644 index 0000000..6c6134e Binary files /dev/null and b/obj/main.o differ diff --git a/src/brightness.c b/src/brightness.c new file mode 100644 index 0000000..78d3411 --- /dev/null +++ b/src/brightness.c @@ -0,0 +1,185 @@ +// brightness.c +#include +#include +#include +#include +#include +#include +#include +#include "brightness.h" + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +int get_max_brightness() { + FILE *file = fopen(MAX_BRIGHTNESS_PATH, "r"); + if (file == NULL) { + perror("Failed to open max brightness file"); + exit(EXIT_FAILURE); + } + int max_brightness; + if (fscanf(file, "%d", &max_brightness) != 1) { + perror("Failed to read max brightness value"); + fclose(file); + exit(EXIT_FAILURE); + } + fclose(file); + return max_brightness; +} + +int get_current_brightness() { + FILE *file = fopen(BRIGHTNESS_PATH, "r"); + if (file == NULL) { + perror("Failed to open brightness file"); + exit(EXIT_FAILURE); + } + int brightness; + if (fscanf(file, "%d", &brightness) != 1) { + perror("Failed to read brightness value"); + fclose(file); + exit(EXIT_FAILURE); + } + fclose(file); + return brightness; +} + +void set_brightness(int value, int raw, int no_display) { + int max_brightness = get_max_brightness(); + if (!raw) { + value = (value * max_brightness) / 100; + } + FILE *brightness_file = fopen(BRIGHTNESS_PATH, "w"); + if (brightness_file == NULL) { + perror("Failed to open brightness file for writing"); + exit(EXIT_FAILURE); + } + if (fprintf(brightness_file, "%d", value) < 0) { + perror("Failed to write brightness value"); + fclose(brightness_file); + exit(EXIT_FAILURE); + } + fclose(brightness_file); + if (!no_display) { + display_brightness(value); + } +} + +void adjust_brightness(const char *direction, int no_display) { + int brightness = get_current_brightness(); + int max_brightness = get_max_brightness(); + + if (strcmp(direction, "up") == 0) { + brightness += max_brightness / 10; + } else if (strcmp(direction, "down") == 0) { + brightness -= max_brightness / 10; + } else { + fprintf(stderr, "Error: Invalid adjust direction. Use 'up' or 'down'.\n"); + exit(EXIT_FAILURE); + } + + if (brightness < 0) { + brightness = 0; + } else if (brightness > max_brightness) { + brightness = max_brightness; + } + + set_brightness(brightness, 1, no_display); +} + +void draw_icon(Display *d, Window w, GC gc) { + int circle_radius = 22; // Radius for a smaller circle + int ray_length = 27; // Length of the rays + int line_thickness = 5; // Increased line thickness + int y_offset = 10; // Amount to move the icon down + + // Set the line attributes to have thicker lines with rounded edges + XSetLineAttributes(d, gc, line_thickness, LineSolid, CapRound, JoinRound); + + // Draw the smaller center circle, moved down by y_offset + XDrawArc(d, w, gc, 100 - circle_radius, 40 - circle_radius + y_offset, 2 * circle_radius, 2 * circle_radius, 0, 360 * 64); + + // Draw the rays with adjusted length, moved down by y_offset + for (int i = 0; i < 8; ++i) { + double angle = i * M_PI / 4.0; + int x1 = 100 + (int)((circle_radius + 5) * cos(angle)); // Start just outside the circle + int y1 = 40 + (int)((circle_radius + 5) * sin(angle)) + y_offset; + int x2 = 100 + (int)((circle_radius + ray_length) * cos(angle)); // Extend to ray_length + int y2 = 40 + (int)((circle_radius + ray_length) * sin(angle)) + y_offset; + XDrawLine(d, w, gc, x1, y1, x2, y2); + } +} + +void display_brightness(int brightness) { + Display *d; + Window w; + XEvent e; + int screen; + unsigned int display_width, display_height; + int width = 200, height = 120; + int sections = 10; + int graph_height = 10; + + if ((d = XOpenDisplay(NULL)) == NULL) { + fprintf(stderr, "Cannot open display\n"); + exit(EXIT_FAILURE); + } + + screen = DefaultScreen(d); + display_width = DisplayWidth(d, screen); + display_height = DisplayHeight(d, screen); + + w = XCreateSimpleWindow(d, RootWindow(d, screen), (display_width - width) / 2, (display_height - height) / 2, width, height, 1, + BlackPixel(d, screen), WhitePixel(d, screen)); + XSetWindowBackground(d, w, 0xD3D3D3); // Light gray + XStoreName(d, w, "BrightnessControl"); + XClassHint *classHint = XAllocClassHint(); + if (classHint) { + classHint->res_name = "brightnesscontrol"; + classHint->res_class = "BrightnessControl"; + XSetClassHint(d, w, classHint); + XFree(classHint); + } + + XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask); + XMapWindow(d, w); + + GC gc = XCreateGC(d, w, 0, NULL); + GC bg_gc = XCreateGC(d, w, 0, NULL); + XSetForeground(d, gc, BlackPixel(d, screen)); + XSetBackground(d, bg_gc, WhitePixel(d, screen)); + XSetLineAttributes(d, gc, 3, LineSolid, CapButt, JoinMiter); + + int max_brightness = get_max_brightness(); + + // Wait for the window to be mapped + while (1) { + XNextEvent(d, &e); + if (e.type == MapNotify) { + break; + } + } + + // Draw the icon + draw_icon(d, w, gc); + + // Draw the background for graph + XSetForeground(d, bg_gc, 0xA9A9A9); // Dark gray + XFillRectangle(d, w, bg_gc, 10, 100, 180, graph_height); + + // Draw the graph + int filled_sections = (int)((brightness / (double)max_brightness) * sections); + for (int i = 0; i < sections; ++i) { + if (i < filled_sections) { + XSetForeground(d, gc, 0xFFFFFF); // White + } else { + XSetForeground(d, gc, 0xA9A9A9); // Dark gray + } + XFillRectangle(d, w, gc, 10 + i * (180 / sections), 100, (180 / sections) - 2, graph_height); + } + + XFlush(d); + sleep(1); // Display the window for a brief period + XDestroyWindow(d, w); + XCloseDisplay(d); +} diff --git a/src/brightness.c.bak b/src/brightness.c.bak new file mode 100644 index 0000000..904d0ee --- /dev/null +++ b/src/brightness.c.bak @@ -0,0 +1,165 @@ +#include +#include +#include +#include +#include +#include +#include +#include "brightness.h" + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +int get_max_brightness() { + FILE *file = fopen(MAX_BRIGHTNESS_PATH, "r"); + if (file == NULL) { + perror("Failed to open max brightness file"); + exit(EXIT_FAILURE); + } + int max_brightness; + fscanf(file, "%d", &max_brightness); + fclose(file); + return max_brightness; +} + +int get_current_brightness() { + FILE *file = fopen(BRIGHTNESS_PATH, "r"); + if (file == NULL) { + perror("Failed to open brightness file"); + exit(EXIT_FAILURE); + } + int brightness; + fscanf(file, "%d", &brightness); + fclose(file); + return brightness; +} + +void set_brightness(int value, int raw) { + int max_brightness = get_max_brightness(); + if (!raw) { + value = (value * max_brightness) / 100; + } + FILE *brightness_file = fopen(BRIGHTNESS_PATH, "w"); + if (brightness_file == NULL) { + perror("Failed to open brightness file"); + exit(EXIT_FAILURE); + } + fprintf(brightness_file, "%d", value); + fclose(brightness_file); + display_brightness(value); +} + +void adjust_brightness(const char *direction) { + int brightness = get_current_brightness(); + int max_brightness = get_max_brightness(); + + if (strcmp(direction, "up") == 0) { + brightness += max_brightness / 10; + } else if (strcmp(direction, "down") == 0) { + brightness -= max_brightness / 10; + } + + if (brightness < 0) { + brightness = 0; + } else if (brightness > max_brightness) { + brightness = max_brightness; + } + + set_brightness(brightness, 1); +} + +void draw_icon(Display *d, Window w, GC gc) { + int circle_radius = 22; // Radius for a smaller circle + int ray_length = 27; // Length of the rays + int line_thickness = 5; // Increased line thickness + int y_offset = 10; // Amount to move the icon down + + // Set the line attributes to have thicker lines with rounded edges + XSetLineAttributes(d, gc, line_thickness, LineSolid, CapRound, JoinRound); + + // Draw the smaller center circle, moved down by y_offset + XDrawArc(d, w, gc, 100 - circle_radius, 40 - circle_radius + y_offset, 2 * circle_radius, 2 * circle_radius, 0, 360 * 64); + + // Draw the rays with adjusted length, moved down by y_offset + for (int i = 0; i < 8; ++i) { + int angle = i * M_PI / 4; + int x1 = 100 + (circle_radius + 5) * cos(angle); // Start just outside the circle + int y1 = 40 + (circle_radius + 5) * sin(angle) + y_offset; + int x2 = 100 + (circle_radius + ray_length) * cos(angle); // Extend to ray_length + int y2 = 40 + (circle_radius + ray_length) * sin(angle) + y_offset; + XDrawLine(d, w, gc, x1, y1, x2, y2); + } +} + +void display_brightness(int brightness) { + Display *d; + Window w; + XEvent e; + int screen; + unsigned int display_width, display_height; + int width = 200, height = 120; + int sections = 10; + int graph_height = 10; + + if ((d = XOpenDisplay(NULL)) == NULL) { + fprintf(stderr, "Cannot open display\n"); + exit(1); + } + + screen = DefaultScreen(d); + display_width = DisplayWidth(d, screen); + display_height = DisplayHeight(d, screen); + + w = XCreateSimpleWindow(d, RootWindow(d, screen), (display_width - width) / 2, (display_height - height) / 2, width, height, 1, + BlackPixel(d, screen), WhitePixel(d, screen)); + XSetWindowBackground(d, w, 0xD3D3D3); // Light gray + XStoreName(d, w, "BrightnessControl"); + XClassHint *classHint = XAllocClassHint(); + classHint->res_name = "brightnesscontrol"; + classHint->res_class = "BrightnessControl"; + XSetClassHint(d, w, classHint); + XFree(classHint); + + XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask); + XMapWindow(d, w); + + GC gc = XCreateGC(d, w, 0, NULL); + GC bg_gc = XCreateGC(d, w, 0, NULL); + XSetForeground(d, gc, BlackPixel(d, screen)); + XSetBackground(d, bg_gc, WhitePixel(d, screen)); + XSetLineAttributes(d, gc, 3, LineSolid, CapButt, JoinMiter); + + int max_brightness = get_max_brightness(); + + // Wait for the window to be mapped + while (1) { + XNextEvent(d, &e); + if (e.type == MapNotify) { + break; + } + } + + // Draw the icon + draw_icon(d, w, gc); + + // Draw the background for graph + XSetForeground(d, bg_gc, 0xA9A9A9); // Dark gray + XFillRectangle(d, w, bg_gc, 10, 100, 180, graph_height); + + // Draw the graph + int filled_sections = (int)((brightness / (double)max_brightness) * sections); + for (int i = 0; i < sections; ++i) { + if (i < filled_sections) { + XSetForeground(d, gc, 0xFFFFFF); // White + } else { + XSetForeground(d, gc, 0xA9A9A9); // Dark gray + } + XFillRectangle(d, w, gc, 10 + i * (180 / sections), 100, 180 / sections - 2, graph_height); + } + + XFlush(d); + sleep(1); // Display the window for a brief period + XDestroyWindow(d, w); + XCloseDisplay(d); +} diff --git a/src/brightness.o b/src/brightness.o new file mode 100644 index 0000000..45c2ece Binary files /dev/null and b/src/brightness.o differ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..265e4e0 --- /dev/null +++ b/src/main.c @@ -0,0 +1,80 @@ +// main.c +#include +#include +#include +#include "brightness.h" + +void print_usage(const char *prog_name) { + fprintf(stderr, "Usage: %s [--machine-readable|-m] [--human-readable|-hr] [--adjust|-a up|down] [--set|-s ] [--nodisplay|-nd]\n", prog_name); +} + +int main(int argc, char *argv[]) { + int show_machine_brightness = 0; + int show_human_brightness = 0; + const char *adjust_direction = NULL; + int set_brightness_value = -1; + int set_brightness_raw = 0; + int no_display = 0; // New flag for --nodisplay + + if (argc < 2) { + print_usage(argv[0]); + return EXIT_FAILURE; + } + + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--machine-readable") == 0 || strcmp(argv[i], "-m") == 0) { + show_machine_brightness = 1; + } else if (strcmp(argv[i], "--human-readable") == 0 || strcmp(argv[i], "-hr") == 0) { + show_human_brightness = 1; + } else if (strcmp(argv[i], "--adjust") == 0 || strcmp(argv[i], "-a") == 0) { + if (i + 1 < argc) { + adjust_direction = argv[++i]; + } else { + print_usage(argv[0]); + return EXIT_FAILURE; + } + } else if (strcmp(argv[i], "--set") == 0 || strcmp(argv[i], "-s") == 0) { + if (i + 1 < argc) { + set_brightness_value = atoi(argv[++i]); + if (set_brightness_value < 0 || set_brightness_value > 100) { + fprintf(stderr, "Error: Value for --set must be between 0 and 100\n"); + return EXIT_FAILURE; + } + } else { + print_usage(argv[0]); + return EXIT_FAILURE; + } + } else if (strcmp(argv[i], "--nodisplay") == 0 || strcmp(argv[i], "-nd") == 0) { + no_display = 1; // Set the no_display flag + } else { + print_usage(argv[0]); + return EXIT_FAILURE; + } + } + + if (show_machine_brightness) { + int brightness = get_current_brightness(); + printf("Current brightness (machine-readable): %d\n", brightness); + return EXIT_SUCCESS; + } + + if (show_human_brightness) { + int brightness = get_current_brightness(); + int max_brightness = get_max_brightness(); + printf("Current brightness (human-readable): %d%%\n", (brightness * 100) / max_brightness); + return EXIT_SUCCESS; + } + + if (adjust_direction) { + adjust_brightness(adjust_direction, no_display); + return EXIT_SUCCESS; + } + + if (set_brightness_value != -1) { + set_brightness(set_brightness_value, set_brightness_raw, no_display); + return EXIT_SUCCESS; + } + + print_usage(argv[0]); + return EXIT_FAILURE; +}