HS Files added for IVI ARM release
[adaptation/panda/libdrm.git] / tests / modeprint / modeprint.c
1 /*
2  * \file modedemo.c
3  * Test program to dump DRM kernel mode setting related information.
4  * Queries the kernel for all available information and dumps it to stdout.
5  *
6  * \author Jakob Bornecrantz <wallbraker@gmail.com>
7  */
8
9 /*
10  * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
11  * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  * IN THE SOFTWARE.
30  *
31  */
32
33 #include <assert.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <inttypes.h>
40
41 #include "xf86drm.h"
42 #include "xf86drmMode.h"
43
44 int connectors;
45 int full_props;
46 int edid;
47 int modes;
48 int full_modes;
49 int encoders;
50 int crtcs;
51 int fbs;
52 char *module_name;
53
54 const char* getConnectionText(drmModeConnection conn)
55 {
56         switch (conn) {
57         case DRM_MODE_CONNECTED:
58                 return "connected";
59         case DRM_MODE_DISCONNECTED:
60                 return "disconnected";
61         default:
62                 return "unknown";
63         }
64
65 }
66
67 int printMode(struct drm_mode_modeinfo *mode)
68 {
69         if (full_modes) {
70                 printf("Mode: %s\n", mode->name);
71                 printf("\tclock       : %i\n", mode->clock);
72                 printf("\thdisplay    : %i\n", mode->hdisplay);
73                 printf("\thsync_start : %i\n", mode->hsync_start);
74                 printf("\thsync_end   : %i\n", mode->hsync_end);
75                 printf("\thtotal      : %i\n", mode->htotal);
76                 printf("\thskew       : %i\n", mode->hskew);
77                 printf("\tvdisplay    : %i\n", mode->vdisplay);
78                 printf("\tvsync_start : %i\n", mode->vsync_start);
79                 printf("\tvsync_end   : %i\n", mode->vsync_end);
80                 printf("\tvtotal      : %i\n", mode->vtotal);
81                 printf("\tvscan       : %i\n", mode->vscan);
82                 printf("\tvrefresh    : %i\n", mode->vrefresh);
83                 printf("\tflags       : %i\n", mode->flags);
84         } else {
85                 printf("Mode: \"%s\" %ix%i %i\n", mode->name,
86                                 mode->hdisplay, mode->vdisplay, mode->vrefresh);
87         }
88         return 0;
89 }
90
91 int printProperty(int fd, drmModeResPtr res, drmModePropertyPtr props, uint64_t value)
92 {
93         const char *name = NULL;
94         int j;
95
96         printf("Property: %s\n", props->name);
97         printf("\tid           : %i\n", props->prop_id);
98         printf("\tflags        : %i\n", props->flags);
99         printf("\tcount_values : %d\n", props->count_values);
100
101
102         if (props->count_values) {
103                 printf("\tvalues       :");
104                 for (j = 0; j < props->count_values; j++)
105                         printf(" %" PRIu64, props->values[j]);
106                 printf("\n");
107         }
108
109
110         printf("\tcount_enums  : %d\n", props->count_enums);
111
112         if (props->flags & DRM_MODE_PROP_BLOB) {
113                 drmModePropertyBlobPtr blob;
114
115                 blob = drmModeGetPropertyBlob(fd, value);
116                 if (blob) {
117                         printf("blob is %d length, %08X\n", blob->length, *(uint32_t *)blob->data);
118                         drmModeFreePropertyBlob(blob);
119                 } else {
120                         printf("error getting blob %" PRIu64 "\n", value);
121                 }
122
123         } else {
124                 if (!strncmp(props->name, "DPMS", 4))
125                         ;
126
127                 for (j = 0; j < props->count_enums; j++) {
128                         printf("\t\t%lld = %s\n", props->enums[j].value, props->enums[j].name);
129                         if (props->enums[j].value == value)
130                                 name = props->enums[j].name;
131                 }
132
133                 if (props->count_enums && name) {
134                         printf("\tcon_value    : %s\n", name);
135                 } else {
136                         printf("\tcon_value    : %" PRIu64 "\n", value);
137                 }
138         }
139
140         return 0;
141 }
142
143 int printConnector(int fd, drmModeResPtr res, drmModeConnectorPtr connector, uint32_t id)
144 {
145         int i = 0;
146         struct drm_mode_modeinfo *mode = NULL;
147         drmModePropertyPtr props;
148
149         printf("Connector: %d-%d\n", connector->connector_type, connector->connector_type_id);
150         printf("\tid             : %i\n", id);
151         printf("\tencoder id     : %i\n", connector->encoder_id);
152         printf("\tconn           : %s\n", getConnectionText(connector->connection));
153         printf("\tsize           : %ix%i (mm)\n", connector->mmWidth, connector->mmHeight);
154         printf("\tcount_modes    : %i\n", connector->count_modes);
155         printf("\tcount_props    : %i\n", connector->count_props);
156         if (connector->count_props) {
157                 printf("\tprops          :");
158                 for (i = 0; i < connector->count_props; i++)
159                         printf(" %i", connector->props[i]);
160                 printf("\n");
161         }
162
163         printf("\tcount_encoders : %i\n", connector->count_encoders);
164         if (connector->count_encoders) {
165                 printf("\tencoders       :");
166                 for (i = 0; i < connector->count_encoders; i++)
167                         printf(" %i", connector->encoders[i]);
168                 printf("\n");
169         }
170
171         if (modes) {
172                 for (i = 0; i < connector->count_modes; i++) {
173                         mode = (struct drm_mode_modeinfo *)&connector->modes[i];
174                         printMode(mode);
175                 }
176         }
177
178         if (full_props) {
179                 for (i = 0; i < connector->count_props; i++) {
180                         props = drmModeGetProperty(fd, connector->props[i]);
181                         if (props) {
182                                 printProperty(fd, res, props, connector->prop_values[i]);
183                                 drmModeFreeProperty(props);
184                         }
185                 }
186         }
187
188         return 0;
189 }
190
191 int printEncoder(int fd, drmModeResPtr res, drmModeEncoderPtr encoder, uint32_t id)
192 {
193         printf("Encoder\n");
194         printf("\tid     :%i\n", id);
195         printf("\tcrtc_id   :%d\n", encoder->crtc_id);
196         printf("\ttype   :%d\n", encoder->encoder_type);
197         printf("\tpossible_crtcs  :0x%x\n", encoder->possible_crtcs);
198         printf("\tpossible_clones :0x%x\n", encoder->possible_clones);
199         return 0;
200 }
201
202 int printCrtc(int fd, drmModeResPtr res, drmModeCrtcPtr crtc, uint32_t id)
203 {
204         printf("Crtc\n");
205         printf("\tid             : %i\n", id);
206         printf("\tx              : %i\n", crtc->x);
207         printf("\ty              : %i\n", crtc->y);
208         printf("\twidth          : %i\n", crtc->width);
209         printf("\theight         : %i\n", crtc->height);
210         printf("\tmode           : %p\n", &crtc->mode);
211         printf("\tgamma size     : %d\n", crtc->gamma_size);
212
213         return 0;
214 }
215
216 int printFrameBuffer(int fd, drmModeResPtr res, drmModeFBPtr fb)
217 {
218         printf("Framebuffer\n");
219         printf("\thandle    : %i\n", fb->handle);
220         printf("\twidth     : %i\n", fb->width);
221         printf("\theight    : %i\n", fb->height);
222         printf("\tpitch     : %i\n", fb->pitch);;
223         printf("\tbpp       : %i\n", fb->bpp);
224         printf("\tdepth     : %i\n", fb->depth);
225         printf("\tbuffer_id : %i\n", fb->handle);
226
227         return 0;
228 }
229
230 int printRes(int fd, drmModeResPtr res)
231 {
232         int i;
233         drmModeFBPtr fb;
234         drmModeCrtcPtr crtc;
235         drmModeEncoderPtr encoder;
236         drmModeConnectorPtr connector;
237
238         printf("Resources\n\n");
239
240         printf("count_connectors : %i\n", res->count_connectors);
241         printf("count_encoders   : %i\n", res->count_encoders);
242         printf("count_crtcs      : %i\n", res->count_crtcs);
243         printf("count_fbs        : %i\n", res->count_fbs);
244
245         printf("\n");
246
247         if (connectors) {
248                 for (i = 0; i < res->count_connectors; i++) {
249                         connector = drmModeGetConnector(fd, res->connectors[i]);
250
251                         if (!connector)
252                                 printf("Could not get connector %i\n", res->connectors[i]);
253                         else {
254                                 printConnector(fd, res, connector, res->connectors[i]);
255                                 drmModeFreeConnector(connector);
256                         }
257                 }
258                 printf("\n");
259         }
260
261
262         if (encoders) {
263                 for (i = 0; i < res->count_encoders; i++) {
264                         encoder = drmModeGetEncoder(fd, res->encoders[i]);
265
266                         if (!encoder)
267                                 printf("Could not get encoder %i\n", res->encoders[i]);
268                         else {
269                                 printEncoder(fd, res, encoder, res->encoders[i]);
270                                 drmModeFreeEncoder(encoder);
271                         }
272                 }
273                 printf("\n");
274         }
275
276         if (crtcs) {
277                 for (i = 0; i < res->count_crtcs; i++) {
278                         crtc = drmModeGetCrtc(fd, res->crtcs[i]);
279
280                         if (!crtc)
281                                 printf("Could not get crtc %i\n", res->crtcs[i]);
282                         else {
283                                 printCrtc(fd, res, crtc, res->crtcs[i]);
284                                 drmModeFreeCrtc(crtc);
285                         }
286                 }
287                 printf("\n");
288         }
289
290         if (fbs) {
291                 for (i = 0; i < res->count_fbs; i++) {
292                         fb = drmModeGetFB(fd, res->fbs[i]);
293
294                         if (!fb)
295                                 printf("Could not get fb %i\n", res->fbs[i]);
296                         else {
297                                 printFrameBuffer(fd, res, fb);
298                                 drmModeFreeFB(fb);
299                         }
300                 }
301         }
302
303         return 0;
304 }
305
306 void args(int argc, char **argv)
307 {
308         int i;
309
310         fbs = 0;
311         edid = 0;
312         crtcs = 0;
313         modes = 0;
314         encoders = 0;
315         full_modes = 0;
316         full_props = 0;
317         connectors = 0;
318
319         module_name = argv[1];
320
321         for (i = 2; i < argc; i++) {
322                 if (strcmp(argv[i], "-fb") == 0) {
323                         fbs = 1;
324                 } else if (strcmp(argv[i], "-crtcs") == 0) {
325                         crtcs = 1;
326                 } else if (strcmp(argv[i], "-cons") == 0) {
327                         connectors = 1;
328                         modes = 1;
329                 } else if (strcmp(argv[i], "-modes") == 0) {
330                         connectors = 1;
331                         modes = 1;
332                 } else if (strcmp(argv[i], "-full") == 0) {
333                         connectors = 1;
334                         modes = 1;
335                         full_modes = 1;
336                 } else if (strcmp(argv[i], "-props") == 0) {
337                         connectors = 1;
338                         full_props = 1;
339                 } else if (strcmp(argv[i], "-edids") == 0) {
340                         connectors = 1;
341                         edid = 1;
342                 } else if (strcmp(argv[i], "-encoders") == 0) {
343                         encoders = 1;
344                 } else if (strcmp(argv[i], "-v") == 0) {
345                         fbs = 1;
346                         edid = 1;
347                         crtcs = 1;
348                         modes = 1;
349                         encoders = 1;
350                         full_modes = 1;
351                         full_props = 1;
352                         connectors = 1;
353                 }
354         }
355
356         if (argc == 2) {
357                 fbs = 1;
358                 edid = 1;
359                 crtcs = 1;
360                 modes = 1;
361                 encoders = 1;
362                 full_modes = 0;
363                 full_props = 0;
364                 connectors = 1;
365         }
366 }
367
368 int main(int argc, char **argv)
369 {
370         int fd;
371         drmModeResPtr res;
372
373         if (argc == 1) {
374                 printf("Please add modulename as first argument\n");
375                 return 1;
376         }
377
378         args(argc, argv);
379
380         printf("Starting test\n");
381
382         fd = drmOpen(module_name, NULL);
383
384         if (fd < 0) {
385                 printf("Failed to open the card fd (%d)\n",fd);
386                 return 1;
387         }
388
389         res = drmModeGetResources(fd);
390         if (res == 0) {
391                 printf("Failed to get resources from card\n");
392                 drmClose(fd);
393                 return 1;
394         }
395
396         printRes(fd, res);
397
398         drmModeFreeResources(res);
399
400         printf("Ok\n");
401
402         return 0;
403 }