Login Register






Basic Alarm Clock [C] filter_list
Author
Message
Basic Alarm Clock [C] #1
Hello guys..I am learning GTK+ in C.So i decided to create a scheduler although i managed to create only alarm clock till now and that too for all days.I just wanna share it with you[dont know why!!].

Current Features:
Simple Alarm Clock
Change Alarm Sound
View About Tongue
Exit

Features Under Development:
Alarms on Day to Day
Sticky Notes
Schedular

P.S. --> I know there are some unused variables but they are for further development that is going on

Place a file named 1.mp3 and an empty file alarms.djgg before running this in Ubuntu.
You will Need gtk 3.0+ to run this.
Compilation code
Code:
gcc -o s FILENAME.c `pkg-config --libs --cflags gtk+-3.0` -w


FILE CODE:
Code:
Code:
#include <gtk/gtk.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <regex.h> #include <glib.h> #include <unistd.h> GtkWidget *window,*calender_months,calender_days,*window2,*test,*alarm_freq,*dialog2,*entr; GtkWidget *view,*not_success,*not_fail,*freq_selected; GtkWidget *vbox,*entry,*label,*label2,*newalarmbutton,*progress; GtkWidget *menubar,*optmenu,*options,*help; GtkWidget *newmenu,*setmenu,*settings,*alarmsettings,*preferences; GtkWidget *quit,*open,*new,*cal,*sticky,*fixed,*next_alarm; GtkWidget *entry1,*format,*newsubmit,*loading_text,*mpg ; int played = 1; GtkWidget *freq1,*freq2,*final_alarm; char hours[3],mins[3],secs[3],distime[15],temp[30],temp2[30],nxt[15]; char mc[10],bc[10]; char alarm_audio[100]; int progress_flag = 0,sel1,sel2; char days[2],months[2],years[5],showdate[15],disdate[15]=""; const gchar *text; const gchar *updated_message; char *markup; char str[6]; int not_fail_flag = 1; int alarm_found = 0; int not_success_flag = 1; int final_alarm_shown = 0; struct get_alarm_list { int a_hr,a_min; char a_type[6]; struct get_alarm_list *next; }*start = NULL; void play_music(); void get_alarms(); void show_preferences_done() { updated_message = gtk_entry_get_text(GTK_ENTRY(entr)); strcpy(alarm_audio,updated_message); gtk_widget_destroy(dialog2); } void show_preferences() { GtkWidget *label, *content_area; char message[50]; strcpy(message,"Enter the mp3 file for alarm"); /* Create the widgets */ dialog2 = gtk_dialog_new_with_buttons ("Alarm Audio", window, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, GTK_RESPONSE_NONE, NULL); content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog2)); label = gtk_label_new (message); entr = gtk_entry_new(); gtk_entry_set_text(GTK_ENTRY(entr),alarm_audio); g_signal_connect_swapped (dialog2,"response",G_CALLBACK (show_preferences_done),NULL); gtk_container_add (GTK_CONTAINER (content_area), label); gtk_container_add (GTK_CONTAINER (content_area), entr); gtk_widget_show_all (dialog2); } void lol() { GtkWidget *aboutdialog = gtk_about_dialog_new(); gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(aboutdialog), "GD Schedular"); gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(aboutdialog), "1.0"); gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(aboutdialog), "Software: GD Schedular\nCreated by Dhruv Jain and Geetika Guleria"); gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(aboutdialog), "http://hackarchives.org/"); gtk_dialog_run(GTK_DIALOG(aboutdialog)); gtk_widget_destroy(aboutdialog); } void reset_after_alarm_creation() { get_alarms(); gtk_widget_hide(not_success); gtk_widget_hide(freq2); gtk_widget_hide(freq1); final_alarm = gtk_label_new(NULL); gtk_label_set_markup(GTK_LABEL(final_alarm), "<span font_desc=\"15.0\" foreground=\"#0B610B\">Alarm has been created</span>"); gtk_fixed_put(GTK_FIXED(fixed), final_alarm, 230, 50); gtk_widget_show(final_alarm); final_alarm_shown = 1; gtk_widget_show(newalarmbutton); } void new_alarm_create_all() { FILE *fp3; fp3 = fopen("alarms.djgg","a"); fprintf(fp3,"ALL\n"); fprintf(fp3,"%s\n",str); fclose(fp3); reset_after_alarm_creation(); // get_alarms(); } void new_alarm_create_once() { FILE *fp3; fp3 = fopen("alarms.djgg","a"); fprintf(fp3,"ONCE\n"); fprintf(fp3,"%s\n",str); fclose(fp3); reset_after_alarm_creation(); } void new_alarm_activated(GtkWidget *widget, gpointer data) { text = gtk_entry_get_text(GTK_ENTRY(entry1)); strcpy(str,text); //WE NEED TO FIX THE REGEX char pattern[]="([0[0-9]|1[0-9]|2[0-3]):[0-59]"; regex_t reg; regcomp(&reg, pattern, REG_EXTENDED|REG_NOSUB); if(regexec(&reg,str,1,NULL,0) != 0) { if(not_fail_flag == 1) { not_fail = gtk_label_new(NULL); gtk_label_set_markup (GTK_LABEL (not_fail), "<span foreground=\"red\">Invalid Alarm Format</span>"); gtk_fixed_put(GTK_FIXED(fixed), not_fail, 230, 20); gtk_widget_set_size_request(not_fail, 170, 50); gtk_widget_show(not_fail); not_fail_flag = 0; } } else { if(not_fail_flag == 0) { not_fail_flag = 1; // edit_alarm_file(); gtk_widget_hide(not_fail); } not_success = gtk_label_new("Select the frequency of alarm"); gtk_fixed_put(GTK_FIXED(fixed), not_success, 230, 50); gtk_widget_set_size_request(not_success, 170, 50); gtk_widget_show(not_success); freq1 = gtk_button_new_with_label("All Days"); gtk_fixed_put(GTK_FIXED(fixed), freq1, 130, 100); gtk_widget_set_size_request(freq1, 170, 50); gtk_widget_show(freq1); freq2 = gtk_button_new_with_label("Only Once"); gtk_fixed_put(GTK_FIXED(fixed), freq2, 330, 100); gtk_widget_set_size_request(freq2, 170, 50); gtk_widget_show(freq2); g_signal_connect(G_OBJECT(freq1), "clicked",G_CALLBACK(new_alarm_create_all), NULL); g_signal_connect(G_OBJECT(freq2), "clicked",G_CALLBACK(new_alarm_create_once), NULL); gtk_widget_hide(newsubmit); gtk_widget_hide(entry1); gtk_widget_hide(format); not_success_flag = 0; } } void new_alarm(GtkWidget *widget, gpointer window) { if(not_success_flag == 0) { gtk_widget_hide(not_success); } if(final_alarm_shown == 1) { gtk_widget_hide(final_alarm); } entry1 = gtk_entry_new(); format = gtk_label_new("HH:MM"); gtk_label_set_markup(GTK_LABEL(format), "<span font_desc=\"30.0\">HH : MM</span>"); gtk_widget_hide(newalarmbutton); gtk_fixed_put(GTK_FIXED(fixed), format, 230, 50); gtk_fixed_put(GTK_FIXED(fixed), entry1, 230, 100); gtk_widget_set_size_request(entry1, 25, 50); newsubmit = gtk_button_new_with_label("Next"); gtk_fixed_put(GTK_FIXED(fixed), newsubmit, 230, 200); gtk_widget_set_size_request(newsubmit, 170, 50); gtk_widget_show(newsubmit); gtk_widget_show(entry1); gtk_widget_show(format); g_signal_connect(G_OBJECT(newsubmit), "clicked",G_CALLBACK(new_alarm_activated), NULL); } int day=0,Month=0,Year=0,hour=0,min=0,sec=0; void currentday() { struct tm *Sys_T = NULL; time_t Tval = 0; Tval = time(NULL); Sys_T = localtime(&Tval); float Day=Sys_T->tm_mday; Month=Sys_T->tm_mon+1; day = (int)Day; Year=1900 + Sys_T->tm_year; } showtime() { time_t epoch_time; struct tm *tm_p; epoch_time = time( NULL ); tm_p = localtime( &epoch_time ); hour = tm_p->tm_hour; min = tm_p->tm_min; sec = tm_p->tm_sec; strcpy(distime,""); strcpy(temp2,""); snprintf(hours, 3, "%d", hour); snprintf(mins, 3, "%d", min); snprintf(secs, 3, "%d", sec); strcpy(distime,hours); strcat(distime," : "); strcat(distime,mins); strcat(distime," : "); strcat(distime,secs); strcpy(temp2,"Current Time : "); strcat(temp2,distime); gtk_label_set_text(GTK_LABEL(label2),temp2); } int total_days; void get_alarms() { FILE *fp; fp = fopen("alarms.djgg","r"); char line[20],tmp1[4],tmp2[10],tmp3[10]; int tmp_hr,tmp_min,flag=0,counter=1; struct get_alarm_list *tmp; while(fgets(line,20,fp) != NULL) { flag = 0; int i = 0,tmpcount=0; int h,j; h = strlen(line)-1; if(counter%2 == 1) { tmp = (struct get_alarm_list *)malloc(sizeof(tmp)); strcpy(tmp->a_type,line); } if(counter%2 == 0) { for(i=0;i<h;i++) { if(line[i] == ':') { flag = 1; j = 0; continue; } if(line[i] == '\n') { break; } if(flag == 0) { tmp2[i] = line[i]; } else if(flag == 1) { tmp3[j] = line[i]; j++; } } if(flag == 1) { tmp->a_hr = atoi(tmp2); tmp->a_min = atoi(tmp3); tmp->next = NULL; if(start == NULL) { start = tmp; } else { struct get_alarm_list *p; p = start; while(p->next != NULL) { p = p->next; } p->next = tmp; } } } counter++; } fclose(fp); } void get_next_alarm() { if(start != NULL) { time_t epoch_time; struct tm *tm_p; epoch_time = time( NULL ); tm_p = localtime( &epoch_time ); hour = tm_p->tm_hour; min = tm_p->tm_min; sec = tm_p->tm_sec; struct get_alarm_list *tmp; tmp = (struct get_alarm_list *)malloc(sizeof(tmp)); tmp = start; struct alarms { int hrs; int mins; }b[20]; struct alarms nmp; int count = 0; while(tmp != NULL) { b[count].hrs = tmp->a_hr; b[count].mins = tmp->a_min; tmp = tmp->next; count++; } int temp3,i,j; for(i=0;i<count;i++) { temp3 = b[i].hrs; for(j=0;j<count;j++) { if(temp3<b[j].hrs) { nmp = b[i]; b[i] = b[j]; b[j] = nmp; } else if(temp3 == b[j].hrs) { if(b[i].mins<b[j].mins) { nmp = b[i]; b[i] = b[j]; b[j] = nmp; } } } } struct alarms mp; temp3 = hour; int found = 0; for(i=0;i<count;i++) { if((temp3-b[i].hrs) == 0) { if(min <= b[i].mins) { mp = b[i]; found = 1; break; } } else if((temp3-b[i].hrs)<0) { mp = b[i]; found = 1; break; } } if(found == 0) { for(i=0;i<count;i++) { if((hour-b[i].hrs)>0) { mp = b[i]; break; } } } int found2=0,count2=0,temp4,min_left; sel1 = mp.hrs; sel2 = mp.mins; if(((sel1 - hour) == 0) && ((sel2 - min) == 0)) { play_music(); played = 0; } else { played = 1; } snprintf(bc, 3, "%d", sel1); snprintf(mc, 4, "%d", sel2); strcat(bc,":"); strcat(bc,mc); strcpy(nxt,"Next Alarm : "); gtk_label_set_text(GTK_LABEL(next_alarm),strcat(nxt,bc)); } } void play_music() { if(played == 1) { pid_t childPID; childPID = fork(); if(childPID >= 0) // fork was successful { if(childPID == 0) // child process { execlp("mpg123", "mpg123", "-q", alarm_audio, 0); played = 0; } } } } int check_mpg() { if(system("mpg321 -h >/dev/null 2>&1") == 0) { return 1; } else { return 0; } } float percent = 0.0; static gboolean inc_progress(gpointer data) { if(percent <= 2.0) { percent = percent + 0.1; } gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress),percent); if(percent >= 2.0) { gtk_widget_hide(progress); gtk_widget_hide(loading_text); if(check_mpg() == 0) { mpg = gtk_label_new("MPG321 Package Not found\n\nRun: 'sudo apt-get install mpg321' in the terminal(without the quotes)"); gtk_fixed_put(GTK_FIXED(fixed), mpg, 50, 50); gtk_widget_show(mpg); } else { if(progress_flag == 0) { pid_t childPID; childPID = fork(); if(childPID >= 0) // fork was successful { if(childPID == 0) // child process { execlp("mpg123", "mpg123", "-q", "2.mp3", 0); } } gtk_widget_show(next_alarm); gtk_widget_show(label); gtk_widget_show(label2); gtk_widget_show(newalarmbutton); } } progress_flag = 1; } } int main( int argc, char *argv[]) { g_timeout_add (100, (GSourceFunc) showtime,NULL); g_timeout_add (999, (GSourceFunc) get_next_alarm,NULL); GtkWidget *alarm; gtk_init(&argc, &argv); strcpy(alarm_audio,"1.mp3"); get_alarms(); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); progress = gtk_progress_bar_new(); gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress),0.0); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress),"50%"); gtk_progress_bar_set_ellipsize(GTK_PROGRESS_BAR(progress),PANGO_ELLIPSIZE_MIDDLE); if(progress_flag == 0) g_timeout_add(400,inc_progress,progress); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(window), 400, 300); gtk_window_set_resizable (GTK_WINDOW(window), FALSE); gtk_window_set_title(GTK_WINDOW(window), "menu"); fixed = gtk_fixed_new(); gtk_container_add(GTK_CONTAINER(window), fixed); menubar = gtk_menu_bar_new(); setmenu = gtk_menu_new(); newmenu = gtk_menu_new(); optmenu = gtk_menu_new(); options = gtk_menu_item_new_with_label("Options"); help = gtk_menu_item_new_with_label("About"); quit = gtk_menu_item_new_with_label("Quit"); new = gtk_menu_item_new_with_label("New"); sticky = gtk_menu_item_new_with_label("Sticky Note"); alarm = gtk_menu_item_new_with_label("Alarm"); cal = gtk_menu_item_new_with_label("Calender Entry"); settings = gtk_menu_item_new_with_label("Settings"); alarmsettings = gtk_menu_item_new_with_label("Alarm Settings"); preferences = gtk_menu_item_new_with_label("Preferences"); gtk_menu_item_set_submenu(GTK_MENU_ITEM(options), optmenu); gtk_menu_item_set_submenu(GTK_MENU_ITEM(new), newmenu); gtk_menu_item_set_submenu(GTK_MENU_ITEM(settings), setmenu); gtk_menu_shell_append(GTK_MENU_SHELL(optmenu), help); gtk_menu_shell_append(GTK_MENU_SHELL(optmenu), quit); gtk_menu_shell_append(GTK_MENU_SHELL(setmenu), alarmsettings); gtk_menu_shell_append(GTK_MENU_SHELL(setmenu), preferences); gtk_menu_shell_append(GTK_MENU_SHELL(newmenu), sticky); gtk_menu_shell_append(GTK_MENU_SHELL(newmenu), alarm); gtk_menu_shell_append(GTK_MENU_SHELL(newmenu), cal); gtk_menu_shell_append(GTK_MENU_SHELL(menubar), new); gtk_menu_shell_append(GTK_MENU_SHELL(menubar), settings); gtk_menu_shell_append(GTK_MENU_SHELL(menubar), options); gtk_fixed_put(GTK_FIXED(fixed), menubar, 0, 0); currentday(); snprintf(days, 2, "%d", day); snprintf(months, 2, "%d", Month); snprintf(years, 5, "%d", Year); strcpy(disdate,days); strcat(disdate," - "); strcat(disdate,months); strcat(disdate," - "); strcat(disdate,years); strcpy(temp,"Current Date : "); label2 = gtk_label_new(NULL); next_alarm = gtk_label_new(NULL); loading_text = gtk_label_new("Loading The Scripts :)"); gtk_fixed_put(GTK_FIXED(fixed), progress, 0, 0); gtk_fixed_put(GTK_FIXED(fixed), loading_text, 3, 18); gtk_widget_show_all(window); gtk_fixed_put(GTK_FIXED(fixed), next_alarm, 500, 65); label = gtk_label_new(strcat(temp,disdate)); gtk_fixed_put(GTK_FIXED(fixed), label, 500, 15); gtk_fixed_put(GTK_FIXED(fixed), label2, 500, 40); newalarmbutton = gtk_button_new_with_label("New Alarm"); gtk_fixed_put(GTK_FIXED(fixed), newalarmbutton, 230, 100); gtk_widget_set_size_request(newalarmbutton, 200, 50); g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); g_signal_connect(G_OBJECT(help), "activate",G_CALLBACK(lol), NULL); g_signal_connect(G_OBJECT(preferences), "activate",G_CALLBACK(show_preferences), NULL); g_signal_connect(G_OBJECT(newalarmbutton), "clicked",G_CALLBACK(new_alarm), NULL); gtk_main(); return 0; }

