Board.KolibriOS.org

Official KolibriOS board
It is currently Sun May 26, 2019 8:32 am

All times are UTC+03:00




Post new topic  Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Tue Sep 01, 2015 11:25 pm 
Offline
Mentor

Joined: Tue Mar 11, 2014 11:37 am
Posts: 184
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:
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:
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:
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:
~/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:
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.


Attachments:
File comment: Wrapper for interfacing C and FASM
loadhttp.asm [1.68 KiB]
Downloaded 113 times
File comment: C File which will use the library
httpget.c [1.67 KiB]
Downloaded 113 times
File comment: Header to work with HTTP procedures
kolibri_http.h [2.54 KiB]
Downloaded 108 times
File comment: Header to Work with debug board
kolibri_debug.h [300 Bytes]
Downloaded 127 times

_________________
---
Check out the Netsurf Web Browser for KolibriOS.
Read the wiki and happy hacking with KolibriOS!
Top
   
PostPosted: Tue Sep 01, 2015 11:27 pm 
Offline
Mentor

Joined: Tue Mar 11, 2014 11:37 am
Posts: 184
Attached binary for the HTTP example using C and FASM libraries (http.obj) to this post.

Attachment:
kolibhttp.png
kolibhttp.png [ 379.53 KiB | Viewed 2658 times ]


Attachments:
File comment: Binary for Example in POST #1
httpbinary [63.34 KiB]
Downloaded 102 times

_________________
---
Check out the Netsurf Web Browser for KolibriOS.
Read the wiki and happy hacking with KolibriOS!
Top
   
PostPosted: Wed Sep 02, 2015 4:04 pm 
Offline
Designer
User avatar

Joined: Thu Jan 25, 2007 3:33 pm
Posts: 5047
Good job, awaiting for a new version of Netsurf :)

_________________
Через тернии к звездам


Top
   
PostPosted: Wed Sep 02, 2015 6:37 pm 
Offline
User avatar

Joined: Wed Apr 15, 2015 11:13 pm
Posts: 253
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:
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:
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:
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:
~/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:
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.

Это то что мне сейчас необходимо, хорошая идея, хорошая работа!!!


Top
   
PostPosted: Wed Sep 02, 2015 11:01 pm 
Offline
Mentor/Kernel Developer
User avatar

Joined: Fri Jun 30, 2006 9:01 am
Posts: 1247
Nice, clean approach.

_________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." Albert Einstein


Top
   
PostPosted: Tue Sep 08, 2015 10:39 pm 
Offline
Mentor

Joined: Tue Mar 11, 2014 11:37 am
Posts: 184
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.


Attachments:
File comment: Generic GUI Windows for Kolibri
gui_window.c [3.32 KiB]
Downloaded 92 times

_________________
---
Check out the Netsurf Web Browser for KolibriOS.
Read the wiki and happy hacking with KolibriOS!
Top
   
PostPosted: Sun Sep 13, 2015 12:01 am 
Offline
Mentor

Joined: Tue Mar 11, 2014 11:37 am
Posts: 184
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.
Attachment:
boardmsg1.png
boardmsg1.png [ 8.13 KiB | Viewed 2459 times ]

Attachment:
boardmsg2.png
boardmsg2.png [ 10.62 KiB | Viewed 2459 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


Attachments:
File comment: Contains all required files for libguic_kolibri
libguic_kolibri.zip [20.84 KiB]
Downloaded 95 times
File comment: boardmsg binary.
boardmsg [50.75 KiB]
Downloaded 92 times

_________________
---
Check out the Netsurf Web Browser for KolibriOS.
Read the wiki and happy hacking with KolibriOS!
Top
   
PostPosted: Tue Apr 05, 2016 3:11 pm 
Offline

Joined: Tue Apr 12, 2011 11:19 pm
Posts: 1148
Wrap in C ready for proc_lib.

_________________
я лишь учусь


Top
   
PostPosted: Wed Apr 13, 2016 12:12 pm 
Offline
Mentor

Joined: Tue Mar 11, 2014 11:37 am
Posts: 184
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!

_________________
---
Check out the Netsurf Web Browser for KolibriOS.
Read the wiki and happy hacking with KolibriOS!


Top
   
PostPosted: Wed Apr 13, 2016 12:34 pm 
Offline
Mentor
User avatar

Joined: Tue Jan 15, 2008 11:27 am
Posts: 752
I think it would be better to separate *.h and *.c files.
And code could look more clear if 'typedef' will be used.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 10 posts ] 

All times are UTC+03:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited