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

Last change on this file since 905 was 905, checked in by Jean-Yves Lamoureux, 15 years ago
  • page up / down
  • Property svn:keywords set to Id
File size: 3.3 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 905 2006-04-26 12:36:31Z jylam $
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_PAGEUP: y-=25; refresh = 1; goto stopevents;
94                    case CACA_KEY_PAGEDOWN: y+=25; refresh = 1; goto stopevents;
95                    case CACA_KEY_ESCAPE:
96                    case 'q':
97                        goto quit;
98                    default:
99                        break;
100                    }
101                case CACA_EVENT_RESIZE:
102                    refresh = 1;
103                    goto stopevents;
104                default:
105                    break;
106            }
107        }
108stopevents:
109
110        w = cucul_get_canvas_width(cv);
111        h = cucul_get_canvas_height(cv);
112        if(x < 0) x = 0; else if(x + w > iw) x = iw > w ? iw - w : 0;
113        if(y < 0) y = 0; else if(y + h > ih) y = ih > h ? ih - h : 0;
114    }
115quit:
116
117    /* Clean up */
118    caca_free_display(dp);
119    cucul_free_canvas(image);
120    cucul_free_canvas(cv);
121
122    return 0;
123}
124
125static int refresh_screen(void)
126{
127    cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_DEFAULT);
128    cucul_clear_canvas(cv);
129
130    cucul_blit(cv, - x, - y, image, NULL);
131
132    caca_refresh_display(dp);
133
134    return 0;
135}
136
Note: See TracBrowser for help on using the repository browser.