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

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

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

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

Loading…
Cancel
Save