~ubuntu-branches/ubuntu/saucy/xmms2/saucy-proposed

« back to all changes in this revision

Viewing changes to doc/tutorial/c/tut8.c

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2011-10-22 23:53:00 UTC
  • mto: (38.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 43.
  • Revision ID: james.westby@ubuntu.com-20111022235300-u50pdo3g341jvk7q
ImportĀ upstreamĀ versionĀ 0.8+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  XMMS2 - X Music Multiplexer System
 
2
 *  Copyright (C) 2003-2006 XMMS2 Team
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
16
 */
 
17
 
 
18
#include <stdlib.h>
 
19
 
 
20
/* include xmmsclient header */
 
21
#include <xmmsclient/xmmsclient.h>
 
22
 
 
23
/* also include this to get glib integration */
 
24
#include <xmmsclient/xmmsclient-glib.h>
 
25
 
 
26
/* include the GLib header */
 
27
#include <glib.h>
 
28
 
 
29
 
 
30
/*
 
31
 * We set this up as a callback for our current_id
 
32
 * method. Read the main program first before
 
33
 * returning here.
 
34
 */
 
35
int
 
36
my_playtime (xmmsv_t *value, void *userdata)
 
37
{
 
38
        /*
 
39
         * At this point the result struct is filled with the
 
40
         * answer. And we can now extract it as normal.
 
41
         */
 
42
        int time;
 
43
 
 
44
        int keep_alive;
 
45
 
 
46
        /*
 
47
         * we passed the mainloop as an argument
 
48
         * to set_notifier, which means it will be
 
49
         * passed as userdata to this function
 
50
         */
 
51
        GMainLoop *ml = (GMainLoop *) userdata;
 
52
 
 
53
        if (!xmmsv_get_int (value, &time)) {
 
54
                fprintf (stderr, "Value didn't contain the expected type!\n");
 
55
                exit (EXIT_FAILURE);
 
56
        }
 
57
 
 
58
        /*
 
59
         * Print the time on the same line and flush stdout
 
60
         * so that the text becomes visible.
 
61
         */
 
62
        printf ("\r%4dms has been played", time);
 
63
        fflush (stdout);
 
64
 
 
65
        /*
 
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.
 
69
         */
 
70
        if (time > 5000) {
 
71
                printf ("\nMore than 5000 ms has been played, exiting...\n");
 
72
                g_main_loop_quit (ml);
 
73
                keep_alive = FALSE;
 
74
        } else {
 
75
                /* Tell the server to send updates to the
 
76
                 * same callback (AKA restart the signal) */
 
77
                keep_alive = TRUE;
 
78
        }
 
79
 
 
80
        /*
 
81
         * We use the return value of the callback once
 
82
         * again to determine whether to restart the signal
 
83
         * or just let it die.
 
84
         */
 
85
        return keep_alive;
 
86
}
 
87
 
 
88
int
 
89
main (int argc, char **argv)
 
90
{
 
91
        /* The mainloop we should use later */
 
92
        GMainLoop *ml;
 
93
 
 
94
        /*
 
95
         * The first part of this program is
 
96
         * commented on in tut1.c
 
97
         */
 
98
        xmmsc_connection_t *connection;
 
99
        xmmsc_result_t *result;
 
100
 
 
101
        /*
 
102
         * In an async client we still connect as
 
103
         * normal. Read up on this in earlier
 
104
         * tutorials if you need.
 
105
         */
 
106
        connection = xmmsc_init ("tutorial6");
 
107
        if (!connection) {
 
108
                fprintf (stderr, "OOM!\n");
 
109
                exit (EXIT_FAILURE);
 
110
        }
 
111
 
 
112
        if (!xmmsc_connect (connection, getenv ("XMMS_PATH"))) {
 
113
                fprintf (stderr, "Connection failed: %s\n",
 
114
                         xmmsc_get_last_error (connection));
 
115
 
 
116
                exit (EXIT_FAILURE);
 
117
        }
 
118
 
 
119
        /*
 
120
         * Initialize the mainloop, for more information about GLib mainloop
 
121
         * see the GTK docs.
 
122
         */
 
123
        ml = g_main_loop_new (NULL, FALSE);
 
124
 
 
125
        /*
 
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.
 
129
         */
 
130
        xmmsc_result_unref (xmmsc_playback_stop (connection));
 
131
        xmmsc_result_unref (xmmsc_playback_start (connection));
 
132
 
 
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);
 
137
 
 
138
        /*
 
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.
 
144
         *
 
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.
 
150
         */
 
151
        xmmsc_mainloop_gmain_init (connection);
 
152
 
 
153
        /*
 
154
         * We are now all set to go. Just run the main loop and watch the magic.
 
155
         */
 
156
 
 
157
        g_main_loop_run (ml);
 
158
 
 
159
        return EXIT_SUCCESS;
 
160
 
 
161
}