source: libcaca/trunk/src/cacaclock.c @ 4773

Last change on this file since 4773 was 4773, checked in by Jean-Yves Lamoureux, 9 years ago
  • Added preliminary cacaclock, a continuous clock
File size: 2.4 KB
Line 
1/*
2 *  cacaclock     Text-mode clock display
3 *  Copyright (c) 2011 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  This program is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13#include "config.h"
14
15#if !defined(__KERNEL__)
16#   include <time.h>   
17#   include <stdio.h>
18#   include <string.h>
19#   include <stdlib.h>
20#endif
21
22#include "caca.h"
23
24#define CACACLOCKVERSION "0.1"
25
26
27static char* get_date(void) {
28        time_t  currtime;                                                   
29        char *charTime = malloc(101);                                     
30
31        time(&currtime);                                                     
32        strftime(charTime, 100,"%R:%S",localtime(&currtime));
33
34        return charTime;
35}
36
37int main(int argc, char *argv[]) {
38
39        caca_canvas_t *cv;
40        caca_canvas_t *figcv;
41        caca_display_t *dp;
42
43        char *font = "/usr/share/figlet/mono12.tlf";
44        if(argc==2) font = argv[1];
45
46
47        cv = caca_create_canvas(0, 0);
48        figcv = caca_create_canvas(0, 0);
49        if(!cv || !figcv)
50        { 
51                fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
52                return 1;
53        }
54
55        if(caca_canvas_set_figfont(figcv, font))
56        { 
57                fprintf(stderr, "Could not open font\n");
58                return -1;
59        }
60
61
62        dp = caca_create_display(cv);
63        if(!dp) {
64                printf("Can't open window. CACA_DRIVER problem ?\n");
65                return -1;
66        }
67
68        for(;;) {
69                caca_event_t ev;
70
71                while(caca_get_event(dp, CACA_EVENT_KEY_PRESS
72                                        | CACA_EVENT_QUIT, &ev, 1))
73                { 
74                        if(caca_get_event_type(&ev))
75                                goto end;
76                }
77                char *d = get_date();
78                uint32_t o = 0;
79
80                // figfont API is not complete, and does not alloq us to put a string
81                // at another position than 0,0
82                // So, we have to create a canvas which will hold the figfont string,
83                // then blit this canvas to the main one at the desired position.
84                caca_clear_canvas(cv);
85                caca_clear_canvas(figcv);
86                while(d[o])
87                { 
88                        caca_set_color_ansi(figcv, CACA_WHITE, CACA_TRANSPARENT);
89                        caca_put_figchar(figcv, d[o++]);
90                }
91                caca_flush_figlet (figcv);
92                free(d);
93                //TODO center etc.
94                caca_blit(cv, 0, 0, figcv, NULL);
95                caca_refresh_display(dp);
96                usleep(250000);
97        }
98end:
99
100        caca_free_canvas(figcv);
101        caca_free_canvas(cv);
102        caca_free_display(dp);
103
104        return 0;
105}
Note: See TracBrowser for help on using the repository browser.