Добавлено: сохранение списка мониторов, выбора основного монитора

This commit is contained in:
Alexander Zhirov 2022-07-18 14:13:49 +03:00
parent b565b3d58b
commit f53fc681ab
10 changed files with 313 additions and 55 deletions

View File

@ -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()

178
db.c
View File

@ -5,9 +5,7 @@
* Author: alexander
*/
#include <stdbool.h>
#include "sqlite3.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -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;
}

20
db.h
View File

@ -8,6 +8,9 @@
#ifndef DB_H_
#define DB_H_
#include <stdlib.h>
#include <stdbool.h>
#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);

Binary file not shown.

82
monitor.c Normal file
View File

@ -0,0 +1,82 @@
/*
* monitor.c
*
* Created on: 18 июл. 2022 г.
* Author: alexander
*/
#include <string.h>
#include <stdio.h>
#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);
}

16
monitor.h Normal file
View File

@ -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_ */

View File

@ -15,7 +15,7 @@
#include <unistd.h>
#include "xrandr.h"
#include "rxrandr.h"
static char *program_name;
static Display *dpy;

View File

View File

@ -5,7 +5,7 @@
* Author: alexander
*/
#include "xrandr.h"
#include "rxrandr.h"
#include <stdlib.h>
#include <stdio.h>
@ -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)

View File

@ -10,7 +10,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include "xrandr.h"
#include "monitor.h"
#include <stdio.h>
#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(