Remove \n on output by default, smarter buffer and better command construction on set

This commit is contained in:
Ethanell 2019-12-19 18:19:34 +01:00
parent 40b3de27aa
commit b745b71207

View file

@ -1,34 +1,30 @@
#include "xbrightness.h" #include "xbrightness.h"
void cmd(char *command, char *output) { static void cmd(char *command, char *output) {
FILE *fp; FILE *fp = popen(command, "r");
fp = popen(command, "r");
if (fp == NULL) { if (fp == NULL) {
output[0] = 0; output[0] = 0;
} else { } else {
fgets(output, sizeof(output), fp); fgets(output, sizeof(output), fp);
pclose(fp); pclose(fp);
if (output && output[strlen(output)-1] == '\n') {
output[strlen(output)-1] = 0;
}
} }
} }
float current() { float current() {
char out[BUFFER]; char out[BUFFER];
cmd("xrandr --verbose | grep Brightness: | cut -f2 -d\" \"", out); //TODO: Check when 0 screen connected and two or more connected and catch output error cmd("xrandr --verbose | grep Brightness: | cut -f2 -d\" \"", out);
return strtof(out, NULL); //TODO: Return NULL when convert fail return strtof(out, NULL);
} }
void set(float brightness) { void set(float brightness) {
char screen[BUFFER]; //TODO: Better buffer char screenName[64], b[3], command[BUFFER] = "xrandr --output ";
char command[BUFFER] = "xrandr --output "; cmd("xrandr | grep \" connected\" | cut -f1 -d\" \"", screenName);
char command2[] = " --brightness "; strcat(strcat(strcat(command, screenName), " --brightness "), gcvt(brightness, 3, b));
char b[3];
cmd("xrandr | grep \" connected\" | cut -f1 -d\" \"", screen); //TODO: Check when 0 screen connected and two or more connected and catch output error
screen[strlen(screen)-1] = 0;
strcat(command, screen);
strcat(command, command2);
gcvt(brightness, 3, b);
strcat(command, b);
cmd(command, NULL); //TODO: Catch output error cmd(command, NULL); //TODO: Catch output error
} }