main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 "rich_text.h"
  19. #include "events.h"
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. enum _t_state { S_RUNNING, S_PAUSED, S_STOP };
  24. typedef enum _t_state t_state;
  25. void displayUsage(char *name)
  26. {
  27. printf("Usage : %s file.srt\n", name);
  28. printf("Options :\n");
  29. printf(" -s sec\t: skip the first x seconds\n");
  30. printf(" -d sec\t: wait x seconds before starting (default : 5)\n");
  31. printf(" -t x\t\t: time factor x1000\n");
  32. printf(" -m px\t\t: margin with the bottom of the screen\n");
  33. printf(" -p px\t\t: padding of the box\n");
  34. printf(" -g px\t\t: gap between two lines\n");
  35. printf(" -k px\t\t: gap between two subtitles\n");
  36. printf(" -f fontname\t: name of the font to use\n");
  37. printf(" -i fontname\t: name of the italic font to use\n");
  38. printf(" -b fontname\t: name of the bold font to use\n");
  39. printf(" -j fontname\t: name of the bold and italic font to use\n");
  40. printf(" -h\t\t: display this help and exit\n");
  41. }
  42. // display a message and program its end (and free) (one at a time)
  43. void display(struct printerEnv *env, struct richText *rt, int time, t_events *events)
  44. {
  45. static int id = -1;
  46. t_event e;
  47. printerHide(env, id);
  48. id--;
  49. e.type = T_HIDE;
  50. e.hide.time = timeGetRelative();
  51. e.hide.time.tv_sec += time;
  52. e.hide.id = id;
  53. e.hide.rt = rt;
  54. printerShow(env, rt, id);
  55. eventsPush(events, e);
  56. }
  57. int main(int argc, char **argv)
  58. {
  59. int i, delay = 5, margin_bottom = 50, padding = 5, gap = 5, gap2 = 20;
  60. double factor = 1.0, shift = 0.;
  61. char *font = NULL, *font_i = NULL, *font_b = NULL, *font_bi = NULL;
  62. FILE *f = NULL;
  63. t_state state = S_RUNNING;
  64. // parse arguments
  65. int c;
  66. while((c = getopt (argc, argv, "s:d:t:m:p:g:k:f:i:b:j:h")) != -1)
  67. switch(c)
  68. {
  69. case 's':
  70. shift = atof(optarg);
  71. break;
  72. case 'd':
  73. delay = atoi(optarg);
  74. break;
  75. case 't':
  76. factor = atoi(optarg)/1000.;
  77. break;
  78. case 'm':
  79. margin_bottom = atoi(optarg);
  80. break;
  81. case 'p':
  82. padding = atoi(optarg);
  83. break;
  84. case 'g':
  85. gap = atoi(optarg);
  86. break;
  87. case 'k':
  88. gap2 = atoi(optarg);
  89. break;
  90. case 'f':
  91. font = malloc(strlen(optarg)+1);
  92. strcpy(font, optarg);
  93. break;
  94. case 'i':
  95. font_i = malloc(strlen(optarg)+1);
  96. strcpy(font_i, optarg);
  97. break;
  98. case 'b':
  99. font_b = malloc(strlen(optarg)+1);
  100. strcpy(font_b, optarg);
  101. break;
  102. case 'j':
  103. font_bi = malloc(strlen(optarg)+1);
  104. strcpy(font_bi, optarg);
  105. break;
  106. case 'h':
  107. displayUsage(argv[0]);
  108. return 0;
  109. //case '?':
  110. default:
  111. return 1;
  112. }
  113. if(optind >= argc)
  114. {
  115. fprintf(stderr, "Missing filename.\n");
  116. displayUsage(argv[0]);
  117. return 1;
  118. }
  119. f = fopen(argv[optind], "r");
  120. if(f == NULL)
  121. {
  122. perror("fopen()");
  123. exit(1);
  124. }
  125. // open the window
  126. struct printerEnv penv = printerOpenWindow(font, font_i, font_b, font_bi);
  127. // set attributes
  128. penv.margin_bottom = margin_bottom;
  129. penv.padding = padding;
  130. penv.gap = gap;
  131. penv.gap2 = gap2;
  132. // show a counter before start the clock
  133. for(i = delay; i > 0; i--)
  134. {
  135. struct richText *rt;
  136. char t[16];
  137. sprintf(t, "<i>%d...</i>\n", i);
  138. printf("%s\n", t);
  139. rt = richTextParse(t);
  140. printerShow(&penv, rt, 0);
  141. sleep(1);
  142. printerHide(&penv, 0);
  143. richTextFree(rt);
  144. }
  145. printerRender(&penv);
  146. printf("0 !\n");
  147. timeInitialize();
  148. timeShift(-factor*shift);
  149. int id = 0;
  150. struct richText *pausert = richTextParse("<i>paused...</i>\n");
  151. t_events events = eventsInit(8);
  152. t_event event;
  153. event.type = T_SHOW;
  154. event.show.id = -1; // first
  155. while(1)
  156. {
  157. switch(state)
  158. {
  159. case S_RUNNING:
  160. switch(event.type)
  161. {
  162. case T_KEYPRESSED:
  163. switch(event.keyPressed.key)
  164. {
  165. case ' ':
  166. state = S_PAUSED;
  167. printf("paused...\n");
  168. printerShow(&penv, pausert, 0);
  169. timePause(1);
  170. break;
  171. case 65363: // right shift
  172. shift += 0.05;
  173. printf("shift : +0.05s (total : %.2fs)\n", shift);
  174. timeShift(-0.05);
  175. {
  176. char *msg = malloc(64);
  177. sprintf(msg, "<i>shift %.2fs</i>\n", shift);
  178. display(&penv, richTextParse(msg), 1, &events);
  179. }
  180. break;
  181. case 65361: // left shift
  182. shift -= 0.05;
  183. printf("shift : -0.05s (total : %.2fs)\n", shift);
  184. timeShift(-0.05);
  185. {
  186. char *msg = malloc(64);
  187. sprintf(msg, "<i>shift %.2fs</i>\n", shift);
  188. display(&penv, richTextParse(msg), 1, &events);
  189. }
  190. break;
  191. }
  192. break;
  193. case T_HIDE:
  194. printerHide(&penv, event.hide.id);
  195. printerRender(&penv);
  196. free(event.hide.rt->raw);
  197. richTextFree(event.hide.rt);
  198. printf("\n");
  199. break;
  200. case T_SHOW:
  201. if(event.hide.id >= 0)
  202. {
  203. printf("%ds\n", event.show.time.tv_sec);
  204. printf("%s", event.show.rt->raw);
  205. printerShow(&penv, event.show.rt, event.show.id);
  206. }
  207. // grab next subtitles
  208. while(1)
  209. {
  210. struct SubtitleLine sline;
  211. if(feof(f))
  212. break;
  213. id = next(f, id+1, &sline);
  214. sline.begin = timeFactor(sline.begin, factor);
  215. sline.end = timeFactor(sline.end, factor);
  216. if(timeInFuture(sline.end))
  217. {
  218. char *copy = NULL;
  219. struct richText *rt;
  220. copy = malloc(sizeof(char[strlen(sline.text)+1]));
  221. if(copy == NULL)
  222. {
  223. perror("malloc()");
  224. exit(1);
  225. }
  226. strcpy(copy, sline.text);
  227. rt = richTextParse(copy);
  228. // show event
  229. t_event show, hide;
  230. show.type = T_SHOW;
  231. show.show.id = id;
  232. show.show.rt = rt;
  233. show.show.time = sline.begin;
  234. // hide event
  235. hide.type = T_HIDE;
  236. hide.hide.id = id;
  237. hide.hide.rt = rt;
  238. hide.hide.time = sline.end;
  239. eventsPush(&events, show);
  240. eventsPush(&events, hide);
  241. break;
  242. }
  243. else
  244. {
  245. printf("skipped :\n");
  246. printf("%s\n", sline.text);
  247. }
  248. }
  249. break;
  250. }
  251. break;
  252. case S_PAUSED:
  253. switch(event.type)
  254. {
  255. case T_KEYPRESSED:
  256. if(event.keyPressed.key == ' ')
  257. {
  258. state = S_RUNNING;
  259. printf("end\n");
  260. printerHide(&penv, 0);
  261. printerRender(&penv);
  262. timePause(0);
  263. }
  264. break;
  265. }
  266. break;
  267. }
  268. if(eventsEmpty(events))
  269. state = S_STOP;
  270. if(state == S_STOP)
  271. break;
  272. // sleep until next event
  273. mytime next_event = eventsNextTime(events);
  274. while(1)
  275. {
  276. int value;
  277. fd_set in_fds;
  278. FD_ZERO(&in_fds);
  279. FD_SET(penv.d_fd, &in_fds);
  280. if(XPending(penv.d)) // event waiting (pselect does no see events that come
  281. // during the beginning of the program
  282. value = 1;
  283. else if(state == S_PAUSED) // blocking select
  284. value = pselect(penv.d_fd+1, &in_fds, NULL, NULL, NULL, NULL);
  285. else // timeout
  286. {
  287. struct timespec to_wait = timeDiff(next_event, timeGetRelative());
  288. if(to_wait.tv_sec < 0)
  289. {
  290. to_wait.tv_sec = 0;
  291. to_wait.tv_nsec = 0;
  292. }
  293. value = pselect(penv.d_fd+1, &in_fds, NULL, NULL, &to_wait, NULL);
  294. }
  295. if(value == -1) // TODO tester interruption par un signal : return 0 ou -1
  296. // dans les deux cas c'est foireux
  297. {
  298. perror("pselect()");
  299. exit(1);
  300. }
  301. else if(value > 0) // x event
  302. {
  303. while(XPending(penv.d))
  304. {
  305. event = manageEvent(&penv);
  306. if(event.type == T_KEYPRESSED)
  307. break;
  308. }
  309. if(event.type == T_KEYPRESSED)
  310. break;
  311. }
  312. else // deadline
  313. {
  314. event = eventsPop(&events);
  315. break;
  316. }
  317. }
  318. }
  319. richTextFree(pausert);
  320. printerCloseWindow(penv);
  321. fclose(f);
  322. }