~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to kcontrol/access/kaccess.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <unistd.h>
 
2
 
 
3
 
 
4
#include <qtimer.h>
 
5
#include <qpainter.h>
 
6
 
 
7
 
 
8
#include <kdebug.h>
 
9
#include <kaudioplayer.h>
 
10
#include <kconfig.h>
 
11
#include <kglobal.h>
 
12
#include <netwm.h>
 
13
 
 
14
 
 
15
#include "kaccess.moc"
 
16
 
 
17
 
 
18
KAccessApp::KAccessApp(bool allowStyles, bool GUIenabled)
 
19
  : KUniqueApplication(allowStyles, GUIenabled), _artsBellBlocked(false),
 
20
                                                 overlay(0), wm(0, KWinModule::INFO_DESKTOP)
 
21
{
 
22
  // verify the Xlib has matching XKB extension
 
23
  int major = XkbMajorVersion;
 
24
  int minor = XkbMinorVersion;
 
25
  if (!XkbLibraryVersion(&major, &minor))
 
26
    {
 
27
      kdError() << "Xlib XKB extension does not match" << endl;
 
28
      exit(-1);
 
29
    }
 
30
  kdDebug() << "Xlib XKB extension major=" << major << " minor=" << minor << endl;
 
31
 
 
32
  // verify the X server has matching XKB extension
 
33
  // if yes, the XKB extension is initialized
 
34
  int opcode_rtrn;
 
35
  int error_rtrn;
 
36
  if (!XkbQueryExtension(qt_xdisplay(), &opcode_rtrn, &xkb_opcode, &error_rtrn,
 
37
                         &major, &minor))
 
38
    {
 
39
      kdError() << "X server has not matching XKB extension" << endl;
 
40
      exit(-1);
 
41
    }
 
42
  kdDebug() << "X server XKB extension major=" << major << " minor=" << minor << endl;
 
43
 
 
44
  _activeWindow = wm.activeWindow();
 
45
  connect(&wm, SIGNAL(activeWindowChanged(WId)), this, SLOT(activeWindowChanged(WId)));
 
46
 
 
47
  artsBellTimer = new QTimer( this );
 
48
  connect( artsBellTimer, SIGNAL( timeout() ), SLOT( slotArtsBellTimeout() ));
 
49
}
 
50
 
 
51
int KAccessApp::newInstance()
 
52
{
 
53
  KGlobal::config()->reparseConfiguration();
 
54
  readSettings();
 
55
  return 0;
 
56
}
 
57
 
 
58
void KAccessApp::readSettings()
 
59
{
 
60
  KConfig *config = KGlobal::config();
 
61
 
 
62
  // bell ---------------------------------------------------------------
 
63
 
 
64
  config->setGroup("Bell");
 
65
  _systemBell = config->readBoolEntry("SystemBell", true);
 
66
  _artsBell = config->readBoolEntry("ArtsBell", false);
 
67
  _artsBellFile = config->readPathEntry("ArtsBellFile");
 
68
  _visibleBell = config->readBoolEntry("VisibleBell", false);
 
69
  _visibleBellInvert = config->readBoolEntry("VisibleBellInvert", false);
 
70
  QColor def(Qt::red);
 
71
  _visibleBellColor = config->readColorEntry("VisibleBellColor", &def);
 
72
  _visibleBellPause = config->readNumEntry("VisibleBellPause", 500);
 
73
 
 
74
  // select bell events if we need them
 
75
  int state = (_artsBell || _visibleBell) ? XkbBellNotifyMask : 0;
 
76
  XkbSelectEvents(qt_xdisplay(), XkbUseCoreKbd, XkbBellNotifyMask, state);
 
77
 
 
78
  // deactivate system bell if not needed
 
79
  if (!_systemBell)
 
80
    XkbChangeEnabledControls(qt_xdisplay(), XkbUseCoreKbd, XkbAudibleBellMask, 0);
 
81
  else
 
82
    XkbChangeEnabledControls(qt_xdisplay(), XkbUseCoreKbd, XkbAudibleBellMask, XkbAudibleBellMask);
 
83
 
 
84
  // keyboard -------------------------------------------------------------
 
85
 
 
86
  config->setGroup("Keyboard");
 
87
 
 
88
  // get keyboard state
 
89
  XkbDescPtr xkb = XkbGetMap(qt_xdisplay(), 0, XkbUseCoreKbd);
 
90
  if (!xkb)
 
91
    return;
 
92
  if (XkbGetControls(qt_xdisplay(), XkbAllControlsMask, xkb) != Success)
 
93
    return;
 
94
 
 
95
  // sticky keys
 
96
  if (config->readBoolEntry("StickyKeys", false))
 
97
    {
 
98
      if (config->readBoolEntry("StickyKeysLatch", true))
 
99
        xkb->ctrls->ax_options |= XkbAX_LatchToLockMask;
 
100
      else
 
101
        xkb->ctrls->ax_options &= ~XkbAX_LatchToLockMask;
 
102
      xkb->ctrls->enabled_ctrls |= XkbStickyKeysMask;
 
103
    }
 
104
  else
 
105
    xkb->ctrls->enabled_ctrls &= ~XkbStickyKeysMask;
 
106
 
 
107
  // turn off two-keys-disable sticky keys option
 
108
  xkb->ctrls->ax_options &= ~XkbAX_TwoKeysMask;
 
109
 
 
110
  // turn off timeout
 
111
  xkb->ctrls->enabled_ctrls &= ~XkbAccessXTimeoutMask;
 
112
 
 
113
  // slow keys
 
114
  if (config->readBoolEntry("SlowKeys", false))
 
115
    {
 
116
      xkb->ctrls->slow_keys_delay = config->readNumEntry("SlowKeysDelay", 500);
 
117
      xkb->ctrls->enabled_ctrls |= XkbSlowKeysMask;
 
118
    }
 
119
  else
 
120
      xkb->ctrls->enabled_ctrls &= ~XkbSlowKeysMask;
 
121
 
 
122
  // bounce keys
 
123
  if (config->readBoolEntry("BounceKeys", false))
 
124
    {
 
125
      xkb->ctrls->debounce_delay = config->readNumEntry("BounceKeysDelay", 500);
 
126
      xkb->ctrls->enabled_ctrls |= XkbBounceKeysMask;
 
127
    }
 
128
  else
 
129
      xkb->ctrls->enabled_ctrls &= ~XkbBounceKeysMask;
 
130
 
 
131
 
 
132
  // mouse-by-keyboard ----------------------------------------------
 
133
 
 
134
  config->setGroup("Mouse");
 
135
 
 
136
  if (config->readBoolEntry("MouseKeys", false))
 
137
    {
 
138
      xkb->ctrls->mk_delay = config->readNumEntry("MKDelay", 160);
 
139
      xkb->ctrls->mk_interval = config->readNumEntry("MKInterval", 5);
 
140
      xkb->ctrls->mk_time_to_max = config->readNumEntry("MKTimeToMax", 1000);
 
141
      xkb->ctrls->mk_max_speed = config->readNumEntry("MKMaxSpeed", 500);
 
142
      xkb->ctrls->mk_curve = config->readNumEntry("MKCurve", 0);
 
143
      xkb->ctrls->mk_dflt_btn = config->readNumEntry("MKDefaultButton", 0);
 
144
 
 
145
      xkb->ctrls->enabled_ctrls |= XkbMouseKeysMask;
 
146
    }
 
147
  else
 
148
    xkb->ctrls->enabled_ctrls &= ~XkbMouseKeysMask;
 
149
 
 
150
  // set state
 
151
  XkbSetControls(qt_xdisplay(), XkbControlsEnabledMask | XkbMouseKeysAccelMask | XkbStickyKeysMask, xkb);
 
152
 
 
153
  // reset them after program exit
 
154
  uint ctrls = XkbStickyKeysMask | XkbSlowKeysMask | XkbBounceKeysMask | XkbMouseKeysMask | XkbAudibleBellMask;
 
155
  uint values = XkbAudibleBellMask;
 
156
  XkbSetAutoResetControls(qt_xdisplay(), ctrls, &ctrls, &values);
 
157
 
 
158
  delete overlay;
 
159
  overlay = 0;
 
160
}
 
