/*
 * Drawing live tile content: the templates of org.cont8nuum.Tiles1 (see
 * the comment in protocols/org.cont8nuum.Tiles1.xml). Each update in
 * a tile's queue is one slide, or two for peek (its image, then its
 * text); Start rotates through them.
 */
#define _GNU_SOURCE
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "shell.h"

#define PAD 12

enum template {
	TPL_TEXT,
	TPL_BLOCK,
	TPL_IMAGE,
	TPL_PEEK,
	TPL_IMAGES,
	TPL_STATS,
	TPL_CALENDAR,
	TPL_FORECAST,
	TPL_IMAGETEXT,
};

static const char *template_names[] = {
	[TPL_TEXT] = "text",
	[TPL_BLOCK] = "block",
	[TPL_IMAGE] = "image",
	[TPL_PEEK] = "peek",
	[TPL_IMAGES] = "images",
	[TPL_STATS] = "stats",
	[TPL_CALENDAR] = "calendar",
	[TPL_FORECAST] = "forecast",
	[TPL_IMAGETEXT] = "imagetext",
};

static const char *field(struct live_update *u, struct tile *tile,
		const char *key) {
	const char *v = live_field(u, tile->size, key);
	return v && v[0] ? v : NULL;
}

static const char *fieldf(struct live_update *u, struct tile *tile,
		const char *fmt, int n) {
	char key[32];
	snprintf(key, sizeof(key), fmt, n);
	return field(u, tile, key);
}

static enum template template_of(struct live_update *u, struct tile *tile) {
	const char *name = field(u, tile, "template");
	for (int i = 0; name && i <= TPL_IMAGETEXT; i++) {
		if (strcmp(name, template_names[i]) == 0) {
			return i;
		}
	}
	bool text = field(u, tile, "heading") || field(u, tile, "text1");
	return field(u, tile, "image") && !text ? TPL_IMAGE : TPL_TEXT;
}

/* Slides */

struct slide {
	struct live_update *u;
	enum template tpl;
	int part; // peek: 0 the image, 1 the text
};

static int count_slides(struct tile *tile, bool is_live) {
	struct live *live = is_live ? live_find(tile->id) : NULL;
	int count = 0;
	for (guint i = 0; live && i < live->updates->len; i++) {
		count += template_of(live->updates->pdata[i], tile) == TPL_PEEK ? 2 : 1;
	}
	return count;
}

static bool get_slide(struct tile *tile, int index, struct slide *out) {
	if (!tile_is_live(tile)) {
		return false;
	}
	struct live *live = live_find(tile->id);
	if (live == NULL) {
		return false;
	}
	for (guint i = 0; i < live->updates->len; i++) {
		struct live_update *u = live->updates->pdata[i];
		enum template tpl = template_of(u, tile);
		int parts = tpl == TPL_PEEK ? 2 : 1;
		if (index < parts) {
			*out = (struct slide){ u, tpl, index };
			return true;
		}
		index -= parts;
	}
	return false;
}

/* Asked for every tile on every frame something moves, and working it
 * out reads each update's template: kept until the updates, the size or
 * being live change */
int tile_slide_count(struct tile *tile) {
	bool is_live = tile_is_live(tile);
	unsigned now = live_stamp() << 4 | tile->size << 1 | is_live;
	if (tile->slides_stamp == now) {
		return tile->slides;
	}
	tile->slides_stamp = now;
	tile->slides = count_slides(tile, is_live);
	return tile->slides;
}

const char *tile_label(struct tile *tile, int index) {
	if (!tile->label || tile->size == TILE_SMALL) {
		return NULL;
	}
	struct slide s;
	if (get_slide(tile, index, &s)) {
		const char *label = live_field(s.u, tile->size, "label");
		if (label) {
			return label[0] ? label : NULL;
		}
		if (s.tpl == TPL_STATS) {
			return NULL; // each cell says what it is
		}
	}
	return tile->name;
}

