source: libcaca/trunk/caca/graphics.c @ 2299

Last change on this file since 2299 was 2299, checked in by Sam Hocevar, 12 years ago
  • Remove #include <stdint.h> etc. from "common.h". Instead, make sure that <cucul.h> will provide the C99 types, even if libcaca has been installed.
  • Rename what's left of "common.h" to "stubs.h".
  • Remove all references to erroneous <inttypes.h> from source files.
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: graphics.c 2299 2008-04-19 12:42:50Z sam $
7 *
8 *  This library 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/*
16 *  This file contains character and string drawing functions.
17 */
18
19#include "config.h"
20
21#if !defined(__KERNEL__)
22#   include <stdio.h>
23#   include <stdlib.h>
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "caca.h"
29#include "caca_internals.h"
30
31/** \brief Set the display title.
32 *
33 *  If libcaca runs in a window, try to change its title. This works with
34 *  the ncurses, S-Lang, OpenGL, X11 and Win32 drivers.
35 *
36 *  If an error occurs, -1 is returned and \b errno is set accordingly:
37 *  - \c ENOSYS Display driver does not support setting the window title.
38 *
39 *  \param dp The libcaca display context.
40 *  \param title The desired display title.
41 *  \return 0 upon success, -1 if an error occurred.
42 */
43int caca_set_display_title(caca_display_t *dp, char const *title)
44{
45    int ret = dp->drv.set_display_title(dp, title);
46
47    if(ret)
48        seterrno(ENOSYS);
49
50    return ret;
51}
52
53/** \brief Get the display width.
54 *
55 *  If libcaca runs in a window, get the usable window width. This value can
56 *  be used for aspect ratio calculation. If libcaca does not run in a window
57 *  or if there is no way to know the font size, most drivers will assume a
58 *  6x10 font is being used. Note that the units are not necessarily pixels.
59 *
60 *  This function never fails.
61 *
62 *  \param dp The libcaca display context.
63 *  \return The display width.
64 */
65unsigned int caca_get_display_width(caca_display_t const *dp)
66{
67    return dp->drv.get_display_width(dp);
68}
69
70/** \brief Get the display height.
71 *
72 *  If libcaca runs in a window, get the usable window height. This value can
73 *  be used for aspect ratio calculation. If libcaca does not run in a window
74 *  or if there is no way to know the font size, assume a 6x10 font is being
75 *  used. Note that the units are not necessarily pixels.
76 *
77 *  This function never fails.
78 *
79 *  \param dp The libcaca display context.
80 *  \return The display height.
81 */
82unsigned int caca_get_display_height(caca_display_t const *dp)
83{
84    return dp->drv.get_display_height(dp);
85}
86
87/** \brief Set the refresh delay.
88 *
89 *  Set the refresh delay in microseconds. The refresh delay is used by
90 *  caca_refresh_display() to achieve constant framerate. See the
91 *  caca_refresh_display() documentation for more details.
92 *
93 *  If the argument is zero, constant framerate is disabled. This is the
94 *  default behaviour.
95 *
96 *  This function never fails.
97 *
98 *  \param dp The libcaca display context.
99 *  \param usec The refresh delay in microseconds.
100 *  \return This function always returns 0.
101 */
102int caca_set_display_time(caca_display_t *dp, unsigned int usec)
103{
104    dp->delay = usec;
105    return 0;
106}
107
108/** \brief Get the display's average rendering time.
109 *
110 *  Get the average rendering time, which is the average measured time
111 *  between two caca_refresh_display() calls, in microseconds. If constant
112 *  framerate was activated by calling caca_set_display_time(), the average
113 *  rendering time will be close to the requested delay even if the real
114 *  rendering time was shorter.
115 *
116 *  This function never fails.
117 *
118 *  \param dp The libcaca display context.
119 *  \return The render time in microseconds.
120 */
121unsigned int caca_get_display_time(caca_display_t const *dp)
122{
123    return dp->rendertime;
124}
125
126/** \brief Flush pending changes and redraw the screen.
127 *
128 *  Flush all graphical operations and print them to the display device.
129 *  Nothing will show on the screen until this function is called.
130 *
131 *  If caca_set_display_time() was called with a non-zero value,
132 *  caca_refresh_display() will use that value to achieve constant
133 *  framerate: if two consecutive calls to caca_refresh_display() are within
134 *  a time range shorter than the value set with caca_set_display_time(),
135 *  the second call will be delayed before performing the screen refresh.
136 *
137 *  This function never fails.
138 *
139 *  \param dp The libcaca display context.
140 *  \return This function always returns 0.
141 */
142int caca_refresh_display(caca_display_t *dp)
143{
144#if !defined(_DOXYGEN_SKIP_ME)
145#define IDLE_USEC 5000
146#endif
147    int ticks = dp->lastticks + _caca_getticks(&dp->timer);
148
149    dp->drv.display(dp);
150
151    /* Once the display is finished, we can ack resizes */
152    if(dp->resize.resized)
153    {
154        dp->resize.resized = 0;
155        _caca_handle_resize(dp);
156    }
157
158    /* Wait until dp->delay + time of last call */
159    ticks += _caca_getticks(&dp->timer);
160    for(ticks += _caca_getticks(&dp->timer);
161        ticks + IDLE_USEC < (int)dp->delay;
162        ticks += _caca_getticks(&dp->timer))
163    {
164        _caca_sleep(IDLE_USEC);
165    }
166
167    /* Update the sliding mean of the render time */
168    dp->rendertime = (7 * dp->rendertime + ticks) / 8;
169
170    dp->lastticks = ticks - dp->delay;
171
172    /* If we drifted too much, it's bad, bad, bad. */
173    if(dp->lastticks > (int)dp->delay)
174        dp->lastticks = 0;
175
176    return 0;
177}
178
179/** \brief Show or hide the cursor.
180 *
181 *  Show or hide the cursor, for devices that support such a feature.
182 *
183 *  If an error occurs, -1 is returned and \b errno is set accordingly:
184 *  - \c ENOSYS Display driver does not support showing the cursor.
185 *
186 *  \param dp The libcaca display context.
187 *  \param flag 0 hides the cursor, 1 shows the system's default cursor
188 *              (usually a white rectangle). Other values are reserved for
189 *              future use.
190 *  \return 0 upon success, -1 if an error occurred.
191 */
192int caca_set_cursor(caca_display_t *dp, int flag)
193{
194    if(!dp->drv.set_cursor)
195    {
196        seterrno(ENOSYS);
197        return -1;
198    }
199
200    dp->drv.set_cursor(dp, flag);
201    return 0;
202}
203
204/** \brief Show or hide the mouse pointer.
205 *
206 *  Show or hide the mouse pointer. This function works with the ncurses,
207 *  S-Lang and X11 drivers.
208 *
209 *  If an error occurs, -1 is returned and \b errno is set accordingly:
210 *  - \c ENOSYS Display driver does not support hiding the mouse pointer.
211 *
212 *  \param dp The libcaca display context.
213 *  \param flag 0 hides the pointer, 1 shows the system's default pointer
214 *              (usually an arrow). Other values are reserved for future use.
215 *  \return 0 upon success, -1 if an error occurred.
216 */
217int caca_set_mouse(caca_display_t *dp, int flag)
218{
219    if(!dp->drv.set_mouse)
220    {
221        seterrno(ENOSYS);
222        return -1;
223    }
224
225    dp->drv.set_mouse(dp, flag);
226    return 0;
227}
228
229/*
230 * XXX: following functions are local
231 */
232
233void _caca_handle_resize(caca_display_t *dp)
234{
235    dp->drv.handle_resize(dp);
236
237    /* Tell libcucul we changed size */
238    if(dp->resize.w != cucul_get_canvas_width(dp->cv)
239        || dp->resize.h != cucul_get_canvas_height(dp->cv))
240    {
241        dp->resize.allow = 1;
242        cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
243        dp->resize.allow = 0;
244    }
245}
246
247void _caca_set_term_title(char const *str)
248{
249#if defined(HAVE_GETENV)
250    char *term;
251
252    term = getenv("TERM");
253
254    if(!term || !strcmp(term, "linux"))
255        return;
256#endif
257
258    fprintf(stdout, "\x1b]0;%s\x07", str);
259    fflush(stdout);
260}
261
Note: See TracBrowser for help on using the repository browser.