/*
 *  psp.h
 *  PSP Library for Mac OS X
 *
 *  Created by XArt on 15/03/2007.
 *  Copyright 2007 XArt. All rights reserved.
 *
 *  PSP Library for Mac PowerPC (Big-Endian) Tested by XArt
 *  PSP Library for Mac intel (Little-Endian) Tested by Bronx
 */

#ifndef _PSP_H_
#define _PSP_H_

#include <SDL/SDL.h>

#define PSP_CTRL_TRIANGLE   0x001000 
#define PSP_CTRL_CIRCLE     0x002000 
#define PSP_CTRL_CROSS      0x004000
#define PSP_CTRL_SQUARE     0x008000 
#define PSP_CTRL_UP         0x000010 
#define PSP_CTRL_DOWN       0x000040 
#define PSP_CTRL_LEFT       0x000080 
#define PSP_CTRL_RIGHT      0x000020 
#define PSP_CTRL_LTRIGGER   0x000100 
#define PSP_CTRL_RTRIGGER   0x000200
#define PSP_CTRL_SELECT     0x000001
#define PSP_CTRL_SCREEN     0x000002
#define PSP_CTRL_START      0x000008
#define PSP_CTRL_HOME       0x010000
#define PSP_CTRL_HOLD       0x020000
#define PSP_CTRL_NOTE       0x800000

/* Returned controller data */
typedef struct SceCtrlData {
	/** The current read frame. */
	unsigned int 	TimeStamp;
	/** Bit mask containing zero or more of ::PspCtrlButtons. */
	unsigned int 	Buttons;
	/** Analogue stick, X axis. */
	unsigned char 	Lx;
	/** Analogue stick, Y axis. */
	unsigned char 	Ly;
	/** Reserved. */
	unsigned char 	Rsrv[6];
} SceCtrlData;

SDL_Event event;

void * memalign (size_t boundary, size_t size)
{
   return (void *)malloc((sizeof(char)*size)+boundary);
}

int stricmp(const char *dest, const char *src)
{
	char *s1 = (char *)dest;
   char *s2 = (char *)src;
   int i=0;
   
   if (strlen(src) > strlen(dest)) return 1;
   if (strlen(src) < strlen(dest)) return -1;
   while ((*s1 !=0) && (*s2 != 0))
   {
      i = ( ((*s2>='A') && (*s2<='Z'))?*s1+32:*s2 ) - ( ((*s1>='A') && (*s1<='Z'))?*s1+32:*s1 );
      if (i!=0) return (i<0)?-1:1;
      s1++;
      s2++;
   }
   return 0;
}

