core: add support for the autoverbose feature
[fbsplash.git] / core / src / mng_callbacks.c
bloba5007bfa8042e9ed25819e27846839cafc6a12d1
1 /*
2 * mng_callbacks.c - Callback functions for libmng
4 * Copyright (C) 2005 Bernard Blackham <bernard@blackham.com.au>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License v2. See the file COPYING in the main directory of this archive for
8 * more details.
12 #include <libmng.h>
13 #include <sys/time.h>
14 #include <time.h>
15 #include "common.h"
16 #include "render.h"
18 mng_ptr fb_mng_memalloc(mng_size_t len)
20 return calloc(1, len);
23 void fb_mng_memfree(mng_ptr p, mng_size_t len)
25 free(p);
28 static mng_bool fb_mng_openstream(mng_handle handle)
30 mng_anim *mng = mng_get_userdata(handle);
32 if (mng->data == NULL || mng->len == 0)
33 return MNG_FALSE;
35 mng->ptr = 0;
36 mng->open = 1;
38 return MNG_TRUE;
41 static mng_bool fb_mng_closestream(mng_handle handle)
43 mng_anim *mng = mng_get_userdata(handle);
45 mng->open = 0;
46 return MNG_TRUE;
49 static mng_bool fb_mng_readdata(mng_handle handle, mng_ptr buf,
50 mng_uint32 len, mng_uint32p pread)
52 mng_anim *mng = mng_get_userdata(handle);
53 char *src_buf;
55 if (mng->data == NULL || !mng->open)
56 return MNG_FALSE;
58 src_buf = ((char*)mng->data) + mng->ptr;
60 if (mng->ptr + len > mng->len)
61 len = mng->len - mng->ptr;
63 memcpy(buf, src_buf, len);
64 if (pread)
65 *pread = len;
67 mng->ptr += len;
69 return MNG_TRUE;
72 static mng_ptr fb_mng_getcanvasline(mng_handle handle, mng_uint32 line_num)
74 mng_anim *mng = mng_get_userdata(handle);
76 if (mng->canvas == NULL || line_num >= mng->canvas_h) {
77 iprint(MSG_ERROR, "%s(mngh, %d): Requested invalid line or canvas was NULL.\n",
78 __FUNCTION__, line_num);
79 return MNG_NULL;
82 return mng->canvas + (line_num * mng->canvas_w * mng->canvas_bytes_pp);
85 static mng_bool fb_mng_refresh(mng_handle handle, mng_uint32 x, mng_uint32 y,
86 mng_uint32 width, mng_uint32 height)
89 /* FIXME */
90 return MNG_TRUE;
93 static mng_uint32 fb_mng_gettickcount(mng_handle handle)
95 struct timeval tv;
96 mng_anim *mng = mng_get_userdata(handle);
98 if (gettimeofday(&tv, NULL) < 0) {
99 perror("fb_mng_gettickcount: gettimeofday");
100 abort();
103 if (mng->start_time.tv_sec == 0) {
104 mng->start_time.tv_sec = tv.tv_sec;
105 mng->start_time.tv_usec = tv.tv_usec;
108 return ((tv.tv_sec - mng->start_time.tv_sec)*1000) +
109 ((tv.tv_usec - mng->start_time.tv_usec)/1000);
112 static mng_bool fb_mng_settimer(mng_handle handle, mng_uint32 msecs)
114 mng_anim *mng = mng_get_userdata(handle);
116 mng->wait_msecs = msecs;
117 return MNG_TRUE;
120 static mng_bool fb_mng_processheader(mng_handle handle, mng_uint32 width,
121 mng_uint32 height)
123 mng_anim *mng = mng_get_userdata(handle);
125 free(mng->canvas);
127 mng->canvas_bytes_pp = 4;
129 if ((mng->canvas = malloc(width*height*mng->canvas_bytes_pp)) == NULL) {
130 iprint(MSG_ERROR, "%s: Unable to allocate memory for MNG canvas\n",
131 __FUNCTION__);
132 return MNG_FALSE;
134 mng->canvas_w = width;
135 mng->canvas_h = height;
137 mng_set_canvasstyle(handle, MNG_CANVAS_RGBA8);
138 #if 0
139 mng_set_bgcolor(handle, 0, 0, 0); /* FIXME - make configurable? */
140 #endif
141 return MNG_TRUE;
144 static mng_bool fb_mng_errorproc(mng_handle handler, mng_int32 code,
145 mng_int8 severity, mng_chunkid chunkname, mng_uint32 chunkseq,
146 mng_int32 extra1, mng_int32 extra2, mng_pchar errtext)
148 iprint(MSG_ERROR, "libmng error: Code: %d, Severity: %d - %s\n",
149 code, severity, errtext);
150 abort();
151 return MNG_TRUE;
154 #ifdef MNG_SUPPORT_TRACE
155 static mng_bool fb_mng_traceproc(mng_handle handle, mng_int32 funcnr,
156 mng_int32 seq, mng_pchar funcname)
158 iprint(MSG_ERROR, "libmng trace: %s (seq %d\n)", funcname, seq);
159 return MNG_TRUE;
162 #endif
164 mng_retcode mng_init_callbacks(mng_handle handle)
166 mng_retcode ret;
168 #define set_cb(x) \
169 if ((ret = mng_setcb_##x(handle, fb_mng_##x)) != MNG_NOERROR) \
170 return ret;
172 set_cb(errorproc);
173 set_cb(openstream);
174 set_cb(closestream);
175 set_cb(readdata);
176 set_cb(getcanvasline);
177 set_cb(refresh);
178 set_cb(gettickcount);
179 set_cb(settimer);
180 set_cb(processheader);
181 #ifdef MNG_SUPPORT_TRACE
182 set_cb(traceproc);
183 #endif
185 #undef set_cb
186 return MNG_NOERROR;