[702] | 1 | /* |
---|
| 2 | * cacaplay caca file player |
---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
| 5 | * |
---|
| 6 | * $Id: cacaplay.c 1754 2007-02-22 15:34:48Z jylam $ |
---|
| 7 | * |
---|
[1462] | 8 | * This program is free software. It comes without any warranty, to |
---|
[1452] | 9 | * the extent permitted by applicable law. You can redistribute it |
---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
[702] | 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #include "config.h" |
---|
[859] | 16 | #include "common.h" |
---|
[702] | 17 | |
---|
[1048] | 18 | #if !defined(__KERNEL__) |
---|
[1352] | 19 | # include <stdio.h> |
---|
| 20 | # include <stdlib.h> |
---|
| 21 | # include <sys/types.h> |
---|
| 22 | # include <sys/stat.h> |
---|
| 23 | # include <fcntl.h> |
---|
| 24 | # include <string.h> |
---|
| 25 | # include <unistd.h> |
---|
[1048] | 26 | #endif |
---|
[702] | 27 | |
---|
| 28 | #include "cucul.h" |
---|
| 29 | #include "caca.h" |
---|
| 30 | |
---|
| 31 | int main(int argc, char **argv) |
---|
| 32 | { |
---|
[1352] | 33 | cucul_canvas_t *cv, *app; |
---|
[811] | 34 | caca_display_t *dp; |
---|
[1352] | 35 | unsigned char *buf = NULL; |
---|
[1355] | 36 | long int bytes = 0, total = 0; |
---|
[1352] | 37 | int fd; |
---|
[702] | 38 | |
---|
[1352] | 39 | if(argc < 2 || !strcmp(argv[1], "-")) |
---|
[702] | 40 | { |
---|
[1352] | 41 | fd = 0; /* use stdin */ |
---|
[702] | 42 | } |
---|
[1352] | 43 | else |
---|
[702] | 44 | { |
---|
[1352] | 45 | fd = open(argv[1], O_RDONLY); |
---|
| 46 | if(fd < 0) |
---|
| 47 | { |
---|
| 48 | fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]); |
---|
| 49 | return 1; |
---|
| 50 | } |
---|
[702] | 51 | } |
---|
| 52 | |
---|
[1352] | 53 | cv = cucul_create_canvas(0, 0); |
---|
| 54 | app = cucul_create_canvas(0, 0); |
---|
[1754] | 55 | if(cv == NULL || app == NULL) |
---|
| 56 | { |
---|
| 57 | printf("Can't created canvas\n"); |
---|
| 58 | return -1; |
---|
| 59 | } |
---|
[819] | 60 | dp = caca_create_display(cv); |
---|
[1754] | 61 | if(dp == NULL) |
---|
| 62 | { |
---|
| 63 | printf("Can't create display\n"); |
---|
| 64 | return -1; |
---|
| 65 | } |
---|
[702] | 66 | |
---|
[1754] | 67 | |
---|
[1352] | 68 | for(;;) |
---|
| 69 | { |
---|
| 70 | caca_event_t ev; |
---|
| 71 | int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0); |
---|
[1355] | 72 | int eof = 0; |
---|
[702] | 73 | |
---|
[1352] | 74 | if(ret && ev.type & CACA_EVENT_KEY_PRESS) |
---|
[702] | 75 | break; |
---|
[1352] | 76 | |
---|
[1355] | 77 | if(bytes == 0) |
---|
| 78 | { |
---|
| 79 | ssize_t n; |
---|
| 80 | buf = realloc(buf, total + 1); |
---|
| 81 | n = read(fd, buf + total, 1); |
---|
| 82 | if(n < 0) |
---|
| 83 | { |
---|
| 84 | fprintf(stderr, "%s: read error\n", argv[0]); |
---|
| 85 | return -1; |
---|
| 86 | } |
---|
| 87 | else if(n == 0) |
---|
| 88 | { |
---|
| 89 | eof = 1; |
---|
| 90 | } |
---|
| 91 | total += n; |
---|
| 92 | } |
---|
| 93 | |
---|
[1352] | 94 | bytes = cucul_import_memory(app, buf, total, "caca"); |
---|
| 95 | |
---|
| 96 | if(bytes > 0) |
---|
| 97 | { |
---|
| 98 | total -= bytes; |
---|
| 99 | memmove(buf, buf + bytes, total); |
---|
| 100 | |
---|
| 101 | cucul_blit(cv, 0, 0, app, NULL); |
---|
| 102 | caca_refresh_display(dp); |
---|
| 103 | } |
---|
[1355] | 104 | else if(bytes < 0) |
---|
[1352] | 105 | { |
---|
| 106 | fprintf(stderr, "%s: corrupted caca file\n", argv[0]); |
---|
[1355] | 107 | break; |
---|
[1352] | 108 | } |
---|
[1355] | 109 | |
---|
| 110 | if(eof) |
---|
| 111 | break; |
---|
[702] | 112 | } |
---|
| 113 | |
---|
[1355] | 114 | caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1); |
---|
| 115 | |
---|
[702] | 116 | /* Clean up */ |
---|
[1352] | 117 | close(fd); |
---|
| 118 | |
---|
[819] | 119 | caca_free_display(dp); |
---|
[813] | 120 | cucul_free_canvas(cv); |
---|
[702] | 121 | |
---|
| 122 | return 0; |
---|
| 123 | } |
---|
| 124 | |
---|