1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
//
// Copyright 2005 The Android Open Source Project
//
// Simulated device data.
//
// For compilers that support precompilation, include "wx/wx.h".
#include "wx/wxprec.h"
// Otherwise, include all standard headers
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
#include "wx/image.h" // needed for Windows build
#include "LinuxKeys.h"
#include "PhoneButton.h"
using namespace android;
/*
* Create a PhoneButton without a backing image.
*/
bool PhoneButton::Create(const char* label)
{
assert(!mHasImage); // quick check for re-use
mKeyCode = LookupKeyCode(label);
if (mKeyCode == kKeyCodeUnknown) {
fprintf(stderr, "WARNING: key code '%s' not recognized\n", label);
// keep going
}
return true;
}
/*
* Create a PhoneButton with an associated image. Don't load the image yet.
*/
bool PhoneButton::Create(const char* label, const char* imageFileName,
int x, int y)
{
if (!Create(label))
return false;
if (mSelectedImage.Create(imageFileName, x, y))
mHasImage = true;
else
fprintf(stderr, "Warning: image create (%s, %d, %d) failed\n",
imageFileName, x, y);
return true;
}
/*
* Load the image, if any.
*/
bool PhoneButton::LoadResources(void)
{
if (!mHasImage)
return true; // no image associated with this button
bool result = mSelectedImage.LoadResources();
if (result)
CreateHighlightedBitmap();
return result;
}
/*
* Unload the image if we loaded one.
*/
bool PhoneButton::UnloadResources(void)
{
if (!mHasImage)
return true;
return mSelectedImage.UnloadResources();
}
/* use an inline instead of macro so we don't evaluate args multiple times */
static inline int MinVal(int a, int b) { return (a < b ? a : b); }
/*
* Create the "highlighted" bitmap from the "selected" image.
*/
void PhoneButton::CreateHighlightedBitmap(void)
{
wxBitmap* src = mSelectedImage.GetBitmap();
assert(src != NULL);
wxImage tmpImage = src->ConvertToImage();
unsigned char* pRGB = tmpImage.GetData(); // top-left RGBRGB...
int x, y;
/*
* Modify the color used for the "highlight" image.
*/
for (y = tmpImage.GetHeight()-1; y >= 0; --y) {
for (x = tmpImage.GetWidth()-1; x >= 0; --x) {
*(pRGB) = MinVal(*(pRGB) + *(pRGB) / 8, 255);
*(pRGB+1) = MinVal(*(pRGB+1) + *(pRGB+1) / 8, 255);
*(pRGB+2) = *(pRGB+2) * 5 / 8;
pRGB += 3;
}
}
mHighlightedBitmap = wxBitmap(tmpImage);
}
/*
* Check to see if the button "collides" with the specified point.
*
* This is currently a simple rectangle check, but could be modified
* to take image transparency into account.
*/
bool PhoneButton::CheckCollision(int x, int y) const
{
if (!mHasImage)
return false;
return (x >= mSelectedImage.GetX() &&
x < mSelectedImage.GetX() + mSelectedImage.GetWidth() &&
y >= mSelectedImage.GetY() &&
y < mSelectedImage.GetY() + mSelectedImage.GetHeight());
}
/*
* Look up a key code based on a string.
*
* Returns kKeyCodeUnknown if the label doesn't match anything.
*/
KeyCode PhoneButton::LookupKeyCode(const char* label) const
{
static const struct {
const char* label;
int keyCode;
} codeList[] = {
{ "soft-left", KEY_MENU },
{ "soft-right", KEY_KBDILLUMUP },
{ "home", KEY_HOME },
{ "back", KEY_BACK },
{ "call", KEY_F3 },
{ "phone-dial", KEY_F3 },
{ "end-call", KEY_F4 },
{ "phone-hangup", KEY_F4 },
{ "0", KEY_0 },
{ "1", KEY_1 },
{ "2", KEY_2 },
{ "3", KEY_3 },
{ "4", KEY_4 },
{ "5", KEY_5 },
{ "6", KEY_6 },
{ "7", KEY_7 },
{ "8", KEY_8 },
{ "9", KEY_9 },
{ "star", KEY_SWITCHVIDEOMODE },
{ "pound", KEY_KBDILLUMTOGGLE },
{ "dpad-up", KEY_UP },
{ "dpad-down", KEY_DOWN },
{ "dpad-left", KEY_LEFT },
{ "dpad-right", KEY_RIGHT },
{ "dpad-center", KEY_REPLY },
{ "volume-up", KEY_VOLUMEUP },
{ "volume-down", KEY_VOLUMEDOWN },
{ "power", KEY_POWER },
{ "camera", KEY_CAMERA },
//{ "clear", kKeyCodeClear },
};
const int numCodes = sizeof(codeList) / sizeof(codeList[0]);
for (int i = 0; i < numCodes; i++) {
if (strcmp(label, codeList[i].label) == 0)
return (KeyCode) codeList[i].keyCode;
}
return kKeyCodeUnknown;
};
|