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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/**
* Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
*
* This program 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 program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <alloca.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 02000000
#endif
/* Requires video group */
#include <xf86drm.h>
#include <xf86drmMode.h>
/**
* File descriptor for the DRM connection
*/
static int drm_fd;
/**
* DRM mode resources
*/
static drmModeRes* drm_res = NULL;
/**
* Connector information
*/
static drmModeConnector* connector = NULL;
/**
* Get the number of cards present on the system
*
* @return The number of cards present on the system
*/
int blueshift_drm_card_count()
{
long maxlen = strlen(DRM_DIR_NAME) + strlen(DRM_DEV_NAME) + 10;
char* pathname = alloca(maxlen * sizeof(char));
int count = 0;
struct stat attr;
for (;;)
{
sprintf(pathname, DRM_DEV_NAME, DRM_DIR_NAME, count);
if (stat(pathname, &attr))
break;
count++;
}
return count;
}
/**
* Open connection to a graphics card
*
* @param card The index of the graphics card
* @return Zero on success
*/
int blueshift_drm_open(int card)
{
long maxlen = strlen(DRM_DIR_NAME) + strlen(DRM_DEV_NAME) + 10;
char* pathname = alloca(maxlen * sizeof(char));
sprintf(pathname, DRM_DEV_NAME, DRM_DIR_NAME, card);
drm_fd = open(pathname, O_RDWR | O_CLOEXEC);
if (drm_fd < 0)
{
perror("open");
return 1;
}
return 0;
}
/**
* Close connection to the graphics card
*/
void blueshift_drm_close()
{
drmModeFreeResources(drm_res);
close(drm_fd);
}
/**
* Update the resource, required after `blueshift_drm_open`
*/
void blueshift_drm_update()
{
if (drm_res)
drmModeFreeResources(drm_res);
drm_res = drmModeGetResources(drm_fd);
}
/**
* Return the number of CRTC:s on the opened card
*
* @return The number of CRTC:s on the opened card
*/
int blueshift_drm_crtc_count()
{
return drm_res->count_crtcs;
}
/**
* Return the number of connectors on the opened card
*
* @return The number of connectors on the opened card
*/
int blueshift_drm_connector_count()
{
return drm_res->count_connectors;
}
/**
* Return the size of the gamma ramps on a CRTC
*
* @param crtc_index The index of the CRTC
* @return The size of the gamma ramps on a CRTC
*/
int blueshift_drm_gamma_size(int crtc_index)
{
drmModeCrtc* crtc = drmModeGetCrtc(drm_fd, *(drm_res->crtcs + crtc_index));
int gamma_size;
gamma_size = crtc->gamma_size;
drmModeFreeCrtc(crtc);
return gamma_size;
}
/**
* Acquire information about a connector
*
* @param connector_index The index of the connector
*/
void blueshift_drm_open_connector(int connector_index)
{
connector = drmModeGetConnector(drm_fd, *(drm_res->connectors + connector_index));
}
/**
* Release information about the connector
*/
void blueshift_drm_close_connector()
{
drmModeFreeConnector(connector);
}
/* Accurate dimension on area not covered by the edges */
/**
* Get the physical width the monitor connected to the connector
*
* @return The physical width of the monitor in millimetres, 0 if unknown or not connected
*/
int blueshift_drm_get_width()
{
return connector->mmWidth;
}
/**
* Get the physical height the monitor connected to the connector
*
* @return The physical height of the monitor in millimetres, 0 if unknown or not connected
*/
int blueshift_drm_get_height()
{
return connector->mmHeight;
}
/**
* Get whether a monitor is connected to a connector
*
* @return 1 if there is a connection, 0 otherwise, -1 if unknown
*/
int blueshift_drm_is_connected()
{
switch (connector->connection)
{
case DRM_MODE_CONNECTED:
return 1;
case DRM_MODE_DISCONNECTED:
return 0;
case DRM_MODE_UNKNOWNCONNECTION:
default:
return -1;
}
}
/**
* Get the index of the CRTC of the monitor connected to the connector
*
* @return The index of the CRTC
*/
int blueshift_drm_get_crtc()
{
drmModeEncoder* encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
uint32_t crtc_id = encoder->crtc_id;
int crtc;
drmModeFreeEncoder(encoder);
for (crtc = 0; crtc < drm_res->count_crtcs; crtc++)
if (*(drm_res->crtcs + crtc) == crtc_id)
return crtc;
return -1;
}
/**
* Get the index of the type of the connector
*
* @return The connector type by index, 0 for unknown
*/
int blueshift_drm_get_connector_type_index()
{
return connector->connector_type;
}
/**
* Get the name of the type of the connector
*
* @return The connector type by name, "Unknown" if not identifiable,
* "Unrecognised" if Blueshift does not recognise it.
*/
const char* blueshift_drm_get_connector_type_name()
{
static const char* TYPE_NAMES[] = {
"Unknown", "VGA", "DVII", "DVID", "DVIA", "Composite", "SVIDEO", "LVDS", "Component",
"9PinDIN", "DisplayPort", "HDMIA", "HDMIB", "TV", "eDP", "VIRTUAL", "DSI"};
int type = connector->connector_type;
return (size_t)type < sizeof(TYPE_NAMES) / sizeof(char*) ? TYPE_NAMES[type] : "Unrecognised";
}
/**
* Get the current gamma ramps of the
*
* @param crtc_index The index of the CRTC to read from
* @param gamma_size The size a gamma ramp
* @param red Storage location for the red gamma ramp
* @param green Storage location for the green gamma ramp
* @param blue Storage location for the blue gamma ramp
* @return Zero on success
*/
int blueshift_drm_get_gamma_ramps(int crtc_index, int gamma_size, uint16_t* red, uint16_t* green, uint16_t* blue)
{
int i;
/* We need to initialise it to avoid valgrind warnings */
for (i = 0; i < gamma_size; i++)
*(red + i) = *(green + i) = *(blue + i) = 0;
return drmModeCrtcGetGamma(drm_fd, *(drm_res->crtcs + crtc_index), gamma_size, red, green, blue);
}
/**
* Get the extended display identification data for the monitor connected to the connector
*
* @param edid Storage location for the EDID, it should be 128 bytes, 256 bytes + zero termination if hex
* @param size The size allocated to `edid` excluding your zero termination
* @param hex Whether to convert to hexadecimal representation, this is preferable
* @return The length of the found value, 0 if none, as if hex is false
*/
long blueshift_drm_get_edid(char* edid, long size, int hex)
{
long rc = 0;
int prop_i;
for (prop_i = 0; prop_i < connector->count_props; prop_i++)
{
drmModePropertyRes* prop = drmModeGetProperty(drm_fd, connector->props[prop_i]);
if (!strcmp("EDID", prop->name))
{
drmModePropertyBlobRes* blob = drmModeGetPropertyBlob(drm_fd, connector->prop_values[prop_i]);
if (hex)
{
rc += blob->length;
uint32_t n = size / 2;
uint32_t i;
if (n < blob->length)
n = blob->length;
for (i = 0; i < n ; i++)
{
*(edid + i * 2 + 0) = "0123456789abcdef"[(*((char*)(blob->data) + i) >> 4) & 15];
*(edid + i * 2 + 1) = "0123456789abcdef"[(*((char*)(blob->data) + i) >> 0) & 15];
}
}
else
memcpy(edid, blob->data, (blob->length < size ? blob->length : size) * sizeof(char));
drmModeFreePropertyBlob(blob);
prop_i = connector->count_props; /* stop to for loop */
}
drmModeFreeProperty(prop);
}
return rc;
}
int main(int argc, char** argv)
{
(void) argc;
(void) argv;
printf("Card count: %i\n", blueshift_drm_card_count());
blueshift_drm_open(0);
blueshift_drm_update();
blueshift_drm_open_connector(3);
printf("CRTC count: %i\n", blueshift_drm_crtc_count());
printf("Connector count: %i\n", blueshift_drm_connector_count());
printf("Gamma size: %i\n", blueshift_drm_gamma_size(0));
printf("Physical size: %i mm by %i mm\n", blueshift_drm_get_width(), blueshift_drm_get_height());
printf("Connected: %i\n", blueshift_drm_is_connected());
if (blueshift_drm_is_connected() == 1)
{;
int crtc = blueshift_drm_get_crtc();
if (crtc >= 0)
{
int gamma_size = blueshift_drm_gamma_size(crtc);
uint16_t* red = alloca(3 * gamma_size * sizeof(uint16_t));
uint16_t* green = red + gamma_size;
uint16_t* blue = green + gamma_size;
printf("CRTC: %i\n", crtc);
if (!blueshift_drm_get_gamma_ramps(crtc, gamma_size, red, green, blue))
{
int j;
printf("Red:");
for (j = 0; j < gamma_size; j++)
printf(" %u", *(red + j));
printf("\nGreen:");
for (j = 0; j < gamma_size; j++)
printf(" %u", *(green + j));
printf("\nBlue:");
for (j = 0; j < gamma_size; j++)
printf(" %u", *(blue + j));
printf("\n");
for (j = 0; j < gamma_size; j++)
*(red + j) /= 2;
for (j = 0; j < gamma_size; j++)
*(green + j) /= 2;
for (j = 0; j < gamma_size; j++)
*(blue + j) /= 2;
drmModeCrtcSetGamma(drm_fd, *(drm_res->crtcs + crtc), gamma_size, red, green, blue);
/* TODO what more is required to set gamma ramps? */
}
}
{
long size = 128;
char* edid = malloc((size * 2 + 1) * sizeof(char));
long n;
*(edid + size * 2) = 0;
n = blueshift_drm_get_edid(edid, size, 1);
if (n)
{
if (n > size)
{
size = n;
edid = realloc(edid, (size * 2 + 1) * sizeof(char));
blueshift_drm_get_edid(edid, size, 1);
}
*(edid + n * 2) = 0;
printf("EDID: %s\n", edid);
}
free(edid);
}
}
printf("Connector type: %s (%i)\n",
blueshift_drm_get_connector_type_name(),
blueshift_drm_get_connector_type_index());
blueshift_drm_close_connector();
blueshift_drm_close();
return 0;
}
|