File: mouse.cpp

package info (click to toggle)
kdebase 2%3A980312-8
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 16,500 kB
  • ctags: 11,017
  • sloc: cpp: 77,278; ansic: 14,050; makefile: 2,643; sh: 2,151; perl: 844
file content (360 lines) | stat: -rw-r--r-- 8,410 bytes parent folder | download
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
/*
 * mouse.cpp
 *
 * Copyright (c) 1997 Patrick Dowler dowler@morgul.fsh.uvic.ca
 *
 * Requires the Qt widget libraries, available at no cost at
 * http://www.troll.no/
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <iostream.h> 

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>

#include <qfileinf.h> 
#include <qstring.h>
#include <qmsgbox.h> 

#include <kmsgbox.h> 
#include "mouse.h"

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "geom.h"

static bool GUI;

MouseConfig::~MouseConfig ()
{
  if (GUI)
    {
      delete alabel;
      delete a;
      delete accel;
      delete tlabel;
      delete t;
      delete thresh;
      delete leftHanded;
      delete rightHanded;
      delete handedBox;
    }
}

MouseConfig::MouseConfig (QWidget * parent, const char *name, bool init)
  : KConfigWidget (parent, name)
{
  if (init)
    GUI = FALSE;
  else
    GUI = TRUE;

  if (GUI)
    {
      accel = new KSlider(1,20,2,20, KSlider::Horizontal, this, "acc");
      a = new QLCDNumber (2, this, "a");
      a->setFrameStyle( QFrame::NoFrame );
      alabel = new QLabel(klocale->translate("Acceleration"), this);
      connect( accel, SIGNAL(valueChanged(int)), a, SLOT(display(int)) );

      thresh = new KSlider(0,20,2,20, KSlider::Horizontal, this, "thr");
      t = new QLCDNumber (2, this, "t");
      t->setFrameStyle( QFrame::NoFrame );
      tlabel = new QLabel(klocale->translate("Threshold"), this);
      connect( thresh, SIGNAL(valueChanged(int)), t, SLOT(display(int)) );

      handedBox = new QButtonGroup(klocale->translate("Button mapping"), 
				   this, "handed");
      rightHanded = new QRadioButton(klocale->translate("Right handed"), 
				     handedBox, "R");
      leftHanded = new QRadioButton(klocale->translate("Left handed"), 
				    handedBox, "L");
 
      a->adjustSize();
      t->adjustSize();
      alabel->adjustSize();
      tlabel->adjustSize();
      rightHanded->adjustSize();
      leftHanded->adjustSize();
    }

  handedEnabled = TRUE;
  config = kapp->getConfig();

  GetSettings();
}

void MouseConfig::resizeEvent(QResizeEvent *)
{
  int h = SPACE_YO;
  int w = 0;
  int center;
  
  w = max( alabel->width(), tlabel->width() );

  alabel->move(SPACE_XO, h);
  QSize qsha = accel->sizeHint();
  accel->setGeometry(w + 2*SPACE_XO, h, 200, qsha.height());
  h += (QMAX( qsha.height(),alabel->height()) + SPACE_YI);

  center = accel->x() + ( accel->width() - a->width() )/2;

  a->move(center, h);
  h += a->height() + SPACE_YO;

  tlabel->move(SPACE_XO, h);
  QSize qsht = thresh->sizeHint();
  thresh->setGeometry(w + 2*SPACE_XO, h, 200, qsht.height());
  h += (QMAX( qsht.height(),tlabel->height()) + SPACE_YI);

  t->move(center, h);
  h += t->height() + SPACE_YO;

  QFontMetrics fm = handedBox->font();
  int titleH = fm.height();
  int titleW = fm.width(handedBox->title());
  int buttonH = rightHanded->height();
  int buttonW = max( rightHanded->width(), leftHanded->width() );
  int boxH = 2*buttonH + 3*SPACE_YI + titleH;
  int boxW = 0;
  if (boxW < titleW + 4*SPACE_XI)
    boxW =  titleW + 4*SPACE_XI;
  if (boxW < buttonW + 2*SPACE_XI)
    boxW = buttonW + 2*SPACE_XI;
  int h_box = h;
  h += boxH + SPACE_YO; 

  rightHanded->move(SPACE_XI, SPACE_YI + titleH);
  leftHanded->move(SPACE_XI, 2*SPACE_YI + buttonH + titleH);
  handedBox->setGeometry(SPACE_XO, h_box, boxW, boxH);
}

int MouseConfig::getAccel()
{
  return a->intValue();
}

void MouseConfig::setAccel(int val)
{
  accel->setValue(val);
  a->display(val);
}

int MouseConfig::getThreshold()
{
  return t->intValue();
}

void MouseConfig::setThreshold(int val)
{
  thresh->setValue(val);
  t->display(val);
}


int MouseConfig::getHandedness()
{
  if (rightHanded->isChecked())
    return RIGHT_HANDED;
  else
    return LEFT_HANDED;
}

void MouseConfig::setHandedness(int val)
{
  rightHanded->setChecked(FALSE);
  leftHanded->setChecked(FALSE);
  if (val == RIGHT_HANDED)
    rightHanded->setChecked(TRUE);
  else
    leftHanded->setChecked(TRUE);
}

void MouseConfig::GetSettings( void )
{
  int accel_num, accel_den, threshold;
  XGetPointerControl( kapp->getDisplay(), 
		      &accel_num, &accel_den, &threshold );
  accel_num /= accel_den;   // integer acceleration only

  // get settings from X server
  int h = RIGHT_HANDED;
  unsigned char map[5];
  num_buttons = XGetPointerMapping(kapp->getDisplay(), map, 5);
      
  switch (num_buttons)
    {
    case 1:
      /* disable button remapping */
      if (GUI)
	{
	  rightHanded->setEnabled(FALSE);
	  leftHanded->setEnabled(FALSE);
	  handedEnabled = FALSE;
	}
      break;
    case 2:
      if ( (int)map[0] == 1 && (int)map[1] == 2 )
	h = RIGHT_HANDED;
      else if ( (int)map[0] == 2 && (int)map[1] == 1 )
	h = LEFT_HANDED;
      else
	{
	  /* custom button setup: disable button remapping */
	  if (GUI)
	    {
	      rightHanded->setEnabled(FALSE);
	      leftHanded->setEnabled(FALSE);
	    }
	}
      break;
    case 3:
      middle_button = (int)map[1];
      if ( (int)map[0] == 1 && (int)map[2] == 3 )
	h = RIGHT_HANDED;
      else if ( (int)map[0] == 3 && (int)map[2] == 1 )
	h = LEFT_HANDED;
      else
	{
	  /* custom button setup: disable button remapping */
	  if (GUI)
	    {
	      rightHanded->setEnabled(FALSE);
	      leftHanded->setEnabled(FALSE);
	      handedEnabled = FALSE;
	    }
	}
      break;
    default:
      /* custom setup with > 3 buttons: disable button remapping */
      if (GUI)
	{
	  rightHanded->setEnabled(FALSE);
	  leftHanded->setEnabled(FALSE);
	  handedEnabled = FALSE;
	}
      break;
    }

  config->setGroup("Mouse");
  int a = config->readNumEntry("Acceleration",-1);
  if (a == -1)
    accelRate = accel_num;
  else
    accelRate = a;

  int t = config->readNumEntry("Threshold",-1);
  if (t == -1)
    thresholdMove = threshold;
  else
    thresholdMove = t;

  QString key = config->readEntry("MouseButtonMapping");
  if (key == "RightHanded")
    handed = RIGHT_HANDED;
  else if (key == "LeftHanded")
    handed = LEFT_HANDED;
  else if (key == NULL)
    handed = h;

  // the GUI should always show the real values
  if (GUI)
    {
      setAccel(accel_num);
      setThreshold(threshold);
      setHandedness(h);
    }
}

