Subversion Repositories Kolibri OS

Rev

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