summaryrefslogtreecommitdiffstats
path: root/kcontrol/joystick/joywidget.cpp
blob: 5d104006b749a0614985afb7c5755ff2db9b4c8e (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
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
/***************************************************************************
 *   Copyright (C) 2003 by Martin Koller                                   *
 *   m.koller@surfeu.at                                                    *
 *   This file is part of the Trinity Control Center Module for Joysticks  *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/
#include "joywidget.h"
#include "joydevice.h"
#include "poswidget.h"
#include "caldialog.h"

#include <tqhbox.h>
#include <tqvbox.h>
#include <tqtable.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqlistbox.h>
#include <tqcheckbox.h>
#include <tqtimer.h>
#include <tqfontmetrics.h>
#include <tqpushbutton.h>

#include <tdelocale.h>
#include <kdialog.h>
#include <tdemessagebox.h>
#include <kiconloader.h>

//--------------------------------------------------------------
static TQString PRESSED = I18N_NOOP("PRESSED");
//--------------------------------------------------------------

JoyWidget::JoyWidget(TQWidget *parent, const char *name)
 : TQWidget(parent, name), idle(0), joydev(0)
{
  TQVBox *mainVbox = new TQVBox(parent);
  mainVbox->setSpacing(KDialog::spacingHint());

  // create area to show an icon + message if no joystick was detected
  {
    messageBox = new TQHBox(mainVbox);
    messageBox->setSpacing(KDialog::spacingHint());
    TQLabel *icon = new TQLabel(messageBox);
    icon->setPixmap(TDEGlobal::iconLoader()->loadIcon("messagebox_warning", TDEIcon::NoGroup,
                                                    TDEIcon::SizeMedium, TDEIcon::DefaultState, 0, true));
    icon->setFixedSize(icon->sizeHint());
    message = new TQLabel(messageBox);
    messageBox->hide();
  }

  TQHBox *devHbox = new TQHBox(mainVbox);
  new TQLabel(i18n("Device:"), devHbox);
  device = new TQComboBox(true, devHbox);
  device->setInsertionPolicy(TQComboBox::NoInsertion);
  connect(device, TQT_SIGNAL(activated(const TQString &)), this, TQT_SLOT(deviceChanged(const TQString &)));
  devHbox->setStretchFactor(device, 3);

  TQHBox *hbox = new TQHBox(mainVbox);
  hbox->setSpacing(KDialog::spacingHint());

  TQVBox *vboxLeft = new TQVBox(hbox);
  vboxLeft->setSpacing(KDialog::spacingHint());

  new TQLabel(i18n("Position:"), vboxLeft);
  xyPos = new PosWidget(vboxLeft);
  trace = new TQCheckBox(i18n("Show trace"), mainVbox);
  connect(trace, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(traceChanged(bool)));

  TQVBox *vboxMid = new TQVBox(hbox);
  vboxMid->setSpacing(KDialog::spacingHint());

  TQVBox *vboxRight = new TQVBox(hbox);
  vboxRight->setSpacing(KDialog::spacingHint());

  // calculate the column width we need
  TQFontMetrics fm(font());
  int colWidth = TQMAX(fm.width(PRESSED), fm.width("-32767")) + 10;  // -32767 largest string

  new TQLabel(i18n("Buttons:"), vboxMid);
  buttonTbl = new TQTable(0, 1, vboxMid);
  buttonTbl->setReadOnly(true);
  buttonTbl->horizontalHeader()->setLabel(0, i18n("State"));
  buttonTbl->horizontalHeader()->setClickEnabled(false);
  buttonTbl->horizontalHeader()->setResizeEnabled(false);
  buttonTbl->verticalHeader()->setClickEnabled(false);
  buttonTbl->verticalHeader()->setResizeEnabled(false);
  buttonTbl->setColumnWidth(0, colWidth);

  new TQLabel(i18n("Axes:"), vboxRight);
  axesTbl = new TQTable(0, 1, vboxRight);
  axesTbl->setReadOnly(true);
  axesTbl->horizontalHeader()->setLabel(0, i18n("Value"));
  axesTbl->horizontalHeader()->setClickEnabled(false);
  axesTbl->horizontalHeader()->setResizeEnabled(false);
  axesTbl->verticalHeader()->setClickEnabled(false);
  axesTbl->verticalHeader()->setResizeEnabled(false);
  axesTbl->setColumnWidth(0, colWidth);

  // calibrate button
  calibrate = new TQPushButton(i18n("Calibrate"), mainVbox);
  connect(calibrate, TQT_SIGNAL(clicked()), this, TQT_SLOT(calibrateDevice()));
  calibrate->setEnabled(false);

  // set up a timer for idle processing of joystick events
  idle = new TQTimer(this);
  connect(idle, TQT_SIGNAL(timeout()), this, TQT_SLOT(checkDevice()));

  // check which devicefiles we have
  init();

  vboxLeft->adjustSize();
  vboxMid->adjustSize();
  vboxRight->adjustSize();
  hbox->adjustSize();
  mainVbox->adjustSize();

  setMinimumSize(mainVbox->size());
}

//--------------------------------------------------------------

JoyWidget::~JoyWidget()
{
  delete joydev;
}

//--------------------------------------------------------------

void JoyWidget::init()
{
  // check which devicefiles we have
  int i;
  bool first = true;
  char dev[30];

  device->clear();
  buttonTbl->setNumRows(0);
  axesTbl->setNumRows(0);

  for (i = 0; i < 5; i++)  // check the first 5 devices
  {
    sprintf(dev, "/dev/js%d", i);  // first look in /dev
    JoyDevice *joy = new JoyDevice(dev);

    if ( joy->open() != JoyDevice::SUCCESS )
    {
      delete joy;
      sprintf(dev, "/dev/input/js%d", i);  // then look in /dev/input
      joy = new JoyDevice(dev);

      if ( joy->open() != JoyDevice::SUCCESS )
      {
        delete joy;
        continue;    // try next number
      }
    }

    // we found one

    device->insertItem(TQString("%1 (%2)").arg(joy->text()).arg(joy->device()));

    // display values for first device
    if ( first )
    {
      showDeviceProps(joy);  // this sets the joy object into this->joydev
      first = false;
    }
    else
      delete joy;
  }

  /* KDE 4: Remove this check(and i18n) when all KCM wrappers properly test modules */
  if ( device->count() == 0 )
  {
    messageBox->show();
    message->setText(TQString("<qt><b>%1</b></qt>").arg(
      i18n("No joystick device automatically found on this computer.<br>"
           "Checks were done in /dev/js[0-4] and /dev/input/js[0-4]<br>"
           "If you know that there is one attached, please enter the correct device file.")));
  }
}

