~macslow/cairo-countdown/trunk

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
/*******************************************************************************
*23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
*       10        20        30        40        50        60        70        80
*
* count-down - An animated count-down rendition drawn with cairo looking like
* the one seen in this video http://www.youtube.com/watch?v=Fuy2W6gY7hQ and
* intended to be used in the kazam screenrecorder
*
* main.c - entry point
*
* Copyright 2012 Mirco Müller
*
* Authors:
*    Mirco "MacSlow" Müller <macslow@gmail.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
*
*******************************************************************************/

#include <gtk/gtk.h>

#include "events.h"

#define FPS             24
#define HEIGHT_FRACTION 2.5
#define COUNTDOWN_STEPS 5

int main (int    argc,
		  char** argv)
{
	gtk_init (&argc, &argv);

	/* create window for drawing */
	GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_app_paintable (window, TRUE);
	gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
	gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
	gtk_window_set_title (GTK_WINDOW (window), "cairo-countdown");
	gtk_window_set_keep_above (GTK_WINDOW (window), TRUE);
	gtk_window_set_focus_on_map (GTK_WINDOW (window), FALSE);
	gtk_window_set_accept_focus (GTK_WINDOW (window), FALSE);
	gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE);
	gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);

	/* make window click-through */
	cairo_region_t* region = NULL;
	const cairo_rectangle_int_t rect = {0, 0, 1, 1};
	region = cairo_region_create_rectangle (&rect);
	if (cairo_region_status (region) == CAIRO_STATUS_SUCCESS)
	{
		gtk_widget_input_shape_combine_region (window, NULL);
		gtk_widget_input_shape_combine_region (window, region);
	}
	cairo_region_destroy (region);

	/* make sure the window opens with a RGBA-visual */
	screen_changed_cb (window, NULL, NULL);
	gtk_widget_realize (window);
	GdkRGBA transparent = {0.0, 0.0, 0.0, 0.0};
	gtk_window_set_type_hint (GTK_WINDOW (window),
				  GDK_WINDOW_TYPE_HINT_DOCK);
	gdk_window_set_background_rgba (gtk_widget_get_window (window),
									&transparent);

	/* center window */
	GdkScreen* screen = gtk_widget_get_screen (window);
	gint monitor = gdk_screen_get_monitor_at_window (screen,
													 gtk_widget_get_window (window));
	GdkRectangle geo = {0, 0, 0, 0};
	gdk_screen_get_monitor_geometry (screen, monitor, &geo);
	int size = size = geo.height / HEIGHT_FRACTION;
	gtk_widget_set_size_request (window, size, size);
	gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);

	gtk_widget_show_all (window);

	/* start timer and timeout */
	g_timeout_add (1000 / FPS, timeout_cb, window);
    GTimer* timerId = g_timer_new ();

	/* hook up signal-callbacks */
	g_signal_connect (G_OBJECT (window),
					  "screen-changed",
					  G_CALLBACK (screen_changed_cb),
					  NULL);
	g_signal_connect (G_OBJECT (window),
					  "configure-event",
					  G_CALLBACK (configure_event_cb),
					  NULL);
	g_signal_connect (G_OBJECT (window),
					  "draw",
					  G_CALLBACK (draw_cb),
					  timerId);
	g_signal_connect (G_OBJECT (window),
					  "delete-event",
					  G_CALLBACK (delete_event_cb),
					  NULL);

	/* let it tick */
	gtk_main ();

	/* clean up */
	g_timer_destroy (timerId);

	return 0;
}