Subversion Repositories Kolibri OS

Rev

Rev 3885 | Rev 4306 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
836 diamond 1
; Функции работы с консолью для программ КолибриОС
852 diamond 2
; diamond, 2006-2008
836 diamond 3
 
4
 
5
format MS COFF
6
 
7
public EXPORTS
8
 
9
section '.flat' code readable align 16
10
 
11
include 'font.inc'
12
include 'conscrl.inc'
13
 
14
;void __stdcall START(dword state);
15
START:
16
; N.B. The current kernel implementation does not require
17
;      evident heap initialization, because if DLL is loaded, heap is already initialized
18
;      (if heap was not initialized, loader does this implicitly).
19
;      So this action does nothing useful, but nothing harmful.
20
        push    68
21
        pop     eax
22
        push    11
23
        pop     ebx
24
        int     0x40
25
        or      eax, -1
26
        ret     4
27
 
28
; Инициализация консоли
29
; void __stdcall con_init(dword wnd_width, dword wnd_height,
30
;       dword scr_width, dword scr_height, const char* title);
31
 
32
align 4
33
con_init:
34
        pop     eax
35
        pop     [con.wnd_width]
36
        pop     [con.wnd_height]
37
        pop     [con.scr_width]
38
        pop     [con.scr_height]
39
        pop     [con.title]
40
        push    eax
2170 serge 41
 
42
        push ebx
43
 
836 diamond 44
        mov     ecx, 4
45
        mov     eax, con.wnd_width
2170 serge 46
        mov     edx, con.def_wnd_width
836 diamond 47
.1:
48
        cmp     dword [eax], -1
49
        jnz     @f
2170 serge 50
        mov     ebx, [edx]
836 diamond 51
        mov     [eax], ebx
52
@@:
53
        add     eax, 4
2170 serge 54
        add     edx, 4
836 diamond 55
        loop    .1
56
; allocate memory for console data & bitmap data
57
        mov     eax, [con.scr_width]
58
        mul     [con.scr_height]
59
        lea     ecx, [eax+eax]
60
        mov     eax, [con.wnd_width]
61
        mul     [con.wnd_height]
62
        imul    eax, font_width*font_height
63
        mov     ebx, eax
64
        push    ebx ecx
65
        add     ecx, eax
66
        push    68
67
        pop     eax
68
        push    12
69
        pop     ebx
70
        int     0x40
71
        pop     ecx ebx
72
        mov     edx, con.nomem_err
73
        test    eax, eax
74
        jz      con.fatal
75
        mov     [con.data], eax
76
        push    edi
77
        mov     edi, eax
78
        shr     ecx, 1
79
        mov     ax, 0x0720
80
        rep     stosw
81
        mov     ecx, ebx
82
        mov     [con.image], edi
83
        xor     eax, eax
84
        rep     stosb
85
        pop     edi
1133 diamond 86
        and     byte [con_flags+1], not 2
836 diamond 87
; create console thread
88
        push    51
89
        pop     eax
90
        xor     ebx, ebx
91
        inc     ebx
92
        mov     ecx, con.thread
93
        mov     edx, con.stack_top
94
        int     0x40
95
        mov     edx, con.thread_err
96
        test    eax, eax
97
        js      con.fatal
98
        mov     [con.console_tid], eax
99
        pop     ebx
100
        ret
101
con.fatal:
102
; output string to debug board and die
103
        mov     cl, [edx]
104
        test    cl, cl
105
        jz      @f
106
        push    63
107
        pop     eax
108
        xor     ebx, ebx
109
        inc     ebx
110
        int     0x40
111
        inc     edx
112
        jmp     con.fatal
113
@@:
114
        or      eax, -1
115
        int     0x40
116
 
117
; dword __stdcall con_get_flags(void);
118
con_get_flags:
119
        mov     eax, [con_flags]
120
        ret
121
 
122
; dword __stdcall con_set_flags(dword flags);
123
con_set_flags:
124
        mov     eax, [esp+4]
1133 diamond 125
        and     ah, not 2
836 diamond 126
        xchg    eax, [con_flags]
127
        ret     4
128
 
129
; dword __stdcall con_get_font_height(void);
130
con_get_font_height:
131
        mov     eax, font_height
132
        ret
133
 
134
; int __stdcall con_get_cursor_height(void);
135
con_get_cursor_height:
136
        mov     eax, [con.cursor_height]
137
        ret
138
 
139
; int __stdcall con_set_cursor_height(int new_height);
140
con_set_cursor_height:
141
        mov     eax, [esp+4]
142
        cmp     eax, font_height
143
        jae     @f
144
        xchg    eax, [con.cursor_height]
145
        ret     4
146
@@:
147
        mov     eax, [con.cursor_height]
148
        ret     4
149
 
150
; void __stdcall con_write_asciiz(const char* string);
151
con_write_asciiz:
152
        push    ebx esi
153
        or      ebx, -1
154
        mov     esi, [esp+12]
155
        call    con.write
156
        pop     esi ebx
157
        ret     4
158
 
159
; void __stdcall con_write_string(const char* string, dword length);
160
con_write_length:
161
        push    ebx esi
162
        mov     esi, [esp+12]
163
        mov     ebx, [esp+16]
164
        call    con.write
165
        pop     esi ebx
166
        ret     8
167
 
168
; Каждый символ классифицируется как один из
169
con.printfc.normal = 0   ; нормальный символ
170
con.printfc.percent = 1  ; '%'
171
con.printfc.dot = 2      ; '.'
172
con.printfc.asterisk = 3 ; '*'
173
con.printfc.zero = 4     ; '0'
174
con.printfc.digit = 5    ; ненулевая цифра
175
con.printfc.plus = 6     ; '+'
176
con.printfc.minus = 7    ; '-'
177
con.printfc.sharp = 8    ; '#'
178
con.printfc.space = 9    ; ' '
179
con.printfc.long = 10    ; 'l' for 'long'
180
con.printfc.short = 11   ; 'h' for 'short'
181
con.printfc.dec = 12     ; 'd' = print decimal
182
con.printfc.oct = 13     ; 'o' = print octal
183
con.printfc.unsigned = 14 ; 'u' = print unsigned decimal
184
con.printfc.hex = 15     ; 'x' = print hexadecimal
185
con.printfc.pointer = 16 ; 'p' = print pointer
186
con.printfc.char = 17    ; 'c' = print char
187
con.printfc.string = 18  ; 's' = print string
188
 