uint32_t tile_slide_color(struct tile *tile, int index) {
	struct slide s;
	uint32_t rgb;
	if (get_slide(tile, index, &s) && field(s.u, tile, "color") &&
			c8ui_parse_color(field(s.u, tile, "color"), &rgb)) {
		return rgb;
	}
	return tile_color(tile);
}

bool tile_slide_clear(struct tile *tile, int index) {
	struct slide s;
	return get_slide(tile, index, &s) && s.tpl == TPL_STATS;
}

/* Pieces */

static double unit(void) {
	return tile_cell() / 70.0;
}

/* One line; a tab puts the rest at the right edge, dimmer */
static double line(cairo_t *cr, const char *family, struct c8ui_font font,
		const char *text, double x, double y, double w, double alpha) {
	const char *tab = strchr(text, '\t');
	double tw, th;
	cairo_set_source_rgba(cr, 1, 1, 1, alpha);
	if (tab == NULL) {
		c8ui_text_size(family, font, text, w, 1, &tw, &th);
		c8ui_text_draw(cr, family, font, text, x, y, w, 1);
		return th;
	}
	char *left = strndup(text, tab - text);
	const char *right = tab + 1;
	c8ui_text_size(family, font, right, w / 2, 1, &tw, &th);
	cairo_set_source_rgba(cr, 1, 1, 1, alpha * 0.75);
	c8ui_text_draw(cr, family, font, right, x + w - tw, y, w / 2, 1);
	cairo_set_source_rgba(cr, 1, 1, 1, alpha);
	double lw = w - tw - 8;
	if (lw > 0) {
		c8ui_text_draw(cr, family, font, left, x, y, lw, 1);
	}
	free(left);
	return th;
}

/* text1.. from `first`, down to `bottom` */
static void lines(cairo_t *cr, const char *family, struct live_update *u,
		struct tile *tile, int first, double x, double y, double w,
		double bottom, double alpha) {
	double k = unit();
	struct c8ui_font font = { 14 * k, C8UI_WEIGHT_NORMAL };
	double lh = font.size * 1.35;
	for (int i = first; i <= 6; i++) {
		const char *text = fieldf(u, tile, "text%d", i);
		if (text == NULL) {
			continue;
		}
		if (y + lh > bottom) {
			break;
		}
		line(cr, family, font, text, x, y, w, alpha);
		y += lh;
	}
}

static void cover(cairo_t *cr, cairo_surface_t *img, double x, double y,
		double w, double h) {
	if (img == NULL) {
		cairo_set_source_rgba(cr, 0, 0, 0, 0.15);
		cairo_rectangle(cr, x, y, w, h);
		cairo_fill(cr);
		return;
	}
	/* Shrunk once and copied after: Start draws its tiles for every
	 * frame it pans */
	c8ui_draw_surface_cover(cr, img, x, y, w, h);
}

static double bottom_of(struct tile *tile, int h, bool labelled) {
	return h - (labelled ? 30 : PAD) * unit();
}

/* Templates */

static void draw_text(cairo_t *cr, const char *family, struct live_update *u,
		struct tile *tile, int w, int h, bool labelled) {
	double k = unit(), pad = PAD * k;
	double bottom = bottom_of(tile, h, labelled);
	double y = pad - 4 * k;
	const char *heading = field(u, tile, "heading");
	if (heading) {
		struct c8ui_font font = { (tile->size == TILE_MEDIUM ? 24 : 30) * k,
			C8UI_WEIGHT_LIGHT };
		int max = tile->size == TILE_LARGE ? 3 : 2;
		cairo_set_source_rgb(cr, 1, 1, 1);
		y += c8ui_text_draw(cr, family, font, heading, pad, y, w - 2 * pad,
			max) + 2 * k;
	}
	lines(cr, family, u, tile, 1, pad, y, w - 2 * pad, bottom, 0.9);
}

