summaryrefslogtreecommitdiffstats
path: root/loader/kon2/src/vc.c
blob: b60068b91724fb4d30ee5643c06dfeaaa1e7d502 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/*
 * KON2 - Kanji ON Console -
 * Copyright (C) 1992, 1993 MAEDA Atusi (mad@math.keio.ac.jp)
 * Copyright (C) 1992-1996 Takashi MANABE (manabe@papilio.tutics.tut.ac.jp)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY MAEDA ATUSI AND TAKASHI MANABE ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 */

#include	<config.h>
#include	<types.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<unistd.h>
#if defined(linux)
#include	<sys/vt.h>
#endif
#include	<fcntl.h>
#include	<signal.h>
#include	<termios.h>
#if defined(__FreeBSD__)
#include	<machine/console.h>
#endif
#include	<sys/ioctl.h>
#ifdef linux
#include	<sys/kd.h>
#endif

#include	<mem.h>
#include	<getcap.h>

#include	<defs.h>
#include	<errors.h>
#include	<fnld.h>
#ifndef	MINI_KON
#include	<mouse.h>
#endif
#include	<vc.h>
#include	<vt.h>
#include	<term.h>

struct dispInfo		dInfo;
struct cursorInfo	cInfo;
struct videoInfo	vInfo;
#ifndef	MINI_KON
static struct cursorInfo	mouseCursor;
#endif

static bool	textClear;

static int	textHead, scrollLine;
static u_int	textSize;
static u_char	*textBuff, *attrBuff, *flagBuff;

static int	saveTime, saverCount;
static bool	saved;
static bool	useHardScroll;
static volatile bool	busy;		 /* TRUE iff updating screen */
static volatile bool	release;	 /* delayed VC switch flag */

static void	ShowCursor(struct cursorInfo *, bool);

static void	LeaveVC(int);
static void	EnterVC(int);

/*

  flagBuff:
  |      7|      6|      5|4||3|2|1|0|
  |CLEAN_S|LATCH_2|LATCH_1| ||<----->|
  |0=latch|  byte2|  byte1| ||   LANG|

  */

#define	KON_TMP_FILE	"/tmp/.kontmp"

static
inline	void	blatch(void *head, int n)
{

    int i;
    unsigned char *p=head;

    for (i = 0; i < n; i ++, p ++) *p &= 0x7F;

#if 0
    __asm__("\t clc\n"
	    "1:\n"
	    "\t andb %%bl, (%%eax)\n"
	    "\t incl %%eax\n"
	    "\t loop 1b\n"
	    :
	    : "eax" ((long)head), "bl" (0x7F), "c" (n)
	    : "bl", "cx" );
#endif
}

static
inline	void	llatch(void *head, int n)
{
    blatch(head, n);

#if 0
    __asm__("\t clc\n"
	    "1:\n"
	    "\t andl %%ebx, (%%eax)\n"
	    "\t addl $4, %%eax\n"
	    "\t loop 1b\n"
	    :
	    : "eax" ((long)head), "ebx" (0x7F7F7F7F), "c" (n>>2)
	    : "ebx", "cx" );
#endif
}

static inline u_int	TextAddress(u_int x, u_int y)
{
    return (textHead + x + y * dInfo.glineByte) % textSize;
}

static inline bool	IsKanji(u_int x, u_int y)
{
    return(*(flagBuff + TextAddress(x, y)) & CODEIS_1);
}

static inline bool	IsKanji2(u_int x, u_int y)
{
    return(*(flagBuff + TextAddress(x, y)) & CODEIS_2);
}

void    euctosjis(ch, cl)
u_char  *ch, *cl;
{
    u_char  nh, nl;
    
    nh = ((*ch - 0x21) >> 1) + 0x81;
    if (nh > 0x9F) nh += 0x40;
    if (*ch & 1) {
	nl = *cl + 0x1F;
	if (*cl > 0x5F)
	    nl ++;
    } else nl = *cl + 0x7E;
    *cl = nl;
    *ch = nh;
}

