The Pedigree Project  0.1
Console.h
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 #ifndef CONSOLE_H
21 #define CONSOLE_H
22 
23 #include "ConsoleDefines.h"
24 #include "modules/system/vfs/File.h"
25 #include "modules/system/vfs/Filesystem.h"
26 #include "pedigree/kernel/Spinlock.h"
27 #include "pedigree/kernel/compiler.h"
28 #include "pedigree/kernel/process/Mutex.h"
29 #include "pedigree/kernel/processor/types.h"
30 #include "pedigree/kernel/utilities/Buffer.h"
31 #include "pedigree/kernel/utilities/String.h"
32 #include "pedigree/kernel/utilities/Vector.h"
33 #include "pedigree/kernel/utilities/utility.h"
34 
35 class Disk;
36 class Event;
37 class Process;
38 class RequestQueue;
39 
40 #define DEFAULT_FLAGS \
41  (ConsoleManager::OPostProcess | ConsoleManager::IMapCRToNL | \
42  ConsoleManager::OMapNLToCRNL | ConsoleManager::LEcho | \
43  ConsoleManager::LEchoErase | ConsoleManager::LEchoKill | \
44  ConsoleManager::LCookedMode | ConsoleManager::LGenerateEvent)
45 
46 class ConsoleFile : public File
47 {
48  friend class ConsoleMasterFile;
49  friend class ConsoleSlaveFile;
50  friend class ConsoleManager;
51 
52  public:
53  ConsoleFile(size_t consoleNumber, String consoleName, Filesystem *pFs);
54  virtual ~ConsoleFile()
55  {
56  }
57 
58  virtual bool isMaster() = 0;
59 
60  void setEvent(Event *e)
61  {
62  if (isMaster())
63  m_pOther->m_pEvent = e;
64  else
65  m_pEvent = e;
66  }
67 
75  virtual char getLast()
76  {
77  return m_Last;
78  }
79 
80  Event *getEvent() const
81  {
82  return m_pEvent;
83  }
84 
86  void getControlCharacters(char *out)
87  {
88  MemoryCopy(out, m_ControlChars, MAX_CONTROL_CHAR);
89  }
90 
91  virtual size_t getBlockSize() const
92  {
93  return PTY_BUFFER_SIZE;
94  }
95 
96  size_t getConsoleNumber() const
97  {
98  return m_ConsoleNumber;
99  }
100 
101  virtual size_t getPhysicalConsoleNumber() const
102  {
103  return ~0U;
104  }
105 
111  virtual void eventComplete()
112  {
113  if (!isMaster())
115  else
117  }
118 
119  protected:
121  virtual int select(bool bWriting, int timeout);
122 
124  void inject(char *buf, size_t len, bool canBlock);
125 
128  virtual void performInject(char *buf, size_t len, bool canBlock);
129 
132  virtual void performEventTrigger(char cause);
133 
136 
137  size_t m_Flags;
138  char m_ControlChars[MAX_CONTROL_CHAR];
139 
140  unsigned short m_Rows;
141  unsigned short m_Cols;
142 
144  static size_t
145  outputLineDiscipline(char *buf, size_t len, size_t maxSz, size_t flags = 0);
146 
148  size_t processInput(char *buf, size_t len);
149 
151  void inputLineDiscipline(
152  char *buf, size_t len, size_t flags = ~0U,
153  const char *controlChars = 0);
154 
156  char m_LineBuffer[LINEBUFFER_MAXIMUM];
157 
160 
163 
165  char m_Last;
166 
167  Buffer<char> m_Buffer;
168 
169  private:
170  size_t m_ConsoleNumber;
171  String m_Name;
172 
179 
182 
184  bool checkForEvent(size_t flags, char check, const char *controlChars);
185 
187  void triggerEvent(char cause);
188 
189  virtual bool isBytewise() const
190  {
191  return true;
192  }
193 };
194 
196 {
197  public:
199  size_t consoleNumber, String consoleName, Filesystem *pFs);
200  virtual ~ConsoleMasterFile()
201  {
202  }
203 
204  virtual uint64_t readBytewise(
205  uint64_t location, uint64_t size, uintptr_t buffer,
206  bool bCanBlock = true);
207  virtual uint64_t writeBytewise(
208  uint64_t location, uint64_t size, uintptr_t buffer,
209  bool bCanBlock = true);
210 
211  void setOther(ConsoleFile *pOther)
212  {
213  m_pOther = pOther;
214  m_Flags = pOther->m_Flags;
215  }
216 
218  bool bLocked;
219 
223 
224  virtual bool isMaster()
225  {
226  return true;
227  }
228 
229  private:
230 };
231 
233 {
234  public:
235  ConsoleSlaveFile(size_t consoleNumber, String consoleName, Filesystem *pFs);
236  virtual ~ConsoleSlaveFile()
237  {
238  }
239 
240  virtual uint64_t readBytewise(
241  uint64_t location, uint64_t size, uintptr_t buffer,
242  bool bCanBlock = true);
243  virtual uint64_t writeBytewise(
244  uint64_t location, uint64_t size, uintptr_t buffer,
245  bool bCanBlock = true);
246 
247  void setOther(ConsoleFile *pOther)
248  {
249  m_pOther = pOther;
250  }
251 
252  virtual bool isMaster()
253  {
254  return false;
255  }
256 
257  virtual char getLast()
258  {
259  return m_pOther->getLast();
260  }
261 };
262 
264 {
265  public:
267  size_t nth, File *pTerminal, String consoleName, Filesystem *pFs);
268  virtual ~ConsolePhysicalFile()
269  {
270  }
271 
272  virtual uint64_t readBytewise(
273  uint64_t location, uint64_t size, uintptr_t buffer,
274  bool bCanBlock = true);
275  virtual uint64_t writeBytewise(
276  uint64_t location, uint64_t size, uintptr_t buffer,
277  bool bCanBlock = true);
278 
279  virtual bool isMaster()
280  {
281  return false;
282  }
283 
284  virtual char getLast()
285  {
287  return '\0';
288  }
289 
290  virtual size_t getPhysicalConsoleNumber() const
291  {
292  return m_TerminalNumber;
293  }
294 
295  virtual int select(bool bWriting, int timeout);
296 
297  private:
298  File *m_pTerminal;
299  Buffer<char> m_ProcessedInput;
300  size_t m_TerminalNumber;
301 
302  virtual void performInject(char *buf, size_t len, bool canBlock);
303 };
304 
317 {
318  public:
319  enum IAttribute
320  {
321  IMapCRToNL = 1,
322  IIgnoreCR = 2,
323  IMapNLToCR = 4,
324  IStripToSevenBits = 8
325  };
326  enum OAttribute
327  {
328  OPostProcess = 16,
329  OMapCRToNL = 32,
330  ONoCrAtCol0 = 64,
331  OMapNLToCRNL = 128,
332  ONLCausesCR = 256
333  };
334  enum LAttribute
335  {
336  LEcho = 512,
337  LEchoErase = 1024,
338  LEchoKill = 2048,
339  LEchoNewline = 4096,
340  LCookedMode = 8192,
341  LGenerateEvent = 16384
342  };
343 
344  ConsoleManager();
345 
346  virtual ~ConsoleManager();
347 
348  static ConsoleManager &instance();
349 
350  //
351  // ConsoleManager interface.
352  //
353  File *getConsole(String consoleName);
354  ConsoleFile *getConsoleFile(RequestQueue *pBackend);
355 
358  bool lockConsole(File *file);
359 
361  void unlockConsole(File *file);
362 
365  void newConsole(char c, size_t i);
366 
367  bool isConsole(File *file);
368  bool isMasterConsole(File *file);
369 
370  void setAttributes(File *file, size_t flags);
371  void getAttributes(File *file, size_t *flags);
372  void getControlChars(File *file, void *p);
373  void setControlChars(File *file, void *p);
374  int getWindowSize(File *file, unsigned short *rows, unsigned short *cols);
375  int setWindowSize(File *file, unsigned short rows, unsigned short cols);
376  bool hasDataAvailable(File *file);
377  void flush(File *file);
378 
379  File *getOther(File *file);
380 
381  //
382  // Filesystem interface.
383  //
384 
385  virtual bool initialise(Disk *pDisk)
386  {
387  return false;
388  }
389  virtual File *getRoot() const
390  {
391  return 0;
392  }
393  virtual String getVolumeLabel() const
394  {
395  return String("consolemanager");
396  }
397 
398  protected:
399  virtual bool createFile(File *parent, const String &filename, uint32_t mask)
400  {
401  return false;
402  }
403  virtual bool
404  createDirectory(File *parent, const String &filename, uint32_t mask)
405  {
406  return false;
407  }
408  virtual bool
409  createSymlink(File *parent, const String &filename, const String &value)
410  {
411  return false;
412  }
413  virtual bool remove(File *parent, File *file)
414  {
415  return false;
416  }
417 
418  private:
419  Vector<ConsoleFile *> m_Consoles;
420  static ConsoleManager m_Instance;
421  Spinlock m_Lock;
422 
423  void newConsole(char c, size_t i, bool lock);
424 };
425 
426 #endif
size_t m_LineBufferSize
Size of the input line buffer.
Definition: Console.h:159
static size_t outputLineDiscipline(char *buf, size_t len, size_t maxSz, size_t flags=0)
Output line discipline.
virtual File * getRoot() const
Definition: Console.h:389
virtual bool isBytewise() const
Definition: Console.h:189
virtual void eventComplete()
Definition: Console.h:111
virtual uint64_t readBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
Definition: File.cc:582
Definition: Mutex.h:58
size_t m_LineBufferFirstNewline
Location of the first newline in the line buffer. ~0 if none.
Definition: Console.h:162
Process * pLocker
Definition: Console.h:222
Definition: String.h:49
virtual bool createDirectory(File *parent, const String &filename, uint32_t mask)
Definition: Console.h:404
Definition: Disk.h:32
size_t processInput(char *buf, size_t len)
Input processing.
bool checkForEvent(size_t flags, char check, const char *controlChars)
Check if the given character requires an event.
ConsoleFile * m_pOther
Other side of the console.
Definition: Console.h:135
virtual void performEventTrigger(char cause)
virtual bool initialise(Disk *pDisk)
Definition: Console.h:385
void release(size_t n=1)
Definition: Semaphore.cc:239
virtual bool createFile(File *parent, const String &filename, uint32_t mask)
Definition: Console.h:399
virtual int select(bool bWriting, int timeout)
select - check and optionally for a particular state.
void inputLineDiscipline(char *buf, size_t len, size_t flags=~0U, const char *controlChars=0)
Input line discipline.
Event * m_pEvent
Definition: Console.h:178
void inject(char *buf, size_t len, bool canBlock)
inject - inject bytes into the ring buffer
char m_Last
Character that triggered an event.
Definition: Console.h:165
void getControlCharacters(char *out)
Grabs the current array of control characters.
Definition: Console.h:86
virtual size_t getBlockSize() const
Definition: Console.h:91
void triggerEvent(char cause)
Triggers our event.
Definition: Event.h:48
bool bLocked
Is this master locked (ie, already opened)?
Definition: Console.h:218
virtual char getLast()
Definition: Console.h:75
virtual char getLast()
Definition: Console.h:257
virtual void performInject(char *buf, size_t len, bool canBlock)
Definition: File.h:66
virtual char getLast()
Definition: Console.h:284
virtual uint64_t writeBytewise(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
Definition: File.cc:592
Mutex m_EventTrigger
Locked when we trigger an event, unlocked when eventComplete called.
Definition: Console.h:181
virtual bool createSymlink(File *parent, const String &filename, const String &value)
Definition: Console.h:409
virtual String getVolumeLabel() const
Definition: Console.h:393
char m_LineBuffer[LINEBUFFER_MAXIMUM]
Input line buffer.
Definition: Console.h:156