]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/sd.c
205fe92e8691796f39faf2fc3d2a390e07003277
[vlc] / modules / misc / lua / libs / sd.c
1 /*****************************************************************************
2  * sd.c: Services discovery related functions
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod 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_services_discovery.h>
37 #include <vlc_playlist.h>
38
39 #include <lua.h>        /* Low level lua C API */
40 #include <lauxlib.h>    /* Higher level C API */
41 #include <lualib.h>     /* Lua libs */
42
43 #include "../vlc.h"
44 #include "../libs.h"
45 #include "playlist.h"
46
47 /*****************************************************************************
48  *
49  *****************************************************************************/
50 static int vlclua_sd_get_services_names( lua_State *L )
51 {
52     vlc_object_t *p_this = vlclua_get_this( L );
53     char **ppsz_longnames;
54     char **ppsz_names = services_discovery_GetServicesNames( p_this, &ppsz_longnames );
55     if( !ppsz_names )
56         return 0;
57
58     char **ppsz_longname = ppsz_longnames;
59     char **ppsz_name = ppsz_names;
60     lua_settop( L, 0 );
61     lua_newtable( L );
62     for( ; *ppsz_name; ppsz_name++,ppsz_longname++ )
63     {
64         lua_pushstring( L, *ppsz_longname );
65         lua_setfield( L, -2, *ppsz_name );
66         free( *ppsz_name );
67         free( *ppsz_longname );
68     }
69     free( ppsz_names );
70     free( ppsz_longnames );
71     return 1;
72 }
73
74 static int vlclua_sd_add( lua_State *L )
75 {
76     const char *psz_sd = luaL_checkstring( L, 1 );
77     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
78     int i_ret = playlist_ServicesDiscoveryAdd( p_playlist, psz_sd );
79     vlc_object_release( p_playlist );
80     return vlclua_push_ret( L, i_ret );
81 }
82
83 static int vlclua_sd_remove( lua_State *L )
84 {
85     const char *psz_sd = luaL_checkstring( L, 1 );
86     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
87     int i_ret = playlist_ServicesDiscoveryRemove( p_playlist, psz_sd );
88     vlc_object_release( p_playlist );
89     return vlclua_push_ret( L, i_ret );
90 }
91
92 static int vlclua_sd_is_loaded( lua_State *L )
93 {
94     const char *psz_sd = luaL_checkstring( L, 1 );
95     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
96     lua_pushboolean( L, playlist_IsServicesDiscoveryLoaded( p_playlist, psz_sd ));
97     vlc_object_release( p_playlist );
98     return 1;
99 }
100
101 /*****************************************************************************
102  *
103  *****************************************************************************/
104 static const luaL_Reg vlclua_sd_reg[] = {
105     { "get_services_names", vlclua_sd_get_services_names },
106     { "add", vlclua_sd_add },
107     { "remove", vlclua_sd_remove },
108     { "is_loaded", vlclua_sd_is_loaded },
109     { NULL, NULL }
110 };
111
112 void luaopen_sd( lua_State *L )
113 {
114     lua_newtable( L );
115     luaL_register( L, NULL, vlclua_sd_reg );
116     lua_setfield( L, -2, "sd" );
117 }