Commit c50d31d8 authored by Thomas Guillem's avatar Thomas Guillem

dialog: add new dialog API

- vlc_dialog_wait_question will replace dialog_Question

- vlc_dialog_wait_question with i_type == VLC_DIALOG_QUESTION_CRITICAL and
  without action1/action2 will replace dialog_FatalWait

- vlc_dialog_wait_login will replace dialog_Login

- vlc_dialog_display_progress will replace dialog_Progress

- dialog_Fatal is replaced by vlc_dialog_display_error

- vlc_dialog_display_error will replace dialog_Fatal

- vlc_ext_dialog_update will repalace dialog_ExtensionUpdate

- vlc_dialog_wait_question and vlc_dialog_wait_login are interruptible via
  vlc_interrupt.

- Nothing prevents to have more than one dialog type displayed at a time.
parent 9d1b941b
......@@ -2,6 +2,7 @@
* vlc_dialog.h: user interaction dialogs
*****************************************************************************
* Copyright (C) 2009 Rémi Denis-Courmont
* Copyright (C) 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
......@@ -133,4 +134,461 @@ VLC_API int dialog_Unregister(vlc_object_t *);
/** @} */
typedef struct vlc_dialog_provider vlc_dialog_provider;
typedef struct vlc_dialog_id vlc_dialog_id;
typedef struct extension_dialog_t extension_dialog_t;
/* Called from src/libvlc.c */
vlc_dialog_provider *
vlc_dialog_provider_new(void);
/* Called from src/libvlc.c */
void
vlc_dialog_provider_release(vlc_dialog_provider *p_provider);
/**
* @defgroup vlc_dialog VLC dialog
* @ingroup interface
* @{
* @file
* This file declares VLC dialog functions
* @defgroup vlc_dialog_api VLC dialog functions
* In order to interact with the user
* @{
*/
/**
* Dialog question type, see vlc_dialog_wait_question()
*/
typedef enum vlc_dialog_question_type
{
VLC_DIALOG_QUESTION_NORMAL,
VLC_DIALOG_QUESTION_WARNING,
VLC_DIALOG_QUESTION_CRITICAL,
} vlc_dialog_question_type;
/**
* Sends an error message
*
* This function returns immediately
*
* @param p_obj the VLC object emitting the error
* @param psz_title title of the error dialog
* @param psz_fmt format string for the error message
* @return VLC_SUCCESS on success, or a VLC error code on error
*/
VLC_API int
vlc_dialog_display_error(vlc_object_t *p_obj, const char *psz_title,
const char *psz_fmt, ...) VLC_FORMAT(3,4);
#define vlc_dialog_display_error(a, b, c, ...) \
vlc_dialog_display_error(VLC_OBJECT(a), b, c, ##__VA_ARGS__)
/**
* Sends an error message
*
* Equivalent to vlc_dialog_display_error() expect that it's called with a
* va_list.
*/
VLC_API int
vlc_dialog_display_error_va(vlc_object_t *p_obj, const char *psz_title,
const char *psz_fmt, va_list ap);
/**
* Requests an user name and a password
*
* This function waits until the user dismisses the dialog or responds. It's
* interruptible via vlc_interrupt. In that case, vlc_dialog_cbs.pf_cancel()
* will be invoked. If p_store is not NULL, the user will be asked to store the
* password or not.
*
* @param p_obj the VLC object emitting the dialog
* @param ppsz_username a pointer to the user name provided by the user, it
* must be freed with free() on success
* @param ppsz_password a pointer to the password provided by the user, it must
* be freed with free() on success
* @param p_store a pointer to the store answer provided by the user (optional)
* @param psz_default_username default user name proposed
* @param psz_title title of the login dialog
* @param psz_fmt format string for the login message
* @return < 0 on error, 0 if the user cancelled it, and 1 if ppsz_username and
* ppsz_password are valid.
*/
VLC_API int
vlc_dialog_wait_login(vlc_object_t *p_obj, char **ppsz_username,
char **ppsz_password, bool *p_store,
const char *psz_default_username,
const char *psz_title, const char *psz_fmt, ...)
VLC_FORMAT(7,8);
#define vlc_dialog_wait_login(a, b, c, d, e, f, g, ...) \
vlc_dialog_wait_login(VLC_OBJECT(a), b, c, d, e, f, g, ##__VA_ARGS__)
/**
* Requests an user name and a password
*
* Equivalent to vlc_dialog_wait_login() expect that it's called with a
* va_list.
*/
VLC_API int
vlc_dialog_wait_login_va(vlc_object_t *p_obj, char **ppsz_username,
char **ppsz_password, bool *p_store,
const char *psz_default_username,
const char *psz_title, const char *psz_fmt, va_list ap);
/**
* Asks a total (Yes/No/Cancel) question
*
* This function waits until the user dismisses the dialog or responds. It's
* interruptible via vlc_interrupt. In that case, vlc_dialog_cbs.pf_cancel()
* will be invoked. The psz_cancel is mandatory since this dialog is always
* cancellable by the user.
*
* @param p_obj the VLC object emitting the dialog
* @param i_type question type (severity of the question)
* @param psz_cancel text of the cancel button
* @param psz_action1 first choice/button text (optional)
* @param psz_action2 second choice/button text (optional)
* @param psz_title title of the question dialog
* @param psz_fmt format string for the question message
* @return < 0 on error, 0 if the user cancelled it, 1 on action1, 2 on action2
*/
VLC_API int
vlc_dialog_wait_question(vlc_object_t *p_obj,
vlc_dialog_question_type i_type,
const char *psz_cancel, const char *psz_action1,
const char *psz_action2, const char *psz_title,
const char *psz_fmt, ...) VLC_FORMAT(7,8);
#define vlc_dialog_wait_question(a, b, c, d, e, f, g, ...) \
vlc_dialog_wait_question(VLC_OBJECT(a), b, c, d, e, f, g, ##__VA_ARGS__)
/**
* Asks a total (Yes/No/Cancel) question
*
* Equivalent to vlc_dialog_wait_question() expect that it's called with a
* va_list.
*/
VLC_API int
vlc_dialog_wait_question_va(vlc_object_t *p_obj,
vlc_dialog_question_type i_type,
const char *psz_cancel, const char *psz_action1,
const char *psz_action2, const char *psz_title,
const char *psz_fmt, va_list ap);
/**
* Display a progress dialog
*
* This function returns immediately
*
* @param p_obj the VLC object emitting the dialog
* @param b_indeterminate true if the progress dialog is indeterminate
* @param f_position initial position of the progress bar (between 0.0 and 1.0)
* @param psz_cancel text of the cancel button, if NULL the dialog is not
* cancellable (optional)
* @param psz_title title of the progress dialog
* @param psz_fmt format string for the progress message
* @return a valid vlc_dialog_id on success, must be released with
* vlc_dialog_id_release()
*/
VLC_API vlc_dialog_id *
vlc_dialog_display_progress(vlc_object_t *p_obj, bool b_indeterminate,
float f_position, const char *psz_cancel,
const char *psz_title, const char *psz_fmt, ...)
VLC_FORMAT(6,7);
#define vlc_dialog_display_progress(a, b, c, d, e, f, ...) \
vlc_dialog_display_progress(VLC_OBJECT(a), b, c, d, e, f, ##__VA_ARGS__)
/**
* Display a progress dialog
*
* Equivalent to vlc_dialog_display_progress() expect that it's called with a
* va_list.
*/
VLC_API vlc_dialog_id *
vlc_dialog_display_progress_va(vlc_object_t *p_obj, bool b_indeterminate,
float f_position, const char *psz_cancel,
const char *psz_title, const char *psz_fmt,
va_list ap);
/**
* Update the position of the progress dialog
*
* @param p_obj the VLC object emitting the dialog
* @param p_id id of the dialog to update
* @param f_position position of the progress bar (between 0.0 and 1.0)
* @return VLC_SUCCESS on success, or a VLC error code on error
*/
VLC_API int
vlc_dialog_update_progress(vlc_object_t *p_obj, vlc_dialog_id *p_id,
float f_position);
#define vlc_dialog_update_progress(a, b, c) \
vlc_dialog_update_progress(VLC_OBJECT(a), b, c)
/**
* Update the position and the message of the progress dialog
*
* @param p_obj the VLC object emitting the dialog
* @param p_id id of the dialog to update
* @param f_position position of the progress bar (between 0.0 and 1.0)
* @param psz_fmt format string for the progress message
* @return VLC_SUCCESS on success, or a VLC error code on error
*/
VLC_API int
vlc_dialog_update_progress_text(vlc_object_t *p_obj, vlc_dialog_id *p_id,
float f_position, const char *psz_fmt, ...)
VLC_FORMAT(4, 5);
#define vlc_dialog_display_progress_text(a, b, c, d, ...) \
vlc_dialog_display_progress_text(VLC_OBJECT(a), b, c, d, ##__VA_ARGS__)
/**
* Update the position and the message of the progress dialog
*
* Equivalent to vlc_dialog_update_progress_text() expect that it's called
* with a va_list.
*/
VLC_API int
vlc_dialog_update_progress_text_va(vlc_object_t *p_obj, vlc_dialog_id *p_id,
float f_position, const char *psz_fmt,
va_list ap);
/**
* Release the dialog id returned by vlc_dialog_display_progress()
*
* It causes the vlc_dialog_cbs.pf_cancel() callback to be invoked.
*
* @param p_obj the VLC object emitting the dialog
* @param p_id id of the dialog to release
*/
VLC_API void
vlc_dialog_release(vlc_object_t *p_obj, vlc_dialog_id *p_id);
#define vlc_dialog_release(a, b) \
vlc_dialog_release(VLC_OBJECT(a), b)
/**
* Return true if the dialog id is cancelled
*
* @param p_obj the VLC object emitting the dialog
* @param p_id id of the dialog
*/
VLC_API bool
vlc_dialog_is_cancelled(vlc_object_t *p_obj, vlc_dialog_id *p_id);
#define vlc_dialog_is_cancelled(a, b) \
vlc_dialog_is_cancelled(VLC_OBJECT(a), b)
/**
* @}
* @defgroup vlc_dialog_impl VLC dialog callbacks
* Need to be implemented by GUI modules or libvlc
* @{
*/
/**
* Dialog callbacks to be implemented
*/
typedef struct vlc_dialog_cbs
{
/**
* Called when an error message needs to be displayed
*
* @param psz_title title of the diaog
* @param psz_text text of the dialog
* @param p_data opaque pointer for the callback
*/
void (*pf_display_error)(const char *psz_title, const char *psz_text,
void *p_data);
/**
* Called when a login dialog needs to be displayed
*
* You can interact with this dialog by calling vlc_dialog_id_post_login()
* to post an answer or vlc_dialog_id_dismiss() to cancel this dialog.
*
* @note to receive this callack, vlc_dialog_cbs.pf_cancel should not be
* NULL.
*
* @param p_id id used to interact with the dialog
* @param psz_title title of the diaog
* @param psz_text text of the dialog
* @param psz_default_username user name that should be set on the user form
* @param b_ask_store if true, ask the user if he wants to save the
* credentials
* @param p_data opaque pointer for the callback
*/
void (*pf_display_login)(vlc_dialog_id *p_id, const char *psz_title,
const char *psz_text,
const char *psz_default_username,
bool b_ask_store, void *p_data);
/**
* Called when a question dialog needs to be displayed
*
* You can interact with this dialog by calling vlc_dialog_id_post_action()
* to post an answer or vlc_dialog_id_dismiss() to cancel this dialog.
*
* @note to receive this callack, vlc_dialog_cbs.pf_cancel should not be
* NULL.
*
* @param p_id id used to interact with the dialog
* @param psz_title title of the diaog
* @param psz_text text of the dialog
* @param i_type question type (or severity) of the dialog
* @param psz_cancel text of the cancel button
* @param psz_action1 text of the first button, if NULL, don't display this
* button
* @param psz_action2 text of the second button, if NULL, don't display
* this button
* @param p_data opaque pointer for the callback
*/
void (*pf_display_question)(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, void *p_data);
/**
* Called when a progress dialog needs to be displayed
*
* If cancellable (psz_cancel != NULL), you can cancel this dialog by
* calling vlc_dialog_id_dismiss()
*
* @note to receive this callack, vlc_dialog_cbs.pf_cancel and
* vlc_dialog_cbs.pf_update_progress should not be NULL.
*
* @param p_id id used to interact with the dialog
* @param psz_title title of the diaog
* @param psz_text text of the dialog
* @param b_indeterminate true if the progress dialog is indeterminate
* @param f_position initial position of the progress bar (between 0.0 and
* 1.0)
* @param psz_cancel text of the cancel button, if NULL the dialog is not
* cancellable
* @param p_data opaque pointer for the callback
*/
void (*pf_display_progress)(vlc_dialog_id *p_id, const char *psz_title,
const char *psz_text, bool b_indeterminate,
float f_position, const char *psz_cancel,
void *p_data);
/**
* Called when a displayed dialog needs to be cancelled
*
* The implementation must call vlc_dialog_id_dismiss() to really release
* the dialog.
*
* @param p_id id of the dialog
* @param p_data opaque pointer for the callback
*/
void (*pf_cancel)(vlc_dialog_id *p_id, void *p_data);
/**
* Called when a progress dialog needs to be updated
*
* @param p_id id of the dialog
* @param f_position osition of the progress bar (between 0.0 and 1.0)
* @param psz_text new text of the progress dialog
* @param p_data opaque pointer for the callback
*/
void (*pf_update_progress)(vlc_dialog_id *p_id, float f_position,
const char *psz_text, void *p_data);
} vlc_dialog_cbs;
/**
* Register callbacks to handle VLC dialogs
*
* @param p_cbs a pointer to callbacks, or NULL to unregister callbacks.
* @param p_data opaque pointer for the callback
*/
VLC_API void
vlc_dialog_provider_set_callbacks(vlc_object_t *p_obj,
const vlc_dialog_cbs *p_cbs, void *p_data);
#define vlc_dialog_provider_set_callbacks(a, b, c) \
vlc_dialog_provider_set_callbacks(VLC_OBJECT(a), b, c)
/**
* Associate an opaque pointer with the dialog id
*/
VLC_API void
vlc_dialog_id_set_context(vlc_dialog_id *p_id, void *p_context);
/**
* Return the opaque pointer associated with the dialog id
*/
VLC_API void *
vlc_dialog_id_get_context(vlc_dialog_id *p_id);
/**
* Post a login answer
*
* After this call, p_id won't be valid anymore
*
* @see vlc_dialog_cbs.pf_display_login
*
* @param p_id id of the dialog
* @param psz_username valid and non empty string
* @param psz_password valid string (can be empty)
* @param b_store if true, store the credentials
* @return VLC_SUCCESS on success, or a VLC error code on error
*/
VLC_API int
vlc_dialog_id_post_login(vlc_dialog_id *p_id, const char *psz_username,
const char *psz_password, bool b_store);
/**
* Post a question answer
*
* After this call, p_id won't be valid anymore
*
* @see vlc_dialog_cbs.pf_display_question
*
* @param p_id id of the dialog
* @param i_action 1 for action1, 2 for action2
* @return VLC_SUCCESS on success, or a VLC error code on error
*/
VLC_API int
vlc_dialog_id_post_action(vlc_dialog_id *p_id, int i_action);
/**
* Dismiss a dialog
*
* After this call, p_id won't be valid anymore
*
* @see vlc_dialog_cbs.pf_cancel
*
* @param p_id id of the dialog
* @return VLC_SUCCESS on success, or a VLC error code on error
*/
VLC_API int
vlc_dialog_id_dismiss(vlc_dialog_id *p_id);
/**
* @}
* @defgroup vlc_dialog_ext VLC extension dialog functions
* @{
*/
VLC_API int
vlc_ext_dialog_update(vlc_object_t *p_obj, extension_dialog_t *dialog);
#define vlc_ext_dialog_update(a, b) \
vlc_ext_dialog_update(VLC_OBJECT(a), b)
/**
* Dialog extension callback to be implemented
*/
typedef void (*vlc_dialog_ext_update_cb)(extension_dialog_t *p_ext_dialog,
void *p_data);
/**
* Register a callback for VLC extension dialog
*
* @param pf_update a pointer to the update callback, or NULL to unregister
* callback
* @param p_data opaque pointer for the callback
*/
VLC_API void
vlc_dialog_provider_set_ext_callback(vlc_object_t *p_obj,
vlc_dialog_ext_update_cb pf_update,
void *p_data);
#define vlc_dialog_provider_set_ext_callback(a, b, c) \
vlc_dialog_provider_set_ext_callback(VLC_OBJECT(a), b, c)
/** @} @} */
#endif
......@@ -41,6 +41,8 @@ struct vlc_object_t
VLC_COMMON_MEMBERS
};
typedef struct vlc_dialog_provider vlc_dialog_provider;
/*****************************************************************************
* Prototypes
*****************************************************************************/
......
......@@ -2,6 +2,7 @@
* dialog.c: User dialog functions
*****************************************************************************
* Copyright © 2009 Rémi Denis-Courmont
* 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
......@@ -18,11 +19,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/**
* \file dialog.c
* User dialogs core
*/
/** @ingroup vlc_dialog */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
......@@ -31,6 +28,7 @@
#include <vlc_common.h>
#include <vlc_dialog.h>
#include <vlc_interrupt.h>
#include <vlc_extensions.h>
#include <assert.h>
#include "libvlc.h"
......@@ -301,3 +299,824 @@ int dialog_ExtensionUpdate (vlc_object_t *obj, extension_dialog_t *dialog)
vlc_object_release (dp);
return ret;
}
struct vlc_dialog_provider
{
vlc_mutex_t lock;
vlc_array_t dialog_array;
vlc_dialog_cbs cbs;
void * p_cbs_data;
vlc_dialog_ext_update_cb pf_ext_update;
void * p_ext_data;
};
enum dialog_type
{
VLC_DIALOG_ERROR,
VLC_DIALOG_LOGIN,
VLC_DIALOG_QUESTION,
VLC_DIALOG_PROGRESS,
};
struct dialog_answer
{
enum dialog_type i_type;
union
{
struct
{
char *psz_username;
char *psz_password;
bool b_store;
} login;
struct
{
int i_action;
} question;
} u;
};
struct dialog
{
enum dialog_type i_type;
const char *psz_title;
const char *psz_text;
union
{
struct
{
const char *psz_default_username;
bool b_ask_store;
} login;
struct
{
vlc_dialog_question_type i_type;
const char *psz_cancel;
const char *psz_action1;
const char *psz_action2;
} question;
struct
{
bool b_indeterminate;
float f_position;
const char *psz_cancel;
} progress;
} u;
};
struct vlc_dialog_id
{
vlc_mutex_t lock;
vlc_cond_t wait;
enum dialog_type i_type;
void * p_context;
int i_refcount;
bool b_cancelled;
bool b_answered;
bool b_progress_indeterminate;
char * psz_progress_text;
struct dialog_answer answer;
};
struct dialog_i11e_context
{
vlc_dialog_provider * p_provider;
vlc_dialog_id * p_id;
};
static inline vlc_dialog_provider *
get_dialog_provider(vlc_object_t *p_obj, bool b_check_interact)
{
if (b_check_interact && p_obj->i_flags & OBJECT_FLAGS_NOINTERACT)
return NULL;
vlc_dialog_provider *p_provider =
libvlc_priv(p_obj->p_libvlc)->p_dialog_provider;
assert(p_provider != NULL);
return p_provider;
}
static void
dialog_id_release(vlc_dialog_id *p_id)
{
if (p_id->answer.i_type == VLC_DIALOG_LOGIN)
{
free(p_id->answer.u.login.psz_username);
free(p_id->answer.u.login.psz_password);
}
free(p_id->psz_progress_text);
vlc_mutex_destroy(&p_id->lock);
vlc_cond_destroy(&p_id->wait);
free(p_id);
}
vlc_dialog_provider *
vlc_dialog_provider_new(void)
{
vlc_dialog_provider *p_provider = malloc(sizeof(*p_provider));
if( p_provider == NULL )
return NULL;
vlc_mutex_init(&p_provider->lock);
vlc_array_init(&p_provider->dialog_array);
memset(&p_provider->cbs, 0, sizeof(p_provider->cbs));
p_provider->p_cbs_data = NULL;
p_provider->pf_ext_update = NULL;
p_provider->p_ext_data = NULL;
return p_provider;
}
static int
dialog_get_idx_locked(vlc_dialog_provider *p_provider, vlc_dialog_id *p_id)
{
for (int i = 0; i < vlc_array_count(&p_provider->dialog_array); ++i)
{
if (p_id == vlc_array_item_at_index(&p_provider->dialog_array, i))
return i;
}
return -1;
}
static void
dialog_cancel_locked(vlc_dialog_provider *p_provider, vlc_dialog_id *p_id)
{
vlc_mutex_lock(&p_id->lock);
if (p_id->b_cancelled || p_id->b_answered)
{
vlc_mutex_unlock(&p_id->lock);
return;
}
p_id->b_cancelled = true;
vlc_mutex_unlock(&p_id->lock);
p_provider->cbs.pf_cancel(p_id, p_provider->p_cbs_data);
}
static vlc_dialog_id *
dialog_add_locked(vlc_dialog_provider *p_provider, enum dialog_type i_type)
{
vlc_dialog_id *p_id = calloc(1, sizeof(*p_id));
if (p_id == NULL)
return NULL;
vlc_mutex_init(&p_id->lock);
vlc_cond_init(&p_id->wait);
p_id->i_type = i_type;
p_id->i_refcount = 2; /* provider and callbacks */
vlc_array_append(&p_provider->dialog_array, p_id);
return p_id;
}
static void
dialog_remove_locked(vlc_dialog_provider *p_provider, vlc_dialog_id *p_id)
{
int i_array_idx = dialog_get_idx_locked(p_provider, p_id);
assert(i_array_idx >= 0);
vlc_array_remove(&p_provider->dialog_array, i_array_idx);
vlc_mutex_lock(&p_id->lock);
p_id->i_refcount--;
if (p_id->i_refcount == 0)
{
vlc_mutex_unlock(&p_id->lock);
dialog_id_release(p_id);
}
else
vlc_mutex_unlock(&p_id->lock);
}
static void
dialog_clear_all_locked(vlc_dialog_provider *p_provider)
{
for (int i = 0; i < vlc_array_count(&p_provider->dialog_array); ++i)
{
vlc_dialog_id *p_id =
vlc_array_item_at_index(&p_provider->dialog_array, i);
dialog_cancel_locked(p_provider, p_id);
}
vlc_array_clear(&p_provider->dialog_array);
}
void
vlc_dialog_provider_release(vlc_dialog_provider *p_provider)
{
assert(p_provider != NULL);
vlc_mutex_lock(&p_provider->lock);
dialog_clear_all_locked(p_provider);
vlc_mutex_unlock(&p_provider->lock);
vlc_mutex_destroy(&p_provider->lock);
free(p_provider);
}
#undef vlc_dialog_provider_set_callbacks
void
vlc_dialog_provider_set_callbacks(vlc_object_t *p_obj,
const vlc_dialog_cbs *p_cbs, void *p_data)
{
assert(p_obj != NULL);
vlc_dialog_provider *p_provider = get_dialog_provider(p_obj, false);
vlc_mutex_lock(&p_provider->lock);
dialog_clear_all_locked(p_provider);
if (p_cbs == NULL)
{
memset(&p_provider->cbs, 0, sizeof(p_provider->cbs));
p_provider->p_cbs_data = NULL;
}
else
{
p_provider->cbs = *p_cbs;
p_provider->p_cbs_data = p_data;
}
vlc_mutex_unlock(&p_provider->lock);
}
static void
dialog_wait_interrupted(void *p_data)
{
struct dialog_i11e_context *p_context = p_data;
vlc_dialog_provider *p_provider = p_context->p_provider;
vlc_dialog_id *p_id = p_context->p_id;
vlc_mutex_lock(&p_provider->lock);
dialog_cancel_locked(p_provider, p_id);
vlc_mutex_unlock(&p_provider->lock);
vlc_mutex_lock(&p_id->lock);
vlc_cond_signal(&p_id->wait);
vlc_mutex_unlock(&p_id->lock);
}
static int
dialog_wait(vlc_dialog_provider *p_provider, vlc_dialog_id *p_id,
enum dialog_type i_type, struct dialog_answer *p_answer)
{
struct dialog_i11e_context context = {
.p_provider = p_provider,
.p_id = p_id,
};
vlc_interrupt_register(dialog_wait_interrupted, &context);
vlc_mutex_lock(&p_id->lock);
/* Wait for the dialog to be dismissed, interrupted or answered */
while (!p_id->b_cancelled && !p_id->b_answered)
vlc_cond_wait(&p_id->wait, &p_id->lock);
int i_ret;
if (p_id->b_cancelled)
i_ret = 0;
else if (p_id->answer.i_type != i_type)
i_ret = VLC_EGENERIC;
else
{
i_ret = 1;
memcpy(p_answer, &p_id->answer, sizeof(p_id->answer));
memset(&p_id->answer, 0, sizeof(p_id->answer));
}
vlc_mutex_unlock(&p_id->lock);
vlc_interrupt_unregister();
vlc_mutex_lock(&p_provider->lock);
dialog_remove_locked(p_provider, p_id);
vlc_mutex_unlock(&p_provider->lock);
return i_ret;
}
static int
dialog_display_error_va(vlc_dialog_provider *p_provider, const char *psz_title,
const char *psz_fmt, va_list ap)
{
vlc_mutex_lock(&p_provider->lock);
if (p_provider->cbs.pf_display_error == NULL)
{
vlc_mutex_unlock(&p_provider->lock);
return VLC_EGENERIC;
}
char *psz_text;
if (vasprintf(&psz_text, psz_fmt, ap) == -1)
{
vlc_mutex_unlock(&p_provider->lock);
return VLC_ENOMEM;
}
p_provider->cbs.pf_display_error(psz_title, psz_text, p_provider->p_cbs_data);
free(psz_text);
vlc_mutex_unlock(&p_provider->lock);
return VLC_SUCCESS;
}
int
vlc_dialog_display_error_va(vlc_object_t *p_obj, const char *psz_title,
const char *psz_fmt, va_list ap)
{
assert(p_obj != NULL && psz_title != NULL && psz_fmt != NULL);
int i_ret;
vlc_dialog_provider *p_provider = get_dialog_provider(p_obj, true);
if (p_provider != NULL)
i_ret = dialog_display_error_va(p_provider, psz_title, psz_fmt, ap);
else
i_ret = VLC_EGENERIC;
if (i_ret != VLC_SUCCESS)
{
msg_Err(p_obj, "%s", psz_title);
msg_GenericVa(p_obj, VLC_MSG_ERR, psz_fmt, ap);
}
return i_ret;
}
#undef vlc_dialog_display_error
int
vlc_dialog_display_error(vlc_object_t *p_obj, const char *psz_title,
const char *psz_fmt, ...)
{
assert(psz_fmt != NULL);
va_list ap;
va_start(ap, psz_fmt);
int i_ret = vlc_dialog_display_error_va(p_obj, psz_title, psz_fmt, ap);
va_end(ap);
return i_ret;
}
static int
dialog_display_login_va(vlc_dialog_provider *p_provider, vlc_dialog_id **pp_id,
const char *psz_default_username, bool b_ask_store,
const char *psz_title, const char *psz_fmt, va_list ap)
{
vlc_mutex_lock(&p_provider->lock);
if (p_provider->cbs.pf_display_login == NULL
|| p_provider->cbs.pf_cancel == NULL)
{
vlc_mutex_unlock(&p_provider->lock);
return VLC_EGENERIC;
}
char *psz_text;
if (vasprintf(&psz_text, psz_fmt, ap) == -1)
{
vlc_mutex_unlock(&p_provider->lock);
return VLC_ENOMEM;
}
vlc_dialog_id *p_id = dialog_add_locked(p_provider, VLC_DIALOG_LOGIN);
if (p_id == NULL)
{
free(psz_text);
vlc_mutex_unlock(&p_provider->lock);
return VLC_ENOMEM;
}
p_provider->cbs.pf_display_login(p_id, psz_title, psz_text,
psz_default_username, b_ask_store,
p_provider->p_cbs_data);
free(psz_text);
vlc_mutex_unlock(&p_provider->lock);
*pp_id = p_id;
return VLC_SUCCESS;
}
int
vlc_dialog_wait_login_va(vlc_object_t *p_obj, char **ppsz_username,
char **ppsz_password, bool *p_store,
const char *psz_default_username,
const char *psz_title, const char *psz_fmt, va_list ap)
{
assert(p_obj != NULL && ppsz_username != NULL && ppsz_password != NULL
&& psz_fmt != NULL && psz_title != NULL);
vlc_dialog_provider *p_provider = get_dialog_provider(p_obj, true);
if (p_provider == NULL)
return VLC_EGENERIC;
vlc_dialog_id *p_id;
int i_ret = dialog_display_login_va(p_provider, &p_id, psz_default_username,
p_store != NULL, psz_title, psz_fmt, ap);
if (i_ret < 0 || p_id == NULL)
return i_ret;
struct dialog_answer answer;
i_ret = dialog_wait(p_provider, p_id, VLC_DIALOG_LOGIN, &answer);
if (i_ret <= 0)
return i_ret;
*ppsz_username = answer.u.login.psz_username;
*ppsz_password = answer.u.login.psz_password;
if (p_store != NULL)
*p_store = answer.u.login.b_store;
return 1;
}
#undef vlc_dialog_wait_login
int
vlc_dialog_wait_login(vlc_object_t *p_obj, char **ppsz_username,
char **ppsz_password, bool *p_store,
const char *psz_default_username, const char *psz_title,
const char *psz_fmt, ...)
{
assert(psz_fmt != NULL);
va_list ap;
va_start(ap, psz_fmt);
int i_ret = vlc_dialog_wait_login_va(p_obj, ppsz_username, ppsz_password,
p_store,psz_default_username,
psz_title, psz_fmt, ap);
va_end(ap);
return i_ret;
}
static int
dialog_display_question_va(vlc_dialog_provider *p_provider, vlc_dialog_id **pp_id,
vlc_dialog_question_type i_type,
const char *psz_cancel, const char *psz_action1,
const char *psz_action2, const char *psz_title,
const char *psz_fmt, va_list ap)
{
vlc_mutex_lock(&p_provider->lock);
if (p_provider->cbs.pf_display_question == NULL
|| p_provider->cbs.pf_cancel == NULL)
{
vlc_mutex_unlock(&p_provider->lock);
return VLC_EGENERIC;
}
char *psz_text;
if (vasprintf(&psz_text, psz_fmt, ap) == -1)
{
vlc_mutex_unlock(&p_provider->lock);
return VLC_ENOMEM;
}
vlc_dialog_id *p_id = dialog_add_locked(p_provider, VLC_DIALOG_QUESTION);
if (p_id == NULL)
{
free(psz_text);
vlc_mutex_unlock(&p_provider->lock);
return VLC_ENOMEM;
}
p_provider->cbs.pf_display_question(p_id, psz_title, psz_text,
i_type, psz_cancel, psz_action1,
psz_action2, p_provider->p_cbs_data);
free(psz_text);
vlc_mutex_unlock(&p_provider->lock);
*pp_id = p_id;
return VLC_SUCCESS;
}
int
vlc_dialog_wait_question_va(vlc_object_t *p_obj,
vlc_dialog_question_type i_type,
const char *psz_cancel, const char *psz_action1,
const char *psz_action2, const char *psz_title,
const char *psz_fmt, va_list ap)
{
assert(p_obj != NULL && psz_fmt != NULL && psz_title != NULL
&& psz_cancel != NULL);
vlc_dialog_provider *p_provider = get_dialog_provider(p_obj, true);
if (p_provider == NULL)
return VLC_EGENERIC;
vlc_dialog_id *p_id;
int i_ret = dialog_display_question_va(p_provider, &p_id, i_type,
psz_cancel, psz_action1,
psz_action2, psz_title, psz_fmt, ap);
if (i_ret < 0 || p_id == NULL)
return i_ret;
struct dialog_answer answer;
i_ret = dialog_wait(p_provider, p_id, VLC_DIALOG_QUESTION, &answer);
if (i_ret <= 0)
return i_ret;
if (answer.u.question.i_action != 1 && answer.u.question.i_action != 2)
return VLC_EGENERIC;
return answer.u.question.i_action;
}
#undef vlc_dialog_wait_question
int
vlc_dialog_wait_question(vlc_object_t *p_obj,
vlc_dialog_question_type i_type,
const char *psz_cancel, const char *psz_action1,
const char *psz_action2, const char *psz_title,
const char *psz_fmt, ...)
{
assert(psz_fmt != NULL);
va_list ap;
va_start(ap, psz_fmt);
int i_ret = vlc_dialog_wait_question_va(p_obj, i_type, psz_cancel,
psz_action1, psz_action2, psz_title,
psz_fmt, ap);
va_end(ap);
return i_ret;
}
static int
display_progress_va(vlc_dialog_provider *p_provider, vlc_dialog_id **pp_id,
bool b_indeterminate, float f_position,
const char *psz_cancel, const char *psz_title,
const char *psz_fmt, va_list ap)
{
vlc_mutex_lock(&p_provider->lock);
if (p_provider->cbs.pf_display_progress == NULL
|| p_provider->cbs.pf_update_progress == NULL
|| p_provider->cbs.pf_cancel == NULL)
{
vlc_mutex_unlock(&p_provider->lock);
return VLC_EGENERIC;
}
char *psz_text;
if (vasprintf(&psz_text, psz_fmt, ap) == -1)
{
vlc_mutex_unlock(&p_provider->lock);
return VLC_ENOMEM;
}
vlc_dialog_id *p_id = dialog_add_locked(p_provider, VLC_DIALOG_PROGRESS);
if (p_id == NULL)
{
free(psz_text);
vlc_mutex_unlock(&p_provider->lock);
return VLC_ENOMEM;
}
p_id->b_progress_indeterminate = b_indeterminate;
p_id->psz_progress_text = psz_text;
p_provider->cbs.pf_display_progress(p_id, psz_title, psz_text,
b_indeterminate, f_position, psz_cancel,
p_provider->p_cbs_data);
vlc_mutex_unlock(&p_provider->lock);
*pp_id = p_id;
return VLC_SUCCESS;
}
vlc_dialog_id *
vlc_dialog_display_progress_va(vlc_object_t *p_obj, bool b_indeterminate,
float f_position, const char *psz_cancel,
const char *psz_title, const char *psz_fmt,
va_list ap)
{
assert(p_obj != NULL && psz_title != NULL && psz_fmt != NULL);
vlc_dialog_provider *p_provider = get_dialog_provider(p_obj, true);
if (p_provider == NULL)
return NULL;
vlc_dialog_id *p_id;
int i_ret = display_progress_va(p_provider, &p_id, b_indeterminate,
f_position, psz_cancel, psz_title, psz_fmt,
ap);
return i_ret == VLC_SUCCESS ? p_id : NULL;
}
#undef vlc_dialog_display_progress
vlc_dialog_id *
vlc_dialog_display_progress(vlc_object_t *p_obj, bool b_indeterminate,
float f_position, const char *psz_cancel,
const char *psz_title, const char *psz_fmt, ...)
{
assert(psz_fmt != NULL);
va_list ap;
va_start(ap, psz_fmt);
vlc_dialog_id *p_id =
vlc_dialog_display_progress_va(p_obj, b_indeterminate, f_position,
psz_cancel, psz_title, psz_fmt, ap);
va_end(ap);
return p_id;
}
static int
dialog_update_progress(vlc_object_t *p_obj, vlc_dialog_id *p_id, float f_value,
char *psz_text)
{
assert(p_obj != NULL && p_id != NULL);
vlc_dialog_provider *p_provider = get_dialog_provider(p_obj, false);
vlc_mutex_lock(&p_provider->lock);
if (p_provider->cbs.pf_update_progress == NULL)
{
vlc_mutex_unlock(&p_provider->lock);
free(psz_text);
return VLC_EGENERIC;
}
if (p_id->b_progress_indeterminate)
{
vlc_mutex_unlock(&p_provider->lock);
free(psz_text);
return VLC_EGENERIC;
}
if (psz_text != NULL)
{
free(p_id->psz_progress_text);
p_id->psz_progress_text = psz_text;
}
p_provider->cbs.pf_update_progress(p_id, f_value, p_id->psz_progress_text,
p_provider->p_cbs_data);
vlc_mutex_unlock(&p_provider->lock);
return VLC_SUCCESS;
}
#undef vlc_dialog_update_progress
int
vlc_dialog_update_progress(vlc_object_t *p_obj, vlc_dialog_id *p_id,
float f_value)
{
return dialog_update_progress(p_obj, p_id, f_value, NULL);
}
int
vlc_dialog_update_progress_text_va(vlc_object_t *p_obj, vlc_dialog_id *p_id,
float f_value, const char *psz_fmt,
va_list ap)
{
assert(psz_fmt != NULL);
char *psz_text;
if (vasprintf(&psz_text, psz_fmt, ap) == -1)
return VLC_ENOMEM;
return dialog_update_progress(p_obj, p_id, f_value, psz_text);
}
#undef vlc_dialog_update_progress_text
int
vlc_dialog_update_progress_text(vlc_object_t *p_obj, vlc_dialog_id *p_id,
float f_value, const char *psz_fmt, ...)
{
assert(psz_fmt != NULL);
va_list ap;
va_start(ap, psz_fmt);
int i_ret = vlc_dialog_update_progress_text_va(p_obj, p_id, f_value,
psz_fmt, ap);
va_end(ap);
return i_ret;
}
#undef vlc_dialog_release
void
vlc_dialog_release(vlc_object_t *p_obj, vlc_dialog_id *p_id)
{
assert(p_obj != NULL && p_id != NULL);
vlc_dialog_provider *p_provider = get_dialog_provider(p_obj, false);
vlc_mutex_lock(&p_provider->lock);
dialog_cancel_locked(p_provider, p_id);
dialog_remove_locked(p_provider, p_id);
vlc_mutex_unlock(&p_provider->lock);
}
#undef vlc_dialog_is_cancelled
bool
vlc_dialog_is_cancelled(vlc_object_t *p_obj, vlc_dialog_id *p_id)
{
(void) p_obj;
assert(p_id != NULL);
vlc_mutex_lock(&p_id->lock);
bool b_cancelled = p_id->b_cancelled;
vlc_mutex_unlock(&p_id->lock);
return b_cancelled;
}
void
vlc_dialog_id_set_context(vlc_dialog_id *p_id, void *p_context)
{
vlc_mutex_lock(&p_id->lock);
p_id->p_context = p_context;
vlc_mutex_unlock(&p_id->lock);
}
void *
vlc_dialog_id_get_context(vlc_dialog_id *p_id)
{
vlc_mutex_lock(&p_id->lock);
void *p_context = p_id->p_context;
vlc_mutex_unlock(&p_id->lock);
return p_context;
}
static int
dialog_id_post(vlc_dialog_id *p_id, struct dialog_answer *p_answer)
{
vlc_mutex_lock(&p_id->lock);
if (p_answer == NULL)
{
p_id->b_cancelled = true;
}
else
{
p_id->answer = *p_answer;
p_id->b_answered = true;
}
p_id->i_refcount--;
if (p_id->i_refcount > 0)
{
vlc_cond_signal(&p_id->wait);
vlc_mutex_unlock(&p_id->lock);
}
else
{
vlc_mutex_unlock(&p_id->lock);
dialog_id_release(p_id);
}
return VLC_SUCCESS;
}
int
vlc_dialog_id_post_login(vlc_dialog_id *p_id, const char *psz_username,
const char *psz_password, bool b_store)
{
assert(p_id != NULL && psz_username != NULL && psz_password != NULL);
struct dialog_answer answer = {
.i_type = VLC_DIALOG_LOGIN,
.u.login = {
.b_store = b_store,
.psz_username = strdup(psz_username),
.psz_password = strdup(psz_password),
},
};
if (answer.u.login.psz_username == NULL
|| answer.u.login.psz_password == NULL)
{
free(answer.u.login.psz_username);
free(answer.u.login.psz_password);
dialog_id_post(p_id, NULL);
return VLC_ENOMEM;
}
return dialog_id_post(p_id, &answer);
}
int
vlc_dialog_id_post_action(vlc_dialog_id *p_id, int i_action)
{
assert(p_id != NULL);
struct dialog_answer answer = {
.i_type = VLC_DIALOG_QUESTION,
.u.question = { .i_action = i_action },
};
return dialog_id_post(p_id, &answer);
}
int
vlc_dialog_id_dismiss(vlc_dialog_id *p_id)
{
return dialog_id_post(p_id, NULL);
}
#undef vlc_dialog_provider_set_ext_callback
void
vlc_dialog_provider_set_ext_callback(vlc_object_t *p_obj,
vlc_dialog_ext_update_cb pf_update,
void *p_data)
{
assert(p_obj != NULL);
vlc_dialog_provider *p_provider = get_dialog_provider(p_obj, false);
vlc_mutex_lock(&p_provider->lock);
p_provider->pf_ext_update = pf_update;
p_provider->p_ext_data = p_data;
vlc_mutex_unlock(&p_provider->lock);
}
#undef vlc_ext_dialog_update
int
vlc_ext_dialog_update(vlc_object_t *p_obj, extension_dialog_t *p_ext_dialog)
{
assert(p_obj != NULL);
vlc_dialog_provider *p_provider = get_dialog_provider(p_obj, false);
vlc_mutex_lock(&p_provider->lock);
if (p_provider->pf_ext_update == NULL)
{
vlc_mutex_unlock(&p_provider->lock);
return VLC_EGENERIC;
}
p_provider->pf_ext_update(p_ext_dialog, p_provider->p_ext_data);
vlc_mutex_unlock(&p_provider->lock);
return VLC_SUCCESS;
}
......@@ -61,6 +61,7 @@
#include <vlc_interface.h>
#include <vlc_charset.h>
#include <vlc_dialog.h>
#include <vlc_fs.h>
#include <vlc_cpu.h>
#include <vlc_url.h>
......@@ -235,6 +236,14 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
}
#endif
priv->p_dialog_provider = vlc_dialog_provider_new();
if( priv->p_dialog_provider == NULL )
{
vlc_LogDeinit (p_libvlc);
module_EndBank (true);
return VLC_ENOMEM;
}
/* FIXME: could be replaced by using Unix sockets */
#ifdef HAVE_DBUS
......@@ -503,6 +512,8 @@ void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
libvlc_Quit( p_libvlc );
intf_DestroyAll( p_libvlc );
vlc_dialog_provider_release( priv->p_dialog_provider );
#ifdef ENABLE_VLM
/* Destroy VLM if created in libvlc_InternalInit */
if( priv->p_vlm )
......
......@@ -138,6 +138,8 @@ module_t *module_find_by_shortcut (const char *psz_shortcut);
/**
* Private LibVLC instance data.
*/
typedef struct vlc_dialog_provider vlc_dialog_provider;
typedef struct libvlc_priv_t
{
libvlc_int_t public_data;
......@@ -149,6 +151,7 @@ typedef struct libvlc_priv_t
vlc_logger_t *logger;
vlm_t *p_vlm; ///< the VLM singleton (or NULL)
vlc_object_t *p_legacy_dialog_provider; ///< dialog provider
vlc_dialog_provider *p_dialog_provider; ///< dialog provider
struct playlist_t *playlist; ///< Playlist for interfaces
struct playlist_preparser_t *parser; ///< Input item meta data handler
struct vlc_actions *actions; ///< Hotkeys handler
......
......@@ -526,6 +526,29 @@ vlc_credential_init
vlc_credential_clean
vlc_credential_get
vlc_credential_store
vlc_dialog_display_error
vlc_dialog_display_error_va
vlc_dialog_display_progress
vlc_dialog_display_progress_va
vlc_dialog_id_dismiss
vlc_dialog_id_get_context
vlc_dialog_id_post_action
vlc_dialog_id_post_login
vlc_dialog_id_set_context
vlc_dialog_is_cancelled
vlc_dialog_provider_new
vlc_dialog_provider_release
vlc_dialog_provider_set_callbacks
vlc_dialog_provider_set_ext_callback
vlc_dialog_release
vlc_dialog_update_progress
vlc_dialog_update_progress_text
vlc_dialog_update_progress_text_va
vlc_dialog_wait_login
vlc_dialog_wait_login_va
vlc_dialog_wait_question
vlc_dialog_wait_question_va
vlc_ext_dialog_update
vlc_sem_init
vlc_sem_destroy
vlc_sem_post
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment