aboutsummaryrefslogtreecommitdiffstats
path: root/src/gammad.c
blob: f5179bdbe64c8793dd9f6dbfd04379c1c5ef0cf3 (plain) (blame)
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
90
/**
 * gammad -- Cooperative gamma server
 * Copyright (C) 2016  Mattias Andrée (maandree@kth.se)
 * 
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <libgamma.h>

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



char* argv0;



int main(int argc, char** argv)
{
  int method, gerror, rc = 1;
  libgamma_site_state_t site;
  libgamma_partition_state_t* partitions = NULL;
  libgamma_crtc_state_t* crtcs = NULL;
  size_t i, j, n, n0, crtcs_n = 0;
  
  argv0 = argv[0];
  
  memset(&site, 0, sizeof(site));
  
  /* Get method */
  if (libgamma_list_methods(&method, 1, 0) < 1)
    return fprintf(stderr, "%s: no adjustment method available\n", argv0), 1;
  
  /* Get site */
  if ((gerror = libgamma_site_initialise(&site, method, NULL)))
    goto fail_libgamma;
  
  /* Get partitions */
  if (site.partitions_available)
    if (!(partitions = calloc(site.partitions_available, sizeof(*partitions))))
      goto fail;
  for (i = 0; i < site.partitions_available; i++)
    {
      if ((gerror = libgamma_partition_initialise(partitions + i, &site, i)))
	goto fail_libgamma;
      crtcs_n += partitions[i].crtcs_available;
    }
  
  /* Get CRTC:s */
  if (crtcs_n)
    if (!(crtcs = calloc(crtcs_n, sizeof(*crtcs))))
      goto fail;
  for (i = 0, j = n = 0; i < site.partitions_available; i++)
    for (n0 = n, n += partitions[i].crtcs_available; j < n; j++)
      if ((gerror = libgamma_crtc_initialise(crtcs + j, partitions + i, j - n0)))
	goto fail_libgamma;
  
  /* Done */
  rc = 0;
 done:
  if (crtcs != NULL)
    for (i = 0; i < crtcs_n; i++)
      libgamma_crtc_destroy(crtcs + i);
  free(crtcs);
  if (partitions != NULL)
    for (i = 0; i < site.partitions_available; i++)
      libgamma_partition_destroy(partitions + i);
  free(partitions);
  libgamma_site_destroy(&site);
  return rc;
  /* Fail */
 fail:
  perror(argv0);
  goto done;
 fail_libgamma:
  libgamma_perror(argv0, gerror);
  goto done;
}