|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2014 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 |
|
32 #include "../../ucx/list.h" |
|
33 #include "types.h" |
|
34 |
|
35 UiObserver* ui_observer_new(ui_callback f, void *data) { |
|
36 UiObserver *observer = malloc(sizeof(UiObserver)); |
|
37 observer->callback = f; |
|
38 observer->data = data; |
|
39 observer->next = NULL; |
|
40 return observer; |
|
41 } |
|
42 |
|
43 UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer) { |
|
44 if(!list) { |
|
45 return observer; |
|
46 } else { |
|
47 UiObserver *l = list; |
|
48 while(l->next) { |
|
49 l = l->next; |
|
50 } |
|
51 l->next = observer; |
|
52 return list; |
|
53 } |
|
54 } |
|
55 |
|
56 UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data) { |
|
57 UiObserver *observer = ui_observer_new(f, data); |
|
58 return ui_obsvlist_add(list, observer); |
|
59 } |
|
60 |
|
61 void ui_notify(UiObserver *observer, void *data) { |
|
62 ui_notify_except(observer, NULL, data); |
|
63 } |
|
64 |
|
65 void ui_notify_except(UiObserver *observer, UiObserver *exc, void *data) { |
|
66 while(observer) { |
|
67 if(observer != exc) { |
|
68 observer->callback(data, observer->data); |
|
69 } |
|
70 observer = observer->next; |
|
71 } |
|
72 } |
|
73 |
|
74 /* --------------------------- UiList --------------------------- */ |
|
75 |
|
76 UiList* ui_list_new() { |
|
77 UiList *list = malloc(sizeof(UiList)); |
|
78 list->first = ui_list_first; |
|
79 list->next = ui_list_next; |
|
80 list->get = ui_list_get; |
|
81 list->count = ui_list_count; |
|
82 list->observers = NULL; |
|
83 |
|
84 list->data = NULL; |
|
85 list->iter = NULL; |
|
86 |
|
87 return list; |
|
88 } |
|
89 |
|
90 void ui_list_free(UiList *list) { |
|
91 ucx_list_free(list->data); |
|
92 free(list); |
|
93 } |
|
94 |
|
95 void* ui_list_first(UiList *list) { |
|
96 UcxList *elm = list->data; |
|
97 list->iter = elm; |
|
98 return elm ? elm->data : NULL; |
|
99 } |
|
100 |
|
101 void* ui_list_next(UiList *list) { |
|
102 UcxList *elm = list->iter; |
|
103 if(elm) { |
|
104 elm = elm->next; |
|
105 if(elm) { |
|
106 list->iter = elm; |
|
107 return elm->data; |
|
108 } |
|
109 } |
|
110 return NULL; |
|
111 } |
|
112 |
|
113 void* ui_list_get(UiList *list, int i) { |
|
114 UcxList *elm = ucx_list_get(list->data, i); |
|
115 if(elm) { |
|
116 list->iter = elm; |
|
117 return elm->data; |
|
118 } else { |
|
119 return NULL; |
|
120 } |
|
121 } |
|
122 |
|
123 int ui_list_count(UiList *list) { |
|
124 UcxList *elm = list->data; |
|
125 return (int)ucx_list_size(elm); |
|
126 } |
|
127 |
|
128 void ui_list_append(UiList *list, void *data) { |
|
129 list->data = ucx_list_append(list->data, data); |
|
130 } |
|
131 |
|
132 void ui_list_prepend(UiList *list, void *data) { |
|
133 list->data = ucx_list_prepend(list->data, data); |
|
134 } |
|
135 |
|
136 void ui_list_addobsv(UiList *list, ui_callback f, void *data) { |
|
137 list->observers = ui_add_observer(list->observers, f, data); |
|
138 } |
|
139 |
|
140 void ui_list_notify(UiList *list) { |
|
141 ui_notify(list->observers, list); |
|
142 } |