aboutsummaryrefslogtreecommitdiffstats
path: root/src/BluetoothAdapter.cpp
blob: 821e3c379033c46191856671b90eccafb6a7e371 (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
/*
 * Author: Petre Eftime <petre.p.eftime@intel.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * 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 AUTHORS OR COPYRIGHT HOLDERS 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.
 */

#include "generated-code.h"
#include "tinyb_utils.hpp"
#include "BluetoothAdapter.hpp"
#include "BluetoothDevice.hpp"
#include "BluetoothManager.hpp"

using namespace tinyb;

std::string BluetoothAdapter::get_class_name() const
{
    return std::string("BluetoothAdapter");
}

std::string BluetoothAdapter::get_java_class() const
{
    return std::string(JAVA_PACKAGE "/BluetoothAdapter");
}

std::string BluetoothAdapter::get_object_path() const
{
    return std::string(g_dbus_proxy_get_object_path(G_DBUS_PROXY(object)));
}

BluetoothType BluetoothAdapter::get_bluetooth_type() const
{
    return BluetoothType::ADAPTER;
}

BluetoothAdapter::BluetoothAdapter(Adapter1 *object)
{
    this->object = object;
    g_object_ref(object);
}

BluetoothAdapter::BluetoothAdapter(const BluetoothAdapter &object)
{
    BluetoothAdapter(object.object);
}

BluetoothAdapter *BluetoothAdapter::clone() const
{
    return new BluetoothAdapter(object);
}

BluetoothAdapter::~BluetoothAdapter()
{
    g_object_unref(object);
}

std::unique_ptr<BluetoothAdapter> BluetoothAdapter::make(Object *object,
        BluetoothType type, std::string *name, std::string *identifier,
        BluetoothObject *parent)
{
    Adapter1 *adapter;
    if((type == BluetoothType::NONE || type == BluetoothType::ADAPTER) &&
        (adapter = object_get_adapter1(object)) != NULL) {

        std::unique_ptr<BluetoothAdapter> p(new BluetoothAdapter(adapter));

        if ((name == nullptr || *name == p->get_name()) &&
            (identifier == nullptr || *identifier == p->get_address()) &&
            (parent == nullptr))
            return p;
    }

    return std::unique_ptr<BluetoothAdapter>();
}

std::vector<std::unique_ptr<BluetoothDevice>> BluetoothAdapter::get_devices()
{
    std::vector<std::unique_ptr<BluetoothDevice>> vector;
    GList *l, *objects = g_dbus_object_manager_get_objects(gdbus_manager);

    for (l = objects; l != NULL; l = l->next) {
        Object *object = OBJECT(l->data);

        auto p = BluetoothDevice::make(object,
            BluetoothType::DEVICE, NULL, NULL, this);
        if (p != nullptr)
            vector.push_back(std::move(p));
    }

    return vector;
}

/* D-Bus method calls: */
bool BluetoothAdapter::start_discovery ()
{
    GError *error = NULL;
    if (get_discovering() == true)
        return true;
    bool result = adapter1_call_start_discovery_sync(
        object,
        NULL,
        &error
    );
    if (error)
        g_printerr("Error: %s\n", error->message);
    return result;
}

bool BluetoothAdapter::stop_discovery ()
{
    GError *error = NULL;
    if (get_discovering() == false)
        return true;
    bool result = adapter1_call_stop_discovery_sync(
        object,
        NULL,
        &error
    );
    if (error)
        g_printerr("Error: %s\n", error->message);
    return result;
}

bool BluetoothAdapter::remove_device (
    const std::string &arg_device)
{
    GError *error = NULL;
    bool result = adapter1_call_remove_device_sync(
        object,
        arg_device.c_str(),
        NULL,
        &error
    );
    if (error)
        g_printerr("Error: %s\n", error->message);
    return result;
}



/* D-Bus property accessors: */
std::string BluetoothAdapter::get_address ()
{
    return std::string(adapter1_get_address (object));
}

std::string BluetoothAdapter::get_name ()
{
    return std::string(adapter1_get_name (object));
}

std::string BluetoothAdapter::get_alias ()
{
    return std::string(adapter1_get_alias (object));
}

void BluetoothAdapter::set_alias (const std::string &value)
{
    adapter1_set_alias (object, value.c_str());
}

unsigned int BluetoothAdapter::get_class ()
{
    return adapter1_get_class (object);
}

bool BluetoothAdapter::get_powered ()
{
    return adapter1_get_powered (object);
}

void BluetoothAdapter::set_powered (bool  value)
{
    if (get_powered() != value)
        adapter1_set_powered (object, value);
}

bool BluetoothAdapter::get_discoverable ()
{
    return adapter1_get_discoverable (object);
}

void BluetoothAdapter::set_discoverable (bool  value)
{
    adapter1_set_discoverable (object, value);
}

unsigned int BluetoothAdapter::get_discoverable_timeout ()
{
    return adapter1_get_discoverable_timeout (object);
}

void BluetoothAdapter::set_discoverable_timeout (unsigned int  value)
{
    adapter1_set_discoverable_timeout (object, value);
}

bool BluetoothAdapter::get_pairable ()
{
    return adapter1_get_pairable (object);
}

void BluetoothAdapter::set_pairable (bool  value)
{
    adapter1_set_pairable (object, value);
}

unsigned int BluetoothAdapter::get_pairable_timeout ()
{
    return adapter1_get_pairable_timeout (object);
}

void BluetoothAdapter::set_pairable_timeout (unsigned int  value)
{
    adapter1_set_pairable_timeout (object, value);
}

bool BluetoothAdapter::get_discovering ()
{
    return adapter1_get_discovering (object);
}

std::vector<std::string> BluetoothAdapter::get_uuids ()
{
    const char * const *uuids_c = adapter1_get_uuids (object);
    std::vector<std::string> uuids;
    for (int i = 0; uuids_c[i] != NULL ;i++)
        uuids.push_back(std::string(uuids_c[i]));
    return uuids;
}

std::string BluetoothAdapter::get_modalias ()
{
    return std::string(adapter1_get_modalias (object));
}
a id='n1045' href='#n1045'>1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
/* $XFree86$ */
/**************************************************************************

Copyright 2002 Tungsten Graphics Inc., Cedar Park, Texas.

All Rights Reserved.

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
on the rights to use, copy, modify, merge, publish, distribute, sub
license, 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 (including the next
paragraph) 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 NON-INFRINGEMENT. IN NO EVENT SHALL
TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS 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.

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

/*
 * Authors:
 *   Keith Whitwell <keith@tungstengraphics.com>
 */



/* Display list compiler attempts to store lists of vertices with the
 * same vertex layout.  Additionally it attempts to minimize the need
 * for execute-time fixup of these vertex lists, allowing them to be
 * cached on hardware.
 *
 * There are still some circumstances where this can be thwarted, for
 * example by building a list that consists of one very long primitive
 * (eg Begin(Triangles), 1000 vertices, End), and calling that list
 * from inside a different begin/end object (Begin(Lines), CallList,
 * End).  
 *
 * In that case the code will have to replay the list as individual
 * commands through the Exec dispatch table, or fix up the copied
 * vertices at execute-time.
 *
 * The other case where fixup is required is when a vertex attribute
 * is introduced in the middle of a primitive.  Eg:
 *  Begin(Lines)
 *  TexCoord1f()           Vertex2f()
 *  TexCoord1f() Color3f() Vertex2f()
 *  End()
 *
 *  If the current value of Color isn't known at compile-time, this
 *  primitive will require fixup.
 *
 *
 * The list compiler currently doesn't attempt to compile lists
 * containing EvalCoord or EvalPoint commands.  On encountering one of
 * these, compilation falls back to opcodes.  
 *
 * This could be improved to fallback only when a mix of EvalCoord and
 * Vertex commands are issued within a single primitive.
 */


#include "dlist.h"
#include "context.h"
#include "macros.h"
#include "api_validate.h"
#include "api_arrayelt.h"
#include "vtxfmt.h"
#include "t_save_api.h"

/*
 * NOTE: Old 'parity' issue is gone, but copying can still be
 * wrong-footed on replay.
 */
static GLuint _save_copy_vertices( GLcontext *ctx, 
				   struct tnl_vertex_list *node )
{
   TNLcontext *tnl = TNL_CONTEXT( ctx );
   struct tnl_prim *prim = &node->prim[node->prim_count-1];
   GLuint nr = prim->count;
   GLuint sz = tnl->save.vertex_size;
   GLfloat *src = node->buffer + prim->start * sz;
   GLfloat *dst = tnl->save.copied.buffer;
   GLenum mode = prim->mode & PRIM_MODE_MASK;
   GLuint ovf, i;

   _mesa_debug( 0, "%s\n", __FUNCTION__); 

   switch( mode )
   {
   case GL_POINTS:
      return 0;
   case GL_LINES:
      ovf = nr&1;
      for (i = 0 ; i < ovf ; i++)
	 memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz*sizeof(GLfloat) );
      return i;
   case GL_TRIANGLES:
      ovf = nr%3;
      for (i = 0 ; i < ovf ; i++)
	 memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz*sizeof(GLfloat) );
      return i;
   case GL_QUADS:
      ovf = nr&3;
      for (i = 0 ; i < ovf ; i++)
	 memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz*sizeof(GLfloat) );
      return i;
   case GL_LINE_STRIP:
      if (nr == 0) 
	 return 0;
      else {
	 memcpy( dst, src+(nr-1)*sz, sz*sizeof(GLfloat) );
	 return 1;
      }
   case GL_LINE_LOOP:
   case GL_TRIANGLE_FAN:
   case GL_POLYGON:
      if (nr == 0) 
	 return 0;
      else if (nr == 1) {
	 memcpy( dst, src+0, sz*sizeof(GLfloat) );
	 return 1;
      } else {
	 memcpy( dst, src+0, sz*sizeof(GLfloat) );
	 memcpy( dst+sz, src+(nr-1)*sz, sz*sizeof(GLfloat) );
	 return 2;
      }
   case GL_TRIANGLE_STRIP:
   case GL_QUAD_STRIP:
      switch (nr) {
      case 0: ovf = 0; break;
      case 1: ovf = 1; break;
      default: ovf = 2 + (nr&1); break;
      }
      for (i = 0 ; i < ovf ; i++)
	 memcpy( dst+i*sz, src+(nr-ovf+i)*sz, sz*sizeof(GLfloat) );
      return i;
   default:
      assert(0);
      return 0;
   }
}


