aboutsummaryrefslogtreecommitdiff
path: root/midimcast.h
blob: 26cab3402bf69a76488198287c5dcfdfb28b3798 (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
#ifndef __MIDIMCAST_H__
#define __MIDIMCAST_H__

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define debug(FMT, ...)					\
	if (midimcast_debug) {				\
		fprintf(stderr,				\
			"[DEBUG] %s:%u:%s: " FMT "\n",	\
			__FILE__,			\
			__LINE__,			\
			__func__ __VA_OPT__(,)		\
			__VA_ARGS__);			\
	}

#define warn(FMT, ...)						\
	fprintf(stderr, "[WARNING] %s:%u:%s: " FMT "\n",	\
		__FILE__,					\
		__LINE__,					\
		__func__ __VA_OPT__(,)				\
		__VA_ARGS__);

#define err(FMT, ...)						\
	({							\
		fprintf(stderr, "[ERROR] %s:%u:%s: " FMT "\n",	\
			__FILE__,				\
			__LINE__,				\
			__func__ __VA_OPT__(,)			\
			__VA_ARGS__);				\
		exit(EXIT_FAILURE);				\
	})

#define err_std_nchk(CALLEE)					\
	({							\
		fprintf(stderr, "[ERROR] %s:%u:%s:%s: %s\n",	\
			__FILE__,				\
			__LINE__,				\
			__func__,				\
			#CALLEE,				\
			strerror(errno));			\
		exit(EXIT_FAILURE);				\
	})

#define err_std(CALLEE, ...)			\
	if (CALLEE(__VA_ARGS__))		\
		err_std_nchk(CALLEE);

#define err_std_neg(CALLEE, ...)			\
	({						\
		const typeof(CALLEE(__VA_ARGS__)) ret =	\
			CALLEE(__VA_ARGS__);		\
		if (ret < 0)				\
			err_std_nchk(CALLEE);		\
		ret;					\
	})

#define err_std_null(CALLEE, ...)			\
	({						\
		const typeof(CALLEE(__VA_ARGS__)) ret =	\
			CALLEE(__VA_ARGS__);		\
		if (!ret)				\
			err_std_nchk(CALLEE);		\
		ret;					\
	})

#define err_snd_nchk(CALLEE, ERR)				\
	({							\
		fprintf(stderr, "[ERROR] %s:%u:%s:%s: %s\n",	\
			__FILE__,				\
			__LINE__,				\
			__func__,				\
			#CALLEE,				\
			snd_strerror(ERR));			\
		exit(EXIT_FAILURE);				\
	})


#define err_snd(CALLEE, ...)				\
	({						\
		const int err = CALLEE(__VA_ARGS__);	\
		if (err)				\
			err_snd_nchk(CALLEE, err);	\
	})

#define err_snd_neg(CALLEE, ...)			\
	({						\
		const typeof(CALLEE(__VA_ARGS__)) ret	\
			= CALLEE(__VA_ARGS__);		\
		if (ret < 0)				\
			err_snd_nchk(CALLEE, ret);	\
		ret;					\
	})

struct msg {
	uint64_t ts;
	uint8_t midi[0x10]; // Large enough buffer
} __attribute__((packed));

extern uint64_t now();

extern bool midimcast_debug;

void env_read_common(const char **const mcast_group,
		     uint16_t *const mcast_port);

#endif