diff --git a/src/a2colorsobserved.cpp b/src/a2colorsobserved.cpp index 076f3f1..ce359ae 100644 --- a/src/a2colorsobserved.cpp +++ b/src/a2colorsobserved.cpp @@ -19,74 +19,74 @@ static unsigned char tobyt(float x) { - x *= 256.0f; - x += 0.0001f; - int xi = (int)x; + x *= 256.0f; + x += 0.0001f; + int xi = (int)x; - if (xi > 255) - xi = 255; + if (xi > 255) + xi = 255; - return (unsigned char)xi; + return (unsigned char)xi; } // 0 <= h < 360 degrees; 0 <= s,v <= 1 static int HSVtoRGB(const int h, const float s, const float v) { - const float f = (h % 60) / 60.0; + const float f = (h % 60) / 60.0; - float r, g, b; - switch (h / 60) - { - case 0: - r = v; - g = v * (1 - s * (1 - f)); - b = v * (1 - s); - break; - case 1: - r = v * (1 - s * f); - g = v; - b = v * (1 - s); - break; - case 2: - r = v * (1 - s); - g = v; - b = v * (1 - s * (1 - f)); - break; - case 3: - r = v * (1 - s); - g = v * (1 - s * f); - b = v; - break; - case 4: - r = v * (1 - s * (1 - f)); - g = v * (1 - s); - b = v; - break; - case 5: - r = v; - g = v * (1 - s); - b = v * (1 - s * f); - break; + float r, g, b; + switch (h / 60) + { + case 0: + r = v; + g = v * (1 - s * (1 - f)); + b = v * (1 - s); + break; + case 1: + r = v * (1 - s * f); + g = v; + b = v * (1 - s); + break; + case 2: + r = v * (1 - s); + g = v; + b = v * (1 - s * (1 - f)); + break; + case 3: + r = v * (1 - s); + g = v * (1 - s * f); + b = v; + break; + case 4: + r = v * (1 - s * (1 - f)); + g = v * (1 - s); + b = v; + break; + case 5: + r = v; + g = v * (1 - s); + b = v * (1 - s * f); + break; default: r = g = b = 0; - } + } - return (tobyt(r) << 16) | (tobyt(g) << 8) | (tobyt(b)); + return (tobyt(r) << 16) | (tobyt(g) << 8) | (tobyt(b)); } A2ColorsObserved::A2ColorsObserved(): - COLOR(0x10) + COLOR(0x10) { // const unsigned int clr[] = { 0x1, 0xB, 0x3, 0x2, 0x7, 0x6, 0x4, 0xE, 0xC, 0x8, 0xD, 0x9, 0x5, 0xA, 0xF, 0x0 }; - const unsigned int map[] = { 0xF, 0x0, 0x3, 0x2, 0x6, 0xC, 0x5, 0x4, 0x9, 0xB, 0xD, 0x1, 0x8, 0xA, 0x7, 0xE }; - const unsigned int hue[] = { 342, 342, 277, 233, 233, 213, 160, 160, 75, 33, 52, 24, 0, 0, 0, 0 }; - const unsigned int sat[] = { 100, 50, 75, 100, 50, 100, 100, 100, 100, 100, 100, 100, 0, 0, 0, 0 }; - const unsigned int val[] = { 67, 100, 100, 75, 100, 100, 33, 100, 75, 50, 100, 100, 50, 50, 100, 0 }; + const unsigned int map[] = { 0xF, 0x0, 0x3, 0x2, 0x6, 0xC, 0x5, 0x4, 0x9, 0xB, 0xD, 0x1, 0x8, 0xA, 0x7, 0xE }; + const unsigned int hue[] = { 342, 342, 277, 233, 233, 213, 160, 160, 75, 33, 52, 24, 0, 0, 0, 0 }; + const unsigned int sat[] = { 100, 50, 75, 100, 50, 100, 100, 100, 100, 100, 100, 100, 0, 0, 0, 0 }; + const unsigned int val[] = { 67, 100, 100, 75, 100, 100, 33, 100, 75, 50, 100, 100, 50, 50, 100, 0 }; - for (unsigned int i(0); i < COLOR.size(); ++i) - { - COLOR[i] = HSVtoRGB(hue[map[i]],sat[map[i]]/100.0f,val[map[i]]/100.0f); - } + for (unsigned int i(0); i < COLOR.size(); ++i) + { + COLOR[i] = HSVtoRGB(hue[map[i]],sat[map[i]]/100.0f,val[map[i]]/100.0f); + } } diff --git a/src/a2colorsobserved.h b/src/a2colorsobserved.h index 8b31ba5..c35b692 100644 --- a/src/a2colorsobserved.h +++ b/src/a2colorsobserved.h @@ -23,33 +23,33 @@ class A2ColorsObserved { private: - std::vector COLOR; + std::vector COLOR; public: - A2ColorsObserved(); - ~A2ColorsObserved(); + A2ColorsObserved(); + ~A2ColorsObserved(); - const std::vector& c() { return this->COLOR; } + const std::vector& c() { return this->COLOR; } - enum - { - BLACK, - DARK_MAGENTA, - DARK_BLUE, - HIRES_VIOLET, - DARK_BLUE_GREEN, - GREY, - HIRES_BLUE, - LIGHT_BLUE, - DARK_BROWN, - HIRES_ORANGE, - GREY_ALTERNATE, - LIGHT_MAGENTA, - HIRES_GREEN, - LIGHT_BROWN, - LIGHT_BLUE_GREEN, - WHITE, - }; + enum + { + BLACK, + DARK_MAGENTA, + DARK_BLUE, + HIRES_VIOLET, + DARK_BLUE_GREEN, + GREY, + HIRES_BLUE, + LIGHT_BLUE, + DARK_BROWN, + HIRES_ORANGE, + GREY_ALTERNATE, + LIGHT_MAGENTA, + HIRES_GREEN, + LIGHT_BROWN, + LIGHT_BLUE_GREEN, + WHITE, + }; }; #endif diff --git a/src/analogtv.cpp b/src/analogtv.cpp index 930a630..f3b7d47 100644 --- a/src/analogtv.cpp +++ b/src/analogtv.cpp @@ -41,25 +41,25 @@ AnalogTV::AnalogTV(ScreenImage& image): - image(image), - on(false), - noise(false), - bleed_down(true) + image(image), + on(false), + noise(false), + bleed_down(true) { - hirescolor.push_back(colors.c()[A2ColorsObserved::HIRES_GREEN]); - hirescolor.push_back(colors.c()[A2ColorsObserved::HIRES_ORANGE]); - hirescolor.push_back(colors.c()[A2ColorsObserved::HIRES_VIOLET]); - hirescolor.push_back(colors.c()[A2ColorsObserved::HIRES_BLUE]); + hirescolor.push_back(colors.c()[A2ColorsObserved::HIRES_GREEN]); + hirescolor.push_back(colors.c()[A2ColorsObserved::HIRES_ORANGE]); + hirescolor.push_back(colors.c()[A2ColorsObserved::HIRES_VIOLET]); + hirescolor.push_back(colors.c()[A2ColorsObserved::HIRES_BLUE]); - loreslightcolor.push_back(colors.c()[A2ColorsObserved::LIGHT_BROWN]); - loreslightcolor.push_back(colors.c()[A2ColorsObserved::LIGHT_MAGENTA]); - loreslightcolor.push_back(colors.c()[A2ColorsObserved::LIGHT_BLUE]); - loreslightcolor.push_back(colors.c()[A2ColorsObserved::LIGHT_BLUE_GREEN]); + loreslightcolor.push_back(colors.c()[A2ColorsObserved::LIGHT_BROWN]); + loreslightcolor.push_back(colors.c()[A2ColorsObserved::LIGHT_MAGENTA]); + loreslightcolor.push_back(colors.c()[A2ColorsObserved::LIGHT_BLUE]); + loreslightcolor.push_back(colors.c()[A2ColorsObserved::LIGHT_BLUE_GREEN]); - loresdarkcolor.push_back(colors.c()[A2ColorsObserved::DARK_BLUE_GREEN]); - loresdarkcolor.push_back(colors.c()[A2ColorsObserved::DARK_BROWN]); - loresdarkcolor.push_back(colors.c()[A2ColorsObserved::DARK_MAGENTA]); - loresdarkcolor.push_back(colors.c()[A2ColorsObserved::DARK_BLUE]); + loresdarkcolor.push_back(colors.c()[A2ColorsObserved::DARK_BLUE_GREEN]); + loresdarkcolor.push_back(colors.c()[A2ColorsObserved::DARK_BROWN]); + loresdarkcolor.push_back(colors.c()[A2ColorsObserved::DARK_MAGENTA]); + loresdarkcolor.push_back(colors.c()[A2ColorsObserved::DARK_BLUE]); } @@ -69,25 +69,25 @@ AnalogTV::~AnalogTV() void AnalogTV::powerOn(bool b) { - this->on = b; - this->image.notifyObservers(); + this->on = b; + this->image.notifyObservers(); } void AnalogTV::setType(DisplayType type) { - this->type = type; + this->type = type; } void AnalogTV::cycleType() { - this->type = (DisplayType)((((int)this->type)+1)%NUM_DISPLAY_TYPES); + this->type = (DisplayType)((((int)this->type)+1)%NUM_DISPLAY_TYPES); } void AnalogTV::toggleBleedDown() { - this->bleed_down = !this->bleed_down; - this->image.blank(); - this->image.notifyObservers(); + this->bleed_down = !this->bleed_down; + this->image.blank(); + this->image.notifyObservers(); } @@ -137,18 +137,18 @@ void AnalogTV::toggleBleedDown() class IQ { public: - double iq[4]; - IQ() - { - for (int i = 0; i < 4; ++i) - iq[i] = 0; - } - IQ(double aiq[]) - { - for (int i = 0; i < 4; ++i) - iq[i] = aiq[i]; - } - double get(int i) const { return this->iq[i]; } + double iq[4]; + IQ() + { + for (int i = 0; i < 4; ++i) + iq[i] = 0; + } + IQ(double aiq[]) + { + for (int i = 0; i < 4; ++i) + iq[i] = aiq[i]; + } + double get(int i) const { return this->iq[i]; } }; const IQ& AnalogTV::BLACK_AND_WHITE = IQ(); @@ -158,67 +158,67 @@ static const int CB_EXTRA(32); class CB { public: - std::vector cb; + std::vector cb; - CB(const int acb[]): - cb(AppleNTSC::CB_END-AppleNTSC::CB_START-CB_EXTRA) - { - for (std::vector::size_type i(0); i < this->cb.size(); ++i) - { - this->cb[i] = acb[i]; - } - } - int get(const int i) const { return this->cb[i]; } - int length() const { return this->cb.size(); } - void getPhase(double phase[]) const - { - { - for (int i = 0; i < 4; ++i) - { - phase[i & 3] = 0; - } - } - { - for (int i = 0; i < length(); ++i) - { - phase[i & 3] += this->cb[i]; - } - } - double tot = 0; - { - for (int i = 0; i < 4; ++i) - { - tot += phase[i] * phase[i]; - } - } - const double tsrt = sqrt(tot); - if (tsrt < .0001) - { - return; - } - for (int i = 0; i < 4; i++) - { - phase[i] /= tsrt; - } - } - bool isColor() const - { - int tot = 0; - for (int i = 0; i < length(); ++i) - { - const int icb = this->cb[i]; - if (icb < 0) - tot += -icb; - else - tot += icb; - } - return 220 < tot && tot < 260; - } + CB(const int acb[]): + cb(AppleNTSC::CB_END-AppleNTSC::CB_START-CB_EXTRA) + { + for (std::vector::size_type i(0); i < this->cb.size(); ++i) + { + this->cb[i] = acb[i]; + } + } + int get(const int i) const { return this->cb[i]; } + int length() const { return this->cb.size(); } + void getPhase(double phase[]) const + { + { + for (int i = 0; i < 4; ++i) + { + phase[i & 3] = 0; + } + } + { + for (int i = 0; i < length(); ++i) + { + phase[i & 3] += this->cb[i]; + } + } + double tot = 0; + { + for (int i = 0; i < 4; ++i) + { + tot += phase[i] * phase[i]; + } + } + const double tsrt = sqrt(tot); + if (tsrt < .0001) + { + return; + } + for (int i = 0; i < 4; i++) + { + phase[i] /= tsrt; + } + } + bool isColor() const + { + int tot = 0; + for (int i = 0; i < length(); ++i) + { + const int icb = this->cb[i]; + if (icb < 0) + tot += -icb; + else + tot += icb; + } + return 220 < tot && tot < 260; + } - bool operator<(const CB& that) const - { - return this->cb < that.cb; - } + bool operator<(const CB& that) const + { + return this->cb < that.cb; + } }; @@ -229,21 +229,21 @@ public: void AnalogTV::drawCurrent() { - if (this->on) - { - switch (this->type) - { - case MONITOR_COLOR: drawMonitorColor(); break; - case MONITOR_GREEN: drawMonitorGreen(); break; - case TV_OLD_COLOR: drawTVOld(); break; - case NUM_DISPLAY_TYPES: break; - } - } - else - { - drawBlank(); - } - this->image.notifyObservers(); + if (this->on) + { + switch (this->type) + { + case MONITOR_COLOR: drawMonitorColor(); break; + case MONITOR_GREEN: drawMonitorGreen(); break; + case TV_OLD_COLOR: drawTVOld(); break; + case NUM_DISPLAY_TYPES: break; + } + } + else + { + drawBlank(); + } + this->image.notifyObservers(); } @@ -252,80 +252,80 @@ static const int D_IP(AppleNTSC::H-2-350); void AnalogTV::drawMonitorColor() { - unsigned int *rgb = new unsigned int[AppleNTSC::H]; - int ip = 0; - for (int row = 0; row < 192; ++row) - { - const CB cb = get_cb(row); - const bool removeColor = !cb.isColor(); - ntsc_to_rgb_monitor(row*AppleNTSC::H+350,AppleNTSC::H-350,rgb); - for (int col = 350; col < AppleNTSC::H-2; ++col) - { - int rgbv = rgb[col-350]; - if (removeColor && rgbv != 0) - { - rgbv = 0xFFFFFF; - } - this->image.setElem(ip,rgbv); - if (bleed_down) - this->image.setElem(ip+D_IP,rgbv); // display same pixel on next row - ++ip; - } - ip += D_IP; - } - delete [] rgb; + unsigned int *rgb = new unsigned int[AppleNTSC::H]; + int ip = 0; + for (int row = 0; row < 192; ++row) + { + const CB cb = get_cb(row); + const bool removeColor = !cb.isColor(); + ntsc_to_rgb_monitor(row*AppleNTSC::H+350,AppleNTSC::H-350,rgb); + for (int col = 350; col < AppleNTSC::H-2; ++col) + { + int rgbv = rgb[col-350]; + if (removeColor && rgbv != 0) + { + rgbv = 0xFFFFFF; + } + this->image.setElem(ip,rgbv); + if (bleed_down) + this->image.setElem(ip+D_IP,rgbv); // display same pixel on next row + ++ip; + } + ip += D_IP; + } + delete [] rgb; } void AnalogTV::drawMonitorGreen() { - drawMonitorMonochrome(colors.c()[A2ColorsObserved::HIRES_GREEN]); + drawMonitorMonochrome(colors.c()[A2ColorsObserved::HIRES_GREEN]); } void AnalogTV::drawMonitorMonochrome(const unsigned int color) { - int ip = 0; - for (int row = 0; row < 192; ++row) - { - for (int col = 350; col < AppleNTSC::H-2; ++col) - { - const int is = row*AppleNTSC::H+col; - const unsigned int rgb = this->signal[is] > 50 ? color : 0; - this->image.setElem(ip,rgb); - if (bleed_down) - this->image.setElem(ip+D_IP,rgb); - ++ip; - } - ip += D_IP; - } + int ip = 0; + for (int row = 0; row < 192; ++row) + { + for (int col = 350; col < AppleNTSC::H-2; ++col) + { + const int is = row*AppleNTSC::H+col; + const unsigned int rgb = this->signal[is] > 50 ? color : 0; + this->image.setElem(ip,rgb); + if (bleed_down) + this->image.setElem(ip+D_IP,rgb); + ++ip; + } + ip += D_IP; + } } void AnalogTV::drawTVOld() { - int *yiq = new int[AppleNTSC::H]; - int ip = 0; - for (int row = 0; row < 192; ++row) - { - IQ iq_factor; + int *yiq = new int[AppleNTSC::H]; + int ip = 0; + for (int row = 0; row < 192; ++row) + { + IQ iq_factor; const CB cb = get_cb(row); iq_factor = get_iq_factor(cb); - ntsc_to_yiq(row*AppleNTSC::H+350,AppleNTSC::H-350,iq_factor,yiq); - for (int col = 350; col < AppleNTSC::H-2; ++col) - { - const int rgb = yiq2rgb(yiq[col-348]); // shift display left 1 pixel - this->image.setElem(ip,rgb); - if (bleed_down) - this->image.setElem(ip+D_IP,rgb); - ++ip; - } - ip += D_IP; - } - delete [] yiq; + ntsc_to_yiq(row*AppleNTSC::H+350,AppleNTSC::H-350,iq_factor,yiq); + for (int col = 350; col < AppleNTSC::H-2; ++col) + { + const int rgb = yiq2rgb(yiq[col-348]); // shift display left 1 pixel + this->image.setElem(ip,rgb); + if (bleed_down) + this->image.setElem(ip+D_IP,rgb); + ++ip; + } + ip += D_IP; + } + delete [] yiq; } void AnalogTV::drawBlank() { - this->image.blank(); + this->image.blank(); } @@ -334,47 +334,47 @@ void AnalogTV::drawBlank() void AnalogTV::ntsc_to_rgb_monitor(const int isignal, const int siglen, unsigned int rgb[]) { - int s0, s1, se; - s0 = s1 = isignal; - se = isignal+siglen; - while (s1 < se) - { - // no signal (black) - while (this->signal[s0] < 50 && s0signal[s0] < 50 && s0signal[s1] > 50 && s1= 4) - { - c = 0xFFFFFF; - } - else if (slen == 1) - { - if (this->signal[s0-2] > 50 && this->signal[s0+2] > 50) - c = 0xFFFFFF; - else - c = loresdarkcolor[s0 % 4]; - } - else if (slen == 2) - { - c = hirescolor[s0 % 4]; - } - else if (slen == 3) - { - c = loreslightcolor[s0 % 4]; - } - else - { - ++s1; - } + // signal (white, grey, or color) + s1 = s0; + while (this->signal[s1] > 50 && s1= 4) + { + c = 0xFFFFFF; + } + else if (slen == 1) + { + if (this->signal[s0-2] > 50 && this->signal[s0+2] > 50) + c = 0xFFFFFF; + else + c = loresdarkcolor[s0 % 4]; + } + else if (slen == 2) + { + c = hirescolor[s0 % 4]; + } + else if (slen == 3) + { + c = loreslightcolor[s0 % 4]; + } + else + { + ++s1; + } - for (int i = s0; i < s1; ++i) - rgb[i-isignal] = c; - s0 = s1; - } + for (int i = s0; i < s1; ++i) + rgb[i-isignal] = c; + s0 = s1; + } } @@ -386,12 +386,12 @@ void AnalogTV::ntsc_to_rgb_monitor(const int isignal, const int siglen, unsigned int *AnalogTV::rcb = new int[AppleNTSC::CB_END-AppleNTSC::CB_START-CB_EXTRA]; CB AnalogTV::get_cb(int lineno) { - const int isp = lineno * AppleNTSC::H; - for (int i = AppleNTSC::CB_START + CB_EXTRA/2; i < AppleNTSC::CB_END - CB_EXTRA/2; ++i) - { - this->rcb[i-(AppleNTSC::CB_START + CB_EXTRA/2)] = this->signal[isp + i]; - } - return CB(this->rcb); + const int isp = lineno * AppleNTSC::H; + for (int i = AppleNTSC::CB_START + CB_EXTRA/2; i < AppleNTSC::CB_END - CB_EXTRA/2; ++i) + { + this->rcb[i-(AppleNTSC::CB_START + CB_EXTRA/2)] = this->signal[isp + i]; + } + return CB(this->rcb); } @@ -406,35 +406,35 @@ const double AnalogTV::COLOR_THRESH(sqrt(2)); IQ AnalogTV::get_iq_factor(const CB& cb) { - std::map::iterator hit = cacheCB.find(cb); - if (hit != cacheCB.end()) - { - return hit->second; - } + std::map::iterator hit = cacheCB.find(cb); + if (hit != cacheCB.end()) + { + return hit->second; + } - double cb_phase[4]; - cb.getPhase(cb_phase); - const double cb_i = cb_phase[2]-cb_phase[0]; - const double cb_q = cb_phase[3]-cb_phase[1]; + double cb_phase[4]; + cb.getPhase(cb_phase); + const double cb_i = cb_phase[2]-cb_phase[0]; + const double cb_q = cb_phase[3]-cb_phase[1]; - if ((cb_i*cb_i) + (cb_q*cb_q) < COLOR_THRESH) - { - return BLACK_AND_WHITE; - } + if ((cb_i*cb_i) + (cb_q*cb_q) < COLOR_THRESH) + { + return BLACK_AND_WHITE; + } - double iq_factor[4]; + double iq_factor[4]; - iq_factor[0] = cb_i * TINT_I + cb_q * TINT_Q; - iq_factor[2] = -iq_factor[0]; - iq_factor[1] = cb_q * TINT_I - cb_i * TINT_Q; - iq_factor[3] = -iq_factor[1]; + iq_factor[0] = cb_i * TINT_I + cb_q * TINT_Q; + iq_factor[2] = -iq_factor[0]; + iq_factor[1] = cb_q * TINT_I - cb_i * TINT_Q; + iq_factor[3] = -iq_factor[1]; - const IQ iq(iq_factor); - if (!this->noise) - { - cacheCB[cb] = iq; - } - return iq; + const IQ iq(iq_factor); + if (!this->noise) + { + cacheCB[cb] = iq; + } + return iq; } const int AnalogTV::IQINTOFF(130); @@ -456,41 +456,41 @@ void AnalogTV::ntsc_to_yiq(const int isignal, const int siglen, const IQ& iq_fac int inline AnalogTV::yiq2rgb(const int yiq) { - double r = (((yiq)&0xFF)-IQINTOFF) + 0.956 * (((yiq>>8)&0xFF)-IQINTOFF) + 0.621 * (((yiq>>16)&0xFF)-IQINTOFF); - double g = (((yiq)&0xFF)-IQINTOFF) - 0.272 * (((yiq>>8)&0xFF)-IQINTOFF) - 0.647 * (((yiq>>16)&0xFF)-IQINTOFF); - double b = (((yiq)&0xFF)-IQINTOFF) - 1.105 * (((yiq>>8)&0xFF)-IQINTOFF) + 1.702 * (((yiq>>16)&0xFF)-IQINTOFF); + double r = (((yiq)&0xFF)-IQINTOFF) + 0.956 * (((yiq>>8)&0xFF)-IQINTOFF) + 0.621 * (((yiq>>16)&0xFF)-IQINTOFF); + double g = (((yiq)&0xFF)-IQINTOFF) - 0.272 * (((yiq>>8)&0xFF)-IQINTOFF) - 0.647 * (((yiq>>16)&0xFF)-IQINTOFF); + double b = (((yiq)&0xFF)-IQINTOFF) - 1.105 * (((yiq>>8)&0xFF)-IQINTOFF) + 1.702 * (((yiq>>16)&0xFF)-IQINTOFF); - const int rgb = - (calc_color(r) << 16)| - (calc_color(g) << 8)| - (calc_color(b) << 0); + const int rgb = + (calc_color(r) << 16)| + (calc_color(g) << 8)| + (calc_color(b) << 0); - return rgb; + return rgb; } int inline AnalogTV::color2bw(const int rgb) { - const int y = rgb2y(rgb); - return y<<16 | y<<8 | y; + const int y = rgb2y(rgb); + return y<<16 | y<<8 | y; } int inline AnalogTV::rgb2y(const int rgb) // y in range 0-255 { - return (int)((0.299*((rgb>>16)&0xFF) + 0.587*((rgb>>8)&0xFF) + 0.114*((rgb)&0xFF))/1.04); + return (int)((0.299*((rgb>>16)&0xFF) + 0.587*((rgb>>8)&0xFF) + 0.114*((rgb)&0xFF))/1.04); } int inline AnalogTV::calc_color(const double color) { - int x = (int)(color * 0x100 / AppleNTSC::LEVEL_RANGE + .5); - x = clamp(0,x,0x100); - return x & 0xFF; + int x = (int)(color * 0x100 / AppleNTSC::LEVEL_RANGE + .5); + x = clamp(0,x,0x100); + return x & 0xFF; } int inline AnalogTV::clamp(int min, int x, int lim) { - if (x < min) - return min; - if (lim <= x) - return lim-1; - return x; + if (x < min) + return min; + if (lim <= x) + return lim-1; + return x; } diff --git a/src/analogtv.h b/src/analogtv.h index e8188b9..5032661 100644 --- a/src/analogtv.h +++ b/src/analogtv.h @@ -32,75 +32,75 @@ class CB; class AnalogTV { public: - enum DisplayType - { - TV_OLD_COLOR, - MONITOR_COLOR, - MONITOR_GREEN, + enum DisplayType + { + TV_OLD_COLOR, + MONITOR_COLOR, + MONITOR_GREEN, - NUM_DISPLAY_TYPES - }; + NUM_DISPLAY_TYPES + }; private: - ScreenImage& image; + ScreenImage& image; - bool on; - bool noise; - DisplayType type; - bool bleed_down; + bool on; + bool noise; + DisplayType type; + bool bleed_down; - static int* rcb; + static int* rcb; - A2ColorsObserved colors; - std::vector hirescolor; - std::vector loreslightcolor; - std::vector loresdarkcolor; + A2ColorsObserved colors; + std::vector hirescolor; + std::vector loreslightcolor; + std::vector loresdarkcolor; - static const int IQINTOFF; - static const double IQ_OFFSET_DEGREES; - static const double IQ_OFFSET_RADIANS; - static const double TINT_I; - static const double TINT_Q; - static const double COLOR_THRESH; - static const IQ& BLACK_AND_WHITE; + static const int IQINTOFF; + static const double IQ_OFFSET_DEGREES; + static const double IQ_OFFSET_RADIANS; + static const double TINT_I; + static const double TINT_Q; + static const double COLOR_THRESH; + static const IQ& BLACK_AND_WHITE; - void drawMonitorColor(); - void drawMonitorWhite(); - void drawMonitorGreen(); - void drawMonitorOrange(); - void drawMonitorMonochrome(const unsigned int color); - void drawTVOld(); - void drawTVNew(); - void drawBlank(); - void ntsc_to_rgb_monitor(const int isignal, const int siglen, unsigned int rgb[]); - void ntsc_to_rgb_newtv(const int isignal, const int siglen, unsigned int rgb[]); - CB get_cb(int lineno); - IQ get_iq_factor(const CB& cb); - void ntsc_to_yiq(const int isignal, const int siglen, const IQ& iq_factor, int yiq[]); - static int yiq2rgb(const int yiq); - static int color2bw(const int rgb); - static int rgb2y(const int rgb); // ;y in range 0-255 - static int calc_color(const double color); - static int clamp(int min, int x, int lim); + void drawMonitorColor(); + void drawMonitorWhite(); + void drawMonitorGreen(); + void drawMonitorOrange(); + void drawMonitorMonochrome(const unsigned int color); + void drawTVOld(); + void drawTVNew(); + void drawBlank(); + void ntsc_to_rgb_monitor(const int isignal, const int siglen, unsigned int rgb[]); + void ntsc_to_rgb_newtv(const int isignal, const int siglen, unsigned int rgb[]); + CB get_cb(int lineno); + IQ get_iq_factor(const CB& cb); + void ntsc_to_yiq(const int isignal, const int siglen, const IQ& iq_factor, int yiq[]); + static int yiq2rgb(const int yiq); + static int color2bw(const int rgb); + static int rgb2y(const int rgb); // ;y in range 0-255 + static int calc_color(const double color); + static int clamp(int min, int x, int lim); public: - void drawCurrent(); - signed char* signal; + void drawCurrent(); + signed char* signal; - AnalogTV(ScreenImage& image); - ~AnalogTV(); + AnalogTV(ScreenImage& image); + ~AnalogTV(); - bool isOn() const - { - return this->on; - } + bool isOn() const + { + return this->on; + } - void powerOn(bool b); - void toggleBleedDown(); - void restartSignal(); - void setType(DisplayType type); - void cycleType(); - void setNoise(bool noise) { this->noise = noise; } + void powerOn(bool b); + void toggleBleedDown(); + void restartSignal(); + void setType(DisplayType type); + void cycleType(); + void setNoise(bool noise) { this->noise = noise; } }; #endif diff --git a/src/apple2.cpp b/src/apple2.cpp index 8275528..5cb34f6 100644 --- a/src/apple2.cpp +++ b/src/apple2.cpp @@ -39,23 +39,23 @@ #include Apple2::Apple2(KeypressQueue& keypresses, PaddleButtonStates& paddleButtonStates, AnalogTV& tv, HyperMode& fhyper, KeyboardBufferMode& buffered, ScreenImage& gui): - slts(gui), - kbd(keypresses,fhyper,buffered), - rom(AddressBus::MOTHERBOARD_ROM_SIZ), + slts(gui), + kbd(keypresses,fhyper,buffered), + rom(AddressBus::MOTHERBOARD_ROM_SIZ), ram(revision), cassetteIn(gui), cassetteOut(gui), addressBus(gui,revision,ram,rom,kbd,videoMode,paddles,paddleButtonStates,speaker,cassetteIn,cassetteOut,slts), picgen(tv,videoMode,revision), - video(videoMode,addressBus,picgen,textRows), + video(videoMode,addressBus,picgen,textRows), #ifdef USE_EMU transistors("transistors"), cpu(transistors,addressBus), #else - cpu(addressBus), + cpu(addressBus), #endif - powerUpReset(*this), - revision(1) + powerUpReset(*this), + revision(1) { } @@ -80,21 +80,21 @@ void Apple2::tick() { void Apple2::powerOn() { - this->ram.powerOn(); - this->cpu.powerOn(); - this->videoMode.powerOn(); - this->video.powerOn(); - this->picgen.powerOn(); - this->powerUpReset.powerOn(); + this->ram.powerOn(); + this->cpu.powerOn(); + this->videoMode.powerOn(); + this->video.powerOn(); + this->picgen.powerOn(); + this->powerUpReset.powerOn(); } void Apple2::powerOff() { - this->ram.powerOff(); + this->ram.powerOff(); } void Apple2::reset() { - this->cpu.reset(); - this->slts.reset(); + this->cpu.reset(); + this->slts.reset(); } diff --git a/src/apple2.h b/src/apple2.h index ff42c16..33b2596 100644 --- a/src/apple2.h +++ b/src/apple2.h @@ -46,39 +46,39 @@ class ScreenImage; class Apple2 : public Timable { - Slots slts; - VideoMode videoMode; - Keyboard kbd; - Paddles paddles; - SpeakerClicker speaker; - Memory rom; + Slots slts; + VideoMode videoMode; + Keyboard kbd; + Paddles paddles; + SpeakerClicker speaker; + Memory rom; MemoryRandomAccess ram; CassetteIn cassetteIn; CassetteOut cassetteOut; AddressBus addressBus; - PictureGenerator picgen; - TextCharacters textRows; - Video video; + PictureGenerator picgen; + TextCharacters textRows; + Video video; #ifdef USE_EMU std::ifstream transistors; Emu6502 cpu; #else - CPU cpu; + CPU cpu; #endif - PowerUpReset powerUpReset; - int revision; + PowerUpReset powerUpReset; + int revision; public: - Apple2(KeypressQueue& keypresses, PaddleButtonStates& paddleButtonStates, AnalogTV& tv, HyperMode& fhyper, KeyboardBufferMode& buffered, ScreenImage& gui); - ~Apple2(); + Apple2(KeypressQueue& keypresses, PaddleButtonStates& paddleButtonStates, AnalogTV& tv, HyperMode& fhyper, KeyboardBufferMode& buffered, ScreenImage& gui); + ~Apple2(); - void powerOn(); - void powerOff(); - void reset(); + void powerOn(); + void powerOff(); + void reset(); - virtual void tick(); + virtual void tick(); - friend class Emulator; + friend class Emulator; }; #endif diff --git a/src/applentsc.h b/src/applentsc.h index c4f1302..f66cef9 100644 --- a/src/applentsc.h +++ b/src/applentsc.h @@ -21,31 +21,31 @@ class AppleNTSC { private: - AppleNTSC() {} + AppleNTSC() {} public: - enum { V = 262, H = (25+40)*14+2 }; - enum { SIGNAL_LEN = V*H }; + enum { V = 262, H = (25+40)*14+2 }; + enum { SIGNAL_LEN = V*H }; - enum - { - FP_START = 0, - SYNC_START = FP_START+126, - BP_START = SYNC_START+112, - CB_START = BP_START+0, - CB_END = CB_START+56, - SPIKE = CB_END+34, - PIC_START = CB_END+56 - }; + enum + { + FP_START = 0, + SYNC_START = FP_START+126, + BP_START = SYNC_START+112, + CB_START = BP_START+0, + CB_END = CB_START+56, + SPIKE = CB_END+34, + PIC_START = CB_END+56 + }; - enum - { - WHITE_LEVEL = 100, - BLANK_LEVEL = 0, - SYNC_LEVEL = -40, - CB_LEVEL = 20, - LEVEL_RANGE = WHITE_LEVEL-SYNC_LEVEL - }; + enum + { + WHITE_LEVEL = 100, + BLANK_LEVEL = 0, + SYNC_LEVEL = -40, + CB_LEVEL = 20, + LEVEL_RANGE = WHITE_LEVEL-SYNC_LEVEL + }; }; #endif diff --git a/src/card.cpp b/src/card.cpp index 1f5f70f..46463aa 100644 --- a/src/card.cpp +++ b/src/card.cpp @@ -19,8 +19,8 @@ #include "configep2.h" Card::Card(): - rom(0x0100), - seventhRom(0x0800) + rom(0x0100), + seventhRom(0x0800) { } @@ -41,44 +41,44 @@ void Card::tick() unsigned char Card::io(const unsigned short /*address*/, const unsigned char data, const bool /*writing*/) { - return data; + return data; } unsigned char Card::readRom(const unsigned short address, const unsigned char data) { - this->activeSeventhRom = true; + this->activeSeventhRom = true; return this->rom.read(address, data); } void Card::readSeventhRom(const unsigned short address, unsigned char* const pb) { - if (address == 0x7FF) - { - this->activeSeventhRom = false; - } - else if (this->activeSeventhRom && hasSeventhRom()) - { + if (address == 0x7FF) + { + this->activeSeventhRom = false; + } + else if (this->activeSeventhRom && hasSeventhRom()) + { *pb = this->seventhRom.read(address, *pb); - } + } } void Card::loadRom(const unsigned short base, std::istream& in) { - this->rom.load(base,in); + this->rom.load(base,in); } void Card::loadSeventhRom(const unsigned short base, std::istream& in) { - this->seventhRom.load(base,in); + this->seventhRom.load(base,in); } bool Card::inhibitMotherboardRom() { - return false; + return false; } @@ -91,17 +91,17 @@ void Card::ioBankRom(const unsigned short /*addr*/, unsigned char* const /*pb*/, void Card::loadBankRom(const unsigned short /*base*/, std::istream& /*in*/) { - throw ConfigException("This card has no $D000 ROM"); + throw ConfigException("This card has no $D000 ROM"); } std::string Card::getName() { - return ""; + return ""; } bool Card::isDirty() { - return false; + return false; } void Card::save(int unit) diff --git a/src/card.h b/src/card.h index 62e6fb5..004602a 100644 --- a/src/card.h +++ b/src/card.h @@ -26,28 +26,28 @@ class Card { private: - bool activeSeventhRom; + bool activeSeventhRom; protected: - Memory rom; - Memory seventhRom; + Memory rom; + Memory seventhRom; public: - Card(); - virtual ~Card(); + Card(); + virtual ~Card(); virtual void tick(); virtual void reset(); - virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); - virtual unsigned char readRom(const unsigned short address, const unsigned char data); - virtual bool hasSeventhRom() { return false; } - virtual void readSeventhRom(const unsigned short address, unsigned char* const pb); - virtual void loadRom(const unsigned short base, std::istream& in); - virtual void loadSeventhRom(const unsigned short base, std::istream& in); - virtual bool inhibitMotherboardRom(); - virtual void ioBankRom(const unsigned short addr, unsigned char* const pb, const bool write); - virtual void loadBankRom(const unsigned short base, std::istream& in); - virtual bool isDirty(); + virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); + virtual unsigned char readRom(const unsigned short address, const unsigned char data); + virtual bool hasSeventhRom() { return false; } + virtual void readSeventhRom(const unsigned short address, unsigned char* const pb); + virtual void loadRom(const unsigned short base, std::istream& in); + virtual void loadSeventhRom(const unsigned short base, std::istream& in); + virtual bool inhibitMotherboardRom(); + virtual void ioBankRom(const unsigned short addr, unsigned char* const pb, const bool write); + virtual void loadBankRom(const unsigned short base, std::istream& in); + virtual bool isDirty(); virtual void save(int unit); - virtual std::string getName(); + virtual std::string getName(); }; #endif diff --git a/src/cassettein.cpp b/src/cassettein.cpp index 874776a..f989da8 100644 --- a/src/cassettein.cpp +++ b/src/cassettein.cpp @@ -233,7 +233,7 @@ bool CassetteIn::load(const std::string& filePath) { if (!eject()) { return false; } - } + } /* convert input sample to floating-point, at rate of 10 CPU cycles per sample, for easy calculation */ SDL_AudioCVT cvt; @@ -256,7 +256,7 @@ bool CassetteIn::load(const std::string& filePath) { rewind(); tone(); - return true; + return true; } bool CassetteIn::eject() { diff --git a/src/clipboardhandler.cpp b/src/clipboardhandler.cpp index 5c4e991..c9bc1d4 100644 --- a/src/clipboardhandler.cpp +++ b/src/clipboardhandler.cpp @@ -30,9 +30,9 @@ ClipboardHandler::~ClipboardHandler() std::string ClipboardHandler::getText() { - std::string ret; + std::string ret; char* sdlAllocatedText = SDL_GetClipboardText(); ret.assign(sdlAllocatedText); SDL_free(sdlAllocatedText); - return ret; + return ret; } diff --git a/src/clipboardhandler.h b/src/clipboardhandler.h index f2d282f..65fa516 100644 --- a/src/clipboardhandler.h +++ b/src/clipboardhandler.h @@ -24,10 +24,10 @@ class ClipboardHandler { public: - ClipboardHandler(); - ~ClipboardHandler(); + ClipboardHandler(); + ~ClipboardHandler(); - std::string getText(); + std::string getText(); }; #endif diff --git a/src/clockcard.cpp b/src/clockcard.cpp index c28275e..faa8482 100644 --- a/src/clockcard.cpp +++ b/src/clockcard.cpp @@ -19,8 +19,8 @@ #include ClockCard::ClockCard(): - latch(0), - pos(0) + latch(0), + pos(0) { } @@ -33,38 +33,38 @@ ClockCard::~ClockCard() unsigned char ClockCard::io(const unsigned short address, const unsigned char data, const bool writing) { - const int sw = address & 0x0F; - if (sw == 0) - { - if (!(this->latch & 0x80)) - { - if (this->pos == 0) - { - getTime(); - } - char c = this->time[this->pos]; - this->latch = (unsigned char)(c | 0x80); - ++this->pos; - if (this->pos >= this->timelen) - { - this->pos = 0; - } - } - } - else if (sw == 1) - { - this->latch &= 0x7F; - } - return this->latch; + const int sw = address & 0x0F; + if (sw == 0) + { + if (!(this->latch & 0x80)) + { + if (this->pos == 0) + { + getTime(); + } + char c = this->time[this->pos]; + this->latch = (unsigned char)(c | 0x80); + ++this->pos; + if (this->pos >= this->timelen) + { + this->pos = 0; + } + } + } + else if (sw == 1) + { + this->latch &= 0x7F; + } + return this->latch; } #define TIMEFORMAT "%m,0%w,%d,%H,%M,%S,000,%Y,%Z,D\r" void ClockCard::getTime() { - time_t now; - ::time(&now); - struct tm* nowtm = ::localtime(&now); - this->timelen = ::strftime(this->time,sizeof(this->time),TIMEFORMAT,nowtm); - this->time[this->timelen-2] = nowtm->tm_isdst>0 ? '1' : '0'; + time_t now; + ::time(&now); + struct tm* nowtm = ::localtime(&now); + this->timelen = ::strftime(this->time,sizeof(this->time),TIMEFORMAT,nowtm); + this->time[this->timelen-2] = nowtm->tm_isdst>0 ? '1' : '0'; } diff --git a/src/clockcard.h b/src/clockcard.h index 37dc1da..c21fd5a 100644 --- a/src/clockcard.h +++ b/src/clockcard.h @@ -24,19 +24,19 @@ class ClockCard : public Card { private: - unsigned char latch; - unsigned int pos; - char time[64]; - size_t timelen; + unsigned char latch; + unsigned int pos; + char time[64]; + size_t timelen; - void getTime(); + void getTime(); public: - ClockCard(); - ~ClockCard(); + ClockCard(); + ~ClockCard(); - virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); - virtual std::string getName() { return "clock"; } + virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); + virtual std::string getName() { return "clock"; } }; #endif diff --git a/src/cpu.cpp b/src/cpu.cpp index acdfcbc..5387450 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -24,7 +24,7 @@ CPU::CPU(AddressBus& addressBus): - addressBus(addressBus) + addressBus(addressBus) { } @@ -34,102 +34,102 @@ CPU::~CPU() void CPU::powerOn() { - this->started = false; - this->pendingReset = false; - this->pendingIRQ = false; - this->pendingNMI = false; - this->p = PMASK_M; + this->started = false; + this->pendingReset = false; + this->pendingIRQ = false; + this->pendingNMI = false; + this->p = PMASK_M; } void CPU::reset() { - this->started = true; - this->pendingReset = true; - this->t = 0; + this->started = true; + this->pendingReset = true; + this->t = 0; } void CPU::IRQ() { - this->pendingIRQ = true; + this->pendingIRQ = true; } void CPU::NMI() { - this->pendingNMI = true; + this->pendingNMI = true; } void CPU::tick() { - if (!this->started) - { - return; - } - if (!this->t) - { - firstCycle(); - } - else - { - subsequentCycle(); - } - ++this->t; + if (!this->started) + { + return; + } + if (!this->t) + { + firstCycle(); + } + else + { + subsequentCycle(); + } + ++this->t; } void CPU::firstCycle() { - const bool interrupt = this->pendingNMI || this->pendingReset || (!(this->p & PMASK_I) && this->pendingIRQ); + const bool interrupt = this->pendingNMI || this->pendingReset || (!(this->p & PMASK_I) && this->pendingIRQ); - if (interrupt) - { - this->pc = getInterruptAddress(); - } + if (interrupt) + { + this->pc = getInterruptAddress(); + } - this->address = this->pc++; + this->address = this->pc++; - read(); + read(); - if (interrupt) - { - this->opcode = getInterruptPseudoOpCode(); - } - else - { - this->opcode = this->data; - } + if (interrupt) + { + this->opcode = getInterruptPseudoOpCode(); + } + else + { + this->opcode = this->data; + } } int CPU::getInterruptAddress() { - if (this->pendingNMI) - { - return NMI_VECTOR-2; - } - if (this->pendingReset) - { - return RESET_VECTOR-2; - } - if (!(this->p & PMASK_I) && this->pendingIRQ) - { - return IRQ_VECTOR-2; - } - return 0; // can't happen + if (this->pendingNMI) + { + return NMI_VECTOR-2; + } + if (this->pendingReset) + { + return RESET_VECTOR-2; + } + if (!(this->p & PMASK_I) && this->pendingIRQ) + { + return IRQ_VECTOR-2; + } + return 0; // can't happen } int CPU::getInterruptPseudoOpCode() { - if (this->pendingNMI) - { - return 0x100; - } - if (this->pendingReset) - { - return 0x101; - } - if (!(this->p & PMASK_I) && this->pendingIRQ) - { - return 0x102; - } - return 0; // can't happen + if (this->pendingNMI) + { + return 0x100; + } + if (this->pendingReset) + { + return 0x101; + } + if (!(this->p & PMASK_I) && this->pendingIRQ) + { + return 0x102; + } + return 0; // can't happen } void (CPU::*CPU::addr[])() = @@ -657,1063 +657,1063 @@ void (CPU::*CPU::exec[])() = void CPU::subsequentCycle() { - (this->*this->addr[this->opcode])(); + (this->*this->addr[this->opcode])(); } void CPU::addr_SINGLE() { - switch (this->t) - { - case 1: - address = pc; - read(); // discard - execute(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc; + read(); // discard + execute(); + done(); + break; + } } void CPU::addr_INTERNAL_IMMEDIATE() { - switch (this->t) - { - case 1: - address = pc++; - read(); - execute(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + execute(); + done(); + break; + } } void CPU::addr_INTERNAL_ZERO_PAGE() { - switch (this->t) - { - case 1: - address = pc++; - read(); - adl = data; - break; - case 2: - adh = 0; - address = ad(); - read(); - execute(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + adl = data; + break; + case 2: + adh = 0; + address = ad(); + read(); + execute(); + done(); + break; + } } void CPU::addr_INTERNAL_ABSOLUTE() { - switch (this->t) - { - case 1: - address = pc++; - read(); - adl = data; - break; - case 2: - address = pc++; - read(); - adh = data; - break; - case 3: - address = ad(); - read(); - execute(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + adl = data; + break; + case 2: + address = pc++; + read(); + adh = data; + break; + case 3: + address = ad(); + read(); + execute(); + done(); + break; + } } void CPU::addr_INTERNAL_INDIRECT_X() { - switch (this->t) - { - case 1: - address = pc++; - read(); - bal = data; - break; - case 2: - address = bal; - read(); // discard - break; - case 3: - address += x; - address &= 0xFF; - read(); - adl = data; - break; - case 4: - ++address; - address &= 0xFF; - read(); - adh = data; - break; - case 5: - address = ad(); - read(); - execute(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + bal = data; + break; + case 2: + address = bal; + read(); // discard + break; + case 3: + address += x; + address &= 0xFF; + read(); + adl = data; + break; + case 4: + ++address; + address &= 0xFF; + read(); + adh = data; + break; + case 5: + address = ad(); + read(); + execute(); + done(); + break; + } } void CPU::addr_INTERNAL_ABSOLUTE_XY() { - switch (this->t) - { - case 1: - address = pc++; - read(); - bal = data; - break; - case 2: - address = pc++; - read(); - bah = data; - break; - case 3: - idx = getIndex(); - wc = ((unsigned short)bal + (unsigned short)idx) >= 0x100; - bal += idx; - address = ba(); - read(); - if (!wc) - { - execute(); - done(); - } - break; - case 4: - ++bah; - address = ba(); - read(); - execute(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + bal = data; + break; + case 2: + address = pc++; + read(); + bah = data; + break; + case 3: + idx = getIndex(); + wc = ((unsigned short)bal + (unsigned short)idx) >= 0x100; + bal += idx; + address = ba(); + read(); + if (!wc) + { + execute(); + done(); + } + break; + case 4: + ++bah; + address = ba(); + read(); + execute(); + done(); + break; + } } void CPU::addr_INTERNAL_ZERO_PAGE_XY() { - switch (this->t) - { - case 1: - address = pc++; - read(); - bal = data; - break; - case 2: - bah = 0; - address = ba(); - read(); // discard - break; - case 3: - idx = getIndex(); - bal += idx; // doesn't leave page zero - address = ba(); - read(); - execute(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + bal = data; + break; + case 2: + bah = 0; + address = ba(); + read(); // discard + break; + case 3: + idx = getIndex(); + bal += idx; // doesn't leave page zero + address = ba(); + read(); + execute(); + done(); + break; + } } void CPU::addr_INTERNAL_INDIRECT_Y() { - switch (this->t) - { - case 1: - address = pc++; - read(); - ial = data; - break; - case 2: - address = ial; - read(); - bal = data; - break; - case 3: - ++address; - address &= 0xFF; // doesn't leave page zero - read(); - bah = data; - break; - case 4: - wc = ((unsigned short)bal + (unsigned short)y) >= 0x100; - bal += y; - address = ba(); - read(); - if (!wc) - { - execute(); - done(); - } - break; - case 5: - ++bah; - address = ba(); - read(); - execute(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + ial = data; + break; + case 2: + address = ial; + read(); + bal = data; + break; + case 3: + ++address; + address &= 0xFF; // doesn't leave page zero + read(); + bah = data; + break; + case 4: + wc = ((unsigned short)bal + (unsigned short)y) >= 0x100; + bal += y; + address = ba(); + read(); + if (!wc) + { + execute(); + done(); + } + break; + case 5: + ++bah; + address = ba(); + read(); + execute(); + done(); + break; + } } void CPU::addr_STORE_ZERO_PAGE() { - switch (this->t) - { - case 1: - address = pc++; - read(); - adl = data; - execute(); - break; - case 2: - adh = 0; - address = ad(); - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + adl = data; + execute(); + break; + case 2: + adh = 0; + address = ad(); + write(); + done(); + break; + } } void CPU::addr_STORE_ABSOLUTE() { - switch (this->t) - { - case 1: - address = pc++; - read(); - adl = data; - break; - case 2: - address = pc++; - read(); - adh = data; - execute(); - break; - case 3: - address = ad(); - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + adl = data; + break; + case 2: + address = pc++; + read(); + adh = data; + execute(); + break; + case 3: + address = ad(); + write(); + done(); + break; + } } void CPU::addr_STORE_INDIRECT_X() { - switch (this->t) - { - case 1: - address = pc++; - read(); - bal = data; - break; - case 2: - address = bal; - address &= 0xFF; - read(); // discard - break; - case 3: - address += x; - address &= 0xFF; - read(); - adl = data; - break; - case 4: - ++address; - address &= 0xFF; - read(); - adh = data; - execute(); - break; - case 5: - address = ad(); - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + bal = data; + break; + case 2: + address = bal; + address &= 0xFF; + read(); // discard + break; + case 3: + address += x; + address &= 0xFF; + read(); + adl = data; + break; + case 4: + ++address; + address &= 0xFF; + read(); + adh = data; + execute(); + break; + case 5: + address = ad(); + write(); + done(); + break; + } } void CPU::addr_STORE_ABSOLUTE_XY() { - switch (this->t) - { - case 1: - address = pc++; - read(); - bal = data; - break; - case 2: - address = pc++; - read(); - bah = data; - break; - case 3: - idx = getIndex(); - address = ba(); - address += idx; - read(); // discard (assume this is the right address, manual is ambiguous) - execute(); - break; - case 4: - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + bal = data; + break; + case 2: + address = pc++; + read(); + bah = data; + break; + case 3: + idx = getIndex(); + address = ba(); + address += idx; + read(); // discard (assume this is the right address, manual is ambiguous) + execute(); + break; + case 4: + write(); + done(); + break; + } } void CPU::addr_STORE_ZERO_PAGE_XY() { - switch (this->t) - { - case 1: - address = pc++; - read(); - bal = data; - break; - case 2: - bah = 0; - address = ba(); - read(); // discard - execute(); - break; - case 3: - idx = getIndex(); - bal += idx; // doesn't leave page zero - address = ba(); - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + bal = data; + break; + case 2: + bah = 0; + address = ba(); + read(); // discard + execute(); + break; + case 3: + idx = getIndex(); + bal += idx; // doesn't leave page zero + address = ba(); + write(); + done(); + break; + } } void CPU::addr_STORE_INDIRECT_Y() { - switch (this->t) - { - case 1: - address = pc++; - read(); - ial = data; - break; - case 2: - address = ial; - read(); - bal = data; - break; - case 3: - ++address; - address &= 0xFF; - read(); - bah = data; - break; - case 4: - address = ba(); - address += y; - read(); // discard - execute(); - break; - case 5: - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + ial = data; + break; + case 2: + address = ial; + read(); + bal = data; + break; + case 3: + ++address; + address &= 0xFF; + read(); + bah = data; + break; + case 4: + address = ba(); + address += y; + read(); // discard + execute(); + break; + case 5: + write(); + done(); + break; + } } void CPU::addr_RMW_ZERO_PAGE() { - switch (this->t) - { - case 1: - address = pc++; - read(); - adl = data; - break; - case 2: - adh = 0; - address = ad(); - read(); - break; - case 3: - write(); - execute(); - break; - case 4: - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + adl = data; + break; + case 2: + adh = 0; + address = ad(); + read(); + break; + case 3: + write(); + execute(); + break; + case 4: + write(); + done(); + break; + } } void CPU::addr_RMW_ABSOLUTE() { - switch (this->t) - { - case 1: - address = pc++; - read(); - adl = data; - break; - case 2: - address = pc++; - read(); - adh = data; - break; - case 3: - address = ad(); - read(); - break; - case 4: - write(); - execute(); - break; - case 5: - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + adl = data; + break; + case 2: + address = pc++; + read(); + adh = data; + break; + case 3: + address = ad(); + read(); + break; + case 4: + write(); + execute(); + break; + case 5: + write(); + done(); + break; + } } void CPU::addr_RMW_ZERO_PAGE_X() { - switch (this->t) - { - case 1: - address = pc++; - read(); - bal = data; - break; - case 2: - bah = 0; - address = ba(); - read(); // discard - break; - case 3: - bal += x; // doesn't leave page zero - address = ba(); - read(); - break; - case 4: - write(); - execute(); - break; - case 5: - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + bal = data; + break; + case 2: + bah = 0; + address = ba(); + read(); // discard + break; + case 3: + bal += x; // doesn't leave page zero + address = ba(); + read(); + break; + case 4: + write(); + execute(); + break; + case 5: + write(); + done(); + break; + } } void CPU::addr_RMW_ABSOLUTE_X() { - switch (this->t) - { - case 1: - address = pc++; - read(); - bal = data; - break; - case 2: - address = pc++; - read(); - bah = data; - break; - case 3: - address = ba(); - address += x; - read(); // discard - break; - case 4: - read(); - break; - case 5: - write(); - execute(); - break; - case 6: - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + bal = data; + break; + case 2: + address = pc++; + read(); + bah = data; + break; + case 3: + address = ba(); + address += x; + read(); // discard + break; + case 4: + read(); + break; + case 5: + write(); + execute(); + break; + case 6: + write(); + done(); + break; + } } void CPU::addr_MISC_PUSH() { - switch (this->t) - { - case 1: - address = pc; - read(); // discard - execute(); - break; - case 2: - address = push(); - write(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc; + read(); // discard + execute(); + break; + case 2: + address = push(); + write(); + done(); + break; + } } void CPU::addr_MISC_PULL() { - switch (this->t) - { - case 1: - address = pc; - read(); // discard - break; - case 2: - address = sp(); - read(); // discard - break; - case 3: - address = pull(); - read(); - execute(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc; + read(); // discard + break; + case 2: + address = sp(); + read(); // discard + break; + case 3: + address = pull(); + read(); + execute(); + done(); + break; + } } void CPU::addr_MISC_JSR() { - switch (this->t) - { - case 1: - address = pc++; - read(); - adl = data; - break; - case 2: - address = push(); - read(); // discard - break; - case 3: - data = pch(); - write(); - address = push(); - break; - case 4: - data = pcl(); - write(); - break; - case 5: - address = pc; - read(); - adh = data; - pc = ad(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + adl = data; + break; + case 2: + address = push(); + read(); // discard + break; + case 3: + data = pch(); + write(); + address = push(); + break; + case 4: + data = pcl(); + write(); + break; + case 5: + address = pc; + read(); + adh = data; + pc = ad(); + done(); + break; + } } void CPU::addr_MISC_BREAK() { - switch (this->t) - { - case 1: - address = pc; - read(); // discard - break; - case 2: - address = push(); - data = pch(); - write(); - break; - case 3: - address = push(); - data = pcl(); - write(); - break; - case 4: - address = push(); - p |= PMASK_B; - data = p; - write(); - break; - case 5: - address = IRQ_VECTOR; - read(); - adl = data; - break; - case 6: - ++address; - read(); - adh = data; - pc = ad(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc; + read(); // discard + break; + case 2: + address = push(); + data = pch(); + write(); + break; + case 3: + address = push(); + data = pcl(); + write(); + break; + case 4: + address = push(); + p |= PMASK_B; + data = p; + write(); + break; + case 5: + address = IRQ_VECTOR; + read(); + adl = data; + break; + case 6: + ++address; + read(); + adh = data; + pc = ad(); + done(); + break; + } } void CPU::addr_MISC_RTI() { - switch (this->t) - { - case 1: - address = pc; - read(); // discard - break; - case 2: - address = sp(); - read(); // discard - break; - case 3: - address = pull(); - read(); - p = data; p |= PMASK_M; - break; - case 4: - address = pull(); - read(); - adl = data; - break; - case 5: - address = pull(); - read(); - adh = data; - pc = ad(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc; + read(); // discard + break; + case 2: + address = sp(); + read(); // discard + break; + case 3: + address = pull(); + read(); + p = data; p |= PMASK_M; + break; + case 4: + address = pull(); + read(); + adl = data; + break; + case 5: + address = pull(); + read(); + adh = data; + pc = ad(); + done(); + break; + } } void CPU::addr_JMP_ABSOLUTE() { - switch (this->t) - { - case 1: - address = pc++; - read(); - adl = data; - break; - case 2: - address = pc; - read(); - adh = data; - pc = ad(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + adl = data; + break; + case 2: + address = pc; + read(); + adh = data; + pc = ad(); + done(); + break; + } } void CPU::addr_JMP_INDIRECT() { - switch (this->t) - { - case 1: - address = pc++; - read(); - ial = data; - break; - case 2: - address = pc; - read(); - iah = data; - break; - case 3: - address = ia(); - read(); - adl = data; - break; - case 4: - /* Interactive [newsletter] (Rockwell Intl., 1980), Issue 2, - currently available here: - http://www.6502.org/documents/publications/interactive/aim_interactive_2.pdf - on page 12, documents a bug where JMP absolute does NOT - leave the page. This causes problems for - example for JMP ($08FF), where high byte - is read from $800 instead of $900. - */ - ++ial; // emulate the bug here (don't touch iah) - address = ia(); - read(); - adh = data; - pc = ad(); - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); + ial = data; + break; + case 2: + address = pc; + read(); + iah = data; + break; + case 3: + address = ia(); + read(); + adl = data; + break; + case 4: + /* Interactive [newsletter] (Rockwell Intl., 1980), Issue 2, + currently available here: + http://www.6502.org/documents/publications/interactive/aim_interactive_2.pdf + on page 12, documents a bug where JMP absolute does NOT + leave the page. This causes problems for + example for JMP ($08FF), where high byte + is read from $800 instead of $900. + */ + ++ial; // emulate the bug here (don't touch iah) + address = ia(); + read(); + adh = data; + pc = ad(); + done(); + break; + } } void CPU::addr_RTS() { - switch (this->t) - { - case 1: - address = pc; - read(); // discard - break; - case 2: - address = sp(); - read(); // discard - break; - case 3: - address = pull(); - read(); - adl = data; - break; - case 4: - address = pull(); - read(); - adh = data; - break; - case 5: - pc = ad(); - address = pc; - read(); // discard - ++pc; - done(); - break; - } + switch (this->t) + { + case 1: + address = pc; + read(); // discard + break; + case 2: + address = sp(); + read(); // discard + break; + case 3: + address = pull(); + read(); + adl = data; + break; + case 4: + address = pull(); + read(); + adh = data; + break; + case 5: + pc = ad(); + address = pc; + read(); // discard + ++pc; + done(); + break; + } } void CPU::addr_BRANCH() { - signed short lo; - switch (this->t) - { - case 1: - address = pc++; - read(); - offset = (signed char)data; - execute(); - if (!branch) - { - done(); - } - break; - case 2: - lo = pcl()+offset; - sc = 0; - if (lo < 0) - { - lo += 0x100; - sc = -1; - } - else if (lo >= 0x100) - { - lo -= 0x100; - sc = 1; - } - pc = (pc & 0xFF00) | lo; - address = pc; - read(); - if (sc == 0) - { - done(); - } - break; - case 3: - unsigned short hi = pch() + sc; - pc = (hi << 8) | pcl(); - read(); - done(); - break; - } + signed short lo; + switch (this->t) + { + case 1: + address = pc++; + read(); + offset = (signed char)data; + execute(); + if (!branch) + { + done(); + } + break; + case 2: + lo = pcl()+offset; + sc = 0; + if (lo < 0) + { + lo += 0x100; + sc = -1; + } + else if (lo >= 0x100) + { + lo -= 0x100; + sc = 1; + } + pc = (pc & 0xFF00) | lo; + address = pc; + read(); + if (sc == 0) + { + done(); + } + break; + case 3: + unsigned short hi = pch() + sc; + pc = (hi << 8) | pcl(); + read(); + done(); + break; + } } void CPU::addr_NMI() { - switch (this->t) - { - case 1: - address = pc; - read(); // discard - break; - case 2: - address = push(); - data = pch(); - write(); - break; - case 3: - address = push(); - data = pcl(); - write(); - break; - case 4: - address = push(); - p |= PMASK_I; + switch (this->t) + { + case 1: + address = pc; + read(); // discard + break; + case 2: + address = push(); + data = pch(); + write(); + break; + case 3: + address = push(); + data = pcl(); + write(); + break; + case 4: + address = push(); + p |= PMASK_I; p &= ~PMASK_B; - data = p; - write(); - break; - case 5: - ++address; - read(); - adl = data; - break; - case 6: - ++address; - read(); - adh = data; - pc = ad(); - pendingNMI = false; - done(); - break; - } + data = p; + write(); + break; + case 5: + ++address; + read(); + adl = data; + break; + case 6: + ++address; + read(); + adh = data; + pc = ad(); + pendingNMI = false; + done(); + break; + } } void CPU::addr_RESET() { - switch (this->t) - { - case 1: - address = pc++; - read(); // discard - break; - case 2: - s = 0xFF; // real CPU doesn't do this ??? - address = push(); - data = pch(); - read(); // discard - break; - case 3: - address = push(); - data = pcl(); - read(); // discard - break; - case 4: - address = push(); - p |= PMASK_I; - data = p; - read(); // discard - break; - case 5: - address = pc++; - read(); - adl = data; - break; - case 6: - address = pc; - read(); - adh = data; - pc = ad(); - pendingReset = false; - done(); - break; - } + switch (this->t) + { + case 1: + address = pc++; + read(); // discard + break; + case 2: + s = 0xFF; // real CPU doesn't do this ??? + address = push(); + data = pch(); + read(); // discard + break; + case 3: + address = push(); + data = pcl(); + read(); // discard + break; + case 4: + address = push(); + p |= PMASK_I; + data = p; + read(); // discard + break; + case 5: + address = pc++; + read(); + adl = data; + break; + case 6: + address = pc; + read(); + adh = data; + pc = ad(); + pendingReset = false; + done(); + break; + } } void CPU::addr_IRQ() { - switch (this->t) - { - case 1: - address = pc; - read(); // discard - break; - case 2: - address = push(); - data = pch(); - write(); - break; - case 3: - address = push(); - data = pcl(); // ??? - write(); - break; - case 4: - address = push(); - p |= PMASK_I; + switch (this->t) + { + case 1: + address = pc; + read(); // discard + break; + case 2: + address = push(); + data = pch(); + write(); + break; + case 3: + address = push(); + data = pcl(); // ??? + write(); + break; + case 4: + address = push(); + p |= PMASK_I; p &= ~PMASK_B; - data = p; - write(); - break; - case 5: - address = pc++; - read(); - adl = data; - break; - case 6: - address = pc; - read(); - adh = data; - pc = ad(); - pendingIRQ = false; - done(); - break; - } + data = p; + write(); + break; + case 5: + address = pc++; + read(); + adl = data; + break; + case 6: + address = pc; + read(); + adh = data; + pc = ad(); + pendingIRQ = false; + done(); + break; + } } void CPU::read() { - this->data = this->addressBus.read(this->address); + this->data = this->addressBus.read(this->address); } void CPU::write() { - this->addressBus.write(this->address,this->data); + this->addressBus.write(this->address,this->data); } void CPU::execute() { - (this->*this->exec[this->opcode])(); + (this->*this->exec[this->opcode])(); } void CPU::done() { - this->t = -1; + this->t = -1; } unsigned char CPU::pch() { - return (unsigned char)(this->pc >> 8); + return (unsigned char)(this->pc >> 8); } unsigned char CPU::pcl() { - return (unsigned char)(this->pc); + return (unsigned char)(this->pc); } unsigned short CPU::sp() { - return 0x100+this->s; + return 0x100+this->s; } unsigned short CPU::push() { - const unsigned short psp = sp(); - --this->s; - return psp; + const unsigned short psp = sp(); + --this->s; + return psp; } unsigned short CPU::pull() { - ++this->s; - return sp(); + ++this->s; + return sp(); } unsigned char CPU::getIndex() { - // opcode: aaabbbcc - const int aaa = (this->opcode & 0xE0) >> 5; - int bbb = (this->opcode & 0x1C) >> 2; - int cc = (this->opcode & 0x03); - if (bbb == 0) - { - return this->x; - } - if (bbb == 4 || bbb == 6) - { - return this->y; - } - if (bbb == 5 || bbb == 7) - { - if (cc == 2 && (aaa == 4 || aaa == 5)) - { - return this->y; - } - return this->x; - } - return 0; + // opcode: aaabbbcc + const int aaa = (this->opcode & 0xE0) >> 5; + int bbb = (this->opcode & 0x1C) >> 2; + int cc = (this->opcode & 0x03); + if (bbb == 0) + { + return this->x; + } + if (bbb == 4 || bbb == 6) + { + return this->y; + } + if (bbb == 5 || bbb == 7) + { + if (cc == 2 && (aaa == 4 || aaa == 5)) + { + return this->y; + } + return this->x; + } + return 0; } unsigned short CPU::ad() { - return combine(this->adl,this->adh); + return combine(this->adl,this->adh); } unsigned short CPU::ia() { - return combine(this->ial,this->iah); + return combine(this->ial,this->iah); } unsigned short CPU::ba() { - return combine(this->bal,this->bah); + return combine(this->bal,this->bah); } unsigned short CPU::combine(const unsigned char lo, const unsigned char hi) { - return hi << 8 | lo; + return hi << 8 | lo; } void CPU::setStatusRegisterNZ(const unsigned char val) { - setP(PMASK_N,val & 0x80); - setP(PMASK_Z,!val); + setP(PMASK_N,val & 0x80); + setP(PMASK_Z,!val); } void CPU::LDA() { - this->a = this->data; - setStatusRegisterNZ(this->a); + this->a = this->data; + setStatusRegisterNZ(this->a); } void CPU::LDX() { - this->x = this->data; - setStatusRegisterNZ(this->x); + this->x = this->data; + setStatusRegisterNZ(this->x); } void CPU::LDY() { - this->y = this->data; - setStatusRegisterNZ(this->y); + this->y = this->data; + setStatusRegisterNZ(this->y); } void CPU::STA() { - this->data = this->a; + this->data = this->a; } void CPU::STX() { - this->data = this->x; + this->data = this->x; } void CPU::STY() { - this->data = this->y; + this->data = this->y; } void CPU::compare(const unsigned char r) { - const signed short tmp = r - this->data; - setP(PMASK_C,0 <= tmp && tmp < 0x100); - setStatusRegisterNZ((const signed char)tmp); + const signed short tmp = r - this->data; + setP(PMASK_C,0 <= tmp && tmp < 0x100); + setStatusRegisterNZ((const signed char)tmp); } void CPU::CMP() { - compare(this->a); + compare(this->a); } void CPU::CPX() { - compare(this->x); + compare(this->x); } void CPU::CPY() { - compare(this->y); + compare(this->y); } void CPU::AND() { - this->a &= this->data; - setStatusRegisterNZ(this->a); + this->a &= this->data; + setStatusRegisterNZ(this->a); } void CPU::ORA() { - this->a |= this->data; - setStatusRegisterNZ(this->a); + this->a |= this->data; + setStatusRegisterNZ(this->a); } void CPU::EOR() { - this->a ^= this->data; - setStatusRegisterNZ(this->a); + this->a ^= this->data; + setStatusRegisterNZ(this->a); } @@ -1723,92 +1723,92 @@ void CPU::EOR() void CPU::ASL() { - this->data = shiftLeft(this->data); + this->data = shiftLeft(this->data); } void CPU::ASL_A() { - this->a = shiftLeft(this->a); + this->a = shiftLeft(this->a); } void CPU::LSR() { - this->data = shiftRight(this->data); + this->data = shiftRight(this->data); } void CPU::LSR_A() { - this->a = shiftRight(this->a); + this->a = shiftRight(this->a); } void CPU::ROL() { - this->data = rotateLeft(this->data); + this->data = rotateLeft(this->data); } void CPU::ROL_A() { - this->a = rotateLeft(this->a); + this->a = rotateLeft(this->a); } void CPU::ROR() { - this->data = rotateRight(this->data); + this->data = rotateRight(this->data); } void CPU::ROR_A() { - this->a = rotateRight(this->a); + this->a = rotateRight(this->a); } unsigned char CPU::shiftLeft(unsigned char byt) { - setP(PMASK_C,byt & 0x80); - byt <<= 1; - setStatusRegisterNZ(byt); - return byt; + setP(PMASK_C,byt & 0x80); + byt <<= 1; + setStatusRegisterNZ(byt); + return byt; } unsigned char CPU::shiftRight(unsigned char byt) { - setP(PMASK_C,byt & 0x01); - byt >>= 1; - setStatusRegisterNZ(byt); - return byt; + setP(PMASK_C,byt & 0x01); + byt >>= 1; + setStatusRegisterNZ(byt); + return byt; } unsigned char CPU::rotateLeft(unsigned char byt) { - const bool newCarry = (byt & 0x80); + const bool newCarry = (byt & 0x80); - byt <<= 1; + byt <<= 1; - if (this->p & PMASK_C) - { - byt |= 0x01; - } + if (this->p & PMASK_C) + { + byt |= 0x01; + } - setP(PMASK_C,newCarry); - setStatusRegisterNZ(byt); + setP(PMASK_C,newCarry); + setStatusRegisterNZ(byt); - return byt; + return byt; } unsigned char CPU::rotateRight(unsigned char byt) { - const bool newCarry = (byt & 0x01); + const bool newCarry = (byt & 0x01); - byt >>= 1; + byt >>= 1; - if (this->p & PMASK_C) - { - byt |= 0x80; - } + if (this->p & PMASK_C) + { + byt |= 0x80; + } - setP(PMASK_C,newCarry); - setStatusRegisterNZ(byt); + setP(PMASK_C,newCarry); + setStatusRegisterNZ(byt); - return byt; + return byt; } @@ -1818,62 +1818,62 @@ unsigned char CPU::rotateRight(unsigned char byt) void CPU::ADC() { - /* - This method based on ADC from the POM1 Apple 1 emulator. - Copyright (C) 2000, by Verhille Arnaud, GPLv2 license. - */ - int Op1 = this->a; - int Op2 = this->data; - if (this->p & PMASK_D) - { - setP(PMASK_Z,!((Op1 + Op2 + !!(this->p & PMASK_C)) & 0xff)); - int tmp = (Op1 & 0xf) + (Op2 & 0xf) + !!(this->p & PMASK_C); - tmp = tmp >= 10 ? tmp + 6 : tmp; - this->a = tmp; - tmp = (Op1 & 0xf0) + (Op2 & 0xf0) + (tmp & 0xf0); - setP(PMASK_N,tmp < 0); - setP(PMASK_V,((Op1 ^ tmp) & ~(Op1 ^ Op2) & 0x80)); - tmp = (this->a & 0xf) | (tmp >= 160 ? tmp + 96 : tmp); - setP(PMASK_C,tmp >= 0x100); - this->a = tmp & 0xff; - } - else - { - int tmp = Op1 + Op2 + !!(this->p & PMASK_C); - this->a = tmp & 0xFF; - setP(PMASK_V,((Op1 ^ this->a) & ~(Op1 ^ Op2) & 0x80)); - setP(PMASK_C,tmp >= 0x100); - setStatusRegisterNZ(this->a); - } + /* + This method based on ADC from the POM1 Apple 1 emulator. + Copyright (C) 2000, by Verhille Arnaud, GPLv2 license. + */ + int Op1 = this->a; + int Op2 = this->data; + if (this->p & PMASK_D) + { + setP(PMASK_Z,!((Op1 + Op2 + !!(this->p & PMASK_C)) & 0xff)); + int tmp = (Op1 & 0xf) + (Op2 & 0xf) + !!(this->p & PMASK_C); + tmp = tmp >= 10 ? tmp + 6 : tmp; + this->a = tmp; + tmp = (Op1 & 0xf0) + (Op2 & 0xf0) + (tmp & 0xf0); + setP(PMASK_N,tmp < 0); + setP(PMASK_V,((Op1 ^ tmp) & ~(Op1 ^ Op2) & 0x80)); + tmp = (this->a & 0xf) | (tmp >= 160 ? tmp + 96 : tmp); + setP(PMASK_C,tmp >= 0x100); + this->a = tmp & 0xff; + } + else + { + int tmp = Op1 + Op2 + !!(this->p & PMASK_C); + this->a = tmp & 0xFF; + setP(PMASK_V,((Op1 ^ this->a) & ~(Op1 ^ Op2) & 0x80)); + setP(PMASK_C,tmp >= 0x100); + setStatusRegisterNZ(this->a); + } } void CPU::SBC() { - /* - This method based on SBC from the POM1 Apple 1 emulator. - Copyright (C) 2000, by Verhille Arnaud, GPLv2 license. - */ - int Op1 = this->a; - int Op2 = this->data; - if (this->p & PMASK_D) - { - int tmp = (Op1 & 0xf) - (Op2 & 0xf) - !(this->p & PMASK_C); - tmp = (tmp & 0x10) != 0 ? tmp - 6 : tmp; - this->a = tmp; - tmp = (Op1 & 0xf0) - (Op2 & 0xf0) - (this->a & 0x10); - this->a = (this->a & 0xf) | ((tmp & 0x100) != 0 ? tmp - 96 : tmp); - tmp = Op1 - Op2 - !(this->p & PMASK_C); - setP(PMASK_C,0 <= tmp && tmp < 0x100); - setStatusRegisterNZ(tmp); - } - else - { - int tmp = Op1 - Op2 - !(this->p & PMASK_C); - this->a = tmp & 0xff; - setP(PMASK_V,((Op1 ^ Op2) & (Op1 ^ this->a) & 0x80)); - setP(PMASK_C,0 <= tmp && tmp < 0x100); - setStatusRegisterNZ(this->a); - } + /* + This method based on SBC from the POM1 Apple 1 emulator. + Copyright (C) 2000, by Verhille Arnaud, GPLv2 license. + */ + int Op1 = this->a; + int Op2 = this->data; + if (this->p & PMASK_D) + { + int tmp = (Op1 & 0xf) - (Op2 & 0xf) - !(this->p & PMASK_C); + tmp = (tmp & 0x10) != 0 ? tmp - 6 : tmp; + this->a = tmp; + tmp = (Op1 & 0xf0) - (Op2 & 0xf0) - (this->a & 0x10); + this->a = (this->a & 0xf) | ((tmp & 0x100) != 0 ? tmp - 96 : tmp); + tmp = Op1 - Op2 - !(this->p & PMASK_C); + setP(PMASK_C,0 <= tmp && tmp < 0x100); + setStatusRegisterNZ(tmp); + } + else + { + int tmp = Op1 - Op2 - !(this->p & PMASK_C); + this->a = tmp & 0xff; + setP(PMASK_V,((Op1 ^ Op2) & (Op1 ^ this->a) & 0x80)); + setP(PMASK_C,0 <= tmp && tmp < 0x100); + setStatusRegisterNZ(this->a); + } } @@ -1881,58 +1881,58 @@ void CPU::SBC() void CPU::INC() { - ++this->data; - setStatusRegisterNZ(this->data); + ++this->data; + setStatusRegisterNZ(this->data); } void CPU::DEC() { - --this->data; - setStatusRegisterNZ(this->data); + --this->data; + setStatusRegisterNZ(this->data); } void CPU::INX() { - ++this->x; - setStatusRegisterNZ(this->x); + ++this->x; + setStatusRegisterNZ(this->x); } void CPU::INY() { - ++this->y; - setStatusRegisterNZ(this->y); + ++this->y; + setStatusRegisterNZ(this->y); } void CPU::DEX() { - --this->x; - setStatusRegisterNZ(this->x); + --this->x; + setStatusRegisterNZ(this->x); } void CPU::DEY() { - --this->y; - setStatusRegisterNZ(this->y); + --this->y; + setStatusRegisterNZ(this->y); } void CPU::setP(const unsigned char mask, const unsigned char val) { - if (val) - this->p |= mask; - else - this->p &= ~mask; + if (val) + this->p |= mask; + else + this->p &= ~mask; } void CPU::BIT() { - setP(PMASK_V,this->data & 0x40); - setP(PMASK_N,this->data & 0x80); - setP(PMASK_Z,!(this->data & this->a)); + setP(PMASK_V,this->data & 0x40); + setP(PMASK_N,this->data & 0x80); + setP(PMASK_Z,!(this->data & this->a)); } void CPU::PHA() { - this->data = this->a; + this->data = this->a; } void CPU::PHP() @@ -1942,14 +1942,14 @@ void CPU::PHP() void CPU::PLA() { - this->a = this->data; - setStatusRegisterNZ(this->a); + this->a = this->data; + setStatusRegisterNZ(this->a); } void CPU::PLP() { - this->p = this->data; - this->p |= PMASK_M; + this->p = this->data; + this->p |= PMASK_M; this->p |= PMASK_B; } @@ -1975,116 +1975,116 @@ void CPU::JSR() void CPU::BNE() { - this->branch = !(this->p & PMASK_Z); + this->branch = !(this->p & PMASK_Z); } void CPU::BEQ() { - this->branch = this->p & PMASK_Z; + this->branch = this->p & PMASK_Z; } void CPU::BVC() { - this->branch = !(this->p & PMASK_V); + this->branch = !(this->p & PMASK_V); } void CPU::BVS() { - this->branch = this->p & PMASK_V; + this->branch = this->p & PMASK_V; } void CPU::BCC() { - this->branch = !(this->p & PMASK_C); + this->branch = !(this->p & PMASK_C); } void CPU::BCS() { - this->branch = this->p & PMASK_C; + this->branch = this->p & PMASK_C; } void CPU::BPL() { - this->branch = !(this->p & PMASK_N); + this->branch = !(this->p & PMASK_N); } void CPU::BMI() { - this->branch = this->p & PMASK_N; + this->branch = this->p & PMASK_N; } void CPU::TAX() { - this->x = this->a; - setStatusRegisterNZ(this->x); + this->x = this->a; + setStatusRegisterNZ(this->x); } void CPU::TXA() { - this->a = this->x; - setStatusRegisterNZ(this->a); + this->a = this->x; + setStatusRegisterNZ(this->a); } void CPU::TAY() { - this->y = this->a; - setStatusRegisterNZ(this->y); + this->y = this->a; + setStatusRegisterNZ(this->y); } void CPU::TYA() { - this->a = this->y; - setStatusRegisterNZ(this->a); + this->a = this->y; + setStatusRegisterNZ(this->a); } void CPU::TXS() { - this->s = this->x; - // make sure this doesn't affect status register - // it doesn't + this->s = this->x; + // make sure this doesn't affect status register + // it doesn't } void CPU::TSX() { - this->x = this->s; - setStatusRegisterNZ(this->x); - // make sure this does affect status register - // it does + this->x = this->s; + setStatusRegisterNZ(this->x); + // make sure this does affect status register + // it does } void CPU::CLC() { - this->p &= ~PMASK_C; + this->p &= ~PMASK_C; } void CPU::SEC() { - this->p |= PMASK_C; + this->p |= PMASK_C; } void CPU::CLI() { - this->p &= ~PMASK_I; + this->p &= ~PMASK_I; } void CPU::SEI() { - this->p |= PMASK_I; + this->p |= PMASK_I; } void CPU::CLV() { - this->p &= ~PMASK_V; + this->p &= ~PMASK_V; } void CPU::CLD() { - this->p &= ~PMASK_D; + this->p &= ~PMASK_D; } void CPU::SED() { - this->p |= PMASK_D; + this->p |= PMASK_D; } void CPU::NOP() @@ -2105,15 +2105,15 @@ void CPU::Unoff1() void CPU::Unoff2() { - this->pc++; + this->pc++; } void CPU::Unoff3() { - this->pc += 2; + this->pc += 2; } void CPU::Hang() { - this->pc--; + this->pc--; } diff --git a/src/cpu.h b/src/cpu.h index 7daea70..ed07e1a 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -23,195 +23,195 @@ class AddressBus; class CPU { private: - enum { MEMORY_LIM = 1 << 0x10 }; - enum { IRQ_VECTOR = MEMORY_LIM-2 }; // or BRK - enum { RESET_VECTOR = IRQ_VECTOR-2 }; // or power-on - enum { NMI_VECTOR = RESET_VECTOR-2 }; + enum { MEMORY_LIM = 1 << 0x10 }; + enum { IRQ_VECTOR = MEMORY_LIM-2 }; // or BRK + enum { RESET_VECTOR = IRQ_VECTOR-2 }; // or power-on + enum { NMI_VECTOR = RESET_VECTOR-2 }; - unsigned char adl; - unsigned char adh; - unsigned char bal; - unsigned char bah; - unsigned char ial; - unsigned char iah; - unsigned char idx; - signed char offset; - bool branch; - signed char sc; - bool wc; + unsigned char adl; + unsigned char adh; + unsigned char bal; + unsigned char bah; + unsigned char ial; + unsigned char iah; + unsigned char idx; + signed char offset; + bool branch; + signed char sc; + bool wc; - bool pendingIRQ; - bool pendingNMI; - bool pendingReset; - - bool started; + bool pendingIRQ; + bool pendingNMI; + bool pendingReset; + + bool started; - unsigned char a; - unsigned char x; - unsigned char y; + unsigned char a; + unsigned char x; + unsigned char y; - unsigned char s; + unsigned char s; - //p = NVMBDIZC - enum { PMASK_C = 1<<0 }; - enum { PMASK_Z = 1<<1 }; - enum { PMASK_I = 1<<2 }; - enum { PMASK_D = 1<<3 }; - enum { PMASK_B = 1<<4 }; - enum { PMASK_M = 1<<5 }; - enum { PMASK_V = 1<<6 }; - enum { PMASK_N = 1<<7 }; - unsigned char p; + //p = NVMBDIZC + enum { PMASK_C = 1<<0 }; + enum { PMASK_Z = 1<<1 }; + enum { PMASK_I = 1<<2 }; + enum { PMASK_D = 1<<3 }; + enum { PMASK_B = 1<<4 }; + enum { PMASK_M = 1<<5 }; + enum { PMASK_V = 1<<6 }; + enum { PMASK_N = 1<<7 }; + unsigned char p; - unsigned short pc; + unsigned short pc; - AddressBus& addressBus; + AddressBus& addressBus; - unsigned short address; - unsigned char data; + unsigned short address; + unsigned char data; - unsigned short opcode; + unsigned short opcode; - signed char t; + signed char t; - static void (CPU::*addr[])(); - static void (CPU::*exec[])(); + static void (CPU::*addr[])(); + static void (CPU::*exec[])(); - void firstCycle(); - int getInterruptAddress(); - int getInterruptPseudoOpCode(); - void subsequentCycle(); + void firstCycle(); + int getInterruptAddress(); + int getInterruptPseudoOpCode(); + void subsequentCycle(); - void read(); - void write(); - void execute(); - void done(); + void read(); + void write(); + void execute(); + void done(); - unsigned char pch(); - unsigned char pcl(); - unsigned short sp(); - unsigned short push(); - unsigned short pull(); - unsigned char getIndex(); - unsigned short ad(); - unsigned short ia(); - unsigned short ba(); - unsigned short combine(const unsigned char lo, const unsigned char hi); - void setP(const unsigned char mask, const unsigned char val); - void setStatusRegisterNZ(const unsigned char val); - unsigned char shiftLeft(unsigned char byt); - unsigned char shiftRight(unsigned char byt); - unsigned char rotateLeft(unsigned char byt); - unsigned char rotateRight(unsigned char byt); - void compare(const unsigned char r); + unsigned char pch(); + unsigned char pcl(); + unsigned short sp(); + unsigned short push(); + unsigned short pull(); + unsigned char getIndex(); + unsigned short ad(); + unsigned short ia(); + unsigned short ba(); + unsigned short combine(const unsigned char lo, const unsigned char hi); + void setP(const unsigned char mask, const unsigned char val); + void setStatusRegisterNZ(const unsigned char val); + unsigned char shiftLeft(unsigned char byt); + unsigned char shiftRight(unsigned char byt); + unsigned char rotateLeft(unsigned char byt); + unsigned char rotateRight(unsigned char byt); + void compare(const unsigned char r); - void addr_SINGLE(); - void addr_INTERNAL_IMMEDIATE(); - void addr_INTERNAL_ZERO_PAGE(); - void addr_INTERNAL_ABSOLUTE(); - void addr_INTERNAL_INDIRECT_X(); - void addr_INTERNAL_ABSOLUTE_XY(); - void addr_INTERNAL_ZERO_PAGE_XY(); - void addr_INTERNAL_INDIRECT_Y(); - void addr_STORE_ZERO_PAGE(); - void addr_STORE_ABSOLUTE(); - void addr_STORE_INDIRECT_X(); - void addr_STORE_ABSOLUTE_XY(); - void addr_STORE_ZERO_PAGE_XY(); - void addr_STORE_INDIRECT_Y(); - void addr_RMW_ZERO_PAGE(); - void addr_RMW_ABSOLUTE(); - void addr_RMW_ZERO_PAGE_X(); - void addr_RMW_ABSOLUTE_X(); - void addr_MISC_PUSH(); - void addr_MISC_PULL(); - void addr_MISC_JSR(); - void addr_MISC_BREAK(); - void addr_MISC_RTI(); - void addr_JMP_ABSOLUTE(); - void addr_JMP_INDIRECT(); - void addr_RTS(); - void addr_BRANCH(); - void addr_NMI(); - void addr_RESET(); - void addr_IRQ(); + void addr_SINGLE(); + void addr_INTERNAL_IMMEDIATE(); + void addr_INTERNAL_ZERO_PAGE(); + void addr_INTERNAL_ABSOLUTE(); + void addr_INTERNAL_INDIRECT_X(); + void addr_INTERNAL_ABSOLUTE_XY(); + void addr_INTERNAL_ZERO_PAGE_XY(); + void addr_INTERNAL_INDIRECT_Y(); + void addr_STORE_ZERO_PAGE(); + void addr_STORE_ABSOLUTE(); + void addr_STORE_INDIRECT_X(); + void addr_STORE_ABSOLUTE_XY(); + void addr_STORE_ZERO_PAGE_XY(); + void addr_STORE_INDIRECT_Y(); + void addr_RMW_ZERO_PAGE(); + void addr_RMW_ABSOLUTE(); + void addr_RMW_ZERO_PAGE_X(); + void addr_RMW_ABSOLUTE_X(); + void addr_MISC_PUSH(); + void addr_MISC_PULL(); + void addr_MISC_JSR(); + void addr_MISC_BREAK(); + void addr_MISC_RTI(); + void addr_JMP_ABSOLUTE(); + void addr_JMP_INDIRECT(); + void addr_RTS(); + void addr_BRANCH(); + void addr_NMI(); + void addr_RESET(); + void addr_IRQ(); - void LDA(); - void LDX(); - void LDY(); - void STA(); - void STX(); - void STY(); - void CMP(); - void CPX(); - void CPY(); - void AND(); - void ORA(); - void EOR(); - void ASL(); - void ASL_A(); - void LSR(); - void LSR_A(); - void ROL(); - void ROL_A(); - void ROR(); - void ROR_A(); - void ADC(); - void SBC(); - void INC(); - void DEC(); - void INX(); - void INY(); - void DEX(); - void DEY(); - void BIT(); - void PHA(); - void PHP(); - void PLA(); - void PLP(); - void BRK(); - void RTI(); - void JMP(); - void RTS(); - void JSR(); - void BNE(); - void BEQ(); - void BVC(); - void BVS(); - void BCC(); - void BCS(); - void BPL(); - void BMI(); - void TAX(); - void TXA(); - void TAY(); - void TYA(); - void TXS(); - void TSX(); - void CLC(); - void SEC(); - void CLI(); - void SEI(); - void CLV(); - void CLD(); - void SED(); - void NOP(); - void Unoff(); - void Unoff1(); - void Unoff2(); - void Unoff3(); - void Hang(); + void LDA(); + void LDX(); + void LDY(); + void STA(); + void STX(); + void STY(); + void CMP(); + void CPX(); + void CPY(); + void AND(); + void ORA(); + void EOR(); + void ASL(); + void ASL_A(); + void LSR(); + void LSR_A(); + void ROL(); + void ROL_A(); + void ROR(); + void ROR_A(); + void ADC(); + void SBC(); + void INC(); + void DEC(); + void INX(); + void INY(); + void DEX(); + void DEY(); + void BIT(); + void PHA(); + void PHP(); + void PLA(); + void PLP(); + void BRK(); + void RTI(); + void JMP(); + void RTS(); + void JSR(); + void BNE(); + void BEQ(); + void BVC(); + void BVS(); + void BCC(); + void BCS(); + void BPL(); + void BMI(); + void TAX(); + void TXA(); + void TAY(); + void TYA(); + void TXS(); + void TSX(); + void CLC(); + void SEC(); + void CLI(); + void SEI(); + void CLV(); + void CLD(); + void SED(); + void NOP(); + void Unoff(); + void Unoff1(); + void Unoff2(); + void Unoff3(); + void Hang(); public: - CPU(AddressBus& addressBus); - ~CPU(); + CPU(AddressBus& addressBus); + ~CPU(); - void powerOn(); - void reset(); - void IRQ(); - void NMI(); - void tick(); + void powerOn(); + void reset(); + void IRQ(); + void NMI(); + void tick(); }; #endif diff --git a/src/e2const.h b/src/e2const.h index e687333..dc0ce69 100644 --- a/src/e2const.h +++ b/src/e2const.h @@ -21,148 +21,148 @@ class E2Const { public: - /* - The NTSC standard defines the field rate as 60 fields per second. The number 60 - is based on the USA AC current frequency of 60 Hz. This, in turn, was based on the - clock standard (60 seconds per minute and 60 minutes per hour). - */ - static const int NTSC_FIELD_HZ = 60; + /* + The NTSC standard defines the field rate as 60 fields per second. The number 60 + is based on the USA AC current frequency of 60 Hz. This, in turn, was based on the + clock standard (60 seconds per minute and 60 minutes per hour). + */ + static const int NTSC_FIELD_HZ = 60; - /* - The NTSC standard defines 525 lines per frame, which was chosen to be a multiple - of a small number of standard tubes at the time, to produce a rate between RCA's - recommended 441 (used by NBC) and Philco's suggested 600-800 lines. - */ - static const int NTSC_LINES_PER_FRAME = 3*5*5*7; + /* + The NTSC standard defines 525 lines per frame, which was chosen to be a multiple + of a small number of standard tubes at the time, to produce a rate between RCA's + recommended 441 (used by NBC) and Philco's suggested 600-800 lines. + */ + static const int NTSC_LINES_PER_FRAME = 3*5*5*7; - /* - When color was added to the NTSC signal, studies by General Electric showed that - minimum interference was achieved using a subcarrier frequency 455 times the field - rate, which can also be obtained using standard tubes. - */ - static const int NTSC_COLOR_MULTIPLE = 5*7*13; + /* + When color was added to the NTSC signal, studies by General Electric showed that + minimum interference was achieved using a subcarrier frequency 455 times the field + rate, which can also be obtained using standard tubes. + */ + static const int NTSC_COLOR_MULTIPLE = 5*7*13; - /* - Adding color to NTSC also required slowing down the frame rate, by dropping one - field after every 1000. - */ - static const int NTSC_COLOR_DROP_FIELD = 1000; + /* + Adding color to NTSC also required slowing down the frame rate, by dropping one + field after every 1000. + */ + static const int NTSC_COLOR_DROP_FIELD = 1000; - /* - Calculate the color sub-channel rate, times 4. - This will be the (approximate) Hz of the "14M" - crystal oscillator in the Apple ][. - 14318181.818181818... Hz rounds to 14318182 Hz - U.A.II, p.3-2 - */ - static const int CRYSTAL_HZ = (int)(1.0F*NTSC_FIELD_HZ * NTSC_LINES_PER_FRAME * NTSC_COLOR_MULTIPLE * NTSC_COLOR_DROP_FIELD / (NTSC_COLOR_DROP_FIELD+1)); + /* + Calculate the color sub-channel rate, times 4. + This will be the (approximate) Hz of the "14M" + crystal oscillator in the Apple ][. + 14318181.818181818... Hz rounds to 14318182 Hz + U.A.II, p.3-2 + */ + static const int CRYSTAL_HZ = (int)(1.0F*NTSC_FIELD_HZ * NTSC_LINES_PER_FRAME * NTSC_COLOR_MULTIPLE * NTSC_COLOR_DROP_FIELD / (NTSC_COLOR_DROP_FIELD+1)); - /* - U.A.II, p. 3-3 - Normal 6502 cycle == 14 crystal periods - Long 6502 cycle == 16 crystal periods - */ - static const int CRYSTAL_CYCLES_PER_CPU_CYCLE = 14; - static const int EXTRA_CRYSTAL_CYCLES_PER_CPU_LONG_CYCLE = 2; + /* + U.A.II, p. 3-3 + Normal 6502 cycle == 14 crystal periods + Long 6502 cycle == 16 crystal periods + */ + static const int CRYSTAL_CYCLES_PER_CPU_CYCLE = 14; + static const int EXTRA_CRYSTAL_CYCLES_PER_CPU_LONG_CYCLE = 2; - /* - 65 bytes per row (64 normal CPU cycles plus one long CPU cycle) - */ - static const int BYTES_PER_ROW = (int)((NTSC_COLOR_DROP_FIELD+1)*1.0F*CRYSTAL_HZ/(NTSC_FIELD_HZ/2*NTSC_COLOR_DROP_FIELD*NTSC_LINES_PER_FRAME*CRYSTAL_CYCLES_PER_CPU_CYCLE)); - static const int HORIZ_CYCLES = BYTES_PER_ROW; + /* + 65 bytes per row (64 normal CPU cycles plus one long CPU cycle) + */ + static const int BYTES_PER_ROW = (int)((NTSC_COLOR_DROP_FIELD+1)*1.0F*CRYSTAL_HZ/(NTSC_FIELD_HZ/2*NTSC_COLOR_DROP_FIELD*NTSC_LINES_PER_FRAME*CRYSTAL_CYCLES_PER_CPU_CYCLE)); + static const int HORIZ_CYCLES = BYTES_PER_ROW; - /* - U.A.II, p. 3-2, "composite frequency... 1.0205 MHz" - Actually 1020484 Hz. - */ - static const int AVG_CPU_HZ = (int)((1.0F*CRYSTAL_HZ*HORIZ_CYCLES)/(CRYSTAL_CYCLES_PER_CPU_CYCLE*HORIZ_CYCLES+EXTRA_CRYSTAL_CYCLES_PER_CPU_LONG_CYCLE)); + /* + U.A.II, p. 3-2, "composite frequency... 1.0205 MHz" + Actually 1020484 Hz. + */ + static const int AVG_CPU_HZ = (int)((1.0F*CRYSTAL_HZ*HORIZ_CYCLES)/(CRYSTAL_CYCLES_PER_CPU_CYCLE*HORIZ_CYCLES+EXTRA_CRYSTAL_CYCLES_PER_CPU_LONG_CYCLE)); - /* - A normal NTSC field is 262.5 lines (half of a full frame's 525 lines). - The Apple rounds this down to 262 lines. - */ - static const int NTSC_WHOLE_LINES_PER_FIELD = NTSC_LINES_PER_FRAME/2; + /* + A normal NTSC field is 262.5 lines (half of a full frame's 525 lines). + The Apple rounds this down to 262 lines. + */ + static const int NTSC_WHOLE_LINES_PER_FIELD = NTSC_LINES_PER_FRAME/2; - static const int BYTES_PER_FIELD = BYTES_PER_ROW*NTSC_WHOLE_LINES_PER_FIELD; + static const int BYTES_PER_FIELD = BYTES_PER_ROW*NTSC_WHOLE_LINES_PER_FIELD; - // exactly 1 million - static const int MEGA = 1000000; + // exactly 1 million + static const int MEGA = 1000000; - static const int VISIBLE_BITS_PER_BYTE = 7; - static const int VISIBLE_LINES_PER_CHARACTER = 8; + static const int VISIBLE_BITS_PER_BYTE = 7; + static const int VISIBLE_LINES_PER_CHARACTER = 8; - /* - * 1000+1 seconds 2 fields 1 frame 1000000 microseconds 63 50 - * total horizontal line period = -------------- * -------- * ------------- * -------------------- = ( -- + -- ) microseconds per line - * 60*1000 fields 1 frame 3*5*5*7 lines 1 second 90 - * - * 10 81 - * horizontal blanking period = (1.5+4.7+.6+2.5+1.6) = 10.9 microseconds per line = ( -- + -- ) microseconds per line - * 90 - * - * visible line period = total horizontal line period minus horizontal blanking period = - * - * 52 59 - * -- + -- microseconds per line - * 90 - * - * - * To avoid the over-scan area, the Apple ][ uses only the middle 75% of the visible line, or 4739/120 microseconds - * - * Apple ][ uses half the clock rate, or 315/44 MHz, to oscillate the video signal. - * - * The result is 315/44 MHz * 4739/120 microseconds/line, rounded down, = 282 full pixel spots across the screen. - * The Apple ][ displays 7 bits per byte hi-res or lo-res, (or 7 pixel-wide characters for text mode), so that - * gives 282/7, which rounds down to 40 bytes per line. - */ - static const int VISIBLE_BYTES_PER_ROW = (int)((((1.0F*(NTSC_COLOR_DROP_FIELD+1)/(NTSC_FIELD_HZ*NTSC_COLOR_DROP_FIELD)*2/NTSC_LINES_PER_FRAME*MEGA)-(1.5+4.7+.6+2.5+1.6)) * 3/4) * (CRYSTAL_HZ/2)) / MEGA / VISIBLE_BITS_PER_BYTE; + /* + * 1000+1 seconds 2 fields 1 frame 1000000 microseconds 63 50 + * total horizontal line period = -------------- * -------- * ------------- * -------------------- = ( -- + -- ) microseconds per line + * 60*1000 fields 1 frame 3*5*5*7 lines 1 second 90 + * + * 10 81 + * horizontal blanking period = (1.5+4.7+.6+2.5+1.6) = 10.9 microseconds per line = ( -- + -- ) microseconds per line + * 90 + * + * visible line period = total horizontal line period minus horizontal blanking period = + * + * 52 59 + * -- + -- microseconds per line + * 90 + * + * + * To avoid the over-scan area, the Apple ][ uses only the middle 75% of the visible line, or 4739/120 microseconds + * + * Apple ][ uses half the clock rate, or 315/44 MHz, to oscillate the video signal. + * + * The result is 315/44 MHz * 4739/120 microseconds/line, rounded down, = 282 full pixel spots across the screen. + * The Apple ][ displays 7 bits per byte hi-res or lo-res, (or 7 pixel-wide characters for text mode), so that + * gives 282/7, which rounds down to 40 bytes per line. + */ + static const int VISIBLE_BYTES_PER_ROW = (int)((((1.0F*(NTSC_COLOR_DROP_FIELD+1)/(NTSC_FIELD_HZ*NTSC_COLOR_DROP_FIELD)*2/NTSC_LINES_PER_FRAME*MEGA)-(1.5+4.7+.6+2.5+1.6)) * 3/4) * (CRYSTAL_HZ/2)) / MEGA / VISIBLE_BITS_PER_BYTE; - /* - * NTSC total lines per frame (525) minus unusable lines (19 plus 20) = 486 usable lines - * To avoid the over-scan area, use the middle 80% of the vertical lines, giving 388 (rounded down) clearly visible lines - * Apple ][ uses only half the vertical resolution because it doesn't interlace, giving 194. - * Text characters are 8 pixels tall, so 194/8 rounded down gives 24 text lines. - * Multiply by 8 to give 192 lines total. - */ - static const int VISIBLE_ROWS_PER_FIELD = (NTSC_LINES_PER_FRAME-(20+19)) * 8/10 / 2 /VISIBLE_LINES_PER_CHARACTER*VISIBLE_LINES_PER_CHARACTER; + /* + * NTSC total lines per frame (525) minus unusable lines (19 plus 20) = 486 usable lines + * To avoid the over-scan area, use the middle 80% of the vertical lines, giving 388 (rounded down) clearly visible lines + * Apple ][ uses only half the vertical resolution because it doesn't interlace, giving 194. + * Text characters are 8 pixels tall, so 194/8 rounded down gives 24 text lines. + * Multiply by 8 to give 192 lines total. + */ + static const int VISIBLE_ROWS_PER_FIELD = (NTSC_LINES_PER_FRAME-(20+19)) * 8/10 / 2 /VISIBLE_LINES_PER_CHARACTER*VISIBLE_LINES_PER_CHARACTER; - static const int BLANKED_BYTES_PER_ROW = BYTES_PER_ROW-VISIBLE_BYTES_PER_ROW; - static const int VISIBLE_BYTES_PER_FIELD = BYTES_PER_ROW*VISIBLE_ROWS_PER_FIELD; - static const int SCANNABLE_ROWS = 0x100; - static const int SCANNABLE_BYTES = SCANNABLE_ROWS*BYTES_PER_ROW; - static const int RESET_ROWS = NTSC_WHOLE_LINES_PER_FIELD-SCANNABLE_ROWS; - static const int RESET_BYTES = RESET_ROWS*BYTES_PER_ROW; + static const int BLANKED_BYTES_PER_ROW = BYTES_PER_ROW-VISIBLE_BYTES_PER_ROW; + static const int VISIBLE_BYTES_PER_FIELD = BYTES_PER_ROW*VISIBLE_ROWS_PER_FIELD; + static const int SCANNABLE_ROWS = 0x100; + static const int SCANNABLE_BYTES = SCANNABLE_ROWS*BYTES_PER_ROW; + static const int RESET_ROWS = NTSC_WHOLE_LINES_PER_FIELD-SCANNABLE_ROWS; + static const int RESET_BYTES = RESET_ROWS*BYTES_PER_ROW; - static const int MIXED_TEXT_LINES = 4; - static const int ROWS_PER_TEXT_LINE = 8; - static const int MIXED_TEXT_CYCLE = (VISIBLE_ROWS_PER_FIELD-MIXED_TEXT_LINES*ROWS_PER_TEXT_LINE)*BYTES_PER_ROW; + static const int MIXED_TEXT_LINES = 4; + static const int ROWS_PER_TEXT_LINE = 8; + static const int MIXED_TEXT_CYCLE = (VISIBLE_ROWS_PER_FIELD-MIXED_TEXT_LINES*ROWS_PER_TEXT_LINE)*BYTES_PER_ROW; - static int test() - { - if (NTSC_FIELD_HZ!=60) return NTSC_FIELD_HZ; - if (NTSC_LINES_PER_FRAME!=525) return NTSC_LINES_PER_FRAME; - if (NTSC_COLOR_MULTIPLE!=455) return NTSC_COLOR_MULTIPLE; - if (NTSC_COLOR_DROP_FIELD!=1000) return NTSC_COLOR_DROP_FIELD; - if (CRYSTAL_HZ!=14318182) return CRYSTAL_HZ; - if (BYTES_PER_ROW!=65) return BYTES_PER_ROW; - if (AVG_CPU_HZ!=1020484) return AVG_CPU_HZ; - if (BYTES_PER_FIELD!=17030) return BYTES_PER_FIELD; - if (VISIBLE_BYTES_PER_ROW!=40) return VISIBLE_BYTES_PER_ROW; - if (VISIBLE_ROWS_PER_FIELD!=192) return VISIBLE_ROWS_PER_FIELD; - if (RESET_BYTES!=390) return RESET_BYTES; - if (BLANKED_BYTES_PER_ROW!=25) return BLANKED_BYTES_PER_ROW; - if (VISIBLE_BYTES_PER_FIELD!=12480) return VISIBLE_BYTES_PER_FIELD; - if (SCANNABLE_BYTES!=16640) return SCANNABLE_BYTES; - return -1; - } + static int test() + { + if (NTSC_FIELD_HZ!=60) return NTSC_FIELD_HZ; + if (NTSC_LINES_PER_FRAME!=525) return NTSC_LINES_PER_FRAME; + if (NTSC_COLOR_MULTIPLE!=455) return NTSC_COLOR_MULTIPLE; + if (NTSC_COLOR_DROP_FIELD!=1000) return NTSC_COLOR_DROP_FIELD; + if (CRYSTAL_HZ!=14318182) return CRYSTAL_HZ; + if (BYTES_PER_ROW!=65) return BYTES_PER_ROW; + if (AVG_CPU_HZ!=1020484) return AVG_CPU_HZ; + if (BYTES_PER_FIELD!=17030) return BYTES_PER_FIELD; + if (VISIBLE_BYTES_PER_ROW!=40) return VISIBLE_BYTES_PER_ROW; + if (VISIBLE_ROWS_PER_FIELD!=192) return VISIBLE_ROWS_PER_FIELD; + if (RESET_BYTES!=390) return RESET_BYTES; + if (BLANKED_BYTES_PER_ROW!=25) return BLANKED_BYTES_PER_ROW; + if (VISIBLE_BYTES_PER_FIELD!=12480) return VISIBLE_BYTES_PER_FIELD; + if (SCANNABLE_BYTES!=16640) return SCANNABLE_BYTES; + return -1; + } }; diff --git a/src/emptyslot.h b/src/emptyslot.h index 3af1d69..e6bdeba 100644 --- a/src/emptyslot.h +++ b/src/emptyslot.h @@ -23,14 +23,14 @@ class EmptySlot : public Card { public: - EmptySlot() {} - virtual ~EmptySlot() {} + EmptySlot() {} + virtual ~EmptySlot() {} virtual std::string getName() { return "[empty]"; } - // empty slots have no ROMs, so just return data (for floating bus emulation) - virtual unsigned char readRom(const unsigned short address, const unsigned char data) { return data; } - virtual void readSeventhRom(const unsigned short address, unsigned char* const pb) { } + // empty slots have no ROMs, so just return data (for floating bus emulation) + virtual unsigned char readRom(const unsigned short address, const unsigned char data) { return data; } + virtual void readSeventhRom(const unsigned short address, unsigned char* const pb) { } }; #endif diff --git a/src/emulator.h b/src/emulator.h index 80bd5ef..c297567 100644 --- a/src/emulator.h +++ b/src/emulator.h @@ -34,48 +34,48 @@ class Config; class Emulator { - PaddleButtonStates paddleButtonStates; - KeypressQueue keypresses; + PaddleButtonStates paddleButtonStates; + KeypressQueue keypresses; - HyperMode fhyper; - KeyboardBufferMode buffered; - ScreenImage screenImage; - AnalogTV display; - VideoStaticGenerator videoStatic; - Apple2 apple2; - ClipboardHandler clip; + HyperMode fhyper; + KeyboardBufferMode buffered; + ScreenImage screenImage; + AnalogTV display; + VideoStaticGenerator videoStatic; + Apple2 apple2; + ClipboardHandler clip; - Timable* timable; + Timable* timable; - bool quit; - bool repeat; - int keysDown; - int rept; - unsigned char lastKeyDown; - bool command; - bool pendingCommandExit; - std::string cmdline; + bool quit; + bool repeat; + int keysDown; + int rept; + unsigned char lastKeyDown; + bool command; + bool pendingCommandExit; + std::string cmdline; - void dispatchKeypress(const SDL_KeyboardEvent& keyEvent); - void dispatchKeyUp(const SDL_KeyboardEvent& keyEvent); - void cmdKey(const SDL_KeyboardEvent& keyEvent); - void processCommand(); + void dispatchKeypress(const SDL_KeyboardEvent& keyEvent); + void dispatchKeyUp(const SDL_KeyboardEvent& keyEvent); + void cmdKey(const SDL_KeyboardEvent& keyEvent); + void processCommand(); bool isSafeToQuit(); public: - Emulator(); - virtual ~Emulator(); + Emulator(); + virtual ~Emulator(); - void config(Config& cfg); + void config(Config& cfg); - virtual void init(); + virtual void init(); - void powerOnComputer(); - void powerOffComputer(); - void toggleComputerPower(); - void cycleDisplayType(); + void powerOnComputer(); + void powerOffComputer(); + void toggleComputerPower(); + void cycleDisplayType(); - virtual int run(); + virtual int run(); }; #endif diff --git a/src/firmwarecard.cpp b/src/firmwarecard.cpp index 2b0af00..d8ecfeb 100644 --- a/src/firmwarecard.cpp +++ b/src/firmwarecard.cpp @@ -19,12 +19,12 @@ #include "memory.h" FirmwareCard::FirmwareCard(ScreenImage& gui, int slot): - gui(gui), - slot(slot), - inhibitBankRom(false), - inhibitF8Rom(false), - inhibit(false), - bankRom(0x10000-0xD000) + gui(gui), + slot(slot), + inhibitBankRom(false), + inhibitF8Rom(false), + inhibit(false), + bankRom(0x10000-0xD000) { } @@ -40,21 +40,21 @@ FirmwareCard::~FirmwareCard() void FirmwareCard::ioBankRom(const unsigned short addr, unsigned char* const pb, const bool) { - this->inhibit = false; - if (addr < 0x2800) - { - if (this->inhibitBankRom) - { + this->inhibit = false; + if (addr < 0x2800) + { + if (this->inhibitBankRom) + { *pb = this->bankRom.read(addr, *pb); - this->inhibit = true; - } - } - else if (0x2800 <= addr && addr < 0x3000) - { - if (this->inhibitF8Rom) - { + this->inhibit = true; + } + } + else if (0x2800 <= addr && addr < 0x3000) + { + if (this->inhibitF8Rom) + { *pb = this->bankRom.read(addr, *pb); - this->inhibit = true; - } - } + this->inhibit = true; + } + } } diff --git a/src/firmwarecard.h b/src/firmwarecard.h index d984244..cf47db2 100644 --- a/src/firmwarecard.h +++ b/src/firmwarecard.h @@ -25,49 +25,49 @@ class FirmwareCard : public Card { private: - ScreenImage& gui; - int slot; - bool inhibitBankRom; - bool inhibitF8Rom; - bool inhibit; - Memory bankRom; + ScreenImage& gui; + int slot; + bool inhibitBankRom; + bool inhibitF8Rom; + bool inhibit; + Memory bankRom; public: - FirmwareCard(ScreenImage& gui, int slot); - ~FirmwareCard(); + FirmwareCard(ScreenImage& gui, int slot); + ~FirmwareCard(); - virtual void ioBankRom(const unsigned short addr, unsigned char* const pb, const bool write); + virtual void ioBankRom(const unsigned short addr, unsigned char* const pb, const bool write); - virtual void reset() - { - this->inhibitBankRom = false; - this->inhibitF8Rom = false; - this->gui.setFirmCard(this->slot,this->inhibitBankRom,this->inhibitF8Rom); - } - - - - virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing) - { - this->inhibitBankRom = !(address & 1); - this->inhibitF8Rom = (address & 2); - this->gui.setFirmCard(this->slot,this->inhibitBankRom,this->inhibitF8Rom); - return data; - } - - virtual void loadBankRom(const unsigned short base, std::istream& in) - { - this->bankRom.load(base,in); - } - - - - virtual bool inhibitMotherboardRom() - { - return this->inhibit; - } + virtual void reset() + { + this->inhibitBankRom = false; + this->inhibitF8Rom = false; + this->gui.setFirmCard(this->slot,this->inhibitBankRom,this->inhibitF8Rom); + } + + + + virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing) + { + this->inhibitBankRom = !(address & 1); + this->inhibitF8Rom = (address & 2); + this->gui.setFirmCard(this->slot,this->inhibitBankRom,this->inhibitF8Rom); + return data; + } + + virtual void loadBankRom(const unsigned short base, std::istream& in) + { + this->bankRom.load(base,in); + } + + + + virtual bool inhibitMotherboardRom() + { + return this->inhibit; + } - virtual std::string getName() { return "firmware "; } + virtual std::string getName() { return "firmware "; } }; #endif diff --git a/src/gui.h b/src/gui.h index b9b1ad3..3e556cc 100644 --- a/src/gui.h +++ b/src/gui.h @@ -23,15 +23,15 @@ class GUI { public: - GUI(); - ~GUI(); + GUI(); + ~GUI(); - class NotInitException : public std::runtime_error - { - public: - NotInitException(); - virtual ~NotInitException() throw () {} - }; + class NotInitException : public std::runtime_error + { + public: + NotInitException(); + virtual ~NotInitException() throw () {} + }; }; diff --git a/src/hypermode.h b/src/hypermode.h index 937a9f8..fd5abca 100644 --- a/src/hypermode.h +++ b/src/hypermode.h @@ -21,25 +21,25 @@ class HyperMode { private: - bool fhyper; + bool fhyper; public: - HyperMode(): fhyper(false) { } - ~HyperMode() { } - bool isHyper() - { - return this->fhyper; - } + HyperMode(): fhyper(false) { } + ~HyperMode() { } + bool isHyper() + { + return this->fhyper; + } - void setHyper(bool isHyper) - { - this->fhyper = isHyper; - } + void setHyper(bool isHyper) + { + this->fhyper = isHyper; + } - void toggleHyper() - { - this->fhyper = !this->fhyper; - } + void toggleHyper() + { + this->fhyper = !this->fhyper; + } }; #endif diff --git a/src/keyboard.cpp b/src/keyboard.cpp index f019c60..1aa2e1f 100644 --- a/src/keyboard.cpp +++ b/src/keyboard.cpp @@ -20,54 +20,54 @@ #include "keyboardbuffermode.h" Keyboard::Keyboard(KeypressQueue& q, HyperMode& fhyper, KeyboardBufferMode& buffered): - keys(q), - fhyper(fhyper), - buffered(buffered), - cGet(0) + keys(q), + fhyper(fhyper), + buffered(buffered), + cGet(0) { } void Keyboard::clear() { - this->latch &= 0x7F; + this->latch &= 0x7F; } unsigned char Keyboard::get() { - waitIfTooFast(); - if (!this->buffered.isBuffered() || !(this->latch & 0x80)) - { - if (!this->keys.empty()) - { - this->latch = this->keys.front() | 0x80; - this->keys.pop(); - } - } - return this->latch; + waitIfTooFast(); + if (!this->buffered.isBuffered() || !(this->latch & 0x80)) + { + if (!this->keys.empty()) + { + this->latch = this->keys.front() | 0x80; + this->keys.pop(); + } + } + return this->latch; } void Keyboard::waitIfTooFast() { - if (this->fhyper.isHyper()) - { - return; - } + if (this->fhyper.isHyper()) + { + return; + } - ++this->cGet; - if (!this->cGet) - { - if (SDL_GetTicks() - this->lastGet <= 1000) - { - /* - * Check every 256 gets to see if they are - * happening too fast (within one second). - * If so, it means we are probably just - * looping waiting for a keypress, so - * wait a millisecond (or so) just to - * prevent us from using 100% of CPU time. - */ - SDL_Delay(1); - } - } - this->lastGet = SDL_GetTicks(); + ++this->cGet; + if (!this->cGet) + { + if (SDL_GetTicks() - this->lastGet <= 1000) + { + /* + * Check every 256 gets to see if they are + * happening too fast (within one second). + * If so, it means we are probably just + * looping waiting for a keypress, so + * wait a millisecond (or so) just to + * prevent us from using 100% of CPU time. + */ + SDL_Delay(1); + } + } + this->lastGet = SDL_GetTicks(); } diff --git a/src/keyboard.h b/src/keyboard.h index d46e985..9d05d46 100644 --- a/src/keyboard.h +++ b/src/keyboard.h @@ -29,20 +29,20 @@ class KeyboardBufferMode; class Keyboard { private: - KeypressQueue& keys; - HyperMode& fhyper; - KeyboardBufferMode& buffered; + KeypressQueue& keys; + HyperMode& fhyper; + KeyboardBufferMode& buffered; - unsigned char latch; - unsigned char cGet; - Uint32 lastGet; + unsigned char latch; + unsigned char cGet; + Uint32 lastGet; - void waitIfTooFast(); + void waitIfTooFast(); public: - Keyboard(KeypressQueue& q, HyperMode& fhyper, KeyboardBufferMode& buffered); - void clear(); - unsigned char get(); + Keyboard(KeypressQueue& q, HyperMode& fhyper, KeyboardBufferMode& buffered); + void clear(); + unsigned char get(); }; #endif diff --git a/src/keyboardbuffermode.h b/src/keyboardbuffermode.h index 700cffb..7f9d951 100644 --- a/src/keyboardbuffermode.h +++ b/src/keyboardbuffermode.h @@ -21,26 +21,26 @@ class KeyboardBufferMode { private: - bool buffered; + bool buffered; public: - KeyboardBufferMode(): buffered(true) { } - ~KeyboardBufferMode() { } + KeyboardBufferMode(): buffered(true) { } + ~KeyboardBufferMode() { } - bool isBuffered() - { - return this->buffered; - } + bool isBuffered() + { + return this->buffered; + } - void setBuffered(bool buffered) - { - this->buffered = buffered; - } + void setBuffered(bool buffered) + { + this->buffered = buffered; + } - void toggleBuffered() - { - this->buffered = !this->buffered; - } + void toggleBuffered() + { + this->buffered = !this->buffered; + } }; #endif diff --git a/src/languagecard.cpp b/src/languagecard.cpp index 22cb3b0..d4d3cc4 100644 --- a/src/languagecard.cpp +++ b/src/languagecard.cpp @@ -19,17 +19,17 @@ #include "screenimage.h" LanguageCard::LanguageCard(ScreenImage& gui, int slot): - gui(gui), - slot(slot), - inhibit(false), - ramTop(0x10000-0xE000), - bank(1), - readEnable(false), - writeEnable(true), - writeCount(0) + gui(gui), + slot(slot), + inhibit(false), + ramTop(0x10000-0xE000), + bank(1), + readEnable(false), + writeEnable(true), + writeCount(0) { - this->ramBank.push_back(new Memory(0xE000-0xD000)); - this->ramBank.push_back(new Memory(0xE000-0xD000)); + this->ramBank.push_back(new Memory(0xE000-0xD000)); + this->ramBank.push_back(new Memory(0xE000-0xD000)); } LanguageCard::~LanguageCard() @@ -39,57 +39,57 @@ LanguageCard::~LanguageCard() unsigned char LanguageCard::io(const unsigned short address, const unsigned char data, const bool writing) { - if ((address & 1) && !writing) - { - ++this->writeCount; - } - else - { - this->writeCount = 0; - } - if (this->writeCount > 1) - { - this->writeEnable = true; - } - if (!(address & 1)) - { - this->writeEnable = false; - } + if ((address & 1) && !writing) + { + ++this->writeCount; + } + else + { + this->writeCount = 0; + } + if (this->writeCount > 1) + { + this->writeEnable = true; + } + if (!(address & 1)) + { + this->writeEnable = false; + } - const int r = address & 3; - this->readEnable = (r==0 || r==3); + const int r = address & 3; + this->readEnable = (r==0 || r==3); - this->bank = !(address & 8); + this->bank = !(address & 8); - this->gui.setLangCard(this->slot,this->readEnable,this->writeEnable,this->bank); + this->gui.setLangCard(this->slot,this->readEnable,this->writeEnable,this->bank); - return data; + return data; } void LanguageCard::ioBankRom(const unsigned short addr, unsigned char* const pb, const bool write) { - this->inhibit = false; - if (this->readEnable && !write) - { - if (addr < 0x1000) - { + this->inhibit = false; + if (this->readEnable && !write) + { + if (addr < 0x1000) + { *pb = this->ramBank[this->bank]->read(addr, *pb); - } - else - { + } + else + { *pb = this->ramTop.read(addr-0x1000, *pb); - } - this->inhibit = true; - } - else if (this->writeEnable && write) - { - if (addr < 0x1000) - { - this->ramBank[this->bank]->write(addr,*pb); - } - else - { - this->ramTop.write(addr-0x1000,*pb); - } - } + } + this->inhibit = true; + } + else if (this->writeEnable && write) + { + if (addr < 0x1000) + { + this->ramBank[this->bank]->write(addr,*pb); + } + else + { + this->ramTop.write(addr-0x1000,*pb); + } + } } diff --git a/src/languagecard.h b/src/languagecard.h index 748d8c6..de29718 100644 --- a/src/languagecard.h +++ b/src/languagecard.h @@ -27,25 +27,25 @@ class ScreenImage; class LanguageCard : public Card { private: - ScreenImage& gui; - int slot; - bool inhibit; - std::vector ramBank; - Memory ramTop; - unsigned char bank; - bool readEnable; - bool writeEnable; - unsigned char writeCount; + ScreenImage& gui; + int slot; + bool inhibit; + std::vector ramBank; + Memory ramTop; + unsigned char bank; + bool readEnable; + bool writeEnable; + unsigned char writeCount; public: - LanguageCard(ScreenImage& gui, int slot); - ~LanguageCard(); + LanguageCard(ScreenImage& gui, int slot); + ~LanguageCard(); - virtual void reset() { /* does nothing */ } - virtual bool inhibitMotherboardRom() { return this->inhibit; } - virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); - virtual void ioBankRom(const unsigned short addr, unsigned char* const pb, const bool write); - virtual std::string getName() { return "language W B2"; } + virtual void reset() { /* does nothing */ } + virtual bool inhibitMotherboardRom() { return this->inhibit; } + virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); + virtual void ioBankRom(const unsigned short addr, unsigned char* const pb, const bool write); + virtual std::string getName() { return "language W B2"; } }; #endif diff --git a/src/paddlebuttonstates.cpp b/src/paddlebuttonstates.cpp index 5424694..c87f831 100644 --- a/src/paddlebuttonstates.cpp +++ b/src/paddlebuttonstates.cpp @@ -31,14 +31,14 @@ PaddleButtonStates::~PaddleButtonStates() bool PaddleButtonStates::isDown(const int paddle) { - if (paddle < 0 || PADDLE_COUNT <= paddle) - { - return false; - } - unsigned char btn = SDL_GetMouseState(0,0); - if (paddle==0) - return btn&SDL_BUTTON_LMASK; - if (paddle==1) - return btn&SDL_BUTTON_RMASK; - return btn&SDL_BUTTON_MMASK; + if (paddle < 0 || PADDLE_COUNT <= paddle) + { + return false; + } + unsigned char btn = SDL_GetMouseState(0,0); + if (paddle==0) + return btn&SDL_BUTTON_LMASK; + if (paddle==1) + return btn&SDL_BUTTON_RMASK; + return btn&SDL_BUTTON_MMASK; } diff --git a/src/paddlebuttonstates.h b/src/paddlebuttonstates.h index d02d024..2aafe25 100644 --- a/src/paddlebuttonstates.h +++ b/src/paddlebuttonstates.h @@ -22,12 +22,12 @@ class PaddleButtonStates { - static const int PADDLE_COUNT; + static const int PADDLE_COUNT; public: - PaddleButtonStates(); - ~PaddleButtonStates(); - bool isDown(const int paddle); + PaddleButtonStates(); + ~PaddleButtonStates(); + bool isDown(const int paddle); }; #endif diff --git a/src/paddles.cpp b/src/paddles.cpp index 1cecc96..42d1260 100644 --- a/src/paddles.cpp +++ b/src/paddles.cpp @@ -23,7 +23,7 @@ Paddles::Paddles(): - rTick(PADDLE_COUNT) + rTick(PADDLE_COUNT) { } @@ -35,59 +35,59 @@ Paddles::~Paddles() void Paddles::tick() { - for (int paddle = 0; paddle < PADDLE_COUNT; ++paddle) - { - if (this->rTick[paddle] > 0) - --this->rTick[paddle]; - } + for (int paddle = 0; paddle < PADDLE_COUNT; ++paddle) + { + if (this->rTick[paddle] > 0) + --this->rTick[paddle]; + } } void Paddles::startTimers() { - try - { - tryStartPaddleTimers(); - } - catch (...) - { - std::cerr << "Warning: cannot start paddle timers; mouse will not function as paddles." << std::endl; - } + try + { + tryStartPaddleTimers(); + } + catch (...) + { + std::cerr << "Warning: cannot start paddle timers; mouse will not function as paddles." << std::endl; + } } void Paddles::tryStartPaddleTimers() { - int x, y; - SDL_GetMouseState(&x,&y); + int x, y; + SDL_GetMouseState(&x,&y); - double pMin = 0; - double pMax = 500; - x = (int)((x-pMin)/(pMax-pMin)*PADDLE_CYCLES+.5); - y = (int)((y-pMin)/(pMax-pMin)*PADDLE_CYCLES+.5); + double pMin = 0; + double pMax = 500; + x = (int)((x-pMin)/(pMax-pMin)*PADDLE_CYCLES+.5); + y = (int)((y-pMin)/(pMax-pMin)*PADDLE_CYCLES+.5); - if (isTimedOut(0)) - this->rTick[0] = x; - if (isTimedOut(1)) - this->rTick[1] = y; + if (isTimedOut(0)) + this->rTick[0] = x; + if (isTimedOut(1)) + this->rTick[1] = y; - /* - Here we emulate having 4700 ohm across pins 7 and 1 - of the game controller, and a 47Kohm resistor acros - pins 11 and 1, to give cheap real-time clocks at - paddles 2 and 3. Paddle 2 is the 100 microsecond reference, - and paddle 3 is the 1 millisecond reference. This is - described in U.A.2, p. 7-33. - */ - if (isTimedOut(2)) - this->rTick[2] = E2Const::AVG_CPU_HZ/10000; // was 90, but why? - if (isTimedOut(3)) - this->rTick[3] = E2Const::AVG_CPU_HZ/1000; + /* + Here we emulate having 4700 ohm across pins 7 and 1 + of the game controller, and a 47Kohm resistor acros + pins 11 and 1, to give cheap real-time clocks at + paddles 2 and 3. Paddle 2 is the 100 microsecond reference, + and paddle 3 is the 1 millisecond reference. This is + described in U.A.2, p. 7-33. + */ + if (isTimedOut(2)) + this->rTick[2] = E2Const::AVG_CPU_HZ/10000; // was 90, but why? + if (isTimedOut(3)) + this->rTick[3] = E2Const::AVG_CPU_HZ/1000; } bool Paddles::isTimedOut(const int paddle) { - if (paddle < 0 || PADDLE_COUNT <= paddle) - { - return false; - } - return this->rTick[paddle] <= 0; + if (paddle < 0 || PADDLE_COUNT <= paddle) + { + return false; + } + return this->rTick[paddle] <= 0; } diff --git a/src/paddles.h b/src/paddles.h index 5ba4379..ca082a4 100644 --- a/src/paddles.h +++ b/src/paddles.h @@ -23,19 +23,19 @@ class Paddles { private: - std::vector rTick; + std::vector rTick; - enum { PADDLE_COUNT = 4 }; - enum { PADDLE_CYCLES = 2805 }; // TODO: document where PADDLE_CYCLES==2805 came from + enum { PADDLE_COUNT = 4 }; + enum { PADDLE_CYCLES = 2805 }; // TODO: document where PADDLE_CYCLES==2805 came from - void tryStartPaddleTimers(); + void tryStartPaddleTimers(); public: - Paddles(); - ~Paddles(); - void tick(); - void startTimers(); - bool isTimedOut(const int paddle); + Paddles(); + ~Paddles(); + void tick(); + void startTimers(); + bool isTimedOut(const int paddle); }; #endif diff --git a/src/picturegenerator.cpp b/src/picturegenerator.cpp index 8cc55e5..ebe8e34 100644 --- a/src/picturegenerator.cpp +++ b/src/picturegenerator.cpp @@ -22,9 +22,9 @@ #include "e2const.h" PictureGenerator::PictureGenerator(AnalogTV& display, VideoMode& mode, const int& revision): - display(display), mode(mode), itestsig(testsig), itestsiglim(testsig+AppleNTSC::SIGNAL_LEN), - VISIBLE_X_OFFSET(E2Const::BYTES_PER_ROW-E2Const::VISIBLE_BYTES_PER_ROW), - revision(revision) + display(display), mode(mode), itestsig(testsig), itestsiglim(testsig+AppleNTSC::SIGNAL_LEN), + VISIBLE_X_OFFSET(E2Const::BYTES_PER_ROW-E2Const::VISIBLE_BYTES_PER_ROW), + revision(revision) { } @@ -36,96 +36,96 @@ PictureGenerator::~PictureGenerator() void PictureGenerator::powerOn() { - this->hpos = 0; - this->line = 0; - this->display.signal = this->testsig; - this->itestsig = this->testsig; + this->hpos = 0; + this->line = 0; + this->display.signal = this->testsig; + this->itestsig = this->testsig; } void inline PictureGenerator::shiftLoRes() { - /* - * For byte ABCDEFGH in register, perform - * the following 4-bit end-around shifts: - * - * +---<----+ +---<----+ - * | | | | - * +->ABCD->+ +->EFGH->+ - * - * Therefore: - * - * ABCDEFGH --> DABCHEFG - */ + /* + * For byte ABCDEFGH in register, perform + * the following 4-bit end-around shifts: + * + * +---<----+ +---<----+ + * | | | | + * +->ABCD->+ +->EFGH->+ + * + * Therefore: + * + * ABCDEFGH --> DABCHEFG + */ - unsigned char rot_bits = this->latchGraphics & 0x11; - // 000D000H - rot_bits <<= 3; - // D000H000 + unsigned char rot_bits = this->latchGraphics & 0x11; + // 000D000H + rot_bits <<= 3; + // D000H000 - this->latchGraphics &= 0xEE; - // ABC0EFG0 - this->latchGraphics >>= 1; - // 0ABC0EFG - this->latchGraphics |= rot_bits; - // DABCHEFG + this->latchGraphics &= 0xEE; + // ABC0EFG0 + this->latchGraphics >>= 1; + // 0ABC0EFG + this->latchGraphics |= rot_bits; + // DABCHEFG } void inline PictureGenerator::shiftHiRes() { - /* - * For byte ABCDEFGH in register, perform - * the following shift: - * - * +---<----+ - * | | - * +->ABCD->+--->EFGH-> - * - * Therefore: - * - * ABCDEFGH --> DABCDEFG - */ + /* + * For byte ABCDEFGH in register, perform + * the following shift: + * + * +---<----+ + * | | + * +->ABCD->+--->EFGH-> + * + * Therefore: + * + * ABCDEFGH --> DABCDEFG + */ - unsigned char rot_bits = this->latchGraphics & 0x10; - // 000D0000 - rot_bits <<= 3; - // D0000000 + unsigned char rot_bits = this->latchGraphics & 0x10; + // 000D0000 + rot_bits <<= 3; + // D0000000 - this->latchGraphics >>= 1; - // 0ABCDEFG - this->latchGraphics |= rot_bits; - // DABCDEFG + this->latchGraphics >>= 1; + // 0ABCDEFG + this->latchGraphics |= rot_bits; + // DABCDEFG } void inline PictureGenerator::shiftText() { - this->latchText >>= 1; + this->latchText >>= 1; } bool inline PictureGenerator::getTextBit() { - return this->latchText & 1; + return this->latchText & 1; } bool inline PictureGenerator::getHiResBit() { - return this->latchGraphics & 1; + return this->latchGraphics & 1; } bool inline PictureGenerator::getLoResBit(const bool odd, const bool vc) { - const int nibble = (this->latchGraphics >> (vc ? 4 : 0)) & 0x0F; - return (nibble >> (odd ? 2 : 0)) & 1; + const int nibble = (this->latchGraphics >> (vc ? 4 : 0)) & 0x0F; + return (nibble >> (odd ? 2 : 0)) & 1; } void inline PictureGenerator::loadGraphics(const unsigned char value) { - this->latchGraphics = value; - this->d7 = this->latchGraphics & 0x80; + this->latchGraphics = value; + this->d7 = this->latchGraphics & 0x80; } void inline PictureGenerator::loadText(const int value) { - this->latchText = value; + this->latchText = value; } // TODO can we hand-optimize the main picture generator algorithm any more? @@ -133,154 +133,154 @@ void inline PictureGenerator::loadText(const int value) // at 14MHz, in order to maintain authentic Apple ][ speed. void PictureGenerator::tick(const int t, const unsigned char rowToPlot) { - const bool isText(this->mode.isDisplayingText(t)); - const bool isHiRes(this->mode.isHiRes()); + const bool isText(this->mode.isDisplayingText(t)); + const bool isHiRes(this->mode.isHiRes()); - signed char* is = this->itestsig; + signed char* is = this->itestsig; - if (isText) - loadText(rowToPlot); - else - loadGraphics(rowToPlot); + if (isText) + loadText(rowToPlot); + else + loadGraphics(rowToPlot); - if (t==0) - { - this->line = 0; - } + if (t==0) + { + this->line = 0; + } - int cycles = E2Const::CRYSTAL_CYCLES_PER_CPU_CYCLE; - if (this->hpos == E2Const::HORIZ_CYCLES-1) - { - cycles += E2Const::EXTRA_CRYSTAL_CYCLES_PER_CPU_LONG_CYCLE; - } + int cycles = E2Const::CRYSTAL_CYCLES_PER_CPU_CYCLE; + if (this->hpos == E2Const::HORIZ_CYCLES-1) + { + cycles += E2Const::EXTRA_CRYSTAL_CYCLES_PER_CPU_LONG_CYCLE; + } - // hi-res half-pixel shift: - const bool shift = !isText && isHiRes && this->d7 && this->line < E2Const::VISIBLE_ROWS_PER_FIELD && !(this->hpos < VISIBLE_X_OFFSET) && this->revision > 0; - const bool showLastHiRes = shift && this->lasthires; + // hi-res half-pixel shift: + const bool shift = !isText && isHiRes && this->d7 && this->line < E2Const::VISIBLE_ROWS_PER_FIELD && !(this->hpos < VISIBLE_X_OFFSET) && this->revision > 0; + const bool showLastHiRes = shift && this->lasthires; - int xtra(0); - if (shift) - { - --cycles; - ++xtra; - } - const int firstBlankedCycle(E2Const::CRYSTAL_CYCLES_PER_CPU_CYCLE-xtra); + int xtra(0); + if (shift) + { + --cycles; + ++xtra; + } + const int firstBlankedCycle(E2Const::CRYSTAL_CYCLES_PER_CPU_CYCLE-xtra); - int hcycle(this->hpos*E2Const::CRYSTAL_CYCLES_PER_CPU_CYCLE); - const bool lineVis(this->line < E2Const::VISIBLE_ROWS_PER_FIELD); - const bool hVis(this->hpos >= VISIBLE_X_OFFSET); - for (int cycle(0); cycle < cycles-1; ++cycle) - { - const bool bit = shiftLatch(t,cycle,isText,isHiRes); - is = writeVideoSignal(shift,showLastHiRes,firstBlankedCycle,cycle,hcycle,bit,lineVis,hVis,is); - ++hcycle; - } - // optimization: pull the last iteration of the loop out, so we don't getHiResBit every time - { - this->lasthires = getHiResBit(); // save it for the next plotted byte, just in case we need it - const int cycle = cycles-1; - const bool bit = shiftLatch(t,cycle,isText,isHiRes); - is = writeVideoSignal(shift,showLastHiRes,firstBlankedCycle,cycle,hcycle,bit,lineVis,hVis,is); - } + int hcycle(this->hpos*E2Const::CRYSTAL_CYCLES_PER_CPU_CYCLE); + const bool lineVis(this->line < E2Const::VISIBLE_ROWS_PER_FIELD); + const bool hVis(this->hpos >= VISIBLE_X_OFFSET); + for (int cycle(0); cycle < cycles-1; ++cycle) + { + const bool bit = shiftLatch(t,cycle,isText,isHiRes); + is = writeVideoSignal(shift,showLastHiRes,firstBlankedCycle,cycle,hcycle,bit,lineVis,hVis,is); + ++hcycle; + } + // optimization: pull the last iteration of the loop out, so we don't getHiResBit every time + { + this->lasthires = getHiResBit(); // save it for the next plotted byte, just in case we need it + const int cycle = cycles-1; + const bool bit = shiftLatch(t,cycle,isText,isHiRes); + is = writeVideoSignal(shift,showLastHiRes,firstBlankedCycle,cycle,hcycle,bit,lineVis,hVis,is); + } - this->itestsig = is; + this->itestsig = is; - ++this->hpos; - if (this->hpos >= E2Const::HORIZ_CYCLES) - { - this->hpos = 0; - ++this->line; - if (this->itestsig >= this->itestsiglim) - { - this->itestsig = this->testsig; - this->display.drawCurrent(); - } - } + ++this->hpos; + if (this->hpos >= E2Const::HORIZ_CYCLES) + { + this->hpos = 0; + ++this->line; + if (this->itestsig >= this->itestsiglim) + { + this->itestsig = this->testsig; + this->display.drawCurrent(); + } + } } bool inline PictureGenerator::shiftLatch(const int t, const int cycle, const bool isText, const bool isHiRes) { - bool bit; - if (isText) - { - bit = getTextBit(); - if (cycle & 1) // @ 7MHz - { - shiftText(); - } - } - else if (isHiRes) - { - bit = getHiResBit(); - if (cycle & 1) // @ 7MHz - { - shiftHiRes(); - } - } - else // LO-RES - { - const int y = t / E2Const::BYTES_PER_ROW; - bit = getLoResBit((t & 1) == (this->line & 1), y & 4); - shiftLoRes(); - } - return bit; + bool bit; + if (isText) + { + bit = getTextBit(); + if (cycle & 1) // @ 7MHz + { + shiftText(); + } + } + else if (isHiRes) + { + bit = getHiResBit(); + if (cycle & 1) // @ 7MHz + { + shiftHiRes(); + } + } + else // LO-RES + { + const int y = t / E2Const::BYTES_PER_ROW; + bit = getLoResBit((t & 1) == (this->line & 1), y & 4); + shiftLoRes(); + } + return bit; } inline signed char* PictureGenerator::writeVideoSignal(const bool shift, const bool showLastHiRes, const int firstBlankedCycle, const int cycle, const int hcycle, const bool bit, const bool lineVis, const bool hVis, signed char* is) { - if (shift && !cycle) - { - *is++ = showLastHiRes ? AppleNTSC::WHITE_LEVEL : AppleNTSC::BLANK_LEVEL; - } + if (shift && !cycle) + { + *is++ = showLastHiRes ? AppleNTSC::WHITE_LEVEL : AppleNTSC::BLANK_LEVEL; + } - signed char sig; - if (lineVis) - { - if (hVis) - { - if (bit && cycle < firstBlankedCycle) - { - sig = AppleNTSC::WHITE_LEVEL; - } - else - { - sig = AppleNTSC::BLANK_LEVEL; - } - } - else - { - sig = hbl(hcycle); - } - } - else - { - sig = vbl(hcycle); - } - *is++ = sig; - return is; + signed char sig; + if (lineVis) + { + if (hVis) + { + if (bit && cycle < firstBlankedCycle) + { + sig = AppleNTSC::WHITE_LEVEL; + } + else + { + sig = AppleNTSC::BLANK_LEVEL; + } + } + else + { + sig = hbl(hcycle); + } + } + else + { + sig = vbl(hcycle); + } + *is++ = sig; + return is; } // TODO Just to be extremely accurate, fix picture signal values during HBL and VBL // (note that they vary by motherboard revision... there is a whole section in U.A.2) signed char inline PictureGenerator::vbl(const int hcycle) { - signed char sig; - if (224 <= this->line && this->line < 240) // VSYNC // TODO symbolize constants - { - sig = AppleNTSC::SYNC_LEVEL; - } - else - { - if (AppleNTSC::SYNC_START <= hcycle && hcycle < AppleNTSC::BP_START) - { - sig = AppleNTSC::SYNC_LEVEL; - } - else - { - sig = AppleNTSC::BLANK_LEVEL; - } - } - return sig; + signed char sig; + if (224 <= this->line && this->line < 240) // VSYNC // TODO symbolize constants + { + sig = AppleNTSC::SYNC_LEVEL; + } + else + { + if (AppleNTSC::SYNC_START <= hcycle && hcycle < AppleNTSC::BP_START) + { + sig = AppleNTSC::SYNC_LEVEL; + } + else + { + sig = AppleNTSC::BLANK_LEVEL; + } + } + return sig; } @@ -299,25 +299,25 @@ const signed char PictureGenerator::lutCB[] = signed char inline PictureGenerator::hbl(const int hcycle) { - signed char cb; - if (AppleNTSC::CB_START <= hcycle && hcycle < AppleNTSC::CB_END) - { - if (this->mode.isText() && this->revision > 0) - { - cb = AppleNTSC::BLANK_LEVEL; - } - else - { - cb = lutCB[(hcycle-AppleNTSC::CB_START)%4]; - } - } - else if (AppleNTSC::SYNC_START <= hcycle && hcycle < AppleNTSC::BP_START) - { - cb = AppleNTSC::SYNC_LEVEL; - } - else - { - cb = AppleNTSC::BLANK_LEVEL; - } - return cb; + signed char cb; + if (AppleNTSC::CB_START <= hcycle && hcycle < AppleNTSC::CB_END) + { + if (this->mode.isText() && this->revision > 0) + { + cb = AppleNTSC::BLANK_LEVEL; + } + else + { + cb = lutCB[(hcycle-AppleNTSC::CB_START)%4]; + } + } + else if (AppleNTSC::SYNC_START <= hcycle && hcycle < AppleNTSC::BP_START) + { + cb = AppleNTSC::SYNC_LEVEL; + } + else + { + cb = AppleNTSC::BLANK_LEVEL; + } + return cb; } diff --git a/src/picturegenerator.h b/src/picturegenerator.h index 7c7d9c7..5a7e6e8 100644 --- a/src/picturegenerator.h +++ b/src/picturegenerator.h @@ -24,45 +24,45 @@ class VideoMode; class PictureGenerator { private: - AnalogTV& display; - VideoMode& mode; + AnalogTV& display; + VideoMode& mode; - unsigned char latchGraphics; - bool d7; - unsigned char latchText; - unsigned int hpos; - unsigned int line; - bool lasthires; - static const signed char lutCB[]; + unsigned char latchGraphics; + bool d7; + unsigned char latchText; + unsigned int hpos; + unsigned int line; + bool lasthires; + static const signed char lutCB[]; - signed char testsig[AppleNTSC::SIGNAL_LEN]; - signed char* itestsig; - signed char* itestsiglim; + signed char testsig[AppleNTSC::SIGNAL_LEN]; + signed char* itestsig; + signed char* itestsiglim; - void shiftLoRes(); - void shiftHiRes(); - void shiftText(); - bool getTextBit(); - bool getHiResBit(); - bool getLoResBit(const bool odd, const bool vc); - void loadGraphics(const unsigned char value); - void loadText(const int value); - bool shiftLatch(const int t, const int cycle, const bool isText, const bool isHiRes); - signed char* writeVideoSignal(const bool shift, const bool showLastHiRes, const int firstBlankedCycle, const int cycle, const int hcycle, const bool bit, const bool lineVis, const bool hVis, signed char* is); - signed char vbl(const int hcycle); - signed char hbl(const int hcycle); + void shiftLoRes(); + void shiftHiRes(); + void shiftText(); + bool getTextBit(); + bool getHiResBit(); + bool getLoResBit(const bool odd, const bool vc); + void loadGraphics(const unsigned char value); + void loadText(const int value); + bool shiftLatch(const int t, const int cycle, const bool isText, const bool isHiRes); + signed char* writeVideoSignal(const bool shift, const bool showLastHiRes, const int firstBlankedCycle, const int cycle, const int hcycle, const bool bit, const bool lineVis, const bool hVis, signed char* is); + signed char vbl(const int hcycle); + signed char hbl(const int hcycle); - const unsigned int VISIBLE_X_OFFSET; + const unsigned int VISIBLE_X_OFFSET; - const int& revision; + const int& revision; public: - PictureGenerator(AnalogTV& display, VideoMode& mode, const int& revision); - ~PictureGenerator(); + PictureGenerator(AnalogTV& display, VideoMode& mode, const int& revision); + ~PictureGenerator(); - void powerOn(); - void tick(const int t, const unsigned char c); + void powerOn(); + void tick(const int t, const unsigned char c); }; #endif diff --git a/src/powerupreset.cpp b/src/powerupreset.cpp index f99bf44..bd87a68 100644 --- a/src/powerupreset.cpp +++ b/src/powerupreset.cpp @@ -20,7 +20,7 @@ #include "e2const.h" PowerUpReset::PowerUpReset(Apple2& apple): - apple(apple) + apple(apple) { } @@ -32,21 +32,21 @@ PowerUpReset::~PowerUpReset() void PowerUpReset::tick() { - if (this->pendingTicks > 0) - { - --this->pendingTicks; - if (this->pendingTicks == 0) - { - this->apple.reset(); - } - } + if (this->pendingTicks > 0) + { + --this->pendingTicks; + if (this->pendingTicks == 0) + { + this->apple.reset(); + } + } } void PowerUpReset::powerOn() { #ifdef USE_EMU - this->pendingTicks = 99; // TODO REMOVE THIS + this->pendingTicks = 99; // TODO REMOVE THIS #else - this->pendingTicks = (int)(E2Const::AVG_CPU_HZ*.3); // U.A.II, p. 7-15 + this->pendingTicks = (int)(E2Const::AVG_CPU_HZ*.3); // U.A.II, p. 7-15 #endif } diff --git a/src/powerupreset.h b/src/powerupreset.h index 4083e23..f5301dd 100644 --- a/src/powerupreset.h +++ b/src/powerupreset.h @@ -23,14 +23,14 @@ class Apple2; class PowerUpReset { private: - Apple2& apple; - int pendingTicks; + Apple2& apple; + int pendingTicks; public: - PowerUpReset(Apple2& apple); - ~PowerUpReset(); - void powerOn(); - void tick(); + PowerUpReset(Apple2& apple); + ~PowerUpReset(); + void powerOn(); + void tick(); }; #endif diff --git a/src/slots.cpp b/src/slots.cpp index 0ca9c36..919c197 100644 --- a/src/slots.cpp +++ b/src/slots.cpp @@ -20,11 +20,11 @@ #include Slots::Slots(ScreenImage& gui): - gui(gui), - empty(), - cards(8,&this->empty) + gui(gui), + empty(), + cards(8,&this->empty) { - forceGuiUpdate(); + forceGuiUpdate(); } Slots::~Slots() @@ -33,17 +33,17 @@ Slots::~Slots() unsigned char Slots::io(const int islot, const int iswch, const unsigned char b, const bool writing) { - return this->cards[islot]->io(iswch,b,writing); + return this->cards[islot]->io(iswch,b,writing); } struct Slots_Card_reset { - void operator() (Card* p) { p->reset(); } + void operator() (Card* p) { p->reset(); } }; void Slots::reset() { - std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_reset()); + std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_reset()); } struct Slots_Card_tick @@ -58,79 +58,79 @@ void Slots::tick() unsigned char Slots::readRom(const int islot, const unsigned short addr, const unsigned char data) { - return this->cards[islot]->readRom(addr,data); + return this->cards[islot]->readRom(addr,data); } struct Slots_Card_readSeventhRom { - const unsigned short addr; - unsigned char* b; - Slots_Card_readSeventhRom(const unsigned short addr, unsigned char* b):addr(addr),b(b){} - void operator() (Card* p) { p->readSeventhRom(this->addr,this->b); } + const unsigned short addr; + unsigned char* b; + Slots_Card_readSeventhRom(const unsigned short addr, unsigned char* b):addr(addr),b(b){} + void operator() (Card* p) { p->readSeventhRom(this->addr,this->b); } }; unsigned char Slots::readSeventhRom(const unsigned short addr, const unsigned char data) { - unsigned char b(data); - std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_readSeventhRom(addr,&b)); - return b; + unsigned char b(data); + std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_readSeventhRom(addr,&b)); + return b; } struct Slots_Card_ioBankRom { - const unsigned short addr; - unsigned char* b; - const bool write; - Slots_Card_ioBankRom(const unsigned short addr, unsigned char* b, const bool write):addr(addr),b(b),write(write){} - void operator() (Card* p) { p->ioBankRom(this->addr,this->b,this->write); } + const unsigned short addr; + unsigned char* b; + const bool write; + Slots_Card_ioBankRom(const unsigned short addr, unsigned char* b, const bool write):addr(addr),b(b),write(write){} + void operator() (Card* p) { p->ioBankRom(this->addr,this->b,this->write); } }; unsigned char Slots::ioBankRom(const unsigned short addr, const unsigned char data, const bool write) { - unsigned char b(data); - std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_ioBankRom(addr,&b,write)); - return b; + unsigned char b(data); + std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_ioBankRom(addr,&b,write)); + return b; } struct Slots_Card_inhibitMotherboardRom { - bool inhibit; - Slots_Card_inhibitMotherboardRom():inhibit(false) { } - void operator() (Card* p) { if (p->inhibitMotherboardRom()) { inhibit = true; }} + bool inhibit; + Slots_Card_inhibitMotherboardRom():inhibit(false) { } + void operator() (Card* p) { if (p->inhibitMotherboardRom()) { inhibit = true; }} }; bool Slots::inhibitMotherboardRom() { - return std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_inhibitMotherboardRom()).inhibit; + return std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_inhibitMotherboardRom()).inhibit; } void Slots::set(const int slot, Card* card) { - remove(slot); - this->cards[slot] = card; - this->gui.updateSlotName(slot,this->cards[slot]); + remove(slot); + this->cards[slot] = card; + this->gui.updateSlotName(slot,this->cards[slot]); } void Slots::remove(const int slot) { - if (this->cards[slot] != &this->empty) - { - delete this->cards[slot]; - this->cards[slot] = &this->empty; - this->gui.removeCard(slot,this->cards[slot]); - } + if (this->cards[slot] != &this->empty) + { + delete this->cards[slot]; + this->cards[slot] = &this->empty; + this->gui.removeCard(slot,this->cards[slot]); + } } Card* Slots::get(const int slot) { - return this->cards[slot]; + return this->cards[slot]; } void Slots::forceGuiUpdate() { - for (int slot(0); slot < 8; ++slot) - this->gui.updateSlotName(slot,this->cards[slot]); + for (int slot(0); slot < 8; ++slot) + this->gui.updateSlotName(slot,this->cards[slot]); } void Slots::save(int unit) { @@ -142,26 +142,26 @@ void Slots::save(int unit) { /* struct isAnyDiskDriveMotorOnCard { - bool on; - isAnyDiskDriveMotorOnCard():on(false) {} - void operator() (Card* p) { if (p->isMotorOn()) on = true; } + bool on; + isAnyDiskDriveMotorOnCard():on(false) {} + void operator() (Card* p) { if (p->isMotorOn()) on = true; } }; bool isAnyDiskDriveMotorOn() { - isAnyDiskDriveMotorOnCard on = isAnyDiskDriveMotorOnCard(); - std::for_each(this->cards.begin(),this->cards.end(),inh); - return on.inhibit; + isAnyDiskDriveMotorOnCard on = isAnyDiskDriveMotorOnCard(); + std::for_each(this->cards.begin(),this->cards.end(),inh); + return on.inhibit; } */ struct Slots_Card_isDirty { - bool dirty; - Slots_Card_isDirty():dirty(false) {} - void operator() (Card* p) { if (p->isDirty()) dirty = true; } + bool dirty; + Slots_Card_isDirty():dirty(false) {} + void operator() (Card* p) { if (p->isDirty()) dirty = true; } }; bool Slots::isDirty() { - return std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_isDirty()).dirty; + return std::for_each(this->cards.begin(),this->cards.end(),Slots_Card_isDirty()).dirty; } diff --git a/src/slots.h b/src/slots.h index c09e8a1..515d02c 100644 --- a/src/slots.h +++ b/src/slots.h @@ -27,25 +27,25 @@ class ScreenImage; class Slots { private: - ScreenImage& gui; - EmptySlot empty; - std::vector cards; + ScreenImage& gui; + EmptySlot empty; + std::vector cards; public: - Slots(ScreenImage& gui); - ~Slots(); + Slots(ScreenImage& gui); + ~Slots(); void tick(); - unsigned char io(const int islot, const int iswch, const unsigned char b, const bool writing); - void reset(); - unsigned char readRom(const int islot, const unsigned short addr, const unsigned char data); - unsigned char readSeventhRom(const unsigned short addr, const unsigned char data); - unsigned char ioBankRom(const unsigned short addr, const unsigned char data, const bool write); - bool inhibitMotherboardRom(); - void set(const int slot, Card* card); - Card* get(const int slot); - void remove(const int slot); - bool isDirty(); + unsigned char io(const int islot, const int iswch, const unsigned char b, const bool writing); + void reset(); + unsigned char readRom(const int islot, const unsigned short addr, const unsigned char data); + unsigned char readSeventhRom(const unsigned short addr, const unsigned char data); + unsigned char ioBankRom(const unsigned short addr, const unsigned char data, const bool write); + bool inhibitMotherboardRom(); + void set(const int slot, Card* card); + Card* get(const int slot); + void remove(const int slot); + bool isDirty(); void save(int unit); void forceGuiUpdate(); }; diff --git a/src/speakerclicker.cpp b/src/speakerclicker.cpp index 60e3135..6a337a6 100644 --- a/src/speakerclicker.cpp +++ b/src/speakerclicker.cpp @@ -29,22 +29,22 @@ SpeakerClicker::SpeakerClicker(): clicked(false), t(TICKS_PER_SAMPLE), positive(false) { - SDL_AudioSpec audio; + SDL_AudioSpec audio; audio.freq = E2Const::AVG_CPU_HZ/TICKS_PER_SAMPLE; // samples per second - audio.format = AUDIO_U8; // 8 bits (1 byte) per sample - audio.channels = 1; // mono + audio.format = AUDIO_U8; // 8 bits (1 byte) per sample + audio.channels = 1; // mono audio.silence = 128; audio.samples = 512; audio.callback = 0; - audio.userdata = 0; + audio.userdata = 0; SDL_AudioSpec obtained; - obtained.callback = 0; - obtained.userdata = 0; + obtained.callback = 0; + obtained.userdata = 0; this->devid = SDL_OpenAudioDevice(0,0,&audio,&obtained,0); if (!this->devid) { - std::cerr << "Unable to initialize audio: " << SDL_GetError() << std::endl; + std::cerr << "Unable to initialize audio: " << SDL_GetError() << std::endl; } else { this->silence = obtained.silence; @@ -57,27 +57,27 @@ SpeakerClicker::SpeakerClicker(): std::cout << " samples: " << obtained.samples << std::endl; std::cout << " size: " << obtained.size << std::endl; SDL_PauseAudioDevice(this->devid, 0); - } + } } SpeakerClicker::~SpeakerClicker() { if (!this->devid) { - return; - } + return; + } SDL_CloseAudioDevice(this->devid); } void SpeakerClicker::tick() { if (!this->devid) { - return; - } + return; + } if (!--this->t) { std::uint8_t amp; if (this->clicked) { amp = this->positive ? this->silence+100u : this->silence-100u; - this->positive = !this->positive; + this->positive = !this->positive; this->clicked = false; } else { amp = silence; @@ -91,5 +91,5 @@ void SpeakerClicker::tick() { } void SpeakerClicker::click() { - this->clicked = true; + this->clicked = true; } diff --git a/src/speakerclicker.h b/src/speakerclicker.h index 2b9e622..2ba9a9b 100644 --- a/src/speakerclicker.h +++ b/src/speakerclicker.h @@ -28,12 +28,12 @@ class SpeakerClicker { std::uint8_t silence; bool clicked; std::uint8_t t; - bool positive; + bool positive; public: - SpeakerClicker(); - ~SpeakerClicker(); - void tick(); - void click(); + SpeakerClicker(); + ~SpeakerClicker(); + void tick(); + void click(); }; #endif diff --git a/src/standardin.cpp b/src/standardin.cpp index 54c8d2b..2120dff 100644 --- a/src/standardin.cpp +++ b/src/standardin.cpp @@ -18,8 +18,8 @@ #include "standardin.h" StandardIn::StandardIn(): - latch(0), - gotEOF(false) + latch(0), + gotEOF(false) { } @@ -33,33 +33,33 @@ StandardIn::~StandardIn() unsigned char StandardIn::io(const unsigned short address, const unsigned char data, const bool writing) { - int sw = address & 0x0F; - if (sw == 0) - { - if (!(this->latch & 0x80)) - { - if (this->gotEOF) - { - this->latch = 0xFF; - } - else - { - if (!this->producer.getKeys().empty()) - { - this->latch = this->producer.getKeys().front(); - this->producer.getKeys().pop(); - if (this->latch == 0xFF) - { - this->gotEOF = true; - } - this->latch |= 0x80; - } - } - } - } - else if (sw == 1) - { - this->latch &= 0x7F; - } - return this->latch; + int sw = address & 0x0F; + if (sw == 0) + { + if (!(this->latch & 0x80)) + { + if (this->gotEOF) + { + this->latch = 0xFF; + } + else + { + if (!this->producer.getKeys().empty()) + { + this->latch = this->producer.getKeys().front(); + this->producer.getKeys().pop(); + if (this->latch == 0xFF) + { + this->gotEOF = true; + } + this->latch |= 0x80; + } + } + } + } + else if (sw == 1) + { + this->latch &= 0x7F; + } + return this->latch; } diff --git a/src/standardin.h b/src/standardin.h index 461b9d4..bc86ce7 100644 --- a/src/standardin.h +++ b/src/standardin.h @@ -24,16 +24,16 @@ class StandardIn : public Card { private: - StandardInProducer producer; - unsigned char latch; - bool gotEOF; + StandardInProducer producer; + unsigned char latch; + bool gotEOF; public: - StandardIn(); - ~StandardIn(); + StandardIn(); + ~StandardIn(); - virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); - virtual std::string getName() { return "standard input"; } + virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); + virtual std::string getName() { return "standard input"; } }; #endif diff --git a/src/standardinproducer.cpp b/src/standardinproducer.cpp index e2bcfb4..64f7f1f 100644 --- a/src/standardinproducer.cpp +++ b/src/standardinproducer.cpp @@ -28,71 +28,71 @@ enum state_t { START, GOT_CR, GOT_LF, GOT_EOF }; static int readInput(void *voidkeys) { - KeypressQueue* keys = (KeypressQueue*)voidkeys; + KeypressQueue* keys = (KeypressQueue*)voidkeys; - /* - * Continuously read characters from standard in - * and put them onto the queue. - * Stop when we hit EOF (placing EOF onto the queue). - * Translate LF to CR. - * If we hit a CR immediately after a LF, drop it. - * If we hit a LF immediately after a CR, drop it. - */ - enum state_t state = START; + /* + * Continuously read characters from standard in + * and put them onto the queue. + * Stop when we hit EOF (placing EOF onto the queue). + * Translate LF to CR. + * If we hit a CR immediately after a LF, drop it. + * If we hit a LF immediately after a CR, drop it. + */ + enum state_t state = START; - while (state != GOT_EOF) - { - char c = std::cin.get(); - c &= 0x7F; - if (!std::cin.good()) - { - state = GOT_EOF; - keys->push(0xFF); - } - else - { - if (state == START) - { - if (c == CR) - { - state = GOT_CR; - } - else if (c == LF) - { - state = GOT_LF; - c = CR; - } - keys->push(c); - } - else if (state == GOT_CR) - { - if (c != LF) - { - keys->push(c); - } - state = START; - } - else if (state == GOT_LF) - { - if (c != CR) - { - if (c == LF) - { - c = CR; - } - keys->push(c); - } - state = START; - } - } - } + while (state != GOT_EOF) + { + char c = std::cin.get(); + c &= 0x7F; + if (!std::cin.good()) + { + state = GOT_EOF; + keys->push(0xFF); + } + else + { + if (state == START) + { + if (c == CR) + { + state = GOT_CR; + } + else if (c == LF) + { + state = GOT_LF; + c = CR; + } + keys->push(c); + } + else if (state == GOT_CR) + { + if (c != LF) + { + keys->push(c); + } + state = START; + } + else if (state == GOT_LF) + { + if (c != CR) + { + if (c == LF) + { + c = CR; + } + keys->push(c); + } + state = START; + } + } + } - return 0; + return 0; } StandardInProducer::StandardInProducer() { - SDL_CreateThread(readInput,"stdin",&this->keys); + SDL_CreateThread(readInput,"stdin",&this->keys); } StandardInProducer::~StandardInProducer() diff --git a/src/standardinproducer.h b/src/standardinproducer.h index 7e94e60..2421441 100644 --- a/src/standardinproducer.h +++ b/src/standardinproducer.h @@ -23,13 +23,13 @@ class StandardInProducer { private: - KeypressQueue keys; + KeypressQueue keys; public: - StandardInProducer(); - ~StandardInProducer(); + StandardInProducer(); + ~StandardInProducer(); - KeypressQueue& getKeys() { return this->keys; } + KeypressQueue& getKeys() { return this->keys; } }; #endif diff --git a/src/standardout.cpp b/src/standardout.cpp index 549ff98..67b4a51 100644 --- a/src/standardout.cpp +++ b/src/standardout.cpp @@ -30,21 +30,21 @@ StandardOut::~StandardOut() unsigned char StandardOut::io(const unsigned short address, const unsigned char data, const bool writing) { - if (!writing) - { - return data; - } + if (!writing) + { + return data; + } - const char c = (char)(data&0x7F); - if (c == '\r') - { - std::cout << std::endl; - } - else - { - std::cout << c; - } - std::cout << std::flush; + const char c = (char)(data&0x7F); + if (c == '\r') + { + std::cout << std::endl; + } + else + { + std::cout << c; + } + std::cout << std::flush; - return data; + return data; } diff --git a/src/standardout.h b/src/standardout.h index f7c1515..b7f2c8d 100644 --- a/src/standardout.h +++ b/src/standardout.h @@ -24,11 +24,11 @@ class StandardOut : public Card { public: - StandardOut(); - ~StandardOut(); + StandardOut(); + ~StandardOut(); - virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); - virtual std::string getName() { return "standard output"; } + virtual unsigned char io(const unsigned short address, const unsigned char data, const bool writing); + virtual std::string getName() { return "standard output"; } }; #endif diff --git a/src/textcharacters.cpp b/src/textcharacters.cpp index f8ef4a6..c44f197 100644 --- a/src/textcharacters.cpp +++ b/src/textcharacters.cpp @@ -18,28 +18,28 @@ #include "textcharacters.h" TextCharacters::TextCharacters(): - rows(0x40*8) + rows(0x40*8) { - int r(0); + int r(0); - const char *pi = + const char *pi = #include "textcharacterimages.h" - ; + ; - for (int ch(0); ch < 0x40; ++ch) - { + for (int ch(0); ch < 0x40; ++ch) + { - rows[r] = 0; - ++r; - for (int ln(1); ln < 8; ++ln) - { - for (int bt(0); bt < 5; ++bt) - { - rows[r] >>= 1; - if (*pi++=='@') - rows[r] |= 0x20; - } - ++r; - } - } + rows[r] = 0; + ++r; + for (int ln(1); ln < 8; ++ln) + { + for (int bt(0); bt < 5; ++bt) + { + rows[r] >>= 1; + if (*pi++=='@') + rows[r] |= 0x20; + } + ++r; + } + } } diff --git a/src/textcharacters.h b/src/textcharacters.h index e02e0ce..6930aae 100644 --- a/src/textcharacters.h +++ b/src/textcharacters.h @@ -23,15 +23,15 @@ class TextCharacters { private: - std::vector rows; + std::vector rows; public: - TextCharacters(); - ~TextCharacters() {} - unsigned char get(unsigned int iRow) - { - return this->rows[iRow]; - } + TextCharacters(); + ~TextCharacters() {} + unsigned char get(unsigned int iRow) + { + return this->rows[iRow]; + } }; #endif diff --git a/src/timable.h b/src/timable.h index 04b4feb..47f2e6e 100644 --- a/src/timable.h +++ b/src/timable.h @@ -21,10 +21,10 @@ class Timable { public: - Timable() {} - virtual ~Timable() {} + Timable() {} + virtual ~Timable() {} - virtual void tick() = 0; + virtual void tick() = 0; }; #endif diff --git a/src/tinyfiledialogs.cpp b/src/tinyfiledialogs.cpp index bdfc607..4f596f0 100644 --- a/src/tinyfiledialogs.cpp +++ b/src/tinyfiledialogs.cpp @@ -184,14 +184,14 @@ char tinyfd_needs[] = "\ static int getenvDISPLAY(void) { - return tinyfd_assumeGraphicDisplay || getenv("DISPLAY"); + return tinyfd_assumeGraphicDisplay || getenv("DISPLAY"); } static char * getCurDir(void) { - static char lCurDir[MAX_PATH_OR_CMD]; - return getcwd(lCurDir, sizeof(lCurDir)); + static char lCurDir[MAX_PATH_OR_CMD]; + return getcwd(lCurDir, sizeof(lCurDir)); } @@ -403,63 +403,63 @@ static void wipefile(char const * aFilename) int tfd_quoteDetected(char const * aString) { - char const * p; + char const * p; - if (!aString) return 0; + if (!aString) return 0; - p = aString; - while ((p = strchr(p, '\''))) - { - return 1; - } + p = aString; + while ((p = strchr(p, '\''))) + { + return 1; + } - p = aString; - while ((p = strchr(p, '\"'))) - { - return 1; - } + p = aString; + while ((p = strchr(p, '\"'))) + { + return 1; + } - return 0; + return 0; } char const * tinyfd_getGlobalChar(char const * aCharVariableName) /* to be called from C# (you don't need this in C or C++) */ { - if (!aCharVariableName || !strlen(aCharVariableName)) return NULL; - else if (!strcmp(aCharVariableName, "tinyfd_version")) return tinyfd_version; - else if (!strcmp(aCharVariableName, "tinyfd_needs")) return tinyfd_needs; - else if (!strcmp(aCharVariableName, "tinyfd_response")) return tinyfd_response; - else return NULL ; + if (!aCharVariableName || !strlen(aCharVariableName)) return NULL; + else if (!strcmp(aCharVariableName, "tinyfd_version")) return tinyfd_version; + else if (!strcmp(aCharVariableName, "tinyfd_needs")) return tinyfd_needs; + else if (!strcmp(aCharVariableName, "tinyfd_response")) return tinyfd_response; + else return NULL ; } int tinyfd_getGlobalInt(char const * aIntVariableName) /* to be called from C# (you don't need this in C or C++) */ { - if ( !aIntVariableName || !strlen(aIntVariableName) ) return -1 ; - else if ( !strcmp(aIntVariableName, "tinyfd_verbose") ) return tinyfd_verbose ; - else if ( !strcmp(aIntVariableName, "tinyfd_silent") ) return tinyfd_silent ; - else if ( !strcmp(aIntVariableName, "tinyfd_allowCursesDialogs") ) return tinyfd_allowCursesDialogs ; - else if ( !strcmp(aIntVariableName, "tinyfd_forceConsole") ) return tinyfd_forceConsole ; - else if ( !strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay") ) return tinyfd_assumeGraphicDisplay ; + if ( !aIntVariableName || !strlen(aIntVariableName) ) return -1 ; + else if ( !strcmp(aIntVariableName, "tinyfd_verbose") ) return tinyfd_verbose ; + else if ( !strcmp(aIntVariableName, "tinyfd_silent") ) return tinyfd_silent ; + else if ( !strcmp(aIntVariableName, "tinyfd_allowCursesDialogs") ) return tinyfd_allowCursesDialogs ; + else if ( !strcmp(aIntVariableName, "tinyfd_forceConsole") ) return tinyfd_forceConsole ; + else if ( !strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay") ) return tinyfd_assumeGraphicDisplay ; #ifdef _WIN32 - else if ( !strcmp(aIntVariableName, "tinyfd_winUtf8") ) return tinyfd_winUtf8 ; + else if ( !strcmp(aIntVariableName, "tinyfd_winUtf8") ) return tinyfd_winUtf8 ; #endif - else return -1; + else return -1; } int tinyfd_setGlobalInt(char const * aIntVariableName, int aValue) /* to be called from C# (you don't need this in C or C++) */ { - if (!aIntVariableName || !strlen(aIntVariableName)) return -1 ; - else if (!strcmp(aIntVariableName, "tinyfd_verbose")) { tinyfd_verbose = aValue; return tinyfd_verbose; } - else if (!strcmp(aIntVariableName, "tinyfd_silent")) { tinyfd_silent = aValue; return tinyfd_silent; } - else if (!strcmp(aIntVariableName, "tinyfd_allowCursesDialogs")) { tinyfd_allowCursesDialogs = aValue; return tinyfd_allowCursesDialogs; } - else if (!strcmp(aIntVariableName, "tinyfd_forceConsole")) { tinyfd_forceConsole = aValue; return tinyfd_forceConsole; } - else if (!strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay")) { tinyfd_assumeGraphicDisplay = aValue; return tinyfd_assumeGraphicDisplay; } + if (!aIntVariableName || !strlen(aIntVariableName)) return -1 ; + else if (!strcmp(aIntVariableName, "tinyfd_verbose")) { tinyfd_verbose = aValue; return tinyfd_verbose; } + else if (!strcmp(aIntVariableName, "tinyfd_silent")) { tinyfd_silent = aValue; return tinyfd_silent; } + else if (!strcmp(aIntVariableName, "tinyfd_allowCursesDialogs")) { tinyfd_allowCursesDialogs = aValue; return tinyfd_allowCursesDialogs; } + else if (!strcmp(aIntVariableName, "tinyfd_forceConsole")) { tinyfd_forceConsole = aValue; return tinyfd_forceConsole; } + else if (!strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay")) { tinyfd_assumeGraphicDisplay = aValue; return tinyfd_assumeGraphicDisplay; } #ifdef _WIN32 - else if (!strcmp(aIntVariableName, "tinyfd_winUtf8")) { tinyfd_winUtf8 = aValue; return tinyfd_winUtf8; } + else if (!strcmp(aIntVariableName, "tinyfd_winUtf8")) { tinyfd_winUtf8 = aValue; return tinyfd_winUtf8; } #endif - else return -1; + else return -1; } @@ -527,18 +527,18 @@ static int windowsVersion(void) static void replaceChr(char * aString, char aOldChr, char aNewChr) { - char * p; + char * p; - if (!aString) return; - if (aOldChr == aNewChr) return; + if (!aString) return; + if (aOldChr == aNewChr) return; - p = aString; - while ((p = strchr(p, aOldChr))) - { - *p = aNewChr; - p++; - } - return; + p = aString; + while ((p = strchr(p, aOldChr))) + { + *p = aNewChr; + p++; + } + return; } @@ -549,31 +549,31 @@ static void replaceChr(char * aString, char aOldChr, char aNewChr) static int sizeUtf16From8(char const * aUtf8string) { - return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, - aUtf8string, -1, NULL, 0); + return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + aUtf8string, -1, NULL, 0); } static int sizeUtf16FromMbcs(char const * aMbcsString) { - return MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, - aMbcsString, -1, NULL, 0); + return MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, + aMbcsString, -1, NULL, 0); } static int sizeUtf8(wchar_t const * aUtf16string) { - return WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, - aUtf16string, -1, NULL, 0, NULL, NULL); + return WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, + aUtf16string, -1, NULL, 0, NULL, NULL); } static int sizeMbcs(wchar_t const * aMbcsString) { - int lRes = WideCharToMultiByte(CP_ACP, 0, - aMbcsString, -1, NULL, 0, NULL, NULL); - /* DWORD licic = GetLastError(); */ - return lRes; + int lRes = WideCharToMultiByte(CP_ACP, 0, + aMbcsString, -1, NULL, 0, NULL, NULL); + /* DWORD licic = GetLastError(); */ + return lRes; } @@ -597,12 +597,12 @@ wchar_t* tinyfd_mbcsTo16(char const* aMbcsString) wchar_t * tinyfd_utf8to16(char const * aUtf8string) { - static wchar_t * lUtf16string = NULL; - int lSize; + static wchar_t * lUtf16string = NULL; + int lSize; - free(lUtf16string); - if (!aUtf8string) {lUtf16string = NULL; return NULL;} - lSize = sizeUtf16From8(aUtf8string); + free(lUtf16string); + if (!aUtf8string) {lUtf16string = NULL; return NULL;} + lSize = sizeUtf16From8(aUtf8string); if (lSize) { lUtf16string = (wchar_t*)malloc(lSize * sizeof(wchar_t)); @@ -621,53 +621,53 @@ wchar_t * tinyfd_utf8to16(char const * aUtf8string) char * tinyfd_utf16toMbcs(wchar_t const * aUtf16string) { - static char * lMbcsString = NULL; - int lSize; + static char * lMbcsString = NULL; + int lSize; - free(lMbcsString); - if (!aUtf16string) { lMbcsString = NULL; return NULL; } - lSize = sizeMbcs(aUtf16string); + free(lMbcsString); + if (!aUtf16string) { lMbcsString = NULL; return NULL; } + lSize = sizeMbcs(aUtf16string); if (lSize) { lMbcsString = (char*)malloc(lSize); lSize = WideCharToMultiByte(CP_ACP, 0, aUtf16string, -1, lMbcsString, lSize, NULL, NULL); } else strcpy(lMbcsString, ""); - return lMbcsString; + return lMbcsString; } char * tinyfd_utf8toMbcs(char const * aUtf8string) { - wchar_t const * lUtf16string; - lUtf16string = tinyfd_utf8to16(aUtf8string); - return tinyfd_utf16toMbcs(lUtf16string); + wchar_t const * lUtf16string; + lUtf16string = tinyfd_utf8to16(aUtf8string); + return tinyfd_utf16toMbcs(lUtf16string); } char * tinyfd_utf16to8(wchar_t const * aUtf16string) { - static char * lUtf8string = NULL; - int lSize; + static char * lUtf8string = NULL; + int lSize; - free(lUtf8string); - if (!aUtf16string) { lUtf8string = NULL; return NULL; } - lSize = sizeUtf8(aUtf16string); + free(lUtf8string); + if (!aUtf16string) { lUtf8string = NULL; return NULL; } + lSize = sizeUtf8(aUtf16string); if (lSize) { lUtf8string = (char*)malloc(lSize); lSize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, aUtf16string, -1, lUtf8string, lSize, NULL, NULL); } else strcpy(lUtf8string, ""); - return lUtf8string; + return lUtf8string; } char * tinyfd_mbcsTo8(char const * aMbcsString) { - wchar_t const * lUtf16string; - lUtf16string = tinyfd_mbcsTo16(aMbcsString); - return tinyfd_utf16to8(lUtf16string); + wchar_t const * lUtf16string; + lUtf16string = tinyfd_mbcsTo16(aMbcsString); + return tinyfd_utf16to8(lUtf16string); } @@ -790,9 +790,9 @@ static void Hex2RGBW(wchar_t const aHexRGB[8], unsigned char aoResultRGB[3]) static void RGB2HexW( unsigned char const aRGB[3], wchar_t aoResultHexRGB[8]) { #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__) - wchar_t const * const lPrintFormat = L"#%02hhx%02hhx%02hhx"; + wchar_t const * const lPrintFormat = L"#%02hhx%02hhx%02hhx"; #else - wchar_t const * const lPrintFormat = L"#%02hx%02hx%02hx"; + wchar_t const * const lPrintFormat = L"#%02hx%02hx%02hx"; #endif if (aoResultHexRGB) @@ -801,9 +801,9 @@ static void RGB2HexW( unsigned char const aRGB[3], wchar_t aoResultHexRGB[8]) { /* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); */ #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) - swprintf(aoResultHexRGB, 8, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]); + swprintf(aoResultHexRGB, 8, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]); #else - swprintf(aoResultHexRGB, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]); + swprintf(aoResultHexRGB, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]); #endif } @@ -826,19 +826,19 @@ static int dirExists(char const * aDirPath) #endif wchar_t * lTmpWChar; int lStatRet; - size_t lDirLen; + size_t lDirLen; - if (!aDirPath) - return 0; - lDirLen = strlen(aDirPath); - if (!lDirLen) - return 1; - if ( (lDirLen == 2) && (aDirPath[1] == ':') ) - return 1; + if (!aDirPath) + return 0; + lDirLen = strlen(aDirPath); + if (!lDirLen) + return 1; + if ( (lDirLen == 2) && (aDirPath[1] == ':') ) + return 1; if (tinyfd_winUtf8) { - lTmpWChar = tinyfd_utf8to16(aDirPath); + lTmpWChar = tinyfd_utf8to16(aDirPath); #if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3) lStatRet = _wstat(lTmpWChar, &lInfo); #else @@ -882,7 +882,7 @@ static int fileExists(char const * aFilePathAndName) if (tinyfd_winUtf8) { - lTmpWChar = tinyfd_utf8to16(aFilePathAndName); + lTmpWChar = tinyfd_utf8to16(aFilePathAndName); #if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3) lStatRet = _wstat(lTmpWChar, &lInfo); #else @@ -909,53 +909,53 @@ static int fileExists(char const * aFilePathAndName) } static void replaceWchar(wchar_t * aString, - wchar_t aOldChr, - wchar_t aNewChr) + wchar_t aOldChr, + wchar_t aNewChr) { - wchar_t * p; + wchar_t * p; - if (!aString) - { - return ; - } + if (!aString) + { + return ; + } - if (aOldChr == aNewChr) - { - return ; - } + if (aOldChr == aNewChr) + { + return ; + } - p = aString; - while ((p = wcsrchr(p, aOldChr))) - { - *p = aNewChr; + p = aString; + while ((p = wcsrchr(p, aOldChr))) + { + *p = aNewChr; #ifdef TINYFD_NOCCSUNICODE - p++; + p++; #endif - p++; - } - return ; + p++; + } + return ; } static int quoteDetectedW(wchar_t const * aString) { - wchar_t const * p; + wchar_t const * p; - if (!aString) return 0; + if (!aString) return 0; - p = aString; - while ((p = wcsrchr(p, L'\''))) - { - return 1; - } + p = aString; + while ((p = wcsrchr(p, L'\''))) + { + return 1; + } - p = aString; - while ((p = wcsrchr(p, L'\"'))) - { - return 1; - } + p = aString; + while ((p = wcsrchr(p, L'\"'))) + { + return 1; + } - return 0; + return 0; } #endif /* _WIN32 */ @@ -1065,8 +1065,8 @@ int tinyfd_messageBoxW( if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return 1; } - if (quoteDetectedW(aTitle)) return tinyfd_messageBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton); - if (quoteDetectedW(aMessage)) return tinyfd_messageBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton); + if (quoteDetectedW(aTitle)) return tinyfd_messageBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton); + if (quoteDetectedW(aMessage)) return tinyfd_messageBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton); if (aIconType && !wcscmp(L"warning", aIconType)) { @@ -1155,7 +1155,7 @@ int tinyfd_notifyPopupW( if (aTitle && !wcscmp(aTitle, L"tinyfd_query")) { strcpy(tinyfd_response, "windows_wchar"); return 1; } if (quoteDetectedW(aTitle)) return tinyfd_notifyPopupW(L"INVALID TITLE WITH QUOTES", aMessage, aIconType); - if (quoteDetectedW(aMessage)) return tinyfd_notifyPopupW(aTitle, L"INVALID MESSAGE WITH QUOTES", aIconType); + if (quoteDetectedW(aMessage)) return tinyfd_notifyPopupW(aTitle, L"INVALID MESSAGE WITH QUOTES", aIconType); lTitleLen = aTitle ? wcslen(aTitle) : 0; lMessageLen = aMessage ? wcslen(aMessage) : 0; @@ -1230,9 +1230,9 @@ wchar_t * tinyfd_inputBoxW( if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } - if (quoteDetectedW(aTitle)) return tinyfd_inputBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDefaultInput); - if (quoteDetectedW(aMessage)) return tinyfd_inputBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDefaultInput); - if (quoteDetectedW(aDefaultInput)) return tinyfd_inputBoxW(aTitle, aMessage, L"INVALID DEFAULT_INPUT WITH QUOTES"); + if (quoteDetectedW(aTitle)) return tinyfd_inputBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDefaultInput); + if (quoteDetectedW(aMessage)) return tinyfd_inputBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDefaultInput); + if (quoteDetectedW(aDefaultInput)) return tinyfd_inputBoxW(aTitle, aMessage, L"INVALID DEFAULT_INPUT WITH QUOTES"); lTitleLen = aTitle ? wcslen(aTitle) : 0 ; lMessageLen = aMessage ? wcslen(aMessage) : 0 ; @@ -1241,7 +1241,7 @@ wchar_t * tinyfd_inputBoxW( if (aDefaultInput) { - swprintf(lDialogString, + swprintf(lDialogString, #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) lDialogStringLen, #endif @@ -1267,16 +1267,16 @@ wchar_t * tinyfd_inputBoxW( wcscpy(lDialogString, L"Dim result:result=InputBox(\""); if (aMessage && wcslen(aMessage)) { - wcscpy(lBuff, aMessage); - replaceWchar(lBuff, L'\n', L' '); - wcscat(lDialogString, lBuff); + wcscpy(lBuff, aMessage); + replaceWchar(lBuff, L'\n', L' '); + wcscat(lDialogString, lBuff); } wcscat(lDialogString, L"\",\"tinyfiledialogsTopWindow\",\""); if (aDefaultInput && wcslen(aDefaultInput)) { - wcscpy(lBuff, aDefaultInput); - replaceWchar(lBuff, L'\n', L' '); - wcscat(lDialogString, lBuff); + wcscpy(lBuff, aDefaultInput); + replaceWchar(lBuff, L'\n', L' '); + wcscat(lDialogString, lBuff); } wcscat(lDialogString, L"\"):If IsEmpty(result) then:WScript.Echo 0"); wcscat(lDialogString, L":Else: WScript.Echo \"1\" & result : End If"); @@ -1391,13 +1391,13 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ L"%ls\\tinyfd.txt",_wgetenv(L"TEMP")); #ifdef TINYFD_NOCCSUNICODE - lFile = _wfopen(lDialogString, L"w"); - fputc(0xFF, lFile); - fputc(0xFE, lFile); + lFile = _wfopen(lDialogString, L"w"); + fputc(0xFF, lFile); + fputc(0xFE, lFile); #else - lFile = _wfopen(lDialogString, L"wt, ccs=UNICODE"); /*or ccs=UTF-16LE*/ + lFile = _wfopen(lDialogString, L"wt, ccs=UNICODE"); /*or ccs=UTF-16LE*/ #endif - fclose(lFile); + fclose(lFile); wcscpy(lDialogString, L"cmd.exe /c cscript.exe //U //Nologo "); wcscat(lDialogString, L"\"%TEMP%\\tinyfd.vbs\" "); @@ -1417,33 +1417,33 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) lDialogStringLen, #endif - L"%ls\\tinyfd.txt", _wgetenv(L"TEMP")); - /* wprintf(L"lDialogString: %ls\n", lDialogString); */ + L"%ls\\tinyfd.txt", _wgetenv(L"TEMP")); + /* wprintf(L"lDialogString: %ls\n", lDialogString); */ #ifdef TINYFD_NOCCSUNICODE - if (!(lIn = _wfopen(lDialogString, L"r"))) + if (!(lIn = _wfopen(lDialogString, L"r"))) #else - if (!(lIn = _wfopen(lDialogString, L"rt, ccs=UNICODE"))) /*or ccs=UTF-16LE*/ + if (!(lIn = _wfopen(lDialogString, L"rt, ccs=UNICODE"))) /*or ccs=UTF-16LE*/ #endif - { + { _wremove(lDialogString); free(lDialogString); return NULL; } - memset(lBuff, 0, MAX_PATH_OR_CMD * sizeof(wchar_t) ); + memset(lBuff, 0, MAX_PATH_OR_CMD * sizeof(wchar_t) ); #ifdef TINYFD_NOCCSUNICODE - fgets((char *)lBuff, 2*MAX_PATH_OR_CMD, lIn); + fgets((char *)lBuff, 2*MAX_PATH_OR_CMD, lIn); #else - fgetws(lBuff, MAX_PATH_OR_CMD, lIn); + fgetws(lBuff, MAX_PATH_OR_CMD, lIn); #endif - fclose(lIn); - wipefileW(lDialogString); - _wremove(lDialogString); + fclose(lIn); + wipefileW(lDialogString); + _wremove(lDialogString); - if (aDefaultInput) - { - swprintf(lDialogString, + if (aDefaultInput) + { + swprintf(lDialogString, #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) lDialogStringLen, #endif @@ -1461,9 +1461,9 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ free(lDialogString); /* wprintf( L"lBuff: %ls\n" , lBuff ) ; */ #ifdef TINYFD_NOCCSUNICODE - lResult = !wcsncmp(lBuff+1, L"1", 1); + lResult = !wcsncmp(lBuff+1, L"1", 1); #else - lResult = !wcsncmp(lBuff, L"1", 1); + lResult = !wcsncmp(lBuff, L"1", 1); #endif /* printf( "lResult: %d \n" , lResult ) ; */ @@ -1475,16 +1475,16 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ /* wprintf( "lBuff+1: %ls\n" , lBuff+1 ) ; */ #ifdef TINYFD_NOCCSUNICODE - if (aDefaultInput) - { - lDialogStringLen = wcslen(lBuff) ; - lBuff[lDialogStringLen - 1] = L'\0'; - lBuff[lDialogStringLen - 2] = L'\0'; - } - return lBuff + 2; + if (aDefaultInput) + { + lDialogStringLen = wcslen(lBuff) ; + lBuff[lDialogStringLen - 1] = L'\0'; + lBuff[lDialogStringLen - 2] = L'\0'; + } + return lBuff + 2; #else - if (aDefaultInput) lBuff[wcslen(lBuff) - 1] = L'\0'; - return lBuff + 1; + if (aDefaultInput) lBuff[wcslen(lBuff) - 1] = L'\0'; + return lBuff + 1; #endif } @@ -1502,20 +1502,20 @@ wchar_t * tinyfd_saveFileDialogW( wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L""; wchar_t * p; wchar_t * lRetval; - wchar_t const * ldefExt = NULL; - int i; + wchar_t const * ldefExt = NULL; + int i; HRESULT lHResult; OPENFILENAMEW ofn = {0}; if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } - if (quoteDetectedW(aTitle)) return tinyfd_saveFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - if (quoteDetectedW(aDefaultPathAndFile)) return tinyfd_saveFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_saveFileDialogW(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES"); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_saveFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL); - } + if (quoteDetectedW(aTitle)) return tinyfd_saveFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (quoteDetectedW(aDefaultPathAndFile)) return tinyfd_saveFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_saveFileDialogW(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES"); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_saveFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL); + } lHResult = CoInitializeEx(NULL, 0); @@ -1524,7 +1524,7 @@ wchar_t * tinyfd_saveFileDialogW( if (aNumOfFilterPatterns > 0) { - ldefExt = aFilterPatterns[0]; + ldefExt = aFilterPatterns[0]; if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) { @@ -1569,7 +1569,7 @@ wchar_t * tinyfd_saveFileDialogW( ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST ; ofn.nFileOffset = 0; ofn.nFileExtension = 0; - ofn.lpstrDefExt = ldefExt; + ofn.lpstrDefExt = ldefExt; ofn.lCustData = 0L; ofn.lpfnHook = NULL; ofn.lpTemplateName = NULL; @@ -1593,10 +1593,10 @@ wchar_t * tinyfd_saveFileDialogW( wchar_t * tinyfd_openFileDialogW( wchar_t const * aTitle, /* NULL or "" */ - wchar_t const * aDefaultPathAndFile, /* NULL or "" */ + wchar_t const * aDefaultPathAndFile, /* NULL or "" */ int aNumOfFilterPatterns, /* 0 */ - wchar_t const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - wchar_t const * aSingleFilterDescription, /* NULL or "image files" */ + wchar_t const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ + wchar_t const * aSingleFilterDescription, /* NULL or "image files" */ int aAllowMultipleSelects) /* 0 or 1 ; -1 to free allocated memory and return */ { size_t lLengths[MAX_MULTIPLE_FILES]; @@ -1606,42 +1606,42 @@ wchar_t * tinyfd_openFileDialogW( wchar_t * lPointers[MAX_MULTIPLE_FILES+1]; wchar_t * p; int i, j; - size_t lBuffLen; - DWORD lFullBuffLen; + size_t lBuffLen; + DWORD lFullBuffLen; HRESULT lHResult; OPENFILENAMEW ofn = { 0 }; - static wchar_t * lBuff = NULL; + static wchar_t * lBuff = NULL; - free(lBuff); - lBuff = NULL; - if (aAllowMultipleSelects < 0) return (wchar_t *)0; + free(lBuff); + lBuff = NULL; + if (aAllowMultipleSelects < 0) return (wchar_t *)0; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } - if (quoteDetectedW(aTitle)) return tinyfd_openFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); - if (quoteDetectedW(aDefaultPathAndFile)) return tinyfd_openFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); - if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_openFileDialogW(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_openFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects); - } + if (quoteDetectedW(aTitle)) return tinyfd_openFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (quoteDetectedW(aDefaultPathAndFile)) return tinyfd_openFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_openFileDialogW(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_openFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects); + } - if (aAllowMultipleSelects) - { - lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; - lBuff = (wchar_t*)(malloc(lFullBuffLen * sizeof(wchar_t))); - if (!lBuff) - { - lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; - lBuff = (wchar_t*)( malloc( lFullBuffLen * sizeof(wchar_t))); - } - } - else - { - lFullBuffLen = MAX_PATH_OR_CMD + 1; - lBuff = (wchar_t*)(malloc(lFullBuffLen * sizeof(wchar_t))); - } - if (!lBuff) return NULL; + if (aAllowMultipleSelects) + { + lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; + lBuff = (wchar_t*)(malloc(lFullBuffLen * sizeof(wchar_t))); + if (!lBuff) + { + lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; + lBuff = (wchar_t*)( malloc( lFullBuffLen * sizeof(wchar_t))); + } + } + else + { + lFullBuffLen = MAX_PATH_OR_CMD + 1; + lBuff = (wchar_t*)(malloc(lFullBuffLen * sizeof(wchar_t))); + } + if (!lBuff) return NULL; lHResult = CoInitializeEx(NULL, 0); @@ -1684,7 +1684,7 @@ wchar_t * tinyfd_openFileDialogW( ofn.nMaxCustFilter = 0; ofn.nFilterIndex = 1; ofn.lpstrFile = lBuff; - ofn.nMaxFile = lFullBuffLen; + ofn.nMaxFile = lFullBuffLen; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = MAX_PATH_OR_CMD / 2; ofn.lpstrInitialDir = wcslen(lDirname) ? lDirname : NULL; @@ -1704,56 +1704,56 @@ wchar_t * tinyfd_openFileDialogW( if (GetOpenFileNameW(&ofn) == 0) { - free(lBuff); - lBuff = NULL; + free(lBuff); + lBuff = NULL; } else { lBuffLen = wcslen(lBuff); lPointers[0] = lBuff + lBuffLen + 1; if (aAllowMultipleSelects && (lPointers[0][0] != L'\0')) - { + { i = 0; do { lLengths[i] = wcslen(lPointers[i]); lPointers[i + 1] = lPointers[i] + lLengths[i] + 1; i++; - } while (lPointers[i][0] != L'\0' && i < MAX_MULTIPLE_FILES ); - if (i > MAX_MULTIPLE_FILES) - { - free(lBuff); - lBuff = NULL; - } - else - { - i--; - p = lBuff + lFullBuffLen - 1; - *p = L'\0'; - for (j = i; j >= 0; j--) - { - p -= lLengths[j]; - memmove(p, lPointers[j], lLengths[j] * sizeof(wchar_t)); - p--; - *p = L'\\'; - p -= lBuffLen; - memmove(p, lBuff, lBuffLen*sizeof(wchar_t)); - p--; - *p = L'|'; - } - p++; - wcscpy(lBuff, p); - lBuffLen = wcslen(lBuff); - } - } - if (lBuff) lBuff = (wchar_t*)(realloc(lBuff, (lBuffLen + 1) * sizeof(wchar_t))); + } while (lPointers[i][0] != L'\0' && i < MAX_MULTIPLE_FILES ); + if (i > MAX_MULTIPLE_FILES) + { + free(lBuff); + lBuff = NULL; + } + else + { + i--; + p = lBuff + lFullBuffLen - 1; + *p = L'\0'; + for (j = i; j >= 0; j--) + { + p -= lLengths[j]; + memmove(p, lPointers[j], lLengths[j] * sizeof(wchar_t)); + p--; + *p = L'\\'; + p -= lBuffLen; + memmove(p, lBuff, lBuffLen*sizeof(wchar_t)); + p--; + *p = L'|'; + } + p++; + wcscpy(lBuff, p); + lBuffLen = wcslen(lBuff); + } + } + if (lBuff) lBuff = (wchar_t*)(realloc(lBuff, (lBuffLen + 1) * sizeof(wchar_t))); } if (lHResult == S_OK || lHResult == S_FALSE) { CoUninitialize(); } - return lBuff; + return lBuff; } @@ -1787,7 +1787,7 @@ wchar_t * tinyfd_selectFolderDialogW( wchar_t const * aDefaultPath) /* NULL or "" */ { static wchar_t lBuff[MAX_PATH_OR_CMD]; - wchar_t * lRetval; + wchar_t * lRetval; BROWSEINFOW bInfo; LPITEMIDLIST lpItem; @@ -1795,8 +1795,8 @@ wchar_t * tinyfd_selectFolderDialogW( if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } - if (quoteDetectedW(aTitle)) return tinyfd_selectFolderDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPath); - if (quoteDetectedW(aDefaultPath)) return tinyfd_selectFolderDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES"); + if (quoteDetectedW(aTitle)) return tinyfd_selectFolderDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPath); + if (quoteDetectedW(aDefaultPath)) return tinyfd_selectFolderDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES"); lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); @@ -1814,20 +1814,20 @@ wchar_t * tinyfd_selectFolderDialogW( lpItem = SHBrowseForFolderW(&bInfo); if (!lpItem) - { - lRetval = NULL; - } - else + { + lRetval = NULL; + } + else { SHGetPathFromIDListW(lpItem, lBuff); - lRetval = lBuff ; + lRetval = lBuff ; } if (lHResult == S_OK || lHResult == S_FALSE) { CoUninitialize(); } - return lRetval; + return lRetval; } @@ -1847,8 +1847,8 @@ wchar_t * tinyfd_colorChooserW( if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } - if (quoteDetectedW(aTitle)) return tinyfd_colorChooserW(L"INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB); - if (quoteDetectedW(aDefaultHexRGB)) return tinyfd_colorChooserW(aTitle, L"INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB); + if (quoteDetectedW(aTitle)) return tinyfd_colorChooserW(L"INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB); + if (quoteDetectedW(aDefaultHexRGB)) return tinyfd_colorChooserW(aTitle, L"INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB); lHResult = CoInitializeEx(NULL, 0); @@ -1897,319 +1897,319 @@ wchar_t * tinyfd_colorChooserW( static int messageBoxWinGui( - char const * aTitle, /* NULL or "" */ - char const * aMessage, /* NULL or "" may contain \n and \t */ - char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */ - char const * aIconType, /* "info" "warning" "error" "question" */ - int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may contain \n and \t */ + char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */ + char const * aIconType, /* "info" "warning" "error" "question" */ + int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { - int lIntRetVal; - wchar_t lTitle[128] = L""; - wchar_t * lMessage = NULL; - wchar_t lDialogType[16] = L""; - wchar_t lIconType[16] = L""; - wchar_t * lTmpWChar; + int lIntRetVal; + wchar_t lTitle[128] = L""; + wchar_t * lMessage = NULL; + wchar_t lDialogType[16] = L""; + wchar_t lIconType[16] = L""; + wchar_t * lTmpWChar; - if (aTitle) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); - else lTmpWChar = tinyfd_mbcsTo16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aMessage) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage); - else lTmpWChar = tinyfd_mbcsTo16(aMessage); - lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t)); - if (lMessage) wcscpy(lMessage, lTmpWChar); - } - if (aDialogType) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDialogType); - else lTmpWChar = tinyfd_mbcsTo16(aDialogType); - wcscpy(lDialogType, lTmpWChar); - } - if (aIconType) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType); - else lTmpWChar = tinyfd_mbcsTo16(aIconType); - wcscpy(lIconType, lTmpWChar); - } + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aMessage) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage); + else lTmpWChar = tinyfd_mbcsTo16(aMessage); + lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t)); + if (lMessage) wcscpy(lMessage, lTmpWChar); + } + if (aDialogType) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDialogType); + else lTmpWChar = tinyfd_mbcsTo16(aDialogType); + wcscpy(lDialogType, lTmpWChar); + } + if (aIconType) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType); + else lTmpWChar = tinyfd_mbcsTo16(aIconType); + wcscpy(lIconType, lTmpWChar); + } - lIntRetVal = tinyfd_messageBoxW(lTitle, lMessage, lDialogType, lIconType, aDefaultButton); + lIntRetVal = tinyfd_messageBoxW(lTitle, lMessage, lDialogType, lIconType, aDefaultButton); - free(lMessage); + free(lMessage); - return lIntRetVal; + return lIntRetVal; } static int notifyWinGui( - char const * aTitle, /* NULL or "" */ - char const * aMessage, /* NULL or "" may NOT contain \n nor \t */ - char const * aIconType) + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may NOT contain \n nor \t */ + char const * aIconType) { - wchar_t lTitle[128] = L""; - wchar_t * lMessage = NULL; - wchar_t lIconType[16] = L""; - wchar_t * lTmpWChar; + wchar_t lTitle[128] = L""; + wchar_t * lMessage = NULL; + wchar_t lIconType[16] = L""; + wchar_t * lTmpWChar; - if (aTitle) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); - else lTmpWChar = tinyfd_mbcsTo16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aMessage) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage); - else lTmpWChar = tinyfd_mbcsTo16(aMessage); - lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t)); + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aMessage) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage); + else lTmpWChar = tinyfd_mbcsTo16(aMessage); + lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t)); if (lMessage) wcscpy(lMessage, lTmpWChar); - } - if (aIconType) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType); - else lTmpWChar = tinyfd_mbcsTo16(aIconType); - wcscpy(lIconType, lTmpWChar); - } + } + if (aIconType) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType); + else lTmpWChar = tinyfd_mbcsTo16(aIconType); + wcscpy(lIconType, lTmpWChar); + } - tinyfd_notifyPopupW(lTitle, lMessage, lIconType); + tinyfd_notifyPopupW(lTitle, lMessage, lIconType); - free(lMessage); + free(lMessage); - return 1; + return 1; } static int inputBoxWinGui( - char * aoBuff, - char const * aTitle, /* NULL or "" */ - char const * aMessage, /* NULL or "" may NOT contain \n nor \t */ - char const * aDefaultInput) /* "" , if NULL it's a passwordBox */ + char * aoBuff, + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may NOT contain \n nor \t */ + char const * aDefaultInput) /* "" , if NULL it's a passwordBox */ { - wchar_t lTitle[128] = L""; - wchar_t * lMessage = NULL; - wchar_t lDefaultInput[MAX_PATH_OR_CMD] = L""; - wchar_t * lTmpWChar; - char * lTmpChar; + wchar_t lTitle[128] = L""; + wchar_t * lMessage = NULL; + wchar_t lDefaultInput[MAX_PATH_OR_CMD] = L""; + wchar_t * lTmpWChar; + char * lTmpChar; - if (aTitle) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); - else lTmpWChar = tinyfd_mbcsTo16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aMessage) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage); - else lTmpWChar = tinyfd_mbcsTo16(aMessage); - lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t)); + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aMessage) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage); + else lTmpWChar = tinyfd_mbcsTo16(aMessage); + lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t)); if (lMessage) wcscpy(lMessage, lTmpWChar); - } - if (aDefaultInput) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultInput); - else lTmpWChar = tinyfd_mbcsTo16(aDefaultInput); - wcscpy(lDefaultInput, lTmpWChar); + } + if (aDefaultInput) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultInput); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultInput); + wcscpy(lDefaultInput, lTmpWChar); lTmpWChar = tinyfd_inputBoxW(lTitle, lMessage, lDefaultInput); - } + } else lTmpWChar = tinyfd_inputBoxW(lTitle, lMessage, NULL); - free(lMessage); + free(lMessage); - if (!lTmpWChar) - { - aoBuff[0] = '\0'; - return 0; - } + if (!lTmpWChar) + { + aoBuff[0] = '\0'; + return 0; + } - if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); - else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); - strcpy(aoBuff, lTmpChar); + strcpy(aoBuff, lTmpChar); - return 1; + return 1; } static char * saveFileDialogWinGui( - char * aoBuff, - char const * aTitle, /* NULL or "" */ - char const * aDefaultPathAndFile, /* NULL or "" */ - int aNumOfFilterPatterns, /* 0 */ - char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - char const * aSingleFilterDescription) /* NULL or "image files" */ + char * aoBuff, + char const * aTitle, /* NULL or "" */ + char const * aDefaultPathAndFile, /* NULL or "" */ + int aNumOfFilterPatterns, /* 0 */ + char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ + char const * aSingleFilterDescription) /* NULL or "image files" */ { - wchar_t lTitle[128] = L""; - wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L""; - wchar_t lSingleFilterDescription[128] = L""; - wchar_t * * lFilterPatterns; - wchar_t * lTmpWChar; - char * lTmpChar; - int i; + wchar_t lTitle[128] = L""; + wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L""; + wchar_t lSingleFilterDescription[128] = L""; + wchar_t * * lFilterPatterns; + wchar_t * lTmpWChar; + char * lTmpChar; + int i; - lFilterPatterns = (wchar_t **)malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); - else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]); - lFilterPatterns[i] = (wchar_t *)malloc((wcslen(lTmpWChar) + 1) * sizeof(wchar_t *)); + lFilterPatterns = (wchar_t **)malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); + else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]); + lFilterPatterns[i] = (wchar_t *)malloc((wcslen(lTmpWChar) + 1) * sizeof(wchar_t *)); if (lFilterPatterns[i]) wcscpy(lFilterPatterns[i], lTmpWChar); - } + } - if (aTitle) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); - else lTmpWChar = tinyfd_mbcsTo16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aDefaultPathAndFile) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); - else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndFile); - wcscpy(lDefaultPathAndFile, lTmpWChar); - } - if (aSingleFilterDescription) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); - else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription); - wcscpy(lSingleFilterDescription, lTmpWChar); - } + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultPathAndFile) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndFile); + wcscpy(lDefaultPathAndFile, lTmpWChar); + } + if (aSingleFilterDescription) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); + else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription); + wcscpy(lSingleFilterDescription, lTmpWChar); + } - lTmpWChar = tinyfd_saveFileDialogW( - lTitle, - lDefaultPathAndFile, - aNumOfFilterPatterns, - (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/ - lSingleFilterDescription); + lTmpWChar = tinyfd_saveFileDialogW( + lTitle, + lDefaultPathAndFile, + aNumOfFilterPatterns, + (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/ + lSingleFilterDescription); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - free(lFilterPatterns[i]); - } - free(lFilterPatterns); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + free(lFilterPatterns[i]); + } + free(lFilterPatterns); - if (!lTmpWChar) - { - return NULL; - } + if (!lTmpWChar) + { + return NULL; + } - if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); - else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); - strcpy(aoBuff, lTmpChar); - if (tinyfd_winUtf8) (void)tinyfd_utf16to8(NULL); - else (void)tinyfd_utf16toMbcs(NULL); + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + strcpy(aoBuff, lTmpChar); + if (tinyfd_winUtf8) (void)tinyfd_utf16to8(NULL); + else (void)tinyfd_utf16toMbcs(NULL); - return aoBuff; + return aoBuff; } static char * openFileDialogWinGui( - char const * aTitle, /* NULL or "" */ - char const * aDefaultPathAndFile, /* NULL or "" */ - int aNumOfFilterPatterns, /* 0 */ - char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - char const * aSingleFilterDescription, /* NULL or "image files" */ - int aAllowMultipleSelects) /* 0 or 1 */ + char const * aTitle, /* NULL or "" */ + char const * aDefaultPathAndFile, /* NULL or "" */ + int aNumOfFilterPatterns, /* 0 */ + char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ + char const * aSingleFilterDescription, /* NULL or "image files" */ + int aAllowMultipleSelects) /* 0 or 1 */ { - wchar_t lTitle[128] = L""; - wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L""; - wchar_t lSingleFilterDescription[128] = L""; - wchar_t * * lFilterPatterns; - wchar_t * lTmpWChar; - char * lTmpChar; - int i; + wchar_t lTitle[128] = L""; + wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L""; + wchar_t lSingleFilterDescription[128] = L""; + wchar_t * * lFilterPatterns; + wchar_t * lTmpWChar; + char * lTmpChar; + int i; - lFilterPatterns = (wchar_t * *)malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); - else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]); - lFilterPatterns[i] = (wchar_t *)malloc((wcslen(lTmpWChar) + 1)*sizeof(wchar_t *)); + lFilterPatterns = (wchar_t * *)malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); + else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]); + lFilterPatterns[i] = (wchar_t *)malloc((wcslen(lTmpWChar) + 1)*sizeof(wchar_t *)); if (lFilterPatterns[i]) wcscpy(lFilterPatterns[i], lTmpWChar); - } + } - if (aTitle) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); - else lTmpWChar = tinyfd_mbcsTo16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aDefaultPathAndFile) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); - else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndFile); - wcscpy(lDefaultPathAndFile, lTmpWChar); - } - if (aSingleFilterDescription) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); - else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription); - wcscpy(lSingleFilterDescription, lTmpWChar); - } + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultPathAndFile) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndFile); + wcscpy(lDefaultPathAndFile, lTmpWChar); + } + if (aSingleFilterDescription) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); + else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription); + wcscpy(lSingleFilterDescription, lTmpWChar); + } - lTmpWChar = tinyfd_openFileDialogW( - lTitle, - lDefaultPathAndFile, - aNumOfFilterPatterns, - (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/ - lSingleFilterDescription, - aAllowMultipleSelects); + lTmpWChar = tinyfd_openFileDialogW( + lTitle, + lDefaultPathAndFile, + aNumOfFilterPatterns, + (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/ + lSingleFilterDescription, + aAllowMultipleSelects); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - free(lFilterPatterns[i]); - } - free(lFilterPatterns); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + free(lFilterPatterns[i]); + } + free(lFilterPatterns); - if (!lTmpWChar) return NULL; + if (!lTmpWChar) return NULL; - if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); - else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); - (void)tinyfd_openFileDialogW(NULL, NULL, 0, NULL, NULL, -1); + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + (void)tinyfd_openFileDialogW(NULL, NULL, 0, NULL, NULL, -1); - return lTmpChar; + return lTmpChar; } static char * selectFolderDialogWinGui( - char * aoBuff, - char const * aTitle, /* NULL or "" */ - char const * aDefaultPath) /* NULL or "" */ + char * aoBuff, + char const * aTitle, /* NULL or "" */ + char const * aDefaultPath) /* NULL or "" */ { - wchar_t lTitle[128] = L""; - wchar_t lDefaultPath[MAX_PATH_OR_CMD] = L""; - wchar_t * lTmpWChar; - char * lTmpChar; + wchar_t lTitle[128] = L""; + wchar_t lDefaultPath[MAX_PATH_OR_CMD] = L""; + wchar_t * lTmpWChar; + char * lTmpChar; - if (aTitle) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); - else lTmpWChar = tinyfd_mbcsTo16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aDefaultPath) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPath); - else lTmpWChar = tinyfd_mbcsTo16(aDefaultPath); - wcscpy(lDefaultPath, lTmpWChar); - } + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultPath) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPath); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultPath); + wcscpy(lDefaultPath, lTmpWChar); + } - lTmpWChar = tinyfd_selectFolderDialogW( - lTitle, - lDefaultPath); + lTmpWChar = tinyfd_selectFolderDialogW( + lTitle, + lDefaultPath); - if (!lTmpWChar) - { - return NULL; - } + if (!lTmpWChar) + { + return NULL; + } - if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); - else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); - strcpy(aoBuff, lTmpChar); + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + strcpy(aoBuff, lTmpChar); - return aoBuff; + return aoBuff; } @@ -2226,18 +2226,18 @@ static char * colorChooserWinGui( wchar_t * lTmpWChar; char * lTmpChar; - if (aTitle) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); - else lTmpWChar = tinyfd_mbcsTo16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aDefaultHexRGB) - { - if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultHexRGB); - else lTmpWChar = tinyfd_mbcsTo16(aDefaultHexRGB); - wcscpy(lDefaultHexRGB, lTmpWChar); - } + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultHexRGB) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultHexRGB); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultHexRGB); + wcscpy(lDefaultHexRGB, lTmpWChar); + } lTmpWChar = tinyfd_colorChooserW( lTitle, @@ -2250,9 +2250,9 @@ static char * colorChooserWinGui( return NULL; } - if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); - else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); - strcpy(lResultHexRGB, lTmpChar); + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + strcpy(lResultHexRGB, lTmpChar); return lResultHexRGB; } @@ -2264,8 +2264,8 @@ static int dialogPresent(void) char lBuff[MAX_PATH_OR_CMD] ; FILE * lIn ; char const * lString = "dialog.exe"; - if (!tinyfd_allowCursesDialogs) return 0; - if (lDialogPresent < 0) + if (!tinyfd_allowCursesDialogs) return 0; + if (lDialogPresent < 0) { if (!(lIn = _popen("where dialog.exe","r"))) { @@ -2288,7 +2288,7 @@ static int dialogPresent(void) lDialogPresent = 1 ; } } - return lDialogPresent; + return lDialogPresent; } @@ -2304,8 +2304,8 @@ static int messageBoxWinConsole( FILE * lIn; char lBuff[MAX_PATH_OR_CMD] = ""; - strcpy(lDialogString, "dialog "); - if (aTitle && strlen(aTitle)) + strcpy(lDialogString, "dialog "); + if (aTitle && strlen(aTitle)) { strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; @@ -2382,7 +2382,7 @@ static int messageBoxWinConsole( remove(lDialogFile); return 0 ; } - while (fgets(lBuff, sizeof(lBuff), lIn) != NULL) + while (fgets(lBuff, sizeof(lBuff), lIn) != NULL) {} fclose(lIn); remove(lDialogFile); @@ -2475,8 +2475,8 @@ static int inputBoxWinConsole( if (!(lIn = fopen(lDialogFile, "r"))) { remove(lDialogFile); - aoBuff[0] = '\0'; - return 0; + aoBuff[0] = '\0'; + return 0; } while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) {} @@ -2493,14 +2493,14 @@ static int inputBoxWinConsole( /* printf( "aoBuff: %s len: %lu \n" , aoBuff , strlen(aoBuff) ) ; */ lResult = strncmp( aoBuff , "1" , 1) ? 0 : 1 ; /* printf( "lResult: %d \n" , lResult ) ; */ - if ( ! lResult ) - { - aoBuff[0] = '\0'; - return 0 ; - } - /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */ - strcpy(aoBuff, aoBuff+3); - return 1; + if ( ! lResult ) + { + aoBuff[0] = '\0'; + return 0 ; + } + /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */ + strcpy(aoBuff, aoBuff+3); + return 1; } @@ -2576,7 +2576,7 @@ static char * openFileDialogWinConsole( char lDialogString[MAX_PATH_OR_CMD] ; FILE * lIn; - static char aoBuff[MAX_PATH_OR_CMD]; + static char aoBuff[MAX_PATH_OR_CMD]; strcpy( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) @@ -2688,133 +2688,133 @@ static char * selectFolderDialogWinConsole( static void writeUtf8( char const * aUtf8String ) { - unsigned long lNum; - void * lConsoleHandle; - wchar_t * lTmpWChar; + unsigned long lNum; + void * lConsoleHandle; + wchar_t * lTmpWChar; - lConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); - lTmpWChar = tinyfd_utf8to16(aUtf8String); - (void)WriteConsoleW(lConsoleHandle, lTmpWChar, (DWORD) wcslen(lTmpWChar), &lNum, NULL); + lConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); + lTmpWChar = tinyfd_utf8to16(aUtf8String); + (void)WriteConsoleW(lConsoleHandle, lTmpWChar, (DWORD) wcslen(lTmpWChar), &lNum, NULL); } int tinyfd_messageBox( - char const * aTitle, /* NULL or "" */ - char const * aMessage, /* NULL or "" may contain \n and \t */ - char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */ - char const * aIconType, /* "info" "warning" "error" "question" */ - int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may contain \n and \t */ + char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */ + char const * aIconType, /* "info" "warning" "error" "question" */ + int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { - char lChar; - UINT lOriginalCP = 0; - UINT lOriginalOutputCP = 0; + char lChar; + UINT lOriginalCP = 0; + UINT lOriginalOutputCP = 0; - if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton); - if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton); + if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton); + if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton); - if ((!tinyfd_forceConsole || !(GetConsoleWindow() || dialogPresent())) - && (!getenv("SSH_CLIENT") || getenvDISPLAY())) - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "windows"); return 1; } - return messageBoxWinGui(aTitle, aMessage, aDialogType, aIconType, aDefaultButton); - } - else if (dialogPresent()) - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return 0; } - return messageBoxWinConsole( - aTitle, aMessage, aDialogType, aIconType, aDefaultButton); - } - else - { - if (!tinyfd_winUtf8) - { - lOriginalCP = GetConsoleCP(); - lOriginalOutputCP = GetConsoleOutputCP(); - (void)SetConsoleCP(GetACP()); - (void)SetConsoleOutputCP(GetACP()); - } + if ((!tinyfd_forceConsole || !(GetConsoleWindow() || dialogPresent())) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "windows"); return 1; } + return messageBoxWinGui(aTitle, aMessage, aDialogType, aIconType, aDefaultButton); + } + else if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return 0; } + return messageBoxWinConsole( + aTitle, aMessage, aDialogType, aIconType, aDefaultButton); + } + else + { + if (!tinyfd_winUtf8) + { + lOriginalCP = GetConsoleCP(); + lOriginalOutputCP = GetConsoleOutputCP(); + (void)SetConsoleCP(GetACP()); + (void)SetConsoleOutputCP(GetACP()); + } - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return 0; } - if (!gWarningDisplayed && !tinyfd_forceConsole) - { - gWarningDisplayed = 1; - printf("\n\n%s\n", gTitle); - printf("%s\n\n", tinyfd_needs); - } + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return 0; } + if (!gWarningDisplayed && !tinyfd_forceConsole) + { + gWarningDisplayed = 1; + printf("\n\n%s\n", gTitle); + printf("%s\n\n", tinyfd_needs); + } - if (aTitle && strlen(aTitle)) - { - printf("\n"); - if (tinyfd_winUtf8) writeUtf8(aTitle); - else printf("%s", aTitle); - printf("\n\n"); - } - if (aDialogType && !strcmp("yesno", aDialogType)) - { - do - { - if (aMessage && strlen(aMessage)) - { - if (tinyfd_winUtf8) writeUtf8(aMessage); - else printf("%s", aMessage); - printf("\n"); - } - printf("y/n: "); - lChar = (char)tolower(_getch()); - printf("\n\n"); - } while (lChar != 'y' && lChar != 'n'); - if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } - return lChar == 'y' ? 1 : 0; - } - else if (aDialogType && !strcmp("okcancel", aDialogType)) - { - do - { - if (aMessage && strlen(aMessage)) - { - if (tinyfd_winUtf8) writeUtf8(aMessage); - else printf("%s", aMessage); - printf("\n"); - } - printf("[O]kay/[C]ancel: "); - lChar = (char)tolower(_getch()); - printf("\n\n"); - } while (lChar != 'o' && lChar != 'c'); - if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } - return lChar == 'o' ? 1 : 0; - } - else if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - do - { - if (aMessage && strlen(aMessage)) - { - if (tinyfd_winUtf8) writeUtf8(aMessage); - else printf("%s", aMessage); - printf("\n"); - } - printf("[Y]es/[N]o/[C]ancel: "); - lChar = (char)tolower(_getch()); - printf("\n\n"); - } while (lChar != 'y' && lChar != 'n' && lChar != 'c'); - if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } - return (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0; - } - else - { - if (aMessage && strlen(aMessage)) - { - if (tinyfd_winUtf8) writeUtf8(aMessage); - else printf("%s", aMessage); - printf("\n\n"); - } - printf("press enter to continue "); - lChar = (char)_getch(); - printf("\n\n"); - if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } - return 1; - } - } + if (aTitle && strlen(aTitle)) + { + printf("\n"); + if (tinyfd_winUtf8) writeUtf8(aTitle); + else printf("%s", aTitle); + printf("\n\n"); + } + if (aDialogType && !strcmp("yesno", aDialogType)) + { + do + { + if (aMessage && strlen(aMessage)) + { + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n"); + } + printf("y/n: "); + lChar = (char)tolower(_getch()); + printf("\n\n"); + } while (lChar != 'y' && lChar != 'n'); + if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } + return lChar == 'y' ? 1 : 0; + } + else if (aDialogType && !strcmp("okcancel", aDialogType)) + { + do + { + if (aMessage && strlen(aMessage)) + { + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n"); + } + printf("[O]kay/[C]ancel: "); + lChar = (char)tolower(_getch()); + printf("\n\n"); + } while (lChar != 'o' && lChar != 'c'); + if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } + return lChar == 'o' ? 1 : 0; + } + else if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + do + { + if (aMessage && strlen(aMessage)) + { + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n"); + } + printf("[Y]es/[N]o/[C]ancel: "); + lChar = (char)tolower(_getch()); + printf("\n\n"); + } while (lChar != 'y' && lChar != 'n' && lChar != 'c'); + if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } + return (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0; + } + else + { + if (aMessage && strlen(aMessage)) + { + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n\n"); + } + printf("press enter to continue "); + lChar = (char)_getch(); + printf("\n\n"); + if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } + return 1; + } + } } @@ -2824,19 +2824,19 @@ int tinyfd_notifyPopup( char const * aMessage , /* NULL or "" may contain \n \t */ char const * aIconType ) /* "info" "warning" "error" */ { - if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType); - if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType); + if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType); + if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType); if ( powershellPresent() && (!tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent())) - && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return 1;} return notifyWinGui(aTitle, aMessage, aIconType); } else - return tinyfd_messageBox(aTitle, aMessage, "ok" , aIconType, 0); + return tinyfd_messageBox(aTitle, aMessage, "ok" , aIconType, 0); } @@ -2846,25 +2846,25 @@ char * tinyfd_inputBox( char const * aMessage , /* NULL or "" (\n and \t have no effect) */ char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */ { - static char lBuff[MAX_PATH_OR_CMD] = ""; - char * lEOF; + static char lBuff[MAX_PATH_OR_CMD] = ""; + char * lEOF; - DWORD mode = 0; - HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + DWORD mode = 0; + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); - unsigned long lNum; - void * lConsoleHandle; - char * lTmpChar; - wchar_t lBuffW[1024]; + unsigned long lNum; + void * lConsoleHandle; + char * lTmpChar; + wchar_t lBuffW[1024]; - UINT lOriginalCP = 0; - UINT lOriginalOutputCP = 0; + UINT lOriginalCP = 0; + UINT lOriginalOutputCP = 0; - if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */ + if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */ - if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput); - if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput); - if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES"); + if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput); + if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput); + if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES"); mode = 0; hStdin = GetStdHandle(STD_INPUT_HANDLE); @@ -2872,20 +2872,20 @@ char * tinyfd_inputBox( if ((!tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent())) - && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} lBuff[0]='\0'; - if (inputBoxWinGui(lBuff, aTitle, aMessage, aDefaultInput)) return lBuff; - else return NULL; - } + if (inputBoxWinGui(lBuff, aTitle, aMessage, aDefaultInput)) return lBuff; + else return NULL; + } else if ( dialogPresent() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} lBuff[0]='\0'; - if (inputBoxWinConsole(lBuff, aTitle, aMessage, aDefaultInput) ) return lBuff; - else return NULL; - } + if (inputBoxWinConsole(lBuff, aTitle, aMessage, aDefaultInput) ) return lBuff; + else return NULL; + } else { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;} @@ -2897,85 +2897,85 @@ char * tinyfd_inputBox( printf("%s\n\n", tinyfd_needs); } - if (!tinyfd_winUtf8) - { - lOriginalCP = GetConsoleCP(); - lOriginalOutputCP = GetConsoleOutputCP(); - (void)SetConsoleCP(GetACP()); - (void)SetConsoleOutputCP(GetACP()); - } - - if (aTitle && strlen(aTitle)) + if (!tinyfd_winUtf8) { - printf("\n"); - if (tinyfd_winUtf8) writeUtf8(aTitle); - else printf("%s", aTitle); - printf("\n\n"); - } + lOriginalCP = GetConsoleCP(); + lOriginalOutputCP = GetConsoleOutputCP(); + (void)SetConsoleCP(GetACP()); + (void)SetConsoleOutputCP(GetACP()); + } + + if (aTitle && strlen(aTitle)) + { + printf("\n"); + if (tinyfd_winUtf8) writeUtf8(aTitle); + else printf("%s", aTitle); + printf("\n\n"); + } if ( aMessage && strlen(aMessage) ) { - if (tinyfd_winUtf8) writeUtf8(aMessage); - else printf("%s", aMessage); - printf("\n"); + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n"); } printf("(ctrl-Z + enter to cancel): "); if ( ! aDefaultInput ) { - (void) GetConsoleMode(hStdin, &mode); - (void) SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); + (void) GetConsoleMode(hStdin, &mode); + (void) SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); } - if (tinyfd_winUtf8) - { - lConsoleHandle = GetStdHandle(STD_INPUT_HANDLE); - (void) ReadConsoleW(lConsoleHandle, lBuffW, MAX_PATH_OR_CMD, &lNum, NULL); - if (!aDefaultInput) - { - (void)SetConsoleMode(hStdin, mode); - printf("\n"); - } - lBuffW[lNum] = '\0'; - if (lBuffW[wcslen(lBuffW) - 1] == '\n') lBuffW[wcslen(lBuffW) - 1] = '\0'; - if (lBuffW[wcslen(lBuffW) - 1] == '\r') lBuffW[wcslen(lBuffW) - 1] = '\0'; - lTmpChar = tinyfd_utf16to8(lBuffW); - if (lTmpChar) - { - strcpy(lBuff, lTmpChar); - return lBuff; - } - else - return NULL; - } - else - { - lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); - if (!aDefaultInput) - { - (void)SetConsoleMode(hStdin, mode); - printf("\n"); - } + if (tinyfd_winUtf8) + { + lConsoleHandle = GetStdHandle(STD_INPUT_HANDLE); + (void) ReadConsoleW(lConsoleHandle, lBuffW, MAX_PATH_OR_CMD, &lNum, NULL); + if (!aDefaultInput) + { + (void)SetConsoleMode(hStdin, mode); + printf("\n"); + } + lBuffW[lNum] = '\0'; + if (lBuffW[wcslen(lBuffW) - 1] == '\n') lBuffW[wcslen(lBuffW) - 1] = '\0'; + if (lBuffW[wcslen(lBuffW) - 1] == '\r') lBuffW[wcslen(lBuffW) - 1] = '\0'; + lTmpChar = tinyfd_utf16to8(lBuffW); + if (lTmpChar) + { + strcpy(lBuff, lTmpChar); + return lBuff; + } + else + return NULL; + } + else + { + lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); + if (!aDefaultInput) + { + (void)SetConsoleMode(hStdin, mode); + printf("\n"); + } - if (!tinyfd_winUtf8) - { - (void)SetConsoleCP(lOriginalCP); - (void)SetConsoleOutputCP(lOriginalOutputCP); - } + if (!tinyfd_winUtf8) + { + (void)SetConsoleCP(lOriginalCP); + (void)SetConsoleOutputCP(lOriginalOutputCP); + } - if (!lEOF) - { - return NULL; - } - printf("\n"); - if (strchr(lBuff, 27)) - { - return NULL; - } - if (lBuff[strlen(lBuff) - 1] == '\n') - { - lBuff[strlen(lBuff) - 1] = '\0'; - } - return lBuff; - } - } + if (!lEOF) + { + return NULL; + } + printf("\n"); + if (strchr(lBuff, 27)) + { + return NULL; + } + if (lBuff[strlen(lBuff) - 1] == '\n') + { + lBuff[strlen(lBuff) - 1] = '\0'; + } + return lBuff; + } + } } @@ -2989,45 +2989,45 @@ char * tinyfd_saveFileDialog( static char lBuff[MAX_PATH_OR_CMD] ; char lString[MAX_PATH_OR_CMD] ; char * p ; - char * lPointerInputBox; - int i; + char * lPointerInputBox; + int i; lBuff[0]='\0'; - if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES"); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL); - } + if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES"); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL); + } - if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) - && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} p = saveFileDialogWinGui(lBuff, - aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, (char const * const *)aFilterPatterns, aSingleFilterDescription); + aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, (char const * const *)aFilterPatterns, aSingleFilterDescription); } - else if (dialogPresent()) - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } - p = saveFileDialogWinConsole(lBuff, aTitle, aDefaultPathAndFile); - } - else - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } - strcpy(lBuff, "Save file in "); - strcat(lBuff, getCurDir()); + else if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } + p = saveFileDialogWinConsole(lBuff, aTitle, aDefaultPathAndFile); + } + else + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } + strcpy(lBuff, "Save file in "); + strcat(lBuff, getCurDir()); - lPointerInputBox = tinyfd_inputBox(NULL,NULL,NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ - if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ - p = tinyfd_inputBox(aTitle, lBuff, ""); - if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; - if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ - p = lBuff; - } + lPointerInputBox = tinyfd_inputBox(NULL,NULL,NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; + } if ( ! p || ! strlen( p ) ) { @@ -3050,50 +3050,50 @@ char * tinyfd_saveFileDialog( /* in case of multiple files, the separator is | */ char * tinyfd_openFileDialog( char const * aTitle , /* NULL or "" */ - char const * aDefaultPathAndFile, /* NULL or "" */ + char const * aDefaultPathAndFile, /* NULL or "" */ int aNumOfFilterPatterns , /* 0 */ - char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - char const * aSingleFilterDescription, /* NULL or "image files" */ + char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ + char const * aSingleFilterDescription, /* NULL or "image files" */ int aAllowMultipleSelects ) /* 0 or 1 */ { - char lString[MAX_PATH_OR_CMD]; - char lBuff[MAX_PATH_OR_CMD]; - char * p; - char * lPointerInputBox; - int i; + char lString[MAX_PATH_OR_CMD]; + char lBuff[MAX_PATH_OR_CMD]; + char * p; + char * lPointerInputBox; + int i; - if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); - if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); - if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects); - } + if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects); + } if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) - && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} p = openFileDialogWinGui( aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, - (char const * const *)aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + (char const * const *)aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + } + else if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } + p = openFileDialogWinConsole(aTitle, aDefaultPathAndFile); + } + else + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } + strcpy(lBuff, "Open file from "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; } - else if (dialogPresent()) - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } - p = openFileDialogWinConsole(aTitle, aDefaultPathAndFile); - } - else - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } - strcpy(lBuff, "Open file from "); - strcat(lBuff, getCurDir()); - lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ - if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ - p = tinyfd_inputBox(aTitle, lBuff, ""); - if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; - if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ - p = lBuff; - } if ( ! p || ! strlen( p ) ) { @@ -3116,38 +3116,38 @@ char * tinyfd_selectFolderDialog( char const * aTitle , /* NULL or "" */ char const * aDefaultPath ) /* NULL or "" */ { - static char lBuff[MAX_PATH_OR_CMD]; - char * p; - char * lPointerInputBox; - char lString[MAX_PATH_OR_CMD]; + static char lBuff[MAX_PATH_OR_CMD]; + char * p; + char * lPointerInputBox; + char lString[MAX_PATH_OR_CMD]; - if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath); - if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES"); + if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath); + if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES"); if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) - && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} p = selectFolderDialogWinGui(lBuff, aTitle, aDefaultPath); } - else - if (dialogPresent()) - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } - p = selectFolderDialogWinConsole(lBuff, aTitle, aDefaultPath); - } - else - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } - strcpy(lBuff, "Select folder from "); - strcat(lBuff, getCurDir()); - lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ - if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ - p = tinyfd_inputBox(aTitle, lBuff, ""); - if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; - if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ - p = lBuff; - } + else + if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } + p = selectFolderDialogWinConsole(lBuff, aTitle, aDefaultPath); + } + else + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } + strcpy(lBuff, "Select folder from "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; + } if ( ! p || ! strlen( p ) || ! dirExists( p ) ) { @@ -3167,22 +3167,22 @@ char * tinyfd_colorChooser( unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { - static char lDefaultHexRGB[16]; + static char lDefaultHexRGB[16]; int i; char * p ; - char * lPointerInputBox; - char lString[MAX_PATH_OR_CMD]; + char * lPointerInputBox; + char lString[MAX_PATH_OR_CMD]; - lDefaultHexRGB[0] = '\0'; + lDefaultHexRGB[0] = '\0'; - if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB); - if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB); + if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB); + if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB); if ( (!tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent()) ) - && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} - p = colorChooserWinGui(aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} + p = colorChooserWinGui(aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB); if (p) { strcpy(lDefaultHexRGB, p); @@ -3190,28 +3190,28 @@ char * tinyfd_colorChooser( } return NULL; } - else if (dialogPresent()) - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } - } - else - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } - } + else if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } + } + else + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } + } - if (aDefaultHexRGB) - { - strncpy(lDefaultHexRGB, aDefaultHexRGB,7); - lDefaultHexRGB[7]='\0'; - } - else - { - RGB2Hex(aDefaultRGB, lDefaultHexRGB); - } + if (aDefaultHexRGB) + { + strncpy(lDefaultHexRGB, aDefaultHexRGB,7); + lDefaultHexRGB[7]='\0'; + } + else + { + RGB2Hex(aDefaultRGB, lDefaultHexRGB); + } - lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ - if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ - p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB); if ( !p || (strlen(p) != 7) || (p[0] != '#') ) { @@ -3226,11 +3226,11 @@ char * tinyfd_colorChooser( } Hex2RGB(p,aoResultRGB); - strcpy(lDefaultHexRGB, p); + strcpy(lDefaultHexRGB, p); - if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ - return lDefaultHexRGB; + return lDefaultHexRGB; } @@ -3311,10 +3311,10 @@ static int detectPresence( char const * aExecutable ) static char * getVersion( char const * aExecutable ) /*version must be first numeral*/ { - static char lBuff[MAX_PATH_OR_CMD] ; - char lTestedString[MAX_PATH_OR_CMD] ; - FILE * lIn ; - char * lTmp ; + static char lBuff[MAX_PATH_OR_CMD] ; + char lTestedString[MAX_PATH_OR_CMD] ; + FILE * lIn ; + char * lTmp ; strcpy( lTestedString , aExecutable ) ; strcat( lTestedString , " --version" ) ; @@ -3323,27 +3323,27 @@ static char * getVersion( char const * aExecutable ) /*version must be first num lTmp = fgets( lBuff , sizeof( lBuff ) , lIn ) ; pclose( lIn ) ; - lTmp += strcspn(lTmp,"0123456789"); - /* printf("lTmp:%s\n", lTmp); */ - return lTmp ; + lTmp += strcspn(lTmp,"0123456789"); + /* printf("lTmp:%s\n", lTmp); */ + return lTmp ; } static int * getMajorMinorPatch( char const * aExecutable ) { - static int lArray[3] ; - char * lTmp ; + static int lArray[3] ; + char * lTmp ; - lTmp = (char *) getVersion(aExecutable); - lArray[0] = atoi( strtok(lTmp," ,.-") ) ; - /* printf("lArray0 %d\n", lArray[0]); */ - lArray[1] = atoi( strtok(0," ,.-") ) ; - /* printf("lArray1 %d\n", lArray[1]); */ - lArray[2] = atoi( strtok(0," ,.-") ) ; - /* printf("lArray2 %d\n", lArray[2]); */ + lTmp = (char *) getVersion(aExecutable); + lArray[0] = atoi( strtok(lTmp," ,.-") ) ; + /* printf("lArray0 %d\n", lArray[0]); */ + lArray[1] = atoi( strtok(0," ,.-") ) ; + /* printf("lArray1 %d\n", lArray[1]); */ + lArray[2] = atoi( strtok(0," ,.-") ) ; + /* printf("lArray2 %d\n", lArray[2]); */ - if ( !lArray[0] && !lArray[1] && !lArray[2] ) return NULL; - return lArray ; + if ( !lArray[0] && !lArray[1] && !lArray[2] ) return NULL; + return lArray ; } @@ -3369,37 +3369,37 @@ static int tryCommand( char const * aCommand ) static int isTerminalRunning(void) { - static int lIsTerminalRunning = -1 ; - if ( lIsTerminalRunning < 0 ) - { - lIsTerminalRunning = isatty(1); - if (tinyfd_verbose) printf("isTerminalRunning %d\n", lIsTerminalRunning ); - } - return lIsTerminalRunning; + static int lIsTerminalRunning = -1 ; + if ( lIsTerminalRunning < 0 ) + { + lIsTerminalRunning = isatty(1); + if (tinyfd_verbose) printf("isTerminalRunning %d\n", lIsTerminalRunning ); + } + return lIsTerminalRunning; } static char * dialogNameOnly(void) { - static char lDialogName[128] = "*" ; - if ( lDialogName[0] == '*' ) - { - if (!tinyfd_allowCursesDialogs) - { - strcpy(lDialogName , "" ); - } - else if ( tfd_isDarwin() && * strcpy(lDialogName , "/opt/local/bin/dialog" ) - && detectPresence( lDialogName ) ) - {} - else if ( * strcpy(lDialogName , "dialog" ) - && detectPresence( lDialogName ) ) - {} - else - { - strcpy(lDialogName , "" ); - } - } - return lDialogName ; + static char lDialogName[128] = "*" ; + if ( lDialogName[0] == '*' ) + { + if (!tinyfd_allowCursesDialogs) + { + strcpy(lDialogName , "" ); + } + else if ( tfd_isDarwin() && * strcpy(lDialogName , "/opt/local/bin/dialog" ) + && detectPresence( lDialogName ) ) + {} + else if ( * strcpy(lDialogName , "dialog" ) + && detectPresence( lDialogName ) ) + {} + else + { + strcpy(lDialogName , "" ); + } + } + return lDialogName ; } @@ -3443,7 +3443,7 @@ int isDialogVersionBetter09b(void) static int whiptailPresentOnly(void) { static int lWhiptailPresent = -1 ; - if (!tinyfd_allowCursesDialogs) return 0; + if (!tinyfd_allowCursesDialogs) return 0; if ( lWhiptailPresent < 0 ) { lWhiptailPresent = detectPresence( "whiptail" ) ; @@ -3464,19 +3464,19 @@ static char * terminalName(void) { strcpy(lShellName , "bash -c " ) ; /*good for basic input*/ } - else if ( strlen(dialogNameOnly()) || whiptailPresentOnly() ) - { - strcpy(lShellName , "sh -c " ) ; /*good enough for dialog & whiptail*/ - } - else - { - strcpy(lTerminalName , "" ) ; - return NULL ; - } + else if ( strlen(dialogNameOnly()) || whiptailPresentOnly() ) + { + strcpy(lShellName , "sh -c " ) ; /*good enough for dialog & whiptail*/ + } + else + { + strcpy(lTerminalName , "" ) ; + return NULL ; + } if ( tfd_isDarwin() ) { - if ( * strcpy(lTerminalName , "/opt/X11/bin/xterm" ) + if ( * strcpy(lTerminalName , "/opt/X11/bin/xterm" ) && detectPresence( lTerminalName ) ) { strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ; @@ -3553,9 +3553,9 @@ static char * terminalName(void) strcat(lTerminalName , " -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( * strcpy(lTerminalName,"gnome-terminal") + else if ( * strcpy(lTerminalName,"gnome-terminal") && detectPresence(lTerminalName) && (lArray = getMajorMinorPatch(lTerminalName)) - && ((lArray[0]<3) || (lArray[0]==3 && lArray[1]<=6)) ) + && ((lArray[0]<3) || (lArray[0]==3 && lArray[1]<=6)) ) { strcat(lTerminalName , " --disable-factory -x " ) ; strcat(lTerminalName , lShellName ) ; @@ -3613,8 +3613,8 @@ static int whiptailPresent(void) static int graphicMode(void) { return !( tinyfd_forceConsole && (isTerminalRunning() || terminalName()) ) - && ( getenvDISPLAY() - || (tfd_isDarwin() && (!getenv("SSH_TTY") || getenvDISPLAY() ) ) ) ; + && ( getenvDISPLAY() + || (tfd_isDarwin() && (!getenv("SSH_TTY") || getenvDISPLAY() ) ) ) ; } @@ -3834,12 +3834,12 @@ int tfd_shellementaryPresent(void) int tfd_xpropPresent(void) { - static int lXpropPresent = -1 ; - if ( lXpropPresent < 0 ) - { - lXpropPresent = detectPresence("xprop") ; - } - return lXpropPresent && graphicMode( ) ; + static int lXpropPresent = -1 ; + if ( lXpropPresent < 0 ) + { + lXpropPresent = detectPresence("xprop") ; + } + return lXpropPresent && graphicMode( ) ; } @@ -3870,7 +3870,7 @@ int tfd_zenity3Present(void) static int lZenity3Present = -1 ; char lBuff[MAX_PATH_OR_CMD] ; FILE * lIn ; - int lIntTmp ; + int lIntTmp ; if ( lZenity3Present < 0 ) { @@ -3883,16 +3883,16 @@ int tfd_zenity3Present(void) if ( atoi(lBuff) >= 3 ) { lZenity3Present = 3 ; - lIntTmp = atoi(strtok(lBuff,".")+2 ) ; - if ( lIntTmp >= 18 ) - { - lZenity3Present = 5 ; - } - else if ( lIntTmp >= 10 ) - { - lZenity3Present = 4 ; - } - } + lIntTmp = atoi(strtok(lBuff,".")+2 ) ; + if ( lIntTmp >= 18 ) + { + lZenity3Present = 5 ; + } + else if ( lIntTmp >= 10 ) + { + lZenity3Present = 4 ; + } + } else if ( ( atoi(lBuff) == 2 ) && ( atoi(strtok(lBuff,".")+2 ) >= 32 ) ) { lZenity3Present = 2 ; @@ -3908,54 +3908,54 @@ int tfd_zenity3Present(void) int tfd_kdialogPresent(void) { - static int lKdialogPresent = -1 ; - char lBuff[MAX_PATH_OR_CMD] ; - FILE * lIn ; - char * lDesktop; + static int lKdialogPresent = -1 ; + char lBuff[MAX_PATH_OR_CMD] ; + FILE * lIn ; + char * lDesktop; - if ( lKdialogPresent < 0 ) - { - if ( tfd_zenityPresent() ) - { - lDesktop = getenv("XDG_SESSION_DESKTOP"); - if ( !lDesktop || ( strcmp(lDesktop, "KDE") && strcmp(lDesktop, "lxqt") ) ) - { - lKdialogPresent = 0 ; - return lKdialogPresent ; - } - } + if ( lKdialogPresent < 0 ) + { + if ( tfd_zenityPresent() ) + { + lDesktop = getenv("XDG_SESSION_DESKTOP"); + if ( !lDesktop || ( strcmp(lDesktop, "KDE") && strcmp(lDesktop, "lxqt") ) ) + { + lKdialogPresent = 0 ; + return lKdialogPresent ; + } + } - lKdialogPresent = detectPresence("kdialog") ; - if ( lKdialogPresent && !getenv("SSH_TTY") ) - { - lIn = popen( "kdialog --attach 2>&1" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - { - if ( ! strstr( "Unknown" , lBuff ) ) - { - lKdialogPresent = 2 ; - if (tinyfd_verbose) printf("kdialog-attach %d\n", lKdialogPresent); - } - } - pclose( lIn ) ; + lKdialogPresent = detectPresence("kdialog") ; + if ( lKdialogPresent && !getenv("SSH_TTY") ) + { + lIn = popen( "kdialog --attach 2>&1" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( ! strstr( "Unknown" , lBuff ) ) + { + lKdialogPresent = 2 ; + if (tinyfd_verbose) printf("kdialog-attach %d\n", lKdialogPresent); + } + } + pclose( lIn ) ; - if (lKdialogPresent == 2) - { - lKdialogPresent = 1 ; - lIn = popen( "kdialog --passivepopup 2>&1" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - { - if ( ! strstr( "Unknown" , lBuff ) ) - { - lKdialogPresent = 2 ; - if (tinyfd_verbose) printf("kdialog-popup %d\n", lKdialogPresent); - } - } - pclose( lIn ) ; - } - } - } - return graphicMode() ? lKdialogPresent : 0 ; + if (lKdialogPresent == 2) + { + lKdialogPresent = 1 ; + lIn = popen( "kdialog --passivepopup 2>&1" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( ! strstr( "Unknown" , lBuff ) ) + { + lKdialogPresent = 2 ; + if (tinyfd_verbose) printf("kdialog-popup %d\n", lKdialogPresent); + } + } + pclose( lIn ) ; + } + } + } + return graphicMode() ? lKdialogPresent : 0 ; } @@ -4011,36 +4011,36 @@ static int python3Present(void) if (tinyfd_verbose) printf("lPython3Present %d\n", lPython3Present) ; if (tinyfd_verbose) printf("gPython3Name %s\n", gPython3Name) ; } - return lPython3Present ; + return lPython3Present ; } static int python2Present(void) { - static int lPython2Present = -1 ; - int i; + static int lPython2Present = -1 ; + int i; - if ( lPython2Present < 0 ) - { - lPython2Present = 0 ; - strcpy(gPython2Name , "python2" ) ; - if ( detectPresence(gPython2Name) ) lPython2Present = 1; - else - { - for ( i = 9 ; i >= 0 ; i -- ) - { - sprintf( gPython2Name , "python2.%d" , i ) ; - if ( detectPresence(gPython2Name) ) - { - lPython2Present = 1; - break; - } - } - } - if (tinyfd_verbose) printf("lPython2Present %d\n", lPython2Present) ; - if (tinyfd_verbose) printf("gPython2Name %s\n", gPython2Name) ; - } - return lPython2Present ; + if ( lPython2Present < 0 ) + { + lPython2Present = 0 ; + strcpy(gPython2Name , "python2" ) ; + if ( detectPresence(gPython2Name) ) lPython2Present = 1; + else + { + for ( i = 9 ; i >= 0 ; i -- ) + { + sprintf( gPython2Name , "python2.%d" , i ) ; + if ( detectPresence(gPython2Name) ) + { + lPython2Present = 1; + break; + } + } + } + if (tinyfd_verbose) printf("lPython2Present %d\n", lPython2Present) ; + if (tinyfd_verbose) printf("gPython2Name %s\n", gPython2Name) ; + } + return lPython2Present ; } @@ -4061,28 +4061,28 @@ static int tkinter3Present(void) } if (tinyfd_verbose) printf("lTkinter3Present %d\n", lTkinter3Present) ; } - return lTkinter3Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); + return lTkinter3Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); } static int tkinter2Present(void) { - static int lTkinter2Present = -1 ; - char lPythonCommand[256]; - char lPythonParams[128] = - "-S -c \"try:\n\timport Tkinter;\nexcept:\n\tprint 0;\""; + static int lTkinter2Present = -1 ; + char lPythonCommand[256]; + char lPythonParams[128] = + "-S -c \"try:\n\timport Tkinter;\nexcept:\n\tprint 0;\""; - if ( lTkinter2Present < 0 ) - { - lTkinter2Present = 0 ; - if ( python2Present() ) - { - sprintf( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; - lTkinter2Present = tryCommand(lPythonCommand) ; - } - if (tinyfd_verbose) printf("lTkinter2Present %d\n", lTkinter2Present) ; - } - return lTkinter2Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); + if ( lTkinter2Present < 0 ) + { + lTkinter2Present = 0 ; + if ( python2Present() ) + { + sprintf( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; + lTkinter2Present = tryCommand(lPythonCommand) ; + } + if (tinyfd_verbose) printf("lTkinter2Present %d\n", lTkinter2Present) ; + } + return lTkinter2Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); } @@ -4126,7 +4126,7 @@ static void sigHandler(int signum) { pclose( lIn ) ; } - if (tinyfd_verbose) printf("tinyfiledialogs caught signal %d\n", signum); + if (tinyfd_verbose) printf("tinyfiledialogs caught signal %d\n", signum); } void tinyfd_beep(void) @@ -4209,8 +4209,8 @@ int tinyfd_messageBox( lBuff[0]='\0'; - if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton); - if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton); + if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton); + if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton); lTitleLen = aTitle ? strlen(aTitle) : 0 ; lMessageLen = aMessage ? strlen(aMessage) : 0 ; @@ -4305,7 +4305,7 @@ int tinyfd_messageBox( if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} strcpy( lDialogString , "kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -4373,7 +4373,7 @@ int tinyfd_messageBox( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} strcpy( lDialogString , "szAnswer=$(zenity" ) ; - if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -4392,7 +4392,7 @@ int tinyfd_messageBox( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;} strcpy( lDialogString , "szAnswer=$(qarma" ) ; - if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -4525,100 +4525,100 @@ int tinyfd_messageBox( } else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter3Present() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return 1;} + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return 1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import messagebox;root=tkinter.Tk();root.withdraw();"); + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import messagebox;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString ,"res=messagebox." ) ; - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat( lDialogString , "askokcancel(" ) ; - if ( aDefaultButton ) - { - strcat( lDialogString , "default=messagebox.OK," ) ; - } - else - { - strcat( lDialogString , "default=messagebox.CANCEL," ) ; - } - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat( lDialogString , "askyesno(" ) ; - if ( aDefaultButton ) - { - strcat( lDialogString , "default=messagebox.YES," ) ; - } - else - { - strcat( lDialogString , "default=messagebox.NO," ) ; - } - } - else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "askyesnocancel(" ) ; - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , "default=messagebox.YES," ); break; - case 2: strcat( lDialogString , "default=messagebox.NO," ); break; - case 0: strcat( lDialogString , "default=messagebox.CANCEL," ); break; - } - } - else - { - strcat( lDialogString , "showinfo(" ) ; - } + strcat( lDialogString ,"res=messagebox." ) ; + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat( lDialogString , "askokcancel(" ) ; + if ( aDefaultButton ) + { + strcat( lDialogString , "default=messagebox.OK," ) ; + } + else + { + strcat( lDialogString , "default=messagebox.CANCEL," ) ; + } + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat( lDialogString , "askyesno(" ) ; + if ( aDefaultButton ) + { + strcat( lDialogString , "default=messagebox.YES," ) ; + } + else + { + strcat( lDialogString , "default=messagebox.NO," ) ; + } + } + else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "askyesnocancel(" ) ; + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , "default=messagebox.YES," ); break; + case 2: strcat( lDialogString , "default=messagebox.NO," ); break; + case 0: strcat( lDialogString , "default=messagebox.CANCEL," ); break; + } + } + else + { + strcat( lDialogString , "showinfo(" ) ; + } - strcat( lDialogString , "icon='" ) ; - if ( aIconType && (! strcmp( "question" , aIconType ) - || ! strcmp( "error" , aIconType ) - || ! strcmp( "warning" , aIconType ) ) ) - { - strcat( lDialogString , aIconType ) ; - } - else - { - strcat( lDialogString , "info" ) ; - } + strcat( lDialogString , "icon='" ) ; + if ( aIconType && (! strcmp( "question" , aIconType ) + || ! strcmp( "error" , aIconType ) + || ! strcmp( "warning" , aIconType ) ) ) + { + strcat( lDialogString , aIconType ) ; + } + else + { + strcat( lDialogString , "info" ) ; + } - strcat(lDialogString, "',") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, "message='") ; - lpDialogString = lDialogString + strlen(lDialogString); - tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; - strcat(lDialogString, "'") ; - } + strcat(lDialogString, "',") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, "message='") ; + lpDialogString = lDialogString + strlen(lDialogString); + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + strcat(lDialogString, "'") ; + } - if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat(lDialogString, ");\n\ + if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat(lDialogString, ");\n\ if res is None :\n\tprint(0)\n\ elif res is False :\n\tprint(2)\n\ else :\n\tprint (1)\n\"" ) ; - } - else - { - strcat(lDialogString, ");\n\ + } + else + { + strcat(lDialogString, ");\n\ if res is False :\n\tprint(0)\n\ else :\n\tprint(1)\n\"" ) ; - } - } - else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter2Present() ) + } + } + else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter2Present() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return 1;} - strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; - strcat( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } @@ -5022,15 +5022,15 @@ tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;} - strcpy( lDialogString , "perl -e \"use Net::DBus;\ + strcpy( lDialogString , "perl -e \"use Net::DBus;\ my \\$sessionBus = Net::DBus->session;\ my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\ my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\ 'org.freedesktop.Notifications');"); - sprintf( lDialogString + strlen(lDialogString), + sprintf( lDialogString + strlen(lDialogString), "my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ", - aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ; + aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ; } else if ( !isTerminalRunning() && notifysendPresent() && !strcmp("ok" , aDialogType) ) { @@ -5189,8 +5189,8 @@ int tinyfd_notifyPopup( size_t lTitleLen ; size_t lMessageLen ; - if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType); - if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType); + if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType); + if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType); if ( getenv("SSH_TTY") ) { @@ -5287,13 +5287,13 @@ int tinyfd_notifyPopup( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;} - strcpy( lDialogString , "perl -e \"use Net::DBus;\ + strcpy( lDialogString , "perl -e \"use Net::DBus;\ my \\$sessionBus = Net::DBus->session;\ my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\ my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\ 'org.freedesktop.Notifications');"); - sprintf( lDialogString + strlen(lDialogString) , + sprintf( lDialogString + strlen(lDialogString) , "my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ", aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ; } @@ -5387,13 +5387,13 @@ char * tinyfd_inputBox( size_t lTitleLen ; size_t lMessageLen ; - if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */ + if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */ lBuff[0]='\0'; - if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput); - if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput); - if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES"); + if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput); + if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput); + if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES"); lTitleLen = aTitle ? strlen(aTitle) : 0 ; lMessageLen = aMessage ? strlen(aMessage) : 0 ; @@ -5441,7 +5441,7 @@ char * tinyfd_inputBox( if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} strcpy( lDialogString , "szAnswer=$(kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -5481,7 +5481,7 @@ char * tinyfd_inputBox( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} strcpy( lDialogString , "szAnswer=$(zenity" ) ; - if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -5500,7 +5500,7 @@ char * tinyfd_inputBox( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "szAnswer=$(qarma" ) ; - if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -5590,55 +5590,55 @@ char * tinyfd_inputBox( strcat(lDialogString, "\"" ) ; strcat( lDialogString , ");echo $?$szAnswer"); } - else if ( !gdialogPresent() && !xdialogPresent() && tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter; from tkinter import simpledialog;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString ,"res=simpledialog.askstring(" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aMessage && strlen(aMessage) ) - { + else if ( !gdialogPresent() && !xdialogPresent() && tkinter3Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter; from tkinter import simpledialog;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString ,"res=simpledialog.askstring(" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aMessage && strlen(aMessage) ) + { - strcat(lDialogString, "prompt='") ; - lpDialogString = lDialogString + strlen(lDialogString); - tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; - strcat(lDialogString, "',") ; - } - if ( aDefaultInput ) - { - if ( strlen(aDefaultInput) ) - { - strcat(lDialogString, "initialvalue='") ; - strcat(lDialogString, aDefaultInput) ; - strcat(lDialogString, "',") ; - } - } - else - { - strcat(lDialogString, "show='*'") ; - } - strcat(lDialogString, ");\nif res is None :\n\tprint(0)"); - strcat(lDialogString, "\nelse :\n\tprint('1'+res)\n\"" ) ; - } - else if ( !gdialogPresent() && !xdialogPresent() && tkinter2Present( ) ) + strcat(lDialogString, "prompt='") ; + lpDialogString = lDialogString + strlen(lDialogString); + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + strcat(lDialogString, "',") ; + } + if ( aDefaultInput ) + { + if ( strlen(aDefaultInput) ) + { + strcat(lDialogString, "initialvalue='") ; + strcat(lDialogString, aDefaultInput) ; + strcat(lDialogString, "',") ; + } + } + else + { + strcat(lDialogString, "show='*'") ; + } + strcat(lDialogString, ");\nif res is None :\n\tprint(0)"); + strcat(lDialogString, "\nelse :\n\tprint('1'+res)\n\"" ) ; + } + else if ( !gdialogPresent() && !xdialogPresent() && tkinter2Present( ) ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} - strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; - strcat( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } - strcat( lDialogString , - " -S -c \"import Tkinter,tkSimpleDialog;root=Tkinter.Tk();root.withdraw();"); + strcat( lDialogString , + " -S -c \"import Tkinter,tkSimpleDialog;root=Tkinter.Tk();root.withdraw();"); if ( tfd_isDarwin( ) ) { @@ -5794,8 +5794,8 @@ frontmost of process \\\"Python\\\" to true' ''');"); strcat( lDialogString , "'" ) ; if ( !gWarningDisplayed && !tinyfd_forceConsole) { - gWarningDisplayed = 1 ; - tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); + gWarningDisplayed = 1 ; + tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); } if ( aTitle && strlen(aTitle) && !tinyfd_forceConsole) { @@ -5820,11 +5820,11 @@ frontmost of process \\\"Python\\\" to true' ''');"); strcat( lDialogString , "cat -v /tmp/tinyfd.txt"); } else if ( !gWarningDisplayed && ! isTerminalRunning( ) && ! terminalName() ) { - gWarningDisplayed = 1 ; - tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"no_solution");return (char *)0;} - free(lDialogString); - return NULL; + gWarningDisplayed = 1 ; + tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"no_solution");return (char *)0;} + free(lDialogString); + return NULL; } else { @@ -5966,17 +5966,17 @@ char * tinyfd_saveFileDialog( int lWasGraphicDialog = 0 ; int lWasXterm = 0 ; char * p ; - char * lPointerInputBox ; + char * lPointerInputBox ; FILE * lIn ; lBuff[0]='\0'; - if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES"); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL); - } + if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES"); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL); + } if ( osascriptPresent( ) ) { @@ -6014,7 +6014,7 @@ char * tinyfd_saveFileDialog( if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} strcpy( lDialogString , "kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -6038,11 +6038,11 @@ char * tinyfd_saveFileDialog( if ( aNumOfFilterPatterns > 0 ) { strcat(lDialogString , " \"" ) ; - strcat( lDialogString , aFilterPatterns[0] ) ; + strcat( lDialogString , aFilterPatterns[0] ) ; for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat( lDialogString , " " ) ; - strcat( lDialogString , aFilterPatterns[i] ) ; + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; } if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) { @@ -6064,7 +6064,7 @@ char * tinyfd_saveFileDialog( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} strcpy( lDialogString , "zenity" ) ; - if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -6083,7 +6083,7 @@ char * tinyfd_saveFileDialog( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "qarma" ) ; - if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -6110,8 +6110,8 @@ char * tinyfd_saveFileDialog( } for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat( lDialogString , " " ) ; - strcat( lDialogString , aFilterPatterns[i] ) ; + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; } strcat( lDialogString , "' --file-filter='All files | *'" ) ; } @@ -6151,63 +6151,63 @@ char * tinyfd_saveFileDialog( if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null "); } else if ( !xdialogPresent() && tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString , "res=filedialog.asksaveasfilename("); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "initialdir='") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "'," ) ; - } - getLastName( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "initialfile='") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "'," ) ; - } - } - if ( ( aNumOfFilterPatterns > 1 ) - || ( (aNumOfFilterPatterns == 1) /* test because poor osx behaviour */ - && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) ) - { - strcat(lDialogString , "filetypes=(" ) ; - strcat( lDialogString , "('" ) ; - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcat( lDialogString , aSingleFilterDescription ) ; - } - strcat( lDialogString , "',(" ) ; - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , "'" ) ; - strcat( lDialogString , aFilterPatterns[i] ) ; - strcat( lDialogString , "'," ) ; - } - strcat( lDialogString , "))," ) ; - strcat( lDialogString , "('All files','*'))" ) ; - } - strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ; - } - else if ( !xdialogPresent() && tkinter2Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString , "res=filedialog.asksaveasfilename("); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "initialdir='") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "'," ) ; + } + getLastName( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "initialfile='") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "'," ) ; + } + } + if ( ( aNumOfFilterPatterns > 1 ) + || ( (aNumOfFilterPatterns == 1) /* test because poor osx behaviour */ + && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) ) + { + strcat(lDialogString , "filetypes=(" ) ; + strcat( lDialogString , "('" ) ; + if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + { + strcat( lDialogString , aSingleFilterDescription ) ; + } + strcat( lDialogString , "',(" ) ; + for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , "'" ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; + strcat( lDialogString , "'," ) ; + } + strcat( lDialogString , "))," ) ; + strcat( lDialogString , "('All files','*'))" ) ; + } + strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ; + } + else if ( !xdialogPresent() && tkinter2Present( ) ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} - strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; - strcat( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && tfd_isDarwin( )) + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( )) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } @@ -6265,8 +6265,8 @@ char * tinyfd_saveFileDialog( strcat( lDialogString , "))," ) ; strcat( lDialogString , "('All files','*'))" ) ; } - strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint res \n\"" ) ; - } + strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint res \n\"" ) ; + } else if ( xdialogPresent() || dialogName() ) { if ( xdialogPresent( ) ) @@ -6345,16 +6345,16 @@ char * tinyfd_saveFileDialog( else { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - strcpy(lBuff, "Save file in "); - strcat(lBuff, getCurDir()); - lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ - if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ - p = tinyfd_inputBox(aTitle, lBuff, ""); - if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; - if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ - p = lBuff; + strcpy(lBuff, "Save file in "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; - getPathWithoutFinalSlash( lString , p ) ; + getPathWithoutFinalSlash( lString , p ) ; if ( strlen( lString ) && ! dirExists( lString ) ) { return NULL ; @@ -6400,10 +6400,10 @@ char * tinyfd_saveFileDialog( /* in case of multiple files, the separator is | */ char * tinyfd_openFileDialog( - char const * aTitle , /* NULL or "" */ - char const * aDefaultPathAndFile , /* NULL or "" */ + char const * aTitle , /* NULL or "" */ + char const * aDefaultPathAndFile , /* NULL or "" */ int aNumOfFilterPatterns , /* 0 */ - char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ + char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ char const * aSingleFilterDescription , /* NULL or "image files" */ int aAllowMultipleSelects ) /* 0 or 1 */ { @@ -6419,39 +6419,39 @@ char * tinyfd_openFileDialog( size_t lFullBuffLen ; static char * lBuff = NULL; - if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); - if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); - if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects); - } + if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects); + } - free(lBuff); - if (aTitle&&!strcmp(aTitle,"tinyfd_query")) - { - lBuff = NULL; - } - else - { - if (aAllowMultipleSelects) - { - lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; - lBuff = (char *)(malloc(lFullBuffLen * sizeof(char))); - if (!lBuff) - { - lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; - lBuff = (char *)( malloc( lFullBuffLen * sizeof(char))); - } - } - else - { - lFullBuffLen = MAX_PATH_OR_CMD + 1; - lBuff = (char *)(malloc(lFullBuffLen * sizeof(char))); - } - if (!lBuff) return NULL; - lBuff[0]='\0'; - } + free(lBuff); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")) + { + lBuff = NULL; + } + else + { + if (aAllowMultipleSelects) + { + lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; + lBuff = (char *)(malloc(lFullBuffLen * sizeof(char))); + if (!lBuff) + { + lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; + lBuff = (char *)( malloc( lFullBuffLen * sizeof(char))); + } + } + else + { + lFullBuffLen = MAX_PATH_OR_CMD + 1; + lBuff = (char *)(malloc(lFullBuffLen * sizeof(char))); + } + if (!lBuff) return NULL; + lBuff[0]='\0'; + } if ( osascriptPresent( ) ) { @@ -6523,7 +6523,7 @@ char * tinyfd_openFileDialog( lWasKdialog = 1 ; strcpy( lDialogString , "kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -6547,11 +6547,11 @@ char * tinyfd_openFileDialog( if ( aNumOfFilterPatterns > 0 ) { strcat(lDialogString , " \"" ) ; - strcat( lDialogString , aFilterPatterns[0] ) ; + strcat( lDialogString , aFilterPatterns[0] ) ; for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat( lDialogString , " " ) ; - strcat( lDialogString , aFilterPatterns[i] ) ; + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; } if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) { @@ -6577,7 +6577,7 @@ char * tinyfd_openFileDialog( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} strcpy( lDialogString , "zenity" ) ; - if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -6596,7 +6596,7 @@ char * tinyfd_openFileDialog( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "qarma" ) ; - if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -6628,8 +6628,8 @@ char * tinyfd_openFileDialog( } for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat( lDialogString , " " ) ; - strcat( lDialogString , aFilterPatterns[i] ) ; + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; } strcat( lDialogString , "' --file-filter='All files | *'" ) ; } @@ -6673,70 +6673,70 @@ char * tinyfd_openFileDialog( if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null "); } else if ( tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString , "lFiles=filedialog.askopenfilename("); - if ( aAllowMultipleSelects ) - { - strcat( lDialogString , "multiple=1," ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "initialdir='") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "'," ) ; - } - getLastName( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "initialfile='") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "'," ) ; - } - } - if ( ( aNumOfFilterPatterns > 1 ) - || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/ - && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) ) - { - strcat(lDialogString , "filetypes=(" ) ; - strcat( lDialogString , "('" ) ; - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcat( lDialogString , aSingleFilterDescription ) ; - } - strcat( lDialogString , "',(" ) ; - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , "'" ) ; - strcat( lDialogString , aFilterPatterns[i] ) ; - strcat( lDialogString , "'," ) ; - } - strcat( lDialogString , "))," ) ; - strcat( lDialogString , "('All files','*'))" ) ; - } - strcat( lDialogString , ");\ + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString , "lFiles=filedialog.askopenfilename("); + if ( aAllowMultipleSelects ) + { + strcat( lDialogString , "multiple=1," ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "initialdir='") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "'," ) ; + } + getLastName( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "initialfile='") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "'," ) ; + } + } + if ( ( aNumOfFilterPatterns > 1 ) + || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/ + && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) ) + { + strcat(lDialogString , "filetypes=(" ) ; + strcat( lDialogString , "('" ) ; + if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + { + strcat( lDialogString , aSingleFilterDescription ) ; + } + strcat( lDialogString , "',(" ) ; + for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , "'" ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; + strcat( lDialogString , "'," ) ; + } + strcat( lDialogString , "))," ) ; + strcat( lDialogString , "('All files','*'))" ) ; + } + strcat( lDialogString , ");\ \nif not isinstance(lFiles, tuple):\n\tprint(lFiles)\nelse:\ \n\tlFilesString=''\n\tfor lFile in lFiles:\n\t\tlFilesString+=str(lFile)+'|'\ \n\tprint(lFilesString[:-1])\n\"" ) ; - } - else if ( tkinter2Present( ) ) + } + else if ( tkinter2Present( ) ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} - strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; - strcat( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } @@ -6880,31 +6880,31 @@ frontmost of process \\\"Python\\\" to true' ''');"); else { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - strcpy(lBuff, "Open file from "); - strcat(lBuff, getCurDir()); - lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ - if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ - p = tinyfd_inputBox(aTitle, lBuff, ""); - if ( p ) strcpy(lBuff, p); else lBuff[0] = '\0'; - if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ + strcpy(lBuff, "Open file from "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if ( p ) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ if ( ! fileExists(lBuff) ) { - free(lBuff); - lBuff = NULL; + free(lBuff); + lBuff = NULL; } - else - { - lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); - } - return lBuff ; + else + { + lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); + } + return lBuff ; } if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; if ( ! ( lIn = popen( lDialogString , "r" ) ) ) { - free(lBuff); - lBuff = NULL; - return NULL ; + free(lBuff); + lBuff = NULL; + return NULL ; } lBuff[0]='\0'; p = lBuff; @@ -6927,30 +6927,30 @@ frontmost of process \\\"Python\\\" to true' ''');"); /* printf( "lBuff2: %s\n" , lBuff ) ; */ if ( ! strlen( lBuff ) ) { - free(lBuff); - lBuff = NULL; - return NULL; + free(lBuff); + lBuff = NULL; + return NULL; } if ( aAllowMultipleSelects && strchr(lBuff, '|') ) { - if( ! ensureFilesExist( lBuff , lBuff ) ) - { - free(lBuff); - lBuff = NULL; - return NULL; - } + if( ! ensureFilesExist( lBuff , lBuff ) ) + { + free(lBuff); + lBuff = NULL; + return NULL; + } } else if ( !fileExists(lBuff) ) { - free(lBuff); - lBuff = NULL; - return NULL; - } + free(lBuff); + lBuff = NULL; + return NULL; + } - lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); + lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); /*printf( "lBuff3: %s\n" , lBuff ) ; */ - return lBuff ; + return lBuff ; } @@ -6962,13 +6962,13 @@ char * tinyfd_selectFolderDialog( char lDialogString[MAX_PATH_OR_CMD] ; FILE * lIn ; char * p ; - char * lPointerInputBox ; + char * lPointerInputBox ; int lWasGraphicDialog = 0 ; int lWasXterm = 0 ; lBuff[0]='\0'; - if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath); - if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES"); + if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath); + if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES"); if ( osascriptPresent( )) { @@ -6997,7 +6997,7 @@ char * tinyfd_selectFolderDialog( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} strcpy( lDialogString , "kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -7031,7 +7031,7 @@ char * tinyfd_selectFolderDialog( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} strcpy( lDialogString , "zenity" ) ; - if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -7050,7 +7050,7 @@ char * tinyfd_selectFolderDialog( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "qarma" ) ; - if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -7088,30 +7088,30 @@ char * tinyfd_selectFolderDialog( if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null "); } else if ( !xdialogPresent() && tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString , "res=filedialog.askdirectory("); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aDefaultPath && strlen(aDefaultPath) ) - { - strcat(lDialogString, "initialdir='") ; - strcat(lDialogString, aDefaultPath ) ; - strcat(lDialogString , "'" ) ; - } - strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ; - } - else if ( !xdialogPresent() && tkinter2Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString , "res=filedialog.askdirectory("); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aDefaultPath && strlen(aDefaultPath) ) + { + strcat(lDialogString, "initialdir='") ; + strcat(lDialogString, aDefaultPath ) ; + strcat(lDialogString , "'" ) ; + } + strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ; + } + else if ( !xdialogPresent() && tkinter2Present( ) ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} - strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; strcat( lDialogString , gPython2Name ) ; if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { @@ -7217,14 +7217,14 @@ frontmost of process \\\"Python\\\" to true' ''');"); else { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - strcpy(lBuff, "Select folder from "); - strcat(lBuff, getCurDir()); - lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ - if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ - p = tinyfd_inputBox(aTitle, lBuff, ""); - if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; - if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ - p = lBuff; + strcpy(lBuff, "Select folder from "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; if ( !p || ! strlen( p ) || ! dirExists( p ) ) { @@ -7263,17 +7263,17 @@ char * tinyfd_colorChooser( unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3] ) /* { 0 , 0 , 0 } */ { - static char lDefaultHexRGB[16]; - char lBuff[128] ; + static char lDefaultHexRGB[16]; + char lBuff[128] ; char lTmp[128] ; #if !((defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)) - char * lTmp2 ; + char * lTmp2 ; #endif char lDialogString[MAX_PATH_OR_CMD] ; unsigned char lDefaultRGB[3]; char * p; - char * lPointerInputBox; + char * lPointerInputBox; FILE * lIn ; int i ; int lWasZenity3 = 0 ; @@ -7281,21 +7281,21 @@ char * tinyfd_colorChooser( int lWasXdialog = 0 ; lBuff[0]='\0'; - if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB); - if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB); + if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB); + if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB); - if (aDefaultHexRGB) - { - Hex2RGB(aDefaultHexRGB, lDefaultRGB); + if (aDefaultHexRGB) + { + Hex2RGB(aDefaultHexRGB, lDefaultRGB); strcpy(lDefaultHexRGB, aDefaultHexRGB); - } - else - { - lDefaultRGB[0] = aDefaultRGB[0]; - lDefaultRGB[1] = aDefaultRGB[1]; - lDefaultRGB[2] = aDefaultRGB[2]; + } + else + { + lDefaultRGB[0] = aDefaultRGB[0]; + lDefaultRGB[1] = aDefaultRGB[1]; + lDefaultRGB[2] = aDefaultRGB[2]; RGB2Hex(aDefaultRGB, lDefaultHexRGB); - } + } if ( osascriptPresent( ) ) { @@ -7340,7 +7340,7 @@ to set mycolor to choose color default color {"); { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} strcpy( lDialogString , "kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -7360,7 +7360,7 @@ to set mycolor to choose color default color {"); { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char *)1;} strcpy( lDialogString , "zenity" ); - if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -7379,7 +7379,7 @@ to set mycolor to choose color default color {"); { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "qarma" ) ; - if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -7417,38 +7417,38 @@ to set mycolor to choose color default color {"); } strcat(lDialogString, "\" 0 60 ") ; #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__) - sprintf(lTmp,"%hhu %hhu %hhu",lDefaultRGB[0],lDefaultRGB[1],lDefaultRGB[2]); + sprintf(lTmp,"%hhu %hhu %hhu",lDefaultRGB[0],lDefaultRGB[1],lDefaultRGB[2]); #else sprintf(lTmp,"%hu %hu %hu",lDefaultRGB[0],lDefaultRGB[1],lDefaultRGB[2]); #endif strcat(lDialogString, lTmp) ; strcat(lDialogString, " 2>&1"); } - else if ( tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import colorchooser;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString , "res=colorchooser.askcolor(color='" ) ; - strcat(lDialogString, lDefaultHexRGB ) ; - strcat(lDialogString, "'") ; + else if ( tkinter3Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import colorchooser;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString , "res=colorchooser.askcolor(color='" ) ; + strcat(lDialogString, lDefaultHexRGB ) ; + strcat(lDialogString, "'") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, ",title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "'") ; - } - strcat( lDialogString , ");\ + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, ",title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "'") ; + } + strcat( lDialogString , ");\ \nif res[1] is not None:\n\tprint(res[1])\"" ) ; - } - else if ( tkinter2Present( ) ) + } + else if ( tkinter2Present( ) ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} - strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; - strcat( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } @@ -7480,9 +7480,9 @@ frontmost of process \\\"Python\\\" to true' ''');"); else { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ - if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ - p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB); if ( !p || (strlen(p) != 7) || (p[0] != '#') ) { @@ -7495,10 +7495,10 @@ frontmost of process \\\"Python\\\" to true' ''');"); return NULL ; } } - Hex2RGB(p,aoResultRGB); - strcpy(lDefaultHexRGB, p); - if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ - return lDefaultHexRGB; + Hex2RGB(p,aoResultRGB); + strcpy(lDefaultHexRGB, p); + if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ + return lDefaultHexRGB; } if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; @@ -7575,8 +7575,8 @@ frontmost of process \\\"Python\\\" to true' ''');"); /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); */ /* printf( "lBuff: %s\n" , lBuff ) ; */ - strcpy(lDefaultHexRGB,lBuff); - return lDefaultHexRGB ; + strcpy(lDefaultHexRGB,lBuff); + return lDefaultHexRGB ; } #endif /* _WIN32 */ diff --git a/src/tinyfiledialogs.h b/src/tinyfiledialogs.h index 96dd77b..ec27ac4 100644 --- a/src/tinyfiledialogs.h +++ b/src/tinyfiledialogs.h @@ -51,7 +51,7 @@ misrepresented as being the original software. #ifdef __cplusplus /* if tinydialogs.c is compiled as C++ code rather than C code, you may need to comment this out - and the corresponding closing bracket near the end of this file. */ + and the corresponding closing bracket near the end of this file. */ extern "C" { #endif @@ -82,7 +82,7 @@ int tinyfd_getGlobalInt(char const * aIntVariableName); /* returns -1 on error * int tinyfd_setGlobalInt(char const * aIntVariableName, int aValue); /* returns -1 on error */ /* aCharVariableName: "tinyfd_version" "tinyfd_needs" "tinyfd_response" aIntVariableName : "tinyfd_verbose" "tinyfd_silent" "tinyfd_allowCursesDialogs" - "tinyfd_forceConsole" "tinyfd_assumeGraphicDisplay" "tinyfd_winUtf8" + "tinyfd_forceConsole" "tinyfd_assumeGraphicDisplay" "tinyfd_winUtf8" **************/ @@ -121,58 +121,58 @@ for console mode: void tinyfd_beep(void); int tinyfd_notifyPopup( - char const * aTitle, /* NULL or "" */ - char const * aMessage, /* NULL or "" may contain \n \t */ - char const * aIconType); /* "info" "warning" "error" */ - /* return has only meaning for tinyfd_query */ + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may contain \n \t */ + char const * aIconType); /* "info" "warning" "error" */ + /* return has only meaning for tinyfd_query */ int tinyfd_messageBox( - char const * aTitle , /* NULL or "" */ - char const * aMessage , /* NULL or "" may contain \n \t */ - char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ - char const * aIconType , /* "info" "warning" "error" "question" */ - int aDefaultButton ) ; - /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" may contain \n \t */ + char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ + char const * aIconType , /* "info" "warning" "error" "question" */ + int aDefaultButton ) ; + /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ char * tinyfd_inputBox( - char const * aTitle , /* NULL or "" */ - char const * aMessage , /* NULL or "" (\n and \t have no effect) */ - char const * aDefaultInput ) ; /* NULL passwordBox, "" inputbox */ - /* returns NULL on cancel */ + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" (\n and \t have no effect) */ + char const * aDefaultInput ) ; /* NULL passwordBox, "" inputbox */ + /* returns NULL on cancel */ char * tinyfd_saveFileDialog( - char const * aTitle , /* NULL or "" */ - char const * aDefaultPathAndFile , /* NULL or "" */ - int aNumOfFilterPatterns , /* 0 (1 in the following example) */ - char const * const * aFilterPatterns , /* NULL or char const * lFilterPatterns[1]={"*.txt"} */ - char const * aSingleFilterDescription ) ; /* NULL or "text files" */ - /* returns NULL on cancel */ + char const * aTitle , /* NULL or "" */ + char const * aDefaultPathAndFile , /* NULL or "" */ + int aNumOfFilterPatterns , /* 0 (1 in the following example) */ + char const * const * aFilterPatterns , /* NULL or char const * lFilterPatterns[1]={"*.txt"} */ + char const * aSingleFilterDescription ) ; /* NULL or "text files" */ + /* returns NULL on cancel */ char * tinyfd_openFileDialog( - char const * aTitle, /* NULL or "" */ - char const * aDefaultPathAndFile, /* NULL or "" */ - int aNumOfFilterPatterns , /* 0 (2 in the following example) */ - char const * const * aFilterPatterns, /* NULL or char const * lFilterPatterns[2]={"*.png","*.jpg"}; */ - char const * aSingleFilterDescription, /* NULL or "image files" */ - int aAllowMultipleSelects ) ; /* 0 or 1 */ - /* in case of multiple files, the separator is | */ - /* returns NULL on cancel */ + char const * aTitle, /* NULL or "" */ + char const * aDefaultPathAndFile, /* NULL or "" */ + int aNumOfFilterPatterns , /* 0 (2 in the following example) */ + char const * const * aFilterPatterns, /* NULL or char const * lFilterPatterns[2]={"*.png","*.jpg"}; */ + char const * aSingleFilterDescription, /* NULL or "image files" */ + int aAllowMultipleSelects ) ; /* 0 or 1 */ + /* in case of multiple files, the separator is | */ + /* returns NULL on cancel */ char * tinyfd_selectFolderDialog( - char const * aTitle, /* NULL or "" */ - char const * aDefaultPath); /* NULL or "" */ - /* returns NULL on cancel */ + char const * aTitle, /* NULL or "" */ + char const * aDefaultPath); /* NULL or "" */ + /* returns NULL on cancel */ char * tinyfd_colorChooser( - char const * aTitle, /* NULL or "" */ - char const * aDefaultHexRGB, /* NULL or "#FF0000" */ - unsigned char const aDefaultRGB[3] , /* unsigned char lDefaultRGB[3] = { 0 , 128 , 255 }; */ - unsigned char aoResultRGB[3] ) ; /* unsigned char lResultRGB[3]; */ - /* returns the hexcolor as a string "#FF0000" */ - /* aoResultRGB also contains the result */ - /* aDefaultRGB is used only if aDefaultHexRGB is NULL */ - /* aDefaultRGB and aoResultRGB can be the same array */ - /* returns NULL on cancel */ + char const * aTitle, /* NULL or "" */ + char const * aDefaultHexRGB, /* NULL or "#FF0000" */ + unsigned char const aDefaultRGB[3] , /* unsigned char lDefaultRGB[3] = { 0 , 128 , 255 }; */ + unsigned char aoResultRGB[3] ) ; /* unsigned char lResultRGB[3]; */ + /* returns the hexcolor as a string "#FF0000" */ + /* aoResultRGB also contains the result */ + /* aDefaultRGB is used only if aDefaultHexRGB is NULL */ + /* aDefaultRGB and aoResultRGB can be the same array */ + /* returns NULL on cancel */ /************ WINDOWS ONLY SECTION ************************/ @@ -180,62 +180,62 @@ char * tinyfd_colorChooser( /* windows only - utf-16 version */ int tinyfd_notifyPopupW( - wchar_t const * aTitle, /* NULL or L"" */ - wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ - wchar_t const * aIconType); /* L"info" L"warning" L"error" */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ + wchar_t const * aIconType); /* L"info" L"warning" L"error" */ /* windows only - utf-16 version */ int tinyfd_messageBoxW( - wchar_t const * aTitle, /* NULL or L"" */ - wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ - wchar_t const * aDialogType, /* L"ok" L"okcancel" L"yesno" */ - wchar_t const * aIconType, /* L"info" L"warning" L"error" L"question" */ - int aDefaultButton ); /* 0 for cancel/no , 1 for ok/yes */ - /* returns 0 for cancel/no , 1 for ok/yes */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ + wchar_t const * aDialogType, /* L"ok" L"okcancel" L"yesno" */ + wchar_t const * aIconType, /* L"info" L"warning" L"error" L"question" */ + int aDefaultButton ); /* 0 for cancel/no , 1 for ok/yes */ + /* returns 0 for cancel/no , 1 for ok/yes */ /* windows only - utf-16 version */ wchar_t * tinyfd_inputBoxW( - wchar_t const * aTitle, /* NULL or L"" */ - wchar_t const * aMessage, /* NULL or L"" (\n nor \t not respected) */ - wchar_t const * aDefaultInput); /* NULL passwordBox, L"" inputbox */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aMessage, /* NULL or L"" (\n nor \t not respected) */ + wchar_t const * aDefaultInput); /* NULL passwordBox, L"" inputbox */ /* windows only - utf-16 version */ wchar_t * tinyfd_saveFileDialogW( - wchar_t const * aTitle, /* NULL or L"" */ - wchar_t const * aDefaultPathAndFile, /* NULL or L"" */ - int aNumOfFilterPatterns, /* 0 (1 in the following example) */ - wchar_t const * const * aFilterPatterns, /* NULL or wchar_t const * lFilterPatterns[1]={L"*.txt"} */ - wchar_t const * aSingleFilterDescription); /* NULL or L"text files" */ - /* returns NULL on cancel */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aDefaultPathAndFile, /* NULL or L"" */ + int aNumOfFilterPatterns, /* 0 (1 in the following example) */ + wchar_t const * const * aFilterPatterns, /* NULL or wchar_t const * lFilterPatterns[1]={L"*.txt"} */ + wchar_t const * aSingleFilterDescription); /* NULL or L"text files" */ + /* returns NULL on cancel */ /* windows only - utf-16 version */ wchar_t * tinyfd_openFileDialogW( - wchar_t const * aTitle, /* NULL or L"" */ - wchar_t const * aDefaultPathAndFile, /* NULL or L"" */ - int aNumOfFilterPatterns , /* 0 (2 in the following example) */ - wchar_t const * const * aFilterPatterns, /* NULL or wchar_t const * lFilterPatterns[2]={L"*.png","*.jpg"} */ - wchar_t const * aSingleFilterDescription, /* NULL or L"image files" */ - int aAllowMultipleSelects ) ; /* 0 or 1 */ - /* in case of multiple files, the separator is | */ - /* returns NULL on cancel */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aDefaultPathAndFile, /* NULL or L"" */ + int aNumOfFilterPatterns , /* 0 (2 in the following example) */ + wchar_t const * const * aFilterPatterns, /* NULL or wchar_t const * lFilterPatterns[2]={L"*.png","*.jpg"} */ + wchar_t const * aSingleFilterDescription, /* NULL or L"image files" */ + int aAllowMultipleSelects ) ; /* 0 or 1 */ + /* in case of multiple files, the separator is | */ + /* returns NULL on cancel */ /* windows only - utf-16 version */ wchar_t * tinyfd_selectFolderDialogW( - wchar_t const * aTitle, /* NULL or L"" */ - wchar_t const * aDefaultPath); /* NULL or L"" */ - /* returns NULL on cancel */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aDefaultPath); /* NULL or L"" */ + /* returns NULL on cancel */ /* windows only - utf-16 version */ wchar_t * tinyfd_colorChooserW( - wchar_t const * aTitle, /* NULL or L"" */ - wchar_t const * aDefaultHexRGB, /* NULL or L"#FF0000" */ - unsigned char const aDefaultRGB[3], /* unsigned char lDefaultRGB[3] = { 0 , 128 , 255 }; */ - unsigned char aoResultRGB[3]); /* unsigned char lResultRGB[3]; */ - /* returns the hexcolor as a string L"#FF0000" */ - /* aoResultRGB also contains the result */ - /* aDefaultRGB is used only if aDefaultHexRGB is NULL */ - /* aDefaultRGB and aoResultRGB can be the same array */ - /* returns NULL on cancel */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aDefaultHexRGB, /* NULL or L"#FF0000" */ + unsigned char const aDefaultRGB[3], /* unsigned char lDefaultRGB[3] = { 0 , 128 , 255 }; */ + unsigned char aoResultRGB[3]); /* unsigned char lResultRGB[3]; */ + /* returns the hexcolor as a string L"#FF0000" */ + /* aoResultRGB also contains the result */ + /* aDefaultRGB is used only if aDefaultHexRGB is NULL */ + /* aDefaultRGB and aoResultRGB can be the same array */ + /* returns NULL on cancel */ #endif /*_WIN32 */ diff --git a/src/util.h b/src/util.h index e74003f..2e7d4d9 100644 --- a/src/util.h +++ b/src/util.h @@ -21,44 +21,44 @@ class Util { public: - static int divideRoundUp(const int num, const int denom) - { - return (num+denom-1)/denom; - } + static int divideRoundUp(const int num, const int denom) + { + return (num+denom-1)/denom; + } - static int divideRound(const int dividend, const int divisor) - { - return (dividend+divisor/2)/divisor; - } + static int divideRound(const int dividend, const int divisor) + { + return (dividend+divisor/2)/divisor; + } - template static T mod(T x, const T m) - { - x %= m; - if (x < 0) - { - x += m; - } - return x; - } + template static T mod(T x, const T m) + { + x %= m; + if (x < 0) + { + x += m; + } + return x; + } - template static void constrain(const T& min, T& x, const T& lim) - { - if (x < min) - { - x = min; - } - else if (lim <= x) - { - x = lim-1; - } - } + template static void constrain(const T& min, T& x, const T& lim) + { + if (x < min) + { + x = min; + } + else if (lim <= x) + { + x = lim-1; + } + } - // 0x0 <= nib <= 0xF - // '0' <= ret <= 'F' - static char hexDigit(unsigned char nib) - { - return nib < 10 ? '0'+nib : 'A'+nib-10; - } + // 0x0 <= nib <= 0xF + // '0' <= ret <= 'F' + static char hexDigit(unsigned char nib) + { + return nib < 10 ? '0'+nib : 'A'+nib-10; + } }; #endif diff --git a/src/video.cpp b/src/video.cpp index 74161f9..a0fced4 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -28,12 +28,12 @@ static const int FLASH_HALF_PERIOD = E2Const::AVG_CPU_HZ/4; // 2 Hz period = 4 H Video::Video(VideoMode& mode, AddressBus& addressBus, PictureGenerator& picgen, TextCharacters& textRows): - mode(mode), addressBus(addressBus), picgen(picgen), textRows(textRows) + mode(mode), addressBus(addressBus), picgen(picgen), textRows(textRows) { - VideoAddressing::buildLUT(TEXT_BASE_1,TEXT_LEN,lutTEXT[0]); - VideoAddressing::buildLUT(TEXT_BASE_2,TEXT_LEN,lutTEXT[1]); - VideoAddressing::buildLUT(HRES_BASE_1,HRES_LEN,lutHRES[0]); - VideoAddressing::buildLUT(HRES_BASE_2,HRES_LEN,lutHRES[1]); + VideoAddressing::buildLUT(TEXT_BASE_1,TEXT_LEN,lutTEXT[0]); + VideoAddressing::buildLUT(TEXT_BASE_2,TEXT_LEN,lutTEXT[1]); + VideoAddressing::buildLUT(HRES_BASE_1,HRES_LEN,lutHRES[0]); + VideoAddressing::buildLUT(HRES_BASE_2,HRES_LEN,lutHRES[1]); } Video::~Video() @@ -42,84 +42,84 @@ Video::~Video() void Video::powerOn() { - this->t = 0; - this->flash = false; - this->cflash = 0; + this->t = 0; + this->flash = false; + this->cflash = 0; } void Video::tick() { - const unsigned char data = getDataByte(); - const unsigned char rowToPlot = getRowToPlot(data); + const unsigned char data = getDataByte(); + const unsigned char rowToPlot = getRowToPlot(data); - this->picgen.tick(this->t,rowToPlot); + this->picgen.tick(this->t,rowToPlot); - updateFlash(); + updateFlash(); - ++this->t; + ++this->t; - if (this->t >= E2Const::BYTES_PER_FIELD) - { - this->t = 0; - } + if (this->t >= E2Const::BYTES_PER_FIELD) + { + this->t = 0; + } } void Video::updateFlash() { - ++this->cflash; - if (this->cflash >= FLASH_HALF_PERIOD) - { - this->flash = !this->flash; - this->cflash = 0; - } + ++this->cflash; + if (this->cflash >= FLASH_HALF_PERIOD) + { + this->flash = !this->flash; + this->cflash = 0; + } } unsigned char Video::getDataByte() { - // TODO should fix the mixed-mode scanning during VBL (see U.A.][, p. 5-13) + // TODO should fix the mixed-mode scanning during VBL (see U.A.][, p. 5-13) - std::vector& lut = - (this->mode.isDisplayingText(this->t) || !this->mode.isHiRes()) - ? - this->lutTEXT[this->mode.getPage()] - : - this->lutHRES[this->mode.getPage()]; + std::vector& lut = + (this->mode.isDisplayingText(this->t) || !this->mode.isHiRes()) + ? + this->lutTEXT[this->mode.getPage()] + : + this->lutHRES[this->mode.getPage()]; - return this->addressBus.read(lut[this->t]); + return this->addressBus.read(lut[this->t]); } unsigned char Video::getRowToPlot(unsigned char d) { - if (this->mode.isDisplayingText(this->t)) - { - const bool inverse = inverseChar(d); - const int y = this->t / E2Const::BYTES_PER_ROW; - d = this->textRows.get(((d & 0x3F) << 3) | (y & 0x07)); - if (inverse) - { - d = ~d & 0x7F; - } - } - return d; + if (this->mode.isDisplayingText(this->t)) + { + const bool inverse = inverseChar(d); + const int y = this->t / E2Const::BYTES_PER_ROW; + d = this->textRows.get(((d & 0x3F) << 3) | (y & 0x07)); + if (inverse) + { + d = ~d & 0x7F; + } + } + return d; } bool Video::inverseChar(const unsigned char d) { - bool inverse; + bool inverse; - const int cs = (d >> 6) & 3; - if (cs == 0) - { - inverse = true; - } - else if (cs == 1) - { - inverse = this->flash; - } - else - { - inverse = false; - } + const int cs = (d >> 6) & 3; + if (cs == 0) + { + inverse = true; + } + else if (cs == 1) + { + inverse = this->flash; + } + else + { + inverse = false; + } - return inverse; + return inverse; } diff --git a/src/video.h b/src/video.h index 18c9483..17894e9 100644 --- a/src/video.h +++ b/src/video.h @@ -28,38 +28,38 @@ class TextCharacters; class Video { private: - enum { TEXT_BASE_1 = 0x0400 }; - enum { TEXT_BASE_2 = 0x0800 }; - enum { TEXT_LEN = 0x0400 }; - - enum { HRES_BASE_1 = 0x2000 }; - enum { HRES_BASE_2 = 0x4000 }; - enum { HRES_LEN = 0x2000 }; - - std::vector lutTEXT[2]; // [0] is page 1, [1] is page 2 - std::vector lutHRES[2]; // [0] is page 1, [1] is page 2 + enum { TEXT_BASE_1 = 0x0400 }; + enum { TEXT_BASE_2 = 0x0800 }; + enum { TEXT_LEN = 0x0400 }; + + enum { HRES_BASE_1 = 0x2000 }; + enum { HRES_BASE_2 = 0x4000 }; + enum { HRES_LEN = 0x2000 }; + + std::vector lutTEXT[2]; // [0] is page 1, [1] is page 2 + std::vector lutHRES[2]; // [0] is page 1, [1] is page 2 - VideoMode& mode; - AddressBus& addressBus; - PictureGenerator& picgen; + VideoMode& mode; + AddressBus& addressBus; + PictureGenerator& picgen; - TextCharacters& textRows; + TextCharacters& textRows; - unsigned int t; + unsigned int t; - bool flash; - int cflash; + bool flash; + int cflash; - void updateFlash(); - unsigned char getDataByte(); - unsigned char getRowToPlot(unsigned char d); - bool inverseChar(const unsigned char d); + void updateFlash(); + unsigned char getDataByte(); + unsigned char getRowToPlot(unsigned char d); + bool inverseChar(const unsigned char d); public: - Video(VideoMode& mode, AddressBus& addressBus, PictureGenerator& picgen, TextCharacters& textRows); - ~Video(); - void powerOn(); - void tick(); + Video(VideoMode& mode, AddressBus& addressBus, PictureGenerator& picgen, TextCharacters& textRows); + ~Video(); + void powerOn(); + void tick(); }; #endif /*VIDEO_H_*/ diff --git a/src/videoaddressing.cpp b/src/videoaddressing.cpp index b41d7a9..73514fa 100644 --- a/src/videoaddressing.cpp +++ b/src/videoaddressing.cpp @@ -24,62 +24,62 @@ VideoAddressing::VideoAddressing() static int calc(const unsigned int t) { - int c = t % E2Const::VISIBLE_BYTES_PER_FIELD; - if (t >= E2Const::SCANNABLE_BYTES) - { - c -= E2Const::RESET_BYTES; - } + int c = t % E2Const::VISIBLE_BYTES_PER_FIELD; + if (t >= E2Const::SCANNABLE_BYTES) + { + c -= E2Const::RESET_BYTES; + } - int n = c / E2Const::BYTES_PER_ROW; - const int s = (n >> 6); - n -= s << 6; - const int q = (n >> 3); - n -= q << 3; - const int base = (n<<10) + (q<<7) + E2Const::VISIBLE_BYTES_PER_ROW*s; + int n = c / E2Const::BYTES_PER_ROW; + const int s = (n >> 6); + n -= s << 6; + const int q = (n >> 3); + n -= q << 3; + const int base = (n<<10) + (q<<7) + E2Const::VISIBLE_BYTES_PER_ROW*s; - const int half_page = base & 0xFF80; + const int half_page = base & 0xFF80; - int a = base+(c%E2Const::BYTES_PER_ROW)-E2Const::BLANKED_BYTES_PER_ROW; - if (a < half_page) - { - a += 0x80; - } - return a; + int a = base+(c%E2Const::BYTES_PER_ROW)-E2Const::BLANKED_BYTES_PER_ROW; + if (a < half_page) + { + a += 0x80; + } + return a; } void VideoAddressing::buildLUT(const unsigned short base, const unsigned short len, std::vector& lut) { - lut.resize(E2Const::BYTES_PER_FIELD); - for (unsigned int t = 0; t < E2Const::BYTES_PER_FIELD; ++t) - { - unsigned int off = (calc(t) % len); + lut.resize(E2Const::BYTES_PER_FIELD); + for (unsigned int t = 0; t < E2Const::BYTES_PER_FIELD; ++t) + { + unsigned int off = (calc(t) % len); - const unsigned int col = t % E2Const::BYTES_PER_ROW; - const unsigned int row = t / E2Const::BYTES_PER_ROW; + const unsigned int col = t % E2Const::BYTES_PER_ROW; + const unsigned int row = t / E2Const::BYTES_PER_ROW; - if (col < E2Const::BLANKED_BYTES_PER_ROW) - { - // HBL - if (base < 0x1000) - { - off += 0x1000; - } - if (col == 0) - { - ++off; - } - } + if (col < E2Const::BLANKED_BYTES_PER_ROW) + { + // HBL + if (base < 0x1000) + { + off += 0x1000; + } + if (col == 0) + { + ++off; + } + } - if (row >= E2Const::VISIBLE_ROWS_PER_FIELD) - { - // VBL - const int base2 = off & 0xFF80; - off &= 0x7F; - off -= 8; - off &= 0x7F; - off += base2; - } + if (row >= E2Const::VISIBLE_ROWS_PER_FIELD) + { + // VBL + const int base2 = off & 0xFF80; + off &= 0x7F; + off -= 8; + off &= 0x7F; + off += base2; + } - lut[t] = base + off; - } + lut[t] = base + off; + } } diff --git a/src/videoaddressing.h b/src/videoaddressing.h index a3e1e3c..084e1b2 100644 --- a/src/videoaddressing.h +++ b/src/videoaddressing.h @@ -23,8 +23,8 @@ class VideoAddressing { public: - VideoAddressing(); - static void buildLUT(const unsigned short base, const unsigned short len, std::vector& lut); + VideoAddressing(); + static void buildLUT(const unsigned short base, const unsigned short len, std::vector& lut); }; #endif diff --git a/src/videomode.cpp b/src/videomode.cpp index 7db19ed..2542079 100644 --- a/src/videomode.cpp +++ b/src/videomode.cpp @@ -23,26 +23,26 @@ VideoMode::VideoMode() unsigned char VideoMode::io(const unsigned short addr, const unsigned char b) { - const int sw = (addr & 0xE) >> 1; - const bool on = addr & 0x1; - switch (sw) - { - case 0: - this->swText = on; break; - case 1: - this->swMixed = on; break; - case 2: - this->swPage2 = on ? 1 : 0; break; - case 3: - this->swHiRes = on; break; - } - return b; + const int sw = (addr & 0xE) >> 1; + const bool on = addr & 0x1; + switch (sw) + { + case 0: + this->swText = on; break; + case 1: + this->swMixed = on; break; + case 2: + this->swPage2 = on ? 1 : 0; break; + case 3: + this->swHiRes = on; break; + } + return b; } void VideoMode::powerOn() { - this->swText = false; - this->swMixed = false; - this->swPage2 = 0; - this->swHiRes = false; + this->swText = false; + this->swMixed = false; + this->swPage2 = 0; + this->swHiRes = false; } diff --git a/src/videomode.h b/src/videomode.h index 572386e..8507407 100644 --- a/src/videomode.h +++ b/src/videomode.h @@ -24,21 +24,21 @@ class VideoMode { private: - bool swText; - bool swMixed; - int swPage2; - bool swHiRes; + bool swText; + bool swMixed; + int swPage2; + bool swHiRes; public: - VideoMode(); - unsigned char io(const unsigned short addr, const unsigned char b); - void powerOn(); + VideoMode(); + unsigned char io(const unsigned short addr, const unsigned char b); + void powerOn(); - bool isText() const { return this->swText; } - bool isHiRes() const { return this->swHiRes; } - bool isMixed() const { return this->swMixed; } - int getPage() const { return this->swPage2; } - bool isDisplayingText(const int atTickInField) const { return this->swText || (this->swMixed && atTickInField >= E2Const::MIXED_TEXT_CYCLE); } + bool isText() const { return this->swText; } + bool isHiRes() const { return this->swHiRes; } + bool isMixed() const { return this->swMixed; } + int getPage() const { return this->swPage2; } + bool isDisplayingText(const int atTickInField) const { return this->swText || (this->swMixed && atTickInField >= E2Const::MIXED_TEXT_CYCLE); } }; #endif diff --git a/src/videostaticgenerator.cpp b/src/videostaticgenerator.cpp index 6f9de86..15addc1 100644 --- a/src/videostaticgenerator.cpp +++ b/src/videostaticgenerator.cpp @@ -22,13 +22,13 @@ #include VideoStaticGenerator::VideoStaticGenerator(AnalogTV& display): - display(display), - isig(sig), - isiglim(sig+AppleNTSC::SIGNAL_LEN), - hpos(0) + display(display), + isig(sig), + isiglim(sig+AppleNTSC::SIGNAL_LEN), + hpos(0) { - this->display.signal = sig; - srand(time(0)); + this->display.signal = sig; + srand(time(0)); } @@ -39,32 +39,32 @@ VideoStaticGenerator::~VideoStaticGenerator() void VideoStaticGenerator::tick() { - signed char* is = this->isig; - unsigned int cycles = E2Const::CRYSTAL_CYCLES_PER_CPU_CYCLE; - if (this->hpos == E2Const::HORIZ_CYCLES-1) - { - cycles += E2Const::EXTRA_CRYSTAL_CYCLES_PER_CPU_LONG_CYCLE; - } - for (unsigned int i = 0; i < cycles; ++i) - { - *is++ = (rand()>>7&0x7F)-27; - } - this->isig = is; - ++this->hpos; - if (this->hpos >= E2Const::HORIZ_CYCLES) - { - this->hpos = 0; - if (isig >= isiglim) - { - isig = sig; - this->display.drawCurrent(); - } - } + signed char* is = this->isig; + unsigned int cycles = E2Const::CRYSTAL_CYCLES_PER_CPU_CYCLE; + if (this->hpos == E2Const::HORIZ_CYCLES-1) + { + cycles += E2Const::EXTRA_CRYSTAL_CYCLES_PER_CPU_LONG_CYCLE; + } + for (unsigned int i = 0; i < cycles; ++i) + { + *is++ = (rand()>>7&0x7F)-27; + } + this->isig = is; + ++this->hpos; + if (this->hpos >= E2Const::HORIZ_CYCLES) + { + this->hpos = 0; + if (isig >= isiglim) + { + isig = sig; + this->display.drawCurrent(); + } + } } void VideoStaticGenerator::powerOn() { - this->hpos = 0; - this->display.signal = sig; - isig = sig; + this->hpos = 0; + this->display.signal = sig; + isig = sig; } diff --git a/src/videostaticgenerator.h b/src/videostaticgenerator.h index eaa59aa..7bf6288 100644 --- a/src/videostaticgenerator.h +++ b/src/videostaticgenerator.h @@ -25,18 +25,18 @@ class AnalogTV; class VideoStaticGenerator : public Timable { private: - AnalogTV& display; - signed char sig[AppleNTSC::SIGNAL_LEN]; - signed char* isig; - signed char* isiglim; - unsigned int hpos; + AnalogTV& display; + signed char sig[AppleNTSC::SIGNAL_LEN]; + signed char* isig; + signed char* isiglim; + unsigned int hpos; public: - VideoStaticGenerator(AnalogTV& display); - ~VideoStaticGenerator(); + VideoStaticGenerator(AnalogTV& display); + ~VideoStaticGenerator(); - virtual void tick(); - void powerOn(); + virtual void tick(); + void powerOn(); }; #endif