macros/src/ui.c

109 lines
2.7 KiB
C
Raw Normal View History

2022-12-13 00:33:56 +00:00
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include <imgui/cimgui.h>
2022-12-14 16:32:36 +00:00
#include <pthread.h>
2022-12-13 00:33:56 +00:00
#include <stdlib.h>
#include <string.h>
2022-12-19 14:48:33 +00:00
#include <unistd.h>
2022-12-13 00:33:56 +00:00
2022-12-13 01:00:00 +00:00
#include "read_events.h"
#include "read_file.h"
2022-12-13 00:33:56 +00:00
#include "ui.h"
2022-12-19 13:52:03 +00:00
const struct ImVec2 zero_vec2 = {0, 0};
ui_infos * init_ui(const char *name)
2022-12-13 00:33:56 +00:00
{
ui_infos *ret = (ui_infos *) malloc(sizeof(ui_infos));
ret->name = (char *) malloc(strlen(name) + 1);
2022-12-13 00:33:56 +00:00
strcpy(ret->name, name);
ret->file = fopen(name, "w+");
ret->is_recording = 0;
2022-12-17 01:31:33 +00:00
ret->args_rf = args_rf_init(ret->file, 1);
2022-12-19 14:48:33 +00:00
ret->nb_loop = 0;
ret->sleep_loop = 0;
2022-12-17 01:31:33 +00:00
return ret;
2022-12-13 00:33:56 +00:00
}
void free_ui(ui_infos *ptr)
{
2022-12-17 01:31:33 +00:00
if (ptr->args_rf != NULL) free(ptr->args_rf);
2022-12-13 00:33:56 +00:00
if (ptr->file != NULL) fclose(ptr->file);
if (ptr->name != NULL) free(ptr->name);
free(ptr);
}
int draw_ui(ui_infos *ptr)
{
/* en plein écran */
ImGuiViewport *vp = igGetMainViewport();
2022-12-19 13:52:03 +00:00
igSetNextWindowPos(vp->Pos, ImGuiCond_None, zero_vec2);
igSetNextWindowSize(vp->Size, ImGuiCond_None);
2022-12-13 00:33:56 +00:00
/*
* Choix du fichier
*
* Bouton enregistrement
*
* Bouton de rewind (bloqué si pas de fichiers)
*
* Liste déroulantes des fenetres pour savoir faire les clics
*/
igBegin("Test", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration);
2022-12-17 01:31:33 +00:00
igText("Fichier : %s", ptr->name);
2022-12-13 01:00:00 +00:00
2022-12-13 00:33:56 +00:00
if (ptr->is_recording)
{
2022-12-19 13:52:03 +00:00
if (igButton("Arrêter l'enregistrement.", zero_vec2))
2022-12-13 00:33:56 +00:00
{
2022-12-14 16:32:36 +00:00
if (pthread_cancel(ptr->pid))
{
fprintf(stderr, "Erreur lors du cancel du thread listen.\n");
return -1;
}
ptr->is_recording = 0;
2022-12-13 00:33:56 +00:00
}
}
else
{
2022-12-19 13:52:03 +00:00
if (igButton("Lancer l'enregistrement.", zero_vec2))
2022-12-13 00:33:56 +00:00
{
ptr->is_recording = 1;
2022-12-14 16:32:36 +00:00
if (pthread_create(&ptr->pid, NULL, listen, ptr->file))
{
fprintf(stderr, "Erreur à la création du thread de listen.\n");
return -1;
}
2022-12-13 00:33:56 +00:00
}
2022-12-13 01:00:00 +00:00
2022-12-17 01:31:33 +00:00
igSetNextItemWidth(100.0);
igInputInt("Temps de repos entre chaque clic (s)", &ptr->args_rf->time_sleep, 1, 10, 0);
if (ptr->args_rf->time_sleep <= 0) ptr->args_rf->time_sleep = 1;
2022-12-19 14:48:33 +00:00
igSetNextItemWidth(100.0);
igInputInt("Nombre de répétitions du fichier", &ptr->nb_loop, 1, 10, 0);
if (ptr->nb_loop < 0) ptr->nb_loop = 0;
igSetNextItemWidth(100.0);
igInputInt("Temps de repos entre chaque répétition (s)", &ptr->sleep_loop, 1, 10, 0);
if (ptr->sleep_loop < 0) ptr->sleep_loop = 0;
2022-12-19 13:52:03 +00:00
if (igButton("Replay", zero_vec2))
2022-12-13 01:00:00 +00:00
{
2022-12-19 14:48:33 +00:00
int i;
for (i = 0; i <= ptr->nb_loop; i++)
{
rewind(ptr->file);
readfile(ptr->args_rf);
sleep(ptr->sleep_loop);
}
2022-12-13 01:00:00 +00:00
}
2022-12-13 00:33:56 +00:00
}
igEnd();
return 0;
}