161
 
 
162
 
 
163
bool KAccessApp::x11EventFilter(XEvent *event)
 
164
{
 
165
  // handle XKB events
 
166
  if (event->type == xkb_opcode)
 
167
    {
 
168
      XkbAnyEvent *ev = (XkbAnyEvent*) event;
 
169
 
 
170
      if (ev->xkb_type == XkbBellNotify)
 
171
        xkbBellNotify((XkbBellNotifyEvent*)event);
 
172
 
 
173
      return true;
 
174
    }
 
175
 
 
176
  // process other events as usual
 
177
  return KApplication::x11EventFilter(event);
 
178
}
 
179
 
 
180
 
 
181
void VisualBell::paintEvent(QPaintEvent *event)
 
182
{
 
183
  QWidget::paintEvent(event);
 
184
  QTimer::singleShot(_pause, this, SLOT(hide()));
 
185
}
 
186
 
 
187
 
 
188
void KAccessApp::activeWindowChanged(WId wid)
 
189
{
 
190
  _activeWindow = wid;
 
191
}
 
192
 
 
193
 
 
194
void KAccessApp::xkbBellNotify(XkbBellNotifyEvent *event)
 
195
{
 
196
  // bail out if we should not really ring
 
197
  if (event->event_only)
 
198
    return;
 
199
 
 
200
  // flash the visible bell
 
201
  if (_visibleBell)
 
202
    {
 
203
      // create overlay widget
 
204
      if (!overlay)
 
205
        overlay = new VisualBell(_visibleBellPause);
 
206
 
 
207
      WId id = _activeWindow;
 
208
 
 
209
      NETRect frame, window;
 
210
      NETWinInfo net(qt_xdisplay(), id, desktop()->winId(), 0);
 
211
 
 
212
      net.kdeGeometry(frame, window);
 
213
 
 
214
      overlay->setGeometry(window.pos.x, window.pos.y, window.size.width, window.size.height);
 
215
 
 
216
      if (_visibleBellInvert)
 
217
        {
 
218
          QPixmap screen = QPixmap::grabWindow(id, 0, 0, window.size.width, window.size.height);
 
219
          QPixmap invert(window.size.width, window.size.height);
 
220
          QPainter p(&invert);
 
221
          p.setRasterOp(QPainter::NotCopyROP);
 
222
          p.drawPixmap(0, 0, screen);
 
223
          overlay->setBackgroundPixmap(invert);
 
224
        }
 
225
      else
 
226
        overlay->setBackgroundColor(_visibleBellColor);
 
227
 
 
228
      // flash the overlay widget
 
229
      overlay->raise();
 
230
      overlay->show();
 
231
      flushX();
 
232
    }
 
233
 
 
234
  // ask artsd to ring a nice bell
 
235
  if (_artsBell && !_artsBellBlocked ) {
 
236
    KAudioPlayer::play(_artsBellFile);
 
237
    _artsBellBlocked = true;
 
238
    artsBellTimer->start( 300, true );
 
239
  }
 
240
}
 
241
 
 
242
void KAccessApp::slotArtsBellTimeout()
 
243
{
 
244
  _artsBellBlocked = false;
 
245
}
 
246