Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSDedicated.cc @ 3343

Last change on this file since 3343 was 3341, checked in by rgrieder, 15 years ago

Dedicated input thread now ends without hitting enter on Unix

  • Property svn:eol-style set to native
File size: 10.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      Fabian 'x3n' Landau
26 *
27 */
28
29#include "GSDedicated.h"
30
31#include "util/Debug.h"
32#include "util/Sleep.h"
33#include "core/Clock.h"
34#include "core/CommandLine.h"
35#include "core/CommandExecutor.h"
36#include "core/Game.h"
37#include "core/GameMode.h"
38#include "network/Server.h"
39
40#include <iostream>
41#include <iomanip>
42#include <boost/bind.hpp>
43
44#ifdef ORXONOX_PLATFORM_UNIX
45#include <termios.h>
46#endif
47
48
49namespace orxonox
50{
51    const unsigned int MAX_COMMAND_LENGTH = 255;
52   
53    DeclareGameState(GSDedicated, "dedicated", false, false);
54   
55    termios* GSDedicated::originalTerminalSettings_;
56
57    GSDedicated::GSDedicated(const GameStateConstrParams& params)
58        : GameState(params)
59        , server_(0)
60        , closeThread_(false)
61        , cleanLine_(true)
62        , inputIterator_(0)
63        , cursorX_(0)
64        , cursorY_(0)
65    {
66    }
67
68    GSDedicated::~GSDedicated()
69    {
70    }
71
72    void GSDedicated::activate()
73    {
74        GameMode::setHasServer(true);
75       
76        this->inputThread_ = new boost::thread(boost::bind(&GSDedicated::inputThread, this));
77       
78#ifndef ORXONOX_PLATFORM_WINDOWS
79        this->originalTerminalSettings_ = new termios;
80        this->setTerminalMode();
81#endif
82
83        this->server_ = new Server(CommandLine::getValue("port"));
84        COUT(0) << "Loading scene in server mode" << std::endl;
85
86        server_->open();
87    }
88
89    void GSDedicated::deactivate()
90    {
91        this->server_->close();
92        delete this->server_;
93       
94        closeThread_ = true;
95#ifdef ORXONOX_PLATFORM_UNIX
96        std::cout << "\033[0G\033[K";
97        std::cout.flush();
98        resetTerminalMode();
99        delete this->originalTerminalSettings_;
100#else
101        COUT(0) << "Press enter to end the game..." << std::endl;
102#endif
103        inputThread_->join();
104        delete this->inputThread_;
105
106        GameMode::setHasServer(false);
107    }
108
109    void GSDedicated::update(const Clock& time)
110    {
111        server_->update(time);
112        processQueue();
113        printLine();
114    }
115   
116    void GSDedicated::inputThread()
117    {
118        this->commandLine_ = new unsigned char[MAX_COMMAND_LENGTH];
119//         memset( this->commandLine_, 0, MAX_COMMAND_LENGTH );
120        unsigned char c;
121        unsigned int  escapeChar=0;
122        while(!closeThread_)
123        {
124            size_t count = read(STDIN_FILENO, &c, 1);
125            if (count == 1)
126            {
127//                 boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
128                if ( inputIterator_>=MAX_COMMAND_LENGTH-1 && c!='\n' )
129                    continue;
130                if( escapeChar > 0 )
131                {
132                    if( c == '[' )
133                    {
134                        escapeChar = 2;
135                        continue;
136                    }
137                    else if ( escapeChar == 2 )
138                    {
139                        switch (c)
140                        {
141                            case 'A': //keyup
142                               
143                                break;
144                            case 'B': //keydown
145                               
146                                break;
147                            case 'C': //keyright
148                                if(cursorX_<inputIterator_)
149                                    ++cursorX_;
150                                break;
151                            case 'D': //keyleft
152                                if(cursorX_>0)
153                                    --cursorX_;
154                                break;
155                            default: //not supported...
156//                                 std::cout << endl << c << endl;
157                                break;
158                        }
159                        escapeChar = 0;
160                    }
161                }
162                else // not in escape sequence mode
163                {
164                    switch (c)
165                    {
166                        case '\n':
167                            this->cleanLine_ = true;
168                            {
169                                boost::recursive_mutex::scoped_lock(this->inputQueueMutex_);
170                                boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
171                                this->commandQueue_.push( std::string((const char*)this->commandLine_,inputIterator_) );
172                            }
173                            memset( this->commandLine_, 0, inputIterator_ );
174                            inputIterator_ = 0;
175                            this->cursorX_ = 0;
176                            this->cursorY_ = 0;
177                            std::cout << endl;
178                            break;
179                        case 127: // backspace
180                        case '\b':
181                            deleteCharacter( this->cursorX_ );
182                            break;
183                        case '\t':
184                        {
185//                             boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
186                            std::cout << endl << CommandExecutor::hint( std::string((const char*)this->commandLine_,inputIterator_) ) << endl;
187                            strncpy(reinterpret_cast<char*>(this->commandLine_), CommandExecutor::complete( std::string(reinterpret_cast<char*>(this->commandLine_),inputIterator_) ).c_str(), MAX_COMMAND_LENGTH);
188                            this->inputIterator_ = strlen((const char*)this->commandLine_);
189                            this->cursorX_ = this->inputIterator_;
190                            break;
191                        }
192                        case '\033': // 1. escape character
193                            escapeChar = 1;
194                            break;
195                        default:
196                            insertCharacter( this->cursorX_, c );
197                            break;
198                    }
199                }
200            }
201        }
202
203        delete[] this->commandLine_;
204    }
205   
206    void GSDedicated::printLine()
207    {
208#ifdef ORXONOX_PLATFORM_UNIX
209        // set cursor to the begining of the line and erase the line
210        std::cout << "\033[0G\033[K";
211//         boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
212        // print status line
213        std::cout << std::fixed << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgFPS() << " fps, " << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgTickTime() << " ms avg ticktime # ";
214        //save cursor position
215        std::cout << "\033[s";
216        //print commandLine buffer
217        std::cout << std::string((const char*)this->commandLine_, inputIterator_);
218        //restore cursor position and move it cursorX_ to the right
219        std::cout << "\033[u";
220        if( this->cursorX_ > 0 )
221            std::cout << "\033[" << this->cursorX_ << "C";
222        std::cout.flush();
223#endif
224    }
225   
226    void GSDedicated::processQueue()
227    {
228        std::string tempstr;
229        {
230            boost::recursive_mutex::scoped_lock lock1(this->inputQueueMutex_);
231            while(true)
232            {
233                if ( !this->commandQueue_.empty() )
234                {
235                    tempstr = this->commandQueue_.front();
236                    this->commandQueue_.pop();
237                    lock1.unlock();
238                }
239                else
240                    break;
241                CommandExecutor::execute(tempstr, true);
242            }
243        }
244    }
245   
246    void GSDedicated::setTerminalMode()
247    {
248#ifdef ORXONOX_PLATFORM_UNIX
249        termios new_settings;
250     
251        tcgetattr(0,this->originalTerminalSettings_);
252        new_settings = *this->originalTerminalSettings_;
253        new_settings.c_lflag &= ~( ICANON | ECHO );
254//         new_settings.c_lflag |= ( ISIG | IEXTEN );
255        new_settings.c_cc[VTIME] = 1;
256        new_settings.c_cc[VMIN] = 0;
257        tcsetattr(0,TCSANOW,&new_settings);
258        COUT(0) << endl;
259//       atexit(&GSDedicated::resetTerminalMode);
260#endif
261    }
262   
263    void GSDedicated::resetTerminalMode()
264    {
265#ifdef ORXONOX_PLATFORM_UNIX
266        tcsetattr(0, TCSANOW, GSDedicated::originalTerminalSettings_);
267#endif
268    }
269   
270    void GSDedicated::insertCharacter( unsigned int position, char c )
271    {
272//         std::cout << endl << static_cast<unsigned int>(c) << endl;
273        // check that we do not exceed MAX_COMMAND_LENGTH
274        if( inputIterator_+1 < MAX_COMMAND_LENGTH )
275        {
276            // if cursor not at end of line then move the rest of the line
277            if( position != this->inputIterator_ )
278                    memmove( this->commandLine_+position+1, this->commandLine_+position, this->inputIterator_-position);
279//             boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
280            this->commandLine_[position] = c;
281            ++this->cursorX_;
282            ++this->inputIterator_;
283        }
284    }
285    void GSDedicated::deleteCharacter( unsigned int position )
286    {
287//         boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
288        if ( this->inputIterator_>0 && position>0 )
289        {
290            if ( position != this->inputIterator_ )
291                memmove( this->commandLine_+position-1, this->commandLine_+position, this->inputIterator_-position);
292            --this->cursorX_;
293            --this->inputIterator_;
294        }
295    }
296   
297}
Note: See TracBrowser for help on using the repository browser.