static struct tnl_vertex_store *alloc_vertex_store( GLcontext *ctx )
{
   struct tnl_vertex_store *store = ALIGN_MALLOC( sizeof(*store), 32 );
   store->used = 0;
   store->refcount = 0;
   return store;
}

static struct tnl_primitive_store *alloc_prim_store( GLcontext *ctx )
{
   struct tnl_primitive_store *store = ALIGN_MALLOC( sizeof(*store), 32 );
   store->used = 0;
   store->refcount = 0;
   return store;
}

static void _save_reset_counters( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);

   _mesa_debug( 0, "%s\n", __FUNCTION__); 

   tnl->save.prim = tnl->save.prim_store->buffer + tnl->save.prim_store->used;
   tnl->save.buffer = (tnl->save.vertex_store->buffer + 
		       tnl->save.vertex_store->used);

   if (tnl->save.vertex_size)
      tnl->save.initial_counter = ((SAVE_BUFFER_SIZE - 
				    tnl->save.vertex_store->used) / 
				   tnl->save.vertex_size);
   else
      tnl->save.initial_counter = 0;

   if (tnl->save.initial_counter > ctx->Const.MaxArrayLockSize )
      tnl->save.initial_counter = ctx->Const.MaxArrayLockSize;

   tnl->save.counter = tnl->save.initial_counter;
   tnl->save.prim_count = 0;
   tnl->save.prim_max = SAVE_PRIM_SIZE - tnl->save.prim_store->used;
   tnl->save.copied.nr = 0;
}


/* Insert the active immediate struct onto the display list currently
 * being built.
 */
static void _save_compile_vertex_list( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct tnl_vertex_list *node;

   _mesa_debug( 0, "%s\n", __FUNCTION__); 

   if (tnl->save.initial_counter == tnl->save.counter) {
      _save_reset_counters( ctx );
      return;
   }

   /* Allocate space for this structure in the display list currently
    * being compiled.
    */
   node = (struct tnl_vertex_list *)
      _mesa_alloc_instruction(ctx, tnl->save.opcode_vertex_list, sizeof(*node));

   if (!node)
      return;

   /* Duplicate our template, increment refcounts to the storage structs:
    */
   memcpy(node->attrsz, tnl->save.attrsz, sizeof(node->attrsz));
   node->vertex_size = tnl->save.vertex_size;
   node->buffer = tnl->save.buffer;
   node->wrap_count = tnl->save.copied.nr;
   node->count = tnl->save.initial_counter - tnl->save.counter;
   node->prim = tnl->save.prim;
   node->prim_count = tnl->save.prim_count;
   node->vertex_store = tnl->save.vertex_store;
   node->prim_store = tnl->save.prim_store;

   node->vertex_store->refcount++;
   node->prim_store->refcount++;

   tnl->save.vertex_store->used += tnl->save.vertex_size * node->count;
   tnl->save.prim_store->used += node->prim_count;

   /* Decide whether the storage structs are full, or can be used for
    * the next vertex lists as well.
    */
   if (tnl->save.vertex_store->used > 
       SAVE_BUFFER_SIZE - 16 * (tnl->save.vertex_size + 4)) {

      tnl->save.vertex_store->refcount--; /* cannot be zero */
      tnl->save.vertex_store = alloc_vertex_store( ctx );
      tnl->save.vbptr = tnl->save.vertex_store->buffer;
   } 

   if (tnl->save.prim_store->used > SAVE_PRIM_SIZE - 6) {
      tnl->save.prim_store->refcount--; /* cannot be zero */
      tnl->save.prim_store = alloc_prim_store( ctx );
   } 

   /* Reset our structures for the next run of vertices:
    */
   _save_reset_counters( ctx );

   /* Copy duplicated vertices 
    */
   tnl->save.copied.nr = _save_copy_vertices( ctx, node );


   /* Deal with GL_COMPILE_AND_EXECUTE:
    */
#if 0
   if (ctx->ExecuteFlag) {
      execute_vertex_list( ctx, (void *)node );
   }
#endif
}


