summaryrefslogtreecommitdiff
path: root/glitchyclock.c
blob: f86d770c37bdf6040288e2ddc222f518f396a0fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * glitchyclock.c
 *
 * Copyright © 2019-2020 Thomas White <taw@bitwiz.me.uk>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/* Get LED display fonts from here: https://www.keshikan.net/fonts-e.html */

#include <gtk/gtk.h>
#include <math.h>
#include <string.h>

struct glitchyclock
{
	GtkWidget *da;
	PangoFontDescription *fontdesc;
	int cue;
	double real_time_last_cue;
	double real_time_last_reset;
	double brightness;
	int base_hours;
	int base_minutes;
	int base_seconds;
	int flag;

	double clock_x;
	double clock_y;
	double clock_w;
	double clock_h;
};


enum glitch_type
{
	BLACKOUT,       /* Fade to black, parameter is fade time in seconds */
	FADE_IN,        /* Fade from black to running clock, parameter is fade time in seconds */
	FADE_IN_FROZEN, /* Fade from black to running clock, parameter is fade time in seconds */
	BLACKOUT_FROZEN,
	GLITCH_BACK,    /* Glitch and then jump back, parameter is number of minutes */
	GLITCH_STOP,    /* Glitch and then stop, parameter is number of minutes */
	GLITCH,         /* Glitch for the number of seconds */
	EOL             /* End of list */
};


struct glitch_cue
{
	int type;
	int parameter;
	int hours;
	int minutes;
	int seconds;
};


struct glitch_cue cues[] = {
	{ BLACKOUT, 0, 0,0,0},
	{ FADE_IN, 1, 23, 10, 50 },  /* Act I sc 1 */
	{ BLACKOUT, 1, 0,0,0},
	{ FADE_IN, 1,   9, 52, 50 }, /* Act I sc 2 */
	{ BLACKOUT, 3, 0,0,0 },
	{ FADE_IN, 1,  22, 27, 50 },
	{ BLACKOUT, 1, 0,0,0},
	{ FADE_IN, 1,   1, 13, 50 },
	{ BLACKOUT, 0, 0,0,0},
	{ FADE_IN, 1,  22, 45, 50 },
	{ GLITCH_BACK, 103, 0,0,0},
	{ GLITCH_BACK, 33, 0,0,0},
	{ BLACKOUT, 1,  0,  0, 12 },
	{ FADE_IN, 1,   2,  1, 50 },
	{ BLACKOUT, 1,  0,0,0 },
	{ FADE_IN, 1,   3, 21, 50 },  /* Act I sc VII */
	{ GLITCH, 1, 0,0,0},
	{ GLITCH, 3, 0,0,0},
	{ GLITCH_STOP, 2, 0, 0, 12},
	//{ BLACKOUT_FROZEN, 3,  0, 0, 12 },
	//{ FADE_IN_FROZEN, 3, 0, 0, 12 },
	//{ BLACKOUT_FROZEN, 3 , 0, 0, 12},
	//{ FADE_IN_FROZEN, 3, 0, 0, 12 },
	{ BLACKOUT_FROZEN, 0 , 0, 0, 12},
	{ EOL, 0, 0, 0, 0 }
};

const double screen_w_frac = 0.2;    /* Fraction of screen width */
const double screen_y_offset = 0.5;  /* Fraction of screen height */
const double clock_aspect = 0.305;   /* Clock height divided by width */

static double get_monotonic_seconds()
{
	struct timespec tp;
	clock_gettime(CLOCK_MONOTONIC, &tp);
	return tp.tv_sec + (double)tp.tv_nsec/1e9;
}


static void run_cue(struct glitchyclock *gc)
{
	if ( cues[gc->cue].type == EOL ) return;
	gc->cue++;
	gc->real_time_last_cue = get_monotonic_seconds();
	gc->flag = 0;
	if ( cues[gc->cue].type == FADE_IN ) {
		gc->real_time_last_reset = get_monotonic_seconds();
		gc->base_hours = cues[gc->cue].hours;
		gc->base_minutes = cues[gc->cue].minutes;
		gc->base_seconds = cues[gc->cue].seconds;
	}
}


