1 /*
2 * \file xf86drmMode.c
3 * Header for DRM modesetting interface.
4 *
5 * \author Jakob Bornecrantz <wallbraker@gmail.com>
6 *
7 * \par Acknowledgements:
8 * Feb 2007, Dave Airlie <airlied@linux.ie>
9 */
11 /*
12 * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
13 * Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie>
14 * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 *
34 */
36 /*
37 * TODO the types we are after are defined in diffrent headers on diffrent
38 * platforms find which headers to include to get uint32_t
39 */
40 #include <stdint.h>
41 #include <sys/ioctl.h>
42 #include <stdio.h>
44 #include "xf86drmMode.h"
45 #include "xf86drm.h"
46 #include <drm.h>
47 #include <string.h>
48 #include <dirent.h>
49 #include <unistd.h>
50 #include <errno.h>
52 #define U642VOID(x) ((void *)(unsigned long)(x))
53 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
55 /*
56 * Util functions
57 */
59 void* drmAllocCpy(void *array, int count, int entry_size)
60 {
61 char *r;
62 int i;
64 if (!count || !array || !entry_size)
65 return 0;
67 if (!(r = drmMalloc(count*entry_size)))
68 return 0;
70 for (i = 0; i < count; i++)
71 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
73 return r;
74 }
76 /*
77 * A couple of free functions.
78 */
80 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
81 {
82 if (!ptr)
83 return;
85 drmFree(ptr);
86 }
88 void drmModeFreeResources(drmModeResPtr ptr)
89 {
90 if (!ptr)
91 return;
93 drmFree(ptr);
95 }
97 void drmModeFreeFB(drmModeFBPtr ptr)
98 {
99 if (!ptr)
100 return;
102 /* we might add more frees later. */
103 drmFree(ptr);
104 }
106 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
107 {
108 if (!ptr)
109 return;
111 drmFree(ptr);
113 }
115 void drmModeFreeConnector(drmModeConnectorPtr ptr)
116 {
117 if (!ptr)
118 return;
120 drmFree(ptr->encoders);
121 drmFree(ptr->prop_values);
122 drmFree(ptr->props);
123 drmFree(ptr->modes);
124 drmFree(ptr);
126 }
128 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
129 {
130 drmFree(ptr);
131 }
133 /*
134 * ModeSetting functions.
135 */
137 drmModeResPtr drmModeGetResources(int fd)
138 {
139 struct drm_mode_card_res res, counts;
140 drmModeResPtr r = 0;
142 retry:
143 memset(&res, 0, sizeof(struct drm_mode_card_res));
144 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
145 return 0;
147 counts = res;
149 if (res.count_fbs) {
150 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
151 if (!res.fb_id_ptr)
152 goto err_allocs;
153 }
154 if (res.count_crtcs) {
155 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
156 if (!res.crtc_id_ptr)
157 goto err_allocs;
158 }
159 if (res.count_connectors) {
160 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
161 if (!res.connector_id_ptr)
162 goto err_allocs;
163 }
164 if (res.count_encoders) {
165 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
166 if (!res.encoder_id_ptr)
167 goto err_allocs;
168 }
170 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
171 goto err_allocs;
173 /* The number of available connectors and etc may have changed with a
174 * hotplug event in between the ioctls, in which case the field is
175 * silently ignored by the kernel.
176 */
177 if (counts.count_fbs < res.count_fbs ||
178 counts.count_crtcs < res.count_crtcs ||
179 counts.count_connectors < res.count_connectors ||
180 counts.count_encoders < res.count_encoders)
181 {
182 drmFree(U642VOID(res.fb_id_ptr));
183 drmFree(U642VOID(res.crtc_id_ptr));
184 drmFree(U642VOID(res.connector_id_ptr));
185 drmFree(U642VOID(res.encoder_id_ptr));
187 goto retry;
188 }
190 /*
191 * return
192 */
193 if (!(r = drmMalloc(sizeof(*r))))
194 goto err_allocs;
196 r->min_width = res.min_width;
197 r->max_width = res.max_width;
198 r->min_height = res.min_height;
199 r->max_height = res.max_height;
200 r->count_fbs = res.count_fbs;
201 r->count_crtcs = res.count_crtcs;
202 r->count_connectors = res.count_connectors;
203 r->count_encoders = res.count_encoders;
205 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
206 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
207 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
208 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
209 if ((res.count_fbs && !r->fbs) ||
210 (res.count_crtcs && !r->crtcs) ||
211 (res.count_connectors && !r->connectors) ||
212 (res.count_encoders && !r->encoders))
213 {
214 drmFree(r->fbs);
215 drmFree(r->crtcs);
216 drmFree(r->connectors);
217 drmFree(r->encoders);
218 drmFree(r);
219 r = 0;
220 }
222 err_allocs:
223 drmFree(U642VOID(res.fb_id_ptr));
224 drmFree(U642VOID(res.crtc_id_ptr));
225 drmFree(U642VOID(res.connector_id_ptr));
226 drmFree(U642VOID(res.encoder_id_ptr));
228 return r;
229 }
231 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
232 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
233 uint32_t *buf_id)
234 {
235 struct drm_mode_fb_cmd f;
236 int ret;
238 f.width = width;
239 f.height = height;
240 f.pitch = pitch;
241 f.bpp = bpp;
242 f.depth = depth;
243 f.handle = bo_handle;
245 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
246 return ret;
248 *buf_id = f.fb_id;
249 return 0;
250 }
252 int drmModeRmFB(int fd, uint32_t bufferId)
253 {
254 return drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
257 }
259 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
260 {
261 struct drm_mode_fb_cmd info;
262 drmModeFBPtr r;
264 info.fb_id = buf;
266 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
267 return NULL;
269 if (!(r = drmMalloc(sizeof(*r))))
270 return NULL;
272 r->fb_id = info.fb_id;
273 r->width = info.width;
274 r->height = info.height;
275 r->pitch = info.pitch;
276 r->bpp = info.bpp;
277 r->handle = info.handle;
278 r->depth = info.depth;
280 return r;
281 }
283 int drmModeDirtyFB(int fd, uint32_t bufferId,
284 drmModeClipPtr clips, uint32_t num_clips)
285 {
286 struct drm_mode_fb_dirty_cmd dirty = { 0 };
288 dirty.fb_id = bufferId;
289 dirty.clips_ptr = VOID2U64(clips);
290 dirty.num_clips = num_clips;
292 return drmIoctl(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
293 }
296 /*
297 * Crtc functions
298 */
300 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
301 {
302 struct drm_mode_crtc crtc;
303 drmModeCrtcPtr r;
305 crtc.crtc_id = crtcId;
307 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
308 return 0;
310 /*
311 * return
312 */
314 if (!(r = drmMalloc(sizeof(*r))))
315 return 0;
317 r->crtc_id = crtc.crtc_id;
318 r->x = crtc.x;
319 r->y = crtc.y;
320 r->mode_valid = crtc.mode_valid;
321 if (r->mode_valid)
322 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
323 r->buffer_id = crtc.fb_id;
324 r->gamma_size = crtc.gamma_size;
325 return r;
326 }
329 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
330 uint32_t x, uint32_t y, uint32_t *connectors, int count,
331 drmModeModeInfoPtr mode)
332 {
333 struct drm_mode_crtc crtc;
335 crtc.x = x;
336 crtc.y = y;
337 crtc.crtc_id = crtcId;
338 crtc.fb_id = bufferId;
339 crtc.set_connectors_ptr = VOID2U64(connectors);
340 crtc.count_connectors = count;
341 if (mode) {
342 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
343 crtc.mode_valid = 1;
344 } else
345 crtc.mode_valid = 0;
347 return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
348 }
350 /*
351 * Cursor manipulation
352 */
354 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
355 {
356 struct drm_mode_cursor arg;
358 arg.flags = DRM_MODE_CURSOR_BO;
359 arg.crtc_id = crtcId;
360 arg.width = width;
361 arg.height = height;
362 arg.handle = bo_handle;
364 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
365 }
367 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
368 {
369 struct drm_mode_cursor arg;
371 arg.flags = DRM_MODE_CURSOR_MOVE;
372 arg.crtc_id = crtcId;
373 arg.x = x;
374 arg.y = y;
376 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
377 }
379 /*
380 * Encoder get
381 */
382 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
383 {
384 struct drm_mode_get_encoder enc;
385 drmModeEncoderPtr r = NULL;
387 enc.encoder_id = encoder_id;
388 enc.encoder_type = 0;
389 enc.possible_crtcs = 0;
390 enc.possible_clones = 0;
392 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
393 return 0;
395 if (!(r = drmMalloc(sizeof(*r))))
396 return 0;
398 r->encoder_id = enc.encoder_id;
399 r->crtc_id = enc.crtc_id;
400 r->encoder_type = enc.encoder_type;
401 r->possible_crtcs = enc.possible_crtcs;
402 r->possible_clones = enc.possible_clones;
404 return r;
405 }
407 /*
408 * Connector manipulation
409 */
411 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
412 {
413 struct drm_mode_get_connector conn, counts;
414 drmModeConnectorPtr r = NULL;
416 retry:
417 memset(&conn, 0, sizeof(struct drm_mode_get_connector));
418 conn.connector_id = connector_id;
420 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
421 return 0;
423 counts = conn;
425 if (conn.count_props) {
426 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
427 if (!conn.props_ptr)
428 goto err_allocs;
429 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
430 if (!conn.prop_values_ptr)
431 goto err_allocs;
432 }
434 if (conn.count_modes) {
435 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
436 if (!conn.modes_ptr)
437 goto err_allocs;
438 }
440 if (conn.count_encoders) {
441 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
442 if (!conn.encoders_ptr)
443 goto err_allocs;
444 }
446 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
447 goto err_allocs;
449 /* The number of available connectors and etc may have changed with a
450 * hotplug event in between the ioctls, in which case the field is
451 * silently ignored by the kernel.
452 */
453 if (counts.count_props < conn.count_props ||
454 counts.count_modes < conn.count_modes ||
455 counts.count_encoders < conn.count_encoders) {
456 drmFree(U642VOID(conn.props_ptr));
457 drmFree(U642VOID(conn.prop_values_ptr));
458 drmFree(U642VOID(conn.modes_ptr));
459 drmFree(U642VOID(conn.encoders_ptr));
461 goto retry;
462 }
464 if(!(r = drmMalloc(sizeof(*r)))) {
465 goto err_allocs;
466 }
468 r->connector_id = conn.connector_id;
469 r->encoder_id = conn.encoder_id;
470 r->connection = conn.connection;
471 r->mmWidth = conn.mm_width;
472 r->mmHeight = conn.mm_height;
473 /* convert subpixel from kernel to userspace */
474 r->subpixel = conn.subpixel + 1;
475 r->count_modes = conn.count_modes;
476 r->count_props = conn.count_props;
477 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
478 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
479 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
480 r->count_encoders = conn.count_encoders;
481 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
482 r->connector_type = conn.connector_type;
483 r->connector_type_id = conn.connector_type_id;
485 if ((r->count_props && !r->props) ||
486 (r->count_props && !r->prop_values) ||
487 (r->count_modes && !r->modes) ||
488 (r->count_encoders && !r->encoders)) {
489 drmFree(r->props);
490 drmFree(r->prop_values);
491 drmFree(r->modes);
492 drmFree(r->encoders);
493 drmFree(r);
494 r = 0;
495 }
497 err_allocs:
498 drmFree(U642VOID(conn.prop_values_ptr));
499 drmFree(U642VOID(conn.props_ptr));
500 drmFree(U642VOID(conn.modes_ptr));
501 drmFree(U642VOID(conn.encoders_ptr));
503 return r;
504 }
506 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
507 {
508 struct drm_mode_mode_cmd res;
510 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
511 res.connector_id = connector_id;
513 return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
514 }
516 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
517 {
518 struct drm_mode_mode_cmd res;
520 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
521 res.connector_id = connector_id;
523 return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
524 }
527 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
528 {
529 struct drm_mode_get_property prop;
530 drmModePropertyPtr r;
532 prop.prop_id = property_id;
533 prop.count_enum_blobs = 0;
534 prop.count_values = 0;
535 prop.flags = 0;
536 prop.enum_blob_ptr = 0;
537 prop.values_ptr = 0;
539 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
540 return 0;
542 if (prop.count_values)
543 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
545 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
546 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
548 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
549 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
550 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
551 }
553 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
554 r = NULL;
555 goto err_allocs;
556 }
558 if (!(r = drmMalloc(sizeof(*r))))
559 return NULL;
561 r->prop_id = prop.prop_id;
562 r->count_values = prop.count_values;
564 r->flags = prop.flags;
565 if (prop.count_values)
566 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
567 if (prop.flags & DRM_MODE_PROP_ENUM) {
568 r->count_enums = prop.count_enum_blobs;
569 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
570 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
571 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
572 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
573 r->count_blobs = prop.count_enum_blobs;
574 }
575 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
576 r->name[DRM_PROP_NAME_LEN-1] = 0;
578 err_allocs:
579 drmFree(U642VOID(prop.values_ptr));
580 drmFree(U642VOID(prop.enum_blob_ptr));
582 return r;
583 }
585 void drmModeFreeProperty(drmModePropertyPtr ptr)
586 {
587 if (!ptr)
588 return;
590 drmFree(ptr->values);
591 drmFree(ptr->enums);
592 drmFree(ptr);
593 }
595 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
596 {
597 struct drm_mode_get_blob blob;
598 drmModePropertyBlobPtr r;
600 blob.length = 0;
601 blob.data = 0;
602 blob.blob_id = blob_id;
604 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
605 return NULL;
607 if (blob.length)
608 blob.data = VOID2U64(drmMalloc(blob.length));
610 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
611 r = NULL;
612 goto err_allocs;
613 }
615 if (!(r = drmMalloc(sizeof(*r))))
616 return NULL;
618 r->id = blob.blob_id;
619 r->length = blob.length;
620 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
622 err_allocs:
623 drmFree(U642VOID(blob.data));
624 return r;
625 }
627 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
628 {
629 if (!ptr)
630 return;
632 drmFree(ptr->data);
633 drmFree(ptr);
634 }
636 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
637 uint64_t value)
638 {
639 struct drm_mode_connector_set_property osp;
640 int ret;
642 osp.connector_id = connector_id;
643 osp.prop_id = property_id;
644 osp.value = value;
646 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
647 return ret;
649 return 0;
650 }
652 /*
653 * checks if a modesetting capable driver has attached to the pci id
654 * returns 0 if modesetting supported.
655 * -EINVAL or invalid bus id
656 * -ENOSYS if no modesetting support
657 */
658 int drmCheckModesettingSupported(const char *busid)
659 {
660 #ifdef __linux__
661 char pci_dev_dir[1024];
662 int domain, bus, dev, func;
663 DIR *sysdir;
664 struct dirent *dent;
665 int found = 0, ret;
667 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
668 if (ret != 4)
669 return -EINVAL;
671 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
672 domain, bus, dev, func);
674 sysdir = opendir(pci_dev_dir);
675 if (sysdir) {
676 dent = readdir(sysdir);
677 while (dent) {
678 if (!strncmp(dent->d_name, "controlD", 8)) {
679 found = 1;
680 break;
681 }
683 dent = readdir(sysdir);
684 }
685 closedir(sysdir);
686 if (found)
687 return 0;
688 }
690 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
691 domain, bus, dev, func);
693 sysdir = opendir(pci_dev_dir);
694 if (!sysdir)
695 return -EINVAL;
697 dent = readdir(sysdir);
698 while (dent) {
699 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
700 found = 1;
701 break;
702 }
704 dent = readdir(sysdir);
705 }
707 closedir(sysdir);
708 if (found)
709 return 0;
710 #endif
711 return -ENOSYS;
713 }
715 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
716 uint16_t *red, uint16_t *green, uint16_t *blue)
717 {
718 int ret;
719 struct drm_mode_crtc_lut l;
721 l.crtc_id = crtc_id;
722 l.gamma_size = size;
723 l.red = VOID2U64(red);
724 l.green = VOID2U64(green);
725 l.blue = VOID2U64(blue);
727 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
728 return ret;
730 return 0;
731 }
733 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
734 uint16_t *red, uint16_t *green, uint16_t *blue)
735 {
736 int ret;
737 struct drm_mode_crtc_lut l;
739 l.crtc_id = crtc_id;
740 l.gamma_size = size;
741 l.red = VOID2U64(red);
742 l.green = VOID2U64(green);
743 l.blue = VOID2U64(blue);
745 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
746 return ret;
748 return 0;
749 }
751 int drmHandleEvent(int fd, drmEventContextPtr evctx)
752 {
753 char buffer[1024];
754 int len, i;
755 struct drm_event *e;
756 struct drm_event_vblank *vblank;
758 /* The DRM read semantics guarantees that we always get only
759 * complete events. */
761 len = read(fd, buffer, sizeof buffer);
762 if (len == 0)
763 return 0;
764 if (len < sizeof *e)
765 return -1;
767 i = 0;
768 while (i < len) {
769 e = (struct drm_event *) &buffer[i];
770 switch (e->type) {
771 case DRM_EVENT_VBLANK:
772 if (evctx->version < 1 ||
773 evctx->vblank_handler == NULL)
774 break;
775 vblank = (struct drm_event_vblank *) e;
776 evctx->vblank_handler(fd,
777 vblank->sequence,
778 vblank->tv_sec,
779 vblank->tv_usec,
780 U642VOID (vblank->user_data));
781 break;
782 case DRM_EVENT_FLIP_COMPLETE:
783 if (evctx->version < 2 ||
784 evctx->page_flip_handler == NULL)
785 break;
786 vblank = (struct drm_event_vblank *) e;
787 evctx->page_flip_handler(fd,
788 vblank->sequence,
789 vblank->tv_sec,
790 vblank->tv_usec,
791 U642VOID (vblank->user_data));
792 break;
793 default:
794 break;
795 }
796 i += e->length;
797 }
799 return 0;
800 }
802 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
803 uint32_t flags, void *user_data)
804 {
805 struct drm_mode_crtc_page_flip flip;
807 flip.fb_id = fb_id;
808 flip.crtc_id = crtc_id;
809 flip.user_data = VOID2U64(user_data);
810 flip.flags = flags;
811 flip.reserved = 0;
813 return drmIoctl(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
814 }