aboutsummaryrefslogtreecommitdiff
path: root/tests/modehotplug/demo.c
blob: 1518e84c06279445efc5553c7fa4b60bcf6af9d6 (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

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

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

/* structs for the demo_driver */

#define DEMO_MAX_OUTPUTS 8

struct demo_driver
{
	/* drm stuff */
	int fd;
	drmModeResPtr res;
	uint32_t counter;

	drmModeOutputPtr outputs[DEMO_MAX_OUTPUTS];
};

struct demo_driver* demoCreateDriver(void);
void demoUpdateRes(struct demo_driver *driver);

void demoPopulateOutputs(struct demo_driver *driver);
void demoHotplug(struct demo_driver *driver);

const char* demoGetStatus(drmModeOutputPtr out);

int main(int argc, char **argv)
{
	struct demo_driver *driver;
	uint32_t temp;
	int i;

	printf("starting demo\n");

	driver = demoCreateDriver();

	if (!driver) {
		printf("failed to create driver\n");
		return 1;
	}

	driver->counter = drmModeGetHotplug(driver->fd);
	demoPopulateOutputs(driver);
	while (driver->counter != (temp = drmModeGetHotplug(driver->fd))) {
		demoPopulateOutputs(driver);
		driver->counter = temp;
	}

	for (i = 0; i < driver->res->count_outputs && i < DEMO_MAX_OUTPUTS; i++) {
		printf("Output %u is %s\n",
			driver->outputs[i]->output_id,
			demoGetStatus(driver->outputs[i]));
	}

	while(1) {
		usleep(100000);
		temp = drmModeGetHotplug(driver->fd);
		if (temp == driver->counter)
			continue;

		demoHotplug(driver);
		driver->counter = temp;
	}

	return 0;
}

const char* demoGetStatus(drmModeOutputPtr output)
{
	switch (output->connection) {
		case DRM_MODE_CONNECTED:
			return "connected";
		case DRM_MODE_DISCONNECTED:
			return "disconnected";
		default:
			return "unknown";
	}
}

void demoHotplug(struct demo_driver *driver)
{
	drmModeResPtr res = driver->res;
	int i;
	drmModeOutputPtr temp, current;

	for (i = 0; i < res->count_outputs && i < DEMO_MAX_OUTPUTS; i++) {
		temp = drmModeGetOutput(driver->fd, res->outputs[i]);
		current = driver->outputs[i];

		if (temp->connection != current->connection) {
			printf("Output %u became %s was %s\n",
				temp->output_id,
				demoGetStatus(temp),
				demoGetStatus(current));
		}

		drmModeFreeOutput(current);
		driver->outputs[i] = temp;
	}
}

void demoPopulateOutputs(struct demo_driver *driver)
{
	drmModeResPtr res = driver->res;
	int i;

	for (i = 0; i < res->count_outputs && i < DEMO_MAX_OUTPUTS; i++) {
		drmModeFreeOutput(driver->outputs[i]);
		driver->outputs[i] = drmModeGetOutput(driver->fd, res->outputs[i]);
	}
}

struct demo_driver* demoCreateDriver(void)
{
	struct demo_driver* driver = malloc(sizeof(struct demo_driver));

	memset(driver, 0, sizeof(struct demo_driver));

	driver->fd = drmOpenControl(0);

	if (driver->fd < 0) {
		printf("Failed to open the card fb\n");
		goto err_driver;
	}

	demoUpdateRes(driver);
	if (!driver->res) {
		printf("could not retrive resources\n");
		goto err_res;
	}

	return driver;

err_res:
	drmClose(driver->fd);
err_driver:
	free(driver);
	return NULL;
}

void demoUpdateRes(struct demo_driver *driver)
{
	if (driver->res)
		drmModeFreeResources(driver->res);

	driver->res = drmModeGetResources(driver->fd);

	if (!driver->res)
		printf("failed to get resources from kernel\n");
}