aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: d0ffe8e7133b210086ba2dc0191680f684fc3dd9 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_framerate.h>
#include <SDL_rotozoom.h>
#include <SDL_gfxPrimitives.h>

#include <vlc/vlc.h>

#include <lo/lo.h>

/*
 * Macros
 */
#define NARRAY(ARRAY) \
	(sizeof(ARRAY) / sizeof((ARRAY)[0]))

#define SDL_MAYBE_LOCK(SURFACE) do {		\
	if (SDL_MUSTLOCK(SURFACE))		\
		SDL_LockSurface(SURFACE);	\
} while (0)

#define SDL_MAYBE_UNLOCK(SURFACE) do {		\
	if (SDL_MUSTLOCK(SURFACE))		\
		SDL_UnlockSurface(SURFACE);	\
} while (0)

#define SDL_ERROR(FMT, ...) do {					\
	fprintf(stderr, "%s(%d): " FMT ": %s\n",			\
		__FILE__, __LINE__, ##__VA_ARGS__, SDL_GetError());	\
} while (0)

#define SDL_IMAGE_ERROR(FMT, ...) do {					\
	fprintf(stderr, "%s(%d): " FMT ": %s\n",			\
		__FILE__, __LINE__, ##__VA_ARGS__, IMG_GetError());	\
} while (0)

/*
 * Default values
 */
#define SCREEN_WIDTH	640
#define SCREEN_HEIGHT	480
#define FRAMERATE	20 /* Hz */

/*
 * Declarations
 */
typedef void (*layer_frame_cb_t)(void *data, SDL_Surface *target);
typedef void (*layer_free_cb_t)(void *data);

static int layer_insert(int pos, const char *name, void *data,
			layer_frame_cb_t frame_cb, layer_free_cb_t free_cb);
static int layer_delete_by_name(const char *name);

static struct layer_image *layer_image_new(const char *file);
static void layer_image_change(struct layer_image *ctx, const char *file);
static void layer_image_frame_cb(void *data, SDL_Surface *target);
static void layer_image_free_cb(void *data);

static struct layer_video *layer_video_new(const char *file, SDL_Color *key);
static void layer_video_change(struct layer_video *ctx,
			       const char *file, SDL_Color *key);
static void layer_video_frame_cb(void *data, SDL_Surface *target);
static void layer_video_free_cb(void *data);

static struct layer_rect *layer_rect_new(SDL_Rect rect, SDL_Color color);
static void layer_rect_change(struct layer_rect *ctx,
			      SDL_Rect rect, SDL_Color color);
static void layer_rect_frame_cb(void *data, SDL_Surface *target);
static void layer_rect_free_cb(void *data);

static SDL_Surface *screen;

static void
osc_error(int num, const char *msg, const char *path)
{
	fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, msg);
}

/* catch any incoming messages and display them. returning 1 means that the
 * message has not been fully handled and the server should try other methods */
static int
osc_generic_handler(const char *path, const char *types, lo_arg **argv,
		    int argc, void *data,
		    void *user_data __attribute__((unused)))
{
    int i;

    printf("path: <%s>\n", path);
    for (i=0; i<argc; i++) {
	printf("arg %d '%c' ", i, types[i]);
	lo_arg_pp(types[i], argv[i]);
	printf("\n");
    }
    printf("\n");

    return 1;
}

static int
osc_layer_delete(const char *path, const char *types, lo_arg **argv,
		 int argc, void *data, void *user_data)
{
	lo_server server = user_data;

	/* path is /layer/[name]/delete */
	char *name = strdup(path + 7);
	*strchr(name, '/') = '\0';

	layer_delete_by_name(name);
	free(name);

	lo_server_del_method(server, path, types);

	return 0;
}

static inline void
osc_add_layer_delete(lo_server server, const char *name)
{
	char path[255];

	snprintf(path, sizeof(path), "/layer/%s/delete", name);
	lo_server_add_method(server, path, "", osc_layer_delete, server);
}

