Difference between revisions of "Writing applications for KolibriOS"

From KolibriOS wiki
Jump to navigation Jump to search
Line 476: Line 476:
 
    
 
    
 
<asm>
 
<asm>
 +
 
 
    
 
    
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 487: Line 488:
 
; The header
 
; The header
 
    
 
    
use32                             ; compiler to use 32 bit instructions
+
use32
 
    
 
    
              org    0x0          ; the base address of code, always 0x0
+
        org    0x0
 
    
 
    
              db    'MENUET01'   ; 8 byte id for application
+
        db    'MENUET01'
              dd    1, START, I_END, 0x100000, 0x7fff0, 0x0, 0x0
+
        dd    1, START, I_END, 0x100000, 0x7fff0, 0x0, 0x0
 +
 
 +
 
 +
        include 'macros.inc'
  
  
Line 498: Line 502:
 
START:                          ; start of execution
 
START:                          ; start of execution
 
    
 
    
    call shape_window           ; function for shaping
+
        call   shape_window   ; function for shaping
 
    
 
    
    call draw_window           ; at first, draw the window
+
        call   draw_window     ; at first, draw the window
 
    
 
    
 
still:
 
still:
 
    
 
    
    mov  eax,10                 ; wait here for event
+
        mcall  10             ; wait here for event
    mcall
+
 
 
+
        dec     eax             ; redraw request ?
     cmp  eax,1                  ; redraw request ?
+
        jz      red
    je  red
+
 
     cmp  eax,2                  ; key in buffer ?
+
        dec     eax             ; key in buffer ?
    je   key
+
        je     key
    cmp  eax,3                  ; button in buffer ?
+
 
    je   button
+
        dec      eax           ; button in buffer ?
 
+
        je     button
     jmp  still
+
 
 +
        jmp     still
 
    
 
    
 
   red:                          ; redraw
 
   red:                          ; redraw
    call draw_window
+
 
     jmp  still
+
        call   draw_window
 +
        jmp     still
 
    
 
    
 
   key:                          ; key
 
   key:                          ; key
    mov  eax,2                 ; just read it and ignore
+
 
     mcall
+
        mcall  2               ; just read it and ignore
    jmp  still
+
        jmp     still
 
    
 
    
 
   button:                      ; button
 
   button:                      ; button
    mov  eax,17                 ; get id
+
 
    mcall
+
        mcall  17             ; get id
 
    
 
    
     cmp  ah,1                   ; button id=1 ?
+
        cmp     ah,1           ; button id=1 ?
     jne  noclose
+
        jne     noclose
    mov  eax,-1                 ; close this program
+
 
    mcall
+
        mcall  -1             ; close this program
 
   noclose:
 
   noclose:
 
    
 
    
     jmp  still
+
        jmp     still
 
    
 
    
 
    
 
    
 
shape_window:
 
shape_window:
 
    
 
    
    pusha
+
        pusha
 +
 
 +
; give the shape reference area
 +
 
 +
        mcall 50, 0, shape_reference
 +
 
 +
; give the shape scale  32 x 32  ->  128 x 128
 +
; you dont have to give this, scale is 1:1 by default 
 +
; scale is set to 2^ecx
 +
 
 +
        mcall 50, 1, 2
 
    
 
    
    mov  eax,50      ; give the shape reference area
+
        popa
    mov  ebx,0
 
    mov  ecx,shape_reference
 
    mcall
 
 
    
 
    
    mov  eax,50      ; give the shape scale  32 x 32  ->  128 x 128
+
        ret
    mov  ebx,1        ; you dont have to give this, scale is 1:1 by default
 
    mov  ecx,2        ; scale is set to 2^ecx
 
    mcall
 
 
 
    popa
 
 
 
    ret
 
 
    
 
    
 
    
 
    
Line 600: Line 606:
 
draw_window:
 
draw_window:
 
    
 
    
    mov  eax,12                    ; function 12:tell os about windowdraw
+
        mcall  12, 1                  ; notice os about start of redraw
    mov  ebx,1                    ; 1, start of draw
 
    mcall
 
 
    
 
    
                                  ; DRAW WINDOW
+
                                        ; DRAW WINDOW
     mov  eax,0                     ; function 0 : define and draw window
+
        mov     eax, 0                 ; function 0 : define and draw window
     mov  ebx,100*65536             ; [x start] *65536 + [x size]
+
        mov     ebx, 100*65536         ; [x start] *65536 + [x size]
     mov  ecx,100*65536             ; [y start] *65536 + [y size]
+
        mov     ecx, 100*65536         ; [y start] *65536 + [y size]
     mov  bx,word [x_size]
+
        mov     bx , [x_size]
     mov  cx,word [y_size]
+
        mov     cx , [y_size]
     mov  edx,0x00cccc00           ; color of work area RRGGBB,8->color glide
