OpenClonk
C4StdInProc.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005, Peter Wortmann
5  * Copyright (c) 2005-2006, Günther Brammer
6  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 #include "C4Include.h"
19 #include "platform/C4StdInProc.h"
20 
21 #include "game/C4Application.h"
22 
23 #ifdef HAVE_LIBREADLINE
24 #include <readline.h>
25 #include <history.h>
26 
27 static void readline_callback (char * line)
28 {
29  if (!line)
30  {
31  Application.Quit();
32  }
33  else
34  {
35  Application.OnCommand(line);
36  }
37  if (line && *line)
38  {
39  add_history (line);
40  }
41  free(line);
42 }
43 
45 {
46  rl_callback_handler_install ("", readline_callback);
47 }
48 
50 {
51  rl_callback_handler_remove();
52 }
53 
54 bool C4StdInProc::Execute(int iTimeout, pollfd *)
55 {
56  rl_callback_read_char();
57  return true;
58 }
59 
60 #else
61 
62 C4StdInProc::C4StdInProc() = default;
63 
64 C4StdInProc::~C4StdInProc() = default;
65 
66 bool C4StdInProc::Execute(int iTimeout, pollfd *)
67 {
68  // Surely not the most efficient way to do it, but we won't have to read much data anyway.
69  char c;
70  if (read(0, &c, 1) != 1)
71  {
72  Application.Quit();
73  return false;
74  }
75  if (c == '\n')
76  {
77  if (!CmdBuf.isNull())
78  {
79  Application.OnCommand(CmdBuf.getData());
80  CmdBuf.Clear();
81  }
82  }
83  else if (isprint((unsigned char)c))
84  CmdBuf.AppendChar(c);
85  return true;
86 }
87 
88 #endif /* HAVE_LIBREADLINE */
C4Application Application
Definition: C4Globals.cpp:44
void Quit() override
void OnCommand(const char *szCmd) override
bool Execute(int iTimeout, pollfd *) override
Definition: C4StdInProc.cpp:66
~C4StdInProc() override
const char * getData() const
Definition: StdBuf.h:442
bool isNull() const
Definition: StdBuf.h:441
void AppendChar(char cChar)
Definition: StdBuf.h:588
void Clear()
Definition: StdBuf.h:466