add basic ui

This commit is contained in:
rick 2022-12-13 01:33:56 +01:00
parent a4a6be6fb4
commit 16a1193e56
Signed by: Rick
GPG key ID: 4A6223D66294EB20
5 changed files with 171 additions and 168 deletions

View file

@ -1,8 +1,8 @@
NAME := macros
SRC_DIR := src
SRCS := main.c event.c read_events.c read_file.c
OBJS := main.o event.o read_events.o read_file.o
SRCS := event.c read_events.c read_file.c ui.c
OBJS := event.o read_events.o read_file.o ui.o main.o
CC := gcc
CCXX := g++
@ -17,7 +17,7 @@ all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(OBJS) $(LDLIBS) -o $(NAME)
main.o: $(SRC_DIR)/main.c
main.o: $(SRC_DIR)/main.cpp
$(CCXX) -I$(SRC_DIR) $(CXXFLAGS) -c -o $@ $<
%.o: $(SRC_DIR)/%.c

View file

@ -1,165 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "read_events.h"
#include "read_file.h"
/*
#include <X11/Xlib.h>
#include <xdo.h>
#include <fcntl.h>
*/
/*
void write_mouse_event(mouse_event *event, FILE *file)
{
fwrite(event, sizeof(mouse_event), 1, file);
fflush(file);
}
*/
/*
void listen() {
FILE *ptr = fopen("t", "a");
if (ptr == NULL) return -1;
mouse_event *my_event = (mouse_event *) malloc(sizeof(mouse_event));
Display *display;
Window root_window;
XEvent event;
display = XOpenDisplay(0);
root_window = DefaultRootWindow(display);
int fd, bytes;
unsigned char data[3];
const char *pDevice = "/dev/input/mice";
// Open Mouse
fd = open(pDevice, O_RDWR);
if(fd == -1)
{
printf("ERROR Opening %s\n", pDevice);
return -1;
}
int left = 0;
int right = 0;
int middle = 0;
signed char x, y;
while(!left || !right)
{
// Read Mouse
bytes = read(fd, data, sizeof(data));
if(bytes > 0)
{
left = data[0] & 0x1;
right = data[0] & 0x2;
middle = data[0] & 0x4;
XQueryPointer(
display,
root_window,
&event.xbutton.root,
&event.xbutton.subwindow,
&event.xbutton.x_root,
&event.xbutton.y_root,
&event.xbutton.x,
&event.xbutton.y,
&event.xbutton.state);
if ((left || right) && !(left && right)) {
my_event->infos &= 0x00; // rese;t
my_event->infos = 0x80 | (0x01 * left) | (0x02 * right) | (0x03 * middle);
my_event->x = event.xmotion.x;
my_event->y = event.xmotion.y;
printf("%d - %d\n", my_event->x, my_event->y);
write_mouse_event(my_event, ptr);
}
//printf("====== %d\n", event.xbutton.button);
printf("x=%d, y=%d, left=%d, middle=%d, right=%d\n", event.xmotion.x, event.xmotion.y, left, middle, right);
}
}
// flush the middle button
bytes = read(fd, data, sizeof(data));
fclose(ptr);
return 0;
// TODO on ecoute la souris
// TODO on repete ce qu'on a entre
}
*/
/*
void readfile() {
FILE *ptr = fopen("t", "r");
xdo_t *x_t = xdo_new(NULL);
if (ptr == NULL) return -1;
mouse_event *my_event = (mouse_event *) malloc(sizeof(mouse_event));
int left, right, x, y;
while (!feof(ptr))
{
printf("Souris \n");
fread(my_event, sizeof(mouse_event), 1, ptr);
left = my_event->infos & 0x01;
right = my_event->infos & 0x02;
x = my_event->x;
y = my_event->y;
printf("%d - %d - %d\n", my_event->infos, x, y);
xdo_move_mouse(x_t, x, y, 0);
sleep(1);
if (left) {
xdo_click_window(x_t, CURRENTWINDOW, 1);
}
if (right) {
xdo_click_window(x_t, CURRENTWINDOW, 3);
}
sleep(5);
}
xdo_free(x_t);
}
*/
int main()
{
int t;
FILE *file = fopen("t", "w+");
if (file == NULL) return -1;
t = listen(file);
if (t) return t;
printf("Fin\n");
rewind(file);
printf("On commence\n");
sleep(5);
readfile(file);
fclose(file);
return 0;
}

89
src/main.cpp Normal file
View file

@ -0,0 +1,89 @@
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <imgui/cimgui.h>
#include <imgui/imgui_impl_opengl3.h>
#include <imgui/imgui_impl_glfw.h>
extern "C" {
#include "ui.h"
}
#define WIDTH 400
#define HEIGHT 200
void glfw_logger(int error, const char *desc)
{
fprintf(stderr, "Erreur %d GLFW: %s\n", error, desc);
}
int main(void)
{
GLFWwindow* window;
ImGuiContext *ctx;
ImGuiIO *io;
const char *glsl_version;
if (!glfwInit())
{
fprintf(stderr, "Erreur à l'initialisation de GLFW.\n");
return -1;
}
glfwSetErrorCallback(glfw_logger);
glfwWindowHint(GLFW_RESIZABLE, 0);
window = glfwCreateWindow(WIDTH, HEIGHT, "Macros", NULL, NULL);
if (!window)
{
fprintf(stderr, "Erreur lors de la création de la fenêtre GLFW.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
ctx = igCreateContext(NULL);
io = igGetIO();
glsl_version = "#version 330 core";
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
igStyleColorsDark(NULL);
char is_recording = 0;
char *name = "fichier.macros";
ui_infos *ui = init_ui(name);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
igNewFrame();
if (draw_ui(ui)) break;
igRender();
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData());
glfwSwapBuffers(window);
}
free_ui(ui);
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
igDestroyContext(ctx);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

65
src/ui.c Normal file
View file

@ -0,0 +1,65 @@
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include <imgui/cimgui.h>
#include <stdlib.h>
#include <string.h>
#include "ui.h"
ui_infos * init_ui(char *name)
{
ui_infos *ret = (ui_infos *) malloc(sizeof(ui_infos));
ret->name = (char *) malloc(strlen(name));
strcpy(ret->name, name);
ret->file = fopen(name, "w+");
ret->is_recording = 0;
}
void free_ui(ui_infos *ptr)
{
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();
igSetNextWindowPos(vp->Pos, NULL, (ImVec2) { 0, 0 });
igSetNextWindowSize(vp->Size, NULL);
/*
* Choix du fichier
*
* Bouton enregistrement
*
* Bouton de rewind (bloqué si pas de fichiers)
*
* Liste déroulantes des fenetres pour savoir faire les clics
*/
igText("Fichier : %s", ptr->name);
igBegin("Test", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration);
igText("Test");
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)
{
fprintf(stderr, "listen a eu un soucis.");
return -1;
}
}
else
{
if (igButton("Lancer l'enregistrement.", (ImVec2) { 0, 0 }))
{
ptr->is_recording = 1;
}
}
igEnd();
return 0;
}

14
src/ui.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef UI
#define UI
typedef struct {
char *name;
FILE *file;
char is_recording;
} ui_infos;
ui_infos * init_ui(char *name);
void free_ui(ui_infos *ptr);
int draw_ui(ui_infos *ptr);
#endif