From 42204aa6108d7e95d04ea660ef7759dbd9c49116 Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Thu, 8 Sep 2011 22:29:27 +0000
Subject: [PATCH] Start working on disconnecting CTerm from CONIO

---
 src/conio/cterm.c |  61 ++++++++++++--------------
 src/conio/cterm.h | 108 +++++++++++++++++++++++++---------------------
 2 files changed, 87 insertions(+), 82 deletions(-)

diff --git a/src/conio/cterm.c b/src/conio/cterm.c
index 11ed9497ff..298c09a256 100644
--- a/src/conio/cterm.c
+++ b/src/conio/cterm.c
@@ -173,10 +173,6 @@ struct note_params {
 	int	foreground;
 };
 
-static int playnote_thread_running=FALSE;
-static link_list_t	notes;
-sem_t	playnote_thread_terminated;
-sem_t	note_completed_sem;
 
 void playnote_thread(void *args)
 {
@@ -186,19 +182,19 @@ void playnote_thread(void *args)
 	struct note_params *note;
 	int	device_open=FALSE;
 
-	playnote_thread_running=TRUE;
+	cterm.playnote_thread_running=TRUE;
 	while(1) {
 		if(device_open) {
-			if(!listSemTryWaitBlock(&notes,5000)) {
+			if(!listSemTryWaitBlock(&cterm.notes,5000)) {
 				xptone_close();
 				device_open=FALSE;
-				listSemWait(&notes);
+				listSemWait(&cterm.notes);
 			}
 		}
 		else
-			listSemWait(&notes);
+			listSemWait(&cterm.notes);
 		device_open=xptone_open();
-		note=listShiftNode(&notes);
+		note=listShiftNode(&cterm.notes);
 		if(note==NULL)
 			break;
 		if(note->dotted)
@@ -229,11 +225,11 @@ void playnote_thread(void *args)
 			SLEEP(duration);
 		SLEEP(pauselen);
 		if(note->foreground)
-			sem_post(&note_completed_sem);
+			sem_post(&cterm.note_completed_sem);
 		free(note);
 	}
-	playnote_thread_running=FALSE;
-	sem_post(&playnote_thread_terminated);
+	cterm.playnote_thread_running=FALSE;
+	sem_post(&cterm.playnote_thread_terminated);
 }
 
 void play_music(void)
@@ -419,7 +415,7 @@ void play_music(void)
 					np->tempo=cterm.tempo;
 					np->noteshape=cterm.noteshape;
 					np->foreground=cterm.musicfore;
-					listPushNode(&notes, np);
+					listPushNode(&cterm.notes, np);
 					if(cterm.musicfore)
 						fore_count++;
 				}
@@ -439,7 +435,7 @@ void play_music(void)
 	cterm.music=0;
 	cterm.musicbuf[0]=0;
 	while(fore_count) {
-		sem_wait(&note_completed_sem);
+		sem_wait(&cterm.note_completed_sem);
 		fore_count--;
 	}
 }
@@ -620,7 +616,6 @@ void do_ansi(char *retbuf, size_t retsize, int *speed)
 							}
 							if(i>255)
 								break;
