aboutsummaryrefslogtreecommitdiffstats
path: root/state.c
blob: 4e7b00daa3cb4f2b02fb2fcd5430188b4d6874ef (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* See LICENSE file for copyright and license details. */
#include "common.h"


/**
 * The framebuffers on the system
 */
framebuffer_t *restrict framebuffers = NULL;

/**
 * The number of elements in `framebuffers`
 */
size_t framebuffer_count = 0;

/**
 * The graphics cards on the system
 */
drm_card_t *restrict cards = NULL;

/**
 * The number of elements in `cards`
 */
size_t card_count = 0;

/**
 * The connected CRT controllers on the system
 */
drm_crtc_t *restrict crtcs = NULL;

/**
 * The software brightness setting on each connected CRT controller, on each channel
 */
double *restrict brightnesses[3];

/**
 * The software contrast setting on each connected CRT controller, on each channel
 */
double *restrict contrasts[3];

/**
 * The gamma correction on each connected CRT controller, on each channel
 */
double *restrict gammas[3];

/**
 * The number of elements in `crtcs`, `brightnesses[]`, `contrasts[]` and `gammas[]`
 */
size_t crtc_count = 0;



/**
 * Acquire video control
 * 
 * @return  Zero on success, -1 on error
 */
int
acquire_video(void)
{
	size_t f, c, i, fn = fb_count(), cn = drm_card_count();
	drm_crtc_t *restrict old_crtcs, crtc;
	drm_card_t card;
	framebuffer_t fb;

	framebuffers = malloc(fn * sizeof(framebuffer_t));
	if (!framebuffers)
		return -1;

	for (f = 0; f < fn; f++) {
		if (fb_open(f, &fb) < 0)
			return -1;
		framebuffers[framebuffer_count++] = fb;
	}

	cards = malloc(cn * sizeof(drm_card_t));
	if (!cards)
		return -1;

	for (c = 0; c < cn; c++) {
		if (drm_card_open(c, &card) < 0)
			return -1;
		cards[card_count++] = card;

		old_crtcs = crtcs;
		crtcs = realloc(crtcs, (crtc_count + card.crtc_count) * sizeof(drm_crtc_t));
		if (!crtcs) {
			crtcs = old_crtcs;
			return -1;
		}

		for (i = 0; i < card.crtc_count; i++) {
			if (drm_crtc_open(i, cards + c, &crtc) < 0)
				return -1;
			if (crtc.connected)
				crtcs[crtc_count++] = crtc;
			else
				drm_crtc_close(&crtc);
		}
	}

	for (c = 0; c < 3; c++) {
		brightnesses[c] = malloc(crtc_count * sizeof(double));
		if (!brightnesses[c])
			return -1;
		contrasts[c] = malloc(crtc_count * sizeof(double));
		if (!contrasts[c])
			return -1;
		gammas[c] = malloc(crtc_count * sizeof(double));
		if (!gammas[c])
			return -1;
	}

	return 0;
}


/**
 * Release video control
 */
void
release_video(void)
{
	size_t i;
	while (crtc_count)
		drm_crtc_close(&crtcs[--crtc_count]);
	while (card_count)
		drm_card_close(&cards[--card_count]);
	while (framebuffer_count)
		fb_close(&framebuffers[--framebuffer_count]);
	for (i = 0; i < 3; i++) {
		free(brightnesses[i]);
		free(contrasts[i]);
		free(gammas[i]);
		brightnesses[i] = NULL;
		contrasts[i] = NULL;
		gammas[i] = NULL;
	}
	free(crtcs);
	free(cards);
	free(framebuffers);
	crtcs = NULL;
	cards = NULL;
	framebuffers = NULL;
}