1 // C++ Interface: lobject
2 //
3 // Description: Lineak Object which is the base class for LKey and
4 //   LButton.
5 //
6 // Author: Sheldon Lee Wen <tormak@rogers.com> (C) 2004
7 //
8 // Copyright: See COPYING file that comes with this distribution
9 //
10 #ifndef LOBJECT_H
11 #define LOBJECT_H
12 
13 #include <string>
14 #include <vector>
15 #include <map>
16 #include <queue>
17 
18 #include <stdio.h>
19 //#include <X11/Xlib.h>
20 //#include <X11/XKBlib.h>
21 //#include <X11/extensions/XKBfile.h>
22 
23 #include <lineak/definitions.h>
24 #include <lineak/lconfig.h>
25 
26 class LCommand;
27 
28 typedef enum { PRESS, RELEASE, UNKNOWN_EVENT } EventType_t;
29 typedef enum { SYM, CODE, BUTTON, UNKNOWN_KEY } KeyType_t;
30 
31 using namespace std;
32 /**
33    Base object from which LKey and LButton inherits.
34    It is initialized with a name, event type, and key type.
35    OK This class is a mess, and it sucks.
36 
37    @author Sheldon Lee Wen.
38 
39 */
40 class LObject {
41    public:
42       LObject();
43       LObject(string iname, EventType_t ietype, KeyType_t itype);
44       virtual ~LObject();
45       //bool operator==(LObject & rhs);
46       /** This method gets the name of the object initially defined in the constructor.
47           (which is set from the unparsed key name from the keyboard definition file.
48           It returns this name. */
getName()49       virtual inline string getName() { return name; }
setName(string iname)50       virtual inline void setName(string iname) {name = iname;}
getModifiers()51       virtual inline const vector<unsigned int>& getModifiers() const { return modifiers; }
getEventType()52       virtual inline const EventType_t getEventType() const { return event_type; }
getType()53       virtual inline const KeyType_t getType() const { return type; }
54       virtual void addModifier(unsigned int imodifier);
setEventType(EventType_t iet)55       virtual inline void setEventType(EventType_t iet) { event_type = iet; }
setType(KeyType_t itype)56       virtual inline void setType(KeyType_t itype) { type = itype; }
57       /** Get a command. The command that gets returned depends on a few things:
58       		If the command is a togglable command, we return the command corresponding to the name at the front of the toggle_names queue.
59 		If the command is not a toggleable command, and we call it with mod = 0, or the default way, we return the value of the default unmodified command for this object.
60 		If the command is not a toggleable command, and we call it with mod != 0, return the command that corresponds to that modifier.
61       */
62       virtual LCommand & getCommand(unsigned int mod = 0);
63       // Get the command corresponding to toggle name togname
64       virtual LCommand & getToggleCommand(const string& togname);
65       // Get the display name associated with the command of modifier mod
66       virtual string & getCommandDisplayName(unsigned int mod = 0);
67       // Get the display name corresponding to command for toggle name togname
68       virtual string & getToggleCommandDisplayName(const string& togname);
69       /** Set a command corresponding to a modifier sequence. If mod = 0, set it as the default command. */
70       virtual void setCommand(LCommand command, unsigned int mod = 0);
71       /** Set a display name corresponding to a modifier sequence. By default set the default display name. */
72       virtual void setCommandDisplayName(string dname, unsigned int mod = 0);
73       //void setCommand(LCommand command);
74       virtual string getModifierString(unsigned int modifier);
75       virtual string getEventTypeString();
76       virtual string getTypeString();
77       virtual void print(ostream &out);
78       //virtual void serialize(ostream &out);
79       //virtual void unserialize(/*istream &in*/) {/** This doesn't do anything yet*/};
80 
81       // TOGGLE SHIT. This is where this gets nasty, probably as a result of poor OO design on my part.
82       /** Return whether or not this is a toggleable key. The distinction is
83           necessary for on screen display where you have a config file entry of the
84 	  form:
85              key|key = commandm
86           In this case the key is not being used as toggleable in the sense that
87           each key press is bound to a different command. We are using it as
88           a normal key. However, for the sake of the on screen display, we want
89           to toggle the display. */
90       //inline bool isDisplayToggle() const { return dtoggle; }
91       /** Return whether or not we are using this key as a toggleable key */
92       virtual bool isUsedAsToggle() const;
93       virtual bool isToggle() const;
94       virtual void setUsedAsToggle(bool tog);
95       //inline void setDisplayToggle(bool tog) { dtoggle = tog; }
96       /** If isToggle() , rotate the key to the next display name. */
97       virtual void toggleState();
98       /** If it is a toggleable key, return the command for the next state
99           (determined by the next display name in the queue. */
100       /** Add a toggleable name. This automatically sets the toggleable status,
101           and adds the name to the display_names queue. */
102       virtual void addToggleName(const string iname);
103       virtual void setToggleCommandDisplayName(string dname, const string iname);
104       virtual vector<string> getToggleNames();
105       /** This method returns true if this object is named, or has a toggleable name
106           that matches iname. */
107       virtual bool ownsName(string iname);
108       virtual bool hasModifier(const unsigned int modifier);
109       virtual string getNextToggleName();
110       virtual void setCommand(LCommand command, string iname);
111       //virtual void setToggleModifier(string iname, unsigned int imodifier);
112       virtual void clear();
113       // Clear all data for the toggle part of the key.
114       virtual void clearToggleData();
115       // Clear all modifier data.
116       virtual void clearModifierData();
117       vector<keycommand_info> getKeycommand_info();
118 
119       // Remove a command.
120       virtual void removeCommand(unsigned int mod = 0);
121       virtual void removeCommand(const string& togname);
122    protected:
123       string name;
124       string blank;
125       LCommand empty;
126       EventType_t event_type;
127       KeyType_t type;
128       // Keep a list of all modifiers we respect.
129       vector<unsigned int> modifiers;
130       // Keep a list of commands indexed by modifiers.
131       map<unsigned int,LCommand> commands;
132       map<unsigned int,string> commandDisplayNames;
133 
134       bool used_toggle,is_toggle,dtoggle;
135       map<string,LCommand> tog_commands;
136       map<string,string> tog_commandDisplayNames;
137       queue<string> toggle_names;
138 
139 };
140 
141 ostream & operator<<(ostream &out, LObject &rhs);
142 
143 #endif
144