-							cterm.font_start_time=time(NULL);
 							cterm.font_read=0;
 							cterm.font_slot=i;
 							switch(j) {
@@ -1234,20 +1229,20 @@ void cterm_init(int height, int width, int xpos, int ypos, int backlines, unsign
 	}
 	strcat(cterm.DA,"c");
 	/* Did someone call _init() without calling _end()? */
-	if(playnote_thread_running) {
-		if(sem_trywait(&playnote_thread_terminated)==-1) {
-			listSemPost(&notes);
-			sem_wait(&playnote_thread_terminated);
+	if(cterm.playnote_thread_running) {
+		if(sem_trywait(&cterm.playnote_thread_terminated)==-1) {
+			listSemPost(&cterm.notes);
+			sem_wait(&cterm.playnote_thread_terminated);
 		}
-		sem_destroy(&playnote_thread_terminated);
-		sem_destroy(&note_completed_sem);
-		listFree(&notes);
+		sem_destroy(&cterm.playnote_thread_terminated);
+		sem_destroy(&cterm.note_completed_sem);
+		listFree(&cterm.notes);
 	}
 	/* Fire up note playing thread */
-	if(!playnote_thread_running) {
-		listInit(&notes, LINK_LIST_SEMAPHORE|LINK_LIST_MUTEX);
-		sem_init(&note_completed_sem,0,0);
-		sem_init(&playnote_thread_terminated,0,0);
+	if(!cterm.playnote_thread_running) {
+		listInit(&cterm.notes, LINK_LIST_SEMAPHORE|LINK_LIST_MUTEX);
+		sem_init(&cterm.note_completed_sem,0,0);
+		sem_init(&cterm.playnote_thread_terminated,0,0);
 		_beginthread(playnote_thread, 0, NULL);
 	}
 
@@ -2084,13 +2079,13 @@ void cterm_end(void)
 		FREE_AND_NULL(conio_fontdata[i].eight_by_eight);
 		FREE_AND_NULL(conio_fontdata[i].desc);
 	}
-	if(playnote_thread_running) {
-		if(sem_trywait(&playnote_thread_terminated)==-1) {
-			listSemPost(&notes);
-			sem_wait(&playnote_thread_terminated);
+	if(cterm.playnote_thread_running) {
+		if(sem_trywait(&cterm.playnote_thread_terminated)==-1) {
+			listSemPost(&cterm.notes);
+			sem_wait(&cterm.playnote_thread_terminated);
 		}
-		sem_destroy(&playnote_thread_terminated);
-		sem_destroy(&note_completed_sem);
-		listFree(&notes);
+		sem_destroy(&cterm.playnote_thread_terminated);
+		sem_destroy(&cterm.note_completed_sem);
+		listFree(&cterm.notes);
 	}
 }
diff --git a/src/conio/cterm.h b/src/conio/cterm.h
index 74ffe6891b..8a73ffec26 100644
--- a/src/conio/cterm.h
+++ b/src/conio/cterm.h
@@ -35,70 +35,80 @@
 #define _CTERM_H_
 
 #include <stdio.h>	/* FILE* */
+#include <link_list.h>
 
-enum {
+typedef enum {
 	 CTERM_MUSIC_NORMAL
 	,CTERM_MUSIC_LEGATO
 	,CTERM_MUSIC_STACATTO
-};
+} cterm_noteshape_t;
 
-enum {
+typedef enum {
 	 CTERM_LOG_NONE
 	,CTERM_LOG_ASCII
 	,CTERM_LOG_RAW
-};
+} cterm_log_t;
+
+typedef enum {
+	 CTERM_EMULATION_ANSI_BBS
+	,CTERM_EMULATION_PETASCII
+	,CTERM_EMULATION_ATASCII
+} cterm_emulation_t;
+
+typedef enum {
+	CTERM_MUSIC_SYNCTERM,
+	CTERM_MUSIC_BANSI,
+	CTERM_MUSIC_ENABLED
+} cterm_music_t;
 
 #define CTERM_LOG_MASK	0x7f
 #define CTERM_LOG_PAUSED	0x80
 
 struct cterminal {
-	int emulation;
-	int c64reversemode;
-	int	height;
-	int	width;
-	int	x;
-	int	y;
-	char *buffer;
-	unsigned char	attr;
-	int save_xpos;
-	int save_ypos;
-	char	escbuf[1024];
-	int	sequence;
-	int music_enable;
-	char	musicbuf[1024];
-	int music;
-	int quiet;
-	int	tempo;
-	int	octave;
-	int notelen;
-	int noteshape;
-	int musicfore;
-	char *scrollback;
-	int backpos;
-	int backlines;
-	int	xpos;
-	int ypos;
-	int log;
-	FILE* logfile;
-	char	DA[1024];
-	char	fontbuf[4096];
-	int		font_slot;
-	int		font_size;
-	int		font_read;
-	int		font_start_time;
-	int		doorway_mode;
-	int		doorway_char;
-	int		cursor;
-};
+	/* conio stuff */
+	int	x;		// X position of the left side on the screen
+	int	y;		// Y position of the top pn the screen
 
-#define CTERM_MUSIC_SYNCTERM	0
-#define	CTERM_MUSIC_BANSI		1
-#define	CTERM_MUSIC_ENABLED		2
+	/* emulation mode */
+	cterm_emulation_t	emulation;
+	int					height;			// Height of the terminal buffer
+	int					width;			// Width of the terminal buffer
+	int					quiet;			// No sounds are made
+	char				*scrollback;
+	int					backlines;		// Number of lines in scrollback
+	char				DA[1024];		// Device Attributes
 
-enum {
-	 CTERM_EMULATION_ANSI_BBS
-	,CTERM_EMULATION_PETASCII
-	,CTERM_EMULATION_ATASCII
+	/* emulation state */
+	int					c64reversemode;	// Commodore 64 reverse mode state
+	unsigned char		attr;			// Current attribute
+	int					save_xpos;		// Saved position (for later restore)
+	int					save_ypos;
+	int					sequence;		// An escape sequence is being parsed
+	char				escbuf[1024];
+	cterm_music_t		music_enable;	// The remotely/locally controled music state
+	char				musicbuf[1024];
+	int					music;			// ANSI music is being parsed
+	int					tempo;
+	int					octave;
+	int					notelen;
+	cterm_noteshape_t	noteshape;
+	int					musicfore;
+	int					playnote_thread_running;
+	link_list_t			notes;
+	sem_t				playnote_thread_terminated;
+	sem_t				note_completed_sem;
+	int					backpos;
+	int					xpos;
+	int					ypos;
+	cterm_log_t			log;
+	FILE*				logfile;
+	char				fontbuf[4096];	// Remote font
+	int					font_read;		// Current position in fontbuf
+	int					font_slot;
+	int					font_size;		// Bytes
+	int					doorway_mode;
+	int					doorway_char;	// Indicates next char is a "doorway" mode char
+	int					cursor;			// Current cursor mode (Normal or None)
 };
 
 #ifdef __cplusplus
-- 
GitLab