callbacks.c

来自「基于V4L开发的一个运动监测的程序」· C语言 代码 · 共 1,163 行 · 第 1/3 页

C
1,163
字号
{
	gdk_draw_rgb_image(widget->window,widget->style->fg_gc[GTK_STATE_NORMAL],
	           0,0,320,240,GDK_RGB_DITHER_NORMAL,ref_buf,3*320); 
  	return FALSE;
}

//=====================================================================
//=====================================================================
gboolean on_drawingarea3_expose_event  (GtkWidget       *widget,
                                        GdkEventExpose  *event,
                                        gpointer         user_data)
{
	gdk_draw_rgb_image(widget->window,widget->style->fg_gc[GTK_STATE_NORMAL],
	           0,0,320,240,GDK_RGB_DITHER_NORMAL,res_buf,3*320); 
  	return FALSE;
}

//=====================================================================
//=====================================================================
gboolean on_drawingarea4_expose_event  (GtkWidget       *widget,
                                        GdkEventExpose  *event,
                                        gpointer         user_data)
{
	gdk_draw_rgb_image(widget->window,widget->style->fg_gc[GTK_STATE_NORMAL],
	           0,0,320,50,GDK_RGB_DITHER_NORMAL,grf_buf,3*320); 

  return FALSE;
}


//=====================================================================
// we get here whenever a user has changed a setup item
// we need to activate the apply button so user knows items have changed
// all data entry items use this as the "changed" event handler
//=====================================================================
void
apply_needed                           (GtkEditable     *editable,
                                        gpointer         user_data)
{
	gnome_property_box_changed((GnomePropertyBox*) propertybox1); 		
}

//=====================================================================
// we get here when the user has clicked apply or the ok button
// in the setup windows
//=====================================================================
void
on_propertybox1_apply                  (GnomePropertyBox *gnomepropertybox,
                                        gint             arg1,
                                        gpointer         user_data)
{
  	GtkWidget *wid;
	gchar *buf;
	// arg refers to the page that has changed
	// with a final value of -1... normally, just save everything on -1
	
	if ( arg1 == -1 )
	{
		//"save all setup items here to ~/.gnome/gspy (like ini file)
		
		wid = lookup_widget(GTK_WIDGET(propertybox1),"image_dir");
		buf = gtk_entry_get_text(GTK_ENTRY(wid));
		strncpy(image_directory,buf,sizeof(image_directory));
		gnome_config_set_string("/gspy/general/image_directory", image_directory);	// file, section, key
		
		wid = lookup_widget(GTK_WIDGET(propertybox1),"img_label");
		buf = gtk_entry_get_text(GTK_ENTRY(wid));
		strncpy(image_label,buf,sizeof(image_label));
		gnome_config_set_string("/gspy/general/image_label", image_label);	// file, section, key
			
		wid = lookup_widget(GTK_WIDGET(propertybox1),"Pix_interval");
		buf = gtk_entry_get_text(GTK_ENTRY(wid));
		picture_interval = atoi(buf);
		gnome_config_set_int("/gspy/general/picture_interval", picture_interval);	// file, section, key	

		wid = lookup_widget(GTK_WIDGET(propertybox1),"save_interval");
		buf = gtk_entry_get_text(GTK_ENTRY(wid));
		periodic_save_interval = atoi(buf);
		gnome_config_set_int("/gspy/general/periodic_save_interval", periodic_save_interval);	// file, section, key	

		wid = lookup_widget(GTK_WIDGET(propertybox1),"video_src");
		buf = gtk_entry_get_text(GTK_ENTRY(wid));
		strncpy(video_device,buf,sizeof(video_device));	
		gnome_config_set_string("/gspy/video4linux/video_device", video_device);	// file, section, key
	
		wid = lookup_widget(GTK_WIDGET(propertybox1),"ref_fraction");
		buf = gtk_entry_get_text(GTK_ENTRY(wid));	
		ref_update_fraction = atof(buf);
		gnome_config_set_float("/gspy/alarms/ref_update_fraction",ref_update_fraction );	// file, section, key

		wid = lookup_widget(GTK_WIDGET(propertybox1),"alrm_threshold");
		buf = gtk_entry_get_text(GTK_ENTRY(wid));
		alarm_threshold = atoi( buf );
		gnome_config_set_int("/gspy/alarms/alarm_threshold",alarm_threshold );	// file, section, key	
			
		wid = lookup_widget(GTK_WIDGET(propertybox1),"sig_pix_thres");
		buf= gtk_entry_get_text(GTK_ENTRY(wid));
		sig_pix_threshold = atof( buf );
		gnome_config_set_float("/gspy/alarms/sig_pix_threshold", sig_pix_threshold);	// file, section, key	

		wid = lookup_widget(GTK_WIDGET(propertybox1),"plt_scaler");
		buf = gtk_entry_get_text(GTK_ENTRY(wid));
		graph_scale = atof( buf );
		gnome_config_set_float("/gspy/alarms/graph_scale", graph_scale);	// file, section, key		

		wid = lookup_widget(GTK_WIDGET(gnomepropertybox),"beep_on_alrm");
		beep_on_alarm = (GTK_TOGGLE_BUTTON (wid)->active);
		gnome_config_set_bool("/gspy/alarms/beep_on_alarm",beep_on_alarm );	// file, section, key
        
        wid = lookup_widget(GTK_WIDGET(gnomepropertybox),"show_target_box");
		show_target_box = (GTK_TOGGLE_BUTTON (wid)->active);
		gnome_config_set_bool("/gspy/alarms/show_target_box",show_target_box );	// file, section, key

		wid = lookup_widget(GTK_WIDGET(propertybox1),"alarm_command");
		buf = gtk_entry_get_text(GTK_ENTRY(wid));
		strncpy(alarm_command,buf,sizeof(alarm_command));

        // filename substitution works better with an extra "token"
        if(strlen(alarm_command) < (sizeof(alarm_command)-2))
            strcat(alarm_command," ");
            	
		gnome_config_set_string("/gspy/alarms/alarm_command", alarm_command);	// file, section, key
	
        
		// force out stuff to disk .. yea I'm paranoid
		gnome_config_sync();	
	
	}
}

