1
0
Fork 0
forked from denis/a811
a811/src/conf.cpp

330 lines
8.6 KiB
C++

#include "conf.h"
#include <QDebug>
#include <iostream>
#include <klocalizedstring.h>
#include <qfiledialog.h>
#define CHK(X) \
if ((r = (X)) != 0) \
return r;
Conf::Conf(QObject *parent) : QObject(parent)
{
m_conf1 = (conf_1 *)malloc(sizeof(conf_1));
m_conf2 = (conf_2 *)malloc(sizeof(conf_2));
std::cout << std::dec << sizeof(conf_1) << "; " << sizeof(conf_2)
<< std::endl;
memset(m_conf1, 0, sizeof(conf_1));
memset(m_conf2, 0, sizeof(conf_2));
connection = &(Connection::getInstance());
if (connection->findDevice())
{
std::cerr << "ERROR: device not found" << std::endl;
exit(1);
}
m_mouse = new A811();
}
ssize_t Conf::appendMacroToFile(macro *macro, const char *filePath)
{
FILE *outfile;
outfile = fopen(filePath, "ab");
if (outfile == NULL)
{
fprintf(stderr, "\nError opened file\n");
exit(1);
}
// write struct to file
int flag = 0;
flag = fwrite(macro, sizeof(struct macro), 1, outfile);
if (flag)
{
printf("Contents of macro %d written "
"successfully %d\n",
macro->macro_nr, flag);
}
else
printf("Error Writing to File!\n");
fclose(outfile);
return 0;
}
ssize_t Conf::deleteMacroFromFile(macro *_macro, const char *filePath)
{
FILE *infile, *tempfile;
macro temp_macro;
// Open the original file for reading
infile = fopen(filePath, "rb");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file for reading\n");
exit(1);
}
// Open a temporary file for writing
tempfile = fopen("tempfile.bin", "wb");
if (tempfile == NULL)
{
fprintf(stderr, "\nError opening temporary file for writing\n");
fclose(infile);
exit(1);
}
// Read each macro from the original file
while (fread(&temp_macro, sizeof(macro), 1, infile) == 1)
{
// If the macro ID does not match the one to delete, write it to the temp
// file
if (temp_macro.macro_nr != _macro->macro_nr)
{
fwrite(&temp_macro, sizeof(macro), 1, tempfile);
}
else
{
printf("Macro with ID %d deleted.\n", _macro->macro_nr);
}
}
// Close both files
fclose(infile);
fclose(tempfile);
// Replace the original file with the temp file
remove(filePath); // Delete the original file
rename("tempfile.bin",
filePath); // Rename the temp file to the original file name
return 0;
return 0;
}
void Conf::testFunc()
{
m_mouse->readConfigFromDevice(CONF_TYPE::BATTERY);
m_mouse->readConfigFromDevice(CONF_TYPE::LIGHT);
m_mouse->readConfigFromDevice(CONF_TYPE::KEY);
m_mouse->setLightMode(LIGHT_MODE::BREATHING, 1, 4);
m_mouse->writeConfigToDevice(CONF_TYPE::LIGHT);
m_mouse->setUSBPollingRate(POLLING_RATE::P_1000);
std::cout << "UBS Polling rate: " << (int)m_mouse->getUSBPollingRate() << std::endl;
std::cout << "BATTERYSTATUS: " << (int)m_mouse->getBatteryStatus() << std::endl;
std::cout << "LIGHTMODE: " << (int)m_mouse->getLightMode() << std::endl;
m_mouse->setLightMode(LIGHT_MODE::COLORFUL_STREAMING, 1, 2);
m_mouse->writeConfigToDevice(CONF_TYPE::LIGHT);
sleep(2);
m_mouse->setMultimediaButton(2, "media_volume_up");
// does not work for some reason
m_mouse->setMultimediaButton(6, "dpi-cycle");
m_mouse->setButton(7, "Volume_Up", 0);
m_mouse->setButton(8, "b", 2);
m_mouse->setButton(9, "c", 4);
m_mouse->setButton(10, "d", 8);
m_mouse->setButton(11, "e", 3);
m_mouse->setButton(12, "f", 5);
m_mouse->setButton(13, "g", 7);
m_mouse->setButton(14, "h", 9);
// dont know if every keycode is working
int bid = 0;
std::cout << "Button " << bid << m_mouse->getButton(bid).name << std::endl;
m_mouse->writeConfigToDevice(CONF_TYPE::KEY);
std::cout << m_mouse->getButton(3).name << std::endl;
m_mouse->setLightMode(LIGHT_MODE::BREATHING, 4, 4);
unsigned char *cols;
cols = m_mouse->getLightModeColors(LIGHT_MODE::BREATHING);
for (int i = 0; i < 21; i++)
{
std::cout << cols[i];
}
std::cout << std::endl;
m_mouse->getLightModeColors(LIGHT_MODE::COLORFUL_STREAMING);
unsigned char cols2[21] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00};
m_mouse->setLightModeColors(LIGHT_MODE::BREATHING, cols2);
unsigned char cols3[3] = {0xff, 0x00, 0x00};
m_mouse->setDPI(DPI_MODE::DPI_1, cols3, 800, 5);
cols3[0] = 0x0;
cols3[1] = 0xff;
m_mouse->setDPI(DPI_MODE::DPI_2, cols3, 1000, 5);
cols3[1] = 0x0;
cols3[2] = 0xff;
m_mouse->setDPI(DPI_MODE::DPI_3, cols3, 1200, 5);
cols3[0] = 0xff;
cols3[2] = 0xff;
m_mouse->setDPI(DPI_MODE::DPI_4, cols3, 1400, 5);
cols3[0] = 0xff;
cols3[1] = 0xff;
cols3[2] = 0xff;
m_mouse->setDPI(DPI_MODE::DPI_5, cols3, 1500, 5);
// m_mouse->writeConfigToDevice(CONF_TYPE::KEY);
std::cout << "DPI at this: " << m_mouse->getDPI(DPI_MODE::DPI_1) << std::endl;
std::cout << "Battery capacity:" << std::dec << m_mouse->getBatteryCapacity() << std::endl;
m_mouse->writeConfigToDevice(CONF_TYPE::LIGHT);
m_mouse->readMacrosFromDevice(4);
}
ssize_t Conf::writeConfigToFile(const char *filePath)
{
FILE *outfile;
outfile = fopen(filePath, "wb");
if (outfile == NULL)
{
fprintf(stderr, "\nError opened file\n");
exit(1);
}
// write struct to file
int flag = 0;
flag = fwrite(m_conf1, sizeof(struct conf_1), 1, outfile);
if (flag)
{
printf("Contents of conf1 written "
"successfully %d\n",
flag);
}
else
printf("Error Writing to File!\n");
flag = fwrite(m_conf2, sizeof(struct conf_2), 1, outfile);
if (flag)
{
printf("Contents of conf2 written "
"successfully %d\n",
flag);
}
else
printf("Error Writing to File!\n");
fclose(outfile);
return 0;
}
ssize_t Conf::writeConfigToFile()
{
QString path = QFileDialog::getSaveFileName(nullptr, i18n("Save File As"));
writeConfigToFile(path.toUtf8().constData());
return 0;
}
ssize_t Conf::writeConfigToFile(conf_2 *conf, const char *filePath)
{
FILE *outfile;
outfile = fopen(filePath, "wb");
if (outfile == NULL)
{
fprintf(stderr, "\nError opened file\n");
exit(1);
}
// write struct to file
int flag = 0;
flag = fwrite(conf, sizeof(struct conf_2), 1, outfile);
if (flag)
{
printf("Contents of the structure written "
"successfully %d\n",
flag);
}
else
printf("Error Writing to File!\n");
fclose(outfile);
return 0;
}
ssize_t Conf::readConfigFromFile(const char *filePath, conf_1 *conf)
{
FILE *infile;
// Open person.dat for reading
infile = fopen(filePath, "rb");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
return ssize_t(1);
}
// reading to read_struct
if (sizeof(struct conf_1) > fread(conf, sizeof(struct conf_1), 1, infile))
fprintf(stderr, "\nError file\n");
// close file
fclose(infile);
return ssize_t(0);
}
ssize_t Conf::readConfigFromFile(const char *filePath, conf_2 *conf)
{
FILE *infile;
// Open person.dat for reading
infile = fopen(filePath, "rb");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
return ssize_t(1);
}
// reading to read_struct
if (sizeof(struct conf_2) > fread(conf, sizeof(struct conf_2), 1, infile))
fprintf(stderr, "\nError file\n");
// close file
fclose(infile);
return ssize_t(0);
}
ssize_t Conf::readConfigFromFile(const char *filePath)
{
FILE *infile;
infile = fopen(filePath, "rb");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
return ssize_t(1);
}
if (sizeof(struct conf_1) > fread(m_conf1, sizeof(struct conf_1), 1, infile) ||
sizeof(struct conf_2) > fread(m_conf2, sizeof(struct conf_2), 1, infile))
fprintf(stderr, "\nError file\n");
fclose(infile);
return 0;
}
ssize_t Conf::readConfigFromFile()
{
QFileDialog dialog(nullptr);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilter(tr("Images (*.bin *.xpm *.jpg)"));
dialog.setAcceptMode(QFileDialog::AcceptOpen);
QString path = dialog.getOpenFileName(nullptr, i18n("Select Configuration"));
readConfigFromFile(path.toUtf8().constData());
return 0;
}