/*
 * header file for some extra function in Linux terminals
 *
 *	-gotoxy(int,int);			usual gotoxy method
						currently maximum 99x99
 *	-clearscr();				clears the screen
 *	-textcolor(COLOR);			changes the text color in terminal
 *						BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
 *	-backgroundcolor(COLOR)			changes the background color in terminal
 *						BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
 *	-defaultcolors();			sets both colors to default (must be used at the end of your program)
 *	-getcharacter();			get PRESSED key value, Enter is not needed
 *
 * author: László Ádám
 * e-mail: adam.laszlo.91@gmail.com
 * 2013. 2. december
 * version: 1.3
*/

#ifndef linuxstuff_h
#define linuxstuff_h

#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#define BLACK 0
#define RED 1
#define GREEN 2
#define YELLOW 3
#define BLUE 4
#define MAGENTA 5
#define CYAN 6
#define WHITE 7

//  l_itoa  //
char l_itoa(char i){
	return i+'0';
}

//  getch  //
int getcharacter(void){							
	struct termios oldt,
	newt;
	int c;
	tcgetattr( STDIN_FILENO, &oldt );
	newt = oldt;
	newt.c_lflag &= ~( ICANON | ECHO );
	tcsetattr( STDIN_FILENO, TCSANOW, &newt );
	c = getchar();
	tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
	return c;
}

//  textcolor  //
// DO NOT FORGET TO USE defaultcolors() AT THE END OF YOUR PROGRAM!
void textcolor(char color){
	if (color == BLACK){
		printf("%s","\033[30m");
	}else if (color == RED){
		printf("%s","\033[31m");
	}else if (color == GREEN){
		printf("%s","\033[32m");
	}else if (color == YELLOW){
		printf("%s","\033[33m");
	}else if (color == BLUE){
		printf("%s","\033[34m");
	}else if (color == MAGENTA){
		printf("%s","\033[35m");
	}else if (color == CYAN){
		printf("%s","\033[36m");
	}else if (color == WHITE){
		printf("%s","\033[37m");
	}
}

//  backgroundcolor  //
// DON'T FORGET TO USE defaultcolors() AT THE END OF YOUR PROGRAM!
void backgroundcolor(char color){
	if (color == BLACK){
		printf("%s","\033[40m");
	}else if (color == RED){
		printf("%s","\033[41m");
	}else if (color == GREEN){
		printf("%s","\033[42m");
	}else if (color == YELLOW){
		printf("%s","\033[43m");
	}else if (color == BLUE){
		printf("%s","\033[44m");
	}else if (color == MAGENTA){
		printf("%s","\033[45m");
	}else if (color == CYAN){
		printf("%s","\033[46m");
	}else if (color == WHITE){
		printf("%s","\033[47m");
	}
}

//  defaultcolors  //
void defaultcolors(){
	printf("%s","\033[0m");
}

//  gotoxy  //
int gotoxy(char x, char y){
	char string[8];
	char tempx[2];
	char tempy[2];
	char ten,one;

	if (x < 1 || x > 99 || y < 1 || y > 99){
		return 1;
	}

	ten = x / 10;
	one = x % 10;	
	tempx[0] = l_itoa(ten);
	tempx[1] = l_itoa(one);
	ten = y / 10;
	one = y % 10;	
	tempy[0] = l_itoa(ten);
	tempy[1] = l_itoa(one);	

	string[0] = '\033';
	string[1] = '[';
	string[2] = tempy[0];
	string[3] = tempy[1];
	string[4] = ';';
	string[5] = tempx[0];
	string[6] = tempx[1];
	string[7] = 'H';
	printf("%s",string);
	
	return 0;	
}			

//  clearscr  //
void clearscr(){
	system("clear");
}

#endif