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

Last change on this file since 4859 was 4859, checked in by Sam Hocevar, 9 years ago

build: fix the WTFPL homepage and copyright information.

File size: 4.9 KB
RevLine 
[4773]1/*
2 *  cacaclock     Text-mode clock display
[4806]3 *  Copyright (c) 2011-2012 Jean-Yves Lamoureux <jylam@lnxscene.org>
[4773]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
[4859]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://www.wtfpl.net/ for more details.
[4773]11 */
12
13#include "config.h"
14
15#if !defined(__KERNEL__)
[4787]16#   include <time.h>
[4773]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
[4777]26static void usage(int argc, char **argv)
27{
[4800]28    fprintf(stderr, "Usage: %s [OPTIONS]...\n", argv[0]);
29    fprintf(stderr, "Display current time in text mode     (q to quit)\n");
30    fprintf(stderr, "Example : %s -d '%%R'\n\n", argv[0]);
31    fprintf(stderr, "Options:\n");
32    fprintf(stderr, "  -h, --help\t\t\tThis help\n");
33    fprintf(stderr, "  -v, --version\t\t\tVersion of the program\n");
34    fprintf(stderr, "  -f, --font=FONT\t\tUse FONT for time display\n");
35    fprintf(stderr, "  -d, --dateformat=FORMAT\tUse FORMAT as strftime argument (default %%R:%%S)\n");
[4777]36}
37
38
39static void version(void)
40{
[4800]41    printf(
[4806]42            "cacaclock Copyright 2011-2012 Jean-Yves Lamoureux\n"
[4800]43            "Internet: <jylam@lnxscene.org> Version: %s (libcaca %s), date: %s\n"
44            "\n"
45            "cacaclock, along with its documentation, may be freely copied and distributed.\n"
46            "\n"
47            "The latest version of cacaclock is available from the web site,\n"
48            "        http://caca.zoy.org/wiki/libcaca in the libcaca package.\n"
49            "\n",
50            CACACLOCKVERSION, caca_get_version(), __DATE__);
[4777]51}
52
53
54static char* get_date(char *format) {
[4800]55    time_t  currtime;
56    char *charTime = malloc(101);
[4773]57
[4800]58    time(&currtime);
59    strftime(charTime, 100,format,localtime(&currtime));
[4773]60
[4800]61    return charTime;
[4773]62}
63
64int main(int argc, char *argv[]) {
65
[4800]66    caca_canvas_t *cv;
67    caca_canvas_t *figcv;
68    caca_display_t *dp;
69    uint32_t w, h, fw, fh;
[4773]70
[4800]71    char *format = "%R:%S";
72    char *font   = "/usr/share/figlet/mono12.tlf";
[4775]73
[4773]74
[4800]75    for(;;)
76    {
77        int option_index = 0;
78        static struct caca_option long_options[] =
79        {
80            { "font",        1, NULL, 'f' },
81            { "dateformat",  1, NULL, 'd' },
82            { "help",        0, NULL, 'h' },
83            { "version",     0, NULL, 'v' },
84        };
85        int c = caca_getopt(argc, argv, "f:d:hv",
86                long_options, &option_index);
87        if(c == -1)
88            break;
[4773]89
[4800]90        switch(c)
91        {
92            case 'h': /* --help       */
93                usage(argc, argv);
94                return 0;
95                break;
96            case 'v': /* --version    */
97                version();
98                return 0;
99                break;
100            case 'f': /* --font       */
101                font = caca_optarg;
102                break;
103            case 'd': /* --dateformat */
104                format = caca_optarg;
105                break;
106            default:
107                return 1;
108                break;
109        }
110    }
[4777]111
112
113
[4800]114    cv = caca_create_canvas(0, 0);
115    figcv = caca_create_canvas(0, 0);
116    if(!cv || !figcv)
117    {
118        fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
119        return 1;
120    }
[4773]121
[4800]122    if(caca_canvas_set_figfont(figcv, font))
123    {
124        fprintf(stderr, "Could not open font\n");
125        return -1;
126    }
[4773]127
128
[4800]129    dp = caca_create_display(cv);
130    if(!dp) {
131        printf("Can't open window. CACA_DRIVER problem ?\n");
132        return -1;
133    }
[4773]134
[4800]135    caca_set_color_ansi(figcv, CACA_DEFAULT, CACA_DEFAULT);
136    caca_clear_canvas(cv);
137    for(;;) {
138        caca_event_t ev;
[4773]139
[4800]140        while(caca_get_event(dp, CACA_EVENT_KEY_PRESS
141                    | CACA_EVENT_QUIT, &ev, 1))
142        {
143            if(caca_get_event_type(&ev))
144                goto end;
145        }
146        char *d = get_date(format);
147        uint32_t o = 0;
[4773]148
[4800]149        // figfont API is not complete, and does not allow us to put a string
150        // at another position than 0,0
151        // So, we have to create a canvas which will hold the figfont string,
152        // then blit this canvas to the main one at the desired position.
153        caca_clear_canvas(cv);
154        caca_clear_canvas(figcv);
155        while(d[o])
156        {
157            caca_put_figchar(figcv, d[o++]);
158        }
159        caca_flush_figlet (figcv);
160        free(d);
[4777]161
[4800]162        w = caca_get_canvas_width (cv);
163        h = caca_get_canvas_height(cv);
[4779]164        fw = caca_get_canvas_width (figcv);
[4800]165        fh = caca_get_canvas_height(figcv);
[4779]166
[4800]167        uint32_t x = (w/2) - (fw/2);
168        uint32_t y = (h/2) - (fh/2);
[4777]169
[4800]170        caca_blit(cv, x, y, figcv, NULL);
171        caca_refresh_display(dp);
172        usleep(250000);
173    }
[4773]174end:
175
[4800]176    caca_free_canvas(figcv);
177    caca_free_canvas(cv);
178    caca_free_display(dp);
[4773]179
[4800]180    return 0;
[4773]181}
Note: See TracBrowser for help on using the repository browser.