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)  

caca.c
Go to the documentation of this file.
1 /*
2  * libcaca Colour ASCII-Art library
3  * Copyright (c) 2006-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 the main functions used by \e libcaca applications to
15  * initialise the library, get the screen properties, set the framerate and
16  * so on.
17  */
18 
19 #include "config.h"
20 
21 #if !defined(__KERNEL__)
22 # include <stdlib.h>
23 # include <string.h>
24 # include <stdio.h>
25 # if defined(USE_PLUGINS)
26 # if defined(HAVE_DLFCN_H)
27 # include <dlfcn.h>
28 # endif
29 # endif
30 #endif
31 
32 #include "caca.h"
33 #include "caca_internals.h"
34 
35 #if defined(USE_PLUGINS)
36 # define gl_install(p) caca_plugin_install(p, "gl")
37 # define x11_install(p) caca_plugin_install(p, "x11")
38 #endif
39 
40 static int caca_can_resize(caca_display_t *);
41 static int caca_install_driver(caca_display_t *, char const *);
43 static int caca_select_driver(caca_display_t *, char const *);
44 #if defined(USE_PLUGINS)
45 static int caca_plugin_install(caca_display_t *, char const *);
46 #endif
47 
69 {
70  return caca_create_display_with_driver(cv, NULL);
71 }
72 
99  char const *driver)
100 {
101  caca_display_t *dp = malloc(sizeof(caca_display_t));
102 
103  if(!dp)
104  {
105  seterrno(ENOMEM);
106  return NULL;
107  }
108 
109  if((dp->autorelease = (cv == NULL)))
110  {
111  cv = caca_create_canvas(0, 0);
112  }
113 
114  dp->cv = cv;
115 
116  if(caca_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
117  {
118  if(dp->autorelease)
120  free(dp);
121  seterrno(EBUSY);
122  return NULL;
123  }
124 
125  if(caca_install_driver(dp, driver))
126  {
127  caca_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
128  if(dp->autorelease)
130  free(dp);
131  seterrno(ENODEV);
132  return NULL;
133  }
134 
135  return dp;
136 }
137 
149 char const * const * caca_get_display_driver_list(void)
150 {
151  static char const * const list[] =
152  {
153 #if defined(USE_COCOA)
154  "cocoa", "Mac OS X Cocoa",
155 #endif
156 #if defined(USE_WIN32)
157  "win32", "Windows console",
158 #endif
159 #if defined(USE_CONIO)
160  "conio", "MS-DOS conio",
161 #endif
162 #if defined(USE_X11)
163  "x11", "X11 graphical window",
164 #endif
165 #if defined(USE_GL)
166  "gl", "OpenGL window",
167 #endif
168 #if defined(USE_SLANG)
169  "slang", "S-Lang console library",
170 #endif
171 #if defined(USE_NCURSES)
172  "ncurses", "ncurses console library",
173 #endif
174 #if defined(USE_VGA)
175  "vga", "direct VGA memory",
176 #endif
177 #if !defined(__KERNEL__)
178  "raw", "raw libcaca output",
179  "null", "null driver",
180 #endif
181  NULL, NULL
182  };
183 
184  return list;
185 }
186 
197 {
198  return dp->drv.driver;
199 }
200 
212 int caca_set_display_driver(caca_display_t *dp, char const *driver)
213 {
215  if(caca_install_driver(dp, driver))
216  {
217  seterrno(ENODEV);
218  return -1;
219  }
220 
221  return 0;
222 }
223 
239 {
241  caca_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp);
242  if(dp->autorelease)
244  free(dp);
245 
246  return 0;
247 }
248 
260 {
261  return dp->cv;
262 }
263 
272 char const * caca_get_version(void)
273 {
274  return PACKAGE_VERSION;
275 }
276 
277 /*
278  * XXX: The following functions are local.
279  */
280 
282 {
283  return dp->resize.allow;
284 }
285 
286 static int caca_install_driver(caca_display_t *dp, char const *driver)
287 {
288 #if defined(USE_PLUGINS)
289  dp->plugin = NULL;
290 #endif
291 
292  if(caca_select_driver(dp, driver))
293  {
294 #if defined(USE_PLUGINS)
295  if(dp->plugin)
296  dlclose(dp->plugin);
297 #endif
298  return -1;
299  }
300 
301  if(dp->drv.init_graphics(dp))
302  {
303 #if defined(USE_PLUGINS)
304  if(dp->plugin)
305  dlclose(dp->plugin);
306 #endif
307  return -1;
308  }
309 
310  /* Graphics stuff */
311  dp->delay = 0;
312  dp->rendertime = 0;
313 
314  /* Events stuff */
315 #if defined(USE_SLANG) || defined(USE_NCURSES)
316  dp->events.key_timer.last_sec = 0;
317  dp->events.key_timer.last_usec = 0;
318  dp->events.last_key_ticks = 0;
319  dp->events.autorepeat_ticks = 0;
320  dp->events.last_key_event.type = CACA_EVENT_NONE;
321 #endif
322 #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
323  dp->events.queue = 0;
324 #endif
325 
326  dp->timer.last_sec = 0;
327  dp->timer.last_usec = 0;
328 #if defined PROF
329  _caca_init_stat(&dp->display_stat, "dp[%p] disp_sys time", dp);
330  _caca_init_stat(&dp->wait_stat, "dp[%p] disp_wait time", dp);
331  _caca_init_stat(&dp->ev_sys_stat, "dp[%p] ev_sys time", dp);
332  _caca_init_stat(&dp->ev_wait_stat, "dp[%p] ev_wait time", dp);
333 #endif
334  dp->lastticks = 0;
335 
336  /* Mouse position */
337  dp->mouse.x = caca_get_canvas_width(dp->cv) / 2;
339 
340  /* Resize events */
341  dp->resize.resized = 0;
342  dp->resize.allow = 0;
343 
344  return 0;
345 }
346 
348 {
349  dp->drv.end_graphics(dp);
350 #if defined(USE_PLUGINS)
351  if(dp->plugin)
352  dlclose(dp->plugin);
353 #endif
354 #if defined PROF
355  _caca_fini_stat(&dp->display_stat);
356  _caca_fini_stat(&dp->wait_stat);
357  _caca_fini_stat(&dp->ev_wait_stat);
358  _caca_fini_stat(&dp->ev_sys_stat);
359 #endif
360 
361  return 0;
362 }
363 
364 static int caca_select_driver(caca_display_t *dp, char const *driver)
365 {
366  char const *var = driver;
367 #if defined(HAVE_GETENV)
368  if(!var)
369  var = getenv("CACA_DRIVER");
370 #endif
371 
372 #if defined(HAVE_STRCASECMP)
373  /* If the environment variable was set, use it */
374  if(var && *var)
375  {
376 #if defined(USE_COCOA)
377  if(!strcasecmp(var, "cocoa")) return cocoa_install(dp);
378 #endif
379 #if defined(USE_WIN32)
380  if(!strcasecmp(var, "win32")) return win32_install(dp);
381 #endif
382 #if defined(USE_CONIO)
383  if(!strcasecmp(var, "conio")) return conio_install(dp);
384 #endif
385 #if defined(USE_X11)
386  if(!strcasecmp(var, "x11")) return x11_install(dp);
387 #endif
388 #if defined(USE_GL)
389  if(!strcasecmp(var, "gl")) return gl_install(dp);
390 #endif
391 #if !defined(__KERNEL__)
392  if(!strcasecmp(var, "raw")) return raw_install(dp);
393 #endif
394 #if defined(USE_SLANG)
395  if(!strcasecmp(var, "slang")) return slang_install(dp);
396 #endif
397 #if defined(USE_NCURSES)
398  if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
399 #endif
400 #if defined(USE_VGA)
401  if(!strcasecmp(var, "vga")) return vga_install(dp);
402 #endif
403 #if !defined(__KERNEL__)
404  if(!strcasecmp(var, "null")) return null_install(dp);
405 #endif
406  return -1;
407  }
408 #endif
409 
410 #if defined(USE_COCOA)
411  if(cocoa_install(dp) == 0) return 0;
412 #endif
413 #if defined(USE_WIN32)
414  if(win32_install(dp) == 0) return 0;
415 #endif
416 #if defined(USE_CONIO)
417  if(conio_install(dp) == 0) return 0;
418 #endif
419 #if defined(USE_VGA)
420  if(vga_install(dp) == 0) return 0;
421 #endif
422 #if defined(USE_X11)
423  if(x11_install(dp) == 0) return 0;
424 #endif
425 #if defined(USE_GL)
426  if(gl_install(dp) == 0) return 0;
427 #endif
428  /* ncurses has a higher priority than slang because it has better colour
429  * support across terminal types, despite being slightly slower. */
430 #if defined(USE_NCURSES)
431  if(ncurses_install(dp) == 0) return 0;
432 #endif
433 #if defined(USE_SLANG)
434  if(slang_install(dp) == 0) return 0;
435 #endif
436  /* Of course we don't try "raw" or "null" if the user did not
437  * specifically ask for them. */
438 
439  return -1;
440 }
441 
442 #if defined(USE_PLUGINS)
443 static int caca_plugin_install(caca_display_t *dp, char const *driver)
444 {
445  char buf[512];
446  int (*sym) (caca_display_t *);
447 
448  sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, driver);
449  dp->plugin = dlopen(buf, RTLD_NOW);
450  if(!dp->plugin)
451  {
452  sprintf(buf, "lib%s_plugin.so", driver);
453  dp->plugin = dlopen(buf, RTLD_NOW);
454  if(!dp->plugin)
455  return -1;
456  }
457 
458  sprintf(buf, "%s_install", driver);
459  sym = dlsym(dp->plugin, buf);
460  if(!sym)
461  {
462  dlclose(dp->plugin);
463  return -1;
464  }
465 
466  return sym(dp);
467 }
468 #endif
469 
470 /*
471  * XXX: The following functions are aliases.
472  */
473 
474 char const * cucul_get_version(void) CACA_ALIAS(caca_get_version);
475 
caca_display::autorelease
int autorelease
Definition: caca_internals.h:171
caca_display::drv::driver
const char * driver
Definition: caca_internals.h:180
CACA_ALIAS
#define CACA_ALIAS(x)
Definition: caca.h:689
caca_display::drv
struct caca_display::drv drv
caca_create_display
caca_display_t * caca_create_display(caca_canvas_t *cv)
Attach a caca graphical context to a caca canvas.
Definition: caca.c:68
caca_display::mouse::y
int y
Definition: caca_internals.h:199
caca_get_version
const char * caca_get_version(void)
Return the libcaca version.
Definition: caca.c:272
cucul_get_version
#define cucul_get_version
Definition: caca.h:835
caca_display::rendertime
int rendertime
Definition: caca_internals.h:211
caca_display::cv
caca_canvas_t * cv
Definition: caca_internals.h:170
caca_display::resize
struct caca_display::resize resize
caca_get_display_driver
const char * caca_get_display_driver(caca_display_t *dp)
Return a caca graphical context's current output driver.
Definition: caca.c:196
PACKAGE_VERSION
#define PACKAGE_VERSION
Definition: config.h:75
caca_can_resize
static int caca_can_resize(caca_display_t *)
Definition: caca.c:281
caca_create_canvas
caca_canvas_t * caca_create_canvas(int, int)
Initialise a libcaca canvas.
Definition: canvas.c:54
caca_display
Definition: caca_internals.h:167
caca_display::lastticks
int lastticks
Definition: caca_internals.h:217
caca_free_canvas
int caca_free_canvas(caca_canvas_t *)
Free a libcaca canvas.
Definition: canvas.c:308
caca_get_canvas_height
int caca_get_canvas_height(caca_canvas_t const *)
Get the canvas height.
Definition: canvas.c:253
caca_set_display_driver
int caca_set_display_driver(caca_display_t *dp, char const *driver)
Set the output driver.
Definition: caca.c:212
caca_get_canvas_width
int caca_get_canvas_width(caca_canvas_t const *)
Get the canvas width.
Definition: canvas.c:239
caca_display::mouse::x
int x
Definition: caca_internals.h:199
caca_display::mouse
struct caca_display::mouse mouse
caca_select_driver
static int caca_select_driver(caca_display_t *, char const *)
Definition: caca.c:364
strcasecmp
#define strcasecmp
Definition: config.h:95
caca_install_driver
static int caca_install_driver(caca_display_t *, char const *)
Definition: caca.c:286
caca_display::resize::allow
int allow
Definition: caca_internals.h:206
caca_get_canvas
caca_canvas_t * caca_get_canvas(caca_display_t *dp)
Get the canvas attached to a caca graphical context.
Definition: caca.c:259
seterrno
#define seterrno(x)
Definition: caca_stubs.h:27
caca_unmanage_canvas
int caca_unmanage_canvas(caca_canvas_t *, int(*)(void *), void *)
unmanage a canvas.
Definition: canvas.c:167
caca_timer::last_usec
int last_usec
Definition: caca_internals.h:142
caca_get_display_driver_list
const char *const * caca_get_display_driver_list(void)
Get available display drivers.
Definition: caca.c:149
cv
caca_canvas_t * cv
Definition: cacaview.c:45
CACA_EVENT_NONE
Definition: caca.h:109
caca_create_display_with_driver
caca_display_t * caca_create_display_with_driver(caca_canvas_t *cv, char const *driver)
Attach a specific caca graphical context to a caca canvas.
Definition: caca.c:98
raw_install
int raw_install(caca_display_t *)
Definition: raw.c:94
caca_free_display
int caca_free_display(caca_display_t *dp)
Detach a caca graphical context from a caca backend context.
Definition: caca.c:238
caca_internals.h
caca_manage_canvas
int caca_manage_canvas(caca_canvas_t *, int(*)(void *), void *)
Manage a canvas.
Definition: canvas.c:133
caca_display::events
struct caca_display::events events
caca_display::drv::end_graphics
int(* end_graphics)(caca_display_t *)
Definition: caca_internals.h:185
caca_display::resize::resized
int resized
Definition: caca_internals.h:205
caca_display::timer
caca_timer_t timer
Definition: caca_internals.h:212
caca_timer::last_sec
int last_sec
Definition: caca_internals.h:142
caca.h
The libcaca public header.
caca_display::delay
int delay
Definition: caca_internals.h:211
config.h
caca_uninstall_driver
static int caca_uninstall_driver(caca_display_t *)
Definition: caca.c:347
dp
caca_display_t * dp
Definition: cacaview.c:45
caca_display::drv::init_graphics
int(* init_graphics)(caca_display_t *)
Definition: caca_internals.h:184
caca_canvas
Definition: caca_internals.h:47
null_install
int null_install(caca_display_t *)
Definition: null.c:71