Browse Source

Added end-user errors

Martijn Braam 4 years ago
parent
commit
35770cfa3d
3 changed files with 105 additions and 17 deletions
  1. 6 1
      camera.css
  2. 59 5
      camera.glade
  3. 40 11
      main.c

+ 6 - 1
camera.css

@@ -1,3 +1,8 @@
 .black {
     background: #000000;
-}
+}
+
+.errorbox {
+    background: #dd0000;
+    color: #ffffff;
+}

+ 59 - 5
camera.glade

@@ -55,11 +55,7 @@
                     <property name="receives-default">True</property>
                     <property name="always-show-image">True</property>
                     <child>
-                      <object class="GtkImage">
-                        <property name="visible">True</property>
-                        <property name="can-focus">False</property>
-                        <property name="stock">gtk-cdrom</property>
-                      </object>
+                      <placeholder/>
                     </child>
                     <style>
                       <class name="suggested-action"/>
@@ -158,9 +154,67 @@
                 <property name="expand">False</property>
                 <property name="fill">True</property>
                 <property name="padding">10</property>
+                <property name="pack-type">end</property>
                 <property name="position">1</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkBox" id="error_box">
+                <property name="can-focus">False</property>
+                <child>
+                  <object class="GtkBox">
+                    <property name="visible">True</property>
+                    <property name="can-focus">False</property>
+                    <property name="margin-start">10</property>
+                    <property name="margin-end">10</property>
+                    <property name="margin-top">10</property>
+                    <property name="margin-bottom">10</property>
+                    <property name="spacing">10</property>
+                    <child>
+                      <object class="GtkLabel" id="error_message">
+                        <property name="visible">True</property>
+                        <property name="can-focus">False</property>
+                        <property name="halign">start</property>
+                        <property name="label" translatable="yes">No error</property>
+                      </object>
+                      <packing>
+                        <property name="expand">True</property>
+                        <property name="fill">True</property>
+                        <property name="position">0</property>
+                      </packing>
+                    </child>
+                    <child>
+                      <object class="GtkButton" id="error_close">
+                        <property name="label">gtk-close</property>
+                        <property name="visible">True</property>
+                        <property name="can-focus">True</property>
+                        <property name="receives-default">True</property>
+                        <property name="use-stock">True</property>
+                        <property name="always-show-image">True</property>
+                      </object>
+                      <packing>
+                        <property name="expand">False</property>
+                        <property name="fill">True</property>
+                        <property name="position">1</property>
+                      </packing>
+                    </child>
+                  </object>
+                  <packing>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <style>
+                  <class name="errorbox"/>
+                </style>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">2</property>
+              </packing>
+            </child>
           </object>
           <packing>
             <property name="name">page0</property>

+ 40 - 11
main.c

@@ -75,6 +75,8 @@ static int preview_height = -1;
 
 // Widgets
 GtkWidget *preview;
+GtkWidget *error_box;
+GtkWidget *error_message;
 
 static int
 xioctl(int fd, int request, void *arg)
@@ -93,6 +95,13 @@ errno_exit(const char *s)
 	exit(EXIT_FAILURE);
 }
 
+static void
+show_error(const char *s)
+{
+	gtk_label_set_text(GTK_LABEL(error_message), s);
+	gtk_widget_show(error_box);
+}
+
 static void
 start_capturing(int fd)
 {
@@ -248,7 +257,7 @@ init_sensor(char *fn, int width, int height, int mbus, int rate)
 	current_fd = fd;
 }
 
-static void
+static int
 init_device(int fd)
 {
 	struct v4l2_capability cap;
@@ -311,9 +320,11 @@ init_device(int fd)
 		fmt.fmt.pix.field = V4L2_FIELD_ANY;
 
 		if (xioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
-			errno_exit("VIDIOC_S_FMT");
+			g_printerr("VIDIOC_S_FMT failed");
+			show_error("Could not set camera mode");
+			return -1;
 		}
-
+		
 		g_printerr("Driver returned %dx%d fmt %d\n",
 			fmt.fmt.pix.width, fmt.fmt.pix.height,
 			fmt.fmt.pix.pixelformat);
@@ -345,6 +356,7 @@ init_device(int fd)
 	}
 
 	init_mmap(fd);
