# HG changeset patch
# User rosfran
# Date 1165516781 0
# Node ID 49ba93ca4586935a8223ba0bb1e208fc3daa36a4
# Parent  8ba1ddedfa2256f6f896828fd9ee201dda4e42ae
[svn r213] Include GObject signal connects/emmits to GMythMonitorHandler.

diff -r 8ba1ddedfa22 -r 49ba93ca4586 gmyth/src/Makefile.am
--- a/gmyth/src/Makefile.am	Wed Dec 06 21:50:34 2006 +0000
+++ b/gmyth/src/Makefile.am	Thu Dec 07 18:39:41 2006 +0000
@@ -2,7 +2,11 @@
 
 lib_LTLIBRARIES = libgmyth.la
 
-libgmyth_la_SOURCES = 			\
+BUILT_SOURCES = 				\
+	gmyth_marshal.c 			\
+	gmyth_marshal.h
+
+libgmyth_la_SOURCES = 				\
 	gmyth_common.c				\
 	gmyth_debug.c				\
 	gmyth_epg.c 				\
@@ -14,34 +18,47 @@
 	gmyth_query.c				\
 	gmyth_socket.c				\
 	gmyth_stringlist.c			\
-	gmyth_monitor_handler.c		\
-	gmyth_file_transfer.c		\
+	gmyth_monitor_handler.c			\
+	gmyth_file_transfer.c			\
 	gmyth_livetv.c				\
 	gmyth_backendinfo.c			\
 	gmyth_programinfo.c			\
-	gmyth_uri.c
+	gmyth_uri.c				\
+	$(BUILT_SOURCES)
 
-libgmyth_la_CFLAGS = 			\
-	-DDATADIR=\"$(pkgdatadir)\" \
+EXTRA_libgmyth_la_SOURCES = gmyth_marshal.list
+
+gmyth_marshal.h: gmyth_marshal.list
+	glib-genmarshal --header --prefix=gmyth_marshal gmyth_marshal.list > gmyth_marshal.h.tmp
+	mv gmyth_marshal.h.tmp gmyth_marshal.h
+
+gmyth_marshal.c: gmyth_marshal.list gmyth_marshal.h
+	echo "#include \"glib-object.h\"" > gmyth_marshal.c.tmp
+	echo "#include \"gmyth_marshal.h\"" >> gmyth_marshal.c.tmp
+	glib-genmarshal --body --prefix=gmyth_marshal $(srcdir)/gmyth_marshal.list >> gmyth_marshal.c.tmp
+	mv gmyth_marshal.c.tmp gmyth_marshal.c
+
+libgmyth_la_CFLAGS = 				\
+	-DDATADIR=\"$(pkgdatadir)\" 		\
 	$(GLIB_CFLAGS) 				\
 	$(GOBJECT_CFLAGS)			\
 	$(GST_CFLAGS) 				\
 	$(GSTBASE_CFLAGS)			\
-	$(GSTPLUGINSBASE_CFLAGS)	\
+	$(GSTPLUGINSBASE_CFLAGS)		\
 	$(MYSQL_CFLAGS)
 
-libgmyth_la_LDFLAGS = 			\
+libgmyth_la_LDFLAGS = 				\
 	-export-dynamic 			\
 	$(MYSQL_LIBS) 				\
 	$(GST_LIBS) 				\
 	$(GSTBASE_LIBS)				\
 	$(GSTPLUGINS_LIBS)
 
-libgmyth_includedir = 			\
+libgmyth_includedir = 				\
 	$(pkgincludedir)
 
-libgmyth_include_HEADERS =    	\
-	gmyth.h						\
+libgmyth_include_HEADERS =    			\
+	gmyth.h					\
 	gmyth_common.h	 			\
 	gmyth_debug.h				\
 	gmyth_epg.h 				\
@@ -53,12 +70,11 @@
 	gmyth_socket.h 				\
 	gmyth_remote_util.h			\
 	gmyth_stringlist.h 			\
-	gmyth_monitor_handler.h		\
-	gmyth_file_transfer.h		\
+	gmyth_monitor_handler.h			\
+	gmyth_file_transfer.h			\
 	gmyth_livetv.h				\
 	gmyth_backendinfo.h			\
 	gmyth_programinfo.h			\
 	gmyth_uri.h
 
 CLEANFILES = $(BUILT_SOURCES)
-
diff -r 8ba1ddedfa22 -r 49ba93ca4586 gmyth/src/gmyth_livetv.c
--- a/gmyth/src/gmyth_livetv.c	Wed Dec 06 21:50:34 2006 +0000
+++ b/gmyth/src/gmyth_livetv.c	Thu Dec 07 18:39:41 2006 +0000
@@ -111,6 +111,13 @@
 	return livetv;
 }
 
