From 171b28947349399d5590210635e57957c80f2876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Wed, 19 Jun 2024 15:44:06 +0900 Subject: =?UTF-8?q?GPU=E6=83=85=E5=A0=B1=E3=81=AF=E6=96=87=E5=AD=97?= =?UTF-8?q?=E5=8C=96=EF=BC=88glxinfo=E3=82=92=E5=AE=9F=E5=8A=B9=E5=87=BA?= =?UTF-8?q?=E6=9D=A5=E3=81=AA=E3=81=91=E3=82=8C=E3=81=B0=E3=80=81=E3=82=B9?= =?UTF-8?q?=E3=82=AD=E3=83=83=E3=83=97=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 10 +++++----- src/gpu.c | 37 ++++++++++++++++++++++++++++++++----- src/gpu.h | 2 +- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 389acde..e565f9d 100644 --- a/main.c +++ b/main.c @@ -143,11 +143,11 @@ int main(int argc, char *argv[]) { printf("\n"); lc++; - printf("%s ", LOGO[lc]); - printf(COLOR"%s%s"RESET, "GPU", ": "); - display_gpu(); - printf("\n"); - lc++; + if (display_gpu()) { + printf("%s ", LOGO[lc]); + printf(COLOR"%s%s%s\n"RESET, "GPU", ": ", display_gpu()); + lc++; + } printf("%s ", LOGO[lc]); printf(COLOR"%s%s"RESET, "Memory", ": "); diff --git a/src/gpu.c b/src/gpu.c index 168fe23..dbcd259 100644 --- a/src/gpu.c +++ b/src/gpu.c @@ -3,26 +3,53 @@ #include #include #include +#include -void run_gpu_command(const char *command) { +const char *run_gpu_command(const char *command) { + if ( + access("/bin/glxinfo", F_OK) == -1 || + access("/usr/bin/glxinfo", F_OK) == -1 || + access("/usr/local/bin/glxinfo", F_OK) == -1 || + access("/usr/X11R6/bin/glxinfo", F_OK) == -1 || + access("/usr/X11R7/bin/glxinfo", F_OK) == -1 || + access("/usr/pkg/bin/glxinfo", F_OK) == -1 + ) return NULL; char buf[128]; + char *out = NULL; + size_t outsize = 0; FILE *p = popen(command, "r"); if (!p) { fprintf(stderr, "GPUコマンドを実効に失敗: %s", command); - return; + return NULL; } while (fgets(buf, sizeof(buf), p) != NULL) { buf[strcspn(buf, "\n")] = '\0'; - printf("%s", buf); + + 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; } -void display_gpu() { - run_gpu_command("glxinfo -B | grep -F 'OpenGL renderer string' | " +const char *display_gpu() { + return run_gpu_command("glxinfo -B | grep -F 'OpenGL renderer string' | " "sed 's/OpenGL renderer string: //' | sed 's/Mesa //' | " "sed 's/DRI //' | sed 's/(R)//' | sed 's/(.*$//'"); } diff --git a/src/gpu.h b/src/gpu.h index 0db6e79..0d4a787 100644 --- a/src/gpu.h +++ b/src/gpu.h @@ -1,6 +1,6 @@ #ifndef GPU_H #define GPU_H -void display_gpu(); +const char *display_gpu(); #endif -- cgit v1.2.3