main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. */
  79. case 'm':
  80. margin_bottom = atoi(optarg);
  81. break;
  82. case 'p':
  83. padding = atoi(optarg);
  84. break;
  85. case 'g':
  86. gap = atoi(optarg);
  87. break;
  88. case 'k':
  89. gap2 = atoi(optarg);
  90. break;
  91. case 'f':
  92. font = malloc(strlen(optarg)+1);
  93. strcpy(font, optarg);
  94. break;
  95. case 'i':
  96. font_i = malloc(strlen(optarg)+1);
  97. strcpy(font_i, optarg);
  98. break;
  99. case 'b':
  100. font_b = malloc(strlen(optarg)+1);
  101. strcpy(font_b, optarg);
  102. break;
  103. case 'j':
  104. font_bi = malloc(strlen(optarg)+1);
  105. strcpy(font_bi, optarg);
  106. break;
  107. case 'h':
  108. displayUsage(argv[0]);
  109. return 0;
  110. //case '?':
  111. default:
  112. return 1;
  113. }
  114. if(optind >= argc)
  115. {
  116. fprintf(stderr, "Missing filename.\n");
  117. displayUsage(argv[0]);
  118. return 1;
  119. }
  120. f = fopen(argv[optind], "r");
  121. if(f == NULL)
  122. {
  123. perror("fopen()");
  124. exit(1);
  125. }
  126. // open the window
  127. struct printerEnv penv = printerOpenWindow(font, font_i, font_b, font_bi);
  128. // set attributes
  129. penv.margin_bottom = margin_bottom;
  130. penv.padding = padding;
  131. penv.gap = gap;
  132. penv.gap2 = gap2;
  133. // show a counter before start the clock
  134. for(i = delay; i > 0; i--)
  135. {
  136. struct richText *rt;
  137. char t[16];
  138. sprintf(t, "<i>%d...</i>\n", i);
  139. printf("%s\n", t);
  140. rt = richTextParse(t);
  141. printerShow(&penv, rt, 0);
  142. sleep(1);
  143. printerHide(&penv, 0);
  144. richTextFree(rt);
  145. }
  146. printerRender(&penv);
  147. printf("0 !\n");
  148. timeInitialize();
  149. timeShift(-factor*shift);
  150. int id = 0;
  151. struct richText *pausert = richTextParse("<i>paused...</i>\n");
  152. t_events events = eventsInit(8);
  153. t_event event;
  154. event.type = T_SHOW;
  155. event.show.id = -1; // first
  156. while(1)
  157. {
  158. switch(state)
  159. {
  160. case S_RUNNING:
  161. switch(event.type)
  162. {
  163. case T_KEYPRESSED:
  164. switch(event.keyPressed.key)
  165. {
  166. case ' ':
  167. state = S_PAUSED;
  168. printf("paused...\n");
  169. printerShow(&penv, pausert, 0);
  170. timePause(1);
  171. break;
  172. case 65363: // right shift
  173. shift += 0.05;
  174. printf("shift : +0.05s (total : %.2fs)\n", shift);
  175. timeShift(-0.05);
  176. {
  177. char *msg = malloc(64);
  178. sprintf(msg, "<i>shift %.2fs</i>\n", shift);
  179. display(&penv, richTextParse(msg), 1, &events);
  180. }
  181. break;
  182. case 65361: // left shift
  183. shift -= 0.05;
  184. printf("shift : -0.05s (total : %.2fs)\n", shift);
  185. timeShift(-0.05);
  186. {
  187. char *msg = malloc(64);
  188. sprintf(msg, "<i>shift %.2fs</i>\n", shift);
  189. display(&penv, richTextParse(msg), 1, &events);
  190. }
  191. break;
  192. }
  193. break;
  194. case T_HIDE:
  195. printerHide(&penv, event.hide.id);
  196. printerRender(&penv);
  197. free(event.hide.rt->raw);
  198. richTextFree(event.hide.rt);
  199. printf("\n");
  200. break;
  201. case T_SHOW:
  202. if(event.hide.id >= 0)
  203. {
  204. printf("%ds\n", timeFactor(event.show.time, 1./factor).tv_sec);
  205. printf("%s", event.show.rt->raw);
  206. printerShow(&penv, event.show.rt, event.show.id);
  207. }
  208. // grab next subtitles
  209. while(1)
  210. {
  211. struct SubtitleLine sline;
  212. if(feof(f))
  213. break;
  214. id = next(f, id+1, &sline);
  215. if(timeInFuture(timeFactor(sline.end, factor)))
  216. {
  217. char *copy = NULL;
  218. struct richText *rt;
  219. copy = malloc(sizeof(char[strlen(sline.text)+1]));
  220. if(copy == NULL)
  221. {
  222. perror("malloc()");
  223. exit(1);
  224. }
  225. strcpy(copy, sline.text);
  226. rt = richTextParse(copy);
  227. // show event
  228. t_event show, hide;
  229. show.type = T_SHOW;
  230. show.show.id = id;
  231. show.show.rt = rt;
  232. show.show.time = timeFactor(sline.begin, factor);
  233. // hide event
  234. hide.type = T_HIDE;
  235. hide.hide.id = id;
  236. hide.hide.rt = rt;
  237. hide.hide.time = timeFactor(sline.end, factor);
  238. eventsPush(&events, show);
  239. eventsPush(&events, hide);
  240. break;
  241. }
  242. else
  243. {
  244. printf("skipped :\n");
  245. printf("%s\n", sline.text);
  246. }
  247. }
  248. break;
  249. }
  250. break;
  251. case S_PAUSED:
  252. switch(event.type)
  253. {
  254. case T_KEYPRESSED:
  255. if(event.keyPressed.key == ' ')
  256. {
  257. state = S_RUNNING;
  258. printf("end\n");
  259. printerHide(&penv, 0);
  260. printerRender(&penv);
  261. timePause(0);
  262. }
  263. break;
  264. }
  265. break;
  266. }
  267. if(eventsEmpty(events))
  268. state = S_STOP;
  269. if(state == S_STOP)
  270. break;
  271. // sleep until next event
  272. mytime next_event = eventsNextTime(events);
  273. while(1)
  274. {
  275. int value;
  276. fd_set in_fds;
  277. FD_ZERO(&in_fds);
  278. FD_SET(penv.d_fd, &in_fds);
  279. if(XPending(penv.d)) // event waiting (pselect does no see events that come
  280. // during the beginning of the program
  281. value = 1;
  282. else if(state == S_PAUSED) // blocking select
  283. value = pselect(penv.d_fd+1, &in_fds, NULL, NULL, NULL, NULL);
  284. else // timeout
  285. {
  286. struct timespec to_wait = timeDiff(next_event, timeGetRelative());
  287. if(to_wait.tv_sec < 0)
  288. {
  289. to_wait.tv_sec = 0;
  290. to_wait.tv_nsec = 0;
  291. }
  292. value = pselect(penv.d_fd+1, &in_fds, NULL, NULL, &to_wait, NULL);
  293. }
  294. if(value == -1) // TODO tester interruption par un signal : return 0 ou -1
  295. // dans les deux cas c'est foireux
  296. {
  297. perror("pselect()");
  298. exit(1);
  299. }
  300. else if(value > 0) // x event
  301. {
  302. while(XPending(penv.d))
  303. {
  304. event = manageEvent(&penv);
  305. if(event.type == T_KEYPRESSED)
  306. break;
  307. }
  308. if(event.type == T_KEYPRESSED)
  309. break;
  310. }
  311. else // deadline
  312. {
  313. event = eventsPop(&events);
  314. break;
  315. }
  316. }
  317. }
  318. richTextFree(pausert);
  319. printerCloseWindow(penv);
  320. fclose(f);
  321. }