add multithreading for listen

This commit is contained in:
rick 2022-12-14 17:32:36 +01:00
parent 2edcdcb3e2
commit 55a4f5d8ab
Signed by: Rick
GPG Key ID: 4A6223D66294EB20
5 changed files with 60 additions and 11 deletions

View File

@ -10,7 +10,7 @@ CFLAGS := -Wall -Wextra -g -I.
CXXFLAGS := -fpermissive -DIMGUI_IMPL_OPENGL_LOADER_GLFW -DIMGUI_IMPL_API="" -Wall -Wextra -g -I.
#CFLAGS := -ansi -pedantic -Wall -Wextra -g -I.
#CFLAGS := -Wall -ansi -g -I.
LDLIBS := -lxdo -lX11 -lglfw -lGL -lcimgui
LDLIBS := -lxdo -lX11 -lglfw -lGL -lcimgui -lpthread
all: $(NAME)

View File

@ -1,4 +1,5 @@
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -7,6 +8,30 @@
#include "event.h"
#include "read_events.h"
args_cancel_thread_read* init_args_read(int fd, event *user_event, Display *display)
{
args_cancel_thread_read *ret = (args_cancel_thread_read *) malloc(sizeof(args_cancel_thread_read));
ret->fd = fd;
ret->user_event = user_event;
ret->display = display;
return ret;
}
void cancel_read_events(args_cancel_thread_read *arg)
{
int fd = arg->fd;
/* flush the middle button */
unsigned char data[3];
read(fd, data, sizeof(data));
read(fd, data, sizeof(data));
free(arg->user_event);
close(fd);
XCloseDisplay(arg->display);
free(arg);
}
void write_event(event *new_event, FILE *file)
{
fwrite(new_event, sizeof(event), 1, file);
@ -38,6 +63,8 @@ int listen(FILE *file)
return -1;
}
pthread_cleanup_push(cancel_read_events, init_args_read(fd, user_event, display));
while (!(left && right))
{
/* Read Mouse */
@ -82,9 +109,7 @@ int listen(FILE *file)
free(user_event);
close(fd);
XCloseDisplay(display);
pthread_cleanup_pop(1);
return 0;
/*
TODO on ecoute la souris
TODO on repete ce qu'on a entre
*/
}

View File

@ -1,7 +1,20 @@
#ifndef READ_EVENTS
#define READ_EVENTS
#include <X11/Xlib.h>
#include "event.h"
/*
* Structure utilisée pour passer des arguments à la fonction
* pthread_cleanup_push.
*/
typedef struct {
int fd;
event *user_event;
Display *display;
} args_cancel_thread_read;
args_cancel_thread_read * init_args_read(int fd, event *user_event, Display *display);
void write_event(event *new_event, FILE *file);
/* Just listen the input and write them in the parameter file. */

View File

@ -1,5 +1,6 @@
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include <imgui/cimgui.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
@ -46,13 +47,15 @@ int draw_ui(ui_infos *ptr)
if (ptr->is_recording)
{
igButton("Appuyer sur les clics droit et gauche pour arrêter d'enregistrer.", (ImVec2) { 0, 0 });
ptr->is_recording = listen(ptr->file);
if (ptr->is_recording)
if (igButton("Arrêter l'enregistrement.", (ImVec2) { 0, 0 }))
{
fprintf(stderr, "listen a eu un soucis.");
return -1;
if (pthread_cancel(ptr->pid))
{
fprintf(stderr, "Erreur lors du cancel du thread listen.\n");
return -1;
}
ptr->is_recording = 0;
}
}
else
@ -60,6 +63,12 @@ int draw_ui(ui_infos *ptr)
if (igButton("Lancer l'enregistrement.", (ImVec2) { 0, 0 }))
{
ptr->is_recording = 1;
if (pthread_create(&ptr->pid, NULL, listen, ptr->file))
{
fprintf(stderr, "Erreur à la création du thread de listen.\n");
return -1;
}
}
if (igButton("Replay", (ImVec2) {0, 0}))

View File

@ -1,10 +1,12 @@
#ifndef UI
#define UI
#include <pthread.h>
typedef struct {
char *name;
FILE *file;
char is_recording;
pthread_t pid;
} ui_infos;
ui_infos * init_ui(char *name);