# HG changeset patch
# User rosfran
# Date 1166053444 0
# Node ID 462a3c81abd6070859da541fe1b48dddf1338bbc
# Parent  5f798037032572de712769fec5d4c5673b2159d5
[svn r221] Added some signals to interact with the MythTV backend messages.

diff -r 5f7980370325 -r 462a3c81abd6 gmyth/src/gmyth_file_transfer.c
--- a/gmyth/src/gmyth_file_transfer.c	Wed Dec 13 23:42:28 2006 +0000
+++ b/gmyth/src/gmyth_file_transfer.c	Wed Dec 13 23:44:04 2006 +0000
@@ -103,7 +103,7 @@
 static void gmyth_file_transfer_finalize (GObject *object);
 
 static void gmyth_file_transfer_program_info_changed( GMythFileTransfer *transfer, 
-										gint msg_code, gpointer livetv_transfer );
+										gint msg_code, gpointer livetv_transfer, gpointer user_data );
 
 static gboolean gmyth_connect_to_backend (GMythFileTransfer *transfer);
 
@@ -162,6 +162,8 @@
   transfer->control_sock = NULL;
   transfer->sock = NULL;
   
+  transfer->mutex = g_mutex_new();
+  
 }
 
 static void
@@ -169,6 +171,12 @@
 {
 	GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (object);
 	
+  if ( transfer->mutex != NULL )  
+  {
+  	g_mutex_free( transfer->mutex );
+  	transfer->mutex = NULL;
+  }
+  
   if ( transfer->control_sock != NULL )  
   {
   	g_object_unref( transfer->control_sock );
@@ -497,7 +505,9 @@
 
   GIOCondition io_cond;
   GIOCondition io_cond_control;
-  GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL;   
+  GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL;
+  
+  gboolean ret = TRUE;   
 
   GString *query;
   
@@ -547,28 +557,58 @@
     // Receives the backand answer    
     gmyth_socket_read_stringlist( transfer->control_sock, strlist );
 
-    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) { 
+    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
+    { 
 	    bytes_sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
 	    gmyth_debug ( "[%s] got SENT buffer message = %d\n", __FUNCTION__, bytes_sent );
 
-      	if ( bytes_sent != 0 ) 
-      	{
-            gchar *data_buffer = g_new0 (gchar, bytes_sent);    
-     	    while ( 0 != bytes_sent) { 
-                io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read, 
-    				(gsize) bytes_sent, // buffer_len
-					&bytes_read, &error );
-    	        total_read += bytes_read;
-                bytes_sent -= bytes_read;
-	        
-	            /* append new data to the increasing byte array */
-		    		data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read);
-		        gmyth_debug ("Total file transfer data read: %d\n", total_read);
-	        }
-            g_free (data_buffer);
+    	if ( bytes_sent != 0 ) 
+    	{
+        gchar *data_buffer = g_new0 ( gchar, bytes_sent );    
+   	    while ( 0 != bytes_sent ) 
+   	    { 
+          io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read, 
+	    				(gsize) bytes_sent,	&bytes_read, &error );
+
+	        total_read += bytes_read;
+          bytes_sent -= bytes_read;
+      
+            /* append new data to the increasing byte array */
+	    		data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read);
+	        gmyth_debug ("Total transfer data read: %d\n", total_read);
+        }
+        g_free (data_buffer);
 	    } else {
-	    	
-      }
+				transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);
+				
+				if ( transfer->priv != NULL && transfer->priv->livetv != NULL )
+				{
+				
+					total_read = GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN;	
+				
+			  	if ( transfer->priv->do_next_program_chain )
+			  	{			  					  		
+			  		
+			  		g_mutex_lock( transfer->mutex );
+			  		
+			  		ret = gmyth_livetv_next_program_chain( transfer->priv->livetv );
+			  		
+			  		g_mutex_unlock( transfer->mutex );
+			  		
+						if ( !ret )
+						{
+							gmyth_debug( "Cannot change to the next program chain!" );
+						}
+						else
+						{
+							gmyth_debug( "OK!!! MOVED to the next program chain [%s]!", 
+													(gmyth_tvchain_get_id( transfer->priv->livetv->tvchain ))->str );
+						}
+						
+			  	}
+			  	
+				} /* if */
+      } /* if */
     } else {
     	total_read = GMYTHTV_FILE_TRANSFER_READ_ERROR;
     	g_object_unref (strlist);
@@ -589,46 +629,21 @@
   }
   
   if ( total_read > 0 )
