summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/PEX5/ddpex/mi/level3/miRndrPick.c
blob: e9737930c240ec9d45b8daa6a3fb1b873b203be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* $XConsortium: miRndrPick.c,v 1.10 93/09/03 15:07:35 hersh Exp $ */

/************************************************************

Copyright (c) 1992  X Consortium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.

******************************************************************/

#include "miLUT.h"
#include "ddpex3.h"
#include "PEXproto.h"
#include "PEXprotost.h"
#include "pexExtract.h"
#include "PEXErr.h"
#include "pexUtils.h"
#include "pixmap.h"
#include "windowstr.h"
#include "regionstr.h"
#include "miscstruct.h"
#include "miRender.h"
#include "miStruct.h"
#include "miStrMacro.h"
#include "miWks.h"
#include "ddpex4.h"

/* External variables used */

extern  void		mi_set_filters();
extern	void		miMatMult();
extern	ddpex3rtn	miBldViewport_xform();
extern	ddpex3rtn	miBldCC_xform();
extern	ocTableType	ParseOCTable[];
extern  void		(*DestroyOCTable[])();
extern  ocTableType	InitExecuteOCTable[];
extern  ocTableType	PickExecuteOCTable[];
extern  ocTableType	SearchExecuteOCTable[];
extern  RendTableType	RenderPrimitiveTable[];
extern  RendTableType	PickPrimitiveTable[];


/* Level III Renderer Pick Procedures */

/*++
|
|  Function Name:      CreatePseudoPickMeasure
|
|  Function Description:
|     Create a Pick Measure for Renderer Picking use 
|
|  Note(s):
|
--*/

ddpex3rtn
CreatePseudoPickMeasure( pRend)
ddRendererPtr       pRend;    /* renderer handle */
{
    register miPickMeasureStr *ppm;

    ppm = (miPickMeasureStr *) xalloc(sizeof(miPickMeasureStr));
    if (!ppm) return (BadAlloc);

    ppm->path = puCreateList(DD_PICK_PATH);
    if (!ppm->path) {
	xfree(ppm);
	return (BadAlloc);
    }

    /* initialize pointers to NULL values */
    ppm->pWks = 0;
    /* initialize type to an out of range value */
    ppm->type = -1;
    ppm->status = PEXNoPick;
    ppm->pathOrder = PEXTopFirst;
    ppm->incl_handle = 0;
    ppm->excl_handle = 0;
    ppm->devPriv = (ddPointer) NULL;

    (pRend->pickstr.pseudoPM)->deviceData = (ddPointer) ppm;
    return(Success);
}


/*++
|
|  Function Name:      ChangePseudoPickMeasure
|
|  Function Description:
|     Change a Pick Measure for Renderer Picking use 
|
|  Note(s):
|
--*/

ddpex3rtn
ChangePseudoPickMeasure( pRend, pRec)
ddRendererPtr       pRend;    /* renderer handle */
ddPickRecord       *pRec;     /* PickRecord */
{
    register miPickMeasureStr *ppm;
    
    ppm = (miPickMeasureStr *) (pRend->pickstr.pseudoPM)->deviceData;

    if (!ppm->path) {
	ppm->path = puCreateList(DD_PICK_PATH);
	if (!ppm->path) {
	    xfree(ppm);
	    return (BadAlloc);
	}
    } else {
	if (puCopyList(pRend->pickStartPath, ppm->path)) {
	    puDeleteList(ppm->path);
	    xfree(ppm);
	    return (BadAlloc);
	}
    }
    ppm->incl_handle = pRend->ns[DD_PICK_INCL_NS];
    ppm->excl_handle = pRend->ns[DD_PICK_EXCL_NS];

    if (ppm->incl_handle)
	UpdateNSRefs(   ppm->incl_handle, (diResourceHandle) NULL,
			PICK_RESOURCE, ADD);

    if (ppm->excl_handle)
	UpdateNSRefs(   ppm->excl_handle, (diResourceHandle) NULL,
			PICK_RESOURCE, ADD);

    /* now store the pick record */
    ppm->type = pRec->pickType;
    switch (ppm->type) {
	case PEXPickDeviceDC_HitBox:
	    memcpy( (char *)&(ppm->input_rec.dc_hit_box), 
		  (char *)&(pRec->hit_box.DC_HitBox), 
		   sizeof(pexPD_DC_HitBox));
            break;

	case PEXPickDeviceNPC_HitVolume:
	    memcpy( (char *)&(ppm->input_rec.npc_hit_volume), 
		  (char *)&(pRec->hit_box.NPC_HitVolume), 
		   sizeof(pexPD_NPC_HitVolume));
            break;
    }


    ppm->status = PEXNoPick;

    return(Success);
}