static int
osc_image_new(const char *path, const char *types, lo_arg **argv,
	      int argc, void *data, void *user_data)
{
	lo_server server = user_data;
	struct layer_image *ctx = layer_image_new(&argv[2]->s);

	if (layer_insert(argv[0]->i, &argv[1]->s, ctx,
			 layer_image_frame_cb, layer_image_free_cb))
		return 0;

	osc_add_layer_delete(server, &argv[1]->s);

	return 0;
}

static int
osc_video_new(const char *path, const char *types, lo_arg **argv,
	      int argc, void *data, void *user_data)
{
	lo_server server = user_data;
	struct layer_video *ctx = layer_video_new(&argv[2]->s, NULL);

	if (layer_insert(argv[0]->i, &argv[1]->s, ctx,
			 layer_video_frame_cb, layer_video_free_cb))
		return 0;

	osc_add_layer_delete(server, &argv[1]->s);

	return 0;
}

static int
osc_rect_new(const char *path, const char *types, lo_arg **argv,
	     int argc, void *data, void *user_data)
{
	lo_server server = user_data;
	SDL_Rect rect = {
		(Sint16)argv[2]->i, (Sint16)argv[3]->i,
		(Uint16)argv[4]->i, (Uint16)argv[5]->i
	};
	SDL_Color color = {(Uint8)argv[6]->i, (Uint8)argv[7]->i, (Uint8)argv[8]->i};

	struct layer_rect *ctx = layer_rect_new(rect, color);

	if (layer_insert(argv[0]->i, &argv[1]->s, ctx,
			 layer_rect_frame_cb, layer_rect_free_cb))
		return 0;

	osc_add_layer_delete(server, &argv[1]->s);

	return 0;
}

static inline lo_server
osc_init(const char *port)
{
	lo_server server = lo_server_new(port, osc_error);

	lo_server_add_method(server, NULL, NULL, osc_generic_handler, NULL);

	lo_server_add_method(server, "/layer/new/image", "iss",
			     osc_image_new, server);
	lo_server_add_method(server, "/layer/new/video", "iss",
			     osc_video_new, server);
	lo_server_add_method(server, "/layer/new/rect", "isiiiiiii",
			     osc_rect_new, server);

	return server;
}

static inline void
osc_process_events(lo_server server)
{
	while (lo_server_recv_noblock(server, 0));
}

/*
 * Image layers
 */
struct layer_image {
	SDL_Surface *surf;
};

static struct layer_image *
layer_image_new(const char *file)
{
	struct layer_image *ctx = malloc(sizeof(struct layer_image));

	if (ctx) {
		memset(ctx, 0, sizeof(*ctx));
		layer_image_change(ctx, file);
	}

	return ctx;
}

static void
layer_image_change(struct layer_image *ctx, const char *file)
{
	if (ctx->surf) {
		SDL_FreeSurface(ctx->surf);
		ctx->surf = NULL;
	}

	if (!file || !*file)
		return;

	ctx->surf = IMG_Load(file);
	if (!ctx->surf) {
		SDL_IMAGE_ERROR("IMG_Load");
		exit(EXIT_FAILURE);
	}

	if (ctx->surf->w != screen->w || ctx->surf->h != screen->h) {
		SDL_Surface *new;

		new = zoomSurface(ctx->surf,
				  (double)screen->w/ctx->surf->w,
				  (double)screen->h/ctx->surf->h,
				  SMOOTHING_ON);
		SDL_FreeSurface(ctx->surf);
		ctx->surf = new;
	}
}

static void
layer_image_frame_cb(void *data, SDL_Surface *target)
{
	struct layer_image *ctx = data;

	if (ctx->surf)
		SDL_BlitSurface(ctx->surf, NULL, target, NULL);
}

static void
layer_image_free_cb(void *data)
{
	struct layer_image *ctx = data;

	if (ctx->surf)
		SDL_FreeSurface(ctx->surf);

	free(ctx);
}