/* TODO -- If no new vertices have been stored, don't bother saving
 * it.
 */
static void _save_wrap_buffers( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLint i = tnl->save.prim_count - 1;
   GLenum mode;

   _mesa_debug( 0, "%s\n", __FUNCTION__); 

   assert(i < TNL_MAX_PRIM);
   assert(i >= 0);

   tnl->save.prim[i].count = ((tnl->save.initial_counter - tnl->save.counter) - 
			     tnl->save.prim[i].start);
   mode = tnl->save.prim[i].mode & ~(PRIM_BEGIN|PRIM_END);
   
   _mesa_debug( 0, "%s: mode %x\n", __FUNCTION__, mode); 

   /* store the copied vertices, and allocate a new list.
    */
   _save_compile_vertex_list( ctx );

   /* Restart interrupted primitive
    */
   tnl->save.prim[0].mode = mode;
   tnl->save.prim[0].start = 0;
   tnl->save.prim[0].count = 0;
   tnl->save.prim_count = 1;
}



/* Called only when buffers are wrapped as the result of filling the
 * vertex_store struct.  
 *
 * -- what about when be wraps?
 *       -- Don't need to copy vertices or emit fake begin/ends
 *       -- Just call _save_compile_vertex_list()
 * -- what about vertex upgrades?
 *       -- Same logic, but want to do more before emitting vertices in new
 *          format.
 */
static void _save_wrap_filled_vertex( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLfloat *data = tnl->save.copied.buffer;
   int i;

   _mesa_debug( 0, "%s\n", __FUNCTION__); 

   /* Emit a glEnd to close off the last vertex list.
    */
   _save_wrap_buffers( ctx );
   
    /* Copy stored stored vertices to start of new list.
    */
   assert(tnl->save.counter > tnl->save.copied.nr);

   for (i = 0 ; i < tnl->save.copied.nr ; i++) {
      memcpy( tnl->save.vbptr, data, tnl->save.vertex_size * sizeof(GLfloat));
      data += tnl->save.vertex_size;
      tnl->save.vbptr += tnl->save.vertex_size;
      tnl->save.counter--;
   }
}


static void _save_copy_to_current( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx); 
   GLuint i;

   _mesa_debug( 0, "%s\n", __FUNCTION__); 

   for (i = _TNL_ATTRIB_POS+1 ; i <= _TNL_ATTRIB_INDEX ; i++) {
      if (tnl->save.attrsz[i]) {
	 tnl->save.currentsz[i][0] = tnl->save.attrsz[i];
	 ASSIGN_4V(tnl->save.current[i], 0, 0, 0, 1);
	 COPY_SZ_4V(tnl->save.current[i], 
		    tnl->save.attrsz[i], 
		    tnl->save.attrptr[i]);
      }
   }

   /* Edgeflag requires special treatment:
    */
   if (tnl->save.attrsz[_TNL_ATTRIB_EDGEFLAG]) {
      ctx->ListState.ActiveEdgeFlag = 1;
      ctx->ListState.CurrentEdgeFlag = 
	 (tnl->save.attrptr[_TNL_ATTRIB_EDGEFLAG][0] == 1.0);
   }
}


static void _save_copy_from_current( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx); 
   GLint i;

   _mesa_debug( 0, "%s\n", __FUNCTION__); 

   for (i = _TNL_ATTRIB_POS+1 ; i <= _TNL_ATTRIB_INDEX ; i++) 
      switch (tnl->save.attrsz[i]) {
      case 4: tnl->save.attrptr[i][3] = tnl->save.current[i][3];
      case 3: tnl->save.attrptr[i][2] = tnl->save.current[i][2];
      case 2: tnl->save.attrptr[i][1] = tnl->save.current[i][1];
      case 1: tnl->save.attrptr[i][0] = tnl->save.current[i][0];
      case 0: break;
      }

   /* Edgeflag requires special treatment:
    */
   if (tnl->save.attrsz[_TNL_ATTRIB_EDGEFLAG]) 
      tnl->save.attrptr[_TNL_ATTRIB_EDGEFLAG][0] = 
	 (GLfloat)ctx->ListState.CurrentEdgeFlag;
}




/* Flush existing data, set new attrib size, replay copied vertices.
 */ 
static void _save_upgrade_vertex( GLcontext *ctx, 
				 GLuint attr,
				 GLuint newsz )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLuint oldsz;
   GLuint i;
   GLfloat *tmp;

   _mesa_debug( 0, "%s\n", __FUNCTION__); 


   /* Store the current run of vertices, and emit a GL_END.  Emit a
    * BEGIN in the new buffer.
    */
   _save_wrap_buffers( ctx );


   /* Do a COPY_TO_CURRENT to ensure back-copying works for the case
    * when the attribute already exists in the vertex and is having
    * its size increased.  
    */
   _save_copy_to_current( ctx );


   /* Fix up sizes:
    */
   oldsz = tnl->save.attrsz[attr];
   tnl->save.attrsz[attr] = newsz;

   tnl->save.vertex_size += newsz - oldsz;
   tnl->save.counter = ((SAVE_BUFFER_SIZE - tnl->save.vertex_store->used) / 
			tnl->save.vertex_size);
   tnl->save.initial_counter = tnl->save.counter;


   /* Recalculate all the attrptr[] values:
    */
   for (i = 0, tmp = tnl->save.vertex ; i < _TNL_ATTRIB_MAX ; i++) {
      if (tnl->save.attrsz[i]) {
	 tnl->save.attrptr[i] = tmp;
	 tmp += tnl->save.attrsz[i];
      }
      else 
	 tnl->save.attrptr[i] = 0; /* will not be dereferenced. */
   }

   /* Copy from current to repopulate the vertex with correct values.
    */
   _save_copy_from_current( ctx );


   /* Replay stored vertices to translate them to new format here.
    *
    * If there are copied vertices and the new (upgraded) attribute
    * has not been defined before, this list is somewhat degenerate,
    * and will need fixup at runtime.
    */
   if (tnl->save.copied.nr)
   {
      GLfloat *data = tnl->save.copied.buffer;
      GLfloat *dest = tnl->save.buffer;
      GLint j;

      /* Need to note this and fix up at runtime (or loopback):
       */
      if (tnl->save.currentsz[attr] == 0) {
	 assert(oldsz == 0);
	 _mesa_debug(0, "%s: dangling reference attr %d\n", 
		     __FUNCTION__, attr); 
      }

      for (i = 0 ; i < tnl->save.copied.nr ; i++) {
	 for (j = 0 ; j < _TNL_ATTRIB_MAX ; j++) {
	    if (tnl->save.attrsz[j]) {
	       if (j == attr) {
		  ASSIGN_4V( dest, 0, 0, 0, 1 );
		  COPY_SZ_4V( dest, oldsz, data );
		  data += oldsz;
		  dest += newsz;
	       }
	       else {
		  GLint sz = tnl->save.attrsz[j];
		  COPY_SZ_4V( dest, sz, data );
		  data += sz;
		  dest += sz;
	       }
	    }
	 }
      }

      tnl->save.buffer = dest;
      tnl->save.counter -= tnl->save.copied.nr;
   }
}




