您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

185 行
4.4 KiB

  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. #include "config.h"
  13. #if !defined(__KERNEL__)
  14. # include <time.h>
  15. # include <stdio.h>
  16. # include <string.h>
  17. # include <stdlib.h>
  18. #endif
  19. #include "caca.h"
  20. #define CACACLOCKVERSION "0.1"
  21. static void usage(int argc, char **argv)
  22. {
  23. char const * const * list;
  24. fprintf(stderr, "Usage: %s [OPTIONS]... <IMAGE>\n", argv[0]);
  25. fprintf(stderr, "Convert IMAGE to any text based available format.\n");
  26. fprintf(stderr, "Example : %s -w 80 -f ansi ./caca.png\n\n", argv[0]);
  27. fprintf(stderr, "Options:\n");
  28. fprintf(stderr, " -h, --help\t\t\tThis help\n");
  29. fprintf(stderr, " -v, --version\t\t\tVersion of the program\n");
  30. fprintf(stderr, " -f, --font=FONT\t\tUse FONT for time display\n");
  31. fprintf(stderr, " -d, --dateformat=FORMAT\tUse FORMAT as strftime argument (default %%R:%%S)\n");
  32. }
  33. static void version(void)
  34. {
  35. printf(
  36. "cacaclock Copyright 2011 Jean-Yves Lamoureux\n"
  37. "Internet: <jylam@lnxscene.org> Version: %s (libcaca %s), date: %s\n"
  38. "\n"
  39. "cacaclock, along with its documentation, may be freely copied and distributed.\n"
  40. "\n"
  41. "The latest version of cacaclock is available from the web site,\n"
  42. " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n"
  43. "\n",
  44. CACACLOCKVERSION, caca_get_version(), __DATE__);
  45. }
  46. static char* get_date(char *format) {
  47. time_t currtime;
  48. char *charTime = malloc(101);
  49. time(&currtime);
  50. strftime(charTime, 100,format,localtime(&currtime));
  51. return charTime;
  52. }
  53. int main(int argc, char *argv[]) {
  54. caca_canvas_t *cv;
  55. caca_canvas_t *figcv;
  56. caca_display_t *dp;
  57. uint32_t w, h, fw, fh;
  58. char *format = "%R:%S";
  59. char *font = "/usr/share/figlet/mono12.tlf";
  60. for(;;)
  61. {
  62. int option_index = 0;
  63. static struct caca_option long_options[] =
  64. {
  65. { "font", 1, NULL, 'f' },
  66. { "dateformat", 1, NULL, 'd' },
  67. { "help", 0, NULL, 'h' },
  68. { "version", 0, NULL, 'v' },
  69. };
  70. int c = caca_getopt(argc, argv, "f:d:hv",
  71. long_options, &option_index);
  72. if(c == -1)
  73. break;
  74. switch(c)
  75. {
  76. case 'h': /* --help */
  77. usage(argc, argv);
  78. return 0;
  79. break;
  80. case 'v': /* --version */
  81. version();
  82. return 0;
  83. break;
  84. case 'f': /* --font */
  85. font = caca_optarg;
  86. break;
  87. case 'd': /* --dateformat */
  88. format = caca_optarg;
  89. break;
  90. default:
  91. return 1;
  92. break;
  93. }
  94. }
  95. cv = caca_create_canvas(0, 0);
  96. figcv = caca_create_canvas(0, 0);
  97. if(!cv || !figcv)
  98. {
  99. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  100. return 1;
  101. }
  102. if(caca_canvas_set_figfont(figcv, font))
  103. {
  104. fprintf(stderr, "Could not open font\n");
  105. return -1;
  106. }
  107. dp = caca_create_display(cv);
  108. if(!dp) {
  109. printf("Can't open window. CACA_DRIVER problem ?\n");
  110. return -1;
  111. }
  112. caca_set_color_ansi(figcv, CACA_DEFAULT, CACA_DEFAULT);
  113. caca_clear_canvas(cv);
  114. for(;;) {
  115. caca_event_t ev;
  116. while(caca_get_event(dp, CACA_EVENT_KEY_PRESS
  117. | CACA_EVENT_QUIT, &ev, 1))
  118. {
  119. if(caca_get_event_type(&ev))
  120. goto end;
  121. }
  122. char *d = get_date(format);
  123. uint32_t o = 0;
  124. // figfont API is not complete, and does not alloq us to put a string
  125. // at another position than 0,0
  126. // So, we have to create a canvas which will hold the figfont string,
  127. // then blit this canvas to the main one at the desired position.
  128. caca_clear_canvas(cv);
  129. caca_clear_canvas(figcv);
  130. while(d[o])
  131. {
  132. caca_put_figchar(figcv, d[o++]);
  133. }
  134. caca_flush_figlet (figcv);
  135. fw = caca_get_canvas_width (figcv);
  136. fh = caca_get_canvas_height(figcv);
  137. free(d);
  138. w = caca_get_canvas_width (cv);
  139. h = caca_get_canvas_height(cv);
  140. uint32_t x = (w/2) - (fw/2);
  141. uint32_t y = (h/2) - (fh/2);
  142. caca_blit(cv, x, y, figcv, NULL);
  143. caca_refresh_display(dp);
  144. usleep(250000);
  145. }
  146. end:
  147. caca_free_canvas(figcv);
  148. caca_free_canvas(cv);
  149. caca_free_display(dp);
  150. return 0;
  151. }