void	TextDeleteChar(int n)
{
    u_int	addr, dx;
    
    addr = TextAddress(con.x, con.y);
    dx = dInfo.glineByte - con.x - n;
    
    memcpy(textBuff + addr, textBuff + addr + n, dx);
    memcpy(attrBuff + addr, attrBuff + addr + n, dx);
    memcpy(flagBuff + addr, flagBuff + addr + n, dx);

    blatch(flagBuff + addr, dx);
    
    addr = TextAddress(dInfo.glineByte - n, con.y);
    bzero(textBuff + addr, n);
    bzero(attrBuff + addr, n);
    bzero(flagBuff + addr, n);
}

void	TextInsertChar(int n)
{
    u_int	addr, dx;
    
    addr = TextAddress(con.x, con.y);

    dx = dInfo.txmax - con.x - n;
    memmove(textBuff + addr + n, textBuff + addr, dx);
    memmove(attrBuff + addr + n, attrBuff + addr, dx);
    memmove(flagBuff + addr + n, flagBuff + addr, dx);

    blatch(flagBuff + addr + n, dx);
    bzero(textBuff + addr, n);
    bzero(attrBuff + addr, n);
    bzero(flagBuff + addr, n);
}

void	TextRefresh(void)
{
    u_int	fnt, i;
    u_char	ch, ch2, fc, bc;
    
    busy = TRUE;
    if (!con.active) {
	busy = FALSE;
	return;
    }
    ShowCursor(&cInfo, FALSE);
#ifndef	MINI_KON
    ShowCursor(&mouseCursor, FALSE);
#endif
    if (textClear) vInfo.clear_all();
    if (useHardScroll) {
	if (scrollLine > 0) vInfo.hard_scroll_up(scrollLine);
	else if (scrollLine < 0) vInfo.hard_scroll_down(- scrollLine);
	scrollLine = 0;
    }
    textClear = FALSE;
    for (i = 0; i < textSize; i ++) {
	if (*(flagBuff + i)&CLEAN_S) continue; /* already clean */
	vInfo.set_address(i);
	fc = *(attrBuff + i);
	bc = *(attrBuff + i) >> 4;
	ch = *(textBuff + i);
	*(flagBuff + i) |= CLEAN_S;
	if (*(flagBuff + i) & CODEIS_1) {
	    dbFReg = &fDRegs[*(flagBuff + i)&LANG_CODE];
	    i ++;
	    *(flagBuff + i) |= CLEAN_S;
	    ch2 = *(textBuff + i);
	    fnt = dbFReg->addr(ch2, ch);
#if 0
		{
		    FILE *fp=fopen("errlog", "a");
		    fprintf(fp,"<%x %s %d %X %X %X>\n",
			    *(flagBuff + i - 1)&LANG_CODE,
			    dbFReg->registry, dbFReg->size, ch2, ch, fnt);
		    fclose(fp);
		}
#endif
	    if (con.ins) TextInsertChar(2);
	    if (fnt < dbFReg->size)
		vInfo.wput(dbFReg->bitmap + fnt, fc, bc);
	} else {
	    sbFReg = &fSRegs[*(flagBuff + i)&LANG_CODE];
#if 0
	    {
		FILE *fp=fopen("errlog", "a");
		fprintf(fp,"<%x %s %d>\n",
			*(flagBuff + i)&LANG_CODE,
			sbFReg->registry, sbFReg->size);
		fclose(fp);
	    }
#endif
	    if (con.ins) TextInsertChar(1);
	    vInfo.sput(ch ? sbFReg->bitmap + (ch << 4):0, fc, bc);
	}
    }
    cInfo.kanji = IsKanji(con.x, con.y);
    vInfo.set_cursor_address(&cInfo, con.x, con.y);
    ShowCursor(&cInfo, TRUE);
    busy = FALSE;
    if (release) LeaveVC(SIGUSR1);
}

static struct winsize text_win;

