[maemo-commits] [maemo-commits] r18743 - in projects/haf/trunk/hildon-welcome: debian src

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Tue Jun 16 23:37:52 EEST 2009
Author: schulhof
Date: 2009-06-16 23:37:30 +0300 (Tue, 16 Jun 2009)
New Revision: 18743

Modified:
   projects/haf/trunk/hildon-welcome/debian/changelog
   projects/haf/trunk/hildon-welcome/src/main.c
Log:
Detach immediately and use a pipe to tell the parent to exit

Modified: projects/haf/trunk/hildon-welcome/debian/changelog
===================================================================
--- projects/haf/trunk/hildon-welcome/debian/changelog	2009-06-16 15:20:51 UTC (rev 18742)
+++ projects/haf/trunk/hildon-welcome/debian/changelog	2009-06-16 20:37:30 UTC (rev 18743)
@@ -1,4 +1,4 @@
-hildon-welcome (0.18-2) unstable; urgency=low
+hildon-welcome (0.19-1) unstable; urgency=low
 
   [ Gabriel Schulhof ]
   * Fork into the background when poised to play first logo.

Modified: projects/haf/trunk/hildon-welcome/src/main.c
===================================================================
--- projects/haf/trunk/hildon-welcome/src/main.c	2009-06-16 15:20:51 UTC (rev 18742)
+++ projects/haf/trunk/hildon-welcome/src/main.c	2009-06-16 20:37:30 UTC (rev 18743)
@@ -23,6 +23,7 @@
 
 #include <string.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <glib.h>
 #include <gst/interfaces/xoverlay.h>
 #include <X11/Xlib.h>
@@ -150,23 +151,8 @@
 }
 
 static void
-maybe_fork()
+wait_for_eos(GstElement *pipeline, Display *dpy, int duration, TimeoutParams *play_to, int detach_fd)
 {
-  int fork_result = -1;
-  static gboolean already_forked = FALSE;
-
-  if (!already_forked) {
-    if ((fork_result = fork())) return 0;
-    else {
-      g_debug("maybe_fork: Forked into the background with fork_result %d\n", fork_result);
-      already_forked = TRUE;
-    }
-  }
-}
-
-static void
-wait_for_eos(GstElement *pipeline, Display *dpy, int duration, TimeoutParams *play_to)
-{
   GError *err = NULL;
   char *debug = NULL;
   gboolean keep_looping = TRUE;
@@ -179,7 +165,12 @@
     switch(GST_MESSAGE_TYPE(message)) {
       case GST_MESSAGE_ASYNC_DONE:
         g_debug("wait_for_eos: Ready to play: duration = %d\n", duration);
-        maybe_fork();
+        if (detach_fd >= 0) {
+          char c = 0;
+          g_debug("wait_for_eos: Asking parent process to exit\n");
+          write (detach_fd, &c, 1);
+          close (detach_fd);
+        }
         if ((duration > 500) && !(play_to->timeout_id))
           post_eos_timeout_add(duration, pipeline, NULL, play_to);
         break;
@@ -232,7 +223,7 @@
 }
 
 static GstElement *
-play_logo(Display *dpy, char *video, char *audio, int duration)
+play_logo(Display *dpy, char *video, char *audio, int duration, int detach_fd)
 {
   GstElement* pipeline = NULL;
   GString *pipeline_str = g_string_new("");
@@ -260,7 +251,7 @@
 
     gst_element_set_state(pipeline, GST_STATE_PLAYING);
     unblank_screen();
-    wait_for_eos(pipeline, dpy, duration, &play_to);
+    wait_for_eos(pipeline, dpy, duration, &play_to, detach_fd);
 
     post_eos_timeout_remove(&kill_to);
     post_eos_timeout_remove(&play_to);
@@ -291,6 +282,47 @@
   XFlush(dpy);
 }
 
+static int
+prepare_detach_fd()
+{
+  static gboolean already_forked = FALSE;
+  int fork_result = -1;
+  int pipe_fd[2] = { -1, -1 };
+
+  if (already_forked)
+    g_warning("prepare_detach_fd: Already forked\n");
+  else {
+    if (!pipe(pipe_fd)) {
+      fork_result = fork();
+
+      if (fork_result < 0) {
+        g_warning("prepare_detach_fd: fork(2) failed\n");
+        close(pipe_fd[0]); pipe_fd[0] = -1;
+        close(pipe_fd[1]); pipe_fd[1] = -1;
+      }
+      else {
+        g_debug("prepare_detach_fd: fork(2) succeeded\n");
+        already_forked = TRUE;
+
+        if (fork_result > 0) {
+          char c;
+
+          g_debug("prepare_detach_fd: waiting for child process to signal exit\n");
+          read(pipe_fd[0], &c, 1);
+          close(pipe_fd[0]); pipe_fd[0] = -1;
+          close(pipe_fd[1]); pipe_fd[1] = -1;
+          g_debug("prepare_detach_fd: exit(2)-ing\n");
+          exit(0);
+        }
+      }
+    }
+    else
+      g_warning("prepare_detach_fd: pipe(2) failed\n");
+  }
+
+  return pipe_fd[1];
+}
+
 int
 main(int argc, char **argv)
 {
@@ -329,7 +361,7 @@
   GError *err;
   Display *display = NULL;
   char *video = NULL, *audio = NULL;
-  int duration;
+  int duration, detach_fd;
   ConfFileIterator *itr;
   GstElement *new_pipeline = NULL, *old_pipeline = NULL;
 
@@ -339,6 +371,8 @@
 
   g_log_set_default_handler(my_log_func, NULL);
 
+  detach_fd = prepare_detach_fd();
+
   ctx = g_option_context_new(NULL);
   g_option_context_add_main_entries (ctx, options, NULL);
   g_option_context_add_group (ctx, gst_init_get_option_group());
@@ -353,7 +387,7 @@
 
   if ((itr = conf_file_iterator_new())) {
     while (conf_file_iterator_get(itr, &video, &audio, &duration)) {
-      new_pipeline = play_logo(display, video, audio, duration);
+      new_pipeline = play_logo(display, video, audio, duration, detach_fd);
       g_free(video); video = NULL;
       g_free(audio); audio = NULL;
       duration = 0;

More information about the maemo-commits mailing list