Page 1 of 1

Mission: Use KolibriOS libraries in C

Posted: Tue Sep 01, 2015 11:25 pm
by ashmew2
Hi,

I've recently started using the i586-kos32 cross-compiler (Thank you, Serge) to build applications in C for kolibriOS. Getting FASM libraries (especially DLLs) to work with C code has been problematic all this while and used to require some (at times, a lot) inline assembly. As there is a lot of code in C out there which can be ported to KolibriOS, it makes sense to simplify interfacing C with KolibriOS and it's libraries.

This is what I've thought of :

Step 1 : Write a wrapper in flat assembly : LoadSomelib.asm

Small piece of FASM code that calls the dll.Load procedure in dll.inc with the specified library at run time. This routine will be called from our C program to load the library at run time inside Kolibri. This wrapper contains public declarations for the procedures that we wish to make available to a C program. As I use linux, I prefer the COFF format instead of MS COFF.

Step 2 : Write a Header file in C : Somelib.h

Write a header file in C which lists all the functions along with the correct prototypes as the main SomeLib.obj that we are targetting. For example, if the SomeLib.asm file has a procedure :

Code: Select all

find_substring haystack, needle
; haystack -> ASCIIZ string in which to find substring
; needle -> substring to find in haystack
; return address in haystack where needle found or 0 on failure
This will translate to :

Code: Select all

extern char* (*find_substring)(char *haystack, char *needle) __attribute__((__stdcall__));
in the header file.

It also makes sense to write another header file if you need more functionality (such as kolibri_debug.h for working with the Debug board).

Step 3 : Write a C Program : someProg.c

Now it's time to write a C program that will use SomeLib.obj. Make sure you include the header file(s) from Step 2 in this C program with the #include directive.

Step 4 : Put it all together

Code: Select all

fasm LoadSomelib.asm LoadSomelib.obj
i586-kos32-gcc someProg.c LoadSomelib.obj -o binaryfile
Transfer binaryfile to Kolibri and watch it run :D
--

As an example, as well as to test the approach, I've written a wrapper around http.obj library (written in FASM) and calling HTTP functions from C. I've attached the files to this post as well as the final binary (to next post). I'm able to fetch the HTML page from http://kolibrios.org/en/ as a result.

Some notes on the cross compiler :

Download it from https://code.google.com/p/kolibri-pe/do ... z&can=2&q=

When compiling with i586-kos32-gcc, you might need to specify include and linker directories, for example, I invoke the cross compiler as :

Code: Select all

~/i586-kos32/kos32/bin/i586-kos32-gcc -I/home/username/kolibrios/contrib/sdk/sources/newlib/libc/include
Since I use Linux, I have created an alias KGCC to invoke this cross compiler. You might find it useful too. Add this line :

Code: Select all

alias kgcc='~/i586-kos32/kos32/bin/i586-kos32-gcc -I/home/ashish/kolibrios/contrib/sdk/sources/newlib/libc/include' 
to your ~/.bashrc file.
---

Hopefully doing this for all other libraries (such as boxlib, libio, libini and others) will bring more HLL programmers and even more programs to KolibriOS. We can also have a C style API for KolibriOS then which HLL programmers can use directly.

Re: Mission: Use KolibriOS libraries in C

Posted: Tue Sep 01, 2015 11:27 pm
by ashmew2
Attached binary for the HTTP example using C and FASM libraries (http.obj) to this post.
kolibhttp.png
kolibhttp.png (379.53 KiB)
Viewed 14020 times

Re: Mission: Use KolibriOS libraries in C

Posted: Wed Sep 02, 2015 4:04 pm
by Leency
Good job, awaiting for a new version of Netsurf :)

Re: Mission: Use KolibriOS libraries in C

Posted: Wed Sep 02, 2015 6:37 pm
by paulcodeman
ashmew2 wrote:Hi,

I've recently started using the i586-kos32 cross-compiler (Thank you, Serge) to build applications in C for kolibriOS. Getting FASM libraries (especially DLLs) to work with C code has been problematic all this while and used to require some (at times, a lot) inline assembly. As there is a lot of code in C out there which can be ported to KolibriOS, it makes sense to simplify interfacing C with KolibriOS and it's libraries.

This is what I've thought of :

Step 1 : Write a wrapper in flat assembly : LoadSomelib.asm

Small piece of FASM code that calls the dll.Load procedure in dll.inc with the specified library at run time. This routine will be called from our C program to load the library at run time inside Kolibri. This wrapper contains public declarations for the procedures that we wish to make available to a C program. As I use linux, I prefer the COFF format instead of MS COFF.

Step 2 : Write a Header file in C : Somelib.h

Write a header file in C which lists all the functions along with the correct prototypes as the main SomeLib.obj that we are targetting. For example, if the SomeLib.asm file has a procedure :

Code: Select all

find_substring haystack, needle
; haystack -> ASCIIZ string in which to find substring
; needle -> substring to find in haystack
; return address in haystack where needle found or 0 on failure
This will translate to :

Code: Select all

