macro-ize the core conversions and fix mistakes in dithering cases
[jack.git] / jack / internal.h
blob472391e00d6fac9ef0924dfa6133859040938415
1 /* -*- mode: c; c-file-style: "bsd"; -*- */
2 /*
3 Internal shared data and functions.
5 If you edit this file, you should carefully consider changing the
6 JACK_PROTOCOL_VERSION in configure.in.
8 Copyright (C) 2001-2003 Paul Davis
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #ifndef __jack_internal_h__
27 #define __jack_internal_h__
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <limits.h>
32 #include <dlfcn.h>
33 #include <pthread.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
37 #ifndef POST_PACKED_STRUCTURE
38 #ifdef __GNUC__
39 /* POST_PACKED_STRUCTURE needs to be a macro which
40 expands into a compiler directive. The directive must
41 tell the compiler to arrange the preceding structure
42 declaration so that it is packed on byte-boundaries rather
43 than use the natural alignment of the processor and/or
44 compiler.
46 #define POST_PACKED_STRUCTURE __attribute__((__packed__))
47 #else
48 /* Add other things here for non-gcc platforms */
49 #endif
50 #endif
52 /* Needed by <sysdeps/time.h> */
53 extern void jack_error (const char *fmt, ...);
55 extern void jack_info (const char *fmt, ...);
57 #include <jack/jack.h>
58 #include <jack/types.h>
59 #include <jack/port.h>
60 #include <jack/transport.h>
62 typedef enum {
63 JACK_TIMER_SYSTEM_CLOCK,
64 JACK_TIMER_CYCLE_COUNTER,
65 JACK_TIMER_HPET,
66 } jack_timer_type_t;
68 void jack_init_time ();
69 void jack_set_clock_source (jack_timer_type_t);
70 const char* jack_clock_source_name (jack_timer_type_t);
72 #include <sysdeps/time.h>
73 #include <sysdeps/atomicity.h>
75 #ifdef JACK_USE_MACH_THREADS
76 #include <sysdeps/mach_port.h>
77 #endif
79 #include <jack/messagebuffer.h>
81 #ifdef DEBUG_ENABLED
83 /* grab thread id instead of PID on linux */
84 #if defined(__gnu_linux__)
85 #ifdef gettid /* glibc has a version */
86 #define GETTID() gettid()
87 #else /* use our own version */
88 #include <sys/syscall.h>
89 #define GETTID() syscall(__NR_gettid)
90 #endif
91 #else
92 #define GETTID() getpid()
93 #endif
95 #define DEBUG(format,args...) \
96 MESSAGE("jack:%5d:%" PRIu64 " %s:%s:%d: " format "", GETTID(), jack_get_microseconds(), __FILE__, __FUNCTION__, __LINE__ , ## args)
98 #else
99 #if JACK_CPP_VARARGS_BROKEN
100 #define DEBUG(format...)
101 #else
102 #define DEBUG(format,args...)
103 #endif
104 #endif
106 /* Enable preemption checking for Linux Realtime Preemption kernels.
108 * This checks if any RT-safe code section does anything to cause CPU
109 * preemption. Examples are sleep() or other system calls that block.
110 * If a problem is detected, the kernel writes a syslog entry, and
111 * sends SIGUSR2 to the client.
113 #ifdef DO_PREEMPTION_CHECKING
114 #define CHECK_PREEMPTION(engine, onoff) \
115 if ((engine)->real_time) gettimeofday (1, (onoff))
116 #else
117 #define CHECK_PREEMPTION(engine, onoff)
118 #endif
120 #ifndef FALSE
121 #define FALSE (0)
122 #endif
124 #ifndef TRUE
125 #define TRUE (1)
126 #endif
128 typedef struct _jack_engine jack_engine_t;
129 typedef struct _jack_request jack_request_t;
131 typedef void * dlhandle;
133 typedef enum {
134 TransportCommandNone = 0,
135 TransportCommandStart = 1,
136 TransportCommandStop = 2,
137 } transport_command_t;
139 typedef struct {
141 volatile uint32_t guard1;
142 volatile jack_nframes_t frames;
143 volatile jack_time_t current_wakeup;
144 volatile jack_time_t next_wakeup;
145 volatile float second_order_integrator;
146 volatile int32_t initialized;
147 volatile uint32_t guard2;
149 /* not accessed by clients */
151 int32_t reset_pending; /* xrun happened, deal with it */
152 float filter_coefficient; /* set once, never altered */
154 } POST_PACKED_STRUCTURE jack_frame_timer_t;
156 /* JACK engine shared memory data structure. */
157 typedef struct {
159 jack_transport_state_t transport_state;
160 volatile transport_command_t transport_cmd;
161 transport_command_t previous_cmd; /* previous transport_cmd */
162 jack_position_t current_time; /* position for current cycle */
163 jack_position_t pending_time; /* position for next cycle */
164 jack_position_t request_time; /* latest requested position */
165 jack_unique_t prev_request; /* previous request unique ID */
166 volatile _Atomic_word seq_number; /* unique ID sequence number */
167 int8_t new_pos; /* new position this cycle */
168 int8_t pending_pos; /* new position request pending */
169 jack_nframes_t pending_frame; /* pending frame number */
170 int32_t sync_clients; /* number of active_slowsync clients */
171 int32_t sync_remain; /* number of them with sync_poll */
172 jack_time_t sync_timeout;
173 jack_time_t sync_time_left;
174 jack_frame_timer_t frame_timer;
175 int32_t internal;
176 jack_timer_type_t clock_source;
177 pid_t engine_pid;
178 jack_nframes_t buffer_size;
179 int8_t real_time;
180 int8_t do_mlock;
181 int8_t do_munlock;
182 int32_t client_priority;
183 int32_t has_capabilities;
184 float cpu_load;
185 float xrun_delayed_usecs;
186 float max_delayed_usecs;
187 uint32_t port_max;
188 int32_t engine_ok;
189 jack_port_type_id_t n_port_types;
190 jack_port_type_info_t port_types[JACK_MAX_PORT_TYPES];
191 jack_port_shared_t ports[0];
193 } POST_PACKED_STRUCTURE jack_control_t;
195 typedef enum {
196 BufferSizeChange,
197 SampleRateChange,
198 AttachPortSegment,
199 PortConnected,
200 PortDisconnected,
201 GraphReordered,
202 PortRegistered,
203 PortUnregistered,
204 XRun,
205 StartFreewheel,
206 StopFreewheel,
207 ClientRegistered,
208 ClientUnregistered
209 } JackEventType;
211 typedef struct {
212 JackEventType type;
213 union {
214 uint32_t n;
215 char name[JACK_CLIENT_NAME_SIZE];
216 jack_port_id_t port_id;
217 jack_port_id_t self_id;
218 } x;
219 union {
220 uint32_t n;
221 jack_port_type_id_t ptid;
222 jack_port_id_t other_id;
223 } y;
224 } POST_PACKED_STRUCTURE jack_event_t;
226 typedef enum {
227 ClientInternal, /* connect request just names .so */
228 ClientDriver, /* code is loaded along with driver */
229 ClientExternal /* client is in another process */
230 } ClientType;
232 typedef enum {
233 NotTriggered,
234 Triggered,
235 Running,
236 Finished
237 } jack_client_state_t;
239 /* JACK client shared memory data structure. */
240 typedef volatile struct {
242 volatile jack_client_id_t id; /* w: engine r: engine and client */
243 volatile jack_nframes_t nframes; /* w: engine r: client */
244 volatile jack_client_state_t state; /* w: engine and client r: engine */
245 volatile char name[JACK_CLIENT_NAME_SIZE];
246 volatile ClientType type; /* w: engine r: engine and client */
247 volatile int8_t active; /* w: engine r: engine and client */
248 volatile int8_t dead; /* r/w: engine */
249 volatile int8_t timed_out; /* r/w: engine */
250 volatile int8_t is_timebase; /* w: engine, r: engine and client */
251 volatile int8_t timebase_new; /* w: engine and client, r: engine */
252 volatile int8_t is_slowsync; /* w: engine, r: engine and client */
253 volatile int8_t active_slowsync; /* w: engine, r: engine and client */
254 volatile int8_t sync_poll; /* w: engine and client, r: engine */
255 volatile int8_t sync_new; /* w: engine and client, r: engine */
256 volatile pid_t pid; /* w: client r: engine; client pid */
257 volatile pid_t pgrp; /* w: client r: engine; client pgrp */
258 volatile uint64_t signalled_at;
259 volatile uint64_t awake_at;
260 volatile uint64_t finished_at;
261 volatile int32_t last_status; /* w: client, r: engine and client */
263 /* indicators for whether callbacks have been set for this client.
264 We do not include ptrs to the callbacks here (or their arguments)
265 so that we can avoid 32/64 bit pointer size mismatches between
266 the jack server and a client. The pointers are in the client-
267 local structure which is part of the libjack compiled for
268 either 32 bit or 64 bit clients.
270 volatile uint8_t process_cbset;
271 volatile uint8_t thread_init_cbset;
272 volatile uint8_t bufsize_cbset;
273 volatile uint8_t srate_cbset;
274 volatile uint8_t port_register_cbset;
275 volatile uint8_t port_connect_cbset;
276 volatile uint8_t graph_order_cbset;
277 volatile uint8_t xrun_cbset;
278 volatile uint8_t sync_cb_cbset;
279 volatile uint8_t timebase_cb_cbset;
280 volatile uint8_t freewheel_cb_cbset;
281 volatile uint8_t client_register_cbset;
282 volatile uint8_t thread_cb_cbset;
284 } POST_PACKED_STRUCTURE jack_client_control_t;
286 typedef struct {
288 uint32_t protocol_v; /* protocol version, must go first */
289 int32_t load;
290 ClientType type;
291 jack_options_t options;
293 char name[JACK_CLIENT_NAME_SIZE];
294 char object_path[PATH_MAX+1];
295 char object_data[1024];
297 } POST_PACKED_STRUCTURE jack_client_connect_request_t;
299 typedef struct {
301 jack_status_t status;
303 jack_shm_registry_index_t client_shm_index;
304 jack_shm_registry_index_t engine_shm_index;
306 char fifo_prefix[PATH_MAX+1];
308 int32_t realtime;
309 int32_t realtime_priority;
311 char name[JACK_CLIENT_NAME_SIZE]; /* unique name, if assigned */
313 /* these two are valid only for internal clients, and thus
314 are exempt from the requirement that we not export
315 pointers back to clients. an internal client must
316 necessarily match the host, so 32/64 bit issues
317 do not apply to these pointers.
319 jack_client_control_t* client_control;
320 jack_control_t* engine_control;
322 #ifdef JACK_USE_MACH_THREADS
323 /* specific resources for server/client real-time thread communication */
324 int32_t portnum;
325 #endif
327 } POST_PACKED_STRUCTURE jack_client_connect_result_t;
329 typedef struct {
330 jack_client_id_t client_id;
331 } POST_PACKED_STRUCTURE jack_client_connect_ack_request_t;
333 typedef struct {
334 int8_t status;
335 } POST_PACKED_STRUCTURE jack_client_connect_ack_result_t;
337 typedef enum {
338 RegisterPort = 1,
339 UnRegisterPort = 2,
340 ConnectPorts = 3,
341 DisconnectPorts = 4,
342 SetTimeBaseClient = 5,
343 ActivateClient = 6,
344 DeactivateClient = 7,
345 DisconnectPort = 8,
346 SetClientCapabilities = 9,
347 GetPortConnections = 10,
348 GetPortNConnections = 11,
349 ResetTimeBaseClient = 12,
350 SetSyncClient = 13,
351 ResetSyncClient = 14,
352 SetSyncTimeout = 15,
353 SetBufferSize = 16,
354 FreeWheel = 17,
355 StopFreeWheel = 18,
356 IntClientHandle = 19,
357 IntClientLoad = 20,
358 IntClientName = 21,
359 IntClientUnload = 22,
360 RecomputeTotalLatencies = 23,
361 RecomputeTotalLatency = 24
362 } RequestType;
364 struct _jack_request {
366 //RequestType type;
367 uint32_t type;
368 union {
369 struct {
370 char name[JACK_PORT_NAME_SIZE];
371 char type[JACK_PORT_TYPE_SIZE];
372 uint32_t flags;
373 jack_shmsize_t buffer_size;
374 jack_port_id_t port_id;
375 jack_client_id_t client_id;
376 } POST_PACKED_STRUCTURE port_info;
377 struct {
378 char source_port[JACK_PORT_NAME_SIZE];
379 char destination_port[JACK_PORT_NAME_SIZE];
380 } POST_PACKED_STRUCTURE connect;
381 struct {
382 int32_t nports;
383 const char **ports; /* this is only exposed to internal clients, so there
384 is no 64/32 issue. external clients read the ports
385 one by one from the server, and allocate their
386 own "ports" array in their own address space.
388 } POST_PACKED_STRUCTURE port_connections;
389 struct {
390 jack_client_id_t client_id;
391 int32_t conditional;
392 } POST_PACKED_STRUCTURE timebase;
393 struct {
394 //jack_options_t options;
395 uint32_t options;
396 jack_client_id_t id;
397 char name[JACK_CLIENT_NAME_SIZE];
398 char path[PATH_MAX+1];
399 char init[JACK_LOAD_INIT_LIMIT];
400 } POST_PACKED_STRUCTURE intclient;
401 jack_client_id_t client_id;
402 jack_nframes_t nframes;
403 jack_time_t timeout;
404 pid_t cap_pid;
405 } POST_PACKED_STRUCTURE x;
406 int32_t status;
407 } POST_PACKED_STRUCTURE;
409 /* Per-client structure allocated in the server's address space.
410 * It's here because its not part of the engine structure.
413 typedef struct _jack_client_internal {
415 jack_client_control_t *control;
417 int request_fd;
418 int event_fd;
419 int subgraph_start_fd;
420 int subgraph_wait_fd;
421 JSList *ports; /* protected by engine->client_lock */
422 JSList *truefeeds; /* protected by engine->client_lock */
423 JSList *sortfeeds; /* protected by engine->client_lock */
424 int fedcount;
425 int tfedcount;
426 jack_shm_info_t control_shm;
427 unsigned long execution_order;
428 struct _jack_client_internal *next_client; /* not a linked list! */
429 dlhandle handle;
430 int (*initialize)(jack_client_t*, const char*); /* int. clients only */
431 void (*finish)(void *); /* internal clients only */
432 int error;
434 #ifdef JACK_USE_MACH_THREADS
435 /* specific resources for server/client real-time thread communication */
436 mach_port_t serverport;
437 trivial_message message;
438 int running;
439 int portnum;
440 #endif /* JACK_USE_MACH_THREADS */
442 #if 0
443 /* callbacks
445 JackProcessCallback process;
446 void *process_arg;
447 JackThreadInitCallback thread_init;
448 void *thread_init_arg;
449 JackBufferSizeCallback bufsize;
450 void *bufsize_arg;
451 JackSampleRateCallback srate;
452 void *srate_arg;
453 JackPortRegistrationCallback port_register;
454 void *port_register_arg;
455 JackPortConnectCallback port_connect;
456 void *port_connect_arg;
457 JackGraphOrderCallback graph_order;
458 void *graph_order_arg;
459 JackXRunCallback xrun;
460 void *xrun_arg;
461 JackSyncCallback sync_cb;
462 void *sync_arg;
463 JackTimebaseCallback timebase_cb;
464 void *timebase_arg;
465 JackFreewheelCallback freewheel_cb;
466 void *freewheel_arg;
467 JackClientRegistrationCallback client_register;
468 void *client_register_arg;
469 JackThreadCallback thread_cb;
470 void *thread_cb_arg;
471 #endif
472 /* external clients: set by libjack
473 * internal clients: set by engine */
474 //int (*deliver_request)(void*, jack_request_t*); /* JOQ: 64/32 bug! */
475 //void *deliver_arg;
476 jack_client_t *private_client;
477 } jack_client_internal_t;
479 typedef struct _jack_thread_arg {
480 jack_client_t* client;
481 void* (*work_function)(void*);
482 int priority;
483 int realtime;
484 void* arg;
485 pid_t cap_pid;
486 } jack_thread_arg_t;
488 extern int jack_client_handle_port_connection (jack_client_t *client,
489 jack_event_t *event);
490 extern jack_client_t *jack_driver_client_new (jack_engine_t *,
491 const char *client_name);
492 extern jack_client_t *jack_client_alloc_internal (jack_client_control_t*,
493 jack_engine_t*);
495 /* internal clients call this. it's defined in jack/engine.c */
496 void handle_internal_client_request (jack_control_t*, jack_request_t*);
498 extern char *jack_tmpdir;
500 extern char *jack_user_dir (void);
502 extern char *jack_server_dir (const char *server_name, char *server_dir);
504 extern void *jack_zero_filled_buffer;
506 extern jack_port_functions_t jack_builtin_audio_functions;
508 extern jack_port_type_info_t jack_builtin_port_types[];
510 extern void jack_client_invalidate_port_buffers (jack_client_t *client);
512 extern void jack_transport_copy_position (jack_position_t *from,
513 jack_position_t *to);
514 extern void jack_call_sync_client (jack_client_t *client);
516 extern void jack_call_timebase_master (jack_client_t *client);
518 extern char *jack_default_server_name (void);
520 void silent_jack_error_callback (const char *desc);
522 /* needed for port management */
523 jack_port_t *jack_port_by_id_int (const jack_client_t *client,
524 jack_port_id_t id, int* free);
526 jack_port_t *jack_port_by_name_int (jack_client_t *client,
527 const char *port_name);
528 int jack_port_name_equals (jack_port_shared_t* port, const char* target);
530 #ifdef __GNUC__
531 # define likely(x) __builtin_expect((x),1)
532 # define unlikely(x) __builtin_expect((x),0)
533 #else
534 # define likely(x) (x)
535 # define unlikely(x) (x)
536 #endif
538 #endif /* __jack_internal_h__ */