static void draw_block(cairo_t *cr, const char *family, struct live_update *u,
		struct tile *tile, int w, int h, bool labelled) {
	double k = unit(), pad = PAD * k;
	double bottom = bottom_of(tile, h, labelled);
	bool wide = tile->size == TILE_WIDE;
	double col = wide ? fmax(w * 0.42, 110 * k) : w - 2 * pad;
	double sizes[] = { [TILE_MEDIUM] = 46, [TILE_WIDE] = 52, [TILE_LARGE] = 72 };
	double y = pad - 8 * k;
	const char *block = field(u, tile, "block");
	if (block) {
		struct c8ui_font font = { sizes[tile->size] * k, C8UI_WEIGHT_LIGHT };
		cairo_set_source_rgb(cr, 1, 1, 1);
		y += c8ui_text_draw(cr, family, font, block, pad, y, col, 1);
	}
	const char *meter = field(u, tile, "meter");
	if (meter) {
		double v = fmin(fmax(strtod(meter, NULL) / 100, 0), 1);
		double mh = fmax(3, 4 * k);
		y += 2 * k;
		cairo_set_source_rgba(cr, 1, 1, 1, 0.25);
		cairo_rectangle(cr, pad, y, col, mh);
		cairo_fill(cr);
		cairo_set_source_rgba(cr, 1, 1, 1, 0.95);
		cairo_rectangle(cr, pad, y, col * v, mh);
		cairo_fill(cr);
		y += mh + 6 * k;
	}
	const char *caption = field(u, tile, "text1");
	if (caption) {
		struct c8ui_font font = { 16 * k, C8UI_WEIGHT_SEMILIGHT };
		y += line(cr, family, font, caption, pad, y, col, 1) + 2 * k;
	}
	if (wide) {
		double x = pad + col + pad * 1.5;
		lines(cr, family, u, tile, 2, x, pad, w - x - pad, h - pad, 0.85);
	} else {
		lines(cr, family, u, tile, 2, pad, y, w - 2 * pad, bottom, 0.85);
	}
}

static void draw_image(cairo_t *cr, const char *family, struct live_update *u,
		struct tile *tile, int w, int h, bool labelled) {
	cover(cr, live_image(u, field(u, tile, "image")), 0, 0, w, h);
	const char *heading = field(u, tile, "heading");
	const char *text = field(u, tile, "text1");
	if (heading == NULL && text == NULL) {
		return;
	}
	/* A shaded band along the bottom, for the caption */
	double k = unit(), pad = PAD * k;
	cairo_pattern_t *shade = cairo_pattern_create_linear(0, h * 0.4, 0, h);
	cairo_pattern_add_color_stop_rgba(shade, 0, 0, 0, 0, 0);
	cairo_pattern_add_color_stop_rgba(shade, 1, 0, 0, 0, 0.7);
	cairo_set_source(cr, shade);
	cairo_paint(cr);
	cairo_pattern_destroy(shade);
	double y = bottom_of(tile, h, labelled);
	struct c8ui_font small = { 12 * k, C8UI_WEIGHT_NORMAL };
	struct c8ui_font big = { 16 * k, C8UI_WEIGHT_SEMILIGHT };
	if (text) {
		y -= small.size * 1.35;
		line(cr, family, small, text, pad, y, w - 2 * pad, 0.85);
	}
	if (heading) {
		y -= big.size * 1.35;
		line(cr, family, big, heading, pad, y, w - 2 * pad, 1);
	}
}

