Kactus2
Kactus2 reference guide
Loading...
Searching...
No Matches
StdInputListener.cpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2// File: InputListener.cpp
3//-----------------------------------------------------------------------------
4// Project: Kactus2
5// Author: Esko Pekkarinen
6// Date: 04.02.2021
7//
8// Description:
9// <Short description of the class/file contents>
10//-----------------------------------------------------------------------------
11#define PY_SSIZE_T_CLEAN
12#include <Python.h>
13
14#include "StdInputListener.h"
15
17
18#include <QString>
19
20#include <iostream>
21
22#include <string>
23
24StdInputListener::StdInputListener(WriteChannel* outputChannel, QObject* parent /*= nullptr*/) :
25 QObject(parent),
26 outputChannel_(outputChannel),
27#ifdef Q_OS_WIN
28 notifier_(new QWinEventNotifier(GetStdHandle(STD_INPUT_HANDLE), this))
29{
30 QObject::connect(notifier_, &QWinEventNotifier::activated, this, &StdInputListener::inputReadable);
31
32#else
33 notifier_(new QSocketNotifier(fileno(stdin), QSocketNotifier::Read))
34{
35 connect(notifier_, &QSocketNotifier::activated, this, &StdInputListener::inputReadable);
36#endif
37}
38
40{
41 if (std::cin.eof()) // Ctrl-D pressed
42 {
43 emit inputFailure();
44 return;
45 }
46
47 std::string line;
48 std::getline(std::cin, line);
49
50 outputChannel_->write(QString::fromStdString(line));
51}
StdInputListener(WriteChannel *outputChannel, QObject *parent=nullptr)
The constructor.