summaryrefslogtreecommitdiff
path: root/codelite_terminal/asyncprocess.h
blob: ac288954684f861b305d1186229bcb1be48cdde5 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2009 by Eran Ifrah
// file name            : asyncprocess.h
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#ifndef I_PROCESS_H
#define I_PROCESS_H

#include <wx/string.h>
#include <wx/event.h>
#include <map>

enum IProcessCreateFlags {
    IProcessCreateDefault             = 0x0000001, // Default: create process with no console window
    IProcessCreateConsole             = 0x0000002, // Create with console window shown
    IProcessCreateWithHiddenConsole   = 0x0000004  // Create process with a hidden console
};

class IProcess;
class IProcessCallback : public wxEvtHandler
{
protected:
    IProcess* m_process;
    
public:
    IProcessCallback() : m_process(NULL) {}
    void SetProcess(IProcess* process) {
        m_process = process;
    }
    virtual void OnProcessOutput(const wxString &str) {
        wxUnusedVar(str);
    }
    virtual void OnProcessTerminated() {}
    virtual void OnProcessStarted(int pid, const wxString &tty) {
        wxUnusedVar(pid);
        wxUnusedVar(tty);
    }
};

/**
 * @class IProcess
 * @author eran
 * @date 10/09/09
 * @file i_process.h
 * @brief
 */
class IProcess
{
protected:
    wxEvtHandler *            m_parent;
    int                       m_pid;
    bool                      m_hardKill;
    IProcessCallback*         m_callback;

public:
    IProcess(wxEvtHandler *parent) : m_parent(parent), m_pid(-1), m_hardKill(false), m_callback(NULL) {}
    virtual ~IProcess() {}

public:
    // Handle process exit code. This is done this way this
    // under Linux / Mac the exit code is returned only after the signal child has been
    // handled by codelite
    static void SetProcessExitCode(int pid, int exitCode);
    static bool GetProcessExitCode(int pid, int &exitCode);
    
    void LinkCallback(IProcessCallback* pcb) {
        m_callback = pcb;
        pcb->SetProcess( this );
    }
    
    void SetCallback(IProcessCallback* callback) {
        this->m_callback = callback;
    }
    // Read from process stdout - return immediately if no data is available
    virtual bool Read(wxString& buff) = 0;

    // Write to the process stdin
    virtual bool Write(const wxString& buff) = 0;

    /**
     * @brief this method is mostly needed on MSW where writing a password
     * is done directly on the console buffer rather than its stdin
     */
    virtual bool WriteToConsole(const wxString &buff) = 0;

    // Return true if the process is still alive
    virtual bool IsAlive() = 0;

    // Clean the process resources and kill the process if it is
    // still alive
    virtual void Cleanup() = 0;

    // Terminate the process. It is recommended to use this method
    // so it will invoke the 'Cleaup' procedure and the process
    // termination event will be sent out
    virtual void Terminate() = 0;

    void SetPid(int pid) {
        this->m_pid = pid;
    }

    int GetPid() const {
        return m_pid;
    }

    void SetHardKill(bool hardKill) {
        this->m_hardKill = hardKill;
    }
    bool GetHardKill() const {
        return m_hardKill;
    }
    IProcessCallback* GetCallback() {
        return m_callback;
    }
};

// Help method
/**
 * @brief start process
 * @param parent the parent. all events will be sent to this object
 * @param cmd command line to execute
 * @param flags possible creation flag
 * @param workingDir set the working directory of the executed process
 * @return
 */
IProcess* CreateAsyncProcess(wxEvtHandler *parent, const wxString& cmd, IProcessCreateFlags flags = IProcessCreateDefault, const wxString &workingDir = wxEmptyString);
/**
 * @brief start process
 * @brief cb callback object. Instead of events, OnProcessOutput and OnProcessTerminated will be called respectively
 * @param parent the parent. all events will be sent to this object
 * @param cmd command line to execute
 * @param flags possible creation flag
 * @param workingDir set the working directory of the executed process
 * @return
 */
IProcess* CreateAsyncProcessCB(wxEvtHandler *parent, IProcessCallback* cb, const wxString& cmd, IProcessCreateFlags flags = IProcessCreateDefault, const wxString &workingDir = wxEmptyString);

#endif // I_PROCESS_H