+static void
+gmyth_livetv_monitor_signal_handler( GMythMonitorHandler *monitor, gint msg_code, 
+							gchar* message )
+{
+	gmyth_debug( "LIVETV Signal handler ( msg = %s, code = %d )\n", message, msg_code );
+}
+
 gboolean
 gmyth_livetv_setup ( GMythLiveTV *livetv, GMythBackendInfo *backend_info )
 {
@@ -143,6 +150,9 @@
   	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,
+                  NULL);
   	}
   	else
   	{
diff -r 8ba1ddedfa22 -r 49ba93ca4586 gmyth/src/gmyth_marshal.list
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gmyth/src/gmyth_marshal.list	Thu Dec 07 18:39:41 2006 +0000
@@ -0,0 +1,1 @@
+VOID:INT,STRING
diff -r 8ba1ddedfa22 -r 49ba93ca4586 gmyth/src/gmyth_monitor_handler.c
--- a/gmyth/src/gmyth_monitor_handler.c	Wed Dec 06 21:50:34 2006 +0000
+++ b/gmyth/src/gmyth_monitor_handler.c	Thu Dec 07 18:39:41 2006 +0000
@@ -32,6 +32,10 @@
  * - port number *   
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include "gmyth_uri.h"
 #include "gmyth_livetv.h"
 #include "gmyth_util.h"
@@ -39,6 +43,7 @@
 #include "gmyth_stringlist.h"
 #include "gmyth_monitor_handler.h"
 #include "gmyth_debug.h"
+#include "gmyth_marshal.h"
 
 #include <unistd.h>
 #include <glib.h>
@@ -74,11 +79,10 @@
 
 GThreadPool *monitor_th = NULL;
 
-enum myth_sock_types {
-  GMYTH_PLAYBACK_TYPE = 0,
-  GMYTH_MONITOR_TYPE,
-  GMYTH_FILETRANSFER_TYPE,
-  GMYTH_RINGBUFFER_TYPE
+enum gmyth_monitor_handler_msg_types {
+	GMYTH_BACKEND_NO_MESSAGE = 0,
+  GMYTH_BACKEND_PROGRAM_INFO_CHANGED,
+  GMYTH_BACKEND_STOP_LIVETV
 };
 
 //static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
@@ -86,7 +90,8 @@
 //				gpointer data );
 //static void myth_control_sock_listener( GIOChannel *io_channel, gboolean* ret );
 
-static void gmyth_monitor_handler_listener ( GMythSocket *gmyth_socket, gboolean* ret );
+static void gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, 
+		gpointer user_data);
 
 static GMutex*				mutex 					 = NULL;
 
@@ -117,16 +122,17 @@
   gobject_class->finalize = gmyth_monitor_handler_finalize;
   
 	gmonitor_class->backend_events_handler_signal_id = 
-	  g_signal_newv ("backend-events-handler",
-	                 G_TYPE_FROM_CLASS (gmonitor_class),
-	                 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
-	                 NULL /* class closure */,
-	                 NULL /* accumulator */,
-	                 NULL /* accu_data */,
-	                 g_cclosure_marshal_VOID__VOID,
-	                 G_TYPE_NONE /* return_type */,
-	                 0     /* n_params */,
-	                 NULL  /* param_types */);
+		  g_signal_new ("backend-events-handler",
+		                 G_TYPE_FROM_CLASS (gmonitor_class),
+		                 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
+		                 0,
+				 NULL,
+				 NULL,
+				 gmyth_marshal_VOID__INT_STRING,
+				 G_TYPE_NONE,
+				 2,
+				 G_TYPE_INT,
+				 G_TYPE_STRING);
 
 }
 
@@ -242,16 +248,66 @@
 
 }
 
+static gint
+gmyth_monitor_handler_is_backend_message( GMythMonitorHandler *monitor,
+                        GMythStringList* strlist, gchar **back_msg_action )
+{
+        gint msg_type = GMYTH_BACKEND_NO_MESSAGE;
+        GString *back_msg = NULL;
+
+        back_msg = gmyth_string_list_get_string( strlist, 0 );
+        if ( back_msg != NULL && back_msg->str != NULL &&
+                                        strstr( back_msg->str, "BACKEND" ) != NULL )
+        {
+	        if ( monitor->backend_msgs != NULL ) 
+	        {
+	        	*back_msg_action = gmyth_string_list_get_char_array( strlist, 1 );	        	
+	        	
+	        	if ( back_msg_action != NULL )
+	        	{	        	
+	        	
+		        	if ( g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "LIVETV_CHAIN" ) ||
+		        			g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "RECORDING_LIST_CHANGE" ) ||
+		        			g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "LIVETV_WATCH" ) )
+		        	{		        	
+		        		msg_type = GMYTH_BACKEND_PROGRAM_INFO_CHANGED;
+		        	}  	
+		
+		          //g_hash_table_insert ( monitor->backend_msgs,
+		          //                       &(monitor->actual_index), *back_msg_action );
+		                                  
+	        	} // if
+	          
+	        } // if
+        }
+        
+        if ( back_msg != NULL )
+        {
+        	g_string_free( back_msg, TRUE );
+        	back_msg = NULL;        	
+        }
+
+        return msg_type;
+
+}
+
+/*
+static void
+gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gpointer message )
+{
+	//assert( message!= NULL );  
+	gmyth_debug( "DEFAULT Signal handler ( msg = %s, code = %d )\n", (gchar*)message, msg_code );
+}
+*/
+
 static void
 gmyth_monitor_handler_print( GString *str, gpointer ptr )
 {
-	gmyth_debug( "Backend message event: %s.\n", str->str );
+	gmyth_debug( "Backend message event: %s --- ", str->str );
 }
 
 static void
