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