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.

windowclasswatcher.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /***************************************************************************
  2. * Copyright (C) 2004 by Leonid Zeitlin *
  3. * lz@europe.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include "windowclasswatcher.h"
  21. #include "kbconfig.h"
  22. #include <twinmodule.h>
  23. #include <kdebug.h>
  24. #include <X11/Xutil.h>
  25. WindowClassWatcher::WindowClassWatcher(KBConfig *kbconf, TQObject *parent, const char *name)
  26. : WindowWatcher(kbconf, parent, name)
  27. {
  28. reset();
  29. }
  30. WindowClassWatcher::~WindowClassWatcher()
  31. {
  32. }
  33. TQString WindowClassWatcher::getWindowClass(WId id)
  34. {
  35. XClassHint hint;
  36. TQString ret = TQString::null;
  37. if (XGetClassHint(tqt_xdisplay(), id, &hint)) {
  38. ret = hint.res_class;
  39. XFree(hint.res_name);
  40. XFree(hint.res_class);
  41. }
  42. return ret;
  43. }
  44. void WindowClassWatcher::windowAdded(WId id)
  45. {
  46. TQString wclass = getWindowClass(id);
  47. KBClassInfo &info = m_class_group_map[wclass];
  48. if (info.refcount == 0) {
  49. // new class
  50. info.groupno = m_kbconf->default_groupno();
  51. info.next_groupno = m_kbconf->getNextGroup(m_kbconf->default_groupno());
  52. }
  53. info.refcount++;
  54. m_win_class_map.insert(id, wclass);
  55. }
  56. void WindowClassWatcher::windowRemoved(WId id)
  57. {
  58. ClassGroupMap::Iterator iter = m_class_group_map.find(m_win_class_map[id]);
  59. if (iter != m_class_group_map.end()) {
  60. iter.data().refcount--;
  61. if (iter.data().refcount <= 0) m_class_group_map.remove(iter);
  62. }
  63. m_win_class_map.remove(id);
  64. }
  65. void WindowClassWatcher::activeWindowChanged(WId id)
  66. {
  67. if (id == 0) return; // no window is active
  68. m_active_class = m_class_group_map.find(m_win_class_map[id]);
  69. if (m_active_class != m_class_group_map.end())
  70. emit windowChanged(m_active_class.data().groupno, m_active_class.data().next_groupno);
  71. }
  72. void WindowClassWatcher::changeGroup(int groupno, int next_groupno)
  73. {
  74. if (m_active_class != m_class_group_map.end()) {
  75. m_active_class.data().groupno = groupno;
  76. m_active_class.data().next_groupno = next_groupno;
  77. }
  78. }
  79. void WindowClassWatcher::reset()
  80. {
  81. m_win_class_map.clear();
  82. m_class_group_map.clear();
  83. // fill the map with existing windows
  84. const TQValueList<WId> windows = m_twin_module->windows();
  85. const TQValueList<WId>::ConstIterator end = windows.end();
  86. for (TQValueList<WId>::ConstIterator it = windows.begin(); it != end; ++it)
  87. windowAdded(*it);
  88. WId active_window = m_twin_module->activeWindow();
  89. if (active_window)
  90. m_active_class = m_class_group_map.find(m_win_class_map[active_window]);
  91. else m_active_class = m_class_group_map.end();
  92. }
  93. #include "windowclasswatcher.moc"