-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrobtkapp.c
255 lines (201 loc) · 5.77 KB
/
robtkapp.c
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
/* robtk GUI application
*
* Copyright (C) 2012-2015 Robin Gareus
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UI_UPDATE_FPS
#define UI_UPDATE_FPS 25
#endif
///////////////////////////////////////////////////////////////////////////////
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifdef WIN32
#include <windows.h>
#include <pthread.h>
#define pthread_t //< override jack.h def
#endif
#include "lv2/lv2plug.in/ns/lv2core/lv2.h"
#include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
extern void rtk_osx_api_init(void);
extern void rtk_osx_api_terminate(void);
extern void rtk_osx_api_run(void);
extern void rtk_osx_api_err(const char *msg);
#endif
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <getopt.h>
#include <assert.h>
#if (defined _WIN32 && defined RTK_STATIC_INIT)
#include <glib-object.h>
#endif
#ifndef _WIN32
#include <signal.h>
#include <pthread.h>
#else
#include <sys/timeb.h>
#endif
#include "./gl/xternalui.h"
#define LV2_EXTERNAL_UI_RUN(ptr) (ptr)->run(ptr)
#define LV2_EXTERNAL_UI_SHOW(ptr) (ptr)->show(ptr)
#define LV2_EXTERNAL_UI_HIDE(ptr) (ptr)->hide(ptr)
#define nan NAN
typedef struct _RtkLv2Description {
const LV2UI_Descriptor* (*lv2ui_descriptor)(uint32_t index);
const uint32_t gui_descriptor_id;
const char *plugin_human_id;
} RtkLv2Description;
static const LV2UI_Descriptor *plugin_gui;
static LV2_Handle plugin_instance = NULL;
static LV2UI_Handle gui_instance = NULL;
static pthread_mutex_t gui_thread_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
static RtkLv2Description const *inst;
/* a simple state machine for this client */
static volatile enum {
Run,
Exit
} client_state = Run;
struct lv2_external_ui_host extui_host;
struct lv2_external_ui *extui = NULL;
static const RtkLv2Description _plugin = {
& RTK_DESCRIPTOR, 0, APPTITLE
};
/******************************************************************************
* MAIN
*/
static void cleanup(int sig) {
if (plugin_gui && gui_instance && plugin_gui->cleanup) {
plugin_gui->cleanup(gui_instance);
}
fprintf(stderr, "bye.\n");
}
static void run_one() {
LV2_EXTERNAL_UI_RUN(extui);
}
#ifdef __APPLE__
static void osx_loop (CFRunLoopTimerRef timer, void *info) {
if (client_state == Run) {
run_one();
}
if (client_state == Exit) {
rtk_osx_api_terminate();
}
}
#else
static void main_loop(void) {
struct timespec timeout;
pthread_mutex_lock (&gui_thread_lock);
while (client_state != Exit) {
run_one();
if (client_state == Exit) break;
#ifdef _WIN32
//Sleep(1000/UI_UPDATE_FPS);
#if (defined(__MINGW64__) || defined(__MINGW32__)) && __MSVCRT_VERSION__ >= 0x0601
struct __timeb64 timebuffer;
_ftime64(&timebuffer);
#else
struct __timeb32 timebuffer;
_ftime(&timebuffer);
#endif
timeout.tv_nsec = timebuffer.millitm * 1000000;
timeout.tv_sec = timebuffer.time;
#else // POSIX
clock_gettime(CLOCK_REALTIME, &timeout);
#endif
timeout.tv_nsec += 1000000000 / (UI_UPDATE_FPS);
if (timeout.tv_nsec >= 1000000000) {timeout.tv_nsec -= 1000000000; timeout.tv_sec+=1;}
pthread_cond_timedwait (&data_ready, &gui_thread_lock, &timeout);
} /* while running */
pthread_mutex_unlock (&gui_thread_lock);
}
#endif // APPLE RUNLOOP
static void catchsig (int sig) {
fprintf(stderr,"caught signal - shutting down.\n");
client_state=Exit;
pthread_cond_signal (&data_ready);
}
static void on_external_ui_closed(void* controller) {
catchsig(0);
}
int main (int argc, char **argv) {
int rv = 0;
inst = & _plugin;
#ifdef __APPLE__
rtk_osx_api_init();
#endif
#ifdef _WIN32
pthread_win32_process_attach_np();
#endif
#if (defined _WIN32 && defined RTK_STATIC_INIT)
glib_init_static();
gobject_init_ctor();
#endif
const LV2_Feature external_lv_feature = { LV2_EXTERNAL_UI_URI, &extui_host};
const LV2_Feature external_kx_feature = { LV2_EXTERNAL_UI_URI__KX__Host, &extui_host};
const LV2_Feature* ui_features[] = {
&external_lv_feature,
&external_kx_feature,
NULL
};
extui_host.plugin_human_id = inst->plugin_human_id;
plugin_gui = inst->lv2ui_descriptor(inst->gui_descriptor_id);
if (plugin_gui) {
/* init plugin GUI */
extui_host.ui_closed = on_external_ui_closed;
gui_instance = plugin_gui->instantiate(plugin_gui,
"URI TODO", NULL, NULL, NULL,
(void **)&extui, ui_features);
}
if (!gui_instance || !extui) {
fprintf(stderr, "Error: GUI was not initialized.\n");
rv |= 2;
goto out;
}
#ifndef _WIN32
signal (SIGHUP, catchsig);
signal (SIGINT, catchsig);
#endif
{
LV2_EXTERNAL_UI_SHOW(extui);
#ifdef __APPLE__
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
CFRunLoopTimerContext context = {0, NULL, NULL, NULL, NULL};
CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0, 1.0/UI_UPDATE_FPS, 0, 0, &osx_loop, &context);
CFRunLoopAddTimer(runLoop, timer, kCFRunLoopCommonModes);
rtk_osx_api_run();
#else
main_loop();
#endif
LV2_EXTERNAL_UI_HIDE(extui);
}
out:
cleanup(0);
#ifdef _WIN32
pthread_win32_process_detach_np();
#endif
#if (defined _WIN32 && defined RTK_STATIC_INIT)
glib_cleanup_static();
#endif
return(rv);
}
/* vi:set ts=2 sts=2 sw=2: */