-  	transfer->readposition += total_read;
-  else
-  {
-		transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);
-		
-		if ( transfer->priv != NULL && transfer->priv->livetv != NULL )
-		{
-		
-			total_read = GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN;	
-		
-	  	if ( max_tries <= 0 && transfer->priv->do_next_program_chain )
-	  	{
-	  		
-				if ( !gmyth_livetv_next_program_chain( transfer->priv->livetv ) )
-				{
-					gmyth_debug( "Cannot change to the next program chain!" );
-				}
-				else
-				{
-					gmyth_debug( "OK!!! MOVED to the next program chain [%s]!", 
-											(gmyth_tvchain_get_id( transfer->priv->livetv->tvchain ))->str );
-				}
-	  	}
-	  	
-		} /* if */
-		
-  } /* if */
+  	transfer->readposition += total_read;  	
   	
   return total_read;
 }
 
 static void 
 gmyth_file_transfer_program_info_changed( GMythFileTransfer *transfer, 
-										gint msg_code, gpointer livetv_transfer )
+										gint msg_code, gpointer livetv_transfer, gpointer user_data )
 {
-	GMythLiveTV *livetv = (GMythLiveTV*)livetv_transfer;
+	GMythLiveTV *livetv = (GMythLiveTV*)user_data;
 	
-	gmyth_debug( "Program info changed! ( file transfer orig. = %p, ptr. = %p )", transfer, livetv_transfer );
+	gmyth_debug( "Program info changed! ( file transfer orig. = %p, ptr. = [%s], user data = [%s] )", transfer, 
+		livetv_transfer != NULL ? "[NOT NULL]" : "[NULL]", livetv != NULL ? "[NOT NULL]" : "[NULL]" );
 	
-	if ( transfer == livetv->file_transfer )
+	if ( livetv != NULL && transfer == livetv->file_transfer )
 	{
 		gmyth_debug( "YES, the requested program info movement on the LiveTV transfer is authentical!" );
 	}
diff -r 5f7980370325 -r 462a3c81abd6 gmyth/src/gmyth_file_transfer.h
--- a/gmyth/src/gmyth_file_transfer.h	Wed Dec 13 23:42:28 2006 +0000
+++ b/gmyth/src/gmyth_file_transfer.h	Wed Dec 13 23:44:04 2006 +0000
@@ -86,6 +86,8 @@
 	/* socket descriptors */
 	GMythSocket 			*control_sock;
 	GMythSocket 			*sock;
+	
+	GMutex 						*mutex;
 
 	gint64 						readposition;
 	guint64 					filesize;
diff -r 5f7980370325 -r 462a3c81abd6 gmyth/src/gmyth_livetv.c
--- a/gmyth/src/gmyth_livetv.c	Wed Dec 13 23:42:28 2006 +0000
+++ b/gmyth/src/gmyth_livetv.c	Wed Dec 13 23:44:04 2006 +0000
@@ -83,7 +83,6 @@
 static void
 gmyth_livetv_dispose  (GObject *object)
 {
-
 	G_OBJECT_CLASS (gmyth_livetv_parent_class)->dispose (object);
 }
 
@@ -139,13 +138,14 @@
 
 static void
 gmyth_livetv_monitor_signal_handler( GMythMonitorHandler *monitor, gint msg_code, 
-							gchar* message, gpointer livetv )
+							gchar* message, gpointer livetv, gpointer user_data )
 {
-	GMythLiveTV *live_tv = (GMythLiveTV*) livetv;
+	GMythLiveTV *live_tv = (GMythLiveTV*) user_data;
 	
-	gmyth_debug( "LIVETV Signal handler ( msg = %s, code = %d )\n", message, msg_code );
+	gmyth_debug( "LIVETV Signal handler ( msg = %s, code = %d, livetv param = %s, user_data = %s )\n", message, msg_code, livetv != NULL ? "" : 
+					"NULL", user_data != NULL ? "" : "NULL" );
 	
-	if ( NULL == live_tv )
+	if ( NULL == live_tv && user_data == NULL )
 		return;		
 	
 	switch ( msg_code ) 
@@ -154,31 +154,81 @@
 		case GMYTH_BACKEND_PROGRAM_INFO_CHANGED:
 		{
 			gmyth_debug( "LIVETV Program Changed event received [ msg = %s ]\n", message );
-			/*
-			if ( !gmyth_livetv_next_program_chain( live_tv ) )
-			{
-				gmyth_debug( "Cannot change to the next program chain!" );
-			}
-			else
-			{
-			*/
 			gmyth_debug( "OK!!! MOVED to the next program chain [%s]!", 
 									(gmyth_tvchain_get_id( live_tv->tvchain ))->str );
 			/* advertises the FileTransfer about the program info changed */
-			g_signal_emit ( live_tv->file_transfer,
-             GMYTH_FILE_TRANSFER_GET_CLASS (live_tv->file_transfer)->program_info_changed_handler_signal_id,
-             0, /* details */
-             msg_code, live_tv );
+			if ( live_tv->file_transfer != NULL )
+			{
+				g_signal_emit ( live_tv->file_transfer,
+	             GMYTH_FILE_TRANSFER_GET_CLASS (live_tv->file_transfer)->program_info_changed_handler_signal_id,
+	             0, /* details */
+	             msg_code, live_tv, live_tv );
+	             
+	      gmyth_livetv_monitor_handler_stop( live_tv );	      
+			} else
+				gmyth_debug( "LIVETV file_transfer is NULL!!! Cannot move to the next program chain event received.\n");
 			/*}*/
 			break;
 		}
 		default:
-			break;
-		
+			break;		
+	} /* switch (Monitor Handler messages) */
+	
+}
+
+gboolean
+gmyth_livetv_monitor_handler_start( GMythLiveTV *livetv )
+{
+	gboolean res = TRUE;
+	
+	if ( livetv->monitor != NULL )
+	{
+		g_object_unref( livetv->monitor );
+		livetv->monitor	= NULL;
 	}
 	
+  livetv->monitor = gmyth_monitor_handler_new ( );
+  
+  res = gmyth_monitor_handler_open (livetv->monitor, livetv->backend_info->hostname, 
+  				livetv->backend_info->port );
+  
+  if ( res == TRUE )
+  {
+  	gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
+  	
+  	res = gmyth_monitor_handler_start ( livetv->monitor );
+  	
+  	if (res)
+  	{
+  		gmyth_debug("MythTV Monitor event socket connected and listening!");
+  		g_signal_connect ( G_OBJECT (livetv->monitor), "backend-events-handler",
+                  (GCallback)gmyth_livetv_monitor_signal_handler,
+                  livetv );
+  	}
+  	else
+  	{
+  		gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
+  		goto error;  		
+  	}
+  }
+  
+error:
+	return res;
+  
 }
 
