| 1 | /* |
|---|
| 2 | * cacaplay caca file player |
|---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This program is free software. It comes without any warranty, to |
|---|
| 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 |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | #include "config.h" |
|---|
| 16 | #include "common.h" |
|---|
| 17 | |
|---|
| 18 | #if !defined(__KERNEL__) |
|---|
| 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> |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | #include "cucul.h" |
|---|
| 29 | #include "caca.h" |
|---|
| 30 | |
|---|
| 31 | int main(int argc, char **argv) |
|---|
| 32 | { |
|---|
| 33 | cucul_canvas_t *cv, *app; |
|---|
| 34 | caca_display_t *dp; |
|---|
| 35 | unsigned char *buf = NULL; |
|---|
| 36 | long int bytes = 0, total = 0; |
|---|
| 37 | int fd; |
|---|
| 38 | |
|---|
| 39 | if(argc < 2 || !strcmp(argv[1], "-")) |
|---|
| 40 | { |
|---|
| 41 | fd = 0; /* use stdin */ |
|---|
| 42 | } |
|---|
| 43 | else |
|---|
| 44 | { |
|---|
| 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 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | cv = cucul_create_canvas(0, 0); |
|---|
| 54 | app = cucul_create_canvas(0, 0); |
|---|
| 55 | if(cv == NULL || app == NULL) |
|---|
| 56 | { |
|---|
| 57 | printf("Can't created canvas\n"); |
|---|
| 58 | return -1; |
|---|
| 59 | } |
|---|
| 60 | dp = caca_create_display(cv); |
|---|
| 61 | if(dp == NULL) |
|---|
| 62 | { |
|---|
| 63 | printf("Can't create display\n"); |
|---|
| 64 | return -1; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | for(;;) |
|---|
| 69 | { |
|---|
| 70 | caca_event_t ev; |
|---|
| 71 | int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0); |
|---|
| 72 | int eof = 0; |
|---|
| 73 | |
|---|
| 74 | if(ret && caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS) |
|---|
| 75 | break; |
|---|
| 76 | |
|---|
| 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 | |
|---|
| 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 | } |
|---|
| 104 | else if(bytes < 0) |
|---|
| 105 | { |
|---|
| 106 | fprintf(stderr, "%s: corrupted caca file\n", argv[0]); |
|---|
| 107 | break; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | if(eof) |
|---|
| 111 | break; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1); |
|---|
| 115 | |
|---|
| 116 | /* Clean up */ |
|---|
| 117 | close(fd); |
|---|
| 118 | |
|---|
| 119 | caca_free_display(dp); |
|---|
| 120 | cucul_free_canvas(cv); |
|---|
| 121 | |
|---|
| 122 | return 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|