1 /* recanim, Copyright (c) 2014-2015 Jamie Zawinski <jwz@jwz.org>
2 * Record animation frames of the running screenhack.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
15 #endif /* HAVE_CONFIG_H */
20 # else /* !HAVE_COCOA -- real Xlib */
23 # endif /* !HAVE_COCOA */
26 # endif /* HAVE_JWZGLES */
29 #ifdef HAVE_GDK_PIXBUF
31 # include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
32 # else /* !HAVE_GTK2 */
33 # include <gdk-pixbuf/gdk-pixbuf-xlib.h>
34 # endif /* !HAVE_GTK2 */
35 #endif /* HAVE_GDK_PIXBUF */
38 #include <sys/types.h>
40 #include "screenhackI.h"
43 struct record_anim_state {
48 XWindowAttributes xgwa;
62 screenhack_record_anim_init (Screen *screen, Window window, int target_frames)
64 Display *dpy = DisplayOfScreen (screen);
65 record_anim_state *st;
71 if (target_frames <= 0) return 0;
73 st = (record_anim_state *) calloc (1, sizeof(*st));
77 st->target_frames = target_frames;
79 st->fade_frames = 30 * 1.5;
81 if (st->fade_frames >= (st->target_frames / 2) - 30)
84 # ifdef HAVE_GDK_PIXBUF
88 unsigned int w, h, d, bw;
89 XGetGeometry (dpy, window, &root, &x, &y, &w, &h, &bw, &d);
90 gdk_pixbuf_xlib_init_with_depth (dpy, screen_number (screen), d);
93 # if !GLIB_CHECK_VERSION(2, 36 ,0)
96 # else /* !HAVE_GTK2 */
97 xlib_rgb_init (dpy, screen);
98 # endif /* !HAVE_GTK2 */
100 # else /* !HAVE_GDK_PIXBUF */
101 # error GDK_PIXBUF is required
102 # endif /* !HAVE_GDK_PIXBUF */
104 XGetWindowAttributes (dpy, st->window, &st->xgwa);
108 st->data = (char *) calloc (st->xgwa.width, st->xgwa.height * 3);
109 st->data2 = (char *) calloc (st->xgwa.width, st->xgwa.height * 3);
113 st->gc = XCreateGC (dpy, st->window, 0, &gcv);
114 st->p = XCreatePixmap (dpy, st->window,
115 st->xgwa.width, st->xgwa.height, st->xgwa.depth);
116 st->img = XCreateImage (dpy, st->xgwa.visual, st->xgwa.depth, ZPixmap,
117 0, 0, st->xgwa.width, st->xgwa.height, 8, 0);
118 st->img->data = (char *) calloc (st->img->height, st->img->bytes_per_line);
119 # endif /* !USE_GL */
123 XFetchName (dpy, st->window, &st->title);
124 # endif /* !HAVE_COCOA */
130 /* Fade to black. Assumes data is 3-byte packed.
133 fade_frame (record_anim_state *st, unsigned char *data, double ratio)
136 int w = st->xgwa.width;
137 int h = st->xgwa.height;
138 unsigned char *s = data;
139 for (y = 0; y < h; y++)
140 for (x = 0; x < w; x++)
141 for (i = 0; i < 3; i++)
147 screenhack_record_anim (record_anim_state *st)
149 int bytes_per_line = st->xgwa.width * 3;
153 Display *dpy = DisplayOfScreen (st->screen);
154 char *data = (char *) st->img->data;
156 /* Under XQuartz we can't just do XGetImage on the Window, we have to
157 go through an intermediate Pixmap first. I don't understand why.
158 Also, the fucking resize handle shows up as black. God dammit.
159 A workaround for that is to temporarily remove /opt/X11/bin/quartz-wm
161 XCopyArea (dpy, st->window, st->p, st->gc, 0, 0,
162 st->xgwa.width, st->xgwa.height, 0, 0);
163 XGetSubImage (dpy, st->p, 0, 0, st->xgwa.width, st->xgwa.height,
164 ~0L, ZPixmap, st->img, 0, 0);
166 /* Convert BGRA to RGB */
168 const char *in = st->img->data;
169 char *out = st->img->data;
171 int w = st->img->width;
172 int h = st->img->height;
173 for (y = 0; y < h; y++)
175 const char *in2 = in;
176 for (x = 0; x < w; x++)
183 in += st->img->bytes_per_line;
188 char *data = st->data2;
192 # undef glReadPixels /* Kludge -- unimplemented in the GLES compat layer */
195 /* First OpenGL frame tends to be random data like a shot of my desktop,
196 since it is the front buffer when we were drawing in the back buffer.
198 /* glDrawBuffer (GL_BACK); */
199 if (st->frame_count != 0)
200 glReadPixels (0, 0, st->xgwa.width, st->xgwa.height,
201 GL_RGB, GL_UNSIGNED_BYTE, st->data);
203 /* Flip vertically */
204 for (y = 0; y < st->xgwa.height; y++)
205 memcpy (data + bytes_per_line * y,
206 st->data + bytes_per_line * (st->xgwa.height - y - 1),
212 if (st->frame_count < st->fade_frames)
213 fade_frame (st, (unsigned char *) data,
214 (double) st->frame_count / st->fade_frames);
215 else if (st->frame_count >= st->target_frames - st->fade_frames)
216 fade_frame (st, (unsigned char *) data,
217 (double) (st->target_frames - st->frame_count - 1) /
220 # ifdef HAVE_GDK_PIXBUF
222 const char *type = "png";
227 pixbuf = gdk_pixbuf_new_from_data ((guchar *) data,
228 GDK_COLORSPACE_RGB, False, /* alpha */
229 8, /* bits per sample */
230 st->xgwa.width, st->xgwa.height,
234 sprintf (fn, "%s-%06d.%s", progname, st->frame_count, type);
235 gdk_pixbuf_save (pixbuf, fn, type, &error, NULL);
239 fprintf (stderr, "%s: %s: %s\n", progname, fn, error->message);
243 g_object_unref (pixbuf);
245 # else /* !HAVE_GDK_PIXBUF */
246 # error GDK_PIXBUF is required
247 # endif /* !HAVE_GDK_PIXBUF */
250 { /* Put percent done in window title */
251 int pct = 100 * (st->frame_count + 1) / st->target_frames;
252 if (pct != st->pct && st->title)
254 Display *dpy = DisplayOfScreen (st->screen);
255 char *t2 = (char *) malloc (strlen(st->title) + 20);
256 sprintf (t2, "%s: %d%%", st->title, pct);
257 XStoreName (dpy, st->window, t2);
263 # endif /* !HAVE_COCOA */
265 if (++st->frame_count >= st->target_frames)
266 screenhack_record_anim_free (st);
271 screenhack_record_anim_free (record_anim_state *st)
274 Display *dpy = DisplayOfScreen (st->screen);
275 # endif /* !USE_GL */
279 const char *type = "png";
282 const char *soundtrack = 0;
284 fprintf (stderr, "%s: wrote %d frames\n", progname, st->frame_count);
290 free (st->img->data);
292 XDestroyImage (st->img);
293 XFreeGC (dpy, st->gc);
294 XFreePixmap (dpy, st->p);
295 # endif /* !USE_GL */
297 sprintf (fn, "%s.%s", progname, "mp4");
300 # define ST "images/drives-200.mp3"
302 if (stat (soundtrack, &s)) soundtrack = 0;
303 if (! soundtrack) soundtrack = "../" ST;
304 if (stat (soundtrack, &s)) soundtrack = 0;
305 if (! soundtrack) soundtrack = "../../" ST;
306 if (stat (soundtrack, &s)) soundtrack = 0;
310 " -framerate 30" /* rate of input: must be before -i */
312 " -r 30", /* rate of output: must be after -i */
315 sprintf (cmd + strlen(cmd),
316 " -i '%s' -map 0:v:0 -map 1:a:0 -acodec libfaac",
318 sprintf (cmd + strlen(cmd),
326 fprintf (stderr, "%s: exec: %s\n", progname, cmd);
331 fprintf (stderr, "%s: %s was not created\n", progname, fn);
335 fprintf (stderr, "%s: wrote %s (%.1f MB)\n", progname, fn,
336 s.st_size / (float) (1024 * 1024));
338 for (i = 0; i < st->frame_count; i++)
340 sprintf (fn, "%s-%06d.%s", progname, i, type);