moved code here. still needs to update gh

This commit is contained in:
klein panic
2025-02-24 15:18:44 -05:00
parent 9ca9f577a4
commit 2726432fe1
106 changed files with 4816 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
CC = gcc
CFLAGS = -Wall -Wextra -std=c99
LDFLAGS = -lX11
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
SRCS = $(wildcard $(SRC_DIR)/*.c)
OBJS = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRCS))
DEPS = $(OBJS:.o=.d)
TARGET = $(BIN_DIR)/brightnessctl
$(TARGET): $(OBJS)
@mkdir -p $(BIN_DIR)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -MMD -c $< -o $@
-include $(DEPS)
clean:
rm -rf $(OBJ_DIR) $(BIN_DIR)
.PHONY: clean

View File

@@ -0,0 +1,2 @@
obj/brightness.o: src/brightness.c src/brightness.h src/display.h \
src/utils.h

Binary file not shown.

View File

@@ -0,0 +1 @@
obj/display.o: src/display.c src/display.h src/brightness.h src/utils.h

Binary file not shown.

View File

@@ -0,0 +1 @@
obj/main.o: src/main.c src/brightness.h

View File

@@ -0,0 +1,184 @@
#include "brightness.h"
#include "display.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // Include string.h for strdup
static int min_value = 0;
static float exponent = 1.0;
static char *device = NULL;
static char *class = NULL;
static int quiet_mode = 0;
static int pretend_mode = 0;
void set_brightness(int value, int raw) {
if (pretend_mode) {
if (!quiet_mode) {
printf("Pretend mode: setting brightness to %d\n", value);
}
return;
}
FILE *brightness_file = fopen(BRIGHTNESS_PATH, "w");
if (brightness_file == NULL) {
perror("Failed to open brightness file");
exit(EXIT_FAILURE);
}
if (!raw) {
FILE *max_brightness_file = fopen(MAX_BRIGHTNESS_PATH, "r");
if (max_brightness_file == NULL) {
perror("Failed to open max brightness file");
fclose(brightness_file);
exit(EXIT_FAILURE);
}
int max_brightness;
fscanf(max_brightness_file, "%d", &max_brightness);
fclose(max_brightness_file);
value = (value * max_brightness) / 100;
}
fprintf(brightness_file, "%d", value);
fclose(brightness_file);
display_brightness(value);
}
void adjust_brightness(const char *direction) {
if (pretend_mode) {
if (!quiet_mode) {
printf("Pretend mode: adjusting brightness %s\n", direction);
}
return;
}
FILE *brightness_file = fopen(BRIGHTNESS_PATH, "r+");
if (brightness_file == NULL) {
perror("Failed to open brightness file");
exit(EXIT_FAILURE);
}
int brightness;
fscanf(brightness_file, "%d", &brightness);
FILE *max_brightness_file = fopen(MAX_BRIGHTNESS_PATH, "r");
if (max_brightness_file == NULL) {
perror("Failed to open max brightness file");
fclose(brightness_file);
exit(EXIT_FAILURE);
}
int max_brightness;
fscanf(max_brightness_file, "%d", &max_brightness);
fclose(max_brightness_file);
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;
}
rewind(brightness_file);
fprintf(brightness_file, "%d", brightness);
fclose(brightness_file);
display_brightness(brightness);
}
void save_brightness(void) {
if (pretend_mode) {
if (!quiet_mode) {
printf("Pretend mode: saving brightness\n");
}
return;
}
FILE *brightness_file = fopen(BRIGHTNESS_PATH, "r");
if (brightness_file == NULL) {
perror("Failed to open brightness file");
exit(EXIT_FAILURE);
}
int brightness;
fscanf(brightness_file, "%d", &brightness);
fclose(brightness_file);
FILE *save_file = fopen("/tmp/brightnessctl_save", "w");
if (save_file == NULL) {
perror("Failed to open save file");
exit(EXIT_FAILURE);
}
fprintf(save_file, "%d", brightness);
fclose(save_file);
if (!quiet_mode) {
printf("Brightness saved: %d\n", brightness);
}
}
void restore_brightness(void) {
if (pretend_mode) {
if (!quiet_mode) {
printf("Pretend mode: restoring brightness\n");
}
return;
}
FILE *save_file = fopen("/tmp/brightnessctl_save", "r");
if (save_file == NULL) {
perror("Failed to open save file");
exit(EXIT_FAILURE);
}
int brightness;
fscanf(save_file, "%d", &brightness);
fclose(save_file);
set_brightness(brightness, 1);
if (!quiet_mode) {
printf("Brightness restored: %d\n", brightness);
}
}
void list_devices(void) {
printf("Listing available devices...\n");
}
void print_info(void) {
printf("Printing device info...\n");
}
void set_min_value(int value) {
min_value = value;
}
void set_exponent(float exp) {
exponent = exp;
}
void set_device(char *device_name) {
device = strdup(device_name);
}
void set_class(char *class_name) {
class = strdup(class_name);
}
void enable_quiet_mode(void) {
quiet_mode = 1;
}
void enable_pretend_mode(void) {
pretend_mode = 1;
}

View File

@@ -0,0 +1,21 @@
#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);
void save_brightness(void);
void restore_brightness(void);
void list_devices(void);
void print_info(void);
void set_min_value(int value);
void set_exponent(float exponent);
void set_device(char *device_name);
void set_class(char *class_name);
void enable_quiet_mode(void);
void enable_pretend_mode(void);
#endif

View File

@@ -0,0 +1,95 @@
#include "display.h"
#include "brightness.h"
#include "utils.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
void draw_icon(Display *d, Window w, GC gc) {
XDrawArc(d, w, gc, 70, 10, 60, 60, 0, 360 * 64);
for (int i = 0; i < 8; ++i) {
int x1 = 100 + 30 * cos(i * M_PI / 4);
int y1 = 40 + 30 * sin(i * M_PI / 4);
int x2 = 100 + 45 * cos(i * M_PI / 4);
int y2 = 40 + 45 * sin(i * M_PI / 4);
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) {
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);
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);
FILE *max_brightness_file = fopen(MAX_BRIGHTNESS_PATH, "r");
int max_brightness;
fscanf(max_brightness_file, "%d", &max_brightness);
fclose(max_brightness_file);
while (1) {
XNextEvent(d, &e);
if (e.type == MapNotify) {
break;
}
}
draw_icon(d, w, gc);
XSetForeground(d, bg_gc, 0xA9A9A9);
XFillRectangle(d, w, bg_gc, 10, 100, 180, graph_height);
int filled_sections = (int)((brightness / (double)max_brightness) * sections);
for (int i = 0; i < sections; ++i) {
if (i < filled_sections) {
XSetForeground(d, gc, 0xFFFFFF);
} else {
XSetForeground(d, gc, 0xA9A9A9);
}
XFillRectangle(d, w, gc, 10 + i * (180 / sections), 100, 180 / sections - 2, graph_height);
}
XFlush(d);
sleep(1);
XDestroyWindow(d, w);
XCloseDisplay(d);
}

View File

@@ -0,0 +1,9 @@
#ifndef DISPLAY_H
#define DISPLAY_H
#include <X11/Xlib.h> // Include X11 headers for Display, Window, and GC types
void draw_icon(Display *d, Window w, GC gc);
void display_brightness(int brightness);
#endif

View File

@@ -0,0 +1,113 @@
#include "brightness.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void print_usage(const char *prog_name) {
printf("Usage: %s [OPTION]...\n", prog_name);
printf("Options:\n");
printf(" -s, --save Save current brightness\n");
printf(" -r, --restore Restore saved brightness\n");
printf(" --set VALUE Set brightness to VALUE\n");
printf(" -a, --adjust DIR Adjust brightness up or down\n");
printf(" -h, --help Display this help and exit\n");
printf(" -l, --list List devices with available brightness controls\n");
printf(" -q, --quiet Suppress output\n");
printf(" -p, --pretend Do not perform write operations\n");
printf(" -n, --min-value MIN_VALUE Set minimum brightness value\n");
printf(" -e, --exponent EXPONENT Change percentage curve to exponential\n");
printf(" -c, --class CLASS Specify device class\n");
printf(" -d, --device DEVICE Specify device name\n");
printf(" -v, --version Print version and exit\n");
}
int main(int argc, char *argv[]) {
int opt;
static struct option long_options[] = {
{"save", no_argument, NULL, 's'},
{"restore", no_argument, NULL, 'r'},
{"set", required_argument, NULL, 0},
{"adjust", required_argument, NULL, 'a'},
{"help", no_argument, NULL, 'h'},
{"list", no_argument, NULL, 'l'},
{"quiet", no_argument, NULL, 'q'},
{"pretend", no_argument, NULL, 'p'},
{"min-value", required_argument, NULL, 'n'},
{"exponent", required_argument, NULL, 'e'},
{"class", required_argument, NULL, 'c'},
{"device", required_argument, NULL, 'd'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
while ((opt = getopt_long(argc, argv, "sra:hlqpne:c:d:v", long_options, NULL)) != -1) {
switch (opt) {
case 's':
save_brightness();
break;
case 'r':
restore_brightness();
break;
case 0: // --set
if (optarg) {
int value = atoi(optarg);
set_brightness(value, 0);
}
break;
case 'a':
if (optarg) {
adjust_brightness(optarg);
}
break;
case 'h':
print_usage(argv[0]);
return EXIT_SUCCESS;
case 'l':
list_devices();
break;
case 'q':
enable_quiet_mode();
break;
case 'p':
enable_pretend_mode();
break;
case 'n':
if (optarg) {
int min_value = atoi(optarg);
set_min_value(min_value);
}
break;
case 'e':
if (optarg) {
float exponent = atof(optarg);
set_exponent(exponent);
}
break;
case 'c':
if (optarg) {
set_class(optarg);
}
break;
case 'd':
if (optarg) {
set_device(optarg);
}
break;
case 'v':
printf("Version 1.0\n");
return EXIT_SUCCESS;
default:
print_usage(argv[0]);
return EXIT_FAILURE;
}
}
if (argc == 1) {
print_usage(argv[0]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,29 @@
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
char *read_file_to_string(const char *filepath) {
FILE *file = fopen(filepath, "r");
if (!file) {
perror("Failed to open file");
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_END);
long length = ftell(file);
fseek(file, 0, SEEK_SET);
char *content = malloc(length + 1);
if (!content) {
perror("Failed to allocate memory");
fclose(file);
exit(EXIT_FAILURE);
}
fread(content, 1, length, file);
content[length] = '\0';
fclose(file);
return content;
}

View File

@@ -0,0 +1,6 @@
#ifndef UTILS_H
#define UTILS_H
char *read_file_to_string(const char *filepath);
#endif