AmiTimeKeeper/win_gad.c

687 lines
17 KiB
C
Raw Normal View History

2018-11-24 21:39:18 +00:00
/*-
* Copyright (c) 2017-2018 Carsten Sonne Larsen <cs@innolan.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#include "message.h"
#include "state.h"
#include "time.h"
#include "mem.h"
#include "win.h"
struct AppSettingWindowGadgets
{
struct Gadget *ServerGadget;
struct Gadget *PortGadget;
struct Gadget *TimeoutGadget;
struct Gadget *IntervalGadget;
struct Gadget *ThresholdGadget;
struct Gadget *VerboseGadget;
struct Gadget *PriorityGadget;
struct Gadget *TimeZoneGadget;
struct Gadget *LastSyncGadget;
};
static const char *serverLabel = "Server address";
static const char *portLabel = "Server port";
static const char *timeoutLabel = "Timeout (ms)";
static const char *intervalLabel = "Interval (ms)";
static const char *thresholdLabel = "Threshold (us)";
static const char *verboseLabel = "Verbose level";
static const char *priorityLabel = "CX priority";
static const char *timeZoneLabel = "Time zone";
static const char *lastSyncLabel = "Last sync";
static const char *saveLabel = "Save";
static const char *useLabel = "Use";
static const char *cancelLabel = "Cancel";
static char *verboseLevel[] = {
"Show none (0)",
"Show some (1)",
"Show more (2)",
"Show all (3)",
NULL};
bool CreateGadgets(void)
{
struct Gadget *gadget;
struct NewGadget *ng;
long x, y, w, h;
long textWidth, boxWidth;
long col1, col2;
long labelId;
long tmp1, tmp2;
Globals->Window->Gadgets = (struct AppSettingWindowGadgets *)
AllocMemSafe(sizeof(struct AppSettingWindowGadgets));
ng = (struct NewGadget *)AllocMemSafe(sizeof(struct NewGadget));
if (!ng)
return false;
gadget = CreateContext(&Globals->Window->GadgetList);
if (!gadget)
return false;
ng->ng_VisualInfo = Globals->Window->VisualInfo;
ng->ng_TextAttr = Globals->Window->Screen->Font;
ng->ng_Flags = 0;
w = Globals->Window->Screen->RastPort.TxWidth;
h = Globals->Window->Screen->RastPort.TxHeight;
x = Globals->Window->Screen->WBorLeft + 8;
y = Globals->Window->Screen->WBorTop + h + 9;
col1 = x;
textWidth = 13 + VARSIZE;
col2 = col1 + w * textWidth;
boxWidth = 13 + VARSIZE * 2;
ng->ng_TopEdge = y;
ng->ng_Height = h + 6;
labelId = 5000;
// Server
ng->ng_LeftEdge = col1;
ng->ng_Width = textWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTTX_Text, (IPTR)serverLabel,
TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge = col2;
ng->ng_Width = boxWidth * w + 20;
ng->ng_GadgetID = GID_SERVER;
gadget = CreateGadget(
STRING_KIND, gadget, ng,
GTST_String, (IPTR)Globals->Settings->DestinationAddress,
TAG_END);
if (!gadget)
return false;
Globals->Window->Gadgets->ServerGadget = gadget;
// Port
ng->ng_LeftEdge = col1;
ng->ng_TopEdge += ng->ng_Height + 4;
ng->ng_Width = textWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTTX_Text, (IPTR)portLabel,
TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge = col2;
ng->ng_Width = boxWidth * w + 20;
ng->ng_GadgetID = GID_PORT;
gadget = CreateGadget(
STRING_KIND, gadget, ng,
GTST_String, (IPTR)Globals->Settings->DestinationPort,
TAG_END);
if (!gadget)
return false;
Globals->Window->Gadgets->PortGadget = gadget;
// Interval
ng->ng_LeftEdge = col1;
ng->ng_TopEdge += ng->ng_Height + 4;
ng->ng_Width = textWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTNM_MaxNumberLen, 8,
GTTX_Text, (IPTR)intervalLabel,
TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge = col2;
ng->ng_Width = boxWidth * w + 20;
ng->ng_GadgetID = GID_INTERVAL;
gadget = CreateGadget(
INTEGER_KIND, gadget, ng,
GTNM_MaxNumberLen, 8,
GTIN_Number, Globals->Settings->Interval,
TAG_END);
if (!gadget)
return false;
gadget->Flags |= GFLG_TABCYCLE;
Globals->Window->Gadgets->IntervalGadget = gadget;
// Timeout
ng->ng_LeftEdge = col1;
ng->ng_TopEdge += ng->ng_Height + 4;
ng->ng_Width = textWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTTX_Text, (IPTR)timeoutLabel,
TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge = col2;
ng->ng_Width = boxWidth * w + 20;
ng->ng_GadgetID = GID_TIMEOUT;
gadget = CreateGadget(
INTEGER_KIND, gadget, ng,
GTNM_MaxNumberLen, 8,
GTIN_Number, Globals->Settings->Timeout,
TAG_END);
if (!gadget)
return false;
gadget->Flags |= GFLG_TABCYCLE;
Globals->Window->Gadgets->TimeoutGadget = gadget;
// Threshold
ng->ng_LeftEdge = col1;
ng->ng_TopEdge += ng->ng_Height + 4;
ng->ng_Width = textWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTTX_Text, (IPTR)thresholdLabel,
TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge = col2;
ng->ng_Width = boxWidth * w + 20;
ng->ng_GadgetID = GID_THRESHOLD;
gadget = CreateGadget(
STRING_KIND, gadget, ng,
GTST_String, (IPTR)Globals->Window->ThresholdText,
TAG_END);
if (!gadget)
return false;
gadget->Flags |= GFLG_TABCYCLE;
Globals->Window->Gadgets->ThresholdGadget = gadget;
// Verbose
ng->ng_LeftEdge = col1;
ng->ng_TopEdge += ng->ng_Height + 4;
ng->ng_Width = textWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTTX_Text, (IPTR)verboseLabel,
TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge = col2;
ng->ng_Width = boxWidth * w + 20;
ng->ng_GadgetID = GID_VERBOSE;
gadget = CreateGadget(
CYCLE_KIND, gadget, ng,
GTCY_Labels, (IPTR)verboseLevel,
GTCY_Active, Globals->Settings->Verbose,
TAG_END);
if (!gadget)
return false;
gadget->Flags |= GFLG_TABCYCLE;
Globals->Window->Gadgets->VerboseGadget = gadget;
// Priority
ng->ng_LeftEdge = col1;
ng->ng_TopEdge += ng->ng_Height + 4;
ng->ng_Width = textWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTTX_Text, (IPTR)priorityLabel,
TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge = col2;
ng->ng_Width = (boxWidth * w + 20) / 2;
ng->ng_GadgetID = GID_PRIORITY;
gadget = CreateGadget(
STRING_KIND, gadget, ng,
GTST_String, (IPTR)Globals->Window->PriorityText,
TAG_END);
if (!gadget)
return false;
gadget->Flags |= GFLG_TABCYCLE;
Globals->Window->Gadgets->PriorityGadget = gadget;
// Time zone
ng->ng_LeftEdge = col1;
ng->ng_TopEdge += ng->ng_Height + 4;
ng->ng_Width = textWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTTX_Text, (IPTR)timeZoneLabel,
TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge = col2;
ng->ng_Width = boxWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTTX_Text, (IPTR)Globals->Window->TimeZoneText,
TAG_END);
if (!gadget)
return false;
Globals->Window->Gadgets->TimeZoneGadget = gadget;
// Last sync
ng->ng_LeftEdge = col1;
ng->ng_TopEdge += ng->ng_Height + 4;
ng->ng_Width = textWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
GTTX_Text, (IPTR)lastSyncLabel,
TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge = col2;
ng->ng_Width = boxWidth * w + 20;
ng->ng_GadgetID = labelId++;
gadget = CreateGadget(
TEXT_KIND, gadget, ng,
TAG_END);
if (!gadget)
return false;
Globals->Window->Gadgets->LastSyncGadget = gadget;
tmp1 = ng->ng_LeftEdge + ng->ng_Width - 18; // Why is this 18?
tmp2 = tmp1 / 3;
ng->ng_TopEdge += ng->ng_Height + 6;
ng->ng_Width = tmp2;
ng->ng_LeftEdge = col1;
ng->ng_GadgetText = (STRPTR)saveLabel;
ng->ng_GadgetID = GID_SAVE;
ng->ng_Flags = 0;
gadget = CreateGadget(BUTTON_KIND, gadget, ng, TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge += ng->ng_Width + 4;
ng->ng_GadgetText = (STRPTR)useLabel;
ng->ng_GadgetID = GID_USE;
gadget = CreateGadget(BUTTON_KIND, gadget, ng, TAG_END);
if (!gadget)
return false;
ng->ng_LeftEdge += ng->ng_Width + 4;
ng->ng_GadgetText = (STRPTR)cancelLabel;
ng->ng_GadgetID = GID_CANCEL;
gadget = CreateGadget(BUTTON_KIND, gadget, ng, TAG_END);
if (!gadget)
return false;
Globals->Window->Height = ng->ng_TopEdge + ng->ng_Height;
Globals->Window->Width = textWidth * w + boxWidth * w;
FreeMemSafe(ng);
return true;
}
static char *BuildLogText(const char *setting, char *oldValue, char *newValue)
{
static const char *txt1 = " changed: ";
static const char *txt2 = " -> ";
char *p = AllocMemSafe(SETTINGMESSAGELEN);
char *s = p;
p = AppendText(p, (char *)setting);
p = AppendText(p, (char *)txt1);
p = AppendText(p, oldValue);
p = AppendText(p, (char *)txt2);
p = AppendText(p, newValue);
return s;
}
void UseSettings(void)
{
SetServer();
SetPort();
SetInterval();
SetTimeout();
SetThreshold();
SetVerbose();
SetCxPriority();
}
void SetServer(void)
{
char *server, *text;
GT_GetGadgetAttrs(
Globals->Window->Gadgets->ServerGadget,
Globals->Window->SettingWindow, NULL,
GTST_String, (IPTR)&server,
TAG_END);
if (Stricmp((STRPTR)server, (STRPTR)Globals->Settings->DestinationAddress) == 0)
return;
text = BuildLogText(SettingKeys->DestinationAddress,
Globals->Settings->DestinationAddress,
server);
FreeMemSafe(Globals->Settings->DestinationAddress);
Globals->Settings->DestinationAddress = StrDupSafe(server);
SendInfoMessage(text);
SendMessage(ATK_RESTART);
}
void SetPort(void)
{
char *port, *text;
GT_GetGadgetAttrs(
Globals->Window->Gadgets->PortGadget,
Globals->Window->SettingWindow, NULL,
GTST_String, (IPTR)&port,
TAG_END);
if (Stricmp((STRPTR)port, (STRPTR)Globals->Settings->DestinationPort) == 0)
return;
text = BuildLogText(SettingKeys->DestinationPort,
Globals->Settings->DestinationPort,
port);
FreeMemSafe(Globals->Settings->DestinationPort);
Globals->Settings->DestinationPort = StrDupSafe(port);
SendInfoMessage(text);
SendMessage(ATK_RESTART);
}
void SetInterval(void)
{
unsigned long value;
GT_GetGadgetAttrs(
Globals->Window->Gadgets->IntervalGadget,
Globals->Window->SettingWindow, NULL,
GTIN_Number, (IPTR)&value,
TAG_END);
if (value < INTERVAL_MIN)
value = INTERVAL_MIN;
if (Globals->Settings->Interval != value)
{
char *text;
char newValue[MAXLONGCHARSIZE];
char oldValue[MAXLONGCHARSIZE];
LongToStr(Globals->Settings->Interval, oldValue);
LongToStr(value, newValue);
Globals->Settings->Interval = value;
text = BuildLogText(SettingKeys->Interval, oldValue, newValue);
SendInfoMessage(text);
}
}
void SetTimeout(void)
{
unsigned long value;
GT_GetGadgetAttrs(
Globals->Window->Gadgets->TimeoutGadget,
Globals->Window->SettingWindow, NULL,
GTIN_Number, (IPTR)&value,
TAG_END);
if (value < TIMEOUT_MIN)
value = TIMEOUT_MIN;
else if (value > TIMEOUT_MAX)
value = TIMEOUT_MAX;
if (Globals->Settings->Timeout != value)
{
char *text;
char newValue[MAXLONGCHARSIZE];
char oldValue[MAXLONGCHARSIZE];
LongToStr(Globals->Settings->Timeout, oldValue);
LongToStr(value, newValue);
Globals->Settings->Timeout = value;
text = BuildLogText(SettingKeys->Timeout, oldValue, newValue);
SendInfoMessage(text);
}
}
void SetThreshold(void)
{
unsigned long long value;
char *tempString;
int result;
GT_GetGadgetAttrs(
Globals->Window->Gadgets->ThresholdGadget,
Globals->Window->SettingWindow, NULL,
GTST_String, (IPTR)&tempString,
TAG_END);
result = StrToLongLong(tempString, &value);
if (result != -1 && Globals->Settings->Threshold != value)
{
char *text;
char newValue[MAXLONGLONGCHARSIZE];
char oldValue[MAXLONGLONGCHARSIZE];
LongLongToStr(Globals->Settings->Threshold, oldValue);
LongLongToStr(value, newValue);
Globals->Settings->Threshold = value;
text = BuildLogText(SettingKeys->Threshold, oldValue, newValue);
SendInfoMessage(text);
SendMessage(ATK_RESTART);
}
}
void SetVerbose(void)
{
long value;
GT_GetGadgetAttrs(
Globals->Window->Gadgets->VerboseGadget,
Globals->Window->SettingWindow, NULL,
GTCY_Active, (IPTR)&value,
TAG_END);
if (value < VERBOSE_MIN)
value = VERBOSE_MIN;
else if (value > VERBOSE_MAX)
value = VERBOSE_MAX;
if (Globals->Settings->Verbose != value)
{
char *text;
char newValue[MAXLONGCHARSIZE];
char oldValue[MAXLONGCHARSIZE];
LongToStr(Globals->Settings->Verbose, oldValue);
LongToStr(value, newValue);
text = BuildLogText(SettingKeys->Verbose, oldValue, newValue);
SendInfoMessage(text);
Globals->Settings->Verbose = value;
}
}
void SetCxPriority(void)
{
LONG value;
char *tempString;
int result;
GT_GetGadgetAttrs(
Globals->Window->Gadgets->PriorityGadget,
Globals->Window->SettingWindow, NULL,
GTST_String, (IPTR)&tempString,
TAG_END);
result = StrToLong((STRPTR)tempString, &value);
if (result != -1)
{
if (value < PRIORITY_MIN)
value = PRIORITY_MIN;
else if (value > PRIORITY_MAX)
value = PRIORITY_MAX;
if (Globals->Settings->Priority != value)
{
char *text;
char newValue[MAXLONGCHARSIZE];
char oldValue[MAXLONGCHARSIZE];
LongToStr(Globals->Settings->Priority, oldValue);
LongToStr(value, newValue);
Globals->Settings->Priority = value;
text = BuildLogText(SettingKeys->Priority, oldValue, newValue);
SetBrokerPriority(value);
SendInfoMessage(text);
}
}
}
void ShowLastSync(void)
{
static char label[10];
struct timeval nowLocal, last, lastLocal;
struct ClockData clockdata;
struct DateTime dateTime = {
.dat_Stamp = {.ds_Days = 0},
.dat_Format = FORMAT_DOS,
.dat_Flags = 0,
.dat_StrDay = NULL,
.dat_StrDate = NULL,
.dat_StrTime = (STRPTR)label};
long seconds;
if (Globals->LastNtpSync.tv_secs == 0)
{
static const char *never = "Not synced yet";
GT_SetGadgetAttrs(
Globals->Window->Gadgets->LastSyncGadget,
Globals->Window->SettingWindow, NULL,
GTTX_Text, (IPTR)never,
TAG_END);
return;
}
GetLocalTimeOfDay(&nowLocal);
Unix2Amiga(&Globals->LastNtpSync, &last);
Utc2Local(&last, &lastLocal);
seconds = nowLocal.tv_secs - lastLocal.tv_secs;
if (seconds > 60 * 60 * 24)
{
static const char *tooOld = "> 24 hours";
GT_SetGadgetAttrs(
Globals->Window->Gadgets->LastSyncGadget,
Globals->Window->SettingWindow, NULL,
GTTX_Text, (IPTR)tooOld,
TAG_END);
return;
}
Amiga2Date(lastLocal.tv_secs, &clockdata);
dateTime.dat_Stamp.ds_Minute = clockdata.hour * 60 + clockdata.min;
dateTime.dat_Stamp.ds_Tick = clockdata.sec * 50;
DateToStr(&dateTime);
GT_SetGadgetAttrs(
Globals->Window->Gadgets->LastSyncGadget,
Globals->Window->SettingWindow, NULL,
GTTX_Text, (IPTR)label,
TAG_END);
}
void SetDefaultSettings(void)
{
static char priorityText[MAXLONGCHARSIZE];
static char thresholdText[MAXLONGLONGCHARSIZE];
GT_SetGadgetAttrs(
Globals->Window->Gadgets->ServerGadget,
Globals->Window->SettingWindow, NULL,
GTST_String, (IPTR)DefaultSettings.DestinationAddress,
TAG_END);
GT_SetGadgetAttrs(
Globals->Window->Gadgets->PortGadget,
Globals->Window->SettingWindow, NULL,
GTST_String, (IPTR)DefaultSettings.DestinationPort,
TAG_END);
GT_SetGadgetAttrs(
Globals->Window->Gadgets->TimeoutGadget,
Globals->Window->SettingWindow, NULL,
GTIN_Number, DefaultSettings.Timeout,
TAG_END);
GT_SetGadgetAttrs(
Globals->Window->Gadgets->IntervalGadget,
Globals->Window->SettingWindow, NULL,
GTIN_Number, DefaultSettings.Interval,
TAG_END);
LongLongToStr(DefaultSettings.Threshold, thresholdText);
GT_SetGadgetAttrs(
Globals->Window->Gadgets->ThresholdGadget,
Globals->Window->SettingWindow, NULL,
GTST_String, (IPTR)thresholdText,
TAG_END);
GT_SetGadgetAttrs(
Globals->Window->Gadgets->VerboseGadget,
Globals->Window->SettingWindow, NULL,
GTCY_Active, DefaultSettings.Verbose,
TAG_END);
LongToStr(DefaultSettings.Priority, priorityText);
GT_SetGadgetAttrs(
Globals->Window->Gadgets->PriorityGadget,
Globals->Window->SettingWindow, NULL,
GTST_String, (IPTR)priorityText,
TAG_END);
}