Friday, March 21, 2014

Getting Started with GtkAda - Packing

When creating an application, you'll want to put more than one widget inside a window. Our first helloworld example only used one widget so we could simply use Add call to "pack" the widget into the window. But when you want to put more than one widget into a window, it it becomes important to control how each widget is positioned and sized. This is where packing comes in.

GtkAda comes with a large variety of layout containers whose purpose it is to control the layout of the child widgets that are added to them. You could see the Gtk+ Layout Containers for an overview. GtkAda has all the containers and normally has the form of Gtk_<Container_Name>

The following example shows how the Gtk_Grid container lets you arrange several buttons. It follows the same directory structure in the helloworld example.

src/grid.adb:
with Gtk.Main;
with Gtk.Window;      use Gtk.Window;
with Gtk.Grid;        use Gtk.Grid;
with Gtk.Button;      use Gtk.Button;
with Gtkada.Handlers; use Gtkada.Handlers;

with grid_cb; use grid_cb;

procedure Grid is
   Win    : Gtk_Window;
   Grid   : Gtk_Grid;
   Button : Gtk_Button;
begin
   --  Initialize GtkAda.
   Gtk.Main.Init;

   -- create a top level window
   Gtk_New (Win);
   Win.Set_Title ("Grid");
   -- set the border width of the window
   Win.Set_Border_Width (10);
   -- connect the "destroy" signal
   Win.On_Destroy (main_quit'Access);

   -- Here we construct the container that is going pack our buttons
   Gtk_New (Grid);

   -- Packed the container in the Window
   Win.Add (Grid);

   -- create a button with label
   Gtk_New (Button, "Button 1");
   -- connect the click signal
   Button.On_Clicked (button_clicked'Access);

   -- Place the first button in the grid cell (0, 0), and make it fill
   -- just 1 cell horizontally and vertically (ie no spanning)
   Grid.Attach (Button, 0, 0, 1, 1);

   -- create another button with label
   Gtk_New (Button, "Button 2");
   Button.On_Clicked (button_clicked'Access);

   -- Place the second button in the grid cell (1, 0), and make it fill
   -- just 1 cell horizontally and vertically (ie no spanning)
   Grid.Attach (Button, 1, 0, 1, 1);

   -- create the quit button
   Gtk_New (Button, "Quit");
   -- connect the "clicked" signal of the button to destroy function
   Widget_Callback.Object_Connect
     (Button,
      "clicked",
      Widget_Callback.To_Marshaller (button_quit'Access),
      Win);
   -- Place the Quit button in the grid cell (0, 1), and make it
   -- span 2 columns.
   Grid.Attach (Button, 0, 1, 2, 1);

   -- Now that we are done packing our widgets, we show them all
   -- in one go, by calling Win.Show_All.
   -- This call recursively calls Show on all widgets
   -- that are contained in the window, directly or indirectly.
   Win.Show_All;

   -- All GTK applications must have a Gtk.Main.Main. Control ends here
   -- and waits for an event to occur (like a key press or a mouse event),
   -- until Gtk.Main.Main_Quit is called.
   Gtk.Main.Main;
end Grid;
src/grid_cb.ads:
with Gtk.Widget;  use Gtk.Widget;
with Gtk.Button;  use Gtk.Button;
with Glib.Object;

with Gdk.Event;

package grid_cb is
   function main_del
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return  Boolean;
   procedure main_quit (Self : access Gtk_Widget_Record'Class);
   procedure button_clicked (Self : access Gtk_Button_Record'Class);
   procedure button_quit (Self : access Gtk_Widget_Record'Class);
end grid_cb;
src/grid_cb.adb:
with Ada.Text_IO; use Ada.Text_IO;

with Gtk.Main;

package body grid_cb is
   -- If you return false in the "delete_event" signal handler,
   -- GTK will emit the "destroy" signal. Returning true means
   -- you don't want the window to be destroyed.
   --
   -- This is useful for popping up 'are you sure you want to quit?'
   -- type dialogs.
   function main_del
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return  Boolean
   is
   begin
      Put_Line ("Delete event encounter.");
      return True;
   end main_del;

   procedure main_quit (Self : access Gtk_Widget_Record'Class) is
   begin
      Gtk.Main.Main_Quit;
   end main_quit;

   procedure button_clicked (Self : access Gtk_Button_Record'Class) is
   begin
      Put_Line ("Hello clicked");
   end button_clicked;

   procedure button_quit (Self : access Gtk_Widget_Record'Class) is
   begin
      Put_Line ("buttion_quit is called");
      Destroy (Self);
   end button_quit;

end grid_cb;
grid.gpr
with "gtkada";

project Grid is

   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Main use ("grid.adb");

   --  Enable Ada 2005.
   package Compiler is
      for Default_Switches ("ada") use ("-gnat05");
   end Compiler;

end Grid
To compile the program:
gprbuild -P grid

You may notice the callback functions are exactly the same as helloworld, you could reuse it by just rename the packages or simple using the same hello_cb package and in grid.adb with and use it with hello_cb; use hello_cb;, then you don't need to change anything on the call backs.

Thursday, March 20, 2014

Getting Started with GtkAda

There is not much resources on how to program with GtkAda. GtkAda User’s Guide provides some information but not enough to start from scratch. I tried to follow the Getting Started with GTK+ but writing it with GtkAda.

I will assume you have gnat gpl 2013 and GtkAda installed. I use the svn version of GtkAda (3.8.3) in stead of the GtkAda 2013 GPL 3.4.2.

Basics

We'll start with the simplest program possible. This program will create an empty window.
The source files are under the following directory structure:
example-top-dir
  |-- obj
  |-- src
  `-- hello.gpr
src/hello.adb:
with Gtk.Main;
with Gtk.Window;      use Gtk.Window;

with Main_quit;

procedure Hello is
   Win   : Gtk_Window;
begin
   --  Initialize GtkAda.
   Gtk.Main.Init;

   -- create a top level window
   Gtk_New (Win);
   Win.Set_Title ("Window");

   -- connect the "destroy" signal
   Win.On_Destroy (main_quit'Access);

   --  Show the window
   Win.Show_All;

   --  Start the Gtk+ main loop
   Gtk.Main.Main;
end Hello
src/main_quit.adb:
with Gtk.Widget;      use Gtk.Widget;
with Gtk.Main;

procedure main_quit (Self : access Gtk_Widget_Record'Class) is
begin
   Gtk.Main.Main_Quit;
end main_quit;
The project file hello.gpr:
with "gtkada";

project Hello is

   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Main use ("hello.adb");

   --  Enable Ada 2005.
   package Compiler is
      for Default_Switches ("ada") use ("-gnat05");
   end Compiler;

end Hello;
You then could compile the program as
gprbuild -P hello

All GtkAda application will, of course, with the Gtk.Main and Gtk.Window packages. We'll need to add a call back to quit the program, so with the Main_quit procedure.

In the main procedure Hello, we declared a Win variable as of type Gtk_Window. Since we have use Gtk.Window, we does not need to give the full path as Gtk.Window.Gtk_Window.

The following line will call Gtk.Main.Init, which is the initialization function for GtkAda; it calls the underline gtk_init() function and will setup GtkAda, the type system, the the connection to the windowing environment, etc. The Gtk.Main.Init take care of passing command line arguments counter and string array to the C gtk_init() function; this allows GTK+ to parse specific command line arguments that control the behavior of GTK+ itself. The parsed arguments will be removed from the array, leaving the unrecognized ones for your application to parse.

For more information on which command line arguments GtkAda recognizes, please refer to the Running GTK+ Applications section.

A call to Gtk_New (Win) will create a new Gtk_Window and store it inside the Win variable. Gtk_New has Gtk.Enums.WindowToplevel as its default The_Type argument, so we don't need to specify it with the call. Which means the the Gtk_Window will be managed by the windowing system: it will have a frame, a title bar and window controls, depending on the platform.

In order to terminate the application when the Win is destroyed we connect the "destroy" signal to the Main_quit function by calling Win.On_Destroy (main_quit'Access);

This function will terminate the GtkAda main loop started by calling Gtk.Main.Main later. The "destroy" signal is emitted when a widget is destroyed, either by explicitly calling Gtk.Widget.Destroy or when the widget is unparented. Top-level Gtk_Windows are also destroyed when the Close window control button is clicked.

Gtk_Window are hidden by default. By calling Win.Show_All, we are asking GtkAda to set the visibility attribute so that it can be displayed. Al this work is done after the main loop has been started.

The last line of interest is the call to Gtk.Main.Main. This function will start the GtkAda main loop and will block the control flow of Hello until Gtk.Main.Main_Quit is called.

While the program is running, GtkAda is receiving events. These are typically input events caused by the user interacting with your program, but also things like messages from the window manager or other applications. GtkAda processes these and as a result, signals may by emitted on your widgets. Connecting handlers for these signals is how you normally make your program do somthing in response to user input.

In the main_quit procedure, Gtk.Main.Main_Quit is called to quit the program.

The following example is slightly more complex, and tries to showcase some of the capabilities of GtkAda.

In the long tradition of programming languages and libraries, it is called Hello, World.

Using the same project file of the first example, here are the sources:

src/hello.adb:
with Gtk.Main;
with Gtk.Window;      use Gtk.Window;
with Gtk.Button;      use Gtk.Button;
with Gtkada.Handlers; use Gtkada.Handlers;

with hello_cb; use hello_cb;

procedure Hello is
   Win    : Gtk_Window;
   Button : Gtk_Button;
begin
   --  Initialize GtkAda.
   Gtk.Main.Init;

   -- create a top level window
   Gtk_New (Win);
   Win.Set_Title ("Window");

   -- When the window emits the "delete-event" signal (which is emitted
   -- by GTK+ in response to an event coming from the window manager,
   -- usually as a result of clicking the "close" window control), we
   -- ask it to call the on_delete_event() function as defined above.
   Win.On_Delete_Event (main_del'Access);

   -- connect the "destroy" signal
   Win.On_Destroy (main_quit'Access);

   -- set the border width of the window
   Win.Set_Border_Width (10);

   -- create a button with label
   Gtk_New (Button, "Hello World");

   -- connect the click signal
   Button.On_Clicked (button_clicked'Access);

   -- connect the "clicked" signal of the button to destroy function,
   -- it will destroy the Win when button is clicked.
   Widget_Callback.Object_Connect
     (Button,
      "clicked",
      Widget_Callback.To_Marshaller (button_quit'Access),
      Win);

   -- This packs the button into the window. A Gtk_Window inherits from Gtk_Bin
   -- which is a special container that can only have one child.
   Win.Add (Button);

   --  Show all the window
   Win.Show_All;

   -- All GTK applications must have a Gtk.Main.Main. Control ends here
   -- and waits for an event to occur (like a key press or a mouse event),
   -- until Gtk.Main.Main_Quit is called.
   Gtk.Main.Main;
end Hello;
src/hello_cb.ads:
with Gtk.Widget;  use Gtk.Widget;
with Gtk.Button;  use Gtk.Button;
with Glib.Object;

with Gdk.Event;

package hello_cb is
   function main_del
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return  Boolean;
   procedure main_quit (Self : access Gtk_Widget_Record'Class);
   procedure button_clicked (Self : access Gtk_Button_Record'Class);
   procedure button_quit (Self : access Gtk_Widget_Record'Class);
end hello_cb;
src/hello_cb.adb:
with Ada.Text_IO; use Ada.Text_IO;
with Gtk.Main;

package body hello_cb is
   -- If you return false in the "delete_event" signal handler,
   -- GTK will emit the "destroy" signal. Returning true means
   -- you don't want the window to be destroyed.
   --
   -- This is useful for popping up 'are you sure you want to quit?'
   -- type dialogs.
   function main_del
     (Self  : access Gtk_Widget_Record'Class;
      Event : Gdk.Event.Gdk_Event)
      return  Boolean
   is
   begin
      Put_Line ("Delete event encounter.");
      return True;
   end main_del;

   procedure main_quit (Self : access Gtk_Widget_Record'Class) is
   begin
      Gtk.Main.Main_Quit;
   end main_quit;

   procedure button_clicked (Self : access Gtk_Button_Record'Class) is
   begin
      Put_Line ("Hello clicked");
   end button_clicked;

   procedure button_quit (Self : access Gtk_Widget_Record'Class) is
   begin
      Put_Line ("buttion_quit is called");
      Destroy (Self);
   end button_quit;

end hello_cb;
You compile the program with the same command:
gprbuild -P hello

This time, we defined a hello_cb package to host all the call back functions.

In GtkAda, there is no need for g_signal_connect_swapped(), we use the Widget_Callback.Object_Connect method.

P.S. All source code from these series of Getting Started with GtkAda are now at GitHub.

Google wearables

If this Google Watch could function independently as a phone without a pairing smart phone, I will immediately get one when its out. If it needs a pairing smart phone then forget it!

Friday, February 21, 2014

A Slackware banner logo for rEFInd Boot Manager

The default rEFInd boot manager's logo causing the boot screen to have a white background, as rEFInd use the color of banner image's the top left corner pixel as its background color. I would like to have a black one, so I try to create a boot logo for it with a black background.
I would like to have a Slackware logo display on the boot screen, look at the the logos from "Slackware Banners, Logos, Propaganda" page, I like this one

But it is a little small and scaling it up does not look nice, I tried to vectorized it with Potrace but the result does not look good. Searching around for the font use in the image, and found out it is computer modern typewriter text. There is this project Computer Modern Unicode fonts that has the computer modern font converted from Khuth's famous TeX fonts. Now I need to use a vector drawing program, I know inkscape could do it, and proceed to install it and all its dependencies with the help of sbopkg. The installation went well.
Here is the result:

The svg source could be downloaded here.

There is a SlackBuild script for rEFInd now. The logo's png and svg source are included in the build script.

Enjoy ^_^

Monday, February 17, 2014

Install Slackware64 14.1 on a MacBook Pro 5.2

The MacBook feels its ages after upgrading to OS X Mavericks‎, it is slow and I really don't have a usage of the OS X itself. So I start my journey to install my favorite Slackware to it.

There are a few helpful sites that describe the installation of Linux on a MacBook:

Disk Partition

There are some problems that I encounter that is quite unique to this machine. I shrank the OS X partition and created a new 40G FAT partition and boot up the newly burned Slackware64 14.1 DVD. It could not boot using EFI mode, and fallback to the legacy BIOS boot mode.

After boot up, I partition the disk using cgdisk, remove the FAT partition and recreate one for Linux. An interesting thing shows up, after selected my target / partition and the ESP, it ask me whether to include an MSDOS partition to be mounted, with the same device number of my main Linux partition. I ignored it, and proceed to install the "A" and "AP" part only. Then when come to install Lilo, it stucks forever without progress. Google around, does not show any direct related problem. Then I found some mentioning of gpartsync tool, it seems in rEFInd (which I chose as my boot manager), there is one included name "gptsync_x64.efi", turn it on from the config and reboot, it does not work for me. Then I reboot into Linux with the DVD and use fdisk, Och, there is a FAT partition there. To play safe, I repartition the disk again in OS X and redo the installation with both GPT and MBR table matched, it seems fine, except it prompt me with 2 identical Linux partitions for my root mount point, I chose one and ignored the other and proceed. Lilo still complaining about config problem about append command, then I remove the append line use standard text mode only. It finally installed. There must be something wrong for what I have done for the partition, but I don't want to experiment and re-install again.

Booting Up

Since the stock kernel has EFI stub build in, I copy the huge kernel to the EFI/slackware64 and create a refind_linux.conf manually

"Boot with defaults"         "root=/dev/sda4 ro"
I hope rEFInd could pick it up and boot me into Linux directly from EFI. I restart the box, the rEFInd boot manager has the Linux choice show up, boot it, kernel complains about conflicting frame buffer driver between EFI VGA and nouveau and stops. I think it should be able to resolve it with the extra parameter "video=efifb"

I reboot and boot the Linux with BIOS mode and Lilo bring up the system. But I only have one CPU show up, it seems this is a known problem. I install the the rest packages from D, X, XAP, N, L, KDE, TCL, XFCE, Y, etc. Then download the 3.10.20 kernel source and config it to use nouveau driver only for frame buffer. It boots up fine and now I have the two CPU cores available.

There is a long delay (around 30 to 40 seconds) before rEFInd show its menu. This seems a known problem on certain MacBook. I tried the clearing the NVRAM entries method first, but it did not work for me. Then I tried using the fallback filename method, rename EFI/refind to EFI/BOOT and rename refind_x64.efi to bootx64.efi. It seems work, I now got around 6 seconds of delay. Not sure if that is normal, but at least its bearable.

Nouveau and X.org

X does not work, it said DRM is not working. I download the latest binary driver from nvidia, and it seems working fine, except after exit from X.org, I got an black screen and has to blind type commands to shutdown the machine. At system boot, I use efifb for the frame buffer driver. It seems it is a common problem, fellow slackers have the same problem "Cannot access TTYs after installing nvidia drivers on macbook pro".

The touch pad respond badly, almost like not working, google around, it seems the default setting of synaptics does not work well, I ended up with this settings under /usr/share/X11/xorg.conf.d/50-synaptics.conf

Section "InputClass"
 Identifier "touchpad"
 Driver "synaptics"
 MatchDevicePath "/dev/input/event*"
 MatchIsTouchpad "on"
 Option "FingerLow" "10"
 Option "FingerHigh" "20"
 Option "TapButton1" "1"
 Option "TapButton2" "2"
 Option "MinSpeed" "0.65"
 Option "MaxSpeed" "0.85"
 Option "TapButton3" "3"
 Option "VertTwoFingerScroll" "true"
EndSection
MinSpeed of 1 is still too fast when accessing some small links, 0.65 seems reasonable for me.

To handle the hotkeys, I used pommed-light, after a few tweak, I got it compile and running, the sound volume control works. Others are still testing.

Annoying things of the system

The iSight built-in web-cam does not work. At first, the system does not recognize the device at all, listing the usb device as some kind of development board. Even booting back to OS X does not recognize the webcam anymore. Using solution in {CODIUM();}, I manage to get the iSight webcam recognized correctly again under OS X and under Linux, lsusb recognize the device as iSight, but no luck getting it working. I don't really making use of it, so it is not a very big problem for me.

The bootup sound before the boot manger is really annoying, looking around the web, seems there are lots of people complaining. I follow the advice from MacTrast, and tried the 3 methods: overwrite the nvram settings, install a control application to mute the bootup sound, and mute the sound device before power off. Mute the sound before poweroff/reboot does not work too well and it seems does not work from Linux. Set the nvram value to 0 or 80 does not work either. The StartNinja apps seems to work first, but then I drained my battery completely and lost the setting, and I thought it did not work until I try the 2nd time, consider working.

Wednesday, September 11, 2013

Redshift to get some relief for your eyes

I am working in front of computer all day long, and looking at the bright screen hurt my eyes.  I was relied on the plugin ToggleDocumentColors to give me a grey background and black text with browser.  But it does not look good on some web sites and it only works on browser.  Then I came across this Redshift program, and it has a SlackBuilds.org script.  After installing it with sbopkg, found my latitude and longitude and fire up the program, instantly I could feel the stress of my eyes are reduced.

It may take sometime to get used to it as the color on the screen has a little reddish tone.  Unless you are working on graphics or color sensitive work, normal interaction with the web site or application has no problem at all and your eyes got some relief.  Since the discovery of this little tool, I have installed it to all my systems.

It has an GUI front end gtk-redshift that comes with the package.  It will display an icon on your system tray area, allow you to toggle it by a single click.

Highly recommend this little tool to anyone that need to work in front of computers.

Friday, August 16, 2013

Automatic new line at program exit for Ada.Text_IO output.

For these two simple programs:

with Ada.Text_IO; use Ada.Text_IO;

procedure Hello is
begin
    Put("Hello");
    Put(" ");
    Put("World!");
end Hello;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO.Text_Streams; use Ada.Text_IO.Text_Streams;

procedure hello_stream is
    std_out   : Stream_Access;
begin
    std_out := Stream (Standard_Output);

    String'Write (std_out, "Hello");
    String'Write (std_out, " ");
    String'Write (std_out, "World!");
end hello_stream;

When executing the two program, you would have two different result, for hello program:

# ./hello
Hello World!
#

For hello_stream program:

# ./hello_stream
Hello World!# 

I have known this problem for some time, but did not dig into the root cause of it.  Recently I asked this question in comp.lang.ada group for a root cause of it. And Jeffrey gave me the answer.

The related references in ARM are A.7(6) and A.10.1(86).

For gnat compiler File_Type is defined at a-textio.ads:

...
type Text_AFCB;
type File_Type is access all Text_AFCB;

type Text_AFCB is new FCB.AFCB with record
...
end record
...
procedure AFCB_Close (File : not null access Text_AFCB);
Where FCB.AFCB is a tagged type defined in s-ficobl.ads.

In AFCB_Close() procedure, it calls Terminate_Line(), which will try to insert a new line if not terminate properly.

It seems, when program exit, gnat doing the finalization for Standard_Output call AFCB_Close(), which insert the new line if necessary.

p.s. This is tested under Slackware64 Linux with GCC gnat compiler.