static void SetTextMode(void)
{
    ShowCursor(&cInfo, FALSE);
    vInfo.clear_all();
    vInfo.text_mode();
    ioctl(0, KDSETMODE, KD_TEXT);
    ioctl(0, TIOCCONS, NULL);
}

void TextMode(void)
{
    struct vt_mode vtm;

    signal(SIGUSR1, SIG_DFL);
    signal(SIGUSR2, SIG_DFL);
    vtm.mode = VT_AUTO;
    vtm.waitv = 0;
    vtm.relsig = 0;
    vtm.acqsig = 0;
    ioctl(0, VT_SETMODE, &vtm);
#if defined(__FreeBSD__)
    ioctl(0, VT_RELDISP, 1);
#endif
    con.text_mode = TRUE;
    SetTextMode();
    ioctl(masterPty, TIOCSWINSZ, &text_win);
}

void GraphMode(void)
{
    struct winsize win;
    struct vt_mode vtm;

    con.text_mode = FALSE;
    ioctl(0, KDSETMODE, KD_GRAPHICS);
#if defined(__FreeBSD__)
    ioctl(0, VT_RELDISP, VT_ACKACQ);
#endif
    signal(SIGUSR1, LeaveVC);
    signal(SIGUSR2, EnterVC);
    vtm.mode = VT_PROCESS;
    vtm.waitv = 0;
    vtm.relsig = SIGUSR1;
    vtm.acqsig = SIGUSR2;
    ioctl(0, VT_SETMODE, &vtm);
    vInfo.graph_mode();
    if (useHardScroll)
	vInfo.set_start_address();
    
    win.ws_row = dInfo.tymax + 1;	 /* Note: con.ymax may be changed by application */
    win.ws_col = dInfo.txmax + 1;
    win.ws_xpixel = win.ws_ypixel = 0;
    ioctl(masterPty, TIOCSWINSZ, &win);
    ioctl(masterPty, TIOCCONS, NULL);
    
    llatch(flagBuff, textSize);
    textClear = TRUE;
    TextRefresh();
}

static
    void	LeaveVC(int signum)
{

    signal(SIGUSR1, LeaveVC);  /* should use sigaction()? */
    if (busy) {
        release = TRUE;
	return;
    }
    release = FALSE;
    con.active = FALSE;
    SetTextMode();
#ifdef	HAS_MOUSE
    if (mInfo.has_mouse) {
	MouseResetRfd(mouseFd);
	MouseCleanup();
    }
#endif
    ioctl(0, VT_RELDISP, 1);
}

static
    void	EnterVC(int signum)
{
    signal(SIGUSR2, EnterVC);
    if (!con.active) {
	con.active = TRUE;
	GraphMode();
    signal(SIGUSR2, EnterVC);
#ifdef	HAS_MOUSE
	if (mInfo.has_mouse) {
	    MouseStart();
	    MouseSetRfd(mouseFd);
	}
#endif
    }
}

static
    void	TextScrollUp(int line)
{
    int	oldhead, len;
    
    oldhead = textHead;
    textHead += line * dInfo.glineByte;
    if (textHead > textSize) {
	textHead -= textSize;
	len = textSize - oldhead;
	if (textHead) {
	    bzero(textBuff, textHead);
	    bzero(attrBuff, textHead);
	    bzero(flagBuff, textHead);
	}
    } else len = textHead - oldhead;
    bzero(textBuff + oldhead, len);
    bzero(attrBuff + oldhead, len);
    bzero(flagBuff + oldhead, len);
}

static
    void	TextScrollDown(int line)
{
    int	oldhead, len;
    
    oldhead = textHead;
    textHead -= line * dInfo.glineByte;
    if (textHead < 0) {
	textHead += textSize;
	if (oldhead) {
	    bzero(textBuff, oldhead);
	    bzero(attrBuff, oldhead);
	    bzero(flagBuff, oldhead);
	}
	len = textSize - textHead;
    } else len = oldhead - textHead;
    bzero(textBuff + textHead, len);
    bzero(attrBuff + textHead, len);
    bzero(flagBuff + textHead, len);
}

