| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifdef HAVE_CONFIG_H |
| 19 |
# include "config.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <glib/gi18n.h> |
| 23 |
|
| 24 |
#include "byzanzsession.h" |
| 25 |
|
| 26 |
static int duration = 10; |
| 27 |
static int delay = 1; |
| 28 |
static gboolean cursor = FALSE; |
| 29 |
static gboolean audio = FALSE; |
| 30 |
static gboolean verbose = FALSE; |
| 31 |
static char *exec = NULL; |
| 32 |
static cairo_rectangle_int_t area = { 0, 0, G_MAXINT / 2, G_MAXINT / 2 }; |
| 33 |
|
| 34 |
static GOptionEntry entries[] = |
| 35 |
{ |
| 36 |
{ "duration", 'd', 0, G_OPTION_ARG_INT, &duration, N_("Duration of animation (default: 10 seconds)"), N_("SECS") }, |
| 37 |
{ "exec", 'e', 0, G_OPTION_ARG_STRING, &exec, N_("Command to execute and time"), N_("COMMAND") }, |
| 38 |
{ "delay", 0, 0, G_OPTION_ARG_INT, &delay, N_("Delay before start (default: 1 second)"), N_("SECS") }, |
| 39 |
{ "cursor", 'c', 0, G_OPTION_ARG_NONE, &cursor, N_("Record mouse cursor"), NULL }, |
| 40 |
{ "audio", 'a', 0, G_OPTION_ARG_NONE, &audio, N_("Record audio"), NULL }, |
| 41 |
{ "x", 'x', 0, G_OPTION_ARG_INT, &area.x, N_("X coordinate of rectangle to record"), N_("PIXEL") }, |
| 42 |
{ "y", 'y', 0, G_OPTION_ARG_INT, &area.y, N_("Y coordinate of rectangle to record"), N_("PIXEL") }, |
| 43 |
{ "width", 'w', 0, G_OPTION_ARG_INT, &area.width, N_("Width of recording rectangle"), N_("PIXEL") }, |
| 44 |
{ "height", 'h', 0, G_OPTION_ARG_INT, &area.height, N_("Height of recording rectangle"), N_("PIXEL") }, |
| 45 |
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, N_("Be verbose"), NULL }, |
| 46 |
{ NULL } |
| 47 |
}; |
| 48 |
|
| 49 |
static void |
| 50 |
verbose_print (const gchar *format, ...) G_GNUC_PRINTF (1, 2); |
| 51 |
static void |
| 52 |
verbose_print (const gchar *format, ...) |
| 53 |
{ |
| 54 |
gchar *buffer; |
| 55 |
va_list args; |
| 56 |
|
| 57 |
if (!verbose) |
| 58 |
return; |
| 59 |
|
| 60 |
va_start (args, format); |
| 61 |
buffer = g_strdup_vprintf (format, args); |
| 62 |
va_end (args); |
| 63 |
g_print ("%s", buffer); |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
usage (void) |
| 68 |
{ |
| 69 |
g_print (_("usage: %s [OPTIONS] filename\n"), g_get_prgname ()); |
| 70 |
g_print (_(" %s --help\n"), g_get_prgname ()); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
session_notify_cb (ByzanzSession *session, GParamSpec *pspec, gpointer unused) |
| 75 |
{ |
| 76 |
const GError *error = byzanz_session_get_error (session); |
| 77 |
|
| 78 |
if (g_str_equal (pspec->name, "error")) { |
| 79 |
g_print (_("Error during recording: %s\n"), error->message); |
| 80 |
gtk_main_quit (); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if (!byzanz_session_is_encoding (session)) { |
| 85 |
verbose_print (_("Recording done.\n")); |
| 86 |
gtk_main_quit (); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
static gboolean |
| 91 |
stop_recording (gpointer session) |
| 92 |
{ |
| 93 |
verbose_print (_("Recording completed. Finishing encoding...\n")); |
| 94 |
byzanz_session_stop (session); |
| 95 |
|
| 96 |
return FALSE; |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
child_exited (GPid pid, |
| 101 |
gint status, |
| 102 |
gpointer session) |
| 103 |
{ |
| 104 |
if (status != 0) |
| 105 |
verbose_print (_("Child exited with error %d\n"), status); |
| 106 |
|
| 107 |
stop_recording (session); |
| 108 |
} |
| 109 |
|
| 110 |
static gboolean |
| 111 |
start_recording (gpointer session) |
| 112 |
{ |
| 113 |
if (exec) { |
| 114 |
GError *error = NULL; |
| 115 |
GPid pid; |
| 116 |
char **args; |
| 117 |
|
| 118 |
if (!g_shell_parse_argv (exec, NULL, &args, &error)) { |
| 119 |
g_print (_("Invalid exec command: %s\n"), error->message); |
| 120 |
g_error_free (error); |
| 121 |
gtk_main_quit (); |
| 122 |
return FALSE; |
| 123 |
} |
| 124 |
|
| 125 |
if (!g_spawn_async (NULL, args, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, |
| 126 |
NULL, NULL, &pid, &error)) { |
| 127 |
g_print (_("Failed to execute: %s\n"), error->message); |
| 128 |
g_error_free (error); |
| 129 |
gtk_main_quit (); |
| 130 |
return FALSE; |
| 131 |
} |
| 132 |
|
| 133 |
verbose_print (_("Recording starts. Will record until child exits...\n")); |
| 134 |
g_child_watch_add (pid, child_exited, session); |
| 135 |
} else { |
| 136 |
verbose_print (ngettext("Recording starts. Will record %d second...\n", |
| 137 |
"Recording starts. Will record %d seconds...\n", |
| 138 |
duration / 1000), |
| 139 |
duration / 1000); |
| 140 |
g_timeout_add (duration, stop_recording, session); |
| 141 |
} |
| 142 |
byzanz_session_start (session); |
| 143 |
|
| 144 |
return FALSE; |
| 145 |
} |
| 146 |
|
| 147 |
static gboolean |
| 148 |
clamp_to_window (cairo_rectangle_int_t *out, GdkWindow *window, cairo_rectangle_int_t *in) |
| 149 |
{ |
| 150 |
cairo_rectangle_int_t window_area = { 0, }; |
| 151 |
|
| 152 |
window_area.width = gdk_window_get_width (window); |
| 153 |
window_area.height = gdk_window_get_height (window); |
| 154 |
return gdk_rectangle_intersect ((GdkRectangle *) in, |
| 155 |
(GdkRectangle *) &window_area, |
| 156 |
(GdkRectangle *) in); |
| 157 |
} |
| 158 |
|
| 159 |
int |
| 160 |
main (int argc, char **argv) |
| 161 |
{ |
| 162 |
ByzanzSession *rec; |
| 163 |
GOptionContext* context; |
| 164 |
GError *error = NULL; |
| 165 |
GFile *file; |
| 166 |
|
| 167 |
g_set_prgname (argv[0]); |
| 168 |
#ifdef GETTEXT_PACKAGE |
| 169 |
bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); |
| 170 |
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 171 |
textdomain (GETTEXT_PACKAGE); |
| 172 |
#endif |
| 173 |
|
| 174 |
context = g_option_context_new (_("record your current desktop session")); |
| 175 |
#ifdef GETTEXT_PACKAGE |
| 176 |
g_option_context_set_translation_domain(context, GETTEXT_PACKAGE); |
| 177 |
#endif |
| 178 |
|
| 179 |
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); |
| 180 |
g_option_context_add_group (context, gtk_get_option_group (TRUE)); |
| 181 |
if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 182 |
g_print (_("Wrong option: %s\n"), error->message); |
| 183 |
usage (); |
| 184 |
return 1; |
| 185 |
} |
| 186 |
if (argc != 2) { |
| 187 |
usage (); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
if (!clamp_to_window (&area, gdk_get_default_root_window (), &area)) { |
| 191 |
g_print (_("Given area is not inside desktop.\n")); |
| 192 |
return 1; |
| 193 |
} |
| 194 |
file = g_file_new_for_commandline_arg (argv[1]); |
| 195 |
rec = byzanz_session_new (file, byzanz_encoder_get_type_from_file (file), |
| 196 |
gdk_get_default_root_window (), &area, cursor, audio); |
| 197 |
g_object_unref (file); |
| 198 |
g_signal_connect (rec, "notify", G_CALLBACK (session_notify_cb), NULL); |
| 199 |
|
| 200 |
delay = MAX (delay, 1); |
| 201 |
delay = (delay - 1) * 1000; |
| 202 |
duration = MAX (duration, 0); |
| 203 |
duration *= 1000; |
| 204 |
g_timeout_add (delay, start_recording, rec); |
| 205 |
|
| 206 |
gtk_main (); |
| 207 |
|
| 208 |
g_object_unref (rec); |
| 209 |
return 0; |
| 210 |
} |