[903] | 1 | /* |
---|
| 2 | * event event lister for libcaca |
---|
| 3 | * Copyright (c) 2004 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
| 5 | * |
---|
| 6 | * $Id: cacadraw.c 945 2006-05-09 01:12:58Z sam $ |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
| 10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
| 11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | #include "config.h" |
---|
| 15 | #include "common.h" |
---|
| 16 | |
---|
| 17 | #include <stdio.h> |
---|
| 18 | #include <string.h> |
---|
| 19 | #include <stdlib.h> |
---|
| 20 | |
---|
| 21 | #include "cucul.h" |
---|
| 22 | #include "caca.h" |
---|
| 23 | |
---|
| 24 | static int refresh_screen(void); |
---|
| 25 | |
---|
| 26 | static cucul_canvas_t *cv, *image; |
---|
| 27 | static caca_display_t *dp; |
---|
| 28 | static int x = 0, y = 0; |
---|
| 29 | |
---|
| 30 | int main(int argc, char **argv) |
---|
| 31 | { |
---|
[945] | 32 | int refresh = 1, file = 1; |
---|
| 33 | unsigned int iw = 0, ih = 0; |
---|
[903] | 34 | |
---|
| 35 | if(argc < 2) |
---|
| 36 | { |
---|
| 37 | fprintf(stderr, "%s: missing argument (filename).\n", argv[0]); |
---|
| 38 | return 1; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | cv = cucul_create_canvas(0, 0); |
---|
| 42 | if(!cv) |
---|
| 43 | return 1; |
---|
| 44 | dp = caca_create_display(cv); |
---|
| 45 | if(!dp) |
---|
| 46 | return 1; |
---|
| 47 | |
---|
[945] | 48 | for(;;) |
---|
[903] | 49 | { |
---|
[945] | 50 | caca_event_t ev; |
---|
| 51 | unsigned int w, h; |
---|
| 52 | int dx = 0, dy = 0; |
---|
[903] | 53 | |
---|
[945] | 54 | if(!image) |
---|
| 55 | { |
---|
| 56 | cucul_buffer_t *b = cucul_load_file(argv[file]); |
---|
| 57 | if(!b) |
---|
| 58 | { |
---|
| 59 | fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]); |
---|
| 60 | return 1; |
---|
| 61 | } |
---|
[903] | 62 | |
---|
[945] | 63 | image = cucul_import_canvas(b, "ansi"); |
---|
| 64 | if(!image) |
---|
| 65 | { |
---|
| 66 | fprintf(stderr, "%s: invalid file `%s'.\n", argv[0], argv[1]); |
---|
| 67 | return 1; |
---|
| 68 | } |
---|
[903] | 69 | |
---|
[945] | 70 | cucul_free_buffer(b); |
---|
[903] | 71 | |
---|
[945] | 72 | ih = cucul_get_canvas_height(image); |
---|
| 73 | iw = cucul_get_canvas_width(image); |
---|
| 74 | x = y = 0; |
---|
[903] | 75 | |
---|
[945] | 76 | caca_set_display_title(dp, argv[file]); |
---|
| 77 | |
---|
| 78 | refresh = 1; |
---|
| 79 | } |
---|
| 80 | |
---|
[903] | 81 | if(refresh) |
---|
| 82 | { |
---|
| 83 | refresh_screen(); |
---|
| 84 | refresh = 0; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | while(caca_get_event(dp, CACA_EVENT_ANY, &ev, -1)) |
---|
| 88 | { |
---|
| 89 | switch(ev.type) |
---|
| 90 | { |
---|
| 91 | case CACA_EVENT_QUIT: |
---|
| 92 | goto quit; |
---|
| 93 | case CACA_EVENT_KEY_PRESS: |
---|
| 94 | switch(ev.data.key.ch) |
---|
| 95 | { |
---|
[945] | 96 | case CACA_KEY_LEFT: dx -= 2; break; |
---|
| 97 | case CACA_KEY_RIGHT: dx += 2; break; |
---|
| 98 | case CACA_KEY_UP: dy--; break; |
---|
| 99 | case CACA_KEY_DOWN: dy++; break; |
---|
| 100 | case CACA_KEY_PAGEUP: dy -= 12; break; |
---|
| 101 | case CACA_KEY_PAGEDOWN: dy += 12; break; |
---|
[903] | 102 | case CACA_KEY_ESCAPE: |
---|
| 103 | case 'q': |
---|
| 104 | goto quit; |
---|
[945] | 105 | case 'n': |
---|
| 106 | file = file + 1 < argc ? file + 1 : 1; |
---|
| 107 | cucul_free_canvas(image); |
---|
| 108 | image = NULL; |
---|
| 109 | goto stopevents; |
---|
| 110 | case 'p': |
---|
| 111 | file = file > 1 ? file - 1 : argc - 1; |
---|
| 112 | cucul_free_canvas(image); |
---|
| 113 | image = NULL; |
---|
| 114 | goto stopevents; |
---|
[903] | 115 | default: |
---|
| 116 | break; |
---|
| 117 | } |
---|
| 118 | case CACA_EVENT_RESIZE: |
---|
| 119 | refresh = 1; |
---|
| 120 | goto stopevents; |
---|
| 121 | default: |
---|
| 122 | break; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | stopevents: |
---|
| 126 | |
---|
| 127 | w = cucul_get_canvas_width(cv); |
---|
| 128 | h = cucul_get_canvas_height(cv); |
---|
[945] | 129 | |
---|
| 130 | if(dx | dy) |
---|
| 131 | { |
---|
| 132 | refresh = 1; |
---|
| 133 | x += dx; |
---|
| 134 | y += dy; |
---|
| 135 | |
---|
| 136 | if(x < 0) x = 0; else if(x + w > iw) x = iw > w ? iw - w : 0; |
---|
| 137 | if(y < 0) y = 0; else if(y + h > ih) y = ih > h ? ih - h : 0; |
---|
| 138 | } |
---|
[903] | 139 | } |
---|
| 140 | quit: |
---|
| 141 | |
---|
| 142 | /* Clean up */ |
---|
| 143 | caca_free_display(dp); |
---|
| 144 | cucul_free_canvas(image); |
---|
| 145 | cucul_free_canvas(cv); |
---|
| 146 | |
---|
| 147 | return 0; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | static int refresh_screen(void) |
---|
| 151 | { |
---|
| 152 | cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_DEFAULT); |
---|
| 153 | cucul_clear_canvas(cv); |
---|
| 154 | |
---|
| 155 | cucul_blit(cv, - x, - y, image, NULL); |
---|
| 156 | |
---|
| 157 | caca_refresh_display(dp); |
---|
| 158 | |
---|
| 159 | return 0; |
---|
| 160 | } |
---|
| 161 | |
---|