aboutsummaryrefslogtreecommitdiff
path: root/desktop-utilities/lumina-terminal/TtyProcess.h
blob: 04f294a2fd2f4aa81cb454c32a9ecee2c2b37b37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2015, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
//  This is basically a replacement for QProcess, where all process/terminal outputs
//   are redirected and not just the standard input/output channels. This allows it
//   to be used for terminal-like apps (shells) which directly modify the terminal output
//   rather than stick to input/output channels for communication.
//===========================================
#ifndef _LUMINA_DESKTOP_UTILITIES_TERMINAL_TTY_PROCESS_WIDGET_H
#define _LUMINA_DESKTOP_UTILITIES_TERMINAL_TTY_PROCESS_WIDGET_H

#include <QDebug>
#include <QSocketNotifier>
#include <QKeyEvent>

//Standard C library functions for PTY access/setup
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

class TTYProcess : public QObject{
	Q_OBJECT
public:
	TTYProcess(QObject *parent = 0);
	~TTYProcess();

	bool startTTY(QString prog, QStringList args = QStringList());
	void closeTTY();

	//Primary read/write functions
	void writeTTY(QByteArray output);
	void writeQtKey(int key); //simplification function for handling special keys like arrows and such ( QKeyEvent()->key() )
	QByteArray readTTY();

	//Setup the terminal size (characters and pixels)
	void setTerminalSize(QSize chars, QSize pixels);

	//Status update checks
	bool isOpen();
	
	//Functions for handling ANSI escape codes (typically not used by hand)
	QByteArray CleanANSI(QByteArray, bool &incomplete);

private:
	pid_t childProc;
	int ttyfd;
	QSocketNotifier *sn;
	QByteArray fragBA; //fragment ByteArray
	
	//====================================
	// C Library function for setting up the PTY
	// Inputs:
	//    int &fd:	(output) file descriptor for the master PTY (positive integer if valid)
	//    char *prog:	program to run
	//    char **args:	program arguments
	// Returns:
	//    -1 for errors, child process PID (positive integer) if successful
	//====================================
	static pid_t LaunchProcess(int& fd, char *prog, char **child_args); 
	
private slots:
	void checkStatus(int);

signals:
	void readyRead();
	void processClosed();
};

#endif
bgstack15