add blend mode tests
[swfdec.git] / player / swfplay.c
blob63c47318a86cd1561bc383e0a1718235583305d2
1 /* Swfdec
2 * Copyright (C) 2006 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <gtk/gtk.h>
24 #include <math.h>
25 #include <swfdec/swfdec.h>
27 #include <swfdec-gtk/swfdec-gtk.h>
29 static GMainLoop *loop = NULL;
31 static void
32 set_title (GtkWindow *window, const SwfdecURL *url)
34 char *title = swfdec_url_format_for_display (url);
36 gtk_window_set_title (window, title);
37 g_free (title);
40 static void
41 set_playing (SwfdecGtkWidget *widget, GParamSpec *pspec, gpointer player)
43 swfdec_gtk_player_set_playing (player, TRUE);
46 static GtkWidget *
47 view_swf (SwfdecPlayer *player, gboolean use_image)
49 GtkWidget *window, *widget;
51 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
52 gtk_window_set_icon_name (GTK_WINDOW (window), "swfdec");
53 widget = swfdec_gtk_widget_new (player);
54 if (use_image)
55 swfdec_gtk_widget_set_renderer (SWFDEC_GTK_WIDGET (widget), CAIRO_SURFACE_TYPE_IMAGE);
56 gtk_container_add (GTK_CONTAINER (window), widget);
57 g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_main_loop_quit), loop);
58 gtk_widget_show_all (window);
59 g_signal_connect (widget, "notify::has-focus", G_CALLBACK (set_playing), player);
61 return window;
64 static void
65 do_fscommand (SwfdecPlayer *player, const char *command, const char *value, gpointer window)
67 if (g_str_equal (command, "quit")) {
68 g_assert (loop);
69 if (g_main_loop_is_running (loop)) {
70 gtk_widget_destroy (window);
71 g_main_loop_quit (loop);
74 /* FIXME: add more */
77 static void
78 print_trace (SwfdecPlayer *player, const char *message, gpointer unused)
80 g_print ("%s\n", message);
83 int
84 main (int argc, char *argv[])
86 int speed = 100;
87 int max_runtime = 0;
88 SwfdecPlayer *player;
89 GError *error = NULL;
90 gboolean use_image = FALSE, no_sound = FALSE;
91 gboolean trace = FALSE, no_scripts = FALSE;
92 gboolean redraws = FALSE, gc = FALSE;
93 char *variables = NULL;
94 GtkWidget *window;
95 SwfdecURL *url;
97 GOptionEntry options[] = {
98 { "always-gc", 'g', 0, G_OPTION_ARG_NONE, &gc, "run the garbage collector as often as possible", NULL },
99 { "image", 'i', 0, G_OPTION_ARG_NONE, &use_image, "use an intermediate image surface for drawing", NULL },
100 { "max-runtime", 0, 0, G_OPTION_ARG_INT, &max_runtime, "maximum time scripts run before aborting the player", "SECS" },
101 { "no-scripts", 0, 0, G_OPTION_ARG_NONE, &no_scripts, "don't execute scripts affecting the application", NULL },
102 { "no-sound", 'n', 0, G_OPTION_ARG_NONE, &no_sound, "don't play sound", NULL },
103 { "redraws", 'r', 0, G_OPTION_ARG_NONE, &redraws, "show redraw regions", NULL },
104 { "speed", 0, 0, G_OPTION_ARG_INT, &speed, "replay speed (will deactivate sound)", "PERCENT" },
105 { "trace", 't', 0, G_OPTION_ARG_NONE, &trace, "print trace output to stdout", NULL },
106 { "variables", 'v', 0, G_OPTION_ARG_STRING, &variables, "variables to pass to player", "VAR=NAME[&VAR=NAME..]" },
107 { NULL }
109 GOptionContext *ctx;
111 ctx = g_option_context_new ("");
112 g_option_context_add_main_entries (ctx, options, "options");
113 g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
114 g_option_context_parse (ctx, &argc, &argv, &error);
115 g_option_context_free (ctx);
117 if (error) {
118 g_printerr ("Error parsing command line arguments: %s\n", error->message);
119 g_error_free (error);
120 return 1;
123 swfdec_init ();
125 if (argc < 2) {
126 g_printerr ("Usage: %s [OPTIONS] filename\n", argv[0]);
127 return 1;
130 loop = g_main_loop_new (NULL, TRUE);
131 player = swfdec_gtk_player_new (NULL);
132 /* this allows the player to continue fine when running in gdb */
133 if (max_runtime >= 0)
134 swfdec_player_set_maximum_runtime (player, max_runtime * 1000);
135 if (gc)
136 g_object_set (player, "memory-until-gc", (gulong) 0, NULL);
137 if (trace)
138 g_signal_connect (player, "trace", G_CALLBACK (print_trace), NULL);
139 swfdec_gtk_player_set_speed (SWFDEC_GTK_PLAYER (player), speed / 100.);
141 if (no_sound)
142 swfdec_gtk_player_set_audio_enabled (SWFDEC_GTK_PLAYER (player), FALSE);
144 window = view_swf (player, use_image);
145 if (redraws)
146 gdk_window_set_debug_updates (TRUE);
148 if (!no_scripts)
149 g_signal_connect (player, "fscommand", G_CALLBACK (do_fscommand), window);
151 if (variables != NULL)
152 swfdec_player_set_variables (player, variables);
153 url = swfdec_url_new_from_input (argv[1]);
154 swfdec_player_set_url (player, url);
155 set_title (GTK_WINDOW (window), url);
156 swfdec_gtk_player_set_missing_plugins_window (SWFDEC_GTK_PLAYER (player),
157 window->window);
158 swfdec_url_free (url);
160 if (g_main_loop_is_running (loop))
161 g_main_loop_run (loop);
163 g_object_unref (player);
164 g_main_loop_unref (loop);
165 loop = NULL;
166 player = NULL;
167 return 0;