dialog.c 6.62 KB
Newer Older
Thomas Guillem's avatar
Thomas Guillem committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*****************************************************************************
 * dialog.c: libvlc dialog API
 *****************************************************************************
 * Copyright © 2016 VLC authors and VideoLAN
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>

#include <vlc/libvlc.h>
#include <vlc/libvlc_dialog.h>

#include <vlc_common.h>
#include <vlc_dialog.h>

#include "libvlc_internal.h"

static libvlc_dialog_question_type
vlc_to_libvlc_dialog_question_type(vlc_dialog_question_type i_type)
{
    switch (i_type)
    {
    case VLC_DIALOG_QUESTION_NORMAL: return LIBVLC_DIALOG_QUESTION_NORMAL;
    case VLC_DIALOG_QUESTION_WARNING: return LIBVLC_DIALOG_QUESTION_WARNING;
    case VLC_DIALOG_QUESTION_CRITICAL: return LIBVLC_DIALOG_QUESTION_CRITICAL;
    default: vlc_assert_unreachable();
    }
}

static void
48
display_error_cb(void *p_data, const char *psz_title, const char *psz_text)
Thomas Guillem's avatar
Thomas Guillem committed
49 50 51
{
    libvlc_instance_t *p_instance = p_data;

52 53
    p_instance->dialog.cbs.pf_display_error(p_instance->dialog.data, psz_title,
                                            psz_text);
Thomas Guillem's avatar
Thomas Guillem committed
54 55 56
}

static void
57
display_login_cb(void *p_data, vlc_dialog_id *p_id, const char *psz_title,
Thomas Guillem's avatar
Thomas Guillem committed
58
                 const char *psz_text, const char *psz_default_username,
59
                 bool b_ask_store)
Thomas Guillem's avatar
Thomas Guillem committed
60 61 62
{
    libvlc_instance_t *p_instance = p_data;

63 64 65 66
    p_instance->dialog.cbs.pf_display_login(p_instance->dialog.data,
                                            (libvlc_dialog_id *) p_id,
                                            psz_title, psz_text,
                                            psz_default_username, b_ask_store);
Thomas Guillem's avatar
Thomas Guillem committed
67 68 69
}

static void
70
display_question_cb(void *p_data, vlc_dialog_id *p_id, const char *psz_title,
Thomas Guillem's avatar
Thomas Guillem committed
71 72
                    const char *psz_text, vlc_dialog_question_type i_type,
                    const char *psz_cancel, const char *psz_action1,
73
                    const char *psz_action2)
Thomas Guillem's avatar
Thomas Guillem committed
74 75 76 77 78
{
    libvlc_instance_t *p_instance = p_data;
    const libvlc_dialog_question_type i_ltype =
        vlc_to_libvlc_dialog_question_type(i_type);

79 80
    p_instance->dialog.cbs.pf_display_question(p_instance->dialog.data,
                                               (libvlc_dialog_id *) p_id,
Thomas Guillem's avatar
Thomas Guillem committed
81 82
                                               psz_title, psz_text, i_ltype,
                                               psz_cancel,
83
                                               psz_action1, psz_action2);
Thomas Guillem's avatar
Thomas Guillem committed
84 85 86
}

static void
87
display_progress_cb(void *p_data, vlc_dialog_id *p_id, const char *psz_title,
Thomas Guillem's avatar
Thomas Guillem committed
88
                    const char *psz_text, bool b_indeterminate,
89
                    float f_position, const char *psz_cancel)
Thomas Guillem's avatar
Thomas Guillem committed
90 91 92
{
    libvlc_instance_t *p_instance = p_data;

93 94
    p_instance->dialog.cbs.pf_display_progress(p_instance->dialog.data,
                                               (libvlc_dialog_id *) p_id,
Thomas Guillem's avatar
Thomas Guillem committed
95 96
                                               psz_title, psz_text,
                                               b_indeterminate, f_position,
97
                                               psz_cancel);
Thomas Guillem's avatar
Thomas Guillem committed
98 99 100
}

static void
101
cancel_cb(void *p_data, vlc_dialog_id *p_id)
Thomas Guillem's avatar
Thomas Guillem committed
102 103
{
    libvlc_instance_t *p_instance = p_data;
104 105
    p_instance->dialog.cbs.pf_cancel(p_instance->dialog.data,
                                     (libvlc_dialog_id *)p_id);
Thomas Guillem's avatar
Thomas Guillem committed
106 107 108
}

static void
109 110
update_progress_cb(void *p_data, vlc_dialog_id *p_id, float f_position,
                   const char *psz_text)
Thomas Guillem's avatar
Thomas Guillem committed
111 112
{
    libvlc_instance_t *p_instance = p_data;
113 114 115
    p_instance->dialog.cbs.pf_update_progress(p_instance->dialog.data,
                                              (libvlc_dialog_id *) p_id,
                                              f_position, psz_text);
Thomas Guillem's avatar
Thomas Guillem committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
}

void
libvlc_dialog_set_callbacks(libvlc_instance_t *p_instance,
                            const libvlc_dialog_cbs *p_cbs, void *p_data)
{
    libvlc_int_t *p_libvlc = p_instance->p_libvlc_int;

    vlc_mutex_lock(&p_instance->instance_lock);
    if (p_cbs != NULL)
    {
        const vlc_dialog_cbs dialog_cbs = {
            .pf_display_error = p_cbs->pf_display_error != NULL ?
                                display_error_cb : NULL,
            .pf_display_login = p_cbs->pf_display_login ?
                                display_login_cb : NULL,
            .pf_display_question = p_cbs->pf_display_question != NULL ?
                                   display_question_cb : NULL,
            .pf_display_progress = p_cbs->pf_display_progress != NULL ?
                                   display_progress_cb : NULL,
            .pf_cancel = p_cbs->pf_cancel != NULL ? cancel_cb : NULL,
            .pf_update_progress = p_cbs->pf_update_progress != NULL ?
                                  update_progress_cb : NULL,
        };

        p_instance->dialog.cbs = *p_cbs;
        p_instance->dialog.data = p_data;

        vlc_dialog_provider_set_callbacks(p_libvlc, &dialog_cbs, p_instance);
    }
    else
        vlc_dialog_provider_set_callbacks(p_libvlc, NULL, NULL);
    vlc_mutex_unlock(&p_instance->instance_lock);
}

void
libvlc_dialog_set_context(libvlc_dialog_id *p_id, void *p_context)
{
    vlc_dialog_id_set_context((vlc_dialog_id *)p_id, p_context);
}

void *
libvlc_dialog_get_context(libvlc_dialog_id *p_id)
{
    return vlc_dialog_id_get_context((vlc_dialog_id *)p_id);
}

int
libvlc_dialog_post_login(libvlc_dialog_id *p_id, const char *psz_username,
                         const char *psz_password, bool b_store)
{
    int i_ret = vlc_dialog_id_post_login((vlc_dialog_id *)p_id, psz_username,
                                         psz_password, b_store);
    return i_ret == VLC_SUCCESS ? 0 : -1;
}

int
libvlc_dialog_post_action(libvlc_dialog_id *p_id, int i_action)
{
    int i_ret = vlc_dialog_id_post_action((vlc_dialog_id *)p_id, i_action);
    return i_ret == VLC_SUCCESS ? 0 : -1;
}

int
libvlc_dialog_dismiss(libvlc_dialog_id *p_id)
{
    int i_ret = vlc_dialog_id_dismiss((vlc_dialog_id *)p_id);
    return i_ret == VLC_SUCCESS ? 0 : -1;
}