void MouseConfig::saveParams( void )
{
  if (GUI)
    {
      accelRate = getAccel();
      thresholdMove = getThreshold();
      handed = getHandedness();
    }

  XChangePointerControl( kapp->getDisplay(),
			 TRUE, TRUE, accelRate, 1, thresholdMove);

  
  unsigned char map[3];
  if (handedEnabled)
    {
      switch (num_buttons)
	{
	case 1:
	  break;
	case 2:
	  if (handed == RIGHT_HANDED)
	    {
	      map[0] = (unsigned char) 1;
	      map[1] = (unsigned char) 3;
	    }
	  else
	    {
	      map[0] = (unsigned char) 3;
	      map[1] = (unsigned char) 1;
	    }
	  break;
	case 3:
	  if (handed == RIGHT_HANDED)
	    {
	      map[0] = (unsigned char) 1;
	      map[1] = (unsigned char) middle_button;
	      map[2] = (unsigned char) 3;
	    }
	  else
	    {
	      map[0] = (unsigned char) 3;
	      map[1] = (unsigned char) middle_button;
	      map[2] = (unsigned char) 1;
	    }
	  break;
	}
      int retval;
      while ((retval=XSetPointerMapping(kapp->getDisplay(), map, num_buttons))
	     == MappingBusy)
	/* keep trying until the pointer is free */ 
	;
    }

  config->setGroup("Mouse");
  config->writeEntry("Acceleration",accelRate);
  config->writeEntry("Threshold",thresholdMove);
  if (handed == RIGHT_HANDED)
    config->writeEntry("MouseButtonMapping","RightHanded");
  else
    config->writeEntry("MouseButtonMapping","LeftHanded");
}

void MouseConfig::loadSettings()
{
  GetSettings();
}

void MouseConfig::applySettings()
{
  saveParams();
}

#include "mouse.moc"