source: libcaca/trunk/caca/caca.c @ 811

Last change on this file since 811 was 811, checked in by Sam Hocevar, 15 years ago
  • Renamed caca_t into caca_display_t.
  • Renamed canvas handle variables from "c" to "cv". Eh ouais mon gros.
  • Property svn:keywords set to Id
File size: 4.2 KB
RevLine 
[35]1/*
[672]2 *  libcaca       Colour ASCII-Art library
[699]3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
[268]4 *                All Rights Reserved
[35]5 *
[769]6 *  $Id: caca.c 811 2006-04-18 15:11:25Z sam $
7 *
[268]8 *  This library is free software; you can redistribute it and/or
[522]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.
[35]12 */
[17]13
[769]14/*
[268]15 *  This file contains the main functions used by \e libcaca applications to
16 *  initialise the library, get the screen properties, set the framerate and
17 *  so on.
[205]18 */
19
[63]20#include "config.h"
21
[565]22#if !defined(__KERNEL__)
23#   include <stdlib.h>
24#   include <string.h>
25#endif
[17]26
[524]27#include "cucul.h"
28#include "cucul_internals.h"
[185]29#include "caca.h"
30#include "caca_internals.h"
[17]31
[811]32static int caca_init_driver(caca_display_t *dp);
[227]33
[810]34/** \brief Attach a caca graphical context to a cucul canvas.
[540]35 *
36 *  Create a graphical context using device-dependent features (ncurses for
37 *  terminals, an X11 window, a DOS command window...) that attaches to a
38 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
39 *  then be displayed by the libcaca driver.
40 *
[811]41 *  \param cv The cucul cavas.
[540]42 *  \return The caca graphical context or NULL if an error occurred.
43 */
[811]44caca_display_t * caca_attach(cucul_canvas_t * cv)
[17]45{
[811]46    caca_display_t *dp = malloc(sizeof(caca_display_t));
[524]47
[811]48    dp->cv = cv;
[232]49
[811]50    if(caca_init_driver(dp))
[539]51    {
[811]52        free(dp);
[524]53        return NULL;
[265]54    }
[79]55
[811]56    if(dp->drv.init_graphics(dp))
[335]57    {
[811]58        free(dp);
[539]59        return NULL;
[483]60    }
[213]61
[540]62    /* Attached! */
[811]63    dp->cv->refcount++;
[540]64
65    /* Graphics stuff */
[811]66    dp->delay = 0;
67    dp->rendertime = 0;
[540]68
69    /* Events stuff */
70#if defined(USE_SLANG) || defined(USE_NCURSES)
[811]71    dp->events.key_timer.last_sec = 0;
72    dp->events.key_timer.last_usec = 0;
73    dp->events.last_key_ticks = 0;
74    dp->events.autorepeat_ticks = 0;
75    dp->events.last_key_event.type = CACA_EVENT_NONE;
[540]76#endif
[681]77#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
[811]78    dp->events.queue = 0;
[593]79#endif
[527]80
[811]81    dp->timer.last_sec = 0;
82    dp->timer.last_usec = 0;
83    dp->lastticks = 0;
[527]84
[553]85    /* Mouse position */
[811]86    dp->mouse.x = dp->cv->width / 2;
87    dp->mouse.y = dp->cv->height / 2;
[541]88
[553]89    /* Resize events */
[811]90    dp->resize.resized = 0;
[306]91
[811]92    return dp;
[17]93}
94
[540]95/** \brief Detach a caca graphical context from a cucul backend context.
96 *
97 *  Detach a graphical context from its cucul backend and destroy it. The
98 *  libcucul canvas continues to exist and other graphical contexts can be
99 *  attached to it afterwards.
100 *
[811]101 *  \param dp The libcaca graphical context.
[540]102 */
[811]103void caca_detach(caca_display_t *dp)
[17]104{
[811]105    dp->drv.end_graphics(dp);
106    dp->cv->refcount--;
107    free(dp);
[17]108}
109
[268]110/*
111 * XXX: The following functions are local.
112 */
113
[811]114static int caca_init_driver(caca_display_t *dp)
[265]115{
116#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
117    char *var = getenv("CACA_DRIVER");
[586]118   
[265]119    /* If the environment variable was set, use it */
120    if(var && *var)
[623]121    {
[335]122#if defined(USE_WIN32)
[811]123        if(!strcasecmp(var, "win32")) return win32_install(dp);
[335]124#endif
[265]125#if defined(USE_CONIO)
[811]126        if(!strcasecmp(var, "conio")) return conio_install(dp);
[265]127#endif
[285]128#if defined(USE_X11)
[811]129        if(!strcasecmp(var, "x11")) return x11_install(dp);
[265]130#endif
[483]131#if defined(USE_GL)
[811]132        if(!strcasecmp(var, "gl")) return gl_install(dp);
[483]133#endif
[811]134        if(!strcasecmp(var, "raw")) return raw_install(dp);
[265]135#if defined(USE_SLANG)
[811]136        if(!strcasecmp(var, "slang")) return slang_install(dp);
[265]137#endif
[285]138#if defined(USE_NCURSES)
[811]139        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
[265]140#endif
[565]141#if defined(USE_VGA)
[811]142        if(!strcasecmp(var, "vga")) return vga_install(dp);
[565]143#endif
[684]144        return -1;
[265]145    }
146#endif
147
[335]148#if defined(USE_WIN32)
[811]149    if(win32_install(dp) == 0) return 0;
[335]150#endif
[265]151#if defined(USE_CONIO)
[811]152    if(conio_install(dp) == 0) return 0;
[265]153#endif
[565]154#if defined(USE_VGA)
[811]155    if(vga_install(dp) == 0) return 0;
[565]156#endif
[285]157#if defined(USE_X11)
[811]158    if(x11_install(dp) == 0) return 0;
[265]159#endif
[483]160#if defined(USE_GL)
[811]161    if(gl_install(dp) == 0) return 0;
[483]162#endif
[265]163#if defined(USE_SLANG)
[811]164    if(slang_install(dp) == 0) return 0;
[265]165#endif
[285]166#if defined(USE_NCURSES)
[811]167    if(ncurses_install(dp) == 0) return 0;
[265]168#endif
[487]169
[539]170    return -1;
[265]171}
172
Note: See TracBrowser for help on using the repository browser.