ref: d9e03f50daffc37027a5c57c88c7fd4241a58782
parent: db3b531e2cab765a00475054d2e9046c9d0437d3
author: Simon Tatham <anakin@pobox.com>
date: Fri Nov 23 18:44:17 EST 2018
Don't initialise GTK in --screenshot mode. I had this idea today and immediately wondered why I'd never had it before! To generate the puzzle screenshots used on the website and as program icons, we run the GTK front end with the --screenshot option, which sets up GTK, insists on connecting to an X server (or other display), draws the state of a puzzle on a Cairo surface, writes that surface out to a .png file, and exits. But there's no reason we actually need the GTK setup during that process, especially because the surface we do the drawing on is our _own_ surface, not even one provided to us by GTK. We could just set up a Cairo surface by itself, draw on it, and save it to a file. Calling gtk_init is not only pointless, but actively inconvenient, because it means the build script depends on having an X server available for the sole purpose of making gtk_init not complain. So now I've simplified things, by adding a 'headless' flag in new_window and the frontend structure, which suppresses all uses of actual GTK, leaving only the Cairo surface setup and enough supporting stuff (like colours) to generate the puzzle image. One awkward build dependency removed. This means that --screenshot no longer works in GTK 2, which I don't care about, because it only needs to run on _one_ platform.
--- a/Buildscr
+++ b/Buildscr
@@ -34,7 +34,7 @@
in puzzles do make -j$(nproc)
# Now build the screenshots and icons.
- in puzzles/icons do xvfb-run -s "-screen 0 1024x768x24" make web winicons gtkicons -j$(nproc)
+ in puzzles/icons do make web winicons gtkicons -j$(nproc)
# Destroy the local binaries and autoconf detritus, mostly to avoid
# wasting network bandwidth by transferring them to the delegate
--- a/gtk.c
+++ b/gtk.c
@@ -139,6 +139,8 @@
* particularly good reason not to.
*/
struct frontend {
+ bool headless; /* true if we're running without GTK, for --screenshot */
+
GtkWidget *window;
GtkAccelGroup *dummy_accelgroup;
GtkWidget *area;
@@ -270,6 +272,9 @@
{
frontend *fe = (frontend *)handle;
+ if (fe->headless)
+ return;
+
assert(fe->statusbar);
gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx);
@@ -297,7 +302,7 @@
fe->cr = NULL;
#ifndef USE_CAIRO_WITHOUT_PIXMAP
- {
+ if (!fe->headless) {
cairo_t *cr = gdk_cairo_create(fe->pixmap);
cairo_set_source_surface(cr, fe->image, 0, 0);
cairo_rectangle(cr,
@@ -507,6 +512,11 @@
static void setup_backing_store(frontend *fe)
{
#ifndef USE_CAIRO_WITHOUT_PIXMAP
+ if (fe->headless) {
+ fprintf(stderr, "headless mode does not work with GDK pixmaps\n");
+ exit(1);
+ }
+
fe->pixmap = gdk_pixmap_new(gtk_widget_get_window(fe->area),
fe->pw, fe->ph, -1);
#endif
@@ -518,7 +528,7 @@
wipe_and_maybe_destroy_cairo(fe, gdk_cairo_create(fe->pixmap), true);
#endif
#if GTK_CHECK_VERSION(3,22,0)
- {
+ if (!fe->headless) {
GdkWindow *gdkwin;
cairo_region_t *region;
GdkDrawingContext *drawctx;
@@ -780,6 +790,11 @@
{
GdkGC *gc;
+ if (fe->headless) {
+ fprintf(stderr, "headless mode does not work with GDK drawing\n");
+ exit(1);
+ }
+
fe->pixmap = gdk_pixmap_new(fe->area->window, fe->pw, fe->ph, -1);
gc = gdk_gc_new(fe->area->window);
@@ -1118,7 +1133,7 @@
teardown_drawing(fe);
- if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
+ if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d && !fe->headless) {
#ifdef USE_CAIRO_WITHOUT_PIXMAP
gtk_widget_queue_draw_area(fe->area,
fe->bbox_l - 1 + fe->ox,
@@ -2470,7 +2485,8 @@
enum { ARG_EITHER, ARG_SAVE, ARG_ID }; /* for argtype */
-static frontend *new_window(char *arg, int argtype, char **error)
+static frontend *new_window(
+ char *arg, int argtype, char **error, bool headless)
{
frontend *fe;
GtkBox *vbox, *hbox;
@@ -2483,8 +2499,15 @@
struct preset_menu *preset_menu;
fe = snew(frontend);
-#if GTK_CHECK_VERSION(3,20,0)
- fe->css_provider = NULL;
+ memset(fe, 0, sizeof(frontend));
+
+#if !GTK_CHECK_VERSION(3,0,0)
+ if (headless) {
+ fprintf(stderr, "headless mode not supported below GTK 3\n");
+ exit(1);
+ }
+#else
+ fe->headless = headless;
#endif
fe->timer_active = false;
@@ -2552,6 +2575,14 @@
midend_new_game(fe->me);
}
+ snaffle_colours(fe);
+
+ if (headless) {
+ get_size(fe, &fe->pw, &fe->ph);
+ setup_backing_store(fe);
+ return fe;
+ }
+
#if !GTK_CHECK_VERSION(3,0,0)
{
/*
@@ -2759,8 +2790,6 @@
changed_preset(fe);
- snaffle_colours(fe);
-
if (midend_wants_statusbar(fe->me)) {
GtkWidget *viewport;
GtkRequisition req;
@@ -3319,10 +3348,12 @@
return 0;
} else {
frontend *fe;
+ bool headless = screenshot_file != NULL;
- gtk_init(&argc, &argv);
+ if (!headless)
+ gtk_init(&argc, &argv);
- fe = new_window(arg, argtype, &error);
+ fe = new_window(arg, argtype, &error, headless);
if (!fe) {
fprintf(stderr, "%s: %s\n", pname, error);