blob: 317aa47f4cc983f71e5c1e9c24ebbf5c64fe952d [file] [log] [blame]
Jesse Barnes731cd552008-12-17 10:09:49 -08001/*
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 */
10
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 */
35
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>
43
44#include "xf86drmMode.h"
45#include "xf86drm.h"
46#include <drm.h>
47#include <string.h>
48#include <dirent.h>
Kristian Høgsbergb0b96632009-09-11 13:27:35 -040049#include <unistd.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080050#include <errno.h>
51
52#define U642VOID(x) ((void *)(unsigned long)(x))
53#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
54
55/*
56 * Util functions
57 */
58
59void* drmAllocCpy(void *array, int count, int entry_size)
60{
61 char *r;
62 int i;
63
64 if (!count || !array || !entry_size)
65 return 0;
66
67 if (!(r = drmMalloc(count*entry_size)))
68 return 0;
69
70 for (i = 0; i < count; i++)
71 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
72
73 return r;
74}
75
76/*
77 * A couple of free functions.
78 */
79
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +010080void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -080081{
82 if (!ptr)
83 return;
84
85 drmFree(ptr);
86}
87
88void drmModeFreeResources(drmModeResPtr ptr)
89{
90 if (!ptr)
91 return;
92
93 drmFree(ptr);
94
95}
96
97void drmModeFreeFB(drmModeFBPtr ptr)
98{
99 if (!ptr)
100 return;
101
102 /* we might add more frees later. */
103 drmFree(ptr);
104}
105
106void drmModeFreeCrtc(drmModeCrtcPtr ptr)
107{
108 if (!ptr)
109 return;
110
111 drmFree(ptr);
112
113}
114
115void drmModeFreeConnector(drmModeConnectorPtr ptr)
116{
117 if (!ptr)
118 return;
119
Keith Packard0a246542009-09-17 17:28:08 -0700120 drmFree(ptr->encoders);
121 drmFree(ptr->prop_values);
122 drmFree(ptr->props);
Jesse Barnes731cd552008-12-17 10:09:49 -0800123 drmFree(ptr->modes);
124 drmFree(ptr);
125
126}
127
128void drmModeFreeEncoder(drmModeEncoderPtr ptr)
129{
130 drmFree(ptr);
131}
132
133/*
134 * ModeSetting functions.
135 */
136
137drmModeResPtr drmModeGetResources(int fd)
138{
139 struct drm_mode_card_res res;
140 drmModeResPtr r = 0;
141
142 memset(&res, 0, sizeof(struct drm_mode_card_res));
143
144 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
145 return 0;
146
147 if (res.count_fbs)
148 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
149 if (res.count_crtcs)
150 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
151 if (res.count_connectors)
152 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
153 if (res.count_encoders)
154 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
155
156 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
157 r = NULL;
158 goto err_allocs;
159 }
160
161 /*
162 * return
163 */
164
165
166 if (!(r = drmMalloc(sizeof(*r))))
167 return 0;
168
169 r->min_width = res.min_width;
170 r->max_width = res.max_width;
171 r->min_height = res.min_height;
172 r->max_height = res.max_height;
173 r->count_fbs = res.count_fbs;
174 r->count_crtcs = res.count_crtcs;
175 r->count_connectors = res.count_connectors;
176 r->count_encoders = res.count_encoders;
177 /* TODO we realy should test if these allocs fails. */
178 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
179 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
180 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
181 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
182
183err_allocs:
184 drmFree(U642VOID(res.fb_id_ptr));
185 drmFree(U642VOID(res.crtc_id_ptr));
186 drmFree(U642VOID(res.connector_id_ptr));
187 drmFree(U642VOID(res.encoder_id_ptr));
188
189 return r;
190}
191
192int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
193 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
194 uint32_t *buf_id)
195{
196 struct drm_mode_fb_cmd f;
197 int ret;
198
199 f.width = width;
200 f.height = height;
201 f.pitch = pitch;
202 f.bpp = bpp;
203 f.depth = depth;
204 f.handle = bo_handle;
205
206 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
207 return ret;
208
209 *buf_id = f.fb_id;
210 return 0;
211}
212
213int drmModeRmFB(int fd, uint32_t bufferId)
214{
215 return drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
216
217
218}
219
220drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
221{
222 struct drm_mode_fb_cmd info;
223 drmModeFBPtr r;
224
225 info.fb_id = buf;
226
227 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
228 return NULL;
229
230 if (!(r = drmMalloc(sizeof(*r))))
231 return NULL;
232
233 r->fb_id = info.fb_id;
234 r->width = info.width;
235 r->height = info.height;
236 r->pitch = info.pitch;
237 r->bpp = info.bpp;
238 r->handle = info.handle;
239 r->depth = info.depth;
240
241 return r;
242}
243
244
245/*
246 * Crtc functions
247 */
248
249drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
250{
251 struct drm_mode_crtc crtc;
252 drmModeCrtcPtr r;
253
254 crtc.crtc_id = crtcId;
255
256 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
257 return 0;
258
259 /*
260 * return
261 */
262
263 if (!(r = drmMalloc(sizeof(*r))))
264 return 0;
265
266 r->crtc_id = crtc.crtc_id;
267 r->x = crtc.x;
268 r->y = crtc.y;
269 r->mode_valid = crtc.mode_valid;
270 if (r->mode_valid)
271 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
272 r->buffer_id = crtc.fb_id;
273 r->gamma_size = crtc.gamma_size;
274 return r;
275}
276
277
278int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
279 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100280 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800281{
282 struct drm_mode_crtc crtc;
283
284 crtc.x = x;
285 crtc.y = y;
286 crtc.crtc_id = crtcId;
287 crtc.fb_id = bufferId;
288 crtc.set_connectors_ptr = VOID2U64(connectors);
289 crtc.count_connectors = count;
290 if (mode) {
291 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
292 crtc.mode_valid = 1;
293 } else
294 crtc.mode_valid = 0;
295
296 return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
297}
298
299/*
300 * Cursor manipulation
301 */
302
303int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
304{
305 struct drm_mode_cursor arg;
306
307 arg.flags = DRM_MODE_CURSOR_BO;
308 arg.crtc_id = crtcId;
309 arg.width = width;
310 arg.height = height;
311 arg.handle = bo_handle;
312
313 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
314}
315
316int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
317{
318 struct drm_mode_cursor arg;
319
320 arg.flags = DRM_MODE_CURSOR_MOVE;
321 arg.crtc_id = crtcId;
322 arg.x = x;
323 arg.y = y;
324
325 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
326}
327
328/*
329 * Encoder get
330 */
331drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
332{
333 struct drm_mode_get_encoder enc;
334 drmModeEncoderPtr r = NULL;
335
336 enc.encoder_id = encoder_id;
337 enc.encoder_type = 0;
338 enc.possible_crtcs = 0;
339 enc.possible_clones = 0;
340
341 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
342 return 0;
343
344 if (!(r = drmMalloc(sizeof(*r))))
345 return 0;
346
347 r->encoder_id = enc.encoder_id;
348 r->crtc_id = enc.crtc_id;
349 r->encoder_type = enc.encoder_type;
350 r->possible_crtcs = enc.possible_crtcs;
351 r->possible_clones = enc.possible_clones;
352
353 return r;
354}
355
356/*
357 * Connector manipulation
358 */
359
360drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
361{
362 struct drm_mode_get_connector conn;
363 drmModeConnectorPtr r = NULL;
364
365 conn.connector_id = connector_id;
366 conn.connector_type_id = 0;
367 conn.connector_type = 0;
Jesse Barnes3637dcc2009-04-10 15:24:10 -0700368 conn.count_modes = 0;
369 conn.modes_ptr = 0;
370 conn.count_props = 0;
371 conn.props_ptr = 0;
372 conn.prop_values_ptr = 0;
373 conn.count_encoders = 0;
374 conn.encoders_ptr = 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800375
376 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
377 return 0;
378
379 if (conn.count_props) {
380 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
381 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
382 }
383
384 if (conn.count_modes)
385 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
386
387 if (conn.count_encoders)
388 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
389
390 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
391 goto err_allocs;
392
393 if(!(r = drmMalloc(sizeof(*r)))) {
394 goto err_allocs;
395 }
396
397 r->connector_id = conn.connector_id;
398 r->encoder_id = conn.encoder_id;
399 r->connection = conn.connection;
400 r->mmWidth = conn.mm_width;
401 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000402 /* convert subpixel from kernel to userspace */
403 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800404 r->count_modes = conn.count_modes;
405 /* TODO we should test if these alloc & cpy fails. */
406 r->count_props = conn.count_props;
407 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
408 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
409 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
410 r->count_encoders = conn.count_encoders;
411 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
412 r->connector_type = conn.connector_type;
413 r->connector_type_id = conn.connector_type_id;
414
415 if (!r->props || !r->prop_values || !r->modes || !r->encoders)
416 goto err_allocs;
417
418err_allocs:
419 drmFree(U642VOID(conn.prop_values_ptr));
420 drmFree(U642VOID(conn.props_ptr));
421 drmFree(U642VOID(conn.modes_ptr));
422 drmFree(U642VOID(conn.encoders_ptr));
423
424 return r;
425}
426
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100427int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800428{
429 struct drm_mode_mode_cmd res;
430
431 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
432 res.connector_id = connector_id;
433
434 return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
435}
436
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100437int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800438{
439 struct drm_mode_mode_cmd res;
440
441 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
442 res.connector_id = connector_id;
443
444 return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
445}
446
447
448drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
449{
450 struct drm_mode_get_property prop;
451 drmModePropertyPtr r;
452
453 prop.prop_id = property_id;
454 prop.count_enum_blobs = 0;
455 prop.count_values = 0;
456 prop.flags = 0;
457 prop.enum_blob_ptr = 0;
458 prop.values_ptr = 0;
459
460 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
461 return 0;
462
463 if (prop.count_values)
464 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
465
466 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
467 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
468
469 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
470 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
471 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
472 }
473
474 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
475 r = NULL;
476 goto err_allocs;
477 }
478
479 if (!(r = drmMalloc(sizeof(*r))))
480 return NULL;
481
482 r->prop_id = prop.prop_id;
483 r->count_values = prop.count_values;
484
485 r->flags = prop.flags;
486 if (prop.count_values)
487 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
488 if (prop.flags & DRM_MODE_PROP_ENUM) {
489 r->count_enums = prop.count_enum_blobs;
490 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
491 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
492 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
493 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
494 r->count_blobs = prop.count_enum_blobs;
495 }
496 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
497 r->name[DRM_PROP_NAME_LEN-1] = 0;
498
499err_allocs:
500 drmFree(U642VOID(prop.values_ptr));
501 drmFree(U642VOID(prop.enum_blob_ptr));
502
503 return r;
504}
505
506void drmModeFreeProperty(drmModePropertyPtr ptr)
507{
508 if (!ptr)
509 return;
510
511 drmFree(ptr->values);
512 drmFree(ptr->enums);
513 drmFree(ptr);
514}
515
516drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
517{
518 struct drm_mode_get_blob blob;
519 drmModePropertyBlobPtr r;
520
521 blob.length = 0;
522 blob.data = 0;
523 blob.blob_id = blob_id;
524
525 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
526 return NULL;
527
528 if (blob.length)
529 blob.data = VOID2U64(drmMalloc(blob.length));
530
531 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
532 r = NULL;
533 goto err_allocs;
534 }
535
536 if (!(r = drmMalloc(sizeof(*r))))
537 return NULL;
538
539 r->id = blob.blob_id;
540 r->length = blob.length;
541 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
542
543err_allocs:
544 drmFree(U642VOID(blob.data));
545 return r;
546}
547
548void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
549{
550 if (!ptr)
551 return;
552
553 drmFree(ptr->data);
554 drmFree(ptr);
555}
556
557int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
558 uint64_t value)
559{
560 struct drm_mode_connector_set_property osp;
561 int ret;
562
563 osp.connector_id = connector_id;
564 osp.prop_id = property_id;
565 osp.value = value;
566
567 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
568 return ret;
569
570 return 0;
571}
572
573/*
574 * checks if a modesetting capable driver has attached to the pci id
575 * returns 0 if modesetting supported.
576 * -EINVAL or invalid bus id
577 * -ENOSYS if no modesetting support
578*/
579int drmCheckModesettingSupported(const char *busid)
580{
581#ifdef __linux__
582 char pci_dev_dir[1024];
583 int domain, bus, dev, func;
584 DIR *sysdir;
585 struct dirent *dent;
586 int found = 0, ret;
587
588 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
589 if (ret != 4)
590 return -EINVAL;
591
592 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
593 domain, bus, dev, func);
594
595 sysdir = opendir(pci_dev_dir);
596 if (sysdir) {
597 dent = readdir(sysdir);
598 while (dent) {
599 if (!strncmp(dent->d_name, "controlD", 8)) {
600 found = 1;
601 break;
602 }
603
604 dent = readdir(sysdir);
605 }
606 closedir(sysdir);
607 if (found)
608 return 0;
609 }
610
611 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
612 domain, bus, dev, func);
613
614 sysdir = opendir(pci_dev_dir);
615 if (!sysdir)
616 return -EINVAL;
617
618 dent = readdir(sysdir);
619 while (dent) {
620 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
621 found = 1;
622 break;
623 }
624
625 dent = readdir(sysdir);
626 }
627
628 closedir(sysdir);
629 if (found)
630 return 0;
631#endif
632 return -ENOSYS;
633
634}
635
Jesse Barnes731cd552008-12-17 10:09:49 -0800636int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
637 uint16_t *red, uint16_t *green, uint16_t *blue)
638{
639 int ret;
640 struct drm_mode_crtc_lut l;
641
642 l.crtc_id = crtc_id;
643 l.gamma_size = size;
644 l.red = VOID2U64(red);
645 l.green = VOID2U64(green);
646 l.blue = VOID2U64(blue);
647
648 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
649 return ret;
650
651 return 0;
652}
653
654int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
655 uint16_t *red, uint16_t *green, uint16_t *blue)
656{
657 int ret;
658 struct drm_mode_crtc_lut l;
659
660 l.crtc_id = crtc_id;
661 l.gamma_size = size;
662 l.red = VOID2U64(red);
663 l.green = VOID2U64(green);
664 l.blue = VOID2U64(blue);
665
666 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
667 return ret;
668
669 return 0;
670}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400671
672int drmHandleEvent(int fd, drmEventContextPtr evctx)
673{
674 char buffer[1024];
675 int len, i;
676 struct drm_event *e;
677 struct drm_event_vblank *vblank;
678
679 /* The DRM read semantics guarantees that we always get only
680 * complete events. */
681
682 len = read(fd, buffer, sizeof buffer);
683 if (len == 0)
684 return 0;
685 if (len < sizeof *e)
686 return -1;
687
688 i = 0;
689 while (i < len) {
690 e = (struct drm_event *) &buffer[i];
691 switch (e->type) {
692 case DRM_EVENT_VBLANK:
693 if (evctx->version < 1 ||
694 evctx->vblank_handler == NULL)
695 break;
696 vblank = (struct drm_event_vblank *) e;
697 evctx->vblank_handler(fd,
698 vblank->sequence,
699 vblank->tv_sec,
700 vblank->tv_usec,
701 U642VOID (vblank->user_data));
702 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500703 case DRM_EVENT_FLIP_COMPLETE:
Jesse Barnes14f59582009-12-03 14:20:51 -0800704 if (evctx->version < 2 ||
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500705 evctx->page_flip_handler == NULL)
706 break;
707 vblank = (struct drm_event_vblank *) e;
708 evctx->page_flip_handler(fd,
709 vblank->sequence,
710 vblank->tv_sec,
711 vblank->tv_usec,
712 U642VOID (vblank->user_data));
713 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400714 default:
715 break;
716 }
717 i += e->length;
718 }
719
720 return 0;
721}
722
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500723int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
724 uint32_t flags, void *user_data)
725{
726 struct drm_mode_crtc_page_flip flip;
727
728 flip.fb_id = fb_id;
729 flip.crtc_id = crtc_id;
730 flip.user_data = VOID2U64(user_data);
731 flip.flags = flags;
732 flip.reserved = 0;
733
734 return drmIoctl(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
735}