+
        mov     edx, 0x00cccc00         ; color of work area RRGGBB,8->color glide
     mov  esi,0x00cccc00           ; color of grab bar  RRGGBB,8->color glide
+
        mov     esi, 0x00cccc00         ; color of grab bar  RRGGBB,8->color glide
     mov  edi,0x00cccc00           ; color of frames    RRGGBB
+
        mov     edi, 0x00cccc00         ; color of frames    RRGGBB
    mcall
+
        mcall
 
    
 
    
 +
                                        ; CLOSE BUTTON
 +
        mov    eax, 8                  ; function 8 : define and draw button
 +
        mov    ebx, 78*65536+12        ; [x start] *65536 + [x size]
 +
        mov    ecx, 20*65536+12        ; [y start] *65536 + [y size]
 +
        mov    edx, 1                  ; button id
 +
        mov    esi, 0x5599cc          ; button color RRGGBB
 +
        mcall
 
    
 
    
                                  ; CLOSE BUTTON
 
    mov  eax,8                    ; function 8 : define and draw button
 
    mov  ebx,78*65536+12          ; [x start] *65536 + [x size]
 
    mov  ecx,20*65536+12          ; [y start] *65536 + [y size]
 
    mov  edx,1                    ; button id
 
    mov  esi,0x5599cc              ; button color RRGGBB
 
    mcall
 
 
    
 
    
 +
        mcall  12, 2                  ; end of redraw
 
    
 
    
    mov  eax,12                    ; function 12:tell os about windowdraw
+
        ret
    mov  ebx,2                    ; 2, end of draw
 
    mcall
 
 
 
    ret
 
 
    
 
    
 
; DATA
 
; DATA
 
    
 
    
x_size  dd 127
+
x_size  dw 127
y_size  dd 127
+
y_size  dw 127
 
    
 
    
 
I_END:
 
I_END:
 
    
 
    
</asm>  
+
</asm>
 
+
 
 
 
 
== Threads ==
 
== Threads ==
 
    
 
    

Revision as of 17:18, 27 December 2009

Writing applications for KolibriOS

Structure of an application

Programming for KolibriOS is easy as you first learn the basic structure of an application. At this point I assume you have some experience in assembly language.

The KolibriOS API (Application Programming Interface) is a easy-to-learn set of functions with practically no hierarchial accesses.

The operating of an application is based on events.

The application is notified by the OS with the event type and the application acts accordingly. There are three event types an application is expected to handle by default: window redraw, keypress and buttonpress.

Flow chart and structure of an application with default events:

<asm>

 ;;;;;;;;;;;;;;;;;;;;;;;;;
 ;                       ;
 ;     HEADER DATA       ;
 ;                       ;
 ;;;;;;::;;;;;;;;;;;;;;;;;
  

START:

 call draw_window
  
 ;;;;;;;;;;;;;;;;::;;;;;;;
 ;                       ;
 ;   WAIT UNTIL EVENT    ;  <-----------------------------------------------I
 ;                       ;                                                  I
 ;;;;;::;;;;;;;;;;;;;;;;;;                                                  I
 ;;;;;;;::;;;;;;;;;;;;;;;;                                                  I
 ;                       ;     redraw   ->  call draw_window             -> I
 ;    READ EVENT TYPE    ; ->  key      ->  read keypress    -> process  -> I
 ;                       ;     button   ->  read buttonpress -> process  -> I
 ;;;;;::;;;;;;;;;;;;;;;;;;
  
  

draw_window:

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;                            ;
 ;  DRAW STATIC WINDOW PARTS  ;
 ;                            ;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
 ret
  
  

DATA AREA:

 ;;;;;;;;;;;;;;;;;;;;;;;;
 ;                      ;
 ;     STATIC DATA      ;
 ;                      ;
 ;;;;;;;;;;;;;;;;;;;;;;;;
  

</asm>


Assembly example

A heavily commented assembly language realization of the above structure.

KolibriOS system calls can be executed with the 'int 0x40' command, altough it is better to use the mcall macro. The function number should be in eax register and other registers are used if necessary. Details of all currently available system calls are at the section (1g) System functions.

<asm>

;
EXAMPLE APPLICATION ;
;
Compile with FASM ;
;
The header