//--------------------------------------------------------------

void JoyWidget::traceChanged(bool state)
{
  xyPos->showTrace(state);
}

//--------------------------------------------------------------

void JoyWidget::restoreCurrDev()
{
  if ( !joydev )  // no device open
  {
    device->setCurrentText("");
    calibrate->setEnabled(false);
  }
  else
  {
    // try to find the current open device in the combobox list
    TQListBoxItem *item;
    item = device->listBox()->findItem(joydev->device(), TQt::Contains);

    if ( !item )  // the current open device is one the user entered (not in the list)
      device->setCurrentText(joydev->device());
    else
      device->setCurrentText(item->text());
  }
}

//--------------------------------------------------------------

void JoyWidget::deviceChanged(const TQString &dev)
{
  // find "/dev" in given string
  int start, stop;
  TQString devName;

  if ( (start = dev.find("/dev")) == -1 )
  {
    KMessageBox::sorry(this,
      i18n("The given device name is invalid (does not contain /dev).\n"
           "Please select a device from the list or\n"
           "enter a device file, like /dev/js0."), i18n("Unknown Device"));

    restoreCurrDev();
    return;
  }

  if ( (stop = dev.find(")", start)) != -1 )  // seems to be text selected from our list
    devName = dev.mid(start, stop - start);
  else
    devName = dev.mid(start);

  if ( joydev && (devName == joydev->device()) ) return;  // user selected the current device; ignore it

  JoyDevice *joy = new JoyDevice(devName);
  JoyDevice::ErrorCode ret = joy->open();

  if ( ret != JoyDevice::SUCCESS )
  {
    KMessageBox::error(this, joy->errText(ret), i18n("Device Error"));

    delete joy;
    restoreCurrDev();
    return;
  }

  showDeviceProps(joy);
}