+void
+gmyth_livetv_monitor_handler_stop( GMythLiveTV *livetv )
+{
+	
+  if ( livetv->monitor != NULL )
+  {
+  	g_object_unref( livetv->monitor );
+  	livetv->monitor = NULL;
+  } 
+  
+}  
+
 gboolean
 gmyth_livetv_setup ( GMythLiveTV *livetv, GMythBackendInfo *backend_info )
 {
@@ -199,31 +249,6 @@
 
 	livetv->is_livetv = TRUE;
 	
-  livetv->monitor = gmyth_monitor_handler_new ( );
-  
-  res = gmyth_monitor_handler_open (livetv->monitor, livetv->backend_info->hostname, 
-  				livetv->backend_info->port );
-  
-  if ( res == TRUE )
-  {
-  	gmyth_debug("Connect MythTV Monitor event socket! Trying to start the message handler...");
-  	
-  	res = gmyth_monitor_handler_start ( livetv->monitor );
-  	
-  	if (res)
-  	{
-  		gmyth_debug("MythTV Monitor event socket connected and listening!");
-  		g_signal_connect (G_OBJECT (livetv->monitor), "backend-events-handler",
-                  (GCallback)gmyth_livetv_monitor_signal_handler,
-                  livetv);
-  	}
-  	else
-  	{
-  		gmyth_debug("Problems when trying to start MythTV Monitor event socket!");
-  		goto error;  		
-  	}
-  }
-
 	livetv->local_hostname  = gmyth_socket_get_local_hostname ( );
 
 	if ( livetv->local_hostname == NULL ) {
@@ -326,6 +351,11 @@
 		livetv->proginfo = NULL;
 	}
 
+	if ( livetv->monitor != NULL ) {
+		g_object_unref (livetv->monitor);
+		livetv->monitor = NULL;
+	}
+
 	return res;
 
 }
