Use pointers-to-const where relevant

Explicitly declare const a function parameter intended to be read-only.
This commit is contained in:
Romain Vimont 2018-01-19 15:12:22 +01:00
parent 53cd59605a
commit 90c69d2c79
7 changed files with 25 additions and 26 deletions

View file

@ -28,7 +28,7 @@ void controller_destroy(struct controller *controller) {
control_event_queue_destroy(&controller->queue); control_event_queue_destroy(&controller->queue);
} }
SDL_bool controller_push_event(struct controller *controller, struct control_event *event) { SDL_bool controller_push_event(struct controller *controller, const struct control_event *event) {
SDL_bool res; SDL_bool res;
mutex_lock(controller->mutex); mutex_lock(controller->mutex);
SDL_bool was_empty = control_event_queue_is_empty(&controller->queue); SDL_bool was_empty = control_event_queue_is_empty(&controller->queue);
@ -40,7 +40,7 @@ SDL_bool controller_push_event(struct controller *controller, struct control_eve
return res; return res;
} }
static SDL_bool process_event(struct controller *controller, struct control_event *event) { static SDL_bool process_event(struct controller *controller, const struct control_event *event) {
unsigned char serialized_event[SERIALIZED_EVENT_MAX_SIZE]; unsigned char serialized_event[SERIALIZED_EVENT_MAX_SIZE];
int length = control_event_serialize(event, serialized_event); int length = control_event_serialize(event, serialized_event);
if (!length) { if (!length) {

View file

@ -24,6 +24,6 @@ void controller_stop(struct controller *controller);
void controller_join(struct controller *controller); void controller_join(struct controller *controller);
// expose simple API to hide control_event_queue // expose simple API to hide control_event_queue
SDL_bool controller_push_event(struct controller *controller, struct control_event *event); SDL_bool controller_push_event(struct controller *controller, const struct control_event *event);
#endif #endif

View file

@ -17,7 +17,7 @@ static inline void write32(Uint8 *buf, Uint32 value) {
buf[3] = value; buf[3] = value;
} }
int control_event_serialize(struct control_event *event, unsigned char *buf) { int control_event_serialize(const struct control_event *event, unsigned char *buf) {
buf[0] = event->type; buf[0] = event->type;
switch (event->type) { switch (event->type) {
case CONTROL_EVENT_TYPE_KEYCODE: case CONTROL_EVENT_TYPE_KEYCODE:
@ -53,11 +53,11 @@ int control_event_serialize(struct control_event *event, unsigned char *buf) {
} }
} }
SDL_bool control_event_queue_is_empty(struct control_event_queue *queue) { SDL_bool control_event_queue_is_empty(const struct control_event_queue *queue) {
return queue->head == queue->tail; return queue->head == queue->tail;
} }
SDL_bool control_event_queue_is_full(struct control_event_queue *queue) { SDL_bool control_event_queue_is_full(const struct control_event_queue *queue) {
return (queue->head + 1) % CONTROL_EVENT_QUEUE_SIZE == queue->tail; return (queue->head + 1) % CONTROL_EVENT_QUEUE_SIZE == queue->tail;
} }
@ -72,7 +72,7 @@ void control_event_queue_destroy(struct control_event_queue *queue) {
// nothing to do in the current implementation // nothing to do in the current implementation
} }
SDL_bool control_event_queue_push(struct control_event_queue *queue, struct control_event *event) { SDL_bool control_event_queue_push(struct control_event_queue *queue, const struct control_event *event) {
if (control_event_queue_is_full(queue)) { if (control_event_queue_is_full(queue)) {
return SDL_FALSE; return SDL_FALSE;
} }

View file

@ -51,16 +51,16 @@ struct control_event_queue {
}; };
// buf size must be at least SERIALIZED_EVENT_MAX_SIZE // buf size must be at least SERIALIZED_EVENT_MAX_SIZE
int control_event_serialize(struct control_event *event, unsigned char *buf); int control_event_serialize(const struct control_event *event, unsigned char *buf);
SDL_bool control_event_queue_init(struct control_event_queue *queue); SDL_bool control_event_queue_init(struct control_event_queue *queue);
void control_event_queue_destroy(struct control_event_queue *queue); void control_event_queue_destroy(struct control_event_queue *queue);
SDL_bool control_event_queue_is_empty(struct control_event_queue *queue); SDL_bool control_event_queue_is_empty(const struct control_event_queue *queue);
SDL_bool control_event_queue_is_full(struct control_event_queue *queue); SDL_bool control_event_queue_is_full(const struct control_event_queue *queue);
// event is copied, the queue does not use the event after the function returns // event is copied, the queue does not use the event after the function returns
SDL_bool control_event_queue_push(struct control_event_queue *queue, struct control_event *event); SDL_bool control_event_queue_push(struct control_event_queue *queue, const struct control_event *event);
SDL_bool control_event_queue_take(struct control_event_queue *queue, struct control_event *event); SDL_bool control_event_queue_take(struct control_event_queue *queue, struct control_event *event);
#endif #endif

View file

@ -117,7 +117,7 @@ static enum android_motionevent_buttons convert_mouse_buttons(Uint32 state) {
return buttons; return buttons;
} }
SDL_bool input_key_from_sdl_to_android(SDL_KeyboardEvent *from, struct control_event *to) { SDL_bool input_key_from_sdl_to_android(const SDL_KeyboardEvent *from, struct control_event *to) {
to->type = CONTROL_EVENT_TYPE_KEYCODE; to->type = CONTROL_EVENT_TYPE_KEYCODE;
if (!convert_keycode_action(from->type, &to->keycode_event.action)) { if (!convert_keycode_action(from->type, &to->keycode_event.action)) {
@ -133,7 +133,7 @@ SDL_bool input_key_from_sdl_to_android(SDL_KeyboardEvent *from, struct control_e
return SDL_TRUE; return SDL_TRUE;
} }
SDL_bool mouse_button_from_sdl_to_android(SDL_MouseButtonEvent *from, struct control_event *to) { SDL_bool mouse_button_from_sdl_to_android(const SDL_MouseButtonEvent *from, struct control_event *to) {
to->type = CONTROL_EVENT_TYPE_MOUSE; to->type = CONTROL_EVENT_TYPE_MOUSE;
if (!convert_mouse_action(from->type, &to->mouse_event.action)) { if (!convert_mouse_action(from->type, &to->mouse_event.action)) {
@ -147,7 +147,7 @@ SDL_bool mouse_button_from_sdl_to_android(SDL_MouseButtonEvent *from, struct con
return SDL_TRUE; return SDL_TRUE;
} }
SDL_bool mouse_motion_from_sdl_to_android(SDL_MouseMotionEvent *from, struct control_event *to) { SDL_bool mouse_motion_from_sdl_to_android(const SDL_MouseMotionEvent *from, struct control_event *to) {
to->type = CONTROL_EVENT_TYPE_MOUSE; to->type = CONTROL_EVENT_TYPE_MOUSE;
to->mouse_event.action = AMOTION_EVENT_ACTION_MOVE; to->mouse_event.action = AMOTION_EVENT_ACTION_MOVE;
to->mouse_event.buttons = convert_mouse_buttons(from->state); to->mouse_event.buttons = convert_mouse_buttons(from->state);
@ -157,7 +157,7 @@ SDL_bool mouse_motion_from_sdl_to_android(SDL_MouseMotionEvent *from, struct con
return SDL_TRUE; return SDL_TRUE;
} }
SDL_bool mouse_wheel_from_sdl_to_android(struct complete_mouse_wheel_event *from, struct control_event *to) { SDL_bool mouse_wheel_from_sdl_to_android(const struct complete_mouse_wheel_event *from, struct control_event *to) {
to->type = CONTROL_EVENT_TYPE_SCROLL; to->type = CONTROL_EVENT_TYPE_SCROLL;
to->scroll_event.x = from->x; to->scroll_event.x = from->x;

View file

@ -12,9 +12,9 @@ struct complete_mouse_wheel_event {
Sint32 y; Sint32 y;
}; };
SDL_bool input_key_from_sdl_to_android(SDL_KeyboardEvent *from, struct control_event *to); SDL_bool input_key_from_sdl_to_android(const SDL_KeyboardEvent *from, struct control_event *to);
SDL_bool mouse_button_from_sdl_to_android(SDL_MouseButtonEvent *from, struct control_event *to); SDL_bool mouse_button_from_sdl_to_android(const SDL_MouseButtonEvent *from, struct control_event *to);
SDL_bool mouse_motion_from_sdl_to_android(SDL_MouseMotionEvent *from, struct control_event *to); SDL_bool mouse_motion_from_sdl_to_android(const SDL_MouseMotionEvent *from, struct control_event *to);
SDL_bool mouse_wheel_from_sdl_to_android(struct complete_mouse_wheel_event *from, struct control_event *to); SDL_bool mouse_wheel_from_sdl_to_android(const struct complete_mouse_wheel_event *from, struct control_event *to);
#endif #endif

View file

@ -203,8 +203,7 @@ static inline SDL_bool prepare_for_frame(SDL_Window *window, SDL_Renderer *rende
return SDL_TRUE; return SDL_TRUE;
} }
static void update_texture(const AVFrame *frame, SDL_Texture *texture) {
static void update_texture(AVFrame *frame, SDL_Texture *texture) {
SDL_UpdateYUVTexture(texture, NULL, SDL_UpdateYUVTexture(texture, NULL,
frame->data[0], frame->linesize[0], frame->data[0], frame->linesize[0],
frame->data[1], frame->linesize[1], frame->data[1], frame->linesize[1],
@ -258,7 +257,7 @@ static SDL_bool handle_new_frame(void) {
return SDL_TRUE; return SDL_TRUE;
} }
static void handle_text_input(SDL_TextInputEvent *event) { static void handle_text_input(const SDL_TextInputEvent *event) {
struct control_event control_event; struct control_event control_event;
control_event.type = CONTROL_EVENT_TYPE_TEXT; control_event.type = CONTROL_EVENT_TYPE_TEXT;
strncpy(control_event.text_event.text, event->text, TEXT_MAX_LENGTH); strncpy(control_event.text_event.text, event->text, TEXT_MAX_LENGTH);
@ -268,7 +267,7 @@ static void handle_text_input(SDL_TextInputEvent *event) {
} }
} }
static void handle_key(SDL_KeyboardEvent *event) { static void handle_key(const SDL_KeyboardEvent *event) {
SDL_Keycode keycode = event->keysym.sym; SDL_Keycode keycode = event->keysym.sym;
SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL); SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL);
SDL_bool shift = event->keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT); SDL_bool shift = event->keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT);
@ -305,7 +304,7 @@ static void handle_key(SDL_KeyboardEvent *event) {
} }
} }
static void handle_mouse_motion(SDL_MouseMotionEvent *event) { static void handle_mouse_motion(const SDL_MouseMotionEvent *event) {
struct control_event control_event; struct control_event control_event;
if (mouse_motion_from_sdl_to_android(event, &control_event)) { if (mouse_motion_from_sdl_to_android(event, &control_event)) {
if (!controller_push_event(&controller, &control_event)) { if (!controller_push_event(&controller, &control_event)) {
@ -314,7 +313,7 @@ static void handle_mouse_motion(SDL_MouseMotionEvent *event) {
} }
} }
static void handle_mouse_button(SDL_MouseButtonEvent *event) { static void handle_mouse_button(const SDL_MouseButtonEvent *event) {
struct control_event control_event; struct control_event control_event;
if (mouse_button_from_sdl_to_android(event, &control_event)) { if (mouse_button_from_sdl_to_android(event, &control_event)) {
if (!controller_push_event(&controller, &control_event)) { if (!controller_push_event(&controller, &control_event)) {
@ -323,7 +322,7 @@ static void handle_mouse_button(SDL_MouseButtonEvent *event) {
} }
} }
static void handle_mouse_wheel(struct complete_mouse_wheel_event *event) { static void handle_mouse_wheel(const struct complete_mouse_wheel_event *event) {
struct control_event control_event; struct control_event control_event;
if (mouse_wheel_from_sdl_to_android(event, &control_event)) { if (mouse_wheel_from_sdl_to_android(event, &control_event)) {
if (!controller_push_event(&controller, &control_event)) { if (!controller_push_event(&controller, &control_event)) {