wayland: Changed output removal in handle_surface_leave()
authorRyan C. Gordon
Thu, 28 May 2020 15:18:41 -0400
changeset 138691cd318c35a97
parent 13868 25b93f5a6cbf
child 13870 2f5b5f4b6faf
wayland: Changed output removal in handle_surface_leave()

No longer needs an extra malloc, handles unexpected cases like the same
output being listed twice.
src/video/wayland/SDL_waylandwindow.c
     1.1 --- a/src/video/wayland/SDL_waylandwindow.c	Thu May 28 14:57:10 2020 -0400
     1.2 +++ b/src/video/wayland/SDL_waylandwindow.c	Thu May 28 15:18:41 2020 -0400
     1.3 @@ -406,21 +406,22 @@
     1.4  handle_surface_leave(void *data, struct wl_surface *surface,
     1.5          struct wl_output *output) {
     1.6      SDL_WindowData *window = data;
     1.7 +    int num_outputs = 0;
     1.8      int i;
     1.9  
    1.10 -    if (window->num_outputs > 1) {
    1.11 -       struct wl_output **new_outputs = SDL_malloc((window->num_outputs - 1) * sizeof *window->outputs), **iter = new_outputs;
    1.12 -       for (i=0; i < window->num_outputs; i++) {
    1.13 -           if (window->outputs[i] != output) {
    1.14 -               *iter = window->outputs[i];
    1.15 -               iter++;
    1.16 -           }
    1.17 -       }
    1.18 -       SDL_free(window->outputs);
    1.19 -       window->outputs = new_outputs;
    1.20 -       window->num_outputs--;
    1.21 -    } else {
    1.22 -       window->num_outputs = 0;
    1.23 +    for (i = 0; i < window->num_outputs; i++) {
    1.24 +        if (window->outputs[i] == output) {  /* remove this one */
    1.25 +            if (i == (window->num_outputs-1)) {
    1.26 +                window->outputs[i] = NULL;
    1.27 +            } else {
    1.28 +                SDL_memmove(&window->outputs[i], &window->outputs[i+1], sizeof (output) * ((window->num_outputs - i) - 1));
    1.29 +            }
    1.30 +            window->num_outputs--;
    1.31 +            i--;
    1.32 +        }
    1.33 +    }
    1.34 +
    1.35 +    if (window->num_outputs == 0) {
    1.36         SDL_free(window->outputs);
    1.37         window->outputs = NULL;
    1.38      }