use32 ; Tell compiler to use 32 bit instructions

       org     0x0             ; the base address of code, always 0x0
       db      'MENUET01'      ; 8 byte id for application
       dd      0x01            ; header version
       dd      START           ; start of execution
       dd      I_END           ; size of image
       dd      0x100000        ; Amount of memory to use
                               ; You can access memory from 0x0 to
                               ; value defined here. The relocation
                               ; of code is done with selectors
                               ; set by the OS.
       dd      0x7fff0         ; stack position in memory area
       dd      0x0             ; Parameter passing value
                               ; if set to other than zero, possible
                               ; parameters are transferred at start.
       dd      0x0             ; Path passing value, works same as parameter passing
  
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
       mcall
                                       ; event type is returned in eax
  
       cmp     eax, 1                  ; Event redraw request ?
       je      red                     ; 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.


 red:                                  ; 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
       mcall                           ; 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
       mcall                           ; is returned to ah.
  
       cmp     ah,1                    ; button id=1 ?
       jne     noclose
       mov     eax,-1                  ; Function -1 : close this program
       mcall
 noclose:
  
       jmp  event_wait             ; This is for ignored events, useful
                               ; at development
  
  
*********************************************
****** 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
       mcall
                                       ; DRAW WINDOW
       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
       mcall


       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
       mcall
       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
       mcall
       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 "Example application", 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.

</asm>

It should look like this (perhaps with other skin):
Example 1.png

KolibriOS's application structure is not specifically reserved for asm programming, the header can be produced with practically any other language. However, the overall application programming design is intended for easy 32 bit asm programming. The GUI is extremely easy to handle with especially asm language.


Using uniform system colours

While previous example concentrated on creating a basic application, in this section more attention is paid on the outlook of the window.

You can use uniform desktop colors defined by a colour setup application.

New fuction in this example is get_system_colours.

<asm>

;
UNIFORM SYSTEM COLOURS EXAMPLE ;
;
Compile with FASM ;
;
The header

use32 ; compiler to use 32 bit instructions

              org    0x0             ; the base address of code, always 0x0
  
              db     'MENUET01'      ; 8 byte id for application
              dd     1, START, I_END, 0x100000, 0x7fff0, 0, 0   
  
  
The code area


window_size_X equ 300 window_size_Y equ 150


       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
   mcall
                               ; event type is returned in eax
  
   cmp  eax,1                  ; Event redraw request ?
   je   red                    ; 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.


 red:                          ; 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
   mcall                       ; 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
   mcall                       ; is returned to ah.
  
   cmp  ah,1                   ; button id=1 ?
   jne  noclose
   mov  eax,-1                 ; Function -1 : close this program
   mcall
 noclose:
  
   jmp  event_wait             ; This is for ignored events, useful
                               ; at development
  
  

get_system_colours:

   pusha
  
   mov  eax,48                       ; fn 48 system colours
   mov  ebx,3                        ; subfn 3 : get
   mov  ecx,app_colours              ; pointer to return area
   mov  edx,10*4                     ; number of bytes to return
   mcall
  
   popa
  
   ret
  
  

app_colours: ; SYSTEM COLOURS TABLE

w_frames dd 0x0 ; - frames w_grab dd 0x0 ; - GRAB AREA w_grab_button dd 0x0 ; grab area button w_grab_button_text dd 0x0 ; grab area button text w_grab_text dd 0x0 ; grab area text w_work dd 0x0 ; - WORK AREA w_work_button dd 0x0 ; work area button w_work_button_text dd 0x0 ; work area button text w_work_text dd 0x0 ; work area text w_work_graph dd 0x0 ; work area graphics


*********************************************
****** 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
processed or recorded data, for example.
The static parts *must* be placed within the fn 12 , ebx = 1 and ebx = 2.
When using system colours, the window colours are read from the
SYSTEM COLOURS TABLE


draw_window:


   mov  eax,12                    ; function 12:tell os about windowdraw
   mov  ebx,1                     ; 1, start of draw
   mcall
  
   call get_system_colours        ; fetches system colours from os
  
                                  ; DRAW WINDOW
   mov  eax,0                     ; function 0 : define and draw window
  
   mov  ebx,100*65536+window_size_X   ; [x start] *65536 + [x size]
   mov  ecx,100*65536+window_size_Y   ; [y start] *65536 + [y size]
  
   mov  edx,[w_work]              ; color of work area 0xRRGGBB
   or   edx,0x14000000            ; 0x02000000 = window type II
                                  ; 0x03000000 = skinned window
   mov  esi,[w_grab]              ; color of grab bar 0xRRGGBB
   or   esi,0x80000000            ; 0x80000000 = colour glide
   mov  edi, title
   mcall
   mov  ebx,25*65536+35           ; draw info text with function 4
   mov  ecx,[w_work_text]
   mov  edx,text
   mov  esi,40
  newline:                        ; text from the DATA AREA
   mov  eax,4
   mcall
   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
   mcall
  
   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 'THIS PROGRAM USES UNIFORM SYSTEM COLOURS'

           db  'RETURNED TO A TABLE                     ', 0
  
  

title db 'EXAMPLE APPLICATION', 0

I_END:

</asm>

Freeform window

In this example we concentrate on shaping the window from rectangle to any form desired by the programmer. New fuction in this example is shape_window.

<a