Fix build of melted.
[melted] / src / melted / melted_commands.c
1 /*
2 * melted_commands.c - global commands
3 * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/poll.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <dirent.h>
30 #include <pthread.h>
31
32 #include "melted_unit.h"
33 #include "melted_commands.h"
34 #include "melted_log.h"
35
36 static melted_unit g_units[MAX_UNITS];
37
38
39 /** Return the melted_unit given a numeric index.
40 */
41
42 melted_unit melted_get_unit( int n )
43 {
44 if (n < MAX_UNITS)
45 return g_units[n];
46 else
47 return NULL;
48 }
49
50 /** Destroy the melted_unit given its numeric index.
51 */
52
53 void melted_delete_unit( int n )
54 {
55 if (n < MAX_UNITS)
56 {
57 melted_unit unit = melted_get_unit(n);
58 if (unit != NULL)
59 {
60 melted_unit_close( unit );
61 g_units[ n ] = NULL;
62 melted_log( LOG_NOTICE, "Deleted unit U%d.", n );
63 }
64 }
65 }
66
67 /** Destroy all allocated units on the server.
68 */
69
70 void melted_delete_all_units( void )
71 {
72 int i;
73 for (i = 0; i < MAX_UNITS; i++)
74 {
75 if ( melted_get_unit(i) != NULL )
76 {
77 melted_unit_close( melted_get_unit(i) );
78 melted_log( LOG_NOTICE, "Deleted unit U%d.", i );
79 }
80 }
81 }
82
83 /** Add a virtual vtr to the server.
84 */
85 response_codes melted_add_unit( command_argument cmd_arg )
86 {
87 int i = 0;
88 for ( i = 0; i < MAX_UNITS; i ++ )
89 if ( g_units[ i ] == NULL )
90 break;
91
92 if ( i < MAX_UNITS )
93 {
94 char *arg = cmd_arg->argument;
95 g_units[ i ] = melted_unit_init( i, arg );
96 if ( g_units[ i ] != NULL )
97 {
98 melted_unit_set_notifier( g_units[ i ], mvcp_parser_get_notifier( cmd_arg->parser ), cmd_arg->root_dir );
99 mvcp_response_printf( cmd_arg->response, 10, "U%1d\n\n", i );
100 }
101 return g_units[ i ] != NULL ? RESPONSE_SUCCESS_N : RESPONSE_ERROR;
102 }
103 mvcp_response_printf( cmd_arg->response, 1024, "no more units can be created\n\n" );
104
105 return RESPONSE_ERROR;
106 }
107
108
109 /** List all AV/C nodes on the bus.
110 */
111 response_codes melted_list_nodes( command_argument cmd_arg )
112 {
113 response_codes error = RESPONSE_SUCCESS_N;
114 return error;
115 }
116
117
118 /** List units already added to server.
119 */
120 response_codes melted_list_units( command_argument cmd_arg )
121 {
122 response_codes error = RESPONSE_SUCCESS_N;
123 int i = 0;
124
125 for ( i = 0; i < MAX_UNITS; i ++ )
126 {
127 melted_unit unit = melted_get_unit( i );
128 if ( unit != NULL )
129 {
130 mlt_properties properties = unit->properties;
131 char *constructor = mlt_properties_get( properties, "constructor" );
132 int node = mlt_properties_get_int( properties, "node" );
133 int online = !mlt_properties_get_int( properties, "offline" );
134 mvcp_response_printf( cmd_arg->response, 1024, "U%d %02d %s %d\n", i, node, constructor, online );
135 }
136 }
137 mvcp_response_printf( cmd_arg->response, 1024, "\n" );
138
139 return error;
140 }
141
142 static int filter_files( const struct dirent *de )
143 {
144 return de->d_name[ 0 ] != '.';
145 }
146
147 /** List clips in a directory.
148 */
149 response_codes melted_list_clips( command_argument cmd_arg )
150 {
151 response_codes error = RESPONSE_BAD_FILE;
152 const char *dir_name = (const char*) cmd_arg->argument;
153 DIR *dir;
154 char fullname[1024];
155 struct dirent **de = NULL;
156 int i, n;
157
158 snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, dir_name );
159 dir = opendir( fullname );
160 if (dir != NULL)
161 {
162 struct stat info;
163 error = RESPONSE_SUCCESS_N;
164 n = scandir( fullname, &de, filter_files, alphasort );
165 for (i = 0; i < n; i++ )
166 {
167 snprintf( fullname, 1023, "%s%s/%s", cmd_arg->root_dir, dir_name, de[i]->d_name );
168 if ( stat( fullname, &info ) == 0 && S_ISDIR( info.st_mode ) )
169 mvcp_response_printf( cmd_arg->response, 1024, "\"%s/\"\n", de[i]->d_name );
170 }
171 for (i = 0; i < n; i++ )
172 {
173 snprintf( fullname, 1023, "%s%s/%s", cmd_arg->root_dir, dir_name, de[i]->d_name );
174 if ( lstat( fullname, &info ) == 0 &&
175 ( S_ISREG( info.st_mode ) || S_ISLNK( info.st_mode ) || ( strstr( fullname, ".clip" ) && info.st_mode | S_IXUSR ) ) )
176 mvcp_response_printf( cmd_arg->response, 1024, "\"%s\" %llu\n", de[i]->d_name, (unsigned long long) info.st_size );
177 free( de[ i ] );
178 }
179 free( de );
180 closedir( dir );
181 mvcp_response_write( cmd_arg->response, "\n", 1 );
182 }
183
184 return error;
185 }
186
187 /** Set a server configuration property.
188 */
189
190 response_codes melted_set_global_property( command_argument cmd_arg )
191 {
192 char *key = (char*) cmd_arg->argument;
193 char *value = NULL;
194
195 value = strchr( key, '=' );
196 if (value == NULL)
197 return RESPONSE_OUT_OF_RANGE;
198 *value = 0;
199 value++;
200 melted_log( LOG_DEBUG, "SET %s = %s", key, value );
201
202 if ( strncasecmp( key, "root", 1024) == 0 )
203 {
204 int len = strlen(value);
205 int i;
206
207 /* stop all units and unload clips */
208 for (i = 0; i < MAX_UNITS; i++)
209 {
210 if (g_units[i] != NULL)
211 melted_unit_terminate( g_units[i] );
212 }
213
214 /* set the property */
215 strncpy( cmd_arg->root_dir, value, 1023 );
216
217 /* add a trailing slash if needed */
218 if ( len && cmd_arg->root_dir[ len - 1 ] != '/')
219 {
220 cmd_arg->root_dir[ len ] = '/';
221 cmd_arg->root_dir[ len + 1 ] = '\0';
222 }
223 }
224 else
225 return RESPONSE_OUT_OF_RANGE;
226
227 return RESPONSE_SUCCESS;
228 }
229
230 /** Get a server configuration property.
231 */
232
233 response_codes melted_get_global_property( command_argument cmd_arg )
234 {
235 char *key = (char*) cmd_arg->argument;
236
237 if ( strncasecmp( key, "root", 1024) == 0 )
238 {
239 mvcp_response_write( cmd_arg->response, cmd_arg->root_dir, strlen(cmd_arg->root_dir) );
240 return RESPONSE_SUCCESS_1;
241 }
242 else
243 return RESPONSE_OUT_OF_RANGE;
244
245 return RESPONSE_SUCCESS;
246 }
247
248