616f374a558d7cfe96bc2257f9839839b01c6cfd
[senf.git] / Utils / Termlib / Terminfo.hh
1 // $Id$
2 //
3 // Copyright (C) 2009 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Terminfo public header */
25
26 #ifndef HH_SENF_Utils_Termlib_Terminfo_
27 #define HH_SENF_Utils_Termlib_Terminfo_ 1
28
29 // Custom includes
30 #include <string>
31 #include <vector>
32 #include <map>
33 #include <iostream>
34 #include <boost/cstdint.hpp>
35 #include <boost/array.hpp>
36 #include <senf/Utils/Exception.hh>
37
38 //#include "Terminfo.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
40
41 namespace senf {
42 namespace term {
43
44     class Terminfo
45     {
46     public:
47         ///////////////////////////////////////////////////////////////////////////
48         // Types
49
50         enum { NoValue = -1 };
51
52         struct properties
53         {
54             enum Boolean {
55                 AutoLeftMargin, AutoRightMargin, NoEscCtlc, CeolStandoutGlitch, EatNewlineGlitch,
56                 EraseOverstrike, GenericType, HardCopy, HasMetaKey, HasStatusLine, InsertNullGlitch,
57                 MemoryAbove, MemoryBelow, MoveInsertMode, MoveStandoutMode, OverStrike,
58                 StatusLineEscOk, DestTabsMagicSmso, TildeGlitch, TransparentUnderline, XonXoff,
59                 NeedsXonXoff, PrtrSilent, HardCursor, NonRevRmcup, NoPadChar, NonDestScrollRegion,
60                 CanChange, BackColorErase, HueLightnessSaturation, ColAddrGlitch,
61                 CrCancelsMicroMode, HasPrintWheel, RowAddrGlitch, SemiAutoRightMargin,
62                 CpiChangesRes, LpiChangesRes, BackspacesWithBs, CrtNoScrolling,
63                 NoCorrectlyWorkingCr, GnuHasMetaKey, LinefeedIsNewline, HasHardwareTabs,
64                 ReturnDoesClrEol };
65             
66             static char const * const BooleanNames[];
67
68             enum Numeric {
69                 Columns, InitTabs, Lines, LinesOfMemory, MagicCookieGlitch, PaddingBaudRate,
70                 VirtualTerminal, WidthStatusLine, NumLabels, LabelHeight, LabelWidth, MaxAttributes,
71                 MaximumWindows, MaxColors, MaxPairs, NoColorVideo, BufferCapacity, DotVertSpacing,
72                 DotHorzSpacing, MaxMicroAddress, MaxMicroJump, MicroColSize, MicroLineSize,
73                 NumberOfPins, OutputResChar, OutputResLine, OutputResHorzInch, OutputResVertInch,
74                 PrintRate, WideCharSize, Buttons, BitImageEntwining, BitImageType,
75                 MagicCookieGlitchUl, CarriageReturnDelay, NewLineDelay, BackspaceDelay,
76                 HorizontalTabDelay, NumberOfFunctionKeys };
77
78             static char const * const NumericNames[];
79             
80             enum String {
81                 BackTab, Bell, CarriageReturn, ChangeScrollRegion, ClearAllTabs, ClearScreen,
82                 ClrEol, ClrEos, ColumnAddress, CommandCharacter, CursorAddress, CursorDown,
83                 CursorHome, CursorInvisible, CursorLeft, CursorMemAddress, CursorNormal,
84                 CursorRight, CursorToLl, CursorUp, CursorVisible, DeleteCharacter, DeleteLine,
85                 DisStatusLine, DownHalfLine, EnterAltCharsetMode, EnterBlinkMode, EnterBoldMode,
86                 EnterCaMode, EnterDeleteMode, EnterDimMode, EnterInsertMode, EnterSecureMode,
87                 EnterProtectedMode, EnterReverseMode, EnterStandoutMode, EnterUnderlineMode,
88                 EraseChars, ExitAltCharsetMode, ExitAttributeMode, ExitCaMode, ExitDeleteMode,
89                 ExitInsertMode, ExitStandoutMode, ExitUnderlineMode, FlashScreen, FormFeed,
90                 FromStatusLine, Init1string, Init2string, Init3string, InitFile, InsertCharacter,
91                 InsertLine, InsertPadding, KeyBackspace, KeyCatab, KeyClear, KeyCtab, KeyDc, KeyDl,
92                 KeyDown, KeyEic, KeyEol, KeyEos, KeyF0, KeyF1, KeyF10, KeyF2, KeyF3, KeyF4, KeyF5,
93                 KeyF6, KeyF7, KeyF8, KeyF9, KeyHome, KeyIc, KeyIl, KeyLeft, KeyLl, KeyNpage,
94                 KeyPpage, KeyRight, KeySf, KeySr, KeyStab, KeyUp, KeypadLocal, KeypadXmit, LabF0,
95                 LabF1, LabF10, LabF2, LabF3, LabF4, LabF5, LabF6, LabF7, LabF8, LabF9, MetaOff,
96                 MetaOn, Newline, PadChar, ParmDch, ParmDeleteLine, ParmDownCursor, ParmIch,
97                 ParmIndex, ParmInsertLine, ParmLeftCursor, ParmRightCursor, ParmRindex,
98                 ParmUpCursor, PkeyKey, PkeyLocal, PkeyXmit, PrintScreen, PrtrOff, PrtrOn,
99                 RepeatChar, Reset1string, Reset2string, Reset3string, ResetFile, RestoreCursor,
100                 RowAddress, SaveCursor, ScrollForward, ScrollReverse, SetAttributes, SetTab,
101                 SetWindow, Tab, ToStatusLine, UnderlineChar, UpHalfLine, InitProg, KeyA1, KeyA3,
102                 KeyB2, KeyC1, KeyC3, PrtrNon, CharPadding, AcsChars, PlabNorm, KeyBtab,
103                 EnterXonMode, ExitXonMode, EnterAmMode, ExitAmMode, XonCharacter, XoffCharacter,
104                 EnaAcs, LabelOn, LabelOff, KeyBeg, KeyCancel, KeyClose, KeyCommand, KeyCopy,
105                 KeyCreate, KeyEnd, KeyEnter, KeyExit, KeyFind, KeyHelp, KeyMark, KeyMessage,
106                 KeyMove, KeyNext, KeyOpen, KeyOptions, KeyPrevious, KeyPrint, KeyRedo, KeyReference,
107                 KeyRefresh, KeyReplace, KeyRestart, KeyResume, KeySave, KeySuspend, KeyUndo,
108                 KeySbeg, KeyScancel, KeyScommand, KeyScopy, KeyScreate, KeySdc, KeySdl, KeySelect,
109                 KeySend, KeySeol, KeySexit, KeySfind, KeyShelp, KeyShome, KeySic, KeySleft,
110                 KeySmessage, KeySmove, KeySnext, KeySoptions, KeySprevious, KeySprint, KeySredo,
111                 KeySreplace, KeySright, KeySrsume, KeySsave, KeySsuspend, KeySundo, ReqForInput,
112                 KeyF11, KeyF12, KeyF13, KeyF14, KeyF15, KeyF16, KeyF17, KeyF18, KeyF19, KeyF20,
113                 KeyF21, KeyF22, KeyF23, KeyF24, KeyF25, KeyF26, KeyF27, KeyF28, KeyF29, KeyF30,
114                 KeyF31, KeyF32, KeyF33, KeyF34, KeyF35, KeyF36, KeyF37, KeyF38, KeyF39, KeyF40,
115                 KeyF41, KeyF42, KeyF43, KeyF44, KeyF45, KeyF46, KeyF47, KeyF48, KeyF49, KeyF50,
116                 KeyF51, KeyF52, KeyF53, KeyF54, KeyF55, KeyF56, KeyF57, KeyF58, KeyF59, KeyF60,
117                 KeyF61, KeyF62, KeyF63, ClrBol, ClearMargins, SetLeftMargin, SetRightMargin,
118                 LabelFormat, SetClock, DisplayClock, RemoveClock, CreateWindow, GotoWindow, Hangup,
119                 DialPhone, QuickDial, Tone, Pulse, FlashHook, FixedPause, WaitTone, User0, User1,
120                 User2, User3, User4, User5, User6, User7, User8, User9, OrigPair, OrigColors,
121                 InitializeColor, InitializePair, SetColorPair, SetForeground, SetBackground,
122                 ChangeCharPitch, ChangeLinePitch, ChangeResHorz, ChangeResVert, DefineChar,
123                 EnterDoublewideMode, EnterDraftQuality, EnterItalicsMode, EnterLeftwardMode,
124                 EnterMicroMode, EnterNearLetterQuality, EnterNormalQuality, EnterShadowMode,
125                 EnterSubscriptMode, EnterSuperscriptMode, EnterUpwardMode, ExitDoublewideMode,
126                 ExitItalicsMode, ExitLeftwardMode, ExitMicroMode, ExitShadowMode, ExitSubscriptMode,
127                 ExitSuperscriptMode, ExitUpwardMode, MicroColumnAddress, MicroDown, MicroLeft,
128                 MicroRight, MicroRowAddress, MicroUp, OrderOfPins, ParmDownMicro, ParmLeftMicro,
129                 ParmRightMicro, ParmUpMicro, SelectCharSet, SetBottomMargin, SetBottomMarginParm,
130                 SetLeftMarginParm, SetRightMarginParm, SetTopMargin, SetTopMarginParm,
131                 StartBitImage, StartCharSetDef, StopBitImage, StopCharSetDef, SubscriptCharacters,
132                 SuperscriptCharacters, TheseCauseCr, ZeroMotion, CharSetNames, KeyMouse, MouseInfo,
133                 ReqMousePos, GetMouse, SetAForeground, SetABackground, PkeyPlab, DeviceType,
134                 CodeSetInit, Set0DesSeq, Set1DesSeq, Set2DesSeq, Set3DesSeq, SetLrMargin,
135                 SetTbMargin, BitImageRepeat, BitImageNewline, BitImageCarriageReturn, ColorNames,
136                 DefineBitImageRegion, EndBitImageRegion, SetColorBand, SetPageLength, DisplayPcChar,
137                 EnterPcCharsetMode, ExitPcCharsetMode, EnterScancodeMode, ExitScancodeMode,
138                 PcTermOptions, ScancodeEscape, AltScancodeEsc, EnterHorizontalHlMode,
139                 EnterLeftHlMode, EnterLowHlMode, EnterRightHlMode, EnterTopHlMode,
140                 EnterVerticalHlMode, SetAAttributes, SetPglenInch, TermcapInit2, TermcapReset,
141                 LinefeedIfNotLf, BackspaceIfNotBs, OtherNonFunctionKeys, ArrowKeyMap, AcsUlcorner,
142                 AcsLlcorner, AcsUrcorner, AcsLrcorner, AcsLtee, AcsRtee, AcsBtee, AcsTtee, AcsHline,
143                 AcsVline, AcsPlus, MemoryLock, MemoryUnlock, BoxChars1 };
144
145             static char const * const StringNames[];
146         };
147
148         typedef boost::int16_t number_t;
149         typedef char const* string_t;
150
151         ///////////////////////////////////////////////////////////////////////////
152         
153         Terminfo();
154         explicit Terminfo(std::string const & term);
155         void load(std::string const & term);
156         
157         bool getFlag(properties::Boolean p) const;
158         number_t getNumber(properties::Numeric p) const;
159         string_t getString(properties::String p) const;
160         bool hasProperty(properties::Boolean p) const;
161         bool hasProperty(properties::Numeric p ) const;
162         bool hasProperty(properties::String p ) const;
163         std::string formatString(properties::String p,
164                                  number_t arg1=NoValue, number_t arg2=NoValue, 
165                                  number_t arg3=NoValue, number_t arg4=NoValue,
166                                  number_t arg5=NoValue, number_t arg6=NoValue,
167                                  number_t arg7=NoValue, number_t arg8=NoValue,
168                                  number_t arg9=NoValue) const;
169
170         ///////////////////////////////////////////////////////////////////////////
171
172         void dump(std::ostream & os) const;
173
174         struct InvalidTerminfoException : public senf::Exception
175         { InvalidTerminfoException() : senf::Exception("Unreadable terminfo file") {} };
176
177     private:
178         typedef std::vector<bool> BoolVec;
179         typedef std::vector<number_t> NumberVec;
180         typedef std::vector<string_t> StringVec;
181         typedef std::vector<char> StringPool;
182
183         std::string findTerminfo(std::string const & name);
184         void load(std::istream & is);
185
186         std::string name_;
187         BoolVec booleans_;
188         NumberVec numbers_;
189         StringVec strings_;
190         StringPool stringPool_;
191     };
192
193
194     class KeyParser
195     {
196     public:
197         ///////////////////////////////////////////////////////////////////////////
198         // Types
199
200         enum KeyCode {
201             Space = ' ', Tab = '\t', Return = '\r', First = 0xE000, Esc = First, Backspace, Backtab,
202             Begin, CATab, CTab, Cancel, Center, Clear, ClearToEOL, ClearToEOS, Close, Command, Copy,
203             Create, Delete, DeleteLine, Down, DownLeft, DownRight, End, Enter, Exit, F0, F1, F2, F3,
204             F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
205             F23, F24, F25, F26, F27, F28, F29, F30, F31, F32, F33, F34, F35, F36, F37, F38, F39,
206             F40, F41, F42, F43, F44, F45, F46, F47, F48, F49, F50, F51, F52, F53, F54, F55, F56,
207             F57, F58, F59, F60, F61, F62, F63, Find, Help, Home, Insert, InsertLine, Left, Mark,
208             Message, Mouse, Move, Next, Open, Options, PageDown, PageUp, Previous, Print, Redo,
209             Reference, Refresh, Replace, Restart, Resume, Right, Save, Select, ShiftBegin,
210             ShiftCancel, ShiftCommand, ShiftCopy, ShiftCreate, ShiftDelete, ShiftDeleteLine,
211             ShiftEnd, ShiftClearToEOL, ShiftExit, ShiftFind, ShiftHelp, ShiftHome, ShiftInsert,
212             ShiftLeft, ShiftMessage, ShiftMove, ShiftNext, ShiftOptions, ShiftPrevious, ShiftPrint,
213             ShiftRedo, ShiftReplace, ShiftResume, ShiftRight, ShiftSave, ShiftSuspend, ShiftTab,
214             ShiftUndo, Suspend, Undo, Up, UpLeft, UpRight, Incomplete = 0xE0FF };
215
216         static char const * const KeyNames[];
217
218         typedef wchar_t keycode_t;
219         typedef std::string::size_type size_type;
220
221         static keycode_t Ctrl(char ch) { return ch-'@'; }
222
223         ///////////////////////////////////////////////////////////////////////////
224
225         KeyParser();
226         explicit KeyParser(Terminfo const & ti);
227         void load(Terminfo const & ti);
228
229         std::pair<keycode_t, size_type> lookup(std::string const & key) const;
230         static std::string describe(keycode_t key);
231
232         void dump(std::ostream & os) const;
233
234     private:
235         typedef std::map<std::string, KeyCode> Keytable;
236
237         Keytable table_;
238     };
239         
240 }}
241
242 ///////////////////////////////hh.e////////////////////////////////////////
243 //#include "Terminfo.cci"
244 //#include "Terminfo.ct"
245 //#include "Terminfo.cti"
246 #endif
247
248 \f
249 // Local Variables:
250 // mode: c++
251 // fill-column: 100
252 // comment-column: 40
253 // c-file-style: "senf"
254 // indent-tabs-mode: nil
255 // ispell-local-dictionary: "american"
256 // compile-command: "scons -u test"
257 // End: