summaryrefslogtreecommitdiff
path: root/tests/mode/modetest.c
blob: 1d5002b095c35996e34b90cc20443f0bb8402c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include "xf86drm.h"
#include "xf86drmMode.h"

const char* getConnectionText(drmModeConnection conn)
{
	switch (conn) {
	case DRM_MODE_CONNECTED:
		return "connected";
	case DRM_MODE_DISCONNECTED:
		return "disconnected";
	default:
		return "unknown";
	}

}

int printMode(struct drm_mode_modeinfo *mode)
{
#if 1
	printf("Mode: %s\n", mode->name);
	printf("\tclock       : %i\n", mode->clock);
	printf("\thdisplay    : %i\n", mode->hdisplay);
	printf("\thsync_start : %i\n", mode->hsync_start);
	printf("\thsync_end   : %i\n", mode->hsync_end);
	printf("\thtotal      : %i\n", mode->htotal);
	printf("\thskew       : %i\n", mode->hskew);
	printf("\tvdisplay    : %i\n", mode->vdisplay);
	printf("\tvsync_start : %i\n", mode->vsync_start);
	printf("\tvsync_end   : %i\n", mode->vsync_end);
	printf("\tvtotal      : %i\n", mode->vtotal);
	printf("\tvscan       : %i\n", mode->vscan);
	printf("\tvrefresh    : %i\n", mode->vrefresh);
	printf("\tflags       : %i\n", mode->flags);
#else
	printf("Mode: \"%s\" %ix%i %.0f\n", mode->name,
		mode->hdisplay, mode->vdisplay, mode->vrefresh / 1000.0);
#endif
	return 0;
}

int printOutput(int fd, drmModeResPtr res, drmModeOutputPtr output, uint32_t id)
{
	int i = 0, j;
	struct drm_mode_modeinfo *mode = NULL;
	drmModePropertyPtr props;
	unsigned char *name = NULL;

	printf("Output: %s\n", output->name);
	printf("\tid           : %i\n", id);
	printf("\tcrtc id      : %i\n", output->crtc);
	printf("\tconn         : %s\n", getConnectionText(output->connection));
	printf("\tsize         : %ix%i (mm)\n", output->mmWidth, output->mmHeight);
	printf("\tcount_crtcs  : %i\n", output->count_crtcs);
	printf("\tcrtcs        : %i\n", output->crtcs);
	printf("\tcount_clones : %i\n", output->count_clones);
	printf("\tclones       : %i\n", output->clones);
	printf("\tcount_modes  : %i\n", output->count_modes);
	printf("\tcount_props  : %i\n", output->count_props);

	for (i = 0; i < output->count_props; i++) {
		props = drmModeGetProperty(fd, output->props[i]);
		name = NULL;
		if (props) {
			printf("Property: %s\n", props->name);
			printf("\tid:        %i\n", props->prop_id);
			printf("\tflags:     %i\n", props->flags);
			printf("\tvalues %d: ", props->count_values);
			for (j = 0; j < props->count_values; j++)
				printf("%d ", props->values[j]);

			printf("\n\tenums %d: \n", props->count_enums);

			for (j = 0; j < props->count_enums; j++) {
				if (output->prop_values[i] == props->enums[j].value)
					name = props->enums[j].name;
				printf("\t\t%d = %s\n", props->enums[j].value, props->enums[j].name);
			}

			if (props->count_enums && name) {
				printf("\toutput property name %s %s\n", props->name, name);
			} else {
				printf("\toutput property id %s %i\n", props->name, output->prop_values[i]);
			}

			drmModeFreeProperty(props);
		}
	}

	for (i = 0; i < output->count_modes; i++) {
		mode = &output->modes[i];
		if (mode)
			printMode(mode);
		else
			printf("\t\tmode: Invalid mode %i\n", output->modes[i]);
	}

	return 0;
}

int printCrtc(int fd, drmModeResPtr res, drmModeCrtcPtr crtc, uint32_t id)
{
	printf("Crtc\n");
	printf("\tid           : %i\n", id);
	printf("\tx            : %i\n", crtc->x);
	printf("\ty            : %i\n", crtc->y);
	printf("\twidth        : %i\n", crtc->width);
	printf("\theight       : %i\n", crtc->height);
	printf("\tmode         : %i\n", crtc->mode);
	printf("\tnum outputs  : %i\n", crtc->count_outputs);
	printf("\toutputs      : %i\n", crtc->outputs);
	printf("\tnum possible : %i\n", crtc->count_possibles);
	printf("\tpossibles    : %i\n", crtc->possibles);

	return 0;
}

int printFrameBuffer(int fd, drmModeResPtr res, drmModeFBPtr fb)
{
	printf("Framebuffer\n");
	printf("\thandle    : %i\n", fb->handle);
	printf("\twidth     : %i\n", fb->width);
	printf("\theight    : %i\n", fb->height);
	printf("\tpitch     : %i\n", fb->pitch);;
	printf("\tbpp       : %i\n", fb->bpp);
	printf("\tdepth     : %i\n", fb->depth);
	printf("\tbuffer_id : %i\n", fb->buffer_id);

	return 0;
}

