Tab indent C programs for consistency

master
Miles Alan 4 years ago
parent 3c65a36fd2
commit 1ac5e64193
  1. 228
      programs/sxmo_screenlock.c
  2. 52
      programs/sxmo_setpinebacklight.c
  3. 82
      programs/sxmo_setpineled.c
  4. 136
      programs/sxmo_vibratepine.c

@ -17,149 +17,147 @@ static char * brightnessfile = "/sys/devices/platform/backlight/backlight/backli
void updatepineled(int red, int brightness) { void updatepineled(int red, int brightness) {
sprintf( sprintf(
pineledcommand, pineledcommand,
"sh -c 'echo %d > /sys/class/leds/%s:indicator/brightness'", "sh -c 'echo %d > /sys/class/leds/%s:indicator/brightness'",
brightness, brightness,
red ? "red" : "blue" red ? "red" : "blue"
); );
system(pineledcommand); system(pineledcommand);
} }
void updatescreenon(int on) { void updatescreenon(int on) {
int b = on ? oldbrightness : 0; int b = on ? oldbrightness : 0;
sprintf(screentogglecommand, "sh -c 'echo %d > %s'", b, brightnessfile); sprintf(screentogglecommand, "sh -c 'echo %d > %s'", b, brightnessfile);
system(screentogglecommand); system(screentogglecommand);
updatepineled(0, b ? 1 : 0); updatepineled(0, b ? 1 : 0);
updatepineled(1, b ? 0 : 1); updatepineled(1, b ? 0 : 1);
} }
void cleanup() { void cleanup() {
updatescreenon(1); updatescreenon(1);
updatepineled(1, 0); updatepineled(1, 0);
updatepineled(0, 0); updatepineled(0, 0);
} }
static void die(const char *err, ...) { static void die(const char *err, ...) {
fprintf(stderr, "Error: %s", err); fprintf(stderr, "Error: %s", err);
cleanup(); cleanup();
exit(1); exit(1);
} }
static void usage(void) { static void usage(void) {
die("usage: slock [-v] [cmd [arg ...]]\n"); die("usage: slock [-v] [cmd [arg ...]]\n");
} }
// Loosely derived from suckless' slock's lockscreen binding logic but // Loosely derived from suckless' slock's lockscreen binding logic but
// alot more coarse, intentionally so can be triggered while grab_key // alot more coarse, intentionally so can be triggered while grab_key
// for dwm multikey path already holding.. // for dwm multikey path already holding..
void lockscreen(Display *dpy, int screen) { void lockscreen(Display *dpy, int screen) {
int i, ptgrab, kbgrab; int i, ptgrab, kbgrab;
//XSetWindowAttributes wa; //XSetWindowAttributes wa;
Window root; Window root;
//win, //win,
root = RootWindow(dpy, screen); root = RootWindow(dpy, screen);
//wa.override_redirect = 1; //wa.override_redirect = 1;
//win = XCreateWindow(dpy, root, 0, 0, //win = XCreateWindow(dpy, root, 0, 0,
// DisplayWidth(dpy, screen), // DisplayWidth(dpy, screen),
// DisplayHeight(dpy, screen), // DisplayHeight(dpy, screen),
// 0, DefaultDepth(dpy, screen), // 0, DefaultDepth(dpy, screen),
// CopyFromParent, // CopyFromParent,
// DefaultVisual(dpy, screen), // DefaultVisual(dpy, screen),
// CWOverrideRedirect | CWBackPixel, &wa); // CWOverrideRedirect | CWBackPixel, &wa);
for (i = 0, ptgrab = kbgrab = -1; i < 9999999; i++) { for (i = 0, ptgrab = kbgrab = -1; i < 9999999; i++) {
if (ptgrab != GrabSuccess) { if (ptgrab != GrabSuccess) {
ptgrab = XGrabPointer(dpy, root, False, ptgrab = XGrabPointer(dpy, root, False,
ButtonPressMask | ButtonReleaseMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask, GrabModeAsync, PointerMotionMask, GrabModeAsync,
GrabModeAsync, None, None, CurrentTime); GrabModeAsync, None, None, CurrentTime);
} }
if (kbgrab != GrabSuccess) { if (kbgrab != GrabSuccess) {
kbgrab = XGrabKeyboard(dpy, root, True, kbgrab = XGrabKeyboard(dpy, root, True,
GrabModeAsync, GrabModeAsync, CurrentTime); GrabModeAsync, GrabModeAsync, CurrentTime);
} }
if (ptgrab == GrabSuccess && kbgrab == GrabSuccess) { if (ptgrab == GrabSuccess && kbgrab == GrabSuccess) {
XSelectInput(dpy, root, SubstructureNotifyMask); XSelectInput(dpy, root, SubstructureNotifyMask);
return; return;
} }
usleep(100000); usleep(100000);
} }
return;
} }
void void
readinputloop(Display *dpy, int screen) { readinputloop(Display *dpy, int screen) {
KeySym keysym; KeySym keysym;
XEvent ev; XEvent ev;
char buf[32]; char buf[32];
while (running && !XNextEvent(dpy, &ev)) { while (running && !XNextEvent(dpy, &ev)) {
if (ev.type == KeyPress) { if (ev.type == KeyPress) {
XLookupString(&ev.xkey, buf, sizeof(buf), &keysym, 0); XLookupString(&ev.xkey, buf, sizeof(buf), &keysym, 0);
if (lastkeysym == keysym) { if (lastkeysym == keysym) {
lastkeyn++; lastkeyn++;
} else { } else {
lastkeysym = keysym; lastkeysym = keysym;
lastkeyn = 1; lastkeyn = 1;
} }
if (lastkeyn < 3) if (lastkeyn < 3)
continue; continue;
lastkeyn = 0; lastkeyn = 0;
lastkeysym = NULL; lastkeysym = NULL;
switch (keysym) { switch (keysym) {
case XF86XK_AudioRaiseVolume: case XF86XK_AudioRaiseVolume:
case XF86XK_AudioLowerVolume: case XF86XK_AudioLowerVolume:
screenon = !screenon; screenon = !screenon;
updatescreenon(screenon); updatescreenon(screenon);
break; break;
case XF86XK_PowerOff: case XF86XK_PowerOff:
cleanup(); cleanup();
running = 0; running = 0;
break; break;
} }
} }
} }
} }
int int
getoldbrightness() { getoldbrightness() {
char * buffer = 0; char * buffer = 0;
long length; long length;
FILE * f = fopen(brightnessfile, "rb"); FILE * f = fopen(brightnessfile, "rb");
if (f) { if (f) {
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
length = ftell(f); length = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
buffer = malloc(length); buffer = malloc(length);
if (buffer) { if (buffer) {
fread(buffer, 1, length, f); fread(buffer, 1, length, f);
} }
fclose(f); fclose(f);
} }
if (buffer) { if (buffer) {
oldbrightness = atoi(buffer); oldbrightness = atoi(buffer);
} }
} }
int int
main(int argc, char **argv) { main(int argc, char **argv) {
Display *dpy; Display *dpy;
Screen *screen; Screen *screen;
if (setuid(0)) if (setuid(0))
die("setuid(0) failed\n"); die("setuid(0) failed\n");
if (!(dpy = XOpenDisplay(NULL))) if (!(dpy = XOpenDisplay(NULL)))
die("Cannot open display\n"); die("Cannot open display\n");
screen = XDefaultScreen(dpy); screen = XDefaultScreen(dpy);
XSync(dpy, 0); XSync(dpy, 0);
getoldbrightness(); getoldbrightness();
lockscreen(dpy, screen); lockscreen(dpy, screen);
updatescreenon(1); updatescreenon(1);
readinputloop(dpy, screen); readinputloop(dpy, screen);
return 0; return 0;
} }

