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
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
)
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)
41 static mng_bool
fb_mng_closestream(mng_handle handle
)
43 mng_anim
*mng
= mng_get_userdata(handle
);
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
);
55 if (mng
->data
== NULL
|| !mng
->open
)
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
);
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
);
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
)
93 static mng_uint32
fb_mng_gettickcount(mng_handle handle
)
96 mng_anim
*mng
= mng_get_userdata(handle
);
98 if (gettimeofday(&tv
, NULL
) < 0) {
99 perror("fb_mng_gettickcount: gettimeofday");
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
;
120 static mng_bool
fb_mng_processheader(mng_handle handle
, mng_uint32 width
,
123 mng_anim
*mng
= mng_get_userdata(handle
);
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",
134 mng
->canvas_w
= width
;
135 mng
->canvas_h
= height
;
137 mng_set_canvasstyle(handle
, MNG_CANVAS_RGBA8
);
139 mng_set_bgcolor(handle
, 0, 0, 0); /* FIXME - make configurable? */
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
);
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
);
164 mng_retcode
mng_init_callbacks(mng_handle handle
)
169 if ((ret = mng_setcb_##x(handle, fb_mng_##x)) != MNG_NOERROR) \
176 set_cb(getcanvasline
);
178 set_cb(gettickcount
);
180 set_cb(processheader
);
181 #ifdef MNG_SUPPORT_TRACE