ddpex3rtn
EndPickOne( pRend, pBuffer, numPickElRefs, pickStatus, betterPick)
/* in */
ddRendererPtr       pRend;    /* renderer handle */
/* out */
ddBufferPtr     pBuffer;    /* list of pick element ref */
ddULONG         *numPickElRefs;
ddUSHORT        *pickStatus;
ddUSHORT        *betterPick;
{
    ddpex3rtn		err = Success;
    miPickMeasureStr    *ppm;
    int                 numbytes, i, j;
    ddPickPath          *per;
    pexPickElementRef   *dest;
    ddPickPath          *sIDpp;

    ppm = (miPickMeasureStr *) (pRend->pickstr.pseudoPM)->deviceData;
    *numPickElRefs = 0;
    *pickStatus = ppm->status;
    *betterPick = 0;

    if (ppm->status == PEXOk && ppm->path) {  /* we have a pick */

	/* send back the number of objects */
	*numPickElRefs = ppm->path->numObj;

	/* Now, tack on the list of Element Refs to the back of the reply 
	   Note that we do NOT include the length of the list. 
	   The length is found in the reply itself. 
	*/
	numbytes = sizeof(ddPickPath) * ppm->path->numObj;

	PU_CHECK_BUFFER_SIZE(pBuffer, numbytes); 
	/* Copy the Pick Path to the buffer */
	for (per = (ddPickPath*) ppm->path->pList,
	     dest = (pexPickElementRef*) pBuffer->pBuf, i=0;
	     i < ppm->path->numObj; per++, dest++, i++) {

	     /* if returned structure handle is in the sIDlist
	        then the pick was on a path below an immediate OC
	        so return the struct id the user sent over in the BeginPick
	        request, otherwise return the resource ID as normal
	     */
	     sIDpp = (ddPickPath *) (pRend->pickstr.sIDlist)->pList;
	     for (j = 0; j < (pRend->pickstr.sIDlist)->numObj; j++, sIDpp++) {
		if ((diStructHandle)(per->structure) == sIDpp->structure) {
		    /* this is CORRECT, pickid is used to store the client
		       provided structure id, yes it is a kludge...
		    */
		    dest->sid = sIDpp->pickid;
		    break;
		}
		else
		  dest->sid = ((ddStructResource*)(per->structure))->id;
	     }
	    dest->offset = per->offset;
	    dest->pickid = per->pickid;
	}

	pBuffer->dataSize = numbytes; /* tells dipex how long the reply is  */
    }

    if (ppm->path) {
	puDeleteList(ppm->path); 
	ppm->path = NULL; 
    }

    if (ppm->incl_handle)
	UpdateNSRefs(   ppm->incl_handle, (diResourceHandle) NULL,
			PICK_RESOURCE, ADD);

    if (ppm->excl_handle)
	UpdateNSRefs(   ppm->excl_handle, (diResourceHandle) NULL,
			PICK_RESOURCE, ADD);


  return(err);
}


ddpex3rtn
PickOne( pRend)
/* in */
ddRendererPtr       pRend;    /* renderer handle */
{
    ddpex3rtn		err = Success;
    ddElementRange      range;
    miStructPtr         pstruct;
    miTraverserState    trav_state;
    diPMHandle          pPM = (diPMHandle) NULL;
    ddULONG            	offset1, offset2;
    diStructHandle 	psh = pRend->pickstr.strHandle;


   pstruct = (miStructPtr) (pRend->pickstr.strHandle)->deviceData;


    /* now call the traverser to traverse this structure */
    /* set exec_str_flag */
    trav_state.exec_str_flag = ES_YES;
    trav_state.p_curr_pick_el = (ddPickPath *) NULL;
    trav_state.p_curr_sc_el = (ddElementRef *) NULL;
    trav_state.max_depth = 0;
    trav_state.pickId = 0;
    trav_state.ROCoffset = 0;
    pPM = pRend->pickstr.pseudoPM;

    offset1 = 1;
    offset2 =  MISTR_NUM_EL(pstruct);

    err = traverser(pRend, psh, offset1, offset2, pPM, NULL, &trav_state);

  return(err);
}

