+static void
+webcollage_atexit (void)
+{
+ int i = 0;
+
+ if (all_states[0] && all_states[0]->verbose_p)
+ fprintf (stderr, "webcollage: atexit handler\n");
+
+ while (all_states[i]) {
+ state *st = all_states[i];
+ if (st->pid) {
+ if (st->verbose_p)
+ fprintf (stderr, "webcollage: kill %d\n", st->pid);
+ if (kill (st->pid, SIGTERM) < 0) {
+ fprintf (stderr, "webcollage: kill (%d, TERM): ", st->pid);
+ perror ("webcollage: kill");
+ }
+ st->pid = 0;
+ }
+ all_states[i] = 0;
+ i++;
+ }
+}
+
+
+static void
+signal_handler (int sig)
+{
+ if (all_states[0] && all_states[0]->verbose_p)
+ fprintf (stderr, "webcollage: signal %d\n", sig);
+ webcollage_atexit ();
+ exit (sig);
+}
+
+
+/* This is important because OSX doesn't actually kill the screen savers!
+ It just sends them a [ScreenSaverView stopAnimation] method. Were
+ they to actually exit, the resultant SIGPIPE should reap the children,
+ but instead, we need to do it here.
+
+ On top of that, there's an atexit() handler because otherwise the
+ inferior perl script process was failing to die when SaverTester or
+ System Preferences exited. I don't pretend to understand.
+
+ It still fails to clean up when I hit the stop button in Xcode.
+ WTF.
+ */