aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 4a938b9d5917873ee416ebf0e63b721fe9948b31 (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
/* FujiNetChat, an IRC client. Based on NetCat and the old FujiChat. */

/*
#define SELF "FujiNetChat"
#define VERSION "0.0"
#define BANNER SELF " v" VERSION " (B. Watson)\n"
*/

#include <atari.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "nio.h"
#include "irc.h"
#include "screen.h"
#include "edbox.h"
#include "config.h"
#include "kgetc.h"
#include "indic8.h"

unsigned char err;              // error code of last operation.
unsigned char trip = 0;         // if trip == 1, fujinet is asking us for attention.
char old_enabled = 0;           // were interrupts enabled for old vector
void *old_vprced;               // old PROCEED vector, restored on exit.
unsigned short rxbuflen;        // RX buffer length
unsigned int txbuflen;          // TX buffer length
char hz; /* 50 for PAL, 60 for NSTC */
char reconnect_timeout = 1;

/* TODO: user modes (default +iw), fg/bg color... */

extern void ih();               // defined in intr.s

void txbuf_init(void) {
	txbuflen = tx_buf[0] = 0;
}

void txbuf_append_str(const char *str) {
	while(*str) {
		tx_buf[txbuflen++] = *str++;
	}
}

void txbuf_append_str2(const char *s1, const char *s2) {
	txbuf_append_str(s1);
	txbuf_append_str(s2);
}

void txbuf_append_str3(const char *s1, const char *s2, const char *s3) {
	txbuf_append_str(s1);
	txbuf_append_str(s2);
	txbuf_append_str(s3);
}

void txbuf_set_str(const char *str) {
	txbuf_init();
	txbuf_append_str(str);
}

void txbuf_set_str2(const char *s1, const char *s2) {
	txbuf_set_str(s1);
	txbuf_append_str(s2);
}

void txbuf_set_str3(const char *s1, const char *s2, const char *s3) {
	txbuf_set_str2(s1, s2);
	txbuf_append_str(s3);
}

void txbuf_send(void) {
	/* don't send empty buffer */
	if(!txbuflen) return;

	/* always terminate with *ASCII* CRLF.
	   DO NOT USE '\n' or even '\x0a', cc65 turns it into $9b! */
	tx_buf[txbuflen++] = 0x0d;
	tx_buf[txbuflen++] = 0x0a;

	ind_net_tx();
	nwrite(tx_buf, txbuflen);
	ind_net_idle();
	txbuf_init();
}

void txbuf_send_str(const char *str) {
	txbuf_init();
	txbuf_append_str(str);
	txbuf_send();
}

int fn_connect(void) {
	scr_display(SCR_SERVER);
	scr_print_current("Connecting to: ");
	scr_print_current(conf->url);
	scr_eol_current();

	err = nopen();

	if(err != SUCCESS) {
		scr_print_current("Connection failed: ");
		print_errnum();
		scr_eol_current();
		bell();
		return 0;
	}

	// Open successful, set up interrupt
	old_vprced  = OS.vprced;     // save the old interrupt vector 
	old_enabled = PIA.pactl & 1; // keep track of old interrupt state
	PIA.pactl  &= (~1);          // Turn off interrupts before changing vector
	OS.vprced   = ih;            // Set PROCEED interrupt vector to our interrupt handler.
	PIA.pactl  |= 1;             // Indicate to PIA we are ready for PROCEED interrupt.

	reconnect_timeout = 1;

	return 1;
}

void fn_disconnect(void) {
	nclose();

	// Restore old PROCEED interrupt.
	PIA.pactl &= ~1; // disable interrupts
	OS.vprced=old_vprced; 
	PIA.pactl |= old_enabled; 
}

void init_channels(void) {
	char i;

	for(i = 0; i < MAX_SCREENS - 2; i++) {
		if(conf->channels[i][0]) {
			scr_status[i + 2] = SCR_INACTIVE;
			strcpy(scr_names[i + 2], conf->channels[i]);
		}
	}
}

void reconnect(void) {
	scr_display(SCR_SERVER);
	scr_print_current("Press a key");

	/* IMO, 0xff is easier to understand than KEY_NONE, here */
	OS.cdtmf3 = OS.ch = 0xff;

	if(reconnect_timeout) {
		OS.cdtmv3 = reconnect_timeout * hz;
		scr_print_current(" or wait ");
		itoa(reconnect_timeout, numbuf, 10);
		scr_print_current(numbuf);
		scr_print_current(" sec");
		if(reconnect_timeout < 64)
			reconnect_timeout <<= 1;
	}
	scr_print_current(" to reconnect.\n");

	while(OS.cdtmf3 == 0xff && !keypress())
		/* NOP */;

	if(keypress()) kgetc();
}

void main(void) {
  bell_type = conf->alert_type; /* TODO: have bell.s read staight from the struct */
  OS.shflok = 0; // turn off shift-lock.
  OS.soundr = 0; // Turn off SIO beeping sound
  OS.color2 = conf->colors[0]; /* text BG, user-selected */
  // OS.color1 = (conf->colors[1] & 0x0f) | 0xc0; /* green (at user's brightness) */
  OS.color1 = conf->colors[1];
  OS.color0 = 0x06; /* grey for inactive */
  OS.color3 = 0x46; /* red for highlight (not used yet) */
  OS.noclik = conf->disable_keyclick;

  hz = (GTIA_READ.pal & 0x0e) ? 60 : 50;

  edbox_clear();
  scr_init();
  init_channels();

  while(1) {
	  edbox_callback = cmd_execute;
	  if(fn_connect()) {
		  ind_net_idle();
		  irc_register();
		  irc_loop();
	  }
	  OS.atract = 0;
	  fn_disconnect();
	  reconnect();
  }
}