diff options
Diffstat (limited to 'khotkeys/shared')
32 files changed, 6988 insertions, 0 deletions
diff --git a/khotkeys/shared/CMakeLists.txt b/khotkeys/shared/CMakeLists.txt new file mode 100644 index 000000000..27227cd2f --- /dev/null +++ b/khotkeys/shared/CMakeLists.txt @@ -0,0 +1,42 @@ +################################################# +# +# (C) 2010-2011 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_BINARY_DIR} + ${TDE_INCLUDE_DIR} + ${TQT_INCLUDE_DIRS} +) + +link_directories( + ${TQT_LIBRARY_DIRS} + ${XTEST_LIBDIR} +) + + +##### khotkeys_shared (shared) ################## + +set( target khotkeys_shared ) + +set( ${target}_SRCS + action_data.cpp khotkeysglobal.cpp triggers.cpp + windows.cpp settings.cpp actions.cpp input.cpp + conditions.cpp khlistview.cpp khlistbox.cpp + gestures.cpp voices.cpp voicesignature.cpp + sound.cpp soundrecorder.cpp +) + +tde_add_library( ${target} SHARED AUTOMOC + SOURCES ${${target}_SRCS} + VERSION 1.0.0 + LINK tdeio-shared ${XTEST_LIBRARIES} + DESTINATION ${LIB_INSTALL_DIR} +) diff --git a/khotkeys/shared/Makefile.am b/khotkeys/shared/Makefile.am new file mode 100644 index 000000000..f318e935d --- /dev/null +++ b/khotkeys/shared/Makefile.am @@ -0,0 +1,15 @@ +lib_LTLIBRARIES = libkhotkeys_shared.la + +libkhotkeys_shared_la_SOURCES = action_data.cpp khotkeysglobal.cpp triggers.cpp windows.cpp \ + settings.cpp actions.cpp input.cpp conditions.cpp khlistview.cpp khlistbox.cpp \ + gestures.cpp voices.cpp voicesignature.cpp sound.cpp soundrecorder.cpp +libkhotkeys_shared_la_LIBADD = $(LIB_TDEIO) $(XTESTLIB) -lXext -lX11 $(X_PRE_LIBS) +libkhotkeys_shared_la_LDFLAGS = $(all_libraries) $(X_LDFLAGS) -version-info 1:0:0 -no-undefined + +INCLUDES = $(all_includes) + +noinst_HEADERS = action_data.h khotkeysglobal.h triggers.h windows.h actions.h input.h \ + settings.h conditions.h khlistview.h khlistbox.h gestures.h voices.h \ + voicesignature.h sound.h soundrecorder.h + +METASOURCES = AUTO diff --git a/khotkeys/shared/action_data.cpp b/khotkeys/shared/action_data.cpp new file mode 100644 index 000000000..4f481ea59 --- /dev/null +++ b/khotkeys/shared/action_data.cpp @@ -0,0 +1,378 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _ACTION_DATA_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "action_data.h" + +#include <tdeconfig.h> + +#include "actions.h" + +namespace KHotKeys +{ + +// Action_data_base + +Action_data_base::Action_data_base( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, Condition_list* conditions_P, bool enabled_P ) + : _parent( parent_P ), _conditions( conditions_P ), _name( name_P ), _comment( comment_P ), + _enabled( enabled_P ) + { + if( parent()) + parent()->add_child( this ); + if( _conditions != NULL ) + _conditions->set_data( this ); + } + +Action_data_base::Action_data_base( TDEConfig& cfg_P, Action_data_group* parent_P ) + : _parent( parent_P ) + { + TQString save_cfg_group = cfg_P.group(); + _name = cfg_P.readEntry( "Name" ); + _comment = cfg_P.readEntry( "Comment" ); + _enabled = cfg_P.readBoolEntry( "Enabled", true ); + cfg_P.setGroup( save_cfg_group + "Conditions" ); + _conditions = new Condition_list( cfg_P, this ); + cfg_P.setGroup( save_cfg_group ); + if( parent()) + parent()->add_child( this ); + } + +Action_data_base::~Action_data_base() + { +// kdDebug( 1217 ) << "~Action_data_base() :" << this << endl; + if( parent()) + parent()->remove_child( this ); + delete _conditions; + } + +void Action_data_base::cfg_write( TDEConfig& cfg_P ) const + { + cfg_P.writeEntry( "Type", "ERROR" ); // derived classes should call with their type + cfg_P.writeEntry( "Name", name()); + cfg_P.writeEntry( "Comment", comment()); + cfg_P.writeEntry( "Enabled", enabled( true )); + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Conditions" ); + assert( conditions() != NULL ); + conditions()->cfg_write( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + } + + +Action_data_base* Action_data_base::create_cfg_read( TDEConfig& cfg_P, Action_data_group* parent_P ) + { + TQString type = cfg_P.readEntry( "Type" ); + if( type == "ACTION_DATA_GROUP" ) + { + if( cfg_P.readBoolEntry( "AllowMerge", false )) + { + for( Action_data_group::Iterator it = parent_P->first_child(); + it; + ++it ) + { + if( Action_data_group* existing = dynamic_cast< Action_data_group* >( *it )) + { + if( cfg_P.readEntry( "Name" ) == existing->name()) + return existing; + } + } + } + return new Action_data_group( cfg_P, parent_P ); + } + if( type == "GENERIC_ACTION_DATA" ) + return new Generic_action_data( cfg_P, parent_P ); + if( type == "COMMAND_URL_SHORTCUT_ACTION_DATA" ) + return new Command_url_shortcut_action_data( cfg_P, parent_P ); + if( type == "MENUENTRY_SHORTCUT_ACTION_DATA" ) + return new Menuentry_shortcut_action_data( cfg_P, parent_P ); + if( type == "DCOP_SHORTCUT_ACTION_DATA" ) + return new Dcop_shortcut_action_data( cfg_P, parent_P ); + if( type == "KEYBOARD_INPUT_SHORTCUT_ACTION_DATA" ) + return new Keyboard_input_shortcut_action_data( cfg_P, parent_P ); + if( type == "KEYBOARD_INPUT_GESTURE_ACTION_DATA" ) + return new Keyboard_input_gesture_action_data( cfg_P, parent_P ); + if( type == "ACTIVATE_WINDOW_SHORTCUT_ACTION_DATA" ) + return new Activate_window_shortcut_action_data( cfg_P, parent_P ); + kdWarning( 1217 ) << "Unknown Action_data_base type read from cfg file\n"; + return NULL; + } + +bool Action_data_base::cfg_is_enabled( TDEConfig& cfg_P ) + { + return cfg_P.readBoolEntry( "Enabled", true ); + } + +void Action_data_base::reparent( Action_data_group* new_parent_P ) + { + if( parent()) + parent()->remove_child( this ); + _parent = new_parent_P; + if( parent()) + parent()->add_child( this ); + } + +bool Action_data_base::enabled( bool ignore_group_P ) const + { + if( ignore_group_P ) + return _enabled; + else + return _enabled && ( parent() == NULL || parent()->enabled( false )); + } + +bool Action_data_base::conditions_match() const + { + return ( conditions() ? conditions()->match() : true ) + && ( parent() ? parent()->conditions_match() : true ); + } + +// Action_data_group + +Action_data_group::Action_data_group( TDEConfig& cfg_P, Action_data_group* parent_P ) + : Action_data_base( cfg_P, parent_P ) + { + unsigned int system_group_tmp = cfg_P.readUnsignedNumEntry( "SystemGroup", 0 ); + if( system_group_tmp >= SYSTEM_MAX ) + system_group_tmp = 0; + _system_group = static_cast< system_group_t >( system_group_tmp ); + } + +void Action_data_group::cfg_write( TDEConfig& cfg_P ) const + { + Action_data_base::cfg_write( cfg_P ); + cfg_P.writeEntry( "SystemGroup", system_group()); + cfg_P.writeEntry( "Type", "ACTION_DATA_GROUP" ); + } + +void Action_data_group::update_triggers() + { + for( Action_data_group::Iterator it = first_child(); + it; + ++it ) + ( *it )->update_triggers(); + } + +// Action_data + +Action_data::Action_data( TDEConfig& cfg_P, Action_data_group* parent_P ) + : Action_data_base( cfg_P, parent_P ) + { + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Triggers" ); + _triggers = new Trigger_list( cfg_P, this ); + cfg_P.setGroup( save_cfg_group + "Actions" ); + _actions = new Action_list( cfg_P, this ); + cfg_P.setGroup( save_cfg_group ); + } + +Action_data::~Action_data() + { +// kdDebug( 1217 ) << "~Action_data" << this << endl; + delete _triggers; + delete _actions; + // CHECKME jeste remove z parenta ? + } + +void Action_data::cfg_write( TDEConfig& cfg_P ) const + { + Action_data_base::cfg_write( cfg_P ); + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Triggers" ); + triggers()->cfg_write( cfg_P ); + cfg_P.setGroup( save_cfg_group + "Actions" ); + actions()->cfg_write( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + } + +void Action_data::execute() + { + for( Action_list::Iterator it( *_actions ); + it; + ++it ) + it.current()->execute(); +// CHECKME nebo nejak zpozdeni ? + } + +void Action_data::add_trigger( Trigger* trigger_P ) + { + _triggers->append( trigger_P ); + } + +void Action_data::add_triggers( Trigger_list* triggers_P ) + { + for( Trigger_list::Iterator it = *triggers_P; + it; + ++it ) + _triggers->append( *it ); + triggers_P->setAutoDelete( false ); + delete triggers_P; + } + +void Action_data::set_triggers( Trigger_list* triggers_P ) + { + assert( _triggers == NULL ); + _triggers = triggers_P; + } + +void Action_data::add_action( Action* action_P, Action* after_P ) + { + int index = 0; + for( Action_list::Iterator it = *_actions; + it; + ++it ) + { + ++index; + if( *it == after_P ) + break; + } + _actions->insert( index, action_P ); + } + +void Action_data::add_actions( Action_list* actions_P, Action* after_P ) + { + int index = 0; + for( Action_list::Iterator it = *_actions; + it; + ++it ) + { + ++index; + if( *it == after_P ) + break; + } + for( Action_list::Iterator it = *actions_P; + it; + ++it ) + _actions->insert( index++, *it ); + actions_P->setAutoDelete( false ); + delete actions_P; + } + +void Action_data::set_actions( Action_list* actions_P ) + { + assert( _actions == NULL ); + _actions = actions_P; + } + +void Action_data::update_triggers() + { + bool activate = conditions_match() && enabled( false ); + kdDebug( 1217 ) << "Update triggers: " << name() << ":" << activate << endl; + for( Trigger_list::Iterator it = ( *triggers()); + it; + ++it ) + ( *it )->activate( activate ); + } + +// Generic_action_data + +void Generic_action_data::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "GENERIC_ACTION_DATA" ); + } + +// Command_url_shortcut_action_data + +Command_url_shortcut_action_data::Command_url_shortcut_action_data( Action_data_group* parent_P, + const TQString& name_P, const TQString& comment_P, + const TDEShortcut& shortcut_P, const TQString& command_url_P, bool enabled_P ) + : Simple_action_data< Shortcut_trigger, Command_url_action >( parent_P, name_P, + comment_P, enabled_P ) + { + set_action( new Command_url_action( this, command_url_P )); + set_trigger( new Shortcut_trigger( this, shortcut_P )); + } + +template<> KDE_EXPORT +void Simple_action_data< Shortcut_trigger, Command_url_action > + ::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "COMMAND_URL_SHORTCUT_ACTION_DATA" ); + } + +// Menuentry_shortcut_action_data + +Menuentry_shortcut_action_data::Menuentry_shortcut_action_data( Action_data_group* parent_P, + const TQString& name_P, const TQString& comment_P, + const TDEShortcut& shortcut_P, const TQString& menuentry_P, bool enabled_P ) + : Simple_action_data< Shortcut_trigger, Menuentry_action >( parent_P, name_P, + comment_P, enabled_P ) + { + set_action( new Menuentry_action( this, menuentry_P )); + set_trigger( new Shortcut_trigger( this, shortcut_P )); + } + +template<> KDE_EXPORT +void Simple_action_data< Shortcut_trigger, Menuentry_action > + ::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "MENUENTRY_SHORTCUT_ACTION_DATA" ); + } + +// Dcop_shortcut_action_data + +template<> KDE_EXPORT +void Simple_action_data< Shortcut_trigger, Dcop_action > + ::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "DCOP_SHORTCUT_ACTION_DATA" ); + } + +// Keyboard_input_shortcut_action_data + +template<> KDE_EXPORT +void Simple_action_data< Shortcut_trigger, Keyboard_input_action > + ::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "KEYBOARD_INPUT_SHORTCUT_ACTION_DATA" ); + } + +// Activate_window_shortcut_action_data + +template<> KDE_EXPORT +void Simple_action_data< Shortcut_trigger, Activate_window_action > + ::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "ACTIVATE_WINDOW_SHORTCUT_ACTION_DATA" ); + } + +// Keyboard_input_gesture_action_data + +void Keyboard_input_gesture_action_data::set_action( Keyboard_input_action* action_P ) + { + Action_list* tmp = new Action_list( "Keyboard_input_gesture_action_data" ); + tmp->append( action_P ); + set_actions( tmp ); + } + +const Keyboard_input_action* Keyboard_input_gesture_action_data::action() const + { + if( actions() == NULL ) // CHECKME tohle poradne zkontrolovat + return NULL; + return static_cast< Keyboard_input_action* >( const_cast< Action_list* >( actions())->first()); + } + +void Keyboard_input_gesture_action_data::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "KEYBOARD_INPUT_GESTURE_ACTION_DATA" ); + } + +} // namespace KHotKeys diff --git a/khotkeys/shared/action_data.h b/khotkeys/shared/action_data.h new file mode 100644 index 000000000..5d84c6f18 --- /dev/null +++ b/khotkeys/shared/action_data.h @@ -0,0 +1,446 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _ACTION_DATA_H_ +#define _ACTION_DATA_H_ + +#include <assert.h> +#include <tqstring.h> +#include <tqptrlist.h> + +#include <kdebug.h> + +class TDEConfig; + +#include "khotkeysglobal.h" + +#include "windows.h" +#include "conditions.h" +#include "triggers.h" +#include "actions.h" + +namespace KHotKeys +{ + +class Action_data_group; + +class KDE_EXPORT Action_data_base + { + public: + Action_data_base( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, Condition_list* condition_P, bool enabled_P ); + Action_data_base( TDEConfig& cfg_P, Action_data_group* parent_P ); + virtual ~Action_data_base(); + virtual void cfg_write( TDEConfig& cfg_P ) const = 0; + const Condition_list* conditions() const; + Action_data_group* parent() const; + void reparent( Action_data_group* new_parent_P ); + virtual void update_triggers() = 0; + bool conditions_match() const; + const TQString& name() const; + void set_name( const TQString& name_P ); + const TQString& comment() const; + bool enabled( bool ignore_group_P ) const; + static Action_data_base* create_cfg_read( TDEConfig& cfg_P, Action_data_group* parent_P ); + static bool cfg_is_enabled( TDEConfig& cfg_P ); + protected: + void set_conditions( Condition_list* conditions_P ); + private: + Action_data_group* _parent; + Condition_list* _conditions; + TQString _name; + TQString _comment; + bool _enabled; // is not really important, only used in conf. module and when reading cfg. file + KHOTKEYS_DISABLE_COPY( Action_data_base ); + }; + +class KDE_EXPORT Action_data_group + : public Action_data_base + { + public: + enum system_group_t { SYSTEM_NONE, SYSTEM_MENUENTRIES, + SYSTEM_ROOT, /* last one*/ SYSTEM_MAX }; // don't remove entries + Action_data_group( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, Condition_list* conditions_P, system_group_t system_group_P, + bool enabled_P ); + Action_data_group( TDEConfig& cfg_P, Action_data_group* parent_P ); + virtual ~Action_data_group(); + virtual void update_triggers(); + virtual void cfg_write( TDEConfig& cfg_P ) const; + typedef TQPtrListIterator< Action_data_base > Iterator; // CHECKME neni const :( + Iterator first_child() const; + bool is_system_group() const; + system_group_t system_group() const; + using Action_data_base::set_conditions; // make public + protected: + TQPtrList< Action_data_base > list; + system_group_t _system_group; // e.g. menuedit entries, can't be deleted or renamed + friend class Action_data_base; // CHECKME + void add_child( Action_data_base* child_P ); + void remove_child( Action_data_base* child_P ); + }; + +// this one represents a "whole" action, i.e. triggers, resulting actions, etc. +class KDE_EXPORT Action_data + : public Action_data_base + { + typedef Action_data_base base; + public: + Action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, Trigger_list* triggers_P, Condition_list* conditions_P, + Action_list* actions_P, bool enabled_P = true ); + Action_data( TDEConfig& cfg_P, Action_data_group* parent_P ); + virtual ~Action_data(); + virtual void update_triggers(); + virtual void cfg_write( TDEConfig& cfg_P ) const = 0; + virtual void execute(); + const Trigger_list* triggers() const; + const Action_list* actions() const; + protected: + virtual void add_trigger( Trigger* trigger_P ); + virtual void add_triggers( + Trigger_list* triggers_P ); // Trigger_list instance will be deleted + virtual void set_triggers( Trigger_list* triggers_P ); + virtual void add_action( Action* action_P, Action* after_P = NULL ); + virtual void add_actions( Action_list* actions_P, + Action* after_P = NULL ); // Action_list will be deleted + virtual void set_actions( Action_list* actions_P ); + private: + Trigger_list* _triggers; + Action_list* _actions; +#if 0 + action_type_t _type; + static const char* const types[]; +#endif + }; + +class KDE_EXPORT Generic_action_data + : public Action_data + { + typedef Action_data base; + public: + Generic_action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, Trigger_list* triggers_P, Condition_list* conditions_P, + Action_list* actions_P, bool enabled_P = true ); + Generic_action_data( TDEConfig& cfg_P, Action_data_group* parent_P ); + virtual void cfg_write( TDEConfig& cfg_P ) const; + using Action_data_base::set_conditions; // make public + using Action_data::add_trigger; // make public + using Action_data::add_triggers; // make public + using Action_data::set_triggers; // make public + using Action_data::add_action; // make public + using Action_data::add_actions; // make public + using Action_data::set_actions; // make public + }; + +template< typename T, typename A > +class KDE_EXPORT Simple_action_data + : public Action_data + { + typedef Action_data base; + public: + Simple_action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, bool enabled_P = true ); + Simple_action_data( TDEConfig& cfg_P, Action_data_group* parent_P ); + const A* action() const; + const T* trigger() const; + // CHECKME kontrola, ze se dava jen jedna akce ? + void set_action( A* action_P ); + void set_trigger( T* trigger_P ); + virtual void cfg_write( TDEConfig& cfg_P ) const; + }; + +class KDE_EXPORT Command_url_shortcut_action_data + : public Simple_action_data< Shortcut_trigger, Command_url_action > + { + typedef Simple_action_data< Shortcut_trigger, Command_url_action > base; + public: + Command_url_shortcut_action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, bool enabled_P = true ); + Command_url_shortcut_action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, const TDEShortcut& shortcut_P, const TQString& command_url_P, + bool enabled_P = true ); + Command_url_shortcut_action_data( TDEConfig& cfg_P, Action_data_group* parent_P ); + }; + +class KDE_EXPORT Menuentry_shortcut_action_data + : public Simple_action_data< Shortcut_trigger, Menuentry_action > + { + typedef Simple_action_data< Shortcut_trigger, Menuentry_action > base; + public: + Menuentry_shortcut_action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, bool enabled_P = true ); + Menuentry_shortcut_action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, const TDEShortcut& shortcut_P, const TQString& command_url_P, + bool enabled_P = true ); + Menuentry_shortcut_action_data( TDEConfig& cfg_P, Action_data_group* parent_P ); + }; + +typedef Simple_action_data< Shortcut_trigger, Dcop_action > Dcop_shortcut_action_data; +typedef Simple_action_data< Shortcut_trigger, Keyboard_input_action > + Keyboard_input_shortcut_action_data; +typedef Simple_action_data< Shortcut_trigger, Activate_window_action > + Activate_window_shortcut_action_data; + +class KDE_EXPORT Keyboard_input_gesture_action_data + : public Action_data + { + typedef Action_data base; + public: + Keyboard_input_gesture_action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, bool enabled_P = true ); + Keyboard_input_gesture_action_data( TDEConfig& cfg_P, Action_data_group* parent_P ); + const Keyboard_input_action* action() const; + // CHECKME kontrola, ze se dava jen jedna akce ? + void set_action( Keyboard_input_action* action_P ); + enum { NUM_TRIGGERS = 3 }; // needs changing code elsewhere + using Action_data::set_triggers; // make public // CHECKME kontrola poctu? + virtual void cfg_write( TDEConfig& cfg_P ) const; + }; + + +//*************************************************************************** +// Inline +//*************************************************************************** + +// Action_data_base + +inline +const Condition_list* Action_data_base::conditions() const + { +// assert( _conditions != NULL ); + return _conditions; + } + +inline +void Action_data_base::set_conditions( Condition_list* conditions_P ) + { + assert( _conditions == NULL ); + _conditions = conditions_P; + } + +inline +Action_data_group* Action_data_base::parent() const + { + return _parent; + } + +inline +void Action_data_base::set_name( const TQString& name_P ) + { + _name = name_P; + } + +inline +const TQString& Action_data_base::name() const + { + return _name; + } + +inline +const TQString& Action_data_base::comment() const + { + return _comment; + } + +// Action_data_group + +inline +Action_data_group::Action_data_group( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, Condition_list* conditions_P, system_group_t system_group_P, + bool enabled_P ) + : Action_data_base( parent_P, name_P, comment_P, conditions_P, enabled_P ), + _system_group( system_group_P ) + { + } + +inline +Action_data_group::~Action_data_group() + { +// kdDebug( 1217 ) << "~Action_data_group() :" << list.count() << endl; + while( list.first()) + delete list.first(); + } + +inline +Action_data_group::Iterator Action_data_group::first_child() const + { + return Iterator( list ); + } + +inline +bool Action_data_group::is_system_group() const + { + return _system_group != SYSTEM_NONE; + } + +inline +Action_data_group::system_group_t Action_data_group::system_group() const + { + return _system_group; + } + +inline +void Action_data_group::add_child( Action_data_base* child_P ) + { + list.append( child_P ); // CHECKME tohle asi znemozni je mit nejak rucne serazene + } + +inline +void Action_data_group::remove_child( Action_data_base* child_P ) + { + list.removeRef( child_P ); // is not auto-delete + } + +// Action_data + +inline +Action_data::Action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, Trigger_list* triggers_P, Condition_list* conditions_P, + Action_list* actions_P, bool enabled_P ) + : Action_data_base( parent_P, name_P, comment_P, conditions_P, enabled_P ), + _triggers( triggers_P ), _actions( actions_P ) + { + } + +inline +const Trigger_list* Action_data::triggers() const + { +// assert( _triggers != NULL ); + return _triggers; + } + +inline +const Action_list* Action_data::actions() const + { +// assert( _actions != NULL ); + return _actions; + } + +// Generic_action_data + +inline +Generic_action_data::Generic_action_data( Action_data_group* parent_P, const TQString& name_P, + const TQString& comment_P, Trigger_list* triggers_P, Condition_list* conditions_P, + Action_list* actions_P, bool enabled_P ) + : Action_data( parent_P, name_P, comment_P, triggers_P, conditions_P, actions_P, enabled_P ) + { + } + +inline +Generic_action_data::Generic_action_data( TDEConfig& cfg_P, Action_data_group* parent_P ) + : Action_data( cfg_P, parent_P ) + { // CHECKME do nothing ? + } + +// Simple_action_data + +template< typename T, typename A > +inline +Simple_action_data< T, A >::Simple_action_data( Action_data_group* parent_P, + const TQString& name_P, const TQString& comment_P, bool enabled_P ) + : Action_data( parent_P, name_P, comment_P, NULL, + new Condition_list( "", this ), NULL, enabled_P ) + { + } + +template< typename T, typename A > +inline +Simple_action_data< T, A >::Simple_action_data( TDEConfig& cfg_P, Action_data_group* parent_P ) + : Action_data( cfg_P, parent_P ) + { // CHECKME nothing ? + } + +template< typename T, typename A > +void Simple_action_data< T, A >::set_action( A* action_P ) + { + Action_list* tmp = new Action_list( "Simple_action_data" ); + tmp->append( action_P ); + set_actions( tmp ); + } + +template< typename T, typename A > +void Simple_action_data< T, A >::set_trigger( T* trigger_P ) + { + Trigger_list* tmp = new Trigger_list( "Simple_action" ); + tmp->append( trigger_P ); + set_triggers( tmp ); + } + +template< typename T, typename A > +const A* Simple_action_data< T, A >::action() const + { + if( actions() == NULL || actions()->count() == 0 ) // CHECKME tohle poradne zkontrolovat + return NULL; + return static_cast< A* >( const_cast< Action_list* >( actions())->first()); + } + +template< typename T, typename A > +const T* Simple_action_data< T, A >::trigger() const + { + if( triggers() == NULL || triggers()->count() == 0 ) // CHECKME tohle poradne zkontrolovat + return NULL; + return static_cast< T* >( const_cast< Trigger_list* >( triggers())->first()); + } + +// Command_url_action_data + +inline +Command_url_shortcut_action_data::Command_url_shortcut_action_data( Action_data_group* parent_P, + const TQString& name_P, const TQString& comment_P, bool enabled_P ) + : Simple_action_data< Shortcut_trigger, Command_url_action >( parent_P, name_P, + comment_P, enabled_P ) + { + } + +inline +Command_url_shortcut_action_data::Command_url_shortcut_action_data( TDEConfig& cfg_P, + Action_data_group* parent_P ) + : Simple_action_data< Shortcut_trigger, Command_url_action >( cfg_P, parent_P ) + { + } + +// Menuentry_action_data + +inline +Menuentry_shortcut_action_data::Menuentry_shortcut_action_data( Action_data_group* parent_P, + const TQString& name_P, const TQString& comment_P, bool enabled_P ) + : Simple_action_data< Shortcut_trigger, Menuentry_action >( parent_P, name_P, + comment_P, enabled_P ) + { + } + +inline +Menuentry_shortcut_action_data::Menuentry_shortcut_action_data( TDEConfig& cfg_P, + Action_data_group* parent_P ) + : Simple_action_data< Shortcut_trigger, Menuentry_action >( cfg_P, parent_P ) + { + } + +// Keyboard_input_gesture_action_data + +inline +Keyboard_input_gesture_action_data::Keyboard_input_gesture_action_data( + Action_data_group* parent_P, const TQString& name_P, const TQString& comment_P, bool enabled_P ) + : Action_data( parent_P, name_P, comment_P, NULL, + new Condition_list( "", this ), NULL, enabled_P ) + { + } + +inline +Keyboard_input_gesture_action_data::Keyboard_input_gesture_action_data( TDEConfig& cfg_P, + Action_data_group* parent_P ) + : Action_data( cfg_P, parent_P ) + { // CHECKME nothing ? + } + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/actions.cpp b/khotkeys/shared/actions.cpp new file mode 100644 index 000000000..c8590789a --- /dev/null +++ b/khotkeys/shared/actions.cpp @@ -0,0 +1,455 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _ACTIONS_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "actions.h" + +#include <krun.h> +#include <tdeconfig.h> +#include <kdebug.h> +#include <kurifilter.h> +#include <tdeglobal.h> +#include <kstandarddirs.h> +#include <tdeapplication.h> +#include <dcopclient.h> +#include <kdesktopfile.h> +#include <tdelocale.h> +#include <tdeaccel.h> +#include <kservice.h> +#include <kprocess.h> +#include <tdemessagebox.h> + +#include "windows.h" +#include "action_data.h" + +#include <X11/X.h> + +namespace KHotKeys +{ + +// Action + +Action* Action::create_cfg_read( TDEConfig& cfg_P, Action_data* data_P ) + { + TQString type = cfg_P.readEntry( "Type" ); + if( type == "COMMAND_URL" ) + return new Command_url_action( cfg_P, data_P ); + if( type == "MENUENTRY" ) + return new Menuentry_action( cfg_P, data_P ); + if( type == "DCOP" ) + return new Dcop_action( cfg_P, data_P ); + if( type == "KEYBOARD_INPUT" ) + return new Keyboard_input_action( cfg_P, data_P ); + if( type == "ACTIVATE_WINDOW" ) + return new Activate_window_action( cfg_P, data_P ); + kdWarning( 1217 ) << "Unknown Action type read from cfg file\n"; + return NULL; + } + +void Action::cfg_write( TDEConfig& cfg_P ) const + { + cfg_P.writeEntry( "Type", "ERROR" ); // derived classes should call with their type + } + +// Action_list + +Action_list::Action_list( TDEConfig& cfg_P, Action_data* data_P ) + : TQPtrList< Action >() + { + setAutoDelete( true ); + TQString save_cfg_group = cfg_P.group(); + int cnt = cfg_P.readNumEntry( "ActionsCount", 0 ); + for( int i = 0; + i < cnt; + ++i ) + { + cfg_P.setGroup( save_cfg_group + TQString::number( i )); + Action* action = Action::create_cfg_read( cfg_P, data_P ); + if( action ) + append( action ); + } + cfg_P.setGroup( save_cfg_group ); + } + +void Action_list::cfg_write( TDEConfig& cfg_P ) const + { + TQString save_cfg_group = cfg_P.group(); + int i = 0; + for( Iterator it( *this ); + it; + ++it, ++i ) + { + cfg_P.setGroup( save_cfg_group + TQString::number( i )); + it.current()->cfg_write( cfg_P ); + } + cfg_P.setGroup( save_cfg_group ); + cfg_P.writeEntry( "ActionsCount", i ); + } + +// Command_url_action + +Command_url_action::Command_url_action( TDEConfig& cfg_P, Action_data* data_P ) + : Action( cfg_P, data_P ) + { + _command_url = cfg_P.readEntry( "CommandURL" ); + } + +void Command_url_action::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "CommandURL", command_url()); + cfg_P.writeEntry( "Type", "COMMAND_URL" ); // overwrites value set in base::cfg_write() + } + +void Command_url_action::execute() + { + if( command_url().isEmpty()) + return; + KURIFilterData uri; + TQString cmd = command_url(); + static bool sm_ready = false; + if( !sm_ready ) + { + kapp->propagateSessionManager(); + sm_ready = true; + } +// int space_pos = command_url().find( ' ' ); +// if( command_url()[ 0 ] != '\'' && command_url()[ 0 ] != '"' && space_pos > -1 +// && command_url()[ space_pos - 1 ] != '\\' ) +// cmd = command_url().left( space_pos ); // get first 'word' + uri.setData( cmd ); + KURIFilter::self()->filterURI( uri ); + if( uri.uri().isLocalFile() && !uri.uri().hasRef() ) + cmd = uri.uri().path(); + else + cmd = uri.uri().url(); + switch( uri.uriType()) + { + case KURIFilterData::LOCAL_FILE: + case KURIFilterData::LOCAL_DIR: + case KURIFilterData::NET_PROTOCOL: + case KURIFilterData::HELP: + { + ( void ) new KRun( uri.uri()); + break; + } + case KURIFilterData::EXECUTABLE: + { + if (!kapp->authorize("shell_access")) + return; + if( !uri.hasArgsAndOptions()) + { + KService::Ptr service = KService::serviceByDesktopName( cmd ); + if( service != NULL ) + { + if (!KRun::run( *service, KURL::List())) + { + KMessageBox::sorry(0, "<qt>" + i18n("KHotKeys was unable to execute") + " '" + cmd + "'<p>" + i18n("Please verify existence of the service") + "</qt>", i18n("Unable to launch service!")); + } + break; + } + } + // fall though + } + case KURIFilterData::SHELL: + { + if (!kapp->authorize("shell_access")) + return; + if( !KRun::runCommand( + cmd + ( uri.hasArgsAndOptions() ? uri.argsAndOptions() : "" ), + cmd, uri.iconName())) { + KMessageBox::sorry(0, "<qt>" + i18n("KHotKeys was unable to execute") + " '" + cmd + "'<p>" + i18n("Please verify existence and permissions of the executable file") + "</qt>", i18n("Unable to launch program")); + } + break; + } + default: // error + KMessageBox::sorry(0, "<qt>" + i18n("KHotKeys was unable to execute") + " '" + cmd + "'<p>" + i18n("Please verify existence and permissions of the executable file") + "</qt>", i18n("Unable to launch program")); + return; + } + timeout.start( 1000, true ); // 1sec timeout + } + +TQString Command_url_action::description() const + { + return i18n( "Command/URL : " ) + command_url(); + } + +Action* Command_url_action::copy( Action_data* data_P ) const + { + return new Command_url_action( data_P, command_url()); + } + +// Menuentry_action + +void Menuentry_action::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "MENUENTRY" ); // overwrites value set in base::cfg_write() + } + +KService::Ptr Menuentry_action::service() const + { + if (!_service) + { + const_cast<Menuentry_action *>(this)->_service = KService::serviceByStorageId(command_url()); + } + return _service; + } + +void Menuentry_action::execute() + { + (void) service(); + if (!_service) + return; + KRun::run( *_service, KURL::List()); + timeout.start( 1000, true ); // 1sec timeout + } + +TQString Menuentry_action::description() const + { + (void) service(); + return i18n( "Menuentry : " ) + (_service ? _service->name() : TQString::null); + } + +Action* Menuentry_action::copy( Action_data* data_P ) const + { + return new Menuentry_action( data_P, command_url()); + } + +// Dcop_action + +Dcop_action::Dcop_action( TDEConfig& cfg_P, Action_data* data_P ) + : Action( cfg_P, data_P ) + { + app = cfg_P.readEntry( "RemoteApp" ); + obj = cfg_P.readEntry( "RemoteObj" ); + call = cfg_P.readEntry( "Call" ); + args = cfg_P.readEntry( "Arguments" ); + } + +void Dcop_action::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "DCOP" ); // overwrites value set in base::cfg_write() + cfg_P.writeEntry( "RemoteApp", app ); + cfg_P.writeEntry( "RemoteObj", obj ); + cfg_P.writeEntry( "Call", call ); + cfg_P.writeEntry( "Arguments", args ); + } + +void Dcop_action::execute() + { + if( app.isEmpty() || obj.isEmpty() || call.isEmpty()) + return; + TQStringList args_list; + TQString args_str = args; + while( !args_str.isEmpty()) + { + unsigned int pos = 0; + while( args_str[ pos ] == ' ' ) + ++pos; + if( args_str[ pos ] == '\"' || args_str[ pos ] == '\'' ) + { + TQString val = ""; + TQChar sep = args_str[ pos ]; + bool skip = false; + ++pos; + for(; + pos < args_str.length(); + ++pos ) + { + if( args_str[ pos ] == '\\' ) + { + skip = true; + continue; + } + if( !skip && args_str[ pos ] == sep ) + break; + skip = false; + val += args_str[ pos ]; + } + if( pos >= args_str.length()) + return; + ++pos; + args_str = args_str.mid( pos ); + args_list.append( val ); + } + else + { + // one word + if( pos != 0 ) + args_str = args_str.mid( pos ); + int nxt_pos = args_str.find( ' ' ); + args_list.append( args_str.left( nxt_pos )); // should be ok if nxt_pos is -1 + args_str = nxt_pos >= 0 ? args_str.mid( nxt_pos ) : ""; + } + } + kdDebug( 1217 ) << "DCOP call:" << app << ":" << obj << ":" << call << ":" << args_list << endl; + TDEProcess proc; + proc << "dcop" << app << obj << call << args_list; + proc.start( TDEProcess::DontCare ); + } + +TQString Dcop_action::description() const + { + return i18n( "DCOP : " ) + remote_application() + "::" + remote_object() + "::" + + called_function(); + } + +Action* Dcop_action::copy( Action_data* data_P ) const + { + return new Dcop_action( data_P, remote_application(), remote_object(), + called_function(), arguments()); + } + +// Keyboard_input_action + +Keyboard_input_action::Keyboard_input_action( TDEConfig& cfg_P, Action_data* data_P ) + : Action( cfg_P, data_P ) + { + _input = cfg_P.readEntry( "Input" ); + if( cfg_P.readBoolEntry( "IsDestinationWindow" )) + { + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "DestinationWindow" ); + _dest_window = new Windowdef_list( cfg_P ); + _active_window = false; // ignored with _dest_window set anyway + cfg_P.setGroup( save_cfg_group ); + } + else + { + _dest_window = NULL; + _active_window = cfg_P.readBoolEntry( "ActiveWindow" ); + } + } + +Keyboard_input_action::~Keyboard_input_action() + { + delete _dest_window; + } + +void Keyboard_input_action::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "KEYBOARD_INPUT" ); // overwrites value set in base::cfg_write() + cfg_P.writeEntry( "Input", input()); + if( dest_window() != NULL ) + { + cfg_P.writeEntry( "IsDestinationWindow", true ); + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "DestinationWindow" ); + dest_window()->cfg_write( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + } + else + cfg_P.writeEntry( "IsDestinationWindow", false ); + cfg_P.writeEntry( "ActiveWindow", _active_window ); + } + +void Keyboard_input_action::execute() + { + if( input().isEmpty()) + return; + Window w = InputFocus; + if( dest_window() != NULL ) + { + w = windows_handler->find_window( dest_window()); + if( w == None ) + w = InputFocus; + } + else + { + if( !_active_window ) + w = windows_handler->action_window(); + if( w == None ) + w = InputFocus; + } + int last_index = -1, start = 0; + while(( last_index = input().find( ':', last_index + 1 )) != -1 ) // find next ';' + { + TQString key = input().mid( start, last_index - start ).stripWhiteSpace(); + if( key == "Enter" && KKey( key ).keyCodeQt() == 0 ) + key = "Return"; // CHECKE hack + keyboard_handler->send_macro_key( KKey( key ), w ); + start = last_index + 1; + } + // and the last one + TQString key = input().mid( start, input().length()).stripWhiteSpace(); + if( key == "Enter" && KKey( key ).keyCodeQt() == 0 ) + key = "Return"; + keyboard_handler->send_macro_key( KKey( key ), w ); // the rest + XFlush( tqt_xdisplay()); + } + +TQString Keyboard_input_action::description() const + { + TQString tmp = input(); + tmp.replace( '\n', ' ' ); + tmp.truncate( 30 ); + return i18n( "Keyboard input : " ) + tmp; + } + +Action* Keyboard_input_action::copy( Action_data* data_P ) const + { + return new Keyboard_input_action( data_P, input(), + dest_window() ? dest_window()->copy() : NULL, _active_window ); + } + +// Activate_window_action + +Activate_window_action::Activate_window_action( TDEConfig& cfg_P, Action_data* data_P ) + : Action( cfg_P, data_P ) + { + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Window" ); + _window = new Windowdef_list( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + } + +Activate_window_action::~Activate_window_action() + { + delete _window; + } + +void Activate_window_action::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "ACTIVATE_WINDOW" ); // overwrites value set in base::cfg_write() + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Window" ); + window()->cfg_write( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + } + +void Activate_window_action::execute() + { + if( window()->match( windows_handler->active_window())) + return; // is already active + WId win_id = windows_handler->find_window( window()); + if( win_id != None ) + windows_handler->activate_window( win_id ); + } + +TQString Activate_window_action::description() const + { + return i18n( "Activate window : " ) + window()->comment(); + } + +Action* Activate_window_action::copy( Action_data* data_P ) const + { + return new Activate_window_action( data_P, window()->copy()); + } + +} // namespace KHotKeys diff --git a/khotkeys/shared/actions.h b/khotkeys/shared/actions.h new file mode 100644 index 000000000..f381683e1 --- /dev/null +++ b/khotkeys/shared/actions.h @@ -0,0 +1,305 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _ACTIONS_H_ +#define _ACTIONS_H_ + + +#include <tqstring.h> +#include <tqptrlist.h> +#include <tqtimer.h> + +#include <kservice.h> + +class TDEConfig; + +#include "khotkeysglobal.h" + +namespace KHotKeys +{ + +class Action_data; +class Windowdef_list; + +// this one is a base for all "real" resulting actions, e.g. running a command, +// Action_data instances usually contain at least one Action +class KDE_EXPORT Action + { + public: + Action( Action_data* data_P ); + Action( TDEConfig& cfg_P, Action_data* data_P ); + virtual ~Action(); + virtual void execute() = 0; + virtual TQString description() const = 0; + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual Action* copy( Action_data* data_P ) const = 0; + static Action* create_cfg_read( TDEConfig& cfg_P, Action_data* data_P ); + protected: + Action_data* const data; + KHOTKEYS_DISABLE_COPY( Action ); + }; + +class KDE_EXPORT Action_list + : public TQPtrList< Action > + { + public: + Action_list( const TQString& comment_P ); // CHECKME nebo i data ? + Action_list( TDEConfig& cfg_P, Action_data* data_P ); + void cfg_write( TDEConfig& cfg_P ) const; + typedef TQPtrListIterator< Action > Iterator; + const TQString& comment() const; + private: + TQString _comment; + KHOTKEYS_DISABLE_COPY( Action_list ); + }; + +class KDE_EXPORT Command_url_action + : public Action + { + typedef Action base; + public: + Command_url_action( Action_data* data_P, const TQString& command_url_P ); + Command_url_action( TDEConfig& cfg_P, Action_data* data_P ); + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual void execute(); + virtual TQString description() const; + const TQString& command_url() const; + virtual Action* copy( Action_data* data_P ) const; + protected: + TQTimer timeout; + private: + TQString _command_url; + }; + +class KDE_EXPORT Menuentry_action + : public Command_url_action + { + typedef Command_url_action base; + public: + Menuentry_action( Action_data* data_P, const TQString& menuentry_P ); + Menuentry_action( TDEConfig& cfg_P, Action_data* data_P ); + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual void execute(); + virtual TQString description() const; + virtual Action* copy( Action_data* data_P ) const; + KService::Ptr service() const; + private: + KService::Ptr _service; + }; + +class KDE_EXPORT Dcop_action + : public Action + { + typedef Action base; + public: + Dcop_action( Action_data* data_P, const TQString& app_P, const TQString& obj_P, + const TQString& call_P, const TQString& args_P ); + Dcop_action( TDEConfig& cfg_P, Action_data* data_P ); + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual void execute(); + const TQString& remote_application() const; + const TQString& remote_object() const; + const TQString& called_function() const; + const TQString& arguments() const; + virtual TQString description() const; + virtual Action* copy( Action_data* data_P ) const; + private: + TQString app; // CHECKME TQCString ? + TQString obj; + TQString call; + TQString args; + }; + +class KDE_EXPORT Keyboard_input_action + : public Action + { + typedef Action base; + public: + Keyboard_input_action( Action_data* data_P, const TQString& input_P, + const Windowdef_list* dest_window_P, bool active_window_P ); + Keyboard_input_action( TDEConfig& cfg_P, Action_data* data_P ); + virtual ~Keyboard_input_action(); + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual void execute(); + const TQString& input() const; + // send to specific window: dest_window != NULL + // send to active window: dest_window == NULL && activeWindow() == true + // send to action window: dest_window == NULL && activeWindow() == false + const Windowdef_list* dest_window() const; + bool activeWindow() const; + virtual TQString description() const; + virtual Action* copy( Action_data* data_P ) const; + private: + TQString _input; + const Windowdef_list* _dest_window; + bool _active_window; + }; + +class KDE_EXPORT Activate_window_action + : public Action + { + typedef Action base; + public: + Activate_window_action( Action_data* data_P, const Windowdef_list* window_P ); + Activate_window_action( TDEConfig& cfg_P, Action_data* data_P ); + virtual ~Activate_window_action(); + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual void execute(); + const Windowdef_list* window() const; + virtual TQString description() const; + virtual Action* copy( Action_data* data_P ) const; + private: + const Windowdef_list* _window; + }; + +//*************************************************************************** +// Inline +//*************************************************************************** + +// Action + +inline +Action::Action( Action_data* data_P ) + : data( data_P ) + { + } + +inline +Action::Action( TDEConfig&, Action_data* data_P ) + : data( data_P ) + { + } + +inline +Action::~Action() + { + } + +// Action_list + +inline +Action_list::Action_list( const TQString& comment_P ) + : TQPtrList< Action >(), _comment( comment_P ) + { + setAutoDelete( true ); + } + +inline +const TQString& Action_list::comment() const + { + return _comment; + } + +// Command_url_action + +inline +Command_url_action::Command_url_action( Action_data* data_P, const TQString& command_url_P ) + : Action( data_P ), _command_url( command_url_P ) + { + } + +inline +const TQString& Command_url_action::command_url() const + { + return _command_url; + } + +// Menuentry_action + +inline +Menuentry_action::Menuentry_action( Action_data* data_P, const TQString& menuentry_P ) + : Command_url_action( data_P, menuentry_P ) + { + } + +inline +Menuentry_action::Menuentry_action( TDEConfig& cfg_P, Action_data* data_P ) + : Command_url_action( cfg_P, data_P ) + { + } + +// DCOP_action + +inline +Dcop_action::Dcop_action( Action_data* data_P, const TQString& app_P, const TQString& obj_P, + const TQString& call_P, const TQString& args_P ) + : Action( data_P ), app( app_P ), obj( obj_P ), call( call_P ), args( args_P ) + { + } + +inline +const TQString& Dcop_action::remote_application() const + { + return app; + } + +inline +const TQString& Dcop_action::remote_object() const + { + return obj; + } + +inline +const TQString& Dcop_action::called_function() const + { + return call; + } + +inline +const TQString& Dcop_action::arguments() const + { + return args; + } + +// Keyboard_input_action + +inline +Keyboard_input_action::Keyboard_input_action( Action_data* data_P, const TQString& input_P, + const Windowdef_list* dest_window_P, bool active_window_P ) + : Action( data_P ), _input( input_P ), _dest_window( dest_window_P ), _active_window( active_window_P ) + { + } + +inline +const TQString& Keyboard_input_action::input() const + { + return _input; + } + +inline +const Windowdef_list* Keyboard_input_action::dest_window() const + { + return _dest_window; + } + +inline +bool Keyboard_input_action::activeWindow() const + { + return _active_window; + } + +// Activate_window_action + +inline +Activate_window_action::Activate_window_action( Action_data* data_P, + const Windowdef_list* window_P ) + : Action( data_P ), _window( window_P ) + { + } + +inline +const Windowdef_list* Activate_window_action::window() const + { + return _window; + } + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/conditions.cpp b/khotkeys/shared/conditions.cpp new file mode 100644 index 000000000..b09284b44 --- /dev/null +++ b/khotkeys/shared/conditions.cpp @@ -0,0 +1,504 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _CONDITIONS_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "conditions.h" + +#ifdef KHOTKEYS_DEBUG +#include <typeinfo> +#endif + +#include <tdeconfig.h> +#include <kdebug.h> +#include <tdelocale.h> +#include <assert.h> + +#include <X11/Xlib.h> +#include <fixx11h.h> + +#include "action_data.h" + +namespace KHotKeys +{ + +// Condition + +Condition::Condition( Condition_list_base* parent_P ) + : _parent( parent_P ) + { + if( _parent ) + _parent->append( this ); + } + +Condition::Condition( TDEConfig&, Condition_list_base* parent_P ) + : _parent( parent_P ) + { + if( _parent ) + _parent->append( this ); + } + +Condition* Condition::create_cfg_read( TDEConfig& cfg_P, Condition_list_base* parent_P ) + { + TQString type = cfg_P.readEntry( "Type" ); + if( type == "ACTIVE_WINDOW" ) + return new Active_window_condition( cfg_P, parent_P ); + if( type == "EXISTING_WINDOW" ) + return new Existing_window_condition( cfg_P, parent_P ); + if( type == "NOT" ) + return new Not_condition( cfg_P, parent_P ); + if( type == "AND" ) + return new And_condition( cfg_P, parent_P ); + if( type == "OR" ) + return new Or_condition( cfg_P, parent_P ); + kdWarning( 1217 ) << "Unknown Condition type read from cfg file\n"; + return NULL; + } + +Condition::~Condition() + { + if( _parent ) + _parent->remove( this ); + } + + +void Condition::cfg_write( TDEConfig& cfg_P ) const + { + cfg_P.writeEntry( "Type", "ERROR" ); + } + +void Condition::updated() const + { + if( !khotkeys_active()) + return; + assert( _parent != NULL ); + _parent->updated(); + } + +#ifdef KHOTKEYS_DEBUG +void Condition::debug( int depth_P ) + { + char tmp[ 1024 ]; + int i; + for( i = 0; + i < depth_P; + ++i ) + tmp[ i ] = ' '; + tmp[ i ] = '\0'; + kdDebug( 1217 ) << tmp << description() << ":(" << this << ")" << endl; + } + +void Condition::debug_list( const TQPtrList< Condition >& list_P, int depth_P ) + { + char tmp[ 1024 ]; + int i; + for( i = 0; + i < depth_P; + ++i ) + tmp[ i ] = ' '; + tmp[ i ] = '\0'; + for( TQPtrListIterator< Condition > it( list_P ); + it; + ++it ) + (*it)->debug( depth_P + 1 ); + } +#endif + + +// Condition_list_base + +Condition_list_base::Condition_list_base( TDEConfig& cfg_P, Condition_list_base* parent_P ) + : Condition( parent_P ) + { + TQString save_cfg_group = cfg_P.group(); + int cnt = cfg_P.readNumEntry( "ConditionsCount", 0 ); + for( int i = 0; + i < cnt; + ++i ) + { + cfg_P.setGroup( save_cfg_group + TQString::number( i )); + (void) Condition::create_cfg_read( cfg_P, this ); + } + cfg_P.setGroup( save_cfg_group ); + } + +Condition_list_base::~Condition_list_base() + { + while( !isEmpty()) + { + Condition* c = getFirst(); + remove( c ); + delete c; + } + } + +void Condition_list_base::cfg_write( TDEConfig& cfg_P ) const + { + TQString save_cfg_group = cfg_P.group(); + int i = 0; + for( Iterator it( *this ); + it; + ++it, ++i ) + { + cfg_P.setGroup( save_cfg_group + TQString::number( i )); + it.current()->cfg_write( cfg_P ); + } + cfg_P.setGroup( save_cfg_group ); + cfg_P.writeEntry( "ConditionsCount", i ); + } + +bool Condition_list_base::accepts_children() const + { + return true; + } + +#ifdef KHOTKEYS_DEBUG +void Condition_list_base::debug( int depth_P ) + { + char tmp[ 1024 ]; + int i; + for( i = 0; + i < depth_P; + ++i ) + tmp[ i ] = ' '; + tmp[ i ] = '\0'; + kdDebug( 1217 ) << tmp << typeid( *this ).name() << ":(" << this << ")" << endl; + debug_list( *this, depth_P + 1 ); + } +#endif + +// Condition_list + +Condition_list::Condition_list( TDEConfig& cfg_P, Action_data_base* data_P ) + : Condition_list_base( cfg_P, NULL ), data( data_P ) + { + _comment = cfg_P.readEntry( "Comment" ); + } + +void Condition_list::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Comment", comment()); + } + +Condition_list* Condition_list::copy( Action_data_base* data_P ) const + { + Condition_list* ret = new Condition_list( comment(), data_P ); + for( Iterator it( *this ); + it; + ++it ) + ret->append( it.current()->copy( ret )); + return ret; + } + + +bool Condition_list::match() const + { + if( count() == 0 ) // no conditions to match => ok + return true; + for( Iterator it( *this ); + it; + ++it ) + if( it.current()->match()) // OR + return true; + return false; + } + +void Condition_list::updated() const + { + if( !khotkeys_active()) + return; + data->update_triggers(); +// base::updated(); no need to, doesn't have parent + } + +// CHECKME tohle je drobet hack, jeste to zvazit +void Condition_list::set_data( Action_data_base* data_P ) + { + assert( data == NULL || data == data_P ); + data = data_P; + } + +const TQString Condition_list::description() const + { + assert( false ); + return TQString::null; + } + +Condition_list* Condition_list::copy( Condition_list_base* ) const + { + assert( false ); + return NULL; + } + +// Active_window_condition + +Active_window_condition::Active_window_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ) + : Condition( cfg_P, parent_P ) + { + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Window" ); + _window = new Windowdef_list( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + init(); + set_match(); + } + +void Active_window_condition::init() + { + connect( windows_handler, TQT_SIGNAL( active_window_changed( WId )), + this, TQT_SLOT( active_window_changed( WId ))); + } + +bool Active_window_condition::match() const + { + return is_match; + } + +void Active_window_condition::set_match() + { + is_match = window()->match( Window_data( windows_handler->active_window())); + kdDebug( 1217 ) << "Active_window_condition::set_match :" << is_match << endl; + updated(); + } + +void Active_window_condition::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Window" ); + window()->cfg_write( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + cfg_P.writeEntry( "Type", "ACTIVE_WINDOW" ); // overwrites value set in base::cfg_write() + } + +#ifndef COVARIANT_RETURN_BROKEN +Active_window_condition* Active_window_condition::copy( Condition_list_base* parent_P ) const +#else +Condition* Active_window_condition::copy( Condition_list_base* parent_P ) const +#endif + { + return new Active_window_condition( window()->copy(), parent_P ); + } + +const TQString Active_window_condition::description() const + { + return i18n( "Active window: " ) + window()->comment(); + } + +void Active_window_condition::active_window_changed( WId ) + { + set_match(); + } + +Active_window_condition::~Active_window_condition() + { + disconnect( windows_handler, NULL, this, NULL ); + delete _window; + } + +// Existing_window_condition + +Existing_window_condition::Existing_window_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ) + : Condition( cfg_P, parent_P ) + { + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Window" ); + _window = new Windowdef_list( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + init(); + set_match(); + } + +void Existing_window_condition::init() + { + connect( windows_handler, TQT_SIGNAL( window_added( WId )), this, TQT_SLOT( window_added( WId ))); + connect( windows_handler, TQT_SIGNAL( window_removed( WId )), this, TQT_SLOT( window_removed( WId ))); + } + +bool Existing_window_condition::match() const + { + return is_match; + } + +void Existing_window_condition::set_match( WId w_P ) + { + if( w_P != None && !is_match ) + is_match = window()->match( Window_data( w_P )); + else + is_match = windows_handler->find_window( window()) != None; + kdDebug( 1217 ) << "Existing_window_condition::set_match :" << is_match << endl; + updated(); + } + +void Existing_window_condition::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Window" ); + window()->cfg_write( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + cfg_P.writeEntry( "Type", "EXISTING_WINDOW" ); // overwrites value set in base::cfg_write() + } + +#ifndef COVARIANT_RETURN_BROKEN +Existing_window_condition* Existing_window_condition::copy( Condition_list_base* parent_P ) const +#else +Condition* Existing_window_condition::copy( Condition_list_base* parent_P ) const +#endif + { + return new Existing_window_condition( window()->copy(), parent_P ); + } + +const TQString Existing_window_condition::description() const + { + return i18n( "Existing window: " ) + window()->comment(); + } + +void Existing_window_condition::window_added( WId w_P ) + { + set_match( w_P ); + } + +void Existing_window_condition::window_removed( WId ) + { + set_match(); + } + +Existing_window_condition::~Existing_window_condition() + { + disconnect( windows_handler, NULL, this, NULL ); + delete _window; + } + +// Not_condition + +Not_condition::Not_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ) + : Condition_list_base( cfg_P, parent_P ) + { + // CHECKME kontrola poctu ? + } + +bool Not_condition::match() const + { + return condition() ? !condition()->match() : false; + } + +void Not_condition::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "NOT" ); // overwrites value set in base::cfg_write() + } + +Not_condition* Not_condition::copy( Condition_list_base* parent_P ) const + { + Not_condition* ret = new Not_condition( parent_P ); + if( condition()) + ret->append( condition()->copy( ret )); + return ret; + } + +const TQString Not_condition::description() const + { + return i18n( "Not_condition", "Not" ); + } + +bool Not_condition::accepts_children() const + { + return count() == 0; + } + +// And_condition + +And_condition::And_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ) + : Condition_list_base( cfg_P, parent_P ) + { + // CHECKME kontrola poctu ? + } + +bool And_condition::match() const + { + for( Iterator it( *this ); + it; + ++it ) + if( !it.current()->match()) // AND + return false; + return true; // all true (or empty) + } + +void And_condition::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "AND" ); // overwrites value set in base::cfg_write() + } + +And_condition* And_condition::copy( Condition_list_base* parent_P ) const + { + And_condition* ret = new And_condition( parent_P ); + for( Iterator it( *this ); + it; + ++it ) + ret->append( (*it)->copy( ret )); + return ret; + } + +const TQString And_condition::description() const + { + return i18n( "And_condition", "And" ); + } + +// Or_condition + +Or_condition::Or_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ) + : Condition_list_base( cfg_P, parent_P ) + { + // CHECKME kontrola poctu ? + } + +bool Or_condition::match() const + { + if( count() == 0 ) // empty => ok + return true; + for( Iterator it( *this ); + it; + ++it ) + if( it.current()->match()) // OR + return true; + return false; + } + +void Or_condition::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Type", "OR" ); // overwrites value set in base::cfg_write() + } + +Or_condition* Or_condition::copy( Condition_list_base* parent_P ) const + { + Or_condition* ret = new Or_condition( parent_P ); + for( Iterator it( *this ); + it; + ++it ) + ret->append( (*it)->copy( ret )); + return ret; + } + +const TQString Or_condition::description() const + { + return i18n( "Or_condition", "Or" ); + } + +} // namespace KHotKeys + +#include "conditions.moc" diff --git a/khotkeys/shared/conditions.h b/khotkeys/shared/conditions.h new file mode 100644 index 000000000..ff1af7d07 --- /dev/null +++ b/khotkeys/shared/conditions.h @@ -0,0 +1,296 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _CONDITIONS_H_ +#define _CONDITIONS_H_ + +#include <tqobject.h> +#include <tqptrlist.h> +#include <tqstring.h> + +#include "khotkeysglobal.h" +#include "windows.h" + +#include <X11/Xlib.h> +#include <fixx11h.h> + +class TDEConfig; + +namespace KHotKeys +{ + +class Action_data_base; +class Action_data; +class Condition_list_base; + +class KDE_EXPORT Condition + { + public: + Condition( Condition_list_base* parent_P ); + Condition( TDEConfig& cfg_P, Condition_list_base* parent_P ); + virtual ~Condition(); + virtual bool match() const = 0; + virtual void updated() const; // called when the condition changes + virtual void cfg_write( TDEConfig& cfg_P ) const = 0; + virtual const TQString description() const = 0; + virtual Condition* copy( Condition_list_base* parent_P ) const = 0; + const Condition_list_base* parent() const; + Condition_list_base* parent(); + static Condition* create_cfg_read( TDEConfig& cfg_P, Condition_list_base* parent_P ); + protected: + Condition_list_base* const _parent; + KHOTKEYS_DISABLE_COPY( Condition ); + }; + +class KDE_EXPORT Condition_list_base + : public Condition, public TQPtrList< Condition > // inheritance ? + { + typedef Condition base; + public: + Condition_list_base( Condition_list_base* parent_P ); + Condition_list_base( const TQPtrList< Condition >& children_P, + Condition_list_base* parent_P ); + Condition_list_base( TDEConfig& cfg_P, Condition_list_base* parent_P ); + virtual ~Condition_list_base(); + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual bool accepts_children() const; + typedef TQPtrListIterator< Condition > Iterator; + }; + +class KDE_EXPORT Condition_list + : public Condition_list_base + { + typedef Condition_list_base base; + public: + Condition_list( const TQString& comment_P, Action_data_base* data_P ); + Condition_list( TDEConfig& cfg_P, Action_data_base* data_P ); + void cfg_write( TDEConfig& cfg_P ) const; + Condition_list* copy( Action_data_base* data_P ) const; + virtual bool match() const; + const TQString& comment() const; + void set_data( Action_data_base* data_P ); + virtual void updated() const; + virtual Condition_list* copy( Condition_list_base* parent_P ) const; + virtual const TQString description() const; + private: + TQString _comment; + Action_data_base* data; + }; + +class KDE_EXPORT Active_window_condition + : public TQObject, public Condition + { + Q_OBJECT + typedef Condition base; + public: + Active_window_condition( Windowdef_list* window_P, Condition_list_base* parent_P ); + Active_window_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ); + virtual ~Active_window_condition(); + virtual bool match() const; + virtual void cfg_write( TDEConfig& cfg_P ) const; + const Windowdef_list* window() const; +#ifndef COVARIANT_RETURN_BROKEN + virtual Active_window_condition* copy( Condition_list_base* parent_P ) const; +#else + virtual Condition* copy( Condition_list_base* parent_P ) const; +#endif + virtual const TQString description() const; + public slots: + void active_window_changed( WId ); + private: + void init(); + void set_match(); + Windowdef_list* _window; + bool is_match; + }; + +class KDE_EXPORT Existing_window_condition + : public TQObject, public Condition + { + Q_OBJECT + typedef Condition base; + public: + Existing_window_condition( Windowdef_list* window_P, Condition_list_base* parent_P ); + Existing_window_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ); + virtual ~Existing_window_condition(); + virtual bool match() const; + virtual void cfg_write( TDEConfig& cfg_P ) const; + const Windowdef_list* window() const; +#ifndef COVARIANT_RETURN_BROKEN + virtual Existing_window_condition* copy( Condition_list_base* parent_P ) const; +#else + virtual Condition* copy( Condition_list_base* parent_P ) const; +#endif + virtual const TQString description() const; + public slots: + void window_added( WId w_P ); + void window_removed( WId w_P ); + private: + void init(); + void set_match( WId w_P = None ); + Windowdef_list* _window; + bool is_match; + }; + +class KDE_EXPORT Not_condition + : public Condition_list_base + { + typedef Condition_list_base base; + public: + Not_condition( Condition_list_base* parent_P ); + Not_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ); + virtual bool match() const; + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual Not_condition* copy( Condition_list_base* parent_P ) const; + virtual const TQString description() const; + const Condition* condition() const; + virtual bool accepts_children() const; + }; + +class KDE_EXPORT And_condition + : public Condition_list_base + { + typedef Condition_list_base base; + public: + And_condition( Condition_list_base* parent_P ); + And_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ); + virtual bool match() const; + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual And_condition* copy( Condition_list_base* parent_P ) const; + virtual const TQString description() const; + }; + +class KDE_EXPORT Or_condition + : public Condition_list_base + { + typedef Condition_list_base base; + public: + Or_condition( Condition_list_base* parent_P ); + Or_condition( TDEConfig& cfg_P, Condition_list_base* parent_P ); + virtual bool match() const; + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual Or_condition* copy( Condition_list_base* parent_P ) const; + virtual const TQString description() const; + }; + +//*************************************************************************** +// Inline +//*************************************************************************** + +// Condition + +inline +const Condition_list_base* Condition::parent() const + { + return _parent; + } + +inline +Condition_list_base* Condition::parent() + { + return _parent; + } + +// Condition_list_base + +inline +Condition_list_base::Condition_list_base( Condition_list_base* parent_P ) + : Condition( parent_P ), TQPtrList< Condition >() + { + } + +inline +Condition_list_base::Condition_list_base( const TQPtrList< Condition >& children_P, + Condition_list_base* parent_P ) + : Condition( parent_P ), TQPtrList< Condition >( children_P ) + { + } + +// Condition_list + +inline +Condition_list::Condition_list( const TQString& comment_P, Action_data_base* data_P ) + : Condition_list_base( NULL ), _comment( comment_P ), data( data_P ) + { + } + +inline +const TQString& Condition_list::comment() const + { + return _comment; + } + +// Active_window_condition + +inline +Active_window_condition::Active_window_condition( Windowdef_list* window_P, + Condition_list_base* parent_P ) + : Condition( parent_P ), _window( window_P ) + { + init(); + set_match(); + } + +inline +const Windowdef_list* Active_window_condition::window() const + { + return _window; + } + +// Existing_window_condition + +inline +Existing_window_condition::Existing_window_condition( Windowdef_list* window_P, + Condition_list_base* parent_P ) + : Condition( parent_P ), _window( window_P ), is_match( false ) + { + init(); + set_match(); + } + +inline +const Windowdef_list* Existing_window_condition::window() const + { + return _window; + } + +// Not_condition + +inline +Not_condition::Not_condition( Condition_list_base* parent_P ) + : Condition_list_base( parent_P ) + { + } + +inline +const Condition* Not_condition::condition() const + { + return getFirst(); + } + +// And_condition + +inline +And_condition::And_condition( Condition_list_base* parent_P ) + : Condition_list_base( parent_P ) + { + } + +// Or_condition + +inline +Or_condition::Or_condition( Condition_list_base* parent_P ) + : Condition_list_base( parent_P ) + { + } + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/gestures.cpp b/khotkeys/shared/gestures.cpp new file mode 100644 index 000000000..9933ba74f --- /dev/null +++ b/khotkeys/shared/gestures.cpp @@ -0,0 +1,449 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2002 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + + Based on LibStroke : + ( libstroke - an X11 stroke interface library + Copyright (c) 1996,1997,1998,1999 Mark F. Willey, ETLA Technical + There is a reference application available on the LibStroke Home Page: + http://www.etla.net/~willey/projects/libstroke/ ) + +****************************************************************************/ + +#define _GESTURES_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "gestures.h" + +#include <stdlib.h> +#include <math.h> +#include <assert.h> + +#include <X11/Xlib.h> + +#include <tdeapplication.h> +#include <kdebug.h> +#include <kxerrorhandler.h> +#include <kkeynative.h> + +#include "input.h" +#include "windows.h" + +#include "voices.h" + +namespace KHotKeys +{ + +Gesture* gesture_handler; + +Gesture::Gesture( bool /*enabled_P*/, TQObject* parent_P ) + : _enabled( false ), recording( false ), button( 0 ), exclude( NULL ) + { + (void) new DeleteObject( this, parent_P ); + assert( gesture_handler == NULL ); + gesture_handler = this; + connect( &nostroke_timer, TQT_SIGNAL( timeout()), TQT_SLOT( stroke_timeout())); + connect( windows_handler, TQT_SIGNAL( active_window_changed( WId )), + TQT_SLOT( active_window_changed( WId ))); + } + +Gesture::~Gesture() + { + enable( false ); + gesture_handler = NULL; + } + +void Gesture::enable( bool enabled_P ) + { + if( _enabled == enabled_P ) + return; + _enabled = enabled_P; + assert( button != 0 ); + update_grab(); + } + +void Gesture::set_exclude( Windowdef_list* windows_P ) + { + delete exclude; + // check for count() > 0 - empty exclude list means no window is excluded, + // but empty Windowdef_list matches everything + if( windows_P != NULL && windows_P->count() > 0 ) + exclude = windows_P->copy(); + else + exclude = NULL; + update_grab(); + } + +void Gesture::update_grab() + { + if( _enabled && handlers.count() > 0 + && ( exclude == NULL || !exclude->match( Window_data( windows_handler->active_window())))) + { + kapp->removeX11EventFilter( this ); // avoid being installed twice + kapp->installX11EventFilter( this ); + // CHECKME at se grabuje jen kdyz je alespon jedno gesto? + grab_mouse( true ); + } + else + { + grab_mouse( false ); + kapp->removeX11EventFilter( this ); + } + } + +void Gesture::active_window_changed( WId ) + { + update_grab(); + } + +void Gesture::register_handler( TQObject* receiver_P, const char* slot_P ) + { + if( handlers.contains( receiver_P )) + return; + handlers[ receiver_P ] = true; + connect( this, TQT_SIGNAL( handle_gesture( const TQString&, WId )), + receiver_P, slot_P ); + if( handlers.count() == 1 ) + update_grab(); + } + +void Gesture::unregister_handler( TQObject* receiver_P, const char* slot_P ) + { + if( !handlers.contains( receiver_P )) + return; + handlers.remove( receiver_P ); + disconnect( this, TQT_SIGNAL( handle_gesture( const TQString&, WId )), + receiver_P, slot_P ); + if( handlers.count() == 0 ) + update_grab(); + } + +bool Gesture::x11Event( XEvent* ev_P ) + { +/* kdDebug(1217) << k_funcinfo << " ( type = " << ev_P->type << " )" << KeyRelease << " " << KeyPress <<endl; + if( ev_P->type == XKeyPress || ev_P->type == XKeyRelease ) + { + return voice_handler->x11Event( ev_P ); + }*/ + + if( ev_P->type == ButtonPress && ev_P->xbutton.button == button ) + { + kdDebug( 1217 ) << "GESTURE: mouse press" << endl; + stroke.reset(); + stroke.record( ev_P->xbutton.x, ev_P->xbutton.y ); + nostroke_timer.start( timeout, true ); + recording = true; + start_x = ev_P->xbutton.x_root; + start_y = ev_P->xbutton.y_root; + return true; + } + else if( ev_P->type == ButtonRelease && ev_P->xbutton.button == button + && recording ) + { + recording = false; + nostroke_timer.stop(); + stroke.record( ev_P->xbutton.x, ev_P->xbutton.y ); + TQString gesture( stroke.translate()); + if( gesture.isEmpty()) + { + kdDebug( 1217 ) << "GESTURE: replay" << endl; + XAllowEvents( tqt_xdisplay(), AsyncPointer, CurrentTime ); + XUngrabPointer( tqt_xdisplay(), CurrentTime ); + mouse_replay( true ); + return true; + } + kdDebug( 1217 ) << "GESTURE: got: " << gesture << endl; + emit handle_gesture( gesture, windows_handler->window_at_position( start_x, start_y )); + return true; + } + else if( ev_P->type == MotionNotify && recording ) + { // ignore small initial movement + if( nostroke_timer.isActive() + && abs( start_x - ev_P->xmotion.x_root ) < 10 + && abs( start_y - ev_P->xmotion.y_root ) < 10 ) + return true; + nostroke_timer.stop(); + stroke.record( ev_P->xmotion.x, ev_P->xmotion.y ); + } + return false; + } + +void Gesture::stroke_timeout() + { + kdDebug( 1217 ) << "GESTURE: timeout" << endl; + XAllowEvents( tqt_xdisplay(), AsyncPointer, CurrentTime ); + XUngrabPointer( tqt_xdisplay(), CurrentTime ); + mouse_replay( false ); + recording = false; + } + +void Gesture::mouse_replay( bool release_P ) + { + bool was_enabled = _enabled; + enable( false ); + Mouse::send_mouse_button( button, release_P ); + enable( was_enabled ); + } + +void Gesture::grab_mouse( bool grab_P ) + { + if( grab_P ) + { + KXErrorHandler handler; + static int mask[] = { 0, Button1MotionMask, Button2MotionMask, Button3MotionMask, + Button4MotionMask, Button5MotionMask, ButtonMotionMask, ButtonMotionMask, + ButtonMotionMask, ButtonMotionMask }; +#define XCapL KKeyNative::modXLock() +#define XNumL KKeyNative::modXNumLock() +#define XScrL KKeyNative::modXScrollLock() + unsigned int mods[ 8 ] = + { + 0, XCapL, XNumL, XNumL | XCapL, + XScrL, XScrL | XCapL, + XScrL | XNumL, XScrL | XNumL | XCapL + }; +#undef XCapL +#undef XNumL +#undef XScrL + for( int i = 0; + i < 8; + ++i ) + XGrabButton( tqt_xdisplay(), button, mods[ i ], tqt_xrootwin(), False, + ButtonPressMask | ButtonReleaseMask | mask[ button ], GrabModeAsync, GrabModeAsync, + None, None ); + bool err = handler.error( true ); + kdDebug( 1217 ) << "Gesture grab:" << err << endl; + } + else + { + kdDebug( 1217 ) << "Gesture ungrab" << endl; + XUngrabButton( tqt_xdisplay(), button, AnyModifier, tqt_xrootwin()); + } + } + +void Gesture::set_mouse_button( unsigned int button_P ) + { + if( button == button_P ) + return; + if( !_enabled ) + { + button = button_P; + return; + } + grab_mouse( false ); + button = button_P; + grab_mouse( true ); + } + +void Gesture::set_timeout( int timeout_P ) + { + timeout = timeout_P; + } + +Stroke::Stroke() + { + reset(); + points = new point[ MAX_POINTS ]; // CHECKME + } + +Stroke::~Stroke() + { + delete[] points; + } + +void Stroke::reset() + { + min_x = 10000; + min_y = 10000; + max_x = -1; + max_y = -1; + point_count = -1; + } + +bool Stroke::record( int x, int y ) + { + if( point_count >= MAX_POINTS ) + return false; + if( point_count == -1 ) + { + ++point_count; + points[ point_count ].x = x; + points[ point_count ].y = y; + min_x = max_x = x; + min_y = max_y = y; + } + else + { + // interpolate between last and current point + int delx = x - points[ point_count ].x; + int dely = y - points[ point_count ].y; + if( abs( delx ) > abs( dely )) // step by the greatest delta direction + { + float iy = points[ point_count ].y; + // go from the last point to the current, whatever direction it may be + for( int ix = points[ point_count ].x; + ( delx > 0 ) ? ( ix < x ) : ( ix > x ); + ( delx > 0 ) ? ++ix : --ix ) + { + // step the other axis by the correct increment + if( dely < 0 ) + iy -= fabs( dely / ( float ) delx ); + else + iy += fabs( dely / ( float ) delx ); + // add the interpolated point + ++point_count; + if( point_count >= MAX_POINTS ) + return false; + points[ point_count ].x = ix; + points[ point_count ].y = ( int )iy; + } + // add the last point + ++point_count; + if( point_count >= MAX_POINTS ) + return false; + points[ point_count ].x = x; + points[ point_count ].y = y; + // update metrics, it's ok to do it only for the last point + if( x < min_x ) + min_x = x; + if( x > max_x ) + max_x = x; + if( y < min_y ) + min_y = y; + if( y > max_y ) + max_y = y; + } + else + { // same thing, but for dely larger than delx case... + float ix = points[ point_count ].x; + // go from the last point to the current, whatever direction it may be + for( int iy = points[ point_count ].y; + ( dely > 0 ) ? ( iy < y ) : ( iy > y ); + ( dely > 0 ) ? ++iy : --iy ) + { + // step the other axis by the correct increment + if( delx < 0 ) + ix -= fabs( delx / ( float ) dely ); + else + ix += fabs( delx / ( float ) dely ); + // add the interpolated point + ++point_count; + if( point_count >= MAX_POINTS ) + return false; + points[ point_count ].x = ( int )ix; + points[ point_count ].y = iy; + } + // add the last point + ++point_count; + if( point_count >= MAX_POINTS ) + return false; + points[ point_count ].x = x; + points[ point_count ].y = y; + // update metrics, ts's ok to do it only for the last point + if( x < min_x ) + min_x = x; + if( x > max_x ) + max_x = x; + if( y < min_y ) + min_y = y; + if( y > max_y ) + max_y = y; + } + } + return true; + } + +char* Stroke::translate( int min_bin_points_percentage_P, int scale_ratio_P, int min_points_P ) + { + if( point_count < min_points_P ) + return NULL; + // determine size of grid + delta_x = max_x - min_x; + delta_y = max_y - min_y; + if( delta_x > scale_ratio_P * delta_y ) + { + int avg_y = ( max_y + min_y ) / 2; + min_y = avg_y - delta_x / 2; + max_y = avg_y + delta_x / 2; + delta_y = max_y - min_y; + } + else if( delta_y > scale_ratio_P * delta_x ) + { + int avg_x = ( max_x + min_x ) / 2; + min_x = avg_x - delta_y / 2; + max_x = avg_x + delta_y / 2; + delta_x = max_x - min_x; + } + // calculate bin boundary positions + bound_x_1 = min_x + delta_x / 3; + bound_x_2 = min_x + 2 * delta_x / 3; + bound_y_1 = min_y + delta_y / 3; + bound_y_2 = min_y + 2 * delta_y / 3; + + int sequence_count = 0; + // points-->sequence translation scratch variables + int prev_bin = 0; + int current_bin = 0; + int bin_count = 0; +// build string by placing points in bins, collapsing bins and discarding +// those with too few points... + for( int pos = 0; + pos <= point_count; + ++pos ) + { + // figure out which bin the point falls in + current_bin = bin( points[ pos ].x, points[ pos ].y ); + // if this is the first point, consider it the previous bin, too. + if( prev_bin == 0 ) + prev_bin = current_bin; + if( prev_bin == current_bin ) + bin_count++; + else + { // we are moving to a new bin -- consider adding to the sequence + // CHECKME tohle taky konfigurovatelne ? + if( bin_count >= ( min_bin_points_percentage_P * point_count / 100 ) + || sequence_count == 0 ) + { + if( sequence_count >= MAX_SEQUENCE ) + return NULL; + ret_val[ sequence_count++ ] = prev_bin + '0'; + } + // restart counting points in the new bin + bin_count=0; + prev_bin = current_bin; + } + } + + // add the last run of points to the sequence + if( sequence_count >= MAX_SEQUENCE - 1 ) + return NULL; + ret_val[ sequence_count++ ] = current_bin + '0'; + ret_val[ sequence_count ] = 0; // endmark + return ret_val; + } + +/* figure out which bin the point falls in */ +int Stroke::bin( int x, int y ) + { + int bin_num = 1; + if( x > bound_x_1 ) + ++bin_num; + if( x > bound_x_2 ) + ++bin_num; + if( y < bound_y_1 ) + bin_num += 3; + if( y < bound_y_2 ) + bin_num += 3; + return bin_num; + } + +} // namespace KHotKeys + +#include "gestures.moc" diff --git a/khotkeys/shared/gestures.h b/khotkeys/shared/gestures.h new file mode 100644 index 000000000..4f0f3f94c --- /dev/null +++ b/khotkeys/shared/gestures.h @@ -0,0 +1,122 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2002 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _GESTURES_H_ +#define _GESTURES_H_ + +#include <tqwidget.h> +#include <tqtimer.h> + +#include <X11/Xlib.h> +#include <fixx11h.h> + +#include "windows.h" + +namespace KHotKeys +{ + +class Gesture; +KDE_EXPORT extern Gesture* gesture_handler; + +class KDE_EXPORT Stroke + { + public: + // maximum number of numbers in stroke + enum { MAX_SEQUENCE = 25 }; + // largest number of points allowed to be sampled + enum { MAX_POINTS = 5000 }; + // default percentage of sample points in a bin from all points to be valid + enum { MIN_BIN_POINTS_PERCENTAGE = 5 }; + // default threshold of size of smaller axis needed for it to define its own bin size + enum { SCALE_RATIO = 4 }; + // default number of sample points required to have a valid stroke + enum { MIN_POINTS = 10 }; + Stroke(); + ~Stroke(); + bool record( int x, int y ); + char* translate( int min_bin_points_percentage_P = MIN_BIN_POINTS_PERCENTAGE, + int scale_ratio_P = SCALE_RATIO, int min_points_P = MIN_POINTS ); // CHECKME returns ret_val ( see below ) + void reset(); + protected: + int bin( int x, int y ); + // metrics for input stroke + int min_x, min_y; + int max_x, max_y; + int point_count; + int delta_x, delta_y; + int bound_x_1, bound_x_2; + int bound_y_1, bound_y_2; + struct point + { + int x; + int y; + }; + point* points; + char ret_val[ MAX_SEQUENCE ]; + }; + +class KDE_EXPORT Gesture + : public TQWidget // not TQObject because of x11EventFilter() + { + Q_OBJECT + public: + Gesture( bool enabled_P, TQObject* parent_P ); + virtual ~Gesture(); + void enable( bool enable_P ); + void set_mouse_button( unsigned int button_P ); + void set_timeout( int time_P ); + void set_exclude( Windowdef_list* windows_P ); + void register_handler( TQObject* receiver_P, const char* slot_P ); + void unregister_handler( TQObject* receiver_P, const char* slot_P ); + protected: + virtual bool x11Event( XEvent* ev_P ); + private slots: + void stroke_timeout(); + void active_window_changed( WId window_P ); + signals: + void handle_gesture( const TQString &gesture, WId window ); + private: + void update_grab(); + void grab_mouse( bool grab_P ); + void mouse_replay( bool release_P ); + bool _enabled; + Stroke stroke; + int start_x, start_y; + TQTimer nostroke_timer; + bool recording; + unsigned int button; + int timeout; + WId gesture_window; + Windowdef_list* exclude; + TQMap< TQObject*, bool > handlers; // bool is just a dummy + }; + +// Gesture class must be TQWidget derived because of x11Event() +// but it should be TQObject owned -> use a TQObject proxy that will delete it +class DeleteObject + : public TQObject + { + Q_OBJECT + public: + DeleteObject( TQWidget* widget_P, TQObject* parent_P ) + : TQObject( parent_P ), widget( widget_P ) {} + virtual ~DeleteObject() { delete widget; } + private: + TQWidget* widget; + }; + + +//*************************************************************************** +// Inline +//*************************************************************************** + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/input.cpp b/khotkeys/shared/input.cpp new file mode 100644 index 000000000..7b9696c7e --- /dev/null +++ b/khotkeys/shared/input.cpp @@ -0,0 +1,253 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _INPUT_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "input.h" + +#include <assert.h> +#include <tqwidget.h> + +#include <kglobalaccel.h> +#include <kdebug.h> +#include <tdeapplication.h> +#include <tdeversion.h> +#include <tqtimer.h> +#include <kkeynative.h> + +#include "khotkeysglobal.h" + +#include <X11/Xlib.h> +#include <X11/keysym.h> + +#include "windows.h" + +namespace KHotKeys +{ + +// Kbd + +Kbd::Kbd( bool grabbing_enabled_P, TQObject* parent_P ) + : TQObject( parent_P ) + { + assert( keyboard_handler == NULL ); + keyboard_handler = this; + kga = new TDEGlobalAccel( NULL ); + kga->setEnabled( grabbing_enabled_P ); + } + +Kbd::~Kbd() + { + keyboard_handler = NULL; + delete kga; + } + +void Kbd::insert_item( const TDEShortcut& shortcut_P, Kbd_receiver* receiver_P ) + { + Receiver_data& rcv = receivers[ receiver_P ]; + rcv.shortcuts.append( shortcut_P ); + if( rcv.active ) + grab_shortcut( shortcut_P ); + } + +void Kbd::remove_item( const TDEShortcut& shortcut_P, Kbd_receiver* receiver_P ) + { + Receiver_data& rcv = receivers[ receiver_P ]; + rcv.shortcuts.remove( shortcut_P ); + if( rcv.active ) + ungrab_shortcut( shortcut_P ); + if( rcv.shortcuts.count() == 0 ) + receivers.remove( receiver_P ); + } + +void Kbd::activate_receiver( Kbd_receiver* receiver_P ) + { + Receiver_data& rcv = receivers[ receiver_P ]; + if( rcv.active ) + return; + rcv.active = true; + for( TQValueList< TDEShortcut >::ConstIterator it( rcv.shortcuts.begin()); + it != rcv.shortcuts.end(); + ++it ) + grab_shortcut( *it ); + } + +void Kbd::deactivate_receiver( Kbd_receiver* receiver_P ) + { + Receiver_data& rcv = receivers[ receiver_P ]; + if( !rcv.active ) + return; + rcv.active = false; + for( TQValueList< TDEShortcut >::ConstIterator it( rcv.shortcuts.begin()); + it != rcv.shortcuts.end(); + ++it ) + ungrab_shortcut( *it ); + } + +void Kbd::grab_shortcut( const TDEShortcut& shortcut_P ) + { + if( grabs.contains( shortcut_P )) + ++grabs[ shortcut_P ]; + else + { + grabs[ shortcut_P ] = 1; +#if 0 + // CHECKME ugly ugly hack + TQString name = ' ' + TQString::number( keycode_P ); + kga->insertItem( "", name, keycode_P ); + kga->connectItem( name, this, TQT_SLOT( key_slot( int ))); +#endif + TQString name = ' ' + shortcut_P.toStringInternal(); + kga->insert( name, name, TQString::null, shortcut_P, shortcut_P, + this, TQT_SLOT( key_slot( TQString ))); + TQTimer::singleShot( 0, this, TQT_SLOT( update_connections())); + } + } + +void Kbd::ungrab_shortcut( const TDEShortcut& shortcut_P ) + { + if( !grabs.contains( shortcut_P )) + return; + if( --grabs[ shortcut_P ] == 0 ) + { +#if 0 + // CHECKME workaround for TDEGlobalAccel::disconnectItem() not working + kga->setItemEnabled( ' ' + TQString::number( keycode_P ), false ); + // kga->disconnectItem( ' ' + TQString::number( keycode_P ), NULL, NULL ); + kga->removeItem( ' ' + TQString::number( keycode_P )); +#endif + kga->remove( ' ' + shortcut_P.toStringInternal()); + grabs.remove( shortcut_P ); + TQTimer::singleShot( 0, this, TQT_SLOT( update_connections())); + } + } + +void Kbd::update_connections() + { + kga->updateConnections(); + } + +void Kbd::key_slot( TQString key_P ) + { + kdDebug( 1217 ) << "Key pressed:" << key_P << endl; + TDEShortcut shortcut( key_P ); + if( !grabs.contains( shortcut )) + return; + for( TQMap< Kbd_receiver*, Receiver_data >::ConstIterator it = receivers.begin(); + it != receivers.end(); + ++it ) + if( ( *it ).shortcuts.contains( shortcut ) && ( *it ).active + && it.key()->handle_key( shortcut )) + return; + } + + +#ifdef HAVE_XTEST + +} // namespace KHotKeys +#include <X11/extensions/XTest.h> +namespace KHotKeys +{ + +static bool xtest_available = false; +static bool xtest_inited = false; +static bool xtest() + { + if( xtest_inited ) + return xtest_available; + xtest_inited = true; + int dummy1, dummy2, dummy3, dummy4; + xtest_available = + ( XTestQueryExtension( tqt_xdisplay(), &dummy1, &dummy2, &dummy3, &dummy4 ) == True ); + return xtest_available; + } +#endif + +// CHECKME nevola XFlush(), musi se pak volat rucne +bool Kbd::send_macro_key( const KKey& key, Window window_P ) + { + unsigned int keysym = KKeyNative( key ).sym(); + KeyCode x_keycode = XKeysymToKeycode( tqt_xdisplay(), keysym ); + if( x_keycode == NoSymbol ) + return false; + unsigned int x_mod = KKeyNative( key ).mod(); +#ifdef HAVE_XTEST + if( xtest() && window_P == None ) + { + // CHECKME tohle jeste potrebuje modifikatory + bool ret = XTestFakeKeyEvent( tqt_xdisplay(), x_keycode, True, CurrentTime ); + ret = ret && XTestFakeKeyEvent( tqt_xdisplay(), x_keycode, False, CurrentTime ); + return ret; + } +#endif + if( window_P == None || window_P == InputFocus ) + window_P = windows_handler->active_window(); + if( window_P == None ) // CHECKME tohle cele je ponekud ... + window_P = InputFocus; + XEvent ev; + ev.type = KeyPress; + ev.xkey.display = tqt_xdisplay(); + ev.xkey.window = window_P; + ev.xkey.root = tqt_xrootwin(); // I don't know whether these have to be set + ev.xkey.subwindow = None; // to these values, but it seems to work, hmm + ev.xkey.time = CurrentTime; + ev.xkey.x = 0; + ev.xkey.y = 0; + ev.xkey.x_root = 0; + ev.xkey.y_root = 0; + ev.xkey.keycode = x_keycode; + ev.xkey.state = x_mod; + ev.xkey.same_screen = True; + bool ret = XSendEvent( tqt_xdisplay(), window_P, True, KeyPressMask, &ev ); +#if 1 + ev.type = KeyRelease; // is this actually really needed ?? + ev.xkey.display = tqt_xdisplay(); + ev.xkey.window = window_P; + ev.xkey.root = tqt_xrootwin(); + ev.xkey.subwindow = None; + ev.xkey.time = CurrentTime; + ev.xkey.x = 0; + ev.xkey.y = 0; + ev.xkey.x_root = 0; + ev.xkey.y_root = 0; + ev.xkey.state = x_mod; + ev.xkey.keycode = x_keycode; + ev.xkey.same_screen = True; + ret = ret && XSendEvent( tqt_xdisplay(), window_P, True, KeyReleaseMask, &ev ); +#endif + // Qt's autorepeat compression is broken and can create "aab" from "aba" + // XSync() should create delay longer than Qt's max autorepeat interval + XSync( tqt_xdisplay(), False ); + return ret; + } + +bool Mouse::send_mouse_button( int button_P, bool release_P ) + { +#ifdef HAVE_XTEST + if( xtest()) + { + // CHECKME tohle jeste potrebuje modifikatory + // a asi i spravnou timestamp misto CurrentTime + bool ret = XTestFakeButtonEvent( tqt_xdisplay(), button_P, True, CurrentTime ); + if( release_P ) + ret = ret && XTestFakeButtonEvent( tqt_xdisplay(), button_P, False, CurrentTime ); + return ret; + } +#endif + return false; + } + +} // namespace KHotKeys + +#include "input.moc" diff --git a/khotkeys/shared/input.h b/khotkeys/shared/input.h new file mode 100644 index 000000000..c6bbdf1e5 --- /dev/null +++ b/khotkeys/shared/input.h @@ -0,0 +1,87 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _INPUT_H_ +#define _INPUT_H_ + +#include <tqobject.h> +#include <tqwindowdefs.h> +#include <tqmap.h> +#include <tqwidget.h> +#include <tqvaluelist.h> +#include <tdeshortcut.h> + +#include <X11/X.h> +#include <fixx11h.h> + +class TDEGlobalAccel; + +namespace KHotKeys +{ + +class Kbd_receiver + { + public: + virtual bool handle_key( const TDEShortcut& shortcut_P ) = 0; + }; + +class Kbd + : public TQObject + { + Q_OBJECT + public: + Kbd( bool grabbing_enabled_P, TQObject* parent_P ); + virtual ~Kbd(); + void insert_item( const TDEShortcut& shortcut_P, Kbd_receiver* receiver_P ); + void remove_item( const TDEShortcut& shortcut_P, Kbd_receiver* receiver_P ); + void activate_receiver( Kbd_receiver* receiver_P ); + void deactivate_receiver( Kbd_receiver* receiver_P ); + static bool send_macro_key( const KKey& key, Window window_P = InputFocus ); + protected: + bool x11EventFilter( const XEvent* ); + void grab_shortcut( const TDEShortcut& shortcut_P ); + void ungrab_shortcut( const TDEShortcut& shortcut_P ); + private slots: + void key_slot( TQString key_P ); + void update_connections(); + private: + struct Receiver_data + { + Receiver_data(); + TQValueList< TDEShortcut > shortcuts; + bool active; + }; + TQMap< Kbd_receiver*, Receiver_data > receivers; + TQMap< TDEShortcut, int > grabs; + TDEGlobalAccel* kga; + }; + +class Mouse + { + public: + static bool send_mouse_button( int button_P, bool release_P ); + }; + + +//*************************************************************************** +// Inline +//*************************************************************************** + +// Kbd::Receiver_data + +inline +Kbd::Receiver_data::Receiver_data() + : active( false ) + { + } + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/khlistbox.cpp b/khotkeys/shared/khlistbox.cpp new file mode 100644 index 000000000..ff6d96221 --- /dev/null +++ b/khotkeys/shared/khlistbox.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2002 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _KHLISTBOX_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "khlistbox.h" + +#include <kdebug.h> + +namespace KHotKeys +{ + +KHListBox::KHListBox( TQWidget* parent_P, const char* name_P ) + : TQListBox( parent_P, name_P ), saved_current_item( NULL ), + in_clear( false ), force_select( false ) + { + connect( this, TQT_SIGNAL( selectionChanged( TQListBoxItem* )), + TQT_SLOT( slot_selection_changed( TQListBoxItem* ))); + connect( this, TQT_SIGNAL( currentChanged( TQListBoxItem* )), + TQT_SLOT( slot_current_changed( TQListBoxItem* ))); + // CHECKME grrr + connect( this, TQT_SIGNAL( selectionChanged()), + TQT_SLOT( slot_selection_changed())); + connect( &insert_select_timer, TQT_SIGNAL( timeout()), + TQT_SLOT( slot_insert_select())); + } + +void KHListBox::slot_selection_changed() + { + if( saved_current_item == NULL ) + slot_selection_changed( NULL ); + else if( !saved_current_item->isSelected()) // no way + setSelected( saved_current_item, true ); + } + +void KHListBox::slot_selection_changed( TQListBoxItem* item_P ) + { + if( item_P == saved_current_item ) + return; + saved_current_item = item_P; + setCurrentItem( saved_current_item ); + emit current_changed( saved_current_item ); + } + +void KHListBox::slot_current_changed( TQListBoxItem* item_P ) + { + insert_select_timer.stop(); + if( item_P == saved_current_item ) + return; + saved_current_item = item_P; + setSelected( saved_current_item, true ); + emit current_changed( saved_current_item ); + } + +// neni virtual :(( +void KHListBox::clear() + { + in_clear = true; + TQListBox::clear(); + in_clear = false; + slot_selection_changed( NULL ); + } + + +// neni virtual :(( a vubec nefunguje +void KHListBox::insertItem( TQListBoxItem* item_P ) + { + bool set = false; + if( !in_clear ) + set = count() == 0; + TQListBox::insertItem( item_P ); + if( set && force_select ) + { + bool block = signalsBlocked(); + blockSignals( true ); + setCurrentItem( item_P ); + blockSignals( block ); + insert_select_timer.start( 0, true ); + } + } + +// items are often inserted using the TQListBoxItem constructor, +// which means that a derived class are not yet fully created +void KHListBox::slot_insert_select() + { + slot_current_changed( item( currentItem())); + } + + +} // namespace KHotKeys + +#include "khlistbox.moc" diff --git a/khotkeys/shared/khlistbox.h b/khotkeys/shared/khlistbox.h new file mode 100644 index 000000000..d0f9b51ae --- /dev/null +++ b/khotkeys/shared/khlistbox.h @@ -0,0 +1,64 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2002 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _KHLISTBOX_H_ +#define _KHLISTBOX_H_ + +#include <tqtimer.h> + +#include <tdelistbox.h> + +namespace KHotKeys +{ + +class KHListBox + : public TQListBox + { + Q_OBJECT + TQ_PROPERTY( bool forceSelect READ forceSelect WRITE setForceSelect ) + public: + KHListBox( TQWidget* parent_P, const char* name_P = NULL ); + virtual void clear(); + virtual void insertItem( TQListBoxItem* item_P ); + bool forceSelect() const; + void setForceSelect( bool force_P ); + signals: + void current_changed( TQListBoxItem* item_P ); + private slots: + void slot_selection_changed( TQListBoxItem* item_P ); + void slot_selection_changed(); + void slot_current_changed( TQListBoxItem* item_P ); + void slot_insert_select(); + private: + TQListBoxItem* saved_current_item; + bool in_clear; + bool force_select; + TQTimer insert_select_timer; + }; + +//*************************************************************************** +// Inline +//*************************************************************************** + +inline +void KHListBox::setForceSelect( bool force_P ) + { + force_select = force_P; + } + +inline +bool KHListBox::forceSelect() const + { + return force_select; + } + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/khlistview.cpp b/khotkeys/shared/khlistview.cpp new file mode 100644 index 000000000..ae348af33 --- /dev/null +++ b/khotkeys/shared/khlistview.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2002 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _KHLISTVIEW_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "khlistview.h" + +#include <kdebug.h> + +namespace KHotKeys +{ + +KHListView::KHListView( TQWidget* parent_P, const char* name_P ) + : TDEListView( parent_P, name_P ), saved_current_item( NULL ), + in_clear( false ), ignore( false ), force_select( false ) + { + connect( this, TQT_SIGNAL( selectionChanged( TQListViewItem* )), + TQT_SLOT( slot_selection_changed( TQListViewItem* ))); + connect( this, TQT_SIGNAL( currentChanged( TQListViewItem* )), + TQT_SLOT( slot_current_changed( TQListViewItem* ))); + // CHECKME grrr + connect( this, TQT_SIGNAL( selectionChanged()), + TQT_SLOT( slot_selection_changed())); + connect( &insert_select_timer, TQT_SIGNAL( timeout()), + TQT_SLOT( slot_insert_select())); + } + +void KHListView::slot_selection_changed() + { + if( ignore ) + return; + if( saved_current_item == NULL ) + slot_selection_changed( NULL ); + else if( !saved_current_item->isSelected()) // no way + setSelected( saved_current_item, true ); + } + +void KHListView::slot_selection_changed( TQListViewItem* item_P ) + { + if( ignore ) + return; + if( item_P == saved_current_item ) + return; + saved_current_item = item_P; + setCurrentItem( saved_current_item ); + emit current_changed( saved_current_item ); + } + +void KHListView::slot_current_changed( TQListViewItem* item_P ) + { + if( ignore ) + return; + insert_select_timer.stop(); + if( item_P == saved_current_item ) + return; + saved_current_item = item_P; + setSelected( saved_current_item, true ); + emit current_changed( saved_current_item ); + } + +void KHListView::clear() + { + in_clear = true; + TDEListView::clear(); + in_clear = false; + slot_selection_changed( NULL ); + } + +void KHListView::insertItem( TQListViewItem* item_P ) + { + bool set = false; + if( !in_clear ) + set = childCount() == 0; + TDEListView::insertItem( item_P ); + if( set && force_select ) + { + bool block = signalsBlocked(); + blockSignals( true ); +// SELI tohle spis jen blokovat sebe? + setCurrentItem( item_P ); + blockSignals( block ); + insert_select_timer.start( 0, true ); + } + } + +void KHListView::clearSelection() + { + TDEListView::clearSelection(); + slot_current_changed( currentItem()); + } + +// items are often inserted using the TQListViewItem constructor, +// which means that a derived class are not yet fully created +void KHListView::slot_insert_select() + { + if( ignore ) + return; + slot_current_changed( currentItem()); + } + +void KHListView::contentsDropEvent( TQDropEvent* e ) + { + bool save_ignore = ignore; + ignore = true; + TDEListView::contentsDropEvent( e ); + ignore = save_ignore; + } + +} // namespace KHotKeys + +#include "khlistview.moc" diff --git a/khotkeys/shared/khlistview.h b/khotkeys/shared/khlistview.h new file mode 100644 index 000000000..2cee34617 --- /dev/null +++ b/khotkeys/shared/khlistview.h @@ -0,0 +1,69 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2002 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _KHLISTVIEW_H_ +#define _KHLISTVIEW_H_ + +#include <tqtimer.h> + +#include <tdelistview.h> +#include <kdemacros.h> + +namespace KHotKeys +{ + +class KDE_EXPORT KHListView + : public TDEListView + { + Q_OBJECT + TQ_PROPERTY( bool forceSelect READ forceSelect WRITE setForceSelect ) + public: + KHListView( TQWidget* parent_P, const char* name_P = NULL ); + virtual void clear(); + virtual void insertItem( TQListViewItem* item_P ); + virtual void clearSelection(); + bool forceSelect() const; + void setForceSelect( bool force_P ); + signals: + void current_changed( TQListViewItem* item_P ); + protected: + virtual void contentsDropEvent (TQDropEvent*); + private slots: + void slot_selection_changed( TQListViewItem* item_P ); + void slot_selection_changed(); + void slot_current_changed( TQListViewItem* item_P ); + void slot_insert_select(); + private: + TQListViewItem* saved_current_item; + bool in_clear; + bool ignore; + bool force_select; + TQTimer insert_select_timer; + }; + +//*************************************************************************** +// Inline +//*************************************************************************** + +inline +void KHListView::setForceSelect( bool force_P ) + { + force_select = force_P; + } + +inline +bool KHListView::forceSelect() const + { + return force_select; + } + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/khotkeysglobal.cpp b/khotkeys/shared/khotkeysglobal.cpp new file mode 100644 index 000000000..87fe74c38 --- /dev/null +++ b/khotkeys/shared/khotkeysglobal.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _KHOTKEYSGLOBAL_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "khotkeysglobal.h" + +#include <assert.h> +#include <kdebug.h> +#include <kstandarddirs.h> +#include <klibloader.h> + +#include "input.h" +#include "windows.h" +#include "triggers.h" +#include "gestures.h" +#include "voices.h" +#include "soundrecorder.h" + +namespace KHotKeys +{ + +Kbd* keyboard_handler; +Windows* windows_handler; +static bool _khotkeys_active = false; + +void init_global_data( bool active_P, TQObject* owner_P ) + { + assert( keyboard_handler == NULL ); + assert( windows_handler == NULL ); + assert( gesture_handler == NULL ); + static_cast< void >( new Kbd( active_P, owner_P )); + static_cast< void >( new Windows( active_P, owner_P )); + static_cast< void >( new Gesture( active_P, owner_P )); + static_cast< void >( new Voice( active_P, owner_P )); + khotkeys_set_active( false ); + } + +void khotkeys_set_active( bool active_P ) + { + _khotkeys_active = active_P; + } + +bool khotkeys_active() + { + return _khotkeys_active; + } + +// does the opposite of TDEStandardDirs::findResource() i.e. e.g. +// "/opt/kde2/share/applnk/System/konsole.desktop" -> "System/konsole.desktop" +TQString get_menu_entry_from_path( const TQString& path_P ) + { + TQStringList dirs = TDEGlobal::dirs()->resourceDirs( "apps" ); + for( TQStringList::ConstIterator it = dirs.begin(); + it != dirs.end(); + ++it ) + if( path_P.find( *it ) == 0 ) + { + TQString ret = path_P; + ret.remove( 0, (*it).length()); + if( ret[ 0 ] == '/' ) + ret.remove( 0, 1 ); + return ret; + } + return path_P; + } + +static int have_arts = -1; + +bool haveArts() + { + if( have_arts == -1 ) + { + have_arts = 0; + KLibrary* arts = KLibLoader::self()->library( "khotkeys_arts" ); + if( arts == NULL ) + kdDebug( 1217 ) << "Couldn't load khotkeys_arts:" << KLibLoader::self()->lastErrorMessage() << endl; + if( arts != NULL && SoundRecorder::init( arts )) + have_arts = 1; + } + return have_arts != 0; + } + +void disableArts() + { + have_arts = 0; + } + +} // namespace KHotKeys diff --git a/khotkeys/shared/khotkeysglobal.h b/khotkeys/shared/khotkeysglobal.h new file mode 100644 index 000000000..c97ef1dea --- /dev/null +++ b/khotkeys/shared/khotkeysglobal.h @@ -0,0 +1,59 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _KHOTKEYSGLOBAL_H_ +#define _KHOTKEYSGLOBAL_H_ + +#define KHOTKEYS_VERSION "2.1" +#define KHOTKEYS_CONFIG_FILE "khotkeysrc" + +//#ifndef NDEBUG +//#define KHOTKEYS_DEBUG +//#endif + +#include <tqstring.h> + +#include <tdelocale.h> + +class TDEConfig; +class TQObject; + +namespace KHotKeys +{ + +class Kbd; +class Windows; +class Action_data_group; + +extern Kbd* keyboard_handler; +extern Windows* windows_handler; + +#define KHOTKEYS_DISABLE_COPY( cls ) private: cls( const cls& ); cls& operator=( const cls& ) + +// CHECKME hmms :( +KDE_EXPORT bool khotkeys_active(); +KDE_EXPORT void khotkeys_set_active( bool active_P ); + +TQString get_menu_entry_from_path( const TQString& path_P ); + +KDE_EXPORT void init_global_data( bool active_P, TQObject* owner_P ); + +const char* const MENU_EDITOR_ENTRIES_GROUP_NAME = I18N_NOOP( "Menu Editor entries" ); + +KDE_EXPORT bool haveArts(); +KDE_EXPORT void disableArts(); + +//*************************************************************************** +// Inline +//*************************************************************************** + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/settings.cpp b/khotkeys/shared/settings.cpp new file mode 100644 index 000000000..76adac6b9 --- /dev/null +++ b/khotkeys/shared/settings.cpp @@ -0,0 +1,265 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _SETTINGS_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "settings.h" + +#include <tdeconfig.h> +#include <kdebug.h> +#include <tdelocale.h> +#include <tdeaccel.h> +#include <tdeglobal.h> +#include <tdemessagebox.h> + +#include "triggers.h" +#include "conditions.h" +#include "action_data.h" + +namespace KHotKeys +{ + +// Settings + +Settings::Settings() + : actions( NULL ), gestures_exclude( NULL ) + { + } + +bool Settings::read_settings( bool include_disabled_P ) + { + TDEConfig cfg( KHOTKEYS_CONFIG_FILE, true ); + return read_settings( cfg, include_disabled_P, ImportNone ); + } + +bool Settings::import( TDEConfig& cfg_P, bool ask_P ) + { + return read_settings( cfg_P, true, ask_P ? ImportAsk : ImportSilent ); + } + +bool Settings::read_settings( TDEConfig& cfg_P, bool include_disabled_P, ImportType import_P ) + { + if( actions == NULL ) + actions = new Action_data_group( NULL, "should never see", "should never see", + NULL, Action_data_group::SYSTEM_ROOT, true ); + if( cfg_P.groupList().count() == 0 ) // empty + return false; + cfg_P.setGroup( "Main" ); // main group + if( import_P == ImportNone ) // reading main cfg file + already_imported = cfg_P.readListEntry( "AlreadyImported" ); + else + { + TQString import_id = cfg_P.readEntry( "ImportId" ); + if( !import_id.isEmpty()) + { + if( already_imported.contains( import_id )) + { + if( import_P == ImportSilent + || KMessageBox::warningContinueCancel( NULL, + i18n( "This \"actions\" file has already been imported before. " + "Are you sure you want to import it again?" )) != KMessageBox::Continue ) + return true; // import "successful" + } + else + already_imported.append( import_id ); + } + else + { + if( import_P != ImportSilent + && KMessageBox::warningContinueCancel( NULL, + i18n( "This \"actions\" file has no ImportId field and therefore it cannot be determined " + "whether or not it has been imported already. Are you sure you want to import it?" )) + == KMessageBox::Cancel ) + return true; + } + } + int version = cfg_P.readNumEntry( "Version", -1234576 ); + switch( version ) + { + case 1: + read_settings_v1( cfg_P ); + break; + case 2: + read_settings_v2( cfg_P, include_disabled_P ); + break; + default: + kdWarning( 1217 ) << "Unknown cfg. file version\n"; + return false; + case -1234576: // no config file + if( import_P ) // if importing, this is an error + return false; + break; + } + if( import_P != ImportNone ) + return true; // don't read global settings + cfg_P.setGroup( "Main" ); // main group + daemon_disabled = cfg_P.readBoolEntry( "Disabled", false ); + cfg_P.setGroup( "Gestures" ); + gestures_disabled_globally = cfg_P.readBoolEntry( "Disabled", true ); + gesture_mouse_button = cfg_P.readNumEntry( "MouseButton", 2 ); + gesture_mouse_button = KCLAMP( gesture_mouse_button, 2, 9 ); + gesture_timeout = cfg_P.readNumEntry( "Timeout", 300 ); + cfg_P.setGroup( "GesturesExclude" ); + delete gestures_exclude; + gestures_exclude = new Windowdef_list( cfg_P ); + cfg_P.setGroup( "Voice" ); + voice_shortcut=TDEShortcut( cfg_P.readEntry("Shortcut" , "") ); + return true; + } + +void Settings::write_settings() + { + TDEConfig cfg( KHOTKEYS_CONFIG_FILE, false ); +// CHECKME smazat stare sekce ? + TQStringList groups = cfg.groupList(); + for( TQStringList::ConstIterator it = groups.begin(); + it != groups.end(); + ++it ) + cfg.deleteGroup( *it ); + cfg.setGroup( "Main" ); // main group + cfg.writeEntry( "Version", 2 ); // now it's version 2 cfg. file + cfg.writeEntry( "AlreadyImported", already_imported ); + cfg.setGroup( "Data" ); + int cnt = write_actions_recursively_v2( cfg, actions, true ); + cfg.setGroup( "Main" ); + cfg.writeEntry( "Autostart", cnt != 0 && !daemon_disabled ); + cfg.writeEntry( "Disabled", daemon_disabled ); + cfg.setGroup( "Gestures" ); + cfg.writeEntry( "Disabled", gestures_disabled_globally ); + cfg.writeEntry( "MouseButton", gesture_mouse_button ); + cfg.writeEntry( "Timeout", gesture_timeout ); + if( gestures_exclude != NULL ) + { + cfg.setGroup( "GesturesExclude" ); + gestures_exclude->cfg_write( cfg ); + } + else + cfg.deleteGroup( "GesturesExclude" ); + cfg.setGroup( "Voice" ); + cfg.writeEntry("Shortcut" , voice_shortcut.toStringInternal() ); + + } + + +// return value means the number of enabled actions written in the cfg file +// i.e. 'Autostart' for value > 0 should be on +int Settings::write_actions_recursively_v2( TDEConfig& cfg_P, Action_data_group* parent_P, bool enabled_P ) + { + int enabled_cnt = 0; + TQString save_cfg_group = cfg_P.group(); + int cnt = 0; + for( Action_data_group::Iterator it = parent_P->first_child(); + it; + ++it ) + { + ++cnt; + if( enabled_P && (*it)->enabled( true )) + ++enabled_cnt; + cfg_P.setGroup( save_cfg_group + "_" + TQString::number( cnt )); + ( *it )->cfg_write( cfg_P ); + Action_data_group* grp = dynamic_cast< Action_data_group* >( *it ); + if( grp != NULL ) + enabled_cnt += write_actions_recursively_v2( cfg_P, grp, enabled_P && (*it)->enabled( true )); + } + cfg_P.setGroup( save_cfg_group ); + cfg_P.writeEntry( "DataCount", cnt ); + return enabled_cnt; + } + +void Settings::read_settings_v2( TDEConfig& cfg_P, bool include_disabled_P ) + { + cfg_P.setGroup( "Data" ); + read_actions_recursively_v2( cfg_P, actions, include_disabled_P ); + } + +void Settings::read_actions_recursively_v2( TDEConfig& cfg_P, Action_data_group* parent_P, + bool include_disabled_P ) + { + TQString save_cfg_group = cfg_P.group(); + int cnt = cfg_P.readNumEntry( "DataCount" ); + for( int i = 1; + i <= cnt; + ++i ) + { + cfg_P.setGroup( save_cfg_group + "_" + TQString::number( i )); + if( include_disabled_P || Action_data_base::cfg_is_enabled( cfg_P )) + { + Action_data_base* new_action = Action_data_base::create_cfg_read( cfg_P, parent_P ); + Action_data_group* grp = dynamic_cast< Action_data_group* >( new_action ); + if( grp != NULL ) + read_actions_recursively_v2( cfg_P, grp, include_disabled_P ); + } + } + cfg_P.setGroup( save_cfg_group ); + } + +// backward compatibility +void Settings::read_settings_v1( TDEConfig& cfg_P ) + { + int sections = cfg_P.readNumEntry( "Num_Sections", 0 ); + Action_data_group* menuentries = NULL; + for( Action_data_group::Iterator it( actions->first_child()); + *it; + ++it ) + { + Action_data_group* tmp = dynamic_cast< Action_data_group* >( *it ); + if( tmp == NULL ) + continue; + if( tmp->system_group() == Action_data_group::SYSTEM_MENUENTRIES ) + { + menuentries = tmp; + break; + } + } + for( int sect = 1; + sect <= sections; + ++sect ) + { + TQString group = TQString( "Section%1" ).arg( sect ); + if( !cfg_P.hasGroup( group )) + continue; + cfg_P.setGroup( group ); + TQString name = cfg_P.readEntry( "Name" ); + if( name.isNull() ) + continue; + TQString shortcut = cfg_P.readEntry( "Shortcut" ); + if( shortcut.isNull() ) + continue; + TQString run = cfg_P.readEntry( "Run" ); + if( run.isNull() ) + continue; + bool menuentry = cfg_P.readBoolEntry( "MenuEntry", false ); + // CHECKME tohle pridavani az pak je trosku HACK + if( menuentry ) + { + if( menuentries == NULL ) + { + menuentries = new Action_data_group( actions, + i18n( MENU_EDITOR_ENTRIES_GROUP_NAME ), + i18n( "These entries were created using Menu Editor." ), NULL, + Action_data_group::SYSTEM_MENUENTRIES, true ); + menuentries->set_conditions( new Condition_list( "", menuentries )); + } + ( void ) new Menuentry_shortcut_action_data( menuentries, name, "", + TDEShortcut( shortcut ), run ); + } + else + { + ( void ) new Command_url_shortcut_action_data( actions, name, "", + TDEShortcut( shortcut ), run ); + } + } + } + +} // namespace KHotKeys diff --git a/khotkeys/shared/settings.h b/khotkeys/shared/settings.h new file mode 100644 index 000000000..e28502cfe --- /dev/null +++ b/khotkeys/shared/settings.h @@ -0,0 +1,61 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _SETTINGS_H_ +#define _SETTINGS_H_ + +#include "actions.h" +#include <tdeshortcut.h> + +class TDEConfig; + +namespace KHotKeys +{ + +enum ImportType + { + ImportNone, // no import is done + ImportAsk, // if already imported before, ask (called from GUI) + ImportSilent // if already imported before, ignore (called from the update script) + }; + +class KDE_EXPORT Settings + { + public: + Settings(); + bool read_settings( bool include_disabled_P ); + void write_settings(); + bool import( TDEConfig& cfg_P, bool ask_P ); + Action_data_group* actions; + bool gestures_disabled_globally; + int gesture_mouse_button; + int gesture_timeout; + bool daemon_disabled; + Windowdef_list* gestures_exclude; + TDEShortcut voice_shortcut; + protected: + bool read_settings( TDEConfig& cfg_P, bool include_disabled_P, ImportType import_P ); + void read_settings_v1( TDEConfig& cfg_P ); + void read_settings_v2( TDEConfig& cfg_P, bool include_disabled_P ); + int write_actions_recursively_v2( TDEConfig& cfg_P, Action_data_group* parent_P, bool enabled_P ); + void read_actions_recursively_v2( TDEConfig& cfg_P, Action_data_group* parent_P, + bool include_disabled_P ); + private: + TQStringList already_imported; + KHOTKEYS_DISABLE_COPY( Settings ); + }; + +//*************************************************************************** +// Inline +//*************************************************************************** + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/sound.cpp b/khotkeys/shared/sound.cpp new file mode 100644 index 000000000..393b02f46 --- /dev/null +++ b/khotkeys/shared/sound.cpp @@ -0,0 +1,343 @@ +/*************************************************************************** + * Copyright (C) 2005 by Olivier Goffart * + * ogoffart@kde.org * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "sound.h" +#include <tqfile.h> +#include <tqdatastream.h> +#include <kdebug.h> + + + + +Sound::Sound() +{ +} + + +Sound::~Sound() +{ +} + + +#define READ_FROM_STREAM(FORMAT,NAME) FORMAT NAME; stream >> NAME; +#define MAGIC(CH) { \ + stream >> magic; \ + if( magic != ( (CH)[0] | (CH)[1]<<8 | (CH)[2]<< 16 | (CH)[3] << 24 ) ) \ + { \ + kdWarning() << k_funcinfo << "bad format " << magic << " != " << CH "\n";\ + return;\ + } } + +#define ABS(X) ( (X>0) ? X : -X ) + +void Sound::load(const TQString& filename) +{ + kdDebug() << k_funcinfo << filename << endl; + data=TQMemArray<TQ_INT32>(); + TQFile file(filename); + if(!file.open(IO_ReadOnly)) + { + kdWarning() << k_funcinfo <<"unable to open file" << endl; + return; + } + TQDataStream stream(&file); + stream.setByteOrder( TQDataStream::LittleEndian ); + TQ_INT32 magic; + + MAGIC("RIFF"); + READ_FROM_STREAM(TQ_UINT32,ChunkSize); + MAGIC("WAVE"); + MAGIC("fmt "); + READ_FROM_STREAM(TQ_UINT32,ChunkSize2); + READ_FROM_STREAM(TQ_INT16,AudioFormat); + READ_FROM_STREAM(TQ_UINT16,NumberOfChannels); + READ_FROM_STREAM(TQ_UINT32,SampleRate); + _fs=SampleRate; + READ_FROM_STREAM(TQ_UINT32,ByteRate); + READ_FROM_STREAM(TQ_UINT16,BlockAlign); + READ_FROM_STREAM(TQ_UINT16,BitsPerSample); + MAGIC("data"); + READ_FROM_STREAM(TQByteArray,SoundData); + NumberOfChannels=1; //Wav i play are broken + + file.close(); + + uint BytePS=BitsPerSample/8; + uint NumberOfSamples = (SoundData.size())/(NumberOfChannels*BytePS); + + + data.resize(NumberOfSamples); + +// kdDebug() << k_funcinfo << NumberOfSamples << " samples" << endl; + + max=0; + for(unsigned long int f=0;f<NumberOfSamples;f++) + { + TQ_INT32 nb=0; + for(uint k=0;k<BytePS;k++) + { + nb |= (SoundData.at(f*BytePS+k)&0x000000FF) << (k*8); + } + if(nb & (1 << (BytePS*8 -1)) ) + nb = nb-(1<<BytePS*8); + data[f]=nb; + if(ABS(nb)>max) + { + max=ABS(nb); + } + } + +/* static int q=0; + TQString name="test" + TQString::number(q++) + ".wav"; + save(name);*/ + +} + +#define SMAGIC(CH) { stream << ( TQ_INT32) ( (CH)[0] | (CH)[1]<<8 | (CH)[2]<< 16 | (CH)[3] << 24 ) ; } + +void Sound::save(const TQString& filename) const +{ + kdDebug( 1217 ) << k_funcinfo << filename << " - " << data.size() << endl; + TQFile file(filename); + if(!file.open(IO_WriteOnly)) + { + kdWarning() << k_funcinfo <<"unable to open file" << endl; + return; + } + TQDataStream stream(&file); + stream.setByteOrder( TQDataStream::LittleEndian ); + + + TQByteArray SoundData(data.size()*2); + + for(unsigned long int f=0;f<data.size();f++) + { + TQ_UINT16 val= (signed short int) ( (data.at(f) * ((double)(1<<13)/(signed)max) ) ); + SoundData.at( 2*f )= val & 0x00FF; + SoundData.at(2*f+1)= (val & 0xFF00) >> 8; + +// kdDebug( 1217 ) << k_funcinfo << data.at(f) << " / " << max << " = " << val << " | " << SoundData[ 2*f ] << " "<< SoundData[ 2*f+1 ] << endl; + } + + TQ_UINT16 NumberOfChannels=2; + TQ_UINT32 SampleRate=_fs; + + SMAGIC("RIFF"); + //READ_FROM_STREAM(TQ_UINT32,ChunkSize); + stream << (TQ_UINT32)(36+ SoundData.size()); + SMAGIC("WAVE"); + SMAGIC("fmt "); + //READ_FROM_STREAM(TQ_UINT32,ChunkSize2); + stream << (TQ_UINT32)(16); + //READ_FROM_STREAM(TQ_INT16,AudioFormat); + stream << (TQ_INT16)(1); + //READ_FROM_STREAM(TQ_UINT16,NumberOfChannels); + stream << (TQ_UINT16)(NumberOfChannels); + //READ_FROM_STREAM(TQ_UINT32,SampleRate); + stream << (TQ_UINT32)(SampleRate); + //READ_FROM_STREAM(TQ_UINT32,ByteRate); + stream << (TQ_UINT32)(NumberOfChannels*SampleRate*16/8); + //READ_FROM_STREAM(TQ_UINT16,BlockAlign); + stream << (TQ_UINT16)(16/8 *NumberOfChannels); + //READ_FROM_STREAM(TQ_UINT16,BitsPerSample); + stream << (TQ_UINT16)(16); + SMAGIC("data"); + //READ_FROM_STREAM(TQByteArray,SoundData); + stream << SoundData; + + file.close(); + +} + + + + +#if 0 +void Sound::load(const TQString& filename) +{ + cout << "saout \n"; + data=TQMemArray<long unsigned int>(); + static const int BUFFER_LEN = 4096; + + //code from libtunepimp + //(wav_trm.cpp) + + FILE *source; + unsigned char buffer[100], *copyBuffer; + unsigned int bytes; + unsigned long ulRIFF; + unsigned long ulLength; + unsigned long ulWAVE; + unsigned long ulType; + unsigned long ulCount; + unsigned long ulLimit; + bool haveWaveHeader = false; + unsigned long waveSize = 0; + WAVEFORMAT waveFormat; + int toRead; + mb_int64_t fileLen = 0; + + source = fopen(filename.ascii(), "rb"); + if (source == NULL) + { +// errorString = string("File not found"); +// fclose(source); + cout << "File not found \n"; + return; + } + + fseek(source, 0, SEEK_END); + fileLen = ftell(source); + fseek(source, 0, SEEK_SET); + + if (fread(buffer, 1, 12, source) != 12) + { +// errorString = string("File is too short"); + cout << "File is to short \n"; + fclose(source); + return ; + } + + ulRIFF = (unsigned long)(((unsigned long *)buffer)[0]); + ulLength = (unsigned long)(((unsigned long *)buffer)[1]); + ulWAVE = (unsigned long)(((unsigned long *)buffer)[2]); + + if(ulRIFF != MAKEFOURCC('R', 'I', 'F', 'F') || + ulWAVE != MAKEFOURCC('W', 'A', 'V', 'E')) + { +// errorString = strdup("File is not in WAVE format"); + cout << "File is not WAVE \n"; + fclose(source); + return ; + } + + // Run through the bytes looking for the tags + ulCount = 0; + ulLimit = ulLength - 4; + while (ulCount < ulLimit && waveSize == 0) + { + if (fread(buffer, 1, 8, source) != 8) + { +// errorString = strdup("File is too short"); + cout << "File is to short \n"; + fclose(source); + return; + } + + ulType = (unsigned long)(((unsigned long *)buffer)[0]); + ulLength = (unsigned long)(((unsigned long *)buffer)[1]); + switch (ulType) + { + // format + case MAKEFOURCC('f', 'm', 't', ' '): + if (ulLength < sizeof(WAVEFORMAT)) + { +// errorString = strdup("File is too short"); + cout << "File is to short \n"; + fclose(source); + return ; + } + + if (fread(&waveFormat, 1, ulLength, source) != ulLength) + { +// errorString = strdup("File is too short"); + cout << "File is to short \n"; + fclose(source); + return ; + } + + if (waveFormat.wFormatTag != WAVE_FORMAT_PCM) + { +// errorString = strdup("Unsupported WAV format"); + cout << "Unsupported WAVE \n"; + fclose(source); + return ; + } + haveWaveHeader = true; + + ulCount += ulLength; + break; + + // data + case MAKEFOURCC('d', 'a', 't', 'a'): + waveSize = ulLength; + break; + + default: + fseek(source, ulLength, SEEK_CUR); + break; + + } + } + + + if (!haveWaveHeader) + { +// errorString = strdup("Could not find WAV header"); + cout << "Header nbot found \n"; + fclose(source); + return ; + } + + fileLen -= (mb_int64_t)ftell(source); + fileLen /= waveFormat.nChannels; + fileLen /= (waveFormat.nBlockAlign / waveFormat.nChannels); + + fileLen /= waveFormat.nSamplesPerSec; + + //on ne lit qu'un channel + //waveSize=fileLen; + data.resize(waveSize); + unsigned long pos=0; + + cout << "Weeee "<< waveSize <<"\n"; + + copyBuffer = (unsigned char*)malloc(BUFFER_LEN); + if (copyBuffer == NULL) + { +// errorString = strdup("Cannot allocate buffer space."); + return ; + } + + for(;;) + { + toRead = min(waveSize, (unsigned long)BUFFER_LEN); + if (toRead <= 0) + break; + + bytes = fread(copyBuffer, 1, toRead, source); + if (bytes <= 0) + break; + + for(uint f=0;f<bytes;f+=4) + { + data[pos]=(((unsigned long*)copyBuffer)[f/4]); + pos++; + } + + waveSize -= toRead; + } + free(copyBuffer); + fclose(source); + + return ; +} + +#endif diff --git a/khotkeys/shared/sound.h b/khotkeys/shared/sound.h new file mode 100644 index 000000000..6453563de --- /dev/null +++ b/khotkeys/shared/sound.h @@ -0,0 +1,58 @@ +/*************************************************************************** + * Copyright (C) 2005 by Olivier Goffart * + * ogoffart@kde.org * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef SOUND_H +#define SOUND_H + +#include <tqmemarray.h> +#include <tqstring.h> +#include <kdemacros.h> + +/** +@author Olivier Goffart +*/ +class KDE_EXPORT Sound{ +public: + Sound(); + ~Sound(); + + void load(const TQString &filename); + void save(const TQString &filename) const; + + unsigned int size() const + { + return data.size(); + } + + inline float at(int pos) const + { + return (float)(data.at(pos))/max; + } + + inline uint fs() const + { + return _fs; + } + + TQMemArray<TQ_INT32> data; + TQ_UINT32 max; + uint _fs; +}; + +#endif diff --git a/khotkeys/shared/soundrecorder.cpp b/khotkeys/shared/soundrecorder.cpp new file mode 100644 index 000000000..b5759542a --- /dev/null +++ b/khotkeys/shared/soundrecorder.cpp @@ -0,0 +1,86 @@ +/*************************************************************************** + * Copyright (C) 2005 by Olivier Goffart * + * ogoffart@kde.org * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + + +#include "soundrecorder.h" + +#include <kdebug.h> +#include <tdelocale.h> +#include <tqtimer.h> +#include <klibloader.h> + +#include "khotkeysglobal.h" + +namespace KHotKeys +{ + +SoundRecorder::create_ptr SoundRecorder::create_fun = NULL; + +bool SoundRecorder::init( KLibrary* lib ) +{ +#ifdef HAVE_ARTS + if( create_fun == NULL && lib != NULL ) + create_fun = (create_ptr) lib->symbol( "khotkeys_soundrecorder_create" ); +#endif +// kdDebug( 1217 ) << "soundrecorder:" << create_fun << ":" << lib << endl; + return create_fun != NULL; +} + +SoundRecorder* SoundRecorder::create( TQObject* parent, const char* name ) +{ +#ifdef HAVE_ARTS + if( create_fun != NULL ) + return create_fun( parent, name ); +#endif + return new SoundRecorder( parent, name ); +} + +SoundRecorder::SoundRecorder(TQObject *parent, const char *name) : TQObject(parent, name) {} + +SoundRecorder::~SoundRecorder() +{ +} + +void SoundRecorder::start() +{ +} + +void SoundRecorder::stop() +{ +} + +void SoundRecorder::abort() +{ +} + + +Sound SoundRecorder::sound() +{ + Sound s; + return s; +} + +} + +#include "soundrecorder.moc" diff --git a/khotkeys/shared/soundrecorder.h b/khotkeys/shared/soundrecorder.h new file mode 100644 index 000000000..c7e55fc22 --- /dev/null +++ b/khotkeys/shared/soundrecorder.h @@ -0,0 +1,62 @@ +/*************************************************************************** + * Copyright (C) 2005 by Olivier Goffart * + * ogoffart@kde.org * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef RECORDER_H +#define RECORDER_H + +#include <tqobject.h> +#include "sound.h" +#include <kdemacros.h> + +class KLibrary; + +namespace KHotKeys +{ + + + +/** +@author Olivier Goffart +*/ +class KDE_EXPORT SoundRecorder : public TQObject +{ +Q_OBJECT +public: + static SoundRecorder* create( TQObject* parent = 0, const char* name = 0 ); + virtual ~SoundRecorder(); + + virtual void start(); + virtual void stop(); + virtual void abort(); + virtual Sound sound(); + + static bool init( KLibrary* ); +signals: + void recorded(const Sound&); + +protected: + SoundRecorder(TQObject *parent = 0, const char *name = 0); + typedef SoundRecorder* (*create_ptr)( TQObject*, const char* ); +private: + static create_ptr create_fun; +}; + +} + +#endif diff --git a/khotkeys/shared/triggers.cpp b/khotkeys/shared/triggers.cpp new file mode 100644 index 000000000..115bc6687 --- /dev/null +++ b/khotkeys/shared/triggers.cpp @@ -0,0 +1,435 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _TRIGGERS_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "triggers.h" + +#include <kglobalaccel.h> +#include <tdeconfig.h> +#include <kdebug.h> +#include <twinmodule.h> +#include <tdelocale.h> +#include <netwm_def.h> +#include <tdeaccel.h> + +#include <X11/Xlib.h> +#include <X11/keysym.h> + +#include "actions.h" +#include "action_data.h" +#include "input.h" +#include "gestures.h" +#include "windows.h" +#include "voices.h" + +namespace KHotKeys +{ + +// Trigger + +void Trigger::cfg_write( TDEConfig& cfg_P ) const + { + cfg_P.writeEntry( "Type", "ERROR" ); + } + +Trigger* Trigger::create_cfg_read( TDEConfig& cfg_P, Action_data* data_P ) + { + TQString type = cfg_P.readEntry( "Type" ); + if( type == "SHORTCUT" || type == "SINGLE_SHORTCUT" ) + return new Shortcut_trigger( cfg_P, data_P ); + if( type == "WINDOW" ) + return new Window_trigger( cfg_P, data_P ); + if( type == "GESTURE" ) + return new Gesture_trigger(cfg_P, data_P ); + if( type == "VOICE" ) + return new Voice_trigger (cfg_P, data_P ); + + kdWarning( 1217 ) << "Unknown Trigger type read from cfg file\n"; + return NULL; + } + +// Trigger_list + +Trigger_list::Trigger_list( TDEConfig& cfg_P, Action_data* data_P ) + : TQPtrList< Trigger >() + { + setAutoDelete( true ); + _comment = cfg_P.readEntry( "Comment" ); + TQString save_cfg_group = cfg_P.group(); + int cnt = cfg_P.readNumEntry( "TriggersCount", 0 ); + for( int i = 0; + i < cnt; + ++i ) + { + cfg_P.setGroup( save_cfg_group + TQString::number( i )); + Trigger* trigger = Trigger::create_cfg_read( cfg_P, data_P ); + if( trigger ) + append( trigger ); + } + cfg_P.setGroup( save_cfg_group ); + } + +void Trigger_list::cfg_write( TDEConfig& cfg_P ) const + { + cfg_P.writeEntry( "Comment", comment()); + TQString save_cfg_group = cfg_P.group(); + int i = 0; + for( Iterator it( *this ); + it; + ++it, ++i ) + { + cfg_P.setGroup( save_cfg_group + TQString::number( i )); + it.current()->cfg_write( cfg_P ); + } + cfg_P.setGroup( save_cfg_group ); + cfg_P.writeEntry( "TriggersCount", i ); + } + +Trigger_list* Trigger_list::copy( Action_data* data_P ) const + { + Trigger_list* ret = new Trigger_list( comment()); + for( Iterator it( *this ); + it; + ++it ) + ret->append( it.current()->copy( data_P )); + return ret; + } + +void Trigger_list::activate( bool activate_P ) + { + for( Iterator it( *this ); + it; + ++it ) + ( *it )->activate( activate_P ); + } + +// Shortcut_trigger + +Shortcut_trigger::Shortcut_trigger( Action_data* data_P, const TDEShortcut& shortcut_P ) + : Trigger( data_P ), _shortcut( shortcut_P ) + { + keyboard_handler->insert_item( shortcut(), this ); + } + +Shortcut_trigger::Shortcut_trigger( TDEConfig& cfg_P, Action_data* data_P ) + : Trigger( cfg_P, data_P ), _shortcut( cfg_P.readEntry( "Key", 0 )) + { + keyboard_handler->insert_item( shortcut(), this ); + } + +Shortcut_trigger::~Shortcut_trigger() + { + keyboard_handler->remove_item( shortcut(), this ); + } + +void Shortcut_trigger::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Key", _shortcut.toStringInternal()); + cfg_P.writeEntry( "Type", "SHORTCUT" ); // overwrites value set in base::cfg_write() + } + +Shortcut_trigger* Shortcut_trigger::copy( Action_data* data_P ) const + { + kdDebug( 1217 ) << "Shortcut_trigger::copy()" << endl; + return new Shortcut_trigger( data_P ? data_P : data, shortcut()); + } + +const TQString Shortcut_trigger::description() const + { + // CHECKME vice mods + return i18n( "Shortcut trigger: " ) + _shortcut.toString(); + // CHECKME i18n pro toString() ? + } + +bool Shortcut_trigger::handle_key( const TDEShortcut& shortcut_P ) + { + if( shortcut() == shortcut_P ) + { + windows_handler->set_action_window( 0 ); // use active window + data->execute(); + return true; + } + return false; + } + +void Shortcut_trigger::activate( bool activate_P ) + { + if( activate_P && khotkeys_active()) + keyboard_handler->activate_receiver( this ); + else + keyboard_handler->deactivate_receiver( this ); + } + +// Window_trigger + +Window_trigger::Window_trigger( TDEConfig& cfg_P, Action_data* data_P ) + : Trigger( cfg_P, data_P ), active( false ) + { +// kdDebug( 1217 ) << "Window_trigger" << endl; + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Windows" ); + _windows = new Windowdef_list( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + window_actions = cfg_P.readNumEntry( "WindowActions" ); + init(); + } + +Window_trigger::~Window_trigger() + { +// kdDebug( 1217 ) << "~Window_trigger :" << this << endl; + disconnect( windows_handler, NULL, this, NULL ); + delete _windows; + } + +void Window_trigger::init() + { + kdDebug( 1217 ) << "Window_trigger::init()" << endl; + connect( windows_handler, TQT_SIGNAL( window_added( WId )), this, TQT_SLOT( window_added( WId ))); + connect( windows_handler, TQT_SIGNAL( window_removed( WId )), this, TQT_SLOT( window_removed( WId ))); + if( window_actions & ( WINDOW_ACTIVATES | WINDOW_DEACTIVATES /*| WINDOW_DISAPPEARS*/ )) + connect( windows_handler, TQT_SIGNAL( active_window_changed( WId )), + this, TQT_SLOT( active_window_changed( WId ))); + connect( windows_handler, TQT_SIGNAL( window_changed( WId, unsigned int )), + this, TQT_SLOT( window_changed( WId, unsigned int ))); + } + +void Window_trigger::activate( bool activate_P ) + { + active = activate_P && khotkeys_active(); + } + +void Window_trigger::window_added( WId window_P ) + { + bool matches = windows()->match( Window_data( window_P )); + existing_windows[ window_P ] = matches; + kdDebug( 1217 ) << "Window_trigger::w_added() : " << matches << endl; + if( active && matches && ( window_actions & WINDOW_APPEARS )) + { + windows_handler->set_action_window( window_P ); + data->execute(); + } + } + +void Window_trigger::window_removed( WId window_P ) + { + if( existing_windows.contains( window_P )) + { + bool matches = existing_windows[ window_P ]; + kdDebug( 1217 ) << "Window_trigger::w_removed() : " << matches << endl; + if( active && matches && ( window_actions & WINDOW_DISAPPEARS )) + { + windows_handler->set_action_window( window_P ); + data->execute(); + } + existing_windows.remove( window_P ); + // CHECKME jenze co kdyz se window_removed zavola pred active_window_changed ? + } + else + kdDebug( 1217 ) << "Window_trigger::w_removed()" << endl; + } + +void Window_trigger::active_window_changed( WId window_P ) + { + bool was_match = false; + if( existing_windows.contains( last_active_window )) + was_match = existing_windows[ last_active_window ]; + if( active && was_match && ( window_actions & WINDOW_DEACTIVATES )) + { + windows_handler->set_action_window( window_P ); + data->execute(); + } +/* bool matches = windows()->match( Window_data( window_P )); + existing_windows[ window_P ] = matches;*/ + bool matches = existing_windows.contains( window_P ) + ? existing_windows[ window_P ] : false; + if( active && matches && ( window_actions & WINDOW_ACTIVATES )) + { + windows_handler->set_action_window( window_P ); + data->execute(); + } + kdDebug( 1217 ) << "Window_trigger::a_w_changed() : " << was_match << "|" << matches << endl; + last_active_window = window_P; + } + +void Window_trigger::window_changed( WId window_P, unsigned int dirty_P ) + { // CHECKME snad nebude mit vliv, kdyz budu kaslat na properties_P a zkratka + // kontrolovat kazdou zmenu + // CHECKME kdyz se zmeni okno z match na non-match, asi to nebrat jako DISAPPEAR + if( ! ( dirty_P & ( NET::WMName | NET::WMWindowType ))) + return; + kdDebug( 1217 ) << "Window_trigger::w_changed()" << endl; + bool was_match = false; + if( existing_windows.contains( window_P )) + was_match = existing_windows[ window_P ]; + bool matches = windows()->match( Window_data( window_P )); + existing_windows[ window_P ] = matches; + if( active && matches && !was_match ) + if( window_actions & WINDOW_APPEARS ) + { + windows_handler->set_action_window( window_P ); + data->execute(); + } + else if( window_actions & WINDOW_ACTIVATES && window_P == windows_handler->active_window()) + { + windows_handler->set_action_window( window_P ); + data->execute(); + } + kdDebug( 1217 ) << "Window_trigger::w_changed() : " << was_match << "|" << matches << endl; + } + +void Window_trigger::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + TQString save_cfg_group = cfg_P.group(); + cfg_P.setGroup( save_cfg_group + "Windows" ); + windows()->cfg_write( cfg_P ); + cfg_P.setGroup( save_cfg_group ); + cfg_P.writeEntry( "WindowActions", window_actions ); + cfg_P.writeEntry( "Type", "WINDOW" ); // overwrites value set in base::cfg_write() + } + +#ifndef COVARIANT_RETURN_BROKEN // stupid gcc, it doesn't even warn it can't do this +Window_trigger* Window_trigger::copy( Action_data* data_P ) const +#else +Trigger* Window_trigger::copy( Action_data* data_P ) const +#endif + { + Window_trigger* ret = new Window_trigger( data_P ? data_P : data, windows()->copy(), + window_actions ); + ret->existing_windows = existing_windows; // CHECKME je tohle vazne treba ? + return ret; + } + +const TQString Window_trigger::description() const + { + return i18n( "Window trigger: " ) + windows()->comment(); + } + +// Gesture_trigger + +Gesture_trigger::Gesture_trigger( Action_data* data_P, const TQString &gesturecode_P ) + : Trigger( data_P ), _gesturecode( gesturecode_P ) + { + } + +Gesture_trigger::Gesture_trigger( TDEConfig& cfg_P, Action_data* data_P ) + : Trigger( cfg_P, data_P ) + { + _gesturecode = cfg_P.readEntry( "Gesture" ); + } + +Gesture_trigger::~Gesture_trigger() + { + gesture_handler->unregister_handler( this, TQT_SLOT( handle_gesture( const TQString&, WId ))); + } + +void Gesture_trigger::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Gesture", gesturecode()); + cfg_P.writeEntry( "Type", "GESTURE" ); // overwrites value set in base::cfg_write() + } + +Trigger* Gesture_trigger::copy( Action_data* data_P ) const + { + kdDebug( 1217 ) << "Gesture_trigger::copy()" << endl; + return new Gesture_trigger( data_P ? data_P : data, gesturecode()); + } + +const TQString Gesture_trigger::description() const + { + return i18n( "Gesture trigger: " ) + gesturecode(); + } + +void Gesture_trigger::handle_gesture( const TQString &gesture_P, WId window_P ) + { + if( gesturecode() == gesture_P ) + { + windows_handler->set_action_window( window_P ); + data->execute(); + } + } + +void Gesture_trigger::activate( bool activate_P ) + { + if( activate_P ) + gesture_handler->register_handler( this, TQT_SLOT( handle_gesture( const TQString&, WId ))); + else + gesture_handler->unregister_handler( this, TQT_SLOT( handle_gesture( const TQString&, WId ))); + } + + +// Voice_trigger + + Voice_trigger::Voice_trigger( Action_data* data_P, const TQString &Voicecode_P, const VoiceSignature& signature1_P, const VoiceSignature& signature2_P ) + : Trigger( data_P ), _voicecode( Voicecode_P ) + { + _voicesignature[0]=signature1_P; + _voicesignature[1]=signature2_P; + } + +Voice_trigger::Voice_trigger( TDEConfig& cfg_P, Action_data* data_P ) + : Trigger( cfg_P, data_P ) + { + _voicecode = cfg_P.readEntry( "Name" ); + _voicesignature[0].read( &cfg_P , "Signature1" ); + _voicesignature[1].read( &cfg_P , "Signature2" ); + } + +Voice_trigger::~Voice_trigger() + { + voice_handler->unregister_handler( this ); + } + +void Voice_trigger::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Name", voicecode()); + cfg_P.writeEntry( "Type", "VOICE" ); // overwrites value set in base::cfg_write() + _voicesignature[0].write( &cfg_P , "Signature1" ); + _voicesignature[1].write( &cfg_P , "Signature2" ); + } + +Trigger* Voice_trigger::copy( Action_data* data_P ) const + { + kdDebug( 1217 ) << "Voice_trigger::copy()" << endl; + return new Voice_trigger( data_P ? data_P : data, voicecode(), voicesignature(1) , voicesignature(2) ); + } + +const TQString Voice_trigger::description() const + { + return i18n( "Voice trigger: " ) + voicecode(); + } + +void Voice_trigger::handle_Voice( ) + { + windows_handler->set_action_window( 0 ); // use active window + data->execute(); + + } + +void Voice_trigger::activate( bool activate_P ) + { + if( activate_P && khotkeys_active()) + voice_handler->register_handler( this ); + else + voice_handler->unregister_handler( this ); + } + + +} // namespace KHotKeys + +#include "triggers.moc" diff --git a/khotkeys/shared/triggers.h b/khotkeys/shared/triggers.h new file mode 100644 index 000000000..c74a3a54a --- /dev/null +++ b/khotkeys/shared/triggers.h @@ -0,0 +1,260 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _TRIGGERS_H_ +#define _TRIGGERS_H_ + +#include <tqptrlist.h> +#include <tqtimer.h> +#include <tqmap.h> +#include <kdemacros.h> + +#include "khotkeysglobal.h" +#include "voicesignature.h" + +#include "input.h" + +class TDEConfig; + +namespace KHotKeys +{ + +class Windowdef_list; +class Action_data; + +class KDE_EXPORT Trigger + { + public: + Trigger( Action_data* data_P ); + Trigger( TDEConfig& cfg_P, Action_data* data_P ); + virtual ~Trigger(); + virtual void cfg_write( TDEConfig& cfg_P ) const = 0; + virtual Trigger* copy( Action_data* data_P ) const = 0; + virtual const TQString description() const = 0; + static Trigger* create_cfg_read( TDEConfig& cfg_P, Action_data* data_P ); + virtual void activate( bool activate_P ) = 0; + protected: + Action_data* const data; + KHOTKEYS_DISABLE_COPY( Trigger ); + }; + +class KDE_EXPORT Trigger_list + : public TQPtrList< Trigger > + { + public: + Trigger_list( const TQString& comment_P ); // CHECKME nebo i data ? + Trigger_list( TDEConfig& cfg_P, Action_data* data_P ); + void activate( bool activate_P ); + void cfg_write( TDEConfig& cfg_P ) const; + typedef TQPtrListIterator< Trigger > Iterator; + const TQString& comment() const; + Trigger_list* copy( Action_data* data_P ) const; + private: + TQString _comment; + KHOTKEYS_DISABLE_COPY( Trigger_list ); + }; + +class KDE_EXPORT Shortcut_trigger + : public Trigger, public Kbd_receiver + { + typedef Trigger base; + public: + Shortcut_trigger( Action_data* data_P, const TDEShortcut& shortcut_P ); + Shortcut_trigger( TDEConfig& cfg_P, Action_data* data_P ); + virtual ~Shortcut_trigger(); + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual Shortcut_trigger* copy( Action_data* data_P ) const; + virtual const TQString description() const; + const TDEShortcut& shortcut() const; + virtual bool handle_key( const TDEShortcut& shortcut_P ); + virtual void activate( bool activate_P ); + private: + TDEShortcut _shortcut; + }; + +class KDE_EXPORT Window_trigger + : public TQObject, public Trigger + { + Q_OBJECT + typedef Trigger base; + public: + enum window_action_t + { + WINDOW_APPEARS = ( 1 << 0 ), + WINDOW_DISAPPEARS = ( 1 << 1 ), + WINDOW_ACTIVATES = ( 1 << 2 ), + WINDOW_DEACTIVATES = ( 1 << 3 ) + }; + Window_trigger( Action_data* data_P, Windowdef_list* windows_P, int window_actions_P ); + Window_trigger( TDEConfig& cfg_P, Action_data* data_P ); + virtual ~Window_trigger(); + virtual void cfg_write( TDEConfig& cfg_P ) const; +#ifndef COVARIANT_RETURN_BROKEN // stupid gcc, it doesn't even warn it can't do this + virtual Window_trigger* copy( Action_data* data_P ) const; +#else + virtual Trigger* copy( Action_data* data_P ) const; +#endif + virtual const TQString description() const; + const Windowdef_list* windows() const; + bool triggers_on( window_action_t w_action_P ) const; + virtual void activate( bool activate_P ); + protected: // CHECKME neco private ? + Windowdef_list* _windows; + int window_actions; + void init(); + typedef TQMap< WId, bool > Windows_map; + Windows_map existing_windows; + WId last_active_window; + protected slots: + void window_added( WId window_P ); + void window_removed( WId window_P ); + void active_window_changed( WId window_P ); + void window_changed( WId window_P, unsigned int dirty_P ); + protected: + bool active; + }; + +class KDE_EXPORT Gesture_trigger + : public TQObject, public Trigger + { + Q_OBJECT + typedef Trigger base; + public: + Gesture_trigger( Action_data* data_P, const TQString& gesture_P ); + Gesture_trigger( TDEConfig& cfg_P, Action_data* data_P ); + virtual ~Gesture_trigger(); + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual Trigger* copy( Action_data* data_P ) const; + virtual const TQString description() const; + const TQString& gesturecode() const; + virtual void activate( bool activate_P ); + protected slots: + void handle_gesture( const TQString& gesture_P, WId window_P ); + private: + TQString _gesturecode; + }; + + +class KDE_EXPORT Voice_trigger + : public TQObject, public Trigger + { + Q_OBJECT + typedef Trigger base; + public: + Voice_trigger( Action_data* data_P, const TQString& Voice_P, const VoiceSignature & signature1_P, const VoiceSignature & signature2_P ); + Voice_trigger( TDEConfig& cfg_P, Action_data* data_P ); + virtual ~Voice_trigger(); + virtual void cfg_write( TDEConfig& cfg_P ) const; + virtual Trigger* copy( Action_data* data_P ) const; + virtual const TQString description() const; + const TQString& voicecode() const; + virtual void activate( bool activate_P ); + VoiceSignature voicesignature( int ech ) const; + public slots: + void handle_Voice( ); + private: + TQString _voicecode; + VoiceSignature _voicesignature[2]; + }; + + +//*************************************************************************** +// Inline +//*************************************************************************** + +// Trigger + +inline +Trigger::Trigger( Action_data* data_P ) + : data( data_P ) + { + } + +inline +Trigger::Trigger( TDEConfig&, Action_data* data_P ) + : data( data_P ) + { + } + +inline +Trigger::~Trigger() + { + } + +// Trigger_list + +inline +Trigger_list::Trigger_list( const TQString& comment_P ) + : TQPtrList< Trigger >(), _comment( comment_P ) + { + setAutoDelete( true ); + } + +inline +const TQString& Trigger_list::comment() const + { + return _comment; + } + +// Shortcut_trigger + +inline +const TDEShortcut& Shortcut_trigger::shortcut() const + { + return _shortcut; + } + +// Window_trigger + +inline +Window_trigger::Window_trigger( Action_data* data_P, Windowdef_list* windows_P, + int window_actions_P ) + : Trigger( data_P ), _windows( windows_P ), window_actions( window_actions_P ), + last_active_window( None ), active( false ) + { + init(); + } + +inline +const Windowdef_list* Window_trigger::windows() const + { + return _windows; + } + +inline +bool Window_trigger::triggers_on( window_action_t w_action_P ) const + { + return window_actions & w_action_P; + } + +// Gesture_trigger + +inline +const TQString& Gesture_trigger::gesturecode() const + { + return _gesturecode; + } + +// Voice_trigger +inline +const TQString& Voice_trigger::voicecode() const + { + return _voicecode; + } + +inline +VoiceSignature Voice_trigger::voicesignature(int ech) const + { + return _voicesignature[ech-1]; + } + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/voices.cpp b/khotkeys/shared/voices.cpp new file mode 100644 index 000000000..a31ac8237 --- /dev/null +++ b/khotkeys/shared/voices.cpp @@ -0,0 +1,303 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 2005 Olivier Goffart <ogoffart @ kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "voices.h" +#include "voicesignature.h" +#include "triggers.h" +#include "soundrecorder.h" + +#include <stdlib.h> +#include <math.h> +#include <assert.h> + + +#include <tdeapplication.h> +#include <kdebug.h> +#include <kxerrorhandler.h> +#include <kkeynative.h> +#include <tqtimer.h> +#include <kglobalaccel.h> + + +#include <X11/Xlib.h> +#include <fixx11h.h> + +namespace KHotKeys +{ + +Voice* voice_handler; + +Voice::Voice( bool enabled_P, TQObject* parent_P ) + : TQObject( parent_P) ,_enabled( enabled_P ), _recording( false ), _recorder(0) + { + assert( voice_handler == NULL ); + voice_handler = this; + + _kga=0L; + _timer=0L; + + kdDebug(1217) << k_funcinfo << endl; + + } + +Voice::~Voice() + { + kdDebug(1217) << k_funcinfo << endl; + enable( false ); + voice_handler = NULL; + } + + +void Voice::enable( bool enabled_P ) + { +#ifndef HAVE_ARTS + enabled_P = false; // never enabled when there's no support +#endif + if( _enabled == enabled_P ) + return; + _enabled = enabled_P; + if( _enabled ) // activate the shortcut + set_shortcut( _shortcut ); + else + { + delete _kga; + _kga = NULL; + } + } + +void Voice::register_handler( Voice_trigger *trigger_P ) + { + if( !_references.contains( trigger_P )) + _references.append(trigger_P); + } + +void Voice::unregister_handler( Voice_trigger *trigger_P ) + { + _references.remove(trigger_P); + } + + +void Voice::record_start() +{ + kdDebug(1217) << k_funcinfo << endl; + if(!_recorder) + { + _recorder= SoundRecorder::create(this); + connect(_recorder, TQT_SIGNAL(recorded(const Sound& )), this, TQT_SLOT(slot_sound_recorded(const Sound& ))); + } + + _recorder->start(); + _recording=true; +} + +void Voice::record_stop() +{ + if(!_recording) + return; + + kdDebug(1217) << k_funcinfo << endl; + delete _timer; + _timer=0L; + _recording=false; + if(_recorder) + _recorder->stop(); +} + + +void Voice::slot_sound_recorded(const Sound &sound_P) +{ + VoiceSignature signature(sound_P); + + Voice_trigger *trig=0L; + Voice_trigger *sec_trig=0L; + double minimum=800000; + double second_minimum=80000; + int got_count=0; + TQValueList<Voice_trigger*>::Iterator it; + for ( it = _references.begin(); it != _references.end(); ++it ) + { + for(int ech=1; ech<=2 ; ech++) + { + Voice_trigger *t=*it; + + double diff=VoiceSignature::diff(signature, t->voicesignature(ech)); + if(minimum>=diff) + { + second_minimum=minimum; + minimum=diff; + sec_trig=trig; + trig=t; + } + else if(second_minimum>=diff) + { + second_minimum=diff; + sec_trig=t; + } + if( diff < REJECT_FACTOR_DIFF ) + got_count++; + kdDebug(1217) << k_funcinfo << ( (diff < REJECT_FACTOR_DIFF) ? "+++" : "---" ) <<t->voicecode() << ech << " : " << diff << endl; + } + } +// double ecart_relatif=(second_minimum-minimum)/minimum; + +// kdDebug(1217) << k_funcinfo << ecart_relatif << endl; + + if(trig) + kdDebug(1217) << k_funcinfo << "**** " << trig->voicecode() << " : " << minimum << endl; + + +// if(trig && ecart_relatif > REJECT_FACTOR_ECART_REL) +// if(trig && got_count==1) + bool selected=trig && (got_count==1 || ( minimum < 1.5*REJECT_FACTOR_DIFF && trig==sec_trig ) ); + + if(selected) + { + trig->handle_Voice(); + } + +} + + +/*bool Voice::x11Event( XEvent* pEvent ) +{ + if( pEvent->type != XKeyPress && pEvent->type != XKeyRelease ) + return false; + + KKeyNative keyNative( pEvent ); + + //kdDebug(1217) << k_funcinfo << keyNative.key().toString() << endl; + + if(_shortcut.contains(keyNative)) + { + if(pEvent->type == XKeyPress && !_recording ) + { + record_start(); + return true; + } + if(pEvent->type == XKeyRelease && _recording ) + { + record_stop(); + return true; + } + } + return false; +} + + +*/ + + +void Voice::set_shortcut( const TDEShortcut &shortcut) +{ + _shortcut = shortcut; + if( !_enabled ) + return; + if(!_kga) + _kga = new TDEGlobalAccel( this ); + _kga->remove("voice"); + + _kga->insert( "voice", i18n("Voice"), TQString::null, shortcut, 0, this, TQT_SLOT(slot_key_pressed())) ; + _kga->updateConnections(); +} + +void Voice::slot_key_pressed() +{ + if( !haveArts()) + return; + if( _recording ) + record_stop(); + else + { + record_start(); + if(!_timer) + { + _timer=new TQTimer(this); + connect(_timer, TQT_SIGNAL(timeout()) , this, TQT_SLOT(slot_timeout())); + } + + _timer->start(1000*20,true); + } +} + + +void Voice::slot_timeout() +{ + if(_recording && _recorder) + { + _recorder->abort(); + _recording=false; + } + _timer->deleteLater(); + _timer=0L; +} + + +TQString Voice::isNewSoundFarEnough(const VoiceSignature& signature, const TQString ¤tTrigger) +{ + Voice_trigger *trig=0L; + Voice_trigger *sec_trig=0L; + double minimum=800000; + double second_minimum=80000; + int got_count=0; + TQValueList<Voice_trigger*>::Iterator it; + for ( it = _references.begin(); it != _references.end(); ++it ) + { + Voice_trigger *t=*it; + if(t->voicecode()==currentTrigger) + continue; + + for(int ech=1; ech<=2 ; ech++) + { + double diff=VoiceSignature::diff(signature, t->voicesignature(ech)); + if(minimum>=diff) + { + second_minimum=minimum; + minimum=diff; + sec_trig=trig; + trig=t; + } + else if(second_minimum>=diff) + { + second_minimum=diff; + sec_trig=t; + } + if( diff < REJECT_FACTOR_DIFF ) + got_count++; + kdDebug(1217) << k_funcinfo << ( (diff < REJECT_FACTOR_DIFF) ? "+++" : "---" ) <<t->voicecode() << ech << " : " << diff << endl; + } + } + + if(trig) + kdDebug(1217) << k_funcinfo << "**** " << trig->voicecode() << " : " << minimum << endl; + + bool selected=trig && ((got_count==1 && minimum < REJECT_FACTOR_DIFF*0.7 ) || ( minimum < REJECT_FACTOR_DIFF && trig==sec_trig ) ); + return selected ? trig->voicecode() : TQString::null; +} + +bool Voice::doesVoiceCodeExists(const TQString &vc) +{ + TQValueList<Voice_trigger*>::Iterator it; + for ( it = _references.begin(); it != _references.end(); ++it ) + { + Voice_trigger *t=*it; + if(t->voicecode()==vc) + return true; + } + return false; +} + +} // namespace KHotKeys + +#include "voices.moc" diff --git a/khotkeys/shared/voices.h b/khotkeys/shared/voices.h new file mode 100644 index 000000000..ccd07563f --- /dev/null +++ b/khotkeys/shared/voices.h @@ -0,0 +1,83 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 2005 Olivier Goffart <ogoffart @ kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef VOICES_H_ +#define VOICES_H_ + +#include <tqwidget.h> +#include <tdeshortcut.h> + +class Sound; +class TQTimer; +class TDEGlobalAccel; + +namespace KHotKeys +{ + +class Voice; +class SoundRecorder; + +class Voice_trigger; +class VoiceSignature; + + +class KDE_EXPORT Voice : public TQObject + { + Q_OBJECT + public: + Voice( bool enabled_P, TQObject* parent_P ); + virtual ~Voice(); + void enable( bool enable_P ); + + void register_handler( Voice_trigger* ); + void unregister_handler( Voice_trigger* ); +// bool x11Event( XEvent* e ); + + void set_shortcut( const TDEShortcut &k); + + /** + * return TQString::null is a new signature is far enough from others signature + * otherwise, return the stringn which match. + */ + TQString isNewSoundFarEnough(const VoiceSignature& s, const TQString& currentTrigger); + + bool doesVoiceCodeExists(const TQString &s); + + public slots: + void record_start(); + void record_stop(); + + private slots: + void slot_sound_recorded( const Sound & ); + void slot_key_pressed(); + void slot_timeout(); + + signals: + void handle_voice( const TQString &voice ); + private: + + bool _enabled; + bool _recording; + + TQValueList<Voice_trigger *> _references; + SoundRecorder *_recorder; + + TDEShortcut _shortcut; + TDEGlobalAccel *_kga; + + TQTimer *_timer; + }; + + +KDE_EXPORT extern Voice* voice_handler; + +} // namespace KHotKeys + +#endif diff --git a/khotkeys/shared/voicesignature.cpp b/khotkeys/shared/voicesignature.cpp new file mode 100644 index 000000000..4a1e12880 --- /dev/null +++ b/khotkeys/shared/voicesignature.cpp @@ -0,0 +1,422 @@ +/*************************************************************************** + * Copyright (C) 2005 by Olivier Goffart * + * ogoffart@kde.org * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include "voicesignature.h" +#include "sound.h" +#include <tdeconfig.h> + +#include <math.h> +#ifdef PI +#undef PI +#endif +#define PI (2.0 * asin(1.0)) + + +#include <kdebug.h> +#include <tqdatetime.h> + +#undef Complex + +namespace KHotKeys +{ + + +inline static float ABS(float X) +{ + return (X>0) ? X : -X ; +} +inline static int MAX(int X , int Y) +{ + return (X>Y) ? X : Y ; +} +inline static int MIN(int X , int Y) +{ + return (X<Y) ? X : Y ; +} + + + + + + +class Complex +{ + public: + Complex () {} + Complex (double re): _re(re), _im(0.0) {} + Complex (double re, double im): _re(re), _im(im) {} + double Re () const { return _re; } + double Im () const { return _im; } + void operator += (const Complex& c) + { + _re += c._re; + _im += c._im; + } + void operator -= (const Complex& c) + { + _re -= c._re; + _im -= c._im; + } + void operator *= (const Complex& c) + { + double reT = c._re * _re - c._im * _im; + _im = c._re * _im + c._im * _re; + _re = reT; + } + Complex operator- () + { + return Complex (-_re, -_im); + } + Complex operator- (const Complex& c) const + { + return Complex (_re - c._re, _im - c._im); + } + Complex operator+ (const Complex& c) const + { + return Complex (_re + c._re, _im + c._im); + } + Complex operator* (const Complex& c) const + { + return Complex (_re * c._re - _im * c._im , _im * c._re + _re * c._im); + } + double Mod () const { return sqrt (_re * _re + _im * _im); } + + static Complex fromExp(double mod, double arg) { return Complex(mod*cos(arg) , mod*sin(arg)); } + private: + double _re; + double _im; +}; + +static inline double hamming(uint n, uint size) +{ + return HAMMING ? 0.54-0.46*cos( 2*PI*n /(size-1) ) : 1; +} + + +static TQMemArray<double> fft(const Sound& sound, unsigned int start, unsigned int stop) +{ + if(start>=stop || sound.size() == 0) + return TQMemArray<double>(); + + //We need a sample with a size of a power of two + uint size=stop-start; + unsigned short log2size=0; + while( (1<<log2size) < size ) + log2size++; + + int diff=(1<<log2size) - size; + if(diff > size/4 || 1<<log2size > sound.size() ) + { + log2size--; + diff=(1<<log2size) - size; + } + size=1<<log2size; + int start2=start-diff/2; + int stop2=start2+ size; + if(start2<0) + { + stop2-=start2; + start2=0; + } + if(stop2>sound.size()) + { + start2-= stop2 - sound.size(); + stop2=sound.size(); + if(start2<0) + { + stop2-=start2; + start2=0; + } + } + + //Generate an array to work in + TQMemArray<Complex> samples(size); + + //Fill it with samples in the "reversed carry" order + int rev_carry = 0; + for (uint f = 0; f < size - 1; f++) + { + samples[f]=sound.at(start2+rev_carry)* hamming(rev_carry, size); +// KDEBUG(rev_carry); + int mask = size>>1; // N / 2 + // add 1 backwards + while (rev_carry >= mask) + { + rev_carry -= mask; // turn off this bit + mask >>= 1; + } + rev_carry += mask; + } + samples[size-1]=sound.at(start2+size-1)*hamming(size-1, size); + + //FFT + for(uint level=0; level < log2size; level++) + { + for( int k=0; k< (size>>1) ; k++) + { + uint indice1 = (k << (level+1) ) % (size-1); // (k*2*2^l)%(N-1) + uint indice2 = indice1 + (1<<level); // (k*2*2^l)%(N-1) + 2^l + + uint coefW = ( k << (level+1) ) / (size-1); // (k*2*2^l) div (N-1) + double Wexpn=-2 * PI * coefW / (2 << level); // -2 pi n / 2^(l+1) + Complex W=Complex::fromExp(1, Wexpn) ; + + + //OPERATION BUTTERFLY + Complex a=samples[indice1]; + Complex b=samples[indice2]; + samples[indice1]=a+W*b; + samples[indice2]=a-W*b; + +// kdDebug() << k_funcinfo << "PAPILLON s_" << indice1 << " s_" << indice2 << " W_" << (2<<level) << "^" << coefW << endl; + } + } + + TQMemArray<double> result(size); + for(uint f=0;f<size;f++) + { + result[f]=samples[f].Mod() / size; + + } + return result; +} + + + + +TQMemArray<double> VoiceSignature::fft(const Sound& sound, unsigned int start, unsigned int stop) +{ + return KHotKeys::fft(sound, start, stop); + /*TQMemArray<double> result(8000); + for(int f=0; f<8000;f++) + { + Complex c(0); + + for(uint x=start; x<stop; x++) + { + Complex s(sound.at(x)); + double angle=-2*PI*f*x/8000; + s*= Complex( cos(angle) , sin(angle) ); + c+=s; + } + result[f]= c.Mod()/(stop-start) ; + } + return result;*/ +} + +bool VoiceSignature::window(const Sound& sound, unsigned int *_start, unsigned int *_stop) +{ + bool isNoise=false; + unsigned int length=sound.size(); + uint unit=WINDOW_UNIT; + if(length < unit ) + return false; + + //Fen�trage + unsigned int start=0 , stop=0; + double moy=0; + for(uint x=0;x<unit;x++) + { + moy+=ABS(sound.at(x)); + } + + if(moy>WINDOW_MINIMUM*unit) + isNoise=true; + + for(uint x=unit; x<length; x++) + { + if(moy<WINDOW_MINIMUM*unit) + { + if(stop==0) + start=x-unit/2; + } + else + stop=x-unit/2; + moy+=ABS(sound.at(x)); + moy-=ABS(sound.at(x-unit)); + + } + + if(moy>WINDOW_MINIMUM*unit && isNoise) + return false; + + stop=MIN(length,stop+WINDOW_MINIMUM_ECART); + start=MAX(0 ,start-WINDOW_MINIMUM_ECART); + + if(_start) + *_start=start; + if(_stop) + *_stop=stop; + return start<stop; +} + +//finally doesn't give better results +/*#define HZ_TO_MEL(F) (1127*log(1+(F)/700.0)) +#define MEL_TO_HZ(M) ( ( exp((M)/1127.0) -1) *700 )*/ +#define HZ_TO_MEL(F) (F) +#define MEL_TO_HZ(F) (F) + + +VoiceSignature::VoiceSignature(const Sound& sound) +{ + static uint temp_wind=0, temp_fft=0, temp_moy=0; + TQTime t; + t.start(); + + unsigned int start , stop; + if(!window(sound,&start,&stop)) + { + kdWarning( 1217 ) << k_funcinfo << "No voice found in the sound" << endl ; + return; + } + + temp_wind+=t.restart(); + + uint length=stop-start; + + for(int wind=0; wind<WINDOW_NUMBER; wind++) + { + unsigned int w_start=MAX(start, start+ (int)((wind - WINDOW_SUPER)*length/WINDOW_NUMBER)); + unsigned int w_stop =MIN(stop , start+ (int)((wind+1.0+WINDOW_SUPER)*length/WINDOW_NUMBER)); + + + TQMemArray<double> fourrier=fft(sound, w_start,w_stop); + + temp_fft+=t.restart(); + + //MEL conversion + double mel_start=HZ_TO_MEL(FFT_RANGE_INF); + uint mel_stop=HZ_TO_MEL(FFT_RANGE_SUP); + + for(int four=0; four<FOUR_NUMBER; four++) + { + unsigned int wf_start=mel_start + four*(mel_stop-mel_start)/FOUR_NUMBER; + unsigned int wf_stop=mel_start + (four+1)*(mel_stop-mel_start)/FOUR_NUMBER; + + unsigned int f_start=MEL_TO_HZ( wf_start )*fourrier.size()/sound.fs(); + unsigned int f_stop=MEL_TO_HZ( wf_stop )*fourrier.size()/sound.fs(); + unsigned int f_size=f_stop-f_start; + + double nb=0; + for(uint f=f_start; f<f_stop; f++) + { + int freq=f*fourrier.size()/sound.fs(); + nb+=fourrier[f]*FFT_PONDERATION(freq); + } + nb/=(f_size); + data[wind][four]=nb; + } + + temp_moy+=t.restart(); + + } + +// kdDebug( 1217 ) << k_funcinfo << "wind: "<< temp_wind << " - fft: " << temp_fft << " - moy: " << temp_moy << endl; +} + + + +VoiceSignature::~VoiceSignature() +{ +} + + + +float VoiceSignature::diff(const VoiceSignature &s1, const VoiceSignature &s2) +{ + if(s1.isNull() || s2.isNull()) + return 1000000; +#if 0 + double result=0; + for(int x=0;x<WINDOW_NUMBER;x++) + for(int y=0;y<FOUR_NUMBER;y++) + { + double d1=s1.data[x][y]-s2.data[x][y]; + result+= d1*d1;//*pond[x][y]; + } + return result; +#endif + + //DTW + // http://tcts.fpms.ac.be/cours/1005-08/speech/projects/2001/delfabro_henry_poitoux/ + + const int I=WINDOW_NUMBER; + const int J=WINDOW_NUMBER; + double g[I+1][J+1]; + for(int f=1;f<=J;f++) + g[0][f]=10000000; + for(int f=1;f<=I;f++) + g[f][0]=10000000; + g[0][0]=0; + for(int i=1;i<=I;i++) + for(int j=1;j<=J;j++) + { + double d=0; + for(int f=0;f<FOUR_NUMBER;f++) + { + double d1=s1.data[i-1][f]-s2.data[j-1][f]; + d+= d1*d1;//*pond[x][y]; + } + d=sqrt(d); + g[i][j]=TQMIN(TQMIN( g[i-1][j]+d, g[i][j-1]+d ) , g[i-1][j-1]+d+d ); + } + + return g[I][J]/(I+J); +} + + + + + +int VoiceSignature::size1() +{ + return WINDOW_NUMBER; +} + +int VoiceSignature::size2() +{ + return FOUR_NUMBER; +} + +TQMap<int, TQMap<int, double> > VoiceSignature::pond; + + + +void VoiceSignature::write(TDEConfigBase *cfg, const TQString &key) const +{ + TQStringList sl; + for(int x=0;x<WINDOW_NUMBER;x++) + for(int y=0;y<FOUR_NUMBER;y++) + { + sl.append( TQString::number(data[x][y]) ); + } + cfg->writeEntry(key,sl); +} + +void VoiceSignature::read(TDEConfigBase *cfg, const TQString &key) +{ + TQStringList sl=cfg->readListEntry(key); + for(int x=0;x<WINDOW_NUMBER;x++) + for(int y=0;y<FOUR_NUMBER;y++) + { + data[x][y]= sl[x*FOUR_NUMBER+y].toDouble(); + } +} + +} diff --git a/khotkeys/shared/voicesignature.h b/khotkeys/shared/voicesignature.h new file mode 100644 index 000000000..af987cce1 --- /dev/null +++ b/khotkeys/shared/voicesignature.h @@ -0,0 +1,101 @@ +/*************************************************************************** + * Copyright (C) 2005 by Olivier Goffart * + * ogoffart@kde.org * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef SIGNATURE_H +#define SIGNATURE_H + + +#include <tqmemarray.h> +#include <tqmap.h> +#include <kdemacros.h> + +class Sound; +class TDEConfigBase; + + +#define WINDOW_MINIMUM 0.10 +#define WINDOW_MINIMUM_ECART 200 +#define WINDOW_NUMBER 7 +#define WINDOW_SUPER 0.43 +#define WINDOW_UNIT sound.fs()/4 +#define FOUR_NUMBER 7 +#define FOUR_SUPER 0 + + +#define FFT_RANGE_INF 370 +#define FFT_RANGE_SUP 2000 +// #define FFT_PONDERATION(f) ((double)log(1+(f))/log(10)) +#define FFT_PONDERATION(f) 1 + + +// theses settings are better in a 8000Hz fs +/*#define FFT_RANGE_INF 300 +#define FFT_RANGE_SUP 1500*/ + + +//#define REJECT_FACTOR_ECART_REL 0.5 +#define REJECT_FACTOR_DIFF 0.0018 + + + +#define HAMMING false + + + + +namespace KHotKeys +{ + + +/** +@author Olivier Goffart +*/ +class KDE_EXPORT VoiceSignature{ +public: + explicit VoiceSignature(const Sound& s); + + VoiceSignature(){} + ~VoiceSignature(); + + TQMap<int, TQMap<int, double> > data; + + static TQMap<int, TQMap<int, double> > pond; + + static float diff(const VoiceSignature &s1, const VoiceSignature &s2); + + + static int size1(); + static int size2(); + + + static TQMemArray<double> fft(const Sound& sound, unsigned int start, unsigned int stop); + static bool window(const Sound& sound, unsigned int *start, unsigned int *stop); + + void write(TDEConfigBase *cfg, const TQString &key) const; + void read(TDEConfigBase *cfg, const TQString &key); + + inline bool isNull() const + { + return data.isEmpty(); + } +}; + +} + +#endif diff --git a/khotkeys/shared/windows.cpp b/khotkeys/shared/windows.cpp new file mode 100644 index 000000000..077bf7b1d --- /dev/null +++ b/khotkeys/shared/windows.cpp @@ -0,0 +1,376 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#define _WINDOWS_CPP_ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "windows.h" + +#include <assert.h> +#include <tqregexp.h> + +#include <tdeconfig.h> +#include <kdebug.h> +#include <twinmodule.h> +#include <twin.h> +#include <tdelocale.h> + +#include "khotkeysglobal.h" + +#include <X11/Xlib.h> +#include <X11/Xutil.h> + +extern Atom tqt_window_role; + +namespace KHotKeys +{ + +// Windows + +Windows::Windows( bool enable_signal_P, TQObject* parent_P ) + : TQObject( parent_P ), signals_enabled( enable_signal_P ), + twin_module( new KWinModule( this )), _action_window( 0 ) + { + assert( windows_handler == NULL ); + windows_handler = this; + if( signals_enabled ) + { + connect( twin_module, TQT_SIGNAL( windowAdded( WId )), TQT_SLOT( window_added_slot( WId ))); + connect( twin_module, TQT_SIGNAL( windowRemoved( WId )), TQT_SLOT( window_removed_slot( WId ))); + connect( twin_module, TQT_SIGNAL( activeWindowChanged( WId )), + TQT_SLOT( active_window_changed_slot( WId ))); + } + } + +Windows::~Windows() + { + windows_handler = NULL; + } + +void Windows::window_added_slot( WId window_P ) + { + if( signals_enabled ) + emit window_added( window_P ); + // CHECKME tyhle i dalsi by asi mely jit nastavit, jestli aktivuji vsechny, nebo jen jeden + // pripojeny slot ( stejne jako u Kdb, kde by to take melo jit nastavit ) + } + +void Windows::window_removed_slot( WId window_P ) + { + if( signals_enabled ) + emit window_removed( window_P ); + if( window_P == _action_window ) + _action_window = 0; + } + +void Windows::active_window_changed_slot( WId window_P ) + { + if( signals_enabled ) + emit active_window_changed( window_P ); + } + +void Windows::window_changed_slot( WId window_P ) + { + if( signals_enabled ) + emit window_changed( window_P ); + } + +void Windows::window_changed_slot( WId window_P, unsigned int flags_P ) + { + if( signals_enabled ) + emit window_changed( window_P, flags_P ); + } + +TQString Windows::get_window_role( WId id_P ) + { + // TODO this is probably just a hack + return KWin::readNameProperty( id_P, tqt_window_role ); + } + +TQString Windows::get_window_class( WId id_P ) + { + XClassHint hints_ret; + if( XGetClassHint( tqt_xdisplay(), id_P, &hints_ret ) == 0 ) // 0 means error + return ""; + TQString ret( hints_ret.res_name ); + ret += ' '; + ret += hints_ret.res_class; + XFree( hints_ret.res_name ); + XFree( hints_ret.res_class ); + return ret; + } + +WId Windows::active_window() + { + return twin_module->activeWindow(); + } + +WId Windows::action_window() + { + return _action_window; + } + +void Windows::set_action_window( WId window_P ) + { + _action_window = window_P; + } + +WId Windows::find_window( const Windowdef_list* window_P ) + { + for( TQValueList< WId >::ConstIterator it = twin_module->windows().begin(); + it != twin_module->windows().end(); + ++it ) + { + Window_data tmp( *it ); + if( window_P->match( tmp )) + return *it; + } + return None; + } + +WId Windows::window_at_position( int x, int y ) + { + Window child, dummy; + Window parent = tqt_xrootwin(); + Atom wm_state = XInternAtom( tqt_xdisplay(), "WM_STATE", False ); + for( int i = 0; + i < 10; + ++i ) + { + int destx, desty; + // find child at that position + if( !XTranslateCoordinates( tqt_xdisplay(), parent, parent, x, y, &destx, &desty, &child ) + || child == None ) + return 0; + // and now transform coordinates to the child + if( !XTranslateCoordinates( tqt_xdisplay(), parent, child, x, y, &destx, &desty, &dummy )) + return 0; + x = destx; + y = desty; + Atom type; + int format; + unsigned long nitems, after; + unsigned char* prop; + if( XGetWindowProperty( tqt_xdisplay(), child, wm_state, 0, 0, False, AnyPropertyType, + &type, &format, &nitems, &after, &prop ) == Success ) + { + if( prop != NULL ) + XFree( prop ); + if( type != None ) + return child; + } + parent = child; + } + return 0; + + } + +void Windows::activate_window( WId id_P ) + { + KWin::forceActiveWindow( id_P ); + } + +// Window_data + +Window_data::Window_data( WId id_P ) + : type( NET::Unknown ) + { + KWin::WindowInfo twin_info = KWin::windowInfo( id_P, NET::WMName | NET::WMWindowType ); // TODO optimize + if( twin_info.valid()) + { + title = twin_info.name(); + role = windows_handler->get_window_role( id_P ); + wclass = windows_handler->get_window_class( id_P ); + type = twin_info.windowType( SUPPORTED_WINDOW_TYPES_MASK ); + if( type == NET::Override ) // HACK consider non-NETWM fullscreens to be normal too + type = NET::Normal; + if( type == NET::Unknown ) + type = NET::Normal; + } + } + +// Windowdef + +void Windowdef::cfg_write( TDEConfig& cfg_P ) const + { + cfg_P.writeEntry( "Type", "ERROR" ); + cfg_P.writeEntry( "Comment", comment()); + } + +Windowdef::Windowdef( TDEConfig& cfg_P ) + { + _comment = cfg_P.readEntry( "Comment" ); + } + +Windowdef* Windowdef::create_cfg_read( TDEConfig& cfg_P ) + { + TQString type = cfg_P.readEntry( "Type" ); + if( type == "SIMPLE" ) + return new Windowdef_simple( cfg_P ); + kdWarning( 1217 ) << "Unknown Windowdef type read from cfg file\n"; + return NULL; + } + +// Windowdef_list + +Windowdef_list::Windowdef_list( TDEConfig& cfg_P ) + : TQPtrList< Windowdef >() + { + setAutoDelete( true ); + TQString save_cfg_group = cfg_P.group(); + _comment = cfg_P.readEntry( "Comment" ); + int cnt = cfg_P.readNumEntry( "WindowsCount", 0 ); + for( int i = 0; + i < cnt; + ++i ) + { + cfg_P.setGroup( save_cfg_group + TQString::number( i )); + Windowdef* window = Windowdef::create_cfg_read( cfg_P ); + if( window ) + append( window ); + } + cfg_P.setGroup( save_cfg_group ); + } + +void Windowdef_list::cfg_write( TDEConfig& cfg_P ) const + { + TQString save_cfg_group = cfg_P.group(); + int i = 0; + for( Iterator it( *this ); + it; + ++it, ++i ) + { + cfg_P.setGroup( save_cfg_group + TQString::number( i )); + it.current()->cfg_write( cfg_P ); + } + cfg_P.setGroup( save_cfg_group ); + cfg_P.writeEntry( "WindowsCount", i ); + cfg_P.writeEntry( "Comment", comment()); + } + +Windowdef_list* Windowdef_list::copy() const + { + Windowdef_list* ret = new Windowdef_list( comment()); + for( Iterator it( *this ); + it; + ++it ) + ret->append( it.current()->copy()); + return ret; + } + + +bool Windowdef_list::match( const Window_data& window_P ) const + { + if( count() == 0 ) // CHECKME no windows to match => ok + return true; + for( Iterator it( *this ); + it; + ++it ) + if( it.current()->match( window_P )) + return true; + return false; + } + +// Windowdef_simple + +Windowdef_simple::Windowdef_simple( const TQString& comment_P, const TQString& title_P, + substr_type_t title_type_P, const TQString& wclass_P, substr_type_t wclass_type_P, + const TQString& role_P, substr_type_t role_type_P, int window_types_P ) + : Windowdef( comment_P ), _title( title_P ), title_type( title_type_P ), + _wclass( wclass_P ), wclass_type( wclass_type_P ), _role( role_P ), + role_type( role_type_P ), _window_types( window_types_P ) + { + } + +Windowdef_simple::Windowdef_simple( TDEConfig& cfg_P ) + : Windowdef( cfg_P ) + { + _title = cfg_P.readEntry( "Title" ); + title_type = static_cast< substr_type_t >( cfg_P.readNumEntry( "TitleType" )); + _wclass = cfg_P.readEntry( "Class" ); + wclass_type = static_cast< substr_type_t >( cfg_P.readNumEntry( "ClassType" )); + _role = cfg_P.readEntry( "Role" ); + role_type = static_cast< substr_type_t >( cfg_P.readNumEntry( "RoleType" )); + _window_types = cfg_P.readNumEntry( "WindowTypes" ); + } + +void Windowdef_simple::cfg_write( TDEConfig& cfg_P ) const + { + base::cfg_write( cfg_P ); + cfg_P.writeEntry( "Title", title()); + cfg_P.writeEntry( "TitleType", title_type ); + cfg_P.writeEntry( "Class", wclass()); + cfg_P.writeEntry( "ClassType", wclass_type ); + cfg_P.writeEntry( "Role", role()); + cfg_P.writeEntry( "RoleType", role_type ); + cfg_P.writeEntry( "WindowTypes", window_types()); + cfg_P.writeEntry( "Type", "SIMPLE" ); // overwrites value set in base::cfg_write() + } + +bool Windowdef_simple::match( const Window_data& window_P ) + { + if( !type_match( window_P.type )) + return false; + if( !is_substr_match( window_P.title, title(), title_type )) + return false; + if( !is_substr_match( window_P.wclass, wclass(), wclass_type )) + return false; + if( !is_substr_match( window_P.role, role(), role_type )) + return false; + kdDebug( 1217 ) << "window match:" << window_P.title << ":OK" << endl; + return true; + } + +bool Windowdef_simple::is_substr_match( const TQString& str1_P, const TQString& str2_P, + substr_type_t type_P ) + { + switch( type_P ) + { + case NOT_IMPORTANT : + return true; + case CONTAINS : + return str1_P.contains( str2_P ) > 0; + case IS : + return str1_P == str2_P; + case REGEXP : + { + TQRegExp rg( str2_P ); + return rg.search( str1_P ) >= 0; + } + case CONTAINS_NOT : + return str1_P.contains( str2_P ) == 0; + case IS_NOT : + return str1_P != str2_P; + case REGEXP_NOT : + { + TQRegExp rg( str2_P ); + return rg.search( str1_P ) < 0; + } + } + return false; + } + +Windowdef* Windowdef_simple::copy() const + { + return new Windowdef_simple( comment(), title(), title_match_type(), wclass(), + wclass_match_type(), role(), role_match_type(), window_types()); + } + +const TQString Windowdef_simple::description() const + { + return i18n( "Window simple: " ) + comment(); + } + +} // namespace KHotKeys + +#include "windows.moc" diff --git a/khotkeys/shared/windows.h b/khotkeys/shared/windows.h new file mode 100644 index 000000000..4fa403436 --- /dev/null +++ b/khotkeys/shared/windows.h @@ -0,0 +1,264 @@ +/**************************************************************************** + + KHotKeys + + Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org> + + Distributed under the terms of the GNU General Public License version 2. + +****************************************************************************/ + +#ifndef _WINDOWS_H_ +#define _WINDOWS_H_ + +#include <sys/types.h> + +#include <tqobject.h> +#include <tqstring.h> +#include <tqptrlist.h> + +#include <netwm_def.h> + +#include "khotkeysglobal.h" + +class TDEConfig; +class KWinModule; + +namespace KHotKeys +{ + +const int SUPPORTED_WINDOW_TYPES_MASK = NET::NormalMask | NET::DesktopMask | NET::DockMask + | NET::ToolbarMask | NET::MenuMask | NET::DialogMask | NET::OverrideMask | NET::TopMenuMask + | NET::UtilityMask | NET::SplashMask; + +class Windowdef_list; +/*class Action_data_base;*/ + +class KDE_EXPORT Windows + : public TQObject + { + Q_OBJECT + public: + Windows( bool enable_signals_P, TQObject* parent_P ); + virtual ~Windows(); + TQString get_window_class( WId id_P ); + TQString get_window_role( WId id_P ); + WId active_window(); + void set_action_window( WId window ); + WId action_window(); + WId find_window( const Windowdef_list* window_P ); + static WId window_at_position( int x, int y ); + static void activate_window( WId id_P ); + signals: + void window_added( WId window_P ); + void window_removed( WId window_P ); + void active_window_changed( WId window_P ); + void window_changed( WId window_P ); + void window_changed( WId window_P, unsigned int flags_P ); + protected slots: + void window_added_slot( WId window_P ); + void window_removed_slot( WId window_P ); + void active_window_changed_slot( WId window_P ); + void window_changed_slot( WId window_P ); + void window_changed_slot( WId window_P, unsigned int flags_P ); + private: + bool signals_enabled; + KWinModule* twin_module; + WId _action_window; + }; + +struct KDE_EXPORT Window_data + { + Window_data( WId id_P ); + TQString title; // _NET_WM_NAME or WM_NAME + TQString role; // WM_WINDOW_ROLE + TQString wclass; // WM_CLASS + NET::WindowType type; + }; + +class KDE_EXPORT Windowdef + { + public: + Windowdef( const TQString& comment_P ); + Windowdef( TDEConfig& cfg_P ); + virtual ~Windowdef(); + const TQString& comment() const; + virtual bool match( const Window_data& window_P ) = 0; + static Windowdef* create_cfg_read( TDEConfig& cfg_P/*, Action_data_base* data_P*/ ); + virtual void cfg_write( TDEConfig& cfg_P ) const = 0; + virtual Windowdef* copy( /*Action_data_base* data_P*/ ) const = 0; + virtual const TQString description() const = 0; + private: + TQString _comment; + KHOTKEYS_DISABLE_COPY( Windowdef ); // CHECKME asi pak udelat i pro vsechny potomky, at se nezapomene + }; + +class KDE_EXPORT Windowdef_list + : public TQPtrList< Windowdef > + { + public: + Windowdef_list( const TQString& comment_P ); + Windowdef_list( TDEConfig& cfg_P/*, Action_data_base* data_P*/ ); + void cfg_write( TDEConfig& cfg_P ) const; + bool match( const Window_data& window_P ) const; + Windowdef_list* copy( /*Action_data_base* data_P*/ ) const; + typedef TQPtrListIterator< Windowdef > Iterator; + const TQString& comment() const; + private: + TQString _comment; + KHOTKEYS_DISABLE_COPY( Windowdef_list ); + }; + +class KDE_EXPORT Windowdef_simple + : public Windowdef + { + typedef Windowdef base; + public: + enum substr_type_t + { + NOT_IMPORTANT, + CONTAINS, + IS, + REGEXP, + CONTAINS_NOT, + IS_NOT, + REGEXP_NOT + }; + enum window_type_t + { + WINDOW_TYPE_NORMAL = ( 1 << NET::Normal ), + WINDOW_TYPE_DESKTOP = ( 1 << NET::Desktop ), + WINDOW_TYPE_DOCK = ( 1 << NET::Dock ), +// WINDOW_TYPE_TOOL = ( 1 << NET::Tool ), +// WINDOW_TYPE_MENU = ( 1 << NET::Menu ), + WINDOW_TYPE_DIALOG = ( 1 << NET::Dialog ) + }; + Windowdef_simple( const TQString& comment_P, const TQString& title_P, + substr_type_t title_type_P, const TQString& wclass_P, substr_type_t wclass_type_P, + const TQString& role_P, substr_type_t role_type_P, int window_types_P ); + Windowdef_simple( TDEConfig& cfg_P ); + virtual bool match( const Window_data& window_P ); + virtual void cfg_write( TDEConfig& cfg_P ) const; + const TQString& title() const; + substr_type_t title_match_type() const; + const TQString& wclass() const; + substr_type_t wclass_match_type() const; + const TQString& role() const; + substr_type_t role_match_type() const; + int window_types() const; + bool type_match( window_type_t type_P ) const; + bool type_match( NET::WindowType type_P ) const; + virtual Windowdef* copy( /*Action_data_base* data_P*/ ) const; + virtual const TQString description() const; + protected: + bool is_substr_match( const TQString& str1_P, const TQString& str2_P, + substr_type_t type_P ); + private: + TQString _title; + substr_type_t title_type; + TQString _wclass; + substr_type_t wclass_type; + TQString _role; + substr_type_t role_type; + int _window_types; + }; + +//*************************************************************************** +// Inline +//*************************************************************************** + +// Windowdef + +inline +Windowdef::Windowdef( const TQString& comment_P ) + : _comment( comment_P ) + { + } + +inline +const TQString& Windowdef::comment() const + { + return _comment; + } + +inline +Windowdef::~Windowdef() + { + } + +// Windowdef_list + +inline +Windowdef_list::Windowdef_list( const TQString& comment_P ) + : TQPtrList< Windowdef >(), _comment( comment_P ) + { + setAutoDelete( true ); + } + +inline +const TQString& Windowdef_list::comment() const + { + return _comment; + } + +// Windowdef_simple + +inline +const TQString& Windowdef_simple::title() const + { + return _title; + } + +inline +Windowdef_simple::substr_type_t Windowdef_simple::title_match_type() const + { + return title_type; + } + +inline +const TQString& Windowdef_simple::wclass() const + { + return _wclass; + } + +inline +Windowdef_simple::substr_type_t Windowdef_simple::wclass_match_type() const + { + return wclass_type; + } + +inline +const TQString& Windowdef_simple::role() const + { + return _role; + } + +inline +Windowdef_simple::substr_type_t Windowdef_simple::role_match_type() const + { + return role_type; + } + +inline +int Windowdef_simple::window_types() const + { + return _window_types; + } + +inline +bool Windowdef_simple::type_match( window_type_t type_P ) const + { + return window_types() & type_P; + } + +inline +bool Windowdef_simple::type_match( NET::WindowType type_P ) const + { + return ( window_types() & ( 1 << type_P )) + || ( type_P == NET::Unknown && ( window_types() & WINDOW_TYPE_NORMAL )); + // CHECKME HACK haaaack ! + } + +} // namespace KHotKeys + +#endif |