static void back_cue(struct glitchyclock *gc)
{
	if ( gc->cue == 0 ) return;
	gc->cue--;
	gc->real_time_last_cue = get_monotonic_seconds() - 10;
	gc->flag = 1;
	if ( cues[gc->cue].type == FADE_IN ) {
		gc->real_time_last_reset = get_monotonic_seconds() - 10;
		gc->base_hours = cues[gc->cue].hours;
		gc->base_minutes = cues[gc->cue].minutes;
		gc->base_seconds = cues[gc->cue].seconds;
	}
}




static gboolean draw_sig(GtkWidget *widget, cairo_t *cr, struct glitchyclock *gc)
{
	double lw, lh;
	int ilw, ilh;
	PangoLayout *layout;
	char timestr[64];
	double seconds_elapsed, seconds_rounded;
	double real_seconds_elapsed, real_seconds_rounded;
	char col;
	int clock_hours, clock_minutes, clock_seconds;
	int glitch = 0;
	double duty;
	const char *frozenstr = "uK:IW";

	/* Overall background */
	cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
	cairo_paint(cr);

	cairo_translate(cr, gc->clock_x, gc->clock_y);

	layout = pango_layout_new(gtk_widget_get_pango_context(widget));
	pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
	pango_layout_set_font_description(layout, gc->fontdesc);

	seconds_elapsed = get_monotonic_seconds() - gc->real_time_last_cue;
	modf(seconds_elapsed, &seconds_rounded);

	real_seconds_elapsed = get_monotonic_seconds() - gc->real_time_last_reset;
	col = (modf(real_seconds_elapsed, &real_seconds_rounded) < 0.5) ? ':' : ' ';
	clock_hours = gc->base_hours;
	clock_minutes = gc->base_minutes;
	clock_seconds = gc->base_seconds + real_seconds_rounded;

	clock_minutes += clock_seconds / 60;
	clock_seconds = clock_seconds % 60;
	clock_hours += clock_minutes / 60;
	clock_minutes = clock_minutes % 60;
	clock_hours = clock_hours % 24;

	snprintf(timestr, 63, "%02i%c%02i", clock_hours, ':', clock_minutes);

	if ( cues[gc->cue].type == FADE_IN ) {
		gc->brightness = seconds_elapsed / cues[gc->cue].parameter;
		if ( gc->brightness > 1.0 ) gc->brightness = 1.0;
	}

	if ( cues[gc->cue].type == FADE_IN_FROZEN ) {
		gc->brightness = seconds_elapsed / cues[gc->cue].parameter;
		if ( gc->brightness > 1.0 ) gc->brightness = 1.0;
		strcpy(timestr, frozenstr);
	}

	if ( cues[gc->cue].type == BLACKOUT ) {
		gc->brightness = 1.0 - (seconds_elapsed / cues[gc->cue].parameter);
		if ( gc->brightness < 0.0 ) gc->brightness = 0.0;
	}

	if ( cues[gc->cue].type == BLACKOUT_FROZEN ) {
		gc->brightness = 1.0 - (seconds_elapsed / cues[gc->cue].parameter);
		if ( gc->brightness < 0.0 ) gc->brightness = 0.0;
		strcpy(timestr, frozenstr);
	}

	if ( (cues[gc->cue].type == GLITCH)
	  && (seconds_elapsed < cues[gc->cue].parameter) )
	{
		duty = 0.5;
		glitch = 1;
	}

	if ( cues[gc->cue].type == GLITCH_STOP ) {
		glitch = 1;
		duty = 0.5 + seconds_elapsed / cues[gc->cue].parameter;
		if ( duty > 1.0 ) duty = 1.0;
	}

	if ( cues[gc->cue].type == GLITCH_BACK ) {
		if ( !gc->flag ) {
			duty = 0.5 + seconds_elapsed / 4;
			glitch = 1;
			if ( duty > 1.0 ) {
				duty = 1.0;
				gc->real_time_last_reset += cues[gc->cue].parameter * 60;

				/* The real_time_elapsed will now probably be
				 * negative (unless the clock glitched back by
				 * less than real_time_elapsed).  To avoid
				 * negative numbers appearing, go back a whole
				 * day.  Yes, I'm a dirty cheat. */
				gc->real_time_last_reset -= 24*60*60;
				gc->flag = 1;
			}
		} else {
			glitch = 0;
		}
	}

	if ( glitch ) {
		if ( fmod(seconds_elapsed, 0.05) > duty*0.05 ) {
			strcpy(timestr, "Tz:Yi");
		} else {
			strcpy(timestr, frozenstr);
		}
	}

	pango_layout_set_text(layout, "88:88", -1);
	pango_cairo_update_layout(cr, layout);
	pango_layout_get_size(layout, &ilw, &ilh);
	lw = (double)ilw / PANGO_SCALE;
	lh = (double)ilh / PANGO_SCALE;
	cairo_scale(cr, gc->clock_w/lw, gc->clock_h/lh);

	pango_cairo_update_layout(cr, layout);
	cairo_set_source_rgb(cr, gc->brightness*0.20, 0.0, 0.0);
	pango_cairo_show_layout(cr, layout);

	pango_layout_set_text(layout, timestr, -1);
	cairo_set_source_rgb(cr, gc->brightness*0.95, gc->brightness*0.0, gc->brightness*0.05);
	pango_cairo_show_layout(cr, layout);

	return FALSE;
}


