Vala and Genie programming

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#466 Post by technosaurus »

apart from the 2-3 examples in the devx - there are none as far as I know - maybe something from another distro... did you have to make any patches to compile without glade? I don't recall seeing a --disable-glade or --without-glade option.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#467 Post by disciple »

I was lazy, so I hand edited the makefile.
I deleted everything to do with glade, except the line that said something along the lines of HAS_LIBGLADE="yes", which I changed to "no"... I don't know whether I needed to do both those things :)

One of the comments in the changelog implies that it will still build if you don't have Glade installed, so I imagine it would be fairly easy to add a --disable-glade.
Do you know a good gtkdialog program? Please post a link here

Classic Puppy quotes

ROOT FOREVER
GTK2 FOREVER

ljfr
Posts: 176
Joined: Thu 23 Apr 2009, 08:35

New version

#468 Post by ljfr »

Hi,
A new version released last week:
March 31, 2010: Vala 0.8.0 released
see:http://live.gnome.org/Vala
regards,

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#469 Post by technosaurus »

pasang-emas board game was recently rewritten in vala
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#470 Post by technosaurus »

TrayIcon template - hacked from Nic's Status Icon Test (still has the same references)

Code: Select all

[indent=4]
 
/* Build with valac --pkg gtk+-2.0 yourfile.gs */
 
uses
    Gtk
 
init
    Gtk.init (ref args)
    var test = new StatusIconTest ()
    test.show_all ()
    Gtk.main ()
 
class StatusIconTest : Widget
 
    trayicon : StatusIcon
    menu : Menu
 
    init
        /* Create tray icon */
        trayicon = new StatusIcon.from_stock(STOCK_HOME)
        trayicon.set_tooltip_text ("Hello, i'm a tooltip !")
        trayicon.set_visible(true)
        trayicon.activate += icon_clicked
        create_menu()
 
        /* Create menu */
    def private create_menu ()
        menu = new Menu()
        var menuItem = new ImageMenuItem.from_stock(STOCK_ABOUT, null)
        menuItem.activate += about_clicked
        menu.append(menuItem)
        var menuItem2 = new ImageMenuItem.from_stock(STOCK_QUIT, null)
        menuItem2.activate += exit_app
        menu.append(menuItem2)
        menu.show_all()
        trayicon.popup_menu += def (button, time)
            show_popup(button, time)
 
        /* Show popup menu */
    def private show_popup (button : uint, time : uint)
        menu.popup(null, null, null, button, time)
 
    def private icon_clicked ()
        print("I know you have clicked on the icon !!")
 
def private about_clicked ()
        var about = new AboutDialog ()
        about.set_version("1.0")
        about.set_program_name("Simple StatusIcon Test")
        about.set_comments("This is a Simple StatusIcon Test")
        about.set_copyright("Nicolas alias nikobordx")
        about.run()
        about.hide()
 
    def private exit_app ()
        Gtk.main_quit();
Compile with:
valac -C --pkg gtk+-2.0 genietray.gs
gcc -Os `pkg-config --cflags --libs gtk+-2.0` -o genietray genietray.c -s
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

Multi-call binaries in Vala

#471 Post by technosaurus »

Multi-call binaries in Vala (like in busybox)

Here is a very basic example using the symlink method:
(just make a symlink called hello and another called goodbye)

Code: Select all

class Demo.HelloWorld : GLib.Object
{
	public static int main(string[] args)
	{
		if ("hello" in args[0]){stdout.printf("Hello World!\n");}
		if ("goodbye" in args[0]){stdout.printf("Good-Bye World!\n");}
		return 0;
    }
}
Notes:
to get the alternate behavior such as in busybox <command> use args[1] instead ... for a larger number of commands a "case" would be better than a bunch of "if"s

The number of libraries available to vala is growing - with something like this it becomes possible to create an entire desktop in a single binary, thus saving significant code size.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#472 Post by technosaurus »

Vala-0.9.1 was released today - it breaks compatibility with most of the plugin examples I have seen including Valide, so I un-patched that part.

http://git.gnome.org/browse/vala/commit ... 4c1e5cf826

I built Valide-0.7.0 against that and it works great so far - unfortunately I had to also add gdl and upgrade gtk>2.18, glib>2.22, and gtksourceview(I just went ahead and compiled the latest... still no genie support though) ... gtk and glib upgrades are available from the ibiblio repo