/*
 * Video layer
 */
struct layer_video {
	libvlc_instance_t *vlcinst;
	libvlc_media_player_t *mp;

	SDL_Surface *surf;
	SDL_mutex *mutex;
};

static void *
layer_video_lock_cb(void *data, void **p_pixels)
{
	struct layer_video *ctx = data;

	SDL_LockMutex(ctx->mutex);
	SDL_LockSurface(ctx->surf);
	*p_pixels = ctx->surf->pixels;

	return NULL; /* picture identifier, not needed here */
}

static void
layer_video_unlock_cb(void *data, void *id, void *const *p_pixels)
{
	struct layer_video *ctx = data;

	assert(id == NULL); /* picture identifier, not needed here */

	SDL_UnlockSurface(ctx->surf);
	SDL_UnlockMutex(ctx->mutex);
}

static void
layer_video_display_cb(void *data __attribute__((unused)), void *id)
{
	/* VLC wants to display the video */
	assert(id == NULL); /* picture identifier, not needed here */
}

static struct layer_video *
layer_video_new(const char *file, SDL_Color *key)
{
	char const *vlc_argv[] = {
		"--no-audio",	/* skip any audio track */
		"--no-xlib"	/* tell VLC to not use Xlib */
	};
	struct layer_video *ctx = malloc(sizeof(struct layer_video));

	if (!ctx)
		return NULL;

	ctx->surf = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h,
					 16, 0x001f, 0x07e0, 0xf800, 0);
	ctx->mutex = SDL_CreateMutex();

	ctx->vlcinst = libvlc_new(NARRAY(vlc_argv), vlc_argv);
	ctx->mp = NULL;

	layer_video_change(ctx, file, key);

	return ctx;
}

static void
layer_video_change(struct layer_video *ctx, const char *file, SDL_Color *key)
{
	libvlc_media_t *m;

	if (ctx->mp) {
		libvlc_media_player_release(ctx->mp);
		ctx->mp = NULL;
	}

	if (!file || !*file)
		return;

	if (key) {
		SDL_SetColorKey(ctx->surf, SDL_SRCCOLORKEY,
				SDL_MapRGB(ctx->surf->format,
					   key->r, key->g, key->b));
	} else {
		SDL_SetColorKey(ctx->surf, 0, 0);
	}

	m = libvlc_media_new_location(ctx->vlcinst, file);
	ctx->mp = libvlc_media_player_new_from_media(m);
	libvlc_media_release(m);

	libvlc_video_set_callbacks(ctx->mp, layer_video_lock_cb,
				   layer_video_unlock_cb, layer_video_display_cb,
				   ctx);
	libvlc_video_set_format(ctx->mp, "RV16",
				ctx->surf->w,
				ctx->surf->h,
				ctx->surf->w*2);

	libvlc_media_player_play(ctx->mp);
}

static void
layer_video_frame_cb(void *data, SDL_Surface *target)
{
	struct layer_video *ctx = data;

	if (ctx->mp) {
		SDL_LockMutex(ctx->mutex);
		SDL_BlitSurface(ctx->surf, NULL, target, NULL);
		SDL_UnlockMutex(ctx->mutex);
	}
}

static void
layer_video_free_cb(void *data)
{
	struct layer_video *ctx = data;

	if (ctx->mp)
		libvlc_media_player_release(ctx->mp);
	libvlc_release(ctx->vlcinst);
	SDL_DestroyMutex(ctx->mutex);
	SDL_FreeSurface(ctx->surf);

	free(ctx);
}

/*
 * Rectangle layer
 */
struct layer_rect {
	SDL_Rect	rect;
	SDL_Color	color;
};

static struct layer_rect *
layer_rect_new(SDL_Rect rect, SDL_Color color)
{
	struct layer_rect *ctx = malloc(sizeof(struct layer_rect));

	if (ctx)
		layer_rect_change(ctx, rect, color);

	return ctx;
}

