void handle_init(void) { window = window_create(); tick_timer_service_subscribe(MINUTE_UNIT, tick_handler); accel_tap_service_subscribe(tap_handler); hour_text_layer = text_layer_create(GRect(0, 0, 144, 107)); text_layer_set_background_color(hour_text_layer, GColorBlack); text_layer_set_text_color(hour_text_layer, GColorWhite); text_layer_set_text(hour_text_layer, "00"); text_layer_set_font(hour_text_layer, fonts_get_system_font(FONT_KEY_ROBOTO_BOLD_SUBSET_49)); //TODO: Make the numbers prettier. Maybe use custom font/rendering? text_layer_set_text_alignment(hour_text_layer, GTextAlignmentCenter); minute_text_layer = text_layer_create(GRect(0, 107, 144, 61)); text_layer_set_background_color(minute_text_layer, GColorBlack); text_layer_set_text_color(minute_text_layer, GColorWhite); text_layer_set_text(minute_text_layer, "00"); text_layer_set_font(minute_text_layer, fonts_get_system_font(FONT_KEY_ROBOTO_BOLD_SUBSET_49)); text_layer_set_text_alignment(minute_text_layer, GTextAlignmentCenter); layer_add_child(window_get_root_layer(window), text_layer_get_layer(hour_text_layer)); layer_add_child(window_get_root_layer(window), text_layer_get_layer(minute_text_layer)); update_hour(); update_minute(); window_stack_push(window, true); }
//updates teh top row with current minutes void update_min(char * top_row, int *minutes_ptr, int *hours_ptr, int *day, int *month, int *year, int am_pm_mode) { // Set our maximum minutes int min_max = 60; // Increment our minutes variable (*minutes_ptr) = (*minutes_ptr) + 1; if ((*minutes_ptr) == min_max) { // Update the hour update_hour(top_row, hours_ptr, day, month, year, am_pm_mode); // Reset the minutes (*minutes_ptr) = 0; } // Write the new minutes out to the display top_row[min_1] = '0' + ((*minutes_ptr) - ((*minutes_ptr) % 10)) / 10; top_row[min_2] = '0' + (*minutes_ptr) % 10; }
static void update_time() { bool bDow = persist_exists(XKEY_VIS_DOW) ? persist_read_bool(XKEY_VIS_DOW) : true; bool bDate = persist_exists(XKEY_VIS_DATE) ? persist_read_bool(XKEY_VIS_DATE) : true; char cDate[7] = ""; if (bDate) {strcat(cDate, "%d. %B");} char cDow[3] = ""; if (bDow) {strcat(cDow, "%a");} char cDateFormat[13] = ""; strcat(cDateFormat, cDate); strcat(cDateFormat, "%n"); strcat(cDateFormat, cDow); // Get a tm structure time_t temp = time(NULL); struct tm *tick_time = localtime(&temp); if ((tick_time->tm_min)==0) { update_hour(); }; // Write the current hours and minutes into a buffer static char s_buffer[8]; strftime(s_buffer, sizeof(s_buffer), clock_is_24h_style() ? "%H:%M" : "%I:%M", tick_time); static char s_bufferd[30]; strftime(s_bufferd, sizeof(s_bufferd), cDateFormat, tick_time); // Display this time on the TextLayer text_layer_set_text(s_time_layer, s_buffer); // Display this date on the TextLayer text_layer_set_text(s_date_layer, s_bufferd); }
int main(int argc, char **argv) { int c; /* Alloc ttyclock */ ttyclock = malloc(sizeof(ttyclock_t)); ttyclock->option.date = True; /* Date format */ ttyclock->option.format = malloc(sizeof(char) * 100); /* Default date format */ strncpy(ttyclock->option.format, "%a %d %b %Y", 100); /* Default color */ ttyclock->option.color = COLOR_GREEN; /* COLOR_GREEN = 2 */ /* Default bottom pos */ ttyclock->option.bottom = False; /* Default delay */ ttyclock->option.delay = 40000000; /* 25FPS */ /* Default blink */ ttyclock->option.blink = False; while ((c = getopt(argc, argv, "tvsrcbihf:DBd:C:")) != -1) { switch(c) { case 'h': default: printf("usage : tty-clock [-scbtrvihDB] [-C [0-7]] [-f format] \n" " -s Show seconds \n" " -c Set the clock at the center of the terminal \n" " -b Set the clock at the bottom of the terminal \n" " -C [0-7] Set the clock color \n" " -t Set the hour in 12h format \n" " -r Do rebound the clock \n" " -f format Set the date format \n" " -v Show tty-clock version \n" " -i Show some info about tty-clock \n" " -h Show this page \n" " -d delay Set the delay between two redraws of the clock \n" " -D Hide date \n" " -B Enable blinking colon \n"); free(ttyclock); exit(EXIT_SUCCESS); break; case 'i': puts("TTY-Clock 2 © by Martin Duquesnoy ([email protected])"); free(ttyclock); free(ttyclock->option.format); exit(EXIT_SUCCESS); break; case 'v': puts("TTY-Clock 2 © devel version"); free(ttyclock); free(ttyclock->option.format); exit(EXIT_SUCCESS); break; case 's': ttyclock->option.second = True; break; case 'c': ttyclock->option.center = True; break; case 'b': ttyclock->option.bottom = True; break; case 'C': if(atoi(optarg) >= 0 && atoi(optarg) < 8) ttyclock->option.color = atoi(optarg); break; case 't': ttyclock->option.twelve = True; break; case 'r': ttyclock->option.rebound = True; break; case 'f': strncpy(ttyclock->option.format, optarg, 100); break; case 'd': if(atol(optarg) >= 0 && atol(optarg) < 1000000000) ttyclock->option.delay = atol(optarg); break; case 'D': ttyclock->option.date = False; break; case 'B': ttyclock->option.blink = True; break; } } init(); while(ttyclock->running) { clock_rebound(); update_hour(); draw_clock(); key_event(); } free(ttyclock); free(ttyclock->option.format); endwin(); return 0; }
void key_event(void) { int i, c; struct timespec length = { 0, ttyclock->option.delay }; switch(c = wgetch(stdscr)) { case KEY_UP: case 'k': case 'K': if(ttyclock->geo.x >= 1 && !ttyclock->option.center) clock_move(ttyclock->geo.x - 1, ttyclock->geo.y, ttyclock->geo.w, ttyclock->geo.h); break; case KEY_DOWN: case 'j': case 'J': if(ttyclock->geo.x <= (LINES - ttyclock->geo.h - DATEWINH) && !ttyclock->option.center) clock_move(ttyclock->geo.x + 1, ttyclock->geo.y, ttyclock->geo.w, ttyclock->geo.h); break; case KEY_LEFT: case 'h': case 'H': if(ttyclock->geo.y >= 1 && !ttyclock->option.center) clock_move(ttyclock->geo.x, ttyclock->geo.y - 1, ttyclock->geo.w, ttyclock->geo.h); break; case KEY_RIGHT: case 'l': case 'L': if(ttyclock->geo.y <= (COLS - ttyclock->geo.w - 1) && !ttyclock->option.center) clock_move(ttyclock->geo.x, ttyclock->geo.y + 1, ttyclock->geo.w, ttyclock->geo.h); break; case 'q': case 'Q': ttyclock->running = False; break; case 's': case 'S': set_second(); break; case 't': case 'T': ttyclock->option.twelve = !ttyclock->option.twelve; /* Set the new ttyclock->date.datestr to resize date window */ update_hour(); clock_move(ttyclock->geo.x, ttyclock->geo.y, ttyclock->geo.w, ttyclock->geo.h); break; case 'c': case 'C': set_center(!ttyclock->option.center); break; case 'b': case 'B': set_bottom(!ttyclock->option.bottom); break; case 'r': case 'R': ttyclock->option.rebound = !ttyclock->option.rebound; if(ttyclock->option.rebound && ttyclock->option.center) ttyclock->option.center = False; break; default: nanosleep(&length, NULL); for(i = 0; i < 8; ++i) if(c == (i + '0')) { ttyclock->option.color = i; init_pair(1, ttyclock->bg, i); init_pair(2, i, ttyclock->bg); } break; } return; }
void init(void) { struct sigaction sig; ttyclock->bg = COLOR_BLACK; /* Init ncurses */ initscr(); cbreak(); noecho(); keypad(stdscr, True); start_color(); curs_set(False); clear(); /* Init default terminal color */ if(use_default_colors() == OK) ttyclock->bg = -1; /* Init color pair */ init_pair(0, ttyclock->bg, ttyclock->bg); init_pair(1, ttyclock->bg, ttyclock->option.color); init_pair(2, ttyclock->option.color, ttyclock->bg); refresh(); /* Init signal handler */ sig.sa_handler = signal_handler; sig.sa_flags = 0; sigaction(SIGWINCH, &sig, NULL); sigaction(SIGTERM, &sig, NULL); sigaction(SIGINT, &sig, NULL); sigaction(SIGSEGV, &sig, NULL); /* Init global struct */ ttyclock->running = True; if(!ttyclock->geo.x) ttyclock->geo.x = 0; if(!ttyclock->geo.y) ttyclock->geo.y = 0; if(!ttyclock->geo.a) ttyclock->geo.a = 1; if(!ttyclock->geo.b) ttyclock->geo.b = 1; ttyclock->geo.w = (ttyclock->option.second) ? SECFRAMEW : NORMFRAMEW; ttyclock->geo.h = 7; ttyclock->tm = localtime(&(ttyclock->lt)); ttyclock->lt = time(NULL); update_hour(); /* Create clock win */ ttyclock->framewin = newwin(ttyclock->geo.h, ttyclock->geo.w, ttyclock->geo.x, ttyclock->geo.y); box(ttyclock->framewin, 0, 0); /* Create the date win */ if (ttyclock->option.date) { ttyclock->datewin = newwin(DATEWINH, strlen(ttyclock->date.datestr) + 2, ttyclock->geo.x + ttyclock->geo.h - 1, ttyclock->geo.y + (ttyclock->geo.w / 2) - (strlen(ttyclock->date.datestr) / 2) - 1); box(ttyclock->datewin, 0, 0); clearok(ttyclock->datewin, True); } set_center(ttyclock->option.center); set_bottom(ttyclock->option.bottom); nodelay(stdscr, True); if (ttyclock->option.date) { wrefresh(ttyclock->datewin); } wrefresh(ttyclock->framewin); return; }
int main(int argc, char **argv){ int c; /* Alloc ttyclock */ ttyclock = malloc(sizeof(ttyclock_t)); assert(ttyclock != NULL); memset(ttyclock, 0, sizeof(ttyclock_t)); ttyclock->option.date = True; /* Date format */ ttyclock->option.format = malloc(sizeof(char) * 100); /* Default date format */ strncpy(ttyclock->option.format, "%F", 100); /* Default color */ ttyclock->option.color = COLOR_RED; /* Default delay */ ttyclock->option.delay = 1; /* 1FPS */ ttyclock->option.nsdelay = 0; /* -0FPS */ ttyclock->option.blink = False; /* Never show seconds */ ttyclock->option.second = False; /* Hide the date */ ttyclock->option.date = False; atexit(cleanup); while ((c = getopt(argc, argv, "ivcbrhBxnC:d:T:a:")) != -1){ switch(c) { case 'h': default: print_usage(); exit(EXIT_SUCCESS); break; case 'i': puts("TTY-Clock 2 © by Martin Duquesnoy ([email protected]), Grey ([email protected])"); exit(EXIT_SUCCESS); break; case 'v': puts("TTY-Clock 2 © devel version"); exit(EXIT_SUCCESS); break; case 'c': ttyclock->option.center = True; break; case 'b': ttyclock->option.bold = True; break; case 'C': if(atoi(optarg) >= 0 && atoi(optarg) < 8) ttyclock->option.color = atoi(optarg); break; case 'r': ttyclock->option.rebound = True; break; case 'd': if(atol(optarg) >= 0 && atol(optarg) < 100) ttyclock->option.delay = atol(optarg); break; case 'B': ttyclock->option.blink = True; break; case 'a': if(atol(optarg) >= 0 && atol(optarg) < 1000000000) ttyclock->option.nsdelay = atol(optarg); break; case 'x': ttyclock->option.box = True; break; case 'T': { struct stat sbuf; if (stat(optarg, &sbuf) == -1) { fprintf(stderr, "tty-clock: error: couldn't stat '%s': %s.\n", optarg, strerror(errno)); exit(EXIT_FAILURE); } else if (!S_ISCHR(sbuf.st_mode)) { fprintf(stderr, "tty-clock: error: '%s' doesn't appear to be a character device.\n", optarg); exit(EXIT_FAILURE); } else { if (ttyclock->tty) free(ttyclock->tty); ttyclock->tty = strdup(optarg); }} break; case 'n': ttyclock->option.noquit = True; break; } } /* Set the default minutes to 25 */ start_minutes = DEFAULT_TIME; /* Check if short or long break */ if (optind < argc){ char *argument = argv[optind]; if (!strcmp(argument, "short")){ start_minutes = SHORT_BREAK; }else if (!strcmp(argument, "long")){ start_minutes = LONG_BREAK; }else{ printf("Command not recognized\n"); print_usage(); exit(EXIT_FAILURE); } } init(); attron(A_BLINK); while(ttyclock->running){ clock_rebound(); update_hour(); draw_clock(); key_event(); } endwin(); return 0; }
void init(void){ struct sigaction sig; ttyclock->bg = COLOR_BLACK; /* Init ncurses */ if (ttyclock->tty) { FILE *ftty = fopen(ttyclock->tty, "r+"); if (!ftty) { fprintf(stderr, "tty-clock: error: '%s' couldn't be opened: %s.\n", ttyclock->tty, strerror(errno)); exit(EXIT_FAILURE); } ttyclock->ttyscr = newterm(NULL, ftty, ftty); assert(ttyclock->ttyscr != NULL); set_term(ttyclock->ttyscr); } else initscr(); cbreak(); noecho(); keypad(stdscr, True); start_color(); curs_set(False); clear(); /* Init default terminal color */ if(use_default_colors() == OK) ttyclock->bg = -1; /* Init color pair */ init_pair(0, ttyclock->bg, ttyclock->bg); init_pair(1, ttyclock->bg, ttyclock->option.color); init_pair(2, ttyclock->option.color, ttyclock->bg); refresh(); /* Init signal handler */ sig.sa_handler = signal_handler; sig.sa_flags = 0; sigaction(SIGWINCH, &sig, NULL); sigaction(SIGTERM, &sig, NULL); sigaction(SIGINT, &sig, NULL); sigaction(SIGSEGV, &sig, NULL); /* Init global struct */ ttyclock->running = True; if(!ttyclock->geo.x) ttyclock->geo.x = 0; if(!ttyclock->geo.y) ttyclock->geo.y = 0; if(!ttyclock->geo.a) ttyclock->geo.a = 1; if(!ttyclock->geo.b) ttyclock->geo.b = 1; ttyclock->geo.w = (ttyclock->option.second) ? SECFRAMEW : NORMFRAMEW; ttyclock->geo.h = 7; ttyclock->tm = localtime(&(ttyclock->lt)); if(ttyclock->option.utc) { ttyclock->tm = gmtime(&(ttyclock->lt)); } ttyclock->lt = time(NULL); update_hour(); /* Create clock win */ ttyclock->framewin = newwin(ttyclock->geo.h, ttyclock->geo.w, ttyclock->geo.x, ttyclock->geo.y); if(ttyclock->option.box) { box(ttyclock->framewin, 0, 0); } if (ttyclock->option.bold) { wattron(ttyclock->framewin, A_BLINK); } /* Create the date win */ ttyclock->datewin = newwin(DATEWINH, strlen(ttyclock->date.datestr) + 2, ttyclock->geo.x + ttyclock->geo.h - 1, ttyclock->geo.y + (ttyclock->geo.w / 2) - (strlen(ttyclock->date.datestr) / 2) - 1); if(ttyclock->option.box) { box(ttyclock->datewin, 0, 0); } clearok(ttyclock->datewin, True); set_center(ttyclock->option.center); nodelay(stdscr, True); if (ttyclock->option.date) { wrefresh(ttyclock->datewin); } wrefresh(ttyclock->framewin); /* Initialize the start timer */ start_time = time(0); return; }
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { update_hour(); update_minute(); }
int main(int argc, char **argv) { int c; /* Alloc ttyclock */ ttyclock = malloc(sizeof(ttyclock_t)); assert(ttyclock != NULL); memset(ttyclock, 0, sizeof(ttyclock_t)); /* Date format */ ttyclock->option.format = malloc(sizeof(char) * 100); /* Default date format */ strncpy(ttyclock->option.format, "%F", 100); /* Default color */ ttyclock->option.color = COLOR_GREEN; /* COLOR_GREEN = 2 */ /* Default delay */ ttyclock->option.delay = 40000000; /* 25FPS */ atexit(cleanup); while ((c = getopt(argc, argv, "tT:nvsSrcihbf:d:C:")) != -1) { switch(c) { case 'h': default: printf("usage : tty-clock [-sSbctrnvih] [-C [0-7]] [-f format] [-d delay] [-T tty] \n" " -s Show seconds \n" " -S Screensaver mode \n" " -b Show box \n" " -c Set the clock at the center of the terminal \n" " -C [0-7] Set the clock color \n" " -t Set the hour in 12h format \n" " -T tty Display the clock on the specified terminal \n" " -r Do rebound the clock \n" " -f format Set the date format \n" " -n Don't quit on keypress \n" " -v Show tty-clock version \n" " -i Show some info about tty-clock \n" " -h Show this page \n" " -d delay Set the delay between two redraws of the clock \n"); exit(EXIT_SUCCESS); break; case 'i': puts("TTY-Clock 2 © by Martin Duquesnoy ([email protected])"); exit(EXIT_SUCCESS); break; case 'v': puts("TTY-Clock 2 © devel version"); exit(EXIT_SUCCESS); break; case 's': ttyclock->option.second = True; break; case 'S': ttyclock->option.screensaver = True; break; case 'c': ttyclock->option.center = True; break; case 'C': if(atoi(optarg) >= 0 && atoi(optarg) < 8) ttyclock->option.color = atoi(optarg); break; case 't': ttyclock->option.twelve = True; break; case 'r': ttyclock->option.rebound = True; break; case 'f': strncpy(ttyclock->option.format, optarg, 100); break; case 'd': if(atol(optarg) >= 0 && atol(optarg) < 1000000000) ttyclock->option.delay = atol(optarg); break; case 'b': ttyclock->option.box = True; break; case 'T': { struct stat sbuf; if (stat(optarg, &sbuf) == -1) { fprintf(stderr, "tty-clock: error: couldn't stat '%s': %s.\n", optarg, strerror(errno)); exit(EXIT_FAILURE); } else if (!S_ISCHR(sbuf.st_mode)) { fprintf(stderr, "tty-clock: error: '%s' doesn't appear to be a character device.\n", optarg); exit(EXIT_FAILURE); } else { if (ttyclock->tty) free(ttyclock->tty); ttyclock->tty = strdup(optarg); } } break; case 'n': ttyclock->option.noquit = True; break; } } init(); while(ttyclock->running) { clock_rebound(); update_hour(); draw_clock(); key_event(); } endwin(); return 0; }