Subversion Repositories Kolibri OS

Rev

Rev 5618 | Rev 6067 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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