void TextWput(u_char ch1, u_char ch2)
{
    u_int addr;
    u_char *p;
    
    addr = TextAddress(con.x, con.y);
    *(attrBuff + addr) = con.fcol | (con.bcol << 4);
    *(p = textBuff + addr) = ch2;
    *(p + 1) = ch1;
    *(p = flagBuff + addr) = con.db;
    *(p + 1) = LATCH_2;
}

void TextSput(u_char ch)
{
    u_int addr;

    addr = TextAddress(con.x, con.y);
    *(flagBuff + addr) = LATCH_S|con.sb;
    *(attrBuff + addr) = con.fcol | (con.bcol << 4);
    *(textBuff + addr) = ch;
}

void TextClearAll(void)
{
#if 1
    u_int y, addr;

    for (y = 0; y <= con.ymax; y ++) {
	addr = TextAddress(0, y);
	bzero(textBuff + addr, dInfo.glineByte);
	bzero(attrBuff + addr, dInfo.glineByte);
    }
#else
    bzero(textBuff, textSize);
    bzero(attrBuff, textSize);
#endif
    bzero(flagBuff, textSize);
#ifndef	MINI_KON
    mInfo.sw = 0;
#endif
    textClear = TRUE;
}

void	TextClearEol(u_char mode)
{
    u_int	addr;
    u_char	len, x=0;
    
    switch(mode) {
    case 1:
	len = con.x;
	break;
    case 2:
	len = dInfo.glineByte;
	break;
    default:
	x = con.x;
	len = dInfo.glineByte - con.x;
	break;
    }
    addr = TextAddress(x, con.y);
    bzero(textBuff + addr, len);
    bzero(attrBuff + addr, len);
    bzero(flagBuff + addr, len);/* needless to latch */
}

void	TextClearEos(u_char mode)
{
    u_int	addr, len, y;
    
    if (mode == 2) {
	TextClearAll();
	return;
    }
    switch(mode) {
    case 1:
	for (y = 0; y < con.y; y ++) {
	    addr = TextAddress(0, y);
	    bzero(textBuff + addr, dInfo.glineByte);
	    bzero(attrBuff + addr, dInfo.glineByte);
	    bzero(flagBuff + addr, dInfo.glineByte);/* needless to latch */
	}
	addr = TextAddress(0, con.y);
	bzero(textBuff + addr, con.x);
	bzero(attrBuff + addr, con.x);
	bzero(flagBuff + addr, con.x);/* needless to latch */
	break;
    default:
	for (y = con.y + 1; y <= con.ymax; y ++) {
	    addr = TextAddress(0, y);
	    bzero(textBuff + addr, dInfo.glineByte);
	    bzero(attrBuff + addr, dInfo.glineByte);
	    bzero(flagBuff + addr, dInfo.glineByte);/* needless to latch */
	}
	addr = TextAddress(con.x, con.y);
	len = dInfo.glineByte - con.x;
	bzero(textBuff + addr, len);
	bzero(attrBuff + addr, len);
	bzero(flagBuff + addr, len);/* needless to latch */
	break;
    }
}

static
    void	TextClearBand(u_int top, u_int btm)
{
    u_int	y, addr;
    
    for (y = top; y <= btm; y ++) {
	addr = TextAddress(0, y);
	bzero(textBuff + addr, dInfo.glineByte);
	bzero(attrBuff + addr, dInfo.glineByte);
	bzero(flagBuff + addr, dInfo.glineByte);/* needless to latch */
    }
}

