/*-
 * Copyright (c) 2001
 * Tatsuya Kudoh(CDR/TK),ROYALPANDA.    All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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 _KSKERNEL_H_
#define _KSKERNEL_H_

/*
** block memory manager
*/

typedef struct KSMemoryBlockManager{
	struct KSMemoryBlockManager *next;
	int block_size;

	void *free_block_list;
}KSMemoryBlockManager;


/*
** message types
*/

#define KS_MSG_NULL		0x00
#define KS_MSG_EXPOSURE		0x01
#define KS_MSG_STRING		0x02
#define KS_MSG_PACKED_STRING	0x03

#define KS_MSG_TEXT		0x80
#define KS_MSG_PACKED_TEXT	0x81


/*
** message structure
*/

typedef struct{
	int type;
	int (*receiver)(void *msg);
	void *call_data;

	void *next;

	unsigned padding[4];
}KSNullMsg;


typedef struct{
	int type;
	int (*receiver)(void *msg);
	void *call_data;

	void *next;

	int x;
	int y;
	int width;
	int height;
}KSExposureMsg;


typedef struct{
	int type;
	int (*receiver)(void *msg);
	void *call_data;

	void *next;

	unsigned char str[8];
}KSPackedStringMsg;


typedef struct{
	int type;
	int (*receiver)(void *msg);
	void *call_data;

	void *next;

	unsigned str[4];
}KSStringMsg;


typedef struct{
	int type;
	int (*receiver)(void *msg);
	void *call_data;

	void *next;

	int len;
	unsigned char *text;
	unsigned padding[2];
}KSPackedTextMsg;


typedef struct{
	int type;
	int (*receiver)(void *msg);
	void *call_data;

	void *next;

	int len;
	unsigned *text;
	unsigned padding[2];
}KSTextMsg;


typedef union KSMsg{
	int type;

	KSNullMsg null;
	KSExposureMsg exposure;
	KSPackedStringMsg packed_string;
	KSStringMsg string;
	KSPackedTextMsg packed_text;
	KSTextMsg text;
}KSMsg;



void KSSetMemoryBlockManager( KSMemoryBlockManager *p, int block_ksize );
void KSCleanMemoryBlockManager( void );
void *KSAllocateMemoryBlock( KSMemoryBlockManager *p );
void *KSFreeMemoryBlock( KSMemoryBlockManager *p, void *block );
void *KSAllocate( int size );
void KSFree( void *p );
void KSInitialize( void );
void KSSetExitFlag( void );
KSMsg *KSAllocateMsg( void );
void KSSendMsg( KSMsg *msg );
void KSMainLoop( void );

#endif
