6 #include <hildon/hildon.h>
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"
19 #include <dbus/dbus.h>
26 #define CALL_SIGNAL_IF "com.nokia.csd.Call"
27 #define CALL_SIGNAL_PATH "/com/nokia/csd/call"
28 #define CALL_INCOMING_SIG "Coming"
30 #define DBUS_RULE_CALL_INCOMING "type='signal',interface='" CALL_SIGNAL_IF \
31 "',path='" CALL_SIGNAL_PATH \
32 "',member='" CALL_INCOMING_SIG "'"
34 osso_context_t* osso = NULL;
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;
45 static GdkImage *image;
46 static HildonAnimationActor *actor;
47 static GtkWidget *window, *drawing = NULL;
49 static int pl_buf_w, pl_buf_h;
51 int direction_keys[4];
53 // map psx4m compatible keymap to PSX keys
54 static const unsigned char keymap2[14] = {
81 int maemo_x11_update_keys();
82 void show_notification(char* text);
84 void change_slot(int delta)
89 else if (state_slot < 0)
92 sprintf(message,"Savestate slot: %i",state_slot + 1);
93 show_notification(message);
96 void save(int state_slot)
98 emu_save_state(state_slot);
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);
105 sprintf(message,"Saved savestate slot: %i",state_slot + 1);
106 show_notification(message);
111 if (bAutosaveOnExit){
112 show_notification("Autosaving");
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);
123 int show_confirmbox(char* text)
129 dialog = gtk_message_dialog_new (GTK_WINDOW(window),
130 GTK_DIALOG_DESTROY_WITH_PARENT,
131 GTK_MESSAGE_QUESTION,
134 gint result = gtk_dialog_run (GTK_DIALOG (dialog));
135 gtk_widget_destroy (dialog);
136 if (result == GTK_RESPONSE_YES)
142 window_button_proxy(GtkWidget *widget,
143 GdkEventButton *event,
149 switch (event->type){
150 case GDK_BUTTON_PRESS:
151 //printf("GDK_BUTTON_PRESS: x=%f y=%f\n", event->x, event->y);
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)
159 else if (press_x > 800 - sens && press_y < sens && event->x > 800 - sens && event->y < sens)
161 else if (press_x > 800 - sens && press_y > 480 - sens && event->x > 800 - sens && event->y > 480 - sens)
163 else if (press_x < sens && press_y > 480 - sens && event->x < sens && event->y > 480 - sens)
174 switch (cornerActions[corner]){
176 if (show_confirmbox("Save savestate?"))
180 if (show_confirmbox("Load savestate?"))
181 emu_load_state(state_slot);
190 if (show_confirmbox("Quit?"))
197 static void *displayThread(void *arg)
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) {
207 dbus_connection_send(system_bus, msg, NULL);
208 dbus_connection_flush(system_bus);
216 dbus_message_unref(msg);
223 void show_notification(char* text)
226 GtkWidget* banner = hildon_banner_show_information(GTK_WIDGET(window), NULL, text);
227 hildon_banner_set_timeout(HILDON_BANNER(banner), 3000);
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");
236 dbus_message_iter_init_append(msg, &args);
238 if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, ¶m)) {
239 dbus_connection_send(session_bus, msg, NULL);
240 dbus_connection_flush(session_bus);
242 dbus_message_unref(msg);
247 void show_messagebox(char* text)
253 dialog = gtk_message_dialog_new (GTK_WINDOW(window),
254 GTK_DIALOG_DESTROY_WITH_PARENT,
258 gtk_dialog_run (GTK_DIALOG (dialog));
259 gtk_widget_destroy (dialog);
262 #include <hildon/hildon-file-chooser-dialog.h>
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");
269 char currentFile[MAXPATHLEN];
270 strcpy(currentFile, GetIsoFile());
271 if (strlen(currentFile))
272 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(dialog), currentFile);
274 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), "/home/user/MyDocs/");
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);
293 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
294 char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
296 //if (strcmp(filename, currentFile)) {
298 CdromLabel[0] = '\0';
300 set_cd_image(filename);
301 if (ReloadCdromPlugin() < 0)
302 printf("Failed to load cdr plugin\n");
305 printf("Failed to open cdr plugin\n");
307 strcpy(file_name, strrchr(filename,'/'));
309 SetCdOpenCaseTime(time(NULL) + 3);
315 gtk_widget_destroy (dialog);
318 void change_multi_disc()
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);
324 GtkWidget* sw = hildon_pannable_area_new ();
325 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(window)->vbox), sw, TRUE, TRUE, 0);
327 GtkWidget* tree_view = hildon_gtk_tree_view_new (HILDON_UI_MODE_EDIT);
328 gtk_widget_set_name (tree_view, "fremantle-widget");
330 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
333 GtkListStore *store = gtk_list_store_new (1, G_TYPE_STRING);
334 for (i = 0; i < cdrIsoMultidiskCount; i++) {
337 str = g_strdup_printf ("Disc %d", i+1);
338 gtk_list_store_insert_with_values (store, NULL, i, 0, str, -1);
341 GtkTreeModel* model = GTK_TREE_MODEL (store);
343 gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), model);
344 g_object_unref (model);
346 GtkTreeSelection* selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
347 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
349 GtkCellRenderer* renderer = gtk_cell_renderer_text_new ();
350 g_object_set (renderer,
352 "weight", PANGO_WEIGHT_NORMAL,
355 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
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);
367 gtk_widget_set_size_request (tree_view, 480, 800);
368 gtk_container_add (GTK_CONTAINER (sw), tree_view);
370 hildon_dialog_add_button (HILDON_DIALOG(window), GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
372 gtk_widget_show_all (GTK_WIDGET(window));
373 gint result = gtk_dialog_run (GTK_DIALOG (window));
374 if (result == GTK_RESPONSE_ACCEPT) {
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) ;
382 cdrIsoMultidiskSelect = *i;
384 CdromLabel[0] = '\0';
387 if (CDR_open() < 0) {
388 printf("Failed to load cdr plugin\n");
392 SetCdOpenCaseTime(time(NULL) + 3);
396 gtk_widget_destroy(GTK_WIDGET(window));
399 static DBusHandlerResult on_msg_recieved(DBusConnection* connection G_GNUC_UNUSED, DBusMessage* message, void* data)
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");
408 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
412 window_key_proxy(GtkWidget *widget,
416 key_press_event(event->hardware_keycode, event->type == GDK_KEY_PRESS ? 1 : (event->type == GDK_KEY_RELEASE ? 2 : 0) );
419 int last_key_pressed = 0;
420 inline void key_press_event(int key2,int type)
422 int psxkey1 = -1, psxkey2 = -1;
423 int key=keymap[key2];
428 if (type == 1 && key2 == last_key_pressed)
430 last_key_pressed = type == 1 ? key2 : 0;
432 //printf("Key: %i %s\n", key2, type == 1 ? "Pressed" : (type == 2 ? "Released" : "Unknown"));
433 if (key < ARRAY_SIZE(keymap2)){
434 psxkey1 = keymap2[key];
445 psxkey2 = DKEY_RIGHT;
453 psxkey2 = DKEY_RIGHT;
461 emu_load_state(state_slot);
473 if (cdrIsoMultidiskCount > 1)
481 if (in_type1 == PSE_PAD_TYPE_GUNCON){
485 in_state_gun |= SACTION_GUN_A;
488 in_state_gun |= SACTION_GUN_B;
491 in_state_gun |= SACTION_GUN_TRIGGER2;
494 in_state_gun |= SACTION_GUN_TRIGGER;
497 }else if (type == 2) {
500 in_state_gun &= ~SACTION_GUN_A;
503 in_state_gun &= ~SACTION_GUN_B;
506 in_state_gun &= ~SACTION_GUN_TRIGGER2;
509 in_state_gun &= ~SACTION_GUN_TRIGGER;
516 in_keystate |= 1 << psxkey1;
518 in_keystate |= 1 << psxkey2;
520 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD){
537 else if (type == 2) {
539 in_keystate &= ~(1 << psxkey1);
541 in_keystate &= ~(1 << psxkey2);
543 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD){
555 emu_set_action(SACTION_NONE);
565 void set_accel_multipliers()
567 accelOptions.xMultiplier = 255.0 / ( (accelOptions.maxValue - accelOptions.sens) * 2.0);
568 accelOptions.yMultiplier = 255.0 / ( (accelOptions.maxValue - accelOptions.sens) * 2.0);
571 #include <gdk/gdkx.h>
572 int maemo_init(int *argc, char ***argv)
574 osso = osso_initialize("pcsxrearmed", PACKAGE_VERSION, FALSE, NULL);
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);
581 pFile = fopen(keys_config_file, "r");
583 fprintf(stderr, "Error opening keys config file %s\n", keys_config_file);
586 printf("Keys config read from %s\n", keys_config_file);
590 for (i=0;i<65536;i++)
594 fscanf(pFile, "%i",&ch);
597 direction_keys[i] = ch;
603 case PSE_PAD_TYPE_GUNCON:
604 memset(cornerActions, 0, sizeof(cornerActions));
605 printf("Controller set to GUNCON (SLPH-00034)\n");
607 case PSE_PAD_TYPE_STANDARD:
608 printf("Controller set to standard (SCPH-1080)\n");
610 case PSE_PAD_TYPE_ANALOGPAD:
611 printf("Controller set to analog (SCPH-1150)\n");
615 if (in_enable_vibration)
616 printf("Vibration enabled\n");
618 if (!(g_maemo_opts&8)){
619 gtk_init (argc, argv);
621 window = hildon_stackable_window_new ();
622 gtk_widget_realize (window);
623 gtk_window_fullscreen (GTK_WINDOW(window));
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);
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);
641 actor = HILDON_ANIMATION_ACTOR (hildon_animation_actor_new());
642 if (g_maemo_opts & 2)
643 hildon_animation_actor_set_position (actor, 0, 0 );
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));
648 drawing = gtk_image_new ();
650 gtk_container_add (GTK_CONTAINER (actor), drawing);
652 gtk_widget_show_all (GTK_WIDGET (actor));
653 gtk_widget_show_all (GTK_WIDGET (window));
655 gtk_init (argc, argv);
656 /*GdkScreen* scr = gdk_screen_get_default();
657 window = GTK_WIDGET(gdk_screen_get_root_window(scr));
659 window = GTK_WIDGET(gdk_get_default_root_window());*/
662 set_accel_multipliers();
665 if (pthread_create(&display_thread, NULL, displayThread, NULL))
666 printf("Failed to create display thread.\n");
669 pl_rearmed_cbs.only_16bpp = 1;
675 if (display_thread > 0){
677 pthread_join(display_thread, NULL);
681 osso_deinitialize(osso);
691 void *plat_gvideo_set_mode(int *w_, int *h_, int *bpp_)
693 int w = *w_, h = *h_;
695 if (g_maemo_opts&8) return pl_vout_buf;
696 //printf("Setting video mode %ix%i\n", w, h);
698 if (w <= 0 || h <= 0)
701 if (image) gdk_image_destroy(image);
702 image = gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), w, h );
704 pl_vout_buf = (void *) image->mem;
706 gtk_image_set_from_image (GTK_IMAGE(drawing), image, NULL);
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
715 hildon_animation_actor_set_scale (actor,
716 (gdouble)D_WIDTH / (gdouble)w,
717 (gdouble)D_HEIGHT / (gdouble)h
719 pl_buf_w=w;pl_buf_h=h;
723 void *plat_gvideo_flip(void)
725 if (!(g_maemo_opts&8))
726 gtk_widget_queue_draw(drawing);
728 // process accelometer
729 if (g_maemo_opts & 4) {
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 );
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;
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;
749 y += accelOptions.y_def;
750 if (y > accelOptions.maxValue) y = accelOptions.maxValue;
751 else if (y < -accelOptions.maxValue) y = -accelOptions.maxValue;
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;
761 //printf("x: %i y: %i\n", in_a1[0], in_a1[1]);
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);}
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);}
777 // for frontend/plugin_lib.c
778 void update_input(void)
780 if (g_maemo_opts & 8)
781 maemo_x11_update_keys();
783 /* process GTK+ events */
784 while (gtk_events_pending())
785 gtk_main_iteration();
789 int omap_enable_layer(int enabled)
794 void menu_notify_mode_change(int w, int h, int bpp)
798 void *plat_prepare_screenshot(int *w, int *h, int *bpp)
803 void plat_step_volume(int is_up)
807 void plat_trigger_vibrate(int pad, int low, int high)
809 const int vDuration = 10;
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");
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);
828 dbus_message_unref(msg);
832 void plat_minimize(void)
836 void plat_gvideo_close(void)
840 void plat_gvideo_open(int is_pal)