[702] | 1 | /* |
---|
| 2 | * cacaplay caca file player |
---|
[3495] | 3 | * Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net> |
---|
[702] | 4 | * All Rights Reserved |
---|
| 5 | * |
---|
| 6 | * $Id: cacaplay.c 3495 2009-05-21 20:55:21Z sam $ |
---|
| 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" |
---|
| 16 | |
---|
[1048] | 17 | #if !defined(__KERNEL__) |
---|
[1352] | 18 | # include <stdio.h> |
---|
| 19 | # include <stdlib.h> |
---|
| 20 | # include <sys/types.h> |
---|
| 21 | # include <sys/stat.h> |
---|
| 22 | # include <fcntl.h> |
---|
| 23 | # include <string.h> |
---|
| 24 | # include <unistd.h> |
---|
[1048] | 25 | #endif |
---|
[702] | 26 | |
---|
| 27 | #include "caca.h" |
---|
| 28 | |
---|
| 29 | int main(int argc, char **argv) |
---|
| 30 | { |
---|
[2821] | 31 | caca_canvas_t *cv, *app; |
---|
[811] | 32 | caca_display_t *dp; |
---|
[2300] | 33 | uint8_t *buf = NULL; |
---|
[1355] | 34 | long int bytes = 0, total = 0; |
---|
[1352] | 35 | int fd; |
---|
[702] | 36 | |
---|
[1352] | 37 | if(argc < 2 || !strcmp(argv[1], "-")) |
---|
[702] | 38 | { |
---|
[1352] | 39 | fd = 0; /* use stdin */ |
---|
[702] | 40 | } |
---|
[1352] | 41 | else |
---|
[702] | 42 | { |
---|
[1352] | 43 | fd = open(argv[1], O_RDONLY); |
---|
| 44 | if(fd < 0) |
---|
| 45 | { |
---|
| 46 | fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]); |
---|
| 47 | return 1; |
---|
| 48 | } |
---|
[702] | 49 | } |
---|
| 50 | |
---|
[2821] | 51 | cv = caca_create_canvas(0, 0); |
---|
| 52 | app = caca_create_canvas(0, 0); |
---|
[1754] | 53 | if(cv == NULL || app == NULL) |
---|
| 54 | { |
---|
| 55 | printf("Can't created canvas\n"); |
---|
| 56 | return -1; |
---|
| 57 | } |
---|
[819] | 58 | dp = caca_create_display(cv); |
---|
[1754] | 59 | if(dp == NULL) |
---|
| 60 | { |
---|
| 61 | printf("Can't create display\n"); |
---|
| 62 | return -1; |
---|
| 63 | } |
---|
[702] | 64 | |
---|
[1754] | 65 | |
---|
[1352] | 66 | for(;;) |
---|
| 67 | { |
---|
| 68 | caca_event_t ev; |
---|
| 69 | int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0); |
---|
[3474] | 70 | int has_eof = 0; |
---|
[702] | 71 | |
---|
[2049] | 72 | if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS) |
---|
[702] | 73 | break; |
---|
[1352] | 74 | |
---|
[1355] | 75 | if(bytes == 0) |
---|
| 76 | { |
---|
| 77 | ssize_t n; |
---|
| 78 | buf = realloc(buf, total + 1); |
---|
| 79 | n = read(fd, buf + total, 1); |
---|
| 80 | if(n < 0) |
---|
| 81 | { |
---|
| 82 | fprintf(stderr, "%s: read error\n", argv[0]); |
---|
| 83 | return -1; |
---|
| 84 | } |
---|
| 85 | else if(n == 0) |
---|
| 86 | { |
---|
[3474] | 87 | has_eof = 1; |
---|
[1355] | 88 | } |
---|
| 89 | total += n; |
---|
| 90 | } |
---|
| 91 | |
---|
[3495] | 92 | bytes = caca_import_canvas_from_memory(app, buf, total, "caca"); |
---|
[1352] | 93 | |
---|
| 94 | if(bytes > 0) |
---|
| 95 | { |
---|
| 96 | total -= bytes; |
---|
| 97 | memmove(buf, buf + bytes, total); |
---|
| 98 | |
---|
[2821] | 99 | caca_blit(cv, 0, 0, app, NULL); |
---|
[1352] | 100 | caca_refresh_display(dp); |
---|
| 101 | } |
---|
[1355] | 102 | else if(bytes < 0) |
---|
[1352] | 103 | { |
---|
| 104 | fprintf(stderr, "%s: corrupted caca file\n", argv[0]); |
---|
[1355] | 105 | break; |
---|
[1352] | 106 | } |
---|
[1355] | 107 | |
---|
[3474] | 108 | if(has_eof) |
---|
[1355] | 109 | break; |
---|
[702] | 110 | } |
---|
| 111 | |
---|
[1355] | 112 | caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1); |
---|
| 113 | |
---|
[702] | 114 | /* Clean up */ |
---|
[1352] | 115 | close(fd); |
---|
| 116 | |
---|
[819] | 117 | caca_free_display(dp); |
---|
[2821] | 118 | caca_free_canvas(cv); |
---|
[702] | 119 | |
---|
| 120 | return 0; |
---|
| 121 | } |
---|
| 122 | |
---|