I just finished making a C program that works as a rudimentary(emphasis on "rudimentary") alarm clock, you start the alarm process by specifying the amount of time that should be elapsed before sounding the alarm, it also has a GTK UI that allows the user to easily end the process without having to do a kill. The code is given below:-
Code:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<gtk/gtk.h>
#include<sys/types.h>
#include<signal.h>
/* We need this global variable to hold the child's PID
* so that it can be accessed by the stop_app() procedure.
*/
pid_t child;
void alarm_daemon(){
int hour, minute;
long int curr_time,end_time;
printf("Enter the amount of time to be elapsed to start the alarm in HH:MM:- ");
scanf("%d:%d",&hour,&minute);
end_time = hour * 60 * 60;
end_time += minute * 60;
end_time += (long int) time(NULL);
for(;;){
curr_time = (long int) time(NULL);
if(curr_time >= end_time) break;
sleep(900);
}
}
void alarm_bell(){
for(;;){
if(system("echo \"Yo\"")){
printf("Fix your system dawg!");
break;
}
sleep(1);
}
}
void stop_app(){
kill(child,SIGKILL);
gtk_main_quit();
}
gboolean delete_event_handler(GtkWidget *widget, GdkEvent *event,
gpointer user_data)
{
return (FALSE);
}
void gtk_front(int argc, char *argv[]){
GtkWidget *gtk_test, *vbox, *sleepy_label, *stop_button, *separator;
gtk_init(&argc,&argv);
gtk_test = gtk_window_new(GTK_WINDOW_TOPLEVEL);
vbox = gtk_vbox_new(TRUE,10);
sleepy_label = gtk_label_new("Got up yet?");
stop_button = gtk_button_new_with_label("Yes");
separator = gtk_hseparator_new();
gtk_window_set_title(GTK_WINDOW(gtk_test),"Alaaarm!");
gtk_container_set_border_width(GTK_CONTAINER(gtk_test),50);
gtk_window_set_policy(GTK_WINDOW(gtk_test),FALSE,FALSE,FALSE);
gtk_signal_connect(GTK_OBJECT(stop_button),"clicked",stop_app,NULL);
gtk_signal_connect(GTK_OBJECT(gtk_test),"delete-event",GTK_SIGNAL_FUNC(delete_event_handler),NULL);
gtk_signal_connect(GTK_OBJECT(gtk_test),"destroy",GTK_SIGNAL_FUNC(stop_app),NULL);
gtk_container_add(GTK_CONTAINER(gtk_test),vbox);
gtk_box_pack_start_defaults(GTK_BOX(vbox),sleepy_label);
gtk_box_pack_start_defaults(GTK_BOX(vbox),separator);
gtk_box_pack_start_defaults(GTK_BOX(vbox),stop_button);
gtk_widget_show_all(gtk_test);
gtk_main();
return;
}
int main(int argc,char *argv[]){
alarm_daemon();
/* We fork the process so that such a situation occurs:-
* One process is the inifinite loop process that keeps doing a certain action
* again and again, sort of like a real world alarm clock(Child).
*
* The other process is the GUI to the alarm clock and allows the user to
* terminate the entire alarm by pressing the "OK" button(Parent).
*/
child = fork();
// The child process is delegated the alarm_bell().
if (!child) alarm_bell();
// The parent process is given the GUI task.
else if (child) gtk_front(argc,argv);
// Once the user presses the "OK" button, we start a sequence of events, each spaced by a 1 second interval and then end the process.
sleep(1);
system("echo \"what\"");
sleep(1);
system("echo \"Yo\"");
sleep(1);
system("echo \"Hello\"");
sleep(1);
return 0;
}
My own concerns are marked in red. Any feedback, criticism or suggestions to the code are more than welcome(which is why I posted it in the first place).
Bookmarks