@@ -342,6 +372,9 @@
 		goto error;		
 	}
 	
+	//if ( !gmyth_livetv_monitor_handler_start( livetv ) )
+	//	goto error;
+	
 	/* Reload all TV chain from Mysql database. */
 	gmyth_tvchain_reload_all (livetv->tvchain);
 
@@ -359,6 +392,8 @@
 	} else {
 		gmyth_debug ("[%s] GMythLiveTV: All requests to backend to start TV were OK, TV chain changed.\n", __FUNCTION__ );
 	}
+	
+	livetv->setup_done = TRUE;
 
 	return res;
 
@@ -416,7 +451,7 @@
   uri = gmyth_uri_new_with_value( livetv->proginfo->pathname->str );
   if ( NULL == uri )
   {
-  	gmyth_debug( "Couldn't parse the URI to start LiveTV! [uri = %s]", livetv->proginfo->pathname->str );  	
+  	gmyth_debug( "Couldn't parse the URI to start LiveTV! [ uri = %s ]", livetv->proginfo->pathname->str );  	
   }
 
 	if ( !gmyth_file_transfer_open( livetv->file_transfer, uri != NULL ? gmyth_uri_get_path(uri) : 
@@ -428,6 +463,9 @@
 		goto done;
 	}
 
+	if ( !gmyth_livetv_monitor_handler_start( livetv ) )
+		goto done;
+
 done:
 	if ( uri != NULL )
 	{
@@ -446,7 +484,7 @@
 	gmyth_debug ("[%s] Stopping the LiveTV...\n", __FUNCTION__);
 
 	if (livetv->is_livetv) {
-		if (!gmyth_recorder_stop_livetv (livetv->recorder)) {
+		if ( !gmyth_recorder_stop_livetv (livetv->recorder) ) {
 			g_warning ("[%s] Error while stoping remote encoder", __FUNCTION__);	
 		}
 	}
diff -r 5f7980370325 -r 462a3c81abd6 gmyth/src/gmyth_livetv.h
--- a/gmyth/src/gmyth_livetv.h	Wed Dec 13 23:42:28 2006 +0000
+++ b/gmyth/src/gmyth_livetv.h	Wed Dec 13 23:44:04 2006 +0000
@@ -92,6 +92,9 @@
 
 GMythFileTransfer *gmyth_livetv_create_file_transfer( GMythLiveTV *livetv );
 
+gboolean gmyth_livetv_monitor_handler_start( GMythLiveTV *livetv );
+void gmyth_livetv_monitor_handler_stop( GMythLiveTV *livetv );
+
 G_END_DECLS
 
 #endif /*GMYTH_LIVETV_H_*/
diff -r 5f7980370325 -r 462a3c81abd6 gmyth/src/gmyth_monitor_handler.c
--- a/gmyth/src/gmyth_monitor_handler.c	Wed Dec 13 23:42:28 2006 +0000
+++ b/gmyth/src/gmyth_monitor_handler.c	Wed Dec 13 23:44:04 2006 +0000
@@ -72,18 +72,18 @@
 
 GMainContext *io_watcher_context = NULL;
 
-GThreadPool *monitor_th = NULL;
+//GThread *monitor_th = NULL;
 
 //static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
-//static gboolean myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, 
-//				gpointer data );
-//static void myth_control_sock_listener( GIOChannel *io_channel, gboolean* ret );
-static void
-gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message, 
-			gpointer live_tv );
+//static gboolean gmyth_monitor_handler_listener( GIOChannel *io_channel, 
+//						GIOCondition condition, gpointer data );
 
-static void gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, 
-		gpointer user_data);
+gboolean* gmyth_monitor_handler_listener( GMythMonitorHandler *monitor );
+
+static void gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message,
+			gpointer live_tv, gpointer user_data );
+//static void gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, 
+//		gpointer user_data);
 
 static GMutex*				mutex 					 = NULL;
 