void	TextMoveDown(int top, int btm, int line)
{
    u_int	n, src, dst;
    
    if (btm - top - line + 1 <= 0) {
	TextClearBand(top, btm);
	return;
    }
    for (n = btm; n >= top + line; n --) {
	dst = TextAddress(0, n);
	src = TextAddress(0, n - line);
	memcpy(textBuff + dst, textBuff + src, dInfo.glineByte);
	memcpy(attrBuff + dst, attrBuff + src, dInfo.glineByte);
	memcpy(flagBuff + dst, flagBuff + src, dInfo.glineByte);
	llatch(flagBuff + dst, dInfo.glineByte);
    }
    TextClearBand(top, top + line - 1);
}

void	TextMoveUp(int top, int btm, int line)
{
    u_int	n, src, dst;
    
    if (btm - top - line + 1 <= 0) {
	TextClearBand(top, btm);
	return;
    }
    for (n = top; n <= btm - line; n ++) {
	dst = TextAddress(0, n);
	src = TextAddress(0, n + line);
	memcpy(textBuff + dst, textBuff + src, dInfo.glineByte);
	memcpy(attrBuff + dst, attrBuff + src, dInfo.glineByte);
	memcpy(flagBuff + dst, flagBuff + src, dInfo.glineByte);
	llatch(flagBuff + dst, dInfo.glineByte);
    }
    TextClearBand(btm - line + 1, btm);
}

void	ScrollUp(int line)
{
    if (useHardScroll && !con.soft) {
	TextScrollUp(line);
	scrollLine += line;
    } else
	TextMoveUp(con.ymin, con.ymax, line);
}

void	ScrollDown(int line)
{
    if (useHardScroll && !con.soft) {
	TextScrollDown(line);
	scrollLine -= line;
    } else
	TextMoveDown(con.ymin, con.ymax, line);
}

static inline void	KanjiAdjust(int *x, int *y)
{
    if (IsKanji2(*x, *y)) {
	--*x;
    }
}

void	TextReverse(int fx, int fy, int tx, int ty)
{
    u_int	from, to, y, swp, xx, x;
    u_char	fc, bc, fc2, bc2;
    
    KanjiAdjust(&fx, &fy);
    KanjiAdjust(&tx, &ty);
    if (fy > ty) {
	swp = fy;
	fy = ty;
	ty = swp;
	swp = fx;
	fx = tx;
	tx = swp;
    } else if (fy == ty && fx > tx) {
	swp = fx;
	fx = tx;
	tx = swp;
    }
    for (xx = dInfo.txmax, y = fy; y <= ty; y ++) {
	if (y == ty) xx = tx;
	from = TextAddress(fx, y);
	to = TextAddress(xx, y);
	if (flagBuff[from] & CODEIS_2)
	    /* 2nd byte of kanji */
	    from--;
	for (x = from; x <= to; x ++) {
	    if (!textBuff[x]) continue;
	    fc = attrBuff[x];
	    bc = fc >> 4;
	    bc2 = (bc & 8) | (fc & 7);
	    fc2 = (fc & 8) | (bc & 7);
	    attrBuff[x] = fc2 | (bc2 << 4);
	    flagBuff[x] &= ~CLEAN_S;
	}
	fx = 0;
    }
}

#ifndef	MINI_KON

void	TextCopy(int fx, int fy, int tx, int ty)
{
    int	fd;
    u_int	from, to, y, swp, xx, x;
    u_char	ch, ch2;

    unlink(KON_TMP_FILE);
    if ((fd = open(KON_TMP_FILE, O_WRONLY|O_CREAT, 0600)) < 0) return;

    KanjiAdjust(&fx, &fy);
    KanjiAdjust(&tx, &ty);
    if (fy > ty) {
	swp = fy;
	fy = ty;
	ty = swp;
	swp = fx;
	fx = tx;
	tx = swp;
    } else if (fy == ty && fx > tx) {
	swp = fx;
	fx = tx;
	tx = swp;
    }
    for (xx = dInfo.txmax, y = fy; y <= ty; y ++) {
	if (y == ty) xx = tx;
	from = TextAddress(fx, y);
	if (flagBuff[from] & CODEIS_2)
	    /* 2nd byte of kanji */
	    from--;
	to = TextAddress(xx, y);
	for (x = to; x >= from; x --) if (textBuff[x] > ' ') break;
	to = x;
	for (x = from; x <= to; x ++) {
	    ch = textBuff[x];
	    if (!ch) ch = ' ';
	    if (flagBuff[x] & CODEIS_1) {
		x ++;
		ch2 = textBuff[x];
		switch(lInfo.sc) {
		case CODE_EUC:
		    ch2 |= 0x80;
		    ch |= 0x80;
		    break;
		case CODE_SJIS:
		    jistosjis(ch2, ch);
		    break;
		}
		write(fd, &ch2, 1);
		write(fd, &ch, 1);
	    } else write(fd, &ch, 1);
	}
	if (y < ty) {
	    ch = '\n';
	    write(fd, &ch, 1);
	}
	fx = 0;
    }
    close(fd);
}

