aboutsummaryrefslogtreecommitdiff
path: root/example/d2tk_fbdev.c
blob: a6c9c260538ed8a3eb252f8b5caf208aa542b3e3 (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
/*
 * Copyright (c) 2018-2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <signal.h>

#include <d2tk/frontend_fbdev.h>
#include "example/example.h"

#define AUTO "auto"

typedef struct _app_t app_t;
typedef void (*foreach_t)(const char *path, const char *d_name);

struct _app_t {
	d2tk_fbdev_t *fbdev;
};

static sig_atomic_t done = false;

static void
_sig(int signum __attribute__((unused)))
{
	done = true;
}

static inline int
_expose(void *data, d2tk_coord_t w, d2tk_coord_t h)
{
	app_t *app = data;
	d2tk_fbdev_t *fbdev = app->fbdev;
	d2tk_base_t *base = d2tk_fbdev_get_base(fbdev);

	d2tk_example_run(base, w, h);

	d2tk_coord_t x = 0;
	d2tk_coord_t y = 0;

	d2tk_base_get_mouse_pos(base, &x, &y);
	d2tk_base_cursor(base, &D2TK_RECT(x, y, 24, 24));

	return EXIT_SUCCESS;
}

static void
_state(const char *path, const char *d_name, int32_t state)
{
	char buf [PATH_MAX];

	snprintf(buf, sizeof(buf), "%s/%s/bind", path, d_name);

	FILE *f = fopen(buf, "w");
	if(f)
	{
		fprintf(f, "%"PRIi32, state);
		fflush(f);
		fclose(f);
	}
}

static void
_unbind(const char *path, const char *d_name)
{
	_state(path, d_name, 0);
}

static void
_bind(const char *path, const char *d_name)
{
	_state(path, d_name, 1);
}

static void
_find_by_format_foreach(const char *path, const char *fmt, void *val,
	foreach_t foreach)
{
	DIR *d = opendir(path);
	if(d)
	{
		struct dirent *dir;

		while( (dir = readdir(d)) )
		{
			if(!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
			{
				continue;
			}

			if(sscanf(dir->d_name, fmt, val) == 1)
			{
				foreach(path, dir->d_name);
			}
		}

		closedir(d);
	}
}

static int
_find_by_format(const char *path, const char *fmt, void *val, char *res)
{
	int ret = EXIT_FAILURE;

	DIR *d = opendir(path);
	if(d)
	{
		struct dirent *dir;

		while( (dir = readdir(d)) )
		{
			if(!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
			{
				continue;
			}

			if(sscanf(dir->d_name, fmt, val) == 1)
			{
				snprintf(res, PATH_MAX, "%s/%s", path, dir->d_name);
				ret = EXIT_SUCCESS;
				break;
			}
		}

		closedir(d);
	}

	return ret;
}

int
main(int argc, char **argv)
{
	static app_t app;
	static char fb_device [PATH_MAX] = AUTO;

	int c;
	while( (c = getopt(argc, argv, "f:")) != -1)
	{
		switch(c)
		{
			case 'f':
			{
				strncpy(fb_device, optarg, PATH_MAX-1);
			} break;

			default:
			{
				fprintf(stderr, "Usage: %s\n"
					"  -f  fb_device    (auto)\n\n",
					argv[0]);
			} return EXIT_FAILURE;
		}
	}

	if(!strcmp(fb_device, AUTO))
	{
		uint32_t num;

		if(_find_by_format("/dev", "fb%"SCNu32, &num, fb_device))
		{
			return EXIT_FAILURE;
		}
	}

	fprintf(stdout,
		"  fb_device:    %s\n\n",
		fb_device);

	const d2tk_fbdev_config_t config = {
		.fb_device = fb_device,
		.bundle_path = "./",
		.expose = _expose,
		.data = &app
	};

	signal(SIGINT, _sig);
	signal(SIGTERM, _sig);
#if !defined(_WIN32)
	signal(SIGQUIT, _sig);
	signal(SIGKILL, _sig);
#endif

	app.fbdev = d2tk_fbdev_new(&config);
	if(app.fbdev)
	{
		uint32_t num;

		_find_by_format_foreach("/sys/class/vtconsole", "vtcon%"SCNu32, &num,
			_unbind);

		d2tk_example_init();

		d2tk_fbdev_run(app.fbdev, &done);

		d2tk_fbdev_free(app.fbdev);

		d2tk_example_deinit();

		_find_by_format_foreach("/sys/class/vtconsole", "vtcon%"SCNu32, &num,
			_bind);

		return EXIT_SUCCESS;
	}

	return EXIT_FAILURE;
}