Board.KolibriOS.org

Официальный форум KolibriOS
Текущее время: Чт май 25, 2017 9:16 pm

Часовой пояс: UTC+03:00




Начать новую тему  Ответить на тему  [ 10 сообщений ] 
Автор Сообщение
 Заголовок сообщения: Mission: Use KolibriOS libraries in C
СообщениеДобавлено: Вт сен 01, 2015 11:25 pm 
Не в сети
Mentor

Зарегистрирован: Вт мар 11, 2014 11:37 am
Сообщения: 157
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 :
Код:
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 :
Код:
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

Код:
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 :
Код:
~/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 :
Код:
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.


Вложения:
Комментарий к файлу: Wrapper for interfacing C and FASM
loadhttp.asm [1.68 КБ]
48 скачиваний
Комментарий к файлу: C File which will use the library
httpget.c [1.67 КБ]
40 скачиваний
Комментарий к файлу: Header to work with HTTP procedures
kolibri_http.h [2.54 КБ]
35 скачиваний
Комментарий к файлу: Header to Work with debug board
kolibri_debug.h [300 байт]
47 скачиваний

_________________
Download web browser for Kolibri : viewtopic.php?f=40&t=2315&p=68785#p68742

Make KolibriOS Better by contributing as a developer or designer or writing Documentation!
You can start at the wiki : wiki.kolibrios.org
Вернуться к началу
 Заголовок сообщения: Re: Mission: Use KolibriOS libraries in C
СообщениеДобавлено: Вт сен 01, 2015 11:27 pm 
Не в сети
Mentor

Зарегистрирован: Вт мар 11, 2014 11:37 am
Сообщения: 157
Attached binary for the HTTP example using C and FASM libraries (http.obj) to this post.

Вложение:
kolibhttp.png
kolibhttp.png [ 379.53 КБ | 1370 просмотров ]


Вложения:
Комментарий к файлу: Binary for Example in POST #1
httpbinary [63.34 КБ]
31 скачивание

_________________
Download web browser for Kolibri : viewtopic.php?f=40&t=2315&p=68785#p68742

Make KolibriOS Better by contributing as a developer or designer or writing Documentation!
You can start at the wiki : wiki.kolibrios.org
Вернуться к началу
 Заголовок сообщения: Re: Mission: Use KolibriOS libraries in C
СообщениеДобавлено: Ср сен 02, 2015 4:04 pm 
Не в сети
Designer
Аватара пользователя

Зарегистрирован: Чт янв 25, 2007 3:33 pm
Сообщения: 4080
Good job, awaiting for a new version of Netsurf :)

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


Вернуться к началу
 Заголовок сообщения: Re: Mission: Use KolibriOS libraries in C
СообщениеДобавлено: Ср сен 02, 2015 6:37 pm 
Не в сети
Аватара пользователя

Зарегистрирован: Ср апр 15, 2015 11:13 pm
Сообщения: 156
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 :
Код:
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 :
Код:
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

Код:
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 :
Код:
~/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 :
Код:
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
СообщениеДобавлено: Ср сен 02, 2015 11:01 pm 
Не в сети
Mentor/Kernel Developer
Аватара пользователя

Зарегистрирован: Пт июн 30, 2006 9:01 am
Сообщения: 1204
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


Вернуться к началу
 Заголовок сообщения: Re: Mission: Use KolibriOS libraries in C
СообщениеДобавлено: Вт сен 08, 2015 10:39 pm 
Не в сети
Mentor

Зарегистрирован: Вт мар 11, 2014 11:37 am
Сообщения: 157
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.


Вложения:
Комментарий к файлу: Generic GUI Windows for Kolibri
gui_window.c [3.32 КБ]
22 скачивания

_________________
Download web browser for Kolibri : viewtopic.php?f=40&t=2315&p=68785#p68742

Make KolibriOS Better by contributing as a developer or designer or writing Documentation!
You can start at the wiki : wiki.kolibrios.org
Вернуться к началу
 Заголовок сообщения: Re: Mission: Use KolibriOS libraries in C
СообщениеДобавлено: Вс сен 13, 2015 12:01 am 
Не в сети
Mentor

Зарегистрирован: Вт мар 11, 2014 11:37 am
Сообщения: 157
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 КБ | 1171 просмотр ]

Вложение:
boardmsg2.png
boardmsg2.png [ 10.62 КБ | 1171 просмотр ]

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


Вложения:
Комментарий к файлу: Contains all required files for libguic_kolibri
libguic_kolibri.zip [20.84 КБ]
29 скачиваний
Комментарий к файлу: boardmsg binary.
boardmsg [50.75 КБ]
21 скачивание

_________________
Download web browser for Kolibri : viewtopic.php?f=40&t=2315&p=68785#p68742

Make KolibriOS Better by contributing as a developer or designer or writing Documentation!
You can start at the wiki : wiki.kolibrios.org
Вернуться к началу
 Заголовок сообщения: Re: Mission: Use KolibriOS libraries in C
СообщениеДобавлено: Вт апр 05, 2016 3:11 pm 
Не в сети

Зарегистрирован: Вт апр 12, 2011 11:19 pm
Сообщения: 1050
Wrap in C ready for proc_lib.

_________________
я лишь учусь


Вернуться к началу
 Заголовок сообщения: Re: Mission: Use KolibriOS libraries in C
СообщениеДобавлено: Ср апр 13, 2016 12:12 pm 
Не в сети
Mentor

Зарегистрирован: Вт мар 11, 2014 11:37 am
Сообщения: 157
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!

_________________
Download web browser for Kolibri : viewtopic.php?f=40&t=2315&p=68785#p68742

Make KolibriOS Better by contributing as a developer or designer or writing Documentation!
You can start at the wiki : wiki.kolibrios.org


Вернуться к началу
 Заголовок сообщения: Re: Mission: Use KolibriOS libraries in C
СообщениеДобавлено: Ср апр 13, 2016 12:34 pm 
Не в сети
Mentor
Аватара пользователя

Зарегистрирован: Вт янв 15, 2008 11:27 am
Сообщения: 750
I think it would be better to separate *.h and *.c files.
And code could look more clear if 'typedef' will be used.


Вернуться к началу
Показать сообщения за:  Поле сортировки  
Начать новую тему  Ответить на тему  [ 10 сообщений ] 

Часовой пояс: UTC+03:00


Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и 1 гость


Вы не можете начинать темы
Вы не можете отвечать на сообщения
Вы не можете редактировать свои сообщения
Вы не можете удалять свои сообщения
Вы не можете добавлять вложения

Найти:
Перейти:  
Создано на основе phpBB® Forum Software © phpBB Limited
Русская поддержка phpBB