From db01f3469c469d9a5faf049441eb1ee0ce36a4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Sun, 27 Aug 2023 14:13:40 +0900 Subject: [PATCH] =?UTF-8?q?get=5Fgpu=E3=82=92=E6=9C=80=E9=81=A9=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.h | 14 +++++++-- paleofetch.c | 83 ++++++++++++++++++++-------------------------------- 2 files changed, 44 insertions(+), 53 deletions(-) diff --git a/config.h b/config.h index 7d1061c..9ec4e0b 100644 --- a/config.h +++ b/config.h @@ -75,15 +75,25 @@ REMOVE("Quad-Core"), \ REMOVE("Six-Core"), \ REMOVE("Eight-Core"), \ - REMOVE("Core"), \ + REMOVE("2-Core"), \ + REMOVE("3-Core"), \ + REMOVE("4-Core"), \ + REMOVE("6-Core"), \ + REMOVE("8-Core"), \ + REMOVE("12-Core"), \ + REMOVE("Processor"), \ REMOVE("CPU"), \ } #define GPU_CONFIG \ { \ - REMOVE("Advanced Micro Devices, Inc. "), \ + REMOVE("Advanced Micro Devices, Inc."), \ + REPLACE(" [AMD/ATI]", "AMD/ATI"), \ REMOVE("Corporation"), \ REMOVE("Controller"), \ REMOVE("Graphics"), \ REMOVE("Integrated"), \ + REMOVE("Navi 14"), \ + REMOVE("["), \ + REMOVE("]"), \ } diff --git a/paleofetch.c b/paleofetch.c index 7bc9946..50cb828 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -412,6 +412,8 @@ static void get_terminal_wayland(char *terminal) { return; } + printf("%s\n", terminal); + wl_display_disconnect(display); } @@ -474,49 +476,29 @@ static char *get_cpu() { } char *cpu_model = malloc(BUF_SIZE / 2); - char *line = NULL; - size_t len; - int num_cores = 0, cpu_freq, prec = 3; + char *line = malloc(BUF_SIZE); + size_t len = 0; + int num_cores = 0, cpu_freq; double freq; char freq_unit[] = "GHz"; + int prec = 3; while (getline(&line, &len, cpuinfo) != -1) { num_cores += sscanf(line, "model name : %[^\n@]", cpu_model); } - free(line); - fclose(cpuinfo); - FILE *cpufreq = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); - line = NULL; + fseek(cpuinfo, 0, SEEK_SET); - if (cpufreq != NULL) { - if (getline(&line, &len, cpufreq) != -1) { - sscanf(line, "%d", &cpu_freq); - cpu_freq /= 1000; - } else { - fclose(cpufreq); - free(line); - goto cpufreq_fallback; + while (getline(&line, &len, cpuinfo) != -1) { + if (sscanf(line, "cpuMHz : %lf", &freq) > 0) { + break; } - } else { -cpufreq_fallback: - cpufreq = fopen("/proc/cpuinfo", "r"); - if (cpufreq == NULL) { - status = -1; - halt_and_catch_fire("Unable to open cpuinfo"); - } - - while (getline(&line, &len, cpufreq) != -1) { - if (sscanf(line, "cpu MHz : %lf", &freq) > 0) { - break; - } - } - - cpu_freq = (int) freq; } + fclose(cpuinfo); free(line); - fclose(cpufreq); + + cpu_freq = (int) freq; if (cpu_freq < 1000) { freq = (double) cpu_freq; @@ -552,53 +534,52 @@ cpufreq_fallback: if (num_cores == 0) { *cpu = '\0'; } + return cpu; } static char *find_gpu(int index) { - char buffer[BUF_SIZE], *device_class, *gpu = malloc(BUF_SIZE); + char buffer[BUF_SIZE], *device_class; struct pci_access *pacc; struct pci_dev *dev; int gpu_index = 0; bool found = false; + char *gpu = malloc(BUF_SIZE); pacc = pci_alloc(); pci_init(pacc); pci_scan_bus(pacc); - dev = pacc->devices; - while (dev != NULL) { + for (dev = pacc->devices; dev != NULL; dev = dev->next) { pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS); device_class = pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_CLASS, dev->device_class); - if (strcmp("VGA compatible controller", device_class) == 0 || strcmp("3D controller", device_class) == 0) { - strncpy(gpu, pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_DEVICE | PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id), BUF_SIZE - 1); + + if (!strcmp("VGA compatible controller", device_class) || !strcmp("3D controller", device_class)) { + snprintf(gpu, BUF_SIZE, "%s", pci_lookup_name(pacc, buffer, sizeof(buffer), PCI_LOOKUP_DEVICE | PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id)); + if (gpu_index == index) { found = true; break; - } else { - gpu_index++; } + gpu_index++; } - - dev = dev->next; - } - - if (found == false) { - *gpu = '\0'; } pci_cleanup(pacc); - for (int i = 0; i < COUNT(gpu_config); ++i) { - if (gpu_config[i].repl_str == NULL) { - remove_substring(gpu, gpu_config[i].substring, gpu_config[i].length); - } else { - replace_substring(gpu, gpu_config[i].substring, gpu_config[i].repl_str, gpu_config[i].length, gpu_config[i].repl_len); + if (!found) { + *gpu = '\0'; + } else { + for (int i = 0; i < COUNT(gpu_config); ++i) { + if (gpu_config[i].repl_str == NULL) { + remove_substring(gpu, gpu_config[i].substring, gpu_config[i].length); + } else { + replace_substring(gpu, gpu_config[i].substring, gpu_config[i].repl_str, gpu_config[i].length, gpu_config[i].repl_len); + } } + truncate_spaces(gpu); } - truncate_spaces(gpu); - return gpu; }