Finalized first public version with many changes.

Made the codebase publishable:
- Removed various testing code.
- Improved the makefile.
- Rewrote README.org.
- Changed filename to reflect project.
- Other minor cleanups
pull/1/head
Christina 1 year ago
parent 9714c5a3c7
commit c65561d6b0
Signed by: cafkafk
GPG Key ID: 8ED4DEF7375A30C8
  1. 31
      Makefile
  2. 79
      README.org
  3. 77
      li.c

@ -1,16 +1,29 @@
TARGET=li
SRC=li.c
PREFIX ?= /usr/local
CC=gcc
CFLAGS+=-O3 -march=native -std=c11 -Wall -Wno-stringop-truncation
lc: lc.c
$(CC) -o lc $(CFLAGS) $<
CFLAGS+=-O3 -march=native -std=c11 -Wall -W -pedantic -Wno-stringop-truncation
CPPFLAGS += -D_POSIX_C_SOURCE=1
debug-lc: lc.c
$(CC) -o lc-debug -Og -g $<
.PHONY: all install uninstall debug clean
check:
echo "check"
all: $(TARGET)
debug: $(TARGET)-debug
clean:
$(RM) lc
$(TARGET): $(SRC)
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(SRC) -o $@
install: $(TARGET)
mkdir -p $(DESTDIR)$(PREFIX)/bin
install -m755 $(TARGET) $(DESTDIR)$(PREFIX)/bin/$(TARGET)
uninstall:
$(RM) $(DESTDIR)$(PREFIX)/bin/$(TARGET)
$(TARGET)-debug: $(SRC)
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(SRC) -Og -o $@
clean:
$(RM) $(TARGET)
$(RM) $(TARGET)-debug

@ -1,37 +1,64 @@
* LC - Light Command
Light command is a small c program that adjusts the brightness of monitors.
At the moment, only built-in monitors function i.e. most laptop monitors.
Why make such a tiny program? There was a vacuum between gui application and
shell scripts. The author did not particularly enjoy xbacklight
(specially it's dependence on a xconfig file for the monitor in some cases,
and the syntax is just stupid... -set?).
* LI - backLIght changer
Light command is a small c program that adjusts the brightness of monitors. At
the moment, only built-in monitors work i.e. most laptop monitors. Why make such
a tiny program? There was a vacuum between gui application and shell scripts.
The author did not particularly enjoy =xbacklight= (specially it's dependence on a
xconfig file for the monitor in some cases, and the syntax is just stupid...
=-set=?).
** USAGE
LI seeks to correct this somewhat... but it's not developed for the masses --
just for me, so don't expect anything.
* Features
- Adjust brightness of built-in monitors (laptop monitors).
- Works on % of maxbrightness, not the arbitrary /sys/class/backlight... value fun.
- Micro codebase.
- Somewhat safe to =setuid=.
* Manual
** Downloading
*** git HTTPS
To get the latest version with git over HTTPS:
#+begin_src sh
git clone https://github.com/cafkafk/li.git
#+end_src
*** git SSH
To get the latest version with git over SSH:
#+begin_src sh
git clone git@github.com:cafkafk/li.git
#+end_src
** Building
To build simply run:
#+begin_src bash
$ make
#+end_src
** Installing
To install simply run:
#+begin_src bash
$ make install
#+end_srcL
** Uninstalling
To uninstall simply run:
#+begin_src bash
$ make uninstall
#+end_src
** Usage
To get help for =lc=
#+begin_src sh
$ lc --help
#+end_src
To set the target brightness (as an integer).
#+begin_src
# lc device brightness
#+end_src
*Finding devices*: lc will tell you the names of devices on help,
but if you would like to find it yourself, simply run:
#+begin_src bash
$ ls /sys/class/backlight/
# lc [int]
#+end_src
*Notice*: the brightness is a percentage, so 100 -> 100% brightness,
50 -> 50% brightness etc...
Further it must be in interval (0;100].
** BUILD
To build simply run make.
#+begin_src bash
$ make
#+end_src
** INSTALL
Simply put the =lc= file into your path, or a folder in your path.
** ROADMAP
- [ ] Down the road, lc looks to support ddcutils,
so it can change desktop monitors as well.
Further it must be in the interval (0;100].
** ROADMAP
- [-] built-in monitor support
- [ ] I2C support
- [ ] (unsure) multi-monitor