/* Helper function for 'CHOOSE' macro.  Do what's necessary when an
 * entrypoint is called for the first time.
 */
static void do_choose( GLuint attr, GLuint sz, 
		       void (*attr_func)( const GLfloat *),
		       void (*choose1)( const GLfloat *),
		       void (*choose2)( const GLfloat *),
		       void (*choose3)( const GLfloat *),
		       void (*choose4)( const GLfloat *),
		       const GLfloat *v )
{ 
   GET_CURRENT_CONTEXT( ctx ); 
   TNLcontext *tnl = TNL_CONTEXT(ctx); 
   static GLfloat id[4] = { 0, 0, 0, 1 };
   int i;

   _mesa_debug( 0, "%s\n", __FUNCTION__); 

   if (tnl->save.attrsz[attr] < sz) {
      /* New size is larger.  Need to flush existing vertices and get
       * an enlarged vertex format.
       */
      _save_upgrade_vertex( ctx, attr, sz );
   }
   else {
      /* New size is equal or smaller - just need to fill in some
       * zeros.
       */
      for (i = sz ; i <= tnl->save.attrsz[attr] ; i++)
	 tnl->save.attrptr[attr][i-1] = id[i-1];
   }

   /* Reset any active pointers for this attribute 
    */
   tnl->save.tabfv[attr][0] = choose1;
   tnl->save.tabfv[attr][1] = choose2;
   tnl->save.tabfv[attr][2] = choose3;
   tnl->save.tabfv[attr][3] = choose4;

   /* Update the secondary dispatch table with the new function
    */
   tnl->save.tabfv[attr][sz-1] = attr_func;

   (*attr_func)(v);
}



/* Only one size for each attribute may be active at once.  Eg. if
 * Color3f is installed/active, then Color4f may not be, even if the
 * vertex actually contains 4 color coordinates.  This is because the
 * 3f version won't otherwise set color[3] to 1.0 -- this is the job
 * of the chooser function when switching between Color4f and Color3f.
 */
#define ATTRFV( ATTR, N )			\
static void save_choose_##ATTR##_##N( const GLfloat *v );	\
							\
static void save_attrib_##ATTR##_##N( const GLfloat *v )	\
{							\
   GET_CURRENT_CONTEXT( ctx );				\
   TNLcontext *tnl = TNL_CONTEXT(ctx);			\
							\
   _mesa_debug( 0, "%s\n", __FUNCTION__);               \
							\
   if ((ATTR) == 0) {					\
      int i;						\
							\
      if (N>0) tnl->save.vbptr[0] = v[0];		\
      if (N>1) tnl->save.vbptr[1] = v[1];		\
      if (N>2) tnl->save.vbptr[2] = v[2];		\
      if (N>3) tnl->save.vbptr[3] = v[3];		\
							\
      for (i = N; i < tnl->save.vertex_size; i++)	\
	 tnl->save.vbptr[i] = tnl->save.vertex[i];	\
							\
      tnl->save.vbptr += tnl->save.vertex_size;		\
							\
      if (--tnl->save.counter == 0)			\
	 _save_wrap_filled_vertex( ctx );			\
   }							\
   else {						\
      GLfloat *dest = tnl->save.attrptr[ATTR];		\
      if (N>0) dest[0] = v[0];			\
      if (N>1) dest[1] = v[1];			\
      if (N>2) dest[2] = v[2];			\
      if (N>3) dest[3] = v[3];			\
   }							\
}

#define CHOOSE( ATTR, N )					\
static void save_choose_##ATTR##_##N( const GLfloat *v )	\
{								\
   _mesa_debug( 0, "%s\n", __FUNCTION__);			\
								\
   do_choose(ATTR, N,						\
	     save_attrib_##ATTR##_##N,				\
	     save_choose_##ATTR##_1,				\
	     save_choose_##ATTR##_2,				\
	     save_choose_##ATTR##_3,				\
	     save_choose_##ATTR##_4,				\
	     v );						\
}

#define INIT(ATTR)					\
static void save_init_##ATTR( TNLcontext *tnl )		\
{							\
   tnl->save.tabfv[ATTR][0] = save_choose_##ATTR##_1;	\
   tnl->save.tabfv[ATTR][1] = save_choose_##ATTR##_2;	\
   tnl->save.tabfv[ATTR][2] = save_choose_##ATTR##_3;	\
   tnl->save.tabfv[ATTR][3] = save_choose_##ATTR##_4;	\
}
   


#define ATTRS( ATTRIB )				\
   ATTRFV( ATTRIB, 1 )				\
   ATTRFV( ATTRIB, 2 )				\
   ATTRFV( ATTRIB, 3 )				\
   ATTRFV( ATTRIB, 4 )				\
   CHOOSE( ATTRIB, 1 )				\
   CHOOSE( ATTRIB, 2 )				\
   CHOOSE( ATTRIB, 3 )				\
   CHOOSE( ATTRIB, 4 )				\
   INIT( ATTRIB )				\


/* Generate a lot of functions.  These are the actual worker
 * functions, which are equivalent to those generated via codegen
 * elsewhere.
 */
ATTRS( 0 )
ATTRS( 1 )
ATTRS( 2 )
ATTRS( 3 )
ATTRS( 4 )
ATTRS( 5 )
ATTRS( 6 )
ATTRS( 7 )
ATTRS( 8 )
ATTRS( 9 )
ATTRS( 10 )
ATTRS( 11 )
ATTRS( 12 )
ATTRS( 13 )
ATTRS( 14 )
ATTRS( 15 )


static void save_init_attrfv( TNLcontext *tnl )
{
   _mesa_debug( 0, "%s\n", __FUNCTION__); 

   save_init_0( tnl );
   save_init_1( tnl );
   save_init_2( tnl );
   save_init_3( tnl );
   save_init_4( tnl );
   save_init_5( tnl );
   save_init_6( tnl );
   save_init_7( tnl );
   save_init_8( tnl );
   save_init_9( tnl );
   save_init_10( tnl );
   save_init_11( tnl );
   save_init_12( tnl );
   save_init_13( tnl );
   save_init_14( tnl );
   save_init_15( tnl );
}


/* Cope with aliasing of classic Vertex, Normal, etc. and the fan-out
 * of glMultTexCoord and glProgramParamterNV by routing all these
 * through a second level dispatch table.
 */
