frontend: Fix buffer overrun
[pcsx_rearmed.git] / maemo / hildon.c
CommitLineData
b7b2fb41
B
1#include <gtk/gtk.h>
2#include <glib.h>
3#include <stdlib.h>
54b4a001 4#include <stdint.h>
b7b2fb41
B
5#include <unistd.h>
6#include <hildon/hildon.h>
a76fd953
PI
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"
a76fd953 16#include "../frontend/libpicofe/readpng.h"
d64280d6 17#include "maemo_common.h"
a76fd953
PI
18#include <libosso.h>
19#include <dbus/dbus.h>
b7b2fb41
B
20
21#define X_RES 800
22#define Y_RES 480
23#define D_WIDTH 640
24#define D_HEIGHT 480
25
a76fd953
PI
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
34osso_context_t* osso = NULL;
35int bRunning = TRUE;
36extern int bKeepDisplayOn;
37extern int bAutosaveOnExit;
38extern int cornerActions[4];
39extern char keys_config_file[MAXPATHLEN];
40static pthread_t display_thread = (pthread_t)0;
41int g_layer_x = (X_RES - D_WIDTH) / 2;
42int g_layer_y = (Y_RES - D_HEIGHT) / 2;
43int g_layer_w = D_WIDTH, g_layer_h = D_HEIGHT;
44
e0c692d9 45static GdkImage *image;
46static HildonAnimationActor *actor;
a76fd953 47static GtkWidget *window, *drawing = NULL;
e0c692d9 48
bbd471af 49static int pl_buf_w, pl_buf_h;
a76fd953
PI
50int keymap[65536];
51int direction_keys[4];
e0c692d9 52
53// map psx4m compatible keymap to PSX keys
54static 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,
a76fd953 65 DKEY_L1, // 10
e0c692d9 66 DKEY_R1,
a76fd953
PI
67 DKEY_L2,
68 DKEY_R2,
e0c692d9 69};
b7b2fb41 70
e0c692d9 71void hildon_quit()
72{
a76fd953 73 maemo_finish();
e0c692d9 74 gtk_main_quit();
75 exit(0);
76}
b7b2fb41 77
a76fd953
PI
78gdouble press_x = -1;
79gdouble press_y = -1;
80
81int maemo_x11_update_keys();
82void show_notification(char* text);
83
84void 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
96void 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
109void 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
123int 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
141static void
142window_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
197static 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
223void 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
247void 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>
263void 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
318void 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
399static 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
b7b2fb41 411static void
e0c692d9 412window_key_proxy(GtkWidget *widget,
b7b2fb41
B
413 GdkEventKey *event,
414 gpointer user_data)
415{
a76fd953
PI
416 key_press_event(event->hardware_keycode, event->type == GDK_KEY_PRESS ? 1 : (event->type == GDK_KEY_RELEASE ? 2 : 0) );
417}
418
419int last_key_pressed = 0;
420inline void key_press_event(int key2,int type)
421{
422 int psxkey1 = -1, psxkey2 = -1;
423 int key=keymap[key2];
e0c692d9 424
e0c692d9 425 if (key < 0)
426 return;
427
a76fd953
PI
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)){
e0c692d9 434 psxkey1 = keymap2[key];
a76fd953 435 }else switch (key) {
e0c692d9 436 case 14:
a76fd953 437 quit();
e0c692d9 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:
a76fd953
PI
456 if (type == 1)
457 save(state_slot);
e0c692d9 458 return;
459 case 20:
a76fd953 460 if (type == 1)
bbd471af
B
461 emu_load_state(state_slot);
462 return;
463 case 21:
a76fd953
PI
464 if (type == 1)
465 change_slot(1);
bbd471af
B
466 return;
467 case 22:
a76fd953
PI
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 }
e0c692d9 478 return;
479 }
480
a76fd953
PI
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) {
e0c692d9 515 if (psxkey1 >= 0)
54b4a001 516 in_keystate |= 1 << psxkey1;
e0c692d9 517 if (psxkey2 >= 0)
54b4a001 518 in_keystate |= 1 << psxkey2;
a76fd953
PI
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 }
b7b2fb41 535 }
a76fd953
PI
536 }
537 else if (type == 2) {
e0c692d9 538 if (psxkey1 >= 0)
54b4a001 539 in_keystate &= ~(1 << psxkey1);
e0c692d9 540 if (psxkey2 >= 0)
54b4a001 541 in_keystate &= ~(1 << psxkey2);
8f892648 542
a76fd953
PI
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 }
8f892648 555 emu_set_action(SACTION_NONE);
b7b2fb41 556 }
a76fd953 557 }
b7b2fb41 558}
b7b2fb41 559
e0c692d9 560void plat_finish()
b7b2fb41 561{
e0c692d9 562 hildon_quit();
b7b2fb41 563}
b7b2fb41 564
a76fd953 565void set_accel_multipliers()
b7b2fb41 566{
a76fd953
PI
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>
572int 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
b7b2fb41 580 FILE* pFile;
a76fd953
PI
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
b7b2fb41
B
588 int ch;
589 int i=0;
590 for (i=0;i<65536;i++)
a76fd953 591 keymap[i]=-1;
b7b2fb41 592 if (NULL != pFile) {
a76fd953 593 for(i=0;i<24;i++){
b7b2fb41
B
594 fscanf(pFile, "%i",&ch);
595 keymap[ch]=i;
a76fd953
PI
596 if (i < 4)
597 direction_keys[i] = ch;
b7b2fb41
B
598 }
599 fclose(pFile);
600 }
b7b2fb41 601
a76fd953
PI
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;
bbd471af
B
613 }
614
a76fd953
PI
615 if (in_enable_vibration)
616 printf("Vibration enabled\n");
617
618 if (!(g_maemo_opts&8)){
b7b2fb41
B
619 gtk_init (argc, argv);
620
621 window = hildon_stackable_window_new ();
622 gtk_widget_realize (window);
623 gtk_window_fullscreen (GTK_WINDOW(window));
a76fd953
PI
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
b7b2fb41
B
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());
d64280d6 642 if (g_maemo_opts & 2)
bbd471af
B
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 );
b7b2fb41
B
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));
a76fd953
PI
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 }
6469a8c4 661
a76fd953
PI
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 }
4ea7de6a 668
669 pl_rearmed_cbs.only_16bpp = 1;
a76fd953
PI
670 return 0;
671}
672
673void 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
687void menu_loop(void)
688{
b7b2fb41
B
689}
690
6469a8c4 691void *plat_gvideo_set_mode(int *w_, int *h_, int *bpp_)
b7b2fb41 692{
6469a8c4 693 int w = *w_, h = *h_;
694
a76fd953
PI
695 if (g_maemo_opts&8) return pl_vout_buf;
696 //printf("Setting video mode %ix%i\n", w, h);
697
b7b2fb41 698 if (w <= 0 || h <= 0)
76f7048e 699 return pl_vout_buf;
b7b2fb41
B
700
701 if (image) gdk_image_destroy(image);
702 image = gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), w, h );
703
76f7048e 704 pl_vout_buf = (void *) image->mem;
b7b2fb41
B
705
706 gtk_image_set_from_image (GTK_IMAGE(drawing), image, NULL);
707
708 gtk_window_resize (GTK_WINDOW (actor), w, h);
d64280d6 709 if (g_maemo_opts & 2)
bbd471af
B
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,
b7b2fb41
B
716 (gdouble)D_WIDTH / (gdouble)w,
717 (gdouble)D_HEIGHT / (gdouble)h
bbd471af
B
718 );
719 pl_buf_w=w;pl_buf_h=h;
76f7048e 720 return pl_vout_buf;
b7b2fb41
B
721}
722
6469a8c4 723void *plat_gvideo_flip(void)
b7b2fb41 724{
a76fd953
PI
725 if (!(g_maemo_opts&8))
726 gtk_widget_queue_draw(drawing);
e0c692d9 727
bbd471af 728 // process accelometer
d64280d6 729 if (g_maemo_opts & 4) {
a76fd953 730 float x, y, z;
bbd471af
B
731 FILE* f = fopen( "/sys/class/i2c-adapter/i2c-3/3-001d/coord", "r" );
732 if( !f ) {printf ("err in accel"); exit(1);}
a76fd953 733 fscanf( f, "%f %f %f", &x, &y, &z );
bbd471af
B
734 fclose( f );
735
a76fd953
PI
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;
d64280d6 765 else {in_keystate &= ~(1 << DKEY_LEFT);in_keystate &= ~(1 << DKEY_RIGHT);}
bbd471af 766
a76fd953
PI
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;
d64280d6 770 else {in_keystate &= ~(1 << DKEY_DOWN);in_keystate &= ~(1 << DKEY_UP);}
a76fd953 771 }
bbd471af
B
772 }
773
7010034e 774 return pl_vout_buf;
775}
776
777// for frontend/plugin_lib.c
778void update_input(void)
779{
a76fd953
PI
780 if (g_maemo_opts & 8)
781 maemo_x11_update_keys();
7010034e 782 else {
783 /* process GTK+ events */
784 while (gtk_events_pending())
785 gtk_main_iteration();
a76fd953 786 }
b7b2fb41
B
787}
788
a76fd953 789int omap_enable_layer(int enabled)
b7b2fb41 790{
a76fd953 791 return 0;
b7b2fb41
B
792}
793
a76fd953 794void menu_notify_mode_change(int w, int h, int bpp)
29a8c4f3 795{
29a8c4f3 796}
797
88d6397d 798void *plat_prepare_screenshot(int *w, int *h, int *bpp)
799{
800 return NULL;
801}
802
a76fd953
PI
803void plat_step_volume(int is_up)
804{
805}
806
d71c7095 807void plat_trigger_vibrate(int pad, int low, int high)
b944a30e 808{
a76fd953
PI
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);
d71c7095 819 // FIXME: somebody with hardware should tune this
820 int speed = high; // is_strong ? 200 : 150;
a76fd953
PI
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 }
b944a30e 830}
a805c855 831
a76fd953
PI
832void plat_minimize(void)
833{
834}
835
836void plat_gvideo_close(void)
837{
838}
839
840void plat_gvideo_open(int is_pal)
a805c855 841{
842}