Fix:Core:Renamed src to navit for cleanup of includes
[navit-package] / navit / gui / sdl / sdl_events.cpp
1 #include "CEGUI.h"
2 #include "sdl_events.h"
3 #include "gui_sdl.h"
4
5 #include <CEGUI/RendererModules/OpenGLGUIRenderer/openglrenderer.h>
6
7 //  FIXME temporary fix for enum
8 #include "projection.h"
9 #include "item.h"
10 #include "navit.h"
11 #include "debug.h"
12 #include "track.h"
13 #include "search.h"
14 #include "coord.h"
15 #include "country.h"
16 #include "string.h"
17
18 // Library for window switching (-> nGhost)
19 #include "wmcontrol.h"
20
21
22 struct sdl_destination{
23         int country;
24         int town;
25         int town_street_assoc;
26         int current_search;
27 } SDL_dest;
28
29
30 static struct search_param {     
31                  struct navit *nav;      
32                  struct mapset *ms;      
33                  struct search_list *sl;         
34                  struct attr attr;       
35          } search_param;
36
37
38 void route_to(int x,int y){
39         struct pcoord pos;
40         pos.x=x;
41         pos.y=y; 
42         /* FIXME: Get projection from list like x,y or use pcoord from search directly */
43         pos.pro = projection_mg;
44         using namespace CEGUI;
45         extern struct navit *sdl_gui_navit;
46
47         try {
48                 WindowManager::getSingleton().getWindow("AdressSearchWindow")->hide();
49                 WindowManager::getSingleton().getWindow("Navit/Routing/Tips")->show();
50
51 //              WindowManager::getSingleton().getWindow("Navit/ProgressWindow")->show();
52         //      route_set_destination(co->route, &pos);
53                 // I could have been using search->nav instead of sdl_gui_navit. is it better this way?
54         
55 //              WindowManager::getSingleton().getWindow("Navit/ProgressWindow")->hide();
56 //              WindowManager::getSingleton().getWindow("OSD/RoadbookButton")->show();
57 //              WindowManager::getSingleton().getWindow("OSD/ETA")->show();
58         }
59         catch (CEGUI::Exception& e)
60         {
61                 fprintf(stderr,"CEGUI Exception occured: \n%s\n", e.getMessage().c_str());
62                 printf("Missing control!...\n");
63         }
64                 navit_set_destination(sdl_gui_navit, &pos, "FIXME");
65
66 }
67
68 bool Handle_Virtual_Key_Down(const CEGUI::EventArgs& event){
69         
70         using namespace CEGUI;
71
72         const WindowEventArgs& we =  static_cast<const CEGUI::WindowEventArgs&>(event);
73         String senderID = we.window->getName();
74
75         Window* editbox = WindowManager::getSingleton().getWindow("Navit/Keyboard/Input");
76         String content=editbox->getText();
77
78
79         if(senderID=="OK"){
80                 WindowManager::getSingleton().getWindow("Navit/Keyboard")->hide();
81                 return 0;
82         } else if(senderID=="BACK"){
83                 content=content.substr(0, content.length()-1);
84                 editbox->setText(content);
85         } else {
86                 content+=senderID;
87                 editbox->setText(content);
88         }
89
90         Window* country_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/CountryEditbox"));
91         Window* town_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
92         Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
93
94         switch (SDL_dest.current_search) {
95                 case SRCH_COUNTRY:
96                         country_edit->setText(content);
97                         break;
98                 case SRCH_TOWN :
99                         town_edit->setText(content);
100                         break;
101                 case SRCH_STREET :
102                         street_edit->setText(content);
103                         break;
104         }
105         handle_destination_change();
106 }
107
108 bool handleItemSelect(int r)
109 {
110         using namespace CEGUI;
111
112         MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
113         
114         ListboxItem * item = mcl->getItemAtGridReference(MCLGridRef(r,0));
115         ListboxItem * itemid = mcl->getItemAtGridReference(MCLGridRef(r,1));
116         ListboxItem * item_assoc = mcl->getItemAtGridReference(MCLGridRef(r,2));
117
118
119         Window* country_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/CountryEditbox"));
120         Window* twn_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
121         Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
122
123         if(SDL_dest.current_search==SRCH_COUNTRY){
124                 country_edit->setText(item->getText());
125                 twn_edit->activate();
126                 SDL_dest.current_search=SRCH_TOWN;
127                 WindowManager::getSingleton().getWindow("Navit/Keyboard/Input")->setText("");
128
129         } else  if(SDL_dest.current_search==SRCH_TOWN){
130                 twn_edit->setText(item->getText());
131
132                 ListboxItem * itemx = mcl->getItemAtGridReference(MCLGridRef(r,3));
133                 ListboxItem * itemy = mcl->getItemAtGridReference(MCLGridRef(r,4));
134         
135                 Window* Dest_x = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_x"));
136                 Dest_x->setText(itemx->getText().c_str());
137
138                 Window* Dest_y = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_y"));
139                 Dest_y->setText(itemy->getText().c_str());
140
141                 mcl->resetList();
142
143                 SDL_dest.current_search=SRCH_STREET;
144                 street_edit->activate();
145                 WindowManager::getSingleton().getWindow("Navit/Keyboard/Input")->setText("");
146
147         } else if(SDL_dest.current_search==SRCH_STREET){
148                 street_edit->setText(item->getText());
149
150                 WindowManager::getSingleton().getWindow("Navit/Keyboard")->hide();
151
152                 ListboxItem * itemx = mcl->getItemAtGridReference(MCLGridRef(r,3));
153                 ListboxItem * itemy = mcl->getItemAtGridReference(MCLGridRef(r,4));
154         
155                 Window* Dest_x = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_x"));
156                 Dest_x->setText(itemx->getText().c_str());
157
158                 Window* Dest_y = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_y"));
159                 Dest_y->setText(itemy->getText().c_str());
160
161                 mcl->resetList();
162
163                 SDL_dest.current_search=SRCH_STREET;
164
165                 WindowManager::getSingleton().getWindow("Navit/Keyboard/Input")->setText("");
166
167
168         } else if (SDL_dest.current_search==SRCH_NUMBER){
169
170                 struct coord pos;
171                 ListboxItem * itemx = mcl->getItemAtGridReference(MCLGridRef(r,3));
172                 ListboxItem * itemy = mcl->getItemAtGridReference(MCLGridRef(r,4));
173         
174                 pos.x=atoi(itemx->getText().c_str());
175                 pos.y=atoi(itemy->getText().c_str());
176
177                 route_to(pos.x,pos.y);
178         }
179
180         return true;
181 }
182
183 bool route_clear(const CEGUI::EventArgs& event)
184 {
185 //      navit_set_destination(gui->nav, NULL, NULL);
186 }
187
188 bool ItemSelect(const CEGUI::EventArgs& event)
189 {
190         //FIXME maybe merge with handleItemSelect
191         using namespace CEGUI;
192
193         MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
194         ListboxItem * item = mcl->getFirstSelectedItem();
195         handleItemSelect(mcl->getItemRowIndex(item));
196 }
197
198 bool BookmarkSelect(const CEGUI::EventArgs& event)
199 {
200         using namespace CEGUI;
201         dbg(0,"1\n");
202         MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("Bookmarks/Listbox"));
203         dbg(0,"2\n");
204         ListboxItem * item = mcl->getFirstSelectedItem();
205         if(item){
206                 dbg(0,"item %s is at row %i\n",item->getText().c_str(),mcl->getItemRowIndex(item));
207                 BookmarkGo(item->getText().c_str());
208         }
209         WindowManager::getSingleton().getWindow("BookmarkSelection")->hide();
210 }
211
212 bool FormerDestSelect(const CEGUI::EventArgs& event)
213 {
214         using namespace CEGUI;
215         dbg(0,"1\n");
216         MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("FormerDests/Listbox"));
217         dbg(0,"2\n");
218         ListboxItem * item = mcl->getFirstSelectedItem();
219         if(item){
220                 dbg(0,"item %s is at row %i\n",item->getText().c_str(),mcl->getItemRowIndex(item));
221                 FormerDestGo(item->getText().c_str());
222         }
223         WindowManager::getSingleton().getWindow("FormerDestSelection")->hide();
224         dbg(0,"hided\n");
225 }
226
227 bool handleMouseEnters(const CEGUI::EventArgs& event)
228 {
229         // FIXME this whole function could maybe be removed
230         const CEGUI::WindowEventArgs& we =  static_cast<const CEGUI::WindowEventArgs&>(event);
231
232         // FIXME theses variables should be shared
233         extern CEGUI::OpenGLRenderer* renderer;
234
235         using namespace CEGUI;
236         WindowManager::getSingleton().getWindow("Navit/Keyboard/Input")->setText("");
237         MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
238
239         String senderID = we.window->getName();
240
241         if (senderID == "AdressSearch/CountryEditbox"){
242                 // First, clean off the Street and Town Editbox
243                 Window* town_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
244                 town_edit->setText("");
245                 Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
246                 street_edit->setText("");
247                 SDL_dest.current_search=SRCH_COUNTRY;
248
249         } else if (senderID == "AdressSearch/TownEditbox"){
250                 // First, clean off the Street Editbox
251                 Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
252                 street_edit->setText("");
253                 SDL_dest.current_search=SRCH_TOWN;
254
255         } else if (senderID == "AdressSearch/StreetEditbox"){
256                 // First, make sure the user selected an entry in the town choice. If he hadn't, select the first for him.
257                 if(SDL_dest.current_search==SRCH_TOWN){
258                         if (mcl->getRowCount()>0)
259                         {
260                                 handleItemSelect(0);
261                         }                       
262                 }
263                 SDL_dest.current_search=SRCH_STREET;
264
265         }
266 }
267
268
269 void handle_destination_change(){
270
271         using namespace CEGUI;
272
273         struct search_param *search=&search_param;
274         struct search_list_result *res;
275
276         MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
277
278
279         if (SDL_dest.current_search==SRCH_COUNTRY)
280         {       
281                 Editbox* country_edit = static_cast<Editbox*>(WindowManager::getSingleton().getWindow("AdressSearch/CountryEditbox"));
282                 String content=country_edit->getText();
283
284                 mcl->resetList();
285                 dbg(0,"Starting a country search : %s\n",content.c_str());
286
287                 search->attr.type=attr_country_all;
288
289                 // FIXME the following codeblock could be shared between country, town and street search
290                 search->attr.u.str=(char *)content.c_str();
291
292                 search_list_search(search->sl, &search->attr, 1);
293                 while((res=search_list_get_result(search->sl))) {
294                         ListboxTextItem* itemListbox = new ListboxTextItem((CEGUI::utf8*)(res->country->name));
295                         itemListbox->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
296
297                         mcl->addRow(itemListbox,0);
298                 }
299
300         } else if (SDL_dest.current_search==SRCH_TOWN)
301         {       
302                 
303                 Editbox* town_edit = static_cast<Editbox*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
304                 String content=town_edit->getText();
305
306
307                 mcl->resetList();
308
309                 if(strlen(content.c_str())<4){
310
311                 }  else {
312                         dbg(0,"town searching for %s\n",content.c_str());
313                         search->attr.type=attr_town_name;
314                         search->attr.u.str=(char *)content.c_str();
315
316                         search_list_search(search->sl, &search->attr, 1);
317                         while((res=search_list_get_result(search->sl))) {
318                                 ListboxTextItem* itemListbox = new ListboxTextItem((CEGUI::utf8*)(res->town->name));
319                                 itemListbox->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
320
321                                 mcl->addRow(itemListbox,0);
322
323                                 char x [256];
324                                 sprintf(x,"%li",res->c->x);
325                                 ListboxTextItem* xitem = new ListboxTextItem(x);
326                         
327                                 char y [256];
328                                 sprintf(y,"%li",res->c->y);
329                         
330                                 ListboxTextItem* yitem = new ListboxTextItem(y);
331                         
332                                 try
333                                 {
334                                         mcl->setItem(xitem, 3, mcl->getRowCount()-1);
335                                         mcl->setItem(yitem, 4, mcl->getRowCount()-1);
336                                 }
337                                 // something went wrong, so cleanup the ListboxTextItem.
338                                 catch (InvalidRequestException)
339                                 {
340 //                                      delete item;
341                                 }
342         
343                         }
344
345                 }
346
347
348         } else if (SDL_dest.current_search==SRCH_STREET)
349         {       
350                 Editbox* street_edit = static_cast<Editbox*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
351                 
352                 String content=street_edit->getText();
353                 if(strlen(content.c_str())<1){
354
355                 }  else {
356                         // dbg(1,"street searching for %s\n",content.c_str());
357                         search->attr.type=attr_street_name;
358                         search->attr.u.str=(char *)content.c_str();
359
360                         mcl->resetList();
361
362                         search_list_search(search->sl, &search->attr, 1);
363                         while((res=search_list_get_result(search->sl))) {
364                                 ListboxTextItem* itemListbox = new ListboxTextItem((CEGUI::utf8*)(res->street->name));
365                                 itemListbox->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
366
367                                 mcl->addRow(itemListbox,0);
368
369                                 char x [256];
370                                 sprintf(x,"%li",res->c->x);
371                                 ListboxTextItem* xitem = new ListboxTextItem(x);
372                         
373                                 char y [256];
374                                 sprintf(y,"%li",res->c->y);
375                         
376                                 ListboxTextItem* yitem = new ListboxTextItem(y);
377                         
378                                 try
379                                 {
380                                         mcl->setItem(xitem, 3, mcl->getRowCount()-1);
381                                         mcl->setItem(yitem, 4, mcl->getRowCount()-1);
382                                 }
383                                 // something went wrong, so cleanup the ListboxTextItem.
384                                 catch (InvalidRequestException)
385                                 {
386 //                                      delete item;
387                                 }
388
389         
390                         }
391                         }
392         }
393
394 }
395
396
397 bool DestinationEntryChange(const CEGUI::EventArgs& event)
398 {
399         handleMouseEnters(event);
400         handle_destination_change();
401
402         return true;
403 }
404
405 bool DestinationWindowSwitch(const CEGUI::EventArgs& event)
406 {
407         using namespace CEGUI;
408
409         WindowManager::getSingleton().getWindow("AdressSearchWindow")->hide();
410         WindowManager::getSingleton().getWindow("BookmarkSelection")->hide();
411         WindowManager::getSingleton().getWindow("FormerDestSelection")->hide();
412
413         if(WindowManager::getSingleton().getWindow("DestinationChoose")->isVisible()){
414                 WindowManager::getSingleton().getWindow("DestinationChoose")->hide();
415         } else {
416                 WindowManager::getSingleton().getWindow("DestinationChoose")->show();
417         }
418 }
419
420 bool BookmarkSelectionSwitch(const CEGUI::EventArgs& event)
421 {
422         using namespace CEGUI;
423         if(WindowManager::getSingleton().getWindow("BookmarkSelection")->isVisible()){
424                 WindowManager::getSingleton().getWindow("BookmarkSelection")->hide();
425         } else {
426                 WindowManager::getSingleton().getWindow("DestinationChoose")->hide();
427                 WindowManager::getSingleton().getWindow("BookmarkSelection")->show();
428         }
429 }
430
431 bool FormerDestSelectionSwitch(const CEGUI::EventArgs& event)
432 {
433         using namespace CEGUI;
434         if(WindowManager::getSingleton().getWindow("FormerDestSelection")->isVisible()){
435                 WindowManager::getSingleton().getWindow("FormerDestSelection")->hide();
436         } else {
437                 WindowManager::getSingleton().getWindow("DestinationChoose")->hide();
438                 WindowManager::getSingleton().getWindow("FormerDestSelection")->show();
439         }
440 }
441
442 bool AddressSearchSwitch(const CEGUI::EventArgs& event)
443 {
444         using namespace CEGUI;
445         WindowManager::getSingleton().getWindow("DestinationChoose")->hide();
446         WindowManager::getSingleton().getWindow("AdressSearchWindow")->show();
447
448
449         if(sdl_gui_navit){      
450         } else {
451                 dbg(0,"*** Invalid navit instance in sdl_events\n");
452         }
453         struct search_param *search=&search_param;
454         struct search_list_result *res;
455
456         // dbg(1,"search->nav=sdl_gui_navit;\n");
457         search->nav=sdl_gui_navit;
458         // dbg(1,"search->ms=navit_get_mapset(sdl_gui_navit);\n");
459         search->ms=navit_get_mapset(sdl_gui_navit);
460         // dbg(1,"search->sl=search_list_new(search->ms);\n");
461         search->sl=search_list_new(search->ms);
462
463
464         const CEGUI::WindowEventArgs& we =  static_cast<const CEGUI::WindowEventArgs&>(event);
465
466         Window* town_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
467         town_edit->setText("");
468         Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
469         street_edit->setText("");
470         town_edit->activate();
471
472         SDL_dest.current_search=SRCH_COUNTRY;
473         MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
474         mcl->resetList();
475         
476         
477         // Code to get the current country and set it by default for the search
478         struct attr search_attr, country_name, *country_attr;
479         struct tracking *tracking;
480         struct country_search *cs;
481         struct item *item;
482
483
484         Editbox* country_edit = static_cast<Editbox*>(WindowManager::getSingleton().getWindow("AdressSearch/CountryEditbox"));
485
486         country_attr=country_default();
487         tracking=navit_get_tracking(sdl_gui_navit);
488         if (tracking && tracking_get_current_attr(tracking, attr_country_id, &search_attr)) 
489                 country_attr=&search_attr;
490         cs=country_search_new(country_attr, 0);
491         item=country_search_get_item(cs);
492         if (item && item_attr_get(item, attr_country_name, &country_name)){
493                 country_edit->setText(country_name.u.str);
494                 handle_destination_change();
495                 }
496         country_search_destroy(cs);
497
498         SDL_dest.current_search=SRCH_TOWN;
499
500         return true;
501 }
502
503 bool Switch_to_nGhost(const CEGUI::EventArgs& event)
504 {
505         extern char media_window_title[255], media_cmd[255];
506         dbg(0,"trying to switch to %s (%s)\n",media_window_title,media_cmd);
507         if (window_switch(media_window_title)==EXIT_FAILURE)
508         {
509                 popen(media_cmd,"r");
510         }
511
512 }
513
514
515 bool RoadBookSwitch(const CEGUI::EventArgs& event)
516 {
517         using namespace CEGUI;
518
519 //      const CEGUI::WindowEventArgs& we =  static_cast<const CEGUI::WindowEventArgs&>(event);
520         if(WindowManager::getSingleton().getWindow("Navit/RoadBook")->isVisible()){
521                 WindowManager::getSingleton().getWindow("Navit/RoadBook")->hide();
522                 WindowManager::getSingleton().getWindow("OSD/RoadbookButton")->show();
523         } else {
524                 WindowManager::getSingleton().getWindow("Navit/RoadBook")->show();
525                 WindowManager::getSingleton().getWindow("OSD/RoadbookButton")->hide();
526         }
527         return true;
528 }
529
530 bool ButtonGo(const CEGUI::EventArgs& event)
531 {
532         using namespace CEGUI;
533         
534         MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
535         // First, make sure the user selected an entry in the town choice. If he hadn't, select the first for him.
536         if(SDL_dest.current_search==SRCH_TOWN){
537                 if (mcl->getRowCount()>0)
538                 {
539                         handleItemSelect(0);
540                 }
541         }
542
543
544         Window* Dest_x = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_x"));
545         Window* Dest_y = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_y"));
546         
547         extern struct navit *sdl_gui_navit;
548         route_to(atoi(Dest_x->getText().c_str()),atoi(Dest_y->getText().c_str()));
549
550         return true;
551 }
552
553
554 bool ZoomIn(const CEGUI::EventArgs& event)
555 {
556         extern struct navit *sdl_gui_navit;
557         navit_zoom_in(sdl_gui_navit, 2, NULL);
558
559 }
560
561 bool ZoomOut(const CEGUI::EventArgs& event)
562 {
563         extern struct navit *sdl_gui_navit;
564         navit_zoom_out(sdl_gui_navit, 2, NULL);
565 }
566
567 bool ButtonQuit(const CEGUI::EventArgs& event)
568 {
569         exit(0);
570 }
571
572
573
574
575 // Nothing really interesting below.
576
577 void handle_mouse_down(Uint8 button)
578 {
579         switch ( button )
580         {
581                 // handle real mouse buttons
582                 case SDL_BUTTON_LEFT:
583                         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
584                         break;
585                 case SDL_BUTTON_MIDDLE:
586                         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
587                         break;
588                 case SDL_BUTTON_RIGHT:
589                         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
590                         break;
591                 
592                 // handle the mouse wheel
593                 case SDL_BUTTON_WHEELDOWN:
594                         CEGUI::System::getSingleton().injectMouseWheelChange( -1 );
595                         break;
596                 case SDL_BUTTON_WHEELUP:
597                         CEGUI::System::getSingleton().injectMouseWheelChange( +1 );
598                         break;
599         }
600 }
601
602
603 void handle_mouse_up(Uint8 button)
604 {
605         switch ( button )
606         {
607         case SDL_BUTTON_LEFT:
608                 CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);
609                 break;
610         case SDL_BUTTON_MIDDLE:
611                 CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);
612                 break;
613         case SDL_BUTTON_RIGHT:
614                 CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);
615                 break;
616         }
617 }
618
619 void inject_time_pulse(double& last_time_pulse)
620 {
621         // get current "run-time" in seconds
622         double t = 0.001*SDL_GetTicks();
623
624         // inject the time that passed since the last call 
625         CEGUI::System::getSingleton().injectTimePulse( float(t-last_time_pulse) );
626
627         // store the new time as the last time
628         last_time_pulse = t;
629 }
630
631  CEGUI::uint SDLKeyToCEGUIKey(SDLKey key)
632  {
633      using namespace CEGUI;
634      switch (key)
635      {
636      case SDLK_BACKSPACE:    return Key::Backspace;
637      case SDLK_TAB:          return Key::Tab;
638      case SDLK_RETURN:       return Key::Return;
639      case SDLK_PAUSE:        return Key::Pause;
640      case SDLK_ESCAPE:       return Key::Escape;
641      case SDLK_SPACE:        return Key::Space;
642      case SDLK_COMMA:        return Key::Comma;
643      case SDLK_MINUS:        return Key::Minus;
644      case SDLK_PERIOD:       return Key::Period;
645      case SDLK_SLASH:        return Key::Slash;
646      case SDLK_0:            return Key::Zero;
647      case SDLK_1:            return Key::One;
648      case SDLK_2:            return Key::Two;
649      case SDLK_3:            return Key::Three;
650      case SDLK_4:            return Key::Four;
651      case SDLK_5:            return Key::Five;
652      case SDLK_6:            return Key::Six;
653      case SDLK_7:            return Key::Seven;
654      case SDLK_8:            return Key::Eight;
655      case SDLK_9:            return Key::Nine;
656      case SDLK_COLON:        return Key::Colon;
657      case SDLK_SEMICOLON:    return Key::Semicolon;
658      case SDLK_EQUALS:       return Key::Equals;
659      case SDLK_LEFTBRACKET:  return Key::LeftBracket;
660      case SDLK_BACKSLASH:    return Key::Backslash;
661      case SDLK_RIGHTBRACKET: return Key::RightBracket;
662      case SDLK_a:            return Key::A;
663      case SDLK_b:            return Key::B;
664      case SDLK_c:            return Key::C;
665      case SDLK_d:            return Key::D;
666      case SDLK_e:            return Key::E;
667      case SDLK_f:            return Key::F;
668      case SDLK_g:            return Key::G;
669      case SDLK_h:            return Key::H;
670      case SDLK_i:            return Key::I;
671      case SDLK_j:            return Key::J;
672      case SDLK_k:            return Key::K;
673      case SDLK_l:            return Key::L;
674      case SDLK_m:            return Key::M;
675      case SDLK_n:            return Key::N;
676      case SDLK_o:            return Key::O;
677      case SDLK_p:            return Key::P;
678      case SDLK_q:            return Key::Q;
679      case SDLK_r:            return Key::R;
680      case SDLK_s:            return Key::S;
681      case SDLK_t:            return Key::T;
682      case SDLK_u:            return Key::U;
683      case SDLK_v:            return Key::V;
684      case SDLK_w:            return Key::W;
685      case SDLK_x:            return Key::X;
686      case SDLK_y:            return Key::Y;
687      case SDLK_z:            return Key::Z;
688      case SDLK_DELETE:       return Key::Delete;
689      case SDLK_KP0:          return Key::Numpad0;
690      case SDLK_KP1:          return Key::Numpad1;
691      case SDLK_KP2:          return Key::Numpad2;
692      case SDLK_KP3:          return Key::Numpad3;
693      case SDLK_KP4:          return Key::Numpad4;
694      case SDLK_KP5:          return Key::Numpad5;
695      case SDLK_KP6:          return Key::Numpad6;
696      case SDLK_KP7:          return Key::Numpad7;
697      case SDLK_KP8:          return Key::Numpad8;
698      case SDLK_KP9:          return Key::Numpad9;
699      case SDLK_KP_PERIOD:    return Key::Decimal;
700      case SDLK_KP_DIVIDE:    return Key::Divide;
701      case SDLK_KP_MULTIPLY:  return Key::Multiply;
702      case SDLK_KP_MINUS:     return Key::Subtract;
703      case SDLK_KP_PLUS:      return Key::Add;
704      case SDLK_KP_ENTER:     return Key::NumpadEnter;
705      case SDLK_KP_EQUALS:    return Key::NumpadEquals;
706      case SDLK_UP:           return Key::ArrowUp;
707      case SDLK_DOWN:         return Key::ArrowDown;
708      case SDLK_RIGHT:        return Key::ArrowRight;
709      case SDLK_LEFT:         return Key::ArrowLeft;
710      case SDLK_INSERT:       return Key::Insert;
711      case SDLK_HOME:         return Key::Home;
712      case SDLK_END:          return Key::End;
713      case SDLK_PAGEUP:       return Key::PageUp;
714      case SDLK_PAGEDOWN:     return Key::PageDown;
715      case SDLK_F1:           return Key::F1;
716      case SDLK_F2:           return Key::F2;
717      case SDLK_F3:           return Key::F3;
718      case SDLK_F4:           return Key::F4;
719      case SDLK_F5:           return Key::F5;
720      case SDLK_F6:           return Key::F6;
721      case SDLK_F7:           return Key::F7;
722      case SDLK_F8:           return Key::F8;
723      case SDLK_F9:           return Key::F9;
724      case SDLK_F10:          return Key::F10;
725      case SDLK_F11:          return Key::F11;
726      case SDLK_F12:          return Key::F12;
727      case SDLK_F13:          return Key::F13;
728      case SDLK_F14:          return Key::F14;
729      case SDLK_F15:          return Key::F15;
730      case SDLK_NUMLOCK:      return Key::NumLock;
731      case SDLK_SCROLLOCK:    return Key::ScrollLock;
732      case SDLK_RSHIFT:       return Key::RightShift;
733      case SDLK_LSHIFT:       return Key::LeftShift;
734      case SDLK_RCTRL:        return Key::RightControl;
735      case SDLK_LCTRL:        return Key::LeftControl;
736      case SDLK_RALT:         return Key::RightAlt;
737      case SDLK_LALT:         return Key::LeftAlt;
738      case SDLK_LSUPER:       return Key::LeftWindows;
739      case SDLK_RSUPER:       return Key::RightWindows;
740      case SDLK_SYSREQ:       return Key::SysRq;
741      case SDLK_MENU:         return Key::AppMenu;
742      case SDLK_POWER:        return Key::Power;
743      default:                return 0;
744      }
745      return 0;
746  }
747
748
749 void inject_input(bool& must_quit)
750 {
751   SDL_Event e;
752   
753   // go through all available events
754   while (SDL_PollEvent(&e))
755   {
756     // we use a switch to determine the event type
757     switch (e.type)
758     {
759       // mouse motion handler
760       case SDL_MOUSEMOTION:
761         // we inject the mouse position directly.
762         CEGUI::System::getSingleton().injectMousePosition(
763           static_cast<float>(e.motion.x),
764           static_cast<float>(e.motion.y)
765         );
766         break;
767     
768     // mouse down handler
769       case SDL_MOUSEBUTTONDOWN:
770         // let a special function handle the mouse button down event
771         handle_mouse_down(e.button.button);
772         break;
773     
774       // mouse up handler
775       case SDL_MOUSEBUTTONUP:
776         // let a special function handle the mouse button up event
777         handle_mouse_up(e.button.button);
778         break;
779     
780     
781       // key down
782       case SDL_KEYDOWN:
783         // to tell CEGUI that a key was pressed, we inject the scancode, translated from SDL
784         CEGUI::System::getSingleton().injectKeyDown(SDLKeyToCEGUIKey(e.key.keysym.sym));
785         
786         // as for the character it's a litte more complicated. we'll use for translated unicode value.
787         // this is described in more detail below.
788         if ((e.key.keysym.unicode & 0xFF80) == 0)
789         {
790           CEGUI::System::getSingleton().injectChar(e.key.keysym.unicode & 0x7F);
791         }
792         break;
793     
794       // key up
795       case SDL_KEYUP:
796         // like before we inject the scancode translated from SDL.
797         CEGUI::System::getSingleton().injectKeyUp(SDLKeyToCEGUIKey(e.key.keysym.sym));
798         break;
799     
800     
801       // WM quit event occured
802       case SDL_QUIT:
803         must_quit = true;
804         break;
805     
806     }
807   
808   }
809
810 }