<commit_before>/* Copyright (c) 2001, Stanford University
	All rights reserved.

	See the file LICENSE.txt for information on redistributing this software. */
	
#include <stdio.h>
#include "cr_spu.h"
#include "chromium.h"
#include "cr_error.h"
#include "cr_mem.h"
#include "cr_net.h"
#include "server_dispatch.h"
#include "server.h"


/* This makes context numbers more readable during debugging */
#define MAGIC_OFFSET 5000


GLint SERVER_DISPATCH_APIENTRY crServerDispatchCreateContext( const char *dpyName, GLint visualBits )
{
	GLint i, retVal = 0, ctxPos = -1;
	CRContext *newCtx;

	/* Since the Cr server serialized all incoming clients/contexts into
	 * one outgoing GL stream, we only need to create one context for the
	 * head SPU.  We'll only have to make it current once too, below.
	 */
	if (cr_server.firstCallCreateContext) {
		cr_server.SpuContext
			= cr_server.head_spu->dispatch_table.CreateContext( dpyName, visualBits );
		if (cr_server.SpuContext < 0) {
			crWarning("headSpu.CreateContext failed in crServerDispatchCreateContext()");
			return -1;
		}
		cr_server.firstCallCreateContext = GL_FALSE;
	}

	/* find an empty position in the context[] array */
	for (i = 0; i < CR_MAX_CONTEXTS; i++) {
		if (cr_server.context[i] == NULL) {
			ctxPos = i;
			break;
		}
	}

	if (ctxPos >= 0) {
		/* Now create a new state-tracker context and initialize the
		 * dispatch function pointers.
		 */
		newCtx = crStateCreateContext( &cr_server.limits, visualBits );
		if (newCtx) {
			cr_server.context[ctxPos] = newCtx;
			crStateSetCurrentPointers( newCtx, &(cr_server.current) );
			retVal = MAGIC_OFFSET + ctxPos;
		}
	}

	crServerReturnValue( &retVal, sizeof(retVal) );
	return retVal;
}


void SERVER_DISPATCH_APIENTRY crServerDispatchDestroyContext( GLint ctx )
{
	const int ctxPos = ctx - MAGIC_OFFSET;
	CRContext *crCtx;

	CRASSERT(ctxPos >= 0);

	crCtx = cr_server.context[ctxPos];
	if (crCtx) {
		crStateDestroyContext( crCtx );
		cr_server.context[ctxPos] = NULL;
	}
}


void SERVER_DISPATCH_APIENTRY crServerDispatchMakeCurrent( GLint window, GLint nativeWindow, GLint context )
{
	CRMuralInfo *mural;
	int ctxPos;
	CRContext *ctx;

	if (context >= 0 && window >= 0) {
		ctxPos = context - MAGIC_OFFSET;
		CRASSERT(ctxPos >= 0);
		CRASSERT(ctxPos < CR_MAX_CONTEXTS);

		mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
		CRASSERT(mural);

		/* Update the state tracker's current context */
		ctx = cr_server.context[ctxPos];
	}
	else {
		ctx = NULL;
		window = -1;
		mural = NULL;<commit_msg>when destroying a context, ensure the current client's current context gets set to NULL if it's the context we're destroying<commit_after>/* Copyright (c) 2001, Stanford University
	All rights reserved.

	See the file LICENSE.txt for information on redistributing this software. */
	
#include <stdio.h>
#include "cr_spu.h"
#include "chromium.h"
#include "cr_error.h"
#include "cr_mem.h"
#include "cr_net.h"
#include "server_dispatch.h"
#include "server.h"


/* This makes context numbers more readable during debugging */
#define MAGIC_OFFSET 5000


GLint SERVER_DISPATCH_APIENTRY crServerDispatchCreateContext( const char *dpyName, GLint visualBits )
{
	GLint i, retVal = 0, ctxPos = -1;
	CRContext *newCtx;

	/* Since the Cr server serialized all incoming clients/contexts into
	 * one outgoing GL stream, we only need to create one context for the
	 * head SPU.  We'll only have to make it current once too, below.
	 */
	if (cr_server.firstCallCreateContext) {
		cr_server.SpuContext
			= cr_server.head_spu->dispatch_table.CreateContext( dpyName, visualBits );
		if (cr_server.SpuContext < 0) {
			crWarning("headSpu.CreateContext failed in crServerDispatchCreateContext()");
			return -1;
		}
		cr_server.firstCallCreateContext = GL_FALSE;
	}

	/* find an empty position in the context[] array */
	for (i = 0; i < CR_MAX_CONTEXTS; i++) {
		if (cr_server.context[i] == NULL) {
			ctxPos = i;
			break;
		}
	}

	if (ctxPos >= 0) {
		/* Now create a new state-tracker context and initialize the
		 * dispatch function pointers.
		 */
		newCtx = crStateCreateContext( &cr_server.limits, visualBits );
		if (newCtx) {
			cr_server.context[ctxPos] = newCtx;
			crStateSetCurrentPointers( newCtx, &(cr_server.current) );
			retVal = MAGIC_OFFSET + ctxPos;
		}
	}

	crServerReturnValue( &retVal, sizeof(retVal) );
	return retVal;
}


void SERVER_DISPATCH_APIENTRY crServerDispatchDestroyContext( GLint ctx )
{
	const int ctxPos = ctx - MAGIC_OFFSET;
	CRContext *crCtx;

	CRASSERT(ctxPos >= 0);

	crCtx = cr_server.context[ctxPos];
	if (crCtx) {
		crStateDestroyContext( crCtx );
		cr_server.context[ctxPos] = NULL;
	}

	if (cr_server.curClient->currentCtx == crCtx)
		cr_server.curClient->currentCtx = NULL;
}


void SERVER_DISPATCH_APIENTRY crServerDispatchMakeCurrent( GLint window, GLint nativeWindow, GLint context )
{
	CRMuralInfo *mural;
	int ctxPos;
	CRContext *ctx;

	if (context >= 0 && window >= 0) {
		ctxPos = context - MAGIC_OFFSET;
		CRASSERT(ctxPos >= 0);
		CRASSERT(ctxPos < CR_MAX_CONTEXTS);

		mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
		CRASSERT(mural);

		/* Update the state tracker's current context */
		ctx = cr_server.context[ctxPos];
	}
	else {
		ctx = NULL;
		window = -1;
		mural = NULL;
{'fraction_non_alphanumeric': 0.10860927152317881, 'fraction_numerical': 0.0071901608325449385, 'mean_word_length': 4.238850346878097, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4795395', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1810, 'n_words': 495}