c396da418f4e24db2acd5780b0856b06b25174da
[profile/ivi/libdrm.git] / tests / mode / modetest.c
1
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6
7 #include "xf86drm.h"
8 #include "xf86drmMode.h"
9
10 const char* getConnectionText(drmModeConnection conn)
11 {
12         switch (conn) {
13         case DRM_MODE_CONNECTED:
14                 return "connected";
15         case DRM_MODE_DISCONNECTED:
16                 return "disconnected";
17         default:
18                 return "unknown";
19         }
20
21 }
22
23 int printMode(struct drm_mode_modeinfo *mode)
24 {
25 #if 1
26         printf("Mode: %s\n", mode->name);
27         printf("\tclock       : %i\n", mode->clock);
28         printf("\thdisplay    : %i\n", mode->hdisplay);
29         printf("\thsync_start : %i\n", mode->hsync_start);
30         printf("\thsync_end   : %i\n", mode->hsync_end);
31         printf("\thtotal      : %i\n", mode->htotal);
32         printf("\thskew       : %i\n", mode->hskew);
33         printf("\tvdisplay    : %i\n", mode->vdisplay);
34         printf("\tvsync_start : %i\n", mode->vsync_start);
35         printf("\tvsync_end   : %i\n", mode->vsync_end);
36         printf("\tvtotal      : %i\n", mode->vtotal);
37         printf("\tvscan       : %i\n", mode->vscan);
38         printf("\tvrefresh    : %i\n", mode->vrefresh);
39         printf("\tflags       : %i\n", mode->flags);
40 #else
41         printf("Mode: \"%s\" %ix%i %.0f\n", mode->name,
42                 mode->hdisplay, mode->vdisplay, mode->vrefresh / 1000.0);
43 #endif
44         return 0;
45 }
46
47 int printOutput(int fd, drmModeResPtr res, drmModeOutputPtr output, uint32_t id)
48 {
49         int i = 0, j;
50         struct drm_mode_modeinfo *mode = NULL;
51         drmModePropertyPtr props;
52         unsigned char *name = NULL;
53
54         printf("Output: %s\n", output->name);
55         printf("\tid           : %i\n", id);
56         printf("\tcrtc id      : %i\n", output->crtc);
57         printf("\tconn         : %s\n", getConnectionText(output->connection));
58         printf("\tsize         : %ix%i (mm)\n", output->mmWidth, output->mmHeight);
59         printf("\tcount_crtcs  : %i\n", output->count_crtcs);
60         printf("\tcrtcs        : %i\n", output->crtcs);
61         printf("\tcount_clones : %i\n", output->count_clones);
62         printf("\tclones       : %i\n", output->clones);
63         printf("\tcount_modes  : %i\n", output->count_modes);
64         printf("\tcount_props  : %i\n", output->count_props);
65
66         for (i = 0; i < output->count_props; i++) {
67                 props = drmModeGetProperty(fd, output->props[i]);
68                 name = NULL;
69                 if (props) {
70                         printf("Property: %s\n", props->name);
71                         printf("\tid:        %i\n", props->prop_id);
72                         printf("\tflags:     %i\n", props->flags);
73                         printf("\tvalues %d: ", props->count_values);
74                         for (j = 0; j < props->count_values; j++)
75                                 printf("%d ", props->values[j]);
76
77                         printf("\n\tenums %d: \n", props->count_enums);
78                         
79                         if (props->flags & DRM_MODE_PROP_BLOB) {
80                                 drmModePropertyBlobPtr blob;
81
82                                 blob = drmModeGetPropertyBlob(fd, output->prop_values[i]);
83
84                                 printf("blob is %d length, %08X\n", blob->length, *(uint32_t *)blob->data);
85                                 drmModeFreePropertyBlob(blob);
86
87                         } else {
88                                 for (j = 0; j < props->count_enums; j++) {
89                                         if (output->prop_values[i] == props->enums[j].value)
90                                                 name = props->enums[j].name;
91                                         printf("\t\t%d = %s\n", props->enums[j].value, props->enums[j].name);
92                                 }
93
94                                 if (props->count_enums && name) {
95                                         printf("\toutput property name %s %s\n", props->name, name);
96                                 } else {
97                                         printf("\toutput property id %s %i\n", props->name, output->prop_values[i]);
98                                 }
99                         }
100
101                         drmModeFreeProperty(props);
102                 }
103         }
104
105         for (i = 0; i < output->count_modes; i++) {
106                 mode = &output->modes[i];
107                 if (mode)
108                         printMode(mode);
109                 else
110                         printf("\t\tmode: Invalid mode %i\n", output->modes[i]);
111         }
112
113         return 0;
114 }
115
116 int printCrtc(int fd, drmModeResPtr res, drmModeCrtcPtr crtc, uint32_t id)
117 {
118         printf("Crtc\n");
119         printf("\tid           : %i\n", id);
120         printf("\tx            : %i\n", crtc->x);
121         printf("\ty            : %i\n", crtc->y);
122         printf("\twidth        : %i\n", crtc->width);
123         printf("\theight       : %i\n", crtc->height);
124         printf("\tmode         : %i\n", crtc->mode);
125         printf("\tnum outputs  : %i\n", crtc->count_outputs);
126         printf("\toutputs      : %i\n", crtc->outputs);
127         printf("\tnum possible : %i\n", crtc->count_possibles);
128         printf("\tpossibles    : %i\n", crtc->possibles);
129
130         return 0;
131 }
132
133 int printFrameBuffer(int fd, drmModeResPtr res, drmModeFBPtr fb)
134 {
135         printf("Framebuffer\n");
136         printf("\thandle    : %i\n", fb->handle);
137         printf("\twidth     : %i\n", fb->width);
138         printf("\theight    : %i\n", fb->height);
139         printf("\tpitch     : %i\n", fb->pitch);;
140         printf("\tbpp       : %i\n", fb->bpp);
141         printf("\tdepth     : %i\n", fb->depth);
142         printf("\tbuffer_id : %i\n", fb->buffer_id);
143
144         return 0;
145 }
146
147 int printRes(int fd, drmModeResPtr res)
148 {
149         int i;
150         drmModeOutputPtr output;
151         drmModeCrtcPtr crtc;
152         drmModeFBPtr fb;
153
154         for (i = 0; i < res->count_outputs; i++) {
155                 output = drmModeGetOutput(fd, res->outputs[i]);
156
157                 if (!output)
158                         printf("Could not get output %i\n", i);
159                 else {
160                         printOutput(fd, res, output, res->outputs[i]);
161                         drmModeFreeOutput(output);
162                 }
163         }
164
165         for (i = 0; i < res->count_crtcs; i++) {
166                 crtc = drmModeGetCrtc(fd, res->crtcs[i]);
167
168                 if (!crtc)
169                         printf("Could not get crtc %i\n", i);
170                 else {
171                         printCrtc(fd, res, crtc, res->crtcs[i]);
172                         drmModeFreeCrtc(crtc);
173                 }
174         }
175
176         for (i = 0; i < res->count_fbs; i++) {
177                 fb = drmModeGetFB(fd, res->fbs[i]);
178
179                 if (!fb)
180                         printf("Could not get fb %i\n", res->fbs[i]);
181                 else {
182                         printFrameBuffer(fd, res, fb);
183                         drmModeFreeFB(fb);
184                 }
185         }
186
187         return 0;
188 }
189
190 static struct drm_mode_modeinfo mode = {
191         .name = "Test mode",
192         .clock = 25200,
193         .hdisplay = 640,
194         .hsync_start = 656,
195         .hsync_end = 752,
196         .htotal = 800,
197         .hskew = 0,
198         .vdisplay = 480,
199         .vsync_start = 490,
200         .vsync_end = 492,
201         .vtotal = 525,
202         .vscan = 0,
203         .vrefresh = 60000, /* vertical refresh * 1000 */
204         .flags = 10,
205 };
206
207 int testMode(int fd, drmModeResPtr res)
208 {
209         uint32_t output = res->outputs[0];
210         uint32_t newMode = 0;
211         int ret = 0;
212         int error = 0;
213
214         printf("Test: adding mode to output %i\n", output);
215
216         /* printMode(&mode); */
217
218         printf("\tAttaching mode %i to output %i\n", newMode, output);
219
220         ret = drmModeAttachMode(fd, output, &mode);
221
222         if (ret)
223                 goto err_mode;
224
225         printf("\tDetaching mode %i from output %i\n", newMode, output);
226         ret = drmModeDetachMode(fd, output, &mode);
227
228         if (ret)
229                 goto err_mode;
230         return 0;
231
232 err_mode:
233
234         printf("\tFailed\n");
235
236         if (error)
237                 printf("\tFailed to delete mode %i\n", newMode);
238         return 1;
239 }
240
241 /*
242 int testFrameBufferGet(int fd, uint32_t fb)
243 {
244         drmModeFBPtr frame;
245
246         printf("Test: get framebuffer %i\n", fb);
247
248         frame = drmModeGetFB(fd, fb);
249
250         if (!frame) {
251                 printf("\tFailed\n");
252         } else {
253                 printFrameBuffer(fd, frame);
254                 drmModeFreeFB(frame);
255         }
256
257         return 0;
258 }
259 */
260
261 int testFrameBufferAdd(int fd, drmModeResPtr res)
262 {
263         uint32_t fb = 0;
264         int ret = 0;
265         drmModeFBPtr frame = 0;
266         drmBO bo;
267
268         printf("Test: adding framebuffer\n");
269
270         printf("\tCreating BO\n");
271
272         /* TODO */
273         ret = 1;
274         if (ret)
275                 goto err;
276
277         printf("\tAdding FB\n");
278         ret = drmModeAddFB(fd, 640, 480, 32, 8, 0, &bo, &fb);
279         if (ret)
280                 goto err_bo;
281
282         frame = drmModeGetFB(fd, fb);
283
284         if (!frame) {
285                 printf("Couldn't retrive created framebuffer\n");
286         } else {
287                 printFrameBuffer(fd, res, frame);
288                 drmModeFreeFB(frame);
289         }
290
291         printf("\tRemoveing FB\n");
292
293         ret = drmModeRmFB(fd, fb);
294
295         if (ret) {
296                 printf("\tFailed this shouldn't happen!\n");
297                 goto err_bo;
298         }
299
300         printf("\tRemoveing BO\n");
301
302         ret = drmBOUnreference(fb, &bo);
303
304         return 0;
305         
306 err_bo:
307         drmBOUnreference(fd, &bo);
308
309 err:
310         printf("\tFailed\n");
311
312         return 1;
313 }
314
315
316 int main(int argc, char **argv)
317 {
318         int fd;
319         const char *driver = "i915"; /* hardcoded for now */
320         drmModeResPtr res;
321
322         printf("Starting test\n");
323
324         fd = drmOpen(driver, NULL);
325
326         if (fd < 0) {
327                 printf("Failed to open the card fb\n");
328                 return 1;
329         }
330
331         res = drmModeGetResources(fd);
332         if (res == 0) {
333                 printf("Failed to get resources from card\n");
334                 drmClose(fd);
335                 return 1;
336         }
337
338         printRes(fd, res);
339
340         testMode(fd, res);
341
342         testFrameBufferAdd(fd, res);
343
344         drmModeFreeResources(res);
345         printf("Ok\n");
346
347         return 0;
348 }