1 /*****************************************************************************
2  * dialog.c: libvlc dialog API
3  *****************************************************************************
4  * Copyright © 2016 VLC authors and VideoLAN
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 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 
25 #include <assert.h>
26 
27 #include <vlc/libvlc.h>
28 #include <vlc/libvlc_dialog.h>
29 
30 #include <vlc_common.h>
31 #include <vlc_dialog.h>
32 
33 #include "libvlc_internal.h"
34 
35 static libvlc_dialog_question_type
vlc_to_libvlc_dialog_question_type(vlc_dialog_question_type i_type)36 vlc_to_libvlc_dialog_question_type(vlc_dialog_question_type i_type)
37 {
38     switch (i_type)
39     {
40     case VLC_DIALOG_QUESTION_NORMAL: return LIBVLC_DIALOG_QUESTION_NORMAL;
41     case VLC_DIALOG_QUESTION_WARNING: return LIBVLC_DIALOG_QUESTION_WARNING;
42     case VLC_DIALOG_QUESTION_CRITICAL: return LIBVLC_DIALOG_QUESTION_CRITICAL;
43     default: vlc_assert_unreachable();
44     }
45 }
46 
47 static void
display_error_cb(void * p_data,const char * psz_title,const char * psz_text)48 display_error_cb(void *p_data, const char *psz_title, const char *psz_text)
49 {
50     libvlc_instance_t *p_instance = p_data;
51 
52     p_instance->dialog.cbs.pf_display_error(p_instance->dialog.data, psz_title,
53                                             psz_text);
54 }
55 
56 static void
display_login_cb(void * p_data,vlc_dialog_id * p_id,const char * psz_title,const char * psz_text,const char * psz_default_username,bool b_ask_store)57 display_login_cb(void *p_data, vlc_dialog_id *p_id, const char *psz_title,
58                  const char *psz_text, const char *psz_default_username,
59                  bool b_ask_store)
60 {
61     libvlc_instance_t *p_instance = p_data;
62 
63     p_instance->dialog.cbs.pf_display_login(p_instance->dialog.data,
64                                             (libvlc_dialog_id *) p_id,
65                                             psz_title, psz_text,
66                                             psz_default_username, b_ask_store);
67 }
68 
69 static void
display_question_cb(void * p_data,vlc_dialog_id * p_id,const char * psz_title,const char * psz_text,vlc_dialog_question_type i_type,const char * psz_cancel,const char * psz_action1,const char * psz_action2)70 display_question_cb(void *p_data, vlc_dialog_id *p_id, const char *psz_title,
71                     const char *psz_text, vlc_dialog_question_type i_type,
72                     const char *psz_cancel, const char *psz_action1,
73                     const char *psz_action2)
74 {
75     libvlc_instance_t *p_instance = p_data;
76     const libvlc_dialog_question_type i_ltype =
77         vlc_to_libvlc_dialog_question_type(i_type);
78 
79     p_instance->dialog.cbs.pf_display_question(p_instance->dialog.data,
80                                                (libvlc_dialog_id *) p_id,
81                                                psz_title, psz_text, i_ltype,
82                                                psz_cancel,
83                                                psz_action1, psz_action2);
84 }
85 
86 static void
display_progress_cb(void * p_data,vlc_dialog_id * p_id,const char * psz_title,const char * psz_text,bool b_indeterminate,float f_position,const char * psz_cancel)87 display_progress_cb(void *p_data, vlc_dialog_id *p_id, const char *psz_title,
88                     const char *psz_text, bool b_indeterminate,
89                     float f_position, const char *psz_cancel)
90 {
91     libvlc_instance_t *p_instance = p_data;
92 
93     p_instance->dialog.cbs.pf_display_progress(p_instance->dialog.data,
94                                                (libvlc_dialog_id *) p_id,
95                                                psz_title, psz_text,
96                                                b_indeterminate, f_position,
97                                                psz_cancel);
98 }
99 
100 static void
cancel_cb(void * p_data,vlc_dialog_id * p_id)101 cancel_cb(void *p_data, vlc_dialog_id *p_id)
102 {
103     libvlc_instance_t *p_instance = p_data;
104     p_instance->dialog.cbs.pf_cancel(p_instance->dialog.data,
105                                      (libvlc_dialog_id *)p_id);
106 }
107 
108 static void
update_progress_cb(void * p_data,vlc_dialog_id * p_id,float f_position,const char * psz_text)109 update_progress_cb(void *p_data, vlc_dialog_id *p_id, float f_position,
110                    const char *psz_text)
111 {
112     libvlc_instance_t *p_instance = p_data;
113     p_instance->dialog.cbs.pf_update_progress(p_instance->dialog.data,
114                                               (libvlc_dialog_id *) p_id,
115                                               f_position, psz_text);
116 }
117 
118 void
libvlc_dialog_set_callbacks(libvlc_instance_t * p_instance,const libvlc_dialog_cbs * p_cbs,void * p_data)119 libvlc_dialog_set_callbacks(libvlc_instance_t *p_instance,
120                             const libvlc_dialog_cbs *p_cbs, void *p_data)
121 {
122     libvlc_int_t *p_libvlc = p_instance->p_libvlc_int;
123 
124     vlc_mutex_lock(&p_instance->instance_lock);
125     if (p_cbs != NULL)
126     {
127         const vlc_dialog_cbs dialog_cbs = {
128             .pf_display_error = p_cbs->pf_display_error != NULL ?
129                                 display_error_cb : NULL,
130             .pf_display_login = p_cbs->pf_display_login ?
131                                 display_login_cb : NULL,
132             .pf_display_question = p_cbs->pf_display_question != NULL ?
133                                    display_question_cb : NULL,
134             .pf_display_progress = p_cbs->pf_display_progress != NULL ?
135                                    display_progress_cb : NULL,
136             .pf_cancel = p_cbs->pf_cancel != NULL ? cancel_cb : NULL,
137             .pf_update_progress = p_cbs->pf_update_progress != NULL ?
138                                   update_progress_cb : NULL,
139         };
140 
141         p_instance->dialog.cbs = *p_cbs;
142         p_instance->dialog.data = p_data;
143 
144         vlc_dialog_provider_set_callbacks(p_libvlc, &dialog_cbs, p_instance);
145     }
146     else
147         vlc_dialog_provider_set_callbacks(p_libvlc, NULL, NULL);
148     vlc_mutex_unlock(&p_instance->instance_lock);
149 }
150 
151 void
libvlc_dialog_set_context(libvlc_dialog_id * p_id,void * p_context)152 libvlc_dialog_set_context(libvlc_dialog_id *p_id, void *p_context)
153 {
154     vlc_dialog_id_set_context((vlc_dialog_id *)p_id, p_context);
155 }
156 
157 void *
libvlc_dialog_get_context(libvlc_dialog_id * p_id)158 libvlc_dialog_get_context(libvlc_dialog_id *p_id)
159 {
160     return vlc_dialog_id_get_context((vlc_dialog_id *)p_id);
161 }
162 
163 int
libvlc_dialog_post_login(libvlc_dialog_id * p_id,const char * psz_username,const char * psz_password,bool b_store)164 libvlc_dialog_post_login(libvlc_dialog_id *p_id, const char *psz_username,
165                          const char *psz_password, bool b_store)
166 {
167     int i_ret = vlc_dialog_id_post_login((vlc_dialog_id *)p_id, psz_username,
168                                          psz_password, b_store);
169     return i_ret == VLC_SUCCESS ? 0 : -1;
170 }
171 
172 int
libvlc_dialog_post_action(libvlc_dialog_id * p_id,int i_action)173 libvlc_dialog_post_action(libvlc_dialog_id *p_id, int i_action)
174 {
175     int i_ret = vlc_dialog_id_post_action((vlc_dialog_id *)p_id, i_action);
176     return i_ret == VLC_SUCCESS ? 0 : -1;
177 }
178 
179 int
libvlc_dialog_dismiss(libvlc_dialog_id * p_id)180 libvlc_dialog_dismiss(libvlc_dialog_id *p_id)
181 {
182     int i_ret = vlc_dialog_id_dismiss((vlc_dialog_id *)p_id);
183     return i_ret == VLC_SUCCESS ? 0 : -1;
184 }
185