Reply

RE: Basic Alarm Clock [UBUNTU] #2
Nice to see you are back Smile
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: Basic Alarm Clock [C] #3
Me too.I have been damn busy...Too many projects and broken hearts to block my way Sad

Reply

RE: Basic Alarm Clock [C] #4
I read the mention of GTK+ and couldn't resist thanking you along with a comment to the same.:lol:

Reply

RE: Basic Alarm Clock [C] #5
(04-25-2013, 10:20 PM)Linuxephus Wrote: I read the mention of GTK+ and couldn't resist thanking you along with a comment to the same.:lol:

I would reply if i could understand

Reply

RE: Basic Alarm Clock [C] #6
(04-26-2013, 01:48 AM)hackarchives Wrote: I would reply if i could understand

You just did reply with enough understanding to make a comment.:lol:
Not to mention, essentially, what I stated is; your mention of learning GTK+ is what attracted my interest, so I Thanked you along with using your thread as an excuse to have a reason to comment.:thumbs:

Reply

RE: Basic Alarm Clock [C] #7
(04-26-2013, 01:54 AM)Linuxephus Wrote:
(04-26-2013, 01:48 AM)hackarchives Wrote: I would reply if i could understand

You just did reply with enough understanding to make a comment.:lol:
Not to mention, essentially, what I stated is; your mention of learning GTK+ is what attracted my interest, so I Thanked you along with using your thread as an excuse to have a reason to comment.:thumbs:

I understand :angel:

Learning socket programming in C .Expect some tools soon Smile

Reply

RE: Basic Alarm Clock [C] #8
(04-26-2013, 01:58 AM)hackarchives Wrote: I understand :angel:

Learning socket programming in C .Expect some tools soon Smile

Thank God, because sometimes I barely understand myself.Wink

Reply

RE: Basic Alarm Clock [C] #9
(06-16-2013, 04:21 AM)moji Wrote: Can you run it on mint aswell with the same compilation code?

I think it should but you will need gtk and gcc installed

Reply