Improve computation of the alsa control device name
[jack2.git] / example-clients / simple_session_client.c
blob8d06127c0abee6aff670d5baa5b02e34e71c6484
1 /** @file simple_session_client.c
3 * @brief This simple client demonstrates the most basic features of JACK
4 * as they would be used by many applications.
5 * this version also adds session manager functionality.
6 */
8 #include <stdio.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include <jack/jack.h>
15 #include <jack/types.h>
16 #include <jack/session.h>
18 jack_port_t *input_port;
19 jack_port_t *output_port;
20 jack_client_t *client;
22 int simple_quit = 0;
24 /**
25 * The process callback for this JACK application is called in a
26 * special realtime thread once for each audio cycle.
28 * This client does nothing more than copy data from its input
29 * port to its output port. It will exit when stopped by
30 * the user (e.g. using Ctrl-C on a unix-ish operating system)
32 int
33 process (jack_nframes_t nframes, void *arg)
35 jack_default_audio_sample_t *in, *out;
37 in = jack_port_get_buffer (input_port, nframes);
38 out = jack_port_get_buffer (output_port, nframes);
39 memcpy (out, in,
40 sizeof (jack_default_audio_sample_t) * nframes);
42 return 0;
45 void
46 session_callback (jack_session_event_t *event, void *arg)
48 char retval[100];
49 printf ("session notification\n");
50 printf ("path %s, uuid %s, type: %s\n", event->session_dir, event->client_uuid, event->type == JackSessionSave ? "save" : "quit");
53 snprintf (retval, 100, "jack_simple_session_client %s", event->client_uuid);
54 event->command_line = strdup (retval);
56 jack_session_reply( client, event );
58 if (event->type == JackSessionSaveAndQuit) {
59 simple_quit = 1;
62 jack_session_event_free (event);
65 /**
66 * JACK calls this shutdown_callback if the server ever shuts down or
67 * decides to disconnect the client.
69 void
70 jack_shutdown (void *arg)
72 exit (1);
75 int
76 main (int argc, char *argv[])
78 const char **ports;
79 const char *client_name = "simple";
80 jack_status_t status;
82 /* open a client connection to the JACK server */
84 if( argc == 1 )
85 client = jack_client_open (client_name, JackNullOption, &status );
86 else if( argc == 2 )
87 client = jack_client_open (client_name, JackSessionID, &status, argv[1] );
89 if (client == NULL) {
90 fprintf (stderr, "jack_client_open() failed, "
91 "status = 0x%2.0x\n", status);
92 if (status & JackServerFailed) {
93 fprintf (stderr, "Unable to connect to JACK server\n");
95 exit (1);
97 if (status & JackServerStarted) {
98 fprintf (stderr, "JACK server started\n");
100 if (status & JackNameNotUnique) {
101 client_name = jack_get_client_name(client);
102 fprintf (stderr, "unique name `%s' assigned\n", client_name);
105 /* tell the JACK server to call `process()' whenever
106 there is work to be done.
109 jack_set_process_callback (client, process, 0);
111 /* tell the JACK server to call `jack_shutdown()' if
112 it ever shuts down, either entirely, or if it
113 just decides to stop calling us.
116 jack_on_shutdown (client, jack_shutdown, 0);
118 /* tell the JACK server to call `session_callback()' if
119 the session is saved.
122 jack_set_session_callback (client, session_callback, NULL);
124 /* display the current sample rate.
127 printf ("engine sample rate: %" PRIu32 "\n",
128 jack_get_sample_rate (client));
130 /* create two ports */
132 input_port = jack_port_register (client, "input",
133 JACK_DEFAULT_AUDIO_TYPE,
134 JackPortIsInput, 0);
135 output_port = jack_port_register (client, "output",
136 JACK_DEFAULT_AUDIO_TYPE,
137 JackPortIsOutput, 0);
139 if ((input_port == NULL) || (output_port == NULL)) {
140 fprintf(stderr, "no more JACK ports available\n");
141 exit (1);
144 /* Tell the JACK server that we are ready to roll. Our
145 * process() callback will start running now. */
147 if (jack_activate (client)) {
148 fprintf (stderr, "cannot activate client");
149 exit (1);
152 /* Connect the ports. You can't do this before the client is
153 * activated, because we can't make connections to clients
154 * that aren't running. Note the confusing (but necessary)
155 * orientation of the driver backend ports: playback ports are
156 * "input" to the backend, and capture ports are "output" from
157 * it.
161 /* only do the autoconnect when not reloading from a session.
162 * in case of a session reload, the SM will restore our connections
165 if (argc==1) {
167 ports = jack_get_ports (client, NULL, NULL,
168 JackPortIsPhysical|JackPortIsOutput);
169 if (ports == NULL) {
170 fprintf(stderr, "no physical capture ports\n");
171 exit (1);
174 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
175 fprintf (stderr, "cannot connect input ports\n");
178 free (ports);
180 ports = jack_get_ports (client, NULL, NULL,
181 JackPortIsPhysical|JackPortIsInput);
182 if (ports == NULL) {
183 fprintf(stderr, "no physical playback ports\n");
184 exit (1);
187 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
188 fprintf (stderr, "cannot connect output ports\n");
191 free (ports);
194 /* keep running until until we get a quit event */
196 while (!simple_quit)
197 sleep(1);
200 jack_client_close (client);
201 exit (0);