1 |
|
---|
2 | // method.cpp, build
|
---|
3 | // g++ -o im_test method.cpp -lbe /boot/system/servers/input_server
|
---|
4 |
|
---|
5 |
|
---|
6 | #include <Application.h>
|
---|
7 | #include <Alert.h>
|
---|
8 | #include <Menu.h>
|
---|
9 | #include <MenuItem.h>
|
---|
10 | #include <Messenger.h>
|
---|
11 | #include <SupportDefs.h>
|
---|
12 |
|
---|
13 | #include <add-ons/input_server/InputServerMethod.h>
|
---|
14 |
|
---|
15 | // circle icon
|
---|
16 | const uchar kCircle[] = {
|
---|
17 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
---|
18 | 0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,
|
---|
19 | 0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,
|
---|
20 | 0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,
|
---|
21 | 0xff,0xff,0x00,0x00,0x00,0x0d,0x1a,0x1f,0x1f,0x1a,0x0d,0x00,0x00,0x00,0xff,0xff,
|
---|
22 | 0xff,0x00,0x00,0x00,0x0d,0x1e,0x1f,0x1f,0x1f,0x1f,0x1e,0x0d,0x00,0x00,0x00,0xff,
|
---|
23 | 0xff,0x00,0x00,0x00,0x1a,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1a,0x00,0x00,0x00,0xff,
|
---|
24 | 0xff,0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0x00,0xff,
|
---|
25 | 0xff,0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0x00,0xff,
|
---|
26 | 0xff,0x00,0x00,0x00,0x1a,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1a,0x00,0x00,0x00,0xff,
|
---|
27 | 0xff,0x00,0x00,0x00,0x0d,0x1e,0x1f,0x1f,0x1f,0x1f,0x1e,0x0d,0x00,0x00,0x00,0xff,
|
---|
28 | 0xff,0xff,0x00,0x00,0x00,0x0d,0x1a,0x1f,0x1f,0x1a,0x0d,0x00,0x00,0x00,0xff,0xff,
|
---|
29 | 0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,
|
---|
30 | 0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,
|
---|
31 | 0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,
|
---|
32 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
---|
33 | };
|
---|
34 |
|
---|
35 | // square icon
|
---|
36 | const uchar kSquare[] = {
|
---|
37 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
---|
38 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
---|
39 | 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
|
---|
40 | 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
|
---|
41 | 0xff,0xff,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0xff,0xff,
|
---|
42 | 0xff,0xff,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0xff,0xff,
|
---|
43 | 0xff,0xff,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0xff,0xff,
|
---|
44 | 0xff,0xff,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0xff,0xff,
|
---|
45 | 0xff,0xff,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0xff,0xff,
|
---|
46 | 0xff,0xff,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0xff,0xff,
|
---|
47 | 0xff,0xff,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0xff,0xff,
|
---|
48 | 0xff,0xff,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x00,0x00,0xff,0xff,
|
---|
49 | 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
|
---|
50 | 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
|
---|
51 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
---|
52 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
---|
53 | };
|
---|
54 |
|
---|
55 |
|
---|
56 | class TestMethod : public BInputServerMethod
|
---|
57 | {
|
---|
58 | public:
|
---|
59 | TestMethod();
|
---|
60 | virtual ~TestMethod();
|
---|
61 |
|
---|
62 | virtual status_t InitCheck();
|
---|
63 | virtual filter_result Filter(BMessage* msg, BList* _list);
|
---|
64 | virtual status_t MethodActivated(bool active);
|
---|
65 |
|
---|
66 | void UpdateMenu();
|
---|
67 | private:
|
---|
68 | BMessenger fLooper;
|
---|
69 | BMenu* fDeskbarMenu;
|
---|
70 | };
|
---|
71 |
|
---|
72 |
|
---|
73 | extern "C" _EXPORT
|
---|
74 | BInputServerMethod *instantiate_input_method()
|
---|
75 | {
|
---|
76 | return new TestMethod();
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | enum {
|
---|
81 | IM_ALERT1 = 'Ima1',
|
---|
82 | IM_ALERT2 = 'Ima2',
|
---|
83 | IM_SQUARE = 'Imsq',
|
---|
84 | IM_CIRCLE = 'Imci',
|
---|
85 | IM_METHOD_ACTIVATED = 'Imac',
|
---|
86 | IM_METHOD_DEACTIVATED = 'Imda',
|
---|
87 | };
|
---|
88 |
|
---|
89 |
|
---|
90 | class TestLooper : public BLooper
|
---|
91 | {
|
---|
92 | public:
|
---|
93 | TestLooper(TestMethod* method);
|
---|
94 | virtual ~TestLooper();
|
---|
95 |
|
---|
96 | virtual void MessageReceived(BMessage* msg);
|
---|
97 | private:
|
---|
98 | TestMethod* fMethod;
|
---|
99 | };
|
---|
100 |
|
---|
101 | TestLooper::TestLooper(TestMethod* method)
|
---|
102 | : BLooper("test_looper")
|
---|
103 | , fMethod(method)
|
---|
104 | {
|
---|
105 | if (be_app) {
|
---|
106 | if (be_app->Lock()) {
|
---|
107 | be_app->AddHandler(this);
|
---|
108 | be_app->Unlock();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | Run();
|
---|
113 | }
|
---|
114 |
|
---|
115 | TestLooper::~TestLooper()
|
---|
116 | {
|
---|
117 | }
|
---|
118 |
|
---|
119 | void TestLooper::MessageReceived(BMessage* msg)
|
---|
120 | {
|
---|
121 | switch(msg->what)
|
---|
122 | {
|
---|
123 | case IM_ALERT1:
|
---|
124 | {
|
---|
125 | BAlert* alert = new BAlert("title1", "Alert 1", "OK");
|
---|
126 | alert->Go();
|
---|
127 | // without updating the menu, any menuitem would not trigger any event
|
---|
128 | break;
|
---|
129 | }
|
---|
130 | case IM_ALERT2:
|
---|
131 | {
|
---|
132 | BAlert* alert = new BAlert("title2", "Alert 2 with set menu again", "OK");
|
---|
133 | alert->Go();
|
---|
134 | fMethod->UpdateMenu();
|
---|
135 | break;
|
---|
136 | }
|
---|
137 | case IM_SQUARE:
|
---|
138 | {
|
---|
139 | fMethod->SetIcon(kSquare);
|
---|
140 | fMethod->UpdateMenu();
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | case IM_CIRCLE:
|
---|
144 | {
|
---|
145 | fMethod->SetIcon(kCircle);
|
---|
146 | fMethod->UpdateMenu();
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | case IM_METHOD_ACTIVATED:
|
---|
150 | {
|
---|
151 | // try to set menu again when this IM is activated
|
---|
152 | fMethod->UpdateMenu();
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | case IM_METHOD_DEACTIVATED:
|
---|
156 | {
|
---|
157 |
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | default:
|
---|
161 | break;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | TestMethod::TestMethod()
|
---|
167 | : BInputServerMethod("im_test", kCircle)
|
---|
168 | {
|
---|
169 | fDeskbarMenu = new BMenu("im_test");
|
---|
170 | fDeskbarMenu->AddItem(new BMenuItem("Alert 1", new BMessage(IM_ALERT1)));
|
---|
171 | fDeskbarMenu->AddItem(new BMenuItem("Alert 2", new BMessage(IM_ALERT2)));
|
---|
172 | fDeskbarMenu->AddSeparatorItem();
|
---|
173 | fDeskbarMenu->AddItem(new BMenuItem("Square", new BMessage(IM_SQUARE)));
|
---|
174 | fDeskbarMenu->AddItem(new BMenuItem("Circle", new BMessage(IM_CIRCLE)));
|
---|
175 | }
|
---|
176 |
|
---|
177 | TestMethod::~TestMethod()
|
---|
178 | {
|
---|
179 | SetMenu(NULL, BMessenger());
|
---|
180 | BLooper* looper = NULL;
|
---|
181 | fLooper.Target(&looper);
|
---|
182 | if (looper != NULL) {
|
---|
183 | if (looper->Lock()) {
|
---|
184 | looper->Quit();
|
---|
185 | }
|
---|
186 | delete looper;
|
---|
187 | looper = NULL;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | void TestMethod::UpdateMenu()
|
---|
192 | {
|
---|
193 | SetMenu(fDeskbarMenu, fLooper);
|
---|
194 | }
|
---|
195 |
|
---|
196 | status_t TestMethod::InitCheck()
|
---|
197 | {
|
---|
198 | TestLooper* looper = new TestLooper(this);
|
---|
199 | fLooper = BMessenger(NULL, looper);
|
---|
200 | UpdateMenu();
|
---|
201 | return B_OK;
|
---|
202 | }
|
---|
203 |
|
---|
204 | filter_result TestMethod::Filter(BMessage* msg, BList* _list)
|
---|
205 | {
|
---|
206 | if (msg->what == B_KEY_DOWN) {
|
---|
207 | fLooper.SendMessage(msg);
|
---|
208 | return B_SKIP_MESSAGE;
|
---|
209 | }
|
---|
210 | return B_DISPATCH_MESSAGE;
|
---|
211 | }
|
---|
212 |
|
---|
213 | status_t TestMethod::MethodActivated(bool active)
|
---|
214 | {
|
---|
215 | BMessage msg(active ? IM_METHOD_ACTIVATED : IM_METHOD_DEACTIVATED);
|
---|
216 | fLooper.SendMessage(&msg);
|
---|
217 | return BInputServerMethod::MethodActivated(active);
|
---|
218 | }
|
---|