//=====================================================================
// restore all the non volatile preferences.. if they have never been
// saved or the config file got deleted, program should be close to 
// on the default compile values.
//=====================================================================
void restore_setup_preferences( void )
{
	gchar 	*var;
	gint 	def;
	
	if (!gnome_config_has_section("/gspy/general/"))
	{		
 		GtkWidget * mbox;
   		mbox = gnome_message_box_new(
			_("Setup File Not Found... \n"
              "A default setup file will be created.\n"
              "Please check setup preferences"), 
            GNOME_MESSAGE_BOX_INFO,
            GNOME_STOCK_BUTTON_OK, 
            NULL);
   		gtk_widget_show (mbox);
	}

	var = gnome_config_get_string_with_default(
	       "/gspy/general/image_directory=./",&def);
	strncpy(image_directory,var,sizeof(image_directory));
	g_free(var);


	var = gnome_config_get_string_with_default(
	      	"/gspy/general/image_label=Room 204 %Y-%m-%d %H:%M:%S",&def);
	strncpy(image_label,var,sizeof(image_label));
	g_free(var);	

	picture_interval = gnome_config_get_int_with_default(
		"/gspy/general/picture_interval=2",&def);

	periodic_save_interval = gnome_config_get_int_with_default(
		"/gspy/general/periodic_save_interval=300",&def);

	var = gnome_config_get_string_with_default(
		"/gspy/video4linux/video_device=/dev/video0",&def);
	strncpy(video_device,var,sizeof(video_device));		
	g_free(var);

	ref_update_fraction = gnome_config_get_float_with_default(
		"/gspy/alarms/ref_update_fraction=0.2",&def);
		
	alarm_threshold = gnome_config_get_int_with_default(
		"/gspy/alarms/alarm_threshold=70",&def);

	sig_pix_threshold = gnome_config_get_float_with_default(
		"/gspy/alarms/sig_pix_threshold=25.0",&def);

	graph_scale = gnome_config_get_float_with_default(
		"/gspy/alarms/graph_scale=1.0",&def);

	beep_on_alarm = gnome_config_get_bool_with_default(
		"/gspy/alarms/beep_on_alarm=1",&def);
        
    show_target_box = gnome_config_get_bool_with_default(
		"/gspy/alarms/show_target_box=1",&def);	

	var = gnome_config_get_string_with_default(
		"/gspy/alarms/alarm_command= ls -l %f% &",&def);
	strncpy(alarm_command,var,sizeof(alarm_command));		
	g_free(var);


}