@ -7,39 +7,39 @@ char * pbpScreen = "/sys/class/backlight/edp-backlight/brightness";
char * ppScreen = "/sys/devices/platform/backlight/backlight/backlight/brightness"; char * ppScreen = "/sys/devices/platform/backlight/backlight/backlight/brightness";
void usage() { void usage() {
fprintf(stderr, "Usage: sxmo_setpinebacklight [number]\n"); fprintf(stderr, "Usage: sxmo_setpinebacklight [number]\n");
} }
void writeFile(char *filepath, int brightness) { void writeFile(char *filepath, int brightness) {
FILE *f; FILE *f;
f = fopen(filepath, "w+"); f = fopen(filepath, "w+");
fprintf(f, "%d\n", brightness); fprintf(f, "%d\n", brightness);
fclose(f); fclose(f);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char * command; char * command;
int brightness; int brightness;
if (argc < 2) { if (argc < 2) {
usage(); usage();
return 1; return 1;
} }
argc--; argc--;
brightness = atoi(argv[argc--]); brightness = atoi(argv[argc--]);
if (setuid(0)) { if (setuid(0)) {
fprintf(stderr, "setuid(0) failed\n"); fprintf(stderr, "setuid(0) failed\n");
return 1; return 1;
} }
if (access(pbpScreen, F_OK) != -1) { if (access(pbpScreen, F_OK) != -1) {
writeFile(pbpScreen, brightness); writeFile(pbpScreen, brightness);
fprintf(stderr, "Set PBP brightness to %d\n", brightness); fprintf(stderr, "Set PBP brightness to %d\n", brightness);
} else if (access(ppScreen, F_OK) != -1) { } else if (access(ppScreen, F_OK) != -1) {
writeFile(ppScreen, brightness); writeFile(ppScreen, brightness);
fprintf(stderr, "Set PP brightness to %d\n", brightness); fprintf(stderr, "Set PP brightness to %d\n", brightness);
} else { } else {
fprintf(stderr, "Neither PP or PBP Screen found!\n"); fprintf(stderr, "Neither PP or PBP Screen found!\n");
} }
} }