/* A small picture with the words beside it (under it on medium tiles) */
static void draw_imagetext(cairo_t *cr, const char *family,
		struct live_update *u, struct tile *tile, int w, int h,
		bool labelled) {
	double k = unit(), pad = PAD * k;
	bool medium = tile->size == TILE_MEDIUM, wide = tile->size == TILE_WIDE;
	double side = medium ? 40 * k : wide ? h - 2 * pad - (labelled ? 14 * k : 0) :
		w * 0.36;
	double bottom = bottom_of(tile, h, labelled);
	cover(cr, live_image(u, field(u, tile, "image")), pad, pad, side, side);
	double tx = pad + side + pad * 0.8, tw = w - tx - pad;
	double y = pad - 4 * k;
	const char *heading = field(u, tile, "heading");
	if (heading) {
		struct c8ui_font font = { (medium ? 16 : wide ? 22 : 26) * k,
			C8UI_WEIGHT_LIGHT };
		cairo_set_source_rgb(cr, 1, 1, 1);
		y += c8ui_text_draw(cr, family, font, heading, tx, y, tw,
			medium ? 3 : 2) + 2 * k;
	}
	if (medium) {
		/* The picture is small: the lines go under it, across */
		lines(cr, family, u, tile, 1, pad, pad + side + 6 * k, w - 2 * pad,
			bottom, 0.9);
	} else {
		lines(cr, family, u, tile, 1, tx, y, tw, bottom, 0.9);
	}
}

static void draw_images(cairo_t *cr, struct live_update *u, struct tile *tile,
		int w, int h) {
	double gap = 4 * unit();
	cairo_surface_t *imgs[5];
	int n = 0;
	for (int i = 1; i <= 5; i++) {
		const char *ref = fieldf(u, tile, "image%d", i);
		if (ref) {
			imgs[n++] = live_image(u, ref);
		}
	}
	if (n == 0) {
		return;
	}
	if (tile->size == TILE_MEDIUM || n == 1) {
		cover(cr, imgs[0], 0, 0, w, h);
		return;
	}
	int rest = n - 1;
	if (tile->size == TILE_WIDE) {
		/* The first square on the left, the rest two by two */
		double big = fmin(h, w / 2.0);
		cover(cr, imgs[0], 0, 0, big, h);
		double x0 = big + gap, cw = (w - x0 - gap) / 2, ch = (h - gap) / 2;
		for (int i = 0; i < rest; i++) {
			cover(cr, imgs[i + 1], x0 + (i % 2) * (cw + gap),
				(i / 2) * (ch + gap), cw, ch);
		}
		return;
	}
	/* Large: the first across the top, the rest in a row under it */
	double rh = (h - gap) / 3, top = h - rh - gap;
	cover(cr, imgs[0], 0, 0, w, top);
	double cw = (w - (rest - 1) * gap) / rest;
	for (int i = 0; i < rest; i++) {
		cover(cr, imgs[i + 1], i * (cw + gap), top + gap, cw, rh);
	}
}

static void draw_stats(cairo_t *cr, const char *family, struct live_update *u,
		struct tile *tile, uint32_t base, int w, int h) {
	double k = unit();
	int n = 0;
	for (int i = 1; i <= 4; i++) {
		if (fieldf(u, tile, "value%d", i)) {
			n = i;
		}
	}
	if (n == 0) {
		return;
	}
	int cols = tile->size == TILE_WIDE ? n : n == 1 ? 1 : 2;
	int rows = (n + cols - 1) / cols;
	double gap = TILE_GAP;
	double cw = (w - (cols - 1) * gap) / cols;
	double ch = (h - (rows - 1) * gap) / rows;
	for (int i = 0; i < n; i++) {
		double x = (i % cols) * (cw + gap), y = (i / cols) * (ch + gap);
		uint32_t rgb = base;
		const char *c = fieldf(u, tile, "color%d", i + 1);
		if (c) {
			c8ui_parse_color(c, &rgb);
		}
		c8ui_set_source(cr, rgb, 1);
		cairo_rectangle(cr, x, y, cw, ch);
		cairo_fill(cr);
		const char *value = fieldf(u, tile, "value%d", i + 1);
		const char *label = fieldf(u, tile, "text%d", i + 1);
		struct c8ui_font vf = { fmin(cw, ch) * 0.34, C8UI_WEIGHT_LIGHT };
		struct c8ui_font lf = { 11 * k, C8UI_WEIGHT_NORMAL };
		double tw, th;
		c8ui_text_size(family, vf, value ? value : "", cw - 12, 1, &tw, &th);
		cairo_set_source_rgb(cr, 1, 1, 1);
		if (value) {
			c8ui_text_draw(cr, family, vf, value, x + 8 * k,
				y + (ch - th) / 2 - 4 * k, cw - 12 * k, 1);
		}
		if (label) {
			line(cr, family, lf, label, x + 8 * k, y + ch - lf.size * 1.35 -
				6 * k, cw - 14 * k, 0.9);
		}
	}
}

