libcaca  0.99.beta19
About: libcaca is a graphics library that outputs text instead of pixels, so that it can work on older video cards or text terminals (something like an advanced AAlib library).
  Fossies Dox: libcaca-0.99.beta19.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

graphics.c
Go to the documentation of this file.
1 /*
2  * libcaca Colour ASCII-Art library
3  * Copyright (c) 2002-2012 Sam Hocevar <sam@hocevar.net>
4  * All Rights Reserved
5  *
6  * This library is free software. It comes without any warranty, to
7  * the extent permitted by applicable law. You can redistribute it
8  * and/or modify it under the terms of the Do What the Fuck You Want
9  * to Public License, Version 2, as published by Sam Hocevar. See
10  * http://www.wtfpl.net/ for more details.
11  */
12 
13 /*
14  * This file contains character and string drawing functions.
15  */
16 
17 #include "config.h"
18 
19 #if !defined(__KERNEL__)
20 # include <stdio.h>
21 # include <stdlib.h>
22 # include <string.h>
23 #endif
24 
25 #include "caca.h"
26 #include "caca_internals.h"
27 
40 int caca_set_display_title(caca_display_t *dp, char const *title)
41 {
42  int ret = dp->drv.set_display_title(dp, title);
43 
44  if(ret)
45  seterrno(ENOSYS);
46 
47  return ret;
48 }
49 
63 {
64  return dp->drv.get_display_width(dp);
65 }
66 
80 {
81  return dp->drv.get_display_height(dp);
82 }
83 
101 {
102  if(usec < 0)
103  {
104  seterrno(EINVAL);
105  return -1;
106  }
107 
108  dp->delay = usec;
109  return 0;
110 }
111 
126 {
127  return dp->rendertime;
128 }
129 
147 {
148 #if defined PROF
149  caca_timer_t proftimer = { 0, 0 };
150 #endif
151 #if !defined(_DOXYGEN_SKIP_ME)
152 # define IDLE_USEC 5000
153 #endif
154  int ticks = dp->lastticks + _caca_getticks(&dp->timer);
155 
156 #if defined PROF
157  _caca_getticks(&proftimer);
158 #endif
159  dp->drv.display(dp);
160 #if defined PROF
161  STAT_IADD(&dp->display_stat, _caca_getticks(&proftimer));
162 #endif
163 
164  /* Invalidate the dirty rectangle */
166 
167  /* Once the display is finished, we can ack resizes */
168  if(dp->resize.resized)
169  {
170  dp->resize.resized = 0;
172  }
173 
174 #if defined PROF
175  _caca_getticks(&proftimer);
176 #endif
177  /* Wait until dp->delay + time of last call */
178  ticks += _caca_getticks(&dp->timer);
179  for(ticks += _caca_getticks(&dp->timer);
180  ticks + IDLE_USEC < (int)dp->delay;
181  ticks += _caca_getticks(&dp->timer))
182  {
184  }
185 #if defined PROF
186  STAT_IADD(&dp->wait_stat, _caca_getticks(&proftimer));
187 #endif
188 
189  /* Update the render time */
190  dp->rendertime = ticks;
191 
192  dp->lastticks = ticks - dp->delay;
193 
194  /* If we drifted too much, it's bad, bad, bad. */
195  if(dp->lastticks > (int)dp->delay)
196  dp->lastticks = 0;
197 
198 #if defined PROF
199  _caca_dump_stats();
200 #endif
201 
202  return 0;
203 }
204 
219 {
220  if(!dp->drv.set_cursor)
221  {
222  seterrno(ENOSYS);
223  return -1;
224  }
225 
226  dp->drv.set_cursor(dp, flag);
227  return 0;
228 }
229 
244 {
245  if(!dp->drv.set_mouse)
246  {
247  seterrno(ENOSYS);
248  return -1;
249  }
250 
251  dp->drv.set_mouse(dp, flag);
252  return 0;
253 }
254 
255 /*
256  * XXX: following functions are local
257  */
258 
260 {
262 
263  /* Tell libcaca we changed size */
266  {
267  dp->resize.allow = 1;
269  dp->resize.allow = 0;
270  }
271 }
272 
273 void _caca_set_term_title(char const *str)
274 {
275 #if defined(HAVE_GETENV)
276  char *term;
277 
278  term = getenv("TERM");
279 
280  if(!term || !strcmp(term, "linux"))
281  return;
282 #endif
283 
284  fprintf(stdout, "\033]0;%s\007", str);
285  fflush(stdout);
286 }
287 
caca_display::drv
struct caca_display::drv drv
caca_set_mouse
int caca_set_mouse(caca_display_t *dp, int flag)
Show or hide the mouse pointer.
Definition: graphics.c:243
caca_display::rendertime
int rendertime
Definition: caca_internals.h:211
caca_clear_dirty_rect_list
int caca_clear_dirty_rect_list(caca_canvas_t *)
Clear a canvas's dirty rectangle list.
Definition: dirty.c:256
caca_set_display_time
int caca_set_display_time(caca_display_t *dp, int usec)
Set the refresh delay.
Definition: graphics.c:100
caca_display::cv
caca_canvas_t * cv
Definition: caca_internals.h:170
caca_display::resize
struct caca_display::resize resize
caca_display
Definition: caca_internals.h:167
caca_display::lastticks
int lastticks
Definition: caca_internals.h:217
caca_timer
Definition: caca_internals.h:140
caca_get_canvas_height
int caca_get_canvas_height(caca_canvas_t const *)
Get the canvas height.
Definition: canvas.c:253
caca_display::drv::set_cursor
void(* set_cursor)(caca_display_t *, int)
Definition: caca_internals.h:193
IDLE_USEC
#define IDLE_USEC
caca_get_canvas_width
int caca_get_canvas_width(caca_canvas_t const *)
Get the canvas width.
Definition: canvas.c:239
caca_display::drv::set_mouse
void(* set_mouse)(caca_display_t *, int)
Definition: caca_internals.h:192
caca_display::drv::get_display_width
int(* get_display_width)(caca_display_t const *)
Definition: caca_internals.h:187
caca_set_cursor
int caca_set_cursor(caca_display_t *dp, int flag)
Show or hide the cursor.
Definition: graphics.c:218
caca_display::resize::allow
int allow
Definition: caca_internals.h:206
_caca_handle_resize
void _caca_handle_resize(caca_display_t *dp)
Definition: graphics.c:259
seterrno
#define seterrno(x)
Definition: caca_stubs.h:27
caca_display::resize::w
int w
Definition: caca_internals.h:207
_caca_set_term_title
void _caca_set_term_title(char const *str)
Definition: graphics.c:273
caca_refresh_display
int caca_refresh_display(caca_display_t *dp)
Flush pending changes and redraw the screen.
Definition: graphics.c:146
_caca_getticks
int _caca_getticks(caca_timer_t *)
Definition: time.c:47
caca_display::drv::display
void(* display)(caca_display_t *)
Definition: caca_internals.h:189
caca_internals.h
caca_display::resize::h
int h
Definition: caca_internals.h:207
caca_display::drv::get_display_height
int(* get_display_height)(caca_display_t const *)
Definition: caca_internals.h:188
caca_display::drv::handle_resize
void(* handle_resize)(caca_display_t *)
Definition: caca_internals.h:190
caca_get_display_height
int caca_get_display_height(caca_display_t const *dp)
Get the display height.
Definition: graphics.c:79
caca_display::resize::resized
int resized
Definition: caca_internals.h:205
caca_display::timer
caca_timer_t timer
Definition: caca_internals.h:212
caca_set_display_title
int caca_set_display_title(caca_display_t *dp, char const *title)
Set the display title.
Definition: graphics.c:40
caca_display::drv::set_display_title
int(* set_display_title)(caca_display_t *, char const *)
Definition: caca_internals.h:186
caca_set_canvas_size
int caca_set_canvas_size(caca_canvas_t *, int, int)
Resize a canvas.
Definition: canvas.c:212
caca.h
The libcaca public header.
caca_display::delay
int delay
Definition: caca_internals.h:211
config.h
STAT_IADD
#define STAT_IADD(_s, _n)
Definition: caca_prof.h:40
caca_get_display_time
int caca_get_display_time(caca_display_t const *dp)
Get the display's average rendering time.
Definition: graphics.c:125
dp
caca_display_t * dp
Definition: cacaview.c:45
caca_get_display_width
int caca_get_display_width(caca_display_t const *dp)
Get the display width.
Definition: graphics.c:62
_caca_sleep
void _caca_sleep(int)
Definition: time.c:36