extern char* (*find_substring)(char *haystack, char *needle) __attribute__((__stdcall__));
in the header file.

It also makes sense to write another header file if you need more functionality (such as kolibri_debug.h for working with the Debug board).

Step 3 : Write a C Program : someProg.c

Now it's time to write a C program that will use SomeLib.obj. Make sure you include the header file(s) from Step 2 in this C program with the #include directive.

Step 4 : Put it all together

Code: Select all

fasm LoadSomelib.asm LoadSomelib.obj
i586-kos32-gcc someProg.c LoadSomelib.obj -o binaryfile
Transfer binaryfile to Kolibri and watch it run :D
--

As an example, as well as to test the approach, I've written a wrapper around http.obj library (written in FASM) and calling HTTP functions from C. I've attached the files to this post as well as the final binary (to next post). I'm able to fetch the HTML page from http://kolibrios.org/en/ as a result.

Some notes on the cross compiler :

Download it from https://code.google.com/p/kolibri-pe/do ... z&can=2&q=

When compiling with i586-kos32-gcc, you might need to specify include and linker directories, for example, I invoke the cross compiler as :

Code: Select all

~/i586-kos32/kos32/bin/i586-kos32-gcc -I/home/username/kolibrios/contrib/sdk/sources/newlib/libc/include
Since I use Linux, I have created an alias KGCC to invoke this cross compiler. You might find it useful too. Add this line :

Code: Select all

alias kgcc='~/i586-kos32/kos32/bin/i586-kos32-gcc -I/home/ashish/kolibrios/contrib/sdk/sources/newlib/libc/include' 
to your ~/.bashrc file.
---

Hopefully doing this for all other libraries (such as boxlib, libio, libini and others) will bring more HLL programmers and even more programs to KolibriOS. We can also have a C style API for KolibriOS then which HLL programmers can use directly.
Это то что мне сейчас необходимо, хорошая идея, хорошая работа!!!

Re: Mission: Use KolibriOS libraries in C

Posted: Wed Sep 02, 2015 11:01 pm
by hidnplayr
Nice, clean approach.

Re: Mission: Use KolibriOS libraries in C

Posted: Tue Sep 08, 2015 10:39 pm
by ashmew2
Generic GUI windows for KolibriOS based on system theme being used on the installation. Their design is modular, and able to contain references to all other smaller GUI elements.

Re: Mission: Use KolibriOS libraries in C

Posted: Sun Sep 13, 2015 12:01 am
by ashmew2
Presenting, LIBGUIC_KOLIBRI : GUI library for KolibriOS

HIGHLIGHTS:
* Uses wrappers written completely in FASM to use existing KolibriOS FASM code.
* Create elements with kolibri_create_new_ELEMENT() functions with extreme ease.
* Add created elements to the main window with kolibri_window_add_element()
* Only include gui.h and call kolibri_gui_init() and library does all hard work for you.
* Event Handling for redraw, mouse and key events is automated in kolibri_gui.h
* Extensible core GUI_ELEMENTS engine, so that adding new elements is easy for developers.

GUI ELEMENTS in the library right now (Working to add more):

GUI Windows: Main windows with captions and skin.
Editboxes : Textboxes for entering text.
Checkboxes: Checkboxes which support either being set or not being set.
Buttons: Buttons that can be pressed and handled in C for use with other parts of GUI.

All elements try to use the system theme and use the least number of parameters for creation.

All these elements of the GUI have their separate kolibri_guiElementName.h files which contains their structures and functions we can use to interact with the elements.

Using the library in C, I have also written an example program BOARDMSG.

About BOARDMSG:
Program accepts a message and prints it to debug board on button press.
Optionally, a checkbox can be set which appends "BOARDMSG:" to the message printed on debug board.
boardmsg1.png
boardmsg1.png (8.13 KiB)
Viewed 13821 times
boardmsg2.png
boardmsg2.png (10.62 KiB)
Viewed 13821 times
boardmsg Binary attached to post.

Attaching the libguic_kolibri.zip file which contains all required .h files.
It also contains loadboxlib.asm and .obj files for linking C and assembly together.
Also contains boardmsg.c for reference on how to use the library.
Check out kolibri_gui.h.
Also, dont forget to check the README! It contains important information for new developers.

All ideas/suggestions welcome. Use this library and let me know if you encounter any bugs or want new features!

Regards,
ashmew2

Re: Mission: Use KolibriOS libraries in C

Posted: Tue Apr 05, 2016 3:11 pm
by punk_joker
Wrap in C ready for proc_lib.

Re: Mission: Use KolibriOS libraries in C

Posted: Wed Apr 13, 2016 12:12 pm
by ashmew2
Sources for C Layer now available at : http://websvn.kolibrios.org/listing.php ... 7ce1b565cb

: Inside contrib/C_Layer in SVN Sources!

Contribute to this growing project!

Re: Mission: Use KolibriOS libraries in C

Posted: Wed Apr 13, 2016 12:34 pm
by Albom
I think it would be better to separate *.h and *.c files.
And code could look more clear if 'typedef' will be used.