void	TextPaste(void)
{
    u_char	ch;
    int	fd;
    
    if ((fd = open(KON_TMP_FILE, O_RDONLY)) < 0) return;
    while(read(fd, &ch, 1) == 1) write(masterPty, &ch, 1);
    close(fd);
}

#endif

/* Cursor related routines. */

static void	ToggleCursor(struct cursorInfo *c)
{
    c->count = 0;
    if (con.text_mode)
	return;
    c->shown = ! c->shown;
    vInfo.cursor(c);
}

static void	ShowCursor(struct cursorInfo *c, bool show)
{
    if (!con.active || !c->sw)
	return;
    if (c->shown != show)
	ToggleCursor(c);
}

static void	SaveScreen(bool save)
{
    if (saved != save) {
	saved = save;
	vInfo.screen_saver(save);
    }
    saverCount = 0;
}

#ifndef	MINI_KON
static void PollMouseCursor(void)
{
    ShowCursor(&mouseCursor, FALSE);
    if (mInfo.sw > 0) {
	--mInfo.sw;
	if (cInfo.shown) {
	    int	x = mInfo.x, y = mInfo.y;
	    
	    KanjiAdjust(&x, &y);
	    mouseCursor.kanji = IsKanji(x, y);
	    vInfo.set_cursor_address(&mouseCursor, x, y);
	    ShowCursor(&mouseCursor, TRUE);
	}
    }
}
#endif

/* Called when some action was over, or every 1/10 sec when idle. */

void	PollCursor(bool wakeup)
{
    if (!con.active)
	return;
    if (wakeup) {
	SaveScreen(FALSE);
	ShowCursor(&cInfo, TRUE);
#ifndef	MINI_KON
	PollMouseCursor();
#endif
	return;
    }
    /* Idle. */
    if (saved)
	return;
    if ((saveTime > 0) && (++saverCount == saveTime)) {
	ShowCursor(&cInfo, FALSE);
#ifndef	MINI_KON
	ShowCursor(&mouseCursor, FALSE);
#endif
	SaveScreen(TRUE);
	return;
    }
    if ((cInfo.interval > 0) && (++cInfo.count == cInfo.interval)) {
	ToggleCursor(&cInfo);
    }
#ifndef	MINI_KON
    if (mInfo.has_mouse) {
	PollMouseCursor();
    }
#endif
}

/* Configuration routines. */

extern int	SvgaSetVideoType(struct videoInfo*, const char*);
extern int	VgaFmSetVideoType(struct videoInfo*, const char*);
extern int	S3SetVideoType(struct videoInfo*, const char*);
extern int	J31SXSetVideoType(struct videoInfo*, const char*);

static struct videoconf {
    const char *name;
    int	(*set)(struct videoInfo*, const char*);
} videos[] = {
#ifdef	HAS_VGA
    {"VGA", SvgaSetVideoType},
#ifndef	MINI_KON
    {"VGAFM", VgaFmSetVideoType},
    {"EGA", SvgaSetVideoType},
    {"SVGA", SvgaSetVideoType},
#endif
#endif
#ifdef	HAS_S3
    {"S3", S3SetVideoType},
#endif
#ifdef	HAS_J31SX
    {"J3100SX", J31SXSetVideoType},
#endif
    {NULL, NULL}
};

