2009-08-11 Chris Toshok <toshok@ximian.com>
[moon.git] / examples / animation-recorder / animation-recorder.cpp
blob21e256a51ea4ed214fe174d82aab81e6806cf0e4
1 /*
2 * Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
4 * Contact:
5 * Moonlight List (moonlight-list@lists.ximian.com)
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use,
11 * copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following
14 * conditions:
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
30 #include <runtime.h>
31 #include <clock.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <gtk/gtk.h>
36 #include <gdk/gdkkeysyms.h>
37 #include <signal.h>
38 #include <sys/syscall.h>
39 #include <sys/wait.h>
40 #include <dlfcn.h>
41 #include <gtkmozembed.h>
42 #include <nsXPCOMGlue.h>
44 static const GREVersionRange gre_version = {
45 "1.9", PR_TRUE,
46 "9.9", PR_TRUE
49 float current_time = 0.0;
50 GtkWindow *window;
51 int image_no = 0;
53 void capture_image (char *filename)
55 int x, y, w, h;
56 gtk_window_get_frame_dimensions (window, &x, &y, &w, &h);
57 gdk_window_get_origin (((GtkWidget *) window)->window, &x, &y);
58 gdk_drawable_get_size (((GtkWidget *) window)->window, &w, &h);
60 GdkWindow* root = gdk_window_foreign_new (GDK_ROOT_WINDOW ());
61 GdkPixbuf* buf = gdk_pixbuf_get_from_drawable (NULL, root, NULL, x, y, 0, 0, w, h);
62 GError* error = NULL;
64 gdk_pixbuf_save (buf, filename, "png", &error, NULL);
65 gdk_pixbuf_unref (buf);
68 gboolean increase_timer (void *data)
70 if (runtime_get_surface_list () == NULL)
71 return TRUE;
73 Surface *surface = (Surface *) runtime_get_surface_list ()->data;
75 if (surface == NULL)
76 return TRUE;
78 TimeManager *manager = surface_get_time_manager (surface);
79 ManualTimeSource *source = (ManualTimeSource *) manager->GetSource ();
81 source->SetCurrentTime (TimeSpan_FromSecondsFloat (current_time));
82 current_time += 0.04; // 25 frames per second
83 image_no++;
85 if (current_time > 5.0) {
86 printf ("ALL DONE! %d images captured\n", image_no - 1);
87 exit (0);
90 return FALSE;
93 void expose_handoff (Surface *s, TimeSpan time, void* data)
95 char *filename = g_strdup_printf ("capture_%.4d.png", image_no);
96 printf ("Capturing %.3fs to %s\n", TimeSpan_ToSecondsFloat (time), filename);
97 g_idle_add (increase_timer, NULL);
98 capture_image (filename);
99 g_free (filename);
102 gboolean setup (void* data)
104 if (runtime_get_surface_list () == NULL)
105 return TRUE;
107 Surface *surface = (Surface *) runtime_get_surface_list ()->data;
109 if (surface == NULL)
110 return TRUE;
112 TimeManager *manager = surface_get_time_manager (surface);
113 ManualTimeSource *source = (ManualTimeSource *) manager->GetSource ();
115 printf ("Setting up...\n");
116 surface->SetExposeHandoffFunc (expose_handoff, NULL);
117 g_idle_add (increase_timer, NULL);
119 return FALSE;
123 main (int argc, char **argv)
125 gtk_init (&argc, &argv);
126 runtime_init (RUNTIME_INIT_BROWSER);
128 window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
130 char xpcom_lib_path [PATH_MAX];
131 char* xpcom_dir_path;
133 GRE_GetGREPathWithProperties (&gre_version, 1, nsnull, 0, xpcom_lib_path, sizeof (xpcom_lib_path));
134 xpcom_dir_path = g_path_get_dirname (xpcom_lib_path);
136 gtk_moz_embed_set_path (xpcom_dir_path);
137 g_free (xpcom_dir_path);
139 GtkWidget *moz_embed = gtk_moz_embed_new();
140 gtk_container_add (GTK_CONTAINER (window), moz_embed);
142 gtk_widget_set_usize (moz_embed, 416, 416);
144 char *current_directory = g_get_current_dir ();
145 char *html_path = g_strdup_printf ("file://%s/test.html", current_directory);
146 gtk_moz_embed_load_url (GTK_MOZ_EMBED (moz_embed), html_path);
148 g_free (current_directory);
149 g_free (html_path);
151 gtk_widget_show_all (moz_embed);
152 gtk_widget_show_all (GTK_WIDGET (window));
153 g_timeout_add (500, setup, NULL);
155 runtime_flags_set_manual_timesource (TRUE);
156 runtime_flags_set_use_shapecache (FALSE);
157 runtime_flags_set_show_fps (FALSE);
159 gtk_main ();
161 return 0;