@@ -148,6 +148,10 @@
   
   /* mutex to control access to the event socket consumer thread */
   mutex = g_mutex_new();
+  
+  monitor->monitor_th = NULL;
+  
+  monitor->gmyth_monitor_handler_listener = gmyth_monitor_handler_listener;
 }
 
 static void
@@ -156,6 +160,13 @@
 	
 	GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER (object);
 	
+	if ( monitor->monitor_th != NULL )  
+  {
+  	//g_thread_exit( monitor->monitor_th, TRUE, FALSE );
+  	g_thread_exit( monitor->monitor_th );
+  	monitor->monitor_th = NULL;
+  }
+	
   if ( monitor->event_sock != NULL )  
   {
   	g_object_unref( monitor->event_sock );
@@ -186,12 +197,6 @@
   	io_watcher_cond = NULL;
   }
   
-  if ( monitor_th != NULL )  
-  {
-  	g_thread_pool_free( monitor_th, TRUE, FALSE );
-  	monitor_th = NULL;
-  }
-
   G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->dispose (object);
 }
 
@@ -213,6 +218,55 @@
   return monitor;
 }
 
+static gboolean 
+myth_control_acquire_context( gboolean do_wait ) 
+{
+	
+	gboolean ret = TRUE;	
+	//guint max_iter = 50;
+	
+	//g_mutex_lock( mutex );
+	
+  //while ( !has_io_access ) 
+  //	g_cond_wait( io_watcher_cond, mutex );
+  	
+  //has_io_access = FALSE;
+  /*
+  if ( do_wait ) {
+  	while ( --max_iter > 0 && !g_main_context_wait( io_watcher_context, io_watcher_cond, mutex ) )
+  		ret = FALSE;
+  } else if ( !g_main_context_acquire( io_watcher_context ) )
+  	ret = FALSE;
+  */
+  	
+  //g_static_mutex_lock( &st_mutex );
+  
+  return ret;
+  
+}
+
+static gboolean 
+myth_control_release_context( ) 
+{
+	
+	gboolean ret = TRUE;
+    
+  //g_static_mutex_unlock( &st_mutex );
+  
+	//g_main_context_release( io_watcher_context );
+  
+  //g_main_context_wakeup( io_watcher_context );
+  
+  //has_io_access = TRUE;
+
+  //g_cond_broadcast( io_watcher_cond );
+  
+  //g_mutex_unlock( mutex );  
+ 
+  return ret;
+  
+}
+
 gboolean
 gmyth_monitor_handler_open (GMythMonitorHandler *monitor, gchar *hostname, gint port)
 {
@@ -287,7 +341,7 @@
 
 static void
 gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message,
-			gpointer live_tv )
+			gpointer live_tv, gpointer user_data )
 {
 	//assert( message!= NULL );  
 	gmyth_debug( "DEFAULT Signal handler ( msg = %s, code = %d, file_transfer ptr. = %p )\n", 
@@ -300,15 +354,16 @@
 	gmyth_debug( "Backend message event: %s --- ", str->str );
 }
 
-static void
-gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
+//static void
+//gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
+//static gboolean
+//gmyth_monitor_handler_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
+gboolean*
+gmyth_monitor_handler_listener( GMythMonitorHandler *monitor )
 {
-
+	//GMythMonitorHandler *monitor = (GMythMonitorHandler*)data; 
   GIOStatus io_status;
-  //GError *error = NULL;
   GIOCondition io_cond;  
-  //gchar *trash = NULL;
-  //GByteArray *byte_array = NULL;
   guint recv = 0;
   gboolean *ret = g_new0( gboolean, 1 );
   *ret = TRUE;
@@ -324,7 +379,7 @@
   
   //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
   
-  //myth_control_acquire_context (TRUE);
+  myth_control_acquire_context (TRUE);
   
   if ( io_channel == NULL ) {
   	g_debug ("Monitor socket is NULL!\n");
@@ -343,42 +398,46 @@
     	gint bytes_sent = 0;
     	
     	strlist = gmyth_string_list_new();
-
-      len = gmyth_socket_read_stringlist( monitor->event_sock, strlist );
-      
-	    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
-	    { 
-		    bytes_sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
-		    gmyth_debug ( "[%s] MONITOR: got SENT buffer message = %d\n", __FUNCTION__, bytes_sent );
-
-	      gmyth_debug ( "[%s] MONITOR: received data buffer from IO event channel... %d strings gone!\n", 
-	      		__FUNCTION__, len );
-	      		
-	      recv += len;
+    	
+    	if ( monitor->event_sock != NULL )
+    	{
+    		
+	      len = gmyth_socket_read_stringlist( monitor->event_sock, strlist );
 	      
-	      /* debug purpose: prints out all the string list elements */
-	      g_list_foreach( strlist->glist, (GFunc)gmyth_monitor_handler_print, NULL );
-	      
-	      gchar *back_msg_action = g_new0( gchar, 1 );
-	      gint msg_type = gmyth_monitor_handler_is_backend_message( monitor, strlist, 
-	      		&back_msg_action );
-	      
-        g_signal_emit ( monitor,
-               GMYTH_MONITOR_HANDLER_GET_CLASS (monitor)->backend_events_handler_signal_id,
-               0, /* details */
-               msg_type, back_msg_action, NULL );
-               
-        if (back_msg_action!= NULL)
-        	g_free( back_msg_action );
-	      
-	    }
+		    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
+		    { 
+			    bytes_sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error
+	
+		      gmyth_debug ( "[%s] MONITOR: received data buffer from IO event channel... %d strings gone!\n", 
+		      		__FUNCTION__, len );
+		      		
+		      recv += len;
+		      
+		      /* debug purpose: prints out all the string list elements */
+		      g_list_foreach( strlist->glist, (GFunc)gmyth_monitor_handler_print, NULL );
+		      
+		      gchar *back_msg_action = g_new0( gchar, 1 );
+		      gint msg_type = gmyth_monitor_handler_is_backend_message( monitor, strlist, 
+		      		&back_msg_action );
+		      
+	        g_signal_emit ( monitor,
+	               GMYTH_MONITOR_HANDLER_GET_CLASS (monitor)->backend_events_handler_signal_id,
+	               0, /* details */
+	               msg_type, back_msg_action, NULL );
+	               
+	        if (back_msg_action!= NULL)
+	        	g_free( back_msg_action );
+		      
+		    }
+		    
+    	}
 	    
 	    if (strlist!=NULL)
 	    	g_object_unref( strlist );
       	
       io_cond = g_io_channel_get_buffer_condition( io_channel );
 
-    } while ( ( io_cond & G_IO_IN ) != 0 );
+    } while ( recv <= 0 && ( io_cond & G_IO_IN ) != 0 );
     
     gmyth_debug ("[%s]\tMONITOR EVENT: Read %d bytes\n", __FUNCTION__, recv );
 
@@ -386,6 +445,8 @@
 	  
   } /* main GThread while */
   
