Xmd/xmbrightness/src/main.c

87 lines
2.0 KiB
C
Raw Normal View History

2023-11-03 15:59:24 -06:00
/* C99 */
#define _XOPEN_SOURCE
#include <Xm/Scale.h>
#include <Xm/Xm.h>
#include <Xm/MwmUtil.h>
#include <X11/Shell.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void valueChanged (Widget, XtPointer, XtPointer);
void setBrightness (int);
int getBrightness ();
int getMaxBrightness ();
int main (int argc, char *argv[]) {
XtAppContext application;
Widget window = XtVaAppInitialize (
&application, "Brightness",
NULL, 0,
&argc, argv,
NULL,
XmNtitle, "Battery",
NULL);
int level = getBrightness();
int max = getMaxBrightness();
int percent =
(float)(level) /
(float)(max) * 100;
Widget scale = XtVaCreateManagedWidget (
"Brightness", xmScaleWidgetClass, window,
XmNmaximum, 100,
XmNminimum, 1,
XmNvalue, percent,
NULL);
XtAddCallback(scale, XmNvalueChangedCallback, valueChanged, NULL);
XtVaSetValues(window, XmNmwmDecorations, MWM_DECOR_BORDER, NULL);
XtVaSetValues(window, XmNmwmFunctions, MWM_FUNC_RESIZE, NULL);
//XtVaSetValues(window, XtNheight, 400, NULL);
//XtVaSetValues(window, XtNwidth, 400, NULL);
XtRealizeWidget(window);
XtAppMainLoop(application);
}
void valueChanged (Widget scale, XtPointer clientData, XtPointer callData) {
(void)(scale);
(void)(clientData);
XmScaleCallbackStruct *event = (XmScaleCallbackStruct *)(callData);
setBrightness(event->value);
}
void setBrightness (int level) {
char command[32] = { 0 };
snprintf(command, 32, "brightnessctl s %d%% -q", level);
system(command);
}
int getBrightness () {
FILE *stream = popen("brightnessctl g", "r");
if (stream == NULL) {
fprintf(stderr, "ERR could not get brightness\n");
return 1;
}
int result = 1;
fscanf(stream, "%d", &result);
pclose(stream);
return result;
}
int getMaxBrightness () {
FILE *stream = popen("brightnessctl m", "r");
if (stream == NULL) {
fprintf(stderr, "ERR could not get brightness\n");
return 1;
}
int result = 1;
fscanf(stream, "%d", &result);
pclose(stream);
return result;
}