]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/dialog.c
Kill warnings about unused variables. (jpeg?)
[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* )
369             lua_topointer( L, lua_gettop( L ) );
370
371     if( !p_dlg )
372         return VLC_SUCCESS;
373
374     int i_ret = VLC_SUCCESS;
375     if( lua_GetDialogUpdate( L ) )
376     {
377         i_ret = dialog_ExtensionUpdate( vlclua_get_this( L ),
378                                         p_dlg );
379         lua_SetDialogUpdate( L, 0 );
380     }
381
382     return i_ret;
383 }
384
385 /**
386  * Create a button: add_button
387  * Arguments: text, function (as string)
388  * Qt: QPushButton
389  **/
390 static int vlclua_dialog_add_button( lua_State *L )
391 {
392     /* Verify arguments */
393     if( !lua_isstring( L, 2 ) || !lua_isstring( L, 3 ) )
394         return luaL_error( L, "dialog:add_button usage: (text, func)" );
395
396     extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
397     p_widget->type = EXTENSION_WIDGET_BUTTON;
398     p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
399     p_widget->p_sys = strdup( luaL_checkstring( L, 3 ) );
400
401     return vlclua_create_widget_inner( L, 2, p_widget );
402 }
403
404 /**
405  * Create a text label: add_label
406  * Arguments: text
407  * Qt: QLabel
408  **/
409 static int vlclua_dialog_add_label( lua_State *L )
410 {
411     /* Verify arguments */
412     if( !lua_isstring( L, 2 ) )
413         return luaL_error( L, "dialog:add_label usage: (text)" );
414     extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
415     p_widget->type = EXTENSION_WIDGET_LABEL;
416     p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
417
418     return vlclua_create_widget_inner( L, 1, p_widget );
419 }
420
421 /**
422  * Create a text area: add_html, add_text_input, add_password
423  * Arguments: text (may be nil)
424  * Qt: QLineEdit (Text/Password) or QTextArea (HTML)
425  **/
426 static int vlclua_dialog_add_text_inner( lua_State *L, int i_type )
427 {
428     /* Verify arguments */
429     if( !lua_isstring( L, 2 ) && !lua_isnil( L, 2 ) )
430         return luaL_error( L, "dialog:add_text_input usage: (text = nil)" );
431
432     extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
433     p_widget->type = i_type;
434     if( !lua_isnil( L, 2 ) )
435         p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
436
437     return vlclua_create_widget_inner( L, 1, p_widget );
438 }
439
440 /**
441  * Create a checkable box: add_check_box
442  * Arguments: text, checked (as bool)
443  * Qt: QCheckBox
444  **/
445 static int vlclua_dialog_add_check_box( lua_State *L )
446 {
447     /* Verify arguments */
448     if( !lua_isstring( L, 2 ) )
449         return luaL_error( L, "dialog:add_check_box usage: (text, checked)" );
450
451     extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
452     p_widget->type = EXTENSION_WIDGET_CHECK_BOX;
453     p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
454     p_widget->b_checked = lua_toboolean( L, 3 );
455
456     return vlclua_create_widget_inner( L, 2, p_widget );
457 }
458
459 /**
460  * Create a drop-down list (non editable)
461  * Arguments: (none)
462  * Qt: QComboBox
463  * @todo make it editable?
464  **/
465 static int vlclua_dialog_add_dropdown( lua_State *L )
466 {
467     extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
468     p_widget->type = EXTENSION_WIDGET_DROPDOWN;
469
470     return vlclua_create_widget_inner( L, 0, p_widget );
471 }
472
473 /**
474  * Create a list panel (multiple selection)
475  * Arguments: (none)
476  * Qt: QListWidget
477  **/
478 static int vlclua_dialog_add_list( lua_State *L )
479 {
480     extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
481     p_widget->type = EXTENSION_WIDGET_LIST;
482
483     return vlclua_create_widget_inner( L, 0, p_widget );
484 }
485
486 /**
487  * Create an image label
488  * Arguments: url
489  * Qt: QLabel with setPixmap( QPixmap& )
490  **/
491 static int vlclua_dialog_add_image( lua_State *L )
492 {
493     /* Verify arguments */
494     if( !lua_isstring( L, 2 ) )
495         return luaL_error( L, "dialog:add_image usage: (filename)" );
496
497     extension_widget_t *p_widget = calloc( 1, sizeof( extension_widget_t ) );
498     p_widget->type = EXTENSION_WIDGET_IMAGE;
499     p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
500
501     return vlclua_create_widget_inner( L, 1, p_widget );
502 }
503
504 /**
505  * Internal helper to finalize the creation of a widget
506  * @param L Lua State
507  * @param i_args Number of arguments before "row" (0 or more)
508  * @param p_widget The widget to add
509  **/
510 static int vlclua_create_widget_inner( lua_State *L, int i_args,
511                                        extension_widget_t *p_widget )
512 {
513     int arg = i_args + 2;
514
515     /* Get dialog */
516     extension_dialog_t **pp_dlg =
517             (extension_dialog_t**) luaL_checkudata( L, 1, "dialog" );
518     if( !pp_dlg || !*pp_dlg )
519         return luaL_error( L, "Can't get pointer to dialog" );
520     extension_dialog_t *p_dlg = *pp_dlg;
521
522     /* Set parent dialog */
523     p_widget->p_dialog = p_dlg;
524
525     /* Set common arguments: col, row, hspan, vspan, width, height */
526     if( lua_isnumber( L, arg ) )
527         p_widget->i_column = luaL_checkinteger( L, arg );
528     else goto end_of_args;
529     if( lua_isnumber( L, ++arg ) )
530         p_widget->i_row = luaL_checkinteger( L, arg );
531     else goto end_of_args;
532     if( lua_isnumber( L, ++arg ) )
533         p_widget->i_horiz_span = luaL_checkinteger( L, arg );
534     else goto end_of_args;
535     if( lua_isnumber( L, ++arg ) )
536         p_widget->i_vert_span = luaL_checkinteger( L, arg );
537     else goto end_of_args;
538     if( lua_isnumber( L, ++arg ) )
539         p_widget->i_width = luaL_checkinteger( L, arg );
540     else goto end_of_args;
541     if( lua_isnumber( L, ++arg ) )
542         p_widget->i_height = luaL_checkinteger( L, arg );
543     else goto end_of_args;
544
545 end_of_args:
546     vlc_mutex_lock( &p_dlg->lock );
547
548     /* Add the widget to the dialog descriptor */
549     AddWidget( p_dlg, p_widget );
550
551     vlc_mutex_unlock( &p_dlg->lock );
552
553     /* Create meta table */
554     extension_widget_t **pp_widget = lua_newuserdata( L, sizeof( void* ) );
555     *pp_widget = p_widget;
556     if( luaL_newmetatable( L, "widget" ) )
557     {
558         lua_newtable( L );
559         luaL_register( L, NULL, vlclua_widget_reg );
560         lua_setfield( L, -2, "__index" );
561     }
562     lua_setmetatable( L, -2 );
563
564     lua_SetDialogUpdate( L, 1 );
565
566     return 1;
567 }
568
569 static int vlclua_widget_set_text( lua_State *L )
570 {
571     /* Get dialog */
572     extension_widget_t **pp_widget =
573             (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
574     if( !pp_widget || !*pp_widget )
575         return luaL_error( L, "Can't get pointer to widget" );
576     extension_widget_t *p_widget = *pp_widget;
577
578     /* Verify arguments */
579     if( !lua_isstring( L, 2 ) )
580         return luaL_error( L, "widget:set_text usage: (text)" );
581
582     /* Verify widget type */
583     switch( p_widget->type )
584     {
585         case EXTENSION_WIDGET_LABEL:
586         case EXTENSION_WIDGET_BUTTON:
587         case EXTENSION_WIDGET_HTML:
588         case EXTENSION_WIDGET_TEXT_FIELD:
589         case EXTENSION_WIDGET_PASSWORD:
590         case EXTENSION_WIDGET_DROPDOWN:
591         case EXTENSION_WIDGET_CHECK_BOX:
592             break;
593         case EXTENSION_WIDGET_LIST:
594         case EXTENSION_WIDGET_IMAGE:
595         default:
596             return luaL_error( L, "method set_text not valid for this widget" );
597     }
598
599     vlc_mutex_lock( &p_widget->p_dialog->lock );
600
601     /* Update widget */
602     p_widget->b_update = true;
603     free( p_widget->psz_text );
604     p_widget->psz_text = strdup( luaL_checkstring( L, 2 ) );
605
606     vlc_mutex_unlock( &p_widget->p_dialog->lock );
607
608     lua_SetDialogUpdate( L, 1 );
609
610     return 1;
611 }
612
613 static int vlclua_widget_get_text( lua_State *L )
614 {
615     /* Get dialog */
616     extension_widget_t **pp_widget =
617             (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
618     if( !pp_widget || !*pp_widget )
619         return luaL_error( L, "Can't get pointer to widget" );
620     extension_widget_t *p_widget = *pp_widget;
621
622     /* Verify widget type */
623     switch( p_widget->type )
624     {
625         case EXTENSION_WIDGET_LABEL:
626         case EXTENSION_WIDGET_BUTTON:
627         case EXTENSION_WIDGET_HTML:
628         case EXTENSION_WIDGET_TEXT_FIELD:
629         case EXTENSION_WIDGET_PASSWORD:
630         case EXTENSION_WIDGET_DROPDOWN:
631         case EXTENSION_WIDGET_CHECK_BOX:
632             break;
633         case EXTENSION_WIDGET_LIST:
634         case EXTENSION_WIDGET_IMAGE:
635         default:
636             return luaL_error( L, "method get_text not valid for this widget" );
637     }
638
639     extension_dialog_t *p_dlg = p_widget->p_dialog;
640     vlc_mutex_lock( &p_dlg->lock );
641
642     char *psz_text = NULL;
643     if( p_widget->psz_text )
644         psz_text = strdup( p_widget->psz_text );
645     vlc_mutex_unlock( &p_dlg->lock );
646
647     lua_pushstring( L, psz_text );
648
649     free( psz_text );
650     return 1;
651 }
652
653 static int vlclua_widget_get_checked( lua_State *L )
654 {
655     /* Get widget */
656     extension_widget_t **pp_widget =
657             (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
658     if( !pp_widget || !*pp_widget )
659         return luaL_error( L, "Can't get pointer to widget" );
660     extension_widget_t *p_widget = *pp_widget;
661
662     if( p_widget->type != EXTENSION_WIDGET_CHECK_BOX )
663         return luaL_error( L, "method get_checked not valid for this widget" );
664
665     vlc_mutex_lock( &p_widget->p_dialog->lock );
666     lua_pushboolean( L, p_widget->b_checked );
667     vlc_mutex_unlock( &p_widget->p_dialog->lock );
668
669     return 1;
670 }
671
672 static int vlclua_widget_add_value( lua_State *L )
673 {
674     /* Get widget */
675     extension_widget_t **pp_widget =
676             (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
677     if( !pp_widget || !*pp_widget )
678         return luaL_error( L, "Can't get pointer to widget" );
679     extension_widget_t *p_widget = *pp_widget;
680
681     if( p_widget->type != EXTENSION_WIDGET_DROPDOWN
682         && p_widget->type != EXTENSION_WIDGET_LIST )
683         return luaL_error( L, "method add_value not valid for this widget" );
684
685     if( !lua_isstring( L, 2 ) )
686         return luaL_error( L, "widget:add_value usage: (text, id = 0)" );
687
688     struct extension_widget_value_t *p_value,
689         *p_new_value = calloc( 1, sizeof( struct extension_widget_value_t ) );
690     p_new_value->psz_text = strdup( luaL_checkstring( L, 2 ) );
691     p_new_value->i_id = lua_tointeger( L, 3 );
692
693     vlc_mutex_lock( &p_widget->p_dialog->lock );
694
695     if( !p_widget->p_values )
696     {
697         p_widget->p_values = p_new_value;
698     }
699     else
700     {
701         for( p_value = p_widget->p_values;
702              p_value->p_next != NULL;
703              p_value = p_value->p_next )
704         { /* Do nothing, iterate to find the end */ }
705         p_value->p_next = p_new_value;
706     }
707
708     p_widget->b_update = true;
709     vlc_mutex_unlock( &p_widget->p_dialog->lock );
710
711     lua_SetDialogUpdate( L, 1 );
712
713     return 1;
714 }
715
716 static int vlclua_widget_get_value( lua_State *L )
717 {
718     /* Get widget */
719     extension_widget_t **pp_widget =
720             (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
721     if( !pp_widget || !*pp_widget )
722         return luaL_error( L, "Can't get pointer to widget" );
723     extension_widget_t *p_widget = *pp_widget;
724
725     if( p_widget->type != EXTENSION_WIDGET_DROPDOWN )
726         return luaL_error( L, "method get_value not valid for this widget" );
727
728     vlc_mutex_lock( &p_widget->p_dialog->lock );
729
730     struct extension_widget_value_t *p_value;
731     for( p_value = p_widget->p_values;
732          p_value != NULL;
733          p_value = p_value->p_next )
734     {
735         if( p_value->b_selected )
736         {
737             lua_pushinteger( L, p_value->i_id );
738             lua_pushstring( L, p_value->psz_text );
739             vlc_mutex_unlock( &p_widget->p_dialog->lock );
740             return 2;
741         }
742     }
743
744     vlc_mutex_unlock( &p_widget->p_dialog->lock );
745
746     lua_pushinteger( L, -1 );
747     lua_pushnil( L );
748     return 2;
749 }
750
751 static int vlclua_widget_clear( lua_State *L )
752 {
753     /* Get widget */
754     extension_widget_t **pp_widget =
755             (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
756     if( !pp_widget || !*pp_widget )
757         return luaL_error( L, "Can't get pointer to widget" );
758     extension_widget_t *p_widget = *pp_widget;
759
760     if( p_widget->type != EXTENSION_WIDGET_DROPDOWN
761         && p_widget->type != EXTENSION_WIDGET_LIST )
762         return luaL_error( L, "method add_value not valid for this widget" );
763
764     struct extension_widget_value_t *p_value, *p_next;
765
766     vlc_mutex_lock( &p_widget->p_dialog->lock );
767
768     for( p_value = p_widget->p_values;
769          p_value != NULL;
770          p_value = p_next )
771     {
772         p_next = p_value->p_next;
773         free( p_value->psz_text );
774         free( p_value );
775     }
776
777     p_widget->p_values = NULL;
778     p_widget->b_update = true;
779
780     vlc_mutex_unlock( &p_widget->p_dialog->lock );
781
782     lua_SetDialogUpdate( L, 1 );
783
784     return 1;
785 }
786
787 static int vlclua_widget_get_selection( lua_State *L )
788 {
789     /* Get widget */
790     extension_widget_t **pp_widget =
791             (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
792     if( !pp_widget || !*pp_widget )
793         return luaL_error( L, "Can't get pointer to widget" );
794     extension_widget_t *p_widget = *pp_widget;
795
796     if( p_widget->type != EXTENSION_WIDGET_LIST )
797         return luaL_error( L, "method get_selection not valid for this widget" );
798
799     /* Create empty table */
800     lua_newtable( L );
801
802     vlc_mutex_lock( &p_widget->p_dialog->lock );
803
804     struct extension_widget_value_t *p_value;
805     for( p_value = p_widget->p_values;
806          p_value != NULL;
807          p_value = p_value->p_next )
808     {
809         if( p_value->b_selected )
810         {
811             lua_pushinteger( L, p_value->i_id );
812             lua_pushstring( L, p_value->psz_text );
813             lua_settable( L, -3 );
814         }
815     }
816
817     vlc_mutex_unlock( &p_widget->p_dialog->lock );
818
819     return 1;
820 }
821
822
823 static int vlclua_widget_set_checked( lua_State *L )
824 {
825     /* Get dialog */
826     extension_widget_t **pp_widget =
827             (extension_widget_t **) luaL_checkudata( L, 1, "widget" );
828     if( !pp_widget || !*pp_widget )
829         return luaL_error( L, "Can't get pointer to widget" );
830     extension_widget_t *p_widget = *pp_widget;
831
832     if( p_widget->type != EXTENSION_WIDGET_CHECK_BOX )
833         return luaL_error( L, "method set_checked not valid for this widget" );
834
835     /* Verify arguments */
836     if( !lua_isboolean( L, 2 ) )
837         return luaL_error( L, "widget:set_checked usage: (bool)" );
838
839     vlc_mutex_lock( &p_widget->p_dialog->lock );
840
841     bool b_old_check = p_widget->b_checked;
842     p_widget->b_checked = lua_toboolean( L, 2 );
843
844     vlc_mutex_unlock( &p_widget->p_dialog->lock );
845
846     if( b_old_check != p_widget->b_checked )
847     {
848         /* Signal interface of the change */
849         p_widget->b_update = true;
850         lua_SetDialogUpdate( L, 1 );
851     }
852
853     return 1;
854 }
855
856 /**
857  * Delete a widget from a dialog
858  * Remove it from the list once it has been safely destroyed by the interface
859  * @note This will always flush the dialog
860  **/
861 static int vlclua_dialog_delete_widget( lua_State *L )
862 {
863     /* Get dialog */
864     extension_dialog_t **pp_dlg =
865             (extension_dialog_t**) luaL_checkudata( L, 1, "dialog" );
866     if( !pp_dlg || !*pp_dlg )
867         return luaL_error( L, "Can't get pointer to dialog" );
868     extension_dialog_t *p_dlg = *pp_dlg;
869
870     /* Get widget */
871     if( !lua_isuserdata( L, 2 ) )
872         return luaL_error( L, "Argument to del_widget is not a widget" );
873
874     /* Get dialog */
875     extension_widget_t **pp_widget =
876             (extension_widget_t **) luaL_checkudata( L, 2, "widget" );
877     if( !pp_widget || !*pp_widget )
878         return luaL_error( L, "Can't get pointer to widget" );
879     extension_widget_t *p_widget = *pp_widget;
880
881     /* Delete widget */
882     *pp_widget = NULL;
883
884     vlc_object_t *p_mgr = vlclua_get_this( L );
885
886     p_widget->b_kill = true;
887
888     lua_SetDialogUpdate( L, 0 ); // Reset update flag
889     int i_ret = dialog_ExtensionUpdate( p_mgr, p_dlg );
890
891     if( i_ret != VLC_SUCCESS )
892     {
893         return luaL_error( L, "Could not delete widget" );
894     }
895
896     vlc_mutex_lock( &p_dlg->lock );
897
898     /* Same remarks as for dialog delete.
899      * If the dialog is deleted or about to be deleted, then there is no
900      * need to wait on this particular widget that already doesn't exist
901      * anymore in the UI */
902     while( p_widget->p_sys_intf != NULL && !p_dlg->b_kill
903            && p_dlg->p_sys_intf != NULL )
904     {
905         vlc_cond_wait( &p_dlg->cond, &p_dlg->lock );
906     }
907
908     i_ret = DeleteWidget( p_dlg, p_widget );
909
910     vlc_mutex_unlock( &p_dlg->lock );
911
912     if( i_ret != VLC_SUCCESS )
913     {
914         return luaL_error( L, "Could not remove widget from list" );
915     }
916
917     return 1;
918 }
919
920
921 /*
922  * Below this line, no Lua specific code.
923  * Extension helpers.
924  */
925
926
927 /**
928  * Add a widget to the widget list of a dialog
929  * @note Must be entered with lock on dialog
930  **/
931 static void AddWidget( extension_dialog_t *p_dialog,
932                        extension_widget_t *p_widget )
933 {
934     ARRAY_APPEND( p_dialog->widgets, p_widget );
935 }
936
937 /**
938  * Remove a widget from the widget list of a dialog
939  * @note The widget MUST have been safely killed before
940  * @note Must be entered with lock on dialog
941  **/
942 static int DeleteWidget( extension_dialog_t *p_dialog,
943                          extension_widget_t *p_widget )
944 {
945     int pos = -1;
946     bool found = false;
947     extension_widget_t *p_iter;
948     FOREACH_ARRAY( p_iter, p_dialog->widgets )
949     {
950         pos++;
951         if( p_iter == p_widget )
952         {
953             found = true;
954             break;
955         }
956     }
957     FOREACH_END()
958
959     if( !found )
960         return VLC_EGENERIC;
961
962     ARRAY_REMOVE( p_dialog->widgets, pos );
963
964     /* Now free the data */
965     free( p_widget->p_sys );
966     struct extension_widget_value_t *p_value = p_widget->p_values;
967     while( p_value )
968     {
969         free( p_value->psz_text );
970         struct extension_widget_value_t *old = p_value;
971         p_value = p_value->p_next;
972         free( old );
973     }
974     free( p_widget->psz_text );
975     free( p_widget );
976
977     return VLC_SUCCESS;
978 }