OSDN Git Service

modetest: fix printing of fourcc on BE machines
[android-x86/external-libdrm.git] / tests / modetest / modetest.c
1 /*
2  * DRM based mode setting test program
3  * Copyright 2008 Tungsten Graphics
4  *   Jakob Bornecrantz <jakob@tungstengraphics.com>
5  * Copyright 2008 Intel Corporation
6  *   Jesse Barnes <jesse.barnes@intel.com>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24  * IN THE SOFTWARE.
25  */
26
27 /*
28  * This fairly simple test program dumps output in a similar format to the
29  * "xrandr" tool everyone knows & loves.  It's necessarily slightly different
30  * since the kernel separates outputs into encoder and connector structures,
31  * each with their own unique ID.  The program also allows test testing of the
32  * memory management and mode setting APIs by allowing the user to specify a
33  * connector and mode to use for mode setting.  If all works as expected, a
34  * blue background should be painted on the monitor attached to the specified
35  * connector after the selected mode is set.
36  *
37  * TODO: use cairo to write the mode info on the selected output once
38  *       the mode has been programmed, along with possible test patterns.
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <assert.h>
46 #include <ctype.h>
47 #include <stdbool.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stdint.h>
51 #include <inttypes.h>
52 #include <unistd.h>
53 #include <string.h>
54 #include <strings.h>
55 #include <errno.h>
56 #include <poll.h>
57 #include <sys/time.h>
58 #ifdef HAVE_SYS_SELECT_H
59 #include <sys/select.h>
60 #endif
61
62 #include "xf86drm.h"
63 #include "xf86drmMode.h"
64 #include "drm_fourcc.h"
65
66 #include "util/common.h"
67 #include "util/format.h"
68 #include "util/kms.h"
69 #include "util/pattern.h"
70
71 #include "buffers.h"
72 #include "cursor.h"
73
74 struct crtc {
75         drmModeCrtc *crtc;
76         drmModeObjectProperties *props;
77         drmModePropertyRes **props_info;
78         drmModeModeInfo *mode;
79 };
80
81 struct encoder {
82         drmModeEncoder *encoder;
83 };
84
85 struct connector {
86         drmModeConnector *connector;
87         drmModeObjectProperties *props;
88         drmModePropertyRes **props_info;
89         char *name;
90 };
91
92 struct fb {
93         drmModeFB *fb;
94 };
95
96 struct plane {
97         drmModePlane *plane;
98         drmModeObjectProperties *props;
99         drmModePropertyRes **props_info;
100 };
101
102 struct resources {
103         drmModeRes *res;
104         drmModePlaneRes *plane_res;
105
106         struct crtc *crtcs;
107         struct encoder *encoders;
108         struct connector *connectors;
109         struct fb *fbs;
110         struct plane *planes;
111 };
112
113 struct device {
114         int fd;
115
116         struct resources *resources;
117
118         struct {
119                 unsigned int width;
120                 unsigned int height;
121
122                 unsigned int fb_id;
123                 struct bo *bo;
124                 struct bo *cursor_bo;
125         } mode;
126 };
127
128 static inline int64_t U642I64(uint64_t val)
129 {
130         return (int64_t)*((int64_t *)&val);
131 }
132
133 #define bit_name_fn(res)                                        \
134 const char * res##_str(int type) {                              \
135         unsigned int i;                                         \
136         const char *sep = "";                                   \
137         for (i = 0; i < ARRAY_SIZE(res##_names); i++) {         \
138                 if (type & (1 << i)) {                          \
139                         printf("%s%s", sep, res##_names[i]);    \
140                         sep = ", ";                             \
141                 }                                               \
142         }                                                       \
143         return NULL;                                            \
144 }
145
146 static const char *mode_type_names[] = {
147         "builtin",
148         "clock_c",
149         "crtc_c",
150         "preferred",
151         "default",
152         "userdef",
153         "driver",
154 };
155
156 static bit_name_fn(mode_type)
157
158 static const char *mode_flag_names[] = {
159         "phsync",
160         "nhsync",
161         "pvsync",
162         "nvsync",
163         "interlace",
164         "dblscan",
165         "csync",
166         "pcsync",
167         "ncsync",
168         "hskew",
169         "bcast",
170         "pixmux",
171         "dblclk",
172         "clkdiv2"
173 };
174
175 static bit_name_fn(mode_flag)
176
177 static void dump_fourcc(uint32_t fourcc)
178 {
179         printf(" %c%c%c%c",
180                 fourcc,
181                 fourcc >> 8,
182                 fourcc >> 16,
183                 fourcc >> 24);
184 }
185
186 static void dump_encoders(struct device *dev)
187 {
188         drmModeEncoder *encoder;
189         int i;
190
191         printf("Encoders:\n");
192         printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
193         for (i = 0; i < dev->resources->res->count_encoders; i++) {
194                 encoder = dev->resources->encoders[i].encoder;
195                 if (!encoder)
196                         continue;
197
198                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
199                        encoder->encoder_id,
200                        encoder->crtc_id,
201                        util_lookup_encoder_type_name(encoder->encoder_type),
202                        encoder->possible_crtcs,
203                        encoder->possible_clones);
204         }
205         printf("\n");
206 }
207
208 static void dump_mode(drmModeModeInfo *mode)
209 {
210         printf("  %s %d %d %d %d %d %d %d %d %d %d",
211                mode->name,
212                mode->vrefresh,
213                mode->hdisplay,
214                mode->hsync_start,
215                mode->hsync_end,
216                mode->htotal,
217                mode->vdisplay,
218                mode->vsync_start,
219                mode->vsync_end,
220                mode->vtotal,
221                mode->clock);
222
223         printf(" flags: ");
224         mode_flag_str(mode->flags);
225         printf("; type: ");
226         mode_type_str(mode->type);
227         printf("\n");
228 }
229
230 static void dump_blob(struct device *dev, uint32_t blob_id)
231 {
232         uint32_t i;
233         unsigned char *blob_data;
234         drmModePropertyBlobPtr blob;
235
236         blob = drmModeGetPropertyBlob(dev->fd, blob_id);
237         if (!blob) {
238                 printf("\n");
239                 return;
240         }
241
242         blob_data = blob->data;
243
244         for (i = 0; i < blob->length; i++) {
245                 if (i % 16 == 0)
246                         printf("\n\t\t\t");
247                 printf("%.2hhx", blob_data[i]);
248         }
249         printf("\n");
250
251         drmModeFreePropertyBlob(blob);
252 }
253
254 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
255                       uint32_t prop_id, uint64_t value)
256 {
257         int i;
258         printf("\t%d", prop_id);
259         if (!prop) {
260                 printf("\n");
261                 return;
262         }
263
264         printf(" %s:\n", prop->name);
265
266         printf("\t\tflags:");
267         if (prop->flags & DRM_MODE_PROP_PENDING)
268                 printf(" pending");
269         if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
270                 printf(" immutable");
271         if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
272                 printf(" signed range");
273         if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
274                 printf(" range");
275         if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
276                 printf(" enum");
277         if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
278                 printf(" bitmask");
279         if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
280                 printf(" blob");
281         if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
282                 printf(" object");
283         printf("\n");
284
285         if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
286                 printf("\t\tvalues:");
287                 for (i = 0; i < prop->count_values; i++)
288                         printf(" %"PRId64, U642I64(prop->values[i]));
289                 printf("\n");
290         }
291
292         if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
293                 printf("\t\tvalues:");
294                 for (i = 0; i < prop->count_values; i++)
295                         printf(" %"PRIu64, prop->values[i]);
296                 printf("\n");
297         }
298
299         if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
300                 printf("\t\tenums:");
301                 for (i = 0; i < prop->count_enums; i++)
302                         printf(" %s=%llu", prop->enums[i].name,
303                                prop->enums[i].value);
304                 printf("\n");
305         } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
306                 printf("\t\tvalues:");
307                 for (i = 0; i < prop->count_enums; i++)
308                         printf(" %s=0x%llx", prop->enums[i].name,
309                                (1LL << prop->enums[i].value));
310                 printf("\n");
311         } else {
312                 assert(prop->count_enums == 0);
313         }
314
315         if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
316                 printf("\t\tblobs:\n");
317                 for (i = 0; i < prop->count_blobs; i++)
318                         dump_blob(dev, prop->blob_ids[i]);
319                 printf("\n");
320         } else {
321                 assert(prop->count_blobs == 0);
322         }
323
324         printf("\t\tvalue:");
325         if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
326                 dump_blob(dev, value);
327         else if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
328                 printf(" %"PRId64"\n", value);
329         else
330                 printf(" %"PRIu64"\n", value);
331 }
332
333 static void dump_connectors(struct device *dev)
334 {
335         int i, j;
336
337         printf("Connectors:\n");
338         printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n");
339         for (i = 0; i < dev->resources->res->count_connectors; i++) {
340                 struct connector *_connector = &dev->resources->connectors[i];
341                 drmModeConnector *connector = _connector->connector;
342                 if (!connector)
343                         continue;
344
345                 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t",
346                        connector->connector_id,
347                        connector->encoder_id,
348                        util_lookup_connector_status_name(connector->connection),
349                        _connector->name,
350                        connector->mmWidth, connector->mmHeight,
351                        connector->count_modes);
352
353                 for (j = 0; j < connector->count_encoders; j++)
354                         printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
355                 printf("\n");
356
357                 if (connector->count_modes) {
358                         printf("  modes:\n");
359                         printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
360                                "vss vse vtot)\n");
361                         for (j = 0; j < connector->count_modes; j++)
362                                 dump_mode(&connector->modes[j]);
363                 }
364
365                 if (_connector->props) {
366                         printf("  props:\n");
367                         for (j = 0; j < (int)_connector->props->count_props; j++)
368                                 dump_prop(dev, _connector->props_info[j],
369                                           _connector->props->props[j],
370                                           _connector->props->prop_values[j]);
371                 }
372         }
373         printf("\n");
374 }
375
376 static void dump_crtcs(struct device *dev)
377 {
378         int i;
379         uint32_t j;
380
381         printf("CRTCs:\n");
382         printf("id\tfb\tpos\tsize\n");
383         for (i = 0; i < dev->resources->res->count_crtcs; i++) {
384                 struct crtc *_crtc = &dev->resources->crtcs[i];
385                 drmModeCrtc *crtc = _crtc->crtc;
386                 if (!crtc)
387                         continue;
388
389                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
390                        crtc->crtc_id,
391                        crtc->buffer_id,
392                        crtc->x, crtc->y,
393                        crtc->width, crtc->height);
394                 dump_mode(&crtc->mode);
395
396                 if (_crtc->props) {
397                         printf("  props:\n");
398                         for (j = 0; j < _crtc->props->count_props; j++)
399                                 dump_prop(dev, _crtc->props_info[j],
400                                           _crtc->props->props[j],
401                                           _crtc->props->prop_values[j]);
402                 } else {
403                         printf("  no properties found\n");
404                 }
405         }
406         printf("\n");
407 }
408
409 static void dump_framebuffers(struct device *dev)
410 {
411         drmModeFB *fb;
412         int i;
413
414         printf("Frame buffers:\n");
415         printf("id\tsize\tpitch\n");
416         for (i = 0; i < dev->resources->res->count_fbs; i++) {
417                 fb = dev->resources->fbs[i].fb;
418                 if (!fb)
419                         continue;
420
421                 printf("%u\t(%ux%u)\t%u\n",
422                        fb->fb_id,
423                        fb->width, fb->height,
424                        fb->pitch);
425         }
426         printf("\n");
427 }
428
429 static void dump_planes(struct device *dev)
430 {
431         unsigned int i, j;
432
433         printf("Planes:\n");
434         printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
435
436         if (!dev->resources->plane_res)
437                 return;
438
439         for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
440                 struct plane *plane = &dev->resources->planes[i];
441                 drmModePlane *ovr = plane->plane;
442                 if (!ovr)
443                         continue;
444
445                 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
446                        ovr->plane_id, ovr->crtc_id, ovr->fb_id,
447                        ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
448                        ovr->gamma_size, ovr->possible_crtcs);
449
450                 if (!ovr->count_formats)
451                         continue;
452
453                 printf("  formats:");
454                 for (j = 0; j < ovr->count_formats; j++)
455                         dump_fourcc(ovr->formats[j]);
456                 printf("\n");
457
458                 if (plane->props) {
459                         printf("  props:\n");
460                         for (j = 0; j < plane->props->count_props; j++)
461                                 dump_prop(dev, plane->props_info[j],
462                                           plane->props->props[j],
463                                           plane->props->prop_values[j]);
464                 } else {
465                         printf("  no properties found\n");
466                 }
467         }
468         printf("\n");
469
470         return;
471 }
472
473 static void free_resources(struct resources *res)
474 {
475         int i;
476
477         if (!res)
478                 return;
479
480 #define free_resource(_res, __res, type, Type)                                  \
481         do {                                                                    \
482                 if (!(_res)->type##s)                                           \
483                         break;                                                  \
484                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
485                         if (!(_res)->type##s[i].type)                           \
486                                 break;                                          \
487                         drmModeFree##Type((_res)->type##s[i].type);             \
488                 }                                                               \
489                 free((_res)->type##s);                                          \
490         } while (0)
491
492 #define free_properties(_res, __res, type)                                      \
493         do {                                                                    \
494                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
495                         drmModeFreeObjectProperties(res->type##s[i].props);     \
496                         free(res->type##s[i].props_info);                       \
497                 }                                                               \
498         } while (0)
499
500         if (res->res) {
501                 free_properties(res, res, crtc);
502
503                 free_resource(res, res, crtc, Crtc);
504                 free_resource(res, res, encoder, Encoder);
505
506                 for (i = 0; i < res->res->count_connectors; i++)
507                         free(res->connectors[i].name);
508
509                 free_resource(res, res, connector, Connector);
510                 free_resource(res, res, fb, FB);
511
512                 drmModeFreeResources(res->res);
513         }
514
515         if (res->plane_res) {
516                 free_properties(res, plane_res, plane);
517
518                 free_resource(res, plane_res, plane, Plane);
519
520                 drmModeFreePlaneResources(res->plane_res);
521         }
522
523         free(res);
524 }
525
526 static struct resources *get_resources(struct device *dev)
527 {
528         struct resources *res;
529         int i;
530
531         res = calloc(1, sizeof(*res));
532         if (res == 0)
533                 return NULL;
534
535         drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
536
537         res->res = drmModeGetResources(dev->fd);
538         if (!res->res) {
539                 fprintf(stderr, "drmModeGetResources failed: %s\n",
540                         strerror(errno));
541                 goto error;
542         }
543
544         res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs));
545         res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders));
546         res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors));
547         res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs));
548
549         if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
550                 goto error;
551
552 #define get_resource(_res, __res, type, Type)                                   \
553         do {                                                                    \
554                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
555                         (_res)->type##s[i].type =                               \
556                                 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
557                         if (!(_res)->type##s[i].type)                           \
558                                 fprintf(stderr, "could not get %s %i: %s\n",    \
559                                         #type, (_res)->__res->type##s[i],       \
560                                         strerror(errno));                       \
561                 }                                                               \
562         } while (0)
563
564         get_resource(res, res, crtc, Crtc);
565         get_resource(res, res, encoder, Encoder);
566         get_resource(res, res, connector, Connector);
567         get_resource(res, res, fb, FB);
568
569         /* Set the name of all connectors based on the type name and the per-type ID. */
570         for (i = 0; i < res->res->count_connectors; i++) {
571                 struct connector *connector = &res->connectors[i];
572                 drmModeConnector *conn = connector->connector;
573
574                 asprintf(&connector->name, "%s-%u",
575                          util_lookup_connector_type_name(conn->connector_type),
576                          conn->connector_type_id);
577         }
578
579 #define get_properties(_res, __res, type, Type)                                 \
580         do {                                                                    \
581                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
582                         struct type *obj = &res->type##s[i];                    \
583                         unsigned int j;                                         \
584                         obj->props =                                            \
585                                 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
586                                                            DRM_MODE_OBJECT_##Type); \
587                         if (!obj->props) {                                      \
588                                 fprintf(stderr,                                 \
589                                         "could not get %s %i properties: %s\n", \
590                                         #type, obj->type->type##_id,            \
591                                         strerror(errno));                       \
592                                 continue;                                       \
593                         }                                                       \
594                         obj->props_info = calloc(obj->props->count_props,       \
595                                                  sizeof(*obj->props_info));     \
596                         if (!obj->props_info)                                   \
597                                 continue;                                       \
598                         for (j = 0; j < obj->props->count_props; ++j)           \
599                                 obj->props_info[j] =                            \
600                                         drmModeGetProperty(dev->fd, obj->props->props[j]); \
601                 }                                                               \
602         } while (0)
603
604         get_properties(res, res, crtc, CRTC);
605         get_properties(res, res, connector, CONNECTOR);
606
607         for (i = 0; i < res->res->count_crtcs; ++i)
608                 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
609
610         res->plane_res = drmModeGetPlaneResources(dev->fd);
611         if (!res->plane_res) {
612                 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
613                         strerror(errno));
614                 return res;
615         }
616
617         res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes));
618         if (!res->planes)
619                 goto error;
620
621         get_resource(res, plane_res, plane, Plane);
622         get_properties(res, plane_res, plane, PLANE);
623
624         return res;
625
626 error:
627         free_resources(res);
628         return NULL;
629 }
630
631 static int get_crtc_index(struct device *dev, uint32_t id)
632 {
633         int i;
634
635         for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
636                 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
637                 if (crtc && crtc->crtc_id == id)
638                         return i;
639         }
640
641         return -1;
642 }
643
644 static drmModeConnector *get_connector_by_name(struct device *dev, const char *name)
645 {
646         struct connector *connector;
647         int i;
648
649         for (i = 0; i < dev->resources->res->count_connectors; i++) {
650                 connector = &dev->resources->connectors[i];
651
652                 if (strcmp(connector->name, name) == 0)
653                         return connector->connector;
654         }
655
656         return NULL;
657 }
658
659 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
660 {
661         drmModeConnector *connector;
662         int i;
663
664         for (i = 0; i < dev->resources->res->count_connectors; i++) {
665                 connector = dev->resources->connectors[i].connector;
666                 if (connector && connector->connector_id == id)
667                         return connector;
668         }
669
670         return NULL;
671 }
672
673 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
674 {
675         drmModeEncoder *encoder;
676         int i;
677
678         for (i = 0; i < dev->resources->res->count_encoders; i++) {
679                 encoder = dev->resources->encoders[i].encoder;
680                 if (encoder && encoder->encoder_id == id)
681                         return encoder;
682         }
683
684         return NULL;
685 }
686
687 /* -----------------------------------------------------------------------------
688  * Pipes and planes
689  */
690
691 /*
692  * Mode setting with the kernel interfaces is a bit of a chore.
693  * First you have to find the connector in question and make sure the
694  * requested mode is available.
695  * Then you need to find the encoder attached to that connector so you
696  * can bind it with a free crtc.
697  */
698 struct pipe_arg {
699         const char **cons;
700         uint32_t *con_ids;
701         unsigned int num_cons;
702         uint32_t crtc_id;
703         char mode_str[64];
704         char format_str[5];
705         unsigned int vrefresh;
706         unsigned int fourcc;
707         drmModeModeInfo *mode;
708         struct crtc *crtc;
709         unsigned int fb_id[2], current_fb_id;
710         struct timeval start;
711
712         int swap_count;
713 };
714
715 struct plane_arg {
716         uint32_t plane_id;  /* the id of plane to use */
717         uint32_t crtc_id;  /* the id of CRTC to bind to */
718         bool has_position;
719         int32_t x, y;
720         uint32_t w, h;
721         double scale;
722         unsigned int fb_id;
723         struct bo *bo;
724         char format_str[5]; /* need to leave room for terminating \0 */
725         unsigned int fourcc;
726 };
727
728 static drmModeModeInfo *
729 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
730         const unsigned int vrefresh)
731 {
732         drmModeConnector *connector;
733         drmModeModeInfo *mode;
734         int i;
735
736         connector = get_connector_by_id(dev, con_id);
737         if (!connector || !connector->count_modes)
738                 return NULL;
739
740         for (i = 0; i < connector->count_modes; i++) {
741                 mode = &connector->modes[i];
742                 if (!strcmp(mode->name, mode_str)) {
743                         /* If the vertical refresh frequency is not specified then return the
744                          * first mode that match with the name. Else, return the mode that match
745                          * the name and the specified vertical refresh frequency.
746                          */
747                         if (vrefresh == 0)
748                                 return mode;
749                         else if (mode->vrefresh == vrefresh)
750                                 return mode;
751                 }
752         }
753
754         return NULL;
755 }
756
757 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
758 {
759         uint32_t possible_crtcs = ~0;
760         uint32_t active_crtcs = 0;
761         unsigned int crtc_idx;
762         unsigned int i;
763         int j;
764
765         for (i = 0; i < pipe->num_cons; ++i) {
766                 uint32_t crtcs_for_connector = 0;
767                 drmModeConnector *connector;
768                 drmModeEncoder *encoder;
769                 int idx;
770
771                 connector = get_connector_by_id(dev, pipe->con_ids[i]);
772                 if (!connector)
773                         return NULL;
774
775                 for (j = 0; j < connector->count_encoders; ++j) {
776                         encoder = get_encoder_by_id(dev, connector->encoders[j]);
777                         if (!encoder)
778                                 continue;
779
780                         crtcs_for_connector |= encoder->possible_crtcs;
781
782                         idx = get_crtc_index(dev, encoder->crtc_id);
783                         if (idx >= 0)
784                                 active_crtcs |= 1 << idx;
785                 }
786
787                 possible_crtcs &= crtcs_for_connector;
788         }
789
790         if (!possible_crtcs)
791                 return NULL;
792
793         /* Return the first possible and active CRTC if one exists, or the first
794          * possible CRTC otherwise.
795          */
796         if (possible_crtcs & active_crtcs)
797                 crtc_idx = ffs(possible_crtcs & active_crtcs);
798         else
799                 crtc_idx = ffs(possible_crtcs);
800
801         return &dev->resources->crtcs[crtc_idx - 1];
802 }
803
804 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
805 {
806         drmModeModeInfo *mode = NULL;
807         int i;
808
809         pipe->mode = NULL;
810
811         for (i = 0; i < (int)pipe->num_cons; i++) {
812                 mode = connector_find_mode(dev, pipe->con_ids[i],
813                                            pipe->mode_str, pipe->vrefresh);
814                 if (mode == NULL) {
815                         fprintf(stderr,
816                                 "failed to find mode \"%s\" for connector %s\n",
817                                 pipe->mode_str, pipe->cons[i]);
818                         return -EINVAL;
819                 }
820         }
821
822         /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
823          * locate a CRTC that can be attached to all the connectors.
824          */
825         if (pipe->crtc_id != (uint32_t)-1) {
826                 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
827                         struct crtc *crtc = &dev->resources->crtcs[i];
828
829                         if (pipe->crtc_id == crtc->crtc->crtc_id) {
830                                 pipe->crtc = crtc;
831                                 break;
832                         }
833                 }
834         } else {
835                 pipe->crtc = pipe_find_crtc(dev, pipe);
836         }
837
838         if (!pipe->crtc) {
839                 fprintf(stderr, "failed to find CRTC for pipe\n");
840                 return -EINVAL;
841         }
842
843         pipe->mode = mode;
844         pipe->crtc->mode = mode;
845
846         return 0;
847 }
848
849 /* -----------------------------------------------------------------------------
850  * Properties
851  */
852
853 struct property_arg {
854         uint32_t obj_id;
855         uint32_t obj_type;
856         char name[DRM_PROP_NAME_LEN+1];
857         uint32_t prop_id;
858         uint64_t value;
859 };
860
861 static void set_property(struct device *dev, struct property_arg *p)
862 {
863         drmModeObjectProperties *props = NULL;
864         drmModePropertyRes **props_info = NULL;
865         const char *obj_type;
866         int ret;
867         int i;
868
869         p->obj_type = 0;
870         p->prop_id = 0;
871
872 #define find_object(_res, __res, type, Type)                                    \
873         do {                                                                    \
874                 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) {     \
875                         struct type *obj = &(_res)->type##s[i];                 \
876                         if (obj->type->type##_id != p->obj_id)                  \
877                                 continue;                                       \
878                         p->obj_type = DRM_MODE_OBJECT_##Type;                   \
879                         obj_type = #Type;                                       \
880                         props = obj->props;                                     \
881                         props_info = obj->props_info;                           \
882                 }                                                               \
883         } while(0)                                                              \
884
885         find_object(dev->resources, res, crtc, CRTC);
886         if (p->obj_type == 0)
887                 find_object(dev->resources, res, connector, CONNECTOR);
888         if (p->obj_type == 0)
889                 find_object(dev->resources, plane_res, plane, PLANE);
890         if (p->obj_type == 0) {
891                 fprintf(stderr, "Object %i not found, can't set property\n",
892                         p->obj_id);
893                         return;
894         }
895
896         if (!props) {
897                 fprintf(stderr, "%s %i has no properties\n",
898                         obj_type, p->obj_id);
899                 return;
900         }
901
902         for (i = 0; i < (int)props->count_props; ++i) {
903                 if (!props_info[i])
904                         continue;
905                 if (strcmp(props_info[i]->name, p->name) == 0)
906                         break;
907         }
908
909         if (i == (int)props->count_props) {
910                 fprintf(stderr, "%s %i has no %s property\n",
911                         obj_type, p->obj_id, p->name);
912                 return;
913         }
914
915         p->prop_id = props->props[i];
916
917         ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
918                                        p->prop_id, p->value);
919         if (ret < 0)
920                 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
921                         obj_type, p->obj_id, p->name, p->value, strerror(errno));
922 }
923
924 /* -------------------------------------------------------------------------- */
925
926 static void
927 page_flip_handler(int fd, unsigned int frame,
928                   unsigned int sec, unsigned int usec, void *data)
929 {
930         struct pipe_arg *pipe;
931         unsigned int new_fb_id;
932         struct timeval end;
933         double t;
934
935         pipe = data;
936         if (pipe->current_fb_id == pipe->fb_id[0])
937                 new_fb_id = pipe->fb_id[1];
938         else
939                 new_fb_id = pipe->fb_id[0];
940
941         drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
942                         DRM_MODE_PAGE_FLIP_EVENT, pipe);
943         pipe->current_fb_id = new_fb_id;
944         pipe->swap_count++;
945         if (pipe->swap_count == 60) {
946                 gettimeofday(&end, NULL);
947                 t = end.tv_sec + end.tv_usec * 1e-6 -
948                         (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
949                 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
950                 pipe->swap_count = 0;
951                 pipe->start = end;
952         }
953 }
954
955 static bool format_support(const drmModePlanePtr ovr, uint32_t fmt)
956 {
957         unsigned int i;
958
959         for (i = 0; i < ovr->count_formats; ++i) {
960                 if (ovr->formats[i] == fmt)
961                         return true;
962         }
963
964         return false;
965 }
966
967 static int set_plane(struct device *dev, struct plane_arg *p)
968 {
969         drmModePlane *ovr;
970         uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
971         uint32_t plane_id;
972         struct bo *plane_bo;
973         uint32_t plane_flags = 0;
974         int crtc_x, crtc_y, crtc_w, crtc_h;
975         struct crtc *crtc = NULL;
976         unsigned int pipe;
977         unsigned int i;
978
979         /* Find an unused plane which can be connected to our CRTC. Find the
980          * CRTC index first, then iterate over available planes.
981          */
982         for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
983                 if (p->crtc_id == dev->resources->res->crtcs[i]) {
984                         crtc = &dev->resources->crtcs[i];
985                         pipe = i;
986                         break;
987                 }
988         }
989
990         if (!crtc) {
991                 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
992                 return -1;
993         }
994
995         plane_id = p->plane_id;
996
997         for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
998                 ovr = dev->resources->planes[i].plane;
999                 if (!ovr)
1000                         continue;
1001
1002                 if (plane_id && plane_id != ovr->plane_id)
1003                         continue;
1004
1005                 if (!format_support(ovr, p->fourcc))
1006                         continue;
1007
1008                 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id) {
1009                         plane_id = ovr->plane_id;
1010                         break;
1011                 }
1012         }
1013
1014         if (i == dev->resources->plane_res->count_planes) {
1015                 fprintf(stderr, "no unused plane available for CRTC %u\n",
1016                         crtc->crtc->crtc_id);
1017                 return -1;
1018         }
1019
1020         fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1021                 p->w, p->h, p->format_str, plane_id);
1022
1023         plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
1024                              pitches, offsets, UTIL_PATTERN_TILES);
1025         if (plane_bo == NULL)
1026                 return -1;
1027
1028         p->bo = plane_bo;
1029
1030         /* just use single plane format for now.. */
1031         if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1032                         handles, pitches, offsets, &p->fb_id, plane_flags)) {
1033                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1034                 return -1;
1035         }
1036
1037         crtc_w = p->w * p->scale;
1038         crtc_h = p->h * p->scale;
1039         if (!p->has_position) {
1040                 /* Default to the middle of the screen */
1041                 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1042                 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1043         } else {
1044                 crtc_x = p->x;
1045                 crtc_y = p->y;
1046         }
1047
1048         /* note src coords (last 4 args) are in Q16 format */
1049         if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1050                             plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1051                             0, 0, p->w << 16, p->h << 16)) {
1052                 fprintf(stderr, "failed to enable plane: %s\n",
1053                         strerror(errno));
1054                 return -1;
1055         }
1056
1057         ovr->crtc_id = crtc->crtc->crtc_id;
1058
1059         return 0;
1060 }
1061
1062 static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1063 {
1064         unsigned int i;
1065
1066         for (i = 0; i < count; i++) {
1067                 if (p[i].fb_id)
1068                         drmModeRmFB(dev->fd, p[i].fb_id);
1069                 if (p[i].bo)
1070                         bo_destroy(p[i].bo);
1071         }
1072 }
1073
1074
1075 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1076 {
1077         uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1078         unsigned int fb_id;
1079         struct bo *bo;
1080         unsigned int i;
1081         unsigned int j;
1082         int ret, x;
1083
1084         dev->mode.width = 0;
1085         dev->mode.height = 0;
1086         dev->mode.fb_id = 0;
1087
1088         for (i = 0; i < count; i++) {
1089                 struct pipe_arg *pipe = &pipes[i];
1090
1091                 ret = pipe_find_crtc_and_mode(dev, pipe);
1092                 if (ret < 0)
1093                         continue;
1094
1095                 dev->mode.width += pipe->mode->hdisplay;
1096                 if (dev->mode.height < pipe->mode->vdisplay)
1097                         dev->mode.height = pipe->mode->vdisplay;
1098         }
1099
1100         bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1101                        dev->mode.height, handles, pitches, offsets,
1102                        UTIL_PATTERN_SMPTE);
1103         if (bo == NULL)
1104                 return;
1105
1106         dev->mode.bo = bo;
1107
1108         ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1109                             pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1110         if (ret) {
1111                 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1112                         dev->mode.width, dev->mode.height, strerror(errno));
1113                 return;
1114         }
1115
1116         dev->mode.fb_id = fb_id;
1117
1118         x = 0;
1119         for (i = 0; i < count; i++) {
1120                 struct pipe_arg *pipe = &pipes[i];
1121
1122                 if (pipe->mode == NULL)
1123                         continue;
1124
1125                 printf("setting mode %s-%dHz@%s on connectors ",
1126                        pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1127                 for (j = 0; j < pipe->num_cons; ++j)
1128                         printf("%s, ", pipe->cons[j]);
1129                 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1130
1131                 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1132                                      x, 0, pipe->con_ids, pipe->num_cons,
1133                                      pipe->mode);
1134
1135                 /* XXX: Actually check if this is needed */
1136                 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1137
1138                 x += pipe->mode->hdisplay;
1139
1140                 if (ret) {
1141                         fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1142                         return;
1143                 }
1144         }
1145 }
1146
1147 static void clear_mode(struct device *dev)
1148 {
1149         if (dev->mode.fb_id)
1150                 drmModeRmFB(dev->fd, dev->mode.fb_id);
1151         if (dev->mode.bo)
1152                 bo_destroy(dev->mode.bo);
1153 }
1154
1155 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1156 {
1157         unsigned int i;
1158
1159         /* set up planes/overlays */
1160         for (i = 0; i < count; i++)
1161                 if (set_plane(dev, &p[i]))
1162                         return;
1163 }
1164
1165 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1166 {
1167         uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1168         struct bo *bo;
1169         unsigned int i;
1170         int ret;
1171
1172         /* maybe make cursor width/height configurable some day */
1173         uint32_t cw = 64;
1174         uint32_t ch = 64;
1175
1176         /* create cursor bo.. just using PATTERN_PLAIN as it has
1177          * translucent alpha
1178          */
1179         bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
1180                        offsets, UTIL_PATTERN_PLAIN);
1181         if (bo == NULL)
1182                 return;
1183
1184         dev->mode.cursor_bo = bo;
1185
1186         for (i = 0; i < count; i++) {
1187                 struct pipe_arg *pipe = &pipes[i];
1188                 ret = cursor_init(dev->fd, handles[0],
1189                                 pipe->crtc->crtc->crtc_id,
1190                                 pipe->mode->hdisplay, pipe->mode->vdisplay,
1191                                 cw, ch);
1192                 if (ret) {
1193                         fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1194                                         pipe->crtc_id);
1195                         return;
1196                 }
1197         }
1198
1199         cursor_start();
1200 }
1201
1202 static void clear_cursors(struct device *dev)
1203 {
1204         cursor_stop();
1205
1206         if (dev->mode.cursor_bo)
1207                 bo_destroy(dev->mode.cursor_bo);
1208 }
1209
1210 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1211 {
1212         uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0};
1213         unsigned int other_fb_id;
1214         struct bo *other_bo;
1215         drmEventContext evctx;
1216         unsigned int i;
1217         int ret;
1218
1219         other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width,
1220                              dev->mode.height, handles, pitches, offsets,
1221                              UTIL_PATTERN_PLAIN);
1222         if (other_bo == NULL)
1223                 return;
1224
1225         ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1226                             pipes[0].fourcc, handles, pitches, offsets,
1227                             &other_fb_id, 0);
1228         if (ret) {
1229                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1230                 goto err;
1231         }
1232
1233         for (i = 0; i < count; i++) {
1234                 struct pipe_arg *pipe = &pipes[i];
1235
1236                 if (pipe->mode == NULL)
1237                         continue;
1238
1239                 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1240                                       other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1241                                       pipe);
1242                 if (ret) {
1243                         fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1244                         goto err_rmfb;
1245                 }
1246                 gettimeofday(&pipe->start, NULL);
1247                 pipe->swap_count = 0;
1248                 pipe->fb_id[0] = dev->mode.fb_id;
1249                 pipe->fb_id[1] = other_fb_id;
1250                 pipe->current_fb_id = other_fb_id;
1251         }
1252
1253         memset(&evctx, 0, sizeof evctx);
1254         evctx.version = DRM_EVENT_CONTEXT_VERSION;
1255         evctx.vblank_handler = NULL;
1256         evctx.page_flip_handler = page_flip_handler;
1257
1258         while (1) {
1259 #if 0
1260                 struct pollfd pfd[2];
1261
1262                 pfd[0].fd = 0;
1263                 pfd[0].events = POLLIN;
1264                 pfd[1].fd = fd;
1265                 pfd[1].events = POLLIN;
1266
1267                 if (poll(pfd, 2, -1) < 0) {
1268                         fprintf(stderr, "poll error\n");
1269                         break;
1270                 }
1271
1272                 if (pfd[0].revents)
1273                         break;
1274 #else
1275                 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1276                 fd_set fds;
1277
1278                 FD_ZERO(&fds);
1279                 FD_SET(0, &fds);
1280                 FD_SET(dev->fd, &fds);
1281                 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1282
1283                 if (ret <= 0) {
1284                         fprintf(stderr, "select timed out or error (ret %d)\n",
1285                                 ret);
1286                         continue;
1287                 } else if (FD_ISSET(0, &fds)) {
1288                         break;
1289                 }
1290 #endif
1291
1292                 drmHandleEvent(dev->fd, &evctx);
1293         }
1294
1295 err_rmfb:
1296         drmModeRmFB(dev->fd, other_fb_id);
1297 err:
1298         bo_destroy(other_bo);
1299 }
1300
1301 #define min(a, b)       ((a) < (b) ? (a) : (b))
1302
1303 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1304 {
1305         unsigned int len;
1306         unsigned int i;
1307         const char *p;
1308         char *endp;
1309
1310         pipe->vrefresh = 0;
1311         pipe->crtc_id = (uint32_t)-1;
1312         strcpy(pipe->format_str, "XR24");
1313
1314         /* Count the number of connectors and allocate them. */
1315         pipe->num_cons = 1;
1316         for (p = arg; *p && *p != ':' && *p != '@'; ++p) {
1317                 if (*p == ',')
1318                         pipe->num_cons++;
1319         }
1320
1321         pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids));
1322         pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons));
1323         if (pipe->con_ids == NULL || pipe->cons == NULL)
1324                 return -1;
1325
1326         /* Parse the connectors. */
1327         for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1328                 endp = strpbrk(p, ",@:");
1329                 if (!endp)
1330                         break;
1331
1332                 pipe->cons[i] = strndup(p, endp - p);
1333
1334                 if (*endp != ',')
1335                         break;
1336         }
1337
1338         if (i != pipe->num_cons - 1)
1339                 return -1;
1340
1341         /* Parse the remaining parameters. */
1342         if (*endp == '@') {
1343                 arg = endp + 1;
1344                 pipe->crtc_id = strtoul(arg, &endp, 10);
1345         }
1346         if (*endp != ':')
1347                 return -1;
1348
1349         arg = endp + 1;
1350
1351         /* Search for the vertical refresh or the format. */
1352         p = strpbrk(arg, "-@");
1353         if (p == NULL)
1354                 p = arg + strlen(arg);
1355         len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1356         strncpy(pipe->mode_str, arg, len);
1357         pipe->mode_str[len] = '\0';
1358
1359         if (*p == '-') {
1360                 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1361                 p = endp;
1362         }
1363
1364         if (*p == '@') {
1365                 strncpy(pipe->format_str, p + 1, 4);
1366                 pipe->format_str[4] = '\0';
1367         }
1368
1369         pipe->fourcc = util_format_fourcc(pipe->format_str);
1370         if (pipe->fourcc == 0)  {
1371                 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1372                 return -1;
1373         }
1374
1375         return 0;
1376 }
1377
1378 static int parse_plane(struct plane_arg *plane, const char *p)
1379 {
1380         char *end;
1381
1382         plane->plane_id = strtoul(p, &end, 10);
1383         if (*end != '@')
1384                 return -EINVAL;
1385
1386         p = end + 1;
1387         plane->crtc_id = strtoul(p, &end, 10);
1388         if (*end != ':')
1389                 return -EINVAL;
1390
1391         p = end + 1;
1392         plane->w = strtoul(p, &end, 10);
1393         if (*end != 'x')
1394                 return -EINVAL;
1395
1396         p = end + 1;
1397         plane->h = strtoul(p, &end, 10);
1398
1399         if (*end == '+' || *end == '-') {
1400                 plane->x = strtol(end, &end, 10);
1401                 if (*end != '+' && *end != '-')
1402                         return -EINVAL;
1403                 plane->y = strtol(end, &end, 10);
1404
1405                 plane->has_position = true;
1406         }
1407
1408         if (*end == '*') {
1409                 p = end + 1;
1410                 plane->scale = strtod(p, &end);
1411                 if (plane->scale <= 0.0)
1412                         return -EINVAL;
1413         } else {
1414                 plane->scale = 1.0;
1415         }
1416
1417         if (*end == '@') {
1418                 p = end + 1;
1419                 if (strlen(p) != 4)
1420                         return -EINVAL;
1421
1422                 strcpy(plane->format_str, p);
1423         } else {
1424                 strcpy(plane->format_str, "XR24");
1425         }
1426
1427         plane->fourcc = util_format_fourcc(plane->format_str);
1428         if (plane->fourcc == 0) {
1429                 fprintf(stderr, "unknown format %s\n", plane->format_str);
1430                 return -EINVAL;
1431         }
1432
1433         return 0;
1434 }
1435
1436 static int parse_property(struct property_arg *p, const char *arg)
1437 {
1438         if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1439                 return -1;
1440
1441         p->obj_type = 0;
1442         p->name[DRM_PROP_NAME_LEN] = '\0';
1443
1444         return 0;
1445 }
1446
1447 static void usage(char *name)
1448 {
1449         fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1450
1451         fprintf(stderr, "\n Query options:\n\n");
1452         fprintf(stderr, "\t-c\tlist connectors\n");
1453         fprintf(stderr, "\t-e\tlist encoders\n");
1454         fprintf(stderr, "\t-f\tlist framebuffers\n");
1455         fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1456
1457         fprintf(stderr, "\n Test options:\n\n");
1458         fprintf(stderr, "\t-P <plane_id>@<crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1459         fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1460         fprintf(stderr, "\t-C\ttest hw cursor\n");
1461         fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1462         fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1463
1464         fprintf(stderr, "\n Generic options:\n\n");
1465         fprintf(stderr, "\t-d\tdrop master after mode set\n");
1466         fprintf(stderr, "\t-M module\tuse the given driver\n");
1467         fprintf(stderr, "\t-D device\tuse the given device\n");
1468
1469         fprintf(stderr, "\n\tDefault is to dump all info.\n");
1470         exit(0);
1471 }
1472
1473 static int page_flipping_supported(void)
1474 {
1475         /*FIXME: generic ioctl needed? */
1476         return 1;
1477 #if 0
1478         int ret, value;
1479         struct drm_i915_getparam gp;
1480
1481         gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1482         gp.value = &value;
1483
1484         ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1485         if (ret) {
1486                 fprintf(stderr, "drm_i915_getparam: %m\n");
1487                 return 0;
1488         }
1489
1490         return *gp.value;
1491 #endif
1492 }
1493
1494 static int cursor_supported(void)
1495 {
1496         /*FIXME: generic ioctl needed? */
1497         return 1;
1498 }
1499
1500 static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe)
1501 {
1502         drmModeConnector *connector;
1503         unsigned int i;
1504         uint32_t id;
1505         char *endp;
1506
1507         for (i = 0; i < pipe->num_cons; i++) {
1508                 id = strtoul(pipe->cons[i], &endp, 10);
1509                 if (endp == pipe->cons[i]) {
1510                         connector = get_connector_by_name(dev, pipe->cons[i]);
1511                         if (!connector) {
1512                                 fprintf(stderr, "no connector named '%s'\n",
1513                                         pipe->cons[i]);
1514                                 return -ENODEV;
1515                         }
1516
1517                         id = connector->connector_id;
1518                 }
1519
1520                 pipe->con_ids[i] = id;
1521         }
1522
1523         return 0;
1524 }
1525
1526 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1527
1528 int main(int argc, char **argv)
1529 {
1530         struct device dev;
1531
1532         int c;
1533         int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1534         int drop_master = 0;
1535         int test_vsync = 0;
1536         int test_cursor = 0;
1537         char *device = NULL;
1538         char *module = NULL;
1539         unsigned int i;
1540         unsigned int count = 0, plane_count = 0;
1541         unsigned int prop_count = 0;
1542         struct pipe_arg *pipe_args = NULL;
1543         struct plane_arg *plane_args = NULL;
1544         struct property_arg *prop_args = NULL;
1545         unsigned int args = 0;
1546         int ret;
1547
1548         memset(&dev, 0, sizeof dev);
1549
1550         opterr = 0;
1551         while ((c = getopt(argc, argv, optstr)) != -1) {
1552                 args++;
1553
1554                 switch (c) {
1555                 case 'c':
1556                         connectors = 1;
1557                         break;
1558                 case 'D':
1559                         device = optarg;
1560                         args--;
1561                         break;
1562                 case 'd':
1563                         drop_master = 1;
1564                         break;
1565                 case 'e':
1566                         encoders = 1;
1567                         break;
1568                 case 'f':
1569                         framebuffers = 1;
1570                         break;
1571                 case 'M':
1572                         module = optarg;
1573                         /* Preserve the default behaviour of dumping all information. */
1574                         args--;
1575                         break;
1576                 case 'P':
1577                         plane_args = realloc(plane_args,
1578                                              (plane_count + 1) * sizeof *plane_args);
1579                         if (plane_args == NULL) {
1580                                 fprintf(stderr, "memory allocation failed\n");
1581                                 return 1;
1582                         }
1583                         memset(&plane_args[plane_count], 0, sizeof(*plane_args));
1584
1585                         if (parse_plane(&plane_args[plane_count], optarg) < 0)
1586                                 usage(argv[0]);
1587
1588                         plane_count++;
1589                         break;
1590                 case 'p':
1591                         crtcs = 1;
1592                         planes = 1;
1593                         break;
1594                 case 's':
1595                         pipe_args = realloc(pipe_args,
1596                                             (count + 1) * sizeof *pipe_args);
1597                         if (pipe_args == NULL) {
1598                                 fprintf(stderr, "memory allocation failed\n");
1599                                 return 1;
1600                         }
1601                         memset(&pipe_args[count], 0, sizeof(*pipe_args));
1602
1603                         if (parse_connector(&pipe_args[count], optarg) < 0)
1604                                 usage(argv[0]);
1605
1606                         count++;
1607                         break;
1608                 case 'C':
1609                         test_cursor = 1;
1610                         break;
1611                 case 'v':
1612                         test_vsync = 1;
1613                         break;
1614                 case 'w':
1615                         prop_args = realloc(prop_args,
1616                                            (prop_count + 1) * sizeof *prop_args);
1617                         if (prop_args == NULL) {
1618                                 fprintf(stderr, "memory allocation failed\n");
1619                                 return 1;
1620                         }
1621                         memset(&prop_args[prop_count], 0, sizeof(*prop_args));
1622
1623                         if (parse_property(&prop_args[prop_count], optarg) < 0)
1624                                 usage(argv[0]);
1625
1626                         prop_count++;
1627                         break;
1628                 default:
1629                         usage(argv[0]);
1630                         break;
1631                 }
1632         }
1633
1634         if (!args)
1635                 encoders = connectors = crtcs = planes = framebuffers = 1;
1636
1637         dev.fd = util_open(device, module);
1638         if (dev.fd < 0)
1639                 return -1;
1640
1641         if (test_vsync && !page_flipping_supported()) {
1642                 fprintf(stderr, "page flipping not supported by drm.\n");
1643                 return -1;
1644         }
1645
1646         if (test_vsync && !count) {
1647                 fprintf(stderr, "page flipping requires at least one -s option.\n");
1648                 return -1;
1649         }
1650
1651         if (test_cursor && !cursor_supported()) {
1652                 fprintf(stderr, "hw cursor not supported by drm.\n");
1653                 return -1;
1654         }
1655
1656         dev.resources = get_resources(&dev);
1657         if (!dev.resources) {
1658                 drmClose(dev.fd);
1659                 return 1;
1660         }
1661
1662         for (i = 0; i < count; i++) {
1663                 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) {
1664                         free_resources(dev.resources);
1665                         drmClose(dev.fd);
1666                         return 1;
1667                 }
1668         }
1669
1670 #define dump_resource(dev, res) if (res) dump_##res(dev)
1671
1672         dump_resource(&dev, encoders);
1673         dump_resource(&dev, connectors);
1674         dump_resource(&dev, crtcs);
1675         dump_resource(&dev, planes);
1676         dump_resource(&dev, framebuffers);
1677
1678         for (i = 0; i < prop_count; ++i)
1679                 set_property(&dev, &prop_args[i]);
1680
1681         if (count || plane_count) {
1682                 uint64_t cap = 0;
1683
1684                 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
1685                 if (ret || cap == 0) {
1686                         fprintf(stderr, "driver doesn't support the dumb buffer API\n");
1687                         return 1;
1688                 }
1689
1690                 if (count)
1691                         set_mode(&dev, pipe_args, count);
1692
1693                 if (plane_count)
1694                         set_planes(&dev, plane_args, plane_count);
1695
1696                 if (test_cursor)
1697                         set_cursors(&dev, pipe_args, count);
1698
1699                 if (test_vsync)
1700                         test_page_flip(&dev, pipe_args, count);
1701
1702                 if (drop_master)
1703                         drmDropMaster(dev.fd);
1704
1705                 getchar();
1706
1707                 if (test_cursor)
1708                         clear_cursors(&dev);
1709
1710                 if (plane_count)
1711                         clear_planes(&dev, plane_args, plane_count);
1712
1713                 if (count)
1714                         clear_mode(&dev);
1715         }
1716
1717         free_resources(dev.resources);
1718
1719         return 0;
1720 }