int sceCtrlReadBufferPositive(SceCtrlData *pad_data, int count)
{
   
   static unsigned int buttons=0;
   
   if ( !SDL_PollEvent(&event) ) {
      switch (event.type) {
         case SDL_KEYDOWN:
            switch ( event.key.keysym.sym ) {
               case SDLK_x:
                  buttons|=PSP_CTRL_CROSS;
                  break;
                  
               case SDLK_o:
                  buttons|=PSP_CTRL_CIRCLE;
                  break;
                  
               case SDLK_s:
                  buttons|=PSP_CTRL_SQUARE;
                  break;
                  
               case SDLK_t:
                  buttons|=PSP_CTRL_TRIANGLE;
                  break;
                  
               case SDLK_LEFT:
                  buttons|=PSP_CTRL_LEFT;
                  pad_data->Lx=0;
                  break;
                  
               case SDLK_RIGHT:
                  buttons|=PSP_CTRL_RIGHT;
                  pad_data->Lx=255;
                  break;
                  
               case SDLK_UP:
                  buttons|=PSP_CTRL_UP;
                  pad_data->Ly=0;
                  break;
                  
               case SDLK_DOWN:
                  buttons|=PSP_CTRL_DOWN;
                  pad_data->Ly=255;
                  break;
                  
               case SDLK_LSHIFT:
                  buttons|=PSP_CTRL_LTRIGGER;
                  break;
                  
               case SDLK_RSHIFT:
                  buttons|=PSP_CTRL_RTRIGGER;
                  break;
                  
               case SDLK_SPACE:
                  buttons|=PSP_CTRL_SELECT;
                  break;
                  
               case SDLK_RETURN:
                  buttons|=PSP_CTRL_START;
                  break;
                  
               case SDLK_ESCAPE:
                  buttons|=PSP_CTRL_HOME;
                  break;
                  
               case SDLK_p:
                  buttons|=PSP_CTRL_HOLD;
                  break;
                  
               case SDLK_n:
                  buttons|=PSP_CTRL_NOTE;
                  break;
                  
               default:
                  break;
            }
            break;
            
         case SDL_KEYUP:
            switch ( event.key.keysym.sym ) {
               case SDLK_x:
                  buttons&=~PSP_CTRL_CROSS;
                  break;
                  
               case SDLK_o:
                  buttons&=~PSP_CTRL_CIRCLE;
                  break;
                  
               case SDLK_s:
                  buttons&=~PSP_CTRL_SQUARE;
                  break;
                  
               case SDLK_t:
                  buttons&=~PSP_CTRL_TRIANGLE;
                  break;
                  
               case SDLK_LEFT:
                  buttons&=~PSP_CTRL_LEFT;
                  pad_data->Lx=127;
                  break;
                  
               case SDLK_RIGHT:
                  buttons&=~PSP_CTRL_RIGHT;
                  pad_data->Lx=127;
                  break;
                  
               case SDLK_UP:
                  buttons&=~PSP_CTRL_UP;
                  pad_data->Ly=127;
                  break;
                  
               case SDLK_DOWN:
                  buttons&=~PSP_CTRL_DOWN;
                  pad_data->Ly=127;
                  break;
                  
               case SDLK_LSHIFT:
                  buttons&=~PSP_CTRL_LTRIGGER;
                  break;
                  
               case SDLK_RSHIFT:
                  buttons&=~PSP_CTRL_RTRIGGER;
                  break;
                  
               case SDLK_SPACE:
                  buttons&=~PSP_CTRL_SELECT;
                  break;
                  
               case SDLK_RETURN:
                  buttons&=~PSP_CTRL_START;
                  break;
                  
               case SDLK_ESCAPE:
                  buttons&=~PSP_CTRL_HOME;
                  break;
                  
               case SDLK_p:
                  buttons&=~PSP_CTRL_HOLD;
                  break;
                  
               case SDLK_n:
                  buttons&=~PSP_CTRL_NOTE;
                  break;
                  
               default:
                  pad_data->Lx=127;
                  pad_data->Ly=127;
                  break;
            }
            break;
            
         case SDL_QUIT:
            buttons|=PSP_CTRL_HOME;
            break;
            
         default:
            break;
      }
   }
   pad_data->Buttons=buttons;  
   return 0;
   
}

void sceKernelExitGame(void)
{
   /* Clean up the SDL library */
   SDL_Quit();
}

SDL_Surface *surface;
char *_vram; // Virtual PSP VRAM for Mac OS X

void sceGuInit(void)
{
   _vram=new char[0x110000];
}

void pspDebugScreenInit(void)
{
   
}

void sceDisplaySetFrameBuf(void *topaddr, int bufferwidth, int pixelformat, int sync)
{
   int bpp = surface->format->BytesPerPixel;
   /* Here p is the address to the pixel we want to set */ 
   Uint8 *p = (Uint8 *)surface->pixels + 0 * surface->pitch + 0 * bpp;
   Uint8 *vptr=(Uint8 *)topaddr;
   int x,y;
   
   int littleEndian = 1;
   littleEndian=(1 == *(char *)&littleEndian)?1:0;
   
   if ( SDL_MUSTLOCK(surface) ) SDL_LockSurface(surface);
   
   switch ( pixelformat ) {
      case 3: // 3=RGBA 88888
         for(y=0; y<272; y++) {
            for(x=0; x<480; x++) {
               if (littleEndian) {
                  *p++=*(vptr+2); // Alpha
                  *p++=*(vptr+1); // Red
                  *p++=*(vptr); // Green
                  *p++=*(vptr+3); // Blue
               } else {
                  *p++=*(vptr); // Alpha
                  *p++=*(vptr+3); // Red
                  *p++=*(vptr+2); // Green
                  *p++=*(vptr+1); // Blue
               }
               
               vptr+=4;
            }
            vptr+=128;
         }
         break;
         
      default:
         break;
   }
   
   if ( SDL_MUSTLOCK(surface) ) SDL_UnlockSurface(surface);
   SDL_Flip(surface);
}

int sceDisplaySetMode(int mode, int width, int height)
{
   /* Initialize the SDL library */
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) return -1;
   
	/* Set width * height video mode */
   surface=SDL_SetVideoMode(width,height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); // SDL_FULLSCREEN
   if (surface == NULL) return -1;
   
   return 0;
}

void sceDisplayWaitVblankStart(void)
{
   return;
}
#endif
