-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomo.c
216 lines (169 loc) · 4.98 KB
/
pomo.c
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
//code by Robert Grider
//Mar 2, 2010
#include <gtk/gtk.h>
#include <stdlib.h>
#define NUMMINS 25
int numsecs;
guint tickid;
GtkTextBuffer * buf;
struct pomo_prog pp;
typedef enum{
BEGIN,
POMODORO,
POMO_TO_BREAK,
BREAK,
NUM_STATES}
Prog_State;
struct pomo_prog {
Prog_State state;
int pomo_length; //pomodoro length, in minutes
int break_length; //break length, in minutes
int seconds_remaining; //seconds remaining in the current pomodoro
GtkTextBuffer * texbuf; //holds the text buffer that is rendered into
GtkWindow * win; //holds the window
GtkButton * button; //holds the button
};
//initializes the incoming program
void init_pomo_prog(struct pomo_prog * p,
GtkTextBuffer * t,
GtkWindow * w,
GtkButton * b){
p->pomo_length = 25;
p->break_length = 5;
p->texbuf = t;
p->win = w;
p->button = b;
p->seconds_remaining = 0;
p->state = BEGIN;
return;
}
void getClockString( int numsecs, char * strret){
//get remaining number of seconds
int displaysecs = numsecs % 60;
int displaymins = numsecs / 60;
//flush to buffer
sprintf(strret, "%i:%i", displaymins, displaysecs);
if(displaysecs < 10){
sprintf(strret, "%i:0%i", displaymins, displaysecs);
}
return;
}
gboolean tick (gpointer data){
struct pomo_prog * p = (struct pomo_prog *) data;
p->seconds_remaining = p->seconds_remaining >= 1 ? (p->seconds_remaining - 1) : 0;
char dispstr[128];
char windowstr[128];
char buttonstr[128];
switch (p->state)
{
case BEGIN:
sprintf(windowstr, "Pomodoro!");
sprintf(dispstr, "Ready?");
sprintf(buttonstr, "Go!");
break;
case POMODORO:
getClockString( p->seconds_remaining, dispstr);
sprintf(windowstr, "Pomodoro! - %s", dispstr);
sprintf(buttonstr, "Squash?");
if( 0 == p->seconds_remaining ){
p->state = POMO_TO_BREAK;
}
break;
case POMO_TO_BREAK:
sprintf(windowstr, "Break time!");
sprintf(dispstr, "Break!");
sprintf(buttonstr, "Begin");
break;
case BREAK:
getClockString( p->seconds_remaining, dispstr);
sprintf(windowstr, "Break! - %s", dispstr);
sprintf(buttonstr, "Enjoy your break!");
if( 0 == p->seconds_remaining ){
gtk_widget_set_sensitive( GTK_WIDGET(p->button), TRUE );
p->state = BEGIN;
}
break;
default:
break;
}
gtk_text_buffer_set_text(buf, dispstr, -1);
gtk_window_set_title( p->win, windowstr);
gtk_button_set_label( p->button, buttonstr);
return TRUE;
}
void click_event(GtkWidget * g, gpointer data ){
struct pomo_prog *p = (struct pomo_prog *)data;
switch( p->state)
{
case BEGIN:
p->state = POMODORO;
p->seconds_remaining = p->pomo_length * 60 + 1;
break;
case POMODORO:
//when it's a Pomodoro, the button with say "squash?", so we transition back to begin
p->state = BEGIN;
p->seconds_remaining = 0;
break;
case BREAK:
//don't need to worry about the button when we're on break, it will be set as inactive
break;
case POMO_TO_BREAK:
p->state = BREAK;
p->seconds_remaining = p->break_length * 60 + 1;
gtk_widget_set_sensitive( GTK_WIDGET( p->button ), FALSE);
break;
default:
break;
}
}
int main ( int argc, char ** argv ) {
GtkBuilder *builder;
GtkWidget * window;
GtkWidget * button;
GtkWidget * textview;
GtkTextView * mytextview;
guint clickint;
GError * error = NULL;
//pp = malloc(sizeof(struct pomo_prog));
//initialize
gtk_init(&argc, &argv);
//make the builder
builder = gtk_builder_new();
//load the pomo UI
if(! gtk_builder_add_from_file( builder, "pomo.glade", &error) ){
g_warning( "%s", error->message);
g_free( error );
return( 1 );
}
//get the window object
window = GTK_WIDGET ( gtk_builder_get_object(builder, "window1") );
//get the button object
button = GTK_WIDGET ( gtk_builder_get_object(builder, "button1") );
//get the textview object
textview = GTK_WIDGET (gtk_builder_get_object(builder, "textview1") );
mytextview = GTK_TEXT_VIEW( textview);
//set buffer
buf = gtk_text_buffer_new(NULL);
gtk_text_view_set_buffer(mytextview, buf);
//set the font size of the textview to be something larger than 12 point Times NewRoman
PangoFontDescription * fnt = pango_font_description_from_string( "Serif 36");
gtk_widget_modify_font( textview, fnt );
pango_font_description_free(fnt);
//get the program state data structure filled out
init_pomo_prog(&pp, buf, GTK_WINDOW( window ), GTK_BUTTON( button ) );
printf("length: %i", pp.pomo_length);
//conect signals
//timeout
tickid = g_timeout_add( 1000, tick , &pp);
//click signal
clickint = g_signal_connect( G_OBJECT(button) , "clicked", G_CALLBACK( click_event),(gpointer) &pp);
//miscellaneous
gtk_builder_connect_signals( builder, NULL );
//destroy builder
g_object_unref( G_OBJECT( builder ) );
//show and run
gtk_widget_show( window );
gtk_main();
//free(pp);
return( 0 );
}