aboutsummaryrefslogtreecommitdiffstats
path: root/monitor-get-edid-using-vbe.c
blob: c6235fd3e75902afee0841a78f82fc4dd7c5147a (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/vt.h>
#include "get-edid.h"

int verbose = 0;

static void timeout_handler(int signal)
{
  fprintf(stderr, "ERROR: timeout during EDID probe\n");
  exit(3);
}

int main(int argc, char **argv)
{
  struct sigaction timeout_action;
  char edid[(1 + MAX_EXTENSION_COUNT) * EDID_BLOCK_SIZE];
  int try_in_console = 0;
  int skip_vbe_check = 0;
  int port = 0;
  int no_timeout = 0;
  int i;

  for (i = 1; i < argc; i++) {
	const char *arg = argv[i];
    if (strcmp(arg, "-v") == 0) verbose = 1;
    else if (strcmp(arg, "--port") == 0 && i+1 < argc) port = atoi(argv[++i]);
    else if (strcmp(arg, "--try-in-console") == 0) try_in_console = 1;
    else if (strcmp(arg, "--skip-vbe-check") == 0) skip_vbe_check = 1;
    else if (strcmp(arg, "--no-timeout") == 0) no_timeout = 1;
    else if (strcmp(arg, "-h") == 0 ||
	     strcmp(arg, "--help") == 0) {
      printf("usage: monitor-get-edid [-v] [--port <0-3>] [--try-in-console]\n"
	     "                        [--skip-vbe-check] [--no-timeout]\n");
      exit(1);
    }
  }

  if (!no_timeout) {
    timeout_action.sa_handler = timeout_handler;
    sigaction(SIGALRM, &timeout_action, NULL);
    alarm(15);
  }

  int size = get_edid(edid, port, skip_vbe_check);

  if (size < 0 && try_in_console) {
    int non_X_console = 1;
    struct vt_stat current;
    int fd = open("/dev/console", O_RDWR);

    if (getenv("DISPLAY") != NULL &&
	ioctl(fd, VT_GETSTATE, &current) == 0 && 
	current.v_active != non_X_console &&	
	ioctl(fd, VT_ACTIVATE, non_X_console) == 0 &&
	ioctl(fd, VT_WAITACTIVE, non_X_console) == 0) {
      /* retrying */
      size = get_edid(edid, port, skip_vbe_check);

      /* restore */
      if (ioctl(fd, VT_ACTIVATE, current.v_active) == 0)
	ioctl(fd, VT_WAITACTIVE, current.v_active);
    }
    close(fd);
  }

  if (size >= 0) {
	 if (write(STDOUT_FILENO, edid, size)) {
     }
	 return 0;
  }

  /* returns 1 on error before any successful calls; no ports work;
   *          calling program may skip other ports
   * returns 2 on other errors; a different port may still work (call to VBE was ok);
   *          calling program may speedup call on other ports with --skip-vbe-check
   * returns 3 on timeout
   */
  return -size;
}