File indexing completed on 2024-11-03 11:09:07
0001 /* 0002 SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org> 0003 SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only 0006 */ 0007 0008 #include "actions.h" 0009 0010 #include "input.h" 0011 #include "shortcuts_handler.h" 0012 #include "windows_handler.h" 0013 #include "windows_helper/window_selection_list.h" 0014 0015 #include <KConfigGroup> 0016 #include <KSharedConfig> 0017 #include <QDebug> 0018 0019 // #include <X11/X.h> 0020 #include <QX11Info> 0021 #include <X11/Xlib.h> 0022 0023 /* 0024 */ 0025 0026 namespace KHotKeys 0027 { 0028 KeyboardInputActionVisitor::~KeyboardInputActionVisitor() 0029 { 0030 } 0031 0032 KeyboardInputAction::KeyboardInputAction(ActionData *data_P, const QString &input_P, Windowdef_list *dest_window_P, bool active_window_P) 0033 : Action(data_P) 0034 , _input(input_P) 0035 , _dest_window(dest_window_P) 0036 { 0037 if (dest_window_P) 0038 _destination = SpecificWindow; 0039 else if (active_window_P) 0040 _destination = ActiveWindow; 0041 else 0042 _destination = ActionWindow; 0043 0044 if (!_dest_window) 0045 _dest_window = new Windowdef_list; 0046 } 0047 0048 KeyboardInputAction::~KeyboardInputAction() 0049 { 0050 delete _dest_window; 0051 } 0052 0053 void KeyboardInputAction::accept(ActionVisitor &visitor) 0054 { 0055 if (KeyboardInputActionVisitor *v = dynamic_cast<KeyboardInputActionVisitor *>(&visitor)) { 0056 v->visit(*this); 0057 } else { 0058 qDebug() << "Visitor error"; 0059 } 0060 } 0061 0062 const QString &KeyboardInputAction::input() const 0063 { 0064 return _input; 0065 } 0066 0067 void KeyboardInputAction::setDestination(const DestinationWindow &dest) 0068 { 0069 _destination = dest; 0070 } 0071 0072 void KeyboardInputAction::setDestinationWindowRules(Windowdef_list *list) 0073 { 0074 if (_dest_window) 0075 delete _dest_window; 0076 0077 _dest_window = list; 0078 } 0079 0080 void KeyboardInputAction::setInput(const QString &input) 0081 { 0082 _input = input; 0083 } 0084 0085 const Windowdef_list *KeyboardInputAction::dest_window() const 0086 { 0087 return _dest_window; 0088 } 0089 0090 Windowdef_list *KeyboardInputAction::dest_window() 0091 { 0092 return _dest_window; 0093 } 0094 0095 KeyboardInputAction::DestinationWindow KeyboardInputAction::destination() const 0096 { 0097 return _destination; 0098 } 0099 0100 void KeyboardInputAction::cfg_write(KConfigGroup &cfg_P) const 0101 { 0102 base::cfg_write(cfg_P); 0103 cfg_P.writeEntry("Type", "KEYBOARD_INPUT"); // overwrites value set in base::cfg_write() 0104 cfg_P.writeEntry("Input", input()); 0105 0106 cfg_P.writeEntry("DestinationWindow", int(_destination)); 0107 0108 if (_destination == SpecificWindow && dest_window() != nullptr) { 0109 KConfigGroup windowGroup(cfg_P.config(), cfg_P.name() + "DestinationWindow"); 0110 dest_window()->cfg_write(windowGroup); 0111 } 0112 } 0113 0114 void KeyboardInputAction::execute() 0115 { 0116 qDebug(); 0117 0118 if (input().isEmpty()) { 0119 qDebug() << "Input is empty"; 0120 return; 0121 } 0122 0123 Window w = InputFocus; 0124 switch (_destination) { 0125 case SpecificWindow: 0126 Q_ASSERT(dest_window()); 0127 w = windows_handler->find_window(dest_window()); 0128 if (w == None) 0129 w = InputFocus; 0130 break; 0131 0132 case ActionWindow: 0133 w = windows_handler->action_window(); 0134 if (w == None) 0135 w = InputFocus; 0136 break; 0137 0138 case ActiveWindow: 0139 // Nothing to do because w is InputFocus already 0140 break; 0141 0142 default: 0143 Q_ASSERT(false); 0144 } 0145 0146 int last_index = -1, start = 0; 0147 while ((last_index = input().indexOf(':', last_index + 1)) != -1) // find next ';' 0148 { 0149 QString key = input().mid(start, last_index - start).trimmed(); 0150 keyboard_handler->send_macro_key(key, w); 0151 start = last_index + 1; 0152 } 0153 // and the last one 0154 QString key = input().mid(start, input().length()).trimmed(); 0155 keyboard_handler->send_macro_key(key, w); // the rest 0156 if (QX11Info::isPlatformX11()) 0157 XFlush(QX11Info::display()); 0158 } 0159 0160 const QString KeyboardInputAction::description() const 0161 { 0162 QString tmp = input(); 0163 tmp.replace('\n', ' '); 0164 tmp.truncate(30); 0165 return i18n("Keyboard input: ") + tmp; 0166 } 0167 0168 Action *KeyboardInputAction::copy(ActionData *data_P) const 0169 { 0170 return new KeyboardInputAction(data_P, input(), dest_window() ? dest_window()->copy() : nullptr, _destination == ActiveWindow); 0171 } 0172 0173 } // namespace KHotKeys