blob: 9ad8bc754ff4a4e9e5c36040eacaeb3c250065c2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <assert.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#ifdef POSIX
33#include <signal.h>
34#include <termios.h>
35#include <unistd.h>
36#endif // POSIX
pbos@webrtc.org371243d2014-03-07 15:22:04 +000037
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/base/logging.h"
39#include "talk/base/messagequeue.h"
40#include "talk/base/stringutils.h"
41#include "talk/examples/call/console.h"
42#include "talk/examples/call/callclient.h"
43
44#ifdef POSIX
45static void DoNothing(int unused) {}
46#endif
47
48Console::Console(talk_base::Thread *thread, CallClient *client) :
49 client_(client),
50 client_thread_(thread),
51 console_thread_(new talk_base::Thread()) {}
52
53Console::~Console() {
54 Stop();
55}
56
57void Console::Start() {
58 if (!console_thread_) {
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_->started()) {
64 LOG(LS_WARNING) << "Already started";
65 return;
66 }
67 console_thread_->Start();
68 console_thread_->Post(this, MSG_START);
69}
70
71void Console::Stop() {
72 if (console_thread_ && console_thread_->started()) {
73#ifdef WIN32
74 CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
75#else
76 close(fileno(stdin));
77 // This forces the read() in fgets() to return with errno = EINTR. fgets()
78 // will retry the read() and fail, thus returning.
79 pthread_kill(console_thread_->GetPThread(), SIGUSR1);
80#endif
81 console_thread_->Stop();
82 console_thread_.reset();
83 }
84}
85
86void Console::SetEcho(bool on) {
87#ifdef WIN32
88 HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
89 if ((hIn == INVALID_HANDLE_VALUE) || (hIn == NULL))
90 return;
91
92 DWORD mode;
93 if (!GetConsoleMode(hIn, &mode))
94 return;
95
96 if (on) {
97 mode = mode | ENABLE_ECHO_INPUT;
98 } else {
99 mode = mode & ~ENABLE_ECHO_INPUT;
100 }
101
102 SetConsoleMode(hIn, mode);
103#else
104 const int fd = fileno(stdin);
105 if (fd == -1)
106 return;
107
108 struct termios tcflags;
109 if (tcgetattr(fd, &tcflags) == -1)
110 return;
111
112 if (on) {
113 tcflags.c_lflag |= ECHO;
114 } else {
115 tcflags.c_lflag &= ~ECHO;
116 }
117
118 tcsetattr(fd, TCSANOW, &tcflags);
119#endif
120}
121
122void Console::PrintLine(const char* format, ...) {
123 va_list ap;
124 va_start(ap, format);
125
126 char buf[4096];
127 int size = vsnprintf(buf, sizeof(buf), format, ap);
128 assert(size >= 0);
129 assert(size < static_cast<int>(sizeof(buf)));
130 buf[size] = '\0';
131 printf("%s\n", buf);
132 fflush(stdout);
133
134 va_end(ap);
135}
136
137void Console::RunConsole() {
138 char input_buffer[128];
139 while (fgets(input_buffer, sizeof(input_buffer), stdin) != NULL) {
140 client_thread_->Post(this, MSG_INPUT,
141 new talk_base::TypedMessageData<std::string>(input_buffer));
142 }
143}
144
145void Console::OnMessage(talk_base::Message *msg) {
146 switch (msg->message_id) {
147 case MSG_START:
148#ifdef POSIX
149 // Install a no-op signal so that we can abort RunConsole() by raising
150 // SIGUSR1.
151 struct sigaction act;
152 act.sa_handler = &DoNothing;
153 sigemptyset(&act.sa_mask);
154 act.sa_flags = 0;
155 if (sigaction(SIGUSR1, &act, NULL) < 0) {
156 LOG(LS_WARNING) << "Can't install signal";
157 }
158#endif
159 RunConsole();
160 break;
161 case MSG_INPUT:
162 talk_base::TypedMessageData<std::string> *data =
163 static_cast<talk_base::TypedMessageData<std::string>*>(msg->pdata);
164 client_->ParseLine(data->data());
165 break;
166 }
167}