@ -4,53 +4,53 @@
#include <unistd.h> #include <unistd.h>
void usage() { void usage() {
fprintf(stderr, "Usage: setpineled [red|green|blue|white] [0-255]\n"); fprintf(stderr, "Usage: setpineled [red|green|blue|white] [0-255]\n");
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int brightness; int brightness;
char * color; char * color;
char * command; char * command;
char * type; char * type;
if (argc < 2) { if (argc < 2) {
usage(); usage();
return 1; return 1;
} }
argc--; argc--;
brightness = atoi(argv[argc--]); brightness = atoi(argv[argc--]);
if (brightness < 0 || brightness > 255) { if (brightness < 0 || brightness > 255) {
usage(); usage();
return 1; return 1;
} }
color = argv[argc--]; color = argv[argc--];
if ( if (
strcmp(color, "red") && strcmp(color, "red") &&
strcmp(color, "blue") && strcmp(color, "blue") &&
strcmp(color, "green") && strcmp(color, "green") &&
strcmp(color, "white") strcmp(color, "white")
) { ) {
usage(); usage();
return 1; return 1;
} }
if (!strcmp(color, "white")) { if (!strcmp(color, "white")) {
type = "flash"; type = "flash";
} else { } else {
type = "indicator"; type = "indicator";
} }
command = malloc(80); command = malloc(80);
sprintf( sprintf(
command, command,
"sh -c 'echo %d > /sys/class/leds/%s:%s/brightness'", "sh -c 'echo %d > /sys/class/leds/%s:%s/brightness'",
brightness, color, type brightness, color, type
); );
if (setuid(0)) { if (setuid(0)) {
fprintf(stderr, "setuid(0) failed\n"); fprintf(stderr, "setuid(0) failed\n");
} else { } else {
return system(command); return system(command);
} }
} }