-//myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
-//myth_control_sock_listener( GIOChannel *io_channel, gboolean* ret )
-gmyth_monitor_handler_listener ( GMythSocket *gmyth_socket, gboolean* ret )
+gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
 {
 
   GIOStatus io_status;
@@ -260,14 +316,14 @@
   //gchar *trash = NULL;
   //GByteArray *byte_array = NULL;
   guint recv = 0;
-  ret = g_new0( gboolean, 1 );
+  gboolean *ret = g_new0( gboolean, 1 );
   *ret = TRUE;
   //gboolean ret = TRUE;
   gsize len = 0;
   
   static guint count = 0;
   
-  GIOChannel *io_channel = gmyth_socket->sd_io_ch;
+  GIOChannel *io_channel = monitor->event_sock->sd_io_ch;
   //GIOCondition condition = g_io_channel_get_buffer_condition( io_channel );
   
   GMythStringList *strlist = NULL;
@@ -287,7 +343,6 @@
   	
   	gmyth_debug ("%d - Listening on Monitor socket...!\n", count);
 	
-  	guint buffer_size = GMYTHTV_BUFFER_SIZE;
     do 
     {
     	
@@ -295,7 +350,7 @@
     	
     	strlist = gmyth_string_list_new();
 
-      len = gmyth_socket_read_stringlist( gmyth_socket, strlist );
+      len = gmyth_socket_read_stringlist( monitor->event_sock, strlist );
       
 	    if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) 
 	    { 
@@ -310,6 +365,18 @@
 	      /* 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 );
+               
+        if (back_msg_action!= NULL)
+        	g_free( back_msg_action );
+	      
 	    }
 	    
 	    if (strlist!=NULL)
@@ -319,9 +386,7 @@
 
     } while ( ( io_cond & G_IO_IN ) != 0 );
     
-    gmyth_debug ("\n\n\n[%s]\tMONITOR EVENT: Read %d bytes: %s\n\n\n", __FUNCTION__, recv, 
-    					strlist != NULL && strlist->glist != NULL ? 
-    					gmyth_string_list_get_char_array( strlist, 0 ) : "[no event data]" );
+    gmyth_debug ("[%s]\tMONITOR EVENT: Read %d bytes\n", __FUNCTION__, recv );
 
 	  g_usleep( 300 );
 	  
@@ -334,11 +399,6 @@
    	goto clean_up;   	
   }
   
-  //g_hash_table_insert( monitor->backend_msgs, (gpointer)monitor->actual_index,
-  //				  byte_array->data );
-  
-  //monitor->actual_index += recv;
-
 clean_up:
 
   if (strlist!=NULL)
@@ -371,6 +431,7 @@
   return ret;
 }    
 
+/*
 static gboolean*
 gmyth_monitor_handler_setup( GIOChannel *channel )
 {
@@ -418,6 +479,7 @@
   return ret;
   
 }
+*/
 
 gboolean 
 gmyth_monitor_handler_start (GMythMonitorHandler *monitor)
@@ -428,9 +490,9 @@
 	if (!g_thread_supported () ) 	g_thread_init (NULL);
 	
   monitor_th = g_thread_pool_new( (GFunc)gmyth_monitor_handler_listener, 
-  					monitor->event_sock, 3, TRUE, NULL );
+  					monitor, 3, TRUE, NULL );
   					
-  g_thread_pool_push( monitor_th, monitor->event_sock, NULL ); 
+  g_thread_pool_push( monitor_th, monitor, NULL ); 
   					
   //if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
   if ( monitor_th != NULL )
diff -r 8ba1ddedfa22 -r 49ba93ca4586 gmyth/src/gmyth_monitor_handler.h
--- a/gmyth/src/gmyth_monitor_handler.h	Wed Dec 06 21:50:34 2006 +0000
+++ b/gmyth/src/gmyth_monitor_handler.h	Thu Dec 07 18:39:41 2006 +0000
@@ -66,7 +66,7 @@
 	guint backend_events_handler_signal_id;
 
   /* signal default handlers */
-  void (*backend_events_handler) (GMythMonitorHandler *self);
+  void (*backend_events_handler) (GMythMonitorHandler *monitor, gint msg_code, gchar* message );
 };
 
 struct _GMythMonitorHandler