1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#define _CRT_SECURE_NO_DEPRECATE 1
29
30#include <assert.h>
31
32#ifdef POSIX
33#include <signal.h>
34#include <termios.h>
35#include <unistd.h>
36#endif  // POSIX
37
38#include "talk/examples/call/callclient.h"
39#include "talk/examples/call/console.h"
40#include "webrtc/base/logging.h"
41#include "webrtc/base/messagequeue.h"
42#include "webrtc/base/stringutils.h"
43
44#ifdef POSIX
45static void DoNothing(int unused) {}
46#endif
47
48Console::Console(rtc::Thread *thread, CallClient *client) :
49  client_(client),
50  client_thread_(thread),
51  stopped_(false) {}
52
53Console::~Console() {
54  Stop();
55}
56
57void Console::Start() {
58  if (stopped_) {
59    // stdin was closed in Stop(), so we can't restart.
60    LOG(LS_ERROR) << "Cannot re-start";
61    return;
62  }
63  if (console_thread_) {
64    LOG(LS_WARNING) << "Already started";
65    return;
66  }
67  console_thread_.reset(new rtc::Thread());
68  console_thread_->Start();
69  console_thread_->Post(this, MSG_START);
70}
71
72void Console::Stop() {
73  if (console_thread_) {
74#ifdef WIN32
75    CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
76#else
77    close(fileno(stdin));
78    // This forces the read() in fgets() to return with errno = EINTR. fgets()
79    // will retry the read() and fail, thus returning.
80    pthread_kill(console_thread_->GetPThread(), SIGUSR1);
81#endif
82    console_thread_->Stop();
83    console_thread_.reset();
84    stopped_ = true;
85  }
86}
87
88void Console::SetEcho(bool on) {
89#ifdef WIN32
90  HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
91  if ((hIn == INVALID_HANDLE_VALUE) || (hIn == NULL))
92    return;
93
94  DWORD mode;
95  if (!GetConsoleMode(hIn, &mode))
96    return;
97
98  if (on) {
99    mode = mode | ENABLE_ECHO_INPUT;
100  } else {
101    mode = mode & ~ENABLE_ECHO_INPUT;
102  }
103
104  SetConsoleMode(hIn, mode);
105#else
106  const int fd = fileno(stdin);
107  if (fd == -1)
108   return;
109
110  struct termios tcflags;
111  if (tcgetattr(fd, &tcflags) == -1)
112    return;
113
114  if (on) {
115    tcflags.c_lflag |= ECHO;
116  } else {
117    tcflags.c_lflag &= ~ECHO;
118  }
119
120  tcsetattr(fd, TCSANOW, &tcflags);
121#endif
122}
123
124void Console::PrintLine(const char* format, ...) {
125  va_list ap;
126  va_start(ap, format);
127
128  char buf[4096];
129  int size = vsnprintf(buf, sizeof(buf), format, ap);
130  assert(size >= 0);
131  assert(size < static_cast<int>(sizeof(buf)));
132  buf[size] = '\0';
133  printf("%s\n", buf);
134  fflush(stdout);
135
136  va_end(ap);
137}
138
139void Console::RunConsole() {
140  char input_buffer[128];
141  while (fgets(input_buffer, sizeof(input_buffer), stdin) != NULL) {
142    client_thread_->Post(this, MSG_INPUT,
143        new rtc::TypedMessageData<std::string>(input_buffer));
144  }
145}
146
147void Console::OnMessage(rtc::Message *msg) {
148  switch (msg->message_id) {
149    case MSG_START:
150#ifdef POSIX
151      // Install a no-op signal so that we can abort RunConsole() by raising
152      // SIGUSR1.
153      struct sigaction act;
154      act.sa_handler = &DoNothing;
155      sigemptyset(&act.sa_mask);
156      act.sa_flags = 0;
157      if (sigaction(SIGUSR1, &act, NULL) < 0) {
158        LOG(LS_WARNING) << "Can't install signal";
159      }
160#endif
161      RunConsole();
162      break;
163    case MSG_INPUT:
164      rtc::TypedMessageData<std::string> *data =
165          static_cast<rtc::TypedMessageData<std::string>*>(msg->pdata);
166      client_->ParseLine(data->data());
167      break;
168  }
169}
170