Debian Codegraph @

code/client/libmumblelink.c

raw symbols SCIP
1 /* libmumblelink.c -- mumble link interface
2
3 Copyright (C) 2008 Ludwig Nussel <[email protected]>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20
21 */
22
23 #ifdef WIN32
24 #include <windows.h>
25 #define uint32_t UINT32
26 #else
27 #include <unistd.h>
28 #ifdef __sun
29 #define _POSIX_C_SOURCE 199309L
30 #endif
31 #include <sys/mman.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #endif
36
37 #include <fcntl.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42
43 #include "libmumblelink.h"
44
45 #ifndef MIN
46 #define MIN(a, b) ((a)<(b)?(a):(b))
47 #endif
48
49 typedef struct
50 {
51 uint32_t uiVersion;
52 uint32_t uiTick;
53 float fAvatarPosition[3];
54 float fAvatarFront[3];
55 float fAvatarTop[3];
56 wchar_t name[256];
57 /* new in mumble 1.2 */
58 float fCameraPosition[3];
59 float fCameraFront[3];
60 float fCameraTop[3];
61 wchar_t identity[256];
62 uint32_t context_len;
63 unsigned char context[256];
64 wchar_t description[2048];
65 } LinkedMem;
66
67 static LinkedMem *lm = NULL;
68
69 #ifdef WIN32
70 static HANDLE hMapObject = NULL;
71 #else
72 static int32_t GetTickCount(void)
73 {
74 struct timeval tv;
75 gettimeofday(&tv,NULL);
76
77 return tv.tv_usec / 1000 + tv.tv_sec * 1000;
78 }
79 #endif
80
81 int mumble_link(const char* name)
82 {
83 #ifdef WIN32
84 if(lm)
85 return 0;
86
87 hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, L"MumbleLink");
88 if (hMapObject == NULL)
89 return -1;
90
91 lm = (LinkedMem *) MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkedMem));
92 if (lm == NULL) {
93 CloseHandle(hMapObject);
94 hMapObject = NULL;
95 return -1;
96 }
97 #else
98 char file[256];
99 int shmfd;
100 if(lm)
101 return 0;
102
103 snprintf(file, sizeof (file), "/MumbleLink.%d", getuid());
104 shmfd = shm_open(file, O_RDWR, S_IRUSR | S_IWUSR);
105 if(shmfd < 0) {
106 return -1;
107 }
108
109 lm = (LinkedMem *) (mmap(NULL, sizeof(LinkedMem), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd,0));
110 if (lm == (void *) (-1)) {
111 lm = NULL;
112 close(shmfd);
113 return -1;
114 }
115 close(shmfd);
116 #endif
117 memset(lm, 0, sizeof(LinkedMem));
118 mbstowcs(lm->name, name, sizeof(lm->name) / sizeof(wchar_t));
119
120 return 0;
121 }
122
123 void mumble_update_coordinates(float fPosition[3], float fFront[3], float fTop[3])
124 {
125 mumble_update_coordinates2(fPosition, fFront, fTop, fPosition, fFront, fTop);
126 }
127
128 void mumble_update_coordinates2(float fAvatarPosition[3], float fAvatarFront[3], float fAvatarTop[3],
129 float fCameraPosition[3], float fCameraFront[3], float fCameraTop[3])
130 {
131 if (!lm)
132 return;
133
134 memcpy(lm->fAvatarPosition, fAvatarPosition, sizeof(lm->fAvatarPosition));
135 memcpy(lm->fAvatarFront, fAvatarFront, sizeof(lm->fAvatarFront));
136 memcpy(lm->fAvatarTop, fAvatarTop, sizeof(lm->fAvatarTop));
137 memcpy(lm->fCameraPosition, fCameraPosition, sizeof(lm->fCameraPosition));
138 memcpy(lm->fCameraFront, fCameraFront, sizeof(lm->fCameraFront));
139 memcpy(lm->fCameraTop, fCameraTop, sizeof(lm->fCameraTop));
140 lm->uiVersion = 2;
141 lm->uiTick = GetTickCount();
142 }
143
144 void mumble_set_identity(const char* identity)
145 {
146 size_t len;
147 if (!lm)
148 return;
149 len = MIN(sizeof(lm->identity)/sizeof(wchar_t), strlen(identity)+1);
150 mbstowcs(lm->identity, identity, len);
151 }
152
153 void mumble_set_context(const unsigned char* context, size_t len)
154 {
155 if (!lm)
156 return;
157 len = MIN(sizeof(lm->context), len);
158 lm->context_len = len;
159 memcpy(lm->context, context, len);
160 }
161
162 void mumble_set_description(const char* description)
163 {
164 size_t len;
165 if (!lm)
166 return;
167 len = MIN(sizeof(lm->description)/sizeof(wchar_t), strlen(description)+1);
168 mbstowcs(lm->description, description, len);
169 }
170
171 void mumble_unlink(void)
172 {
173 if(!lm)
174 return;
175 #ifdef WIN32
176 UnmapViewOfFile(lm);
177 CloseHandle(hMapObject);
178 hMapObject = NULL;
179 #else
180 munmap(lm, sizeof(LinkedMem));
181 #endif
182 lm = NULL;
183 }
184
185 int mumble_islinked(void)
186 {
187 return lm != NULL;
188 }