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

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