| 1 | #include "gpu.h" |
| 2 | #include "common.h" |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | |
| 6 | #if !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__FreeBSD__) &&\ |
| 7 | !defined(__linux__) && !defined(__DragonFly__) && !defined(__APPLE__) &&\ |
| 8 | !defined(__HAIKU__) |
| 9 | #include <unistd.h> |
| 10 | #endif |
| 11 | |
| 12 | #if defined(__FreeBSD__) || defined(__Dragonfly__) || defined(__HAIKU__) |
| 13 | #include <string.h> |
| 14 | #endif |
| 15 | |
| 16 | const char *display_gpu() { |
| 17 | #if !defined(__HAIKU__) |
| 18 | const char *out = from_cache("/tmp/farfetch/gpu"); |
| 19 | if (out) return out; |
| 20 | #else |
| 21 | const char *out = NULL; |
| 22 | #endif |
| 23 | char *cmd = NULL; |
| 24 | |
| 25 | #if defined(__OpenBSD__) || defined(__NetBSD__) |
| 26 | cmd = malloc(2048); |
| 27 | if (!cmd) return NULL; |
| 28 | snprintf(cmd, 2048, "dmesg | " |
| 29 | "if [ \"$(dmesg | grep \"radeondrm.* at pci.*\")\" ]; " |
| 30 | "then grep -i \"radeondrm.* at pci.*\"; " |
| 31 | "elif [ \"$(dmesg | grep \"inteldrm.* at pci.*\")\" ]; " |
| 32 | "then grep -i \"inteldrm.* at pci.*\"; " |
| 33 | "elif [ \"$(dmesg | grep \"amdgpu.* at pci.*\")\" ]; " |
| 34 | "then grep -i \"amdgpu.* at pci.*\"; " |
| 35 | "elif [ \"$(dmesg | grep \"agp.* at intagp.*\")\" ]; " |
| 36 | "then grep -i \"agp.* at intagp.*\"; " |
| 37 | "elif [ \"$(dmesg | grep \"intagp.* at inteldrm.*\")\" ]; " |
| 38 | "then grep -i \"intagp.* at inteldrm.*\"; " |
| 39 | "elif [ \"$(dmesg | grep \"drm.* at inteldrm.*\")\" ]; " |
| 40 | "then grep -i \"drm.* at inteldrm.*\"; " |
| 41 | "elif [ \"$(dmesg | grep \"drm.* at radeondrm.*\")\" ]; " |
| 42 | "then grep -i \"drm.* at radeondrm.*\"; " |
| 43 | "elif [ \"$(dmesg | grep \"drm.* at amdgpu.*\")\" ]; " |
| 44 | "then grep -i \"drm.* at amdgpu.*\"; " |
| 45 | "elif [ \"$(dmesg | grep \"wsdisplay.* at amdgpu.*\")\" ]; " |
| 46 | "then grep -i \"wsdisplay.* at amdgpu.*\"; " |
| 47 | "elif [ \"$(dmesg | grep \"i915drmkms.* at pci.*\")\" ]; " |
| 48 | "then grep -i \"i915drmkms.* at pci.*\"; " |
| 49 | "elif [ \"$(dmesg | grep \"nouveau.* at pci.*\")\" ]; " |
| 50 | "then grep -i \"nouveau.* at pci.*\"; " |
| 51 | "elif [ \"$(dmesg | grep \"radeon.* at pci.*\")\" ]; " |
| 52 | "then grep -i \"radeon.* at pci.*\"; " |
| 53 | "elif [ \"$(dmesg | grep \"rkdrm.* at fdt.*\")\" ]; " |
| 54 | "then grep -i \"rkdrm.* at fdt.*\"; " |
| 55 | "elif [ \"$(dmesg | grep \"sunxidrm.* at fdt.*\")\" ]; " |
| 56 | "then grep -i \"sunxidrm.* at fdt.*\"; " |
| 57 | "elif [ \"$(dmesg | grep \"tegradrm.* at fdt.*\")\" ]; " |
| 58 | "then grep -i \"tegradrm.* at fdt.*\"; " |
| 59 | "elif [ \"$(dmesg | grep \"viadrmums.* at drm.*\")\" ]; " |
| 60 | "then grep -i \"viadrmums.* at drm.*\"; " |
| 61 | "else grep -i \"graphics\"; fi | " |
| 62 | "sed 's/^.*: //' | " |
| 63 | "sed 's/^.* \"//' | " |
| 64 | "sed 's/\".*$//' | head -1"); |
| 65 | #elif defined(__FreeBSD__) || defined(__DragonFly__) |
| 66 | const char *test = run_command_s("pciconf -lv | grep -B 4 -F \"VGA\" | head -1"); |
| 67 | if (!strstr(test, "vgapci")) { |
| 68 | free((void *)test); |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | free((void *)test); |
| 73 | |
| 74 | cmd = malloc(256); |
| 75 | if (!cmd) return NULL; |
| 76 | snprintf(cmd, 256, "pciconf -lv | grep -B 4 -F \"VGA\" | " |
| 77 | "grep -F \"device\" | sed 's/^.* device//' | " |
| 78 | "sed \"s/^.* '//\" | sed \"s/'//\" | tail -1 | " |
| 79 | "sed 's/ Core Processor Integrated Graphics Controller//'"); |
| 80 | #elif defined(__sun) |
| 81 | cmd = malloc(512); |
| 82 | if (!cmd) return NULL; |
| 83 | snprintf(cmd, 512, "prtconf -v | grep -A 30 \"value='display'\" | " |
| 84 | "grep -A 1 vendor-name | tail -1 | sed 's/^.*value=//' | sed \"s/'//g\" | " |
| 85 | "sed 's/ Corporation//' && echo \" \" && prtconf -v | " |
| 86 | "grep -A 30 \"value='display'\" | grep -A 1 device-name | tail -1 | " |
| 87 | "sed 's/^.*value=//' | sed \"s/'//g\""); |
| 88 | #elif defined(__linux__) |
| 89 | cmd = malloc(256); |
| 90 | if (!cmd) return NULL; |
| 91 | snprintf(cmd, 256, "lspci | grep VGA | sed 's/^.*: //' | " |
| 92 | "sed 's/Corporation //' | sed 's/ (.*$//' | " |
| 93 | "sed 's/Advanced Micro Devices//' | " |
| 94 | "sed 's/, Inc. //' | sed 's/Navi [0-9]* //' | " |
| 95 | "sed 's/\\[//g' | sed 's/\\]//g'"); |
| 96 | #elif defined(__APPLE__) |
| 97 | cmd = malloc(128); |
| 98 | if (!cmd) return NULL; |
| 99 | snprintf(cmd, 128, "system_profiler SPDisplaysDataType | " |
| 100 | "awk -F': ' '/^ *Chipset Model:/ {printf $2 \", \"}'"); |
| 101 | #elif defined(__HAIKU__) |
| 102 | const char *vendor = run_command_s("listdev | grep -A1 \"device Display\" | " |
| 103 | "tail -1 | sed 's/^.*: //' | sed 's/ Corporation//'"); |
| 104 | const char *device = run_command_s("listdev | grep -A2 \"device Display\" | " |
| 105 | "tail -1 | sed 's/^.*: //'"); |
| 106 | |
| 107 | char *cmd = (char *)malloc(128 * sizeof(char)); |
| 108 | if (!cmd) return NULL; |
| 109 | |
| 110 | if (strncmp(vendor, device, strlen(device)) == 0) { |
| 111 | snprintf(cmd, 128, "%s", device); |
| 112 | free((void *)device); |
| 113 | } else { |
| 114 | snprintf(cmd, 128, "%s %s", device, vendor); |
| 115 | free((void *)device); |
| 116 | free((void *)vendor); |
| 117 | } |
| 118 | #else |
| 119 | if ( |
| 120 | access("/bin/glxinfo", F_OK) == -1 && |
| 121 | access("/usr/bin/glxinfo", F_OK) == -1 && |
| 122 | access("/usr/local/bin/glxinfo", F_OK) == -1 && |
| 123 | access("/usr/X11R6/bin/glxinfo", F_OK) == -1 && |
| 124 | access("/usr/X11R7/bin/glxinfo", F_OK) == -1 && |
| 125 | access("/usr/pkg/bin/glxinfo", F_OK) == -1 |
| 126 | ) return NULL; |
| 127 | |
| 128 | cmd = malloc(256); |
| 129 | if (!cmd) return NULL; |
| 130 | snprintf(cmd, 256, "glxinfo -B | grep -F 'OpenGL renderer string' | " |
| 131 | "sed 's/OpenGL renderer string: //' | sed 's/Mesa //' | " |
| 132 | "sed 's/DRI //' | sed 's/(R)//' | sed 's/(.*$//'"); |
| 133 | #endif |
| 134 | |
| 135 | if (!cmd) return NULL; |
| 136 | out = run_command_s(cmd); |
| 137 | free((void *)cmd); |
| 138 | #if !defined(__HAIKU__) |
| 139 | if (out) to_cache("/tmp/farfetch/gpu", out); |
| 140 | #endif |
| 141 | |
| 142 | return out; |
| 143 | } |