summaryrefslogtreecommitdiff
path: root/src/resolution.c
blob: 249d6c68bffb9ed1633841424f4c30fc282e1400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "resolution.h"
#include "common.h"

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const char *display_resolution() {
  const char *isexist = run_command_s("which xrandr");
  if (
      isexist == NULL ||
      strlen(isexist) == 0 ||
      strncmp(isexist, "xrandr not found", strlen("xrandr not found")) == 0
  ) {
    if (isexist) free((void *)isexist);
    return NULL;
  }
  free((void *)isexist);

  const char *display = run_command_s("echo $DISPLAY");
  if (display == NULL || strlen(display) == 0) return NULL;

  return run_command_s("xrandr --nograb --current | "
                       "awk -F 'connected |\\\\+|\\\\(' '/ "
                       "connected.*[0-9]+x[0-9]+\\+/ && $2 {printf $2 "
                       "\", \"}' | sed 's/primary //' | "
                       "sed 's/,\\([^,]*\\)$/\\1/'");
}