Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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