ddpex3rtn
EndPickAll( pRend, pBuffer)
/* in */
ddRendererPtr       pRend;    /* renderer handle */
/* out */
ddBufferPtr     pBuffer;    /* list of pick element ref */
{
    ddpex3rtn		err = Success;

    pexEndPickAllReply	*reply = (pexEndPickAllReply *)(pBuffer->pHead);
    int 		i, j, k, numbytes = 0, pbytes, numObj; 
    listofObj		*list;
    listofObj		**listofp;
    ddPickPath		*pp, *sIDpp;
    ddPointer		pplist;
    ddPickElementRef	ref;
    miPickMeasureStr    *ppm;

    ppm = (miPickMeasureStr *) (pRend->pickstr.pseudoPM)->deviceData;

    reply->numPicked = (pRend->pickstr.list)->numObj;
    reply->pickStatus = ((pRend->pickstr.list)->numObj) ?1:0;
    reply->morePicks = pRend->pickstr.more_hits;

    numObj = (pRend->pickstr.list)->numObj;
    listofp = (listofObj **)(pRend->pickstr.list)->pList;

    /* convert the pick path to a pick element ref for return */
    for (i = 0; i < numObj; i++) {
	list = listofp[0];
	pbytes = list->numObj * sizeof(ddPickElementRef);
	numbytes += pbytes + sizeof(CARD32);
	PU_CHECK_BUFFER_SIZE(pBuffer, numbytes);
	PACK_CARD32(list->numObj,  pBuffer->pBuf);
	pplist = list->pList;

	/* now convert each pick path to a pick element ref */
	/* and pack it into the reply buffer */
	for (j = 0; j < list->numObj; j++) {
	  pp = (ddPickPath *) pplist;
	  pplist = (ddPointer)(pp+1);
	  sIDpp = (ddPickPath *) (pRend->pickstr.sIDlist)->pList;
	  for (k = 0; k < (pRend->pickstr.sIDlist)->numObj; k++, sIDpp++) {
	    if ((diStructHandle)(pp->structure) == sIDpp->structure) {
		/* this is CORRECT, pickid is used to store the client
		   provided structure id, yes it is a kludge...
		*/
		ref.sid = sIDpp->pickid;
		break;
	    }
	    else
		ref.sid = ((ddStructResource *)(pp->structure))->id;
	  }
	  ref.offset = pp->offset;
	  ref.pickid = pp->pickid;
	  PACK_STRUCT(ddPickElementRef, &ref, pBuffer->pBuf);
	}

	/* remove the list from the list of list */
	puRemoveFromList( (ddPointer) &list, pRend->pickstr.list);

	/* if there are more hits when doing a server side pick all
	   save the last hit into the start path 
	*/
	if ((pRend->pickstr.more_hits == PEXMoreHits) && (i == numObj-1)
	    && (pRend->pickstr.server == DD_SERVER)) 
	    pRend->pickStartPath = list;
	else 
	    puDeleteList( list);
	
    }

    /* if there were no more hits empty the pickStartPath */
    if (pRend->pickstr.more_hits == PEXNoMoreHits) {
	PU_EMPTY_LIST(pRend->pickStartPath);
    }

    pRend->pickstr.more_hits = PEXNoMoreHits;
    pBuffer->dataSize = numbytes;

    if (ppm->incl_handle)
	UpdateNSRefs(   ppm->incl_handle, (diResourceHandle) NULL,
			PICK_RESOURCE, ADD);

    if (ppm->excl_handle)
	UpdateNSRefs(   ppm->excl_handle, (diResourceHandle) NULL,
			PICK_RESOURCE, ADD);


  return(err);
}


