OSDN Git Service

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