1 /*****************************************************************************
2  *
3  * Copyright (c) 2008-2010, CoreCodec, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of CoreCodec, Inc. nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CoreCodec, Inc. ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL CoreCodec, Inc. BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  ****************************************************************************/
29 
30 #include "parser.h"
31 
32 typedef struct hotkeyname
33 {
34     uint32_t Key;
35     const tchar_t* XML;
36 
37 } hotkeyname;
38 
39 static const hotkeyname HotKeyName[] =
40 {
41     // first the masks
42     { HOTKEY_SHIFT,             T("Shift")},
43     { HOTKEY_CTRL,              T("Ctrl")},
44     { HOTKEY_ALT,               T("Alt")},
45     { HOTKEY_WIN,               T("Win")},
46     { HOTKEY_HOLD,              T("Hold")},
47 
48     { HOTKEY_MEDIA_STOP,        T("Stop")},
49     { HOTKEY_MEDIA_PLAY,		T("Play")},
50     { HOTKEY_MEDIA_PREV,		T("Prev")},
51     { HOTKEY_MEDIA_NEXT,		T("Next")},
52     { HOTKEY_MEDIA_VOLUME_UP,  	T("VolUp")},
53     { HOTKEY_MEDIA_VOLUME_DOWN, T("VolDown")},
54     { HOTKEY_MEDIA_MUTE,		T("Mute")},
55     { HOTKEY_MEDIA_MOVEFWD,		T("MoveFwd")},
56     { HOTKEY_MEDIA_MOVEBACK,	T("MoveBack")},
57 
58     { HOTKEY_DPAD_ACTION,		T("Action")},
59     { HOTKEY_DPAD_LEFT, 		T("Left")},
60     { HOTKEY_DPAD_RIGHT,		T("Right")},
61     { HOTKEY_DPAD_UP,		    T("Up")},
62     { HOTKEY_DPAD_DOWN,         T("Down")},
63 
64     { HOTKEY_ENTER,             T("Enter")},
65     { HOTKEY_SPACE,             T("Space")},
66     { HOTKEY_ESCAPE,            T("Escape")},
67     { HOTKEY_TABNEXT,           T("TabNext")},
68     { HOTKEY_TABPREV,           T("TabPrev")},
69     { HOTKEY_BACKSPACE,         T("BackSpace")},
70     { HOTKEY_DELETE,            T("Delete")},
71 
72     { HOTKEY_SELECT,            T("Select")},
73     { HOTKEY_START,             T("Start")},
74     { HOTKEY_TRIANGLE,          T("Triangle")},
75     { HOTKEY_SQUARE,            T("Square")},
76     { HOTKEY_CIRCLE,            T("Circle")},
77     { HOTKEY_CROSS,             T("Cross")},
78 
79     {0,                         NULL},
80 
81     // should be right after {0}
82     { HOTKEY_APP_FIRST,         T("App%d")},
83 };
84 
GetHotKeyName(const hotkeyname * i,hotkeygetname GetName,void * GetNameParam)85 static const tchar_t* GetHotKeyName(const hotkeyname* i, hotkeygetname GetName, void* GetNameParam)
86 {
87     if (GetName)
88         return GetName(GetNameParam,i->Key);
89     return i->XML;
90 }
91 
HotKeyToString(tchar_t * Out,size_t OutLen,uint32_t HotKey,hotkeygetname GetName,void * GetNameParam)92 void HotKeyToString(tchar_t* Out, size_t OutLen, uint32_t HotKey, hotkeygetname GetName, void* GetNameParam)
93 {
94     const hotkeyname* i = HotKeyName;
95     if (!GetName)
96     {
97         // force #<N> XML exporting to make import safer...
98         stprintf_s(Out,OutLen,T("#%x"),(int)HotKey);
99         return;
100     }
101 
102     *Out = 0;
103 
104     for (;i->Key > HOTKEY_MASK;++i)
105         if (HotKey & i->Key)
106         {
107             tcscat_s(Out,OutLen,GetHotKeyName(i,GetName,GetNameParam));
108             tcscat_s(Out,OutLen,T("+"));
109         }
110 
111     HotKey &= HOTKEY_MASK;
112 
113     for (;i->Key;++i)
114         if (HotKey == i->Key)
115         {
116             tcscat_s(Out,OutLen,GetHotKeyName(i,GetName,GetNameParam));
117             return;
118         }
119 
120 	if ((HotKey >= '0' && HotKey <= '9') || (HotKey >= 'A' && HotKey <= 'Z'))
121 		stcatprintf_s(Out,OutLen,T("%c"),HotKey);
122     else
123     if (HotKey >= HOTKEY_FUNC_FIRST && HotKey <= HOTKEY_FUNC_LAST)
124 		stcatprintf_s(Out,OutLen,T("F%d"),HotKey-HOTKEY_FUNC_FIRST+1);
125     else
126     if (HotKey >= HOTKEY_APP_FIRST && HotKey <= HOTKEY_APP_LAST)
127         stcatprintf_s(Out,OutLen,GetHotKeyName(i+1,GetName,GetNameParam),HotKey-HOTKEY_APP_FIRST+1);
128     else
129 		stcatprintf_s(Out,OutLen,T("#%02X"),HotKey);
130 }
131 
StringToHotKey(const tchar_t * In)132 uint32_t StringToHotKey(const tchar_t* In)
133 {
134     const tchar_t* m;
135     const hotkeyname* i;
136     uint32_t HotKey = 0;
137     size_t n;
138 
139     ExprSkipSpace(&In);
140 
141     while ((m = tcschr(In,'+')) != NULL)
142     {
143         n = m-In;
144         while (n>0 && IsSpace(In[n-1]))
145             --n;
146 
147         for (i = HotKeyName;i->Key > HOTKEY_MASK;++i)
148             if (tcsnicmp(In,i->XML,n)==0)
149             {
150                 HotKey |= i->Key;
151                 break;
152             }
153 
154         In = m+1;
155     }
156 
157     if (!In[0])
158         return 0;
159 
160     for (i = HotKeyName;i->Key;++i)
161         if (tcsisame_ascii(In,i->XML))
162             return HotKey | i->Key;
163 
164     if ((In[0] == 'f' || In[0] == 'F') && IsDigit(In[1]))
165         return HotKey | (HOTKEY_FUNC_FIRST + StringToInt(In+1,0) - 1);
166 
167     if (In[0] == '#')
168         return HotKey | StringToInt(In+1,1);
169 
170     n = tcslen(In);
171     if (n>1 && IsDigit(In[n-1]))
172     {
173         while (n>0 && IsDigit(In[n-1]))
174             --n;
175         return HotKey | (HOTKEY_APP_FIRST + StringToInt(In+n,0) - 1);
176     }
177 
178 	return HotKey | In[0];
179 }
180