int printRes(int fd, drmModeResPtr res)
{
	int i;
	drmModeOutputPtr output;
	drmModeCrtcPtr crtc;
	drmModeFBPtr fb;

	for (i = 0; i < res->count_outputs; i++) {
		output = drmModeGetOutput(fd, res->outputs[i]);

		if (!output)
			printf("Could not get output %i\n", i);
		else {
			printOutput(fd, res, output, res->outputs[i]);
			drmModeFreeOutput(output);
		}
	}

	for (i = 0; i < res->count_crtcs; i++) {
		crtc = drmModeGetCrtc(fd, res->crtcs[i]);

		if (!crtc)
			printf("Could not get crtc %i\n", i);
		else {
			printCrtc(fd, res, crtc, res->crtcs[i]);
			drmModeFreeCrtc(crtc);
		}
	}

	for (i = 0; i < res->count_fbs; i++) {
		fb = drmModeGetFB(fd, res->fbs[i]);

		if (!fb)
			printf("Could not get fb %i\n", res->fbs[i]);
		else {
			printFrameBuffer(fd, res, fb);
			drmModeFreeFB(fb);
		}
	}

	return 0;
}

static struct drm_mode_modeinfo mode = {
	.name = "Test mode",
	.clock = 25200,
	.hdisplay = 640,
	.hsync_start = 656,
	.hsync_end = 752,
	.htotal = 800,
	.hskew = 0,
	.vdisplay = 480,
	.vsync_start = 490,
	.vsync_end = 492,
	.vtotal = 525,
	.vscan = 0,
	.vrefresh = 60000, /* vertical refresh * 1000 */
	.flags = 10,
};

int testMode(int fd, drmModeResPtr res)
{
	uint32_t output = res->outputs[0];
	uint32_t newMode = 0;
	int ret = 0;
	int error = 0;

	printf("Test: adding mode to output %i\n", output);

	/* printMode(&mode); */

	printf("\tAttaching mode %i to output %i\n", newMode, output);

	ret = drmModeAttachMode(fd, output, &mode);

	if (ret)
		goto err_mode;

	printf("\tDetaching mode %i from output %i\n", newMode, output);
	ret = drmModeDetachMode(fd, output, &mode);

	if (ret)
		goto err_mode;
	return 0;

err_mode:

	printf("\tFailed\n");

	if (error)
		printf("\tFailed to delete mode %i\n", newMode);
	return 1;
}

/*
int testFrameBufferGet(int fd, uint32_t fb)
{
	drmModeFBPtr frame;

	printf("Test: get framebuffer %i\n", fb);

	frame = drmModeGetFB(fd, fb);

	if (!frame) {
		printf("\tFailed\n");
	} else {
		printFrameBuffer(fd, frame);
		drmModeFreeFB(frame);
	}

	return 0;
}
*/

int testFrameBufferAdd(int fd, drmModeResPtr res)
{
	uint32_t fb = 0;
	int ret = 0;
	drmModeFBPtr frame = 0;
	drmBO bo;

	printf("Test: adding framebuffer\n");

	printf("\tCreating BO\n");

	/* TODO */
	ret = 1;
	if (ret)
		goto err;

	printf("\tAdding FB\n");
	ret = drmModeAddFB(fd, 640, 480, 32, 8, 0, &bo, &fb);
	if (ret)
		goto err_bo;

	frame = drmModeGetFB(fd, fb);

	if (!frame) {
		printf("Couldn't retrive created framebuffer\n");
	} else {
		printFrameBuffer(fd, res, frame);
		drmModeFreeFB(frame);
	}

	printf("\tRemoveing FB\n");

	ret = drmModeRmFB(fd, fb);

	if (ret) {
		printf("\tFailed this shouldn't happen!\n");
		goto err_bo;
	}

	printf("\tRemoveing BO\n");

	ret = drmBOUnreference(fb, &bo);

	return 0;
	
err_bo:
	drmBOUnreference(fd, &bo);

err:
	printf("\tFailed\n");

	return 1;
}


int main(int argc, char **argv)
{
	int fd;
	const char *driver = "i915"; /* hardcoded for now */
	drmModeResPtr res;

	printf("Starting test\n");

	fd = drmOpen(driver, NULL);

	if (fd < 0) {
		printf("Failed to open the card fb\n");
		return 1;
	}

	res = drmModeGetResources(fd);
	if (res == 0) {
		printf("Failed to get resources from card\n");
		drmClose(fd);
		return 1;
	}

	printRes(fd, res);

	testMode(fd, res);

	testFrameBufferAdd(fd, res);

	drmModeFreeResources(res);
	printf("Ok\n");

	return 0;
}