source: libcaca/trunk/src/cacadraw.c @ 903

Last change on this file since 903 was 903, checked in by Sam Hocevar, 14 years ago
  • Added cacadraw. It only shows files and lets you scroll for now, but it will eventually evolve into something better.
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
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 903 2006-04-26 12:25:44Z 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
24static int refresh_screen(void);
25
26static cucul_canvas_t *cv, *image;
27static caca_display_t *dp;
28static int x = 0, y = 0;
29
30int main(int argc, char **argv)
31{
32    cucul_buffer_t *b;
33    int refresh = 1;
34    unsigned int iw, ih;
35
36    if(argc < 2)
37    {
38        fprintf(stderr, "%s: missing argument (filename).\n", argv[0]);
39        return 1;
40    }
41
42    cv = cucul_create_canvas(0, 0);
43    if(!cv)
44        return 1;
45    dp = caca_create_display(cv);
46    if(!dp)
47        return 1;
48    caca_set_display_title(dp, argv[1]);
49
50    b = cucul_load_file(argv[1]);
51    if(!b)
52    {
53        fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]);
54        return 1;
55    }
56
57    image = cucul_import_canvas(b, "");
58    if(!image)
59    {
60        fprintf(stderr, "%s: could not import `%s'.\n", argv[0], argv[1]);
61        return 1;
62    }
63
64    cucul_free_buffer(b);
65
66    ih = cucul_get_canvas_height(image);
67    iw = cucul_get_canvas_width(image);
68
69    for(;;)
70    {
71        caca_event_t ev;
72        unsigned int w, h;
73
74        if(refresh)
75        {
76            refresh_screen();
77            refresh = 0;
78        }
79
80        while(caca_get_event(dp, CACA_EVENT_ANY, &ev, -1))
81        {
82            switch(ev.type)
83            {
84                case CACA_EVENT_QUIT:
85                    goto quit;
86                case CACA_EVENT_KEY_PRESS:
87                    switch(ev.data.key.ch)
88                    {
89                    case CACA_KEY_LEFT: x -= 2; refresh = 1; goto stopevents;
90                    case CACA_KEY_RIGHT: x += 2; refresh = 1; goto stopevents;
91                    case CACA_KEY_UP: y--; refresh = 1; goto stopevents;
92                    case CACA_KEY_DOWN: y++; refresh = 1; goto stopevents;
93                    case CACA_KEY_ESCAPE:
94                    case 'q':
95                        goto quit;
96                    default:
97                        break;
98                    }
99                case CACA_EVENT_RESIZE:
100                    refresh = 1;
101                    goto stopevents;
102                default:
103                    break;
104            }
105        }
106stopevents:
107
108        w = cucul_get_canvas_width(cv);
109        h = cucul_get_canvas_height(cv);
110        if(x < 0) x = 0; else if(x + w > iw) x = iw > w ? iw - w : 0;
111        if(y < 0) y = 0; else if(y + h > ih) y = ih > h ? ih - h : 0;
112    }
113quit:
114
115    /* Clean up */
116    caca_free_display(dp);
117    cucul_free_canvas(image);
118    cucul_free_canvas(cv);
119
120    return 0;
121}
122
123static int refresh_screen(void)
124{
125    cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_DEFAULT);
126    cucul_clear_canvas(cv);
127
128    cucul_blit(cv, - x, - y, image, NULL);
129
130    caca_refresh_display(dp);
131
132    return 0;
133}
134
Note: See TracBrowser for help on using the repository browser.