@ -1,5 +1,5 @@
/* lc - light command: a brightness setter
* Copyright (C) 2020 - 2021 Christina Sørensen
* Copyright (C) 2020 - 2021 Christina (cafkafk)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -18,98 +18,93 @@
#define MAXPATHLENGTH 128
#include <assert.h> // for assert
#include <dirent.h>
#include <errno.h> // for errno, strerror
#include <error.h> // for strcat, strcpy
#include <stdio.h> // for printf, etc...
#include <stdlib.h> // for EXIT_FAILURE, EXIT_SUCCESS, etc...
#include <string.h> // for strcat, strcpy
#include <error.h> // for strcat, strcpy
#include <dirent.h>
int main(int argc, char *argv[]) {
// CHECK INPUT
DIR *d;
struct dirent *dir;
if (argc == 2)
if ((!(strcmp(argv[1], "--help"))) || (!(strcmp(argv[1], "help"))) ||
(!(strcmp(argv[1], "-h")))) {
printf("Usage: li <brighness>\n");
printf("Where brightness is a integer percentage of max(e.g. 1-100)\n");
exit(EXIT_SUCCESS);
}
if (argc != 2)
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "not enough arguments - try running help");
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"not enough arguments - try running help");
else if (atoi(argv[1]) == 0 || atoi(argv[1]) > 100)
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "invalid brightness [1-100]");
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"invalid brightness [1-100]");
// INITIALIZE PATH VARS
char *pre = "/sys/class/backlight/";
char basepath[MAXPATHLENGTH] = {0};
char maxpath[MAXPATHLENGTH] = {0};
char brightpath[MAXPATHLENGTH] = {0};
strcpy(basepath, pre); // This is safe. PRE is of known length
// VALIDATE USER INPUT LENGTH
DIR *d;
struct dirent *dir;
d = opendir(pre);
if(d == NULL)
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "opendir(path) failed");
if (d == NULL)
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"opendir(path) failed");
// FIXME THIS IS UGLY
if((dir=readdir(d)) == NULL)
if ((dir = readdir(d)) == NULL)
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "readdir(d) failed");
if((dir=readdir(d)) == NULL)
if ((dir = readdir(d)) == NULL)
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "readdir(d) failed");
if((dir=readdir(d)) == NULL)
if ((dir = readdir(d)) == NULL) {
fprintf(stderr, "%s", "No compatible display found.\n");
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "readdir(d) failed");
}
closedir(d);
printf("%s\n", dir->d_name);
if (closedir(d))
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"closedir(d) failed");
strncat(basepath, dir->d_name, MAXPATHLENGTH - strlen(pre) - 1);
strncat(maxpath, basepath, MAXPATHLENGTH - 1);
strncat(brightpath, basepath, MAXPATHLENGTH - 1);
printf("%s\n", basepath);
printf("%s\n", maxpath);
printf("%s\n", brightpath);
// SET PATHS FOR BACKLIGHT FILE I/O
strcat(maxpath,
"/max_brightness"); // Not really safe, but not exploitable either.
strcat(brightpath, "/brightness"); // Will just crash if length exceeds
printf("%s\n", maxpath);
printf("%s\n", brightpath);
// OPEN FILES FOR I/O
FILE *m_fp = fopen(maxpath, "r");
if (m_fp == NULL)
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "fopen(maxpath, \"r\")");
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"fopen(maxpath, \"r\")");
FILE *b_fp = fopen(brightpath, "w");
if (b_fp == NULL)
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "fopen(brightpath, \"w\")");
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"fopen(brightpath, \"w\")");
// READ MAX BRIGHTNESS
int m_b;
if(0 >= fscanf(m_fp, "%d", &m_b))
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "fscanf(m_fp...) failed");
if (0 >= fscanf(m_fp, "%d", &m_b))
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"fscanf(m_fp...) failed");
// CALCULATE TARGET BRIGHTNESS AS % OF MAX
if (!fprintf(b_fp, "%d", atoi(argv[1]) * (m_b / 100)))
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "fprintf(b_fp...) failed");
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"fprintf(b_fp...) failed");
// CLOSE FILES
if(fclose(m_fp)) {
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "fclose(m_fp) failed");
if (fclose(m_fp)) {
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"fclose(m_fp) failed");
}
if(fclose(b_fp)) {
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__, "fclose(b_fp) failed");
if (fclose(b_fp)) {
error_at_line(EXIT_FAILURE, errno, __FILE__, __LINE__,
"fclose(b_fp) failed");
}
return EXIT_SUCCESS;
Loading…
Cancel
Save