summaryrefslogtreecommitdiff
path: root/src/resolution.c
diff options
context:
space:
mode:
author諏訪子 <suwako@076.moe>2024-06-19 22:53:46 +0900
committer諏訪子 <suwako@076.moe>2024-06-19 22:53:46 +0900
commitb2155d447caf2d69db6fea4d4d47e9b4053696c7 (patch)
tree3a1ddf3d716174c5bc405ca9632093ba93cef5b5 /src/resolution.c
parent4605ae223e5746a69940959cff651c491758f515 (diff)
解像度の追加
Diffstat (limited to 'src/resolution.c')
-rw-r--r--src/resolution.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/resolution.c b/src/resolution.c
new file mode 100644
index 0000000..17a6ac3
--- /dev/null
+++ b/src/resolution.c
@@ -0,0 +1,48 @@
+#include "gpu.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+const char *run_res_command(const char *command) {
+ char buf[128];
+ char *out = NULL;
+ size_t outsize = 0;
+
+ FILE *p = popen(command, "r");
+ if (!p) {
+ fprintf(stderr, "解像度コマンドを実効に失敗: %s", command);
+ return NULL;
+ }
+
+ while (fgets(buf, sizeof(buf), p) != NULL) {
+ buf[strcspn(buf, "\n")] = '\0';
+
+ size_t len = strlen(buf);
+ char *nout = realloc(out, outsize + len + 1);
+ if (nout == NULL) {
+ perror("メモリの役割に失敗");
+ free(out);
+ pclose(p);
+ return NULL;
+ }
+
+ out = nout;
+
+ memccpy(out + outsize, buf, sizeof(buf), len);
+ outsize += len;
+ out[outsize] = '\0';
+ }
+
+ pclose(p);
+
+ return out;
+}
+
+const char *display_resolution() {
+ return run_res_command("xrandr --nograb --current | "
+ "awk -F 'connected |\\\\+|\\\\(' '/ "
+ "connected.*[0-9]+x[0-9]+\\+/ && $2 {printf $2 "
+ "\", \"}' | sed 's/primary //' | sed 's/,//'");
+}