Fix last commit
[carla.git] / source / tests.old / WineJack.cpp
blob1160f8fea584110d78bc4dd8b9519c88d9454de7
1 /*
2 * Wine+JACK Test
3 * Copyright (C) 2015 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include <semaphore.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include <unistd.h>
22 #include <windows.h>
24 #include <sys/types.h>
25 #include <jack/jack.h>
27 #undef _WIN32
28 #include <jack/thread.h>
30 static bool gCloseNow = false;
32 static void _close(int)
34 gCloseNow = true;
37 static int _process(jack_nframes_t, void*)
39 printf("%s\n", __PRETTY_FUNCTION__);
40 return 0;
43 #if 0
44 // TEST
45 void* (*creator_func)(void*) = NULL;
46 void* creator_arg = NULL;
47 HANDLE creator_handle = 0;
48 pthread_t creator_pthread = 0;
50 static DWORD WINAPI thread_creator_helper(LPVOID)
52 printf("%s\n", __PRETTY_FUNCTION__);
53 creator_pthread = pthread_self();
54 SetEvent(creator_handle);
55 creator_func(creator_arg);
56 return 0;
59 static int thread_creator(pthread_t* thread_id, const pthread_attr_t*, void *(*function)(void*), void* arg)
61 printf("%s\n", __PRETTY_FUNCTION__);
62 creator_func = function;
63 creator_arg = arg;
64 creator_handle = ::CreateEventA(NULL, false, false, NULL);
66 ::CreateThread(NULL, 0, thread_creator_helper, arg, 0, 0);
67 ::WaitForSingleObject(creator_handle, INFINITE);
69 *thread_id = creator_pthread;
70 return 0;
72 #endif
74 struct JackWineThread {
75 void* (*func)(void*);
76 void* arg;
77 pthread_t pthid;
78 sem_t sema;
81 static DWORD WINAPI
82 wine_thread_aux( LPVOID arg ) {
83 struct JackWineThread* jwt = (struct JackWineThread*) arg;
85 printf("%s\n", __PRETTY_FUNCTION__);
87 void* func_arg = jwt->arg;
88 void* (*func)(void*) = jwt->func;
90 jwt->pthid = pthread_self();
91 sem_post( &jwt->sema );
93 func ( func_arg );
95 return 0;
98 static int
99 wine_thread_create (pthread_t* thread_id, const pthread_attr_t*, void *(*function)(void*), void* arg) {
100 struct JackWineThread jwt;
102 printf("%s\n", __PRETTY_FUNCTION__);
104 sem_init( &jwt.sema, 0, 0 );
105 jwt.func = function;
106 jwt.arg = arg;
108 CreateThread( NULL, 0, wine_thread_aux, &jwt, 0, 0 );
109 sem_wait( &jwt.sema );
111 *thread_id = jwt.pthid;
112 return 0;
115 int main()
117 struct sigaction sterm;
118 sterm.sa_handler = _close;
119 sterm.sa_flags = SA_RESTART;
120 sterm.sa_restorer = NULL;
121 sigemptyset(&sterm.sa_mask);
122 sigaction(SIGTERM, &sterm, NULL);
123 sigaction(SIGINT, &sterm, NULL);
125 jack_set_thread_creator(wine_thread_create);
127 jack_client_t* const client = jack_client_open("WineJack2", JackNullOption, NULL);
128 if (client == NULL)
129 return 1;
131 jack_set_process_callback(client, _process, NULL);
132 jack_activate(client);
134 for (; ! gCloseNow;) {
135 usleep(100000);
138 jack_deactivate(client);
139 jack_client_close(client);
141 return 0;