URI: 
       Add brightness.c and charge-thresholds.c - system76-tools - collection of utilities for system76 laptops
  HTML git clone https://git.parazyd.org/system76-tools
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 13b5b35c45ecc3ee8acf46ef95c20a54e4bd1c66
  HTML Author: parazyd <parazyd@dyne.org>
       Date:   Wed, 19 Oct 2022 14:07:40 +0200
       
       Add brightness.c and charge-thresholds.c
       
       Diffstat:
         A Makefile                            |      35 +++++++++++++++++++++++++++++++
         A brightness.c                        |      86 ++++++++++++++++++++++++++++++
         A charge-thresholds.c                 |      82 +++++++++++++++++++++++++++++++
       
       3 files changed, 203 insertions(+), 0 deletions(-)
       ---
   DIR diff --git a/Makefile b/Makefile
       @@ -0,0 +1,35 @@
       +.POSIX:
       +
       +PREFIX = /usr/local
       +CFLAGS = -std=c99 -pedantic -Wall -Wextra -Werror -Os -static
       +LDFLAGS = -s -static
       +
       +SBIN = charge-thresholds brightness
       +
       +CC = cc
       +
       +all: $(SBIN)
       +
       +$(SBIN):
       +        $(CC) -c $(CFLAGS) $@.c
       +        $(CC) -o $@ $@.o $(LDFLAGS)
       +
       +clean:
       +        for i in $(SBIN); do \
       +                rm -f $$i.o $$i ; \
       +        done
       +
       +install: all
       +        mkdir -p $(DESTDIR)$(PREFIX)/bin
       +        for i in $(SBIN); do \
       +                cp -f $$i $(DESTDIR)$(PREFIX)/bin ; \
       +                chmod 4711 $(DESTDIR)$(PREFIX)/bin/$$i ; \
       +                chmod u+s $(DESTDIR)$(PREFIX)/bin/$$i ; \
       +        done
       +
       +uninstall:
       +        for i in $(SBIN); do \
       +                rm -f $(DESTDIR)$(PREFIX)/bin/$$i ; \
       +        done
       +
       +.PHONY: all clean install uninstall
   DIR diff --git a/brightness.c b/brightness.c
       @@ -0,0 +1,86 @@
       +#define _GNU_SOURCE
       +#include <stdio.h>
       +#include <string.h>
       +#include <stdlib.h>
       +
       +const char *BRIGHTNESS_MAX = "/sys/class/backlight/intel_backlight/max_brightness";
       +const char *BRIGHTNESS_CUR = "/sys/class/backlight/intel_backlight/brightness";
       +
       +void usage(void)
       +{
       +        fprintf(stderr, "usage: brightness up|dn\n");
       +        exit(1);
       +}
       +
       +void die(const char *m)
       +{
       +        perror(m);
       +        exit(1);
       +}
       +
       +enum Op {
       +        UP,
       +        DN,
       +};
       +
       +int main(int argc, char *argv[])
       +{
       +        enum Op op;
       +        int new, max, cur, inc;
       +        size_t len;
       +        char *line;
       +        FILE *fd;
       +
       +        if (argc != 2)
       +                usage();
       +
       +        if (!strcmp(argv[1], "up"))
       +                op = UP;
       +        else if (!strcmp(argv[1], "dn"))
       +                op = DN;
       +        else
       +                usage();
       +
       +        /* Find out max brightness */
       +        if ((fd = fopen(BRIGHTNESS_MAX, "r")) == NULL)
       +                die("fopen");
       +
       +        size_t nread = getline(&line, &len, fd);
       +        (void)nread;
       +        fclose(fd);
       +        fd = NULL;
       +
       +        max = atoi(line);
       +        free(line);
       +        line = NULL;
       +
       +        inc = max / 20;
       +
       +        if ((fd = fopen(BRIGHTNESS_CUR, "w+")) == NULL)
       +                die("fopen");
       +
       +        nread = getline(&line, &len, fd);
       +        (void)nread;
       +        cur = atoi(line);
       +        free(line);
       +        line = NULL;
       +
       +        switch(op) {
       +        case UP:
       +                if (cur + inc > max)
       +                        new = max;
       +                else
       +                        new = cur + inc;
       +                break;
       +        case DN:
       +                if (cur - inc < 1)
       +                        new = 1;
       +                else
       +                        new = cur - inc;
       +        }
       +
       +        fprintf(fd, "%d", new);
       +        fclose(fd);
       +
       +        return 0;
       +}
   DIR diff --git a/charge-thresholds.c b/charge-thresholds.c
       @@ -0,0 +1,82 @@
       +#include <stdio.h>
       +#include <string.h>
       +#include <stdlib.h>
       +
       +static const char *START_FD = "/sys/class/power_supply/BAT0/charge_control_start_threshold";
       +static const char *END_FD = "/sys/class/power_supply/BAT0/charge_control_end_threshold";
       +
       +void usage(void)
       +{
       +        printf("usage: charge-thresholds full-charge|balanced|max-lifespan\n\n");
       +    printf("Profiles:\n");
       +
       +    printf("full-charge: Battery is charged to its full capacity for\n"
       +            "the longest possible use on battery power.\n"
       +            "Charging resumes when the battery falls below 96%%.\n\n");
       +
       +        printf("balanced: Use this threshold when you unplug frequently but\n"
       +                "don't need the full battery capacity. Charging stops when the\n"
       +                "battery reaches 90%% capacity and resumes when the battery\n"
       +                "falls below 85%%.\n\n");
       +
       +    printf("max-lifespan: Use this threshold if you rarely use the system\n"
       +            "on battery for extended periods. Charging stops when the battery\n"
       +            "reaches 60%% capacity and resumes when the battery falls below 50%%\n");
       +
       +    exit(1);
       +}
       +
       +void die(const char *m)
       +{
       +    perror(m);
       +    exit(1);
       +}
       +
       +int main(int argc, char *argv[])
       +{
       +    int start, end; 
       +    FILE *fd;
       +
       +    if (argc != 2)
       +            usage();
       +
       +    if (!strcmp(argv[1], "full-charge")) {
       +        start = 96;
       +        end = 100;
       +    } else if (!strcmp(argv[1], "balanced")) {
       +        start = 86;
       +        end = 90;
       +    } else if (!strcmp(argv[1], "max-lifespan")) {
       +        start = 50;
       +        end = 60;
       +    } else {
       +        usage();
       +    }
       +
       +    /* Without this, setting start threshold may fail if the previous end
       +     * threshold is higher
       +     */
       +    if ((fd = fopen(END_FD, "w")) == NULL)
       +        die("fopen");
       +
       +    fprintf(fd, "100");
       +    fclose(fd);
       +    fd = NULL;
       +
       +    if ((fd = fopen(START_FD, "w")) == NULL)
       +        die("fopen");
       +
       +    fprintf(fd, "%d", start);
       +    fclose(fd);
       +    fd = NULL;
       +
       +    if ((fd = fopen(END_FD, "w")) == NULL)
       +        die("fopen");
       +
       +    fprintf(fd, "%d", end);
       +    fclose(fd);
       +    fd = NULL;
       +
       +    printf("Thresholds set to: %d-%d\n", start, end);
       +    return 0;
       +}