1 /* recanim, Copyright (c) 2014 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;
61 screenhack_record_anim_init (Screen *screen, Window window, int target_frames)
63 Display *dpy = DisplayOfScreen (screen);
64 record_anim_state *st;
70 if (target_frames <= 0) return 0;
72 st = (record_anim_state *) calloc (1, sizeof(*st));
76 st->target_frames = target_frames;
79 # ifdef HAVE_GDK_PIXBUF
83 unsigned int w, h, d, bw;
84 XGetGeometry (dpy, window, &root, &x, &y, &w, &h, &bw, &d);
85 gdk_pixbuf_xlib_init_with_depth (dpy, screen_number (screen), d);
88 # if !GLIB_CHECK_VERSION(2, 36 ,0)
91 # else /* !HAVE_GTK2 */
92 xlib_rgb_init (dpy, screen);
93 # endif /* !HAVE_GTK2 */
95 # else /* !HAVE_GDK_PIXBUF */
96 # error GDK_PIXBUF is required
97 # endif /* !HAVE_GDK_PIXBUF */
99 XGetWindowAttributes (dpy, st->window, &st->xgwa);
103 st->data = (char *) calloc (st->xgwa.width, st->xgwa.height * 3);
104 st->data2 = (char *) calloc (st->xgwa.width, st->xgwa.height * 3);
108 st->gc = XCreateGC (dpy, st->window, 0, &gcv);
109 st->p = XCreatePixmap (dpy, st->window,
110 st->xgwa.width, st->xgwa.height, st->xgwa.depth);
111 st->img = XCreateImage (dpy, st->xgwa.visual, st->xgwa.depth, ZPixmap,
112 0, 0, st->xgwa.width, st->xgwa.height, 8, 0);
113 st->img->data = (char *) calloc (st->img->height, st->img->bytes_per_line);
114 # endif /* !USE_GL */
118 XFetchName (dpy, st->window, &st->title);
119 # endif /* !HAVE_COCOA */
126 screenhack_record_anim (record_anim_state *st)
128 int bytes_per_line = st->xgwa.width * 3;
132 Display *dpy = DisplayOfScreen (st->screen);
133 char *data = (char *) st->img->data;
135 /* Under XQuartz we can't just do XGetImage on the Window, we have to
136 go through an intermediate Pixmap first. I don't understand why.
137 Also, the fucking resize handle shows up as black. God dammit.
138 A workaround for that is to temporarily remove /opt/X11/bin/quartz-wm
140 XCopyArea (dpy, st->window, st->p, st->gc, 0, 0,
141 st->xgwa.width, st->xgwa.height, 0, 0);
142 XGetSubImage (dpy, st->p, 0, 0, st->xgwa.width, st->xgwa.height,
143 ~0L, ZPixmap, st->img, 0, 0);
145 /* Convert BGRA to RGB */
147 const char *in = st->img->data;
148 char *out = st->img->data;
150 int w = st->img->width;
151 int h = st->img->height;
152 for (y = 0; y < h; y++)
154 const char *in2 = in;
155 for (x = 0; x < w; x++)
162 in += st->img->bytes_per_line;
167 char *data = st->data2;
171 # undef glReadPixels /* Kludge -- unimplemented in the GLES compat layer */
174 /* First OpenGL frame tends to be random data like a shot of my desktop,
175 since it is the front buffer when we were drawing in the back buffer.
177 /* glDrawBuffer (GL_BACK); */
178 if (st->frame_count != 0)
179 glReadPixels (0, 0, st->xgwa.width, st->xgwa.height,
180 GL_RGB, GL_UNSIGNED_BYTE, st->data);
182 /* Flip vertically */
183 for (y = 0; y < st->xgwa.height; y++)
184 memcpy (data + bytes_per_line * y,
185 st->data + bytes_per_line * (st->xgwa.height - y - 1),
191 # ifdef HAVE_GDK_PIXBUF
193 const char *type = "png";
198 pixbuf = gdk_pixbuf_new_from_data ((guchar *) data,
199 GDK_COLORSPACE_RGB, False, /* alpha */
200 8, /* bits per sample */
201 st->xgwa.width, st->xgwa.height,
205 sprintf (fn, "%s-%06d.%s", progname, st->frame_count, type);
206 gdk_pixbuf_save (pixbuf, fn, type, &error, NULL);
210 fprintf (stderr, "%s: %s: %s\n", progname, fn, error->message);
214 g_object_unref (pixbuf);
216 # else /* !HAVE_GDK_PIXBUF */
217 # error GDK_PIXBUF is required
218 # endif /* !HAVE_GDK_PIXBUF */
221 { /* Put percent done in window title */
222 int pct = 100 * (st->frame_count + 1) / st->target_frames;
223 if (pct != st->pct && st->title)
225 Display *dpy = DisplayOfScreen (st->screen);
226 char *t2 = (char *) malloc (strlen(st->title) + 20);
227 sprintf (t2, "%s: %d%%", st->title, pct);
228 XStoreName (dpy, st->window, t2);
234 # endif /* !HAVE_COCOA */
236 if (++st->frame_count >= st->target_frames)
237 screenhack_record_anim_free (st);
242 screenhack_record_anim_free (record_anim_state *st)
245 Display *dpy = DisplayOfScreen (st->screen);
246 # endif /* !USE_GL */
250 const char *type = "png";
253 const char *soundtrack = 0;
255 fprintf (stderr, "%s: wrote %d frames\n", progname, st->frame_count);
261 free (st->img->data);
263 XDestroyImage (st->img);
264 XFreeGC (dpy, st->gc);
265 XFreePixmap (dpy, st->p);
266 # endif /* !USE_GL */
268 sprintf (fn, "%s.%s", progname, "mp4");
271 # define ST "images/drives-200.mp3"
273 if (stat (soundtrack, &s)) soundtrack = 0;
274 if (! soundtrack) soundtrack = "../" ST;
275 if (stat (soundtrack, &s)) soundtrack = 0;
276 if (! soundtrack) soundtrack = "../../" ST;
277 if (stat (soundtrack, &s)) soundtrack = 0;
281 " -r 30" /* Must be before -i */
286 sprintf (cmd + strlen(cmd),
287 " -i '%s' -map 0:v:0 -map 1:a:0 -acodec libfaac",
289 sprintf (cmd + strlen(cmd),
297 fprintf (stderr, "%s: exec: %s\n", progname, cmd);
302 fprintf (stderr, "%s: %s was not created\n", progname, fn);
306 fprintf (stderr, "%s: wrote %s (%.1f MB)\n", progname, fn,
307 s.st_size / (float) (1024 * 1024));
309 for (i = 0; i < st->frame_count; i++)
311 sprintf (fn, "%s-%06d.%s", progname, i, type);