189
macro set char,type
190
{store byte con.printfc.#type at con.charcodes + char - ' '}
191
 
192
con.charcodes:
193
times 'x'-' '+1         db      con.printfc.normal
194
        set     '%', percent
195
        set     '.', dot
196
        set     '*', asterisk
197
        set     '0', zero
198
        set     '1', digit
199
        set     '2', digit
200
        set     '3', digit
201
        set     '4', digit
202
        set     '5', digit
203
        set     '6', digit
204
        set     '7', digit
205
        set     '8', digit
206
        set     '9', digit
207
        set     ' ', space
208
        set     '#', sharp
209
        set     '+', plus
210
        set     '-', minus
211
        set     'X', hex
212
        set     'x', hex
213
        set     'c', char
214
        set     'd', dec
215
        set     'h', short
216
        set     'i', dec
217
        set     'l', long
218
        set     'o', oct
219
        set     'p', pointer
220
        set     's', string
221
        set     'u', unsigned
222
purge set
223
align 4
3690 hidnplayr 224
con.charjump:
225
        dd      con_printf.normal
836 diamond 226
        dd      con_printf.percent
227
        dd      con_printf.dot
228
        dd      con_printf.asterisk
229
        dd      con_printf.zero
230
        dd      con_printf.digit
231
        dd      con_printf.plus
232
        dd      con_printf.minus
233
        dd      con_printf.sharp
234
        dd      con_printf.space
235
        dd      con_printf.long
236
        dd      con_printf.short
237
        dd      con_printf.dec
238
        dd      con_printf.oct
239
        dd      con_printf.unsigned
240
        dd      con_printf.hex
241
        dd      con_printf.pointer
242
        dd      con_printf.char
243
        dd      con_printf.string
244
 
245
; int __cdecl con_printf(const char* format, ...)
246
con_printf:
247
        xor     eax, eax
248
        pushad
249
        call    con.get_data_ptr
250
        lea     ebp, [esp+20h+8]
251
        mov     esi, [ebp-4]
252
        sub     esp, 64         ; reserve space for buffer
253
.loop:
254
        xor     eax, eax
255
        lodsb
256
        test    al, al
257
        jz      .done
258
        cmp     al, '%'
259
        jz      .spec_begin
260
.normal:
261
        call    con.write_char_ex
262
        inc     dword [esp+64+28]
263
        jmp     .loop
264
.errspec:
265
.percent:
266
        add     esp, 12
267
        jmp     .normal
268
.spec_begin:
269
        xor     ebx, ebx
270
; bl = тип позиции:
271
; 0 = начало
272
; 1 = прочитан ведущий 0 в спецификации формата
273
; 2 = читаем поле ширины
274
; 3 = читаем поле точности
275
; 4 = прочитано поле размера аргумента
276
; 5 = читаем поле типа
277
; bh = флаги:
278
; 1 = флаг '#', выводить 0/0x/0X
279
; 2 = флаг '-', выравнивание влево
280
; 4 = флаг '0', дополнение нулями
281
; 8 = флаг 'h', короткий аргумент
282
        push    -1
283
; dword [esp+8] = precision
284
        push    -1
285
; dword [esp+4] = width
286
        push    0
287
; byte [esp] = флаг 0/'+'/' '
288
.spec:
289
        xor     eax, eax
290
        lodsb
291
        test    al, al
292
        jz      .done
293
        cmp     al, ' '
294
        jb      .normal
295
        cmp     al, 'x'
296
        ja      .normal
297
        movzx   ecx, byte [con.charcodes + eax - ' ']
3690 hidnplayr 298
        jmp     dword[con.charjump + ecx*4]
836 diamond 299
 
300
.sharp:
301
        test    bl, bl
302
        jnz     .errspec
303
        or      bh, 1
304
        jmp     .spec
305
.minus:
306
        test    bl, bl
307
        jnz     .errspec
308
        or      bh, 2
309
        jmp     .spec
310
.plus:
311
.space:
312
        test    bl, bl
313
        jnz     .errspec
314
        cmp     byte [esp], '+'
315
        jz      .spec
316
        mov     byte [esp], al
317
        jmp     .spec
318
.zero:
319
        test    bl, bl
320
        jnz     .digit
321
        test    bh, 2
322
        jnz     .spec
323
        or      bh, 4
324
        inc     ebx
325
        jmp     .spec
326
.digit:
327
        sub     al, '0'
328
        cmp     bl, 2
329
        ja      .precision
330
        mov     bl, 2
331
        xchg    eax, [esp+4]
332
        test    eax, eax
333
        js      .spec
334
        lea     eax, [eax*5]
335
        add     eax, eax
336
        add     [esp+4], eax
337
        jmp     .spec
338
.precision:
339
        cmp     bl, 3
340
        jnz     .errspec
341
        xchg    eax, [esp+8]
342
        lea     eax, [eax*5]
343
        add     eax, eax
344
        add     [esp+8], eax
345
        jmp     .spec
346
.asterisk:
347
        mov     eax, [ebp]
348
        add     ebp, 4
349
        cmp     bl, 2
350
        ja      .asterisk_precision
351
        test    eax, eax
352
        jns     @f
353
        neg     eax
354
        or      bh, 2
355
@@:
356
        mov     [esp+4], eax
357
        mov     bl, 3
358
        jmp     .spec
359
.asterisk_precision:
360
        cmp     bl, 3
361
        jnz     .errspec
362
        mov     [esp+8], eax
363
        inc     ebx
364
        jmp     .spec
365
.dot:
366
        cmp     bl, 2
367
        ja      .errspec
368
        mov     bl, 3
369
        and     dword [esp+8], 0
370
        jmp     .spec
371
.long:
372
        cmp     bl, 3
373
        ja      .errspec
374
        mov     bl, 4
375
        jmp     .spec
376
.short:
377
        cmp     bl, 3
378
        ja      .errspec
379
        mov     bl, 4
380
        or      bh, 8
381
        jmp     .spec
382
.unsigned:
383
.dec:
384
        push    10
385
        jmp     .write_number
386
.pointer:
387
        mov     dword [esp+12], 8
388
        or      bh, 4
389
        and     bh, not 8
390
.hex:
391
        push    16
392
        jmp     @f
393
.oct:
394
        push    8
395
@@:
396
        mov     byte [esp+4], 0
397
.write_number:
398
        pop     ecx
399
        push    edi
400
        lea     edi, [esp+16+64-1]      ; edi -> end of buffer
401
        mov     byte [edi], 0
402
        push    edx
403
        push    eax
404
        mov     eax, [ebp]
405
        add     ebp, 4
406
        test    bh, 8
407
        jz      @f
408
        movzx   eax, ax
409
        cmp     byte [esp], 'd'
410
        jnz     @f
411
        movsx   eax, ax
412
@@:
413
        xor     edx, edx
414
        test    eax, eax
415
        jns     @f
416
        cmp     byte [esp], 'd'
417
        jnz     @f
418
        inc     edx
419
        neg     eax
420
@@:
421
        push    edx
422
        xor     edx, edx
423
; число в eax, основание системы счисления в ecx
424
@@:
425
        cmp     dword [esp+16+8], 0
426
        jnz     .print_num
427
        test    eax, eax
428
        jz      .zeronum
429
.print_num:
430
        div     ecx
431
        xchg    eax, edx
432
        cmp     al, 10
433
        sbb     al, 69h
434
        das
435
        cmp     byte [esp+4], 'x'
436
        jnz     @f
437
        or      al, 20h
438
@@:
439
        dec     edi
440
        mov     [edi], al
441
        xor     eax, eax
442
        xchg    eax, edx
443
        test    eax, eax
444
        jnz     .print_num
445
.zeronum:
446
        push    0
447
        mov     edx, [esp+12]
448
        lea     eax, [esp+32+64-1]
449
        sub     eax, edi
450
        cmp     dword [esp+20+8], -1
451
        jz      .noprec1
452
        cmp     eax, [esp+20+8]
453
        jae     .len_found1
454
        mov     eax, [esp+20+8]
455
        jmp     .len_found1
456
.noprec1:
457
        test    bh, 4
458
        jnz     .do_print_num
459
.len_found1:
460
        test    bh, 2
461
        jnz     .do_print_num
462
        cmp     byte [esp+20], 0
463
        jz      @f
464
        inc     eax
465
@@:
466
        cmp     byte [esp+20], 0
467
        jnz     @f
468
        cmp     byte [esp+4], 0
469
        jz      @f
470
        inc     eax
471
@@:
472
        test    bh, 1
473
        jz      .nosharp1
474
        cmp     cl, 8
475
        jnz     @f
476
        inc     eax
477
        jmp     .nosharp1
478
@@:
479
        cmp     cl, 16
480
        jnz     .nosharp1
481
        inc     eax
482
        inc     eax
483
.nosharp1:
484
        cmp     dword [esp+20+4], -1
485
        jz      .do_print_num
486
        sub     eax, [esp+20+4]
487
        jae     .do_print_num
488
        push    ecx
489
        mov     ecx, eax
490
        mov     al, ' '
491
@@:
492
        xchg    edi, [esp+20]
493
        call    con.write_char_ex
494
        inc     dword [esp+24+12+64+28]
495
        xchg    edi, [esp+20]
496
        inc     dword [esp+4]
497
        inc     ecx
498
        jnz     @b
499
        pop     ecx
500
.do_print_num:
501
        mov     al, '-'
502
        cmp     byte [esp+4], 0
503
        jnz     .write_sign
504
        mov     al, [esp+20]
505
        test    al, al
506
        jz      .sign_written
507
.write_sign:
508
        call    .num_write_char
509
.sign_written:
510
        test    bh, 1
511
        jz      .nosharp2
512
        mov     al, '0'
513
        cmp     cl, 8
514
        jz      @f
515
        cmp     cl, 16
516
        jnz     .nosharp2
517
        call    .num_write_char
518
        mov     al, [esp+8]
519
@@:
520
        call    .num_write_char
521
.nosharp2:
522
        lea     ecx, [esp+32+64-1]
523
        sub     ecx, edi
524
        cmp     dword [esp+20+8], -1
525
        jz      .noprec2
526
        sub     ecx, [esp+20+8]
527
        jmp     .lead_zeroes
528
.noprec2:
529
        test    bh, 4
530
        jz      .do_print_num2
531
        add     ecx, [esp]
532
        sub     ecx, [esp+20+4]
533
.lead_zeroes:
534
        jae     .do_print_num2
535
@@:
536
        mov     al, '0'
537
        call    .num_write_char
538
        inc     ecx
539
        jnz     @b
540
.do_print_num2:
541
        mov     al, [edi]
542
        test    al, al
543
        jz      .num_written
544
        call    .num_write_char
545
        inc     edi
546
        jmp     .do_print_num2
547
.num_written:
548
        pop     ecx
549
        mov     edi, [esp+12]
550
        cmp     dword [esp+16+4], -1
551
        jz      .num_written2
552
@@:
553
        cmp     ecx, [esp+16+4]
554
        jae     .num_written2
555
        mov     al, ' '
556
        call    con.write_char
557
        inc     ecx
558
        jmp     @b
559
.num_written2:
560
        add     esp, 16
561
.spec_done:
562
        add     esp, 12
563
        jmp     .loop
564
.char:
565
        mov     ecx, [esp+4]
566
        cmp     ecx, -1
567
        jnz     @f
568
        inc     ecx
569
@@:
570
        test    ecx, ecx
571
        jnz     @f
572
        inc     ecx
573
@@:
574
        test    bh, 2
575
        jnz     .char_left_pad
576
        mov     al, ' '
577
        dec     ecx
578
        jz      .nowidth
579
        add     [esp+12+64+28], ecx
580
@@:
581
        call    con.write_char
582
        loop    @b
583
.nowidth:
584
        mov     al, [ebp]
585
        add     ebp, 4
586
        jmp     .percent
587
.char_left_pad:
588
        mov     al, [ebp]
589
        add     ebp, 4
590
        call    con.write_char_ex
591
        add     [esp+12+64+28], ecx
592
        dec     ecx
593
        jz      .nowidth2
594
        mov     al, ' '
595
@@:
596
        call    con.write_char
597
        loop    @b
598
.nowidth2:
599
        jmp     .spec_done
600
.string:
601
        push    esi
602
        mov     esi, [ebp]
603
        test    esi, esi
604
        jnz     @f
605
        mov     esi, con.aNull
606
@@:
607
        add     ebp, 4
608
        or      ecx, -1
609
@@:
610
        inc     ecx
611
        cmp     byte [esi+ecx], 0
612
        jnz     @b
613
        cmp     ecx, [esp+12]
614
        jb      @f
615
        mov     ecx, [esp+12]
616
@@:
617
        test    bh, 2
618
        jnz     .write_string
619
        cmp     dword [esp+8], -1
620
        jz      .write_string
621
        push    ecx
622
        sub     ecx, [esp+12]
623
        jae     .nospace
624
        mov     al, ' '
625
@@:
626
        call    con.write_char
627
        inc     dword [esp+20+64+28]
628
        inc     ecx
629
        jnz     @b
630
.nospace:
631
        pop     ecx
632
.write_string:
633
        jecxz   .string_written
634
        add     dword [esp+16+64+28], ecx
635
        push    ecx
636
@@:
637
        lodsb
638
        call    con.write_char_ex
639
        loop    @b
640
        pop     ecx
641
.string_written:
642
        pop     esi
643
        test    bh, 2
644
        jz      .spec_done
645
        cmp     dword [esp+4], -1
646
        jz      .spec_done
647
        sub     ecx, [esp+4]
648
        jae     .spec_done
649
        mov     al, ' '
650
@@:
651
        call    con.write_char
652
        inc     dword [esp+12+64+28]
653
        inc     ecx
654
        jnz     @b
655
        jmp     .spec_done
656
.done:
657
        add     esp, 64
658
        popad
659
        jmp     con.update_screen
660
.num_write_char:
661
        xchg    edi, [esp+20]
662
        call    con.write_char_ex
663
        inc     dword [esp+24+12+64+28]
664
        xchg    edi, [esp+20]
665
        inc     dword [esp+4]
666
        ret
667
 
668
con.write:
669
; esi = string, ebx = length (ebx=-1 for ASCIIZ strings)
670
        push    edi
671
        call    con.get_data_ptr
672
        test    ebx, ebx
673
        jz      .done
674
.loop:
675
        lodsb
676
        cmp     ebx, -1
677
        jnz     @f
678
        test    al, al
679
        jz      .done
680
@@:
681
        call    con.write_char_ex
682
.next:
683
        cmp     ebx, -1
684
        jz      .loop
685
        dec     ebx
686
        jnz     .loop
687
.done:
688
        pop     edi
689
        jmp     con.update_screen
690
 
691
con.get_data_ptr:
692
        mov     edi, [con.cur_y]
693
        imul    edi, [con.scr_width]
694
        add     edi, [con.cur_x]
695
        add     edi, edi
696
        add     edi, [con.data]
697
        ret
698
 
699
con.write_char_ex:
700
        test    byte [con_flags+1], 1
701
        jz      con.write_special_char
702
 
703
con.write_char:
704
        push    eax
3690 hidnplayr 705
 
706
        mov     eax, [con.cur_x]
707
        cmp     eax, [con.scr_width]
708
        jb      @f
709
        and     [con.cur_x], 0
710
        call    con.newline
711
@@:
712
        mov     eax, [esp]
836 diamond 713
        stosb
714
        mov     al, byte [con_flags]
715
        stosb
3690 hidnplayr 716
 
836 diamond 717
        mov     eax, [con.cur_x]
718
        inc     eax
719
        mov     [con.cur_x], eax
3690 hidnplayr 720
 
836 diamond 721
        pop     eax
722
        ret
723
 
724
con.write_special_char:
725
        cmp     [con_esc], 0
726
        jnz     .esc_mode
727
.normal_mode:
728
        cmp     al, 10
729
        jz      .write_lf
730
        cmp     al, 13
731
        jz      .write_cr
732
        cmp     al, 27
733
        jz      .write_esc
734
        cmp     al, 8
735
        jz      .write_bs
736
        cmp     al, 9
737
        jnz     con.write_char
738
.write_tab:
739
        mov     al, ' '
740
        call    con.write_char
741
        test    [con.cur_x], 7
742
        jnz     .write_tab
743
        ret
744
.write_cr:
745
        and     [con.cur_x], 0
746
        jmp     con.get_data_ptr
747
.write_lf:
748
        and     [con.cur_x], 0
749
        jmp     con.newline
750
.write_bs:
751
        cmp     [con.cur_x], 0
752
        jz      @f
753
        dec     [con.cur_x]
754
        dec     edi
755
        dec     edi
756
        ret
757
@@:
758
        push    eax
759
        mov     eax, [con.cur_y]
760
        dec     eax
761
        js      @f
762
        mov     [con.cur_y], eax
763
        mov     eax, [con.scr_width]
764
        dec     eax
765
        mov     [con.cur_x], eax
766
        dec     edi
767
        dec     edi
768
@@:
769
        pop     eax
770
        ret
771
.write_esc:
772
        mov     [con_esc], 1
773
        mov     [con_esc_attr_n], 1
774
        and     [con_esc_attrs], 0
775
        ret
776
.esc_mode:
777
        cmp     [con_sci], 0
778
        jnz     .esc_sci
779
        cmp     al, '['
780
        jnz     @f
781
        mov     [con_sci], 1
782
        ret
783
@@:
784
        push    eax
785
        mov     al, 27
786
        call    con.write_char
787
        pop     eax
788
        jmp     con.write_char
789
.esc_sci:
790
; this is real Esc sequence
3886 hidnplayr 791
        cmp     al, '?'         ; DEC private mode (DECSET/DECRST sequences)
792
        je      .questionmark
836 diamond 793
        cmp     al, ';'
794
        jz      .next_arg
795
        cmp     al, '0'
796
        jb      .not_digit
797
        cmp     al, '9'
798
        ja      .not_digit
799
        push    eax ecx edx
800
        sub     al, '0'
801
        movzx   eax, al
802
        mov     ecx, [con_esc_attr_n]
803
        mov     edx, [con_esc_attrs+(ecx-1)*4]
804
        lea     edx, [edx*5]
805
        lea     edx, [edx*2+eax]
806
        mov     [con_esc_attrs+(ecx-1)*4], edx
807
        pop     edx ecx eax
808
        ret
3886 hidnplayr 809
.questionmark:
810
        push    ecx
811
        mov     ecx, [con_esc_attr_n]
812
        mov     dword[con_esc_attrs+(ecx-1)*4], 0xffffffff
813
        pop     ecx
836 diamond 814
.next_arg:
815
        push    eax
816
        mov     eax, [con_esc_attr_n]
817
        inc     eax
818
        cmp     al, 4
819
        jbe     @f
820
        dec     eax
821
@@:
822
        mov     [con_esc_attr_n], eax
823
        and     [con_esc_attrs+(eax-1)*4], 0
824
        pop     eax
825
        ret
826
.not_digit:
827
        mov     [con_esc], 0
828
        mov     [con_sci], 0    ; in any case, leave Esc mode
853 diamond 829
        cmp     al, 'J'
3885 hidnplayr 830
        jz      .clear
853 diamond 831
        cmp     al, 'H'
832
        jz      .setcursor
833
        cmp     al, 'f'
834
        jz      .setcursor
836 diamond 835
        cmp     al, 'm'
836
        jz      .set_attr
853 diamond 837
        cmp     al, 'A'
838
        jz      .cursor_up
839
        cmp     al, 'B'
840
        jz      .cursor_down
841
        cmp     al, 'C'
842
        jz      .cursor_right
843
        cmp     al, 'D'
844
        jz      .cursor_left
3886 hidnplayr 845
        cmp     al, 'l'
846
        je      .dec_rst
847
        cmp     al, 'h'
848
        je      .dec_set
836 diamond 849
        ret     ; simply skip unknown sequences
3886 hidnplayr 850
 
851
.dec_rst:
852
        mov     eax, [con_esc_attrs]
853
        cmp     eax, 0xffffffff
854
        jne     .no_dec_rst
855
        mov     eax, [con_esc_attrs+4]
856
        cmp     eax, 25
857
        je      .hide_cursor
858
.no_dec_rst:
859
        ret
860
.hide_cursor:
861
        mov     [con.cursor_height], 0
862
        ret
863
 
864
.dec_set:
865
        mov     eax, [con_esc_attrs]
866
        cmp     eax, 0xffffffff
867
        jne     .no_dec_set
868
        mov     eax, [con_esc_attrs+4]
869
        cmp     eax, 25
870
        je      .show_cursor
871
.no_dec_set:
872
        ret
873
 
874
.show_cursor:
875
        mov     [con.cursor_height], (15*font_height+50)/100    ; default height
876
        ret
3885 hidnplayr 877
.clear:
878
        mov     eax, [con_esc_attrs]
879
        test    eax, eax
880
        jz      .clear_till_end_of_screen       ; [0J (or [J)
881
        dec     eax
882
        jz      .clear_till_start_of_screen     ; [1J
883
        dec     eax
884
        je      .cls                            ; [2J
885
        ret     ; unknown sequence
886
 
887
.clear_till_end_of_screen:
888
        push    edi ecx
889
        mov     ecx, [con.scr_width]
890
        imul    ecx, [con.scr_height]
891
 
892
        mov     edi, [con.cur_y]
893
        imul    edi, [con.scr_width]
894
        add     edi, [con.cur_x]
895
 
896
        sub     ecx, edi
897
        shl     edi, 1
898
        add     edi, [con.data]
899
        mov     ah, byte[con_flags]
900
        mov     al, ' '
901
        rep     stosw
902
 
903
        and     [con.cur_x], 0
904
        and     [con.cur_y], 0
905
        pop     ecx edi
906
        ret
907
 
908
.clear_till_start_of_screen:
909
        push    edi ecx
910
        mov     ecx, [con.cur_y]
911
        imul    ecx, [con.scr_width]
912
        add     ecx, [con.cur_x]
913
        mov     edi, [con.data]
914
        mov     ah, byte[con_flags]
915
        mov     al, ' '
916
        rep     stosw
917
        pop     ecx edi
918
        ret
919
 
920
.cls:   ; clear screen completely
853 diamond 921
        push    ecx
922
        and     [con.cur_x], 0
923
        and     [con.cur_y], 0
924
        mov     edi, [con.data]
925
        push    edi
926
        mov     ecx, [con.scr_width]
927
        imul    ecx, [con.scr_height]
928
        mov     ax, 0720h
929
        rep     stosw
930
        pop     edi ecx
931
.nosetcursor:
932
        ret
933
.setcursor:
934
        cmp     [con_esc_attr_n], 2
3686 hidnplayr 935
        je      @f
936
        xor     eax, eax
3688 hidnplayr 937
        mov     [con.cur_x], eax
938
        mov     [con.cur_y], eax
939
        jmp     .j_get_data
3686 hidnplayr 940
@@:
853 diamond 941
        mov     eax, [con_esc_attrs]
942
        cmp     eax, [con.scr_width]
943
        jae     @f
944
        mov     [con.cur_x], eax
945
@@:
946
        mov     eax, [con_esc_attrs+4]
947
        cmp     eax, [con.scr_height+4]
948
        jae     @f
949
        mov     [con.cur_y], eax
950
.j_get_data:
951
        jmp     con.get_data_ptr
952
.cursor_up:
953
        cmp     [con_esc_attr_n], 1
954
        jnz     .nosetcursor
955
        mov     eax, [con.cur_y]
956
        sub     eax, [con_esc_attrs]
957
        jnc     @f
958
        xor     eax, eax
959
@@:
960
        mov     [con.cur_y], eax
961
        jmp     .j_get_data
962
.cursor_down:
963
        cmp     [con_esc_attr_n], 1
964
        jnz     .nosetcursor
965
        mov     eax, [con.cur_y]
966
        add     eax, [con_esc_attrs]
967
        cmp     eax, [con.scr_height]
968
        jb      @f
969
        mov     eax, [con.scr_height]
970
        dec     eax
971
@@:
972
        mov     [con.cur_y], eax
973
        jmp     .j_get_data
974
.cursor_right:
975
        cmp     [con_esc_attr_n], 1
976
        jnz     .nosetcursor
977
        mov     eax, [con.cur_x]
978
        add     eax, [con_esc_attrs]
979
        cmp     eax, [con.scr_width]
980
        jb      @f
981
        mov     eax, [con.scr_width]
982
        dec     eax
983
@@:
984
        mov     [con.cur_x], eax
985
        jmp     .j_get_data
986
.cursor_left:
987
        cmp     [con_esc_attr_n], 1
988
        jnz     .nosetcursor
989
        mov     eax, [con.cur_x]
990
        sub     eax, [con_esc_attrs]
991
        jnc     @f
992
        xor     eax, eax
993
@@:
994
        mov     [con.cur_x], eax
995
        jmp     .j_get_data
836 diamond 996
.set_attr:
997
        push    eax ecx edx
998
        xor     ecx, ecx
999
.set_one_attr:
1000
        mov     eax, [con_esc_attrs+ecx*4]
1001
        cmp     al, 0
1002
        jz      .attr_normal
1003
        cmp     al, 1
1004
        jz      .attr_bold
1005
        cmp     al, 5
1006
        jz      .attr_bgr_bold
1007
        cmp     al, 7
1008
        jz      .attr_reversed
3690 hidnplayr 1009
 
836 diamond 1010
        xor     edx, edx
1011
        cmp     al, 30
1012
        jz      .attr_color
1013
        mov     dl, 4
1014
        cmp     al, 31
1015
        jz      .attr_color
1016
        mov     dl, 2
1017
        cmp     al, 32
1018
        jz      .attr_color
1019
        mov     dl, 6
1020
        cmp     al, 33
1021
        jz      .attr_color
1022
        mov     dl, 1
1023
        cmp     al, 34
1024
        jz      .attr_color
1025
        mov     dl, 5
1026
        cmp     al, 35
1027
        jz      .attr_color
1028
        mov     dl, 3
1029
        cmp     al, 36
1030
        jz      .attr_color
1031
        mov     dl, 7
1032
        cmp     al, 37
1033
        jz      .attr_color
3690 hidnplayr 1034
 
836 diamond 1035
        xor     edx, edx
1036
        cmp     al, 40
1037
        jz      .attr_bgr_color
1038
        mov     dl, 0x40
1039
        cmp     al, 41
1040
        jz      .attr_bgr_color
1041
        mov     dl, 0x20
1042
        cmp     al, 42
1043
        jz      .attr_bgr_color
1044
        mov     dl, 0x60
1045
        cmp     al, 43
1046
        jz      .attr_bgr_color
1047
        mov     dl, 0x10
1048
        cmp     al, 44
1049
        jz      .attr_bgr_color
1050
        mov     dl, 0x50
1051
        cmp     al, 45
1052
        jz      .attr_bgr_color
1053
        mov     dl, 0x30
1054
        cmp     al, 46
1055
        jz      .attr_bgr_color
1056
        mov     dl, 0x70
1057
        cmp     al, 47
3690 hidnplayr 1058
        jz      .attr_bgr_color
1059
 
1060
        mov     dl, 0x08
3692 hidnplayr 1061
        cmp     al, 90
3690 hidnplayr 1062
        jz      .attr_color
1063
        mov     dl, 4 + 8
3692 hidnplayr 1064
        cmp     al, 91
3690 hidnplayr 1065
        jz      .attr_color
1066
        mov     dl, 2 + 8
3692 hidnplayr 1067
        cmp     al, 92
3690 hidnplayr 1068
        jz      .attr_color
1069
        mov     dl, 6 + 8
3692 hidnplayr 1070
        cmp     al, 93
3690 hidnplayr 1071
        jz      .attr_color
1072
        mov     dl, 1 + 8
3692 hidnplayr 1073
        cmp     al, 94
3690 hidnplayr 1074
        jz      .attr_color
1075
        mov     dl, 5 + 8
3692 hidnplayr 1076
        cmp     al, 95
3690 hidnplayr 1077
        jz      .attr_color
1078
        mov     dl, 3 + 8
3692 hidnplayr 1079
        cmp     al, 96
3690 hidnplayr 1080
        jz      .attr_color
1081
        mov     dl, 7 + 8
3692 hidnplayr 1082
        cmp     al, 97
3690 hidnplayr 1083
        jz      .attr_color
1084
 
1085
        mov     dl, 0x80
1086
        cmp     al, 100
1087
        jz      .attr_bgr_color
1088
        mov     dl, 0x80 + 0x40
1089
        cmp     al, 101
1090
        jz      .attr_bgr_color
1091
        mov     dl, 0x80 + 0x20
1092
        cmp     al, 102
1093
        jz      .attr_bgr_color
1094
        mov     dl, 0x80 + 0x60
1095
        cmp     al, 103
1096
        jz      .attr_bgr_color
1097
        mov     dl, 0x80 + 0x10
1098
        cmp     al, 104
1099
        jz      .attr_bgr_color
1100
        mov     dl, 0x80 + 0x50
1101
        cmp     al, 105
1102
        jz      .attr_bgr_color
1103
        mov     dl, 0x80 + 0x30
1104
        cmp     al, 106
1105
        jz      .attr_bgr_color
1106
        mov     dl, 0x80 + 0x70
1107
        cmp     al, 107
836 diamond 1108
        jnz     .attr_continue
3690 hidnplayr 1109
 
836 diamond 1110
.attr_bgr_color:
1111
        mov     eax, [con_flags]
3690 hidnplayr 1112
        and     al, 0x0F
836 diamond 1113
        or      al, dl
1114
        mov     [con_flags], eax
1115
        jmp     .attr_continue
1116
.attr_color:
1117
        mov     eax, [con_flags]
3690 hidnplayr 1118
        and     al, 0xF0
836 diamond 1119
        or      al, dl
1120
        mov     [con_flags], eax
1121
        jmp     .attr_continue
1122
.attr_normal:
1123
        mov     byte [con_flags], 7
1124
        jmp     .attr_continue
1125
.attr_reversed:
1126
        mov     byte [con_flags], 0x70
1127
        jmp     .attr_continue
1128
.attr_bold:
1129
        or      byte [con_flags], 8
1130
        jmp     .attr_continue
1131
.attr_bgr_bold:
1132
        or      byte [con_flags], 0x80
1133
.attr_continue:
1134
        inc     ecx
1135
        cmp     ecx, [con_esc_attr_n]
1136
        jb      .set_one_attr
1137
        pop     edx ecx eax
1138
        ret
1139
 
1140
con.newline:
1141
        mov     eax, [con.cur_y]
1142
        inc     eax
1143
        mov     [con.cur_y], eax
1144
        cmp     eax, [con.scr_height]
1145
        jb      @f
1146
        call    con.scr_scroll_up
1147
@@:
1148
        call    con.get_data_ptr
1149
        ret
1150
 
1151
con.scr_scroll_up:
1152
        pushad
1153
        mov     edi, [con.data]
1154
        mov     esi, edi
1155
        add     esi, [con.scr_width]
1156
        add     esi, [con.scr_width]
1157
        dec     [con.cur_y]
1158
        mov     ecx, [con.scr_height]
1159
        dec     ecx
1160
        imul    ecx, [con.scr_width]
1161
        shr     ecx, 1
1162
        rep     movsd
1163
        adc     ecx, ecx
1164
        rep     movsw
1165
        mov     ax, 0x0720
1166
        mov     ecx, [con.scr_width]
1167
        rep     stosw
1168
        popad
1169
        ret
1170
 
1171
con.data2image:
1172
        pushad
1173
        mov     edi, [con.image]
1174
        mov     esi, [con.data]
1175
        mov     eax, [con.wnd_ypos]
1176
        mul     [con.scr_width]
1177
        add     eax, [con.wnd_xpos]
1178
        lea     esi, [esi+eax*2]
1179
        mov     ecx, [con.wnd_height]
1180
.lh:
1181
        push    ecx
1182
        mov     ecx, [con.wnd_width]
1183
.lw:
1184
        push    ecx edi
1185
        xor     eax, eax
1186
        mov     al, [esi+1]
1187
        push    eax
1188
        and     al, 0xF
1189
        mov     ebx, eax                ; цвет текста
1190
        pop     eax
1191
        shr     al, 4
1192
        mov     ebp, eax                ; цвет фона
1193
        sub     ebx, ebp
1194
        lodsb
1195
        inc     esi
1196
if font_width > 8
1197
        lea     edx, [eax+eax+font]
1198
else
1199
        lea     edx, [eax+font]
1200
end if
1201
.sh:
1202
        mov     ecx, [edx]
1203
repeat font_width
1204
        shr     ecx, 1
1205
        sbb     eax, eax
1206
        and     eax, ebx
1207
        add     eax, ebp
1208
        mov     [edi+%-1], al
1209
end repeat
1210
        mov     eax, [con.wnd_width]
1211
;        imul    eax, font_width
1212
;        add     edi, eax
1213
if font_width = 6
1214
        lea     eax, [eax*2+eax]
1215
        lea     edi, [edi+eax*2]
1216
else if font_width = 7
1217
        lea     edi, [edi+eax*8]
1218
        sub     edi, eax
1219
else if font_width = 8
1220
        lea     edi, [edi+eax*8]
1221
else if font_width = 9
1222
        lea     edi, [edi+eax*8]
1223
        add     edi, eax
1224
else if font_width = 10
1225
        lea     eax, [eax*4+eax]
1226
        lea     edi, [edi+eax*2]
1227
else
1228
Unknown font_width value!
1229
end if
1230
if font_width > 8
1231
        add     edx, 256*2
1232
        cmp     edx, font+256*2*font_height
1233
else
1234
        add     edx, 256
1235
        cmp     edx, font+256*font_height
1236
end if
1237
        jb      .sh
1238
        pop     edi ecx
1239
        add     edi, font_width
1240
        sub     ecx, 1
1241
        jnz     .lw
1242
        mov     eax, [con.wnd_width]
1243
        imul    eax, (font_height-1)*font_width
1244
        add     edi, eax
1245
        pop     ecx
1246
        mov     eax, [con.scr_width]
1247
        sub     eax, [con.wnd_width]
1248
        lea     esi, [esi+eax*2]
1249
        dec     ecx
1250
        jnz     .lh
1251
        mov     eax, [con.cur_y]
1252
        sub     eax, [con.wnd_ypos]
1253
        jb      .nocursor
1254
        cmp     eax, [con.wnd_height]
1255
        jae     .nocursor
1256
        inc     eax
1257
        mul     [con.wnd_width]
1258
        imul    eax, font_height*font_width
1259
        mov     edx, [con.cur_x]
1260
        sub     edx, [con.wnd_xpos]
1261
        jb      .nocursor
1262
        cmp     edx, [con.wnd_width]
1263
        jae     .nocursor
1264
        inc     edx
1265
        imul    edx, font_width
1266
        add     eax, edx
1267
        add     eax, [con.image]
1268
        mov     edx, [con.wnd_width]
1269
        imul    edx, font_width
1270
        neg     edx
1271
        mov     ecx, [con.cursor_height]
1272
        jecxz   .nocursor
1273
.cursor_loop:
1274
        push    ecx
1275
        mov     ecx, font_width
1276
        add     eax, edx
1277
        push    eax
1278
@@:
1279
        xor     byte [eax-1], 7
1280
        dec     eax
1281
        loop    @b
1282
        pop     eax
1283
        pop     ecx
1284
        loop    .cursor_loop
1285
.nocursor:
1286
        popad
1287
        ret
1288
 
1289
con_exit:
1290
        cmp     byte [esp+4], 0
1291
        jz      .noexit
1292
        mov     [con.thread_op], 1
1293
        call    con.wake
1294
        ret     4
1295
.noexit:
1296
        push    esi
1297
        mov     esi, [con.title]
1298
        mov     edx, con.finished_title
1299
        mov     ecx, 255
1300
        call    .strcpy
1301
        mov     esi, con.aFinished
1302
        call    .strcpy
1303
        mov     byte [edx], 0
1304
        pop     esi
1305
        and     [con.cursor_height], 0
1306
        push    con.finished_title
1307
        call    con_set_title
1308
        ret     4
1309
.strcpy:
1310
        jecxz   .ret
1311
@@:
1312
        lodsb
1313
        test    al, al
1314
        jz      .ret
1315
        mov     [edx], al
1316
        inc     edx
1317
        loop    @b
1318
.ret:
1319
        ret
1320
 
1321
con_set_title:
1322
        mov     eax, [esp+4]
1323
        mov     [con.title], eax
1324
        mov     [con.thread_op], 2
1325
        call    con.wake
1326
        ret     4
1327
 
1328
; int __stdcall con_kbhit(void);
1329
con_kbhit:
1133 diamond 1330
        test    byte [con_flags+1], 2
1331
        jnz     @f
836 diamond 1332
        mov     eax, [con.input_start]
1333
        cmp     eax, [con.input_end]
1133 diamond 1334
@@:
836 diamond 1335
        setnz   al
1336
        movzx   eax, al
1337
        ret
1338
 
1339
con.force_entered_char:
1340
        cmp     [con.entered_char], -1
1341
        jnz     .ret
1342
        mov     [con.thread_op], 4
1343
        call    con.wake
1133 diamond 1344
        test    byte [con_flags+1], 2
1345
        jnz     .ret
836 diamond 1346
; wait for response
1347
        push    ebx
1348
        push    5
1349
        pop     eax
1350
        push    2
1351
        pop     ebx
1352
@@:
1353
        int     0x40
1354
        cmp     [con.entered_char], -1
1355
        jz      @b
1356
        pop     ebx
1357
.ret:
1358
        ret
1359
 
1360
; int __stdcall con_getch(void);
1361
con_getch:
1362
        call    con.force_entered_char
1133 diamond 1363
        test    byte [con_flags+1], 2
1364
        jnz     con_getch_closed
836 diamond 1365
        movzx   eax, byte [con.entered_char]
1366
        sar     [con.entered_char], 8
1367
        mov     byte [con.entered_char+1], 0xFF
1368
        test    al, al
1369
        jz      @f
1370
        mov     byte [con.entered_char], 0xFF
1371
@@:
1372
        ret
1373
 
1133 diamond 1374
con_getch_closed:
1145 diamond 1375
        xor     eax, eax
1133 diamond 1376
        ret
1377
 
836 diamond 1378
; int __stdcall con_getch2(void);
1379
con_getch2:
1380
        call    con.force_entered_char
1133 diamond 1381
        test    byte [con_flags+1], 2
1382
        jnz     con_getch_closed
836 diamond 1383
        mov     eax, 0xFFFF
1384
        xchg    ax, [con.entered_char]
1385
        ret
1386
 
1133 diamond 1387
; char* __stdcall con_gets(char* str, int n);
836 diamond 1388
con_gets:
852 diamond 1389
        pop     eax
1390
        push    0
1391
        push    eax
1133 diamond 1392
; char* __stdcall con_gets2(con_gets2_callback callback, char* str, int n);
852 diamond 1393
con_gets2:
1133 diamond 1394
        mov     eax, [esp+8]            ; str
836 diamond 1395
        pushad
1133 diamond 1396
        mov     esi, eax                ; str
852 diamond 1397
        mov     ebx, [esp+20h+12]       ; n
836 diamond 1398
        sub     ebx, 1
1399
        jle     .ret
1400
        mov     byte [esi], 0
1401
        xor     ecx, ecx                ; длина уже введённой строки
1402
        call    con.get_data_ptr
1403
.loop:
852 diamond 1404
        call    con_getch2
836 diamond 1405
        test    al, al
1406
        jz      .extended
1407
        cmp     al, 8
1408
        jz      .backspace
1409
        cmp     al, 27
1410
        jz      .esc
1411
        cmp     al, 13
1412
        jz      .enter
852 diamond 1413
        cmp     al, 9
1414
        jz      .tab
836 diamond 1415
        inc     ecx
1416
        mov     dl, al
1417
        call    con.write_char_ex
1418
        push    [con.cur_x]
1419
        push    [con.cur_y]
1420
        push    edi
1421
        push    esi
1422
@@:
1423
        lodsb
1424
        mov     [esi-1], dl
1425
        mov     dl, al
1426
        test    al, al
1427
        jz      @f
1428
        call    con.write_char_ex
1429
        jmp     @b
1430
@@:
1431
        mov     [esi], dl
1432
        pop     esi
1433
        inc     esi
1434
        pop     edi
1435
        pop     [con.cur_y]
1436
        pop     [con.cur_x]
1437
.update_screen_and_loop:
1438
        call    con.update_screen
1439
        cmp     ecx, ebx
1440
        jb      .loop
852 diamond 1441
.ret_us:
836 diamond 1442
        mov     edx, [con.cur_x]
1443
@@:
1444
        lodsb
1445
        test    al, al
1446
        jz      @f
1447
        inc     edx
1448
        cmp     edx, [con.scr_width]
1449
        jb      @b
1450
        xor     edx, edx
1451
        call    con.newline
1452
        jmp     @b
1453
@@:
1454
        mov     [con.cur_x], edx
1455
        call    con.get_data_ptr
1456
        call    con.update_screen
1457
        jmp     .ret
1458
.esc:
1459
        mov     edx, [con.cur_x]
1460
@@:
1461
        lodsb
1462
        test    al, al
1463
        jz      @f
1464
        inc     edx
1465
        cmp     edx, [con.scr_width]
1466
        jb      @b
1467
        xor     edx, edx
1468
        call    con.newline
1469
        jmp     @b
1470
@@:
1471
        mov     [con.cur_x], edx
1472
        call    con.get_data_ptr
1473
        dec     esi
1474
        xor     ecx, ecx
1475
@@:
1476
        mov     byte [esi], 0
852 diamond 1477
        cmp     esi, [esp+20h+8]
836 diamond 1478
        jbe     .update_screen_and_loop
1479
        mov     al, 8
1480
        call    con.write_special_char
1481
        mov     al, ' '
1482
        call    con.write_char
1483
        mov     al, 8
1484
        call    con.write_special_char
1485
        dec     esi
1486
        jmp     @b
1487
.delete:
1488
        cmp     byte [esi], 0
1489
        jz      .loop
1490
        lodsb
1491
        call    con.write_char_ex
1492
.backspace:
852 diamond 1493
        cmp     esi, [esp+20h+8]
836 diamond 1494
        jbe     .loop
1495
        push    esi
1496
        mov     edx, [con.cur_x]
1497
@@:
1498
        lodsb
1499
        test    al, al
1500
        jz      @f
1501
        inc     edx
1502
        cmp     edx, [con.scr_width]
1503
        jb      @b
1504
        xor     edx, edx
1505
        call    con.newline
1506
        jmp     @b
1507
@@:
1508
        mov     [con.cur_x], edx
1509
        call    con.get_data_ptr
1510
        dec     esi
1511
        mov     al, 8
1512
        call    con.write_special_char
1513
        mov     al, ' '
1514
        call    con.write_char
1515
        mov     al, 8
1516
        call    con.write_special_char
1517
        mov     dl, 0
1518
@@:
1519
        cmp     esi, [esp]
1520
        jbe     @f
1521
        mov     al, 8
1522
        call    con.write_special_char
1523
        dec     esi
1524
        xchg    dl, [esi]
1525
        mov     al, dl
1526
        call    con.write_char
1527
        mov     al, 8
1528
        call    con.write_special_char
1529
        jmp     @b
1530
@@:
1531
        pop     esi
1532
        dec     esi
1533
        mov     [esi], dl
1534
        dec     ecx
1535
        jmp     .update_screen_and_loop
1536
.enter:
1537
        mov     edx, [con.cur_x]
1538
@@:
1539
        lodsb
1540
        test    al, al
1541
        jz      @f
1542
        inc     edx
1543
        cmp     edx, [con.scr_width]
1544
        jb      @b
1545
        xor     edx, edx
1546
        call    con.newline
1547
        jmp     @b
1548
@@:
1549
        mov     [con.cur_x], edx
1550
        call    con.get_data_ptr
1551
        mov     al, 10
1552
        mov     [esi-1], al
1553
        mov     byte [esi], 0
1554
        call    con.write_special_char
1555
        call    con.update_screen
1556
        jmp     .ret
852 diamond 1557
.tab:
1558
        mov     al, 0
1559
        mov     ah, 0xF
836 diamond 1560
.extended:
1145 diamond 1561
        test    ah, ah
1562
        jz      .closed
852 diamond 1563
        xchg    al, ah
836 diamond 1564
        cmp     al, 0x4B
1565
        jz      .left
1566
        cmp     al, 0x4D
1567
        jz      .right
1568
        cmp     al, 0x47
1569
        jz      .home
1570
        cmp     al, 0x4F
1571
        jz      .end
1572
        cmp     al, 0x53
1573
        jz      .delete
852 diamond 1574
; give control to callback function
1575
        cmp     dword [esp+20h+4], 0
1576
        jz      .loop
1577
; remember length of text before and length of text after
1578
; and advance cursor to the end of line
1579
        push    ecx
1580
        push    eax
1581
        lea     edx, [esi+1]
1582
@@:
1583
        lodsb
1584
        test    al, al
1585
        jz      @f
1586
        call    con.write_char_ex
1587
        jmp     @b
1588
@@:
1589
        sub     esi, edx
1590
        pop     eax
1591
        push    esi
1592
        dec     edx
1593
        sub     edx, [esp+28h+8]
1594
        push    edx
1595
        push    esp             ; ppos
1596
        mov     ecx, [esp+30h+4]
1597
        lea     edx, [esp+30h+12]
1598
        push    edx             ; pn
1599
        lea     edx, [esp+34h+8]
1600
        push    edx             ; pstr
1601
        push    eax             ; keycode
1602
        call    ecx
1603
        call    con.get_data_ptr
1604
        dec     eax
1605
        js      .callback_nochange
1606
        jz      .callback_del
1607
        dec     eax
1608
        jz      .callback_output
1609
; callback returned 2 - exit
1610
        add     esp, 12
1611
        jmp     .ret
1612
.callback_nochange:
1613
; callback returned 0 - string was not changed, only restore cursor position
1614
        pop     esi
1615
        pop     ecx
1616
        test    ecx, ecx
1617
        jz      .cncs
1618
@@:
1619
        mov     al, 8
1620
        call    con.write_special_char
1621
        loop    @b
1622
.cncs:
1623
        pop     ecx
1624
        add     esi, [esp+20h+8]
1625
        jmp     .callback_done
1626
.callback_del:
1627
; callback returned 1 - string was changed, delete old string and output new
1628
        mov     ecx, [esp+8]
1629
        test    ecx, ecx
1630
        jz      .cds
1631
@@:
1632
        mov     al, 8
1633
        call    con.write_special_char
1634
        mov     al, ' '
1635
        call    con.write_char_ex
1636
        mov     al, 8
1637
        call    con.write_special_char
1638
        loop    @b
1639
.cds:
1640
.callback_output:
1641
; callback returned 2 - string was changed, output new string
1642
        pop     edx
1643
        pop     esi
1644
        pop     ecx
1645
        mov     esi, [esp+20h+8]
1646
        xor     ecx, ecx
1647
@@:
1648
        lodsb
1649
        test    al, al
1650
        jz      @f
1651
        call    con.write_char_ex
1652
        inc     ecx
1653
        jmp     @b
1654
@@:
1655
        dec     esi
1656
        push    ecx
1657
        sub     ecx, edx
1658
        jz      .cos
1659
@@:
1660
        mov     al, 8
1661
        call    con.write_special_char
1662
        dec     esi
1663
        loop    @b
1664
.cos:
1665
        pop     ecx
1666
.callback_done:
1667
        call    con.update_screen
1668
        mov     ebx, [esp+20h+12]
1669
        dec     ebx
1670
        cmp     ecx, ebx
1671
        jae     .ret_us
836 diamond 1672
        jmp     .loop
1673
.left:
852 diamond 1674
        cmp     esi, [esp+20h+8]
836 diamond 1675
        jbe     .loop
1676
        dec     esi
1677
        mov     al, 8
1678
        call    con.write_special_char
1679
        jmp     .update_screen_and_loop
1680
.right:
1681
        cmp     byte [esi], 0
1682
        jz      .loop
1683
        lodsb
1684
        call    con.write_char_ex
1685
        jmp     .update_screen_and_loop
1686
.home:
852 diamond 1687
        cmp     esi, [esp+20h+8]
836 diamond 1688
        jz      .update_screen_and_loop
1689
        dec     esi
1690
        mov     al, 8
1691
        call    con.write_special_char
1692
        jmp     .home
1693
.end:
1694
        lodsb
1695
        test    al, al
1696
        jz      @f
1697
        call    con.write_char_ex
1698
        jmp     .end
1699
@@:
1700
        dec     esi
1701
        jmp     .update_screen_and_loop
1133 diamond 1702
.closed:
1703
        and     dword [esp+1Ch], 0
836 diamond 1704
.ret:
1705
        popad
852 diamond 1706
        ret     12
836 diamond 1707
 
853 diamond 1708
; void __stdcall con_cls();
1709
con_cls:
1710
        push    edi
1711
        call    con.write_special_char.cls
1712
        pop     edi
1713
        call    con.update_screen
1714
        ret
1715
 
1716
; void __stdcall con_get_cursor_pos(int* px, int* py);
1717
con_get_cursor_pos:
1718
        push    eax ecx
1719
        mov     eax, [esp+12]
1720
        mov     ecx, [con.cur_x]
1721
        mov     [eax], ecx
1722
        mov     eax, [esp+16]
1723
        mov     ecx, [con.cur_y]
1724
        mov     [eax], ecx
1725
        pop     ecx eax
1726
        ret     8
1727
 
1728
; void __stdcall con_set_cursor_pos(int px, int py);
1729
con_set_cursor_pos:
1730
        push    eax
1731
        mov     eax, [esp+8]
1732
        cmp     eax, [con.scr_width]
1733
        jae     @f
1734
        mov     [con.cur_x], eax
1735
@@:
1736
        mov     eax, [esp+12]
1737
        cmp     eax, [con.scr_height]
1738
        jae     @f
1739
        mov     [con.cur_y], eax
1740
@@:
1741
        pop     eax
1742
        call    con.update_screen
1743
        ret     8
1744
 
836 diamond 1745
con.update_screen:
1746
        push    eax
1747
        mov     eax, [con.cur_y]
1748
        sub     eax, [con.wnd_ypos]
1749
        jb      .up
1750
        cmp     eax, [con.wnd_height]
1751
        jb      .done
1752
        mov     eax, [con.cur_y]
1753
        sub     eax, [con.wnd_height]
1754
        inc     eax
1755
        jmp     .set
1756
.up:
1757
        mov     eax, [con.cur_y]
1758
.set:
1759
        mov     [con.wnd_ypos], eax
1760
.done:
1761
        pop     eax
1762
        mov     [con.thread_op], 3
1763
 
1764
con.wake:
1765
        pushad
1766
        mov     al, [con.thread_op]
1767
        cmp     al, byte [con.ipc_buf+0x10]
1768
        jz      .ret
1769
@@:
1770
        push    60
1771
        pop     eax
1772
        push    2
1773
        pop     ebx
1774
        mov     ecx, [con.console_tid]
1133 diamond 1775
        jecxz   .ret
836 diamond 1776
        mov     edx, con.thread_op
1777
        push    1
1778
        pop     esi
1779
        int     0x40
1780
        test    eax, eax
1781
        jz      @f
1782
        push    5
1783
        pop     eax
1784
        mov     bl, 1
1785
        int     0x40
1786
        jmp     @b
1787
@@:
1788
.ret:
1789
        popad
1790
        ret
1791
 
1792
; Поток окна консоли. Обрабатывает ввод и вывод.
1793
con.thread:
1794
; Поток реагирует на IPC, которое используется только для того, чтобы его можно было "разбудить"
1795
        push    40
1796
        pop     eax
1797
        push    0x67
1798
        pop     ebx
1799
        int     0x40
1800
        mov     al, 60
1801
        mov     bl, 1
1802
        mov     ecx, con.ipc_buf
1803
        push    0x11
1804
        pop     edx
1805
        int     0x40
1806
        mov     al, 66
1807
        mov     bl, 1
1808
        mov     ecx, ebx
1809
        int     0x40
1810
con.redraw:
1811
        call    con.draw_window
1812
con.msg_loop:
1813
        cmp     dword [con.bUpPressed], 0
1814
        jnz     .wait_timeout
1815
        push    10
1816
        pop     eax
1817
        jmp     @f
1818
.wait_timeout:
1819
        push    23
1820
        pop     eax
1821
        push    5
1822
        pop     ebx
1823
@@:
1824
        int     0x40
1825
        dec     eax
1826
        jz      con.redraw
1827
        dec     eax
1828
        jz      con.key
1829
        dec     eax
1830
        jz      con.button
1831
        cmp     al, 4
1832
        jz      con.ipc
1833
        jmp     con.mouse
1834
con.button:
1835
; we have only one button, close
1836
con.thread_exit:
1133 diamond 1837
        or      byte [con_flags+1], 2
1838
        and     [con.console_tid], 0
1134 diamond 1839
        and     [con.entered_char], 0
836 diamond 1840
        or      eax, -1
1841
        int     0x40
1842
con.key:
1843
        mov     al, 2
1844
        int     0x40
1845
; ah = scancode
1846
        cmp     ah, 0xE0
1847
        jnz     @f
1848
        mov     [con.bWasE0], 1
1849
        jmp     con.msg_loop
1850
@@:
1851
        shr     eax, 8
1852
        xchg    ah, [con.bWasE0]
1853
        test    al, al
1854
        jle     con.msg_loop
1855
        cmp     al, 0x1D
1856
        jz      con.msg_loop
1857
        cmp     al, 0x2A
1858
        jz      con.msg_loop
1859
        cmp     al, 0x36
1860
        jz      con.msg_loop
1861
        cmp     al, 0x38
1862
        jz      con.msg_loop
1863
        cmp     al, 0x3A
1864
        jz      con.msg_loop
1865
        cmp     al, 0x45
1866
        jz      con.msg_loop
1867
        cmp     al, 0x46
1868
        jz      con.msg_loop
1869
        mov     edx, eax
1870
        push    66
1871
        pop     eax
1872
        push    3
1873
        pop     ebx
1874
        int     0x40    ; eax = control key state
1875
        test    dh, dh
1876
        jnz     .extended
1877
        bt      [scan_has_ascii], edx
1878
        jnc     .extended
1879
        test    al, 0x30
1880
        jnz     .extended
1881
; key has ASCII code
1882
        push    eax edx
1883
        push    2
1884
        pop     ecx
1885
        test    al, 3
1886
        jnz     @f
1887
        dec     ecx
1888
@@:
1889
        push    26
1890
        pop     eax
1891
        mov     bl, 2
1892
        mov     edx, con.kbd_layout
1893
        int     0x40
1894
        pop     edx eax
1895
        mov     dh, [con.kbd_layout+edx]
1896
        test    al, 0xC
1897
        jz      @f
1898
        sub     dh, 0x60
1899
        jmp     @f
1900
.extended:
1901
        mov     dh, 0   ; no ASCII code
1902
@@:
1903
; dh contains ASCII-code; now convert scancode to extended key code
1904
        mov     ecx, con.extended_alt
1905
        test    al, 0x30
1906
        jnz     .xlat
1907
        mov     ecx, con.extended_shift
1908
        test    al, 3
1909
        jnz     .xlat
1910
        mov     ecx, con.extended_ctrl
1911
        test    al, 0xC
1912
        jnz     .xlat
1913
        xchg    dl, dh
1914
        cmp     dh, 0x57
1915
        jz      @f
1916
        cmp     dh, 0x58
1917
        jnz     .gotcode
1918
@@:
1919
        add     dh, 0x85-0x57
1920
        jmp     .gotcode
1921
.xlat:
1922
        movzx   eax, dl
1923
        mov     dl, dh
1924
        mov     dh, [eax+ecx]
1925
.gotcode:
1926
        test    dh, dh
1927
        jz      con.msg_loop
1928
        cmp     dh, 0x94
1929
        jnz     @f
1930
        mov     dl, 0
1931
@@:
1932
; dx contains full keycode
1933
        cmp     [con.bGetchRequested], 0
1934
        jz      @f
1935
        mov     [con.entered_char], dx
1936
        jmp     con.msg_loop
1937
@@:
1938
        mov     eax, [con.input_end]
1939
        mov     ecx, eax
1940
        add     eax, 2
1941
        cmp     eax, con.input_buffer_end
1942
        jnz     @f
1943
        mov     eax, con.input_buffer
1944
@@:
1945
        cmp     eax, [con.input_start]
1946
        jnz     @f
1947
; buffer overflow, make beep and continue
1948
        push    55
1949
        pop     eax
1950
        mov     ebx, eax
1951
        mov     esi, con.beep
1952
        int     0x40
1953
        jmp     con.msg_loop
1954
@@:
1955
        mov     [ecx], dx
1956
        mov     [con.input_end], eax
1957
        jmp     con.msg_loop
1958
con.ipc:
1959
        movzx   eax, byte [con.ipc_buf+0x10]
1960
        mov     byte [con.ipc_buf+4], 8
1961
        mov     byte [con.ipc_buf+0x10], 0
1962
        dec     eax
1963
        jz      con.thread_exit
1964
        dec     eax
1965
        jz      con.set_title
1966
        dec     eax
1967
        jz      con.redraw_image
1968
        dec     eax
1969
        jz      con.getch
1970
        jmp     con.msg_loop
1971
con.set_title:
1972
        push    71
1973
        pop     eax
1974
        push    1
1975
        pop     ebx
1976
        mov     ecx, [con.title]
1977
        int     0x40
1978
        jmp     con.msg_loop
1979
con.redraw_image:
1980
        call    con.data2image
1981
        call    con.draw_image
1982
        jmp     con.msg_loop
1983
con.getch:
1984
        mov     eax, [con.input_start]
1985
        cmp     eax, [con.input_end]
1986
        jz      .noinput
1987
        mov     ecx, [eax]
1988
        mov     [con.entered_char], cx
1989
        inc     eax
1990
        inc     eax
1991
        cmp     eax, con.input_buffer_end
1992
        jnz     @f
1993
        mov     eax, con.input_buffer
1994
@@:
1995
        mov     [con.input_start], eax
1996
        jmp     con.msg_loop
1997
.noinput:
1998
        mov     [con.bGetchRequested], 1
1999
        jmp     con.msg_loop
2000
con.mouse:
2001
        xor     eax, eax
2002
        xchg    eax, dword [con.bUpPressed]
2003
        mov     dword [con.bUpPressed_saved], eax
2004
        push    37
2005
        pop     eax
2006
        push    2
2007
        pop     ebx
2008
        int     0x40
2009
        test    al, 1
2010
        jnz     @f
2011
        cmp     [con.vscroll_pt], -1
2012
        jz      .redraw_if_needed
2013
        or      [con.vscroll_pt], -1
2014
.redraw_if_needed:
2015
        cmp     dword [con.bUpPressed_saved], 0
2016
        jnz     con.redraw_image
2017
        jmp     con.msg_loop
2018
@@:
2019
        mov     al, 37
2020
        dec     ebx
2021
        int     0x40
2022
        movsx   ebx, ax
2023
        sar     eax, 16
2024
        cmp     [con.vscroll_pt], -1
2025
        jnz     .vscrolling
2026
        test    ebx, ebx
2027
        js      .redraw_if_needed
2028
        sub     ax, [con.data_width]
2029
        jb      .redraw_if_needed
2030
        cmp     eax, con.vscroll_width
2031
        jae     .redraw_if_needed
2032
        cmp     ebx, con.vscroll_btn_height
2033
        jb      .up
2034
        sub     bx, [con.data_height]
2035
        jae     .redraw_if_needed
2036
        cmp     bx, -con.vscroll_btn_height
2037
        jge     .down
2038
        add     bx, [con.data_height]
2039
        sub     bx, word [con.vscrollbar_pos]
2040
        jl      .vscroll_up
2041
        cmp     bx, word [con.vscrollbar_size]
2042
        jl      .vscroll
2043
.vscroll_down:
2044
        cmp     [con.bScrollingDown_saved], 0
2045
        jz      .vscroll_down_first
2046
        cmp     [con.bScrollingDown_saved], 1
2047
        jz      .vscroll_down_wasfirst
2048
        mov     [con.bScrollingDown], 2
2049
.vscroll_down_do:
2050
        mov     eax, [con.wnd_ypos]
2051
        add     eax, [con.wnd_height]
2052
        dec     eax
2053
        mov     ebx, [con.scr_height]
2054
        sub     ebx, [con.wnd_height]
2055
        cmp     eax, ebx
2056
        jb      @f
2057
        mov     eax, ebx
2058
@@:
2059
        mov     [con.wnd_ypos], eax
2060
        jmp     con.redraw_image
2061
.vscroll_down_first:
2062
        push    26
2063
        pop     eax
2064
        push    9
2065
        pop     ebx
2066
        int     0x40
2067
        mov     [con.scroll_down_first_time], eax
2068
        mov     [con.bScrollingDown], 1
2069
        jmp     .vscroll_down_do
2070
.vscroll_down_wasfirst:
2071
        push    26
2072
        pop     eax
2073
        push    9
2074
        pop     ebx
2075
        int     0x40
2076
        sub     eax, [con.scroll_down_first_time]
2077
        cmp     eax, 25
2078
        jb      @f
2079
        mov     [con.bScrollingDown], 2
2080
        jmp     .vscroll_down_do
2081
@@:
2082
        mov     [con.bScrollingDown], 1
2083
        jmp     con.msg_loop
2084
.vscroll_up:
2085
        cmp     [con.bScrollingUp_saved], 0
2086
        jz      .vscroll_up_first
2087
        cmp     [con.bScrollingUp_saved], 1
2088
        jz      .vscroll_up_wasfirst
2089
        mov     [con.bScrollingUp], 2
2090
.vscroll_up_do:
2091
        mov     eax, [con.wnd_ypos]
2092
        inc     eax
2093
        sub     eax, [con.wnd_height]
2094
        jns     @f
2095
        xor     eax, eax
2096
@@:
2097
        mov     [con.wnd_ypos], eax
2098
        jmp     con.redraw_image
2099
.vscroll_up_first:
2100
        push    26
2101
        pop     eax
2102
        push    9
2103
        pop     ebx
2104
        int     0x40
2105
        mov     [con.scroll_up_first_time], eax
2106
        mov     [con.bScrollingUp], 1
2107
        jmp     .vscroll_up_do
2108
.vscroll_up_wasfirst:
2109
        push    26
2110
        pop     eax
2111
        push    9
2112
        pop     ebx
2113
        int     0x40
2114
        sub     eax, [con.scroll_up_first_time]
2115
        cmp     eax, 25
2116
        jb      @f
2117
        mov     [con.bScrollingUp], 2
2118
        jmp     .vscroll_up_do
2119
@@:
2120
        mov     [con.bScrollingUp], 1
2121
        jmp     con.msg_loop
2122
.up:
2123
        cmp     [con.bUpPressed_saved], 0
2124
        jz      .up_first
2125
        cmp     [con.bUpPressed_saved], 1
2126
        jz      .up_wasfirst
2127
        mov     [con.bUpPressed], 2
2128
.up_do:
2129
        mov     eax, [con.wnd_ypos]
2130
        dec     eax
2131
        js      @f
2132
        mov     [con.wnd_ypos], eax
2133
@@:
2134
        jmp     con.redraw_image
2135
.up_first:
2136
        push    26
2137
        pop     eax
2138
        push    9
2139
        pop     ebx
2140
        int     0x40
2141
        mov     [con.up_first_time], eax
2142
        mov     [con.bUpPressed], 1
2143
        jmp     .up_do
2144
.up_wasfirst:
2145
        push    26
2146
        pop     eax
2147
        push    9
2148
        pop     ebx
2149
        int     0x40
2150
        sub     eax, [con.up_first_time]
2151
        cmp     eax, 25
2152
        jb      @f
2153
        mov     [con.bUpPressed], 2
2154
        jmp     .up_do
2155
@@:
2156
        mov     [con.bUpPressed], 1
2157
        jmp     con.msg_loop
2158
.down:
2159
        cmp     [con.bDownPressed_saved], 0
2160
        jz      .down_first
2161
        cmp     [con.bDownPressed_saved], 1
2162
        jz      .down_wasfirst
2163
        mov     [con.bDownPressed], 2
2164
.down_do:
2165
        mov     eax, [con.scr_height]
2166
        sub     eax, [con.wnd_height]
2167
        jbe     con.redraw_image
2168
        cmp     [con.wnd_ypos], eax
2169
        jae     con.redraw_image
2170
        inc     [con.wnd_ypos]
2171
        jmp     con.redraw_image
2172
.down_first:
2173
        push    26
2174
        pop     eax
2175
        push    9
2176
        pop     ebx
2177
        int     0x40
2178
        mov     [con.down_first_time], eax
2179
        mov     [con.bDownPressed], 1
2180
        jmp     .down_do
2181
.down_wasfirst:
2182
        push    26
2183
        pop     eax
2184
        push    9
2185
        pop     ebx
2186
        int     0x40
2187
        sub     eax, [con.down_first_time]
2188
        cmp     eax, 25
2189
        jb      @f
2190
        mov     [con.bDownPressed], 2
2191
        jmp     .down_do
2192
@@:
2193
        mov     [con.bDownPressed], 1
2194
        jmp     con.msg_loop
2195
.vscroll:
2196
        mov     [con.vscroll_pt], ebx
2197
        call    con.draw_image
2198
        jmp     con.msg_loop
2199
.vscrolling:
2200
        sub     ebx, [con.vscroll_pt]
2201
        sub     ebx, con.vscroll_btn_height
2202
        jge     @f
2203
        xor     ebx, ebx
2204
@@:
2205
        movzx   eax, [con.data_height]
2206
        sub     eax, 2*con.vscroll_btn_height
2207
        sub     eax, [con.vscrollbar_size]
2208
        cmp     ebx, eax
2209
        jb      @f
2210
        lea     ebx, [eax-1]
2211
@@:
2212
        xchg    eax, ebx
2213
        mov     edx, [con.scr_height]
2214
        sub     edx, [con.wnd_height]
2215
        inc     edx
2216
        mul     edx
2217
        div     ebx
2218
        cmp     [con.wnd_ypos], eax
2219
        jz      con.msg_loop
2220
        mov     [con.wnd_ypos], eax
2221
        jmp     con.redraw_image
2222
 
2223
con.draw_window:
2224
        push    12
2225
        pop     eax
2226
        xor     ebx, ebx
2227
        inc     ebx
2228
        int     0x40
2229
        mov     al, 48
2230
        mov     bl, 4
2231
        int     0x40
2232
        mov     ebx, [con.def_wnd_x-2]
2233
        mov     bx, word [con.wnd_width]
2234
        imul    bx, font_width
2235
        add     bx, 5+5-1
2236
        mov     ecx, [con.def_wnd_y-2]
2237
        mov     cx, word [con.wnd_height]
2238
        imul    cx, font_height
2239
        lea     ecx, [ecx+eax+5-1]
2530 leency 2240
        mov     edx, 0x74000000
836 diamond 2241
        mov     edi, [con.title]
2242
; place for scrollbar
2243
        mov     eax, [con.wnd_height]
2244
        cmp     eax, [con.scr_height]
2245
        jae     @f
2246
        add     ebx, con.vscroll_width
2247
@@:
2248
        xor     eax, eax
2249
        int     0x40
3034 leency 2250
        ;Leency{
2251
        mov     eax,9
2252
        mov     ebx,process_info_buffer
2253
        mov     ecx,-1
2254
        int     0x40
2255
        mov     eax,[ebx+70]
2256
        mov     [window_status],eax
3686 hidnplayr 2257
                test    [window_status],100b   ; window is rolled up
3034 leency 2258
        jnz     .exit
2259
        test    [window_status],10b    ; window is minimized to panel
2260
        jnz     .exit
2261
        ;}Leency - I'm in diamond code...
836 diamond 2262
        call    con.draw_image
3034 leency 2263
 
2264
.exit:
836 diamond 2265
        push    12
2266
        pop     eax
2267
        push    2
2268
        pop     ebx
2269
        int     0x40
3686 hidnplayr 2270
 
836 diamond 2271
        ret
2272
 
2273
con.draw_image:
2274
        xor     edx, edx
2275
        mov     ecx, [con.wnd_width]
2276
        imul    ecx, font_width
2277
        mov     [con.data_width], cx
2278
        shl     ecx, 16
2279
        mov     cx, word [con.wnd_height]
2280
        imul    cx, font_height
2281
        mov     [con.data_height], cx
2282
        mov     ebx, [con.image]
2283
        push    65
2284
        pop     eax
2285
        xor     ebp, ebp
2286
        mov     edi, con.colors
2287
        push    8
2288
        pop     esi
2289
        int     0x40
2290
        mov     al, 7
2291
        mov     edx, [con.wnd_height]
2292
        cmp     edx, [con.scr_height]
2293
        jae     .skip_vscroll
2294
        push    ecx
2295
        mov     edx, ecx
2296
        xor     dx, dx
2297
        mov     ebx, con.vscroll_btn3
2298
        cmp     [con.bUpPressed], 0
2299
        jnz     @f
2300
        mov     ebx, con.vscroll_btn1
2301
@@:
2302
        mov     ecx, con.vscroll_width*65536 + con.vscroll_btn_height
2303
        int     0x40
2304
        pop     edx
2305
        sub     dx, con.vscroll_btn_height
2306
        mov     ebx, con.vscroll_btn4
2307
        cmp     [con.bDownPressed], 0
2308
        jnz     @f
2309
        mov     ebx, con.vscroll_btn2
2310
@@:
2311
        int     0x40
2312
        push    edx
2313
; Вычисляем высоту бегунка
2314
        mov     ax, dx
2315
        sub     eax, con.vscroll_btn_height
2316
        mov     ecx, eax
2317
        mul     [con.wnd_height]
2318
        div     [con.scr_height]
2319
        cmp     eax, 5
2320
        jae     @f
2321
        mov     al, 5
2322
@@:
2323
; eax = высота бегунка. Вычисляем положение бегунка
2324
        mov     [con.vscrollbar_size], eax
2325
        xchg    eax, ecx
2326
        sub     eax, ecx
2327
        mul     [con.wnd_ypos]
2328
        mov     ebx, [con.scr_height]
2329
        sub     ebx, [con.wnd_height]
2330
        div     ebx
2331
        pop     edx
2332
        push    edx
2333
; ecx = высота бегунка, eax = положение
2334
        add     eax, con.vscroll_btn_height
2335
        mov     [con.vscrollbar_pos], eax
2336
        mov     ebx, con.vscroll_bgr2
2337
        cmp     [con.bScrollingUp], 0
2338
        jnz     @f
2339
        mov     ebx, con.vscroll_bgr1
2340
@@:
2341
        mov     ecx, con.vscroll_width*65536 + con.vscroll_bgr_height
2342
        push    eax
2343
        push    7
2344
        pop     eax
2345
        mov     dx, con.vscroll_btn_height
2346
        call    .vpattern
2347
        mov     dx, word [con.vscrollbar_pos]
2348
        add     dx, word [con.vscrollbar_size]
2349
        mov     cx, con.vscroll_bgr_height
2350
        mov     ebx, con.vscroll_bgr2
2351
        cmp     [con.bScrollingDown], 0
2352
        jnz     @f
2353
        mov     ebx, con.vscroll_bgr1
2354
@@:
2355
        call    .vpattern
2356
        mov     ecx, [con.vscrollbar_pos]
2357
        mov     dx, cx
2358
        add     ecx, [con.vscrollbar_size]
2359
        sub     ecx, con.vscroll_bar_height3
2360
        push    ecx
2361
        mov     ebx, con.vscroll_bar1
2362
        mov     ecx, con.vscroll_width*65536 + con.vscroll_bar_height1
2363
        int     0x40
2364
        add     dx, cx
2365
        mov     cx, con.vscroll_bar_height2
2366
        mov     ebx, con.vscroll_bar2
2367
        call    .vpattern
2368
        mov     ebx, con.vscroll_bar3
2369
        mov     cx, con.vscroll_bar_height3
2370
        int     0x40
2371
.skip_vscroll:
2372
        ret
2373
 
2374
.vpattern:
2375
        push    edx
2376
        add     dx, cx
2377
        cmp     dx, [esp+8]
2378
        pop     edx
2379
        jbe     @f
2380
        mov     cx, [esp+4]
2381
        sub     cx, dx
2382
        jz      .ret
2383
@@:
2384
        int     0x40
2385
        add     dx, cx
2386
        cmp     dx, [esp+4]
2387
        jb      .vpattern
2388
.ret:
2389
        ret     4
2390
 
2391
align 4
2392
con.colors      dd      0x000000, 0x000080, 0x008000, 0x008080
2393
                dd      0x800000, 0x800080, 0x808000, 0xC0C0C0
2394
                dd      0x808080, 0x0000FF, 0x00FF00, 0x00FFFF
2395
                dd      0xFF0000, 0xFF00FF, 0xFFFF00, 0xFFFFFF
2396
 
2397
scan_has_ascii:
2398
        dd      11011111111111111111111111111110b
2399
        dd      00000010001111111111101111111111b
2400
        dd      00000000000000000000000000000000b
2401
        dd      0
2402
 
2403
con.extended_alt:
2404
        db      00h,01h,78h,79h,7Ah,7Bh,7Ch,7Dh,7Eh,7Fh,80h,81h,82h,83h,0Eh,0A5h
2405
        db      10h,11h,12h,13h,14h,15h,16h,17h,18h,19h,1Ah,1Bh,1Ch,00h,1Eh,1Fh
2406
        db      20h,21h,22h,23h,24h,25h,26h,27h,28h,29h,00h,2Bh,2Ch,2Dh,2Eh,2Fh
2407
        db      30h,31h,32h,33h,34h,35h,00h,37h,00h,39h,00h,68h,69h,6Ah,6Bh,6Ch
2408
        db      6Dh,6Eh,6Fh,70h,71h,00h,00h,97h,98h,99h,4Ah,9Bh,9Ch,9Dh,4Eh,9Fh
2409
        db      0A0h,0A1h,0A2h,0A3h,00h,00h,00h,8Bh,8Ch,00h,00h,00h,00h,00h,00h,00h
2410
        times 20h db 0
2411
con.extended_ctrl:
2412
        times 0Fh db %-1
2413
        db      0x94
2414
        times 2Bh db %-1
2415
        db      5Eh,5Fh,60h,61h,62h,63h,64h,65h,66h,67h,00h,00h
2416
        db      77h,8Dh,84h,8Eh,73h,8Fh,74h,90h,75h,91h,76h,92h,93h,00h,00h,00h,89h,8Ah
2417
        times 0x80-0x59 db 0
2418
con.extended_shift:
2419
        times 3Bh db %-1
2420
        db      54h,55h,56h,57h,58h,59h,5Ah,5Bh,5Ch,5Dh,00h,00h
2421
        db      47h,48h,49h,4Ah,4Bh,4Ch,4Dh,4Eh,4Fh,50h,51h,52h,53h,00h,00h,00h,87h,88h
2422
        times 0x80-0x59 db 0
2423
 
2424
; В текущей реализации значения по умолчанию таковы.
2425
; В будущем они, возможно, будут считываться как параметры из ini-файла console.ini.
2426
con.def_wnd_width   dd    80
2427
con.def_wnd_height  dd    25
2428
con.def_scr_width   dd    80
2429
con.def_scr_height  dd    300
2430
con.def_wnd_x       dd    200
2431
con.def_wnd_y       dd    50
2432
 
3034 leency 2433
 
2434
struc process_info
2435
{
2436
  cpu_usage               dd ?  ; +0
2437
  window_stack_position   dw ?  ; +4
2438
  window_stack_value      dw ?  ; +6
2439
                          dw ?  ; +8
2440
  process_name            rb 12 ; +10
2441
  memory_start            dd ?  ; +22
2442
  used_memory             dd ?  ; +26
2443
  PID                     dd ?  ; +30
2444
  box.x                   dd ?  ; +34
2445
  box.y                   dd ?  ; +38
2446
  box.width               dd ?  ; +42
2447
  box.height              dd ?  ; +46
2448
  slot_state              dw ?  ; +50
2449
                          dw ?  ; +52
2450
  client_box.x            dd ?  ; +54
2451
  client_box.y            dd ?  ; +58
2452
  client_box.width        dd ?  ; +62
2453
  client_box.height       dd ?  ; +66
2454
  wnd_state               db ?  ; +70
2455
  rb (1024-71)
2456
}
2457
process_info_buffer process_info
3686 hidnplayr 2458
window_status           rd 1
3034 leency 2459
 
836 diamond 2460
con.vscroll_pt      dd    -1
2461
 
2462
align 16
2463
EXPORTS:
2464
        dd      szStart,                START
1145 diamond 2465
        dd      szVersion,              0x00020007
836 diamond 2466
        dd      szcon_init,             con_init
2467
        dd      szcon_write_asciiz,     con_write_asciiz
3686 hidnplayr 2468
        dd      szcon_write_string,     con_write_length
836 diamond 2469
        dd      szcon_printf,           con_printf
2470
        dd      szcon_exit,             con_exit
2471
        dd      szcon_get_flags,        con_get_flags
2472
        dd      szcon_set_flags,        con_set_flags
2473
        dd      szcon_kbhit,            con_kbhit
2474
        dd      szcon_getch,            con_getch
2475
        dd      szcon_getch2,           con_getch2
2476
        dd      szcon_gets,             con_gets
852 diamond 2477
        dd      szcon_gets2,            con_gets2
836 diamond 2478
        dd      szcon_get_font_height,  con_get_font_height
2479
        dd      szcon_get_cursor_height,con_get_cursor_height
2480
        dd      szcon_set_cursor_height,con_set_cursor_height
853 diamond 2481
        dd      szcon_cls,              con_cls
2482
        dd      szcon_get_cursor_pos,   con_get_cursor_pos
2483
        dd      szcon_set_cursor_pos,   con_set_cursor_pos
836 diamond 2484
        dd      0
2485
 
2486
con_flags       dd      7
2487
con.cursor_height dd    (15*font_height+50)/100
2488
con.input_start dd      con.input_buffer
2489
con.input_end   dd      con.input_buffer
2490
 
2491
con_esc_attr_n  dd      0
2492
con_esc_attrs   dd      0,0,0,0
2493
con_esc         db      0
2494
con_sci         db      0
2495
 
2496
con.entered_char dw     -1
2497
con.bGetchRequested db  0
2498
con.bWasE0       db     0
2499
 
2500
szStart                 db 'START',0
2501
 
2502
szcon_init              db 'con_init',0
2503
szcon_write_asciiz      db 'con_write_asciiz',0
3686 hidnplayr 2504
szcon_write_string      db 'con_write_string',0
836 diamond 2505
szcon_printf            db 'con_printf',0
2506
szcon_exit              db 'con_exit',0
2507
szVersion               db 'version',0
2508
szcon_get_flags         db 'con_get_flags',0
2509
szcon_set_flags         db 'con_set_flags',0
2510
szcon_kbhit             db 'con_kbhit',0
2511
szcon_getch             db 'con_getch',0
2512
szcon_getch2            db 'con_getch2',0
2513
szcon_gets              db 'con_gets',0
852 diamond 2514
szcon_gets2             db 'con_gets2',0
836 diamond 2515
szcon_get_font_height   db 'con_get_font_height',0
2516
szcon_get_cursor_height db 'con_get_cursor_height',0
2517
szcon_set_cursor_height db 'con_set_cursor_height',0
853 diamond 2518
szcon_cls               db 'con_cls',0
2519
szcon_get_cursor_pos    db 'con_get_cursor_pos',0
2520
szcon_set_cursor_pos    db 'con_set_cursor_pos',0
836 diamond 2521
 
2522
con.thread_err      db 'Cannot create console thread!',13,10,0
2523
con.nomem_err       db 'Not enough memory!',13,10,0
2524
con.aFinished       db ' [Finished]',0
2525
con.aNull           db '(null)',0
2526
con.beep                db      0x90, 0x3C, 0x00
2527
con.ipc_buf         dd 0,8,0,0
2528
                    db 0
2529
 
2530
section '.data' data readable writable align 16
2531
 
2532
con.finished_title          rb 256
2533
 
2534
con.cur_x                   rd 1
2535
con.cur_y                   rd 1
2536
con.wnd_xpos                rd 1
2537
con.wnd_ypos                rd 1
2538
 
2539
con.wnd_width               rd 1
2540
con.wnd_height              rd 1
2541
con.scr_width               rd 1
2542
con.scr_height              rd 1
2543
con.title                   rd 1
2544
con.data                    rd 1
2545
con.image                   rd 1
2546
con.console_tid             rd 1
2547
con.data_width              rw 1
2548
con.data_height             rw 1
2549
con.vscrollbar_size         rd 1
2550
con.vscrollbar_pos          rd 1
2551
con.up_first_time           rd 1
2552
con.down_first_time         rd 1
2553
con.scroll_up_first_time    rd 1
2554
con.scroll_down_first_time  rd 1
2555
con.bUpPressed_saved        rb 1
2556
con.bDownPressed_saved      rb 1
2557
con.bScrollingUp_saved      rb 1
2558
con.bScrollingDown_saved    rb 1
2559
 
2560
con.input_buffer                rw      128
2561
con.input_buffer_end = $
2562
 
2563
con.kbd_layout          rb      128
2564
 
2565
; 1 = exit, 2 = set title, 3 = redraw, 4 = getch
2566
con.thread_op               rb 1
2567
con.bUpPressed              rb 1
2568
con.bDownPressed            rb 1
2569
con.bScrollingUp            rb 1
2570
con.bScrollingDown          rb 1
2571
 
2572
con.stack                   rb 1024
2573
con.stack_top = $