static void
layer_rect_change(struct layer_rect *ctx, SDL_Rect rect, SDL_Color color)
{
	ctx->rect = rect;
	ctx->color = color;
}

static void
layer_rect_frame_cb(void *data, SDL_Surface *target)
{
	struct layer_rect *ctx = data;
	SDL_Rect *dstrect = &ctx->rect;

	if (!dstrect->x && !dstrect->y && !dstrect->w && !dstrect->h)
		dstrect = NULL;

	SDL_FillRect(target, dstrect,
		     SDL_MapRGB(target->format,
				ctx->color.r, ctx->color.g, ctx->color.b));
}

static void
layer_rect_free_cb(void *data)
{
	struct layer_rect *ctx = data;

	free(ctx);
}

static inline void
sdl_process_events(void)
{
	SDL_Event event;

	while (SDL_PollEvent(&event)) {
		switch (event.type) {
		case SDL_KEYDOWN:
			switch (event.key.keysym.sym) {
			case SDLK_F11:
				if (!SDL_WM_ToggleFullScreen(screen)) {
					SDL_ERROR("SDL_WM_ToggleFullScreen");
					exit(EXIT_FAILURE);
				}
				break;

			case SDLK_F10:
				SDL_ShowCursor(!SDL_ShowCursor(SDL_QUERY));
				break;

			case SDLK_ESCAPE:
				exit(EXIT_SUCCESS);

			default:
				break;
			}
			break;

		case SDL_QUIT:
			exit(EXIT_SUCCESS);

		default:
			break;
		}
	}
}

struct layer {
	struct layer *next;

	char *name;

	void			*data;
	layer_frame_cb_t	frame_cb;
	layer_free_cb_t		free_cb;
};
static struct layer layers_head = {NULL};

#define FOREACH_LAYER(VAR) \
	for (struct layer *VAR = layers_head.next; VAR; VAR = VAR->next)

static int
layer_insert(int pos, const char *name, void *data,
	     layer_frame_cb_t frame_cb, layer_free_cb_t free_cb)
{
	struct layer *new, *cur;

	new = malloc(sizeof(struct layer));
	if (!new)
		return -1;

	new->name = strdup(name);
	if (!new->name) {
		free(new);
		return -1;
	}
	new->data = data;
	new->frame_cb = frame_cb;
	new->free_cb = free_cb;

	for (cur = &layers_head; cur->next && pos; cur = cur->next, pos--);
	new->next = cur->next;
	cur->next = new;

	return 0;
}

static int
layer_delete_by_name(const char *name)
{
	for (struct layer *cur = &layers_head; cur->next; cur = cur->next) {
		if (!strcmp(cur->next->name, name)) {
			struct layer *l = cur->next;

			cur->next = l->next;

			l->free_cb(l->data);
			free(l->name);
			free(l);

			return 0;
		}
	}

	return -1;
}

int
main(int argc, char **argv)
{
	lo_server osc_server;
	FPSmanager fpsm;

	osc_server = osc_init("7770");
	if (!osc_server)
		return EXIT_FAILURE;

	if (SDL_Init(SDL_INIT_VIDEO)) {
		SDL_ERROR("SDL_Init");
		return EXIT_FAILURE;
	}
	atexit(SDL_Quit);

	SDL_WM_SetCaption("OSC Graphics", NULL);

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32,
				  SDL_HWSURFACE | SDL_DOUBLEBUF);
	if (screen == NULL) {
		SDL_ERROR("SDL_SetVideoMode");
		return EXIT_FAILURE;
	}

	SDL_initFramerate(&fpsm);
	SDL_setFramerate(&fpsm, FRAMERATE);

	for (;;) {
		sdl_process_events();
		osc_process_events(osc_server);

		FOREACH_LAYER(cur)
			cur->frame_cb(cur->data, screen);

		SDL_Flip(screen);
		SDL_framerateDelay(&fpsm);
	}

	/* never reached */
	return EXIT_FAILURE;
}