Image
Attachments
gdl_DEV-2.30.0-i486.pet
(9.03 KiB) Downloaded 798 times
gdl-2.30.0-i486.pet
(64.76 KiB) Downloaded 797 times
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
growler
Posts: 209
Joined: Mon 24 Mar 2008, 04:42
Location: Kapiti - New Zealand

Genie and Sqlite

#473 Post by growler »

Having a go with my first forays into Genie programming.... appreciated all the examples in this thread.

I am trying to understand how to use the sqlite bindings and as an exercise I tried to convert an example in Vala to get the code below:

Code: Select all

[indent=4]
uses Sqlite
 
class SqliteSample : Object
 
    def callback (n_columns: int, values : array of string, column_names : array of string ) : int
        for i:int = 0 to n_columns
            print ("%s = %s\n", column_names[i], values[i]);

        print ("\n");
        return 0;

    def main (args : array of string): int
        var db = new Database 
        rc :int
        if args.length != 2
            print ("Usage: %s DATABASE SQL-STATEMENT\n", args[0])
            return 1

        if !FileUtils.test (args[1], FileTest.IS_REGULAR)
            print ("Database %s does not exist or is directory\n", args[1])
            return 1
		
        rc = Database.open (args[1], out db)
		
        if rc != Sqlite.OK
            print ("Can't open database: %d, %s\n", rc, db.errmsg ())
            return 1
 
        rc = db.exec (args[2], callback, null)
        if rc != Sqlite.OK
            print ("SQL error: %d, %s\n", rc, db.errmsg ())
            return 1
	
        return 0

init
    var SQLLiteTest = new SqliteSample
    params: array of string = {"/root/my-applications/PointOfSale.sqlite", "SELECT * FROM currencies"}
    SQLLiteTest.main(params)

I get:

Code: Select all

# valac --pkg sqlite3 sqlite.gs
sqlite.gs:39.5-39.10: error: syntax error, expected identifier
    params: array of string = {"/root/my-applications/PointOfSale.sqlite", "SELECT * FROM currencies"}
    ^^^^^^
Compilation failed: 1 error(s), 0 warning(s)

Appreciate any pointers.
Last edited by growler on Sat 28 Aug 2010, 08:25, edited 3 times in total.

User avatar
growler
Posts: 209
Joined: Mon 24 Mar 2008, 04:42
Location: Kapiti - New Zealand

XML-RPC with libSOUP

#474 Post by growler »

I am trying to get data from another application (webERP) over XML-RPC and started to play with libsoup - again there is a vala example which I have tried to hack into Genie but coming unstuck.

I can get the following to compile:

Code: Select all

indent=4]
/* Build with "valac --thread --pkg libsoup-2.4 yourfile.gs */
uses 
    Soup
	
init 
    var session = new Soup.SessionAsync ()
    ItemCode:string = "DVD-TOPGUN"
    UserName:string = "admin"
    Password:string = "weberp"
    
    var message = Soup.xmlrpc_request_new ("http://localhost/webERP/api/api_xml-rpc.php", "weberp.xmlrpc_GetStockBalance", typeof(string),ItemCode, typeof(string), UserName, typeof(string), Password)
    
    session.send_message(message)
    
    ResponseData:string = message.response_body.flatten().data
    
    ErrorNumber:int = typeof(int)
    try    
        xmlrpc_parse_method_response (ResponseData, -1, ErrorNumber)
        print("Stock of %s = %s",ItemCode, ResponseData)
        print("Error number %d", ErrorNumber)
    except
        print("Could not get webERP API location stock over XML-RPC")

    /*Is this how to create an associative array

    struct LocQty
          quantity : double
          loccode : string

    var StockQuantities = new array of LocQty
    ErrorMessage:string
    xmlrpc_extract_method_response(ResponseData, -1, ErrorMessage, typeof(array of dict of string, double), StockQuantities)
	*/
However, I can't get the last bit to xmlrpc_extract_method_response call to work - I think this is supposed to parse the response XML to nice variables. I think the 3rd parameter is supposed to be the data type of the return variables - which is an array of structs - the program as compiled returns:

Code: Select all

# ./SoupXMLRPC 
Stock of DVD-TOPGUN = <?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><array>
<data>
<value><int>0</int></value>
<value><array>
<data>
<value><struct>
<member><name>quantity</name>
<value><string>-1</string></value>
</member>
<member><name>loccode</name>
<value><string>MEL</string></value>
</member>
</struct></value>
<value><struct>
<member><name>quantity</name>
<value><string>0</string></value>
</member>
<member><name>loccode</name>
<value><string>TOR</string></value>
</member>
</struct></value>
</data>
</array></value>
</data>
</array></value>
</param>
</params>
</methodResponse>
Error number 24

