| 1 | #include "resolution.h" |
| 2 | #include "common.h" |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | const char *usingxrandr() { |
| 10 | return run_command_s("xrandr --nograb --current | " |
| 11 | "awk -F 'connected |\\\\+|\\\\(' '/ " |
| 12 | "connected.*[0-9]+x[0-9]+\\+/ && $2 {printf $2 " |
| 13 | "\", \"}' | sed 's/primary //' | " |
| 14 | "sed 's/,\\([^,]*\\)$/\\1/'"); |
| 15 | } |
| 16 | |
| 17 | const char *usingxwininfo() { |
| 18 | return run_command_s("xwininfo -root | awk '/Width/ {width=$2} /Height/ " |
| 19 | "{height=$2} END {printf \"%dx%d\\n\", width, height}'"); |
| 20 | } |
| 21 | |
| 22 | const char *usingxdpyinfo() { |
| 23 | return run_command_s("xdpyinfo | awk '/dimensions:/ {printf $2}'"); |
| 24 | } |
| 25 | |
| 26 | const char *usingdrm() { |
| 27 | return run_command_s("for dev in /sys/class/drm/card*-*/status; do " |
| 28 | "if grep -q \"connected\" \"$dev\"; then " |
| 29 | "dir=$(dirname \"$dev\"); " |
| 30 | "modefile=\"$dir/modes\"; " |
| 31 | "dis=$(echo \"$dir\" | sed -e 's/^.*drm\\///'); " |
| 32 | "if [ -f \"$modefile\" ]; then " |
| 33 | "mode=$(head -n 1 \"$modefile\"); " |
| 34 | "if [ -n \"$mode\" ]; then " |
| 35 | "echo \"$mode, \"; " |
| 36 | "fi " |
| 37 | "fi " |
| 38 | "fi " |
| 39 | "done | sed '$s/,//'"); |
| 40 | } |
| 41 | |
| 42 | const char *display_resolution() { |
| 43 | #if defined(__HAIKU__) |
| 44 | return run_command_s("screenmode | sed 's/Resolution: //' | sed 's/,.*$//' | " |
| 45 | "sed 's/ /x/'"); |
| 46 | #elif defined(__APPLE__) |
| 47 | return run_command_s("system_profiler SPDisplaysDataType | grep Resolution | " |
| 48 | "sed 's/^.*: //' | sed 's/ Retina//' | sed 's/ //g'"); |
| 49 | #else |
| 50 | if (access("/sys/class/drm", F_OK) == 0) { |
| 51 | return usingdrm(); |
| 52 | } |
| 53 | |
| 54 | const char *display = run_command_s("echo $DISPLAY"); |
| 55 | if (display == NULL || strlen(display) == 0) return NULL; |
| 56 | else free((void *)display); |
| 57 | |
| 58 | const char *isxrandr = run_command_s("which xrandr"); |
| 59 | if ( |
| 60 | isxrandr != NULL && |
| 61 | strlen(isxrandr) != 0 && |
| 62 | strncmp(isxrandr, "xrandr not found", strlen("xrandr not found")) != 0 && |
| 63 | !strstr(isxrandr, "which: no xrandr in") |
| 64 | ) { |
| 65 | free((void *)isxrandr); |
| 66 | return usingxrandr(); |
| 67 | } |
| 68 | free((void *)isxrandr); |
| 69 | |
| 70 | const char *isxwininfo = run_command_s("which xwininfo"); |
| 71 | if ( |
| 72 | isxwininfo != NULL && |
| 73 | strlen(isxwininfo) != 0 && |
| 74 | strncmp(isxwininfo, "xwininfo not found", strlen("xwininfo not found")) != 0 && |
| 75 | !strstr(isxwininfo, "which: no xwininfo in") |
| 76 | ) { |
| 77 | free((void *)isxwininfo); |
| 78 | return usingxwininfo(); |
| 79 | } |
| 80 | free((void *)isxwininfo); |
| 81 | |
| 82 | const char *isxdpyinfo = run_command_s("which xdpyinfo"); |
| 83 | if ( |
| 84 | isxdpyinfo != NULL && |
| 85 | strlen(isxdpyinfo) != 0 && |
| 86 | strncmp(isxdpyinfo, "xdpyinfo not found", strlen("xdpyinfo not found")) != 0 && |
| 87 | !strstr(isxdpyinfo, "which: no xdpyinfo in") |
| 88 | ) { |
| 89 | free((void *)isxdpyinfo); |
| 90 | return usingxdpyinfo(); |
| 91 | } |
| 92 | free((void *)isxdpyinfo); |
| 93 | |
| 94 | return NULL; |
| 95 | #endif |
| 96 | } |