diff --git a/arguments.c b/arguments.c index 30da47c..bf42643 100644 --- a/arguments.c +++ b/arguments.c @@ -10,51 +10,21 @@ #include "node_settings.h" #include "concat.h" -#include "xrandr.h" +#include "rxrandr.h" #include "db.h" +#include "monitor.h" void settingsLoad() { -// dbGetHostsList(); - if (!dbLoadData()) + dbLoadData(); + + Monitors *monitors = dbGetMonitorsList(); + for (size_t i = 0; i < monitors->size; ++i) { - addParameterKey(PARAMETER_XFREERDP, "xfreerdp", true, true, NULL, NULL); - addParameterKey(PARAMETER_SERVER, "/v:", true, true, NULL, NULL); - addParameterKey(PARAMETER_USERNAME, "/u:", true, true, NULL, NULL); - addParameterKey(PARAMETER_PASSWORD, "/p:", true, true, NULL, NULL); - addParameterKey(PARAMETER_CERTIGNORE, "/cert-ignore", true, true, NULL, NULL); - addParameterKey(PARAMETER_THEMES, "-themes", false, true, NULL, NULL); - addParameterKey(PARAMETER_WALLPAPER, "-wallpaper", false, true, NULL, NULL); - addParameterKey(PARAMETER_ENCRYPTION, "-encryption", false, true, NULL, NULL); - addParameterKey(PARAMETER_FONTS, "-fonts", false, true, NULL, NULL); - addParameterKey(PARAMETER_SOUND, "/sound", false, true, NULL, NULL); - addParameterKey(PARAMETER_COMPRESSION, "+compression", false, true, NULL, NULL); - addParameterKey(PARAMETER_FULLSCREEN, "/f", false, true, NULL, NULL); - addParameterKey(PARAMETER_MULTIMONITOR, "/multimon", false, true, getParameter(PARAMETER_FULLSCREEN), NULL); - addParameterKey(PARAMETER_AUTHENTICATION, "-authentication", true, true, NULL, NULL); - addParameterKey(PARAMETER_SECURITY, "/sec:", true, true, NULL, NULL); - addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_TLS, "tls", true); - addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_RDP, "rdp", false); - addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_NLA, "nla", false); - addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_EXT, "ext", false); - addParameterKey(PARAMETER_BITSPERPIXEL, "/bpp:", true, true, NULL, NULL); - addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_8, "8", true); - addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_16, "16", false); - addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_24, "24", false); - addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_32, "32", false); - - addParameterKey(PARAMETER_USB, "/a:drive,USB,", true, true, NULL, NULL); - addParameterValue(PARAMETER_USB, VALUES_USB_DRIVE, "/mnt/usbdevice", true); - - addParameterKey(PARAMETER_MONITORS, "/monitors:", false, true, getParameter(PARAMETER_FULLSCREEN), getParameter(PARAMETER_MULTIMONITOR)); + addParameterValue(PARAMETER_MONITORS, i, monitors->monitor[i]->data[3], !i); } - x_info *monitors = getXInfo(); - for (size_t i = 0; i < monitors->count; ++i) - { - addParameterValue(PARAMETER_MONITORS, i, monitors->monitor[i].ptrIndexMonitor, !i); - } - freeXInfo(monitors); + dbFreeMonitors(monitors); } void settingsFree() diff --git a/db.c b/db.c index 5cacce3..585e48d 100644 --- a/db.c +++ b/db.c @@ -5,9 +5,7 @@ * Author: alexander */ -#include #include "sqlite3.h" -#include #include #include @@ -386,3 +384,179 @@ bool dbSetUserNameCurrent(char *current) return true; } + +static int dbCreateMonitorsList(void *answer, int argc, char **argv, char **azColName) +{ + Monitors *monitors = *(Monitors **)answer; + Monitor *monitor = (Monitor*) malloc(sizeof(Monitor)); + monitor->data = (char **) malloc(sizeof(char*) * argc); + monitor->size = argc; + + for (int i = 0; i < argc; i++) + { + monitor->data[i] = (char *) malloc(sizeof(char) * strlen(argv[i])); + strcpy(monitor->data[i], argv[i]); + } + + Monitor **tmp = monitors->monitor; + monitors->monitor = (Monitor**) malloc(sizeof(Monitor*) * ++monitors->size); + for (size_t i = 0; i < monitors->size - 1; ++i) + { + monitors->monitor[i] = tmp[i]; + } + if (tmp) + { + free(tmp); + } + monitors->monitor[monitors->size - 1] = monitor; + + return 0; +} + +Monitors *dbGetMonitorsList() +{ + sqlite3 *db = dbGetBase("freerdp.db"); + if (!db) + { + return false; + } + + char *err_msg = 0; + char *sql = "SELECT ROW_NUMBER () OVER (ORDER BY `monitors`.`set` DESC) `item`, `monitors`.`name` as `name`, `monitors`.`set` as `set`, (ROW_NUMBER () OVER (ORDER BY `monitors`.`id`)) - 1 `item` FROM `monitors` as `monitors`"; + + Monitors *monitors = (Monitors *) malloc(sizeof(Monitors)); + monitors->size = 0; + monitors->monitor = NULL; + + if (sqlite3_exec(db, sql, dbCreateMonitorsList, &monitors, &err_msg) != SQLITE_OK) + { + fprintf(stderr, "Ошибка выполнения запроса: %s\n", err_msg); + sqlite3_free(err_msg); + sqlite3_close(db); + exit(-1); + } + + sqlite3_close(db); + + return monitors; +} + +void dbFreeMonitors(Monitors *monitors) +{ + for (size_t i = 0; i < monitors->size; ++i) + { + for (size_t j = 0; j < monitors->monitor[i]->size; ++j) + { + free(monitors->monitor[i]->data[j]); + } + free(monitors->monitor[i]->data); + free(monitors->monitor[i]); + } + free(monitors); +} + +bool deleteAllMonitors() +{ + sqlite3 *db = dbGetBase("freerdp.db"); + if (!db) + { + return false; + } + + bool result = false; + char *sql = "DELETE FROM `monitors`"; + + if (sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK) + { + result = true; + } + + sqlite3_close(db); + + return result; +} + +int dbAddMonitor(char *monitor, bool set) +{ + if (!(monitor && strlen(monitor))) + return -1; + + sqlite3 *db = dbGetBase("freerdp.db"); + if (!db) + { + return -2; + } + + sqlite3_stmt *res; + char *sql = "INSERT INTO `monitors` (`name`, `set`) VALUES (?, ?)"; + if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK) + { + sqlite3_bind_text(res, 1, monitor, -1, 0); + sqlite3_bind_int(res, 2, set); + } + +// if (sqlite3_step(res) == SQLITE_BUSY) +// { +// fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", text ? "установлено" : "очищено", text ? getValue(VALUE_USERNAME)->current : ""); +// } +// if (sqlite3_step(res) == SQLITE_ERROR) +// { +// fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", text ? "установлено" : "очищено", text ? getValue(VALUE_USERNAME)->current : ""); +// } + if (sqlite3_step(res) == SQLITE_DONE) + { + fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", monitor ? "установлено" : "очищено", monitor ? monitor : ""); + return 0; + } + + sqlite3_finalize(res); + sqlite3_close(db); + + return -3; +} + +int dbSaveMonitors(char *name) +{ + int result = 0; + + if (!(name && strlen(name))) + return -1; + + sqlite3 *db = dbGetBase("freerdp.db"); + if (!db) + { + return -2; + } + + char *sql = "UPDATE `monitors` set `set` = 0"; + if (sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK) + { + sqlite3_stmt *res; + sql = "UPDATE `monitors` SET `set` = 1 WHERE `name` = ?"; + if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK) + { + sqlite3_bind_text(res, 1, name, -1, 0); + } + + if (sqlite3_step(res) == SQLITE_BUSY) + { + fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", name ? "установлено" : "очищено", name); + result = 2; + } + if (sqlite3_step(res) == SQLITE_ERROR) + { + fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", name ? "установлено" : "очищено", name); + result = 1; + } + if (sqlite3_step(res) == SQLITE_DONE) + { + fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", name ? "установлено" : "очищено", name); + result = 0; + } + sqlite3_finalize(res); + } + + sqlite3_close(db); + + return result; +} diff --git a/db.h b/db.h index 325bcce..2f9b864 100644 --- a/db.h +++ b/db.h @@ -8,6 +8,9 @@ #ifndef DB_H_ #define DB_H_ +#include +#include + #include "parameter.h" #include "value.h" @@ -23,9 +26,26 @@ typedef struct Hosts size_t size; } Hosts; +typedef struct Monitor +{ + char **data; + size_t size; +} Monitor; + +typedef struct Monitors +{ + Monitor **monitor; + size_t size; +} Monitors; + bool dbLoadData(); Hosts *dbGetHostsList(); void dbFreeHosts(Hosts *hosts); +Monitors *dbGetMonitorsList(); +void dbFreeMonitors(Monitors *monitors); +bool deleteAllMonitors(); +int dbAddMonitor(char *monitor, bool set); +int dbSaveMonitors(char *name); bool dbWriteParameter(Parameter name, bool set); bool dbWriteValue(Value name, bool set); bool dbSetUserNameCurrent(char *current); diff --git a/freerdp.db b/freerdp.db index d3ca45b..9809b4b 100644 Binary files a/freerdp.db and b/freerdp.db differ diff --git a/monitor.c b/monitor.c new file mode 100644 index 0000000..940a885 --- /dev/null +++ b/monitor.c @@ -0,0 +1,82 @@ +/* + * monitor.c + * + * Created on: 18 июл. 2022 г. + * Author: alexander + */ + +#include +#include + +#include "monitor.h" +#include "rxrandr.h" + +static bool checkMonitor(Monitors *dbMonitors, x_info *pcMonitors) +{ + if (!dbMonitors->size || !pcMonitors->count || + !(dbMonitors->size == pcMonitors->count)) + { + return false; + } + + bool compare = false; + + for (size_t i = 0; i < dbMonitors->size; ++i) + { + for (size_t j = 0; j < pcMonitors->count; ++j) + { + if (!strcmp(pcMonitors->monitor[j].name, dbMonitors->monitor[i]->data[1])) + { + compare = true; + } + } + + if (!compare) + { + return false; + } + + compare = false; + } + + return true; +} + +Monitors *loadMonitors() +{ + Monitors *dbMonitors = dbGetMonitorsList(); + x_info *pcMonitors = getXInfo(); + if (!checkMonitor(dbMonitors, pcMonitors)) // Если строки не равны, произвести перезапись в БД + { + dbFreeMonitors(dbMonitors); + if (!deleteAllMonitors()) + { + fprintf(stderr, "Не удалось удалить записи мониторов из БД\n"); + exit(1); + } + + for (size_t i = 0; i < pcMonitors->count; ++i) + { + dbAddMonitor(pcMonitors->monitor[i].name, pcMonitors->monitor[i].primary); + } + freeXInfo(pcMonitors); + + return dbGetMonitorsList(); + } + + freeXInfo(pcMonitors); + + return dbMonitors; +} + +void freeMonitors(Monitors *monitors) +{ + for (size_t i = 0; i < monitors->size; ++i) + { + free(monitors->monitor[i]->data[2]); + free(monitors->monitor[i]->data[3]); + free(monitors->monitor[i]->data); + free(monitors->monitor[i]); + } + free(monitors); +} diff --git a/monitor.h b/monitor.h new file mode 100644 index 0000000..1a6dde2 --- /dev/null +++ b/monitor.h @@ -0,0 +1,16 @@ +/* + * monitor.h + * + * Created on: 18 июл. 2022 г. + * Author: alexander + */ + +#ifndef MONITOR_H_ +#define MONITOR_H_ + +#include "db.h" + +Monitors *loadMonitors(); +void freeMonitors(Monitors *monitors); + +#endif /* MONITOR_H_ */ diff --git a/xrandr.c b/rxrandr.c similarity index 99% rename from xrandr.c rename to rxrandr.c index b529932..eaee924 100644 --- a/xrandr.c +++ b/rxrandr.c @@ -15,7 +15,7 @@ #include -#include "xrandr.h" +#include "rxrandr.h" static char *program_name; static Display *dpy; diff --git a/xrandr.h b/rxrandr.h similarity index 100% rename from xrandr.h rename to rxrandr.h diff --git a/xrandr_broker.c b/rxrandr_broker.c similarity index 96% rename from xrandr_broker.c rename to rxrandr_broker.c index e94d549..137cc24 100644 --- a/xrandr_broker.c +++ b/rxrandr_broker.c @@ -5,7 +5,7 @@ * Author: alexander */ -#include "xrandr.h" +#include "rxrandr.h" #include #include @@ -14,6 +14,7 @@ x_info *getXInfo() { x_info *monitors = (x_info *) malloc(sizeof(x_info)); + monitors->count = 0; XInfo(monitors); for (int i = 0; i < monitors->count; ++i) diff --git a/settings.c b/settings.c index 15ac171..2286f84 100644 --- a/settings.c +++ b/settings.c @@ -10,7 +10,7 @@ #include #include -#include "xrandr.h" +#include "monitor.h" #include #include "settings.h" @@ -33,6 +33,7 @@ static int settingsClose(Ihandle *self) static int settingsSave(Ihandle *self) { saveChangeSettings(); + dbSaveMonitors(IupGetAttribute(IupGetDialogChild(self, "MONITORS"), "VALUESTRING")); IupSetAttribute(IupGetDialog(self), "SIMULATEMODAL", "OFF"); IupHide(IupGetDialog(self)); return IUP_DEFAULT; @@ -324,27 +325,21 @@ static Ihandle* settingsBoxMonitor() tglFullscreen = IupToggle("На весь экран", NULL); ddMonitor = IupList(NULL); - x_info *monitors = getXInfo(); - char *allMonitorIndex = (char*) malloc(sizeof(char) * 3); - sprintf(allMonitorIndex, "%hu", monitors->count + 1); - size_t setValueIndex = 0; - - for (short i = 0; i < monitors->count; ++i) + Monitors *monitors = loadMonitors(); + for (size_t i = 0; i < monitors->size; ++i) { - IupSetAttribute(ddMonitor, monitors->monitor[i].ptrIndexItem, monitors->monitor[i].ptrName); - if (getSetValue(PARAMETER_MONITORS, i)) - setValueIndex = i + 1; + IupSetAttribute(ddMonitor, monitors->monitor[i]->data[0], monitors->monitor[i]->data[1]); + if (monitors->monitor[i]->data[2][0] == '1') + IupSetAttribute(ddMonitor, "VALUE", monitors->monitor[i]->data[0]); } + freeMonitors(monitors); - IupSetInt(ddMonitor, "VALUE", setValueIndex ? setValueIndex : 1); IupSetInt(tglMultimonitor, "VALUE", getSetParameter(PARAMETER_MULTIMONITOR)); IupSetInt(tglFullscreen, "VALUE", getSetParameter(PARAMETER_FULLSCREEN)); IupSetInt(tglMultimonitor, "ACTIVE", getSetParameter(PARAMETER_FULLSCREEN)); IupSetInt(ddMonitor, "ACTIVE", getSetParameter(PARAMETER_FULLSCREEN) && !getSetParameter(PARAMETER_MULTIMONITOR)); - free(monitors); - return IupSetAttributes( IupFrame( IupVbox(