summaryrefslogtreecommitdiff
path: root/glitchyclock.c
blob: 89ba8e31d22a5ba86e62e20a19e2db449fe4e5a8 (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
/*
 * glitchyclock.c
 *
 * Copyright © 2019 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>

struct glitchyclock
{
	GtkWidget *da;
	PangoFontDescription *fontdesc;
	int nglitch;
	time_t glitch_base;
};


const int glitch_times[] = {
	 7, 15,
	23, 58,
	-1, -1
};


static time_t get_monotonic_seconds()
{
	struct timespec tp;
	clock_gettime(CLOCK_MONOTONIC, &tp);
	return tp.tv_sec;
}


static void do_glitch(struct glitchyclock *gc)
{
	if ( glitch_times[2*(gc->nglitch+1)] == -1 ) return;
	gc->nglitch++;
	gc->glitch_base = get_monotonic_seconds();
}


static gboolean draw_sig(GtkWidget *widget, cairo_t *cr, struct glitchyclock *gc)
{
	int w, h;
	int lw, lh;
	double sf;
	PangoLayout *layout;
	const double screen_w_frac = 0.4;
	int glitch_hours, glitch_minutes;
	char timestr[64];
	int seconds;

	w = gtk_widget_get_allocated_width(widget);
	h = gtk_widget_get_allocated_height(widget);

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

	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);

	glitch_hours = glitch_times[gc->nglitch*2];
	glitch_minutes = glitch_times[gc->nglitch*2+1];
	seconds = get_monotonic_seconds() - gc->glitch_base;

	if ( seconds < 4 ) {
		strcpy(timestr, "rK:S8");
	} else {
		int hours, minutes;
		hours = (seconds/3600 + glitch_hours) % 24;
		minutes = (seconds/60 + glitch_minutes) % 60;
		snprintf(timestr, 63, "%02i:%02i", hours, minutes);
	}
	pango_layout_set_text(layout, "88:88", -1);

	pango_cairo_update_layout(cr, layout);
	pango_layout_get_size(layout, &lw, &lh);

	sf = (double)PANGO_SCALE*screen_w_frac*w/lw;
	cairo_scale(cr, sf, sf);
	pango_cairo_update_layout(cr, layout);

	pango_layout_get_size(layout, &lw, &lh);
	lw /= PANGO_SCALE;  lh /= PANGO_SCALE;
	w /= sf;  h /= sf;
	cairo_translate(cr, (w-lw)/2.0, h-lh);
	pango_cairo_update_layout(cr, layout);

	cairo_set_source_rgb(cr, 0.04, 0.0, 0.0);
	pango_cairo_show_layout(cr, layout);

	pango_layout_set_text(layout, timestr, -1);
	cairo_set_source_rgb(cr, 0.95, 0.0, 0.05);
	pango_cairo_show_layout(cr, layout);

	return FALSE;
}


static gboolean redraw_cb(gpointer data)
{
	gint w, h;
	struct glitchyclock *gc = data;
	w = gtk_widget_get_allocated_width(GTK_WIDGET(gc->da));
	h = gtk_widget_get_allocated_height(GTK_WIDGET(gc->da));
	gtk_widget_queue_draw_area(GTK_WIDGET(gc->da), 0, 0, w, h);
	return G_SOURCE_CONTINUE;
}


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

	return FALSE;
}


static gint realise_sig(GtkWidget *da, struct glitchyclock *gc)
{
	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 16");

	return FALSE;
}


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

	gc.nglitch = 0;
	gc.glitch_base = get_monotonic_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);

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

	g_timeout_add(1000, redraw_cb, &gc);

	gtk_main();

	return 0;
}