1 /*****************************************************************************
2 * dialog.c: Functions to create interface dialogs from Lua extensions
3 *****************************************************************************
4 * Copyright (C) 2009-2010 VideoLAN and authors
7 * Authors: Jean-Philippe André < jpeg # videolan.org >
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_extensions.h>
38 #include <lua.h> /* Low level lua C API */
39 #include <lauxlib.h> /* Higher level C API */
46 /*****************************************************************************
48 *****************************************************************************/
50 /* Dialog functions */
51 static int vlclua_dialog_create( lua_State *L );
52 static int vlclua_dialog_delete( lua_State *L );
53 static int vlclua_dialog_show( lua_State *L );
54 static int vlclua_dialog_hide( lua_State *L );
55 static int vlclua_dialog_flush( lua_State *L );
56 static void lua_SetDialogUpdate( lua_State *L, int flag );
57 static int lua_GetDialogUpdate( lua_State *L );
58 int lua_DialogFlush( lua_State *L );
60 static int vlclua_dialog_add_button( lua_State *L );
61 static int vlclua_dialog_add_label( lua_State *L );
62 static int vlclua_dialog_add_html( lua_State *L );
63 static int vlclua_dialog_add_text_inner( lua_State *L, int );
64 static inline int vlclua_dialog_add_text_input( lua_State *L )
66 return vlclua_dialog_add_text_inner( L, EXTENSION_WIDGET_TEXT_FIELD );
68 static inline int vlclua_dialog_add_password( lua_State *L )
70 return vlclua_dialog_add_text_inner( L, EXTENSION_WIDGET_PASSWORD );
72 static inline int vlclua_dialog_add_html( lua_State *L )
74 return vlclua_dialog_add_text_inner( L, EXTENSION_WIDGET_HTML );
76 static int vlclua_dialog_add_check_box( lua_State *L );
77 static int vlclua_dialog_add_list( lua_State *L );
78 static int vlclua_dialog_add_dropdown( lua_State *L );
79 static int vlclua_dialog_add_image( lua_State *L );
80 static int vlclua_create_widget_inner( lua_State *L, int i_args,
81 extension_widget_t *p_widget);
83 static int vlclua_dialog_delete_widget( lua_State *L );
86 static int vlclua_widget_set_text( lua_State *L );
87 static int vlclua_widget_get_text( lua_State *L );
88 static int vlclua_widget_set_checked( lua_State *L );
89 static int vlclua_widget_get_checked( lua_State *L );
90 static int vlclua_widget_add_value( lua_State *L );
91 static int vlclua_widget_get_value( lua_State *L );
92 static int vlclua_widget_clear( lua_State *L );
93 static int vlclua_widget_get_selection( lua_State *L );
96 static void AddWidget( extension_dialog_t *p_dialog,
97 extension_widget_t *p_widget );
98 static int DeleteWidget( extension_dialog_t *p_dialog,
99 extension_widget_t *p_widget );
101 static const luaL_Reg vlclua_dialog_reg[] = {
102 { "show", vlclua_dialog_show },
103 { "hide", vlclua_dialog_hide },
104 { "close", vlclua_dialog_delete },
105 { "flush", vlclua_dialog_flush },
107 { "add_button", vlclua_dialog_add_button },
108 { "add_label", vlclua_dialog_add_label },
109 { "add_html", vlclua_dialog_add_html },
110 { "add_text_input", vlclua_dialog_add_text_input },
111 { "add_password", vlclua_dialog_add_password },
112 { "add_check_box", vlclua_dialog_add_check_box },
113 { "add_dropdown", vlclua_dialog_add_dropdown },
114 { "add_list", vlclua_dialog_add_list },
115 { "add_image", vlclua_dialog_add_image },
117 { "del_widget", vlclua_dialog_delete_widget },
121 static const luaL_Reg vlclua_widget_reg[] = {
122 { "set_text", vlclua_widget_set_text },
123 { "get_text", vlclua_widget_get_text },
124 { "set_checked", vlclua_widget_set_checked },
125 { "get_checked", vlclua_widget_get_checked },
126 { "add_value", vlclua_widget_add_value },
127 { "get_value", vlclua_widget_get_value },
128 { "clear", vlclua_widget_clear },
129 { "get_selection", vlclua_widget_get_selection },
133 /** Private static variable used for the registry index */
134 static const char key_opaque = 'A',
138 * Open dialog library for Lua
140 * @param opaque Object associated to this lua state
141 * @note opaque will be p_ext for extensions, p_sd for service discoveries
143 void luaopen_dialog( lua_State *L, void *opaque )
145 lua_getglobal( L, "vlc" );
146 lua_pushcfunction( L, vlclua_dialog_create );
147 lua_setfield( L, -2, "dialog" );
149 /* Add a private pointer (opaque) in the registry
150 * The &key pointer is used to have a unique entry in the registry
152 lua_pushlightuserdata( L, (void*) &key_opaque );
153 lua_pushlightuserdata( L, opaque );
154 lua_settable( L, LUA_REGISTRYINDEX );
156 /* Add private data: dialog update flag */
157 lua_SetDialogUpdate( L, 0 );
160 static int vlclua_dialog_create( lua_State *L )
162 if( !lua_isstring( L, 1 ) )
163 return luaL_error( L, "vlc.dialog() usage: (title)" );
164 const char *psz_title = luaL_checkstring( L, 1 );
166 vlc_object_t *p_this = vlclua_get_this( L );
168 extension_dialog_t *p_dlg = calloc( 1, sizeof( extension_dialog_t ) );
170 return 0; // luaL_error( L, "Out Of Memory" );
172 lua_getglobal( L, "vlc" );
173 lua_getfield( L, -1, "__dialog" );
174 if( lua_topointer( L, lua_gettop( L ) ) != NULL )
177 return luaL_error( L, "Only one dialog allowed per extension!" );
180 p_dlg->p_object = p_this;
181 p_dlg->psz_title = strdup( psz_title );
182 p_dlg->b_kill = false;
183 ARRAY_INIT( p_dlg->widgets );
185 /* Read the opaque value stored while loading the dialog library */
186 lua_pushlightuserdata( L, (void*) &key_opaque );
187 lua_gettable( L, LUA_REGISTRYINDEX );
188 p_dlg->p_sys = (void*) lua_topointer( L, -1 ); // "const" discarded
191 vlc_mutex_init( &p_dlg->lock );
192 vlc_cond_init( &p_dlg->cond );
194 /** @todo Use the registry instead of __dialog,
195 so that the user can't tamper with it */
197 lua_getglobal( L, "vlc" );
198 lua_pushlightuserdata( L, p_dlg );
199 lua_setfield( L, -2, "__dialog" );
202 extension_dialog_t **pp_dlg = lua_newuserdata( L, sizeof( void* ) );
205 if( luaL_newmetatable( L, "dialog" ) )
208 luaL_register( L, NULL, vlclua_dialog_reg );
209 lua_setfield( L, -2, "__index" );
210 lua_pushcfunction( L, vlclua_dialog_delete );
211 lua_setfield( L, -2, "__gc" );
214 lua_setmetatable( L, -2 );
216 msg_Dbg( p_this, "Creating dialog '%s'", psz_title );
217 lua_SetDialogUpdate( L, 0 );
222 static int vlclua_dialog_delete( lua_State *L )
224 vlc_object_t *p_mgr = vlclua_get_this( L );
226 /* Get dialog descriptor */
227 extension_dialog_t **pp_dlg =
228 (extension_dialog_t**) luaL_checkudata( L, 1, "dialog" );
230 if( !pp_dlg || !*pp_dlg )
231 return luaL_error( L, "Can't get pointer to dialog" );
233 extension_dialog_t *p_dlg = *pp_dlg;
236 /* Remove private __dialog field */
237 lua_getglobal( L, "vlc" );
239 lua_setfield( L, -2, "__dialog" );
241 assert( !p_dlg->b_kill );
243 /* Immediately deleting the dialog */
244 msg_Dbg( p_mgr, "Deleting dialog '%s'", p_dlg->psz_title );
245 p_dlg->b_kill = true;
246 lua_SetDialogUpdate( L, 0 ); // Reset the update flag
247 dialog_ExtensionUpdate( p_mgr, p_dlg );
249 /* After dialog_ExtensionUpdate, the UI thread must take the lock asap and
250 * then signal us when it's done deleting the dialog.
252 msg_Dbg( p_mgr, "Waiting for the dialog to be deleted..." );
253 vlc_mutex_lock( &p_dlg->lock );
254 while( p_dlg->p_sys_intf != NULL )
256 vlc_cond_wait( &p_dlg->cond, &p_dlg->lock );
258 vlc_mutex_unlock( &p_dlg->lock );
260 free( p_dlg->psz_title );
261 p_dlg->psz_title = NULL;
263 /* Destroy widgets */
264 extension_widget_t *p_widget;
265 FOREACH_ARRAY( p_widget, p_dlg->widgets )
269 free( p_widget->psz_text );
272 struct extension_widget_value_t *p_value, *p_next;
273 for( p_value = p_widget->p_values; p_value != NULL; p_value = p_next )
275 p_next = p_value->p_next;
276 free( p_value->psz_text );
282 ARRAY_RESET( p_dlg->widgets );
284 /* Note: At this point, the UI must not use these resources */
285 vlc_mutex_destroy( &p_dlg->lock );
286 vlc_cond_destroy( &p_dlg->cond );
291 /** Show the dialog */
292 static int vlclua_dialog_show( lua_State *L )
294 extension_dialog_t **pp_dlg =
295 (extension_dialog_t**) luaL_checkudata( L, 1, "dialog" );
296 if( !pp_dlg || !*pp_dlg )
297 return luaL_error( L, "Can't get pointer to dialog" );
298 extension_dialog_t *p_dlg = *pp_dlg;
300 p_dlg->b_hide = false;
301 lua_SetDialogUpdate( L, 1 );
306 /** Hide the dialog */
307 static int vlclua_dialog_hide( lua_State *L )
309 extension_dialog_t **pp_dlg =
310 (extension_dialog_t**) luaL_checkudata( L, 1, "dialog" );
311 if( !pp_dlg || !*pp_dlg )
312 return luaL_error( L, "Can't get pointer to dialog" );
313 extension_dialog_t *p_dlg = *pp_dlg;
315 p_dlg->b_hide = true;
316 lua_SetDialogUpdate( L, 1 );
322 /** Flush the dialog */
323 static int vlclua_dialog_flush( lua_State *L )
325 vlc_object_t *p_mgr = vlclua_get_this( L );
327 extension_dialog_t **pp_dlg =
328 (extension_dialog_t**) luaL_checkudata( L, 1, "dialog" );
329 if( !pp_dlg || !*pp_dlg )
330 return luaL_error( L, "Can't get pointer to dialog" );
331 extension_dialog_t *p_dlg = *pp_dlg;
333 // Updating dialog immediately
334 dialog_ExtensionUpdate( p_mgr, p_dlg );
337 lua_SetDialogUpdate( L, 0 );
342 static void lua_SetDialogUpdate( lua_State *L, int flag )
344 /* Set entry in the Lua registry */
345 lua_pushlightuserdata( L, (void*) &key_update );
346 lua_pushinteger( L, flag );
347 lua_settable( L, LUA_REGISTRYINDEX );
350 static int lua_GetDialogUpdate( lua_State *L )
352 /* Read entry in the Lua registry */
353 lua_pushlightuserdata( L, (void*) &key_update );
354 lua_gettable( L, LUA_REGISTRYINDEX );
355 return luaL_checkinteger( L, -1 );
358 /** Manually flush a dialog
359 * This can be called after a lua_pcall
360 * @return SUCCESS if there is no dialog or the update was successful
361 * @todo If there can be multiple dialogs, this function will have to
362 * be fixed (lookup for dialog)
364 int lua_DialogFlush( lua_State *L )
366 lua_getglobal( L, "vlc" );
367 lua_getfield( L, -1, "__dialog" );
368 extension_dialog_t *p_dlg = ( extension_dialog_t* )lua_topointer( L, -1 );
373 int i_ret = VLC_SUCCESS;
374 if( lua_GetDialogUpdate( L ) )
376 i_ret = dialog_ExtensionUpdate( vlclua_get_this( L ), p_dlg );
377 lua_SetDialogUpdate( L, 0 );
384 * Create a button: add_button
385 * Arguments: text, function (as string)
388 static int vlclua_dialog_add_button( lua_State *L )
390 /* Verify arguments */
391 if( !lua_isstring( L, 2 ) || !lua_isstring( L, 3 ) )
392 return luaL_error( L, "dialog:add_button usage: (text, func)" );
394 extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
395 p_widget->type = EXTENSION_WIDGET_BUTTON;
396 p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
397 p_widget->p_sys = strdup( luaL_checkstring( L, 3 ) );
399 return vlclua_create_widget_inner( L, 2, p_widget );
403 * Create a text label: add_label
407 static int vlclua_dialog_add_label( lua_State *L )
409 /* Verify arguments */
410 if( !lua_isstring( L, 2 ) )
411 return luaL_error( L, "dialog:add_label usage: (text)" );
412 extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
413 p_widget->type = EXTENSION_WIDGET_LABEL;
414 p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
416 return vlclua_create_widget_inner( L, 1, p_widget );
420 * Create a text area: add_html, add_text_input, add_password
421 * Arguments: text (may be nil)
422 * Qt: QLineEdit (Text/Password) or QTextArea (HTML)
424 static int vlclua_dialog_add_text_inner( lua_State *L, int i_type )
426 /* Verify arguments */
427 if( !lua_isstring( L, 2 ) && !lua_isnil( L, 2 ) )
428 return luaL_error( L, "dialog:add_text_input usage: (text = nil)" );
430 extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
431 p_widget->type = i_type;
432 if( !lua_isnil( L, 2 ) )
433 p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
435 return vlclua_create_widget_inner( L, 1, p_widget );
439 * Create a checkable box: add_check_box
440 * Arguments: text, checked (as bool)
443 static int vlclua_dialog_add_check_box( lua_State *L )
445 /* Verify arguments */
446 if( !lua_isstring( L, 2 ) )
447 return luaL_error( L, "dialog:add_check_box usage: (text, checked)" );
449 extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
450 p_widget->type = EXTENSION_WIDGET_CHECK_BOX;
451 p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
452 p_widget->b_checked = lua_toboolean( L, 3 );
454 return vlclua_create_widget_inner( L, 2, p_widget );
458 * Create a drop-down list (non editable)
461 * @todo make it editable?
463 static int vlclua_dialog_add_dropdown( lua_State *L )
465 extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
466 p_widget->type = EXTENSION_WIDGET_DROPDOWN;
468 return vlclua_create_widget_inner( L, 0, p_widget );
472 * Create a list panel (multiple selection)
476 static int vlclua_dialog_add_list( lua_State *L )
478 extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
479 p_widget->type = EXTENSION_WIDGET_LIST;
481 return vlclua_create_widget_inner( L, 0, p_widget );
485 * Create an image label
487 * Qt: QLabel with setPixmap( QPixmap& )
489 static int vlclua_dialog_add_image( lua_State *L )
491 /* Verify arguments */
492 if( !lua_isstring( L, 2 ) )
493 return luaL_error( L, "dialog:add_image usage: (filename)" );
495 extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
496 p_widget->type = EXTENSION_WIDGET_IMAGE;
497 p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
499 return vlclua_create_widget_inner( L, 1, p_widget );
503 * Internal helper to finalize the creation of a widget
505 * @param i_args Number of arguments before "row" (0 or more)
506 * @param p_widget The widget to add
508 static int vlclua_create_widget_inner( lua_State *L, int i_args,
509 extension_widget_t *p_widget )
511 int arg = i_args + 2;
514 extension_dialog_t **pp_dlg =
515 (extension_dialog_t**) luaL_checkudata( L, 1, "dialog" );
516 if( !pp_dlg || !*pp_dlg )
517 return luaL_error( L, "Can't get pointer to dialog" );
518 extension_dialog_t *p_dlg = *pp_dlg;
520 /* Set parent dialog */
521 p_widget->p_dialog = p_dlg;
523 /* Set common arguments: col, row, hspan, vspan, width, height */
524 if( lua_isnumber( L, arg ) )
525 p_widget->i_column = luaL_checkinteger( L, arg );
526 else goto end_of_args;
527 if( lua_isnumber( L, ++arg ) )
528 p_widget->i_row = luaL_checkinteger( L, arg );
529 else goto end_of_args;
530 if( lua_isnumber( L, ++arg ) )
531 p_widget->i_horiz_span = luaL_checkinteger( L, arg );
532 else goto end_of_args;
533 if( lua_isnumber( L, ++arg ) )
534 p_widget->i_vert_span = luaL_checkinteger( L, arg );
535 else goto end_of_args;
536 if( lua_isnumber( L, ++arg ) )
537 p_widget->i_width = luaL_checkinteger( L, arg );
538 else goto end_of_args;
539 if( lua_isnumber( L, ++arg ) )
540 p_widget->i_height = luaL_checkinteger( L, arg );
541 else goto end_of_args;
544 vlc_mutex_lock( &p_dlg->lock );
546 /* Add the widget to the dialog descriptor */
547 AddWidget( p_dlg, p_widget );
549 vlc_mutex_unlock( &p_dlg->lock );
551 /* Create meta table */
552 extension_widget_t **pp_widget = lua_newuserdata( L, sizeof( void* ) );
553 *pp_widget = p_widget;
554 if( luaL_newmetatable( L, "widget" ) )
557 luaL_register( L, NULL, vlclua_widget_reg );
558 lua_setfield( L, -2, "__index" );
560 lua_setmetatable( L, -2 );
562 lua_SetDialogUpdate( L, 1 );
567 static int vlclua_widget_set_text( lua_State *L )
570 extension_widget_t **pp_widget =
571 (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
572 if( !pp_widget || !*pp_widget )
573 return luaL_error( L, "Can't get pointer to widget" );
574 extension_widget_t *p_widget = *pp_widget;
576 /* Verify arguments */
577 if( !lua_isstring( L, 2 ) )
578 return luaL_error( L, "widget:set_text usage: (text)" );
580 /* Verify widget type */
581 switch( p_widget->type )
583 case EXTENSION_WIDGET_LABEL:
584 case EXTENSION_WIDGET_BUTTON:
585 case EXTENSION_WIDGET_HTML:
586 case EXTENSION_WIDGET_TEXT_FIELD:
587 case EXTENSION_WIDGET_PASSWORD:
588 case EXTENSION_WIDGET_DROPDOWN:
589 case EXTENSION_WIDGET_CHECK_BOX:
591 case EXTENSION_WIDGET_LIST:
592 case EXTENSION_WIDGET_IMAGE:
594 return luaL_error( L, "method set_text not valid for this widget" );
597 vlc_mutex_lock( &p_widget->p_dialog->lock );
600 p_widget->b_update = true;
601 free( p_widget->psz_text );
602 p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
604 vlc_mutex_unlock( &p_widget->p_dialog->lock );
606 lua_SetDialogUpdate( L, 1 );
611 static int vlclua_widget_get_text( lua_State *L )
614 extension_widget_t **pp_widget =
615 (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
616 if( !pp_widget || !*pp_widget )
617 return luaL_error( L, "Can't get pointer to widget" );
618 extension_widget_t *p_widget = *pp_widget;
620 /* Verify widget type */
621 switch( p_widget->type )
623 case EXTENSION_WIDGET_LABEL:
624 case EXTENSION_WIDGET_BUTTON:
625 case EXTENSION_WIDGET_HTML:
626 case EXTENSION_WIDGET_TEXT_FIELD:
627 case EXTENSION_WIDGET_PASSWORD:
628 case EXTENSION_WIDGET_DROPDOWN:
629 case EXTENSION_WIDGET_CHECK_BOX:
631 case EXTENSION_WIDGET_LIST:
632 case EXTENSION_WIDGET_IMAGE:
634 return luaL_error( L, "method get_text not valid for this widget" );
637 extension_dialog_t *p_dlg = p_widget->p_dialog;
638 vlc_mutex_lock( &p_dlg->lock );
640 char *psz_text = NULL;
641 if( p_widget->psz_text )
642 psz_text = strdup( p_widget->psz_text );
643 vlc_mutex_unlock( &p_dlg->lock );
645 lua_pushstring( L, psz_text );
651 static int vlclua_widget_get_checked( lua_State *L )
654 extension_widget_t **pp_widget =
655 (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
656 if( !pp_widget || !*pp_widget )
657 return luaL_error( L, "Can't get pointer to widget" );
658 extension_widget_t *p_widget = *pp_widget;
660 if( p_widget->type != EXTENSION_WIDGET_CHECK_BOX )
661 return luaL_error( L, "method get_checked not valid for this widget" );
663 vlc_mutex_lock( &p_widget->p_dialog->lock );
664 lua_pushboolean( L, p_widget->b_checked );
665 vlc_mutex_unlock( &p_widget->p_dialog->lock );
670 static int vlclua_widget_add_value( lua_State *L )
673 extension_widget_t **pp_widget =
674 (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
675 if( !pp_widget || !*pp_widget )
676 return luaL_error( L, "Can't get pointer to widget" );
677 extension_widget_t *p_widget = *pp_widget;
679 if( p_widget->type != EXTENSION_WIDGET_DROPDOWN
680 && p_widget->type != EXTENSION_WIDGET_LIST )
681 return luaL_error( L, "method add_value not valid for this widget" );
683 if( !lua_isstring( L, 2 ) )
684 return luaL_error( L, "widget:add_value usage: (text, id = 0)" );
686 struct extension_widget_value_t *p_value,
687 *p_new_value = calloc( 1, sizeof( struct extension_widget_value_t ) );
688 p_new_value->psz_text = strdup( luaL_checkstring( L, 2 ) );
689 p_new_value->i_id = lua_tointeger( L, 3 );
691 vlc_mutex_lock( &p_widget->p_dialog->lock );
693 if( !p_widget->p_values )
695 p_widget->p_values = p_new_value;
699 for( p_value = p_widget->p_values;
700 p_value->p_next != NULL;
701 p_value = p_value->p_next )
702 { /* Do nothing, iterate to find the end */ }
703 p_value->p_next = p_new_value;
706 p_widget->b_update = true;
707 vlc_mutex_unlock( &p_widget->p_dialog->lock );
709 lua_SetDialogUpdate( L, 1 );
714 static int vlclua_widget_get_value( lua_State *L )
717 extension_widget_t **pp_widget =
718 (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
719 if( !pp_widget || !*pp_widget )
720 return luaL_error( L, "Can't get pointer to widget" );
721 extension_widget_t *p_widget = *pp_widget;
723 if( p_widget->type != EXTENSION_WIDGET_DROPDOWN )
724 return luaL_error( L, "method get_value not valid for this widget" );
726 vlc_mutex_lock( &p_widget->p_dialog->lock );
728 struct extension_widget_value_t *p_value;
729 for( p_value = p_widget->p_values;
731 p_value = p_value->p_next )
733 if( p_value->b_selected )
735 lua_pushinteger( L, p_value->i_id );
736 lua_pushstring( L, p_value->psz_text );
737 vlc_mutex_unlock( &p_widget->p_dialog->lock );
742 vlc_mutex_unlock( &p_widget->p_dialog->lock );
744 lua_pushinteger( L, -1 );
749 static int vlclua_widget_clear( lua_State *L )
752 extension_widget_t **pp_widget =
753 (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
754 if( !pp_widget || !*pp_widget )
755 return luaL_error( L, "Can't get pointer to widget" );
756 extension_widget_t *p_widget = *pp_widget;
758 if( p_widget->type != EXTENSION_WIDGET_DROPDOWN
759 && p_widget->type != EXTENSION_WIDGET_LIST )
760 return luaL_error( L, "method clear not valid for this widget" );
762 struct extension_widget_value_t *p_value, *p_next;
764 vlc_mutex_lock( &p_widget->p_dialog->lock );
766 for( p_value = p_widget->p_values;
770 p_next = p_value->p_next;
771 free( p_value->psz_text );
775 p_widget->p_values = NULL;
776 p_widget->b_update = true;
778 vlc_mutex_unlock( &p_widget->p_dialog->lock );
780 lua_SetDialogUpdate( L, 1 );
785 static int vlclua_widget_get_selection( lua_State *L )
788 extension_widget_t **pp_widget =
789 (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
790 if( !pp_widget || !*pp_widget )
791 return luaL_error( L, "Can't get pointer to widget" );
792 extension_widget_t *p_widget = *pp_widget;
794 if( p_widget->type != EXTENSION_WIDGET_LIST )
795 return luaL_error( L, "method get_selection not valid for this widget" );
797 /* Create empty table */
800 vlc_mutex_lock( &p_widget->p_dialog->lock );
802 struct extension_widget_value_t *p_value;
803 for( p_value = p_widget->p_values;
805 p_value = p_value->p_next )
807 if( p_value->b_selected )
809 lua_pushinteger( L, p_value->i_id );
810 lua_pushstring( L, p_value->psz_text );
811 lua_settable( L, -3 );
815 vlc_mutex_unlock( &p_widget->p_dialog->lock );
821 static int vlclua_widget_set_checked( lua_State *L )
824 extension_widget_t **pp_widget =
825 (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
826 if( !pp_widget || !*pp_widget )
827 return luaL_error( L, "Can't get pointer to widget" );
828 extension_widget_t *p_widget = *pp_widget;
830 if( p_widget->type != EXTENSION_WIDGET_CHECK_BOX )
831 return luaL_error( L, "method set_checked not valid for this widget" );
833 /* Verify arguments */
834 if( !lua_isboolean( L, 2 ) )
835 return luaL_error( L, "widget:set_checked usage: (bool)" );
837 vlc_mutex_lock( &p_widget->p_dialog->lock );
839 bool b_old_check = p_widget->b_checked;
840 p_widget->b_checked = lua_toboolean( L, 2 );
842 vlc_mutex_unlock( &p_widget->p_dialog->lock );
844 if( b_old_check != p_widget->b_checked )
846 /* Signal interface of the change */
847 p_widget->b_update = true;
848 lua_SetDialogUpdate( L, 1 );
855 * Delete a widget from a dialog
856 * Remove it from the list once it has been safely destroyed by the interface
857 * @note This will always flush the dialog
859 static int vlclua_dialog_delete_widget( lua_State *L )
862 extension_dialog_t **pp_dlg =
863 (extension_dialog_t**) luaL_checkudata( L, 1, "dialog" );
864 if( !pp_dlg || !*pp_dlg )
865 return luaL_error( L, "Can't get pointer to dialog" );
866 extension_dialog_t *p_dlg = *pp_dlg;
869 if( !lua_isuserdata( L, 2 ) )
870 return luaL_error( L, "Argument to del_widget is not a widget" );
873 extension_widget_t **pp_widget =
874 (extension_widget_t **) luaL_checkudata( L, 2, "widget" );
875 if( !pp_widget || !*pp_widget )
876 return luaL_error( L, "Can't get pointer to widget" );
877 extension_widget_t *p_widget = *pp_widget;
882 vlc_object_t *p_mgr = vlclua_get_this( L );
884 p_widget->b_kill = true;
886 lua_SetDialogUpdate( L, 0 ); // Reset update flag
887 int i_ret = dialog_ExtensionUpdate( p_mgr, p_dlg );
889 if( i_ret != VLC_SUCCESS )
891 return luaL_error( L, "Could not delete widget" );
894 vlc_mutex_lock( &p_dlg->lock );
896 /* Same remarks as for dialog delete.
897 * If the dialog is deleted or about to be deleted, then there is no
898 * need to wait on this particular widget that already doesn't exist
899 * anymore in the UI */
900 while( p_widget->p_sys_intf != NULL && !p_dlg->b_kill
901 && p_dlg->p_sys_intf != NULL )
903 vlc_cond_wait( &p_dlg->cond, &p_dlg->lock );
906 i_ret = DeleteWidget( p_dlg, p_widget );
908 vlc_mutex_unlock( &p_dlg->lock );
910 if( i_ret != VLC_SUCCESS )
912 return luaL_error( L, "Could not remove widget from list" );
920 * Below this line, no Lua specific code.
926 * Add a widget to the widget list of a dialog
927 * @note Must be entered with lock on dialog
929 static void AddWidget( extension_dialog_t *p_dialog,
930 extension_widget_t *p_widget )
932 ARRAY_APPEND( p_dialog->widgets, p_widget );
936 * Remove a widget from the widget list of a dialog
937 * @note The widget MUST have been safely killed before
938 * @note Must be entered with lock on dialog
940 static int DeleteWidget( extension_dialog_t *p_dialog,
941 extension_widget_t *p_widget )
945 extension_widget_t *p_iter;
946 FOREACH_ARRAY( p_iter, p_dialog->widgets )
949 if( p_iter == p_widget )
960 ARRAY_REMOVE( p_dialog->widgets, pos );
962 /* Now free the data */
963 free( p_widget->p_sys );
964 struct extension_widget_value_t *p_value = p_widget->p_values;
967 free( p_value->psz_text );
968 struct extension_widget_value_t *old = p_value;
969 p_value = p_value->p_next;
972 free( p_widget->psz_text );