bigendian again
[pcsx_rearmed.git] / maemo / hildon.c
1 #include <gtk/gtk.h>
2 #include <glib.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 #include <hildon/hildon.h>
7 #include <string.h>
8 #include <pthread.h>
9
10 #include "../frontend/plugin_lib.h"
11 #include "../frontend/main.h"
12 #include "../libpcsxcore/misc.h"
13 #include "../include/psemu_plugin_defs.h"
14 #include "../libpcsxcore/cdrom.h"
15 #include "../libpcsxcore/cdriso.h"
16 #include "../frontend/libpicofe/readpng.h"
17 #include "maemo_common.h"
18 #include <libosso.h>
19 #include <dbus/dbus.h>
20
21 #define X_RES           800
22 #define Y_RES           480
23 #define D_WIDTH                 640
24 #define D_HEIGHT                480
25
26 #define CALL_SIGNAL_IF "com.nokia.csd.Call"
27 #define CALL_SIGNAL_PATH "/com/nokia/csd/call"
28 #define CALL_INCOMING_SIG "Coming"
29
30 #define DBUS_RULE_CALL_INCOMING "type='signal',interface='" CALL_SIGNAL_IF \
31                                 "',path='" CALL_SIGNAL_PATH \
32                                 "',member='" CALL_INCOMING_SIG "'"
33
34 osso_context_t* osso = NULL;
35 int bRunning = TRUE;
36 extern int bKeepDisplayOn;
37 extern int bAutosaveOnExit;
38 extern int cornerActions[4];
39 extern char keys_config_file[MAXPATHLEN];
40 static pthread_t display_thread = (pthread_t)0;
41 int g_layer_x = (X_RES - D_WIDTH) / 2;
42 int g_layer_y = (Y_RES - D_HEIGHT) / 2;
43 int g_layer_w = D_WIDTH, g_layer_h = D_HEIGHT;
44
45 static GdkImage *image;
46 static HildonAnimationActor *actor;
47 static GtkWidget *window, *drawing = NULL;
48
49 static int pl_buf_w, pl_buf_h;
50 int keymap[65536];
51 int direction_keys[4];
52
53 // map psx4m compatible keymap to PSX keys
54 static const unsigned char keymap2[14] = {
55         DKEY_LEFT,   // 0
56         DKEY_RIGHT,
57         DKEY_UP,
58         DKEY_DOWN,
59         DKEY_CIRCLE,
60         DKEY_CROSS,  // 5
61         DKEY_TRIANGLE,
62         DKEY_SQUARE,
63         DKEY_SELECT,
64         DKEY_START,
65         DKEY_L1,     // 10
66         DKEY_R1,
67         DKEY_L2,
68         DKEY_R2,
69 };
70
71 void hildon_quit()
72 {
73         maemo_finish();
74         gtk_main_quit();
75         exit(0);
76 }
77
78 gdouble press_x = -1;
79 gdouble press_y = -1;
80
81 int maemo_x11_update_keys();
82 void show_notification(char* text);
83
84 void change_slot(int delta)
85 {
86         state_slot += delta;
87         if (state_slot > 9)
88                 state_slot = 0;
89         else if (state_slot < 0)
90                 state_slot = 9;
91         char message[50];
92         sprintf(message,"Savestate slot: %i",state_slot + 1);
93         show_notification(message);
94 }
95
96 void save(int state_slot)
97 {
98         emu_save_state(state_slot);
99         char buf[MAXPATHLEN];
100         if (image && image->mem){
101                 sprintf (buf,"/opt/maemo/usr/games/screenshots%s.%3.3d",file_name,state_slot);
102                 writepng(buf, image->mem, pl_buf_w,pl_buf_h);
103         }
104         char message[50];
105         sprintf(message,"Saved savestate slot: %i",state_slot + 1);
106         show_notification(message);
107 }
108
109 void quit()
110 {
111         if (bAutosaveOnExit){
112                 show_notification("Autosaving");
113                 emu_save_state(99);
114                 char buf[MAXPATHLEN];
115                 if (image && image->mem){
116                         sprintf (buf,"/opt/maemo/usr/games/screenshots%s.%3.3d",file_name,99);
117                         writepng(buf, image->mem, pl_buf_w,pl_buf_h);
118                 }
119         }
120         hildon_quit();
121 }
122
123 int show_confirmbox(char* text)
124 {
125         if (!window)
126                 return TRUE;
127
128         GtkWidget *dialog;
129         dialog = gtk_message_dialog_new (GTK_WINDOW(window),
130                                                                          GTK_DIALOG_DESTROY_WITH_PARENT,
131                                                                          GTK_MESSAGE_QUESTION,
132                                                                          GTK_BUTTONS_YES_NO,
133                                                                          text);
134         gint result = gtk_dialog_run (GTK_DIALOG (dialog));
135         gtk_widget_destroy (dialog);
136         if (result == GTK_RESPONSE_YES)
137                 return TRUE;
138         return FALSE;
139 }
140
141 static void
142 window_button_proxy(GtkWidget *widget,
143                                     GdkEventButton *event,
144                                     gpointer user_data)
145 {
146         int corner = -1;
147         int sens = 100;
148
149         switch (event->type){
150         case GDK_BUTTON_PRESS:
151                 //printf("GDK_BUTTON_PRESS: x=%f y=%f\n", event->x, event->y);
152                 press_x = event->x;
153                 press_y = event->y;
154                 break;
155         case GDK_BUTTON_RELEASE:
156                 //printf("GDK_BUTTON_RELEASE: x=%f y=%f\n", event->x, event->y);
157                 if (press_x < sens && press_y < sens && event->x < sens && event->y < sens)
158                         corner = 0;
159                 else if (press_x > 800 - sens && press_y < sens && event->x > 800 - sens && event->y < sens)
160                         corner = 1;
161                 else if (press_x > 800 - sens && press_y > 480 - sens && event->x > 800 - sens && event->y > 480 - sens)
162                         corner = 2;
163                 else if (press_x < sens && press_y > 480 - sens && event->x < sens && event->y > 480 - sens)
164                         corner = 3;
165
166                 press_x = -1;
167                 press_y = -1;
168                 break;
169         default:
170                 break;
171         }
172
173         if (corner >= 0){
174                 switch (cornerActions[corner]){
175                         case 1:
176                                 if (show_confirmbox("Save savestate?"))
177                                         save(state_slot);
178                                 break;
179                         case 2:
180                                 if (show_confirmbox("Load savestate?"))
181                                         emu_load_state(state_slot);
182                                 break;
183                         case 3:
184                                 change_slot(1);
185                                 break;
186                         case 4:
187                                 change_slot(-1);
188                                 break;
189                         case 5:
190                                 if (show_confirmbox("Quit?"))
191                                         quit();
192                                 break;
193                 }
194         }
195 }
196
197 static void *displayThread(void *arg)
198 {
199         DBusConnection* system_bus = (DBusConnection*)osso_get_sys_dbus_connection(osso);
200         DBusMessage* msg = dbus_message_new_method_call("com.nokia.mce",
201                                                                                                     "/com/nokia/mce/request",
202                                                                                                     "com.nokia.mce.request",
203                                                                                                     "req_display_blanking_pause");
204         if (msg && system_bus) {
205                 bRunning = TRUE;
206                 while (bRunning) {
207                         dbus_connection_send(system_bus, msg, NULL);
208                         dbus_connection_flush(system_bus);
209                         int i = 0;
210                         for (i=0; i<8; i++){
211                                 usleep(500000);
212                                 if (!bRunning)
213                                         break;
214                         }
215                 }
216                 dbus_message_unref(msg);
217         }
218
219         pthread_exit(0);
220         return NULL;
221 }
222
223 void show_notification(char* text)
224 {
225         if (window){
226                 GtkWidget* banner = hildon_banner_show_information(GTK_WIDGET(window), NULL, text);
227                 hildon_banner_set_timeout(HILDON_BANNER(banner), 3000);
228         }else{
229                 DBusConnection* session_bus = (DBusConnection*)osso_get_dbus_connection(osso);
230                 DBusMessageIter args;
231                 DBusMessage*msg = dbus_message_new_method_call("org.freedesktop.Notifications",
232                                                                                                            "/org/freedesktop/Notifications",
233                                                                                                            "org.freedesktop.Notifications",
234                                                                                                            "SystemNoteInfoprint");
235                 if (msg) {
236                         dbus_message_iter_init_append(msg, &args);
237                         char* param = text;
238                         if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &param)) {
239                                 dbus_connection_send(session_bus, msg, NULL);
240                                 dbus_connection_flush(session_bus);
241                         }
242                         dbus_message_unref(msg);
243                 }
244         }
245 }
246
247 void show_messagebox(char* text)
248 {
249         if (!window)
250                 return;
251
252         GtkWidget *dialog;
253         dialog = gtk_message_dialog_new (GTK_WINDOW(window),
254                                                                          GTK_DIALOG_DESTROY_WITH_PARENT,
255                                                                          GTK_MESSAGE_INFO,
256                                                                          GTK_BUTTONS_OK,
257                                                                          text);
258         gtk_dialog_run (GTK_DIALOG (dialog));
259         gtk_widget_destroy (dialog);
260 }
261
262 #include <hildon/hildon-file-chooser-dialog.h>
263 void change_disc()
264 {
265         GtkWidget *dialog;
266         dialog = hildon_file_chooser_dialog_new (GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_OPEN);
267     gtk_window_set_title (GTK_WINDOW (dialog), "Change disc");
268
269         char currentFile[MAXPATHLEN];
270         strcpy(currentFile, GetIsoFile());
271         if (strlen(currentFile))
272                 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(dialog), currentFile);
273         else
274                 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), "/home/user/MyDocs/");
275
276         GtkFileFilter *filter=gtk_file_filter_new();
277         gtk_file_filter_add_pattern (filter,"*.bin");
278         gtk_file_filter_add_pattern (filter,"*.BIN");
279         gtk_file_filter_add_pattern (filter,"*.iso");
280         gtk_file_filter_add_pattern (filter,"*.ISO");
281         gtk_file_filter_add_pattern (filter,"*.img");
282         gtk_file_filter_add_pattern (filter,"*.IMG");
283         gtk_file_filter_add_pattern (filter,"*.z");
284         gtk_file_filter_add_pattern (filter,"*.Z");
285         gtk_file_filter_add_pattern (filter,"*.znx");
286         gtk_file_filter_add_pattern (filter,"*.ZNX");
287         gtk_file_filter_add_pattern (filter,"*.pbp");
288         gtk_file_filter_add_pattern (filter,"*.PBP");
289         gtk_file_filter_add_pattern (filter,"*.mdf");
290         gtk_file_filter_add_pattern (filter,"*.MDF");
291         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog),filter);
292
293         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
294                 char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
295
296                 //if (strcmp(filename, currentFile)) {
297                         CdromId[0] = '\0';
298                         CdromLabel[0] = '\0';
299
300                         set_cd_image(filename);
301                         if (ReloadCdromPlugin() < 0)
302                                 printf("Failed to load cdr plugin\n");
303
304                         if (CDR_open() < 0)
305                                 printf("Failed to open cdr plugin\n");
306
307                         strcpy(file_name, strrchr(filename,'/'));
308
309                         SetCdOpenCaseTime(time(NULL) + 3);
310                         LidInterrupt();
311                 //}
312                 g_free (filename);
313         }
314
315         gtk_widget_destroy (dialog);
316 }
317
318 void change_multi_disc()
319 {
320     HildonDialog* window = HILDON_DIALOG(hildon_dialog_new());
321     gtk_window_set_title (GTK_WINDOW (window), "Change disc");
322     gtk_window_set_default_size(GTK_WINDOW (window), 480, 300);
323
324     GtkWidget* sw = hildon_pannable_area_new ();
325     gtk_box_pack_start (GTK_BOX(GTK_DIALOG(window)->vbox), sw, TRUE, TRUE, 0);
326
327     GtkWidget* tree_view = hildon_gtk_tree_view_new (HILDON_UI_MODE_EDIT);
328     gtk_widget_set_name (tree_view, "fremantle-widget");
329
330     gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
331
332     int i;
333     GtkListStore *store = gtk_list_store_new (1, G_TYPE_STRING);
334     for (i = 0; i < cdrIsoMultidiskCount; i++) {
335         gchar *str;
336
337         str = g_strdup_printf ("Disc %d", i+1);
338         gtk_list_store_insert_with_values (store, NULL, i, 0, str, -1);
339         g_free (str);
340     }
341     GtkTreeModel* model =  GTK_TREE_MODEL (store);
342
343     gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), model);
344     g_object_unref (model);
345
346     GtkTreeSelection* selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
347     gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
348
349     GtkCellRenderer* renderer = gtk_cell_renderer_text_new ();
350     g_object_set (renderer,
351                   "xalign", 0.5,
352                   "weight", PANGO_WEIGHT_NORMAL,
353                   NULL);
354
355     gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
356                                                  0, "Column 0",
357                                                  renderer,
358                                                  "text", 0,
359                                                  NULL);
360
361     char current[5];
362     sprintf(current, "%i", cdrIsoMultidiskSelect);
363     GtkTreePath* path = gtk_tree_path_new_from_string(current);
364     gtk_tree_selection_select_path (selection, path);
365     gtk_tree_path_free(path);
366
367     gtk_widget_set_size_request (tree_view, 480, 800);
368     gtk_container_add (GTK_CONTAINER (sw), tree_view);
369
370     hildon_dialog_add_button (HILDON_DIALOG(window), GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
371
372     gtk_widget_show_all (GTK_WIDGET(window));
373     gint result = gtk_dialog_run (GTK_DIALOG (window));
374     if (result == GTK_RESPONSE_ACCEPT) {
375       GtkTreeModel* model;
376       GtkTreeIter iter;
377       GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
378       if (gtk_tree_selection_get_selected(selection, &model, &iter)){
379             GtkTreePath* path = gtk_tree_model_get_path(model , &iter);
380                 int* i = gtk_tree_path_get_indices(path) ;
381
382                 cdrIsoMultidiskSelect = *i;
383                 CdromId[0] = '\0';
384                 CdromLabel[0] = '\0';
385
386                 CDR_close();
387                 if (CDR_open() < 0) {
388                         printf("Failed to load cdr plugin\n");
389                         return;
390                 }
391
392                 SetCdOpenCaseTime(time(NULL) + 3);
393                 LidInterrupt();
394       }
395     }
396         gtk_widget_destroy(GTK_WIDGET(window));
397 }
398
399 static DBusHandlerResult on_msg_recieved(DBusConnection* connection G_GNUC_UNUSED, DBusMessage* message, void* data)
400 {
401         const char* path = dbus_message_get_path(message);
402         if (path && g_str_equal(path, CALL_SIGNAL_PATH)){
403                 const char* mbr = dbus_message_get_member(message);
404                 if (mbr && g_str_equal(mbr, CALL_INCOMING_SIG))
405                         show_messagebox("Paused");
406         }
407
408         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
409 }
410
411 static void
412 window_key_proxy(GtkWidget *widget,
413                      GdkEventKey *event,
414                      gpointer user_data)
415 {
416         key_press_event(event->hardware_keycode, event->type == GDK_KEY_PRESS ? 1 : (event->type == GDK_KEY_RELEASE ? 2 : 0) );
417 }
418
419 int last_key_pressed = 0;
420 inline void key_press_event(int key2,int type)
421 {
422         int psxkey1 = -1, psxkey2 = -1;
423         int key=keymap[key2];
424
425         if (key < 0)
426                 return;
427
428         if (type == 1 && key2 == last_key_pressed)
429                 return;
430         last_key_pressed = type == 1 ? key2 : 0;
431
432         //printf("Key: %i %s\n", key2, type == 1 ? "Pressed" : (type == 2 ? "Released" : "Unknown"));
433         if (key < ARRAY_SIZE(keymap2)){
434                 psxkey1 = keymap2[key];
435         }else switch (key) {
436                 case 14:
437                         quit();
438                         break;
439                 case 15:
440                         psxkey1 = DKEY_UP;
441                         psxkey2 = DKEY_LEFT;
442                         break;
443                 case 16:
444                         psxkey1 = DKEY_UP;
445                         psxkey2 = DKEY_RIGHT;
446                         break;
447                 case 17:
448                         psxkey1 = DKEY_DOWN;
449                         psxkey2 = DKEY_LEFT;
450                         break;
451                 case 18:
452                         psxkey1 = DKEY_DOWN;
453                         psxkey2 = DKEY_RIGHT;
454                         break;
455                 case 19:
456                         if (type == 1)
457                                 save(state_slot);
458                         return;
459                 case 20:
460                         if (type == 1)
461                                 emu_load_state(state_slot);
462                         return;
463                 case 21:
464                         if (type == 1)
465                                 change_slot(1);
466                         return;
467                 case 22:
468                         if (type == 1)
469                                 change_slot(-1);
470                         return;
471                 case 23:
472                         if (type == 1){
473                                 if (cdrIsoMultidiskCount > 1)
474                                         change_multi_disc();
475                                 else
476                                         change_disc();
477                         }
478                         return;
479         }
480
481         if (in_type1 == PSE_PAD_TYPE_GUNCON){
482                 if (type == 1) {
483                         switch (psxkey1){
484                                 case DKEY_CROSS:
485                                         in_state_gun |= SACTION_GUN_A;
486                                         break;          
487                                 case DKEY_CIRCLE:
488                                         in_state_gun |= SACTION_GUN_B;
489                                         break;          
490                                 case DKEY_TRIANGLE:
491                                         in_state_gun |= SACTION_GUN_TRIGGER2;
492                                         break;          
493                                 case DKEY_SQUARE:
494                                         in_state_gun |= SACTION_GUN_TRIGGER;
495                                         break;          
496                         }
497                 }else if (type == 2) {
498                         switch (psxkey1){
499                                 case DKEY_CROSS:
500                                         in_state_gun &= ~SACTION_GUN_A;
501                                         break;          
502                                 case DKEY_CIRCLE:
503                                         in_state_gun &= ~SACTION_GUN_B;
504                                         break;          
505                                 case DKEY_TRIANGLE:
506                                         in_state_gun &= ~SACTION_GUN_TRIGGER2;
507                                         break;          
508                                 case DKEY_SQUARE:
509                                         in_state_gun &= ~SACTION_GUN_TRIGGER;
510                                         break;          
511                         }
512                 }
513         }else{
514                 if (type == 1) {
515                 if (psxkey1 >= 0)
516                         in_keystate |= 1 << psxkey1;
517                 if (psxkey2 >= 0)
518                         in_keystate |= 1 << psxkey2;
519
520                         if (in_type1 == PSE_PAD_TYPE_ANALOGPAD){
521                                 switch(psxkey1){
522                                         case DKEY_LEFT:
523                                                 in_a1[0] = 0;
524                                                 break;
525                                         case DKEY_RIGHT:
526                                                 in_a1[0] = 255;
527                                                 break;
528                                         case DKEY_UP:
529                                                 in_a1[1] = 0;
530                                                 break;
531                                         case DKEY_DOWN:
532                                                 in_a1[1] = 255;
533                                                 break;
534                                 }
535         }
536                 }
537                 else if (type == 2) {
538                 if (psxkey1 >= 0)
539                         in_keystate &= ~(1 << psxkey1);
540                 if (psxkey2 >= 0)
541                         in_keystate &= ~(1 << psxkey2);
542
543                         if (in_type1 == PSE_PAD_TYPE_ANALOGPAD){
544                                 switch(psxkey1){
545                                         case DKEY_LEFT:
546                                         case DKEY_RIGHT:
547                                                 in_a1[0] = 127;
548                                                 break;
549                                         case DKEY_UP:
550                                         case DKEY_DOWN:
551                                                 in_a1[1] = 127;
552                                                 break;
553                                 }
554                         }
555                 emu_set_action(SACTION_NONE);
556         }
557         }
558 }
559
560 void plat_finish()
561 {
562         hildon_quit();
563 }
564
565 void set_accel_multipliers()
566 {
567         accelOptions.xMultiplier = 255.0 / ( (accelOptions.maxValue - accelOptions.sens) * 2.0);
568         accelOptions.yMultiplier = 255.0 / ( (accelOptions.maxValue - accelOptions.sens) * 2.0);
569 }
570
571 #include <gdk/gdkx.h>
572 int maemo_init(int *argc, char ***argv)
573 {
574         osso = osso_initialize("pcsxrearmed", PACKAGE_VERSION, FALSE, NULL);
575
576         DBusConnection* system_bus = (DBusConnection*)osso_get_sys_dbus_connection(osso);
577     dbus_bus_add_match(system_bus, DBUS_RULE_CALL_INCOMING, NULL);
578         dbus_connection_add_filter(system_bus, on_msg_recieved, NULL, NULL);
579
580         FILE* pFile;
581         pFile = fopen(keys_config_file, "r");
582         if (pFile == NULL){
583                 fprintf(stderr, "Error opening keys config file %s\n", keys_config_file);
584                 return 1;
585         }
586         printf("Keys config read from %s\n", keys_config_file);
587
588         int ch;
589         int i=0;
590         for (i=0;i<65536;i++)
591                 keymap[i]=-1;
592         if (NULL != pFile) {
593                 for(i=0;i<24;i++){
594                         fscanf(pFile, "%i",&ch);
595                         keymap[ch]=i;
596                         if (i < 4)
597                                 direction_keys[i] = ch;
598                 }
599                 fclose(pFile);
600         }
601         
602         switch (in_type1){
603                 case PSE_PAD_TYPE_GUNCON:
604                         memset(cornerActions, 0, sizeof(cornerActions));
605                         printf("Controller set to GUNCON (SLPH-00034)\n");
606                         break;
607                 case PSE_PAD_TYPE_STANDARD:
608                         printf("Controller set to standard (SCPH-1080)\n");
609                         break;
610                 case PSE_PAD_TYPE_ANALOGPAD:
611                         printf("Controller set to analog (SCPH-1150)\n");
612                         break;  
613         }
614
615         if (in_enable_vibration)
616                 printf("Vibration enabled\n");
617
618         if (!(g_maemo_opts&8)){
619         gtk_init (argc, argv);
620
621         window = hildon_stackable_window_new ();
622         gtk_widget_realize (window);
623         gtk_window_fullscreen (GTK_WINDOW(window));
624
625                 if (cornerActions[0] + cornerActions[1] + cornerActions[2] + cornerActions[3] > 0){
626                         g_signal_connect (G_OBJECT (window), "button_release_event",
627                                                 G_CALLBACK (window_button_proxy), NULL);
628                         g_signal_connect (G_OBJECT (window), "button_press_event",
629                                                 G_CALLBACK (window_button_proxy), NULL);
630                 }
631
632         g_signal_connect (G_OBJECT (window), "key-press-event",
633                                 G_CALLBACK (window_key_proxy), NULL);
634         g_signal_connect (G_OBJECT (window), "key-release-event",
635                                 G_CALLBACK (window_key_proxy), NULL);
636         g_signal_connect (G_OBJECT (window), "delete_event",
637                                 G_CALLBACK (hildon_quit), NULL);
638         gtk_widget_add_events (window,
639                                 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
640
641         actor = HILDON_ANIMATION_ACTOR (hildon_animation_actor_new());
642         if (g_maemo_opts & 2)
643                 hildon_animation_actor_set_position (actor, 0, 0 );
644         else
645                 hildon_animation_actor_set_position (actor, (X_RES - D_WIDTH)/2, (Y_RES - D_HEIGHT)/2 );
646         hildon_animation_actor_set_parent (actor, GTK_WINDOW (window));
647
648         drawing = gtk_image_new ();
649
650         gtk_container_add (GTK_CONTAINER (actor), drawing);
651
652         gtk_widget_show_all (GTK_WIDGET (actor));
653         gtk_widget_show_all (GTK_WIDGET (window));
654         }else{
655                 gtk_init (argc, argv);
656                 /*GdkScreen* scr = gdk_screen_get_default();
657                 window = GTK_WIDGET(gdk_screen_get_root_window(scr));
658                 if (!window)
659                         window = GTK_WIDGET(gdk_get_default_root_window());*/
660         }
661
662         set_accel_multipliers();
663
664         if (bKeepDisplayOn){
665                 if (pthread_create(&display_thread, NULL, displayThread, NULL))
666                         printf("Failed to create display thread.\n");           
667         }
668
669         pl_rearmed_cbs.only_16bpp = 1;
670         return 0;
671 }
672
673 void maemo_finish()
674 {
675         if (display_thread > 0){
676                 bRunning = FALSE;
677                 pthread_join(display_thread, NULL);
678         }
679
680         if (osso){
681                 osso_deinitialize(osso);
682                 osso = NULL;
683         }
684         printf("Exiting\n");
685 }
686
687 void menu_loop(void)
688 {
689 }
690
691 void *plat_gvideo_set_mode(int *w_, int *h_, int *bpp_)
692 {
693         int w = *w_, h = *h_;
694
695         if (g_maemo_opts&8) return pl_vout_buf;
696         //printf("Setting video mode %ix%i\n", w, h);
697
698         if (w <= 0 || h <= 0)
699                 return pl_vout_buf;
700
701         if (image) gdk_image_destroy(image);
702         image = gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), w, h );
703
704         pl_vout_buf = (void *) image->mem;
705
706         gtk_image_set_from_image (GTK_IMAGE(drawing), image, NULL);
707
708         gtk_window_resize (GTK_WINDOW (actor), w, h);
709         if (g_maemo_opts & 2)
710                 hildon_animation_actor_set_scale (actor,
711                                 (gdouble)800 / (gdouble)w,
712                                 (gdouble)480 / (gdouble)h
713                                 );
714         else
715                 hildon_animation_actor_set_scale (actor,
716                                 (gdouble)D_WIDTH / (gdouble)w,
717                                 (gdouble)D_HEIGHT / (gdouble)h
718                                 );
719         pl_buf_w=w;pl_buf_h=h;
720         return pl_vout_buf;
721 }
722
723 void *plat_gvideo_flip(void)
724 {
725         if (!(g_maemo_opts&8))
726                 gtk_widget_queue_draw(drawing);
727
728         // process accelometer
729         if (g_maemo_opts & 4) {
730                 float x, y, z;
731                 FILE* f = fopen( "/sys/class/i2c-adapter/i2c-3/3-001d/coord", "r" );
732                 if( !f ) {printf ("err in accel"); exit(1);}
733                 fscanf( f, "%f %f %f", &x, &y, &z );
734                 fclose( f );
735
736                 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD){
737                         if (x > accelOptions.maxValue) x = accelOptions.maxValue;
738                         else if (x < -accelOptions.maxValue) x = -accelOptions.maxValue;
739
740                         const int maxValue = accelOptions.maxValue - accelOptions.sens;
741                         if(x > accelOptions.sens){
742                                 x -= accelOptions.sens;
743                                 in_a1[0] = (-x + maxValue ) *  accelOptions.xMultiplier;
744                         }else if (x < -accelOptions.sens){
745                                 x += accelOptions.sens;
746                                 in_a1[0] = (-x + maxValue ) *  accelOptions.xMultiplier;
747                         }else in_a1[0] = 127;
748
749                         y += accelOptions.y_def;
750                         if (y > accelOptions.maxValue) y = accelOptions.maxValue;
751                         else if (y < -accelOptions.maxValue) y = -accelOptions.maxValue;
752
753                         if(y > accelOptions.sens){
754                                 y -= accelOptions.sens;
755                                 in_a1[1] = (-y + maxValue ) *  accelOptions.yMultiplier;
756                         }else if (y < -accelOptions.sens){
757                                 y += accelOptions.sens;
758                                 in_a1[1] = (-y + maxValue ) *  accelOptions.yMultiplier;
759                         }else in_a1[1] = 127;
760
761                         //printf("x: %i y: %i\n", in_a1[0], in_a1[1]);
762                 }else{
763                         if( x > accelOptions.sens ) in_keystate |= 1 << DKEY_LEFT;
764                         else if( x < -accelOptions.sens ) in_keystate |= 1 << DKEY_RIGHT;
765                 else {in_keystate &= ~(1 << DKEY_LEFT);in_keystate &= ~(1 << DKEY_RIGHT);}
766
767                         y += accelOptions.y_def;
768                         if( y > accelOptions.sens )in_keystate |= 1 << DKEY_UP;
769                         else if( y < -accelOptions.sens ) in_keystate |= 1 << DKEY_DOWN;
770                 else {in_keystate &= ~(1 << DKEY_DOWN);in_keystate &= ~(1 << DKEY_UP);}
771                 }
772         }
773
774         return pl_vout_buf;
775 }
776
777 // for frontend/plugin_lib.c
778 void update_input(void)
779 {
780         if (g_maemo_opts & 8)
781                 maemo_x11_update_keys();
782         else {
783                 /* process GTK+ events */
784                 while (gtk_events_pending())
785                         gtk_main_iteration();
786         }
787 }
788
789 int omap_enable_layer(int enabled)
790 {
791         return 0;
792 }
793
794 void menu_notify_mode_change(int w, int h, int bpp)
795 {
796 }
797
798 void *plat_prepare_screenshot(int *w, int *h, int *bpp)
799 {
800         return NULL;
801 }
802
803 void plat_step_volume(int is_up)
804 {
805 }
806
807 void plat_trigger_vibrate(int pad, int low, int high)
808 {
809         const int vDuration = 10;
810
811         DBusConnection* system_bus = (DBusConnection*)osso_get_sys_dbus_connection(osso);
812         DBusMessageIter args;
813         DBusMessage*msg = dbus_message_new_method_call("com.nokia.mce",
814                                                                                                    "/com/nokia/mce/request",
815                                                                                                    "com.nokia.mce.request",
816                                                                                                    "req_start_manual_vibration");
817         if (msg) {
818                 dbus_message_iter_init_append(msg, &args);
819                 // FIXME: somebody with hardware should tune this
820                 int speed = high; // is_strong ? 200 : 150;
821                 int duration = vDuration;
822                 if (dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &speed)) {
823                         if (dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &duration)) {
824                                 dbus_connection_send(system_bus, msg, NULL);
825                                 //dbus_connection_flush(system_bus);
826                         }
827                 }
828                 dbus_message_unref(msg);
829         }
830 }
831
832 void plat_minimize(void)
833 {
834 }
835
836 void plat_gvideo_close(void)
837 {
838 }
839
840 void plat_gvideo_open(int is_pal)
841 {
842 }