/* Calendar: a month as a grid, today marked */

static void centred(cairo_t *cr, const char *family, struct c8ui_font font,
		const char *text, double cx, double cy, double w) {
	double tw, th;
	c8ui_text_size(family, font, text, w, 1, &tw, &th);
	c8ui_text_draw(cr, family, font, text, cx - tw / 2, cy - th / 2, w, 1);
}

static void draw_month(cairo_t *cr, const char *family, struct live_update *u,
		struct tile *tile, uint32_t base, double x, double y, double w,
		double h) {
	const char *month = field(u, tile, "month");
	int year, mon;
	if (month == NULL || sscanf(month, "%d-%d", &year, &mon) != 2 ||
			mon < 1 || mon > 12 || year < 1900) {
		return;
	}
	const char *t = field(u, tile, "today");
	const char *ws = field(u, tile, "weekstart");
	int today = t ? atoi(t) : 0;
	int start = ws ? ((atoi(ws) % 7) + 7) % 7 : 0;
	struct tm tm = { .tm_year = year - 1900, .tm_mon = mon - 1, .tm_mday = 1,
		.tm_hour = 12 };
	mktime(&tm);
	int first = (tm.tm_wday - start + 7) % 7;
	int days = g_date_get_days_in_month(mon, year);
	int rows = (first + days + 6) / 7;
	double cw = w / 7, ch = h / (rows + 1);
	struct c8ui_font font = { fmin(cw * 0.5, ch * 0.66), C8UI_WEIGHT_NORMAL };
	for (int d = 0; d < 7; d++) {
		struct tm wd = { .tm_wday = (start + d) % 7 };
		char name[16];
		strftime(name, sizeof(name), "%a", &wd);
		/* One letter: the first character, not the first byte */
		char *end = g_utf8_offset_to_pointer(name, 1);
		*end = '\0';
		cairo_set_source_rgba(cr, 1, 1, 1, 0.6);
		centred(cr, family, font, name, x + (d + 0.5) * cw, y + ch / 2, cw);
	}
	for (int day = 1; day <= days; day++) {
		int idx = first + day - 1;
		double cx = x + (idx % 7 + 0.5) * cw;
		double cy = y + (idx / 7 + 1.5) * ch;
		char num[8];
		snprintf(num, sizeof(num), "%d", day);
		if (day == today) {
			double side = fmin(cw, ch) - 2 * unit();
			cairo_set_source_rgb(cr, 1, 1, 1);
			cairo_rectangle(cr, cx - cw / 2 + (cw - side) / 2,
				cy - side / 2, side, side);
			cairo_fill(cr);
			c8ui_set_source(cr, base, 1);
		} else {
			cairo_set_source_rgba(cr, 1, 1, 1, 0.9);
		}
		centred(cr, family, font, num, cx, cy, cw);
	}
}

