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>
24 #include <sys/types.h>
25 #include <jack/jack.h>
28 #include <jack/thread.h>
30 static bool gCloseNow
= false;
32 static void _close(int)
37 static int _process(jack_nframes_t
, void*)
39 printf("%s\n", __PRETTY_FUNCTION__
);
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
);
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
;
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
;
74 struct JackWineThread
{
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
);
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 );
108 CreateThread( NULL
, 0, wine_thread_aux
, &jwt
, 0, 0 );
109 sem_wait( &jwt
.sema
);
111 *thread_id
= jwt
.pthid
;
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
);
131 jack_set_process_callback(client
, _process
, NULL
);
132 jack_activate(client
);
134 for (; ! gCloseNow
;) {
138 jack_deactivate(client
);
139 jack_client_close(client
);