kpmpgsmkii/client/src/welcome.c

254 lines
6.7 KiB
C

/*
* Kangaroo Punch MultiPlayer Game Server Mark II
* Copyright (C) 2020-2021 Scott Duensing
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "welcome.h"
#include "settings.h"
#include "taglist.h"
#include "task.h"
#include "config.h"
#include "comport.h"
#include "timer.h"
#include "window.h"
#include "picture.h"
#include "button.h"
#include "msgbox.h"
static WindowT *winWelcome = NULL;
static PictureT *picLogo = NULL;
static PictureT *picInit = NULL;
static PictureT *picDialing = NULL;
static PictureT *picConnect = NULL;
static ButtonT *btnQuit = NULL;
static ButtonT *btnSettings = NULL;
static ButtonT *btnConnect = NULL;
static void btnConnectClick(WidgetT *widget);
static void btnMsgBox(MsgBoxButtonT button);
static void btnMsgBoxQuit(MsgBoxButtonT button);
static void btnQuitClick(WidgetT *widget);
static void btnSettingsClick(WidgetT *widget);
static void setButtons(uint8_t enabled);
static void settingsFinished(WidgetT *widget);
static void btnMsgBox(MsgBoxButtonT button) {
(void)button;
// Re-enable buttons.
setButtons(1);
// Reset to Welcome image.
widgetVisibleSet(W(picLogo), 1);
widgetVisibleSet(W(picInit), 0);
widgetVisibleSet(W(picDialing), 0);
widgetVisibleSet(W(picConnect), 0);
}
static void btnMsgBoxQuit(MsgBoxButtonT button) {
if (button == MSGBOX_BUTTON_ONE) {
guiStop();
} else {
setButtons(1);
}
}
static void btnConnectClick(WidgetT *widget) {
uint32_t r;
uint32_t len;
char buffer[1024];
int8_t timeout = 7 * 4; // Seven seconds (of quarter seconds) to connect to server.
uint16_t count = 0;
uint8_t connected = 0;
char *banner = "KPMPGSMKII";
(void)widget;
// Ghost all buttons.
widgetEnableSet(W(btnConnect), 0);
widgetEnableSet(W(btnSettings), 0);
widgetEnableSet(W(btnQuit), 0);
// Hide welcome banner, show init.
widgetVisibleSet(W(picLogo), 0);
widgetVisibleSet(W(picInit), 1);
taskYield();
// Open COM port.
r = comOpen(_configData.serialCom - 1, 57600L, 8, 'n', 1, SER_HANDSHAKING_RTSCTS);
if (r != SER_SUCCESS) {
msgBoxOne("COM Problem", MSGBOX_ICON_ERROR, "Unable to open COM port!\nPlease check settings.", "Okay", btnMsgBox);
return;
}
logWrite("COM open\n");
// Send a CR to clear anything in the modem.
snprintf(buffer, 1023, "%c", 13);
comWrite(_configData.serialCom - 1, buffer, strlen(buffer));
logWrite("CR sent\n");
// Wait roughly a second.
comWaitWithTimeout(_configData.serialCom - 1, buffer, 1023, 4, "weExpectNothing");
//uint32_t dumbThing = timerQuarterSecondsWait(4);
logWrite("Wait over\n");
// Flush COM port.
comReceiveBufferFlush(_configData.serialCom - 1);
logWrite("Port flushed\n");
// Send actual init
snprintf(buffer, 1023, "%s%c", "AT+SOCK1", 13);
comWrite(_configData.serialCom - 1, buffer, strlen(buffer));
// Wait roughly a second.
// timerQuarterSecondsWait(4);
// Read COM port & check for OK.
len = comRead(_configData.serialCom - 1, buffer, 1023);
buffer[len] = 0;
if (!strstr(buffer, "OK")) {
comClose(_configData.serialCom - 1);
msgBoxOne("Modem Problem", MSGBOX_ICON_ERROR, "Modem does not support ENET!\nPlease check settings.", "Okay", btnMsgBox);
return;
}
// Show dialing, dial service.
widgetVisibleSet(W(picDialing), 1);
taskYield();
snprintf(buffer, 1023, "ATDT%s:%d%c", _configData.serverHost, _configData.serverPort, 13);
comWrite(_configData.serialCom - 1, buffer, strlen(buffer));
while (timeout > 0 && connected == 0) {
len = comRead(_configData.serialCom - 1, buffer, 1);
// Did we get a character?
if (len == 1) {
// Is it the next we expect in the server banner?
if (buffer[0] == banner[count]) {
count++;
} else {
count = 0;
}
// Are we connected?
if (count == strlen(banner)) {
connected = 1;
}
}
// Tick timeout.
if (_timerQuarterSecondTick) timeout--;
taskYield();
}
// timerQuarterSecondsWait(4);
comReceiveBufferFlush(_configData.serialCom - 1);
// Did we connect?
if (!connected) {
comClose(_configData.serialCom - 1);
msgBoxOne("No Connection", MSGBOX_ICON_INFORMATION, "Unable to connect to server!\nPlease check settings or try later.", "Okay", btnMsgBox);
return;
}
// Connected! Show icon and negotiate session.
widgetVisibleSet(W(picConnect), 1);
taskYield();
}
static void btnQuitClick(WidgetT *widget) {
(void)widget;
setButtons(0);
msgBoxTwo("Quit?", MSGBOX_ICON_QUESTION, "Exit to DOS?", "Okay", btnMsgBoxQuit, "Cancel", btnMsgBoxQuit);
}
static void btnSettingsClick(WidgetT *widget) {
(void)widget;
setButtons(0);
taskCreate(taskSettings, settingsFinished);
}
void setButtons(uint8_t enabled) {
widgetEnableSet(W(btnConnect), (_configData.serialCom > 0 && strlen(_configData.serverHost) > 2) ? enabled : 0);
widgetEnableSet(W(btnSettings), enabled);
widgetEnableSet(W(btnQuit), enabled);
}
static void settingsFinished(WidgetT *widget) {
(void)widget;
setButtons(1);
}
void taskWelcome(void *data) {
(void)data;
// 450x128 logo
TagItemT uiWelcome[] = {
T_START,
T_WINDOW, O(winWelcome),
T_TITLE, P("Welcome to KangaWorld!"),
T_WIDTH, 500, T_HEIGHT, 225,
T_PICTURE, O(picLogo),
T_FILENAME, P("data/logo.png"),
T_X, 18, T_Y, 18,
T_PICTURE, T_DONE,
T_PICTURE, O(picInit),
T_FILENAME, P("data/dinit.png"),
T_X, 18, T_Y, 18,
T_VISIBLE, T_FALSE,
T_PICTURE, T_DONE,
T_PICTURE, O(picDialing),
T_FILENAME, P("data/ddialing.png"),
T_X, 179, T_Y, 18,
T_VISIBLE, T_FALSE,
T_PICTURE, T_DONE,
T_PICTURE, O(picConnect),
T_FILENAME, P("data/dconnect.png"),
T_X, 339, T_Y, 18,
T_VISIBLE, T_FALSE,
T_PICTURE, T_DONE,
T_BUTTON, O(btnQuit),
T_TITLE, P("Quit"),
T_X, 30, T_Y, 157,
T_CLICK, P(btnQuitClick),
T_BUTTON, T_DONE,
T_BUTTON, O(btnSettings),
T_TITLE, P("Settings"),
T_X, 201, T_Y, 157,
T_CLICK, P(btnSettingsClick),
T_BUTTON, T_DONE,
T_BUTTON, O(btnConnect),
T_TITLE, P("Connect"),
T_X, 379, T_Y, 157,
T_CLICK, P(btnConnectClick),
T_ENABLED, (_configData.serialCom > 0 && strlen(_configData.serverHost) > 2) ? T_TRUE : T_FALSE,
T_BUTTON, T_DONE,
T_WINDOW, T_DONE,
T_END
};
tagListRun(uiWelcome);
}