@ -14,97 +14,97 @@
void syscall_error(int is_err, const char* fmt, ...) void syscall_error(int is_err, const char* fmt, ...)
{ {
va_list ap; va_list ap;
if (!is_err) if (!is_err)
return; return;
printf("ERROR: "); printf("ERROR: ");
va_start(ap, fmt); va_start(ap, fmt);
vprintf(fmt, ap); vprintf(fmt, ap);
va_end(ap); va_end(ap);
printf(": %s\n", strerror(errno)); printf(": %s\n", strerror(errno));
exit(1); exit(1);
} }
int open_event_dev(const char* name_needle, int flags) int open_event_dev(const char* name_needle, int flags)
{ {
char path[256]; char path[256];
char name[256]; char name[256];
int fd, ret; int fd, ret;
// find the right device and open it // find the right device and open it
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
snprintf(path, sizeof path, "/dev/input/event%d", i); snprintf(path, sizeof path, "/dev/input/event%d", i);
fd = open(path, flags); fd = open(path, flags);
if (fd < 0) if (fd < 0)
continue; continue;
ret = ioctl(fd, EVIOCGNAME(256), name); ret = ioctl(fd, EVIOCGNAME(256), name);
if (ret < 0) if (ret < 0)
continue; continue;
if (strstr(name, name_needle)) if (strstr(name, name_needle))
return fd; return fd;
close(fd); close(fd);
} }
errno = ENOENT; errno = ENOENT;
return -1; return -1;
} }
void usage() { void usage() {
fprintf(stderr, "Usage: sxmo_vibratepine duration_ms\n"); fprintf(stderr, "Usage: sxmo_vibratepine duration_ms\n");
fprintf(stderr, " sxmo_vibratepine duration_ms strength_number\n"); fprintf(stderr, " sxmo_vibratepine duration_ms strength_number\n");
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int fd, ret; int fd, ret;
struct pollfd pfds[1]; struct pollfd pfds[1];
int effects; int effects;
int durationMs, strength; int durationMs, strength;
if (argc < 2) { if (argc < 2) {
usage(); usage();
return 1; return 1;
} }
argc--; argc--;
if (argc > 1) { if (argc > 1) {
strength = atoi(argv[argc--]); strength = atoi(argv[argc--]);
} else { } else {
strength = 4000; strength = 4000;
} }
durationMs = atoi(argv[argc--]); durationMs = atoi(argv[argc--]);
fd = open_event_dev("vibrator", O_RDWR | O_CLOEXEC); fd = open_event_dev("vibrator", O_RDWR | O_CLOEXEC);
syscall_error(fd < 0, "Can't open vibrator event device"); syscall_error(fd < 0, "Can't open vibrator event device");
ret = ioctl(fd, EVIOCGEFFECTS, &effects); ret = ioctl(fd, EVIOCGEFFECTS, &effects);
syscall_error(ret < 0, "EVIOCGEFFECTS failed"); syscall_error(ret < 0, "EVIOCGEFFECTS failed");
struct ff_effect e = { struct ff_effect e = {
.type = FF_RUMBLE, .type = FF_RUMBLE,
.id = -1, .id = -1,
.u.rumble = { .strong_magnitude = strength }, .u.rumble = { .strong_magnitude = strength },
}; };
ret = ioctl(fd, EVIOCSFF, &e); ret = ioctl(fd, EVIOCSFF, &e);
syscall_error(ret < 0, "EVIOCSFF failed"); syscall_error(ret < 0, "EVIOCSFF failed");
struct input_event play = { .type = EV_FF, .code = e.id, .value = 3 }; struct input_event play = { .type = EV_FF, .code = e.id, .value = 3 };
ret = write(fd, &play, sizeof play); ret = write(fd, &play, sizeof play);
syscall_error(ret < 0, "write failed"); syscall_error(ret < 0, "write failed");
usleep(durationMs * 1000); usleep(durationMs * 1000);
ret = ioctl(fd, EVIOCRMFF, e.id); ret = ioctl(fd, EVIOCRMFF, e.id);
syscall_error(ret < 0, "EVIOCRMFF failed"); syscall_error(ret < 0, "EVIOCRMFF failed");
close(fd); close(fd);
return 0; return 0;
} }

Loading…
Cancel
Save