summaryrefslogtreecommitdiff
path: root/src/client/term.c
blob: 3357f0bbac18a6b946c125319706c7e3c30faf46 (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

#include <stdlib.h>
#include <assert.h>

#include "base/main.h"
#include "base/list.h"
#include "base/myTimer.h"

#include "client/interface.h"
#include "client/term.h"
#include "client/font.h"

typedef struct struct_term_t
{
	char *text;
	my_time_t delTime;
} term_t;

static list_t *listText;

static term_t* newTermItem(char *s)
{
	term_t *new;
	char *line;
	int len;

	assert( s != NULL );
	
	line = strdup(s);
	len = strlen(line);
	line[len-1] = '\0';

	new = malloc( sizeof(term_t) );
	memset(new, 0, sizeof(term_t));

	new->text = line;
	new->delTime = getMyTime() + TERM_DEL_TEXT_TIMEOUT;

	return new;
}

static void destroyTermItem(term_t *p)
{
	assert( p != NULL );
	
	free(p->text);
	free(p);
}

void initTerm()
{
	listText = newList();
}

void drawTerm()
{
	int i;

	for( i = 0 ; i < listText->count ; i++ )
	{
		char *line;

		line = (char *)( (term_t *)listText->list[i] )->text;
		drawFont(line, 10, 10 + i*20, COLOR_WHITE);
	}
}

void eventTerm()
{
	my_time_t currentTime;
	int i;

	currentTime = getMyTime();


	for( i = 0 ; i < listText->count ; i++ )
	{
		my_time_t thisTime;

		thisTime = ( (term_t *)listText->list[i] )->delTime;

		if( currentTime > thisTime )
		{
			delListItem(listText, i, destroyTermItem);
			i--;
		}
	}

}

void appendTextInTerm(char *s)
{

	addList(listText, newTermItem(s) );
	
	if( listText->count > TERM_MAX_LINES )
	{
		delListItem(listText, 0, destroyTermItem);
	}
}

void quitTerm()
{
	assert( listText != NULL );
	destroyListItem(listText, destroyTermItem);
}