wxMaxima
Maxima.h
Go to the documentation of this file.
1 // -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 //
3 // Copyright (C) 2004-2015 Andrej Vodopivec <andrej.vodopivec@gmail.com>
4 // (C) 2014-2018 Gunter Königsmann <wxMaxima@physikbuch.de>
5 // (C) 2020 Kuba Ober <kuba@bertec.com>
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 //
22 // SPDX-License-Identifier: GPL-2.0+
23 
24 #ifndef WXMAXIMA_MAXIMA_H
25 #define WXMAXIMA_MAXIMA_H
26 
32 #include "StreamUtils.h"
33 #include <wx/buffer.h>
34 #include <wx/event.h>
35 #include <wx/sckstrm.h>
36 #include <wx/socket.h>
37 #include <wx/string.h>
38 #include <wx/timer.h>
39 #include <memory>
40 
51 class Maxima : public wxEvtHandler
52 {
53 public:
56  explicit Maxima(wxSocketBase *socket);
57  ~Maxima() override;
58 
59  wxSocketBase *Socket() const { return m_socket.get(); }
60 
61  bool IsConnected() const { return m_socket->IsConnected(); }
62 
63  void SetPipeToStdOut(bool pipe) { m_pipeToStdout = pipe; }
64 
73  bool Write(const void *buffer, size_t length);
74 
77  void ReadSocket();
78 
81  void ClearFirstPrompt() { m_first = false; }
82 
83 private:
85  UTF8Decoder::DecodeResult DecodeFromSocket(size_t maxRead);
86 
88  void SocketEvent(wxSocketEvent &event);
90  void TimerEvent(wxTimerEvent &event);
91 
92  std::unique_ptr<wxSocketBase> m_socket;
93  wxSocketInputStream m_socketInput{*m_socket};
94  UTF8Decoder m_decoder;
95  UTF8Decoder::State m_socketDecodeState;
96  wxString m_socketInputData;
97  wxMemoryBuffer m_socketOutputData;
98 
99  bool m_first = true;
100  bool m_pipeToStdout = false;
101 
102  wxTimer m_stringEndTimer{this};
103  wxTimer m_readIdleTimer{this};
104 };
105 
106 class MaximaEvent : public wxEvent
107 {
108 public:
109  enum Cause {
122  };
123  MaximaEvent(Cause cause, Maxima *source);
124  MaximaEvent(Cause cause, Maxima *source, wxString &&data);
125  wxEvent *Clone() const override;
126  Cause GetCause() const { return m_cause; }
127  Maxima *GetSource() const { return m_source; }
128  const wxString &GetData() const { return m_data; }
129  wxString &GetData() { return m_data; }
130  void SetData(const wxString &data) { m_data = data; }
131 private:
132  Cause m_cause;
133  Maxima *m_source;
134  wxString m_data;
135 };
136 
137 wxDECLARE_EVENT(EVT_MAXIMA, MaximaEvent);
138 
139 #endif
MaximaEvent
Definition: Maxima.h:106
UTF8Decoder::DecodeResult
Definition: StreamUtils.h:71
UTF8Decoder
Definition: StreamUtils.h:68
MaximaEvent::READ_DATA
@ READ_DATA
A complete response has been read from Maxima.
Definition: Maxima.h:113
Maxima::Maxima
Maxima(wxSocketBase *socket)
Definition: Maxima.cpp:43
Maxima::Write
bool Write(const void *buffer, size_t length)
Definition: Maxima.cpp:68
MaximaEvent::READ_TIMEOUT
@ READ_TIMEOUT
Reading from Maxima had timed out while awaiting an end marker - partial response is provided.
Definition: Maxima.h:115
MaximaEvent::WRITE_ERROR
@ WRITE_ERROR
The transmission has failed - this is an unrecoverable error, most likely.
Definition: Maxima.h:121
Maxima::ReadSocket
void ReadSocket()
Definition: Maxima.cpp:151
MaximaEvent::WRITE_PENDING
@ WRITE_PENDING
A write to Maxima is still ongoing. We use this event to keep the traffic indicator alive.
Definition: Maxima.h:119
StreamUtils.h
MaximaEvent::DISCONNECTED
@ DISCONNECTED
Maxima has disconnected (possibly because the process had died).
Definition: Maxima.h:117
MaximaEvent::Cause
Cause
Definition: Maxima.h:109
MaximaEvent::READ_PENDING
@ READ_PENDING
There's still pending data coming from Maxima. The Data member is empty at the moment.
Definition: Maxima.h:111
Maxima::ClearFirstPrompt
void ClearFirstPrompt()
Definition: Maxima.h:81
UTF8Decoder::State
Definition: StreamUtils.h:80
Maxima
Definition: Maxima.h:51