summaryrefslogtreecommitdiffstats
path: root/lskat/lskat/KEInput.cpp
blob: 91377f38f065ff9b875976e88a80c7a6cc2071f5 (plain)
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
/***************************************************************************
                          KEInput.cpp  -  description
                             -------------------
    begin                : Tue May 2 2000
    copyright            : (C) 2000 by Martin Heni
    email                : martin@heni-online.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include <stdio.h>
#include "KEInput.h"
#include "KConnectEntry.h"

#define K_INPUT_DELAY 25     // delay following non interactive moves

#include "KEInput.moc"

KEInput::KEInput(TQObject * parent)
  : TQObject(parent,0)
{
  number_of_inputs=0;
  locked=FALSE;
  previous_input=-1;
  next_input=-1;
  cTimer=0;
//  mMsg=(KEMessage *)0;
}

KEInput::~KEInput()
{
  int i;
  for (i=number_of_inputs-1;i>=0;i--)
  {
    RemoveInput(i);
  }
  delete cTimer;
}

int KEInput::QueryNumberOfInputs()
{
  return number_of_inputs;
}

int KEInput::QueryNext()
{
  return next_input;
}

int KEInput::QueryPrevious()
{
  return previous_input;
}

bool KEInput::IsInput(int no)
{
  if (no>=number_of_inputs || no<0) return FALSE;
  if (QueryType(no)==0) return FALSE;
  return TRUE;
}

// Default is the type of the current player
bool KEInput::IsInteractive(int no)
{
  return QueryType(no)==KG_INPUTTYPE_INTERACTIVE;
}
bool KEInput::IsProcess(int no)
{
  return QueryType(no)==KG_INPUTTYPE_PROCESS;
}
bool KEInput::IsRemote(int no)
{
  return QueryType(no)==KG_INPUTTYPE_REMOTE;
}
KG_INPUTTYPE KEInput::QueryType(int no)
{
  if (no==-1) no=QueryNext();
  if (no>=number_of_inputs || no<0) return KG_INPUTTYPE_INVALID;
  return playerArray[no].QueryType();
}

KR_STATUS KEInput::QueryStatus(int no)
{
  if (no==-1) no=QueryNext();
  if (no>=number_of_inputs || no<0)
  {
    return KR_INVALID;
  }
  return playerArray[no].QueryStatus();
}
bool KEInput::SendMsg(KEMessage *msg,int no)
{
  if (no==-1) no=QueryNext();
  if (no>=number_of_inputs || no<0)
  {
    return false;
  }
  return playerArray[no].SendMsg(msg);
}


bool KEInput::SetInputDevice(int no, KG_INPUTTYPE type,KEMessage *msg)
{
  bool result;
  // Grow if necessary
  if (no<0) return false;
  else if (no<number_of_inputs)
  {
    RemoveInput(no);
  }
  if (no>=number_of_inputs)
  {
    playerArray.resize(no+1);
    number_of_inputs=no+1;
  }
  result=playerArray[no].Init(type,no,msg);
  // if (result)
  // Connect even if remote connection is not yet build
  if (result || playerArray[no].QueryStatus()==KR_WAIT_FOR_CLIENT)
  {
    switch(QueryType(no))
    {
        case KG_INPUTTYPE_INTERACTIVE:
          connect(playerArray[no].QueryInteractiveConnect(),TQ_SIGNAL(signalReceiveMsg(KEMessage *,int )),
                  this,TQ_SLOT(slotSetInput(KEMessage *,int )));
          connect(playerArray[no].QueryInteractiveConnect(),TQ_SIGNAL(signalPrepareMove(KEMessage *,KG_INPUTTYPE)),
                  this,TQ_SLOT(slotPrepareMove(KEMessage *,KG_INPUTTYPE)));
        break;
        case KG_INPUTTYPE_REMOTE:
          connect(playerArray[no].QueryRemoteConnect(),TQ_SIGNAL(signalReceiveMsg(KEMessage *,int )),
                  this,TQ_SLOT(slotSetInput(KEMessage *,int )));
          connect(playerArray[no].QueryRemoteConnect(),TQ_SIGNAL(signalPrepareMove(KEMessage *,KG_INPUTTYPE)),
                  this,TQ_SLOT(slotPrepareMove(KEMessage *,KG_INPUTTYPE)));
        break;
        case KG_INPUTTYPE_PROCESS:
          connect(playerArray[no].QueryProcessConnect(),TQ_SIGNAL(signalReceiveMsg(KEMessage *,int )),
                  this,TQ_SLOT(slotSetInput(KEMessage *,int )));
          connect(playerArray[no].QueryProcessConnect(),TQ_SIGNAL(signalPrepareMove(KEMessage *,KG_INPUTTYPE)),
                  this,TQ_SLOT(slotPrepareMove(KEMessage *,KG_INPUTTYPE)));
        break;
      default:
        break;
    }
  }
  return result;
}

bool KEInput::RemoveInput(int no)
{
  bool result;
  if (no>=number_of_inputs || no<0) return FALSE;
  result=playerArray[no].Exit();
  // shrink if last entry is removed
  if (no==number_of_inputs-1)
  {
    playerArray.resize(no);
    number_of_inputs=no;
  }
  return result;
}


// Sets a new player and sends it a message
bool KEInput::Next(int number, bool force)
{
  if (locked && !force) return FALSE;
  if (!IsInput(number)) return FALSE;
  locked=TRUE;

  // printf("KEInput::Next %d OK  ... lock set!!\n",number);

  previous_input=next_input;
  next_input=number;

  switch(QueryType(number))
  {
    case KG_INPUTTYPE_INTERACTIVE:
      playerArray[number].QueryInteractiveConnect()->Next();
    break;
    case KG_INPUTTYPE_REMOTE:
      if (QueryType(previous_input)!=0 &&
          QueryType(previous_input)!=KG_INPUTTYPE_INTERACTIVE)
      {
        // delay non interactive move to allow interactive inout
        if (cTimer) delete cTimer; // Ouch...
        cTimer=new TQTimer(this);
        connect(cTimer,TQ_SIGNAL(timeout()),this,TQ_SLOT(slotTimerNextRemote()));
        cTimer->start(K_INPUT_DELAY,TRUE);
      }
      else
      {
        playerArray[number].QueryRemoteConnect()->Next();
      }
    break;
    case KG_INPUTTYPE_PROCESS:
      if (QueryType(previous_input)!=0 &&
          QueryType(previous_input)!=KG_INPUTTYPE_INTERACTIVE)
      {
        // delay non interactive move to allow interactive inout
        cTimer=new TQTimer(this);
        connect(cTimer,TQ_SIGNAL(timeout()),this,TQ_SLOT(slotTimerNextProcess()));
        cTimer->start(K_INPUT_DELAY,TRUE);
      }
      else
        playerArray[number].QueryProcessConnect()->Next();
    break;
    default: return FALSE;
  }
  return TRUE;
}

void KEInput::slotTimerNextRemote()
{
  delete cTimer;
  cTimer=0;
  if (next_input>=0 && next_input<number_of_inputs)
    playerArray[next_input].QueryRemoteConnect()->Next();
}

void KEInput::slotTimerNextProcess()
{
  delete cTimer;
  cTimer=0;
  if (next_input>=0 && next_input<number_of_inputs)
    playerArray[next_input].QueryProcessConnect()->Next();
}

// called to prepare a move which is send to a remote/computer
// should fill in message data
void KEInput::slotPrepareMove(KEMessage *msg,KG_INPUTTYPE type)
{
  // just forward it
  switch(type)
  {
    case KG_INPUTTYPE_INTERACTIVE:
      emit signalPrepareInteractiveMove(msg);
    break;
    case KG_INPUTTYPE_PROCESS:
      emit signalPrepareProcessMove(msg);
    break;
    case KG_INPUTTYPE_REMOTE:
      emit signalPrepareRemoteMove(msg);
    break;
    default: // Do nothing
    break;
  }
}
// called by ReceiveMsg
void KEInput::slotSetInput(KEMessage *msg,int id)
{
  if (!msg) return ;
  SetInput(msg,id);
}

bool KEInput::SetInput(KEMessage *msg,int number)
{
  /*
  if (!locked)
  {
    printf("KEINput:SetInput not locked(should be) returning FALSE\n");
    return FALSE;
  }
  */
  if (number<0 || number>=number_of_inputs) number=next_input; // automatically select player
//  KEMessage *mMsg= new KEMessage;
  // evt if (mMsg) delete mMsg;
//  *mMsg=*msg;
  // locked=FALSE;
  // printf("**** KEInput: emitting signalReceiveInput 474\n");
  emit signalReceiveInput(msg,number);
//  emit signalReceiveInput(mMsg);
//  delete mMsg;
  return TRUE;
}

void KEInput::Lock()
{
  locked=true;
}

void KEInput::Unlock()
{
  locked=false;
}

bool KEInput::IsLocked()
{
  return locked;
}