+  myth_control_release_context ();
+  
   if ( io_status == G_IO_STATUS_ERROR ) {
     //gmyth_debug ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
     gmyth_debug ("Error reading MONITOR event socket.\n");
@@ -397,6 +458,8 @@
 
   if (strlist!=NULL)
   	g_object_unref( strlist );
+  	
+ 	return ret;
 
 }
 
@@ -425,44 +488,44 @@
   return ret;
 }    
 
-/*
 static gboolean*
-gmyth_monitor_handler_setup( GIOChannel *channel )
+gmyth_monitor_handler_setup( GMythMonitorHandler *monitor, GIOChannel *channel )
 {
 	gboolean *ret = g_new0( gboolean, 1 );
-	//guint src_id = 0;
+	guint src_id = 0;
 	
 	*ret = TRUE;
 	
-  io_watcher_context = g_main_context_default();
-  GMainLoop *loop = g_main_loop_new( io_watcher_context, TRUE );
+  //io_watcher_context = g_main_context_default();
+  //GMainLoop *loop = g_main_loop_new( io_watcher_context, TRUE );
 
-  GSource *source;
+  //GSource *source;
 
   if ( channel != NULL ) {
-    source = g_io_create_watch( channel, G_IO_IN | G_IO_HUP );
-    //src_id = g_io_add_watch( channel, G_IO_IN | G_IO_HUP, 
-    //					(GSourceFunc)myth_control_sock_listener, NULL );
+    //source = g_io_create_watch( channel, G_IO_IN | G_IO_HUP );
+    src_id = g_io_add_watch( channel, G_IO_IN, 
+    					(GIOFunc)gmyth_monitor_handler_listener, monitor );
   } else {
   	*ret = FALSE;
   	goto cleanup;
   }
 
-  g_source_set_callback ( source, (GSourceFunc)gmyth_monitor_handler_listener, NULL, NULL );
+  //g_source_set_callback ( source, (GSourceFunc)gmyth_monitor_handler_listener, NULL, NULL );
 
-  g_source_attach( source, io_watcher_context );
+  //g_source_attach( source, io_watcher_context );
   
-  if (NULL == source){
+  //if (NULL == source){
+  if (src_id < 0){
     gmyth_debug( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
     *ret = FALSE;
     goto cleanup;
   }
   
-  g_main_loop_run( loop );
+  //g_main_loop_run( loop );
   
 cleanup:
-  if ( source != NULL )
-    g_source_unref( source );
+  //if ( source != NULL )
+  //  g_source_unref( source );
     
   //if ( io_watcher_context != NULL )
   //  g_main_context_unref( io_watcher_context );
@@ -473,7 +536,6 @@
   return ret;
   
 }
-*/
 
 gboolean 
 gmyth_monitor_handler_start (GMythMonitorHandler *monitor)
