kolibrios/programs/network/browser/browser.asm

322 lines
9.6 KiB
NASM
Raw Normal View History

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; BROWSER for KolibriOS ;
; ;
; Compile with FASM ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; It's a work in progress but I (esevece) commit it, because if I don't continue developing it someone can continue.
; Please, keep the code clean
; The header
use32 ; Tell compiler to use 32 bit instructions
org 0x0 ; the base address of code, always 0x0
db 'MENUET01'
dd 0x01
dd START
dd I_END
dd 0x100000
dd 0x7fff0
dd 0, 0
; The code area
;include 'macros.inc'
START: ; start of execution
call draw_window ; draw the window
; After the window is drawn, it's practical to have the main loop.
; Events are distributed from here.
event_wait:
mov eax, 10 ; function 10 : wait until event
int 0x40 ; event type is returned in eax
cmp eax, 1 ; Event redraw request ?
je redraw ; Expl.: there has been activity on screen and
; parts of the applications has to be redrawn.
cmp eax, 2 ; Event key in buffer ?
je key ; Expl.: User has pressed a key while the
; app is at the top of the window stack.
cmp eax, 3 ; Event button in buffer ?
je button ; Expl.: User has pressed one of the
; applications buttons.
jmp event_wait
; The next section reads the event and processes data.
redraw: ; Redraw event handler
call draw_window ; We call the window_draw function and
jmp event_wait ; jump back to event_wait
key: ; Keypress event handler
mov eax, 2 ; The key is returned in ah. The key must be
int 0x40 ; read and cleared from the system queue.
jmp event_wait ; Just read the key, ignore it and jump to event_wait.
button: ; Buttonpress event handler
mov eax,17 ; The button number defined in window_draw
int 0x40 ; is returned to ah.
cmp ah,1 ; button id=1 ?
jne noclose
mov eax,-1 ; Function -1 : close this program
int 0x40
noclose:
cmp ah, 2 ; call fetch_thread
jne no_fetch_thread
call fetch_thread
jmp event_wait ; This is for ignored events, useful at development
no_fetch_thread:
jmp event_wait
; FETCHER
FETCH:
mov ecx, 1000
get_free_port:
inc ecx
mov eax, 53
mov ebx, 9
int 0x40
cmp eax, 0 ; is port used?
jz get_free_port
open_socket:
mov eax, 53
mov ebx, 5
mov edx, 80
mov esi, 0x0417042E ; 46.4.23.4 (kolibrios.org), only for try
mov edi, 1
int 0x40
mov [socket], eax
cmp [socket], -1 ; error?
jne status_socket
ret
status_socket:
mov eax, 53
mov ebx, 6
mov ecx, [socket]
int 0x40
cmp eax, 4 ; connected?
jne status_socket
send_request:
.concat_request:
mov edi, request
mov ebx, 0 ; request_length
mov esi, request_first_line
._first_line:
movsb
inc ebx ; request_length
cmp byte [edi], 0
jne ._first_line
mov esi, request_second_line
._second_line:
movsb
inc ebx ; request_length
cmp byte [edi], 0
jne ._second_line
mov esi, host
._second_line_host:
movsb
inc ebx ; request_length
cmp byte [edi], 0
jne ._second_line_host
mov esi, request_third_line
._third_line:
movsb
inc ebx ; request_length
cmp byte [edi], 0
jne ._third_line
mov [request_length], ebx
.write_to_socket:
mov eax, 53
mov ebx, 7
mov ecx, [socket]
mov edx, [request_length]
mov esi, request
int 0x40
cmp eax, -1 ; error?
je .write_to_socket
.poll_socket:
mov eax, 53
mov ebx, 2
mov ecx, [socket]
int 0x40
cmp eax, 0
jne .read_socket
; only for test purposes
mov eax, 63
mov ebx, 1
mov cl, 's'
int 0x40
.status_socket:
mov eax, 53
mov ebx, 6
mov ecx, [socket]
int 0x40
cmp eax, 4
jne end_of_request
.read_socket:
mov eax, 53
mov ebx, 11
mov ecx, [socket]
mov edx, [buffer]
mov esi, 4096
int 0x40
; only for test purposes
mov eax, 63
mov ebx, 1
mov cl, 'p'
int 0x40
jmp .poll_socket
end_of_request:
.close_socket:
mov eax, 53
mov ebx, 8
mov ecx, [socket]
int 0x40
; only for test purposes
mov eax, 63
mov ebx, 1
mov cl, 'b'
int 0x40
jmp terminate_thread
fetch_thread:
cmp [thread_id], 0
jne terminate_thread
mov eax, 51
mov ebx, 1
mov ecx, FETCH
mov edx, [thread_stack]
int 0x40
jmp event_wait
no_new_thread:
ret
terminate_thread:
mov eax, 18
mov ebx, 18
mov ecx, [thread_id]
int 0x40
cmp eax, 0
jne terminate_thread
mov [thread_id], 0
ret
thread_stack dd 0x80000
thread_id dd 0x0
; *********************************************
; ****** WINDOW DEFINITIONS AND DRAW ********
; *********************************************
;
; The static window parts are drawn in this function. The window canvas can
; be accessed later from any parts of this code (thread) for displaying
; processes or recorded data, for example.
;
; The static parts *must* be placed within the fn 12 , ebx = 1 and ebx = 2.
draw_window:
mov eax, 12 ; function 12: tell os about windowdraw
mov ebx, 1 ; 1, start of draw
int 0x40
mov eax, 0 ; function 0 : define and draw window
mov ebx, 100 * 65536 + 300 ; [x start] *65536 + [x size]
mov ecx, 100 * 65536 + 120 ; [y start] *65536 + [y size]
mov edx, 0x14ffffff ; color of work area RRGGBB
; 0x02000000 = window type 4 (fixed size, skinned window)
mov esi, 0x808899ff ; color of grab bar RRGGBB
; 0x80000000 = color glide
mov edi, title
int 0x40
; Fetch button
mov eax, 8
mov ebx, 25 * 65536 + 128
mov ecx, 88 * 65536 + 20
mov edx, 2
mov esi, 0x6677cc
int 0x40
mov ebx, 25 * 65536 + 35 ; draw info text with function 4
mov ecx, 0x224466
mov edx, text
mov esi, 40
mov eax, 4
.newline: ; text from the DATA AREA
int 0x40
add ebx, 10
add edx, 40
cmp byte[edx], 0
jne .newline
mov eax, 12 ; function 12:tell os about windowdraw
mov ebx, 2 ; 2, end of draw
int 0x40
ret
; *********************************************
; ************* DATA AREA *****************
; *********************************************
;
; Data can be freely mixed with code to any parts of the image.
; Only the header information is required at the beginning of the image.
text db "It look's like you have just compiled "
db "your first program for KolibriOS. "
db " "
db "Congratulations! ", 0
title db "KolibriOS Browser", 0
socket dd 0
socket_status dd 0
server_ip db 046,004,023,004
server_port db 0,0
request_first_line db 'GET / HTTP/1.1',0
request_second_line db 13,10,'Host: ',0
request_third_line db 13,10,'Connection: close',13,10,13,10,0
request db 0
request_length dd 0
host db 'kolibrios.org',0
buffer dd 0
I_END:
; The area after I_END is free for use as the application memory,
; just avoid the stack.
;
; Application memory structure, according to the used header, 1 Mb.
;
; 0x00000 - Start of compiled image
; I_END - End of compiled image
;
; + Free for use in the application
;
; 0x7ff00 - Start of stack area
; 0x7fff0 - End of stack area - defined in the header
;
; + Free for use in the application
;
; 0xFFFFF - End of freely useable memory - defined in the header
;
; All of the the areas can be modified within the application with a
; direct reference.
; For example, mov [0x80000],byte 1 moves a byte above the stack area.