Fix changelog email address
[freerdp-ubuntu-pcb-backport.git] / client / test / freerdp.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * FreeRDP Test UI
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 #ifndef _WIN32
21 #include <unistd.h>
22 #include <pthread.h>
23 #include <sys/select.h>
24 #else
25 #include <winsock2.h>
26 #include <Windows.h>
27 #include <ws2tcpip.h>
28 #endif
29
30 #include <errno.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <freerdp/gdi/gdi.h>
34 #include <freerdp/utils/args.h>
35 #include <freerdp/utils/memory.h>
36 #include <freerdp/utils/semaphore.h>
37 #include <freerdp/utils/event.h>
38 #include <freerdp/constants.h>
39 #include <freerdp/channels/channels.h>
40 #include <freerdp/plugins/cliprdr.h>
41
42 struct tf_info
43 {
44         void* data;
45 };
46 typedef struct tf_info tfInfo;
47
48 struct tf_context
49 {
50         rdpContext _p;
51
52         tfInfo* tfi;
53 };
54 typedef struct tf_context tfContext;
55
56 freerdp_sem g_sem;
57 static int g_thread_count = 0;
58
59 struct thread_data
60 {
61         freerdp* instance;
62 };
63
64 #include <freerdp/freerdp.h>
65 #include <freerdp/utils/args.h>
66
67 void tf_context_new(freerdp* instance, rdpContext* context)
68 {
69         context->channels = freerdp_channels_new();
70 }
71
72 void tf_context_free(freerdp* instance, rdpContext* context)
73 {
74
75 }
76
77 void tf_begin_paint(rdpContext* context)
78 {
79         rdpGdi* gdi = context->gdi;
80         gdi->primary->hdc->hwnd->invalid->null = 1;
81 }
82
83 void tf_end_paint(rdpContext* context)
84 {
85         rdpGdi* gdi = context->gdi;
86
87         if (gdi->primary->hdc->hwnd->invalid->null)
88                 return;
89 }
90
91 int tf_receive_channel_data(freerdp* instance, int channelId, uint8* data, int size, int flags, int total_size)
92 {
93         return freerdp_channels_data(instance, channelId, data, size, flags, total_size);
94 }
95
96 int tf_process_plugin_args(rdpSettings* settings, const char* name, RDP_PLUGIN_DATA* plugin_data, void* user_data)
97 {
98         rdpChannels* channels = (rdpChannels*) user_data;
99
100         printf("Load plugin %s\n", name);
101         freerdp_channels_load_plugin(channels, settings, name, plugin_data);
102
103         return 1;
104 }
105
106 void tf_process_cb_monitor_ready_event(rdpChannels* channels, freerdp* instance)
107 {
108         RDP_EVENT* event;
109         RDP_CB_FORMAT_LIST_EVENT* format_list_event;
110
111         event = freerdp_event_new(RDP_EVENT_CLASS_CLIPRDR, RDP_EVENT_TYPE_CB_FORMAT_LIST, NULL, NULL);
112
113         format_list_event = (RDP_CB_FORMAT_LIST_EVENT*) event;
114         format_list_event->num_formats = 0;
115
116         freerdp_channels_send_event(channels, event);
117 }
118
119 void tf_process_channel_event(rdpChannels* channels, freerdp* instance)
120 {
121         RDP_EVENT* event;
122
123         event = freerdp_channels_pop_event(channels);
124
125         if (event)
126         {
127                 switch (event->event_type)
128                 {
129                         case RDP_EVENT_TYPE_CB_MONITOR_READY:
130                                 tf_process_cb_monitor_ready_event(channels, instance);
131                                 break;
132                         default:
133                                 printf("tf_process_channel_event: unknown event type %d\n", event->event_type);
134                                 break;
135                 }
136
137                 freerdp_event_free(event);
138         }
139 }
140
141 boolean tf_pre_connect(freerdp* instance)
142 {
143         tfInfo* tfi;
144         tfContext* context;
145         rdpSettings* settings;
146
147         context = (tfContext*) instance->context;
148         tfi = (tfInfo*) xzalloc(sizeof(tfInfo));
149         context->tfi = tfi;
150
151         settings = instance->settings;
152
153         settings->order_support[NEG_DSTBLT_INDEX] = true;
154         settings->order_support[NEG_PATBLT_INDEX] = true;
155         settings->order_support[NEG_SCRBLT_INDEX] = true;
156         settings->order_support[NEG_OPAQUE_RECT_INDEX] = true;
157         settings->order_support[NEG_DRAWNINEGRID_INDEX] = true;
158         settings->order_support[NEG_MULTIDSTBLT_INDEX] = true;
159         settings->order_support[NEG_MULTIPATBLT_INDEX] = true;
160         settings->order_support[NEG_MULTISCRBLT_INDEX] = true;
161         settings->order_support[NEG_MULTIOPAQUERECT_INDEX] = true;
162         settings->order_support[NEG_MULTI_DRAWNINEGRID_INDEX] = true;
163         settings->order_support[NEG_LINETO_INDEX] = true;
164         settings->order_support[NEG_POLYLINE_INDEX] = true;
165         settings->order_support[NEG_MEMBLT_INDEX] = true;
166         settings->order_support[NEG_MEM3BLT_INDEX] = true;
167         settings->order_support[NEG_SAVEBITMAP_INDEX] = true;
168         settings->order_support[NEG_GLYPH_INDEX_INDEX] = true;
169         settings->order_support[NEG_FAST_INDEX_INDEX] = true;
170         settings->order_support[NEG_FAST_GLYPH_INDEX] = true;
171         settings->order_support[NEG_POLYGON_SC_INDEX] = true;
172         settings->order_support[NEG_POLYGON_CB_INDEX] = true;
173         settings->order_support[NEG_ELLIPSE_SC_INDEX] = true;
174         settings->order_support[NEG_ELLIPSE_CB_INDEX] = true;
175
176         freerdp_channels_pre_connect(instance->context->channels, instance);
177
178         return true;
179 }
180
181 boolean tf_post_connect(freerdp* instance)
182 {
183         rdpGdi* gdi;
184
185         gdi_init(instance, CLRCONV_ALPHA | CLRCONV_INVERT | CLRBUF_16BPP | CLRBUF_32BPP, NULL);
186         gdi = instance->context->gdi;
187
188         instance->update->BeginPaint = tf_begin_paint;
189         instance->update->EndPaint = tf_end_paint;
190
191         freerdp_channels_post_connect(instance->context->channels, instance);
192
193         return true;
194 }
195
196 int tfreerdp_run(freerdp* instance)
197 {
198         int i;
199         int fds;
200         int max_fds;
201         int rcount;
202         int wcount;
203         void* rfds[32];
204         void* wfds[32];
205         fd_set rfds_set;
206         fd_set wfds_set;
207         rdpChannels* channels;
208
209         memset(rfds, 0, sizeof(rfds));
210         memset(wfds, 0, sizeof(wfds));
211
212         channels = instance->context->channels;
213
214         freerdp_connect(instance);
215
216         while (1)
217         {
218                 rcount = 0;
219                 wcount = 0;
220
221                 if (freerdp_get_fds(instance, rfds, &rcount, wfds, &wcount) != true)
222                 {
223                         printf("Failed to get FreeRDP file descriptor\n");
224                         break;
225                 }
226                 if (freerdp_channels_get_fds(channels, instance, rfds, &rcount, wfds, &wcount) != true)
227                 {
228                         printf("Failed to get channel manager file descriptor\n");
229                         break;
230                 }
231
232                 max_fds = 0;
233                 FD_ZERO(&rfds_set);
234
235                 for (i = 0; i < rcount; i++)
236                 {
237                         fds = (int)(long)(rfds[i]);
238
239                         if (fds > max_fds)
240                                 max_fds = fds;
241
242                         FD_SET(fds, &rfds_set);
243                 }
244
245                 if (max_fds == 0)
246                         break;
247
248                 if (select(max_fds + 1, &rfds_set, &wfds_set, NULL, NULL) == -1)
249                 {
250                         /* these are not really errors */
251                         if (!((errno == EAGAIN) ||
252                                 (errno == EWOULDBLOCK) ||
253                                 (errno == EINPROGRESS) ||
254                                 (errno == EINTR))) /* signal occurred */
255                         {
256                                 printf("tfreerdp_run: select failed\n");
257                                 break;
258                         }
259                 }
260
261                 if (freerdp_check_fds(instance) != true)
262                 {
263                         printf("Failed to check FreeRDP file descriptor\n");
264                         break;
265                 }
266                 if (freerdp_channels_check_fds(channels, instance) != true)
267                 {
268                         printf("Failed to check channel manager file descriptor\n");
269                         break;
270                 }
271                 tf_process_channel_event(channels, instance);
272         }
273
274         freerdp_channels_close(channels, instance);
275         freerdp_channels_free(channels);
276         freerdp_free(instance);
277
278         return 0;
279 }
280
281 void* thread_func(void* param)
282 {
283         struct thread_data* data;
284         data = (struct thread_data*) param;
285
286         tfreerdp_run(data->instance);
287
288         xfree(data);
289
290         pthread_detach(pthread_self());
291
292         g_thread_count--;
293
294         if (g_thread_count < 1)
295                 freerdp_sem_signal(&g_sem);
296
297         return NULL;
298 }
299
300 int main(int argc, char* argv[])
301 {
302         pthread_t thread;
303         freerdp* instance;
304         struct thread_data* data;
305         rdpChannels* channels;
306
307         freerdp_channels_global_init();
308
309         g_sem = freerdp_sem_new(1);
310
311         instance = freerdp_new();
312         instance->PreConnect = tf_pre_connect;
313         instance->PostConnect = tf_post_connect;
314         instance->ReceiveChannelData = tf_receive_channel_data;
315
316         instance->context_size = sizeof(tfContext);
317         instance->ContextNew = tf_context_new;
318         instance->ContextFree = tf_context_free;
319         freerdp_context_new(instance);
320
321         channels = instance->context->channels;
322         freerdp_parse_args(instance->settings, argc, argv, tf_process_plugin_args, channels, NULL, NULL);
323
324         data = (struct thread_data*) xzalloc(sizeof(struct thread_data));
325         data->instance = instance;
326
327         g_thread_count++;
328         pthread_create(&thread, 0, thread_func, data);
329
330         while (g_thread_count > 0)
331         {
332                 freerdp_sem_wait(g_sem);
333         }
334
335         freerdp_channels_global_uninit();
336
337         return 0;
338 }