1
/* XMMS2 - X Music Multiplexer System
2
* Copyright (C) 2003-2006 XMMS2 Team
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* This file is a part of the XMMS2 client tutorial #8
15
* Using a callback to catch a signal and restart it a few times
20
/* include xmmsclient header */
21
#include <xmmsclient/xmmsclient.h>
23
/* also include this to get glib integration */
24
#include <xmmsclient/xmmsclient-glib.h>
26
/* include the GLib header */
31
* We set this up as a callback for our current_id
32
* method. Read the main program first before
36
my_playtime (xmmsv_t *value, void *userdata)
39
* At this point the result struct is filled with the
40
* answer. And we can now extract it as normal.
47
* we passed the mainloop as an argument
48
* to set_notifier, which means it will be
49
* passed as userdata to this function
51
GMainLoop *ml = (GMainLoop *) userdata;
53
if (!xmmsv_get_int (value, &time)) {
54
fprintf (stderr, "Value didn't contain the expected type!\n");
59
* Print the time on the same line and flush stdout
60
* so that the text becomes visible.
62
printf ("\r%4dms has been played", time);
66
* We will exit the application after 5000ms have passed.
67
* Otherwise we will restart the signal to enable this
68
* callback to be called yet another time.
71
printf ("\nMore than 5000 ms has been played, exiting...\n");
72
g_main_loop_quit (ml);
75
/* Tell the server to send updates to the
76
* same callback (AKA restart the signal) */
81
* We use the return value of the callback once
82
* again to determine whether to restart the signal
89
main (int argc, char **argv)
91
/* The mainloop we should use later */
95
* The first part of this program is
96
* commented on in tut1.c
98
xmmsc_connection_t *connection;
99
xmmsc_result_t *result;
102
* In an async client we still connect as
103
* normal. Read up on this in earlier
104
* tutorials if you need.
106
connection = xmmsc_init ("tutorial6");
108
fprintf (stderr, "OOM!\n");
112
if (!xmmsc_connect (connection, getenv ("XMMS_PATH"))) {
113
fprintf (stderr, "Connection failed: %s\n",
114
xmmsc_get_last_error (connection));
120
* Initialize the mainloop, for more information about GLib mainloop
123
ml = g_main_loop_new (NULL, FALSE);
126
* We issue two async commands to restart playback.
127
* Since we don't want to set a notifier for those,
128
* we can free the result immediately.
130
xmmsc_result_unref (xmmsc_playback_stop (connection));
131
xmmsc_result_unref (xmmsc_playback_start (connection));
133
printf ("Playtime: \n");
134
result = xmmsc_signal_playback_playtime (connection);
135
xmmsc_result_notifier_set (result, my_playtime, ml);
136
xmmsc_result_unref (result);
139
* As you see we do it pretty much the same way that we did in tut2, but
140
* instead of being able to access the current id directly (as we would
141
* have if we where blocking) we need to wait until xmms calls our
142
* my_current_id function. This will keep your GUI from hanging while
143
* waiting for xmms2 to answer your command.
145
* In order to make xmmsclient call your callback functions we need to put
146
* the fd of the connection into the mainloop of our program. For your
147
* convenience the xmmsclient lib ships with automatic integration with
148
* GMainLoop. We just need to link with xmmsclient-glib and do the following
149
* call to make it work.
151
xmmsc_mainloop_gmain_init (connection);
154
* We are now all set to go. Just run the main loop and watch the magic.
157
g_main_loop_run (ml);