feat: add given files
Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
commit
7178dbb6a7
151 files changed
+13845
No files matched your search
@@ -0,0 +1,57 @@
|
||||
#
|
||||
# Copyright (c) LSE
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
include ../../config.mk
|
||||
|
||||
LIB = libc.a
|
||||
OBJS = \
|
||||
memchr.o \
|
||||
memcmp.o \
|
||||
memcpy.o \
|
||||
memmove.o \
|
||||
memset.o \
|
||||
printf.o \
|
||||
puts.o \
|
||||
strcasecmp.o \
|
||||
strcat.o \
|
||||
strcmp.o \
|
||||
strcpy.o \
|
||||
strlen.o \
|
||||
strncasecmp.o \
|
||||
strncmp.o \
|
||||
strncpy.o \
|
||||
strnlen.o \
|
||||
|
||||
DEPS = $(OBJS:.o=.d)
|
||||
|
||||
$(OBJS): CPPFLAGS += -MMD -Iinclude -I../../k/include/
|
||||
|
||||
all: $(LIB)
|
||||
|
||||
$(LIB): $(OBJS)
|
||||
$(AR) $(ARFLAGS) $@ $^
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJS) $(DEPS) $(LIB)
|
||||
|
||||
-include $(DEPS)
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef ASSERT_H
|
||||
#define ASSERT_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef NDEBUG
|
||||
#define assert(exp) \
|
||||
do \
|
||||
{ \
|
||||
if (!(exp)) \
|
||||
{ \
|
||||
printf("%s, %d: assertion '%s' failed\n", \
|
||||
__BASE_FILE__, __LINE__, exp); \
|
||||
printf("System halted.\n"); \
|
||||
while (1) \
|
||||
continue; \
|
||||
} \
|
||||
} \
|
||||
while (0)
|
||||
#else
|
||||
#define assert(exp) ((void) 0)
|
||||
#endif
|
||||
|
||||
#endif /* !ASSERT_H */
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef CTYPE_H_
|
||||
#define CTYPE_H_
|
||||
|
||||
static inline int tolower(int c)
|
||||
{
|
||||
if (c >= 'A' && c <= 'Z')
|
||||
return c + ('a' - 'A');
|
||||
return c;
|
||||
}
|
||||
|
||||
static inline int toupper(int c)
|
||||
{
|
||||
if (c >= 'a' && c <= 'z')
|
||||
return c - ('a' - 'A');
|
||||
return c;
|
||||
}
|
||||
|
||||
#endif /* !CTYPE_H_ */
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef LIMITS_H_
|
||||
#define LIMITS_H_
|
||||
|
||||
#if '\0'-1 > 0
|
||||
#define CHAR_MIN 0
|
||||
#define CHAR_MAX 255
|
||||
#else
|
||||
#define CHAR_MIN (-128)
|
||||
#define CHAR_MAX 127
|
||||
#endif
|
||||
|
||||
#define CHAR_BIT 8
|
||||
#define SCHAR_MIN (-128)
|
||||
#define SCHAR_MAX 127
|
||||
#define UCHAR_MAX 255
|
||||
#define SHRT_MIN (-1-0x7fff)
|
||||
#define SHRT_MAX 0x7fff
|
||||
#define USHRT_MAX 0xffff
|
||||
#define INT_MIN (-1-0x7fffffff)
|
||||
#define INT_MAX 0x7fffffff
|
||||
#define UINT_MAX 0xffffffffU
|
||||
#define LONG_MIN (-LONG_MAX-1)
|
||||
#define ULONG_MAX (2UL*LONG_MAX+1)
|
||||
#define LLONG_MIN (-LLONG_MAX-1)
|
||||
#define ULLONG_MAX (2ULL*LLONG_MAX+1)
|
||||
|
||||
#endif /* !LIMITS_H_ */
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef STDIO_H_
|
||||
#define STDIO_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
int puts(const char *s);
|
||||
int printf(const char *format, ...);
|
||||
int sprintf(char *buf, const char *format, ...);
|
||||
int vsprintf(char *buf, const char *format, va_list args);
|
||||
|
||||
#endif /* !STDLIB_H_ */
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef STRING_H_
|
||||
#define STRING_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void *memchr(const void *s, int c, size_t n);
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
void *memcpy(void *dest, const void *src, size_t n);
|
||||
void *memmove(void *dest, const void *src, size_t n);
|
||||
void *memset(void *s, int c, size_t n);
|
||||
int strcasecmp(const char *s1, const char *s2);
|
||||
int strncasecmp(const char *s1, const char *s2, size_t n);
|
||||
char *strcat(char *dest, const char *src);
|
||||
int strcmp(const char *s1, const char *s2);
|
||||
char *strcpy(char *dest, const char *src);
|
||||
char *strdup(const char *s);
|
||||
size_t strlen(const char *s);
|
||||
size_t strnlen(const char *s, size_t maxlen);
|
||||
int strncmp(const char *s1, const char *s2, size_t n);
|
||||
char *strncpy(char *dest, const char *src, size_t n);
|
||||
|
||||
#endif /* !STRING_H_ */
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <stddef.h>
|
||||
|
||||
void *memchr(const void *s, int c, size_t n)
|
||||
{
|
||||
const char *data = s;
|
||||
|
||||
while (n--)
|
||||
if (*data++ == (char)c)
|
||||
return (void *)(data - 1);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <string.h>
|
||||
|
||||
int memcmp(const void *s1, const void *s2, size_t n)
|
||||
{
|
||||
const char *d1 = s1;
|
||||
const char *d2 = s2;
|
||||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
if (d1[i] != d2[i])
|
||||
return d1[i] - d2[i];
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <string.h>
|
||||
|
||||
void *memcpy(void *dest, const void *src, size_t n)
|
||||
{
|
||||
const char *s = src;
|
||||
char *d = dest;
|
||||
|
||||
for (size_t i = 0; i < n; i++)
|
||||
*d++ = *s++;
|
||||
|
||||
return dest;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
void *memmove(void *dest, const void *src, size_t n)
|
||||
{
|
||||
char *d = dest;
|
||||
const char *s = src;
|
||||
|
||||
if (s < d && s + n > d) {
|
||||
while (n-- > 0)
|
||||
d[n - 1] = s[n - 1];
|
||||
return dest;
|
||||
}
|
||||
|
||||
return memcpy(dest, src, n);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <string.h>
|
||||
|
||||
void *memset(void *s, int c, size_t n)
|
||||
{
|
||||
char *p = s;
|
||||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
p[i] = c;
|
||||
|
||||
return s;
|
||||
}
|
||||
@@ -0,0 +1,318 @@
|
||||
/* -*- linux-c -*- ------------------------------------------------------- *
|
||||
*
|
||||
* Copyright (C) 1991, 1992 Linus Torvalds
|
||||
* Copyright 2007 rPath, Inc. - All Rights Reserved
|
||||
*
|
||||
* This file is part of the Linux kernel, and is made available under
|
||||
* the terms of the GNU General Public License version 2.
|
||||
*
|
||||
* ----------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Oh, it's a waste of space, but oh-so-yummy for debugging. This
|
||||
* version of printf() does not include 64-bit support. "Live with
|
||||
* it."
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static inline int isdigit(int c)
|
||||
{
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
static int skip_atoi(const char **s)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while (isdigit(**s))
|
||||
i = i * 10 + *((*s)++) - '0';
|
||||
return i;
|
||||
}
|
||||
|
||||
#define ZEROPAD 1 /* pad with zero */
|
||||
#define SIGN 2 /* unsigned/signed long */
|
||||
#define PLUS 4 /* show plus */
|
||||
#define SPACE 8 /* space if plus */
|
||||
#define LEFT 16 /* left justified */
|
||||
#define SMALL 32 /* Must be 32 == 0x20 */
|
||||
#define SPECIAL 64 /* 0x */
|
||||
|
||||
#define __do_div(n, base) ({ \
|
||||
int __res; \
|
||||
__res = ((unsigned long) n) % (unsigned) base; \
|
||||
n = ((unsigned long) n) / (unsigned) base; \
|
||||
__res; })
|
||||
|
||||
static char *number(char *str, long num, int base, int size, int precision,
|
||||
int type)
|
||||
{
|
||||
/* we are called with base 8, 10 or 16, only, thus don't need "G..." */
|
||||
static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
|
||||
|
||||
char tmp[66];
|
||||
char c, sign, locase;
|
||||
int i;
|
||||
|
||||
/* locase = 0 or 0x20. ORing digits or letters with 'locase'
|
||||
* produces same digits or (maybe lowercased) letters */
|
||||
locase = (type & SMALL);
|
||||
if (type & LEFT)
|
||||
type &= ~ZEROPAD;
|
||||
if (base < 2 || base > 16)
|
||||
return NULL;
|
||||
c = (type & ZEROPAD) ? '0' : ' ';
|
||||
sign = 0;
|
||||
if (type & SIGN) {
|
||||
if (num < 0) {
|
||||
sign = '-';
|
||||
num = -num;
|
||||
size--;
|
||||
} else if (type & PLUS) {
|
||||
sign = '+';
|
||||
size--;
|
||||
} else if (type & SPACE) {
|
||||
sign = ' ';
|
||||
size--;
|
||||
}
|
||||
}
|
||||
if (type & SPECIAL) {
|
||||
if (base == 16)
|
||||
size -= 2;
|
||||
else if (base == 8)
|
||||
size--;
|
||||
}
|
||||
i = 0;
|
||||
if (num == 0)
|
||||
tmp[i++] = '0';
|
||||
else
|
||||
while (num != 0)
|
||||
tmp[i++] = (digits[__do_div(num, base)] | locase);
|
||||
if (i > precision)
|
||||
precision = i;
|
||||
size -= precision;
|
||||
if (!(type & (ZEROPAD + LEFT)))
|
||||
while (size-- > 0)
|
||||
*str++ = ' ';
|
||||
if (sign)
|
||||
*str++ = sign;
|
||||
if (type & SPECIAL) {
|
||||
if (base == 8)
|
||||
*str++ = '0';
|
||||
else if (base == 16) {
|
||||
*str++ = '0';
|
||||
*str++ = ('X' | locase);
|
||||
}
|
||||
}
|
||||
if (!(type & LEFT))
|
||||
while (size-- > 0)
|
||||
*str++ = c;
|
||||
while (i < precision--)
|
||||
*str++ = '0';
|
||||
while (i-- > 0)
|
||||
*str++ = tmp[i];
|
||||
while (size-- > 0)
|
||||
*str++ = ' ';
|
||||
return str;
|
||||
}
|
||||
|
||||
int vsprintf(char *buf, const char *fmt, va_list args)
|
||||
{
|
||||
int len;
|
||||
unsigned long num;
|
||||
int i, base;
|
||||
char *str;
|
||||
const char *s;
|
||||
|
||||
int flags; /* flags to number() */
|
||||
|
||||
int field_width; /* width of output field */
|
||||
int precision; /* min. # of digits for integers; max
|
||||
number of chars for from string */
|
||||
int qualifier; /* 'h', 'l', or 'L' for integer fields */
|
||||
|
||||
for (str = buf; *fmt; ++fmt) {
|
||||
if (*fmt != '%') {
|
||||
*str++ = *fmt;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* process flags */
|
||||
flags = 0;
|
||||
repeat:
|
||||
++fmt; /* this also skips first '%' */
|
||||
switch (*fmt) {
|
||||
case '-':
|
||||
flags |= LEFT;
|
||||
goto repeat;
|
||||
case '+':
|
||||
flags |= PLUS;
|
||||
goto repeat;
|
||||
case ' ':
|
||||
flags |= SPACE;
|
||||
goto repeat;
|
||||
case '#':
|
||||
flags |= SPECIAL;
|
||||
goto repeat;
|
||||
case '0':
|
||||
flags |= ZEROPAD;
|
||||
goto repeat;
|
||||
}
|
||||
|
||||
/* get field width */
|
||||
field_width = -1;
|
||||
if (isdigit(*fmt))
|
||||
field_width = skip_atoi(&fmt);
|
||||
else if (*fmt == '*') {
|
||||
++fmt;
|
||||
/* it's the next argument */
|
||||
field_width = va_arg(args, int);
|
||||
if (field_width < 0) {
|
||||
field_width = -field_width;
|
||||
flags |= LEFT;
|
||||
}
|
||||
}
|
||||
|
||||
/* get the precision */
|
||||
precision = -1;
|
||||
if (*fmt == '.') {
|
||||
++fmt;
|
||||
if (isdigit(*fmt))
|
||||
precision = skip_atoi(&fmt);
|
||||
else if (*fmt == '*') {
|
||||
++fmt;
|
||||
/* it's the next argument */
|
||||
precision = va_arg(args, int);
|
||||
}
|
||||
if (precision < 0)
|
||||
precision = 0;
|
||||
}
|
||||
|
||||
/* get the conversion qualifier */
|
||||
qualifier = -1;
|
||||
if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
|
||||
qualifier = *fmt;
|
||||
++fmt;
|
||||
}
|
||||
|
||||
/* default base */
|
||||
base = 10;
|
||||
|
||||
switch (*fmt) {
|
||||
case 'c':
|
||||
if (!(flags & LEFT))
|
||||
while (--field_width > 0)
|
||||
*str++ = ' ';
|
||||
*str++ = (unsigned char)va_arg(args, int);
|
||||
while (--field_width > 0)
|
||||
*str++ = ' ';
|
||||
continue;
|
||||
|
||||
case 's':
|
||||
s = va_arg(args, char *);
|
||||
len = strnlen(s, precision);
|
||||
|
||||
if (!(flags & LEFT))
|
||||
while (len < field_width--)
|
||||
*str++ = ' ';
|
||||
for (i = 0; i < len; ++i)
|
||||
*str++ = *s++;
|
||||
while (len < field_width--)
|
||||
*str++ = ' ';
|
||||
continue;
|
||||
|
||||
case 'p':
|
||||
if (field_width == -1) {
|
||||
field_width = 2 * sizeof(void *);
|
||||
flags |= ZEROPAD;
|
||||
}
|
||||
str = number(str,
|
||||
(unsigned long)va_arg(args, void *), 16,
|
||||
field_width, precision, flags);
|
||||
continue;
|
||||
|
||||
case 'n':
|
||||
if (qualifier == 'l') {
|
||||
long *ip = va_arg(args, long *);
|
||||
*ip = (str - buf);
|
||||
} else {
|
||||
int *ip = va_arg(args, int *);
|
||||
*ip = (str - buf);
|
||||
}
|
||||
continue;
|
||||
|
||||
case '%':
|
||||
*str++ = '%';
|
||||
continue;
|
||||
|
||||
/* integer number formats - set up the flags and "break" */
|
||||
case 'o':
|
||||
base = 8;
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
flags |= SMALL;
|
||||
/* Falls through. */
|
||||
case 'X':
|
||||
base = 16;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
case 'i':
|
||||
flags |= SIGN;
|
||||
case 'u':
|
||||
break;
|
||||
|
||||
default:
|
||||
*str++ = '%';
|
||||
if (*fmt)
|
||||
*str++ = *fmt;
|
||||
else
|
||||
--fmt;
|
||||
continue;
|
||||
}
|
||||
if (qualifier == 'l')
|
||||
num = va_arg(args, unsigned long);
|
||||
else if (qualifier == 'h') {
|
||||
num = (unsigned short)va_arg(args, int);
|
||||
if (flags & SIGN)
|
||||
num = (short)num;
|
||||
} else if (flags & SIGN)
|
||||
num = va_arg(args, int);
|
||||
else
|
||||
num = va_arg(args, unsigned int);
|
||||
str = number(str, num, base, field_width, precision, flags);
|
||||
}
|
||||
*str = '\0';
|
||||
return str - buf;
|
||||
}
|
||||
|
||||
int sprintf(char *buf, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
int i;
|
||||
|
||||
va_start(args, fmt);
|
||||
i = vsprintf(buf, fmt, args);
|
||||
va_end(args);
|
||||
return i;
|
||||
}
|
||||
|
||||
int printf(const char *fmt, ...)
|
||||
{
|
||||
char printf_buf[1024];
|
||||
va_list args;
|
||||
int printed;
|
||||
|
||||
va_start(args, fmt);
|
||||
printed = vsprintf(printf_buf, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
puts(printf_buf);
|
||||
|
||||
return printed;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <string.h>
|
||||
|
||||
int write(const char *s, size_t nb);
|
||||
|
||||
int puts(const char *s)
|
||||
{
|
||||
return write(s, strlen(s));
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <ctype.h>
|
||||
|
||||
int strcasecmp(const char *s1, const char *s2)
|
||||
{
|
||||
for (; *s1; s1++, s2++)
|
||||
if (tolower(*s1) != tolower(*s2))
|
||||
break;
|
||||
|
||||
return tolower(*s1) - tolower(*s2);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#include <string.h>
|
||||
|
||||
char *strcat(char *dest, const char *src)
|
||||
{
|
||||
return strcpy(dest + strlen(dest), src);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <string.h>
|
||||
|
||||
int strcmp(const char *s1, const char *s2)
|
||||
{
|
||||
for (; *s1 == *s2 && *s1 != '\0'; s1++, s2++)
|
||||
continue;
|
||||
|
||||
return *s1 - *s2;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <string.h>
|
||||
|
||||
char *strcpy(char *dest, const char *src)
|
||||
{
|
||||
char *p = dest;
|
||||
|
||||
while (*src)
|
||||
*p++ = *src++;
|
||||
|
||||
*p = '\0';
|
||||
|
||||
return dest;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
|
||||
size_t strlen(const char *s)
|
||||
{
|
||||
const char *p = s;
|
||||
|
||||
while (*p)
|
||||
p++;
|
||||
|
||||
return (p - s);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stddef.h>
|
||||
#include <ctype.h>
|
||||
|
||||
int strncasecmp(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
for (; *s1 && n > 0; s1++, s2++, n--)
|
||||
if (tolower(*s1) != tolower(*s2))
|
||||
break;
|
||||
|
||||
return n ? tolower(*s1) - tolower(*s2) : 0;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include <string.h>
|
||||
|
||||
int strncmp(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
for (; *s1 == *s2 && *s1 != '\0' && n > 0; s1++, s2++, n--)
|
||||
continue;
|
||||
|
||||
return n ? *s1 - *s2 : 0;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
|
||||
char *strncpy(char *dest, const char *src, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < n && src[i] != '\0'; ++i)
|
||||
dest[i] = src[i];
|
||||
memset(dest + i, '\0', n - i);
|
||||
|
||||
return dest;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <string.h>
|
||||
|
||||
size_t strnlen(const char *s, size_t maxlen)
|
||||
{
|
||||
size_t i = 0;
|
||||
for (; i < maxlen; ++i)
|
||||
if (!s[i])
|
||||
return i;
|
||||
return i;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#
|
||||
# Copyright (c) LSE
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
include ../../config.mk
|
||||
|
||||
LIB = libk.a
|
||||
OBJS = \
|
||||
graphic.o \
|
||||
malloc.o \
|
||||
sound.o \
|
||||
strdup.o \
|
||||
syscalls.o \
|
||||
|
||||
DEPS = $(OBJS:.o=.d)
|
||||
|
||||
$(OBJS): CPPFLAGS += -MMD -Iinclude -I../../k/include/ -I../libc/include/
|
||||
malloc.o: CPPFLAGS += -include malloc-k.h
|
||||
|
||||
all: $(LIB)
|
||||
|
||||
$(LIB): $(OBJS)
|
||||
$(AR) $(ARFLAGS) $@ $^
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJS) $(DEPS) $(LIB)
|
||||
|
||||
-include $(DEPS)
|
||||
@@ -0,0 +1,706 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <graphic.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
* offscreen buffer (for double buffering).
|
||||
*/
|
||||
|
||||
static unsigned char offbuffer[FB_SIZE];
|
||||
|
||||
/*
|
||||
* the font is composed of 8*8 characters.
|
||||
*/
|
||||
|
||||
static unsigned char font[2048] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x7E, 0x81, 0xA5, 0x81, 0xBD, 0x99, 0x81, 0x7E,
|
||||
0x7E, 0xFF, 0xDB, 0xFF, 0xC3, 0xE7, 0xFF, 0x7E,
|
||||
0x6C, 0xFE, 0xFE, 0xFE, 0x7C, 0x38, 0x10, 0x00,
|
||||
0x10, 0x38, 0x7C, 0xFE, 0x7C, 0x38, 0x10, 0x00,
|
||||
0x38, 0x7C, 0x38, 0xFE, 0xFE, 0x92, 0x10, 0x7C,
|
||||
0x00, 0x10, 0x38, 0x7C, 0xFE, 0x7C, 0x38, 0x7C,
|
||||
0x00, 0x00, 0x18, 0x3C, 0x3C, 0x18, 0x00, 0x00,
|
||||
0xFF, 0xFF, 0xE7, 0xC3, 0xC3, 0xE7, 0xFF, 0xFF,
|
||||
0x00, 0x3C, 0x66, 0x42, 0x42, 0x66, 0x3C, 0x00,
|
||||
0xFF, 0xC3, 0x99, 0xBD, 0xBD, 0x99, 0xC3, 0xFF,
|
||||
0x0F, 0x07, 0x0F, 0x7D, 0xCC, 0xCC, 0xCC, 0x78,
|
||||
0x3C, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x7E, 0x18,
|
||||
0x3F, 0x33, 0x3F, 0x30, 0x30, 0x70, 0xF0, 0xE0,
|
||||
0x7F, 0x63, 0x7F, 0x63, 0x63, 0x67, 0xE6, 0xC0,
|
||||
0x99, 0x5A, 0x3C, 0xE7, 0xE7, 0x3C, 0x5A, 0x99,
|
||||
0x80, 0xE0, 0xF8, 0xFE, 0xF8, 0xE0, 0x80, 0x00,
|
||||
0x02, 0x0E, 0x3E, 0xFE, 0x3E, 0x0E, 0x02, 0x00,
|
||||
0x18, 0x3C, 0x7E, 0x18, 0x18, 0x7E, 0x3C, 0x18,
|
||||
0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x00,
|
||||
0x7F, 0xDB, 0xDB, 0x7B, 0x1B, 0x1B, 0x1B, 0x00,
|
||||
0x3E, 0x63, 0x38, 0x6C, 0x6C, 0x38, 0x86, 0xFC,
|
||||
0x00, 0x00, 0x00, 0x00, 0x7E, 0x7E, 0x7E, 0x00,
|
||||
0x18, 0x3C, 0x7E, 0x18, 0x7E, 0x3C, 0x18, 0xFF,
|
||||
0x18, 0x3C, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x00,
|
||||
0x18, 0x18, 0x18, 0x18, 0x7E, 0x3C, 0x18, 0x00,
|
||||
0x00, 0x18, 0x0C, 0xFE, 0x0C, 0x18, 0x00, 0x00,
|
||||
0x00, 0x30, 0x60, 0xFE, 0x60, 0x30, 0x00, 0x00,
|
||||
0x00, 0x00, 0xC0, 0xC0, 0xC0, 0xFE, 0x00, 0x00,
|
||||
0x00, 0x24, 0x66, 0xFF, 0x66, 0x24, 0x00, 0x00,
|
||||
0x00, 0x18, 0x3C, 0x7E, 0xFF, 0xFF, 0x00, 0x00,
|
||||
0x00, 0xFF, 0xFF, 0x7E, 0x3C, 0x18, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00,
|
||||
0x6C, 0x6C, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x6C, 0x6C, 0xFE, 0x6C, 0xFE, 0x6C, 0x6C, 0x00,
|
||||
0x18, 0x7E, 0xC0, 0x7C, 0x06, 0xFC, 0x18, 0x00,
|
||||
0x00, 0xC6, 0xCC, 0x18, 0x30, 0x66, 0xC6, 0x00,
|
||||
0x38, 0x6C, 0x38, 0x76, 0xDC, 0xCC, 0x76, 0x00,
|
||||
0x30, 0x30, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x18, 0x30, 0x60, 0x60, 0x60, 0x30, 0x18, 0x00,
|
||||
0x60, 0x30, 0x18, 0x18, 0x18, 0x30, 0x60, 0x00,
|
||||
0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00,
|
||||
0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30,
|
||||
0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00,
|
||||
0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x80, 0x00,
|
||||
0x7C, 0xCE, 0xDE, 0xF6, 0xE6, 0xC6, 0x7C, 0x00,
|
||||
0x30, 0x70, 0x30, 0x30, 0x30, 0x30, 0xFC, 0x00,
|
||||
0x78, 0xCC, 0x0C, 0x38, 0x60, 0xCC, 0xFC, 0x00,
|
||||
0x78, 0xCC, 0x0C, 0x38, 0x0C, 0xCC, 0x78, 0x00,
|
||||
0x1C, 0x3C, 0x6C, 0xCC, 0xFE, 0x0C, 0x1E, 0x00,
|
||||
0xFC, 0xC0, 0xF8, 0x0C, 0x0C, 0xCC, 0x78, 0x00,
|
||||
0x38, 0x60, 0xC0, 0xF8, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0xFC, 0xCC, 0x0C, 0x18, 0x30, 0x30, 0x30, 0x00,
|
||||
0x78, 0xCC, 0xCC, 0x78, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0x78, 0xCC, 0xCC, 0x7C, 0x0C, 0x18, 0x70, 0x00,
|
||||
0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x00,
|
||||
0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x30,
|
||||
0x18, 0x30, 0x60, 0xC0, 0x60, 0x30, 0x18, 0x00,
|
||||
0x00, 0x00, 0x7E, 0x00, 0x7E, 0x00, 0x00, 0x00,
|
||||
0x60, 0x30, 0x18, 0x0C, 0x18, 0x30, 0x60, 0x00,
|
||||
0x3C, 0x66, 0x0C, 0x18, 0x18, 0x00, 0x18, 0x00,
|
||||
0x7C, 0xC6, 0xDE, 0xDE, 0xDC, 0xC0, 0x7C, 0x00,
|
||||
0x30, 0x78, 0xCC, 0xCC, 0xFC, 0xCC, 0xCC, 0x00,
|
||||
0xFC, 0x66, 0x66, 0x7C, 0x66, 0x66, 0xFC, 0x00,
|
||||
0x3C, 0x66, 0xC0, 0xC0, 0xC0, 0x66, 0x3C, 0x00,
|
||||
0xF8, 0x6C, 0x66, 0x66, 0x66, 0x6C, 0xF8, 0x00,
|
||||
0xFE, 0x62, 0x68, 0x78, 0x68, 0x62, 0xFE, 0x00,
|
||||
0xFE, 0x62, 0x68, 0x78, 0x68, 0x60, 0xF0, 0x00,
|
||||
0x3C, 0x66, 0xC0, 0xC0, 0xCE, 0x66, 0x3A, 0x00,
|
||||
0xCC, 0xCC, 0xCC, 0xFC, 0xCC, 0xCC, 0xCC, 0x00,
|
||||
0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
|
||||
0x1E, 0x0C, 0x0C, 0x0C, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0xE6, 0x66, 0x6C, 0x78, 0x6C, 0x66, 0xE6, 0x00,
|
||||
0xF0, 0x60, 0x60, 0x60, 0x62, 0x66, 0xFE, 0x00,
|
||||
0xC6, 0xEE, 0xFE, 0xFE, 0xD6, 0xC6, 0xC6, 0x00,
|
||||
0xC6, 0xE6, 0xF6, 0xDE, 0xCE, 0xC6, 0xC6, 0x00,
|
||||
0x38, 0x6C, 0xC6, 0xC6, 0xC6, 0x6C, 0x38, 0x00,
|
||||
0xFC, 0x66, 0x66, 0x7C, 0x60, 0x60, 0xF0, 0x00,
|
||||
0x7C, 0xC6, 0xC6, 0xC6, 0xD6, 0x7C, 0x0E, 0x00,
|
||||
0xFC, 0x66, 0x66, 0x7C, 0x6C, 0x66, 0xE6, 0x00,
|
||||
0x7C, 0xC6, 0xE0, 0x78, 0x0E, 0xC6, 0x7C, 0x00,
|
||||
0xFC, 0xB4, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
|
||||
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xFC, 0x00,
|
||||
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x78, 0x30, 0x00,
|
||||
0xC6, 0xC6, 0xC6, 0xC6, 0xD6, 0xFE, 0x6C, 0x00,
|
||||
0xC6, 0xC6, 0x6C, 0x38, 0x6C, 0xC6, 0xC6, 0x00,
|
||||
0xCC, 0xCC, 0xCC, 0x78, 0x30, 0x30, 0x78, 0x00,
|
||||
0xFE, 0xC6, 0x8C, 0x18, 0x32, 0x66, 0xFE, 0x00,
|
||||
0x78, 0x60, 0x60, 0x60, 0x60, 0x60, 0x78, 0x00,
|
||||
0xC0, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x02, 0x00,
|
||||
0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78, 0x00,
|
||||
0x10, 0x38, 0x6C, 0xC6, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
|
||||
0x30, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x78, 0x0C, 0x7C, 0xCC, 0x76, 0x00,
|
||||
0xE0, 0x60, 0x60, 0x7C, 0x66, 0x66, 0xDC, 0x00,
|
||||
0x00, 0x00, 0x78, 0xCC, 0xC0, 0xCC, 0x78, 0x00,
|
||||
0x1C, 0x0C, 0x0C, 0x7C, 0xCC, 0xCC, 0x76, 0x00,
|
||||
0x00, 0x00, 0x78, 0xCC, 0xFC, 0xC0, 0x78, 0x00,
|
||||
0x38, 0x6C, 0x64, 0xF0, 0x60, 0x60, 0xF0, 0x00,
|
||||
0x00, 0x00, 0x76, 0xCC, 0xCC, 0x7C, 0x0C, 0xF8,
|
||||
0xE0, 0x60, 0x6C, 0x76, 0x66, 0x66, 0xE6, 0x00,
|
||||
0x30, 0x00, 0x70, 0x30, 0x30, 0x30, 0x78, 0x00,
|
||||
0x0C, 0x00, 0x1C, 0x0C, 0x0C, 0xCC, 0xCC, 0x78,
|
||||
0xE0, 0x60, 0x66, 0x6C, 0x78, 0x6C, 0xE6, 0x00,
|
||||
0x70, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
|
||||
0x00, 0x00, 0xCC, 0xFE, 0xFE, 0xD6, 0xD6, 0x00,
|
||||
0x00, 0x00, 0xB8, 0xCC, 0xCC, 0xCC, 0xCC, 0x00,
|
||||
0x00, 0x00, 0x78, 0xCC, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0x00, 0x00, 0xDC, 0x66, 0x66, 0x7C, 0x60, 0xF0,
|
||||
0x00, 0x00, 0x76, 0xCC, 0xCC, 0x7C, 0x0C, 0x1E,
|
||||
0x00, 0x00, 0xDC, 0x76, 0x62, 0x60, 0xF0, 0x00,
|
||||
0x00, 0x00, 0x7C, 0xC0, 0x70, 0x1C, 0xF8, 0x00,
|
||||
0x10, 0x30, 0xFC, 0x30, 0x30, 0x34, 0x18, 0x00,
|
||||
0x00, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0x76, 0x00,
|
||||
0x00, 0x00, 0xCC, 0xCC, 0xCC, 0x78, 0x30, 0x00,
|
||||
0x00, 0x00, 0xC6, 0xC6, 0xD6, 0xFE, 0x6C, 0x00,
|
||||
0x00, 0x00, 0xC6, 0x6C, 0x38, 0x6C, 0xC6, 0x00,
|
||||
0x00, 0x00, 0xCC, 0xCC, 0xCC, 0x7C, 0x0C, 0xF8,
|
||||
0x00, 0x00, 0xFC, 0x98, 0x30, 0x64, 0xFC, 0x00,
|
||||
0x1C, 0x30, 0x30, 0xE0, 0x30, 0x30, 0x1C, 0x00,
|
||||
0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00,
|
||||
0xE0, 0x30, 0x30, 0x1C, 0x30, 0x30, 0xE0, 0x00,
|
||||
0x76, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x10, 0x38, 0x6C, 0xC6, 0xC6, 0xFE, 0x00,
|
||||
0x7C, 0xC6, 0xC0, 0xC6, 0x7C, 0x0C, 0x06, 0x7C,
|
||||
0x00, 0xCC, 0x00, 0xCC, 0xCC, 0xCC, 0x76, 0x00,
|
||||
0x1C, 0x00, 0x78, 0xCC, 0xFC, 0xC0, 0x78, 0x00,
|
||||
0x7E, 0x81, 0x3C, 0x06, 0x3E, 0x66, 0x3B, 0x00,
|
||||
0xCC, 0x00, 0x78, 0x0C, 0x7C, 0xCC, 0x76, 0x00,
|
||||
0xE0, 0x00, 0x78, 0x0C, 0x7C, 0xCC, 0x76, 0x00,
|
||||
0x30, 0x30, 0x78, 0x0C, 0x7C, 0xCC, 0x76, 0x00,
|
||||
0x00, 0x00, 0x7C, 0xC6, 0xC0, 0x78, 0x0C, 0x38,
|
||||
0x7E, 0x81, 0x3C, 0x66, 0x7E, 0x60, 0x3C, 0x00,
|
||||
0xCC, 0x00, 0x78, 0xCC, 0xFC, 0xC0, 0x78, 0x00,
|
||||
0xE0, 0x00, 0x78, 0xCC, 0xFC, 0xC0, 0x78, 0x00,
|
||||
0xCC, 0x00, 0x70, 0x30, 0x30, 0x30, 0x78, 0x00,
|
||||
0x7C, 0x82, 0x38, 0x18, 0x18, 0x18, 0x3C, 0x00,
|
||||
0xE0, 0x00, 0x70, 0x30, 0x30, 0x30, 0x78, 0x00,
|
||||
0xC6, 0x10, 0x7C, 0xC6, 0xFE, 0xC6, 0xC6, 0x00,
|
||||
0x30, 0x30, 0x00, 0x78, 0xCC, 0xFC, 0xCC, 0x00,
|
||||
0x1C, 0x00, 0xFC, 0x60, 0x78, 0x60, 0xFC, 0x00,
|
||||
0x00, 0x00, 0x7F, 0x0C, 0x7F, 0xCC, 0x7F, 0x00,
|
||||
0x3E, 0x6C, 0xCC, 0xFE, 0xCC, 0xCC, 0xCE, 0x00,
|
||||
0x78, 0x84, 0x00, 0x78, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0x00, 0xCC, 0x00, 0x78, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0x00, 0xE0, 0x00, 0x78, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0x78, 0x84, 0x00, 0xCC, 0xCC, 0xCC, 0x76, 0x00,
|
||||
0x00, 0xE0, 0x00, 0xCC, 0xCC, 0xCC, 0x76, 0x00,
|
||||
0x00, 0xCC, 0x00, 0xCC, 0xCC, 0x7C, 0x0C, 0xF8,
|
||||
0xC3, 0x18, 0x3C, 0x66, 0x66, 0x3C, 0x18, 0x00,
|
||||
0xCC, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0x18, 0x18, 0x7E, 0xC0, 0xC0, 0x7E, 0x18, 0x18,
|
||||
0x38, 0x6C, 0x64, 0xF0, 0x60, 0xE6, 0xFC, 0x00,
|
||||
0xCC, 0xCC, 0x78, 0x30, 0xFC, 0x30, 0xFC, 0x30,
|
||||
0xF8, 0xCC, 0xCC, 0xFA, 0xC6, 0xCF, 0xC6, 0xC3,
|
||||
0x0E, 0x1B, 0x18, 0x3C, 0x18, 0x18, 0xD8, 0x70,
|
||||
0x1C, 0x00, 0x78, 0x0C, 0x7C, 0xCC, 0x76, 0x00,
|
||||
0x38, 0x00, 0x70, 0x30, 0x30, 0x30, 0x78, 0x00,
|
||||
0x00, 0x1C, 0x00, 0x78, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0x00, 0x1C, 0x00, 0xCC, 0xCC, 0xCC, 0x76, 0x00,
|
||||
0x00, 0xF8, 0x00, 0xB8, 0xCC, 0xCC, 0xCC, 0x00,
|
||||
0xFC, 0x00, 0xCC, 0xEC, 0xFC, 0xDC, 0xCC, 0x00,
|
||||
0x3C, 0x6C, 0x6C, 0x3E, 0x00, 0x7E, 0x00, 0x00,
|
||||
0x38, 0x6C, 0x6C, 0x38, 0x00, 0x7C, 0x00, 0x00,
|
||||
0x18, 0x00, 0x18, 0x18, 0x30, 0x66, 0x3C, 0x00,
|
||||
0x00, 0x00, 0x00, 0xFC, 0xC0, 0xC0, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0xFC, 0x0C, 0x0C, 0x00, 0x00,
|
||||
0xC6, 0xCC, 0xD8, 0x36, 0x6B, 0xC2, 0x84, 0x0F,
|
||||
0xC3, 0xC6, 0xCC, 0xDB, 0x37, 0x6D, 0xCF, 0x03,
|
||||
0x18, 0x00, 0x18, 0x18, 0x3C, 0x3C, 0x18, 0x00,
|
||||
0x00, 0x33, 0x66, 0xCC, 0x66, 0x33, 0x00, 0x00,
|
||||
0x00, 0xCC, 0x66, 0x33, 0x66, 0xCC, 0x00, 0x00,
|
||||
0x22, 0x88, 0x22, 0x88, 0x22, 0x88, 0x22, 0x88,
|
||||
0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
|
||||
0xDB, 0xF6, 0xDB, 0x6F, 0xDB, 0x7E, 0xD7, 0xED,
|
||||
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
|
||||
0x18, 0x18, 0x18, 0x18, 0xF8, 0x18, 0x18, 0x18,
|
||||
0x18, 0x18, 0xF8, 0x18, 0xF8, 0x18, 0x18, 0x18,
|
||||
0x36, 0x36, 0x36, 0x36, 0xF6, 0x36, 0x36, 0x36,
|
||||
0x00, 0x00, 0x00, 0x00, 0xFE, 0x36, 0x36, 0x36,
|
||||
0x00, 0x00, 0xF8, 0x18, 0xF8, 0x18, 0x18, 0x18,
|
||||
0x36, 0x36, 0xF6, 0x06, 0xF6, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x00, 0x00, 0xFE, 0x06, 0xF6, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0xF6, 0x06, 0xFE, 0x00, 0x00, 0x00,
|
||||
0x36, 0x36, 0x36, 0x36, 0xFE, 0x00, 0x00, 0x00,
|
||||
0x18, 0x18, 0xF8, 0x18, 0xF8, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0xF8, 0x18, 0x18, 0x18,
|
||||
0x18, 0x18, 0x18, 0x18, 0x1F, 0x00, 0x00, 0x00,
|
||||
0x18, 0x18, 0x18, 0x18, 0xFF, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0xFF, 0x18, 0x18, 0x18,
|
||||
0x18, 0x18, 0x18, 0x18, 0x1F, 0x18, 0x18, 0x18,
|
||||
0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
|
||||
0x18, 0x18, 0x18, 0x18, 0xFF, 0x18, 0x18, 0x18,
|
||||
0x18, 0x18, 0x1F, 0x18, 0x1F, 0x18, 0x18, 0x18,
|
||||
0x36, 0x36, 0x36, 0x36, 0x37, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x37, 0x30, 0x3F, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x3F, 0x30, 0x37, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0xF7, 0x00, 0xFF, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0xFF, 0x00, 0xF7, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x37, 0x30, 0x37, 0x36, 0x36, 0x36,
|
||||
0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00,
|
||||
0x36, 0x36, 0xF7, 0x00, 0xF7, 0x36, 0x36, 0x36,
|
||||
0x18, 0x18, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00,
|
||||
0x36, 0x36, 0x36, 0x36, 0xFF, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0xFF, 0x00, 0xFF, 0x18, 0x18, 0x18,
|
||||
0x00, 0x00, 0x00, 0x00, 0xFF, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x3F, 0x00, 0x00, 0x00,
|
||||
0x18, 0x18, 0x1F, 0x18, 0x1F, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x1F, 0x18, 0x1F, 0x18, 0x18, 0x18,
|
||||
0x00, 0x00, 0x00, 0x00, 0x3F, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0xFF, 0x36, 0x36, 0x36,
|
||||
0x18, 0x18, 0xFF, 0x18, 0xFF, 0x18, 0x18, 0x18,
|
||||
0x18, 0x18, 0x18, 0x18, 0xF8, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x1F, 0x18, 0x18, 0x18,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x76, 0xDC, 0xC8, 0xDC, 0x76, 0x00,
|
||||
0x00, 0x78, 0xCC, 0xF8, 0xCC, 0xF8, 0xC0, 0xC0,
|
||||
0x00, 0xFC, 0xCC, 0xC0, 0xC0, 0xC0, 0xC0, 0x00,
|
||||
0x00, 0x00, 0xFE, 0x6C, 0x6C, 0x6C, 0x6C, 0x00,
|
||||
0xFC, 0xCC, 0x60, 0x30, 0x60, 0xCC, 0xFC, 0x00,
|
||||
0x00, 0x00, 0x7E, 0xD8, 0xD8, 0xD8, 0x70, 0x00,
|
||||
0x00, 0x66, 0x66, 0x66, 0x66, 0x7C, 0x60, 0xC0,
|
||||
0x00, 0x76, 0xDC, 0x18, 0x18, 0x18, 0x18, 0x00,
|
||||
0xFC, 0x30, 0x78, 0xCC, 0xCC, 0x78, 0x30, 0xFC,
|
||||
0x38, 0x6C, 0xC6, 0xFE, 0xC6, 0x6C, 0x38, 0x00,
|
||||
0x38, 0x6C, 0xC6, 0xC6, 0x6C, 0x6C, 0xEE, 0x00,
|
||||
0x1C, 0x30, 0x18, 0x7C, 0xCC, 0xCC, 0x78, 0x00,
|
||||
0x00, 0x00, 0x7E, 0xDB, 0xDB, 0x7E, 0x00, 0x00,
|
||||
0x06, 0x0C, 0x7E, 0xDB, 0xDB, 0x7E, 0x60, 0xC0,
|
||||
0x38, 0x60, 0xC0, 0xF8, 0xC0, 0x60, 0x38, 0x00,
|
||||
0x78, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x00,
|
||||
0x00, 0x7E, 0x00, 0x7E, 0x00, 0x7E, 0x00, 0x00,
|
||||
0x18, 0x18, 0x7E, 0x18, 0x18, 0x00, 0x7E, 0x00,
|
||||
0x60, 0x30, 0x18, 0x30, 0x60, 0x00, 0xFC, 0x00,
|
||||
0x18, 0x30, 0x60, 0x30, 0x18, 0x00, 0xFC, 0x00,
|
||||
0x0E, 0x1B, 0x1B, 0x18, 0x18, 0x18, 0x18, 0x18,
|
||||
0x18, 0x18, 0x18, 0x18, 0x18, 0xD8, 0xD8, 0x70,
|
||||
0x18, 0x18, 0x00, 0x7E, 0x00, 0x18, 0x18, 0x00,
|
||||
0x00, 0x76, 0xDC, 0x00, 0x76, 0xDC, 0x00, 0x00,
|
||||
0x38, 0x6C, 0x6C, 0x38, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
|
||||
0x0F, 0x0C, 0x0C, 0x0C, 0xEC, 0x6C, 0x3C, 0x1C,
|
||||
0x58, 0x6C, 0x6C, 0x6C, 0x6C, 0x00, 0x00, 0x00,
|
||||
0x70, 0x98, 0x30, 0x60, 0xF8, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x3C, 0x3C, 0x3C, 0x3C, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
void draw_begin(void)
|
||||
{
|
||||
draw_clear(CONS_BLACK);
|
||||
}
|
||||
|
||||
void draw_end(void)
|
||||
{
|
||||
swap_frontbuffer(offbuffer);
|
||||
}
|
||||
|
||||
void draw_clear(color_t color)
|
||||
{
|
||||
memset(offbuffer, color, FB_SIZE);
|
||||
}
|
||||
|
||||
void draw_pixel(unsigned int x, unsigned int y, color_t color)
|
||||
{
|
||||
if (x >= GRAPHIC_WIDTH)
|
||||
return;
|
||||
if (y >= GRAPHIC_HEIGHT)
|
||||
return;
|
||||
|
||||
offbuffer[y * GRAPHIC_WIDTH + x] = color;
|
||||
}
|
||||
|
||||
void draw_line(unsigned int x1, unsigned int y1,
|
||||
unsigned int x2, unsigned int y2, color_t color)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
if (x1 >= GRAPHIC_WIDTH)
|
||||
return;
|
||||
if (y1 >= GRAPHIC_HEIGHT)
|
||||
return;
|
||||
if (x2 >= GRAPHIC_WIDTH)
|
||||
return;
|
||||
if (y2 >= GRAPHIC_HEIGHT)
|
||||
return;
|
||||
|
||||
if (x1 == x2) {
|
||||
for (i = y1; i < y2; i++)
|
||||
draw_pixel(x1, i, color);
|
||||
} else if (y1 == y2) {
|
||||
for (i = x1; i < x2; i++)
|
||||
draw_pixel(i, y1, color);
|
||||
} else {
|
||||
int s;
|
||||
int steep = abs(y2 - y1) > abs(x2 - x1);
|
||||
if (steep) {
|
||||
s = x1;
|
||||
x1 = y1;
|
||||
y1 = s;
|
||||
s = x2;
|
||||
x2 = y2;
|
||||
y2 = s;
|
||||
}
|
||||
if (x1 > x2) {
|
||||
s = x1;
|
||||
x1 = x2;
|
||||
x2 = s;
|
||||
s = y2;
|
||||
y2 = y1;
|
||||
y1 = s;
|
||||
}
|
||||
int deltax = x2 - x1;
|
||||
int deltay = abs(y2 - y1);
|
||||
int error = -deltax / 2;
|
||||
int ystep;
|
||||
int y = y1;
|
||||
|
||||
if (y1 < y2)
|
||||
ystep = 1;
|
||||
else
|
||||
ystep = -1;
|
||||
unsigned int x;
|
||||
for (x = x1; x < x2; x++) {
|
||||
if (steep)
|
||||
draw_pixel(y, x, color);
|
||||
else
|
||||
draw_pixel(x, y, color);
|
||||
|
||||
error = error + deltay;
|
||||
|
||||
if (error > 0) {
|
||||
y = y + ystep;
|
||||
error = error - deltax;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_rect(unsigned int x1, unsigned int y1,
|
||||
unsigned int x2, unsigned int y2, color_t color)
|
||||
{
|
||||
unsigned int x;
|
||||
unsigned int y;
|
||||
|
||||
for (x = x1; x < x2 && x < GRAPHIC_WIDTH; x++) {
|
||||
draw_pixel(x, y1, color);
|
||||
draw_pixel(x, y2, color);
|
||||
}
|
||||
for (y = y1; y < y2 && y < GRAPHIC_HEIGHT; y++) {
|
||||
draw_pixel(x1, y, color);
|
||||
draw_pixel(x2, y, color);
|
||||
}
|
||||
}
|
||||
|
||||
void draw_fillrect(unsigned int x1, unsigned int y1,
|
||||
unsigned int x2, unsigned int y2,
|
||||
color_t color, color_t interior)
|
||||
{
|
||||
unsigned int x;
|
||||
unsigned int y;
|
||||
|
||||
for (x = x1; x < x2 && x < GRAPHIC_WIDTH; x++) {
|
||||
draw_pixel(x, y1, color);
|
||||
draw_pixel(x, y2, color);
|
||||
}
|
||||
for (y = y1; y <= y2 && y < GRAPHIC_HEIGHT; y++) {
|
||||
draw_pixel(x1, y, color);
|
||||
draw_pixel(x2, y, color);
|
||||
}
|
||||
for (x = x1 + 1; x < x2 && x < GRAPHIC_WIDTH; x++)
|
||||
for (y = y1 + 1; y < y2 && y < GRAPHIC_HEIGHT; y++)
|
||||
draw_pixel(x, y, interior);
|
||||
}
|
||||
|
||||
/*
|
||||
* Windows BMP file header.
|
||||
*/
|
||||
|
||||
struct bitmap_header {
|
||||
char signature[2];
|
||||
unsigned long filesize;
|
||||
unsigned long reserved1;
|
||||
unsigned long offset;
|
||||
unsigned long reserved2;
|
||||
unsigned long width;
|
||||
unsigned long height;
|
||||
unsigned short planes;
|
||||
unsigned short bpp;
|
||||
unsigned long reserved3;
|
||||
unsigned long size;
|
||||
char reserved[16];
|
||||
} __attribute__ ((packed));
|
||||
|
||||
struct image *load_image(const char *path)
|
||||
{
|
||||
struct bitmap_header bmp;
|
||||
|
||||
int fd = open(path, 0);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
int rc = read(fd, &bmp, sizeof(bmp));
|
||||
if (rc < (int)sizeof(bmp)) {
|
||||
goto err_img;
|
||||
}
|
||||
|
||||
if (!(bmp.signature[0] == 'B' && bmp.signature[1] == 'M')) {
|
||||
goto err_img;
|
||||
}
|
||||
|
||||
struct image *img = malloc(sizeof(struct image));
|
||||
if (!img) {
|
||||
goto err_img;
|
||||
}
|
||||
|
||||
img->width = bmp.width;
|
||||
img->height = bmp.height;
|
||||
|
||||
img->data = calloc(img->height, sizeof(*img->data));
|
||||
if (!img->data)
|
||||
goto err_buf;
|
||||
|
||||
for (unsigned int i = 0; i < img->height; i++) {
|
||||
img->data[i] = calloc(img->width, sizeof(*img->data[i]));
|
||||
if (!img->data[i])
|
||||
goto err;
|
||||
}
|
||||
|
||||
int ppl = (bmp.size - (img->width * img->height)) / img->height;
|
||||
|
||||
if (lseek(fd, bmp.offset, SEEK_SET) == (off_t) - 1) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < img->height; i++) {
|
||||
rc = read(fd, img->data[i], img->width);
|
||||
if (rc < (int)img->width)
|
||||
goto err;
|
||||
|
||||
rc = lseek(fd, ppl, SEEK_CUR);
|
||||
|
||||
if (rc == (off_t)-1)
|
||||
goto err;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return img;
|
||||
|
||||
err:
|
||||
for (unsigned int i = 0; i < img->height; i++)
|
||||
free(img->data[i]);
|
||||
free(img->data);
|
||||
err_buf:
|
||||
free(img);
|
||||
err_img:
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void clear_image( struct image * image)
|
||||
{
|
||||
for (unsigned int i = 0; i < image->height; i++)
|
||||
free(image->data[i]);
|
||||
free(image->data);
|
||||
free(image);
|
||||
}
|
||||
|
||||
void draw_image_alpha(struct image *image, unsigned int x, unsigned int y, unsigned int alpha)
|
||||
{
|
||||
for (unsigned int i = 0; i < image->height; i++)
|
||||
for (unsigned int j = 0; j < image->width; j++) {
|
||||
if ((alpha == (unsigned int)-1) || (alpha != image->data[i][j]))
|
||||
draw_pixel(x + j, y + image->height - i, image->data[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
void draw_image(struct image *image, unsigned int x, unsigned int y)
|
||||
{
|
||||
draw_image_alpha(image, x, y, -1);
|
||||
}
|
||||
|
||||
/*
|
||||
* used to decompose a byte
|
||||
*/
|
||||
|
||||
static int bit_on(char c, int n)
|
||||
{
|
||||
int mask;
|
||||
|
||||
mask = 1 << (7 - n);
|
||||
return c & mask;
|
||||
}
|
||||
|
||||
void draw_text(const char *s, unsigned int x, unsigned int y, color_t fg, color_t bg)
|
||||
{
|
||||
char c;
|
||||
char ch;
|
||||
char p;
|
||||
unsigned int pos;
|
||||
unsigned int strp = 0;
|
||||
|
||||
for (; *s; s++, strp++) {
|
||||
c = *s;
|
||||
|
||||
for (unsigned int i = 0; i < 8; ++i) {
|
||||
for (unsigned int j = 0; j < 8; ++j) {
|
||||
ch = font[c * 8 + i];
|
||||
p = bit_on(ch, j) ? fg : bg;
|
||||
|
||||
pos = ((y + i) * GRAPHIC_WIDTH) + (strp * 8 + x) + j;
|
||||
if (!((bg == (unsigned int)-1) && (p == -1)))
|
||||
offbuffer[pos] = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct anim *load_anim(char *paths, int delay)
|
||||
{
|
||||
char *p, *bck;
|
||||
char *filename;
|
||||
struct anim *anim = NULL;
|
||||
int i;
|
||||
|
||||
if (!paths || !*paths)
|
||||
return NULL;
|
||||
|
||||
if (!(anim = malloc(sizeof(struct anim))))
|
||||
return NULL;
|
||||
|
||||
anim->nr_img = 1;
|
||||
anim->current_img = 0;
|
||||
anim->delay = delay;
|
||||
anim->jiffies = 0;
|
||||
|
||||
for (p = paths; *p; p++)
|
||||
if (*p == ' ')
|
||||
anim->nr_img++;
|
||||
|
||||
if (!(anim->imgs = calloc(anim->nr_img, sizeof(struct image *)))) {
|
||||
free(anim);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!(p = strdup(paths))) {
|
||||
free(anim);
|
||||
return NULL;
|
||||
}
|
||||
bck = p;
|
||||
|
||||
for (i = 0; i < anim->nr_img; i++) {
|
||||
filename = p;
|
||||
|
||||
while (*p && *p != ' ')
|
||||
p++;
|
||||
|
||||
*p = '\0';
|
||||
p++;
|
||||
|
||||
if (!(anim->imgs[i] = load_image(filename)))
|
||||
blue_screen("failed to load animation");
|
||||
}
|
||||
|
||||
free(bck);
|
||||
return anim;
|
||||
}
|
||||
|
||||
void draw_anim(struct anim * anim, int x, int y, unsigned long jiffies)
|
||||
{
|
||||
if (anim->jiffies + anim->delay <= jiffies || anim->jiffies > jiffies) {
|
||||
anim->jiffies = jiffies;
|
||||
anim->current_img = (anim->current_img + 1) % anim->nr_img;
|
||||
}
|
||||
|
||||
draw_image_alpha(anim->imgs[anim->current_img], x, y, 0);
|
||||
}
|
||||
|
||||
static void blue_screen_cons(const char *message)
|
||||
{
|
||||
char seq[] = {
|
||||
CONS_ESCAPE, CONS_COLOR,
|
||||
CONS_BACK(CONS_BLUE) | CONS_FRONT(CONS_WHITE) | CONS_LIGHT,
|
||||
CONS_ESCAPE, CONS_CLEAR
|
||||
};
|
||||
char fatal[] = {
|
||||
CONS_ESCAPE, CONS_SETX, 32,
|
||||
CONS_ESCAPE, CONS_SETY, 10,
|
||||
CONS_ESCAPE, CONS_COLOR,
|
||||
CONS_BACK(CONS_WHITE) | CONS_FRONT(CONS_BLUE)
|
||||
};
|
||||
char msg[] = {
|
||||
CONS_ESCAPE, CONS_SETX, 0,
|
||||
CONS_ESCAPE, CONS_SETY, 13,
|
||||
CONS_ESCAPE, CONS_COLOR,
|
||||
CONS_BACK(CONS_BLUE) | CONS_FRONT(CONS_WHITE) | CONS_LIGHT
|
||||
};
|
||||
char *chiche1 = "If this is not the first time you encounter";
|
||||
char *chiche2 = "this problem, please contact chiche@epita.fr";
|
||||
|
||||
write(seq, sizeof(seq) / sizeof(char));
|
||||
|
||||
write(fatal, sizeof(fatal) / sizeof(char));
|
||||
printf("K -- FATAL ERROR", fatal);
|
||||
|
||||
msg[2] = 40 - strlen(message) / 2;
|
||||
write(msg, sizeof(msg) / sizeof(char));
|
||||
printf("%s", message);
|
||||
msg[2] = 40 - strlen(chiche1) / 2;
|
||||
msg[5] += 2;
|
||||
write(msg, sizeof(msg) / sizeof(char));
|
||||
printf("%s", chiche1);
|
||||
msg[2] = 40 - strlen(chiche2) / 2;
|
||||
msg[5]++;
|
||||
write(msg, sizeof(msg) / sizeof(char));
|
||||
printf("%s", chiche2);
|
||||
|
||||
while (1)
|
||||
continue;
|
||||
}
|
||||
|
||||
static void blue_screen_fb(const char *message)
|
||||
{
|
||||
draw_begin();
|
||||
draw_clear(CONS_BLUE);
|
||||
|
||||
draw_text("K -- FATAL ERROR", GRAPHIC_WIDTH / 2 - 8 * 8, 60, CONS_BLUE,
|
||||
CONS_WHITE);
|
||||
|
||||
draw_text(message, GRAPHIC_WIDTH / 2 - (strlen(message) / 2) * 8, 90,
|
||||
CONS_WHITE, CONS_BLUE);
|
||||
|
||||
draw_text("If this problem repeats,", GRAPHIC_WIDTH / 2 - 12 * 8, 120,
|
||||
CONS_WHITE, CONS_BLUE);
|
||||
draw_text("Please contact chiche@epita.fr", GRAPHIC_WIDTH / 2 - 15 * 8,
|
||||
130, CONS_WHITE, CONS_BLUE);
|
||||
|
||||
draw_end();
|
||||
|
||||
while (1)
|
||||
continue;
|
||||
}
|
||||
|
||||
void (*blue_screen)(const char *message) = blue_screen_cons;
|
||||
|
||||
void switch_graphic(void)
|
||||
{
|
||||
if (setvideo(VIDEO_GRAPHIC))
|
||||
blue_screen("Unable to switch to graphic mode");
|
||||
blue_screen = blue_screen_fb;
|
||||
}
|
||||
|
||||
void switch_text(void)
|
||||
{
|
||||
if (setvideo(VIDEO_TEXT))
|
||||
blue_screen("Unable to switch to text mode");
|
||||
blue_screen = blue_screen_cons;
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef GRAPHIC_H_
|
||||
#define GRAPHIC_H_
|
||||
|
||||
#include <kstd.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* this file should really be cleaned :) */
|
||||
|
||||
/*
|
||||
* some characteristics about the display.
|
||||
*/
|
||||
#define GRAPHIC_WIDTH 320
|
||||
#define GRAPHIC_HEIGHT 200
|
||||
#define FB_SIZE (GRAPHIC_WIDTH * GRAPHIC_HEIGHT)
|
||||
#define PALETTE_SIZE 256
|
||||
|
||||
/*
|
||||
* image structure. used load_image, clear_image and draw_image to
|
||||
* manipulate images.
|
||||
*/
|
||||
|
||||
struct image {
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
unsigned char **data;
|
||||
};
|
||||
|
||||
/*
|
||||
* animation structure.
|
||||
*
|
||||
*/
|
||||
|
||||
struct anim {
|
||||
int nr_img;
|
||||
int current_img;
|
||||
unsigned long delay;
|
||||
unsigned long jiffies;
|
||||
struct image **imgs;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* a color is an index in the palette.
|
||||
*/
|
||||
typedef unsigned int color_t;
|
||||
|
||||
/*
|
||||
* some colors.
|
||||
*/
|
||||
enum colors {
|
||||
BLACK = 0,
|
||||
WHITE = 255,
|
||||
RED = 249,
|
||||
GREEN = 250,
|
||||
YELLOW = 251,
|
||||
BLUE = 252,
|
||||
PURPLE = 253,
|
||||
AQUA = 254,
|
||||
ORANGE = 23
|
||||
};
|
||||
|
||||
/*
|
||||
* this function switches to graphic mode.
|
||||
*/
|
||||
void switch_graphic(void);
|
||||
|
||||
/*
|
||||
* this function get back to text mode.
|
||||
*/
|
||||
void switch_text(void);
|
||||
|
||||
/*
|
||||
* this fumction changes the color palette of the VGA.
|
||||
*/
|
||||
void set_palette(unsigned int *new_palette, size_t size);
|
||||
|
||||
/*
|
||||
* call this function at the beginning of drawing a frame.
|
||||
*/
|
||||
void draw_begin(void);
|
||||
|
||||
/*
|
||||
* call this function when finished drawing. this is the function that
|
||||
* copy your buffered draw from off-screen buffer to framebuffer.
|
||||
*/
|
||||
void draw_end(void);
|
||||
|
||||
/*
|
||||
* clears the screen with given color.
|
||||
*/
|
||||
void draw_clear(color_t color);
|
||||
|
||||
/*
|
||||
* this function plot a pixel of given color at given position.
|
||||
*/
|
||||
void draw_pixel(unsigned int x, unsigned int y, color_t color);
|
||||
|
||||
/*
|
||||
* draw a line.
|
||||
*/
|
||||
void draw_line(unsigned int x1, unsigned int y1,
|
||||
unsigned int x2, unsigned int y2, color_t color);
|
||||
|
||||
/*
|
||||
* draw an empty rectangle.
|
||||
*/
|
||||
void draw_rect(unsigned int x1, unsigned int y1,
|
||||
unsigned int x2, unsigned int y2, color_t color);
|
||||
|
||||
/*
|
||||
* draw a solid rectangle.
|
||||
*/
|
||||
void draw_fillrect(unsigned int x1, unsigned int y1,
|
||||
unsigned int x2, unsigned int y2,
|
||||
color_t color, color_t interior);
|
||||
|
||||
/*
|
||||
* load a Windows BITMAP (BMP) from file.
|
||||
* the only supported files are 8 bits per pixels paletted.
|
||||
* the only supported palette is the default one (obtained with Paint).
|
||||
*/
|
||||
struct image *load_image(const char *path);
|
||||
|
||||
/*
|
||||
* destroy a loaded image.
|
||||
*/
|
||||
void clear_image(struct image *image);
|
||||
|
||||
/*
|
||||
* display a loaded image with transparency.
|
||||
*/
|
||||
void draw_image_alpha(struct image *image,
|
||||
unsigned int x, unsigned int y, unsigned int alpha);
|
||||
|
||||
/*
|
||||
* display a loaded image.
|
||||
*/
|
||||
void draw_image(struct image *image, unsigned int x, unsigned int y);
|
||||
|
||||
/*
|
||||
* draw some text.
|
||||
*/
|
||||
void draw_text(const char *s,
|
||||
unsigned int x, unsigned int y, color_t fg, color_t bg);
|
||||
|
||||
/*
|
||||
* load an animation.
|
||||
* paths is string containing the images name separated by a space.
|
||||
* load_anim supports the same image formats as load_image.
|
||||
* delay is the displaying time of each image (in ticks).
|
||||
*
|
||||
* invocation example: load_anim("pic1 pic2 pic3 pic4 pic5", PIC_ANIM_DELAY);
|
||||
*/
|
||||
struct anim *load_anim(char *paths, int delay);
|
||||
|
||||
/*
|
||||
* draw an animation at coordinates (x, y)
|
||||
*
|
||||
* jiffies is the reference ticks counter which should
|
||||
* be incremented at every timer tick.
|
||||
*/
|
||||
void draw_anim(struct anim * anim, int x, int y, unsigned long jiffies);
|
||||
|
||||
/*
|
||||
* video blue screen.
|
||||
*/
|
||||
extern void (*blue_screen)(const char *message);
|
||||
|
||||
#endif /* !GRAPHIC_H_ */
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef KSTD_H
|
||||
#define KSTD_H
|
||||
|
||||
#include <k/kstd.h>
|
||||
|
||||
int write(const void *s, size_t length);
|
||||
void *sbrk(ssize_t increment);
|
||||
int getkey(void);
|
||||
unsigned long gettick(void);
|
||||
int open(const char *pathname, int flags);
|
||||
ssize_t read(int fd, void *buf, size_t count);
|
||||
off_t lseek(int filedes, off_t offset, int whence);
|
||||
int close(int fd);
|
||||
int setvideo(int mode);
|
||||
void swap_frontbuffer(const void *buffer);
|
||||
int playsound(struct melody *melody, int repeat);
|
||||
int getmouse(int *x, int *y, int *buttons);
|
||||
int getkeymode(int mode);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef SOUND_H
|
||||
#define SOUND_H
|
||||
|
||||
#include <kstd.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
struct melody *load_sound(const char *path);
|
||||
|
||||
void clear_sound(struct melody *melody);
|
||||
|
||||
#endif /* !SOUND_H_ */
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef STDLIB_H_
|
||||
#define STDLIB_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void *malloc(size_t size);
|
||||
|
||||
void *realloc(void *ptr, size_t size);
|
||||
|
||||
void free(void *ptr);
|
||||
|
||||
void *calloc(size_t nmemb, size_t size);
|
||||
|
||||
static inline int abs(int j)
|
||||
{
|
||||
return j > 0 ? j : -j;
|
||||
}
|
||||
|
||||
#endif /* !STDLIB_H_ */
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <k/kstd.h>
|
||||
#include <k/types.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define HAVE_MMAP 0
|
||||
#define USE_LOCKS 0
|
||||
#define NO_MALLOC_STATS 1
|
||||
|
||||
#define LACKS_TIME_H
|
||||
#define LACKS_UNISTD_H
|
||||
#define LACKS_STDLIB_H
|
||||
#define LACKS_ERRNO_H
|
||||
#define LACKS_SYS_TYPES_H
|
||||
|
||||
#define malloc_getpagesize 4096
|
||||
|
||||
#define MALLOC_FAILURE_ACTION \
|
||||
do { \
|
||||
printf("[!] malloc internal error \n"); \
|
||||
} while(0)
|
||||
|
||||
|
||||
#define USAGE_ERROR_ACTION(m, p) \
|
||||
do { \
|
||||
printf("[!] malloc: corrupted chunk: 0x%x\n", p); \
|
||||
} while (0)
|
||||
|
||||
#define abort() \
|
||||
do { \
|
||||
printf("[!!!] malloc: abort\n"); \
|
||||
for (;;) \
|
||||
continue; \
|
||||
} while(0)
|
||||
+6280
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <sound.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct melody *load_sound(const char *path)
|
||||
{
|
||||
struct melody *melody = NULL;
|
||||
int fd = -1;
|
||||
int nb = -1;
|
||||
int i = -1;
|
||||
char *magic = ".KSF";
|
||||
char buf[5];
|
||||
|
||||
if ((fd = open(path, O_RDONLY)) < 0)
|
||||
return NULL;
|
||||
|
||||
buf[4] = 0;
|
||||
/* check the magic number */
|
||||
if ((read(fd, buf, 4) != 4) || (strcmp(magic, buf) != 0)) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* read the numbers of tones */
|
||||
if (read(fd, &nb, sizeof(int)) != sizeof(int)) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* allocate space to store the new melody */
|
||||
if (!(melody = malloc((nb + 1) * sizeof(struct melody)))) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* load the melody */
|
||||
for (i = 0; i < nb; i++) {
|
||||
if (read(fd, &melody[i].freq, sizeof(int)) != sizeof(int) ||
|
||||
read(fd, &melody[i].duration, sizeof(int)) != sizeof(int)) {
|
||||
close(fd);
|
||||
free(melody);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* put a null tones to indicate end of melody */
|
||||
melody[nb].freq = 0;
|
||||
melody[nb].duration = (unsigned long)-1;
|
||||
|
||||
close(fd);
|
||||
|
||||
return (melody);
|
||||
}
|
||||
|
||||
void clear_sound(struct melody *melody)
|
||||
{
|
||||
free(melody);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *strdup(const char *s)
|
||||
{
|
||||
char *r = NULL;
|
||||
char *p = NULL;
|
||||
|
||||
r = malloc(strlen(s) + 1);
|
||||
if (!r)
|
||||
return NULL;
|
||||
|
||||
for (p = r; *s != '\0'; s++, p++)
|
||||
*p = *s;
|
||||
|
||||
*p = '\0';
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright (c) LSE
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY LSE AS IS AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL LSE BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <kstd.h>
|
||||
#include <stddef.h>
|
||||
|
||||
static inline u32 syscall0(int syscall_nb)
|
||||
{
|
||||
u32 res;
|
||||
|
||||
asm volatile ("int $0x80" : "=a"(res) : "a"(syscall_nb));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline u32 syscall1(int syscall_nb, u32 ebx)
|
||||
{
|
||||
u32 res;
|
||||
|
||||
asm volatile ("int $0x80" : "=a"(res) : "a"(syscall_nb), "b"(ebx));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline u32 syscall2(int syscall_nb, u32 ebx, u32 ecx)
|
||||
{
|
||||
u32 res;
|
||||
|
||||
asm volatile ("int $0x80" : "=a"(res) : "a"(syscall_nb), "b"(ebx), "c"(ecx));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline u32 syscall3(int syscall_nb, u32 ebx, u32 ecx, u32 edx)
|
||||
{
|
||||
u32 res;
|
||||
|
||||
asm volatile ("int $0x80" : "=a"(res) : "a"(syscall_nb), "b"(ebx), "c"(ecx), "d"(edx));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int write(const void *s, size_t length)
|
||||
{
|
||||
return ((int)syscall2(SYSCALL_WRITE, (u32)s, length));
|
||||
}
|
||||
|
||||
void *sbrk(ssize_t increment)
|
||||
{
|
||||
return ((void *)syscall1(SYSCALL_SBRK, increment));
|
||||
}
|
||||
|
||||
int getkey(void)
|
||||
{
|
||||
return ((int)syscall0(SYSCALL_GETKEY));
|
||||
}
|
||||
|
||||
unsigned long gettick(void)
|
||||
{
|
||||
return ((unsigned long)syscall0(SYSCALL_GETTICK));
|
||||
}
|
||||
|
||||
int open(const char *pathname, int flags)
|
||||
{
|
||||
return ((int)syscall2(SYSCALL_OPEN, (u32)pathname, flags));
|
||||
}
|
||||
|
||||
ssize_t read(int fd, void *buf, size_t count)
|
||||
{
|
||||
return ((ssize_t)syscall3(SYSCALL_READ, fd, (u32)buf, count));
|
||||
}
|
||||
|
||||
off_t lseek(int filedes, off_t offset, int whence)
|
||||
{
|
||||
return ((off_t)syscall3(SYSCALL_SEEK, filedes, offset, whence));
|
||||
}
|
||||
|
||||
int close(int fd)
|
||||
{
|
||||
return ((int)syscall1(SYSCALL_CLOSE, fd));
|
||||
}
|
||||
|
||||
int playsound(struct melody *melody, int repeat)
|
||||
{
|
||||
return ((int)syscall2(SYSCALL_PLAYSOUND, (u32)melody, repeat));
|
||||
}
|
||||
|
||||
int setvideo(int mode)
|
||||
{
|
||||
return ((int)syscall1(SYSCALL_SETVIDEO, mode));
|
||||
}
|
||||
|
||||
void swap_frontbuffer(const void *buffer)
|
||||
{
|
||||
syscall1(SYSCALL_SWAP_FRONTBUFFER, (u32)buffer);
|
||||
}
|
||||
|
||||
int getmouse(int *x, int *y, int *buttons)
|
||||
{
|
||||
return ((int)syscall3(SYSCALL_GETMOUSE, (u32)x, (u32)y, (u32)buttons));
|
||||
}
|
||||
|
||||
|
||||
void set_palette(unsigned int *new_palette, size_t size)
|
||||
{
|
||||
syscall2(SYSCALL_SETPALETTE, (u32)new_palette, size);
|
||||
}
|
||||