1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
/*
This file is part of the KDE games library
Copyright (C) 2001 Martin Heni (kde at heni-online.de)
Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kgameio.h"
#include "kgame.h"
#include "kplayer.h"
#include "kgamemessage.h"
#include "kmessageio.h"
#include <QWidget>
#include <QBuffer>
#include <QTimer>
//Added by qt3to4:
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QEvent>
#include <stdlib.h>
class KGameIOPrivate
{
public:
KGameIOPrivate()
: mPlayer(nullptr)
{
}
KPlayer *mPlayer;
};
// ----------------------- Generic IO -------------------------
KGameIO::KGameIO()
: d(new KGameIOPrivate)
{
qCDebug(GAMES_PRIVATE_KGAME) << ": this=" << this << ", sizeof(this)" << sizeof(KGameIO);
}
KGameIO::KGameIO(KPlayer* player)
: d(new KGameIOPrivate)
{
qCDebug(GAMES_PRIVATE_KGAME) << ": this=" << this << ", sizeof(this)" << sizeof(KGameIO);
if (player)
{
player->addGameIO(this);
}
}
KGameIO::~KGameIO()
{
qCDebug(GAMES_PRIVATE_KGAME) << ": this=" << this;
// unregister ourselves
if (player())
{
player()->removeGameIO(this, false);
}
delete d;
}
KPlayer* KGameIO::player() const
{
return d->mPlayer;
}
void KGameIO::setPlayer(KPlayer *p)
{
d->mPlayer = p;
}
void KGameIO::initIO(KPlayer *p)
{
setPlayer(p);
}
void KGameIO::notifyTurn(bool b)
{
if (!player())
{
qCWarning(GAMES_PRIVATE_KGAME) << ": player() is NULL";
return;
}
bool sendit=false;
QByteArray buffer;
QDataStream stream(&buffer, QIODevice::WriteOnly);
Q_EMIT signalPrepareTurn(stream, b, this, &sendit);
if (sendit)
{
QDataStream ostream(buffer);
quint32 sender = player()->id(); // force correct sender
qCDebug(GAMES_PRIVATE_KGAME) << "Prepare turn sendInput";
sendInput(ostream, true, sender);
}
}
KGame* KGameIO::game() const
{
if (!player())
{
return nullptr;
}
return player()->game();
}
bool KGameIO::sendInput(QDataStream& s, bool transmit, quint32 sender)
{
if (!player())
{
return false;
}
return player()->forwardInput(s, transmit, sender);
}
void KGameIO::Debug()
{
qCDebug(GAMES_PRIVATE_KGAME) << "------------------- KGAMEINPUT --------------------";
qCDebug(GAMES_PRIVATE_KGAME) << "this: " << this;
qCDebug(GAMES_PRIVATE_KGAME) << "rtti : " << rtti();
qCDebug(GAMES_PRIVATE_KGAME) << "Player: " << player();
qCDebug(GAMES_PRIVATE_KGAME) << "---------------------------------------------------";
}
// ----------------------- Key IO ---------------------------
class KGameKeyIOPrivate
{
};
KGameKeyIO::KGameKeyIO(QWidget *parent)
: KGameIO(), d(nullptr)
{
if (parent)
{
qCDebug(GAMES_PRIVATE_KGAME) << "Key Event filter installed";
parent->installEventFilter(this);
}
}
KGameKeyIO::~KGameKeyIO()
{
if (parent())
{
parent()->removeEventFilter(this);
}
delete d;
}
int KGameKeyIO::rtti() const { return KeyIO; }
bool KGameKeyIO::eventFilter( QObject *o, QEvent *e )
{
if (!player())
{
return false;
}
// key press/release
if ( e->type() == QEvent::KeyPress ||
e->type() == QEvent::KeyRelease )
{
QKeyEvent *k = (QKeyEvent*)e;
// qCDebug(GAMES_PRIVATE_KGAME) << "KGameKeyIO" << this << "key press/release" << k->key();
QByteArray buffer;
QDataStream stream(&buffer,QIODevice::WriteOnly);
bool eatevent=false;
Q_EMIT signalKeyEvent(this,stream,k,&eatevent);
QDataStream msg(buffer);
if (eatevent && sendInput(msg))
{
return eatevent;
}
return false; // do not eat otherwise
}
return QObject::eventFilter( o, e ); // standard event processing
}
// ----------------------- Mouse IO ---------------------------
class KGameMouseIOPrivate
{
};
KGameMouseIO::KGameMouseIO(QWidget *parent,bool trackmouse)
: KGameIO(), d(nullptr)
{
if (parent)
{
qCDebug(GAMES_PRIVATE_KGAME) << "Mouse Event filter installed tracking=" << trackmouse;
parent->installEventFilter(this);
parent->setMouseTracking(trackmouse);
}
}
KGameMouseIO::KGameMouseIO(QGraphicsScene *parent,bool /*trackmouse*/)
: KGameIO(), d(nullptr)
{
if (parent)
{
//qCDebug(GAMES_PRIVATE_KGAME) << "Mouse Event filter installed tracking=" << trackmouse;
parent->installEventFilter(this);
// parent->setMouseTracking(trackmouse);
}
}
KGameMouseIO::~KGameMouseIO()
{
if (parent())
{
parent()->removeEventFilter(this);
}
delete d;
}
int KGameMouseIO::rtti() const
{
return MouseIO;
}
void KGameMouseIO::setMouseTracking(bool b)
{
if (parent())
{
((QWidget*)parent())->setMouseTracking(b);
}
}
bool KGameMouseIO::eventFilter( QObject *o, QEvent *e )
{
if (!player())
{
return false;
}
//qCDebug(GAMES_PRIVATE_KGAME) << "KGameMouseIO" << this << QLatin1String( " " ) << e->type();
// mouse action
if ( e->type() == QEvent::MouseButtonPress ||
e->type() == QEvent::MouseButtonRelease ||
e->type() == QEvent::MouseButtonDblClick ||
e->type() == QEvent::Wheel ||
e->type() == QEvent::MouseMove ||
e->type() == QEvent::GraphicsSceneMousePress ||
e->type() == QEvent::GraphicsSceneMouseRelease ||
e->type() == QEvent::GraphicsSceneMouseDoubleClick ||
e->type() == QEvent::GraphicsSceneWheel ||
e->type() == QEvent::GraphicsSceneMouseMove
)
{
QMouseEvent *k = (QMouseEvent*)e;
// qCDebug(GAMES_PRIVATE_KGAME) << "KGameMouseIO" << this;
QByteArray buffer;
QDataStream stream(&buffer,QIODevice::WriteOnly);
bool eatevent=false;
Q_EMIT signalMouseEvent(this,stream,k,&eatevent);
// qCDebug(GAMES_PRIVATE_KGAME) << "################# eatevent=" << eatevent;
QDataStream msg(buffer);
if (eatevent && sendInput(msg))
{
return eatevent;
}
return false; // do not eat otherwise
}
return QObject::eventFilter( o, e ); // standard event processing
}
// ----------------------- KGameProcesPrivate ---------------------------
class KGameProcessIOPrivate
{
public:
KGameProcessIOPrivate()
{
//mMessageServer = 0;
//mMessageClient = 0;
mProcessIO=nullptr;
}
//KMessageServer *mMessageServer;
//KMessageClient *mMessageClient;
KMessageProcess *mProcessIO;
};
// ----------------------- Process IO ---------------------------
KGameProcessIO::KGameProcessIO(const QString& name)
: KGameIO(), d(new KGameProcessIOPrivate)
{
qCDebug(GAMES_PRIVATE_KGAME) << ": this=" << this << ", sizeof(this)=" << sizeof(KGameProcessIO);
//qCDebug(GAMES_PRIVATE_KGAME) << "================= KMEssageServer ====================";
//d->mMessageServer=new KMessageServer(0,this);
//qCDebug(GAMES_PRIVATE_KGAME) << "================= KMEssageClient ====================";
//d->mMessageClient=new KMessageClient(this);
qCDebug(GAMES_PRIVATE_KGAME) << "================= KMEssageProcessIO ====================";
d->mProcessIO=new KMessageProcess(this,name);
qCDebug(GAMES_PRIVATE_KGAME) << "================= KMEssage Add client ====================";
//d->mMessageServer->addClient(d->mProcessIO);
//qCDebug(GAMES_PRIVATE_KGAME) << "================= KMEssage SetSErver ====================";
//d->mMessageClient->setServer(d->mMessageServer);
qCDebug(GAMES_PRIVATE_KGAME) << "================= KMEssage: Connect ====================";
//connect(d->mMessageClient, SIGNAL(broadcastReceived(QByteArray,quint32)),
// this, SLOT(clientMessage(QByteArray,quint32)));
//connect(d->mMessageClient, SIGNAL(forwardReceived(QByteArray,quint32,QValueList<quint32>)),
// this, SLOT(clientMessage(QByteArray,quint32,QValueList<quint32>)));
connect(d->mProcessIO, &KMessageProcess::received, this, &KGameProcessIO::receivedMessage);
// Relay signal
connect(d->mProcessIO, &KMessageProcess::signalReceivedStderr, this, &KGameProcessIO::signalReceivedStderr);
//qCDebug(GAMES_PRIVATE_KGAME) << "Our client is id="<<d->mMessageClient->id();
}
KGameProcessIO::~KGameProcessIO()
{
qCDebug(GAMES_PRIVATE_KGAME) << ": this=" << this;
qCDebug(GAMES_PRIVATE_KGAME) << "player="<<player();
if (player())
{
player()->removeGameIO(this,false);
}
if (d->mProcessIO)
{
delete d->mProcessIO;
d->mProcessIO=nullptr;
}
delete d;
}
int KGameProcessIO::rtti() const
{
return ProcessIO;
}
void KGameProcessIO::initIO(KPlayer *p)
{
KGameIO::initIO(p);
// Send 'hello' to process
QByteArray buffer;
QDataStream stream(&buffer, QIODevice::WriteOnly);
bool sendit=true;
if (p)
{
qint16 id = p->userId();
stream << id;
Q_EMIT signalIOAdded(this,stream,p,&sendit);
if (sendit )
{
quint32 sender = p->id();
qCDebug(GAMES_PRIVATE_KGAME) << "Sending IOAdded to process player !!!!!!!!!!!!!! ";
sendSystemMessage(stream, KGameMessage::IdIOAdded, 0, sender);
}
}
}
void KGameProcessIO::notifyTurn(bool b)
{
if (!player())
{
qCWarning(GAMES_PRIVATE_KGAME) << ": player() is NULL";
return;
}
bool sendit=true;
QByteArray buffer;
QDataStream stream(&buffer,QIODevice::WriteOnly);
stream << (qint8)b;
Q_EMIT signalPrepareTurn(stream,b,this,&sendit);
if (sendit)
{
quint32 sender=player()->id();
qCDebug(GAMES_PRIVATE_KGAME) << "Sending Turn to process player !!!!!!!!!!!!!! ";
sendSystemMessage(stream, KGameMessage::IdTurn, 0, sender);
}
}
void KGameProcessIO::sendSystemMessage(QDataStream &stream,int msgid, quint32 receiver, quint32 sender)
{
sendAllMessages(stream, msgid, receiver, sender, false);
}
void KGameProcessIO::sendMessage(QDataStream &stream,int msgid, quint32 receiver, quint32 sender)
{
sendAllMessages(stream, msgid, receiver, sender, true);
}
void KGameProcessIO::sendAllMessages(QDataStream &stream,int msgid, quint32 receiver, quint32 sender, bool usermsg)
{
qCDebug(GAMES_PRIVATE_KGAME) << "==============> KGameProcessIO::sendMessage (usermsg="<<usermsg<<")";
// if (!player()) return ;
//if (!player()->isActive()) return ;
if (usermsg)
{
msgid+=KGameMessage::IdUser;
}
qCDebug(GAMES_PRIVATE_KGAME) << "=============* ProcessIO (" << msgid << "," << receiver << "," << sender << ") ===========";
QByteArray buffer;
QDataStream ostream(&buffer,QIODevice::WriteOnly);
QBuffer *device=(QBuffer *)stream.device();
QByteArray data=device->buffer();;
KGameMessage::createHeader(ostream,sender,receiver,msgid);
// ostream.writeRawBytes(data.data()+device->at(),data.size()-device->at());
ostream.writeRawData(data.data(),data.size());
qCDebug(GAMES_PRIVATE_KGAME) << " Adding user data from pos="<< device->pos() <<" amount=" << data.size() << "byte";
//if (d->mMessageClient) d->mMessageClient->sendBroadcast(buffer);
if (d->mProcessIO)
{
d->mProcessIO->send(buffer);
}
}
//void KGameProcessIO::clientMessage(const QByteArray& receiveBuffer, quint32 clientID, const QValueList <quint32> &recv)
void KGameProcessIO::receivedMessage(const QByteArray& receiveBuffer)
{
QDataStream stream(receiveBuffer);
int msgid;
quint32 sender;
quint32 receiver;
KGameMessage::extractHeader(stream,sender,receiver,msgid);
qCDebug(GAMES_PRIVATE_KGAME) << "************* Got process message sender =" << sender
<< "receiver=" << receiver << " msgid=" << msgid;
// Cut out the header part...to not confuse network code
QBuffer *buf=(QBuffer *)stream.device();
QByteArray newbuffer;
newbuffer = QByteArray::fromRawData(buf->buffer().data()+buf->pos(),buf->size()-buf->pos());
QDataStream ostream(newbuffer);
qCDebug(GAMES_PRIVATE_KGAME) << "Newbuffer size=" << newbuffer.size();
// This is a dummy message which allows us the process to talk with its owner
if (msgid==KGameMessage::IdProcessQuery)
{
Q_EMIT signalProcessQuery(ostream,this);
}
else if (player())
{
sender = player()->id(); // force correct sender
if (msgid==KGameMessage::IdPlayerInput)
{
sendInput(ostream,true,sender);
}
else
{
player()->forwardMessage(ostream,msgid,receiver,sender);
}
}
else
{
qCDebug(GAMES_PRIVATE_KGAME) << ": Got message from process but no player defined!";
}
newbuffer.clear();
}
// ----------------------- Computer IO --------------------------
class KGameComputerIOPrivate
{
//TODO: maybe these should be KGameProperties!!
public:
KGameComputerIOPrivate()
{
mAdvanceCounter = 0;
mReactionPeriod = 0;
mPauseCounter = 0;
mAdvanceTimer = nullptr;
}
int mAdvanceCounter;
int mReactionPeriod;
int mPauseCounter;
QTimer* mAdvanceTimer;
};
KGameComputerIO::KGameComputerIO()
: KGameIO(), d(new KGameComputerIOPrivate)
{
}
KGameComputerIO::KGameComputerIO(KPlayer *p)
: KGameIO(p), d(new KGameComputerIOPrivate)
{
}
KGameComputerIO::~KGameComputerIO()
{
if (d->mAdvanceTimer)
{
delete d->mAdvanceTimer;
}
delete d;
}
int KGameComputerIO::rtti() const
{
return ComputerIO;
}
void KGameComputerIO::setReactionPeriod(int calls)
{
d->mReactionPeriod = calls;
}
int KGameComputerIO::reactionPeriod() const
{
return d->mReactionPeriod;
}
void KGameComputerIO::setAdvancePeriod(int ms)
{
stopAdvancePeriod();
d->mAdvanceTimer = new QTimer(this);
connect(d->mAdvanceTimer, &QTimer::timeout, this, &KGameComputerIO::advance);
d->mAdvanceTimer->start(ms);
}
void KGameComputerIO::stopAdvancePeriod()
{
if (d->mAdvanceTimer)
{
d->mAdvanceTimer->stop();
delete d->mAdvanceTimer;
}
}
void KGameComputerIO::pause(int calls)
{
d->mPauseCounter = calls;
}
void KGameComputerIO::unpause()
{
pause(0);
}
void KGameComputerIO::advance()
{
if (d->mPauseCounter > 0)
{
d->mPauseCounter--;
return;
}
else if (d->mPauseCounter < 0)
{
return;
}
d->mAdvanceCounter++;
if (d->mAdvanceCounter >= d->mReactionPeriod)
{
d->mAdvanceCounter = 0;
reaction();
}
}
void KGameComputerIO::reaction()
{
Q_EMIT signalReaction();
}
|