The Pedigree Project  0.1
user/applications/TUI/main.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <pthread.h>
23 #include <sched.h>
24 #include <signal.h>
25 #include <stddef.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/klog.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 
34 #include "pedigree/native/graphics/Graphics.h"
35 #include "pedigree/native/input/Input.h"
36 
37 #include <Widget.h>
38 
39 #include <tui.h>
40 
42 {
43  public:
44  PedigreeTerminalEmulator() : Widget(), m_nWidth(0), m_nHeight(0){};
45 
46  virtual ~PedigreeTerminalEmulator(){};
47 
48  virtual bool
50  {
51  return true;
52  }
53 
54  void handleReposition(const PedigreeGraphics::Rect &rt)
55  {
56  m_nWidth = rt.getW();
57  m_nHeight = rt.getH();
58  }
59 
60  size_t getWidth() const
61  {
62  return m_nWidth;
63  }
64 
65  size_t getHeight() const
66  {
67  return m_nHeight;
68  }
69 
70  private:
71  size_t m_nWidth;
72  size_t m_nHeight;
73 };
74 
75 static Tui *g_Tui = nullptr;
76 static PedigreeTerminalEmulator *g_pEmu = nullptr;
77 
78 void sigint(int)
79 {
80  klog(LOG_NOTICE, "TUI received SIGINT, oops!");
81 }
82 
83 bool callback(WidgetMessages message, size_t msgSize, const void *msgData)
84 {
85  if (!g_Tui)
86  {
87  return false;
88  }
89 
90  switch (message)
91  {
92  case Reposition:
93  {
94  klog(LOG_INFO, "-- REPOSITION --");
95  const PedigreeGraphics::Rect *rt =
96  reinterpret_cast<const PedigreeGraphics::Rect *>(msgData);
97  klog(LOG_INFO, " -> handling...");
98  g_pEmu->handleReposition(*rt);
99  klog(LOG_INFO, " -> registering the mode change");
100  g_Tui->resize(rt->getW(), rt->getH());
101  klog(LOG_INFO, " -> creating new framebuffer");
102  g_Tui->recreateSurfaces(g_pEmu->getRawFramebuffer());
103  klog(LOG_INFO, " -> reposition complete!");
104  }
105  break;
106  case KeyUp:
107  g_Tui->keyInput(*reinterpret_cast<const uint64_t *>(msgData));
108  break;
109  case Focus:
110  g_Tui->setCursorStyle(true);
111  break;
112  case NoFocus:
113  g_Tui->setCursorStyle(false);
114  break;
115  case RawKeyDown:
116  case RawKeyUp:
117  // Ignore.
118  break;
119  case Terminate:
120  klog(LOG_INFO, "TUI: termination request");
121  g_Tui->stop();
122  break;
123  default:
124  klog(LOG_INFO, "TUI: unhandled callback");
125  }
126 
127  return true;
128 }
129 
130 int main(int argc, char *argv[])
131 {
132 #ifdef TARGET_LINUX
133  openlog("tui", LOG_PID, LOG_USER);
134 #endif
135 
136  klog(LOG_INFO, "I am %d", getpid());
137 
138  char endpoint[256];
139  sprintf(endpoint, "tui.%d", getpid());
140 
142 
143  g_pEmu = new PedigreeTerminalEmulator();
144  g_Tui = new Tui(g_pEmu);
145 
146  klog(LOG_INFO, "TUI: constructing widget '%s'...", endpoint);
147  if (!g_pEmu->construct(endpoint, "Pedigree xterm Emulator", callback, rt))
148  {
149  klog(LOG_ERR, "tui: couldn't construct widget");
150  delete g_Tui;
151  delete g_pEmu;
152  return 1;
153  }
154  klog(LOG_INFO, "TUI: widget constructed!");
155 
156  signal(SIGINT, sigint);
157 
158  // Handle initial reposition event.
160 
161  if (!g_Tui->initialise(g_pEmu->getWidth(), g_pEmu->getHeight()))
162  {
163  return 1;
164  }
165  else
166  {
167  g_Tui->run();
168  }
169 
170  delete g_Tui;
171  delete g_pEmu;
172 
173  return 0;
174 }
Definition: tui.h:36
void run()
Runs the TUI main loop.
Definition: tui.cc:261
static void checkForEvents(bool bAsync=false)
Definition: Widget.cc:339
void recreateSurfaces(void *fb)
Re-create rendering surfaces from the newest framebuffer.
Definition: tui.cc:189
void * getRawFramebuffer()
Definition: Widget.h:142
void setCursorStyle(bool filled)
Set the cursor fill state (e.g. box outline vs shaded box)
Definition: tui.cc:178
void keyInput(uint64_t key)
Handles a key press with all Pedigree input special flags.
Definition: tui.cc:355
bool initialise(size_t width, size_t height)
(Re-)initialise the terminal
Definition: tui.cc:71
Definition: Widget.h:63
void resize(size_t newWidth, size_t newHeight)
Handle a resize of the terminal.
Definition: tui.cc:230
bool construct(const char *endpoint, const char *title, widgetCallback_t cb, PedigreeGraphics::Rect &dimensions)
Definition: Widget.cc:90
void stop()
Stops the TUI main loop.
Definition: tui.cc:350
virtual bool render(PedigreeGraphics::Rect &rt, PedigreeGraphics::Rect &dirty)