3a5db91b81a2087a685850e2c2239676d8b87040
[melted] / src / miracle / miracle_commands.c
1 /*
2 * global_commands.c
3 * Copyright (C) 2002-2003 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 "miracle_unit.h"
33 #include "miracle_commands.h"
34 #include "miracle_log.h"
35
36 static miracle_unit g_units[MAX_UNITS];
37
38
39 /** Return the miracle_unit given a numeric index.
40 */
41
42 miracle_unit miracle_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 miracle_unit given its numeric index.
51 */
52
53 void miracle_delete_unit( int n )
54 {
55 if (n < MAX_UNITS)
56 {
57 miracle_unit unit = miracle_get_unit(n);
58 if (unit != NULL)
59 {
60 miracle_unit_close( unit );
61 g_units[ n ] = NULL;
62 miracle_log( LOG_NOTICE, "Deleted unit U%d.", n );
63 }
64 }
65 }
66
67 /** Destroy all allocated units on the server.
68 */
69
70 void miracle_delete_all_units( void )
71 {
72 int i;
73 for (i = 0; i < MAX_UNITS; i++)
74 {
75 if ( miracle_get_unit(i) != NULL )
76 {
77 miracle_unit_close( miracle_get_unit(i) );
78 miracle_log( LOG_NOTICE, "Deleted unit U%d.", i );
79 }
80 }
81 }
82
83 /** Add a DV virtual vtr to the server.
84 */
85 response_codes miracle_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 ] = miracle_unit_init( i, arg );
96 if ( g_units[ i ] != NULL )
97 miracle_unit_set_notifier( g_units[ i ], valerie_parser_get_notifier( cmd_arg->parser ), cmd_arg->root_dir );
98 return g_units[ i ] != NULL ? RESPONSE_SUCCESS : RESPONSE_ERROR;
99 }
100
101 return RESPONSE_ERROR;
102 }
103
104
105 /** List all AV/C nodes on the bus.
106 */
107 response_codes miracle_list_nodes( command_argument cmd_arg )
108 {
109 response_codes error = RESPONSE_SUCCESS_N;
110 return error;
111 }
112
113
114 /** List units already added to server.
115 */
116 response_codes miracle_list_units( command_argument cmd_arg )
117 {
118 response_codes error = RESPONSE_SUCCESS_N;
119 int i = 0;
120
121 for ( i = 0; i < MAX_UNITS; i ++ )
122 {
123 miracle_unit unit = miracle_get_unit( i );
124 if ( unit != NULL )
125 {
126 mlt_properties properties = unit->properties;
127 char *constructor = mlt_properties_get( properties, "constructor" );
128 int node = mlt_properties_get_int( properties, "node" );
129 int online = !mlt_properties_get_int( properties, "offline" );
130 valerie_response_printf( cmd_arg->response, 1024, "U%d %02d %s %d\n", i, node, constructor, online );
131 }
132 }
133
134 return error;
135 }
136
137 static int filter_files( const struct dirent *de )
138 {
139 return de->d_name[ 0 ] != '.';
140 }
141
142 /** List clips in a directory.
143 */
144 response_codes miracle_list_clips( command_argument cmd_arg )
145 {
146 response_codes error = RESPONSE_BAD_FILE;
147 const char *dir_name = (const char*) cmd_arg->argument;
148 DIR *dir;
149 char fullname[1024];
150 struct dirent **de = NULL;
151 int i, n;
152
153 snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, dir_name );
154 dir = opendir( fullname );
155 if (dir != NULL)
156 {
157 struct stat info;
158 error = RESPONSE_SUCCESS_N;
159 n = scandir( fullname, &de, filter_files, alphasort );
160 for (i = 0; i < n; i++ )
161 {
162 snprintf( fullname, 1023, "%s%s/%s", cmd_arg->root_dir, dir_name, de[i]->d_name );
163 if ( stat( fullname, &info ) == 0 && S_ISDIR( info.st_mode ) )
164 valerie_response_printf( cmd_arg->response, 1024, "\"%s/\"\n", de[i]->d_name );
165 }
166 for (i = 0; i < n; i++ )
167 {
168 snprintf( fullname, 1023, "%s%s/%s", cmd_arg->root_dir, dir_name, de[i]->d_name );
169 if ( lstat( fullname, &info ) == 0 &&
170 ( S_ISREG( info.st_mode ) || S_ISLNK( info.st_mode ) || ( strstr( fullname, ".clip" ) && info.st_mode | S_IXUSR ) ) )
171 valerie_response_printf( cmd_arg->response, 1024, "\"%s\" %llu\n", de[i]->d_name, (unsigned long long) info.st_size );
172 free( de[ i ] );
173 }
174 free( de );
175 closedir( dir );
176 valerie_response_write( cmd_arg->response, "\n", 1 );
177 }
178
179 return error;
180 }
181
182 /** Set a server configuration property.
183 */
184
185 response_codes miracle_set_global_property( command_argument cmd_arg )
186 {
187 char *key = (char*) cmd_arg->argument;
188 char *value = NULL;
189
190 value = strchr( key, '=' );
191 if (value == NULL)
192 return RESPONSE_OUT_OF_RANGE;
193 *value = 0;
194 value++;
195 miracle_log( LOG_DEBUG, "SET %s = %s", key, value );
196
197 if ( strncasecmp( key, "root", 1024) == 0 )
198 {
199 int len = strlen(value);
200 int i;
201
202 /* stop all units and unload clips */
203 for (i = 0; i < MAX_UNITS; i++)
204 {
205 if (g_units[i] != NULL)
206 miracle_unit_terminate( g_units[i] );
207 }
208
209 /* set the property */
210 strncpy( cmd_arg->root_dir, value, 1023 );
211
212 /* add a trailing slash if needed */
213 if ( cmd_arg->root_dir[ len - 1 ] != '/')
214 {
215 cmd_arg->root_dir[ len ] = '/';
216 cmd_arg->root_dir[ len + 1 ] = '\0';
217 }
218 }
219 else
220 return RESPONSE_OUT_OF_RANGE;
221
222 return RESPONSE_SUCCESS;
223 }
224
225 /** Get a server configuration property.
226 */
227
228 response_codes miracle_get_global_property( command_argument cmd_arg )
229 {
230 char *key = (char*) cmd_arg->argument;
231
232 if ( strncasecmp( key, "root", 1024) == 0 )
233 {
234 valerie_response_write( cmd_arg->response, cmd_arg->root_dir, strlen(cmd_arg->root_dir) );
235 return RESPONSE_SUCCESS_1;
236 }
237 else
238 return RESPONSE_OUT_OF_RANGE;
239
240 return RESPONSE_SUCCESS;
241 }
242
243