/*********************************************************************************
source of "nwriter", a basic text-editor for the Nspire; last modified 25.04.11
Copyright (C) 2011 Olivier Thill alias shrear
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*********************************************************************************/
#include
#include "key.h"
#include "store.h"
#include "screen.h"
int main(int argc, char* argv[])
{
//silence compiler about argc
argc = argc ;
//local variables
FILE* file ;
int for_control ;
//allocate screen buffers
char* screen_buffer = (char*)malloc(SCREEN_HEIGHT*SCREEN_WIDTH) ;
//initial clear screen
char screen_color = 0xF ;
memset(screen_buffer, screen_color, SCREEN_HEIGHT*SCREEN_WIDTH) ;
display_buffer(screen_buffer) ;
//create basic path
char path[64] ;
strcpy(path, argv[0]) ;
char* path_tns ;
//create default .txt path
char file_path[64] ;
strcpy(file_path, path) ;
path_tns = strstr(file_path, ".tns") ;
strcpy(path_tns, ".txt.tns") ;
//create default .font path
char font_path[64] ;
strcpy(font_path, path) ;
path_tns = strstr(font_path, ".tns") ;
strcpy(path_tns, ".font.tns") ;
//create font pointers
char* file_temp ;
char* file_meta ;
char* font_description ;
int* font_meta ;
char* font ;
//open font file
file = fopen(font_path, "rb") ;
//run only if file opend succesfull
if ( file != NULL )
{
//load file_meta
file_meta = (char*)malloc(20) ;
fread(file_meta, 1, 20, file) ;
rewind(file) ;
//allocate and read to file_temp
int file_size = file_meta[0]*0x1000000+file_meta[1]*0x10000+file_meta[2]*0x100+file_meta[3] ;
file_temp = (char*)malloc(file_size) ;
fread(file_temp, 1, file_size, file) ;
//close font file
fclose(file) ;
//load font_description
font_description = (char*)malloc(file_meta[4]-20) ;
for ( for_control = 20 ; for_control < file_meta[4] ; for_control++ )
font_description[for_control-20] = file_temp[for_control] ;
//load font_meta
font_meta = (int*)malloc(36) ;
font_meta[0] = file_meta[5] ;
font_meta[1] = file_meta[6] ;
font_meta[2] = file_meta[7] ;
font_meta[3] = file_meta[8]*0x100+file_meta[9] ;
font_meta[4] = file_meta[10]*0x100+file_meta[11] ;
font_meta[5] = file_meta[12]*0x100+file_meta[13] ;
font_meta[6] = file_meta[14] ;
font_meta[7] = file_meta[15]*0x1000000+file_meta[16]*0x10000+file_meta[17]*0x100+file_meta[18] ;
font_meta[8] = file_meta[19] ;
//load font
font = (char*)malloc(font_meta[6]*font_meta[7]) ;
for ( for_control = file_meta[4] ; for_control < font_meta[6]*font_meta[7]+file_meta[4] ; for_control++ )
font[for_control-file_meta[4]] = file_temp[for_control] ;
//free file_temp
free(file_temp) ;
}
//exit program if no font could be loaded
else
return error(1) ;
//screen variables
int ycord_start = 1 ;
int xcord_start = 1 ;
int ycord_end = 1 ;
int xcord_end = 1 ;
//display variables
int line_number = 0x600 ;
int line_start = 0 ;
int line_current = 0 ;
int line_max = 0 ;
int line_page = (SCREEN_HEIGHT-ycord_start-ycord_end)/font_meta[2]-1 ;
//setup index
short* index_line = (short*)malloc(line_number*2) ;
memset(index_line, 0, line_number*2) ;
int index = 0 ;
int index_max = 0 ;
//setup variable for vertical navigation
int offset_x = 0 ;
//allocate memory for store and set to zero
int store_size = 0x4000 ;
char* store = (char*)malloc(store_size) ;
memset(store, 0, store_size) ;
//start of interaction
while ( !isKeyPressed(KEY_NSPIRE_ESC) )
{
//if no input save battery
idle() ;
if ( any_key_pressed() )
{
if ( !isKeyPressed(KEY_NSPIRE_CTRL) )
{
//navigate througth text
if ( key_pressed(KEY_NSPIRE_CLICK) )
index = index_max ;
else if ( key_pressed(KEY_NSPIRE_UP) && line_current )
{
offset_x = get_offset(store, font, font_meta, index_line[line_current], index) ;
index = get_index(store, font, font_meta, offset_x, index_line[line_current-1], index_line[line_current]-1) ;
}
else if ( key_pressed(KEY_NSPIRE_UPRIGHT) )
{
if ( line_current )
{
offset_x = get_offset(store, font, font_meta, index_line[line_current], index) ;
index = get_index(store, font, font_meta, offset_x, index_line[line_current-1], index_line[line_current]-1) ;
}
if ( index != index_max )
index++ ;
}
else if ( key_pressed(KEY_NSPIRE_RIGHT) && index != index_max )
index++ ;
else if ( key_pressed(KEY_NSPIRE_RIGHTDOWN) )
{
if ( line_current != line_max )
{
offset_x = get_offset(store, font, font_meta, index_line[line_current], index) ;
if ( line_current+2 < line_max )
index = get_index(store, font, font_meta, offset_x, index_line[line_current+1], index_line[line_current+2]-1) ;
else
index = get_index(store, font, font_meta, offset_x, index_line[line_current+1], index_max) ;
}
if ( index != index_max )
index++ ;
}
else if ( key_pressed(KEY_NSPIRE_DOWN) && line_current != line_max )
{
offset_x = get_offset(store, font, font_meta, index_line[line_current], index) ;
if ( line_current+2 < line_max )
index = get_index(store, font, font_meta, offset_x, index_line[line_current+1], index_line[line_current+2]-1) ;
else
index = get_index(store, font, font_meta, offset_x, index_line[line_current+1], index_max) ;
}
else if ( key_pressed(KEY_NSPIRE_DOWNLEFT) )
{
if ( line_current != line_max )
{
offset_x = get_offset(store, font, font_meta, index_line[line_current], index) ;
if ( line_current+2 < line_max )
index = get_index(store, font, font_meta, offset_x, index_line[line_current+1], index_line[line_current+2]-1) ;
else
index = get_index(store, font, font_meta, offset_x, index_line[line_current+1], index_max) ;
}
if ( index != 0 )
index-- ;
}
else if ( key_pressed(KEY_NSPIRE_LEFT) && index != 0 )
index-- ;
else if ( key_pressed(KEY_NSPIRE_LEFTUP) )
{
if ( line_current != 0 )
{
offset_x = get_offset(store, font, font_meta, index_line[line_current], index) ;
index = get_index(store, font, font_meta, offset_x, index_line[line_current-1], index_line[line_current]-1) ;
}
if ( index != 0 )
index-- ;
}
//flip screen color
else if ( key_pressed(KEY_NSPIRE_CAT) )
screen_color = 0xF-screen_color ;
//delete last char
else if ( key_pressed(KEY_NSPIRE_DEL) )
{
if ( index != 0 )
{
index-- ;
if ( store[index] != 0 )
store_char(0, store, index) ;
}
}
//save to file
else if ( key_pressed(KEY_NSPIRE_VAR) )
save_file(store, index_max, file_path) ;
//load from file
else if ( key_pressed(KEY_NSPIRE_MENU) )
index = load_file(store, store_size, file_path) ;
}
//key combos
else if ( isKeyPressed(KEY_NSPIRE_CTRL) )
{
//navigate througth text
if ( key_pressed(KEY_NSPIRE_CLICK) )
{
line_current = 0 ;
index = 0 ;
}
else if ( key_pressed(KEY_NSPIRE_LEFT) && index != index_line[line_current] )
index = index_line[line_current] ;
else if ( key_pressed(KEY_NSPIRE_RIGHT) )
{
if ( index_line[line_current+1] != 0 )
{
index = index_line[line_current+1] ;
if ( store[index-1] == 0x0A )
index-- ;
}
else
index = index_max ;
}
else if ( key_pressed(KEY_NSPIRE_UP) && line_start )
line_start-- ;
else if ( key_pressed(KEY_NSPIRE_DOWN) && index_line[line_start+1] )
line_start++ ;
//reset
else if ( key_pressed(KEY_NSPIRE_DEL) )
{
index = 0 ;
memset(store, 0, store_size) ;
}
//configure txt || font file
else if ( key_pressed(KEY_NSPIRE_SHIFT) || key_pressed(KEY_NSPIRE_CAPS) )
while (set_configuration(file_path, font_path, font, font_meta, font_description, screen_buffer, screen_color) )
{
//open font file
file = fopen(font_path, "rb") ;
//run only if file opend succesfull
if ( file != NULL )
{
//load file_meta
file_meta = (char*)malloc(20) ;
fread(file_meta, 1, 20, file) ;
rewind(file) ;
//allocate and read to file_temp
int file_size = file_meta[0]*0x1000000+file_meta[1]*0x10000+file_meta[2]*0x100+file_meta[3] ;
file_temp = (char*)malloc(file_size) ;
fread(file_temp, 1, file_size, file) ;
//close font file
fclose(file) ;
//load font_description
font_description = (char*)malloc(file_meta[4]-20) ;
for ( for_control = 20 ; for_control < file_meta[4] ; for_control++ )
font_description[for_control-20] = file_temp[for_control] ;
//load font_meta
font_meta = (int*)malloc(36) ;
font_meta[0] = file_meta[5] ;
font_meta[1] = file_meta[6] ;
font_meta[2] = file_meta[7] ;
font_meta[3] = file_meta[8]*0x100+file_meta[9] ;
font_meta[4] = file_meta[10]*0x100+file_meta[11] ;
font_meta[5] = file_meta[12]*0x100+file_meta[13] ;
font_meta[6] = file_meta[14] ;
font_meta[7] = file_meta[15]*0x1000000+file_meta[16]*0x10000+file_meta[17]*0x100+file_meta[18] ;
font_meta[8] = file_meta[19] ;
//load font
font = (char*)malloc(font_meta[6]*font_meta[7]) ;
for ( for_control = file_meta[4] ; for_control < font_meta[6]*font_meta[7]+file_meta[4] ; for_control++ )
font[for_control-file_meta[4]] = file_temp[for_control] ;
//free file_temp
free(file_temp) ;
}
else
error(1) ;
}
}
//write characters to store
index = write_key(store, index) ;
//update index_max and index_line
index_max = update_index(store, font, font_meta, xcord_start, xcord_end, index_line, line_number) ;
//check if index_max is smaller than store
if ( index_max > store_size )
error(4) ;
//check for invalid index
if ( index > store_size-1 )
{
index = 0 ;
index_max = 0 ;
}
if ( index < 0 || index > index_max)
{
error(2) ;
index = index_max ;
}
//get the line of index_max
line_max = 0 ;
while ( index_line[line_max+1] )
line_max++ ;
//check current line
while ( index < index_line[line_current] )
line_current-- ;
while ( index >= index_line[line_current+1] && line_current != line_max )
line_current++ ;
while ( line_current < line_start )
line_start-- ;
while ( line_current > line_start+line_page-1 )
line_start++ ;
//update buffer
update_buffer(store, font, font_meta, screen_buffer, screen_color, ycord_start, xcord_start, ycord_end, xcord_end, index_line[line_start], index) ;
//update screen
display_buffer(screen_buffer) ;
}
}
//deallocate screen_buffers
free(screen_buffer) ;
//unload font
free(font) ;
free(font_meta) ;
free(file_meta) ;
free(font_description) ;
//deallocate store and store_save
free(store) ;
//deallocate line index
free(index_line) ;
//the sily return statement of main()
return 0 ;
}