main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. Copyright (C) 2014 Olivier Marty <olivier.marty.m at gmail.com>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. */
  15. #include "parser.h"
  16. #include "time.h"
  17. #include "printer.h"
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. void displayUsage(char *name)
  22. {
  23. printf("Usage : %s file.srt\n", name);
  24. printf("Options :\n");
  25. printf(" -s sec\t: skip the first x seconds\n");
  26. printf(" -d sec\t: wait x seconds before starting (default : 5)\n");
  27. printf(" -t x\t\t: time factor x1000\n");
  28. printf(" -m px\t\t: margin with the bottom of the screen\n");
  29. printf(" -p px\t\t: padding of the box\n");
  30. printf(" -g px\t\t: gap between two lines\n");
  31. printf(" -f fontname\t: name of the font to use\n");
  32. printf(" -i fontname\t: name of the italic font to use\n");
  33. printf(" -b fontname\t: name of the bold font to use\n");
  34. printf(" -j fontname\t: name of the bold and italic font to use\n");
  35. printf(" -h\t\t: display this help and exit\n");
  36. }
  37. void callbackEvent(struct printerEnv* env, int key, void* a) {
  38. if(key == ' ') {
  39. // display a message
  40. if(!timeIsPaused())
  41. printerShow(env, "(paused - press space to resume)", 0);
  42. else
  43. printerClean(*env);
  44. // toggle pause
  45. timePause(!timeIsPaused());
  46. // if paused, wait for an event
  47. while(timeIsPaused())
  48. {
  49. waitEvent(env);
  50. manageEvent(env, callbackEvent, a);
  51. }
  52. }
  53. }
  54. int main(int argc, char **argv)
  55. {
  56. int i, shift = 0, delay = 5, margin_bottom = 50, padding = 5, gap = 5;
  57. float factor = 1.0;
  58. char *font = NULL, *font_i = NULL, *font_b = NULL, *font_bi = NULL;
  59. FILE *f = NULL;
  60. // parse arguments
  61. int c;
  62. while((c = getopt (argc, argv, "s:d:t:m:p:g:f:i:b:j:h")) != -1)
  63. switch(c)
  64. {
  65. case 's':
  66. shift = atoi(optarg);
  67. break;
  68. case 'd':
  69. delay = atoi(optarg);
  70. break;
  71. case 't':
  72. factor = (float)atoi(optarg)/1000;
  73. break;
  74. case 'm':
  75. margin_bottom = atoi(optarg);
  76. break;
  77. case 'p':
  78. padding = atoi(optarg);
  79. break;
  80. case 'g':
  81. gap = atoi(optarg);
  82. break;
  83. case 'f':
  84. font = malloc(strlen(optarg)+1);
  85. strcpy(font, optarg);
  86. break;
  87. case 'i':
  88. font_i = malloc(strlen(optarg)+1);
  89. strcpy(font_i, optarg);
  90. break;
  91. case 'b':
  92. font_b = malloc(strlen(optarg)+1);
  93. strcpy(font_b, optarg);
  94. break;
  95. case 'j':
  96. font_bi = malloc(strlen(optarg)+1);
  97. strcpy(font_bi, optarg);
  98. break;
  99. case 'h':
  100. displayUsage(argv[0]);
  101. return 0;
  102. //case '?':
  103. default:
  104. return 1;
  105. }
  106. if(optind >= argc)
  107. {
  108. fprintf(stderr, "Missing filename.\n");
  109. displayUsage(argv[0]);
  110. return 1;
  111. }
  112. f = fopen(argv[optind], "r");
  113. if(f == NULL)
  114. {
  115. perror("fopen()");
  116. exit(1);
  117. }
  118. // open the window
  119. struct printerEnv penv = printerOpenWindow(font, font_i, font_b, font_bi);
  120. // set attributes
  121. penv.margin_bottom = margin_bottom;
  122. penv.padding = padding;
  123. penv.gap = gap;
  124. // show a counter before start the clock
  125. for(i = delay; i > 0; i--)
  126. {
  127. char t[16];
  128. sprintf(t, "%d...\n", i);
  129. printf("%s", t);
  130. printerShow(&penv, t, T_ITALIC);
  131. sleep(1);
  132. }
  133. printf("0 !\n");
  134. printerClean(penv);
  135. timeInitialize(-factor*shift);
  136. struct SubtitleLine sline;
  137. int id = 0;
  138. while(!feof(f))
  139. {
  140. id = next(f, id+1, &sline);
  141. if(timeInFuture(timeFactor(sline.end, factor)))
  142. {
  143. timeSleepUntil(timeFactor(sline.begin, factor));
  144. printf("%ds\n", sline.begin.tv_sec);
  145. // show
  146. printf("%s\n", sline.text);
  147. printerShow(&penv, sline.text, 0);
  148. // hide
  149. timeSleepUntil(timeFactor(sline.end, factor));
  150. // TODO manage when the next subtitle appear before
  151. printf("\n");
  152. printerClean(penv);
  153. manageEvent(&penv, callbackEvent, NULL);
  154. }
  155. else
  156. {
  157. printf("skipped :\n");
  158. printf("%s\n", sline.text);
  159. }
  160. }
  161. printerCloseWindow(penv);
  162. fclose(f);
  163. }