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);
<