So it works with a good XML message returned.

Do I then have to parse the XML using xml as per Caleb's jwmconf example or does that method xmlrpc_extract_method_response do the trick....

This is very steep learning for me, I am in WAY over my head here!

Hope someone has done this before and can point me in the right direction

nikobordx
Posts: 84
Joined: Sat 23 May 2009, 09:08
Location: Bordeaux, France

Genie and Sqlite

#475 Post by nikobordx »

Hi growler,

Can you try this:

Code: Select all

[indent=4]

uses
    Sqlite
 
class SqliteSample : Object

    db : Sqlite.Database
 
    def callback (n_columns: int, values : array of string, column_names : array of string ) : int
        for i:int = 0 to n_columns
            print ("%s = %s\n", column_names[i], values[i])

        print ("\n");
        return 0;

    def begin (args : array of string) : int
        rc : int
        if args.length != 2
            print ("Usage: %s DATABASE SQL-STATEMENT\n", args[0])
            return 1

        if !FileUtils.test (args[0], FileTest.IS_REGULAR)
            print ("Database %s does not exist or is directory\n", args[0])
            return 1
      
        rc = Database.open (args[0], out db)
      
        if rc != Sqlite.OK
            print ("Can't open database: %d, %s\n", rc, db.errmsg ())
            return 1
 
        rc = db.exec (args[1], callback, null)
        if rc != Sqlite.OK
            print ("SQL error: %d, %s\n", rc, db.errmsg ())
            return 1
   
        return 0

init
    var SQLLiteTest = new SqliteSample
    test : array of string = {"/root/my-applications/PointOfSale.sqlite", "SELECT * FROM currencies"}
    SQLLiteTest.begin(test) 
Do not use "def main" in fonction, and "params" for arrays of string.

Nicolas.

User avatar
growler
Posts: 209
Joined: Mon 24 Mar 2008, 04:42
Location: Kapiti - New Zealand

Genie and Sqlite

#476 Post by growler »

Not sure what the first parameter was meant to be I made the parameters array to have a blank element since the DB file was the second element and the SQL the third. et voila ...

Code: Select all

test : array of string = {"", "/root/my-applications/PointOfSale.sqlite", "SELECT * FROM currencies"} 
Merci beaucoup Nicolas!!

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#477 Post by Mobeus »

With Vala I can't find a means to explicitly close a file opened with FileStream.open. Is there not a wrapper for fclose()? How is it done?

tia
Mobeus

nikobordx
Posts: 84
Joined: Sat 23 May 2009, 09:08
Location: Bordeaux, France

#478 Post by nikobordx »

Hi everyone,

growler:
Why not using

Code: Select all

test : array of string = {"/root/my-applications/PointOfSale.sqlite", "SELECT * FROM currencies"}
and change number of your "args" ?

Mobeus:
You don't need to specify to close the file because it is automatically closed!
In GLib's vapi file you can see on the class "FileStream":

Code: Select all

[CCode (cname = "FILE", free_function = "fclose", cheader_filename = "stdio.h")]
See you,
Nicolas.

User avatar
growler
Posts: 209
Joined: Mon 24 Mar 2008, 04:42
Location: Kapiti - New Zealand

#479 Post by growler »

Yes your solution is better.

Have you ever used libsoup - I can't get to grips with the

xmlrpc_extract_method_response method. I find the documentation very cryptic and spartan!!

nikobordx
Posts: 84
Joined: Sat 23 May 2009, 09:08
Location: Bordeaux, France

#480 Post by nikobordx »

I made only 2 small samples with libsoup:
http://code.valaide.org/content/genie-l ... ple-sample
http://code.valaide.org/content/genie-l ... ple-server

I can help you if you post your code (if your code is not top secret ! :D )

Nicolas.

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#481 Post by Mobeus »

Hi Nicolas
You don't need to specify to close the file because it is automatically closed!
Oh that's quite awkward. That makes Vala the only language I have ever used that decides when the file can be closed instead of the programmer. :(

Consider:
Open a file for writing
Write a bash script to it.
Flush the file
chmod it
try to execute the new script from within your vala program
Error: File is busy!

