| 
									
										
										
										
											2007-11-18 14:36:08 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Gamepad style buttons connected to IRQ/GPIO lines | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2007 CodeSourcery. | 
					
						
							|  |  |  |  * Written by Paul Brook | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2011-06-26 12:21:35 +10:00
										 |  |  |  * This code is licensed under the GPL. | 
					
						
							| 
									
										
										
										
											2007-11-18 14:36:08 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 18:17:05 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-12 18:54:11 +02:00
										 |  |  | #include "hw/input/gamepad.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:42 +02:00
										 |  |  | #include "hw/irq.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:45 +02:00
										 |  |  | #include "migration/vmstate.h"
 | 
					
						
							| 
									
										
										
										
											2012-11-28 12:06:30 +01:00
										 |  |  | #include "ui/console.h"
 | 
					
						
							| 
									
										
										
										
											2007-11-18 14:36:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     qemu_irq irq; | 
					
						
							|  |  |  |     int keycode; | 
					
						
							| 
									
										
										
										
											2010-12-02 02:36:38 +01:00
										 |  |  |     uint8_t pressed; | 
					
						
							| 
									
										
										
										
											2007-11-18 14:36:08 +00:00
										 |  |  | } gamepad_button; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     gamepad_button *buttons; | 
					
						
							|  |  |  |     int num_buttons; | 
					
						
							|  |  |  |     int extension; | 
					
						
							|  |  |  | } gamepad_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void stellaris_gamepad_put_key(void * opaque, int keycode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     gamepad_state *s = (gamepad_state *)opaque; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     int down; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (keycode == 0xe0 && !s->extension) { | 
					
						
							|  |  |  |         s->extension = 0x80; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     down = (keycode & 0x80) == 0; | 
					
						
							|  |  |  |     keycode = (keycode & 0x7f) | s->extension; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < s->num_buttons; i++) { | 
					
						
							|  |  |  |         if (s->buttons[i].keycode == keycode | 
					
						
							|  |  |  |                 && s->buttons[i].pressed != down) { | 
					
						
							|  |  |  |             s->buttons[i].pressed = down; | 
					
						
							|  |  |  |             qemu_set_irq(s->buttons[i].irq, down); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s->extension = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-02 02:36:38 +01:00
										 |  |  | static const VMStateDescription vmstate_stellaris_button = { | 
					
						
							|  |  |  |     .name = "stellaris_button", | 
					
						
							|  |  |  |     .version_id = 0, | 
					
						
							|  |  |  |     .minimum_version_id = 0, | 
					
						
							| 
									
										
										
										
											2014-05-13 16:09:35 +01:00
										 |  |  |     .fields = (VMStateField[]) { | 
					
						
							| 
									
										
										
										
											2010-12-02 02:36:38 +01:00
										 |  |  |         VMSTATE_UINT8(pressed, gamepad_button), | 
					
						
							|  |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2008-07-02 16:48:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-02 02:36:38 +01:00
										 |  |  | static const VMStateDescription vmstate_stellaris_gamepad = { | 
					
						
							|  |  |  |     .name = "stellaris_gamepad", | 
					
						
							| 
									
										
										
										
											2019-07-26 15:40:28 +01:00
										 |  |  |     .version_id = 2, | 
					
						
							|  |  |  |     .minimum_version_id = 2, | 
					
						
							| 
									
										
										
										
											2014-05-13 16:09:35 +01:00
										 |  |  |     .fields = (VMStateField[]) { | 
					
						
							| 
									
										
										
										
											2010-12-02 02:36:38 +01:00
										 |  |  |         VMSTATE_INT32(extension, gamepad_state), | 
					
						
							| 
									
										
										
										
											2019-07-26 15:40:28 +01:00
										 |  |  |         VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons, gamepad_state, | 
					
						
							|  |  |  |                                             num_buttons, | 
					
						
							|  |  |  |                                             vmstate_stellaris_button, | 
					
						
							|  |  |  |                                             gamepad_button), | 
					
						
							| 
									
										
										
										
											2010-12-02 02:36:38 +01:00
										 |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2008-07-02 16:48:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 22:45:14 +01:00
										 |  |  | /* Returns an array of 5 output slots.  */ | 
					
						
							| 
									
										
										
										
											2007-11-18 14:36:08 +00:00
										 |  |  | void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     gamepad_state *s; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												arm: Use g_new() & friends where that makes obvious sense
g_new(T, n) is neater than g_malloc(sizeof(T) * n).  It's also safer,
for two reasons.  One, it catches multiplication overflowing size_t.
Two, it returns T * rather than void *, which lets the compiler catch
more type errors.
This commit only touches allocations with size arguments of the form
sizeof(T).
Coccinelle semantic patch:
    @@
    type T;
    @@
    -g_malloc(sizeof(T))
    +g_new(T, 1)
    @@
    type T;
    @@
    -g_try_malloc(sizeof(T))
    +g_try_new(T, 1)
    @@
    type T;
    @@
    -g_malloc0(sizeof(T))
    +g_new0(T, 1)
    @@
    type T;
    @@
    -g_try_malloc0(sizeof(T))
    +g_try_new0(T, 1)
    @@
    type T;
    expression n;
    @@
    -g_malloc(sizeof(T) * (n))
    +g_new(T, n)
    @@
    type T;
    expression n;
    @@
    -g_try_malloc(sizeof(T) * (n))
    +g_try_new(T, n)
    @@
    type T;
    expression n;
    @@
    -g_malloc0(sizeof(T) * (n))
    +g_new0(T, n)
    @@
    type T;
    expression n;
    @@
    -g_try_malloc0(sizeof(T) * (n))
    +g_try_new0(T, n)
    @@
    type T;
    expression p, n;
    @@
    -g_realloc(p, sizeof(T) * (n))
    +g_renew(T, p, n)
    @@
    type T;
    expression p, n;
    @@
    -g_try_realloc(p, sizeof(T) * (n))
    +g_try_renew(T, p, n)
    @@
    type T;
    expression n;
    @@
    -(T *)g_new(T, n)
    +g_new(T, n)
    @@
    type T;
    expression n;
    @@
    -(T *)g_new0(T, n)
    +g_new0(T, n)
    @@
    type T;
    expression p, n;
    @@
    -(T *)g_renew(T, p, n)
    +g_renew(T, p, n)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-id: 1440524394-15640-1-git-send-email-armbru@redhat.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
											
										 
											2015-09-07 10:39:27 +01:00
										 |  |  |     s = g_new0(gamepad_state, 1); | 
					
						
							|  |  |  |     s->buttons = g_new0(gamepad_button, n); | 
					
						
							| 
									
										
										
										
											2007-11-18 14:36:08 +00:00
										 |  |  |     for (i = 0; i < n; i++) { | 
					
						
							|  |  |  |         s->buttons[i].irq = irq[i]; | 
					
						
							|  |  |  |         s->buttons[i].keycode = keycode[i]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     s->num_buttons = n; | 
					
						
							|  |  |  |     qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s); | 
					
						
							| 
									
										
										
										
											2019-10-16 10:29:30 +08:00
										 |  |  |     vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY, | 
					
						
							|  |  |  |                      &vmstate_stellaris_gamepad, s); | 
					
						
							| 
									
										
										
										
											2007-11-18 14:36:08 +00:00
										 |  |  | } |