From 5df9d3d8f715aa739a10c7b17a732d208bcd48c2 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sun, 12 Apr 2020 19:49:17 +1000 Subject: [PATCH 01/14] fix use autocreate pref file. user pref for color/emoji linux --- client/preferences.c | 1 + client/proxmark3.c | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/client/preferences.c b/client/preferences.c index 21c38fb2e..b57170295 100644 --- a/client/preferences.c +++ b/client/preferences.c @@ -476,6 +476,7 @@ static int CmdPrefSet (const char *Cmd) if (w != -99999) session.window_plot_wsize = w; // Need to work out how to change live.... // calling data plot seems to work + //plotwidget->MoveWindows (); showPlotPosState (); break; diff --git a/client/proxmark3.c b/client/proxmark3.c index bfc7b6212..d152fa93a 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -30,7 +30,7 @@ #include "preferences.h" // Used to enable/disable use of preferences json file -// #define USE_PREFERENCE_FILE +#define USE_PREFERENCE_FILE #ifdef _WIN32 @@ -844,10 +844,20 @@ int main(int argc, char *argv[]) { // For info, grep --color=auto is doing sth like this, plus test getenv("TERM") != "dumb": // struct stat tmp_stat; // if ((fstat (STDOUT_FILENO, &tmp_stat) == 0) && (S_ISCHR (tmp_stat.st_mode)) && isatty(STDIN_FILENO)) +#ifdef USE_PREFERENCE_FILE + if (!session.preferences_loaded) { + if (session.stdinOnTTY && session.stdoutOnTTY) { + session.supports_colors = true; + session.emoji_mode = EMOJI; + } + } +#else if (session.stdinOnTTY && session.stdoutOnTTY) { session.supports_colors = true; session.emoji_mode = EMOJI; } +#endif + #endif // Let's take a baudrate ok for real UART, USB-CDC & BT don't use that info anyway if (speed == 0) @@ -907,8 +917,10 @@ int main(int argc, char *argv[]) { // Save settings if not load from settings json file. // Doing this here will ensure other checks and updates are saved to over rule default // e.g. Linux color use check - if (!session.preferences_loaded) - preferences_save (); + if (!session.preferences_loaded) { + preferences_save (); // Save defaults + session.preferences_loaded = true; + } #endif #ifdef HAVE_GUI From 7fa8587dd20f0cb4e8ccad558cff3cc4f875b22e Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sun, 12 Apr 2020 20:24:56 +1000 Subject: [PATCH 02/14] Plot Windows Move Realtime --- client/preferences.c | 47 ++++++++------------------------------------ client/preferences.h | 3 --- client/proxgui.cpp | 3 +++ client/proxgui.h | 1 + client/proxguiqt.cpp | 12 ++++++++++- client/proxguiqt.h | 3 ++- client/proxmark3.c | 2 +- 7 files changed, 26 insertions(+), 45 deletions(-) diff --git a/client/preferences.c b/client/preferences.c index b57170295..ebe72df94 100644 --- a/client/preferences.c +++ b/client/preferences.c @@ -1,40 +1,7 @@ -/***************************************************************************** - * WARNING - * - * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY. - * - * USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL - * PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL, - * AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES. - * - * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS. - * - ***************************************************************************** - * - * This file is part of loclass. It is a reconstructon of the cipher engine - * used in iClass, and RFID techology. - * - * The implementation is based on the work performed by - * Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and - * Milosch Meriac in the paper "Dismantling IClass". - * - * Copyright (C) 2014 Martin Holst Swende - * - * This is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as published - * by the Free Software Foundation, or, at your option, any later version. - * - * This file 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 loclass. If not, see . - * - * - ****************************************************************************/ - +//----------------------------------------------------------------------------- +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. //----------------------------------------------------------------------------- // Preferences Functions //----------------------------------------------------------------------------- @@ -56,7 +23,8 @@ #include #include "cmdparser.h" #include - +//#include "proxgui.h" +extern void SetWindowsPosition (void); static int CmdHelp(const char *Cmd); // Load all settings into memory (struct) @@ -477,7 +445,7 @@ static int CmdPrefSet (const char *Cmd) // Need to work out how to change live.... // calling data plot seems to work //plotwidget->MoveWindows (); - + SetWindowsPosition(); showPlotPosState (); break; case prefOVERLAY: @@ -500,6 +468,7 @@ static int CmdPrefSet (const char *Cmd) if (y != -99999) session.window_overlay_ypos = y; if (h != -99999) session.window_overlay_hsize = h; if (w != -99999) session.window_overlay_wsize = w; + SetWindowsPosition(); showOverlayPosState (); // Need to work out how to change live.... break; diff --git a/client/preferences.h b/client/preferences.h index 7dce07fc6..edc426086 100644 --- a/client/preferences.h +++ b/client/preferences.h @@ -1,7 +1,4 @@ //----------------------------------------------------------------------------- -// Copyright (C) 2009 Michael Gernoth -// Copyright (C) 2010 iZsh -// // This code is licensed to you under the terms of the GNU GPL, version 2 or, // at your option, any later version. See the LICENSE.txt file for the text of // the license. diff --git a/client/proxgui.cpp b/client/proxgui.cpp index d5f82ffe9..3a7ac9ff1 100644 --- a/client/proxgui.cpp +++ b/client/proxgui.cpp @@ -54,6 +54,9 @@ extern "C" void MainGraphics(void) { gui->MainLoop(); } +extern "C" void SetWindowsPosition (void) { + gui->SetWindowsPosition (); +} extern "C" void InitGraphics(int argc, char **argv, char *script_cmds_file, char *script_cmd, bool stayInCommandLoop) { #ifdef Q_WS_X11 diff --git a/client/proxgui.h b/client/proxgui.h index c9cd0096d..635adffe6 100644 --- a/client/proxgui.h +++ b/client/proxgui.h @@ -24,6 +24,7 @@ void ShowGraphWindow(void); void HideGraphWindow(void); void RepaintGraphWindow(void); void MainGraphics(void); +void SetWindowsPosition (void); void InitGraphics(int argc, char **argv, char *script_cmds_file, char *script_cmd, bool stayInCommandLoop); void ExitGraphics(void); #ifndef MAX_GRAPH_TRACE_LEN diff --git a/client/proxguiqt.cpp b/client/proxguiqt.cpp index cbe4ade8d..c65248719 100644 --- a/client/proxguiqt.cpp +++ b/client/proxguiqt.cpp @@ -128,7 +128,10 @@ ProxGuiQT::~ProxGuiQT(void) { plotapp = NULL; } } - +void ProxGuiQT::SetWindowsPosition (void) +{ + plotwidget->SetWindowsPosition (); +} //-------------------- void ProxWidget::applyOperation() { //printf("ApplyOperation()"); @@ -258,6 +261,13 @@ void ProxWidget::showEvent(QShowEvent *event) { controlWidget->show(); plot->show(); } +void ProxWidget::SetWindowsPosition(void) { + printf ("Settings windows Pos\n"); + if (session.preferences_loaded) { + setGeometry (session.window_plot_xpos,session.window_plot_ypos,session.window_plot_wsize,session.window_plot_hsize); + controlWidget->setGeometry (session.window_overlay_xpos,session.window_overlay_ypos,session.window_overlay_wsize,session.window_overlay_hsize); + } +} //----------- Plotting diff --git a/client/proxguiqt.h b/client/proxguiqt.h index 80d9e1ef4..f5e9d2736 100644 --- a/client/proxguiqt.h +++ b/client/proxguiqt.h @@ -88,6 +88,7 @@ class ProxWidget : public QWidget { void vchange_askedge(int v); void vchange_dthr_up(int v); void vchange_dthr_down(int v); + void SetWindowsPosition(void); }; class WorkerThread : public QThread { @@ -121,7 +122,7 @@ class ProxGuiQT : public QObject { void HideGraphWindow(void); void MainLoop(void); void Exit(void); - + void SetWindowsPosition (void); private slots: void _ShowGraphWindow(void); void _RepaintGraphWindow(void); diff --git a/client/proxmark3.c b/client/proxmark3.c index d152fa93a..910d49327 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -914,7 +914,7 @@ int main(int argc, char *argv[]) { showBanner(); #ifdef USE_PREFERENCE_FILE - // Save settings if not load from settings json file. + // Save settings if not loaded from settings json file. // Doing this here will ensure other checks and updates are saved to over rule default // e.g. Linux color use check if (!session.preferences_loaded) { From 87e58442fbcc8d457b46cac96d7e36a92adfdb5c Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sun, 12 Apr 2020 20:36:05 +1000 Subject: [PATCH 03/14] Update proxguiqt.cpp --- client/proxguiqt.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/client/proxguiqt.cpp b/client/proxguiqt.cpp index c65248719..2b39956cf 100644 --- a/client/proxguiqt.cpp +++ b/client/proxguiqt.cpp @@ -130,6 +130,8 @@ ProxGuiQT::~ProxGuiQT(void) { } void ProxGuiQT::SetWindowsPosition (void) { + if (!plotapp || !plotwidget) + return; plotwidget->SetWindowsPosition (); } //-------------------- @@ -262,11 +264,14 @@ void ProxWidget::showEvent(QShowEvent *event) { plot->show(); } void ProxWidget::SetWindowsPosition(void) { - printf ("Settings windows Pos\n"); + +// plotwidget->update(); if (session.preferences_loaded) { setGeometry (session.window_plot_xpos,session.window_plot_ypos,session.window_plot_wsize,session.window_plot_hsize); controlWidget->setGeometry (session.window_overlay_xpos,session.window_overlay_ypos,session.window_overlay_wsize,session.window_overlay_hsize); - } + update(); + controlWidget->update(); + } } //----------- Plotting From ce38dd92ba6d83f9c319f05dc934b8b29810e98c Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sun, 12 Apr 2020 20:42:10 +1000 Subject: [PATCH 04/14] Update proxguiqt.cpp --- client/proxguiqt.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/proxguiqt.cpp b/client/proxguiqt.cpp index 2b39956cf..abc201a60 100644 --- a/client/proxguiqt.cpp +++ b/client/proxguiqt.cpp @@ -269,8 +269,8 @@ void ProxWidget::SetWindowsPosition(void) { if (session.preferences_loaded) { setGeometry (session.window_plot_xpos,session.window_plot_ypos,session.window_plot_wsize,session.window_plot_hsize); controlWidget->setGeometry (session.window_overlay_xpos,session.window_overlay_ypos,session.window_overlay_wsize,session.window_overlay_hsize); - update(); - controlWidget->update(); + // update(); + // controlWidget->update(); } } From 4177f51770c7d22344ef642cc892a0d6f9b0e73e Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Sun, 12 Apr 2020 20:59:18 +1000 Subject: [PATCH 05/14] Update proxguiqt.cpp --- client/proxguiqt.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/client/proxguiqt.cpp b/client/proxguiqt.cpp index abc201a60..b9860058f 100644 --- a/client/proxguiqt.cpp +++ b/client/proxguiqt.cpp @@ -130,8 +130,6 @@ ProxGuiQT::~ProxGuiQT(void) { } void ProxGuiQT::SetWindowsPosition (void) { - if (!plotapp || !plotwidget) - return; plotwidget->SetWindowsPosition (); } //-------------------- @@ -269,8 +267,8 @@ void ProxWidget::SetWindowsPosition(void) { if (session.preferences_loaded) { setGeometry (session.window_plot_xpos,session.window_plot_ypos,session.window_plot_wsize,session.window_plot_hsize); controlWidget->setGeometry (session.window_overlay_xpos,session.window_overlay_ypos,session.window_overlay_wsize,session.window_overlay_hsize); - // update(); - // controlWidget->update(); + update(); + controlWidget->update(); } } From 69eef755d3a518d55819d0bc8f2788e30f754185 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 13 Apr 2020 09:32:47 +1000 Subject: [PATCH 06/14] Window Size/Pos Tracking --- client/preferences.c | 6 ++--- client/proxgui.cpp | 3 --- client/proxgui.h | 1 - client/proxguiqt.cpp | 53 ++++++++++++++++++++++++++++++-------------- client/proxguiqt.h | 17 +++++++++++--- 5 files changed, 53 insertions(+), 27 deletions(-) diff --git a/client/preferences.c b/client/preferences.c index ebe72df94..6c7fd5a6d 100644 --- a/client/preferences.c +++ b/client/preferences.c @@ -24,7 +24,7 @@ #include "cmdparser.h" #include //#include "proxgui.h" -extern void SetWindowsPosition (void); +//extern void SetWindowsPosition (void); static int CmdHelp(const char *Cmd); // Load all settings into memory (struct) @@ -445,7 +445,7 @@ static int CmdPrefSet (const char *Cmd) // Need to work out how to change live.... // calling data plot seems to work //plotwidget->MoveWindows (); - SetWindowsPosition(); + // SetWindowsPosition(); showPlotPosState (); break; case prefOVERLAY: @@ -468,7 +468,7 @@ static int CmdPrefSet (const char *Cmd) if (y != -99999) session.window_overlay_ypos = y; if (h != -99999) session.window_overlay_hsize = h; if (w != -99999) session.window_overlay_wsize = w; - SetWindowsPosition(); + // SetWindowsPosition(); showOverlayPosState (); // Need to work out how to change live.... break; diff --git a/client/proxgui.cpp b/client/proxgui.cpp index 3a7ac9ff1..d5f82ffe9 100644 --- a/client/proxgui.cpp +++ b/client/proxgui.cpp @@ -54,9 +54,6 @@ extern "C" void MainGraphics(void) { gui->MainLoop(); } -extern "C" void SetWindowsPosition (void) { - gui->SetWindowsPosition (); -} extern "C" void InitGraphics(int argc, char **argv, char *script_cmds_file, char *script_cmd, bool stayInCommandLoop) { #ifdef Q_WS_X11 diff --git a/client/proxgui.h b/client/proxgui.h index 635adffe6..c9cd0096d 100644 --- a/client/proxgui.h +++ b/client/proxgui.h @@ -24,7 +24,6 @@ void ShowGraphWindow(void); void HideGraphWindow(void); void RepaintGraphWindow(void); void MainGraphics(void); -void SetWindowsPosition (void); void InitGraphics(int argc, char **argv, char *script_cmds_file, char *script_cmd, bool stayInCommandLoop); void ExitGraphics(void); #ifndef MAX_GRAPH_TRACE_LEN diff --git a/client/proxguiqt.cpp b/client/proxguiqt.cpp index b9860058f..dfa9aa9fc 100644 --- a/client/proxguiqt.cpp +++ b/client/proxguiqt.cpp @@ -128,10 +128,30 @@ ProxGuiQT::~ProxGuiQT(void) { plotapp = NULL; } } -void ProxGuiQT::SetWindowsPosition (void) -{ - plotwidget->SetWindowsPosition (); + +// ------------------------------------------------- +// Slider Widget form based on a class to enable +// Event override functions +// ------------------------------------------------- + +SliderWidget::SliderWidget() { + // Set the initail postion and size from settings + if (session.preferences_loaded) + setGeometry (session.window_overlay_xpos,session.window_overlay_ypos,session.window_overlay_wsize,session.window_overlay_hsize); + else + resize(800, 400); } + +void SliderWidget::resizeEvent (QResizeEvent *event) { + session.window_overlay_hsize = event->size().height(); + session.window_overlay_wsize = event->size().width(); +} + +void SliderWidget::moveEvent (QMoveEvent *event) { + session.window_overlay_xpos = event->pos().x(); + session.window_overlay_ypos = event->pos().y(); +} + //-------------------- void ProxWidget::applyOperation() { //printf("ApplyOperation()"); @@ -179,7 +199,7 @@ ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) { resize(800, 400); // Setup the controller widget - controlWidget = new QWidget(); + controlWidget = new SliderWidget ();//new QWidget(); opsController = new Ui::Form(); opsController->setupUi(controlWidget); //Due to quirks in QT Designer, we need to fiddle a bit @@ -192,7 +212,6 @@ ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) { opsController->horizontalSlider_askedge->setValue(25); opsController->horizontalSlider_window->setValue(4000); - QObject::connect(opsController->pushButton_apply, SIGNAL(clicked()), this, SLOT(applyOperation())); QObject::connect(opsController->pushButton_sticky, SIGNAL(clicked()), this, SLOT(stickOperation())); QObject::connect(opsController->horizontalSlider_window, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int))); @@ -200,6 +219,8 @@ ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) { QObject::connect(opsController->horizontalSlider_dirthr_down, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int))); QObject::connect(opsController->horizontalSlider_askedge, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int))); + controlWidget->setGeometry (session.window_overlay_xpos,session.window_overlay_ypos,session.window_overlay_wsize,session.window_overlay_hsize); + // Set up the plot widget, which does the actual plotting plot = new Plot(this); QVBoxLayout *layout = new QVBoxLayout; @@ -213,9 +234,8 @@ ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) { // shows plot window on the screen. show(); - if (session.preferences_loaded) - controlWidget->setGeometry (session.window_overlay_xpos,session.window_overlay_ypos,session.window_overlay_wsize,session.window_overlay_hsize); - else { + // Set Slider/Overlay position if no settings. + if (!session.preferences_loaded){ // Move controller widget below plot controlWidget->move(x(), y() + frameSize().height()); controlWidget->resize(size().width(), 200); @@ -261,15 +281,14 @@ void ProxWidget::showEvent(QShowEvent *event) { controlWidget->show(); plot->show(); } -void ProxWidget::SetWindowsPosition(void) { - -// plotwidget->update(); - if (session.preferences_loaded) { - setGeometry (session.window_plot_xpos,session.window_plot_ypos,session.window_plot_wsize,session.window_plot_hsize); - controlWidget->setGeometry (session.window_overlay_xpos,session.window_overlay_ypos,session.window_overlay_wsize,session.window_overlay_hsize); - update(); - controlWidget->update(); - } +void ProxWidget::moveEvent(QMoveEvent *event) { + session.window_plot_xpos = event->pos().x(); + session.window_plot_ypos = event->pos().y(); + +} +void ProxWidget::resizeEvent(QResizeEvent *event) { + session.window_plot_hsize = event->size().height(); + session.window_plot_wsize = event->size().width(); } //----------- Plotting diff --git a/client/proxguiqt.h b/client/proxguiqt.h index f5e9d2736..4b4a21c11 100644 --- a/client/proxguiqt.h +++ b/client/proxguiqt.h @@ -56,9 +56,19 @@ class Plot: public QWidget { }; class ProxGuiQT; +// Added class for SliderWidget to allow move/resize event override +class SliderWidget : public QWidget { +protected: + void resizeEvent (QResizeEvent *event); + void moveEvent (QMoveEvent *event); +public: + SliderWidget(); +}; + /** * The window with plot and controls */ + class ProxWidget : public QWidget { Q_OBJECT; //needed for slot/signal classes @@ -66,8 +76,8 @@ class ProxWidget : public QWidget { ProxGuiQT *master; Plot *plot; Ui::Form *opsController; - QWidget *controlWidget; - +// QWidget *controlWidget; + SliderWidget *controlWidget; public: ProxWidget(QWidget *parent = 0, ProxGuiQT *master = NULL); ~ProxWidget(void); @@ -78,6 +88,8 @@ class ProxWidget : public QWidget { void closeEvent(QCloseEvent *event); void showEvent(QShowEvent *event); void hideEvent(QHideEvent *event); + void moveEvent(QMoveEvent *event); + void resizeEvent(QResizeEvent *event); // void mouseMoveEvent(QMouseEvent *event); // void mousePressEvent(QMouseEvent *event) { mouseMoveEvent(event); } // void keyPressEvent(QKeyEvent *event); @@ -88,7 +100,6 @@ class ProxWidget : public QWidget { void vchange_askedge(int v); void vchange_dthr_up(int v); void vchange_dthr_down(int v); - void SetWindowsPosition(void); }; class WorkerThread : public QThread { From 0b360d4dc0b7a79f26d0dd1551257f9fa3d7e4f8 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 13 Apr 2020 12:12:47 +1000 Subject: [PATCH 07/14] Preference commands rework --- client/preferences.c | 519 +++++++++++++++++++++---------------------- client/preferences.h | 1 + 2 files changed, 252 insertions(+), 268 deletions(-) diff --git a/client/preferences.c b/client/preferences.c index 6c7fd5a6d..f88145ff5 100644 --- a/client/preferences.c +++ b/client/preferences.c @@ -26,8 +26,23 @@ //#include "proxgui.h" //extern void SetWindowsPosition (void); static int CmdHelp(const char *Cmd); +static int setCmdHelp(const char *Cmd); // Load all settings into memory (struct) +static char* prefGetFilename (void) { + static char Buffer[500]; + + getcwd(Buffer, sizeof(Buffer)); +#ifdef _WIN32 + strncat (Buffer,"\\",sizeof(Buffer)-strlen(Buffer)); +#else + strncat (Buffer,"/",sizeof(Buffer)-strlen(Buffer)); +#endif + strncat (Buffer,preferencesFilename,sizeof(Buffer)-strlen(Buffer)); + + return Buffer; +} + int preferences_load (void) { // Set all defaults @@ -49,7 +64,7 @@ int preferences_load (void) { uint8_t dummyData = 0x00; size_t dummyDL = 0x00; - if (loadFileJSON(preferencesFilename, &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { + if (loadFileJSON(prefGetFilename(), &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { session.preferences_loaded = true; } // Note, if session.settings_loaded == false then the settings_save @@ -63,7 +78,7 @@ int preferences_save (void) { // Note sure if backup has value ? char backupFilename[500]; - snprintf (backupFilename,sizeof(backupFilename),"%s.bak",preferencesFilename); + snprintf (backupFilename,sizeof(backupFilename),"%s.bak",prefGetFilename()); if (fileExists (backupFilename)) { if (remove (backupFilename) != 0) { @@ -72,9 +87,9 @@ int preferences_save (void) { } } - if (fileExists (preferencesFilename)) { - if (rename (preferencesFilename,backupFilename) != 0) { - PrintAndLogEx (FAILED, "Error - could not backup settings file \"%s\" to \"%s\"",preferencesFilename,backupFilename); + if (fileExists (prefGetFilename())) { + if (rename (prefGetFilename(),backupFilename) != 0) { + PrintAndLogEx (FAILED, "Error - could not backup settings file \"%s\" to \"%s\"",prefGetFilename(),backupFilename); return PM3_ESOFT; } } @@ -82,8 +97,8 @@ int preferences_save (void) { uint8_t dummyData = 0x00; size_t dummyDL = 0x00; - if (saveFileJSON(preferencesFilename, jsfSettings, &dummyData, dummyDL) != PM3_SUCCESS) - PrintAndLogEx (ERR, "Error saving preferences to \"%s\"",preferencesFilename); + if (saveFileJSON(prefGetFilename(), jsfSettings, &dummyData, dummyDL) != PM3_SUCCESS) + PrintAndLogEx (ERR, "Error saving preferences to \"%s\"",prefGetFilename()); return PM3_SUCCESS; } @@ -183,106 +198,99 @@ void preferences_load_callback (json_t *root) { } // Help Functions -static int usage_pref_set() { - PrintAndLogEx(NORMAL, "Usage: pref set [(h)elp] [(e)moji ...] [(c)olor ...] [(hi)nts ...] [debug ...]"); - PrintAndLogEx(NORMAL, " [(p)lot ...] [(o)verlay ...]"); + +static int usage_set_emoji() { + PrintAndLogEx(NORMAL, "Usage: pref set emoji "); PrintAndLogEx(NORMAL, "Options:"); - PrintAndLogEx(NORMAL, " help - This help"); - PrintAndLogEx(NORMAL, " emoji <(ali)as | (em)oji | (alt)text | (er)ase> - Set the level of emoji support"); - PrintAndLogEx(NORMAL, " alias : show alias"); - PrintAndLogEx(NORMAL, " emoji : show emoji"); - PrintAndLogEx(NORMAL, " alttext : show alternative text"); - PrintAndLogEx(NORMAL, " erase : dont show any emoji"); - - PrintAndLogEx(NORMAL, " color <(o)ff|(a)nsi> - Color support level"); - PrintAndLogEx(NORMAL, " off : dont use color"); - PrintAndLogEx(NORMAL, " ansi : use ansi color (linux, mac, windows terminal)"); - - PrintAndLogEx(NORMAL, " hints <(of)f | on> - Show hints on/off"); - - PrintAndLogEx(NORMAL, " debug <(o)ff | (s)imple | (f)ull> - Client debug level"); - PrintAndLogEx(NORMAL, " off : no debug output"); - PrintAndLogEx(NORMAL, " simple : information level debug"); - PrintAndLogEx(NORMAL, " full : full debug information"); - - PrintAndLogEx(NORMAL, " plot [x ] [y ] [h ] [w ] - Position the plot window"); - PrintAndLogEx(NORMAL, " overlay [x ] [y ] [h ] [w ] - Position the overlay/slider window"); + PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); + PrintAndLogEx(NORMAL, " "_GREEN_("alias")" - Show alias for emoji"); + PrintAndLogEx(NORMAL, " "_GREEN_("emoji")" - Show amoji"); + PrintAndLogEx(NORMAL, " "_GREEN_("alttext")" - Show alt text for emoji"); + PrintAndLogEx(NORMAL, " "_GREEN_("erase")" - Dont show emoji or text"); return PM3_SUCCESS; } -static int usage_pref_show() { - PrintAndLogEx(NORMAL, "Usage: pref show [help] [emoji|color]"); +static int usage_set_color() { + PrintAndLogEx(NORMAL, "Usage: pref set color "); PrintAndLogEx(NORMAL, "Options:"); - PrintAndLogEx(NORMAL, " help - This help"); - PrintAndLogEx(NORMAL, " emoji - show current settings for emoji"); - PrintAndLogEx(NORMAL, " color - show current settings for color"); + PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); + PrintAndLogEx(NORMAL, " "_GREEN_("off")" - Dont use colors"); + PrintAndLogEx(NORMAL, " "_GREEN_("ansi")" - Use ANSI colors"); + + return PM3_SUCCESS; +} + +static int usage_set_debug() { + PrintAndLogEx(NORMAL, "Usage: pref set debug "); + PrintAndLogEx(NORMAL, "Options:"); + PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); + PrintAndLogEx(NORMAL, " "_GREEN_("off")" - no debug messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("simple")" - simple debug messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("full")" - full debug messages"); + + return PM3_SUCCESS; +} +static int usage_set_hints() { + PrintAndLogEx(NORMAL, "Usage: pref set hints "); + PrintAndLogEx(NORMAL, "Options:"); + PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); + PrintAndLogEx(NORMAL, " "_GREEN_("off")" - Dont display hints"); + PrintAndLogEx(NORMAL, " "_GREEN_("on")" - Display hints"); return PM3_SUCCESS; } // Preference Processing Functions typedef enum preferenceId {prefNONE,prefHELP,prefEMOJI,prefCOLOR,prefPLOT,prefOVERLAY,prefHINTS,prefCLIENTDEBUG} preferenceId_t; +typedef enum prefShowOpt {prefShowNone,prefShowOLD,prefShowNEW} prefShowOpt_t; -// Enumerate text to ID -preferenceId_t prefGetID (char* cmdOpt) +const char *prefShowMsg (prefShowOpt_t Opt) { - str_lower (cmdOpt); - - if (strncmp (cmdOpt,"hi",2) == 0) return prefHINTS; - if (strncmp (cmdOpt,"h",1) == 0) return prefHELP; - if (strncmp (cmdOpt,"e",1) == 0) return prefEMOJI; - if (strncmp (cmdOpt,"c",1) == 0) return prefCOLOR; - if (strncmp (cmdOpt,"p",1) == 0) return prefPLOT; - if (strncmp (cmdOpt,"o",1) == 0) return prefOVERLAY; - if (strncmp (cmdOpt,"d",1) == 0) return prefCLIENTDEBUG; - - return NONE; + switch (Opt) { + case prefShowOLD: return _YELLOW_("[old]"); //strncpy(Msg,"Before ",sizeof(Msg)-1); break; + case prefShowNEW: return _GREEN_("[new]"); // strncpy(Msg,"After ",sizeof(Msg)-1); break; + case prefShowNone: return ""; + } + + return ""; } -void showEmojiState (void) { +void showEmojiState (prefShowOpt_t Opt) { + switch (session.emoji_mode) { - case ALIAS: PrintAndLogEx(NORMAL, " emoji.................. "_GREEN_("show alias")); + case ALIAS: PrintAndLogEx(NORMAL, " %s emoji.................. "_GREEN_("alias"),prefShowMsg (Opt)); break; - case EMOJI: PrintAndLogEx(NORMAL, " emoji.................. "_GREEN_("show emoji")); + case EMOJI: PrintAndLogEx(NORMAL, " %s emoji.................. "_GREEN_("emoji"),prefShowMsg (Opt)); break; - case ALTTEXT: PrintAndLogEx(NORMAL, " emoji.................. "_GREEN_("show alt text")); + case ALTTEXT: PrintAndLogEx(NORMAL, " %s emoji.................. "_GREEN_("alttext"),prefShowMsg (Opt)); break; - case ERASE: PrintAndLogEx(NORMAL, " emoji.................. "_GREEN_("dont show emoji")); + case ERASE: PrintAndLogEx(NORMAL, " %s emoji.................. "_GREEN_("erase"),prefShowMsg (Opt)); break; default: - PrintAndLogEx(NORMAL, " emoji.................. "_RED_("unknown")); + PrintAndLogEx(NORMAL, " %s emoji.................. "_RED_("unknown"),prefShowMsg(Opt)); } } -void showColorState (void) { -/* - switch (session.supports_colors) { - case false: PrintAndLogEx(NORMAL, "Color : "_GREEN_("off")); - break; - case true: PrintAndLogEx(NORMAL, "Color : "_GREEN_("ansi")); - break; - default: - PrintAndLogEx(NORMAL, "Color support set to : "_RED_("unknown")); - } -*/ - // this will change to 1 of a set from bool +void showColorState (prefShowOpt_t Opt) { + if (session.supports_colors) - PrintAndLogEx(NORMAL, " color.................. "_GREEN_("ansi")); + PrintAndLogEx(NORMAL, " %s color.................. "_GREEN_("ansi"),prefShowMsg(Opt)); else - PrintAndLogEx(NORMAL, " color.................. "_GREEN_("off")); + PrintAndLogEx(NORMAL, " %s color.................. "_GREEN_("off"),prefShowMsg(Opt)); } -void showClientDebugState (void) { +void showClientDebugState (prefShowOpt_t Opt) { + switch (session.client_debug_level) { - case OFF: PrintAndLogEx (NORMAL," client debug........... "_GREEN_("off")); + case OFF: PrintAndLogEx (NORMAL," %s client debug........... "_GREEN_("off"),prefShowMsg(Opt)); break; - case SIMPLE: PrintAndLogEx (NORMAL," client debug........... "_GREEN_("simple")); + case SIMPLE: PrintAndLogEx (NORMAL," %s client debug........... "_GREEN_("simple"),prefShowMsg(Opt)); break; - case FULL: PrintAndLogEx (NORMAL," client debug........... "_GREEN_("full")); + case FULL: PrintAndLogEx (NORMAL," %s client debug........... "_GREEN_("full"),prefShowMsg(Opt)); break; default: - PrintAndLogEx(NORMAL, " client debug........... "_RED_("unknown")); + PrintAndLogEx(NORMAL, " %s client debug........... "_RED_("unknown"),prefShowMsg(Opt)); } } @@ -296,19 +304,170 @@ void showOverlayPosState (void){ session.window_overlay_xpos,session.window_overlay_ypos,session.window_overlay_hsize,session.window_overlay_wsize); } -void showHintsState (void){ +void showHintsState (prefShowOpt_t Opt){ if (session.show_hints) - PrintAndLogEx (NORMAL," Hints.................. "_GREEN_("on")); + PrintAndLogEx (NORMAL," %s Hints.................. "_GREEN_("on"),prefShowMsg(Opt)); else - PrintAndLogEx (NORMAL," Hints.................. "_GREEN_("off")); + PrintAndLogEx (NORMAL," %s Hints.................. "_GREEN_("off"),prefShowMsg(Opt)); +} + +static int setCmdEmoji (const char *Cmd) { + uint8_t cmdp = 0; + bool errors = false; + char strOpt[50]; + emojiMode_t newValue = session.emoji_mode; + + if (param_getchar(Cmd, cmdp) == 0x00) + return usage_set_emoji(); + + while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { + + if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { + str_lower(strOpt); // convert to lowercase + + if (strncmp (strOpt,"help",4) == 0) usage_set_emoji(); + if (strncmp (strOpt,"alias",5) == 0) newValue = ALIAS; + if (strncmp (strOpt,"emoji",5) == 0) newValue = EMOJI; + if (strncmp (strOpt,"alttext",7) == 0) newValue = ALTTEXT; + if (strncmp (strOpt,"erase",5) == 0) newValue = ERASE; + + if (session.emoji_mode != newValue) {// changed + showEmojiState (prefShowOLD); + session.emoji_mode = newValue; + showEmojiState (prefShowNEW); + } + } else { + // error + } + } + + return PM3_SUCCESS; +} + +static int setCmdColor (const char *Cmd) +{ + uint8_t cmdp = 0; + bool errors = false; + char strOpt[50]; + bool newValue = session.supports_colors; + + if (param_getchar(Cmd, cmdp) == 0x00) + return usage_set_color(); + + while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { + + if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { + str_lower(strOpt); // convert to lowercase + + if (strncmp (strOpt,"help",4) == 0) usage_set_color(); + if (strncmp (strOpt,"off",3) == 0) newValue = false; + if (strncmp (strOpt,"ansi",4) == 0) newValue = true; + + if (session.supports_colors != newValue) {// changed + showColorState (prefShowOLD); + session.supports_colors = newValue; + showColorState (prefShowNEW); + } + } else { + // error + } + } + + return PM3_SUCCESS; +} + +static int setCmdDebug (const char *Cmd) +{ + uint8_t cmdp = 0; + bool errors = false; + char strOpt[50]; + clientdebugLevel_t newValue = session.client_debug_level; + + if (param_getchar(Cmd, cmdp) == 0x00) + return usage_set_debug(); + + while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { + + if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { + str_lower(strOpt); // convert to lowercase + + if (strncmp (strOpt,"help",4) == 0) usage_set_debug(); + if (strncmp (strOpt,"off",3) == 0) newValue = OFF; + if (strncmp (strOpt,"simple",6) == 0) newValue = SIMPLE; + if (strncmp (strOpt,"full",4) == 0) newValue = FULL; + + if (session.client_debug_level != newValue) {// changed + showClientDebugState (prefShowOLD); + session.client_debug_level = newValue; + g_debugMode = newValue; + showClientDebugState (prefShowNEW); + } + } else { + // error + } + } + + return PM3_SUCCESS; +} + +static int setCmdHint (const char *Cmd) +{ + uint8_t cmdp = 0; + bool errors = false; + char strOpt[50]; + bool newValue = session.show_hints; + + if (param_getchar(Cmd, cmdp) == 0x00) + return usage_set_hints(); + + while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { + + if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { + str_lower(strOpt); // convert to lowercase + + if (strncmp (strOpt,"help",4) == 0) usage_set_hints(); + if (strncmp (strOpt,"off",3) == 0) newValue = false; + if (strncmp (strOpt,"on",2) == 0) newValue = true; + + if (session.show_hints != newValue) {// changed + showHintsState (prefShowOLD); + session.show_hints = newValue; + showHintsState (prefShowNEW); + } + } else { + // error + } + } + + return PM3_SUCCESS; +} + +static command_t setCommandTable[] = { + {"help", setCmdHelp, AlwaysAvailable, "This help"}, + {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, + {"color", setCmdColor, AlwaysAvailable, "Set color support"}, + {"debug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, + {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, + {NULL, NULL, NULL, NULL} +}; + + +static int setCmdHelp(const char *Cmd) { + (void)Cmd; // Cmd is not used so far + CmdsHelp(setCommandTable); + + return PM3_SUCCESS; +} + +int CmdPrefSet (const char *Cmd) +{ + clearCommandBuffer(); + + return CmdsParse(setCommandTable, Cmd); } static int CmdPrefShow (const char *Cmd) { - uint8_t cmdp = 0; - preferenceId_t CmdPref; - bool errors = false; - char strOpt[50]; - + PrintAndLogEx(NORMAL,""); PrintAndLogEx(NORMAL,_BLUE_("Preferences")); @@ -317,205 +476,29 @@ static int CmdPrefShow (const char *Cmd) { return PM3_ESOFT; } - if (param_getchar(Cmd, cmdp) == 0x00) { // No options - Show all - showEmojiState (); - showColorState (); - showPlotPosState (); - showOverlayPosState (); - showClientDebugState(); - showHintsState (); - } - else { - - while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { + showEmojiState (prefShowNone); + showColorState (prefShowNone); + showPlotPosState (); + showOverlayPosState (); + showClientDebugState(prefShowNone); + showHintsState (prefShowNone); - if (param_getstr(Cmd, cmdp, strOpt, sizeof(strOpt)) != 0) { - CmdPref = prefGetID(strOpt); - } - else - CmdPref = prefNONE; - - switch (CmdPref) { - case prefHELP: - return usage_pref_show(); - case prefEMOJI: - showEmojiState (); - break; - case prefCOLOR: // color - showColorState (); - break; - case prefPLOT: - showPlotPosState (); - break; - case prefOVERLAY: - showOverlayPosState (); - break; - case prefCLIENTDEBUG: - showClientDebugState(); - break; - case prefHINTS: - showHintsState(); - break; - case prefNONE: - PrintAndLogEx (ERR,"Invalid option supplied"); - errors = true; - break; - // errors - } - cmdp ++; - } - } PrintAndLogEx(NORMAL,""); + return PM3_SUCCESS; } -static int CmdPrefSet (const char *Cmd) -{ - uint8_t cmdp = 0; - preferenceId_t CmdPref; - bool errors = false; - // char charOpt; - char strOpt[50]; - int x,y,h,w; - - if (param_getchar(Cmd, cmdp) == 0x00) - return usage_pref_set(); - - while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { - - if (param_getstr(Cmd, cmdp, strOpt, sizeof(strOpt)) != 0) { - CmdPref = prefGetID(strOpt); - } - else - CmdPref = prefNONE; - - switch (CmdPref) { - case prefHELP: - return usage_pref_set(); - case prefEMOJI: - showEmojiState (); - cmdp++; - if (param_getstr(Cmd, cmdp, strOpt, sizeof(strOpt)) != 0) { - str_lower(strOpt); - if (strncmp (strOpt,"ali",3) == 0) { session.emoji_mode = ALIAS; showEmojiState (); break; } - if (strncmp (strOpt,"em",2) == 0) { session.emoji_mode = EMOJI; showEmojiState (); break; } - if (strncmp (strOpt,"alt",3) == 0) { session.emoji_mode = ALTTEXT; showEmojiState (); break; } - if (strncmp (strOpt,"er",2) == 0) { session.emoji_mode = ERASE; showEmojiState (); break; } - // if we get this far, then an error in the mode - PrintAndLogEx(ERR,"Invalid emoji option"); - errors = true; - } - else - errors = true; - break; - case prefCOLOR: // color - showColorState (); - cmdp++; - if (param_getstr(Cmd, cmdp, strOpt, sizeof(strOpt)) != 0) { - str_lower(strOpt); - if (strncmp(strOpt,"a",1) == 0) { session.supports_colors = true; showColorState (); break; } - if (strncmp(strOpt,"o",1) == 0) { session.supports_colors = false; showColorState (); break; } - // if we get this far, then an error in the mode - PrintAndLogEx(ERR,"Invalid color option"); - errors = true; - } - else - errors = true; - break; - case prefPLOT: - showPlotPosState (); - cmdp++; - x = y = h = w = -99999; // Some invalid value - for (int i = 0; i < 4; i++) { // upto 4 values X, Y, H, WARNING - if (param_getchar(Cmd, cmdp) != 0){ - switch (tolower(param_getchar(Cmd, cmdp++))) { - case 'x': x = param_get32ex(Cmd,cmdp++,-99999,10); break; - case 'y': y = param_get32ex(Cmd,cmdp++,-99999,10); break; - case 'h': h = param_get32ex(Cmd,cmdp++,-99999,10); break; - case 'w': w = param_get32ex(Cmd,cmdp++,-99999,10); break; - default: - errors = true; - } - } - } - if (x != -99999) session.window_plot_xpos = x; - if (y != -99999) session.window_plot_ypos = y; - if (h != -99999) session.window_plot_hsize = h; - if (w != -99999) session.window_plot_wsize = w; - // Need to work out how to change live.... - // calling data plot seems to work - //plotwidget->MoveWindows (); - // SetWindowsPosition(); - showPlotPosState (); - break; - case prefOVERLAY: - showOverlayPosState (); - cmdp++; - x = y = h = w = -99999; // Some invalid value - for (int i = 0; i < 4; i++) { // upto 4 values X, Y, H, WARNING - if (param_getchar(Cmd, cmdp) != 0){ - switch (tolower(param_getchar(Cmd, cmdp++))) { - case 'x': x = param_get32ex(Cmd,cmdp++,-99999,10); break; - case 'y': y = param_get32ex(Cmd,cmdp++,-99999,10); break; - case 'h': h = param_get32ex(Cmd,cmdp++,-99999,10); break; - case 'w': w = param_get32ex(Cmd,cmdp++,-99999,10); break; - default: - errors = true; - } - } - } - if (x != -99999) session.window_overlay_xpos = x; - if (y != -99999) session.window_overlay_ypos = y; - if (h != -99999) session.window_overlay_hsize = h; - if (w != -99999) session.window_overlay_wsize = w; - // SetWindowsPosition(); - showOverlayPosState (); - // Need to work out how to change live.... - break; - case prefCLIENTDEBUG: - showClientDebugState(); - cmdp++; - if (param_getstr(Cmd, cmdp, strOpt, sizeof(strOpt)) != 0) { - str_lower(strOpt); - if (strncmp(strOpt,"o",1) == 0) { session.client_debug_level = OFF; g_debugMode = OFF; showClientDebugState(); break; } - if (strncmp(strOpt,"s",1) == 0) { session.client_debug_level = SIMPLE; g_debugMode = SIMPLE; showClientDebugState(); break; } - if (strncmp(strOpt,"f",1) == 0) { session.client_debug_level = FULL; g_debugMode = FULL; showClientDebugState(); break; } - // if we get this far, then an error in the mode - PrintAndLogEx(ERR,"Invalid client debug option"); - errors = true; - } - else - errors = true; - break; - case prefHINTS: - showHintsState (); - cmdp++; - if (param_getstr(Cmd, cmdp, strOpt, sizeof(strOpt)) != 0) { - str_lower(strOpt); - if (strncmp(strOpt,"on",2) == 0) { session.show_hints = true; showHintsState (); break; } - if (strncmp(strOpt,"of",2) == 0) { session.show_hints = false; showHintsState (); break; } - // if we get this far, then an error in the mode - PrintAndLogEx(ERR,"Invalid hint option"); - errors = true; - } - else - errors = true; - break; - case prefNONE: - PrintAndLogEx (ERR,"Invalid option supplied"); - errors = true; - break; - } - cmdp ++; - } +static int CmdPrefSave (const char *Cmd) { preferences_save(); + return PM3_SUCCESS; } static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, {"set", CmdPrefSet, AlwaysAvailable, "Set a preference"}, - {"show", CmdPrefShow, AlwaysAvailable, "Show (a preference)"}, + {"show", CmdPrefShow, AlwaysAvailable, "Show preferences"}, + {"save", CmdPrefSave, AlwaysAvailable, "Save preferences now"}, {NULL, NULL, NULL, NULL} }; diff --git a/client/preferences.h b/client/preferences.h index edc426086..527ca5ffb 100644 --- a/client/preferences.h +++ b/client/preferences.h @@ -10,6 +10,7 @@ #include "fileutils.h" +// Current working directory will be prepended. #define preferencesFilename "preferences.json" int CmdPreferences (const char *Cmd); From b72123a093a31c71f8ede0324171f3356aa4442f Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 13 Apr 2020 12:44:34 +1000 Subject: [PATCH 08/14] Update preferences.c --- client/preferences.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/client/preferences.c b/client/preferences.c index f88145ff5..94b97666a 100644 --- a/client/preferences.c +++ b/client/preferences.c @@ -23,6 +23,8 @@ #include #include "cmdparser.h" #include +#include + //#include "proxgui.h" //extern void SetWindowsPosition (void); static int CmdHelp(const char *Cmd); @@ -30,16 +32,16 @@ static int setCmdHelp(const char *Cmd); // Load all settings into memory (struct) static char* prefGetFilename (void) { - static char Buffer[500]; - - getcwd(Buffer, sizeof(Buffer)); -#ifdef _WIN32 - strncat (Buffer,"\\",sizeof(Buffer)-strlen(Buffer)); -#else - strncat (Buffer,"/",sizeof(Buffer)-strlen(Buffer)); -#endif - strncat (Buffer,preferencesFilename,sizeof(Buffer)-strlen(Buffer)); + static char Buffer[500+sizeof(preferencesFilename)+2] = {0}; + char PATH[500] = {0}; + getcwd(PATH, sizeof(PATH)); +#ifdef _WIN32 + snprintf (Buffer,sizeof(Buffer)-1,"%s\\%s",PATH,preferencesFilename); +#else + snprintf (Buffer,sizeof(Buffer)-1,"%s/%s",PATH,preferencesFilename); +#endif + return Buffer; } @@ -76,9 +78,9 @@ int preferences_load (void) { // Save all settings from memory (struct) to file int preferences_save (void) { // Note sure if backup has value ? - char backupFilename[500]; + char backupFilename[500+sizeof(preferencesFilename)+10] = {0}; - snprintf (backupFilename,sizeof(backupFilename),"%s.bak",prefGetFilename()); + snprintf (backupFilename,sizeof(backupFilename)-1,"%s.bak",prefGetFilename()); if (fileExists (backupFilename)) { if (remove (backupFilename) != 0) { From c924e2d5898fc0803a0dac022778eec80674794e Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 13 Apr 2020 13:51:03 +1000 Subject: [PATCH 09/14] save on exit --- client/preferences.c | 1 + client/proxmark3.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/client/preferences.c b/client/preferences.c index 94b97666a..1f409f745 100644 --- a/client/preferences.c +++ b/client/preferences.c @@ -80,6 +80,7 @@ int preferences_save (void) { // Note sure if backup has value ? char backupFilename[500+sizeof(preferencesFilename)+10] = {0}; + PrintAndLogEx(INFO,"Saving preferences ..."); snprintf (backupFilename,sizeof(backupFilename)-1,"%s.bak",prefGetFilename()); if (fileExists (backupFilename)) { diff --git a/client/proxmark3.c b/client/proxmark3.c index 910d49327..b981d1758 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -948,5 +948,8 @@ int main(int argc, char *argv[]) { CloseProxmark(); } +#ifdef USE_PREFERENCE_FILE + preferences_save (); +#endif exit(EXIT_SUCCESS); } From 82bbea3874e49750dda70407f2e0dac3ef865d3c Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 13 Apr 2020 14:12:54 +1000 Subject: [PATCH 10/14] Update preferences.c --- client/preferences.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/preferences.c b/client/preferences.c index 1f409f745..52e39cf41 100644 --- a/client/preferences.c +++ b/client/preferences.c @@ -32,8 +32,8 @@ static int setCmdHelp(const char *Cmd); // Load all settings into memory (struct) static char* prefGetFilename (void) { - static char Buffer[500+sizeof(preferencesFilename)+2] = {0}; - char PATH[500] = {0}; + static char Buffer[FILENAME_MAX+sizeof(preferencesFilename)+2] = {0}; + char PATH[FILENAME_MAX] = {0}; getcwd(PATH, sizeof(PATH)); #ifdef _WIN32 @@ -78,7 +78,7 @@ int preferences_load (void) { // Save all settings from memory (struct) to file int preferences_save (void) { // Note sure if backup has value ? - char backupFilename[500+sizeof(preferencesFilename)+10] = {0}; + char backupFilename[FILENAME_MAX+sizeof(preferencesFilename)+10] = {0}; PrintAndLogEx(INFO,"Saving preferences ..."); snprintf (backupFilename,sizeof(backupFilename)-1,"%s.bak",prefGetFilename()); From 0cc2bda952434cb1353b968fb2e542560503db75 Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 13 Apr 2020 14:39:16 +1000 Subject: [PATCH 11/14] Update preferences.c Error msg and show help if invalid option detected --- client/preferences.c | 157 ++++++++++++++++++++++++++++++------------- 1 file changed, 111 insertions(+), 46 deletions(-) diff --git a/client/preferences.c b/client/preferences.c index 52e39cf41..616fb17c0 100644 --- a/client/preferences.c +++ b/client/preferences.c @@ -317,6 +317,7 @@ void showHintsState (prefShowOpt_t Opt){ static int setCmdEmoji (const char *Cmd) { uint8_t cmdp = 0; bool errors = false; + bool validValue = false; char strOpt[50]; emojiMode_t newValue = session.emoji_mode; @@ -328,22 +329,41 @@ static int setCmdEmoji (const char *Cmd) { if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { str_lower(strOpt); // convert to lowercase - if (strncmp (strOpt,"help",4) == 0) usage_set_emoji(); - if (strncmp (strOpt,"alias",5) == 0) newValue = ALIAS; - if (strncmp (strOpt,"emoji",5) == 0) newValue = EMOJI; - if (strncmp (strOpt,"alttext",7) == 0) newValue = ALTTEXT; - if (strncmp (strOpt,"erase",5) == 0) newValue = ERASE; - - if (session.emoji_mode != newValue) {// changed - showEmojiState (prefShowOLD); - session.emoji_mode = newValue; - showEmojiState (prefShowNEW); + if (strncmp (strOpt,"help",4) == 0) + return usage_set_emoji(); + if (strncmp (strOpt,"alias",5) == 0) { + validValue = true; + newValue = ALIAS; } - } else { - // error - } + if (strncmp (strOpt,"emoji",5) == 0) { + validValue = true; + newValue = EMOJI; + } + if (strncmp (strOpt,"alttext",7) == 0) { + validValue = true; + newValue = ALTTEXT; + } + if (strncmp (strOpt,"erase",5) == 0) { + validValue = true; + newValue = ERASE; + } + + if (validValue) { + if (session.emoji_mode != newValue) {// changed + showEmojiState (prefShowOLD); + session.emoji_mode = newValue; + showEmojiState (prefShowNEW); + } else { + PrintAndLogEx(INFO,"nothing changed"); + showEmojiState (prefShowNone); + } + } else { + PrintAndLogEx(ERR,"invalid option"); + return usage_set_emoji(); + } + } } - + return PM3_SUCCESS; } @@ -351,6 +371,7 @@ static int setCmdColor (const char *Cmd) { uint8_t cmdp = 0; bool errors = false; + bool validValue = false; char strOpt[50]; bool newValue = session.supports_colors; @@ -362,17 +383,30 @@ static int setCmdColor (const char *Cmd) if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { str_lower(strOpt); // convert to lowercase - if (strncmp (strOpt,"help",4) == 0) usage_set_color(); - if (strncmp (strOpt,"off",3) == 0) newValue = false; - if (strncmp (strOpt,"ansi",4) == 0) newValue = true; - - if (session.supports_colors != newValue) {// changed - showColorState (prefShowOLD); - session.supports_colors = newValue; - showColorState (prefShowNEW); + if (strncmp (strOpt,"help",4) == 0) + return usage_set_color(); + if (strncmp (strOpt,"off",3) == 0) { + validValue = true; + newValue = false; + } + if (strncmp (strOpt,"ansi",4) == 0) { + validValue = true; + newValue = true; + } + + if (validValue) { + if (session.supports_colors != newValue) {// changed + showColorState (prefShowOLD); + session.supports_colors = newValue; + showColorState (prefShowNEW); + } else { + PrintAndLogEx(INFO,"nothing changed"); + showColorState (prefShowNone); + } + } else { + PrintAndLogEx(ERR,"invalid option"); + return usage_set_color(); } - } else { - // error } } @@ -383,6 +417,7 @@ static int setCmdDebug (const char *Cmd) { uint8_t cmdp = 0; bool errors = false; + bool validValue = false; char strOpt[50]; clientdebugLevel_t newValue = session.client_debug_level; @@ -394,19 +429,35 @@ static int setCmdDebug (const char *Cmd) if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { str_lower(strOpt); // convert to lowercase - if (strncmp (strOpt,"help",4) == 0) usage_set_debug(); - if (strncmp (strOpt,"off",3) == 0) newValue = OFF; - if (strncmp (strOpt,"simple",6) == 0) newValue = SIMPLE; - if (strncmp (strOpt,"full",4) == 0) newValue = FULL; - - if (session.client_debug_level != newValue) {// changed - showClientDebugState (prefShowOLD); - session.client_debug_level = newValue; - g_debugMode = newValue; - showClientDebugState (prefShowNEW); + if (strncmp (strOpt,"help",4) == 0) + return usage_set_debug(); + if (strncmp (strOpt,"off",3) == 0) { + validValue = true; + newValue = OFF; + } + if (strncmp (strOpt,"simple",6) == 0) { + validValue = true; + newValue = SIMPLE; + } + if (strncmp (strOpt,"full",4) == 0) { + validValue = true; + newValue = FULL; + } + + if (validValue) { + if (session.client_debug_level != newValue) {// changed + showClientDebugState (prefShowOLD); + session.client_debug_level = newValue; + g_debugMode = newValue; + showClientDebugState (prefShowNEW); + } else { + PrintAndLogEx(INFO,"nothing changed"); + showClientDebugState (prefShowNone); + } + } else { + PrintAndLogEx(ERR,"invalid option"); + return usage_set_debug(); } - } else { - // error } } @@ -417,6 +468,7 @@ static int setCmdHint (const char *Cmd) { uint8_t cmdp = 0; bool errors = false; + bool validValue = false; char strOpt[50]; bool newValue = session.show_hints; @@ -428,17 +480,30 @@ static int setCmdHint (const char *Cmd) if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { str_lower(strOpt); // convert to lowercase - if (strncmp (strOpt,"help",4) == 0) usage_set_hints(); - if (strncmp (strOpt,"off",3) == 0) newValue = false; - if (strncmp (strOpt,"on",2) == 0) newValue = true; - - if (session.show_hints != newValue) {// changed - showHintsState (prefShowOLD); - session.show_hints = newValue; - showHintsState (prefShowNEW); + if (strncmp (strOpt,"help",4) == 0) + return usage_set_hints(); + if (strncmp (strOpt,"off",3) == 0) { + validValue = true; + newValue = false; + } + if (strncmp (strOpt,"on",2) == 0) { + validValue = true; + newValue = true; + } + + if (validValue) { + if (session.show_hints != newValue) {// changed + showHintsState (prefShowOLD); + session.show_hints = newValue; + showHintsState (prefShowNEW); + } else { + PrintAndLogEx(INFO,"nothing changed"); + showHintsState (prefShowNone); + } + } else { + PrintAndLogEx(ERR,"invalid option"); + return usage_set_hints(); } - } else { - // error } } From 3b4f851d2d3453204aceb8cd8a6c3d9c01e4530f Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 13 Apr 2020 16:17:57 +1000 Subject: [PATCH 12/14] tweak save --- client/preferences.c | 17 ++++++++++++----- client/proxguiqt.cpp | 13 ++++++++++++- client/proxmark3.c | 3 ++- client/ui.h | 1 + 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/client/preferences.c b/client/preferences.c index 616fb17c0..7fa01ee3a 100644 --- a/client/preferences.c +++ b/client/preferences.c @@ -49,6 +49,7 @@ int preferences_load (void) { // Set all defaults session.client_debug_level = OFF; + session.window_changed = false; session.window_plot_xpos = 10; session.window_plot_ypos = 30; session.window_plot_hsize = 400; @@ -61,6 +62,7 @@ int preferences_load (void) { session.show_hints = false; session.supports_colors = false; + // loadFileJson wants these, so pass in place holder values, though not used // in settings load; uint8_t dummyData = 0x00; @@ -78,6 +80,7 @@ int preferences_load (void) { // Save all settings from memory (struct) to file int preferences_save (void) { // Note sure if backup has value ? + char backupFilename[FILENAME_MAX+sizeof(preferencesFilename)+10] = {0}; PrintAndLogEx(INFO,"Saving preferences ..."); @@ -353,6 +356,7 @@ static int setCmdEmoji (const char *Cmd) { showEmojiState (prefShowOLD); session.emoji_mode = newValue; showEmojiState (prefShowNEW); + preferences_save (); } else { PrintAndLogEx(INFO,"nothing changed"); showEmojiState (prefShowNone); @@ -399,6 +403,7 @@ static int setCmdColor (const char *Cmd) showColorState (prefShowOLD); session.supports_colors = newValue; showColorState (prefShowNEW); + preferences_save (); } else { PrintAndLogEx(INFO,"nothing changed"); showColorState (prefShowNone); @@ -450,6 +455,7 @@ static int setCmdDebug (const char *Cmd) session.client_debug_level = newValue; g_debugMode = newValue; showClientDebugState (prefShowNEW); + preferences_save (); } else { PrintAndLogEx(INFO,"nothing changed"); showClientDebugState (prefShowNone); @@ -496,6 +502,7 @@ static int setCmdHint (const char *Cmd) showHintsState (prefShowOLD); session.show_hints = newValue; showHintsState (prefShowNEW); + preferences_save (); } else { PrintAndLogEx(INFO,"nothing changed"); showHintsState (prefShowNone); @@ -546,8 +553,8 @@ static int CmdPrefShow (const char *Cmd) { showEmojiState (prefShowNone); showColorState (prefShowNone); - showPlotPosState (); - showOverlayPosState (); + // showPlotPosState (); + // showOverlayPosState (); showClientDebugState(prefShowNone); showHintsState (prefShowNone); @@ -555,18 +562,18 @@ static int CmdPrefShow (const char *Cmd) { return PM3_SUCCESS; } - +/* static int CmdPrefSave (const char *Cmd) { preferences_save(); return PM3_SUCCESS; } - +*/ static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, {"set", CmdPrefSet, AlwaysAvailable, "Set a preference"}, {"show", CmdPrefShow, AlwaysAvailable, "Show preferences"}, - {"save", CmdPrefSave, AlwaysAvailable, "Save preferences now"}, +// {"save", CmdPrefSave, AlwaysAvailable, "Save preferences now"}, {NULL, NULL, NULL, NULL} }; diff --git a/client/proxguiqt.cpp b/client/proxguiqt.cpp index dfa9aa9fc..7caf695ce 100644 --- a/client/proxguiqt.cpp +++ b/client/proxguiqt.cpp @@ -28,6 +28,8 @@ #include #include "ui.h" +extern "C" int preferences_save (void); + extern "C" { #include "util_darwin.h" } @@ -145,11 +147,14 @@ SliderWidget::SliderWidget() { void SliderWidget::resizeEvent (QResizeEvent *event) { session.window_overlay_hsize = event->size().height(); session.window_overlay_wsize = event->size().width(); + session.window_changed = true; + } void SliderWidget::moveEvent (QMoveEvent *event) { session.window_overlay_xpos = event->pos().x(); session.window_overlay_ypos = event->pos().y(); + session.window_changed = true; } //-------------------- @@ -246,6 +251,10 @@ ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) { controlWidget->setWindowTitle(ct); controlWidget->show(); + + // now that is up, reset pos/size change flags + session.window_changed = false; + } // not 100% sure what i need in this block @@ -268,6 +277,7 @@ ProxWidget::~ProxWidget(void) { plot = NULL; } } + void ProxWidget::closeEvent(QCloseEvent *event) { event->ignore(); this->hide(); @@ -284,11 +294,12 @@ void ProxWidget::showEvent(QShowEvent *event) { void ProxWidget::moveEvent(QMoveEvent *event) { session.window_plot_xpos = event->pos().x(); session.window_plot_ypos = event->pos().y(); - + session.window_changed = true; } void ProxWidget::resizeEvent(QResizeEvent *event) { session.window_plot_hsize = event->size().height(); session.window_plot_wsize = event->size().width(); + session.window_changed = true; } //----------- Plotting diff --git a/client/proxmark3.c b/client/proxmark3.c index b981d1758..940a000b2 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -949,7 +949,8 @@ int main(int argc, char *argv[]) { } #ifdef USE_PREFERENCE_FILE - preferences_save (); + if (session.window_changed) // Plot/Overlay moved or resized + preferences_save (); #endif exit(EXIT_SUCCESS); } diff --git a/client/ui.h b/client/ui.h index 3875c27c8..e28eca530 100644 --- a/client/ui.h +++ b/client/ui.h @@ -30,6 +30,7 @@ typedef struct { bool pm3_present; bool help_dump_mode; bool show_hints; + bool window_changed; // track if plot/overlay pos/size changed to save on exit int window_plot_xpos; int window_plot_ypos; int window_plot_hsize; From dded69c989b2f43dee5d6bb982a1275220be4f0a Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 13 Apr 2020 19:35:19 +1000 Subject: [PATCH 13/14] Update proxmark3.c Disable use_prefence_file --- client/proxmark3.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/proxmark3.c b/client/proxmark3.c index 940a000b2..e1d6556b6 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -30,7 +30,7 @@ #include "preferences.h" // Used to enable/disable use of preferences json file -#define USE_PREFERENCE_FILE +// #define USE_PREFERENCE_FILE #ifdef _WIN32 From acf4c09aa34d26af1169f9512d202a21505fd4cd Mon Sep 17 00:00:00 2001 From: mwalker33 Date: Mon, 13 Apr 2020 19:43:15 +1000 Subject: [PATCH 14/14] Update proxguiqt.h Cleanup --- client/proxguiqt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/proxguiqt.h b/client/proxguiqt.h index 4b4a21c11..bbc0fcefd 100644 --- a/client/proxguiqt.h +++ b/client/proxguiqt.h @@ -133,7 +133,7 @@ class ProxGuiQT : public QObject { void HideGraphWindow(void); void MainLoop(void); void Exit(void); - void SetWindowsPosition (void); + private slots: void _ShowGraphWindow(void); void _RepaintGraphWindow(void);