KKBSwitch – keyboard layout indicator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kbswitchapp.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /***************************************************************************
  2. kbswitchapp.cpp - description
  3. -------------------
  4. begin : Sun Jul 1 2001
  5. copyright : (C) 2001 by Leonid Zeitlin
  6. email : lz@europe.com
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "config.h"
  17. #include "kbswitchapp.h"
  18. #include "kbconfigdlg.h"
  19. #include "singlewindowwatcher.h"
  20. #include "windowclasswatcher.h"
  21. #include <kdebug.h>
  22. #include <tdelocale.h>
  23. #include <twinmodule.h>
  24. KBSwitchApp::KBSwitchApp()
  25. {
  26. #ifndef HAVE_LIBXKLAVIER
  27. //m_twin_module = NULL;
  28. m_watcher = NULL;
  29. #endif
  30. if (!m_xkb.xkbAvailable()) return; // oops! No XKB in the server
  31. m_kbconf.load(config());
  32. m_cur_groupno = m_next_groupno = -1;
  33. #ifdef HAVE_LIBXKLAVIER
  34. XklSetGroupPerApp(m_kbconf.group_scope() != KBConfig::SCOPE_GLOBAL);
  35. if (m_kbconf.toggle_mode())
  36. XklSetSecondaryGroupsMask(12); /* binary 1100 */
  37. #else
  38. //resetWindowMap();
  39. //enableKWinModule();
  40. enableWatcher();
  41. #endif
  42. m_intf = new KBSwitchIntf(this, &m_kbconf);
  43. TQObject::connect(m_intf, SIGNAL(nextGroupSelected()), this, SLOT(slotSelectNextGroup()));
  44. TQObject::connect(m_intf, SIGNAL(groupSelected(int)), this, SLOT(slotGroupSelected(int)));
  45. TQObject::connect(&m_xkb, SIGNAL(layoutChanged()), this, SLOT(reconfigure()));
  46. TQObject::connect(&m_xkb, SIGNAL(groupChanged(int)), this, SLOT(slotXkbGroupChanged(int)));
  47. m_force_group_setting = false;
  48. int start_group = m_kbconf.default_groupno();
  49. m_trayicon = new KBSwitchTrayIcon(&m_kbconf);
  50. TQObject::connect(m_trayicon, SIGNAL(groupSelected(int)), this,
  51. SLOT(slotGroupSelected(int)));
  52. TQObject::connect(m_trayicon, SIGNAL(clicked()), this, SLOT(slotSelectNextGroup()));
  53. TQObject::connect(m_trayicon, SIGNAL(preferencesSelected()), this,
  54. SLOT(slotPreferences()));
  55. if (start_group != m_xkb.getGroupNo()) {
  56. setStartGroup(start_group);
  57. }
  58. else {
  59. adaptToGroup(start_group);
  60. }
  61. setMainWidget(m_trayicon);
  62. m_trayicon->show();
  63. #ifdef HAVE_LIBXKLAVIER
  64. XklStartListen();
  65. #endif
  66. }
  67. KBSwitchApp::~KBSwitchApp(){
  68. #ifndef HAVE_LIBXKLAVIER
  69. //disableKWinModule();
  70. disableWatcher();
  71. #endif
  72. }
  73. /** No descriptions */
  74. bool KBSwitchApp::x11EventFilter(XEvent *e){
  75. // let m_xkb process the event and emit signals if necessary
  76. m_xkb.processEvent(e);
  77. return TDEApplication::x11EventFilter(e);
  78. }
  79. /** Update the tray icon to show the flag corresponding to the current keyboard group */
  80. void KBSwitchApp::updateIcon(int groupno){
  81. if (groupno >= 0 && groupno < m_kbconf.groupCount()) { // check just in case
  82. m_trayicon->updateTrayIcon(groupno);
  83. if (m_kbconf.toggle_mode()) m_trayicon->setToggleGroups(m_cur_groupno, m_next_groupno);
  84. }
  85. }
  86. /** No descriptions */
  87. void KBSwitchApp::slotGroupSelected(int groupno)
  88. {
  89. #ifdef HAVE_LIBXKLAVIER
  90. XklAllowOneSwitchToSecondaryGroup();
  91. m_xkb.setGroupNo(groupno);
  92. #else
  93. if (m_cur_groupno != groupno) {
  94. setGroups(groupno, m_cur_groupno);
  95. }
  96. #endif
  97. }
  98. /** No descriptions */
  99. void KBSwitchApp::internalToggleGroups()
  100. {
  101. int tmp = m_next_groupno;
  102. m_next_groupno = m_cur_groupno;
  103. m_cur_groupno = tmp;
  104. }
  105. /** No descriptions */
  106. void KBSwitchApp::forceSetGroup(int groupno)
  107. {
  108. m_force_group_setting = true;
  109. #ifndef HAVE_LIBXKLAVIER
  110. //if (m_active_window != m_window_map.end())
  111. // m_active_window.data().groupno = groupno;
  112. #endif
  113. m_xkb.setGroupNo(groupno);
  114. }
  115. /** No descriptions */
  116. void KBSwitchApp::slotSelectNextGroup()
  117. {
  118. #ifdef HAVE_LIBXKLAVIER
  119. m_xkb.setGroupNo(XklGetNextGroup());
  120. #else
  121. //forceSetGroup(m_next_groupno);
  122. m_xkb.setGroupNo(m_next_groupno);
  123. #endif
  124. }
  125. /** No descriptions */
  126. void KBSwitchApp::slotPreferences(){
  127. KBConfigDlg dlg(&m_kbconf);
  128. TQObject::connect(&dlg, SIGNAL(okClicked()), m_trayicon, SLOT(slotUpdateIcons()));
  129. TQObject::connect(&dlg, SIGNAL(applyClicked()), m_trayicon, SLOT(slotUpdateIcons()));
  130. TQObject::connect(&dlg, SIGNAL(okClicked()), this, SLOT(slotPrefChanged()));
  131. TQObject::connect(&dlg, SIGNAL(applyClicked()), this, SLOT(slotPrefChanged()));
  132. dlg.exec();
  133. }
  134. /** Called when XKeyboard configuration changes */
  135. void KBSwitchApp::reconfigure(){
  136. m_kbconf.load(config());
  137. m_trayicon->reconfigure();
  138. // make sure default group is still valid
  139. if (m_kbconf.default_groupno() >= m_xkb.getNumKbdGroups())
  140. m_kbconf.set_default_groupno(0);
  141. int groupno = m_xkb.getGroupNo();
  142. if (groupno >= m_xkb.getNumKbdGroups()) {
  143. // current group no longer valid, reset to default group
  144. setStartGroup(m_kbconf.default_groupno());
  145. }
  146. else if (!m_force_group_setting) {
  147. adaptToGroup(groupno);
  148. }
  149. #ifndef HAVE_LIBXKLAVIER
  150. //resetWindowMap();
  151. if (m_watcher) m_watcher->reset();
  152. #endif
  153. }
  154. /** Respond to XKB changing the current group */
  155. void KBSwitchApp::slotXkbGroupChanged(int groupno){
  156. #ifdef HAVE_LIBXKLAVIER
  157. updateIcon(groupno);
  158. /* XWindowAttributes attrs;
  159. XGetWindowAttributes(tqt_xdisplay(), tqt_xrootwin(), &attrs);
  160. kdDebug() << "root event mask is " << attrs.your_event_mask << endl;
  161. kdDebug() << "SubstructureNotifyMask is " <<
  162. ((attrs.your_event_mask & SubstructureNotifyMask) ? "ON" : "OFF") << endl;*/
  163. #else
  164. bool accept = false;
  165. if (m_force_group_setting) { // the change of group is forced by us
  166. if (groupno == m_cur_groupno) { // the group is what we wanted, fine
  167. m_force_group_setting = false;
  168. accept = true;
  169. }
  170. else { // oops, not the group we expected
  171. forceSetGroup(m_cur_groupno);
  172. }
  173. }
  174. else { // the change is caused by something external, we didn't request it
  175. if (m_kbconf.toggle_mode() && groupno != m_next_groupno) { // toggle mode, and the group is not correct
  176. m_xkb.setGroupNo(m_next_groupno);
  177. }
  178. else { // adjust to this group
  179. if (m_kbconf.toggle_mode()) internalToggleGroups();
  180. else {
  181. m_cur_groupno = groupno;
  182. m_next_groupno = m_kbconf.getNextGroup(groupno);
  183. }
  184. accept = true;
  185. }
  186. }
  187. if (accept) {
  188. updateIcon(groupno);
  189. if (m_watcher) m_watcher->changeGroup(groupno, m_next_groupno);
  190. }
  191. /*if (m_kbconf.toggle_mode() && !force_group_setting && groupno != m_next_groupno) {
  192. forceSetGroup(m_next_groupno);
  193. }
  194. else {
  195. updateIcon(groupno);
  196. force_group_setting = false;
  197. if (m_kbconf.toggle_mode()) internalToggleGroups();
  198. else {
  199. kdDebug() << "slotXkbGroupChanged, change group to " << groupno << endl;
  200. m_cur_groupno = groupno;
  201. m_next_groupno = m_kbconf.getNextGroup(groupno);
  202. }
  203. if (m_watcher) m_watcher->changeGroup(groupno, m_next_groupno);
  204. }*/
  205. #endif
  206. }
  207. /** Set the keyboard to the given group and set internal variable accordingly */
  208. void KBSwitchApp::setStartGroup(int start_group){
  209. /*m_next_groupno = start_group;
  210. if (m_kbconf.toggle_mode()) {
  211. m_cur_groupno = getNextGroup(start_group);
  212. }
  213. else {
  214. m_cur_groupno = start_group;
  215. }
  216. forceSetGroup(m_next_groupno);*/
  217. setGroups(start_group, m_kbconf.getNextGroup(start_group));
  218. }
  219. /** adapt internal state to the given group */
  220. void KBSwitchApp::adaptToGroup(int groupno) {
  221. if (! m_kbconf.toggle_mode() || // if not in toggle mode
  222. (m_cur_groupno != groupno // or in toggle mode and internal variables are invalid
  223. || m_next_groupno >= m_kbconf.groupCount()
  224. || m_next_groupno == m_cur_groupno)) {
  225. m_cur_groupno = groupno;
  226. m_next_groupno = m_kbconf.getNextGroup(groupno);
  227. }
  228. #ifndef HAVE_LIBXKLAVIER
  229. if (m_watcher) m_watcher->changeGroup(groupno, m_next_groupno);
  230. /*if (m_active_window != m_window_map.end()) {
  231. m_active_window.data().groupno = groupno;
  232. m_active_window.data().next_groupno = m_next_groupno;
  233. }*/
  234. #endif
  235. updateIcon(groupno);
  236. }
  237. /*void KBSwitchApp::slotWindowChanged(WId activeWindow)
  238. {
  239. #ifndef HAVE_LIBXKLAVIER
  240. m_active_window = m_window_map.find(activeWindow);
  241. if (m_active_window == m_window_map.end())
  242. addWindowToMap(activeWindow);
  243. if (m_active_window.data().groupno != m_cur_groupno) {
  244. setGroups(m_active_window.data().groupno, m_active_window.data().next_groupno);
  245. }
  246. else m_next_groupno = m_active_window.data().next_groupno;
  247. #endif
  248. }*/
  249. void KBSwitchApp::slotWindowChanged(int groupno, int next_groupno)
  250. {
  251. if (groupno != m_cur_groupno) {
  252. setGroups(groupno, next_groupno);
  253. }
  254. else m_next_groupno = next_groupno;
  255. }
  256. /*void KBSwitchApp::slotWindowRemoved(WId window)
  257. {
  258. #ifndef HAVE_LIBXKLAVIER
  259. m_window_map.remove(window);
  260. #endif
  261. }
  262. #ifndef HAVE_LIBXKLAVIER
  263. void KBSwitchApp::resetWindowMap()
  264. {
  265. WId active_window_id;
  266. m_window_map.clear();
  267. if (m_twin_module && (active_window_id = m_twin_module->activeWindow()))
  268. addWindowToMap(active_window_id);
  269. else
  270. m_active_window = m_window_map.end();
  271. }*/
  272. /** Enable window manager notifications */
  273. /*void KBSwitchApp::enableKWinModule()
  274. {
  275. if (m_twin_module == NULL) {
  276. m_twin_module = new KWinModule();
  277. connect(m_twin_module, SIGNAL(activeWindowChanged(WId)), SLOT(slotWindowChanged(WId)));
  278. connect(m_twin_module, SIGNAL(windowRemoved(WId)), SLOT(slotWindowRemoved(WId)));
  279. resetWindowMap();
  280. if (m_cur_groupno != -1 && m_cur_groupno != m_kbconf.default_groupno())
  281. setStartGroup(m_kbconf.default_groupno());
  282. }
  283. }*/
  284. /** Disable window manager notifications */
  285. /*void KBSwitchApp::disableKWinModule()
  286. {
  287. if (m_twin_module) {
  288. m_twin_module->disconnect();
  289. delete m_twin_module;
  290. m_twin_module = NULL;
  291. resetWindowMap();
  292. }
  293. }*/
  294. /** adds a new window to the internal window map */
  295. /*void KBSwitchApp::addWindowToMap(WId window_id)
  296. {
  297. KBWinInfo wininfo = { m_kbconf.default_groupno(),
  298. m_kbconf.getNextGroup(m_kbconf.default_groupno()) };
  299. m_active_window = m_window_map.insert(window_id, wininfo);
  300. }
  301. #endif*/
  302. void KBSwitchApp::enableWatcher()
  303. {
  304. if (!m_watcher) {
  305. m_watcher_type = m_kbconf.group_scope();
  306. if (m_watcher_type == KBConfig::SCOPE_WINDOW)
  307. m_watcher = new SingleWindowWatcher(&m_kbconf, this);
  308. else if (m_watcher_type == KBConfig::SCOPE_CLASS)
  309. m_watcher = new WindowClassWatcher(&m_kbconf, this);
  310. else return; // if scope is global, don't create watcher
  311. connect(m_watcher, SIGNAL(windowChanged(int, int )),
  312. SLOT(slotWindowChanged(int, int)));
  313. }
  314. }
  315. void KBSwitchApp::disableWatcher()
  316. {
  317. if (m_watcher) {
  318. m_watcher->disconnect();
  319. delete m_watcher;
  320. m_watcher = NULL;
  321. }
  322. }
  323. /** React to a change in KKBSwitch's user preferences,
  324. * made by user in Configure dialog */
  325. void KBSwitchApp::slotPrefChanged()
  326. {
  327. #ifndef HAVE_LIBXKLAVIER
  328. if (m_kbconf.group_scope() != m_watcher_type) {
  329. disableWatcher();
  330. enableWatcher();
  331. }
  332. #endif
  333. }
  334. /** Set the current and next groups */
  335. void KBSwitchApp::setGroups(int group, int next_group)
  336. {
  337. m_cur_groupno = group;
  338. m_next_groupno = next_group;
  339. forceSetGroup(group);
  340. }
  341. #include "kbswitchapp.moc"