+	return 0;
 }
 
 static void
@@ -803,6 +815,12 @@ on_shutter_clicked(GtkWidget *widget, gpointer user_data)
 	capture = 1;
 }
 
+void
+on_error_close_clicked(GtkWidget *widget, gpointer user_data)
+{
+	gtk_widget_hide(error_box);
+}
+
 void
 on_camera_switch_clicked(GtkWidget *widget, gpointer user_data)
 {
@@ -815,8 +833,7 @@ on_camera_switch_clicked(GtkWidget *widget, gpointer user_data)
 		setup_rear();
 		current_is_rear = 1;
 	}
-	printf("close() = %d\n", close(video_fd));
-	printf("Opening %s again\n", dev_name);
+	close(video_fd);
 	video_fd = open(dev_name, O_RDWR);
 	if (video_fd == -1) {
 		g_printerr("Error opening video device: %s\n", dev_name);
@@ -853,9 +870,13 @@ main(int argc, char *argv[])
 	GtkWidget *shutter = GTK_WIDGET(gtk_builder_get_object(builder, "shutter"));
 	GtkWidget *switch_btn = GTK_WIDGET(gtk_builder_get_object(builder, "switch_camera"));
 	GtkWidget *settings_btn = GTK_WIDGET(gtk_builder_get_object(builder, "settings"));
+	GtkWidget *error_close = GTK_WIDGET(gtk_builder_get_object(builder, "error_close"));
 	preview = GTK_WIDGET(gtk_builder_get_object(builder, "preview"));
+	error_box = GTK_WIDGET(gtk_builder_get_object(builder, "error_box"));
+	error_message = GTK_WIDGET(gtk_builder_get_object(builder, "error_message"));
 	g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
 	g_signal_connect(shutter, "clicked", G_CALLBACK(on_shutter_clicked), NULL);
+	g_signal_connect(error_close, "clicked", G_CALLBACK(on_error_close_clicked), NULL);
 	g_signal_connect(switch_btn, "clicked", G_CALLBACK(on_camera_switch_clicked), NULL);
 	g_signal_connect(preview, "draw", G_CALLBACK(preview_draw), NULL);
 	g_signal_connect(preview, "configure-event", G_CALLBACK(preview_configure), NULL);
@@ -867,6 +888,10 @@ main(int argc, char *argv[])
 		gtk_css_provider_load_from_path(provider, "/usr/share/megapixels/ui/camera.css", NULL);
 	}
 	GtkStyleContext *context = gtk_widget_get_style_context(preview_box);
+	gtk_style_context_add_provider(context,
+		GTK_STYLE_PROVIDER(provider),
+		GTK_STYLE_PROVIDER_PRIORITY_USER);
+	context = gtk_widget_get_style_context(error_box);
 	gtk_style_context_add_provider(context,
 		GTK_STYLE_PROVIDER(provider),
 		GTK_STYLE_PROVIDER_PRIORITY_USER);
@@ -882,29 +907,33 @@ main(int argc, char *argv[])
 		g_printerr("Could not parse config file\n");
 		return 1;
 	}
-
 	if (find_media_fd() == -1) {
 		g_printerr("Could not find the media node\n");
-		return 1;
+		show_error("Could not find the media node");
+		goto failed;
 	}
 	if (find_cameras() == -1) {
 		g_printerr("Could not find the cameras\n");
-		return 1;
+		show_error("Could not find the cameras");
+		goto failed;
 	}
 	setup_rear();
 
 	int fd = open(dev_name, O_RDWR);
 	if (fd == -1) {
 		g_printerr("Error opening video device: %s\n", dev_name);
-		return 1;
+		show_error("Error opening the video device");
+		goto failed;
 	}
 
 	video_fd = fd;
 
-	init_device(fd);
+	if(init_device(fd) < 0){
+		goto failed;
+	}
 	start_capturing(fd);
 
-	// Get a new frame every 34ms ~30fps
+failed:
 	printf("window show\n");
 	gtk_widget_show(window);
 	g_idle_add((GSourceFunc)get_frame, NULL);