Parcourir la source

Added first cli test interaction

Cyamond il y a 5 ans
Parent
commit
76646d7db7
3 fichiers modifiés avec 56 ajouts et 13 suppressions
  1. 42 0
      gui/src/main.cpp
  2. 12 7
      gui/src/qmlhandler.cpp
  3. 2 6
      gui/src/qmlhandler.h

+ 42 - 0
gui/src/main.cpp

@@ -1,11 +1,51 @@
 #include <QGuiApplication>
 #include <QObject>
 #include <QQmlApplicationEngine>
+#include <csignal>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
 #include <iostream>
+#include <sys/prctl.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
 #include "qmlhandler.h"
 
+using namespace std;
+int inpipefd[2];
+int outpipefd[2];
+char buf[1024];
+
 int main(int argc, char *argv[]) {
+  pid_t pid = 0;
+  char msg[256];
+  int status;
+
+  pipe(inpipefd);
+  pipe(outpipefd);
+  pid = fork();
+  if (pid == 0) {
+    // Child
+    dup2(outpipefd[0], STDIN_FILENO);
+    dup2(inpipefd[1], STDOUT_FILENO);
+    dup2(inpipefd[1], STDERR_FILENO);
+
+    // ask kernel to deliver SIGTERM in case the parent dies
+    prctl(PR_SET_PDEATHSIG, SIGTERM);
+
+    // replace tee with your process
+    execl("../../cli/ccats-cli", "h", (char *)NULL);
+    // Nothing below this line should be executed by child process. If so,
+    // it means that the execl function wasn't successfull, so lets exit:
+    exit(1);
+  }
+
+  close(outpipefd[0]);
+  close(inpipefd[1]);
+
+  // ########## GUI CODE; DO NOT TOUCH ##########
+
   QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
 
   QGuiApplication app(argc, argv);
@@ -31,4 +71,6 @@ int main(int argc, char *argv[]) {
                    SLOT(setStatusMessage(QVariant)));
 
   return app.exec();
+
+  // ########## GUI CODE; DO NOT TOUCH ##########
 }

+ 12 - 7
gui/src/qmlhandler.cpp

@@ -1,14 +1,19 @@
-//
-// Created by Tobi on 14.11.2019.
-//
-
 #include "qmlhandler.h"
 
+#include <unistd.h>
+
+extern int *inpipefd;
+extern int *outpipefd;
+extern char *buf;
+
 QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
 
-void QMLHandler::onClick() {
-  qInfo() << "BAM";
+void QMLHandler::onConnectClick() {
   emit setStatusMessage("Connecting");
+  read(inpipefd[0], buf, 1024);
+  buf[1023] = 0;
+  buf[strlen(buf)] = 0;
+  qInfo() << buf;
 }
 
-void QMLHandler::onClick2() { qInfo() << "BAM 2"; }
+void QMLHandler::onDisconnectClick() { qInfo() << "BAM 2"; }

+ 2 - 6
gui/src/qmlhandler.h

@@ -1,7 +1,3 @@
-//
-// Created by Tobi on 14.11.2019.
-//
-
 #ifndef CCATS_GUI_QMLHANDLER_H
 #define CCATS_GUI_QMLHANDLER_H
 
@@ -18,8 +14,8 @@ signals:
   void setStatusMessage(QVariant text);
 
 public slots:
-  void onClick();
-  void onClick2();
+  void onConnectClick();
+  void onDisconnectClick();
 };
 
 #endif // CCATS_GUI_QMLHANDLER_H