#define DISPATCH_ATTRFV( ATTR, COUNT, P )	\
do {						\
   GET_CURRENT_CONTEXT( ctx ); 			\
   TNLcontext *tnl = TNL_CONTEXT(ctx); 		\
   tnl->save.tabfv[ATTR][COUNT-1]( P );		\
} while (0)

#define DISPATCH_ATTR1FV( ATTR, V ) DISPATCH_ATTRFV( ATTR, 1, V )
#define DISPATCH_ATTR2FV( ATTR, V ) DISPATCH_ATTRFV( ATTR, 2, V )
#define DISPATCH_ATTR3FV( ATTR, V ) DISPATCH_ATTRFV( ATTR, 3, V )
#define DISPATCH_ATTR4FV( ATTR, V ) DISPATCH_ATTRFV( ATTR, 4, V )

#define DISPATCH_ATTR1F( ATTR, S ) DISPATCH_ATTRFV( ATTR, 1, &(S) )

#define DISPATCH_ATTR2F( ATTR, S,T ) 		\
do { 						\
   GLfloat v[2]; 				\
   v[0] = S; v[1] = T;				\
   DISPATCH_ATTR2FV( ATTR, v );			\
} while (0)
#define DISPATCH_ATTR3F( ATTR, S,T,R ) 		\
do { 						\
   GLfloat v[3]; 				\
   v[0] = S; v[1] = T; v[2] = R;		\
   DISPATCH_ATTR3FV( ATTR, v );			\
} while (0)
#define DISPATCH_ATTR4F( ATTR, S,T,R,Q )	\
do { 						\
   GLfloat v[4]; 				\
   v[0] = S; v[1] = T; v[2] = R; v[3] = Q;	\
   DISPATCH_ATTR4FV( ATTR, v );			\
} while (0)


static void enum_error( void )
{
   GET_CURRENT_CONTEXT( ctx );
   _mesa_error( ctx, GL_INVALID_ENUM, __FUNCTION__ );
}

static void _save_Vertex2f( GLfloat x, GLfloat y )
{
   DISPATCH_ATTR2F( _TNL_ATTRIB_POS, x, y );
}

static void _save_Vertex2fv( const GLfloat *v )
{
   DISPATCH_ATTR2FV( _TNL_ATTRIB_POS, v );
}

static void _save_Vertex3f( GLfloat x, GLfloat y, GLfloat z )
{
   DISPATCH_ATTR3F( _TNL_ATTRIB_POS, x, y, z );
}

static void _save_Vertex3fv( const GLfloat *v )
{
   DISPATCH_ATTR3FV( _TNL_ATTRIB_POS, v );
}

