]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/dialog.c
0cddd3072defb867f21acbaac8400372001a5f9f
[vlc] / modules / misc / lua / libs / dialog.c
1 /*****************************************************************************
2  * dialog.c: Functions to create interface dialogs from Lua extensions
3  *****************************************************************************
4  * Copyright (C) 2009-2010 VideoLAN and authors
5  * $Id$
6  *
7  * Authors: Jean-Philippe AndrĂ© < jpeg # videolan.org >
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_extensions.h>
37
38 #include <lua.h>        /* Low level lua C API */
39 #include <lauxlib.h>    /* Higher level C API */
40
41 #include "../vlc.h"
42 #include "../libs.h"
43
44 #include "assert.h"
45
46 /*****************************************************************************
47  *
48  *****************************************************************************/
49
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 );
59
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 )
65 {
66     return vlclua_dialog_add_text_inner( L, EXTENSION_WIDGET_TEXT_FIELD );
67 }
68 static inline int vlclua_dialog_add_password( lua_State *L )
69 {
70     return vlclua_dialog_add_text_inner( L, EXTENSION_WIDGET_PASSWORD );
71 }
72 static inline int vlclua_dialog_add_html( lua_State *L )
73 {
74     return vlclua_dialog_add_text_inner( L, EXTENSION_WIDGET_HTML );
75 }
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);
82
83 static int vlclua_dialog_delete_widget( lua_State *L );
84
85 /* Widget methods */
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 );
94
95 /* Helpers */
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 );
100
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 },
106
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 },
116
117     { "del_widget", vlclua_dialog_delete_widget },
118     { NULL, NULL }
119 };
120
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 },
130     { NULL, NULL }
131 };
132
133 /** Private static variable used for the registry index */
134 static const char key_opaque = 'A',
135                   key_update = 'B';
136
137 /**
138  * Open dialog library for Lua
139  * @param L lua_State
140  * @param opaque Object associated to this lua state
141  * @note opaque will be p_ext for extensions, p_sd for service discoveries
142  **/
143 void luaopen_dialog( lua_State *L, void *opaque )
144 {
145     lua_getglobal( L, "vlc" );
146     lua_pushcfunction( L, vlclua_dialog_create );
147     lua_setfield( L, -2, "dialog" );
148
149     /* Add a private pointer (opaque) in the registry
150      * The &key pointer is used to have a unique entry in the registry
151      */
152     lua_pushlightuserdata( L, (void*) &key_opaque );
153     lua_pushlightuserdata( L, opaque );
154     lua_settable( L, LUA_REGISTRYINDEX );
155
156     /* Add private data: dialog update flag */
157     lua_SetDialogUpdate( L, 0 );
158 }
159
160 static int vlclua_dialog_create( lua_State *L )
161 {
162     if( !lua_isstring( L, 1 ) )
163         return luaL_error( L, "vlc.dialog() usage: (title)" );
164     const char *psz_title = luaL_checkstring( L, 1 );
165
166     vlc_object_t *p_this = vlclua_get_this( L );
167
168     extension_dialog_t *p_dlg = calloc( 1, sizeof( extension_dialog_t ) );
169     if( !p_dlg )
170         return 0; // luaL_error( L, "Out Of Memory" );
171
172     lua_getglobal( L, "vlc" );
173     lua_getfield( L, -1, "__dialog" );
174     if( lua_topointer( L, lua_gettop( L ) ) != NULL )
175     {
176         free( p_dlg );
177         return luaL_error( L, "Only one dialog allowed per extension!" );
178     }
179
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 );
184
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
189     lua_pop( L, 1 );
190
191     vlc_mutex_init( &p_dlg->lock );
192     vlc_cond_init( &p_dlg->cond );
193
194     /** @todo Use the registry instead of __dialog,
195         so that the user can't tamper with it */
196
197     lua_getglobal( L, "vlc" );
198     lua_pushlightuserdata( L, p_dlg );
199     lua_setfield( L, -2, "__dialog" );
200     lua_pop( L, 1 );
201
202     extension_dialog_t **pp_dlg = lua_newuserdata( L, sizeof( void* ) );
203     *pp_dlg = p_dlg;
204
205     if( luaL_newmetatable( L, "dialog" ) )
206     {
207         lua_newtable( L );
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" );
212     }
213
214     lua_setmetatable( L, -2 );
215
216     msg_Dbg( p_this, "Creating dialog '%s'", psz_title );
217     lua_SetDialogUpdate( L, 0 );
218
219     return 1;
220 }
221
222 static int vlclua_dialog_delete( lua_State *L )
223 {
224     vlc_object_t *p_mgr = vlclua_get_this( L );
225
226     /* Get dialog descriptor */
227     extension_dialog_t **pp_dlg =
228             (extension_dialog_t**) luaL_checkudata( L, 1, "dialog" );
229
230     if( !pp_dlg || !*pp_dlg )
231         return luaL_error( L, "Can't get pointer to dialog" );
232
233     extension_dialog_t *p_dlg = *pp_dlg;
234     *pp_dlg = NULL;
235
236     /* Remove private __dialog field */
237     lua_getglobal( L, "vlc" );
238     lua_pushnil( L );
239     lua_setfield( L, -2, "__dialog" );
240
241     assert( !p_dlg->b_kill );
242
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 );
248
249     /* After dialog_ExtensionUpdate, the UI thread must take the lock asap and
250      * then signal us when it's done deleting the dialog.
251      */
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 )
255     {
256         vlc_cond_wait( &p_dlg->cond, &p_dlg->lock );
257     }
258     vlc_mutex_unlock( &p_dlg->lock );
259
260     free( p_dlg->psz_title );
261     p_dlg->psz_title = NULL;
262
263     /* Destroy widgets */
264     extension_widget_t *p_widget;
265     FOREACH_ARRAY( p_widget, p_dlg->widgets )
266     {
267         if( !p_widget )
268             continue;
269         free( p_widget->psz_text );
270
271         /* Free data */
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 )
274         {
275             p_next = p_value->p_next;
276             free( p_value->psz_text );
277             free( p_value );
278         }
279     }
280     FOREACH_END()
281
282     ARRAY_RESET( p_dlg->widgets );
283
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 );
287
288     return 1;
289 }
290
291 /** Show the dialog */
292 static int vlclua_dialog_show( lua_State *L )
293 {
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;
299
300     p_dlg->b_hide = false;
301     lua_SetDialogUpdate( L, 1 );
302
303     return 1;
304 }
305
306 /** Hide the dialog */
307 static int vlclua_dialog_hide( lua_State *L )
308 {
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;
314
315     p_dlg->b_hide = true;
316     lua_SetDialogUpdate( L, 1 );
317
318     return 1;
319 }
320
321
322 /** Flush the dialog */
323 static int vlclua_dialog_flush( lua_State *L )
324 {
325     vlc_object_t *p_mgr = vlclua_get_this( L );
326
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;
332
333     // Updating dialog immediately
334     dialog_ExtensionUpdate( p_mgr, p_dlg );
335
336     // Reset update flag
337     lua_SetDialogUpdate( L, 0 );
338
339     return 1;
340 }
341
342 static void lua_SetDialogUpdate( lua_State *L, int flag )
343 {
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 );
348 }
349
350 static int lua_GetDialogUpdate( lua_State *L )
351 {
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 );
356 }
357
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)
363  */
364 int lua_DialogFlush( lua_State *L )
365 {
366     lua_getglobal( L, "vlc" );
367     lua_getfield( L, -1, "__dialog" );
368     extension_dialog_t *p_dlg = ( extension_dialog_t* )lua_topointer( L, -1 );
369
370     if( !p_dlg )
371         return VLC_SUCCESS;
372
373     int i_ret = VLC_SUCCESS;
374     if( lua_GetDialogUpdate( L ) )
375     {
376         i_ret = dialog_ExtensionUpdate( vlclua_get_this( L ), p_dlg );
377         lua_SetDialogUpdate( L, 0 );
378     }
379
380     return i_ret;
381 }
382
383 /**
384  * Create a button: add_button
385  * Arguments: text, function (as string)
386  * Qt: QPushButton
387  **/
388 static int vlclua_dialog_add_button( lua_State *L )
389 {
390     /* Verify arguments */
391     if( !lua_isstring( L, 2 ) || !lua_isstring( L, 3 ) )
392         return luaL_error( L, "dialog:add_button usage: (text, func)" );
393
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 ) );
398
399     return vlclua_create_widget_inner( L, 2, p_widget );
400 }
401
402 /**
403  * Create a text label: add_label
404  * Arguments: text
405  * Qt: QLabel
406  **/
407 static int vlclua_dialog_add_label( lua_State *L )
408 {
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 ) );
415
416     return vlclua_create_widget_inner( L, 1, p_widget );
417 }
418
419 /**
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)
423  **/
424 static int vlclua_dialog_add_text_inner( lua_State *L, int i_type )
425 {
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)" );
429
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 ) );
434
435     return vlclua_create_widget_inner( L, 1, p_widget );
436 }
437
438 /**
439  * Create a checkable box: add_check_box
440  * Arguments: text, checked (as bool)
441  * Qt: QCheckBox
442  **/
443 static int vlclua_dialog_add_check_box( lua_State *L )
444 {
445     /* Verify arguments */
446     if( !lua_isstring( L, 2 ) )
447         return luaL_error( L, "dialog:add_check_box usage: (text, checked)" );
448
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 );
453
454     return vlclua_create_widget_inner( L, 2, p_widget );
455 }
456
457 /**
458  * Create a drop-down list (non editable)
459  * Arguments: (none)
460  * Qt: QComboBox
461  * @todo make it editable?
462  **/
463 static int vlclua_dialog_add_dropdown( lua_State *L )
464 {
465     extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
466     p_widget->type = EXTENSION_WIDGET_DROPDOWN;
467
468     return vlclua_create_widget_inner( L, 0, p_widget );
469 }
470
471 /**
472  * Create a list panel (multiple selection)
473  * Arguments: (none)
474  * Qt: QListWidget
475  **/
476 static int vlclua_dialog_add_list( lua_State *L )
477 {
478     extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
479     p_widget->type = EXTENSION_WIDGET_LIST;
480
481     return vlclua_create_widget_inner( L, 0, p_widget );
482 }
483
484 /**
485  * Create an image label
486  * Arguments: url
487  * Qt: QLabel with setPixmap( QPixmap& )
488  **/
489 static int vlclua_dialog_add_image( lua_State *L )
490 {
491     /* Verify arguments */
492     if( !lua_isstring( L, 2 ) )
493         return luaL_error( L, "dialog:add_image usage: (filename)" );
494
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 ) );
498
499     return vlclua_create_widget_inner( L, 1, p_widget );
500 }
501
502 /**
503  * Internal helper to finalize the creation of a widget
504  * @param L Lua State
505  * @param i_args Number of arguments before "row" (0 or more)
506  * @param p_widget The widget to add
507  **/
508 static int vlclua_create_widget_inner( lua_State *L, int i_args,
509                                        extension_widget_t *p_widget )
510 {
511     int arg = i_args + 2;
512
513     /* Get dialog */
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;
519
520     /* Set parent dialog */
521     p_widget->p_dialog = p_dlg;
522
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;
542
543 end_of_args:
544     vlc_mutex_lock( &p_dlg->lock );
545
546     /* Add the widget to the dialog descriptor */
547     AddWidget( p_dlg, p_widget );
548
549     vlc_mutex_unlock( &p_dlg->lock );
550
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" ) )
555     {
556         lua_newtable( L );
557         luaL_register( L, NULL, vlclua_widget_reg );
558         lua_setfield( L, -2, "__index" );
559     }
560     lua_setmetatable( L, -2 );
561
562     lua_SetDialogUpdate( L, 1 );
563
564     return 1;
565 }
566
567 static int vlclua_widget_set_text( lua_State *L )
568 {
569     /* Get dialog */
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;
575
576     /* Verify arguments */
577     if( !lua_isstring( L, 2 ) )
578         return luaL_error( L, "widget:set_text usage: (text)" );
579
580     /* Verify widget type */
581     switch( p_widget->type )
582     {
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:
590             break;
591         case EXTENSION_WIDGET_LIST:
592         case EXTENSION_WIDGET_IMAGE:
593         default:
594             return luaL_error( L, "method set_text not valid for this widget" );
595     }
596
597     vlc_mutex_lock( &p_widget->p_dialog->lock );
598
599     /* Update widget */
600     p_widget->b_update = true;
601     free( p_widget->psz_text );
602     p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
603
604     vlc_mutex_unlock( &p_widget->p_dialog->lock );
605
606     lua_SetDialogUpdate( L, 1 );
607
608     return 1;
609 }
610
611 static int vlclua_widget_get_text( lua_State *L )
612 {
613     /* Get dialog */
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;
619
620     /* Verify widget type */
621     switch( p_widget->type )
622     {
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:
630             break;
631         case EXTENSION_WIDGET_LIST:
632         case EXTENSION_WIDGET_IMAGE:
633         default:
634             return luaL_error( L, "method get_text not valid for this widget" );
635     }
636
637     extension_dialog_t *p_dlg = p_widget->p_dialog;
638     vlc_mutex_lock( &p_dlg->lock );
639
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 );
644
645     lua_pushstring( L, psz_text );
646
647     free( psz_text );
648     return 1;
649 }
650
651 static int vlclua_widget_get_checked( lua_State *L )
652 {
653     /* Get widget */
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;
659
660     if( p_widget->type != EXTENSION_WIDGET_CHECK_BOX )
661         return luaL_error( L, "method get_checked not valid for this widget" );
662
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 );
666
667     return 1;
668 }
669
670 static int vlclua_widget_add_value( lua_State *L )
671 {
672     /* Get widget */
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;
678
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" );
682
683     if( !lua_isstring( L, 2 ) )
684         return luaL_error( L, "widget:add_value usage: (text, id = 0)" );
685
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 );
690
691     vlc_mutex_lock( &p_widget->p_dialog->lock );
692
693     if( !p_widget->p_values )
694     {
695         p_widget->p_values = p_new_value;
696     }
697     else
698     {
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;
704     }
705
706     p_widget->b_update = true;
707     vlc_mutex_unlock( &p_widget->p_dialog->lock );
708
709     lua_SetDialogUpdate( L, 1 );
710
711     return 1;
712 }
713
714 static int vlclua_widget_get_value( lua_State *L )
715 {
716     /* Get widget */
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;
722
723     if( p_widget->type != EXTENSION_WIDGET_DROPDOWN )
724         return luaL_error( L, "method get_value not valid for this widget" );
725
726     vlc_mutex_lock( &p_widget->p_dialog->lock );
727
728     struct extension_widget_value_t *p_value;
729     for( p_value = p_widget->p_values;
730          p_value != NULL;
731          p_value = p_value->p_next )
732     {
733         if( p_value->b_selected )
734         {
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 );
738             return 2;
739         }
740     }
741
742     vlc_mutex_unlock( &p_widget->p_dialog->lock );
743
744     lua_pushinteger( L, -1 );
745     lua_pushnil( L );
746     return 2;
747 }
748
749 static int vlclua_widget_clear( lua_State *L )
750 {
751     /* Get widget */
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;
757
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" );
761
762     struct extension_widget_value_t *p_value, *p_next;
763
764     vlc_mutex_lock( &p_widget->p_dialog->lock );
765
766     for( p_value = p_widget->p_values;
767          p_value != NULL;
768          p_value = p_next )
769     {
770         p_next = p_value->p_next;
771         free( p_value->psz_text );
772         free( p_value );
773     }
774
775     p_widget->p_values = NULL;
776     p_widget->b_update = true;
777
778     vlc_mutex_unlock( &p_widget->p_dialog->lock );
779
780     lua_SetDialogUpdate( L, 1 );
781
782     return 1;
783 }
784
785 static int vlclua_widget_get_selection( lua_State *L )
786 {
787     /* Get widget */
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;
793
794     if( p_widget->type != EXTENSION_WIDGET_LIST )
795         return luaL_error( L, "method get_selection not valid for this widget" );
796
797     /* Create empty table */
798     lua_newtable( L );
799
800     vlc_mutex_lock( &p_widget->p_dialog->lock );
801
802     struct extension_widget_value_t *p_value;
803     for( p_value = p_widget->p_values;
804          p_value != NULL;
805          p_value = p_value->p_next )
806     {
807         if( p_value->b_selected )
808         {
809             lua_pushinteger( L, p_value->i_id );
810             lua_pushstring( L, p_value->psz_text );
811             lua_settable( L, -3 );
812         }
813     }
814
815     vlc_mutex_unlock( &p_widget->p_dialog->lock );
816
817     return 1;
818 }
819
820
821 static int vlclua_widget_set_checked( lua_State *L )
822 {
823     /* Get dialog */
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;
829
830     if( p_widget->type != EXTENSION_WIDGET_CHECK_BOX )
831         return luaL_error( L, "method set_checked not valid for this widget" );
832
833     /* Verify arguments */
834     if( !lua_isboolean( L, 2 ) )
835         return luaL_error( L, "widget:set_checked usage: (bool)" );
836
837     vlc_mutex_lock( &p_widget->p_dialog->lock );
838
839     bool b_old_check = p_widget->b_checked;
840     p_widget->b_checked = lua_toboolean( L, 2 );
841
842     vlc_mutex_unlock( &p_widget->p_dialog->lock );
843
844     if( b_old_check != p_widget->b_checked )
845     {
846         /* Signal interface of the change */
847         p_widget->b_update = true;
848         lua_SetDialogUpdate( L, 1 );
849     }
850
851     return 1;
852 }
853
854 /**
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
858  **/
859 static int vlclua_dialog_delete_widget( lua_State *L )
860 {
861     /* Get dialog */
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;
867
868     /* Get widget */
869     if( !lua_isuserdata( L, 2 ) )
870         return luaL_error( L, "Argument to del_widget is not a widget" );
871
872     /* Get dialog */
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;
878
879     /* Delete widget */
880     *pp_widget = NULL;
881
882     vlc_object_t *p_mgr = vlclua_get_this( L );
883
884     p_widget->b_kill = true;
885
886     lua_SetDialogUpdate( L, 0 ); // Reset update flag
887     int i_ret = dialog_ExtensionUpdate( p_mgr, p_dlg );
888
889     if( i_ret != VLC_SUCCESS )
890     {
891         return luaL_error( L, "Could not delete widget" );
892     }
893
894     vlc_mutex_lock( &p_dlg->lock );
895
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 )
902     {
903         vlc_cond_wait( &p_dlg->cond, &p_dlg->lock );
904     }
905
906     i_ret = DeleteWidget( p_dlg, p_widget );
907
908     vlc_mutex_unlock( &p_dlg->lock );
909
910     if( i_ret != VLC_SUCCESS )
911     {
912         return luaL_error( L, "Could not remove widget from list" );
913     }
914
915     return 1;
916 }
917
918
919 /*
920  * Below this line, no Lua specific code.
921  * Extension helpers.
922  */
923
924
925 /**
926  * Add a widget to the widget list of a dialog
927  * @note Must be entered with lock on dialog
928  **/
929 static void AddWidget( extension_dialog_t *p_dialog,
930                        extension_widget_t *p_widget )
931 {
932     ARRAY_APPEND( p_dialog->widgets, p_widget );
933 }
934
935 /**
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
939  **/
940 static int DeleteWidget( extension_dialog_t *p_dialog,
941                          extension_widget_t *p_widget )
942 {
943     int pos = -1;
944     bool found = false;
945     extension_widget_t *p_iter;
946     FOREACH_ARRAY( p_iter, p_dialog->widgets )
947     {
948         pos++;
949         if( p_iter == p_widget )
950         {
951             found = true;
952             break;
953         }
954     }
955     FOREACH_END()
956
957     if( !found )
958         return VLC_EGENERIC;
959
960     ARRAY_REMOVE( p_dialog->widgets, pos );
961
962     /* Now free the data */
963     free( p_widget->p_sys );
964     struct extension_widget_value_t *p_value = p_widget->p_values;
965     while( p_value )
966     {
967         free( p_value->psz_text );
968         struct extension_widget_value_t *old = p_value;
969         p_value = p_value->p_next;
970         free( old );
971     }
972     free( p_widget->psz_text );
973     free( p_widget );
974
975     return VLC_SUCCESS;
976 }