Browse Source

Switched to glade+gtk3

Dmitry Yu Okunev 4 years ago
parent
commit
690c907b49
3 changed files with 58 additions and 34 deletions
  1. 3 3
      GNUmakefile
  2. 53 29
      main.c
  3. 2 2
      oscilloscope.glade

+ 3 - 3
GNUmakefile

@@ -7,12 +7,12 @@ EXAMPLES ?= yes
 
 CSECFLAGS ?= -fstack-protector-all -Wall --param ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -fstack-check -DPARANOID -std=gnu99
 CFLAGS ?= -pipe -O2
-CFLAGS += $(CSECFLAGS) $(shell pkg-config --cflags gtk+-2.0) -flto
-DEBUGCFLAGS ?= -pipe -Wall -Werror -ggdb3 -export-dynamic -Wno-error=unused-variable -O0 -pipe $(CSECFLAGS) $(shell pkg-config --cflags gtk+-2.0)
+CFLAGS += $(CSECFLAGS) $(shell pkg-config --cflags gtk+-3.0) -flto
+DEBUGCFLAGS ?= -pipe -Wall -Werror -ggdb3 -export-dynamic -Wno-error=unused-variable -O0 -pipe $(CSECFLAGS) $(shell pkg-config --cflags gtk+-3.0)
 
 CARCHFLAGS ?= -march=native
 
-LIBS := -lm $(shell pkg-config --libs gtk+-2.0)
+LIBS := -lm $(shell pkg-config --libs gtk+-3.0)
 LDSECFLAGS ?= -Xlinker -zrelro
 LDFLAGS += $(LDSECFLAGS) -pthread -flto
 INC := $(INC)

+ 53 - 29
main.c

@@ -41,6 +41,8 @@ FILE *dump;
 #define MAX_MATH_CHANNELS 3
 #define Y_BITS 12
 