static void draw_calendar(cairo_t *cr, const char *family,
		struct live_update *u, struct tile *tile, uint32_t base, int w,
		int h, bool labelled) {
	double k = unit(), pad = PAD * k;
	double bottom = bottom_of(tile, h, labelled);
	bool large = tile->size == TILE_LARGE;
	bool medium = tile->size == TILE_MEDIUM;
	struct c8ui_font big = { (medium ? 54 : large ? 72 : 58) * k,
		C8UI_WEIGHT_LIGHT };
	struct c8ui_font mid = { 16 * k, C8UI_WEIGHT_SEMILIGHT };
	struct c8ui_font small = { 12 * k, C8UI_WEIGHT_NORMAL };
	double left = medium ? w - 2 * pad : large ? w * 0.42 : w * 0.38;
	double y = pad - 8 * k;
	const char *block = field(u, tile, "block");
	if (block) {
		cairo_set_source_rgb(cr, 1, 1, 1);
		y += c8ui_text_draw(cr, family, big, block, pad, y, left, 1);
	}
	const char *weekday = field(u, tile, "text1");
	if (weekday) {
		y += line(cr, family, mid, weekday, pad, y, left, 1);
	}
	const char *month = field(u, tile, "text2");
	if (month) {
		line(cr, family, small, month, pad, y + 2 * k, left, 0.75);
	}
	if (medium) {
		return;
	}
	if (!large) {
		double x = left + pad;
		draw_month(cr, family, u, tile, base, x, pad, w - x - pad,
			bottom - pad);
		return;
	}
	/* Large: the extra lines at the top right, the month under both */
	double x = w * 0.5;
	lines(cr, family, u, tile, 3, x, pad + 6 * k, w - x - pad, h * 0.34,
		0.85);
	double top = fmax(y + 2 * k + small.size * 1.35, h * 0.34) + 6 * k;
	draw_month(cr, family, u, tile, base, pad, top, w - 2 * pad, bottom - top);
}

/* Forecast: c8ui's weather glyphs, in white, like the rest of the tile */

static void draw_forecast(cairo_t *cr, const char *family,
		struct live_update *u, struct tile *tile, uint32_t base, int w, int h,
		bool labelled) {
	double k = unit(), pad = PAD * k;
	double bottom = bottom_of(tile, h, labelled);
	enum tile_size size = tile->size;
	const char *glyph = field(u, tile, "glyph");
	const char *block = field(u, tile, "block");
	int days = 0;
	for (int i = 1; i <= 4; i++) {
		if (fieldf(u, tile, "day%d", i)) {
			days = i;
		}
	}
	/* The current weather on top (large) or on the left (wide) */
	double gs = (size == TILE_LARGE ? 84 : size == TILE_WIDE ? 52 : 50) * k;
	double fs = (size == TILE_LARGE ? 58 : size == TILE_WIDE ? 42 : 38) * k;
	double area = size == TILE_WIDE ? w * 0.5 : w;
	double y = pad - 4 * k;
	if (glyph) {
		c8ui_weather_glyph(cr, glyph, pad + gs / 2, y + gs / 2, gs, 1, base);
	}
	double x = pad + gs + 6 * k;
	if (block) {
		struct c8ui_font font = { fs, C8UI_WEIGHT_LIGHT };
		cairo_set_source_rgb(cr, 1, 1, 1);
		c8ui_text_draw(cr, family, font, block, x, y + (gs - fs * 1.3) / 2,
			area - x - pad, 1);
	}
	y += gs + 2 * k;
	struct c8ui_font cond = { (size == TILE_LARGE ? 18 : 15) * k,
		C8UI_WEIGHT_SEMILIGHT };
	const char *text1 = field(u, tile, "text1");
	if (text1) {
		y += line(cr, family, cond, text1, pad, y, area - 2 * pad, 1) + 2 * k;
	}
	double limit = size == TILE_LARGE ? h * 0.6 : bottom;
	lines(cr, family, u, tile, 2, pad, y, area - 2 * pad, limit, 0.8);
	if (days == 0 || size == TILE_MEDIUM) {
		return;
	}
	/* The days: columns, split by a thin line when under the rest */
	double dx, dy, dw, dh;
	if (size == TILE_WIDE) {
		dx = w * 0.5 + pad / 2;
		dy = pad;
		dw = w - dx - pad;
		dh = bottom - pad;
	} else {
		dx = pad;
		dy = h * 0.62;
		dw = w - 2 * pad;
		dh = bottom - dy;
		cairo_set_source_rgba(cr, 1, 1, 1, 0.3);
		cairo_rectangle(cr, pad, dy - 6 * k, dw, 1);
		cairo_fill(cr);
	}
	int n = days > 3 ? 3 : days;
	double cw = dw / n;
	struct c8ui_font nf = { 12 * k, C8UI_WEIGHT_NORMAL };
	struct c8ui_font hf = { 15 * k, C8UI_WEIGHT_SEMILIGHT };
	struct c8ui_font lf = { 12 * k, C8UI_WEIGHT_NORMAL };
	double gsz = fmin(cw * 0.7, dh * 0.42);
	for (int i = 0; i < n; i++) {
		double cx = dx + (i + 0.5) * cw;
		double cy = dy;
		const char *name = fieldf(u, tile, "day%d", i + 1);
		const char *g = fieldf(u, tile, "glyph%d", i + 1);
		const char *hi = fieldf(u, tile, "value%d", i + 1);
		const char *lo = fieldf(u, tile, "low%d", i + 1);
		cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
		if (name) {
			centred(cr, family, nf, name, cx, cy + nf.size * 0.7, cw);
		}
		cy += nf.size * 1.5;
		if (g) {
			c8ui_weather_glyph(cr, g, cx, cy + gsz / 2, gsz, 0.95, base);
		}
		cy += gsz + 2 * k;
		if (hi) {
			cairo_set_source_rgb(cr, 1, 1, 1);
			centred(cr, family, hf, hi, cx, cy + hf.size * 0.7, cw);
		}
		cy += hf.size * 1.4;
		if (lo) {
			cairo_set_source_rgba(cr, 1, 1, 1, 0.65);
			centred(cr, family, lf, lo, cx, cy + lf.size * 0.7, cw);
		}
	}
}

