aboutsummaryrefslogtreecommitdiff
path: root/maemo/hildon.c
blob: 8e7635a6c09b223d7f3f3852518b0340c9718a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <hildon/hildon.h>
#include "plugin_lib.h"

#include "main.h"
#include "plat.h"
#include "../libpcsxcore/psemu_plugin_defs.h"
#include "common/readpng.h"
#include "maemo_common.h"

#define X_RES           800
#define Y_RES           480
#define D_WIDTH			640
#define D_HEIGHT		480

static GdkImage *image;
static HildonAnimationActor *actor;
static GtkWidget *window, *drawing;

static int pl_buf_w, pl_buf_h;
static int sens, y_def;
static int keymap[65536];

// map psx4m compatible keymap to PSX keys
static const unsigned char keymap2[14] = {
	DKEY_LEFT,   // 0
	DKEY_RIGHT,
	DKEY_UP,
	DKEY_DOWN,
	DKEY_CIRCLE,
	DKEY_CROSS,  // 5
	DKEY_TRIANGLE,
	DKEY_SQUARE,
	DKEY_SELECT,
	DKEY_START,
	DKEY_L2,     // 10
	DKEY_R2,
	DKEY_L1,
	DKEY_R1,
};

void hildon_quit()
{
	gtk_main_quit();
	exit(0);
}

static void
window_key_proxy(GtkWidget *widget,
		     GdkEventKey *event,
		     gpointer user_data)
{
	int key, psxkey1 = -1, psxkey2 = -1;

	key = keymap[event->hardware_keycode];
	if (key < 0)
		return;

	if (key < ARRAY_SIZE(keymap2))
		psxkey1 = keymap2[key];
	else switch (key) {
		case 14:
			hildon_quit();
			break;
		case 15:
			psxkey1 = DKEY_UP;
			psxkey2 = DKEY_LEFT;
			break;
		case 16:
			psxkey1 = DKEY_UP;
			psxkey2 = DKEY_RIGHT;
			break;
		case 17:
			psxkey1 = DKEY_DOWN;
			psxkey2 = DKEY_LEFT;
			break;
		case 18:
			psxkey1 = DKEY_DOWN;
			psxkey2 = DKEY_RIGHT;
			break;
		case 19:
			if (event->type == GDK_KEY_PRESS)
			{
				emu_save_state(state_slot);
				char buf[MAXPATHLEN];
				sprintf (buf,"/opt/maemo/usr/games/screenshots%s.%3.3d",file_name,state_slot);
				writepng(buf, image->mem, pl_buf_w,pl_buf_h);
			}
			return;
		case 20:
			if (event->type == GDK_KEY_PRESS)
				emu_load_state(state_slot);
			return;
		case 21:
			if (event->type == GDK_KEY_PRESS)
				state_slot=(state_slot<9)?state_slot+1:0;
			return;
		case 22:
			if (event->type == GDK_KEY_PRESS)
				state_slot=(state_slot>0)?state_slot-1:8;
			return;
	}

	if (event->type == GDK_KEY_PRESS) {
		if (psxkey1 >= 0)
			in_keystate |= 1 << psxkey1;
		if (psxkey2 >= 0)
			in_keystate |= 1 << psxkey2;
	}
	else if (event->type == GDK_KEY_RELEASE) {
		if (psxkey1 >= 0)
			in_keystate &= ~(1 << psxkey1);
		if (psxkey2 >= 0)
			in_keystate &= ~(1 << psxkey2);

		emu_set_action(SACTION_NONE);
	}
}

void plat_finish()
{
	hildon_quit();
}

