aboutsummaryrefslogtreecommitdiffstats
path: root/drmgamma.c
blob: 43adb2ee7b72b308fc4cd55cbdab19d415864632 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* See LICENSE file for copyright and license details. */
#include "common.h"


/**
 * The number of elements to allocates to a buffer for a DRM device pathname
 */
#define DRM_DEV_NAME_MAX_LEN\
  ((sizeof(DRM_DEV_NAME) + sizeof(DRM_DIR_NAME)) / sizeof(char) + 3 * sizeof(int))



/**
 * Figure out how many graphics cards there are on the system
 * 
 * @return  The number of graphics cards on the system
 */
size_t
drm_card_count(void)
{
	char buf[DRM_DEV_NAME_MAX_LEN];
	size_t count = 0;
	for (;; count++) {
		sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, (int)count);
		if (access(buf, F_OK) < 0)
			return count;
	}
}

/**
 * Acquire access to a graphics card
 * 
 * @param   index  The index of the graphics card
 * @param   card   Graphics card information to fill in
 * @return         Zero on success, -1 on error
 */
int
drm_card_open(size_t index, drm_card_t *restrict card)
{
	char buf[DRM_DEV_NAME_MAX_LEN];
	int old_errno;
	size_t i, n;

	card->fd = -1;
	card->res = NULL;
	card->connectors = NULL;
	card->encoders = NULL;
	card->connector_count = 0;

	sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, (int)index);
	card->fd = open(buf, O_RDWR);
	if (card->fd < 0)
		goto fail;

	card->res = drmModeGetResources(card->fd);
	if (!card->res)
		goto fail;

	card->crtc_count      = (size_t)(card->res->count_crtcs);
	card->connector_count = (size_t)(card->res->count_connectors);
	n = card->connector_count;

	card->connectors = calloc(n, sizeof(drmModeConnector*));
	if (!card->connectors)
		goto fail;
	card->encoders   = calloc(n, sizeof(drmModeEncoder*));
	if (!card->encoders)
		goto fail;

	for (i = 0; i < n; i++) {
		card->connectors[i] = drmModeGetConnector(card->fd, card->res->connectors[i]);
		if (!card->connectors[i])
			goto fail;

		if (card->connectors[i]->encoder_id) {
			card->encoders[i] = drmModeGetEncoder(card->fd, card->connectors[i]->encoder_id);
			if (!card->encoders[i])
				goto fail;
		}
	}

	return 0;
fail:
	old_errno = errno;
	drm_card_close(card);
	errno = old_errno;
	return -1;
}


/**
 * Release access to a graphics card
 * 
 * @param  card  The graphics card information
 */
void
drm_card_close(drm_card_t *restrict card)
{
	size_t i, n = card->connector_count;

	if (card->encoders) {
		for (i = 0; i < n; i++)
			if (card->encoders[i])
				drmModeFreeEncoder(card->encoders[i]);
		free(card->encoders);
		card->encoders = NULL;
	}

	if (card->connectors) {
		for (i = 0; i < n; i++)
			if (card->connectors[i])
				drmModeFreeConnector(card->connectors[i]);
		free(card->connectors);
		card->connectors = NULL;
	}

	if (card->res) {
		drmModeFreeResources(card->res);
		card->res = NULL;
	}
	if (card->fd >= 0) {
		close(card->fd);
		card->fd = -1;
	}
}


/**
 * Acquire access to a CRT controller
 * 
 * @param   index  The index of the CRT controller
 * @param   card   The graphics card information
 * @param   crtc   CRT controller information to fill in
 * @return         Zero on success, -1 on error
 */
int
drm_crtc_open(size_t index, drm_card_t *restrict card, drm_crtc_t *restrict crtc)
{
	drmModePropertyRes *restrict prop;
	drmModePropertyBlobRes *restrict blob;
	drmModeCrtc *restrict info;
	size_t i, j;
	int old_errno;
	unsigned char c;

	crtc->edid  = NULL;
	crtc->red   = NULL;
	crtc->green = NULL;
	crtc->blue  = NULL;

	crtc->id = card->res->crtcs[index];
	crtc->card = card;

	crtc->connector = NULL;
	crtc->encoder = NULL;
	for (i = 0; i < card->connector_count; i++) {
		if (card->encoders[i]) {
			if (card->encoders[i]->crtc_id == crtc->id) {
				crtc->connector = card->connectors[i];
				crtc->encoder = card->encoders[i];
			}
		}
	}

	crtc->connected = crtc->connector && crtc->connector->connection == DRM_MODE_CONNECTED;

	info = drmModeGetCrtc(card->fd, crtc->id);
	if (!info)
		return -1;
	crtc->gamma_stops = (size_t)info->gamma_size;
	drmModeFreeCrtc(info);

	/* `calloc` is for some reason required when reading the gamma ramps. */
	crtc->red = calloc(3 * crtc->gamma_stops, sizeof(uint16_t));
	if (!crtc->red)
		return -1;
	crtc->green = crtc->red   + crtc->gamma_stops;
	crtc->blue  = crtc->green + crtc->gamma_stops;

	if (!crtc->connector)
		return 0;
	for (i = 0; i < (size_t)crtc->connector->count_props; i++) {
		prop = drmModeGetProperty(card->fd, crtc->connector->props[i]);
		if (!prop)
			continue;

		if (strcmp(prop->name, "EDID"))
			goto free_prop;

		blob = drmModeGetPropertyBlob(card->fd, (uint32_t)crtc->connector->prop_values[i]);
		i = (size_t)crtc->connector->count_props;
		if (!blob || !blob->data)
			goto free_blob;

		crtc->edid = malloc((blob->length * 2 + 1) * sizeof(char));
		if (!crtc->edid) {
			old_errno = errno;
			drmModeFreePropertyBlob(blob);
			drmModeFreeProperty(prop);
			free(crtc->red);
			crtc->red = NULL;
			errno = old_errno;
			return -1;
		}
		for (j = 0; j < blob->length; j++) {
			c = ((unsigned char *)blob->data)[j];
			crtc->edid[j * 2 + 0] = "0123456789ABCDEF"[(c >> 4) & 15];
			crtc->edid[j * 2 + 1] = "0123456789ABCDEF"[(c >> 0) & 15];
		}
		crtc->edid[blob->length * 2] = '\0';

	free_blob:
		if (blob)
			drmModeFreePropertyBlob(blob);

	free_prop:
		drmModeFreeProperty(prop);
	}

	return 0;
}


/**
 * Release access to a CRT controller
 * 
 * @param  crtc  The CRT controller information to fill in
 */
void
drm_crtc_close(drm_crtc_t *restrict crtc)
{
	free(crtc->edid);
	free(crtc->red);
	crtc->edid = NULL;
	crtc->red = NULL;
}


/**
 * Read the gamma ramps for a CRT controller
 * 
 * @param   crtc  CRT controller information
 * @return        Zero on success, -1 on error 
 */
int
drm_get_gamma(drm_crtc_t *restrict crtc)
{
	return -!!drmModeCrtcGetGamma(crtc->card->fd, crtc->id, (uint32_t)crtc->gamma_stops, crtc->red, crtc->green, crtc->blue);
}


/**
 * Apply gamma ramps for a CRT controller
 * 
 * @param   crtc  CRT controller information
 * @return        Zero on success, -1 on error
 */
int
drm_set_gamma(drm_crtc_t *restrict crtc)
{
	return -!!drmModeCrtcSetGamma(crtc->card->fd, crtc->id, (uint32_t)crtc->gamma_stops, crtc->red, crtc->green, crtc->blue);
}