1 |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Seamless windows - Virtual channel handling |
4 |
|
5 |
Copyright (C) Pierre Ossman <ossman@cendio.se> 2006 |
6 |
|
7 |
This program is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with this program; if not, write to the Free Software |
19 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
*/ |
21 |
|
22 |
#include <assert.h> |
23 |
#include <stdio.h> |
24 |
#include <stdarg.h> |
25 |
|
26 |
#include <windows.h> |
27 |
#include <wtsapi32.h> |
28 |
#include <cchannel.h> |
29 |
|
30 |
#include "vchannel.h" |
31 |
|
32 |
#define CHANNELNAME "seamrdp" |
33 |
|
34 |
static HANDLE g_mutex = NULL; |
35 |
static HANDLE g_vchannel = NULL; |
36 |
|
37 |
void |
38 |
debug(char *format, ...) |
39 |
{ |
40 |
va_list argp; |
41 |
char buf[256]; |
42 |
|
43 |
sprintf(buf, "DEBUG1,"); |
44 |
|
45 |
va_start(argp, format); |
46 |
_vsnprintf(buf + sizeof("DEBUG1,") - 1, sizeof(buf) - sizeof("DEBUG1,") + 1, format, argp); |
47 |
va_end(argp); |
48 |
|
49 |
vchannel_write(buf); |
50 |
} |
51 |
|
52 |
|
53 |
int |
54 |
vchannel_open() |
55 |
{ |
56 |
g_vchannel = WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE, |
57 |
WTS_CURRENT_SESSION, CHANNELNAME); |
58 |
|
59 |
if (g_vchannel == NULL) |
60 |
return -1; |
61 |
|
62 |
g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessChannel"); |
63 |
if (!g_mutex) { |
64 |
WTSVirtualChannelClose(g_vchannel); |
65 |
g_vchannel = NULL; |
66 |
return -1; |
67 |
} |
68 |
|
69 |
return 0; |
70 |
} |
71 |
|
72 |
void |
73 |
vchannel_close() |
74 |
{ |
75 |
if (g_mutex) |
76 |
CloseHandle(g_mutex); |
77 |
|
78 |
if (g_vchannel) |
79 |
WTSVirtualChannelClose(g_vchannel); |
80 |
|
81 |
g_mutex = NULL; |
82 |
g_vchannel = NULL; |
83 |
} |
84 |
|
85 |
int |
86 |
vchannel_is_open() |
87 |
{ |
88 |
if (g_vchannel == NULL) |
89 |
return 0; |
90 |
else |
91 |
return 1; |
92 |
} |
93 |
|
94 |
int |
95 |
vchannel_read(char *buffer, size_t length) |
96 |
{ |
97 |
return -1; |
98 |
} |
99 |
|
100 |
int |
101 |
vchannel_write(char *format, ...) |
102 |
{ |
103 |
BOOL result; |
104 |
va_list argp; |
105 |
char buf[1024]; |
106 |
int size; |
107 |
ULONG bytes_written; |
108 |
|
109 |
assert(vchannel_is_open()); |
110 |
|
111 |
va_start(argp, format); |
112 |
size = _vsnprintf(buf, sizeof(buf), format, argp); |
113 |
va_end(argp); |
114 |
|
115 |
assert(size < sizeof(buf)); |
116 |
|
117 |
WaitForSingleObject(g_mutex, INFINITE); |
118 |
result = WTSVirtualChannelWrite(g_vchannel, buf, (ULONG) strlen(buf), &bytes_written); |
119 |
result = WTSVirtualChannelWrite(g_vchannel, "\n", (ULONG) 1, &bytes_written); |
120 |
ReleaseMutex(g_mutex); |
121 |
|
122 |
if (!result) |
123 |
return -1; |
124 |
|
125 |
return bytes_written; |
126 |
} |