1 | |
---|
2 | |
---|
3 | #include <fstream> |
---|
4 | #include <iostream> |
---|
5 | #include <map> |
---|
6 | #include <gtkmm.h> |
---|
7 | #include <glibmm.h> |
---|
8 | #include <dlfcn.h> |
---|
9 | #include <libintl.h> |
---|
10 | #include <locale.h> |
---|
11 | |
---|
12 | #include <mrpdi/Core.h> |
---|
13 | #include <mrpdi/Input.h> |
---|
14 | |
---|
15 | #include <libconfig.h++> |
---|
16 | #include <libappindicator/app-indicator.h> |
---|
17 | |
---|
18 | |
---|
19 | #define T(String) gettext(String) |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | using namespace net::lliurex::mrpdi; |
---|
25 | |
---|
26 | /** |
---|
27 | * Application class |
---|
28 | */ |
---|
29 | class Application |
---|
30 | { |
---|
31 | public: |
---|
32 | |
---|
33 | Gtk::Window * window; |
---|
34 | Gtk::Window * dlgClose; |
---|
35 | |
---|
36 | Gtk::TreeView * treeview; |
---|
37 | Core * core; |
---|
38 | input::InputHandler * handler; |
---|
39 | Glib::RefPtr <Gdk::Pixbuf> pix_tablet,pix_whiteboard; |
---|
40 | Glib::RefPtr<Gtk::ListStore> store; |
---|
41 | Glib::RefPtr<Gtk::StatusIcon> statusIcon; |
---|
42 | |
---|
43 | AppIndicator *indicator; |
---|
44 | |
---|
45 | |
---|
46 | map<unsigned int,input::DeviceSettingsEntry> settings_map; |
---|
47 | bool corrupted_settings; |
---|
48 | |
---|
49 | |
---|
50 | /** |
---|
51 | * Constructor |
---|
52 | */ |
---|
53 | Application() |
---|
54 | { |
---|
55 | cout<<"* Starting MrPDI gui"<<endl; |
---|
56 | |
---|
57 | load_setup(); |
---|
58 | |
---|
59 | Glib::RefPtr<Gtk::Builder> glade; |
---|
60 | glade=Gtk::Builder::create_from_file("/usr/share/mrpdi/gui/interface.glade"); |
---|
61 | glade->get_widget("winManager",window); |
---|
62 | |
---|
63 | Gtk::Button * button; |
---|
64 | glade->get_widget("btnRefresh",button); |
---|
65 | button->signal_clicked().connect(sigc::mem_fun(*this,&Application::on_btnrefresh_clicked)); |
---|
66 | |
---|
67 | glade->get_widget("btnRun",button); |
---|
68 | button->signal_clicked().connect(sigc::mem_fun(*this,&Application::on_btnrun_clicked)); |
---|
69 | |
---|
70 | glade->get_widget("btnStop",button); |
---|
71 | button->signal_clicked().connect(sigc::mem_fun(*this,&Application::on_btnstop_clicked)); |
---|
72 | |
---|
73 | glade->get_widget("btnDebug",button); |
---|
74 | button->signal_clicked().connect(sigc::mem_fun(*this,&Application::on_btndebug_clicked)); |
---|
75 | |
---|
76 | glade->get_widget("btnCalibrate",button); |
---|
77 | button->signal_clicked().connect(sigc::mem_fun(*this,&Application::on_btncalibrate_clicked)); |
---|
78 | |
---|
79 | glade->get_widget("btnClose",button); |
---|
80 | button->signal_clicked().connect(sigc::mem_fun(*this,&Application::on_btnclose_clicked)); |
---|
81 | |
---|
82 | glade->get_widget("dlgClose",dlgClose); |
---|
83 | glade->get_widget("dlgbtnCancel",button); |
---|
84 | button->signal_clicked().connect(sigc::mem_fun(*this,&Application::on_dlg_cancel)); |
---|
85 | |
---|
86 | glade->get_widget("dlgbtnAccept",button); |
---|
87 | button->signal_clicked().connect(sigc::mem_fun(*this,&Application::on_dlg_accept)); |
---|
88 | |
---|
89 | window->signal_window_state_event().connect(sigc::mem_fun(*this,&Application::on_window_state_changed)); |
---|
90 | window->signal_delete_event().connect(sigc::mem_fun(*this,&Application::on_window_destroy)); |
---|
91 | |
---|
92 | glade->get_widget("treeDevices",treeview); |
---|
93 | |
---|
94 | |
---|
95 | pix_tablet = Gdk::Pixbuf::create_from_file("/usr/share/mrpdi/gui/tablet.svg"); |
---|
96 | pix_whiteboard = Gdk::Pixbuf::create_from_file("/usr/share/mrpdi/gui/whiteboard.svg"); |
---|
97 | |
---|
98 | Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> >pixbuf_col; |
---|
99 | Gtk::TreeModelColumn<unsigned int> id_col; |
---|
100 | Gtk::TreeModelColumn<unsigned int> address_col; |
---|
101 | Gtk::TreeModelColumn<string> name_col; |
---|
102 | Gtk::TreeModelColumn<string> status_col; |
---|
103 | |
---|
104 | Gtk::TreeModel::ColumnRecord cr; |
---|
105 | cr.add(pixbuf_col); |
---|
106 | cr.add(address_col); |
---|
107 | cr.add(id_col); |
---|
108 | cr.add(name_col); |
---|
109 | cr.add(status_col); |
---|
110 | |
---|
111 | |
---|
112 | store=Gtk::ListStore::create(cr); |
---|
113 | |
---|
114 | treeview->set_model(store); |
---|
115 | treeview->append_column(T("Type"),pixbuf_col); |
---|
116 | //treeview->append_column_numeric(T("Id"),id_col,"%08x"); |
---|
117 | //treeview->append_column_numeric(T("Address"),address_col,"%08x"); |
---|
118 | treeview->append_column(T("Name"),name_col); |
---|
119 | treeview->append_column(T("Status"),status_col); |
---|
120 | |
---|
121 | |
---|
122 | window->show_all(); |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | indicator = app_indicator_new ("mrpdi","input-tablet", APP_INDICATOR_CATEGORY_APPLICATION_STATUS); |
---|
127 | app_indicator_set_status (indicator, APP_INDICATOR_STATUS_ACTIVE); |
---|
128 | app_indicator_set_attention_icon (indicator, "input-tablet"); |
---|
129 | |
---|
130 | |
---|
131 | Gtk::Menu * menu = new Gtk::Menu(); |
---|
132 | Gtk::MenuItem * itm_show=new Gtk::MenuItem(T("Show")); |
---|
133 | |
---|
134 | |
---|
135 | menu->append(*itm_show); |
---|
136 | |
---|
137 | itm_show->signal_activate().connect(sigc::mem_fun(*this,&Application::on_indicator_show_click)); |
---|
138 | |
---|
139 | menu->show_all(); |
---|
140 | |
---|
141 | app_indicator_set_menu(indicator,menu->gobj()); |
---|
142 | |
---|
143 | core = new Core(); |
---|
144 | handler = new input::InputHandler(); |
---|
145 | core->init(); |
---|
146 | |
---|
147 | core->set_input_handler(handler); |
---|
148 | handler->set_settings(settings_map); |
---|
149 | |
---|
150 | refresh(); |
---|
151 | |
---|
152 | |
---|
153 | Glib::signal_timeout().connect(sigc::mem_fun(*this,&Application::on_timer), 1000); |
---|
154 | |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | /** |
---|
159 | * Destructor |
---|
160 | * things should be closed here |
---|
161 | */ |
---|
162 | ~Application() |
---|
163 | { |
---|
164 | cout<<"* Shutting down MrPDI gui"<<endl; |
---|
165 | core->shutdown(); |
---|
166 | |
---|
167 | settings_map = handler->get_settings(); |
---|
168 | if(!corrupted_settings) |
---|
169 | save_setup(); |
---|
170 | |
---|
171 | delete handler; |
---|
172 | delete core; |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | void on_indicator_show_click() |
---|
177 | { |
---|
178 | window->deiconify(); |
---|
179 | window->show_all(); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * loads calibration setup |
---|
184 | * */ |
---|
185 | void load_setup() |
---|
186 | { |
---|
187 | struct passwd * pwd; |
---|
188 | |
---|
189 | cout<<"* Loading settings"<<endl; |
---|
190 | |
---|
191 | string path("/etc/mrpdi/settings.conf"); |
---|
192 | |
---|
193 | libconfig::Config cfg; |
---|
194 | |
---|
195 | |
---|
196 | ifstream myfile(path.c_str(),std::ifstream::in); |
---|
197 | |
---|
198 | if(myfile.good()) |
---|
199 | { |
---|
200 | myfile.close(); |
---|
201 | |
---|
202 | try |
---|
203 | { |
---|
204 | cfg.readFile(path.c_str()); |
---|
205 | |
---|
206 | libconfig::Setting & version = cfg.lookup("version"); |
---|
207 | std::string version_str=version; |
---|
208 | |
---|
209 | if(version_str!="2.0") |
---|
210 | { |
---|
211 | cout<<"Warning: Unknown format version: "<<version_str<<endl; |
---|
212 | } |
---|
213 | |
---|
214 | libconfig::Setting & setting = cfg.lookup("mrpdi.devices"); |
---|
215 | |
---|
216 | for(int n=0;n<setting.getLength();n++) |
---|
217 | { |
---|
218 | unsigned int id; |
---|
219 | |
---|
220 | string name; |
---|
221 | setting[n].lookupValue("name",name); |
---|
222 | |
---|
223 | id=setting[n]["id"]; |
---|
224 | |
---|
225 | settings_map[id].id=id; //and fuck that redundant shit |
---|
226 | settings_map[id].name=name; |
---|
227 | |
---|
228 | for(int m=0;m<8;m++) |
---|
229 | { |
---|
230 | settings_map[id].calibration[m]=setting[n]["calibration"][m]; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | cout<<"name:"<<name<<endl; |
---|
235 | cout<<"params: "<<setting[n]["params"].getLength()<<endl; |
---|
236 | for(int m=0;m<setting[n]["params"].getLength();m++) |
---|
237 | { |
---|
238 | string name; |
---|
239 | unsigned int value; |
---|
240 | setting[n]["params"][m].lookupValue("name",name); |
---|
241 | setting[n]["params"][m].lookupValue("value",value); |
---|
242 | cout<<"* "<<name<<":"<<value<<endl; |
---|
243 | settings_map[id].params[name]=value; |
---|
244 | } |
---|
245 | |
---|
246 | }//for |
---|
247 | |
---|
248 | |
---|
249 | |
---|
250 | }//try |
---|
251 | catch(libconfig::ParseException &e) |
---|
252 | { |
---|
253 | cerr<<"* Error parsing config file"<<endl; |
---|
254 | settings_map.clear(); |
---|
255 | corrupted_settings=true; |
---|
256 | } |
---|
257 | }//if |
---|
258 | |
---|
259 | |
---|
260 | |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * saves |
---|
265 | **/ |
---|
266 | void save_setup() |
---|
267 | { |
---|
268 | cout<<"* Saving settings"<<endl; |
---|
269 | |
---|
270 | string path("/etc/mrpdi/settings.conf"); |
---|
271 | |
---|
272 | libconfig::Config cfg; |
---|
273 | |
---|
274 | libconfig::Setting & root = cfg.getRoot(); |
---|
275 | libconfig::Setting & version = root.add("version",libconfig::Setting::TypeString); |
---|
276 | version="2.0"; |
---|
277 | |
---|
278 | libconfig::Setting & mrpdi = root.add("mrpdi",libconfig::Setting::TypeGroup); |
---|
279 | libconfig::Setting & devices = mrpdi.add("devices",libconfig::Setting::TypeList); |
---|
280 | |
---|
281 | std::map<unsigned int,input::DeviceSettingsEntry>::iterator it; |
---|
282 | |
---|
283 | for(it=settings_map.begin();it!=settings_map.end();it++) |
---|
284 | { |
---|
285 | libconfig::Setting & device = devices.add(libconfig::Setting::TypeGroup); |
---|
286 | //id |
---|
287 | libconfig::Setting & id = device.add("id",libconfig::Setting::TypeInt); |
---|
288 | id.setFormat(libconfig::Setting::FormatHex); |
---|
289 | id=(int)it->first; |
---|
290 | //name |
---|
291 | libconfig::Setting & name = device.add("name",libconfig::Setting::TypeString); |
---|
292 | name=it->second.name; |
---|
293 | //calibration |
---|
294 | libconfig::Setting & calibration = device.add("calibration",libconfig::Setting::TypeArray); |
---|
295 | for(int n=0;n<8;n++) |
---|
296 | { |
---|
297 | libconfig::Setting & f = calibration.add(libconfig::Setting::TypeFloat); |
---|
298 | f=it->second.calibration[n]; |
---|
299 | } |
---|
300 | |
---|
301 | //params |
---|
302 | libconfig::Setting & params = device.add("params",libconfig::Setting::TypeList); |
---|
303 | std::map<string,unsigned int>::iterator pit; |
---|
304 | |
---|
305 | for(pit=it->second.params.begin();pit!=it->second.params.end();pit++) |
---|
306 | { |
---|
307 | cout<<"* name:"<<pit->first<<" value:"<<pit->second<<endl; |
---|
308 | libconfig::Setting & param = params.add(libconfig::Setting::TypeGroup); |
---|
309 | libconfig::Setting & pname = param.add("name",libconfig::Setting::TypeString); |
---|
310 | pname=pit->first; |
---|
311 | libconfig::Setting & pvalue = param.add("value",libconfig::Setting::TypeInt); |
---|
312 | pvalue=(int)pit->second; |
---|
313 | } |
---|
314 | |
---|
315 | } |
---|
316 | |
---|
317 | |
---|
318 | cfg.writeFile(path.c_str()); |
---|
319 | |
---|
320 | |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | /** |
---|
325 | * Refresh device table |
---|
326 | */ |
---|
327 | void refresh() |
---|
328 | { |
---|
329 | vector<Gtk::TreeModel::Path> selection; |
---|
330 | vector<connected_device_info> list; |
---|
331 | |
---|
332 | core->update_devices(&list); |
---|
333 | |
---|
334 | selection=treeview->get_selection()->get_selected_rows(); |
---|
335 | store->clear(); |
---|
336 | |
---|
337 | for(int n=0;n<list.size();n++) |
---|
338 | { |
---|
339 | |
---|
340 | Gtk::TreeModel::Row row= *(store->append()); |
---|
341 | if(list[n].type==0)row.set_value<Glib::RefPtr<Gdk::Pixbuf> >(0,pix_tablet); |
---|
342 | else row.set_value<Glib::RefPtr<Gdk::Pixbuf> >(0,pix_whiteboard); |
---|
343 | |
---|
344 | row.set_value<unsigned int>(1,list[n].address); |
---|
345 | row.set_value<unsigned int>(2,list[n].id); |
---|
346 | row.set_value<string>(3,list[n].name); |
---|
347 | |
---|
348 | if(list[n].status==1)row.set_value<string>(4,T("Running")); |
---|
349 | else row.set_value<string>(4,T("Stop")); |
---|
350 | |
---|
351 | vector<string> parameters; |
---|
352 | core->get_parameter_list(list[n].id,¶meters); |
---|
353 | |
---|
354 | } |
---|
355 | |
---|
356 | if(selection.size()>0) |
---|
357 | treeview->get_selection()->select(selection[0]); |
---|
358 | } |
---|
359 | |
---|
360 | /** |
---|
361 | * Window state hook |
---|
362 | */ |
---|
363 | bool on_window_state_changed(GdkEventWindowState* event) |
---|
364 | { |
---|
365 | cout<<"* State:"<<event->new_window_state<<endl; |
---|
366 | |
---|
367 | if( (event->new_window_state & GDK_WINDOW_STATE_ICONIFIED) == GDK_WINDOW_STATE_ICONIFIED) |
---|
368 | { |
---|
369 | cout<<"* Minimize!"<<endl; |
---|
370 | window->hide(); |
---|
371 | } |
---|
372 | return false; |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | /** |
---|
378 | * Timer handler |
---|
379 | */ |
---|
380 | int on_timer() |
---|
381 | { |
---|
382 | //cout<<"tic tac!!!"<<endl; |
---|
383 | refresh(); |
---|
384 | } |
---|
385 | |
---|
386 | /** |
---|
387 | * Dialog cancel |
---|
388 | */ |
---|
389 | void on_dlg_cancel() |
---|
390 | { |
---|
391 | dlgClose->hide(); |
---|
392 | } |
---|
393 | |
---|
394 | /** |
---|
395 | * Dialog accept |
---|
396 | */ |
---|
397 | void on_dlg_accept() |
---|
398 | { |
---|
399 | core->shutdown(); |
---|
400 | Gtk::Main::quit(); |
---|
401 | } |
---|
402 | |
---|
403 | |
---|
404 | /** |
---|
405 | * Close button |
---|
406 | */ |
---|
407 | void on_btnclose_clicked() |
---|
408 | { |
---|
409 | dlgClose->show_all(); |
---|
410 | } |
---|
411 | |
---|
412 | /** |
---|
413 | * Calibrate |
---|
414 | */ |
---|
415 | void on_btncalibrate_clicked() |
---|
416 | { |
---|
417 | Glib::RefPtr<Gtk::TreeSelection> selection; |
---|
418 | Gtk::TreeModel::iterator iter; |
---|
419 | unsigned int id,address,value; |
---|
420 | |
---|
421 | cout<<"* Calibrate"<<endl; |
---|
422 | selection = treeview->get_selection(); |
---|
423 | iter=selection->get_selected(); |
---|
424 | if(iter) |
---|
425 | { |
---|
426 | Gtk::TreeModel::Row row = *iter; |
---|
427 | row.get_value(2,id); |
---|
428 | row.get_value(1,address); |
---|
429 | cout<<"* Debugging "<<hex<<address<<endl; |
---|
430 | |
---|
431 | handler->calibrate(address); |
---|
432 | |
---|
433 | |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | /** |
---|
438 | * Debug mode |
---|
439 | */ |
---|
440 | void on_btndebug_clicked() |
---|
441 | { |
---|
442 | Glib::RefPtr<Gtk::TreeSelection> selection; |
---|
443 | Gtk::TreeModel::iterator iter; |
---|
444 | unsigned int id,address,value; |
---|
445 | |
---|
446 | cout<<"* Debug"<<endl; |
---|
447 | selection = treeview->get_selection(); |
---|
448 | iter=selection->get_selected(); |
---|
449 | if(iter) |
---|
450 | { |
---|
451 | Gtk::TreeModel::Row row = *iter; |
---|
452 | row.get_value(2,id); |
---|
453 | row.get_value(1,address); |
---|
454 | cout<<"* Debugging "<<hex<<address<<endl; |
---|
455 | |
---|
456 | core->get_parameter(id,"common.debug",&value); |
---|
457 | core->set_parameter(id,"common.debug",(~value) & 1); |
---|
458 | |
---|
459 | } |
---|
460 | |
---|
461 | } |
---|
462 | |
---|
463 | /** |
---|
464 | * Refresh button click signal |
---|
465 | */ |
---|
466 | void on_btnrefresh_clicked() |
---|
467 | { |
---|
468 | cout<<"* Refresh"<<endl; |
---|
469 | refresh(); |
---|
470 | } |
---|
471 | /** |
---|
472 | * Run button clicked |
---|
473 | */ |
---|
474 | void on_btnrun_clicked() |
---|
475 | { |
---|
476 | Glib::RefPtr<Gtk::TreeSelection> selection; |
---|
477 | Gtk::TreeModel::iterator iter; |
---|
478 | unsigned int id,address; |
---|
479 | |
---|
480 | cout<<"* Run"<<endl; |
---|
481 | |
---|
482 | selection = treeview->get_selection(); |
---|
483 | iter=selection->get_selected(); |
---|
484 | if(iter) |
---|
485 | { |
---|
486 | Gtk::TreeModel::Row row = *iter; |
---|
487 | row.get_value(2,id); |
---|
488 | row.get_value(1,address); |
---|
489 | cout<<"* Turning on "<<hex<<address<<endl; |
---|
490 | core->start(id,address); |
---|
491 | |
---|
492 | } |
---|
493 | |
---|
494 | |
---|
495 | |
---|
496 | } |
---|
497 | /** |
---|
498 | * Stop button clicked |
---|
499 | */ |
---|
500 | void on_btnstop_clicked() |
---|
501 | { |
---|
502 | |
---|
503 | Glib::RefPtr<Gtk::TreeSelection> selection; |
---|
504 | Gtk::TreeModel::iterator iter; |
---|
505 | unsigned int id,address; |
---|
506 | |
---|
507 | cout<<"* Stop"<<endl; |
---|
508 | |
---|
509 | selection = treeview->get_selection(); |
---|
510 | iter=selection->get_selected(); |
---|
511 | if(iter) |
---|
512 | { |
---|
513 | Gtk::TreeModel::Row row = *iter; |
---|
514 | row.get_value(2,id); |
---|
515 | row.get_value(1,address); |
---|
516 | cout<<"* Turning off "<<hex<<address<<endl; |
---|
517 | core->stop(id,address); |
---|
518 | |
---|
519 | } |
---|
520 | } |
---|
521 | |
---|
522 | /** |
---|
523 | * Destroy window event |
---|
524 | */ |
---|
525 | bool on_window_destroy(GdkEventAny* event) |
---|
526 | { |
---|
527 | |
---|
528 | dlgClose->show_all(); |
---|
529 | return true; |
---|
530 | } |
---|
531 | |
---|
532 | |
---|
533 | }; |
---|
534 | |
---|
535 | |
---|
536 | /** |
---|
537 | * Main |
---|
538 | */ |
---|
539 | int main(int argc,char * argv[]) |
---|
540 | { |
---|
541 | textdomain("mrpdi-gui"); |
---|
542 | Gtk::Main kit(argc, argv); |
---|
543 | Application app; |
---|
544 | Gtk::Main::run(); |
---|
545 | |
---|
546 | return 0; |
---|
547 | } |
---|