summaryrefslogtreecommitdiff
path: root/modules/sdl/sdl.c
blob: ea60a38090a08504e5ef11262c70f97f88781bde (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
/**
 * @file sdl/sdl.c  SDL - Simple DirectMedia Layer v1.2
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <SDL/SDL.h>
#include <string.h>
#include <re.h>
#include <rem.h>
#include <baresip.h>
#include "sdl.h"


/** Local constants */
enum {
	KEY_RELEASE_VAL = 250  /**< Key release value in [ms] */
};

struct vidisp_st {
	const struct vidisp *vd;  /* inheritance */
};

/** Global SDL data */
static struct {
	struct tmr tmr;
	SDL_Surface *screen;            /**< SDL Surface           */
	SDL_Overlay *bmp;               /**< SDL YUV Overlay       */
	struct vidsz size;              /**< Current size          */
	vidisp_resize_h *resizeh;       /**< Screen resize handler */
	void *arg;                      /**< Handler argument      */
	bool fullscreen;
	bool open;
} sdl;


static struct vidisp *vid;


static void event_handler(void *arg);


static void sdl_reset(void)
{
	if (sdl.bmp) {
		SDL_FreeYUVOverlay(sdl.bmp);
		sdl.bmp = NULL;
	}

	if (sdl.screen) {
		SDL_FreeSurface(sdl.screen);
		sdl.screen = NULL;
	}
}


static void handle_resize(int w, int h)
{
	struct vidsz size;

	size.w = w;
	size.h = h;

	/* notify app */
	if (sdl.resizeh)
		sdl.resizeh(&size, sdl.arg);
}


static void timeout(void *arg)
{
	(void)arg;

	tmr_start(&sdl.tmr, 1, event_handler, NULL);

	/* Emulate key-release */
	ui_input(0x00);
}


static void event_handler(void *arg)
{
	SDL_Event event;
	char ch;

	(void)arg;

	tmr_start(&sdl.tmr, 100, event_handler, NULL);

	while (SDL_PollEvent(&event)) {

		switch (event.type) {

		case SDL_KEYDOWN:

			switch (event.key.keysym.sym) {

			case SDLK_ESCAPE:
				if (!sdl.fullscreen)
					break;

				sdl.fullscreen = false;
				sdl_reset();
				break;

			case SDLK_f:
				if (sdl.fullscreen)
					break;

				sdl.fullscreen = true;
				sdl_reset();
				break;

			default:
				ch = event.key.keysym.unicode & 0x7f;

				/* Relay key-press to UI subsystem */
				if (isprint(ch)) {
					tmr_start(&sdl.tmr, KEY_RELEASE_VAL,
						  timeout, NULL);
					ui_input(ch);
				}
				break;
			}

			break;

		case SDL_VIDEORESIZE:
			handle_resize(event.resize.w, event.resize.h);
			break;

		case SDL_QUIT:
			ui_input('q');
			break;

		default:
			break;
		}
	}
}


static int sdl_open(void)
{
	if (sdl.open)
		return 0;

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		warning("sdl: unable to init SDL: %s\n", SDL_GetError());
		return ENODEV;
	}

	SDL_EnableUNICODE(1);

	tmr_start(&sdl.tmr, 100, event_handler, NULL);
	sdl.open = true;

	return 0;
}


static void sdl_close(void)
{
	tmr_cancel(&sdl.tmr);
	sdl_reset();

	if (sdl.open) {
		SDL_Quit();
		sdl.open = false;
	}
}


static void destructor(void *arg)
{
	struct vidisp_st *st = arg;

	sdl_close();
}


static int alloc(struct vidisp_st **stp, const struct vidisp *vd,
		 struct vidisp_prm *prm, const char *dev,
		 vidisp_resize_h *resizeh, void *arg)
{
	struct vidisp_st *st;
	int err;

	/* Not used by SDL */
	(void)prm;
	(void)dev;

	if (sdl.open)
		return EBUSY;

	st = mem_zalloc(sizeof(*st), destructor);
	if (!st)
		return ENOMEM;

	st->vd = vd;

	sdl.resizeh = resizeh;
	sdl.arg     = arg;

	err = sdl_open();

	if (err)
		mem_deref(st);
	else
		*stp = st;

	return err;
}


/**
 * Display a video frame
 *
 * @param st    Video display state
 * @param title Window title
 * @param frame Video frame
 *
 * @return 0 if success, otherwise errorcode
 *
 * @note: On Darwin, this must be called from the main thread
 */
static int display(struct vidisp_st *st, const char *title,
		   const struct vidframe *frame)
{
	SDL_Rect rect;

	if (!st || !sdl.open)
		return EINVAL;

	if (!vidsz_cmp(&sdl.size, &frame->size)) {
		if (sdl.size.w && sdl.size.h) {
			info("sdl: reset size %u x %u  --->  %u x %u\n",
			     sdl.size.w, sdl.size.h,
			     frame->size.w, frame->size.h);
		}
		sdl_reset();
	}

	if (!sdl.screen) {
		int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
		char capt[256];

		if (sdl.fullscreen)
			flags |= SDL_FULLSCREEN;
		else if (sdl.resizeh)
			flags |= SDL_RESIZABLE;

		if (title) {
			re_snprintf(capt, sizeof(capt), "%s - %u x %u",
				    title, frame->size.w, frame->size.h);
		}
		else {
			re_snprintf(capt, sizeof(capt), "%u x %u",
				    frame->size.w, frame->size.h);
		}

		SDL_WM_SetCaption(capt, capt);

		sdl.screen = SDL_SetVideoMode(frame->size.w, frame->size.h,
					      0, flags);
		if (!sdl.screen) {
			warning("sdl: unable to get video screen: %s\n",
				SDL_GetError());
			return ENODEV;
		}

		sdl.size = frame->size;
	}

	if (!sdl.bmp) {
		sdl.bmp = SDL_CreateYUVOverlay(frame->size.w, frame->size.h,
					       SDL_YV12_OVERLAY, sdl.screen);
		if (!sdl.bmp) {
			warning("sdl: unable to create overlay: %s\n",
				SDL_GetError());
			return ENODEV;
		}
	}

	SDL_LockYUVOverlay(sdl.bmp);
	picture_copy(sdl.bmp->pixels, sdl.bmp->pitches, frame);
	SDL_UnlockYUVOverlay(sdl.bmp);

	rect.x = 0;
	rect.y = 0;
	rect.w = sdl.size.w;
	rect.h = sdl.size.h;

	SDL_DisplayYUVOverlay(sdl.bmp, &rect);

	return 0;
}


static int module_init(void)
{
	return vidisp_register(&vid, "sdl", alloc, NULL, display, NULL);
}


static int module_close(void)
{
	vid = mem_deref(vid);

	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(sdl) = {
	"sdl",
	"vidisp",
	module_init,
	module_close,
};