static void _save_Vertex4f( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
{
   DISPATCH_ATTR4F( _TNL_ATTRIB_POS, x, y, z, w );
}

static void _save_Vertex4fv( const GLfloat *v )
{
   DISPATCH_ATTR4FV( _TNL_ATTRIB_POS, v );
}

static void _save_TexCoord1f( GLfloat x )
{
   DISPATCH_ATTR1F( _TNL_ATTRIB_TEX0, x );
}

static void _save_TexCoord1fv( const GLfloat *v )
{
   DISPATCH_ATTR1FV( _TNL_ATTRIB_TEX0, v );
}

static void _save_TexCoord2f( GLfloat x, GLfloat y )
{
   DISPATCH_ATTR2F( _TNL_ATTRIB_TEX0, x, y );
}

static void _save_TexCoord2fv( const GLfloat *v )
{
   DISPATCH_ATTR2FV( _TNL_ATTRIB_TEX0, v );
}

static void _save_TexCoord3f( GLfloat x, GLfloat y, GLfloat z )
{
   DISPATCH_ATTR3F( _TNL_ATTRIB_TEX0, x, y, z );
}

static void _save_TexCoord3fv( const GLfloat *v )
{
   DISPATCH_ATTR3FV( _TNL_ATTRIB_TEX0, v );
}

static void _save_TexCoord4f( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
{
   DISPATCH_ATTR4F( _TNL_ATTRIB_TEX0, x, y, z, w );
}

static void _save_TexCoord4fv( const GLfloat *v )
{
   DISPATCH_ATTR4FV( _TNL_ATTRIB_TEX0, v );
}

static void _save_Normal3f( GLfloat x, GLfloat y, GLfloat z )
{
   DISPATCH_ATTR3F( _TNL_ATTRIB_NORMAL, x, y, z );
}

static void _save_Normal3fv( const GLfloat *v )
{
   DISPATCH_ATTR3FV( _TNL_ATTRIB_NORMAL, v );
}

static void _save_FogCoordfEXT( GLfloat x )
{
   DISPATCH_ATTR1F( _TNL_ATTRIB_FOG, x );
}

static void _save_FogCoordfvEXT( const GLfloat *v )
{
   DISPATCH_ATTR1FV( _TNL_ATTRIB_FOG, v );
}

static void _save_Color3f( GLfloat x, GLfloat y, GLfloat z )
{
   DISPATCH_ATTR3F( _TNL_ATTRIB_COLOR0, x, y, z );
}

static void _save_Color3fv( const GLfloat *v )
{
   DISPATCH_ATTR3FV( _TNL_ATTRIB_COLOR0, v );
}

static void _save_Color4f( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
{
   DISPATCH_ATTR4F( _TNL_ATTRIB_COLOR0, x, y, z, w );
}

static void _save_Color4fv( const GLfloat *v )
{
   DISPATCH_ATTR4FV( _TNL_ATTRIB_COLOR0, v );
}

static void _save_SecondaryColor3fEXT( GLfloat x, GLfloat y, GLfloat z )
{
   DISPATCH_ATTR3F( _TNL_ATTRIB_COLOR1, x, y, z );
}

static void _save_SecondaryColor3fvEXT( const GLfloat *v )
{
   DISPATCH_ATTR3FV( _TNL_ATTRIB_COLOR1, v );
}

static void _save_MultiTexCoord1f( GLenum target, GLfloat x  )
{
   GLuint attr = (target & 0x7) + _TNL_ATTRIB_TEX0;
   DISPATCH_ATTR1F( attr, x );
}

static void _save_MultiTexCoord1fv( GLenum target, const GLfloat *v )
{
   GLuint attr = (target & 0x7) + _TNL_ATTRIB_TEX0;
   DISPATCH_ATTR1FV( attr, v );
}

static void _save_MultiTexCoord2f( GLenum target, GLfloat x, GLfloat y )
{
   GLuint attr = (target & 0x7) + _TNL_ATTRIB_TEX0;
   DISPATCH_ATTR2F( attr, x, y );
}

static void _save_MultiTexCoord2fv( GLenum target, const GLfloat *v )
{
   GLuint attr = (target & 0x7) + _TNL_ATTRIB_TEX0;
   DISPATCH_ATTR2FV( attr, v );
}

static void _save_MultiTexCoord3f( GLenum target, GLfloat x, GLfloat y,
				    GLfloat z)
{
   GLuint attr = (target & 0x7) + _TNL_ATTRIB_TEX0;
   DISPATCH_ATTR3F( attr, x, y, z );
}

static void _save_MultiTexCoord3fv( GLenum target, const GLfloat *v )
{
   GLuint attr = (target & 0x7) + _TNL_ATTRIB_TEX0;
   DISPATCH_ATTR3FV( attr, v );
}

static void _save_MultiTexCoord4f( GLenum target, GLfloat x, GLfloat y,
				    GLfloat z, GLfloat w )
{
   GLuint attr = (target & 0x7) + _TNL_ATTRIB_TEX0;
   DISPATCH_ATTR4F( attr, x, y, z, w );
}

static void _save_MultiTexCoord4fv( GLenum target, const GLfloat *v )
{
   GLuint attr = (target & 0x7) + _TNL_ATTRIB_TEX0;
   DISPATCH_ATTR4FV( attr, v );
}

static void _save_VertexAttrib1fNV( GLuint index, GLfloat x )
{
   if (index >= _TNL_ATTRIB_POS && index < _TNL_ATTRIB_MAX)
      DISPATCH_ATTR1F( index, x );
   else
      enum_error(); 
}

static void _save_VertexAttrib1fvNV( GLuint index, const GLfloat *v )
{
   if (index >= _TNL_ATTRIB_POS && index < _TNL_ATTRIB_MAX)
      DISPATCH_ATTR1FV( index, v );
   else
      enum_error();
}

static void _save_VertexAttrib2fNV( GLuint index, GLfloat x, GLfloat y )
{
   if (index >= _TNL_ATTRIB_POS && index < _TNL_ATTRIB_MAX)
      DISPATCH_ATTR2F( index, x, y );
   else
      enum_error();
}

static void _save_VertexAttrib2fvNV( GLuint index, const GLfloat *v )
{
   if (index >= _TNL_ATTRIB_POS && index < _TNL_ATTRIB_MAX)
      DISPATCH_ATTR2FV( index, v );
   else
      enum_error();
}

static void _save_VertexAttrib3fNV( GLuint index, GLfloat x, GLfloat y, 
				  GLfloat z )
{
   if (index >= _TNL_ATTRIB_POS && index < _TNL_ATTRIB_MAX)
      DISPATCH_ATTR3F( index, x, y, z );
   else
      enum_error();
}

static void _save_VertexAttrib3fvNV( GLuint index, const GLfloat *v )
{
   if (index >= _TNL_ATTRIB_POS && index < _TNL_ATTRIB_MAX)
      DISPATCH_ATTR3FV( index, v );
   else
      enum_error();
}

static void _save_VertexAttrib4fNV( GLuint index, GLfloat x, GLfloat y,
				  GLfloat z, GLfloat w )
{
   if (index >= _TNL_ATTRIB_POS && index < _TNL_ATTRIB_MAX)
      DISPATCH_ATTR4F( index, x, y, z, w );
   else
      enum_error();
}

static void _save_VertexAttrib4fvNV( GLuint index, const GLfloat *v )
{
   if (index >= _TNL_ATTRIB_POS && index < _TNL_ATTRIB_MAX)
      DISPATCH_ATTR4FV( index, v );
   else
      enum_error();
}


/* Materials:  
 * 
 * These are treated as per-vertex attributes, at indices above where
 * the NV_vertex_program leaves off.  There are a lot of good things
 * about treating materials this way.  
 *
 * However: I don't want to double the number of generated functions
 * just to cope with this, so I unroll the 'C' varients of CHOOSE and
 * ATTRF into this function, and dispense with codegen and
 * second-level dispatch.
 *
 * There is no aliasing of material attributes with other entrypoints.
 */
#define MAT_ATTR( A, N, params )			\
do {							\
   if (tnl->save.attrsz[A] < N) {			\
      _save_upgrade_vertex( ctx, A, N );		\
   }							\
							\
   {							\
      GLfloat *dest = tnl->save.attrptr[A];	      	\
      if (N>0) dest[0] = params[0];			\
      if (N>1) dest[1] = params[1];			\
      if (N>2) dest[2] = params[2];			\
      if (N>3) dest[3] = params[3];			\
   }							\
} while (0)


#define MAT( ATTR, N, face, params )			\
do {							\
   if (face != GL_BACK)					\
      MAT_ATTR( ATTR, N, params ); /* front */		\
   if (face != GL_FRONT)				\
      MAT_ATTR( ATTR + 1, N, params ); /* back */	\
} while (0)


/* NOTE: Have to remove/deal-with colormaterial crossovers, probably
 * later on - in the meantime just store everything.  
 */
static void _save_Materialfv( GLenum face, GLenum pname, 
			       const GLfloat *params )
{
   GET_CURRENT_CONTEXT( ctx ); 
   TNLcontext *tnl = TNL_CONTEXT(ctx);

   switch (pname) {
   case GL_EMISSION:
      MAT( _TNL_ATTRIB_MAT_FRONT_EMISSION, 4, face, params );
      break;
   case GL_AMBIENT:
      MAT( _TNL_ATTRIB_MAT_FRONT_AMBIENT, 4, face, params );
      break;
   case GL_DIFFUSE:
      MAT( _TNL_ATTRIB_MAT_FRONT_DIFFUSE, 4, face, params );
      break;
   case GL_SPECULAR:
      MAT( _TNL_ATTRIB_MAT_FRONT_SPECULAR, 4, face, params );
      break;
   case GL_SHININESS:
      MAT( _TNL_ATTRIB_MAT_FRONT_SHININESS, 1, face, params );
      break;
   case GL_COLOR_INDEXES:
      MAT( _TNL_ATTRIB_MAT_FRONT_INDEXES, 3, face, params );
      break;
   case GL_AMBIENT_AND_DIFFUSE:
      MAT( _TNL_ATTRIB_MAT_FRONT_AMBIENT, 4, face, params );
      MAT( _TNL_ATTRIB_MAT_FRONT_DIFFUSE, 4, face, params );
      break;
   default:
      _mesa_error( ctx, GL_INVALID_ENUM, __FUNCTION__ );
      return;
   }
}


#define IDX_ATTR( A, IDX )				\
do {							\
   GET_CURRENT_CONTEXT( ctx );				\
   TNLcontext *tnl = TNL_CONTEXT(ctx);			\
							\
   if (tnl->save.attrsz[A] < 1) {			\
      _save_upgrade_vertex( ctx, A, 1 );		\
   }							\
							\
   {							\
      GLfloat *dest = tnl->save.attrptr[A];		\
      dest[0] = IDX;				\
   }							\
} while (0)


static void _save_EdgeFlag( GLboolean b )
{
   IDX_ATTR( _TNL_ATTRIB_EDGEFLAG, (GLfloat)b );
}

static void _save_EdgeFlagv( const GLboolean *v )
{
   IDX_ATTR( _TNL_ATTRIB_EDGEFLAG, (GLfloat)(v[0]) );
}

static void _save_Indexf( GLfloat f )
{
   IDX_ATTR( _TNL_ATTRIB_INDEX, f );
}

static void _save_Indexfv( const GLfloat *f )
{
   IDX_ATTR( _TNL_ATTRIB_INDEX, f[0] );
}




/* EvalCoord 
 *
 *  -- Flush current buffer
 *  -- Set a flag
 *  -- Fallback to opcodes for the rest of the list.
 */
#define FALLBACK(ctx) 				\
do {						\
} while (0)

static void _save_EvalCoord1f( GLfloat u )
{
   GET_CURRENT_CONTEXT(ctx);
   FALLBACK(ctx);
   ctx->Save->EvalCoord1f( u );
}

static void _save_EvalCoord1fv( const GLfloat *v )
{
   GET_CURRENT_CONTEXT(ctx);
   FALLBACK(ctx);
   ctx->Save->EvalCoord1fv( v );
}

static void _save_EvalCoord2f( GLfloat u, GLfloat v )
{
   GET_CURRENT_CONTEXT(ctx);
   FALLBACK(ctx);
   ctx->Save->EvalCoord2f( u, v );
}

static void _save_EvalCoord2fv( const GLfloat *v )
{
   GET_CURRENT_CONTEXT(ctx);
   FALLBACK(ctx);
   ctx->Save->EvalCoord2fv( v );
}

static void _save_EvalPoint1( GLint i )
{
   GET_CURRENT_CONTEXT(ctx);
   FALLBACK(ctx);
   ctx->Save->EvalPoint1( i );
}

static void _save_EvalPoint2( GLint i, GLint j )
{
   GET_CURRENT_CONTEXT(ctx);
   FALLBACK(ctx);
   ctx->Save->EvalPoint2( i, j );
}

static void _save_CallList( GLuint l )
{
   GET_CURRENT_CONTEXT(ctx);
   FALLBACK(ctx);
   ctx->Save->CallList( l );
}



/* This begin is hooked into ...  Updating of
 * ctx->Driver.CurrentSavePrimitive is already taken care of.
 */
static GLboolean _save_NotifyBegin( GLcontext *ctx, GLenum mode )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx); 
   int i = tnl->save.prim_count++;

   tnl->save.prim[i].mode = mode | PRIM_BEGIN;
   tnl->save.prim[i].start = tnl->save.initial_counter - tnl->save.counter;
   tnl->save.prim[i].count = 0;   

   _mesa_install_save_vtxfmt( ctx, &tnl->save_vtxfmt );

   ctx->Driver.SaveNeedFlush = 1;
   return GL_TRUE;
}



static void _save_End( void )
{
   GET_CURRENT_CONTEXT( ctx ); 
   TNLcontext *tnl = TNL_CONTEXT(ctx); 
   int i = tnl->save.prim_count - 1;

   tnl->save.prim[i].mode |= PRIM_END;
   tnl->save.prim[i].count = ((tnl->save.initial_counter - tnl->save.counter) - 
			      tnl->save.prim[i].start);

   _mesa_debug(0, "%s: prim nr %d  prim %x start %d len %d\n",
	       __FUNCTION__, i, 
	       tnl->save.prim[i].mode,
	       tnl->save.prim[i].start,
	       tnl->save.prim[i].count);


   if (i == tnl->save.prim_max)
      _save_compile_vertex_list( ctx );


   /* Swap out this vertex format while outside begin/end.  Any color,
    * etc. received between here and the next begin will be compiled
    * as opcodes.
    *
    *   -- Need to ensure that these vertices are flushed in that case.
    *   -- Just use the regular flush mechanism.
    */   
   ctx->Driver.CurrentSavePrimitive = PRIM_OUTSIDE_BEGIN_END;

   _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
}


/* These are all errors as this vtxfmt is only installed inside
 * begin/end pairs.
 */
static void _save_DrawElements(GLenum mode, GLsizei count, GLenum type,
			       const GLvoid *indices)
{
   GET_CURRENT_CONTEXT(ctx);
   _mesa_error( ctx, GL_INVALID_OPERATION, __FUNCTION__);
}


static void _save_DrawRangeElements(GLenum mode,
				    GLuint start, GLuint end,
				    GLsizei count, GLenum type,
				    const GLvoid *indices)
{
   GET_CURRENT_CONTEXT(ctx);
   _mesa_error( ctx, GL_INVALID_OPERATION, __FUNCTION__);
}

static void _save_DrawArrays(GLenum mode, GLint start, GLsizei count)
{
   GET_CURRENT_CONTEXT(ctx);
   _mesa_error( ctx, GL_INVALID_OPERATION, __FUNCTION__);
}

static void _save_Rectf( GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2 )
{
   GET_CURRENT_CONTEXT(ctx);
   _mesa_error( ctx, GL_INVALID_OPERATION, __FUNCTION__);
}

static void _save_EvalMesh1( GLenum mode, GLint i1, GLint i2 )
{
   GET_CURRENT_CONTEXT(ctx);
   _mesa_error( ctx, GL_INVALID_OPERATION, __FUNCTION__);
}

static void _save_EvalMesh2( GLenum mode, GLint i1, GLint i2,
				  GLint j1, GLint j2 )
{
   GET_CURRENT_CONTEXT(ctx);
   _mesa_error( ctx, GL_INVALID_OPERATION, __FUNCTION__);
}

static void _save_Begin( GLenum mode )
{
   GET_CURRENT_CONTEXT( ctx );
   _mesa_error( ctx, GL_INVALID_OPERATION, "Recursive begin" );
}



static void _save_vtxfmt_init( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLvertexformat *vfmt = &tnl->save_vtxfmt;

   vfmt->ArrayElement = _ae_loopback_array_elt;	        /* generic helper */
   vfmt->Begin = _save_Begin;
   vfmt->CallList = _mesa_CallList;
   vfmt->Color3f = _save_Color3f;
   vfmt->Color3fv = _save_Color3fv;
   vfmt->Color4f = _save_Color4f;
   vfmt->Color4fv = _save_Color4fv;
   vfmt->EdgeFlag = _save_EdgeFlag;
   vfmt->EdgeFlagv = _save_EdgeFlagv;
   vfmt->End = _save_End;
   vfmt->FogCoordfEXT = _save_FogCoordfEXT;
   vfmt->FogCoordfvEXT = _save_FogCoordfvEXT;
   vfmt->Indexf = _save_Indexf;
   vfmt->Indexfv = _save_Indexfv;
   vfmt->Materialfv = _save_Materialfv;
   vfmt->MultiTexCoord1fARB = _save_MultiTexCoord1f;
   vfmt->MultiTexCoord1fvARB = _save_MultiTexCoord1fv;
   vfmt->MultiTexCoord2fARB = _save_MultiTexCoord2f;
   vfmt->MultiTexCoord2fvARB = _save_MultiTexCoord2fv;
   vfmt->MultiTexCoord3fARB = _save_MultiTexCoord3f;
   vfmt->MultiTexCoord3fvARB = _save_MultiTexCoord3fv;
   vfmt->MultiTexCoord4fARB = _save_MultiTexCoord4f;
   vfmt->MultiTexCoord4fvARB = _save_MultiTexCoord4fv;
   vfmt->Normal3f = _save_Normal3f;
   vfmt->Normal3fv = _save_Normal3fv;
   vfmt->SecondaryColor3fEXT = _save_SecondaryColor3fEXT;
   vfmt->SecondaryColor3fvEXT = _save_SecondaryColor3fvEXT;
   vfmt->TexCoord1f = _save_TexCoord1f;
   vfmt->TexCoord1fv = _save_TexCoord1fv;
   vfmt->TexCoord2f = _save_TexCoord2f;
   vfmt->TexCoord2fv = _save_TexCoord2fv;
   vfmt->TexCoord3f = _save_TexCoord3f;
   vfmt->TexCoord3fv = _save_TexCoord3fv;
   vfmt->TexCoord4f = _save_TexCoord4f;
   vfmt->TexCoord4fv = _save_TexCoord4fv;
   vfmt->Vertex2f = _save_Vertex2f;
   vfmt->Vertex2fv = _save_Vertex2fv;
   vfmt->Vertex3f = _save_Vertex3f;
   vfmt->Vertex3fv = _save_Vertex3fv;
   vfmt->Vertex4f = _save_Vertex4f;
   vfmt->Vertex4fv = _save_Vertex4fv;
   vfmt->VertexAttrib1fNV = _save_VertexAttrib1fNV;
   vfmt->VertexAttrib1fvNV = _save_VertexAttrib1fvNV;
   vfmt->VertexAttrib2fNV = _save_VertexAttrib2fNV;
   vfmt->VertexAttrib2fvNV = _save_VertexAttrib2fvNV;
   vfmt->VertexAttrib3fNV = _save_VertexAttrib3fNV;
   vfmt->VertexAttrib3fvNV = _save_VertexAttrib3fvNV;
   vfmt->VertexAttrib4fNV = _save_VertexAttrib4fNV;
   vfmt->VertexAttrib4fvNV = _save_VertexAttrib4fvNV;
   
   /* This will all require us to fallback to saving the list as opcodes:
    */ 
   vfmt->CallList = _save_CallList; /* inside begin/end */
   vfmt->EvalCoord1f = _save_EvalCoord1f;
   vfmt->EvalCoord1fv = _save_EvalCoord1fv;
   vfmt->EvalCoord2f = _save_EvalCoord2f;
   vfmt->EvalCoord2fv = _save_EvalCoord2fv;
   vfmt->EvalPoint1 = _save_EvalPoint1;
   vfmt->EvalPoint2 = _save_EvalPoint2;

   /* These are all errors as we at least know we are in some sort of
    * begin/end pair:
    */
   vfmt->EvalMesh1 = _save_EvalMesh1;	
   vfmt->EvalMesh2 = _save_EvalMesh2;
   vfmt->Begin = _save_Begin;
   vfmt->Rectf = _save_Rectf;
   vfmt->DrawArrays = _save_DrawArrays;
   vfmt->DrawElements = _save_DrawElements;
   vfmt->DrawRangeElements = _save_DrawRangeElements;

}



void _tnl_NewList( GLcontext *ctx, GLuint list, GLenum mode )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   assert(tnl->save.vertex_size == 0);

   if (!tnl->save.prim_store)
      tnl->save.prim_store = alloc_prim_store( ctx );

   if (!tnl->save.vertex_store) {
      tnl->save.vertex_store = alloc_vertex_store( ctx );
      tnl->save.vbptr = tnl->save.vertex_store->buffer;
   }
   
   _save_reset_counters( ctx ); 
}

