mirror of
https://github.com/bvanroll/_dotfiles.git
synced 2025-08-29 20:12:42 +00:00
init
This commit is contained in:
21
.config/i3blocks/brightness/LICENSE
Normal file
21
.config/i3blocks/brightness/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Oleg Katkov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
63
.config/i3blocks/brightness/Makefile
Normal file
63
.config/i3blocks/brightness/Makefile
Normal file
@@ -0,0 +1,63 @@
|
||||
CC=gcc
|
||||
AS=as
|
||||
LD=ld
|
||||
OBJDUMP=objdump
|
||||
OBJCOPY=objcopy
|
||||
SIZE=size
|
||||
LINK=$(CC)
|
||||
|
||||
BUILD_DIR=build
|
||||
OBJ_DIR=$(BUILD_DIR)/obj
|
||||
BIN_DIR=bin
|
||||
|
||||
LIBS :=
|
||||
DEFS := -DVERSION=\"0.1\"
|
||||
WARN_LEVEL = -Wall -Wextra -pedantic
|
||||
|
||||
PRG = brightness
|
||||
INCLUDES := -Iinc
|
||||
CFLAGS := $(INCLUDES) $(DEFS) $(WARN_LEVEL) -pipe -O0 -g3 -std=c11
|
||||
|
||||
debug: CFLAGS += -O0 -g3
|
||||
debug: all
|
||||
|
||||
release: CFLAGS += -O2
|
||||
release: all
|
||||
LDFLAGS = $(LIBS) -ffunction-sections -Wl,--gc-sections
|
||||
|
||||
SRC_C := $(wildcard *.c) $(wildcard src/*.c)
|
||||
SRC_A := $(wildcard src/*.s)
|
||||
|
||||
OBJECTS := $(SRC_C:%.c=$(OBJ_DIR)/%.o)
|
||||
OBJECTS += $(SRC_A:%.s=$(OBJ_DIR)/%.o)
|
||||
|
||||
all: directories $(PRG)
|
||||
|
||||
$(PRG): $(BIN_DIR)/$(PRG)
|
||||
|
||||
$(OBJ_DIR)/%.o: %.s
|
||||
@mkdir -p $(@D)
|
||||
$(AS) $(INCLUDES) $(MMCU) -g -o $@ $^
|
||||
|
||||
$(OBJ_DIR)/%.o: %.c
|
||||
@mkdir -p $(@D)
|
||||
$(CC) $(CFLAGS) -o $@ -c $<
|
||||
|
||||
$(BIN_DIR)/$(PRG): $(OBJECTS)
|
||||
@mkdir -p $(@D)
|
||||
$(LINK) -o $(BIN_DIR)/$(PRG) $^ $(LDFLAGS) $(LIBS)
|
||||
|
||||
.PHONY: directories
|
||||
directories:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
@mkdir -p $(BIN_DIR)
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@rm -rf $(BUILD_DIR)/*
|
||||
@rm -rf $(BIN_DIR)/*
|
||||
|
||||
.PHONY: mrproper
|
||||
mrproper:
|
||||
@rm -rf $(BUILD_DIR)
|
||||
@rm -rf $(BIN_DIR)
|
41
.config/i3blocks/brightness/README.md
Normal file
41
.config/i3blocks/brightness/README.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# brightness
|
||||
|
||||
## Description
|
||||
|
||||
This small tool listens to file "actual_brightness" modification and reports current brightness percentage. Written in C. It can be used with i3blocks persistent block.
|
||||
|
||||

|
||||
|
||||
## How to build
|
||||
|
||||
`make release`
|
||||
|
||||
The result executable will be placed into bin directory.
|
||||
|
||||
## Config
|
||||
|
||||
```
|
||||
[brightness]
|
||||
command=./brightness/bin/brightness -a /sys/class/backlight/intel_backlight/actual_brightness -m /sys/class/backlight/intel_backlight/max_brightness
|
||||
interval=persist
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
brightness 0.1 - read actual brightness value in non-blocking style.
|
||||
|
||||
Usage: brightness [options]
|
||||
|
||||
Options:
|
||||
-a, --actual_brightness_path path to file with actual brightness string
|
||||
-m, --max_brightness_path path to file with max brightness string
|
||||
-h, --help print this help.
|
||||
-V, --version print version and exit.
|
||||
|
||||
```
|
||||
|
||||
## Roadmap
|
||||
|
||||
- [x] Connect tool with i3blocks
|
||||
- [ ] Remove full paths from command arguments. Make it possible to set class or device and parse full path automatically via "/sys/brightness" or "/sys/leds".
|
BIN
.config/i3blocks/brightness/brightness.png
Normal file
BIN
.config/i3blocks/brightness/brightness.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1000 B |
199
.config/i3blocks/brightness/main.c
Normal file
199
.config/i3blocks/brightness/main.c
Normal file
@@ -0,0 +1,199 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <dirent.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <getopt.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/inotify.h>
|
||||
|
||||
#define INOTIFY_EVENT_SIZE (sizeof (struct inotify_event))
|
||||
#define INOTIFY_BUFF_SIZE 64
|
||||
|
||||
static bool get_float_value_from_file(const char *path,
|
||||
float *dst_val);
|
||||
|
||||
static void print_brightness_percent(const char *actual_path,
|
||||
const char *max_path);
|
||||
|
||||
static void usage(void) {
|
||||
fprintf(stderr, "brightness %s - read actual brightness value in non-blocking style.\n\n", VERSION);
|
||||
fprintf(stderr,
|
||||
"Usage: brightness [options]\n\
|
||||
\n\
|
||||
Options:\n\
|
||||
-a, --actual_brightness_path \tpath to file with actual brightness string\n\
|
||||
-m, --max_brightness_path \tpath to file with max brightness string\n\
|
||||
-h, --help \tprint this help.\n\
|
||||
-V, --version \tprint version and exit.\n\
|
||||
\n");
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
int
|
||||
main(int argc, char *argv[]) {
|
||||
static const char *default_abp = "/sys/class/backlight/intel_backlight/actual_brightness";
|
||||
static const char *default_mbp = "/sys/class/backlight/intel_backlight/max_brightness";
|
||||
char *actual_brightness_path = default_abp;
|
||||
char *max_brightness_path = default_mbp;
|
||||
|
||||
char buff[INOTIFY_BUFF_SIZE] = {0};
|
||||
fd_set read_descriptors;
|
||||
struct timeval time_to_wait;
|
||||
int ifd, wd, read_len, rc, opt_idx;
|
||||
size_t opt_len;
|
||||
// ifd - inotify_file_descriptor
|
||||
// wd - inotify wait descriptor
|
||||
// rc - result code
|
||||
|
||||
static const struct option lopts[] = {
|
||||
{"actual_brightness_path", required_argument, NULL, 'a'},
|
||||
{"max_brightness_path", required_argument, NULL, 'm'},
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{"version", no_argument, NULL, 'V'},
|
||||
{NULL, 0, NULL, 0},
|
||||
};
|
||||
|
||||
while ((opt_idx = getopt_long(argc, argv, "a:m:hV", lopts, NULL)) != -1) {
|
||||
switch (opt_idx) {
|
||||
case 'a':
|
||||
opt_len = strlen(optarg);
|
||||
actual_brightness_path = malloc(opt_len + 1);
|
||||
strcpy(actual_brightness_path, optarg);
|
||||
break;
|
||||
case 'm':
|
||||
opt_len = strlen(optarg);
|
||||
max_brightness_path = malloc(opt_len + 1);
|
||||
strcpy(max_brightness_path, optarg);
|
||||
break;
|
||||
case 'h':
|
||||
usage();
|
||||
return 0;
|
||||
case 'V':
|
||||
printf("%s\n", VERSION);
|
||||
return 0;
|
||||
default:
|
||||
printf("something bad is happened. option index is out of bounds (%d %c)\n", opt_idx, (char)opt_idx);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
print_brightness_percent(actual_brightness_path,
|
||||
max_brightness_path);
|
||||
|
||||
ifd = inotify_init();
|
||||
if (ifd == -1) {
|
||||
perror("inotify init failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
wd = inotify_add_watch(ifd, actual_brightness_path, IN_MODIFY);
|
||||
FD_ZERO (&read_descriptors);
|
||||
FD_SET (ifd, &read_descriptors);
|
||||
time_to_wait.tv_sec = 10;
|
||||
time_to_wait.tv_usec = 0;
|
||||
|
||||
while (1) {
|
||||
fd_set tmp_set = read_descriptors;
|
||||
rc = select(ifd+1, &tmp_set, NULL, NULL, &time_to_wait);
|
||||
if (rc < 0) {
|
||||
perror("select failed");
|
||||
break;
|
||||
}
|
||||
|
||||
if (rc == 0) { //timeout
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!FD_ISSET(ifd, &read_descriptors)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
read_len = read(ifd, (void*)buff, INOTIFY_BUFF_SIZE);
|
||||
if (read_len < 0) {
|
||||
perror("read failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int eix = 0; eix < read_len; ) {
|
||||
struct inotify_event *event = (struct inotify_event*) &buff[eix];
|
||||
eix += INOTIFY_EVENT_SIZE + event->len;
|
||||
if (!(event->mask & IN_MODIFY)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
print_brightness_percent(actual_brightness_path,
|
||||
max_brightness_path);
|
||||
}
|
||||
}
|
||||
|
||||
//actually when we receive some signal (like sigterm or something like that)
|
||||
//we will be here.
|
||||
|
||||
inotify_rm_watch(ifd, wd);
|
||||
close(ifd);
|
||||
|
||||
if (actual_brightness_path != default_abp)
|
||||
free(actual_brightness_path);
|
||||
if (max_brightness_path != default_mbp)
|
||||
free(max_brightness_path);
|
||||
return 0;
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
bool
|
||||
get_float_value_from_file(const char *path,
|
||||
float *dst_val) {
|
||||
#define BUFF_SIZE 16
|
||||
char buff[BUFF_SIZE] = {0};
|
||||
FILE *f = fopen(path, "r");
|
||||
bool result = false;
|
||||
if (f == NULL) {
|
||||
perror("failed to open file");
|
||||
return false;
|
||||
}
|
||||
|
||||
do {
|
||||
if (!(fread((void*) buff, 1, BUFF_SIZE, f))) {
|
||||
perror("failed to read file");
|
||||
break;
|
||||
}
|
||||
|
||||
char *unused;
|
||||
*dst_val = strtof(buff, &unused);
|
||||
result = true;
|
||||
} while (0);
|
||||
|
||||
fclose(f);
|
||||
return result;
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
void
|
||||
print_brightness_percent(const char *actual_path,
|
||||
const char *max_path) {
|
||||
float curr, max;
|
||||
const char *parr[] = {actual_path, max_path};
|
||||
float *farr[] = {&curr, &max};
|
||||
|
||||
for (size_t i = 0; i < 2; ++i) {
|
||||
bool success = get_float_value_from_file(parr[i], farr[i]);\
|
||||
if (!success) {
|
||||
printf("\xF0\x9F\x94\x86"); //brightness symbol 🔆
|
||||
printf(": NA\n");
|
||||
fflush(stdout); // because we use select(), ant it blocks stdout
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\xF0\x9F\x94\x86"); //brightness symbol 🔆
|
||||
printf(": %02.f%%\n", (curr / max) * 100.f);
|
||||
fflush(stdout); // because we use select(), ant it blocks stdout
|
||||
}
|
||||
//////////////////////////////////////////////////////////////
|
Reference in New Issue
Block a user