static int	ConfigHardScroll(const char *confstr)
{
    bool value = BoolConf(confstr);
    useHardScroll = value;
    if (value) {
	message("hardware scroll mode.\r\n");
    }
    return SUCCESS;
}

static	char	*videoName;

static int	ConfigDisplay(const char *config)
{
    struct videoconf *v;
    char name[MAX_COLS];
    
    sscanf(config, "%s", name);
    for (v = videos; v->name != NULL; v++) {
	if (strcasecmp(name, v->name) == 0) {
	    config = strchr(config, '\n');
	    if (config == NULL) {
		error("invalid entry for %s\r\n", videoName);
		return FAILURE;
	    }
	    if (v->set(&vInfo, config) == FAILURE)
		return FAILURE;
	    message("video type `%s' selected\r\n", name);
	    if (vInfo.has_hard_scroll) {
		DefineCap("HardScroll", ConfigHardScroll, "On");
	    } else
		useHardScroll = FALSE;
	    return SUCCESS;
	}
    }
    error("unknown video type `%s'\r\n", name);
    return FAILURE;
}

/* Beep routines. */

#define	COUNTER_ADDR	0x61

static int	beepCount;

static int	ConfigBeep(const char *confstr)
{
    beepCount = atoi(confstr) * 10000;
#if defined(linux)
    if (beepCount > 0)
	ioperm(COUNTER_ADDR, 1, TRUE);
#endif
    return SUCCESS;
}

void	Beep(void)
{
    if (!con.active || beepCount <= 0) return;
#if defined(linux)
    PortOutb(PortInb(COUNTER_ADDR)|3, COUNTER_ADDR);
    usleep(beepCount);
    PortOutb(PortInb(COUNTER_ADDR)&0xFC, COUNTER_ADDR);
#endif
}

static int	ConfigInterval(const char *confstr)
{
    cInfo.interval = atoi(confstr);
#ifndef	MINI_KON
    mouseCursor.interval = cInfo.interval;
#endif
    return SUCCESS;
}

static int	ConfigSaver(const char *confstr)
{
    saveTime = atoi(confstr) * 600; /* convert unit from minitue to 1/10 sec */
    return SUCCESS;
}

/* Initialize routine. */

void	ConsoleInit(const char *video)
{
    videoName = strdup(video);
    DefineCap(videoName, ConfigDisplay, NULL);
    DefineCap("BeepCounter", ConfigBeep, "5");
    DefineCap("CursorInterval", ConfigInterval, "4");
    DefineCap("SaveTime", ConfigSaver, "4");
}

void	ConsoleStart(void)
{
    /* What to do if calloc failed? */
    textBuff = (u_char *)calloc(dInfo.glineByte, dInfo.tymax + 1);
    attrBuff = (u_char *)calloc(dInfo.glineByte, dInfo.tymax + 1);
    flagBuff = (u_char *)calloc(dInfo.glineByte, dInfo.tymax + 1);
    textSize = dInfo.glineByte * (dInfo.tymax + 1);
    ioctl(0, KDSETMODE, KD_GRAPHICS);
    ioctl(0, TIOCGWINSZ, &text_win);
    vInfo.init();
    cInfo.shown = FALSE;
#ifndef	MINI_KON
    mouseCursor.shown = FALSE;
    mouseCursor.sw = TRUE;
#endif
    saved = FALSE;
    GraphMode();
}

void	ConsoleCleanup(void)
{
    scrollLine = textHead = 0;
    vInfo.detatch();
    SafeFree((void **)&textBuff);
    SafeFree((void **)&attrBuff);
    SafeFree((void **)&flagBuff);
#ifdef linux
    ioperm(COUNTER_ADDR, 1, FALSE);
#endif
    
    SafeFree((void **)&videoName);
}