static gboolean configure_sig(GtkWidget *da, GdkEventConfigure *event, struct glitchyclock *gc)
{
	gc->clock_w = screen_w_frac * event->width;
	gc->clock_h = clock_aspect * gc->clock_w;
	gc->clock_x = (event->width - gc->clock_w)/2.0;
	gc->clock_y = event->height * screen_y_offset;

	return FALSE;
}


static gboolean redraw_cb(gpointer data)
{
	struct glitchyclock *gc = data;
	gtk_widget_queue_draw_area(GTK_WIDGET(gc->da), gc->clock_x, gc->clock_y,
	                                               gc->clock_w, gc->clock_h);
	return G_SOURCE_CONTINUE;
}


static gboolean key_press_sig(GtkWidget *da, GdkEventKey *event, struct glitchyclock *gc)
{
	if ( event->keyval == GDK_KEY_space ) {
		run_cue(gc);
		redraw_cb(gc);
		return TRUE;
	}

	if ( event->keyval == GDK_KEY_BackSpace ) {
		back_cue(gc);
		redraw_cb(gc);
		return TRUE;
	}

	if ( event->keyval == GDK_KEY_q ) {
		gtk_main_quit();
	}

	return FALSE;
}


static gint realise_sig(GtkWidget *da, struct glitchyclock *gc)
{
	GdkCursor *cursor;
	GdkWindow *win = gtk_widget_get_window(da);

	/* Keyboard and input method stuff */
	gdk_window_set_accept_focus(win, TRUE);
	g_signal_connect(G_OBJECT(da), "key-press-event", G_CALLBACK(key_press_sig), gc);

	gc->fontdesc = pango_font_description_from_string("DSEG7 Modern Bold Italic 24");

	cursor = gdk_cursor_new_for_display(gtk_widget_get_display(da), GDK_BLANK_CURSOR);
	gdk_window_set_cursor(gtk_widget_get_window(da), cursor);

	return FALSE;
}


int main(int argc, char *argv[])
{
	struct glitchyclock gc;
	GtkWidget *mainwindow;

	gc.cue = 0;
	gc.brightness = 0.0;
	gc.real_time_last_cue = get_monotonic_seconds();
	gc.real_time_last_reset = get_monotonic_seconds();
	gc.base_hours = cues[gc.cue].hours;
	gc.base_minutes = cues[gc.cue].minutes;
	gc.base_seconds = cues[gc.cue].seconds;
	gtk_init(&argc, &argv);

	mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_fullscreen(GTK_WINDOW(mainwindow));
	g_signal_connect_swapped(G_OBJECT(mainwindow), "destroy", gtk_main_quit, NULL);

	gc.da = gtk_drawing_area_new();
	gtk_container_add(GTK_CONTAINER(mainwindow), GTK_WIDGET(gc.da));
	gtk_widget_set_can_focus(GTK_WIDGET(gc.da), TRUE);
	gtk_widget_add_events(gc.da, GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
	g_signal_connect(G_OBJECT(gc.da), "draw", G_CALLBACK(draw_sig), &gc);
	g_signal_connect(G_OBJECT(gc.da), "realize", G_CALLBACK(realise_sig), &gc);
	g_signal_connect(G_OBJECT(gc.da), "configure-event", G_CALLBACK(configure_sig), &gc);

	gtk_widget_grab_focus(GTK_WIDGET(gc.da));
	gtk_widget_show_all(mainwindow);

	g_timeout_add(20, redraw_cb, &gc);

	gtk_main();

	return 0;
}