@@ -483,13 +545,14 @@
 	
 	if (!g_thread_supported () ) 	g_thread_init (NULL);
 	
-  monitor_th = g_thread_pool_new( (GFunc)gmyth_monitor_handler_listener, 
-  					monitor, 3, TRUE, NULL );
+  monitor->monitor_th = g_thread_create( (GThreadFunc)gmyth_monitor_handler_listener, 
+  					monitor, TRUE, NULL );
   					
-  g_thread_pool_push( monitor_th, monitor, NULL ); 
+  //g_thread_pool_push( monitor_th, monitor, NULL ); 
   					
   //if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
-  if ( monitor_th != NULL )
+  if ( monitor->monitor_th != NULL )
+  //if ( gmyth_monitor_handler_setup( monitor, monitor->event_sock->sd_io_ch ) )
   {
   	gmyth_debug ( "\n[%s]\tOK! Starting listener on the MONITOR event socket...[thread location = %p]\n", 
   				__FUNCTION__, g_thread_self( ) );
diff -r 5f7980370325 -r 462a3c81abd6 gmyth/src/gmyth_monitor_handler.h
--- a/gmyth/src/gmyth_monitor_handler.h	Wed Dec 13 23:42:28 2006 +0000
+++ b/gmyth/src/gmyth_monitor_handler.h	Wed Dec 13 23:44:04 2006 +0000
@@ -71,7 +71,8 @@
 	guint backend_events_handler_signal_id;
 
   /* signal default handlers */
-  void (*backend_events_handler) (GMythMonitorHandler *monitor, gint msg_code, gchar* message, gpointer livetv );
+  void (*backend_events_handler) (GMythMonitorHandler *monitor, gint msg_code, gchar* message, 
+  					gpointer livetv, gpointer user_data );
 };
 
 struct _GMythMonitorHandler
@@ -84,6 +85,10 @@
 	/* socket descriptors */
 	GMythSocket *event_sock;
 	
+	GThread *monitor_th;
+	
+	gboolean* (*gmyth_monitor_handler_listener)( GMythMonitorHandler *monitor );
+	
 	gchar *hostname;
 	gint port;