/* Draws slide `index` over whatever is there (the face's colour, or
 * nothing for stats); false if there is no such slide */
bool tile_draw_slide(cairo_t *cr, const char *family, struct tile *tile,
		int index, int w, int h) {
	struct slide s;
	if (!get_slide(tile, index, &s)) {
		return false;
	}
	bool labelled = tile_label(tile, index) != NULL;
	cairo_save(cr);
	cairo_rectangle(cr, 0, 0, w, h);
	cairo_clip(cr);
	switch (s.tpl) {
	case TPL_TEXT:
		draw_text(cr, family, s.u, tile, w, h, labelled);
		break;
	case TPL_BLOCK:
		draw_block(cr, family, s.u, tile, w, h, labelled);
		break;
	case TPL_IMAGE:
		draw_image(cr, family, s.u, tile, w, h, labelled);
		break;
	case TPL_PEEK:
		if (s.part == 0) {
			cover(cr, live_image(s.u, field(s.u, tile, "image")), 0, 0, w, h);
		} else {
			draw_text(cr, family, s.u, tile, w, h, labelled);
		}
		break;
	case TPL_IMAGES:
		draw_images(cr, s.u, tile, w, h);
		break;
	case TPL_STATS:
		draw_stats(cr, family, s.u, tile, tile_slide_color(tile, index), w, h);
		break;
	case TPL_CALENDAR:
		draw_calendar(cr, family, s.u, tile, tile_slide_color(tile, index),
			w, h, labelled);
		break;
	case TPL_FORECAST:
		draw_forecast(cr, family, s.u, tile, tile_slide_color(tile, index),
			w, h, labelled);
		break;
	case TPL_IMAGETEXT:
		draw_imagetext(cr, family, s.u, tile, w, h, labelled);
		break;
	}
	cairo_restore(cr);
	return true;
}
