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