5f6b66d335d8de923328378c01658d80f5b764cf
[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 label_name[ 256 ];
91
92 format_status_string(temp, sizeof(temp), status , "\n");
93 sprintf( label_name, "label_unit_%d", status->unit );
94
95 gdk_threads_enter();
96 widget = lookup_widget( widget, label_name );
97 gtk_label_set_text( GTK_LABEL( widget ), temp );
98 if ( status->unit == dv1394app_get_selected_unit( this->app ) )
99 {
100 char tc1[12], tc2[12];
101
102 snprintf( temp, sizeof(temp), "U%d - [%s] %s",
103 status->unit,
104 status_to_text( status ),
105 status->clip);
106 widget = lookup_widget( dv1394app_get_widget( this->app ), "statusbar" );
107 gtk_statusbar_pop( GTK_STATUSBAR( widget ), this->context );
108 gtk_statusbar_push( GTK_STATUSBAR( widget ), this->context, temp );
109
110 widget = lookup_widget( dv1394app_get_widget( this->app ), "bbt_label_1a" );
111 snprintf( temp, sizeof(temp), "%s / %s",
112 frames2tc(status->position, status->fps, tc1),
113 frames2tc(status->out - status->position, status->fps, tc2));
114 gtk_label_set_text( GTK_LABEL( widget ), temp );
115
116 dv1394app_show_status( this->app, status );
117 }
118 gdk_flush();
119 gdk_threads_leave();
120
121 return status->unit >= this->count;
122 }
123
124 static void show_units( page_status this, gboolean active )
125 {
126 int index = 0;
127 int index2 = 0;
128
129 GtkWidget *widget;
130 char temp[ 1024 ] = "";
131 char button_name[ 256 ];
132
133 mvcp_units units = NULL;
134 mvcp_unit_entry_t unit;
135 int unit_count = 0;
136
137 mvcp_nodes nodes = NULL;
138 mvcp_node_entry_t node;
139 int node_count = 0;
140
141 if ( active )
142 {
143 units = mvcp_units_init( this->status );
144 unit_count = mvcp_units_count( units );
145 nodes = mvcp_nodes_init( this->status );
146 node_count = mvcp_nodes_count( nodes );
147 this->count = unit_count;
148 }
149
150 gdk_threads_enter();
151
152 for ( index = 0; index < MAX_UNITS; index ++ )
153 {
154 if ( index < unit_count )
155 {
156 mvcp_units_get( units, index, &unit );
157
158 for ( index2 = 0; index2 < node_count; index2 ++ )
159 {
160 mvcp_nodes_get( nodes, index2, &node );
161 if ( !strcmp( node.guid, unit.guid ) )
162 break;
163 }
164
165 if ( index2 < node_count )
166 sprintf( temp, "U%d - %s (%s)", unit.unit, node.name, unit.guid );
167 else
168 sprintf( temp, "U%d - %s", unit.unit, unit.guid );
169
170 sprintf( button_name, "radiobutton_%d", index );
171 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
172 gtk_widget_show( widget );
173 gtk_button_set_label( GTK_BUTTON( widget ), temp );
174 sprintf( button_name, "label_unit_%d", index );
175 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
176 gtk_widget_show( widget );
177 }
178 else
179 {
180 sprintf( button_name, "radiobutton_%d", index );
181 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
182 gtk_widget_hide( widget );
183 sprintf( button_name, "label_unit_%d", index );
184 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
185 gtk_widget_hide( widget );
186 }
187 }
188 gdk_flush();
189 gdk_threads_leave();
190
191 mvcp_notifier notifier = mvcp_get_notifier( this->status );
192 mvcp_status_t status;
193
194 for ( index = 0; index < MAX_UNITS; index ++ )
195 {
196 status.unit = index;
197 if ( !active )
198 status.status = unit_disconnected;
199 else
200 mvcp_notifier_get( notifier, &status, index );
201 show_status( this, &status );
202 }
203
204 if ( active )
205 {
206 mvcp_nodes_close( nodes );
207 mvcp_units_close( units );
208 }
209 }
210
211 /** Status monitoring thread.
212 */
213
214 static void *status_thread( void *arg )
215 {
216 int i;
217 page_status this = arg;
218 mvcp_notifier notifier = mvcp_get_notifier( this->status );
219 mvcp_status_t status;
220 mvcp_status_t *statuses;
221
222 show_units( this, TRUE );
223
224 statuses = malloc(sizeof(mvcp_status_t) * this->count);
225 memset(statuses, 0, sizeof(mvcp_status_t) * this->count);
226
227 while ( !this->terminated )
228 {
229 char buf[32];
230
231 /* wait for any event from notifier */
232 mvcp_notifier_wait( notifier, &status );
233
234 /* get status for all units */
235 for(i = 0; i < this->count; i++)
236 {
237 mvcp_notifier_get( notifier, &status, i);
238
239 if ( status.status == unit_disconnected )
240 goto break_unit_disconnected;
241
242 if( memcmp(&status, &statuses[i], sizeof(mvcp_status_t)) || !this->app->eof[i])
243 {
244 /* save status */
245 statuses[i] = status;
246
247 /* save eof status */
248 if ( !this->app->eof[i] && mvcp_unit_get( this->app->command, i,
249 "eof", buf, sizeof(buf) ) != -1)
250 this->app->eof[i] = buf[0];
251
252 /* send status update */
253 if ( show_status( this, &status ) )
254 show_units( this, TRUE );
255 }
256 }
257 }
258
259 break_unit_disconnected:
260
261 free(statuses);
262
263 show_units( this, FALSE );
264
265 if ( !this->terminated )
266 {
267 GtkWidget *widget = lookup_widget( dv1394app_get_widget( this->app ), "button_disconnect" );
268 gdk_threads_enter();
269 gtk_signal_emit_by_name( GTK_OBJECT( widget ), "clicked" );
270 gdk_threads_leave();
271 }
272
273 return NULL;
274 }
275
276 void on_radiobutton_toggled( GtkToggleButton *togglebutton, gpointer user_data )
277 {
278 page_status this = user_data;
279 dv1394app app = this->app;
280 int index = 0;
281 GtkWidget *widget = this_page_get_widget( ( page )this );
282 for ( index = 0; index < MAX_UNITS; index ++ )
283 {
284 char button_name[ 256 ];
285 sprintf( button_name, "radiobutton_%d", index );
286 if ( lookup_widget( widget, button_name ) == GTK_WIDGET( togglebutton ) )
287 break;
288 }
289 if ( index < MAX_UNITS )
290 {
291 mvcp_status_t status;
292 mvcp_notifier notifier = mvcp_get_notifier( this->status );
293 dv1394app_on_unit_change( app, index );
294 mvcp_notifier_get( notifier, &status, index );
295 gdk_threads_leave( );
296 show_status( this, &status );
297 gdk_threads_enter( );
298 }
299 }
300
301 static GtkWidget *this_page_get_widget( page super )
302 {
303 page_status this = ( page_status )super;
304 if ( this->widget == NULL )
305 this->widget = this->parent_page->get_widget(this->parent_page);
306 return this->widget;
307 }
308
309 static void this_page_get_toolbar_info( page super, GtkIconSize size, GtkWidget **icon, char **label )
310 {
311 *icon = gtk_image_new_from_stock( "gtk-home", size );
312 *label = _("_Units");
313 }
314
315 static void this_page_on_connect( page super )
316 {
317 page_status this = ( page_status )super;
318 if ( this->terminated )
319 {
320 this->terminated = 0;
321 this->status = mvcp_init( dv1394app_get_parser( this->app ) );
322 pthread_create( &this->status_thread, NULL, status_thread, this );
323 }
324 }
325
326 static void this_page_on_disconnect( page super )
327 {
328 page_status this = ( page_status )super;
329 if ( !this->terminated )
330 {
331 GtkWidget *widget;
332 this->terminated = 1;
333 gdk_threads_leave();
334 pthread_join( this->status_thread, NULL );
335 gdk_threads_enter();
336 mvcp_close( this->status );
337 widget = lookup_widget( dv1394app_get_widget( this->app ), "statusbar" );
338 gtk_statusbar_push( GTK_STATUSBAR( widget ), this->context, "Disconnected." );
339 }
340 }
341
342 static void this_page_close( page super )
343 {
344 page_status this = ( page_status )super;
345 if ( this != NULL )
346 free( this );
347 }
348
349 page page_status_init( dv1394app app, struct page_t *parent_page )
350 {
351 page_status this = calloc( 1, sizeof( page_status_t ) );
352 int index = 0;
353 GtkWidget *widget;
354
355 this->parent_page = parent_page;
356 this->parent.get_widget = this_page_get_widget;
357 this->parent.get_toolbar_info = this_page_get_toolbar_info;
358 this->parent.on_connect = this_page_on_connect;
359 this->parent.on_disconnect = this_page_on_disconnect;
360 this->parent.close = this_page_close;
361 this->app = app;
362 this->terminated = 1;
363
364 for ( index = 0; index < MAX_UNITS; index ++ )
365 {
366 char button_name[ 256 ];
367 sprintf( button_name, "radiobutton_%d", index );
368 widget = lookup_widget( this_page_get_widget( ( page )this ), button_name );
369 gtk_signal_connect( GTK_OBJECT( widget ), "toggled", GTK_SIGNAL_FUNC( on_radiobutton_toggled ), this );
370 }
371
372 widget = lookup_widget( dv1394app_get_widget( this->app ), "statusbar" );
373 this->context = gtk_statusbar_get_context_id( GTK_STATUSBAR( widget ), "info" );
374 gtk_statusbar_push( GTK_STATUSBAR( widget ), this->context, "Disconnected." );
375
376 return ( page )this;
377 }