| 1 |
/* |
| 2 |
* cacaclock Text-mode clock display |
| 3 |
* Copyright (c) 2011-2012 Jean-Yves Lamoureux <[email protected]> |
| 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://www.wtfpl.net/ 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 |
static void usage(int argc, char **argv) |
| 27 |
{ |
| 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"); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
static void version(void) |
| 40 |
{ |
| 41 |
printf( |
| 42 |
"cacaclock Copyright 2011-2012 Jean-Yves Lamoureux\n" |
| 43 |
"Internet: <[email protected]> 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__); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
static char* get_date(char *format) { |
| 55 |
time_t currtime; |
| 56 |
char *charTime = malloc(101); |
| 57 |
|
| 58 |
time(&currtime); |
| 59 |
strftime(charTime, 100,format,localtime(&currtime)); |
| 60 |
|
| 61 |
return charTime; |
| 62 |
} |
| 63 |
|
| 64 |
int main(int argc, char *argv[]) { |
| 65 |
|
| 66 |
caca_canvas_t *cv; |
| 67 |
caca_canvas_t *figcv; |
| 68 |
caca_display_t *dp; |
| 69 |
uint32_t w, h, fw, fh; |
| 70 |
|
| 71 |
char *format = "%R:%S"; |
| 72 |
char *font = "/usr/share/figlet/mono12.tlf"; |
| 73 |
|
| 74 |
|
| 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; |
| 89 |
|
| 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 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
} |
| 121 |
|
| 122 |
if(caca_canvas_set_figfont(figcv, font)) |
| 123 |
{ |
| 124 |
fprintf(stderr, "Could not open font\n"); |
| 125 |
return -1; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
dp = caca_create_display(cv); |
| 130 |
if(!dp) { |
| 131 |
printf("Can't open window. CACA_DRIVER problem ?\n"); |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
caca_set_color_ansi(figcv, CACA_DEFAULT, CACA_DEFAULT); |
| 136 |
caca_clear_canvas(cv); |
| 137 |
for(;;) { |
| 138 |
caca_event_t ev; |
| 139 |
|
| 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; |
| 148 |
|
| 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); |
| 161 |
|
| 162 |
w = caca_get_canvas_width (cv); |
| 163 |
h = caca_get_canvas_height(cv); |
| 164 |
fw = caca_get_canvas_width (figcv); |
| 165 |
fh = caca_get_canvas_height(figcv); |
| 166 |
|
| 167 |
uint32_t x = (w/2) - (fw/2); |
| 168 |
uint32_t y = (h/2) - (fh/2); |
| 169 |
|
| 170 |
caca_blit(cv, x, y, figcv, NULL); |
| 171 |
caca_refresh_display(dp); |
| 172 |
usleep(250000); |
| 173 |
} |
| 174 |
end: |
| 175 |
|
| 176 |
caca_free_canvas(figcv); |
| 177 |
caca_free_canvas(cv); |
| 178 |
caca_free_display(dp); |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |