Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-rail / window_list.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * RAIL Window List
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <freerdp/utils/stream.h>
21 #include <freerdp/utils/memory.h>
22
23 #include "librail.h"
24
25 #include <freerdp/rail/window_list.h>
26
27 void window_list_rewind(rdpWindowList* list)
28 {
29         list->iterator = list->head;
30 }
31
32 boolean window_list_has_next(rdpWindowList* list)
33 {
34         if (list->iterator != NULL)
35         {
36                 if (list->iterator != NULL)
37                         return true;
38         }
39
40         return false;
41 }
42
43 rdpWindow* window_list_get_next(rdpWindowList* list)
44 {
45         rdpWindow* next = NULL;
46
47         if (list->iterator != NULL)
48         {
49                 next = list->iterator;
50                 list->iterator = list->iterator->next;
51         }
52
53         return next;
54 }
55
56 rdpWindow* window_list_get_by_extra_id(rdpWindowList* list, void* extraId)
57 {
58         rdpWindow* window;
59
60         window = list->head;
61
62         if (window == NULL)
63                 return NULL;
64
65         while (window != NULL)
66         {
67                 if (window->extraId == extraId)
68                         return window;
69
70                 window = window->next;
71         }
72
73         return NULL;
74 }
75
76 rdpWindow* window_list_get_by_id(rdpWindowList* list, uint32 windowId)
77 {
78         rdpWindow* window;
79
80         window = list->head;
81
82         if (window == NULL)
83                 return NULL;
84
85         while (window != NULL)
86         {
87                 if (window->windowId == windowId)
88                         return window;
89
90                 window = window->next;
91         }
92
93         return NULL;
94 }
95
96 void window_list_create(rdpWindowList* list, WINDOW_ORDER_INFO* orderInfo, WINDOW_STATE_ORDER* window_state)
97 {
98         rdpWindow* window;
99
100         window = (rdpWindow*) xzalloc(sizeof(rdpWindow));
101
102         if (window == NULL)
103                 return;
104
105         window->windowId = orderInfo->windowId;
106
107         if (list->head == NULL)
108         {
109                 list->head = list->tail = window;
110                 window->prev = NULL;
111                 window->next = NULL;
112         }
113         else
114         {
115                 window->prev = list->tail;
116                 list->tail->next = window;
117                 window->next = NULL;
118                 list->tail = window;
119         }
120
121         window->windowId = orderInfo->windowId;
122
123         window_state_update(window, orderInfo, window_state);
124
125         rail_CreateWindow(list->rail, window);
126 }
127
128 void window_list_update(rdpWindowList* list, WINDOW_ORDER_INFO* orderInfo, WINDOW_STATE_ORDER* window_state)
129 {
130         rdpWindow* window;
131
132         window = window_list_get_by_id(list, orderInfo->windowId);
133
134         if (window == NULL)
135                 return;
136
137         window_state_update(window, orderInfo, window_state);
138
139         rail_UpdateWindow(list->rail, window);
140 }
141
142 void window_list_delete(rdpWindowList* list, WINDOW_ORDER_INFO* orderInfo)
143 {
144         rdpWindow* prev;
145         rdpWindow* next;
146         rdpWindow* window;
147
148         window = window_list_get_by_id(list, orderInfo->windowId);
149
150         if (window == NULL)
151                 return;
152
153         prev = window->prev;
154         next = window->next;
155
156         if (prev != NULL)
157                 prev->next = next;
158
159         if (next != NULL)
160                 next->prev = prev;
161
162         if (list->head == list->tail)
163         {
164                 list->head = list->tail = NULL;
165         }
166         else
167         {
168                 if (list->head == window)
169                         list->head = next;
170
171                 if (list->tail == window)
172                         list->tail = prev;
173         }
174
175         rail_DestroyWindow(list->rail, window);
176 }
177
178 rdpWindowList* window_list_new(rdpRail* rail)
179 {
180         rdpWindowList* list;
181
182         list = (rdpWindowList*) xzalloc(sizeof(rdpWindowList));
183
184         if (list != NULL)
185         {
186                 list->head = NULL;
187                 list->tail = NULL;
188                 list->rail = rail;
189         }
190
191         return list;
192 }
193
194 void window_list_free(rdpWindowList* list)
195 {
196         if (list != NULL)
197         {
198                 xfree(list);
199         }
200 }