drop spaces from icons filenames
[rugen] / src / page_status.c
1 /*
2 * page_status.c -- Status Page Handling
3 * Copyright (C) 2002-2003 Charles Yates <charles.yates@pandora.be>
4 * Copyright (C) 2010 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 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <gtk/gtk.h>
29
30 #include "interface.h"
31 #include "support.h"
32 #include "dv1394app.h"
33 #include "page.h"
34
35 typedef struct
36 {
37 struct page_t parent;
38 dv1394app app;
39 GtkWidget *widget;
40 mvcp status;
41 int terminated;
42 pthread_t status_thread;
43 guint context;
44 int unit;
45 int count;
46 struct page_t *parent_page;
47 }
48 *page_status, page_status_t;
49
50 static GtkWidget *this_page_get_widget( page );
51
52 /** Show the status associated to a unit.
53 */
54
55 static char* status_to_text( mvcp_status status )
56 {
57 switch( status->status )
58 {
59 case unit_offline: return "offline"; break;
60 case unit_undefined: return "undefined"; break;
61 case unit_not_loaded: return "unloaded"; break;
62 case unit_stopped: return "stopped"; break;
63 case unit_playing: return "playing"; break;
64 case unit_paused: return "paused"; break;
65 case unit_disconnected: return "disconnect"; break;
66 case unit_unknown: return "unknown"; break;
67 }
68 return "unknown";
69 }
70
71 static void format_status_string(char* buf, int size, mvcp_status status , char* sep)
72 {
73 char tc1[12], tc2[12], tc3[12];
74
75 snprintf( buf, size,
76 "[%s]%sIN:%s%sPOS:%s%sOUT:%s%s%s%s",
77 status_to_text( status ), sep,
78 frames2tc(status->in, status->fps, tc1), sep,
79 frames2tc(status->position, status->fps, tc2), sep,
80 frames2tc(status->out, status->fps, tc3), sep,
81 status->clip, sep
82 );
83
84 }
85
86 static int show_status( page_status this, mvcp_status status )
87 {
88 GtkWidget *widget = this_page_get_widget( ( page )this );
89 char temp[ 1024 ] = "";
90 char temp2[ 1024 ];
91 char label_name[ 256 ];
92
93 format_status_string(temp, sizeof(temp), status , "\n");
94 sprintf( label_name, "label_unit_%d", status->unit );
95
96 gdk_threads_enter();
97 widget = lookup_widget( widget, label_name );
98 gtk_label_set_text( GTK_LABEL( widget ), temp );
99 if ( status->unit == dv1394app_get_selected_unit( this->app ) )
100 {
101 format_status_string(temp, sizeof(temp), status , " ");
102 sprintf( temp2, "U%d - %s", status->unit, temp );
103 widget = lookup_widget( dv1394app_get_widget( this->app ), "statusbar" );
104 gtk_statusbar_pop( GTK_STATUSBAR( widget ), this->context );
105 gtk_statusbar_push( GTK_STATUSBAR( widget ), this->context, temp2 );
106 dv1394app_show_status( this->app, status );
107 }
108 gdk_flush();
109 gdk_threads_leave();
110
111 return status->unit >= this->count;
112 }
113
114 static void show_units( page_status this, gboolean active )
115 {
116 int index = 0;
117 int index2 = 0;
118
119 GtkWidget *widget;
120 char temp[ 1024 ] = "";
121 char button_name[ 256 ];
122
123 mvcp_units units = NULL;
124 mvcp_unit_entry_t unit;
125 int unit_count = 0;
126
127 mvcp_nodes nodes = NULL;
128 mvcp_node_entry_t node;
129 int node_count = 0;
130
131 if ( active )
132 {
133 units = mvcp_units_init( this->status );
134 unit_count = mvcp_units_count( units );
135 nodes = mvcp_nodes_init( this->status );
136 node_count = mvcp_nodes_count( nodes );
137 this->count = unit_count;
138 }
139
140 gdk_threads_enter();
141
142 for ( index = 0; index < MAX_UNITS; index ++ )
143 {
144 if ( index < unit_count )
145 {
146 mvcp_units_get( units, index, &unit );
147
148 for ( index2 = 0; index2 < node_count; index2 ++ )
149 {
150 mvcp_nodes_get( nodes, index2, &node );
151 if ( !strcmp( node.guid, unit.guid ) )
152 break;
153 }
154
155 if ( index2 < node_count )
156 sprintf( temp, "U%d - %s (%s)", unit.unit, node.name, unit.guid );
157 else
158 sprintf( temp, "U%d - %s", unit.unit, unit.guid );
159
160 sprintf( button_name, "radiobutton_%d", index );
161 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
162 gtk_widget_show( widget );
163 gtk_button_set_label( GTK_BUTTON( widget ), temp );
164 sprintf( button_name, "label_unit_%d", index );
165 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
166 gtk_widget_show( widget );
167 }
168 else
169 {
170 sprintf( button_name, "radiobutton_%d", index );
171 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
172 gtk_widget_hide( widget );
173 sprintf( button_name, "label_unit_%d", index );
174 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
175 gtk_widget_hide( widget );
176 }
177 }
178 gdk_flush();
179 gdk_threads_leave();
180
181 mvcp_notifier notifier = mvcp_get_notifier( this->status );
182 mvcp_status_t status;
183
184 for ( index = 0; index < MAX_UNITS; index ++ )
185 {
186 status.unit = index;
187 if ( !active )
188 status.status = unit_disconnected;
189 else
190 mvcp_notifier_get( notifier, &status, index );
191 show_status( this, &status );
192 }
193
194 if ( active )
195 {
196 mvcp_nodes_close( nodes );
197 mvcp_units_close( units );
198 }
199 }
200
201 /** Status monitoring thread.
202 */
203
204 static void *status_thread( void *arg )
205 {
206 int i;
207 page_status this = arg;
208 mvcp_notifier notifier = mvcp_get_notifier( this->status );
209 mvcp_status_t status;
210 mvcp_status_t *statuses;
211
212 show_units( this, TRUE );
213
214 statuses = malloc(sizeof(mvcp_status_t) * this->count);
215 memset(statuses, 0, sizeof(mvcp_status_t) * this->count);
216
217 while ( !this->terminated )
218 {
219 char buf[32];
220
221 /* wait for any event from notifier */
222 mvcp_notifier_wait( notifier, &status );
223
224 /* get status for all units */
225 for(i = 0; i < this->count; i++)
226 {
227 mvcp_notifier_get( notifier, &status, i);
228
229 if ( status.status == unit_disconnected )
230 goto break_unit_disconnected;
231
232 if( memcmp(&status, &statuses[i], sizeof(mvcp_status_t)) || !this->app->eof[i])
233 {
234 /* save status */
235 statuses[i] = status;
236
237 /* save eof status */
238 if ( !this->app->eof[i] && mvcp_unit_get( this->app->command, i,
239 "eof", buf, sizeof(buf) ) != -1)
240 this->app->eof[i] = buf[0];
241
242 /* send status update */
243 if ( show_status( this, &status ) )
244 show_units( this, TRUE );
245 }
246 }
247 }
248
249 break_unit_disconnected:
250
251 free(statuses);
252
253 show_units( this, FALSE );
254
255 if ( !this->terminated )
256 {
257 GtkWidget *widget = lookup_widget( dv1394app_get_widget( this->app ), "button_disconnect" );
258 gdk_threads_enter();
259 gtk_signal_emit_by_name( GTK_OBJECT( widget ), "clicked" );
260 gdk_threads_leave();
261 }
262
263 return NULL;
264 }
265
266 void on_radiobutton_toggled( GtkToggleButton *togglebutton, gpointer user_data )
267 {
268 page_status this = user_data;
269 dv1394app app = this->app;
270 int index = 0;
271 GtkWidget *widget = this_page_get_widget( ( page )this );
272 for ( index = 0; index < MAX_UNITS; index ++ )
273 {
274 char button_name[ 256 ];
275 sprintf( button_name, "radiobutton_%d", index );
276 if ( lookup_widget( widget, button_name ) == GTK_WIDGET( togglebutton ) )
277 break;
278 }
279 if ( index < MAX_UNITS )
280 {
281 mvcp_status_t status;
282 mvcp_notifier notifier = mvcp_get_notifier( this->status );
283 dv1394app_on_unit_change( app, index );
284 mvcp_notifier_get( notifier, &status, index );
285 gdk_threads_leave( );
286 show_status( this, &status );
287 gdk_threads_enter( );
288 }
289 }
290
291 static GtkWidget *this_page_get_widget( page super )
292 {
293 page_status this = ( page_status )super;
294 if ( this->widget == NULL )
295 this->widget = this->parent_page->get_widget(this->parent_page);
296 return this->widget;
297 }
298
299 static void this_page_get_toolbar_info( page super, GtkIconSize size, GtkWidget **icon, char **label )
300 {
301 *icon = gtk_image_new_from_stock( "gtk-home", size );
302 *label = _("_Units");
303 }
304
305 static void this_page_on_connect( page super )
306 {
307 page_status this = ( page_status )super;
308 if ( this->terminated )
309 {
310 this->terminated = 0;
311 this->status = mvcp_init( dv1394app_get_parser( this->app ) );
312 pthread_create( &this->status_thread, NULL, status_thread, this );
313 }
314 }
315
316 static void this_page_on_disconnect( page super )
317 {
318 page_status this = ( page_status )super;
319 if ( !this->terminated )
320 {
321 GtkWidget *widget;
322 this->terminated = 1;
323 gdk_threads_leave();
324 pthread_join( this->status_thread, NULL );
325 gdk_threads_enter();
326 mvcp_close( this->status );
327 widget = lookup_widget( dv1394app_get_widget( this->app ), "statusbar" );
328 gtk_statusbar_push( GTK_STATUSBAR( widget ), this->context, "Disconnected." );
329 }
330 }
331
332 static void this_page_close( page super )
333 {
334 page_status this = ( page_status )super;
335 if ( this != NULL )
336 free( this );
337 }
338
339 page page_status_init( dv1394app app, struct page_t *parent_page )
340 {
341 page_status this = calloc( 1, sizeof( page_status_t ) );
342 int index = 0;
343 GtkWidget *widget;
344
345 this->parent_page = parent_page;
346 this->parent.get_widget = this_page_get_widget;
347 this->parent.get_toolbar_info = this_page_get_toolbar_info;
348 this->parent.on_connect = this_page_on_connect;
349 this->parent.on_disconnect = this_page_on_disconnect;
350 this->parent.close = this_page_close;
351 this->app = app;
352 this->terminated = 1;
353
354 for ( index = 0; index < MAX_UNITS; index ++ )
355 {
356 char button_name[ 256 ];
357 sprintf( button_name, "radiobutton_%d", index );
358 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
359 gtk_signal_connect( GTK_OBJECT( widget ), "toggled", GTK_SIGNAL_FUNC( on_radiobutton_toggled ), this );
360 }
361
362 widget = lookup_widget( dv1394app_get_widget( this->app ), "statusbar" );
363 this->context = gtk_statusbar_get_context_id( GTK_STATUSBAR( widget ), "info" );
364 gtk_statusbar_push( GTK_STATUSBAR( widget ), this->context, "Disconnected." );
365
366 return ( page )this;
367 }