void _tnl_EndList( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   assert(tnl->save.vertex_size == 0);
}
 
void _tnl_BeginCallList( GLcontext *ctx, GLuint list )
{
}

void _tnl_EndCallList( GLcontext *ctx )
{
}

/* Called when 
 */
void _tnl_SaveFlushVertices( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLint i;

   if (ctx->Driver.CurrentSavePrimitive == PRIM_INSIDE_UNKNOWN_PRIM ||
       ctx->Driver.CurrentSavePrimitive <= GL_POLYGON)
      return;

   _save_compile_vertex_list( ctx );
   
   save_init_attrfv( tnl );

   for (i = 0 ; i < _TNL_ATTRIB_MAX ; i++)
      tnl->save.attrsz[i] = 0;

   tnl->save.vertex_size = 0;

   ctx->Driver.SaveNeedFlush = 0;
}

static void _tnl_destroy_vertex_list( GLcontext *ctx, void *data )
{
   struct tnl_vertex_list *node = (struct tnl_vertex_list *)data;

   if ( --node->vertex_store->refcount == 0 )
      ALIGN_FREE( node->vertex_store );

   if ( --node->prim_store->refcount == 0 )
      ALIGN_FREE( node->prim_store );

   FREE( node );
}


static void _tnl_print_vertex_list( GLcontext *ctx, void *data )
{
   struct tnl_vertex_list *node = (struct tnl_vertex_list *)data;

   _mesa_debug(ctx, "TNL-VERTEX-LIST, %u vertices %d primitives\n",
               node->count,
	       node->prim_count);
}