+#define GLADE_PATH "oscilloscope.glade"
+
 typedef struct {
 	uint64_t timestamp;
 	uint32_t value[MAX_REAL_CHANNELS];
@@ -172,6 +174,8 @@ dump_fetch(history_t *p)
 	(void)ts_parse;
 	p->timestamp = ts_device;
 
+	//fprintf(stderr, "%u\n", p->value[0]);
+
 	//sleep(1);
 
 	return 1;
@@ -199,10 +203,12 @@ history_flush()
 void *
 history_fetcher(void *arg)
 {
+	//fprintf(stderr, "history_fetcher\n");
+
 	while (running) {
 		//sensor_fetch(&history[0][ history_length[0]++ ]);
 		dump_fetch(&history[ history_length++ ]);
-//		printf("%lu; %u\n", history[0][ history_length[0]-1].timestamp, history[0][ history_length[0]-1].value);
+		//printf("%lu; %u\n", history[ history_length-1].timestamp, history[ history_length-1].value[0]);
 		if (history_length >= HISTORY_SIZE * 2) 
 			history_flush();
 	}
@@ -211,16 +217,21 @@ history_fetcher(void *arg)
 }
 
 static gboolean
-cb_expose (GtkWidget      *area,
-           GdkEventExpose *event,
-           gpointer       *data)
+cb_draw (GtkWidget	*area,
+         cairo_t	*cr,
+         gpointer	*data)
 {
 	int width, height;
-	cairo_t *cr;
 
-	cr	= gdk_cairo_create (event->window);
-	width	= gdk_window_get_width (event->window);
-	height	= gdk_window_get_height (event->window);
+	GdkWindow *areaGdkWindow = gtk_widget_get_window(area);
+
+	assert (areaGdkWindow != NULL);
+
+	//gtk_window_get_size (gtk_widget_get_window(area), &width, &height);
+	width	= gdk_window_get_width  (areaGdkWindow);
+	height	= gdk_window_get_height (areaGdkWindow);
+
+	//fprintf(stderr, "%i %i\n", width, height);
 
 	cairo_rectangle(cr, 0, 0, width, height);
 	cairo_set_source_rgb(cr, 0, 0, 0);
@@ -359,7 +370,6 @@ cb_expose (GtkWidget      *area,
 	cairo_line_to(cr, width, height/2);
 	cairo_stroke(cr);
 
-	cairo_destroy (cr);
 	return TRUE;
 }
 
@@ -386,6 +396,17 @@ main (int    argc,
 	char tailonly = 0;
 	//sensor_open();
 
+	history = xcalloc(HISTORY_SIZE * 2 + 1, sizeof(history_t));
+
+
+	GtkWidget *main_window,
+	          *button,
+	          *area;
+
+	GtkBuilder *builder;
+
+	gtk_init (&argc, &argv);
+
 	// Parsing arguments
 	char c;
 	while ((c = getopt (argc, argv, "i:tfC:M:")) != -1) {
@@ -412,11 +433,8 @@ main (int    argc,
 	}
 
 
-	assert (channelsNum     < MAX_REAL_CHANNELS);
-	assert (mathChannelsNum < MAX_MATH_CHANNELS);
-
-	history = xcalloc(HISTORY_SIZE * 2 + 1, sizeof(history_t));
-
+	assert ( channelsNum     < MAX_REAL_CHANNELS );
+	assert ( mathChannelsNum < MAX_MATH_CHANNELS );
 
 	dump_open(dumppath, tailonly);
 
@@ -425,25 +443,31 @@ main (int    argc,
 		return 1;
 	}
 
-	GtkWidget *main_window,
-	          *vbox,
-	          *button,
-	          *area;
+	builder = gtk_builder_new();
 
-	gtk_init (&argc, &argv);
+	GError *gerr = NULL;
+
+	if ( gtk_builder_add_from_file (builder, GLADE_PATH, &gerr) <= 0 ) {
+		fprintf(stderr, "Cannot parse file \""GLADE_PATH"\": %s\n", gerr->message);
+		return 3;
+	}
+	main_window = GTK_WIDGET ( gtk_builder_get_object (builder, "mainwindow") );
+	assert ( main_window != NULL );
 
-	//gtk_builder_add_from_file (builder, "oscilloscope.glade", NULL);
+	//main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+	//gtk_window_set_default_size (GTK_WINDOW (main_window), 800, 600);
 
-	main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-	gtk_window_set_default_size (GTK_WINDOW (main_window), 800, 600);
 	g_signal_connect (main_window, "destroy", gtk_main_quit, NULL);
-	vbox = gtk_vbox_new (FALSE, 5);
-	gtk_container_add (GTK_CONTAINER (main_window), vbox);
-	button = gtk_button_new_from_stock (GTK_STOCK_REFRESH);
-	gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
-	area = gtk_drawing_area_new ();
-	g_signal_connect (area, "expose-event", G_CALLBACK (cb_expose), NULL);
-	gtk_box_pack_start (GTK_BOX (vbox), area, TRUE, TRUE, 0);
+	//button = gtk_button_new_from_stock (GTK_STOCK_REFRESH);
+	button = GTK_WIDGET ( gtk_builder_get_object(builder, "refresh") );
+
+	assert (button != NULL);
+
+	area = GTK_WIDGET ( gtk_builder_get_object(builder, "oscillogram") );
+
+	assert (area != NULL);
+
+	g_signal_connect (area, "draw", G_CALLBACK (cb_draw), NULL);
 	g_signal_connect_swapped (button, "clicked",
 	                          G_CALLBACK (gtk_widget_queue_draw), area);
 	gtk_widget_show_all (main_window);

+ 2 - 2
oscilloscope.glade

@@ -2,10 +2,10 @@
 <!-- Generated with glade 3.18.3 -->
 <interface>
   <requires lib="gtk+" version="3.12"/>
-  <object class="GtkWindow" id="window1">
+  <object class="GtkWindow" id="mainwindow">
     <property name="can_focus">False</property>
     <child>
-      <object class="GtkGrid" id="grid1">
+      <object class="GtkGrid" id="grid">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <child>