I can code around this by putting the file writing in a sub-routine, the file is closed when the routine finishes and goes out of scope.

FileStream.close(fp) would be so much better. :?

Mobeus

nikobordx
Posts: 84
Joined: Sat 23 May 2009, 09:08
Location: Bordeaux, France

#482 Post by nikobordx »

Try this:

Code: Select all

[indent=4]

// Build with valac --pkg gio-2.0 yourfile.gs
 
init
    create_new_file()

// Use this function when file is not exist
def static create_new_file ()
    var file = File.new_for_path("test.sh")
    var thefile_stream = file.create (FileCreateFlags.NONE, null)
    var data_stream = new DataOutputStream (thefile_stream)
    data_stream.put_string ("echo \"hello\"", null) // Write something in file
    FileUtils.chmod ("test.txt", 0755) // Chmod the file to executable
    Process.spawn_command_line_async("sh test.sh") // Exec the file

// Use this function when file already exist
def static use_file ()
    var file = File.new_for_path("test.sh")
    var renamed = file.set_display_name ("samplefile.bak", null)
    var thefile_stream = file.create (FileCreateFlags.NONE, null)
    var data_stream = new DataOutputStream (thefile_stream)
    data_stream.put_string ("echo \"hello\"", null) // Write something in file
    renamed.delete(null)
    FileUtils.chmod ("test.txt", 0755) // Chmod the file to executable
    Process.spawn_command_line_async("sh test.sh") // Exec the file
Nicolas.

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#483 Post by Mobeus »

Hi Nicolas,

I appreciate your help and examples. I never would have thought to even try something like that. This is an example of what I used that “Failed to execute child process “/tmp/test.sh

User avatar
growler
Posts: 209
Joined: Mon 24 Mar 2008, 04:42
Location: Kapiti - New Zealand

XML-RPC

#484 Post by growler »

I think I took your example as the base - it looks like you converted from Evan Nemmerson's vala example. I posted the code I have been working with - slightly modified version of your code in this thread above. I did post it almost at the same time as the sqlite code I was playing with that you already figured out for me!

I was hoping that the xmlrpc_extract_method_response of the libsoup library might be the method to call to parse the xml returned from the response into an array of structs but I am at a complete loss as to how to call this. What is GValue? and how do we get at the **error parameter presumably an out array - seems to be an int and a string - perhaps a dict or a struct. There is something funny going on with the number of parameters as the /usr/share/vala/valide/libsoup.val or something like that file shows I think 5 parameters for xmlrpc_parse_method_response from memory (which is dangerously bad) and if I give any more than 4 I get compile errors. What does that method do anyway?

Many thanks again for your help Nicolas

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#485 Post by 01micko »

Hi guys and gals

I know next to nought about genie programming but using Nicolas' template for tray icons I came up with something useful.. mainly coded in bash :lol: :roll: , depending on 2 tray icons compiled in genie.

It's a little daemon to monitor RAM usage and it actually works! It gives warnings when RAM usage is high and critical and offers to start pprocess (if installed) or top.

I just tried it out on my XO-1 and it worked a charm, with mavrothal's xopup based on lupu-510. Xo's only have 256M of RAM and I have no swap file. Once Puppy is loaded there is about 40M free, then you load Chromium and there is less than 5M left.. enter my program.

I called it freeramdaemon and it has a simple config gui too.

There is a menu entry under 'system'

Maybe the technosaur might want to expand on this..

Sources are included and I commented here and there.

Enjoy
Cheers

Edit1: just removed some useless code so now it's 0.1.1 (not changed in script tho :wink: )

Edit2: updated to 0.2. It seemed more logical to give warnings when the actual amount of RAM becomes critical, not the percentage. :roll: . I found this out on my fast box when I was getting the warning when I still had 200M of free RAM! :lol: Now the 'warning' kicks in at 32M and the critical warning kicks in at 8M, giving the user enough chance to do something about the situation without losing their work. :wink:
Attachments
freeramdaemon-0.2.pet
latest, commented old stuff
(8.64 KiB) Downloaded 1213 times
critcal.png
(11.95 KiB) Downloaded 2307 times
warning.png
(11.25 KiB) Downloaded 2282 times
freeramdaemon-0.1.1.pet
leaving older version up
(8.39 KiB) Downloaded 1080 times
Last edited by 01micko on Tue 07 Sep 2010, 06:39, edited 2 times in total.
Puppy Linux Blog - contact me for access

Post Reply