Alexandros Frantzis | 8b6daa4 | 2020-07-09 13:20:19 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018 Simon Ser |
| 3 | * Copyright 2021 Collabora, Ltd. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 14 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 15 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 17 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 18 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | /* Implementation copied from wlroots util/signal.c file */ |
| 24 | |
| 25 | #include "signal.h" |
| 26 | |
| 27 | static void |
| 28 | handle_noop(struct wl_listener *listener, void *data) |
| 29 | { |
| 30 | /* Do nothing */ |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | weston_signal_emit_mutable(struct wl_signal *signal, void *data) |
| 35 | { |
| 36 | struct wl_listener cursor; |
| 37 | struct wl_listener end; |
| 38 | |
| 39 | /* Add two special markers: one cursor and one end marker. This way, we |
| 40 | * know that we've already called listeners on the left of the cursor |
| 41 | * and that we don't want to call listeners on the right of the end |
| 42 | * marker. The 'it' function can remove any element it wants from the |
| 43 | * list without troubles. |
| 44 | * |
| 45 | * There was a previous attempt that used to steal the whole list of |
| 46 | * listeners but then that broke wl_signal_get(). |
| 47 | * |
| 48 | * wl_list_for_each_safe tries to be safe but it fails: it works fine |
| 49 | * if the current item is removed, but not if the next one is. */ |
| 50 | wl_list_insert(&signal->listener_list, &cursor.link); |
| 51 | cursor.notify = handle_noop; |
| 52 | wl_list_insert(signal->listener_list.prev, &end.link); |
| 53 | end.notify = handle_noop; |
| 54 | |
| 55 | while (cursor.link.next != &end.link) { |
| 56 | struct wl_list *pos = cursor.link.next; |
| 57 | struct wl_listener *l = wl_container_of(pos, l, link); |
| 58 | |
| 59 | wl_list_remove(&cursor.link); |
| 60 | wl_list_insert(pos, &cursor.link); |
| 61 | |
| 62 | l->notify(l, data); |
| 63 | } |
| 64 | |
| 65 | wl_list_remove(&cursor.link); |
| 66 | wl_list_remove(&end.link); |
| 67 | } |