OSDN Git Service

modes: Free local resources after allocation failure in GETRESOURCES
[android-x86/external-libdrm.git] / xf86drmMode.c
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  */
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>
49 #include <unistd.h>
50 #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
59 void* 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
80 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
81 {
82         if (!ptr)
83                 return;
84
85         drmFree(ptr);
86 }
87
88 void drmModeFreeResources(drmModeResPtr ptr)
89 {
90         if (!ptr)
91                 return;
92
93         drmFree(ptr);
94
95 }
96
97 void drmModeFreeFB(drmModeFBPtr ptr)
98 {
99         if (!ptr)
100                 return;
101
102         /* we might add more frees later. */
103         drmFree(ptr);
104 }
105
106 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
107 {
108         if (!ptr)
109                 return;
110
111         drmFree(ptr);
112
113 }
114
115 void drmModeFreeConnector(drmModeConnectorPtr ptr)
116 {
117         if (!ptr)
118                 return;
119
120         drmFree(ptr->encoders);
121         drmFree(ptr->prop_values);
122         drmFree(ptr->props);
123         drmFree(ptr->modes);
124         drmFree(ptr);
125
126 }
127
128 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
129 {
130         drmFree(ptr);
131 }
132
133 /*
134  * ModeSetting functions.
135  */
136
137 drmModeResPtr drmModeGetResources(int fd)
138 {
139         struct drm_mode_card_res res, counts;
140         drmModeResPtr r = 0;
141
142 retry:
143         memset(&res, 0, sizeof(struct drm_mode_card_res));
144         if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
145                 return 0;
146
147         counts = res;
148
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         }
169
170         if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
171                 goto err_allocs;
172
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));
186
187                 goto retry;
188         }
189
190         /*
191          * return
192          */
193         if (!(r = drmMalloc(sizeof(*r))))
194                 goto err_allocs;
195
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;
204
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 (!r->fbs || !r->crtcs || !r->connectors || !r->encoders) {
210                 drmFree(r->fbs);
211                 drmFree(r->crtcs);
212                 drmFree(r->connectors);
213                 drmFree(r->encoders);
214                 drmFree(r);
215                 r = 0;
216         }
217
218 err_allocs:
219         drmFree(U642VOID(res.fb_id_ptr));
220         drmFree(U642VOID(res.crtc_id_ptr));
221         drmFree(U642VOID(res.connector_id_ptr));
222         drmFree(U642VOID(res.encoder_id_ptr));
223
224         return r;
225 }
226
227 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
228                  uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
229                  uint32_t *buf_id)
230 {
231         struct drm_mode_fb_cmd f;
232         int ret;
233
234         f.width  = width;
235         f.height = height;
236         f.pitch  = pitch;
237         f.bpp    = bpp;
238         f.depth  = depth;
239         f.handle = bo_handle;
240
241         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
242                 return ret;
243
244         *buf_id = f.fb_id;
245         return 0;
246 }
247
248 int drmModeRmFB(int fd, uint32_t bufferId)
249 {
250         return drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
251
252
253 }
254
255 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
256 {
257         struct drm_mode_fb_cmd info;
258         drmModeFBPtr r;
259
260         info.fb_id = buf;
261
262         if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
263                 return NULL;
264
265         if (!(r = drmMalloc(sizeof(*r))))
266                 return NULL;
267
268         r->fb_id = info.fb_id;
269         r->width = info.width;
270         r->height = info.height;
271         r->pitch = info.pitch;
272         r->bpp = info.bpp;
273         r->handle = info.handle;
274         r->depth = info.depth;
275
276         return r;
277 }
278
279 int drmModeDirtyFB(int fd, uint32_t bufferId,
280                    drmModeClipPtr clips, uint32_t num_clips)
281 {
282         struct drm_mode_fb_dirty_cmd dirty = { 0 };
283
284         dirty.fb_id = bufferId;
285         dirty.clips_ptr = VOID2U64(clips);
286         dirty.num_clips = num_clips;
287
288         return drmIoctl(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
289 }
290
291
292 /*
293  * Crtc functions
294  */
295
296 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
297 {
298         struct drm_mode_crtc crtc;
299         drmModeCrtcPtr r;
300
301         crtc.crtc_id = crtcId;
302
303         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
304                 return 0;
305
306         /*
307          * return
308          */
309
310         if (!(r = drmMalloc(sizeof(*r))))
311                 return 0;
312
313         r->crtc_id         = crtc.crtc_id;
314         r->x               = crtc.x;
315         r->y               = crtc.y;
316         r->mode_valid      = crtc.mode_valid;
317         if (r->mode_valid)
318                 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
319         r->buffer_id       = crtc.fb_id;
320         r->gamma_size      = crtc.gamma_size;
321         return r;
322 }
323
324
325 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
326                    uint32_t x, uint32_t y, uint32_t *connectors, int count,
327                    drmModeModeInfoPtr mode)
328 {
329         struct drm_mode_crtc crtc;
330
331         crtc.x             = x;
332         crtc.y             = y;
333         crtc.crtc_id       = crtcId;
334         crtc.fb_id         = bufferId;
335         crtc.set_connectors_ptr = VOID2U64(connectors);
336         crtc.count_connectors = count;
337         if (mode) {
338           memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
339           crtc.mode_valid = 1;
340         } else
341           crtc.mode_valid = 0;
342
343         return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
344 }
345
346 /*
347  * Cursor manipulation
348  */
349
350 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
351 {
352         struct drm_mode_cursor arg;
353
354         arg.flags = DRM_MODE_CURSOR_BO;
355         arg.crtc_id = crtcId;
356         arg.width = width;
357         arg.height = height;
358         arg.handle = bo_handle;
359
360         return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
361 }
362
363 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
364 {
365         struct drm_mode_cursor arg;
366
367         arg.flags = DRM_MODE_CURSOR_MOVE;
368         arg.crtc_id = crtcId;
369         arg.x = x;
370         arg.y = y;
371
372         return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
373 }
374
375 /*
376  * Encoder get
377  */
378 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
379 {
380         struct drm_mode_get_encoder enc;
381         drmModeEncoderPtr r = NULL;
382
383         enc.encoder_id = encoder_id;
384         enc.encoder_type = 0;
385         enc.possible_crtcs = 0;
386         enc.possible_clones = 0;
387
388         if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
389                 return 0;
390
391         if (!(r = drmMalloc(sizeof(*r))))
392                 return 0;
393
394         r->encoder_id = enc.encoder_id;
395         r->crtc_id = enc.crtc_id;
396         r->encoder_type = enc.encoder_type;
397         r->possible_crtcs = enc.possible_crtcs;
398         r->possible_clones = enc.possible_clones;
399
400         return r;
401 }
402
403 /*
404  * Connector manipulation
405  */
406
407 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
408 {
409         struct drm_mode_get_connector conn;
410         drmModeConnectorPtr r = NULL;
411
412         conn.connector_id = connector_id;
413         conn.connector_type_id = 0;
414         conn.connector_type  = 0;
415         conn.count_modes  = 0;
416         conn.modes_ptr    = 0;
417         conn.count_props  = 0;
418         conn.props_ptr    = 0;
419         conn.prop_values_ptr = 0;
420         conn.count_encoders  = 0;
421         conn.encoders_ptr = 0;
422
423         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
424                 return 0;
425
426         if (conn.count_props) {
427                 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
428                 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
429         }
430
431         if (conn.count_modes)
432                 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
433
434         if (conn.count_encoders)
435                 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
436
437         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
438                 goto err_allocs;
439
440         if(!(r = drmMalloc(sizeof(*r)))) {
441                 goto err_allocs;
442         }
443
444         r->connector_id = conn.connector_id;
445         r->encoder_id = conn.encoder_id;
446         r->connection   = conn.connection;
447         r->mmWidth      = conn.mm_width;
448         r->mmHeight     = conn.mm_height;
449         /* convert subpixel from kernel to userspace */
450         r->subpixel     = conn.subpixel + 1;
451         r->count_modes  = conn.count_modes;
452         /* TODO we should test if these alloc & cpy fails. */
453         r->count_props  = conn.count_props;
454         r->props        = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
455         r->prop_values  = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
456         r->modes        = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
457         r->count_encoders = conn.count_encoders;
458         r->encoders     = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
459         r->connector_type  = conn.connector_type;
460         r->connector_type_id = conn.connector_type_id;
461
462         if (!r->props || !r->prop_values || !r->modes || !r->encoders)
463                 goto err_allocs;
464
465 err_allocs:
466         drmFree(U642VOID(conn.prop_values_ptr));
467         drmFree(U642VOID(conn.props_ptr));
468         drmFree(U642VOID(conn.modes_ptr));
469         drmFree(U642VOID(conn.encoders_ptr));
470
471         return r;
472 }
473
474 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
475 {
476         struct drm_mode_mode_cmd res;
477
478         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
479         res.connector_id = connector_id;
480
481         return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
482 }
483
484 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
485 {
486         struct drm_mode_mode_cmd res;
487
488         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
489         res.connector_id = connector_id;
490
491         return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
492 }
493
494
495 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
496 {
497         struct drm_mode_get_property prop;
498         drmModePropertyPtr r;
499
500         prop.prop_id = property_id;
501         prop.count_enum_blobs = 0;
502         prop.count_values = 0;
503         prop.flags = 0;
504         prop.enum_blob_ptr = 0;
505         prop.values_ptr = 0;
506
507         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
508                 return 0;
509
510         if (prop.count_values)
511                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
512
513         if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
514                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
515
516         if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
517                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
518                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
519         }
520
521         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
522                 r = NULL;
523                 goto err_allocs;
524         }
525
526         if (!(r = drmMalloc(sizeof(*r))))
527                 return NULL;
528
529         r->prop_id = prop.prop_id;
530         r->count_values = prop.count_values;
531
532         r->flags = prop.flags;
533         if (prop.count_values)
534                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
535         if (prop.flags & DRM_MODE_PROP_ENUM) {
536                 r->count_enums = prop.count_enum_blobs;
537                 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
538         } else if (prop.flags & DRM_MODE_PROP_BLOB) {
539                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
540                 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
541                 r->count_blobs = prop.count_enum_blobs;
542         }
543         strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
544         r->name[DRM_PROP_NAME_LEN-1] = 0;
545
546 err_allocs:
547         drmFree(U642VOID(prop.values_ptr));
548         drmFree(U642VOID(prop.enum_blob_ptr));
549
550         return r;
551 }
552
553 void drmModeFreeProperty(drmModePropertyPtr ptr)
554 {
555         if (!ptr)
556                 return;
557
558         drmFree(ptr->values);
559         drmFree(ptr->enums);
560         drmFree(ptr);
561 }
562
563 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
564 {
565         struct drm_mode_get_blob blob;
566         drmModePropertyBlobPtr r;
567
568         blob.length = 0;
569         blob.data = 0;
570         blob.blob_id = blob_id;
571
572         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
573                 return NULL;
574
575         if (blob.length)
576                 blob.data = VOID2U64(drmMalloc(blob.length));
577
578         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
579                 r = NULL;
580                 goto err_allocs;
581         }
582
583         if (!(r = drmMalloc(sizeof(*r))))
584                 return NULL;
585
586         r->id = blob.blob_id;
587         r->length = blob.length;
588         r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
589
590 err_allocs:
591         drmFree(U642VOID(blob.data));
592         return r;
593 }
594
595 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
596 {
597         if (!ptr)
598                 return;
599
600         drmFree(ptr->data);
601         drmFree(ptr);
602 }
603
604 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
605                              uint64_t value)
606 {
607         struct drm_mode_connector_set_property osp;
608         int ret;
609
610         osp.connector_id = connector_id;
611         osp.prop_id = property_id;
612         osp.value = value;
613
614         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
615                 return ret;
616
617         return 0;
618 }
619
620 /*
621  * checks if a modesetting capable driver has attached to the pci id
622  * returns 0 if modesetting supported.
623  *  -EINVAL or invalid bus id
624  *  -ENOSYS if no modesetting support
625 */
626 int drmCheckModesettingSupported(const char *busid)
627 {
628 #ifdef __linux__
629         char pci_dev_dir[1024];
630         int domain, bus, dev, func;
631         DIR *sysdir;
632         struct dirent *dent;
633         int found = 0, ret;
634
635         ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
636         if (ret != 4)
637                 return -EINVAL;
638
639         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
640                 domain, bus, dev, func);
641
642         sysdir = opendir(pci_dev_dir);
643         if (sysdir) {
644                 dent = readdir(sysdir);
645                 while (dent) {
646                         if (!strncmp(dent->d_name, "controlD", 8)) {
647                                 found = 1;
648                                 break;
649                         }
650
651                         dent = readdir(sysdir);
652                 }
653                 closedir(sysdir);
654                 if (found)
655                         return 0;
656         }
657
658         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
659                 domain, bus, dev, func);
660
661         sysdir = opendir(pci_dev_dir);
662         if (!sysdir)
663                 return -EINVAL;
664
665         dent = readdir(sysdir);
666         while (dent) {
667                 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
668                         found = 1;
669                         break;
670                 }
671
672                 dent = readdir(sysdir);
673         }
674
675         closedir(sysdir);
676         if (found)
677                 return 0;
678 #endif
679         return -ENOSYS;
680
681 }
682
683 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
684                         uint16_t *red, uint16_t *green, uint16_t *blue)
685 {
686         int ret;
687         struct drm_mode_crtc_lut l;
688
689         l.crtc_id = crtc_id;
690         l.gamma_size = size;
691         l.red = VOID2U64(red);
692         l.green = VOID2U64(green);
693         l.blue = VOID2U64(blue);
694
695         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
696                 return ret;
697
698         return 0;
699 }
700
701 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
702                         uint16_t *red, uint16_t *green, uint16_t *blue)
703 {
704         int ret;
705         struct drm_mode_crtc_lut l;
706
707         l.crtc_id = crtc_id;
708         l.gamma_size = size;
709         l.red = VOID2U64(red);
710         l.green = VOID2U64(green);
711         l.blue = VOID2U64(blue);
712
713         if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
714                 return ret;
715
716         return 0;
717 }
718
719 int drmHandleEvent(int fd, drmEventContextPtr evctx)
720 {
721         char buffer[1024];
722         int len, i;
723         struct drm_event *e;
724         struct drm_event_vblank *vblank;
725         
726         /* The DRM read semantics guarantees that we always get only
727          * complete events. */
728
729         len = read(fd, buffer, sizeof buffer);
730         if (len == 0)
731                 return 0;
732         if (len < sizeof *e)
733                 return -1;
734
735         i = 0;
736         while (i < len) {
737                 e = (struct drm_event *) &buffer[i];
738                 switch (e->type) {
739                 case DRM_EVENT_VBLANK:
740                         if (evctx->version < 1 ||
741                             evctx->vblank_handler == NULL)
742                                 break;
743                         vblank = (struct drm_event_vblank *) e;
744                         evctx->vblank_handler(fd,
745                                               vblank->sequence, 
746                                               vblank->tv_sec,
747                                               vblank->tv_usec,
748                                               U642VOID (vblank->user_data));
749                         break;
750                 case DRM_EVENT_FLIP_COMPLETE:
751                         if (evctx->version < 2 ||
752                             evctx->page_flip_handler == NULL)
753                                 break;
754                         vblank = (struct drm_event_vblank *) e;
755                         evctx->page_flip_handler(fd,
756                                                  vblank->sequence,
757                                                  vblank->tv_sec,
758                                                  vblank->tv_usec,
759                                                  U642VOID (vblank->user_data));
760                         break;
761                 default:
762                         break;
763                 }
764                 i += e->length;
765         }
766
767         return 0;
768 }
769
770 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
771                     uint32_t flags, void *user_data)
772 {
773         struct drm_mode_crtc_page_flip flip;
774
775         flip.fb_id = fb_id;
776         flip.crtc_id = crtc_id;
777         flip.user_data = VOID2U64(user_data);
778         flip.flags = flags;
779         flip.reserved = 0;
780
781         return drmIoctl(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
782 }