File: mapid.c

package info (click to toggle)
slashem 0.0.7E7F2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 23,456 kB
  • ctags: 22,409
  • sloc: ansic: 263,134; cpp: 7,180; yacc: 2,154; sh: 737; lex: 440; makefile: 150; awk: 97; sed: 11
file content (158 lines) | stat: -rw-r--r-- 4,223 bytes parent folder | download | duplicates (10)
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
/* $Id: mapid.c,v 1.2 2001/12/24 07:56:33 j_ali Exp $ */
/* Copyright (c) Slash'EM Development Team 2001-2002 */
/* NetHack may be freely redistributed.  See license for details. */

#include "hack.h"
#include "nhxdr.h"
#include "winproxy.h"

/*
 * [ALI] It would be far too much work to pass generic identifiers to
 * plug-in window ports (and also pointless). Instead we substitute
 * integers. We keep a record of the mapping so that we can reverse
 * this in the reply.
 * Note: zero is a special value and must be preserved. Window ports
 * should treat all other values as opaque.
 */

struct mapid__window {
    winid id;
    int no_identifiers;
    anything *identifiers;
};
static int mapid__no_windows;
static struct mapid__window *mapid__windows;

/*
 * Get the index into mapid__windows for window id, adding an
 * entry if necessary.
 */

static int mapid__add_winid(id)
winid id;
{
    int i;
    if (id == WIN_ERR)
	panic("mapid__add_winid: Bad window ID");
    for(i = 0; i < mapid__no_windows; i++)
	if (mapid__windows[i].id == id)
	    return i;
    for(i = 0; i < mapid__no_windows; i++)
	if (mapid__windows[i].id == WIN_ERR)
	    break;
    if (i == mapid__no_windows) {
	if (mapid__no_windows++)
	    mapid__windows = (struct mapid__window *)realloc(mapid__windows, 
	      mapid__no_windows * sizeof(*mapid__windows));
	else
	    mapid__windows = (struct mapid__window *)malloc(sizeof(*mapid__windows));
	if (!mapid__windows)
	    panic("proxy: can't get %d bytes",
	      mapid__no_windows * sizeof(*mapid__windows));
    }
    mapid__windows[i].id = id;
    mapid__windows[i].no_identifiers = 0;
    mapid__windows[i].identifiers = (anything *)0;
    return i;
}

/*
 * Scrap any entry in mapid__windows for window id.
 */

void mapid_del_winid(id)
winid id;
{
    int i;
    if (id == WIN_ERR)
	panic("mapid_del_winid: Bad window ID");
    for(i = 0; i < mapid__no_windows; i++)
	if (mapid__windows[i].id == id) {
	    if (mapid__windows[i].no_identifiers)
		free(mapid__windows[i].identifiers);
	    mapid__windows[i].id = WIN_ERR;
	    break;
	}
}

/*
 * Map identifier into an integer which can be used to unmap back to
 * the same identifier later. Zero is treated specially and will always
 * map to zero (and non-zero identifiers will never map to zero).
 */

int mapid_map_identifier(id, identifier)
winid id;
const anything *identifier;
{
    int i, j;
    if (identifier->a_void == 0)
	return 0;
    i = mapid__add_winid(id);
    if (j = mapid__windows[i].no_identifiers++)
	mapid__windows[i].identifiers =
	  (anything *)realloc(mapid__windows[i].identifiers, 
	  mapid__windows[i].no_identifiers * sizeof(*mapid__windows->identifiers));
    else
	mapid__windows[i].identifiers =
	  (anything *)malloc(sizeof(*mapid__windows->identifiers));
    if (!mapid__windows[i].identifiers)
	panic("proxy: can't get %d bytes",
	  mapid__windows[i].no_identifiers * sizeof(*mapid__windows->identifiers));
    mapid__windows[i].identifiers[j] = *identifier;
    return j + 1;
}

/*
 * Retrieve the identifier from the mapping.
 */

void mapid_unmap_identifier(id, mapping, identifier)
winid id;
int mapping;
anything *identifier;
{
    int i;
    if (mapping == 0) {
	identifier->a_void = 0;
	return;
    }
    if (id == WIN_ERR)
	panic("mapid_unmap_identifier: Bad window ID");
    for(i = 0; i < mapid__no_windows; i++)
	if (mapid__windows[i].id == id)
	    break;
    if (i == mapid__no_windows)
    {
	impossible("Ext: Trying to unmap on an unopened window?");
	identifier->a_void = 0;
	return;
    }
    if (mapping < 1 || mapping > mapid__windows[i].no_identifiers)
    {
	impossible("Bad identifier returned from plug-in.");
	identifier->a_void = 0;
	return;
    }
    *identifier = mapid__windows[i].identifiers[mapping - 1];
}

/*
 * Discard all previously mapped identifiers for window.
 */

void mapid_del_identifiers(id)
winid id;
{
    int i;
    if (id == WIN_ERR)
	panic("mapid_del_indentifiers: Bad window ID");
    for(i = 0; i < mapid__no_windows; i++)
	if (mapid__windows[i].id == id) {
	    if (mapid__windows[i].no_identifiers)
		free(mapid__windows[i].identifiers);
	    mapid__windows[i].identifiers = (anything *)0;
	    mapid__windows[i].no_identifiers = 0;
	    break;
	}
}