1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/************************************************************************
Copyright mooby 2002
CDRMooby2 ConfigCallbacks.cpp
http://mooby.psxfanatics.com
This file is protected by the GNU GPL which should be included with
the source code distribution.
************************************************************************/
#pragma warning(disable:4786)
#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/fl_ask.H>
#include <FL/Fl_File_Chooser.H>
#include <FL/Fl_Check_Button.H>
#include <FL/Fl_Value_Slider.H>
#include <FL/Fl_Progress.H>
#include "FileInterface.hpp"
#include "Preferences.hpp"
#include "ConfigCallbacks.hpp"
#include "ConfigFunctions.hpp"
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
extern Preferences prefs;
// a container for a window with a progress bar. used for compress and decompress
class ProgWindow
{
public:
ProgWindow(const string& text)
{
{
Fl_Window* o = new Fl_Window(220, 50, text.c_str());
w = o;
prog = new Fl_Progress(0,0,200,20);
prog->minimum(0);
prog->maximum(1);
prog->value(0);
o->end();
}
w->show();
Fl::wait(0);
}
void update(const float moo)
{
prog->value(moo);
w->redraw();
Fl::wait(0);
}
~ProgWindow() {w->hide(); w->redraw(); delete w;}
private:
Fl_Window* w;
Fl_Progress* prog;
};
// compresses cd using the compression methods in zipCD
void compressIt(FileInterface* cd,
CompressedFileInterface* zipCD,
const string& outputFileName,
const string& indexFileName)
{
cd->setCacheMode(FileInterface::oldMode);
CDTime start(0,2,0);
// these hold the info needed for generating the .index or .table file
vector<unsigned long> indexTable;
vector<unsigned long> sizeTable;
// open the output file
std::ofstream outfile(outputFileName.c_str(), ios::binary);
// the buffers for compression
unsigned char* inputBuffer =
new unsigned char[zipCD->getCompressedFrames() * bytesPerFrame];
unsigned char* outputBuffer =
new unsigned char[(zipCD->getCompressedFrames() + 1) * bytesPerFrame];
ProgWindow* p = new ProgWindow("Compressing...");
while (start < cd->getCDLength())
{
unsigned int i = 0;
// read up to compressedFrames or until the end of the CD, whichever is smaller
while ( (start < cd->getCDLength()) && (i < zipCD->getCompressedFrames()) )
{
p->update((float)start.getAbsoluteFrame() / (float)cd->getCDLength().getAbsoluteFrame());
cd->seek(start);
memcpy(inputBuffer + bytesPerFrame * i, cd->getBuffer(), bytesPerFrame);
i++;
start += CDTime(0,0,1);
}
// add the index information for the file location
indexTable.push_back(outfile.tellp());
// compress the data
unsigned int outputBytes = (zipCD->getCompressedFrames() + 1) * bytesPerFrame;
unsigned int inputBytes = i * bytesPerFrame;
zipCD->compressData((char*)inputBuffer, (char*)outputBuffer,
inputBytes, outputBytes);
// write the compressed data and add the compressed size to the sizeTable
outfile.write((char*)outputBuffer, outputBytes);
sizeTable.push_back(outputBytes);
}
// write the index file
string tableToWrite = zipCD->toTable(indexTable, sizeTable);
ofstream indexFile(indexFileName.c_str(), ios::binary);
indexFile.write(tableToWrite.c_str(), tableToWrite.size());
// clean up
indexFile.close();
outfile.close();
delete [] inputBuffer;
delete [] outputBuffer;
delete cd;
delete zipCD;
delete p;
moobyMessage("Done");
}
// decompressing is much easier
void decompressIt(FileInterface* cd,
const string& outputFileName)
{
cd->setCacheMode(FileInterface::oldMode);
CDTime start(0,2,0);
std::ofstream outfile(outputFileName.c_str(), ios::binary);
ProgWindow* p = new ProgWindow("Decompressing...");
// write out every frame in the file
while (start < cd->getCDLength())
{
p->update((float)start.getAbsoluteFrame() / (float)cd->getCDLength().getAbsoluteFrame());
cd->seek(start);
outfile.write((char*)cd->getBuffer(), bytesPerFrame);
start += CDTime(0,0,1);
}
outfile.close();
delete cd;
delete p;
moobyMessage("Done");
}
// callback for bzcompress
void bzCompress(Fl_Button*, void*)
{
char * returned;
if ( (returned = moobyFileChooser("Choose a file to compress in bz.index format", theUsualSuspects.c_str())) == NULL)
{
return;
}
string outputFileName(returned);
outputFileName += ".bz";
string indexFileName(outputFileName + string(".index"));
FileInterface* cd = new UncompressedFileInterface(1);
CompressedFileInterface* zipCD = new BZIndexFileInterface(1);
cd->openFile(returned);
compressIt(cd, zipCD, outputFileName, indexFileName);
}
// callback for bzdecompress
void bzDecompress(Fl_Button*, void*)
{
char * returned;
if ( (returned = moobyFileChooser("Choose a .bz file to decompress", "*.bz")) == NULL)
{
return;
}
FileInterface* cd = new BZIndexFileInterface(1);
string theFile(returned);
cd->openFile(theFile);
string outputFileName(theFile);
outputFileName.erase(theFile.rfind(".bz"));
decompressIt(cd, outputFileName);
}
// callback for zcompress
void zCompress(Fl_Button*, void*)
{
char * returned;
if ( (returned = moobyFileChooser("Choose a file to compress in Z.table format", theUsualSuspects.c_str())) == NULL)
{
return;
}
string outputFileName(returned);
outputFileName += ".Z";
string indexFileName(outputFileName + string(".table"));
FileInterface* cd = new UncompressedFileInterface(1);
CompressedFileInterface* zipCD = new ZTableFileInterface(1);
cd->openFile(returned);
compressIt(cd, zipCD, outputFileName, indexFileName);
}
// callback for zdecompress
void zDecompress(Fl_Button*, void*)
{
char * returned;
if ( (returned = moobyFileChooser("Choose a .Z file to decompress", "*.Z")) == NULL)
{
return;
}
FileInterface* cd = new ZTableFileInterface(1);
string theFile(returned);
cd->openFile(theFile);
string outputFileName(theFile);
outputFileName.erase(theFile.rfind(".Z"));
decompressIt(cd, outputFileName);
}
// callback for cddavolume
void CDDAVolume(Fl_Value_Slider* slider, void*)
{
ostringstream ost;
ost << slider->value();
prefs.prefsMap[volumeString] = ost.str();
prefs.write();
}
// callback for repeat
void repeatAllCDDA(Fl_Check_Button* button, void* val)
{
if (button == NULL)
{
((Fl_Check_Button*)val)->value(0);
}
else
{
button->value(1);
prefs.prefsMap[repeatString] = std::string(repeatAllString);
prefs.write();
repeatOneCDDA(NULL, ((ConfigWindow*)val)->repeatOneButton);
playOneCDDA(NULL, ((ConfigWindow*)val)->playOneButton);
}
}
// callback for repeat
void repeatOneCDDA(Fl_Check_Button* button, void* val)
{
if (button == NULL)
{
((Fl_Check_Button*)val)->value(0);
}
else
{
button->value(1);
prefs.prefsMap[repeatString] = std::string(repeatOneString);
prefs.write();
repeatAllCDDA(NULL, ((ConfigWindow*)val)->repeatAllButton);
playOneCDDA(NULL, ((ConfigWindow*)val)->playOneButton);
}
}
// callback for repeat
void playOneCDDA(Fl_Check_Button* button, void* val)
{
if (button == NULL)
{
((Fl_Check_Button*)val)->value(0);
}
else
{
button->value(1);
prefs.prefsMap[repeatString] = std::string(playOneString);
prefs.write();
repeatAllCDDA(NULL, ((ConfigWindow*)val)->repeatAllButton);
repeatOneCDDA(NULL, ((ConfigWindow*)val)->repeatOneButton);
}
}
// callback for selecting an image for autorun
void chooseAutorunImage(Fl_Button* button, void* val)
{
char *returned = NULL;
bool done = false;
while (!done)
{
returned = moobyFileChooser("Choose an image to run", theUsualSuspects.c_str(), prefs.prefsMap[lastrunString]);
if (returned == NULL)
{
if (moobyAsk("You hit cancel or didn't pick a file.\nPick a different file?") == 0)
{
done = true;
}
}
else
{
done = true;
}
}
if (returned != NULL)
{
prefs.prefsMap[autorunString] = std::string(returned);
}
((ConfigWindow*)val)->autorunBox->label(prefs.prefsMap[autorunString].c_str());
}
void clearAutorunImage(Fl_Button* button, void* val)
{
prefs.prefsMap[autorunString] = std::string();
((ConfigWindow*)val)->autorunBox->label("No autorun image selected");
}
// callback for OK
void configOK(Fl_Return_Button* button, void* val)
{
delete ((ConfigWindow*)val)->w;
}
// callback for cachesize
void cacheSize(Fl_Value_Slider* slider, void*)
{
ostringstream ost;
ost << slider->value();
prefs.prefsMap[cacheSizeString] = ost.str();
prefs.write();
}
// callback for new caching button
void newCaching(Fl_Check_Button* button, void*)
{
if (button->value() == 1)
prefs.prefsMap[cachingModeString] = std::string(newCachingString);
else
prefs.prefsMap[cachingModeString] = std::string(oldCachingString);
prefs.write();
}
// callback for enable/disable subchannel
void subEnable(Fl_Check_Button* button, void*)
{
if (button->value() == 1)
prefs.prefsMap[subEnableString] = std::string("booyah");
else
prefs.prefsMap[subEnableString] = std::string();
prefs.write();
}
|