Commit 833d8b5c247ba463c8d3b36a09daa44dae02e8df
1 parent
37ad659b
Exists in
master
and in
3 other branches
Starting use of events to signal states.
Showing
4 changed files
with
88 additions
and
0 deletions
Show diff stats
src/include/lib3270++.h
... | ... | @@ -64,6 +64,36 @@ |
64 | 64 | |
65 | 65 | class Host; |
66 | 66 | |
67 | + class Event { | |
68 | + public: | |
69 | + enum Type : uint8_t { | |
70 | + All, ///< @brief All events (undefined). | |
71 | + Popup, ///< @brief Popup message. | |
72 | + Trace, ///< @brief Trace message. | |
73 | + Message, ///< @brief Generic message. | |
74 | + }; | |
75 | + | |
76 | + private: | |
77 | + Type type; | |
78 | + | |
79 | + protected: | |
80 | + Event(enum Type type) { | |
81 | + this->type = type; | |
82 | + } | |
83 | + | |
84 | + public: | |
85 | + virtual ~Event(); | |
86 | + | |
87 | + /// @brief Check event type | |
88 | + inline bool is(Event::Type type) const noexcept { | |
89 | + return this->type == type; | |
90 | + } | |
91 | + | |
92 | + /// @brief Get event description. | |
93 | + virtual std::string toString() const = 0; | |
94 | + | |
95 | + }; | |
96 | + | |
67 | 97 | enum ProgramMessage : uint8_t { |
68 | 98 | MESSAGE_NONE = LIB3270_MESSAGE_NONE, ///< @brief No message |
69 | 99 | MESSAGE_SYSWAIT = LIB3270_MESSAGE_SYSWAIT, ///< @brief -- |
... | ... | @@ -144,6 +174,8 @@ |
144 | 174 | /// @brief Write error to log file. |
145 | 175 | void error(const char *fmt, ...) const; |
146 | 176 | |
177 | + void fire(const Event &event); | |
178 | + | |
147 | 179 | public: |
148 | 180 | |
149 | 181 | /// @brief Create a tn3270 session. | ... | ... |
src/lib3270++/lib3270++.cbp
... | ... | @@ -41,6 +41,7 @@ |
41 | 41 | </Linker> |
42 | 42 | <Unit filename="../include/lib3270++.h" /> |
43 | 43 | <Unit filename="../include/lib3270.h" /> |
44 | + <Unit filename="../include/lib3270/popup.h" /> | |
44 | 45 | <Unit filename="../include/lib3270/session.h" /> |
45 | 46 | <Unit filename="abstract.cc" /> |
46 | 47 | <Unit filename="host.cc" /> | ... | ... |
src/lib3270++/local.cc
... | ... | @@ -39,6 +39,7 @@ |
39 | 39 | #include "private.h" |
40 | 40 | #include <lib3270/actions.h> |
41 | 41 | |
42 | + using std::string; | |
42 | 43 | |
43 | 44 | /*---[ Implement ]----------------------------------------------------------------------------------*/ |
44 | 45 | |
... | ... | @@ -52,6 +53,56 @@ |
52 | 53 | lib3270_set_user_data(this->hSession,(void *) this); |
53 | 54 | setCharSet(lib3270_get_display_charset(this->hSession)); |
54 | 55 | |
56 | + lib3270_set_popup_handler(this->hSession, popupHandler); | |
57 | + | |
58 | + } | |
59 | + | |
60 | + /// @brief Popup Handler. | |
61 | + int LocalSession::popupHandler(H3270 *h3270, LIB3270_NOTIFY type, const char *title, const char *msg, const char *fmt, va_list arg) { | |
62 | + | |
63 | + LocalSession * session = (LocalSession *) lib3270_get_user_data(h3270); | |
64 | + | |
65 | + if(!session) { | |
66 | + throw std::runtime_error("Invalid session handler"); | |
67 | + } | |
68 | + | |
69 | + class PopupEvent : public Event { | |
70 | + private: | |
71 | + LIB3270_NOTIFY type; | |
72 | + string title; | |
73 | + string msg; | |
74 | + string description; | |
75 | + | |
76 | + public: | |
77 | + PopupEvent(LIB3270_NOTIFY type, const char *title, const char *msg, const char *fmt, va_list arg) : Event(Event::Popup) { | |
78 | + | |
79 | + this->type = type; | |
80 | + this->title = title; | |
81 | + this->msg = msg; | |
82 | + | |
83 | + char * buffer = NULL; | |
84 | + if(vasprintf(&buffer,fmt,arg) != -1) { | |
85 | + this->description = buffer; | |
86 | + free(buffer); | |
87 | + } | |
88 | + | |
89 | + } | |
90 | + | |
91 | + virtual ~PopupEvent() { | |
92 | + } | |
93 | + | |
94 | + /// @brief Get event description. | |
95 | + std::string toString() const override { | |
96 | + return msg; | |
97 | + } | |
98 | + | |
99 | + | |
100 | + }; | |
101 | + | |
102 | + session->fire(PopupEvent(type,title,msg,fmt,arg)); | |
103 | + | |
104 | + return 0; | |
105 | + | |
55 | 106 | } |
56 | 107 | |
57 | 108 | LocalSession::~LocalSession() { | ... | ... |
src/lib3270++/private.h
... | ... | @@ -43,6 +43,7 @@ |
43 | 43 | #include <config.h> |
44 | 44 | #include <mutex> |
45 | 45 | #include <lib3270++.h> |
46 | + #include <lib3270/popup.h> | |
46 | 47 | #include <system_error> |
47 | 48 | |
48 | 49 | |
... | ... | @@ -108,6 +109,9 @@ |
108 | 109 | /// @brief Mutex to serialize access to lib3270 |
109 | 110 | std::mutex sync; |
110 | 111 | |
112 | + /// @brief Popup Handler. | |
113 | + static int popupHandler(H3270 *session, LIB3270_NOTIFY type, const char *title, const char *msg, const char *fmt, va_list arg); | |
114 | + | |
111 | 115 | public: |
112 | 116 | LocalSession(); |
113 | 117 | virtual ~LocalSession(); | ... | ... |