void maemo_init(int *argc, char ***argv)
{
	FILE* pFile;
	pFile = fopen("/opt/psx4m/keys", "r"); // assume the file exists and has data
	int ch;
	int i=0;
	for (i=0;i<65536;i++)
		keymap[i]=164;
	if (NULL != pFile) {
		for(i=0;i<21;i++){
			fscanf(pFile, "%i",&ch);
			keymap[ch]=i;
		}
		fclose(pFile);
	}
	
	pFile = fopen("/opt/psx4m/config", "r");
	if (NULL != pFile) {
		fscanf(pFile, "%d %d",&sens,&y_def);
		fclose(pFile);
	} else {
		sens=150;
		y_def=500; //near 45 degrees =)
	}

	gtk_init (argc, argv);

	window = hildon_stackable_window_new ();
	gtk_widget_realize (window);
	gtk_window_fullscreen (GTK_WINDOW(window));
	g_signal_connect (G_OBJECT (window), "key-press-event",
				G_CALLBACK (window_key_proxy), NULL);
	g_signal_connect (G_OBJECT (window), "key-release-event",
				G_CALLBACK (window_key_proxy), NULL);
	g_signal_connect (G_OBJECT (window), "delete_event",
				G_CALLBACK (hildon_quit), NULL);
	gtk_widget_add_events (window,
				GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);

	actor = HILDON_ANIMATION_ACTOR (hildon_animation_actor_new());
	if (g_maemo_opts & 2)
		hildon_animation_actor_set_position (actor, 0, 0 );
	else
		hildon_animation_actor_set_position (actor, (X_RES - D_WIDTH)/2, (Y_RES - D_HEIGHT)/2 );
	hildon_animation_actor_set_parent (actor, GTK_WINDOW (window));

	drawing = gtk_image_new ();

	gtk_container_add (GTK_CONTAINER (actor), drawing);

	gtk_widget_show_all (GTK_WIDGET (actor));
	gtk_widget_show_all (GTK_WIDGET (window));

	g_layer_x = (X_RES - D_WIDTH) / 2;
	g_layer_y = (Y_RES - D_HEIGHT) / 2;
	g_layer_w = D_WIDTH, g_layer_h = D_HEIGHT;

	pl_rearmed_cbs.only_16bpp = 1;
}

void menu_loop(void)
{
}

void *plat_gvideo_set_mode(int *w_, int *h_, int *bpp_)
{
	int w = *w_, h = *h_;

	if (w <= 0 || h <= 0)
		return pl_vout_buf;

	if (image) gdk_image_destroy(image);
	image = gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), w, h );

	pl_vout_buf = (void *) image->mem;

	gtk_image_set_from_image (GTK_IMAGE(drawing), image, NULL);

	gtk_window_resize (GTK_WINDOW (actor), w, h);
	if (g_maemo_opts & 2)
		hildon_animation_actor_set_scale (actor,
				(gdouble)800 / (gdouble)w,
				(gdouble)480 / (gdouble)h
				);
	else
		hildon_animation_actor_set_scale (actor,
				(gdouble)D_WIDTH / (gdouble)w,
				(gdouble)D_HEIGHT / (gdouble)h
				);
	pl_buf_w=w;pl_buf_h=h;
	return pl_vout_buf;
}

void *plat_gvideo_flip(void)
{
	gtk_widget_queue_draw (drawing);

	// process accelometer
	if (g_maemo_opts & 4) {
		int x, y, z;
		FILE* f = fopen( "/sys/class/i2c-adapter/i2c-3/3-001d/coord", "r" );
		if( !f ) {printf ("err in accel"); exit(1);}
		fscanf( f, "%d %d %d", &x, &y, &z );
		fclose( f );

		if( x > sens ) in_keystate |= 1 << DKEY_LEFT;
		else if( x < -sens ) in_keystate |= 1 << DKEY_RIGHT;
		else {in_keystate &= ~(1 << DKEY_LEFT);in_keystate &= ~(1 << DKEY_RIGHT);}

		y+=y_def;
		if( y > sens )in_keystate |= 1 << DKEY_UP;
		else if( y < -sens ) in_keystate |= 1 << DKEY_DOWN; 
		else {in_keystate &= ~(1 << DKEY_DOWN);in_keystate &= ~(1 << DKEY_UP);}

	}

	/* process GTK+ events */
	while (gtk_events_pending())
		gtk_main_iteration();

	return pl_vout_buf;
}

void plat_gvideo_open(void)
{
}

void plat_gvideo_close(void)
{
}

void *plat_prepare_screenshot(int *w, int *h, int *bpp)
{
	return NULL;
}

void plat_step_volume(int is_up)
{
}

void plat_trigger_vibrate(int is_strong)
{
}

void plat_minimize(void)
{
}