]> git.sesse.net Git - vlc/blob - src/interface/dialog.c
decoder: do not wait for buffering when there is no data
[vlc] / src / interface / dialog.c
1 /*****************************************************************************
2  * dialog.c: User dialog functions
3  *****************************************************************************
4  * Copyright © 2009 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 /**
22  * \file dialog.c
23  * User dialogs core
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdarg.h>
31
32 #include <vlc_common.h>
33 #include <vlc_dialog.h>
34 #include <vlc_extensions.h>
35 #include <assert.h>
36 #include "libvlc.h"
37
38 static vlc_mutex_t provider_lock = VLC_STATIC_MUTEX;
39
40 #undef dialog_Register
41 /**
42  * Registers an object as the dialog provider.
43  * It is assumed that the appropriate variable callbacks are already
44  * registered.
45  */
46 int dialog_Register (vlc_object_t *obj)
47 {
48     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
49     int ret = VLC_EGENERIC;
50
51     vlc_mutex_lock (&provider_lock);
52     if (priv->p_dialog_provider == NULL)
53     {   /* Since the object is responsible for unregistering itself before
54          * it terminates, at reference is not needed. */
55         priv->p_dialog_provider = obj;
56         ret = VLC_SUCCESS;
57     }
58     vlc_mutex_unlock (&provider_lock);
59     return ret;
60 }
61
62 #undef dialog_Unregister
63 /**
64  * Unregisters the dialog provider.
65  * Note that unless you have unregistered the callbacks already, the provider
66  * might still be in use by other threads. Also, you need to cancel all
67  * pending dialogs yourself.
68  */
69 int dialog_Unregister (vlc_object_t *obj)
70 {
71     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
72     int ret = VLC_EGENERIC;
73
74     vlc_mutex_lock (&provider_lock);
75     if (priv->p_dialog_provider == obj)
76     {
77         priv->p_dialog_provider = NULL;
78         ret = VLC_SUCCESS;
79     }
80     vlc_mutex_unlock (&provider_lock);
81     return ret;
82 }
83
84 static vlc_object_t *dialog_GetProvider (vlc_object_t *obj)
85 {
86     libvlc_priv_t *priv = libvlc_priv (obj->p_libvlc);
87     vlc_object_t *provider;
88
89     vlc_mutex_lock (&provider_lock);
90     if ((provider = priv->p_dialog_provider) != NULL)
91         vlc_object_hold (provider);
92     vlc_mutex_unlock (&provider_lock);
93     return provider;
94 }
95
96 /**
97  * Sends an error message through the user interface (if any).
98  * @param obj the VLC object emitting the error
99  * @param modal whether to wait for user to acknowledge the error
100  *              before returning control to the caller
101  * @param title title of the error dialog
102  * @param fmt format string for the error message
103  * @param ap parameters list for the formatted error message
104  */
105 void dialog_VFatal (vlc_object_t *obj, bool modal, const char *title,
106                     const char *fmt, va_list ap)
107 {
108     char *text;
109
110     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
111         return;
112
113     vlc_object_t *provider = dialog_GetProvider (obj);
114     if (provider == NULL)
115     {
116         msg_Err (obj, "%s", title);
117         msg_GenericVa (obj, VLC_MSG_ERR, fmt, ap);
118         return;
119     }
120
121     if (vasprintf (&text, fmt, ap) != -1)
122     {
123         dialog_fatal_t dialog = { title, text, };
124         var_SetAddress (provider,
125                         modal ? "dialog-critical" : "dialog-error", &dialog);
126         free (text);
127     }
128     vlc_object_release (provider);
129 }
130
131 #undef dialog_Login
132 /**
133  * Requests a username and password through the user interface.
134  * @param obj the VLC object requesting credential information
135  * @param username a pointer to the specified username [OUT]
136  * @param password a pointer to the specified password [OUT]
137  * @param title title for the dialog
138  * @param text format string for the message in the dialog
139  * @return Nothing. If a user name resp. a password was specified,
140  * it will be returned as a heap-allocated character array
141  * into the username resp password pointer. Those must be freed with free().
142  * Otherwise *username resp *password will be NULL.
143  */
144 void dialog_Login (vlc_object_t *obj, char **username, char **password,
145                    const char *title, const char *fmt, ...)
146 {
147     assert ((username != NULL) && (password != NULL));
148
149     *username = *password = NULL;
150     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
151         return;
152
153     vlc_object_t *provider = dialog_GetProvider (obj);
154     if (provider == NULL)
155         return;
156
157     char *text;
158     va_list ap;
159
160     va_start (ap, fmt);
161     if (vasprintf (&text, fmt, ap) != -1)
162     {
163         dialog_login_t dialog = { title, text, username, password, };
164         var_SetAddress (provider, "dialog-login", &dialog);
165         free (text);
166     }
167     va_end (ap);
168     vlc_object_release (provider);
169 }
170
171
172 #undef dialog_Question
173 /**
174  * Asks a total (Yes/No/Cancel) question through the user interface.
175  * @param obj VLC object emitting the question
176  * @param title dialog box title
177  * @param fmt format string for the dialog box text
178  * @param yes first choice/button text
179  * @param no second choice/button text
180  * @param cancel third answer/button text, or NULL if no third option
181  * @return 0 if the user could not answer the question (e.g. there is no UI),
182  * 1, 2 resp. 3 if the user pressed the first, second resp. third button.
183  */
184 int dialog_Question (vlc_object_t *obj, const char *title, const char *fmt,
185                      const char *yes, const char *no, const char *cancel, ...)
186 {
187     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
188         return 0;
189
190     vlc_object_t *provider = dialog_GetProvider (obj);
191     if (provider == NULL)
192         return 0;
193
194     char *text;
195     va_list ap;
196     int answer = 0;
197
198     va_start (ap, cancel);
199     if (vasprintf (&text, fmt, ap) != -1)
200     {
201         dialog_question_t dialog = { title, text, yes, no, cancel, 0, };
202         var_SetAddress (provider, "dialog-question", &dialog);
203         answer = dialog.answer;
204     }
205     va_end (ap);
206     vlc_object_release (provider);
207     return answer;
208 }
209
210 #undef dialog_ProgressCreate
211 /**
212  * Creates a progress bar dialog.
213  */
214 dialog_progress_bar_t *
215 dialog_ProgressCreate (vlc_object_t *obj, const char *title,
216                        const char *message, const char *cancel)
217 {
218     if (obj->i_flags & OBJECT_FLAGS_NOINTERACT)
219         return NULL;
220
221     vlc_object_t *provider = dialog_GetProvider (obj);
222     if (provider == NULL)
223         return NULL;
224
225     dialog_progress_bar_t *dialog = malloc (sizeof (*dialog));
226     if (dialog != NULL)
227     {
228         dialog->title = title;
229         dialog->message = message;
230         dialog->cancel = cancel;
231         var_SetAddress (provider, "dialog-progress-bar", dialog);
232 #ifndef NDEBUG
233         dialog->title = dialog->message = dialog->cancel = NULL;
234 #endif
235         assert (dialog->pf_update);
236         assert (dialog->pf_check);
237         assert (dialog->pf_destroy);
238     }
239
240     /* FIXME: This could conceivably crash if the dialog provider is destroyed
241      * before the dialog user. Holding the provider does not help, as it only
242      * protects object variable operations. For instance, it does not prevent
243      * unloading of the interface plugin. In the short term, the only solution
244      * is to not use progress dialog after deinitialization of the interfaces.
245      */
246     vlc_object_release (provider);
247     return dialog;
248 }
249
250 void dialog_ProgressDestroy (dialog_progress_bar_t *dialog)
251 {
252     assert (dialog);
253
254     dialog->pf_destroy (dialog->p_sys);
255     free (dialog);
256 }
257
258 void dialog_ProgressSet (dialog_progress_bar_t *dialog, const char *text,
259                          float value)
260 {
261     assert (dialog);
262
263     dialog->pf_update (dialog->p_sys, text, value);
264 }
265
266 bool dialog_ProgressCancelled (dialog_progress_bar_t *dialog)
267 {
268     assert (dialog);
269
270     return dialog->pf_check (dialog->p_sys);
271 }
272
273 #undef dialog_ExtensionUpdate
274 int dialog_ExtensionUpdate (vlc_object_t *obj, extension_dialog_t *dialog)
275 {
276     assert (obj);
277     assert (dialog);
278
279     vlc_object_t *dp = dialog_GetProvider(obj);
280     if (!dp)
281     {
282         msg_Warn (obj, "Dialog provider is not set, can't update dialog '%s'",
283                   dialog->psz_title);
284         return VLC_EGENERIC;
285     }
286
287     // Signaling the dialog provider
288     int ret = var_SetAddress (dp, "dialog-extension", dialog);
289
290     vlc_object_release (dp);
291     return ret;
292 }