//======================================================================
//	Signal Handler for SIGCHILD signals used when forking to generate 
//  mpg files.
//======================================================================
void sigchld_handler(int num)
{
	sigset_t set, oldset;
	pid_t pid;
	int status, exitstatus;

	/* block other incoming SIGCHLD signals */
	sigemptyset(&set);
	sigaddset(&set, SIGCHLD);
	sigprocmask(SIG_BLOCK, &set, &oldset);

	/* wait for child */
	while((pid = waitpid((pid_t)-1, &status, WNOHANG)) > 0)
  	{
		if(WIFEXITED(status))
	  	{
			exitstatus = WEXITSTATUS(status);

			fprintf(stderr, 
				"Parent: child exited, pid = %d, exit status = %d\n", 
				(int)pid, exitstatus);
	  	}
		else if(WIFSIGNALED(status))
	  	{
			exitstatus = WTERMSIG(status);

			fprintf(stderr,
				"Parent: child terminated by signal %d, pid = %d\n",
				exitstatus, (int)pid);
	  	}
		else if(WIFSTOPPED(status))
	  	{
			exitstatus = WSTOPSIG(status);

			fprintf(stderr,
				"Parent: child stopped by signal %d, pid = %d\n",
				exitstatus, (int)pid);
	  	}
		else
	  	{
			fprintf(stderr,
				"Parent: child exited magically, pid = %d\n",
				(int)pid);
	  	}
	}

	/* re-install the signal handler (some systems need this) */
	signal(SIGCHLD, sigchld_handler);

	/* and unblock it */
	sigemptyset(&set);
	sigaddset(&set, SIGCHLD);
	sigprocmask(SIG_UNBLOCK, &set, &oldset);
}



// helper function for selecting files from directory
static int one(const struct dirent *entry)
{
	if (strstr(entry->d_name,".jpg"))
		return 1;
	else
		return 0;
}

void make_mpeg_part2(GtkFileSelection *selector, gpointer user_data) 
{
  	pid_t pid;
    struct dirent **eps;
    int n;

    selected_filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION(file_selector));
	g_print(_("user selected directory %s\n"),selected_filename);

  	pid = fork();

  	if(pid == -1)
	{
	  	/* ouch, fork() failed */
	  	perror("fork");
	  	exit(-1);
	}
  	else if(pid == 0)
	{
		FILE *fp=NULL;
		
	  	/* child */
	  	fprintf(stderr, "Child: pid = %d\n", (int)getpid());
		// we need to build the config file for the mpeg encoder
		// and then call the mpeg encoder
       	n = scandir(selected_filename, &eps, one, alphasort);
		if (n == 0)
		{
			g_print(_("Child: No jpg files found in directory selected\n"));
			_exit(-1);
		}
       	if (n > 0)
        {
			int cnt;
			g_print(_("Child: found %d files \n"),n);
			fp=fopen("video.params", "w");
			if (!fp) 
			{	
				g_print(_("Child: Failed to open/create video.params file\n"));
				_exit(-1);
			}
			fprintf(fp,
			"PATTERN IBBPBBPBBPBBPBBP\n"
			"OUTPUT %svideo.mpg\n"
			"BASE_FILE_FORMAT JPEG\n"
			"INPUT_CONVERT *\n"
			"GOP_SIZE 30\n"
			"SLICES_PER_FRAME 1\n"
			"INPUT_DIR %s\n"
			"INPUT\n",selected_filename,selected_filename);
			for (cnt = 0; cnt < n; ++cnt)
				fprintf(fp,"%s\n",eps[cnt]->d_name);
				
			fprintf(fp,
				"END_INPUT\n"
				"PIXEL HALF\n"
				"RANGE 10\n"
				"PSEARCH_ALG LOGARITHMIC\n"
				"BSEARCH_ALG CROSS2\n"
				"IQSCALE 8\n"
				"PQSCALE 10\n"
				"BQSCALE 25\n"
				"FORCE_ENCODE_LAST_FRAME\n"
				"REFERENCE_FRAME ORIGINAL\n"
				"FRAME_RATE 25\n");
			fclose(fp);
			nice(5);
			g_print("Child: executing mpeg encoder...\n");
			execlp("mpeg_encode", "mpeg_encode", "video.params", 0);
	  		/* if exec() returns, there is something wrong */
			perror("Child: Failed to start mpeg_encode");
        }
       	else
         	perror(_("Child: Couldn't open the directory"));


	  	/* exit child. note the use of _exit() instead of exit() */
	  	_exit(-1);
	}
  	else
	{
	  	/* parent */
	  	fprintf(stderr, "Parent: forked a child with pid = %d\n", (int)pid);
	}
}


void on_makempeg1_activate(GtkMenuItem *menuitem, gpointer user_data)
{
	g_print(_("make me an mpeg\n"));
   /* Create the selector */
    
    file_selector = gtk_file_selection_new(_("Please select a directory of .jpg files"));
    
    gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(file_selector)->ok_button),
                            "clicked", GTK_SIGNAL_FUNC (make_mpeg_part2), NULL);
                            
    /* Ensure that the dialog box is destroyed when the user clicks a button. */
    
    gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(file_selector)->ok_button),
                                           "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
                                           (gpointer) file_selector);

    gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(file_selector)->cancel_button),
                                           "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
                                           (gpointer) file_selector);

	gtk_file_selection_set_filename((GtkFileSelection *)file_selector,(const gchar *)image_directory);    
    /* Display that dialog */   
    gtk_widget_show (file_selector);	
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?