diff --git a/Makefile b/Makefile index 0d1a263..e22f5ef 100755 --- a/Makefile +++ b/Makefile @@ -46,8 +46,8 @@ $(TARGET).gba.nds: $(TARGET).nds cat ndsloader.bin $(TARGET).nds > $(TARGET).gba.nds #--------------------------------------------------------------------------------- -$(TARGET).nds : libdsmi libntxm tobkit $(TARGET).arm7 $(TARGET).arm9 - ndstool -c $(TARGET).nds -7 $(TARGET).arm7 -9 $(TARGET).arm9 -b icon.bmp "NitroTracker" +$(TARGET).nds : libdsmi libntxm tobkit arm7/$(TARGET).elf arm9/$(TARGET).elf + ndstool -c $(TARGET).nds -7 arm7/$(TARGET).arm7.elf -9 arm9/$(TARGET).arm9.elf -b icon.bmp "NitroTracker" #--------------------------------------------------------------------------------- $(TARGET).arm7 : arm7/$(TARGET).elf @@ -65,7 +65,7 @@ arm9/$(TARGET).elf: clean: $(MAKE) -C arm9 clean $(MAKE) -C arm7 clean - rm -f $(TARGET).ds.gba $(TARGET).nds $(TARGET).arm7 $(TARGET).arm9 + rm -f $(TARGET).ds.gba $(TARGET).nds # Custom targets for copying stuff to the DS -include mytargets.mk diff --git a/arm7/Makefile b/arm7/Makefile index 38e1966..62d056b 100644 --- a/arm7/Makefile +++ b/arm7/Makefile @@ -58,7 +58,6 @@ LIBDIRS := $(LIBNDS) $(LIBNTXM) ifneq ($(BUILD),$(notdir $(CURDIR))) #--------------------------------------------------------------------------------- -export ARM7BIN := $(TOPDIR)/$(TARGET).arm7 export ARM7ELF := $(CURDIR)/$(TARGET).arm7.elf export DEPSDIR := $(CURDIR)/$(BUILD) @@ -113,11 +112,6 @@ DEPENDS := $(OFILES:.o=.d) #--------------------------------------------------------------------------------- # main targets #--------------------------------------------------------------------------------- -$(ARM7BIN) : $(ARM7ELF) - @$(OBJCOPY) -O binary $< $@ - @echo built ... $(notdir $@) - - $(ARM7ELF) : $(OFILES) $(LIBNTXM) @echo linking $(notdir $@) @$(LD) $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@ diff --git a/arm7/source/arm7_main.cpp b/arm7/source/arm7_main.cpp index c9ecee6..dbcbf70 100644 --- a/arm7/source/arm7_main.cpp +++ b/arm7/source/arm7_main.cpp @@ -28,7 +28,6 @@ extern "C" { #include "ntxm/linear_freq_table.h" - #include "ntxm/tobmic.h" } #define WIFI @@ -37,30 +36,16 @@ extern "C" { #include #endif -#define LID_BIT BIT(7) - #define abs(x) ((x)>=0?(x):-(x)) NTXM7 *ntxm7 = 0; -bool lidwasdown = false; +static volatile bool exitflag = false; extern bool ntxm_recording; int vcount; touchPosition first,tempPos; -// Thanks to LiraNuna for this cool function -void PM_SetRegister(int reg, int control) -{ - SerialWaitBusy(); - REG_SPICNT = SPI_ENABLE | SPI_DEVICE_POWER |SPI_BAUD_1MHz | SPI_CONTINUOUS; - REG_SPIDATA = reg; - SerialWaitBusy(); - REG_SPICNT = SPI_ENABLE | SPI_DEVICE_POWER |SPI_BAUD_1MHz; - REG_SPIDATA = control; -} - - //--------------------------------------------------------------------------------- void VcountHandler() { //--------------------------------------------------------------------------------- @@ -73,18 +58,6 @@ void VcountHandler() { void VblankHandler(void) { - if((!lidwasdown)&&(REG_KEYXY & LID_BIT)) - { - PM_SetRegister(0, 0x30); - lidwasdown = true; - } - - if((lidwasdown)&&(!(REG_KEYXY & LID_BIT))) - { - PM_SetRegister(0, 0x0D); - lidwasdown = false; - } - #ifdef WIFI if(ntxm_recording == false) Wifi_Update(); // update wireless in vblank @@ -105,6 +78,10 @@ void enableSound() { REG_MASTER_VOLUME = 127; } +void powerButtonHandler(void) { + exitflag = true; +} + //--------------------------------------------------------------------------------- int main(int argc, char ** argv) { //--------------------------------------------------------------------------------- @@ -112,6 +89,7 @@ int main(int argc, char ** argv) { rtcReset(); // Reset the clock if needed irqInit(); fifoInit(); + touchInit(); #ifdef WIFI installWifiFIFO(); #endif @@ -124,9 +102,6 @@ int main(int argc, char ** argv) { irqSet(IRQ_VCOUNT, VcountHandler); irqEnable(IRQ_VCOUNT); - irqSet(IRQ_TIMER1, tob_ProcessMicrophoneTimerIRQ); - irqEnable(IRQ_TIMER1); - #ifdef WIFI irqEnable(IRQ_NETWORK); #endif @@ -140,8 +115,12 @@ int main(int argc, char ** argv) { irqSet(IRQ_TIMER0, ntxmTimerHandler); irqEnable(IRQ_TIMER0); + setPowerButtonCB(powerButtonHandler); + // Keep the ARM7 out of main RAM - while (1) { + while (!exitflag) { swiWaitForVBlank(); } + + return 0; } diff --git a/arm9/Makefile b/arm9/Makefile index 3edec92..d65c458 100644 --- a/arm9/Makefile +++ b/arm9/Makefile @@ -46,7 +46,7 @@ CXXFLAGS := $(CFLAGS) -fno-rtti ASFLAGS := -g $(ARCH) -march=armv5te -mtune=arm946e-s -LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -mno-fpu -Wl,-Map,$(notdir $*.map) +LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project @@ -66,7 +66,6 @@ LIBDIRS := $(LIBDSMI) $(LIBNTXM) $(TOPDIR)/tobkit $(LIBNDS) ifneq ($(BUILD),$(notdir $(CURDIR))) #--------------------------------------------------------------------------------- -export ARM9BIN := $(TOPDIR)/$(TARGET).arm9 export ARM9ELF := $(CURDIR)/$(TARGET).arm9.elf export DEPSDIR := $(CURDIR)/$(BUILD) @@ -122,10 +121,6 @@ DEPENDS := $(OFILES:.o=.d) #--------------------------------------------------------------------------------- # main targets #--------------------------------------------------------------------------------- -$(ARM9BIN) : $(ARM9ELF) - @$(OBJCOPY) -O binary $< $@ - @echo built ... $(notdir $@) - $(ARM9ELF) : $(OFILES) @echo linking $(notdir $@) $(LD) $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@ diff --git a/arm9/source/main.cpp b/arm9/source/main.cpp index f88656d..b91113a 100644 --- a/arm9/source/main.cpp +++ b/arm9/source/main.cpp @@ -124,6 +124,7 @@ u8 active_buffer = FRONT_BUFFER; u16 *main_vram_front, *main_vram_back, *sub_vram; bool typewriter_active = false; +bool exit_requested = false; u16 keys_that_are_repeated = KEY_UP | KEY_DOWN | KEY_LEFT | KEY_RIGHT; u16 repeatkeys = 0, repeatkeys_last = 0; @@ -258,8 +259,8 @@ void clearMainScreen(void) { u16 col = settings->getTheme()->col_dark_bg; u32 colcol = col | col << 16; - swiFastCopy(&colcol, main_vram_front, 192*256/2 | COPY_MODE_FILL); - swiFastCopy(&colcol, main_vram_back, 192*256/2 | COPY_MODE_FILL); + dmaFillWords(colcol, main_vram_front, 256 * 192 * 2); + dmaFillWords(colcol, main_vram_back, 256 * 192 * 2); } void clearSubScreen(void) @@ -267,12 +268,13 @@ void clearSubScreen(void) u16 col = settings->getTheme()->col_dark_bg; u32 colcol = col | col << 16; // Fill the bg with the bg color except for the place where the keyboard is - swiFastCopy(&colcol, sub_vram, 153*256/2 | COPY_MODE_FILL); + dmaFillWords(colcol, sub_vram, 256 * 153 * 2); for(int y=154;y<192;++y) { - for(int x=0;x<224;++x) + int x = 0; + for(;x<224;++x) sub_vram[256*y+x] = 0; - for(int x=224;x<256;++x) + for(;x<256;++x) sub_vram[256*y+x] = colcol; } } @@ -294,8 +296,8 @@ void drawSampleNumbers(void) for(u8 key=0; key<24; ++key) { note = state->basenote + key; - sample_id = inst->getNoteSample(note); - sprintf(&label, "%x", sample_id); + sample_id = inst->getNoteSample(note) & 0x0F; + label = (sample_id >= 0xA) ? (sample_id - 0xA + 'a') : (sample_id + '0'); kb->setKeyLabel(key, label); } @@ -352,7 +354,8 @@ void handleNoteStroke(u8 note) } char label; - sprintf(&label, "%x", state->sample); + u8 sample_id = state->sample & 0xF; + label = (sample_id >= 0xA) ? (sample_id - 0xA + 'a') : (sample_id + '0'); kb->setKeyLabel(note, label); } @@ -393,14 +396,13 @@ void updateSampleList(Instrument *inst) else { Sample *sample; - char *str=(char*)malloc(255); + char *str=(char*) calloc(1, SAMPLE_NAME_LENGTH + 1); for(u8 i=0; igetSample(i); if(sample != NULL) { - strcpy(str, sample->getName()); + strncpy(str, sample->getName(), SAMPLE_NAME_LENGTH); lbsamples->set(i, str); } else { lbsamples->set(i, ""); @@ -504,16 +506,16 @@ void handleInstChange(u16 newinst) void updateLabelSongLen(void) { - char *labelstr = (char*)malloc(12); - sprintf(labelstr, "songlen:%2d", song->getPotLength()); - //labelsonglen->setCaption(labelstr); - free(labelstr); + /* char *labelstr = (char*)malloc(12); + snprintf(labelstr, 12, "songlen:%2d", song->getPotLength()); + labelsonglen->setCaption(labelstr); + free(labelstr); */ } void updateLabelChannels(void) { - char *labelstr = (char*)malloc(8); - sprintf(labelstr, "chn: %2d", song->getChannels()); + char *labelstr = (char*)malloc(9); + snprintf(labelstr, 9, "chn: %2d", song->getChannels()); labelchannels->setCaption(labelstr); free(labelstr); } @@ -527,6 +529,7 @@ void updateTempoAndBpm(void) void setSong(Song *newsong) { song = newsong; + char *str = (char*) calloc(1, 256); CommandSetSong(song); @@ -540,24 +543,19 @@ void setSong(Song *newsong) // Update POT lbpot->clear(); u8 potentry; - char *str=(char*)malloc(3); - str[2]=0; for(u8 i=0;igetPotLength();++i) { potentry = song->getPotEntry(i); - sprintf(str, "%2x", potentry); + snprintf(str, 255, "%2x", potentry); lbpot->add(str); } - free(str); // Update instrument list Instrument *inst; - str=(char*)malloc(255); for(u8 i=0;igetInstrument(i); if(inst!=NULL) { - strcpy(str, inst->getName()); + strncpy(str, inst->getName(), 255); lbinstruments->set(i, str); } else { lbinstruments->set(i, ""); @@ -597,8 +595,7 @@ void setSong(Song *newsong) lbsamples->select(0); - memset(str, 0, 255); - strcpy(str, song->getName()); + strncpy(str, song->getName(), 255); labelsongname->setCaption(str); free(str); @@ -859,9 +856,8 @@ void deleteTypewriter(void) void handleTypewriterFilenameOk(void) { - char *text = tw->getText(); - char *name = 0; + char *name = NULL; iprintf("%s\n", text); if(strcmp(text,"") != 0) { @@ -898,6 +894,7 @@ void handleTypewriterFilenameOk(void) } } deleteTypewriter(); + if (name != NULL) free(name); } @@ -992,9 +989,9 @@ void drawMainScreen(void) void redrawSubScreen(void) { // Fill screen - for(u16 i=0;i<256*153;++i) { - sub_vram[i] = RGB15(4,6,15)|BIT(15); - } + u16 col = RGB15(4,6,15)|BIT(15); + u32 colcol = col | col << 16; + dmaFillWords(colcol, sub_vram, 256 * 153 * 2); // Redraw GUI gui->drawSubScreen(); @@ -1507,18 +1504,18 @@ void handleFileChange(File file) { if(!file.is_dir) { - char *str = (char*)malloc(256); - strncpy(str, file.name.c_str(), 256); + char *str = (char*)malloc(file.name.length() + 1); + strcpy(str, file.name.c_str()); lowercase(str); labelFilename->setCaption(str); if(rbsong->getActive() == true) { - strcpy(state->song_filename, str); + strncpy(state->song_filename, str, STATE_FILENAME_LEN); } else if(rbsample->getActive() == true) { - strcpy(state->sample_filename, str); + strncpy(state->sample_filename, str, STATE_FILENAME_LEN); } // Preview wav files @@ -1621,7 +1618,7 @@ void setNoteVol(u16 vol) DC_FlushAll(); } -void handleNoteVolumeChanged(int vol) +void handleNoteVolumeChanged(s32 vol) { setNoteVol(vol); } @@ -1978,6 +1975,10 @@ void deleteMessageBox(void) redrawSubScreen(); } +void requestExit(void) +{ + exit_requested = true; +} void showMessage(const char *msg) { @@ -2481,7 +2482,7 @@ void sampleDrawToggle(bool on) sampledisplay->setDrawMode(on); } -void setupGUI(void) +void setupGUI(int argc, char **argv) { gui = new GUI(); gui->setTheme(settings->getTheme(), settings->getTheme()->col_dark_bg); @@ -2519,6 +2520,15 @@ void setupGUI(void) fileselector->registerFileSelectCallback(handleFileChange); fileselector->registerDirChangeCallback(handleDirChange); + // check argv to set working directory + if (argc >= 1 && argv != NULL && argv[0] != NULL) { + char *path_split = strrchr(argv[0], '/'); + if (path_split != NULL && (path_split - argv[0]) >= 1) { + std::string dir(argv[0]); + fileselector->setDir(dir.substr(0, path_split - argv[0])); + } + } + rbgdiskop = new RadioButton::RadioButtonGroup(); rbsong = new RadioButton(2, 21, 36, 14, &sub_vram, rbgdiskop); @@ -3228,25 +3238,14 @@ void VblankHandler(void) }*/ } - /* - * reboot code commented out for now if( (keysheld & KEY_START) && (keysheld & KEY_SELECT) && (mb == 0) ) { - fatInitDefault(); -#ifdef DEBUG - if(can_reboot()) - reboot(); // >:-) -#else - if(can_reboot()) - { - mb = new MessageBox(&sub_vram, "really exit", 2, "yes", reboot, "no", deleteMessageBox); - gui->registerOverlayWidget(mb, 0, SUB_SCREEN); - mb->show(); - mb->pleaseDraw(); - } -#endif + mb = new MessageBox(&sub_vram, "really exit", 2, "yes", requestExit, "no", deleteMessageBox); + gui->registerOverlayWidget(mb, 0, SUB_SCREEN); + mb->reveal(); + mb->pleaseDraw(); } - */ + if(keysup) { gui->buttonRelease(keysup); @@ -3377,7 +3376,7 @@ void applySettings(void) } //--------------------------------------------------------------------------------- -int main(void) { +int main(int argc, char **argv) { //--------------------------------------------------------------------------------- #ifdef GURU defaultExceptionHandler(); @@ -3406,7 +3405,7 @@ int main(void) { // Sub screen: Keyboard tiles, Typewriter tiles and ERB videoSetModeSub(MODE_5_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG1_ACTIVE | DISPLAY_BG2_ACTIVE); - vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000, VRAM_B_MAIN_BG_0x06020000, + vramSetPrimaryBanks(VRAM_A_MAIN_BG_0x06000000, VRAM_B_MAIN_BG_0x06020000, VRAM_C_SUB_BG_0x06200000 , VRAM_D_LCD); // SUB_BG0 for Piano Tiles @@ -3514,7 +3513,7 @@ int main(void) { CommandSetSong(song); - setupGUI(); + setupGUI(argc, argv); applySettings(); #ifndef DEBUG @@ -3532,7 +3531,7 @@ int main(void) { iprintf("NitroTracker debug build.\nBuilt %s %s\n clears messages.\n", __DATE__, __TIME__); #endif - while(1) + while(!exit_requested) { VblankHandler(); #ifdef WIFIDEBUG @@ -3547,9 +3546,11 @@ int main(void) { #ifdef DEBUG if(keysHeld() == (KEY_START | KEY_SELECT | KEY_L | KEY_R)) { - return 0; + exit_requested = true; } #endif swiWaitForVBlank(); } + + return 0; } diff --git a/arm9/source/settings.cpp b/arm9/source/settings.cpp index f83e524..519617b 100644 --- a/arm9/source/settings.cpp +++ b/arm9/source/settings.cpp @@ -33,29 +33,32 @@ /* ===================== PUBLIC ===================== */ +static void dir_create(const char *path) { + DIR *dir; + if (!(dir = opendir(path))) { + mkdir(path, 0777); + } else { + closedir(dir); + } +} + Settings::Settings(bool use_fat) : handedness(RIGHT_HANDED), sample_preview(true), theme(new Theme()), fat(use_fat) { - memset(songpath, 0, 255); - memset(samplepath, 0, 255); + memset(songpath, 0, SETTINGS_FILENAME_LEN + 1); + memset(samplepath, 0, SETTINGS_FILENAME_LEN + 1); - strcpy(songpath,"/"); - strcpy(samplepath,"/"); + strncpy(songpath, "/", SETTINGS_FILENAME_LEN); + strncpy(samplepath, "/", SETTINGS_FILENAME_LEN); if(fat == true) { // Check if the config file exists and, if not, create it - if(!opendir("/data")) - { - mkdir("/data", 777); - } - if(!opendir("/data/NitroTracker")) - { - mkdir("/data/NitroTracker", 777); - } + dir_create("/data"); + dir_create("/data/NitroTracker"); FILE *conf = fopen("/data/NitroTracker/NitroTracker.conf", "r"); if(conf == NULL) { @@ -150,15 +153,15 @@ void Settings::setTheme(Theme *theme_) char *Settings::getSongPath(void) { if(!opendir(songpath)) { - strcpy(songpath, "/"); + strncpy(songpath, "/", SETTINGS_FILENAME_LEN); } return songpath; } void Settings::setSongPath(const char* songpath_) { - strncpy(songpath, songpath_, 255); - songpath[min(255, strlen(songpath_))] = 0; + strncpy(songpath, songpath_, SETTINGS_FILENAME_LEN); + songpath[SETTINGS_FILENAME_LEN] = '\0'; if(fat) { write(); } @@ -167,15 +170,15 @@ void Settings::setSongPath(const char* songpath_) char *Settings::getSamplePath(void) { if(!opendir(samplepath)) { - strcpy(samplepath, "/"); + strncpy(samplepath, "/", SETTINGS_FILENAME_LEN); } return samplepath; } void Settings::setSamplePath(const char* samplepath_) { - strncpy(samplepath, samplepath_, 255); - samplepath[min(255, strlen(samplepath_))] = 0; + strncpy(samplepath, samplepath_, SETTINGS_FILENAME_LEN); + samplepath[SETTINGS_FILENAME_LEN] = '\0'; if(fat) { write(); } diff --git a/arm9/source/settings.h b/arm9/source/settings.h index 9ee1403..3623049 100644 --- a/arm9/source/settings.h +++ b/arm9/source/settings.h @@ -31,6 +31,8 @@ #include +#define SETTINGS_FILENAME_LEN 255 + enum Handedness {LEFT_HANDED, RIGHT_HANDED}; class Settings { @@ -67,8 +69,8 @@ class Settings { Handedness handedness; bool sample_preview; Theme *theme; - char songpath[256]; - char samplepath[256]; + char songpath[SETTINGS_FILENAME_LEN + 1]; + char samplepath[SETTINGS_FILENAME_LEN + 1]; bool fat; }; diff --git a/arm9/source/state.cpp b/arm9/source/state.cpp index 5df32bd..5ed3ce8 100644 --- a/arm9/source/state.cpp +++ b/arm9/source/state.cpp @@ -36,8 +36,8 @@ void State::reset(void) dsmi_send = true; dsmi_recv = true; preview_sample = 0; - song_filename = (char*)calloc(1, 512); - sample_filename = (char*)calloc(1, 512); + song_filename = (char*)calloc(1, STATE_FILENAME_LEN + 1); + sample_filename = (char*)calloc(1, STATE_FILENAME_LEN + 1); resetSong(); } diff --git a/arm9/source/state.h b/arm9/source/state.h index 4db326f..699caa6 100644 --- a/arm9/source/state.h +++ b/arm9/source/state.h @@ -31,6 +31,8 @@ #include #include "ntxm/song.h" +#define STATE_FILENAME_LEN 511 + class State { public: State(void); diff --git a/arm9/source/tobkit/patternview.cpp b/arm9/source/tobkit/patternview.cpp index b83b1af..8c93f6a 100644 --- a/arm9/source/tobkit/patternview.cpp +++ b/arm9/source/tobkit/patternview.cpp @@ -237,7 +237,7 @@ void PatternView::unmute(u16 channel) void PatternView::draw(void) { u32 colcol = col_bg | col_bg << 16; - dmaFillWordsDamnFast(colcol, *vram, 192*256*2); + dmaFillWords(colcol, *vram, 192*256*2); // Selection if(selection_exists == true) { @@ -331,11 +331,11 @@ void PatternView::draw(void) } // Channel indices - char *numberstr = (char*)malloc(3); + char *numberstr = (char*) malloc(3); for(u16 i=0;icol_dark_bg); u32 colcol = theme->col_dark_bg | theme->col_dark_bg << 16; - for(int j=y;jcol_light_ctrl, theme->col_dark_ctrl, i); - // TODO; Eliminate floats here!! + int32 step = divf32(inttof32(smp->getNSamples() >> zoom_level), inttof32(width-2)); + int32 pos = 0; - float step = (float)(smp->getNSamples() >> zoom_level) / (float)(width-2); - float pos = 0.0f; - - u32 renderwindow = (u32)MAX(1, MIN(100, ceil(step))); + u32 renderwindow = (u32)MAX(1, MIN(100, ceil_f32toint(step))); u16 middle = (DRAW_HEIGHT+2)/2;//-1; @@ -505,7 +503,7 @@ void SampleDisplay::draw(void) for(u32 i=1; igetData(); + s8 *base = (s8*)smp->getData() + pixelToSample(0); for(u32 i=1; i 1 + LOOP_TRIANGLE_SIZE) diff --git a/arm9/source/tools.cpp b/arm9/source/tools.cpp index 14bb08c..d22ec59 100644 --- a/arm9/source/tools.cpp +++ b/arm9/source/tools.cpp @@ -50,11 +50,21 @@ void dbgWaitButton(void) while(! (keysDown()&KEY_A) ) scanKeys(); } -#endif +/* https://devkitpro.org/viewtopic.php?f=6&t=3057 */ + +extern u8 *fake_heap_end; +extern u8 *fake_heap_start; + +static int getFreeMem() { + struct mallinfo mi = mallinfo(); + return mi.fordblks + (fake_heap_end - (u8*)sbrk(0)); +} + +/* end */ void PrintFreeMem(void) { - iprintf("FreeMem=%dbyte \n", my_get_free_mem()); + iprintf("FreeMem=%dbyte \n", getFreeMem()); } void printMallInfo(void) @@ -65,3 +75,5 @@ void printMallInfo(void) iprintf("mmap bytes: %d\n", mi.hblkhd); iprintf("malloc chunks: %d\n", mi.uordblks); } + +#endif \ No newline at end of file diff --git a/arm9/source/tools.h b/arm9/source/tools.h index 8e3a95c..b4ad373 100644 --- a/arm9/source/tools.h +++ b/arm9/source/tools.h @@ -32,7 +32,7 @@ // A collection of utilities for everyday DS coding -#define DMA0FILL (*(vuint32*)0x040000E0) +#define ceil_f32toint(n) (((n) + ((1 << 12) - 1)) >> 12) #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) @@ -43,16 +43,4 @@ void dbgWaitButton(void); void PrintFreeMem(void); void printMallInfo(void); -// The fastest known clear method on the DS known to man (or to me, at least) -// I found out that the version in libnds doesn't use the special fill -// register that has fast, cache-less read access. -inline void dmaFillWordsDamnFast(const u32 word, void* dest, uint32 size) -{ - DMA0FILL = word; - DMA_SRC(0) = (u32)(&DMA0FILL); - DMA_DEST(0) = (uint32)dest; - DMA_CR(0) = DMA_SRC_FIX | DMA_COPY_WORDS | size >> 2; - while(DMA_CR(0) & DMA_BUSY); -} - #endif diff --git a/tobkit/include/tobkit/togglebutton.h b/tobkit/include/tobkit/togglebutton.h index 512ed26..027b40c 100644 --- a/tobkit/include/tobkit/togglebutton.h +++ b/tobkit/include/tobkit/togglebutton.h @@ -7,6 +7,7 @@ class ToggleButton: public Widget { public: ToggleButton(u8 _x, u8 _y, u8 _width, u8 _height, u16 **_vram, bool _visible=true); + ~ToggleButton(); // Callback registration void registerToggleCallback(void (*onToggle_)(bool)); diff --git a/tobkit/source/button.cpp b/tobkit/source/button.cpp index d901f00..5d44250 100644 --- a/tobkit/source/button.cpp +++ b/tobkit/source/button.cpp @@ -15,9 +15,7 @@ Button::Button(u8 _x, u8 _y, u8 _width, u8 _height, uint16 **_vram, bool _visibl Button::~Button() { - if(caption != 0) { - free(caption); - } + if (caption) free(caption); } void Button::registerPushCallback(void (*onPush_)(void)) { @@ -56,6 +54,7 @@ void Button::buttonPress(u16 button) { } void Button::setCaption(const char *_caption) { + if (caption) free(caption); caption = (char*)malloc(strlen(_caption)+1); strcpy(caption, _caption); } diff --git a/tobkit/source/checkbox.cpp b/tobkit/source/checkbox.cpp index 0231d48..d6f4196 100644 --- a/tobkit/source/checkbox.cpp +++ b/tobkit/source/checkbox.cpp @@ -17,14 +17,14 @@ CheckBox::CheckBox(u8 _x, u8 _y, u8 _width, u8 _height, u16 **_vram, bool _visib CheckBox::~CheckBox() { - if(label) - free(label); + if (label) free(label); } void CheckBox::setCaption(const char *_label) { - label = (char*)calloc(256, 1); - strncpy(label, _label, 256); + if (label) free(label); + label = (char*) malloc(sizeof(_label) + 1); + strcpy(label, _label); } void CheckBox::setChecked(bool checked_) diff --git a/tobkit/source/fileselector.cpp b/tobkit/source/fileselector.cpp index 859e497..bd2d0de 100644 --- a/tobkit/source/fileselector.cpp +++ b/tobkit/source/fileselector.cpp @@ -149,36 +149,25 @@ std::string stringtolowercase(std::string str) { std::string outstr = str; for(u8 i=0;i=65)&&(outstr[i]<=90)) { - outstr[i]+=32; - } + outstr[i] = tolower(outstr[i]); } return outstr; } inline bool compare_filenames(File f1, File f2) { - char *fn1, *fn2; - fn1 = (char*)malloc(256); - fn2 = (char*)malloc(256); - - strcpy(fn1, stringtolowercase(f1.name).c_str()); - strcpy(fn2, stringtolowercase(f2.name).c_str()); - bool res; + if((f1.is_dir)&&(!f2.is_dir)) { res = true; } else if ((!f1.is_dir)&&(f2.is_dir)) { res = false; - } else if(strcmp(fn1,fn2)<0) { + } else if(strcasecmp(f1.name.c_str(),f2.name.c_str())<0) { res = true; } else { res = false; } - free(fn1); - free(fn2); - return res; } @@ -198,8 +187,6 @@ void FileSelector::read_directory(void) return; } //iprintf("%d\n", __LINE__); - char *filename = (char*)malloc(PATH_MAX); - DIR *dir; struct stat filestats; //iprintf("%d\n", __LINE__); @@ -214,8 +201,11 @@ void FileSelector::read_directory(void) if(direntry == NULL) { iprintf("No files found!\n"); + closedir(dir); return; } + + char *filename = (char*)malloc(PATH_MAX); //iprintf("%d\n", __LINE__); while(direntry != NULL) { @@ -293,7 +283,7 @@ void FileSelector::read_directory(void) activeelement = 0; scrollpos = 0; //iprintf("%d\n", __LINE__); - filename = (char*)malloc(256); + filename = (char*) calloc(1, 256); std::string newentry; for(u16 i=0;itext, text, 256); + strncpy(this->text, text, 255); pleaseDraw(); } diff --git a/tobkit/source/label.cpp b/tobkit/source/label.cpp index 33f6a5c..fc64b06 100644 --- a/tobkit/source/label.cpp +++ b/tobkit/source/label.cpp @@ -17,9 +17,7 @@ Label::Label(u8 _x, u8 _y, u8 _width, u8 _height, uint16 **_vram, bool _has_bord Label::~Label(void) { - if(caption!=0) { - free(caption); - } + if (caption) free(caption); } // Callback registration @@ -44,9 +42,7 @@ void Label::penDown(u8 x, u8 y) void Label::setCaption(const char *_caption) { - if(caption!=0) { - free(caption); - } + if (caption) free(caption); caption = (char*)malloc(strlen(_caption)+1); strcpy(caption, _caption); diff --git a/tobkit/source/listbox.cpp b/tobkit/source/listbox.cpp index c649fe3..321b71b 100644 --- a/tobkit/source/listbox.cpp +++ b/tobkit/source/listbox.cpp @@ -303,7 +303,7 @@ void ListBox::draw(void) // Numbers (if enabled) u16 contentoffset; if(show_numbers) { - char *numberstr = (char*)malloc(4); + char *numberstr = (char*)malloc(4 + 1); //iprintf("%u %u\n",scrollpos,elements.size()); u16 offset; if(zero_offset==true) { @@ -312,7 +312,7 @@ void ListBox::draw(void) offset=1; } for(i=0;(icol_lighter_bg); char *numberstr = (char*)malloc(4); + if(hex==true) { - sprintf(numberstr, "%2x", value); + snprintf(numberstr, 4, "%2lx", value); } else { - sprintf(numberstr, "%3d", value); + snprintf(numberstr, 4, "%3ld", value); } drawString(numberstr, 10, 5); free(numberstr); diff --git a/tobkit/source/togglebutton.cpp b/tobkit/source/togglebutton.cpp index 1401a6d..760c92f 100644 --- a/tobkit/source/togglebutton.cpp +++ b/tobkit/source/togglebutton.cpp @@ -13,6 +13,11 @@ ToggleButton::ToggleButton(u8 _x, u8 _y, u8 _width, u8 _height, u16 **_vram, boo caption = (char*)calloc(1, 1); } +ToggleButton::~ToggleButton() +{ + if (caption) free(caption); +} + // Callback registration void ToggleButton::registerToggleCallback(void (*onToggle_)(bool)) { onToggle = onToggle_; @@ -51,9 +56,7 @@ void ToggleButton::buttonPress(u16 button) void ToggleButton::setCaption(const char *_caption) { - if(caption != 0) - free(caption); - + if (caption) free(caption); caption = (char*)malloc(strlen(_caption)+1); strcpy(caption, _caption); } diff --git a/tobkit/source/typewriter.cpp b/tobkit/source/typewriter.cpp index af122d0..1929eef 100644 --- a/tobkit/source/typewriter.cpp +++ b/tobkit/source/typewriter.cpp @@ -204,15 +204,9 @@ void Typewriter::registerCancelCallback(void (*onCancel_)(void)) void Typewriter::setText(const char *text_) { - for(u16 i=0;isetCaption(text); redraw(); diff --git a/tobkit/source/widget.cpp b/tobkit/source/widget.cpp index 9418e47..995ea81 100644 --- a/tobkit/source/widget.cpp +++ b/tobkit/source/widget.cpp @@ -113,12 +113,12 @@ void Widget::drawString(const char* str, u8 tx, u8 ty, u8 maxwidth, u16 color) } for(j=0;j<11;++j) { - for(i=0;i<8;++i) { + col = font_8x11_raw[N_FONT_CHARS*j+charidx]; + for(i=0;i<8;++i,col>>=1) { // Print a character from the bitmap font // each char is 8 pixels wide, and 8 pixels // are in a byte. - col = font_8x11_raw[N_FONT_CHARS*j+charidx]; - if(col & BIT(i)) { + if(col & 1) { *(*vram+SCREEN_WIDTH*(j+y+ty)+(i+x+tx+drawpos)) = color; } /* @@ -311,10 +311,9 @@ u8 Widget::getStringWidth(const char *str, u16 limit) } void Widget::drawMonochromeIcon(u8 tx, u8 ty, u8 tw, u8 th, const u8 *icon, u16 color) { - + u16 pixelidx = 0; for(u8 j=0;j