//--------------------------------------------------------------

void JoyWidget::showDeviceProps(JoyDevice *joy)
{
  joydev = joy;

  buttonTbl->setNumRows(joydev->numButtons());

  axesTbl->setNumRows(joydev->numAxes());
  if ( joydev->numAxes() >= 2 )
  {
    axesTbl->verticalHeader()->setLabel(0, "1(x)");
    axesTbl->verticalHeader()->setLabel(1, "2(y)");
  }

  calibrate->setEnabled(true);
  idle->start(0);

  // make both tables use the same space for header; this looks nicer
  buttonTbl->setLeftMargin(TQMAX(buttonTbl->verticalHeader()->width(),
                                  axesTbl->verticalHeader()->width()));
  axesTbl->setLeftMargin(buttonTbl->verticalHeader()->width());
}

//--------------------------------------------------------------

void JoyWidget::checkDevice()
{
  if ( !joydev ) return;  // no open device yet

  JoyDevice::EventType type;
  int number, value;

  if ( !joydev->getEvent(type, number, value) )
    return;

  if ( type == JoyDevice::BUTTON )
  {
    if ( value == 0 )  // button release
      buttonTbl->setText(number, 0, "-");
    else
      buttonTbl->setText(number, 0, PRESSED);
  }

  if ( type == JoyDevice::AXIS )
  {
    if ( number == 0 ) // x-axis
      xyPos->changeX(value);

    if ( number == 1 ) // y-axis
      xyPos->changeY(value);

    axesTbl->setText(number, 0, TQString("%1").arg(int(value)));
  }
}

//--------------------------------------------------------------

void JoyWidget::calibrateDevice()
{
  if ( !joydev ) return;  // just to be save

  JoyDevice::ErrorCode ret = joydev->initCalibration();

  if ( ret != JoyDevice::SUCCESS )
  {
    KMessageBox::error(this, joydev->errText(ret), i18n("Communication Error"));
    return;
  }

  if ( KMessageBox::messageBox(this, KMessageBox::Information,
        i18n("<qt>Calibration is about to check the precision.<br><br>"
             "<b>Please move all axes to their center position and then "
             "do not touch the joystick anymore.</b><br><br>"
             "Click OK to start the calibration.</qt>"),
        i18n("Calibration"),
        KStdGuiItem::ok(), KStdGuiItem::cancel()) != KMessageBox::Ok )
    return;

  idle->stop();  // stop the joystick event getting; this must be done inside the calibrate dialog

  CalDialog dlg(this, joydev);
  dlg.calibrate();

  // user cancelled somewhere during calibration, therefore the device is in a bad state
  if ( dlg.result() == TQDialog::Rejected )
    joydev->restoreCorr();

  idle->start(0);  // continue with event getting
}

//--------------------------------------------------------------

void JoyWidget::resetCalibration()
{
  if ( !joydev ) return;  // just to be save

  JoyDevice::ErrorCode ret = joydev->restoreCorr();

  if ( ret != JoyDevice::SUCCESS )
  {
    KMessageBox::error(this, joydev->errText(ret), i18n("Communication Error"));
  }
  else
  {
    KMessageBox::information(this,
      i18n("Restored all calibration values for joystick device %1.").arg(joydev->device()),
      i18n("Calibration Success"));
  }
}

//--------------------------------------------------------------

#include "joywidget.moc"