Mission: Use KolibriOS libraries in C

Discussing libraries simplifying applications development
  • Attached binary for the HTTP example using C and FASM libraries (http.obj) to this post.
    kolibhttp.png
    kolibhttp.png (379.53 KiB)
    Viewed 13936 times
    Attachments
    httpbinary (63.34 KiB)
    Binary for Example in POST #1
    Downloaded 367 times
    ---
    Check out the Netsurf Web Browser for KolibriOS.
    Read the wiki and happy hacking with KolibriOS!
  • Good job, awaiting for a new version of Netsurf :)
    Из хаоса в космос
  • 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.
    Это то что мне сейчас необходимо, хорошая идея, хорошая работа!!!
    Технологии меняют мир, а я - меняю технологии.
  • 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
  • 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
    gui_window.c (3.32 KiB)
    Generic GUI Windows for Kolibri
    Downloaded 357 times
    ---
    Check out the Netsurf Web Browser for KolibriOS.
    Read the wiki and happy hacking with KolibriOS!
  • 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 13737 times
    boardmsg2.png
    boardmsg2.png (10.62 KiB)
    Viewed 13737 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
    Contains all required files for libguic_kolibri
    Downloaded 367 times
    boardmsg (50.75 KiB)
    boardmsg binary.
    Downloaded 358 times
    ---
    Check out the Netsurf Web Browser for KolibriOS.
    Read the wiki and happy hacking with KolibriOS!
  • Wrap in C ready for proc_lib.
    to infinity and beyond
  • 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!
  • I think it would be better to separate *.h and *.c files.
    And code could look more clear if 'typedef' will be used.
  • Who is online

    Users browsing this forum: No registered users and 3 guests