1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "server.h"
|
---|
8 | #include "server_dispatch.h"
|
---|
9 | #include "cr_mem.h"
|
---|
10 | #include "cr_rand.h"
|
---|
11 | #include "cr_string.h"
|
---|
12 |
|
---|
13 | GLint SERVER_DISPATCH_APIENTRY
|
---|
14 | crServerDispatchWindowCreate(const char *dpyName, GLint visBits)
|
---|
15 | {
|
---|
16 | return crServerDispatchWindowCreateEx(dpyName, visBits, -1);
|
---|
17 | }
|
---|
18 |
|
---|
19 |
|
---|
20 | GLint
|
---|
21 | crServerDispatchWindowCreateEx(const char *dpyName, GLint visBits, GLint preloadWinID)
|
---|
22 | {
|
---|
23 | CRMuralInfo *mural;
|
---|
24 | GLint windowID = -1;
|
---|
25 | GLint spuWindow;
|
---|
26 | GLint dims[2];
|
---|
27 | CRCreateInfo_t *pCreateInfo;
|
---|
28 |
|
---|
29 | if (cr_server.sharedWindows) {
|
---|
30 | int pos, j;
|
---|
31 |
|
---|
32 | /* find empty position in my (curclient) windowList */
|
---|
33 | for (pos = 0; pos < CR_MAX_WINDOWS; pos++) {
|
---|
34 | if (cr_server.curClient->windowList[pos] == 0) {
|
---|
35 | break;
|
---|
36 | }
|
---|
37 | }
|
---|
38 | if (pos == CR_MAX_WINDOWS) {
|
---|
39 | crWarning("Too many windows in crserver!");
|
---|
40 | return -1;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /* Look if any other client has a window for this slot */
|
---|
44 | for (j = 0; j < cr_server.numClients; j++) {
|
---|
45 | if (cr_server.clients[j]->windowList[pos] != 0) {
|
---|
46 | /* use that client's window */
|
---|
47 | windowID = cr_server.clients[j]->windowList[pos];
|
---|
48 | cr_server.curClient->windowList[pos] = windowID;
|
---|
49 | crServerReturnValue( &windowID, sizeof(windowID) ); /* real return value */
|
---|
50 | crDebug("CRServer: client %p sharing window %d",
|
---|
51 | cr_server.curClient, windowID);
|
---|
52 | return windowID;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * Have first SPU make a new window.
|
---|
59 | */
|
---|
60 | spuWindow = cr_server.head_spu->dispatch_table.WindowCreate( dpyName, visBits );
|
---|
61 | if (spuWindow < 0) {
|
---|
62 | crServerReturnValue( &spuWindow, sizeof(spuWindow) );
|
---|
63 | return spuWindow;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* get initial window size */
|
---|
67 | cr_server.head_spu->dispatch_table.GetChromiumParametervCR(GL_WINDOW_SIZE_CR, spuWindow, GL_INT, 2, dims);
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Create a new mural for the new window.
|
---|
71 | */
|
---|
72 | mural = (CRMuralInfo *) crCalloc(sizeof(CRMuralInfo));
|
---|
73 | if (mural) {
|
---|
74 | CRMuralInfo *defaultMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, 0);
|
---|
75 | CRASSERT(defaultMural);
|
---|
76 | mural->gX = 0;
|
---|
77 | mural->gY = 0;
|
---|
78 | mural->width = dims[0];
|
---|
79 | mural->height = dims[1];
|
---|
80 |
|
---|
81 | mural->spuWindow = spuWindow;
|
---|
82 | mural->screenId = 0;
|
---|
83 | mural->bVisible = GL_FALSE;
|
---|
84 | mural->bUseFBO = GL_FALSE;
|
---|
85 |
|
---|
86 | mural->cVisibleRects = 0;
|
---|
87 | mural->pVisibleRects = NULL;
|
---|
88 | mural->bReceivedRects = GL_FALSE;
|
---|
89 |
|
---|
90 | mural->pvOutputRedirectInstance = NULL;
|
---|
91 |
|
---|
92 | /* generate ID for this new window/mural (special-case for file conns) */
|
---|
93 | if (cr_server.curClient && cr_server.curClient->conn->type == CR_FILE)
|
---|
94 | windowID = spuWindow;
|
---|
95 | else
|
---|
96 | windowID = preloadWinID<0 ? crServerGenerateID(&cr_server.idsPool.freeWindowID) : preloadWinID;
|
---|
97 | crHashtableAdd(cr_server.muralTable, windowID, mural);
|
---|
98 |
|
---|
99 | pCreateInfo = (CRCreateInfo_t *) crAlloc(sizeof(CRCreateInfo_t));
|
---|
100 | pCreateInfo->pszDpyName = dpyName ? crStrdup(dpyName) : NULL;
|
---|
101 | pCreateInfo->visualBits = visBits;
|
---|
102 | crHashtableAdd(cr_server.pWindowCreateInfoTable, windowID, pCreateInfo);
|
---|
103 |
|
---|
104 | crServerSetupOutputRedirect(mural);
|
---|
105 | }
|
---|
106 |
|
---|
107 | crDebug("CRServer: client %p created new window %d (SPU window %d)",
|
---|
108 | cr_server.curClient, windowID, spuWindow);
|
---|
109 |
|
---|
110 | if (windowID != -1 && !cr_server.bIsInLoadingState) {
|
---|
111 | int pos;
|
---|
112 | for (pos = 0; pos < CR_MAX_WINDOWS; pos++) {
|
---|
113 | if (cr_server.curClient->windowList[pos] == 0) {
|
---|
114 | cr_server.curClient->windowList[pos] = windowID;
|
---|
115 | break;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | crServerReturnValue( &windowID, sizeof(windowID) );
|
---|
121 | return windowID;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int crServerRemoveClientWindow(CRClient *pClient, GLint window)
|
---|
125 | {
|
---|
126 | int pos;
|
---|
127 |
|
---|
128 | for (pos = 0; pos < CR_MAX_WINDOWS; ++pos)
|
---|
129 | {
|
---|
130 | if (pClient->windowList[pos] == window)
|
---|
131 | {
|
---|
132 | pClient->windowList[pos] = 0;
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void SERVER_DISPATCH_APIENTRY
|
---|
141 | crServerDispatchWindowDestroy( GLint window )
|
---|
142 | {
|
---|
143 | CRMuralInfo *mural;
|
---|
144 | int32_t client;
|
---|
145 | CRClientNode *pNode;
|
---|
146 | int found=false;
|
---|
147 |
|
---|
148 | if (!window)
|
---|
149 | {
|
---|
150 | crWarning("Unexpected attempt to delete default mural, ignored!");
|
---|
151 | return;
|
---|
152 | }
|
---|
153 |
|
---|
154 | mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
|
---|
155 | if (!mural) {
|
---|
156 | crWarning("CRServer: invalid window %d passed to WindowDestroy()", window);
|
---|
157 | return;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (mural->pvOutputRedirectInstance)
|
---|
161 | {
|
---|
162 | cr_server.outputRedirect.CROREnd(mural->pvOutputRedirectInstance);
|
---|
163 | mural->pvOutputRedirectInstance = NULL;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (cr_server.currentWindow == window)
|
---|
167 | {
|
---|
168 | cr_server.currentWindow = -1;
|
---|
169 | }
|
---|
170 |
|
---|
171 | crServerRedirMuralFBO(mural, GL_FALSE);
|
---|
172 | crServerDeleteMuralFBO(mural);
|
---|
173 |
|
---|
174 | crDebug("CRServer: Destroying window %d (spu window %d)", window, mural->spuWindow);
|
---|
175 | cr_server.head_spu->dispatch_table.WindowDestroy( mural->spuWindow );
|
---|
176 |
|
---|
177 | if (cr_server.curClient)
|
---|
178 | {
|
---|
179 | if (cr_server.curClient->currentMural == mural)
|
---|
180 | {
|
---|
181 | cr_server.curClient->currentMural = NULL;
|
---|
182 | cr_server.curClient->currentWindow = -1;
|
---|
183 | }
|
---|
184 |
|
---|
185 | found = crServerRemoveClientWindow(cr_server.curClient, window);
|
---|
186 |
|
---|
187 | /*Same as with contexts, some apps destroy it not in a thread where it was created*/
|
---|
188 | if (!found)
|
---|
189 | {
|
---|
190 | for (client=0; client<cr_server.numClients; ++client)
|
---|
191 | {
|
---|
192 | if (cr_server.clients[client]==cr_server.curClient)
|
---|
193 | continue;
|
---|
194 |
|
---|
195 | found = crServerRemoveClientWindow(cr_server.clients[client], window);
|
---|
196 |
|
---|
197 | if (found) break;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (!found)
|
---|
202 | {
|
---|
203 | pNode=cr_server.pCleanupClient;
|
---|
204 |
|
---|
205 | while (pNode && !found)
|
---|
206 | {
|
---|
207 | found = crServerRemoveClientWindow(pNode->pClient, window);
|
---|
208 | pNode = pNode->next;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | CRASSERT(found);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /*Make sure this window isn't active in other clients*/
|
---|
216 | for (client=0; client<cr_server.numClients; ++client)
|
---|
217 | {
|
---|
218 | if (cr_server.clients[client]->currentMural == mural)
|
---|
219 | {
|
---|
220 | cr_server.clients[client]->currentMural = NULL;
|
---|
221 | cr_server.clients[client]->currentWindow = -1;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | pNode=cr_server.pCleanupClient;
|
---|
226 | while (pNode)
|
---|
227 | {
|
---|
228 | if (pNode->pClient->currentMural == mural)
|
---|
229 | {
|
---|
230 | pNode->pClient->currentMural = NULL;
|
---|
231 | pNode->pClient->currentWindow = -1;
|
---|
232 | }
|
---|
233 | pNode = pNode->next;
|
---|
234 | }
|
---|
235 |
|
---|
236 | crHashtableDelete(cr_server.pWindowCreateInfoTable, window, crServerCreateInfoDeleteCB);
|
---|
237 |
|
---|
238 | if (mural->pVisibleRects)
|
---|
239 | {
|
---|
240 | crFree(mural->pVisibleRects);
|
---|
241 | }
|
---|
242 | crHashtableDelete(cr_server.muralTable, window, crFree);
|
---|
243 | }
|
---|
244 |
|
---|
245 | void SERVER_DISPATCH_APIENTRY
|
---|
246 | crServerDispatchWindowSize( GLint window, GLint width, GLint height )
|
---|
247 | {
|
---|
248 | CRMuralInfo *mural;
|
---|
249 |
|
---|
250 | /* crDebug("CRServer: Window %d size %d x %d", window, width, height);*/
|
---|
251 | mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
|
---|
252 | if (!mural) {
|
---|
253 | #if EXTRA_WARN
|
---|
254 | crWarning("CRServer: invalid window %d passed to WindowSize()", window);
|
---|
255 | #endif
|
---|
256 | return;
|
---|
257 | }
|
---|
258 |
|
---|
259 | mural->width = width;
|
---|
260 | mural->height = height;
|
---|
261 |
|
---|
262 | if (cr_server.curClient && cr_server.curClient->currentMural == mural)
|
---|
263 | {
|
---|
264 | crStateGetCurrent()->buffer.width = mural->width;
|
---|
265 | crStateGetCurrent()->buffer.height = mural->height;
|
---|
266 | }
|
---|
267 |
|
---|
268 | crServerCheckMuralGeometry(mural);
|
---|
269 |
|
---|
270 | cr_server.head_spu->dispatch_table.WindowSize(mural->spuWindow, width, height);
|
---|
271 |
|
---|
272 | /* Work-around Intel driver bug */
|
---|
273 | CRASSERT(!cr_server.curClient
|
---|
274 | || !cr_server.curClient->currentMural
|
---|
275 | || cr_server.curClient->currentMural == mural);
|
---|
276 | if (cr_server.curClient && cr_server.curClient->currentMural == mural)
|
---|
277 | {
|
---|
278 | CRContextInfo * ctxInfo = cr_server.currentCtxInfo;
|
---|
279 | CRASSERT(ctxInfo);
|
---|
280 | crServerDispatchMakeCurrent(mural->spuWindow, 0, ctxInfo->CreateInfo.externalID);
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | void SERVER_DISPATCH_APIENTRY
|
---|
286 | crServerDispatchWindowPosition( GLint window, GLint x, GLint y )
|
---|
287 | {
|
---|
288 | CRMuralInfo *mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
|
---|
289 | /* crDebug("CRServer: Window %d pos %d, %d", window, x, y);*/
|
---|
290 | if (!mural) {
|
---|
291 | #if EXTRA_WARN
|
---|
292 | crWarning("CRServer: invalid window %d passed to WindowPosition()", window);
|
---|
293 | #endif
|
---|
294 | return;
|
---|
295 | }
|
---|
296 | mural->gX = x;
|
---|
297 | mural->gY = y;
|
---|
298 |
|
---|
299 | crServerCheckMuralGeometry(mural);
|
---|
300 | }
|
---|
301 |
|
---|
302 | void SERVER_DISPATCH_APIENTRY
|
---|
303 | crServerDispatchWindowVisibleRegion( GLint window, GLint cRects, GLint *pRects )
|
---|
304 | {
|
---|
305 | CRMuralInfo *mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
|
---|
306 | if (!mural) {
|
---|
307 | #if EXTRA_WARN
|
---|
308 | crWarning("CRServer: invalid window %d passed to WindowVisibleRegion()", window);
|
---|
309 | #endif
|
---|
310 | return;
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (mural->pVisibleRects)
|
---|
314 | {
|
---|
315 | crFree(mural->pVisibleRects);
|
---|
316 | mural->pVisibleRects = NULL;
|
---|
317 | }
|
---|
318 |
|
---|
319 | mural->cVisibleRects = cRects;
|
---|
320 | mural->bReceivedRects = GL_TRUE;
|
---|
321 | if (cRects)
|
---|
322 | {
|
---|
323 | mural->pVisibleRects = (GLint*) crAlloc(4*sizeof(GLint)*cRects);
|
---|
324 | if (!mural->pVisibleRects)
|
---|
325 | {
|
---|
326 | crError("Out of memory in crServerDispatchWindowVisibleRegion");
|
---|
327 | }
|
---|
328 | crMemcpy(mural->pVisibleRects, pRects, 4*sizeof(GLint)*cRects);
|
---|
329 | }
|
---|
330 |
|
---|
331 | cr_server.head_spu->dispatch_table.WindowVisibleRegion(mural->spuWindow, cRects, pRects);
|
---|
332 |
|
---|
333 | if (mural->pvOutputRedirectInstance)
|
---|
334 | {
|
---|
335 | /* @todo the code assumes that RTRECT == four GLInts. */
|
---|
336 | cr_server.outputRedirect.CRORVisibleRegion(mural->pvOutputRedirectInstance,
|
---|
337 | cRects, (RTRECT *)pRects);
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 |
|
---|
343 | void SERVER_DISPATCH_APIENTRY
|
---|
344 | crServerDispatchWindowShow( GLint window, GLint state )
|
---|
345 | {
|
---|
346 | CRMuralInfo *mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
|
---|
347 | if (!mural) {
|
---|
348 | #if EXTRA_WARN
|
---|
349 | crWarning("CRServer: invalid window %d passed to WindowShow()", window);
|
---|
350 | #endif
|
---|
351 | return;
|
---|
352 | }
|
---|
353 |
|
---|
354 | if (!mural->bUseFBO)
|
---|
355 | {
|
---|
356 | cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, state);
|
---|
357 | }
|
---|
358 |
|
---|
359 | mural->bVisible = state;
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|
363 | GLint
|
---|
364 | crServerSPUWindowID(GLint serverWindow)
|
---|
365 | {
|
---|
366 | CRMuralInfo *mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, serverWindow);
|
---|
367 | if (!mural) {
|
---|
368 | #if EXTRA_WARN
|
---|
369 | crWarning("CRServer: invalid window %d passed to crServerSPUWindowID()",
|
---|
370 | serverWindow);
|
---|
371 | #endif
|
---|
372 | return -1;
|
---|
373 | }
|
---|
374 | return mural->spuWindow;
|
---|
375 | }
|
---|