static void _save_current_init( GLcontext *ctx ) 
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLint i;

   for (i = 0; i < _TNL_ATTRIB_MAT_FRONT_AMBIENT; i++) {
      tnl->save.currentsz[i] = &ctx->ListState.ActiveAttribSize[i];
      tnl->save.current[i] = ctx->ListState.CurrentAttrib[i];
   }

   for (i = _TNL_ATTRIB_MAT_FRONT_AMBIENT; i < _TNL_ATTRIB_INDEX; i++) {
      tnl->save.currentsz[i] = &ctx->ListState.ActiveMaterialSize[i];
      tnl->save.current[i] = ctx->ListState.CurrentMaterial[i];
   }

   tnl->save.currentsz[_TNL_ATTRIB_INDEX] = &ctx->ListState.ActiveIndex;
   tnl->save.current[_TNL_ATTRIB_INDEX] = &ctx->ListState.CurrentIndex;

   /* Current edgeflag is handled individually */
}

/**
 * Setup vector pointers that will be used to bind immediates to VB's.
 */
void _tnl_save_init( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct tnl_vertex_arrays *tmp = &tnl->save_inputs;
   GLuint i;

   save_init_attrfv( tnl );

   for (i = 0 ; i < _TNL_ATTRIB_MAX ; i++)
      tnl->save.attrsz[i] = 0;

   tnl->save.vertex_size = 0;

   _save_vtxfmt_init( ctx );

   for (i = 0; i < _TNL_ATTRIB_MAX; i++)
      _mesa_vector4f_init( &tmp->Attribs[i], 0, 0);

   tnl->save.opcode_vertex_list =
      _mesa_alloc_opcode( ctx,
			  sizeof(struct tnl_vertex_list),
			  _tnl_playback_vertex_list,
			  _tnl_destroy_vertex_list,
			  _tnl_print_vertex_list );

   ctx->Driver.NotifySaveBegin = _save_NotifyBegin;

   _save_current_init( ctx );
}


/**
 * Deallocate the immediate-mode buffer for the given context, if
 * its reference count goes to zero.
 */
void _tnl_save_destroy( GLcontext *ctx )
{
}