ddpex3rtn
PickAll( pRend)
/* in */
ddRendererPtr       pRend;    /* renderer handle */
{
    ddpex3rtn			err = Success;
    miTraverserState      	trav_state;
    ddULONG            		offset1, offset2, numberOCs;
    diStructHandle 		pstruct = 0;
    miStructPtr 		pheader;
    ddPickPath			*pp;
    diPMHandle            	pPM = (diPMHandle) NULL;
    ddpex3rtn 			ValidatePickPath();

    if (!pRend->pickStartPath)  return (PEXERR(PEXPathError));
    err = ValidatePickPath(pRend->pickStartPath);
    if (err != Success) return(err);

    /* now call the traverser to traverse this structure */
    /* set exec_str_flag */
    trav_state.exec_str_flag = ES_FOLLOW_PICK;
    trav_state.p_curr_pick_el = (ddPickPath *) pRend->pickStartPath->pList ;
    trav_state.p_curr_sc_el = (ddElementRef *) NULL;
    trav_state.max_depth = 0;
    trav_state.pickId = 0;
    trav_state.ROCoffset = 0;
    pPM = pRend->pickstr.pseudoPM;

    pp = (ddPickPath *) pRend->pickStartPath->pList ;
    pstruct = pp->structure;
    pheader = (miStructPtr) pstruct->deviceData;

    offset1 = 1;
    offset2 =  MISTR_NUM_EL(pheader);

    err = traverser(pRend, pstruct, offset1, offset2, pPM, NULL, &trav_state);

  return(err);
}

ddpex3rtn
AddPickPathToList( pRend, depth, path)
ddRendererPtr		pRend;		/* renderer handle */
int			depth;		/* pick path depth */
miPPLevel		*path;		/* the path 	   */
{
    listofObj		*list;
    int 		i, err;
    ddPickPath		*patharray;


    /* dont know what this is supposed to do */
    if ((pRend->pickstr.list)->numObj >= pRend->pickstr.max_hits) {
	pRend->pickstr.more_hits = PEXMoreHits;
	return;
    }
    else pRend->pickstr.more_hits = PEXNoMoreHits;

    /* allocate space to store path while reversing */
    patharray = (ddPickPath *)  xalloc(depth * sizeof(ddPickPath));

    /* create list to place the path into */
    list = puCreateList(DD_PICK_PATH);

    /* traverse the list from bottom up and copy into temp store */
    for (i = 0; i < depth; i++){
	patharray[i] = path->pp;	
	path = path->up;
    }

    /* now store the path from top down */
    for (i = depth-1; i >= 0; i--){
	err = puAddToList((ddPointer) &patharray[i], (ddULONG) 1, list);
	if (err != Success) return(err);
    }

    xfree(patharray);

    err = puAddToList( (ddPointer) &list, (ddULONG) 1, pRend->pickstr.list);
    if (err != Success) return(err);

    if ((pRend->pickstr.send_event) && 
	((pRend->pickstr.list)->numObj == pRend->pickstr.max_hits))
	err = PEXMaxHitsReachedNotify( pRend->pickstr.client, pRend->rendId);

    return(err);
}

ddpex3rtn
ValidatePickPath(pPath)
    listofObj      *pPath;
{
    miGenericElementPtr p_element;
    diStructHandle  pStruct, pNextStruct;
    miStructPtr     pstruct;
    ddULONG         offset;
    int             i;
    ddPickPath     *pPickPath;

    
    pPickPath = (ddPickPath *) pPath->pList;
    pNextStruct = pPickPath->structure;

    for (i = pPath->numObj; i > 0; i--, pPickPath++) {
	pStruct = pPickPath->structure;
	if (pNextStruct != pStruct) return (PEXERR(PEXPathError));

	pstruct = (miStructPtr) pStruct->deviceData;

	offset = pPickPath->offset;
	if (offset > MISTR_NUM_EL(pstruct)) return (PEXERR(PEXPathError));

	/* dont bother with the leaves */
	if (i == 1) break;

	MISTR_FIND_EL(pstruct, offset, p_element);

	if (MISTR_EL_TYPE(p_element) != PEXOCExecuteStructure)
	    return (PEXERR(PEXPathError));

	pNextStruct = (diStructHandle) MISTR_GET_EXSTR_STR(p_element);
    }
    return (Success);
}