54 lines
1.4 KiB
C
54 lines
1.4 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|||
|
|
#include <fcntl.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include <sys/socket.h>
|
||
|
|
#include <sys/un.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
#define SOCK_PATH "/run/geminux/powerd.sock"
|
||
|
|
|
||
|
|
int main(int argc, char **argv) {
|
||
|
|
if (argc < 2) {
|
||
|
|
fprintf(stderr, "Usage: %s <status|suspend|profile eco|balanced|performance|inhibit add|del TAG|lid-suspend on|off>\n",
|
||
|
|
argv[0]);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
char cmd[256] = {0};
|
||
|
|
for (int i = 1; i < argc; i++) {
|
||
|
|
if (i > 1 && strlen(cmd) + 1 < sizeof(cmd)) strcat(cmd, " ");
|
||
|
|
strncat(cmd, argv[i], sizeof(cmd) - strlen(cmd) - 2);
|
||
|
|
}
|
||
|
|
|
||
|
|
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||
|
|
if (fd >= 0) {
|
||
|
|
struct sockaddr_un addr;
|
||
|
|
memset(&addr, 0, sizeof(addr));
|
||
|
|
addr.sun_family = AF_UNIX;
|
||
|
|
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", SOCK_PATH);
|
||
|
|
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
|
||
|
|
dprintf(fd, "%s\n", cmd);
|
||
|
|
shutdown(fd, SHUT_WR);
|
||
|
|
char resp[512];
|
||
|
|
ssize_t n;
|
||
|
|
while ((n = read(fd, resp, sizeof(resp))) > 0) {
|
||
|
|
fwrite(resp, 1, (size_t)n, stdout);
|
||
|
|
}
|
||
|
|
close(fd);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
close(fd);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (strcmp(cmd, "suspend") == 0) {
|
||
|
|
int pfd = open("/sys/power/state", O_WRONLY);
|
||
|
|
if (pfd >= 0) {
|
||
|
|
ssize_t w = write(pfd, "mem", 3);
|
||
|
|
close(pfd);
|
||
|
|
return (w == 3) ? 0 : 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
fprintf(stderr, "geminux-power-ctl: cannot connect to %s\n", SOCK_PATH);
|
||
|
|
return 1;
|
||
|
|
}
|