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

Last change on this file since 1063 was 1054, checked in by Sam Hocevar, 14 years ago
  • Do not build the raw output in kernel mode.
  • Fix cucul_utf32_to_cp437() call in the VGA output.
  • Property svn:keywords set to Id
File size: 4.9 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 1054 2006-09-18 07:20:06Z 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"
[859]21#include "common.h"
[63]22
[565]23#if !defined(__KERNEL__)
24#   include <stdlib.h>
25#   include <string.h>
[1006]26#   if defined(HAVE_ERRNO_H)
27#       include <errno.h>
28#   endif
[565]29#endif
[17]30
[524]31#include "cucul.h"
32#include "cucul_internals.h"
[185]33#include "caca.h"
34#include "caca_internals.h"
[17]35
[1006]36static int caca_select_driver(caca_display_t *dp);
[227]37
[810]38/** \brief Attach a caca graphical context to a cucul canvas.
[540]39 *
40 *  Create a graphical context using device-dependent features (ncurses for
41 *  terminals, an X11 window, a DOS command window...) that attaches to a
42 *  libcucul canvas. Everything that gets drawn in the libcucul canvas can
43 *  then be displayed by the libcaca driver.
44 *
[1006]45 *  If an error occurs, NULL is returned and \b errno is set accordingly:
46 *  - \c ENOMEM Not enough memory.
47 *  - \c ENODEV Graphical device could not be initialised.
48 *
[811]49 *  \param cv The cucul cavas.
[540]50 *  \return The caca graphical context or NULL if an error occurred.
51 */
[819]52caca_display_t * caca_create_display(cucul_canvas_t * cv)
[17]53{
[811]54    caca_display_t *dp = malloc(sizeof(caca_display_t));
[524]55
[1006]56    if(!dp)
57    {
58#if defined(HAVE_ERRNO_H)
59        errno = ENOMEM;
60#endif
61        return NULL;
62    }
63
[811]64    dp->cv = cv;
[232]65
[1006]66    if(caca_select_driver(dp))
[539]67    {
[811]68        free(dp);
[1006]69#if defined(HAVE_ERRNO_H)
70        errno = ENODEV;
71#endif
[524]72        return NULL;
[265]73    }
[79]74
[811]75    if(dp->drv.init_graphics(dp))
[335]76    {
[811]77        free(dp);
[1006]78#if defined(HAVE_ERRNO_H)
79        errno = ENODEV;
80#endif
[539]81        return NULL;
[483]82    }
[213]83
[540]84    /* Attached! */
[811]85    dp->cv->refcount++;
[540]86
87    /* Graphics stuff */
[811]88    dp->delay = 0;
89    dp->rendertime = 0;
[540]90
91    /* Events stuff */
92#if defined(USE_SLANG) || defined(USE_NCURSES)
[811]93    dp->events.key_timer.last_sec = 0;
94    dp->events.key_timer.last_usec = 0;
95    dp->events.last_key_ticks = 0;
96    dp->events.autorepeat_ticks = 0;
97    dp->events.last_key_event.type = CACA_EVENT_NONE;
[540]98#endif
[681]99#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL)
[811]100    dp->events.queue = 0;
[593]101#endif
[527]102
[811]103    dp->timer.last_sec = 0;
104    dp->timer.last_usec = 0;
105    dp->lastticks = 0;
[527]106
[553]107    /* Mouse position */
[811]108    dp->mouse.x = dp->cv->width / 2;
109    dp->mouse.y = dp->cv->height / 2;
[541]110
[553]111    /* Resize events */
[811]112    dp->resize.resized = 0;
[306]113
[811]114    return dp;
[17]115}
116
[540]117/** \brief Detach a caca graphical context from a cucul backend context.
118 *
119 *  Detach a graphical context from its cucul backend and destroy it. The
120 *  libcucul canvas continues to exist and other graphical contexts can be
121 *  attached to it afterwards.
122 *
[1006]123 *  This function never fails.
124 *
[811]125 *  \param dp The libcaca graphical context.
[1006]126 *  \return This function always returns 0.
[540]127 */
[1006]128int caca_free_display(caca_display_t *dp)
[17]129{
[811]130    dp->drv.end_graphics(dp);
131    dp->cv->refcount--;
132    free(dp);
[1006]133
134    return 0;
[17]135}
136
[268]137/*
138 * XXX: The following functions are local.
139 */
140
[1006]141static int caca_select_driver(caca_display_t *dp)
[265]142{
143#if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
144    char *var = getenv("CACA_DRIVER");
[586]145   
[265]146    /* If the environment variable was set, use it */
147    if(var && *var)
[623]148    {
[335]149#if defined(USE_WIN32)
[811]150        if(!strcasecmp(var, "win32")) return win32_install(dp);
[335]151#endif
[265]152#if defined(USE_CONIO)
[811]153        if(!strcasecmp(var, "conio")) return conio_install(dp);
[265]154#endif
[285]155#if defined(USE_X11)
[811]156        if(!strcasecmp(var, "x11")) return x11_install(dp);
[265]157#endif
[483]158#if defined(USE_GL)
[811]159        if(!strcasecmp(var, "gl")) return gl_install(dp);
[483]160#endif
[1054]161#if !defined(__KERNEL__)
[811]162        if(!strcasecmp(var, "raw")) return raw_install(dp);
[1054]163#endif
[265]164#if defined(USE_SLANG)
[811]165        if(!strcasecmp(var, "slang")) return slang_install(dp);
[265]166#endif
[285]167#if defined(USE_NCURSES)
[811]168        if(!strcasecmp(var, "ncurses")) return ncurses_install(dp);
[265]169#endif
[565]170#if defined(USE_VGA)
[811]171        if(!strcasecmp(var, "vga")) return vga_install(dp);
[565]172#endif
[684]173        return -1;
[265]174    }
175#endif
176
[335]177#if defined(USE_WIN32)
[811]178    if(win32_install(dp) == 0) return 0;
[335]179#endif
[265]180#if defined(USE_CONIO)
[811]181    if(conio_install(dp) == 0) return 0;
[265]182#endif
[565]183#if defined(USE_VGA)
[811]184    if(vga_install(dp) == 0) return 0;
[565]185#endif
[285]186#if defined(USE_X11)
[811]187    if(x11_install(dp) == 0) return 0;
[265]188#endif
[483]189#if defined(USE_GL)
[811]190    if(gl_install(dp) == 0) return 0;
[483]191#endif
[984]192    /* ncurses has a higher priority than slang because it has better colour
193     * support across terminal types, despite being slightly slower. */
194#if defined(USE_NCURSES)
195    if(ncurses_install(dp) == 0) return 0;
196#endif
[265]197#if defined(USE_SLANG)
[811]198    if(slang_install(dp) == 0) return 0;
[265]199#endif
[487]200
[539]201    return -1;
[265]202}
203
Note: See TracBrowser for help on using the repository browser.