Saturday, July 17, 2010

GUI in Bash using GTK-server

Gtk-server allows to create GUI providing access to GTK+ library functions in interpreted programming and scripting languages such as bash (it  can also be used for many libraries other than GTK).To use gtk-server in bash , it has to be started asynchronously,upon starting it reads the configuration file (more on this later) and then GTK functions can be executed by sending it as plain text to gtk-server.Each time you send some text to gtk-server it sends reply in form of strings back.These functions can be send and replies obtained by various ways.

Gtk-server comes bundled with examples for using bash via fifo(named pipes),tcp and ipc to communicate with gtk-server.However, bash 4 supports coprocesses which allows execution of a command in the background with two file descriptors connected to standard output and standard input of the command.The coprocess can be created by using coproc builtin(see man bash or type help coproc at bash prompt for more information).We will be using coprocess for communication with gtk-server.(It is possible to create something similar to coprocess using named pipes and exec in earlier bash versions,but since gtk-server allows a  -fifo=fifo-name internal argument for creation of named pipes, its not worth the effort)

First off , we need a function to communicate with gtk-server and for that we need to write a function that will do exactly that.Here is a basic function that we can use to communicate with gtk-server.

#!/bin/bash

gtk()
{ 
printf '%s\n' "$*" >&"${COPROC[1]}" 
read -ru "$COPROC" 
}

Now we can go on to start gtk-server

coproc gtk-server -stdin

This starts gtk-server asynchronously and gives us two file descriptors to communicate with gtk-server stored in COPROC array which we used in the gtk function above and also a variable COPROC_PID which contains the pid of gtk-server.

With gtk-server started and communication function defined all we have to do is send gtk functions to gtk-server.But thats not all, you would need to define these functions in gtk-server's configuration file.For now we will not define any functions and use the standard configuration file that comes with gtk-server assuming that you have the configuration file in the same directory as your script or in one of the standard locations(read the manual for more on standard location of configuration file :) ).

First we need to initialize the gtk library.In C, you would do this in main as follows:
gtk_init( &argc, &argv );
The function would initialize gtk and parse some command line arguments.Using our gtk function, we will initialize the library as
gtk gtk_init NULL NULL 

Notice that we did not pass command line arguments to gtk_init(More on this later).We would not use the REPLY read by gtk function because gtk_init does not return any value.If you do an `echo "$REPLY"` just after this line in your script, it will echo ok.Gtk-server sends back "ok" for functions which do not return anything and -1 for any function which was send to it but was not described in the configuration file.

Now, lets create our main window,we will use gtk_window_new function
gtk gtk_window_new GTK_WINDOW_TOPLEVEL
window=$REPLY 

This time we stored the reply from gtk-server into variable window.gtk_window_new returns a pointer to the newly created window.Gtk-servr in turn replies back with a Widget Id to refer to the newly created window to our client script.We store the reply from gtk-server and use the value as Widget Id every time we want to refernce to the top level window.

Next we create a button with label hello on it.
gtk gtk_button_new_with_label hello 
button=$REPLY

Its time to add the button to the container window.In gtk,you add widgets to containers.The window can be used as a container and you can add buttons to it.But generally you would add a vbox or hbox or a table to the window and pack other widgets inside it.

gtk gtk_container_add "$window" "$button" 

Now that we have our button added to the window, we are ready to show it all,we shall use gtk_widget_show_all for this but you can use gtk_widget_show for each widget too.

gtk gtk_widget_show_all "$window" 

Having everything ready, all we need to do is create a main loop:
until [[ $event = $window ]];do
  gtk gtk_server_callback wait
  event=$REPLY
done

gtk gtk_server_exit
wait "$COPROC_PID"

In the standard configuration file, gtk_window_new is defined with callback signal delete-event.So if you click on the main window close button provided by the window manager, signal
is emitted.gtk_server_callback is an internal function provided by gtk-server fetches signals .The argument wait tells it to return only when a signal has occured.By default Widget ID is returned by gtk_server_callback on a signal.So unless widget ID of the main window which we stored in variable window, is returned we continue with the loop(gtk_server_callback also updates Gtk widgets).

Once the loop ends gtk_server_exit tells gtk-server to cleanup and exit.Finally we use wait builtin to wait for gtk-server to complete.This also returns the exit status of gtk-server.

Here is all of it put together.Please note that the example has been simplified and a lot of error checking has been omitted for the purpose of illustration.However this should get you started.You would notice that the window does not look very good, you need some more GTK functions to make it look decent(Check the GTK API documnetation at gtk.org link provided at the end)

gtk()
{ 
printf '%s\n' "$*" >&"${COPROC[1]}" 
read -ru "$COPROC" 
}
 
coproc gtk-server -stdin

gtk gtk_init NULL NULL 

gtk gtk_window_new GTK_WINDOW_TOPLEVEL
window=$REPLY 

gtk gtk_button_new_with_label hello 
button=$REPLY

gtk gtk_container_add "$window" "$button" 
gtk gtk_widget_show_all "$window" 

until [[ $event = $window ]];do
  gtk gtk_server_callback wait
  event=$REPLY
done

gtk gtk_server_exit
wait "$COPROC_PID"

The configuration file not only allows you to tell gtk-server about a gtk function but also define some constants and enumerations, include another configuration file and define macros.The macros make gtk-server highly programmable(you can write a simple stand alone program in it).I will take a closer look on gtk-server configuration file and macros in my future posts.

To Be Continued...

External Links:

Tuesday, April 27, 2010

making sense of "the common sense"

So i had a clumsy moment , and then i hear some loud voice punching on my ear drums--"Where is your COMMON SENSE?"


"I do have common sense but probably thats not your definition of the terminology" , i yelled back.

( Yelling is like reflex action , when some one yells at you , you yell back.In this way it is similar to yawning, the consequences of yelling are most of the time worse than result of yawning though )

So we all have common sense.However, the "common" in it is quite misleading.There isn't a specific and "common" to all set of "senses" defined in "The Common Sense Bible" that people should possess in order to have common sense.Common sense is rather a set of "senses" that is  "unique" to an individual( this set overlaps or intersects to certain extent with other individual sets and one can argue that it is not unique at all ).And using this "set of senses" in our "common" or day to day life we find it so natural that we consider it as "common sense".People have common sense that seems natural and common to them and it is not the same as the common sense I  have, We all have common sense but it is not common to all of us.

Common sense is not something we are born with hard coded in our genes, it is acquired. Doing simple task as buttoning a shirt or balancing on single or both feet(walking i mean) may seem trivial today, but we had to learn it once upon a time

Brain is the best organ of all in Human body, the reason why there is war and why we do everything we do, good or bad (In fact "good" and "bad" are the creation of the brain too).It is interesting but complicated and the power and complexities of the organ "brain" continues to amaze many great "brains" even today.  It is our brain that learns each and every thing we do consciously or unconsciously with every repetition storing it in memory so that walking or cycling which once seems so difficult, becomes effortless ( not completely, you still need your voluntary will and muscle power ).It is amazing how the brain deals with the physics without you thinking about it, making you lean and move in direction that help you keep your balance.It creates an abstraction layer between things stored in the memory and our conscious movements.

Brain makes mistakes too, and while cycling you may fall, but again brain learns from this mistake and stores it in the memory.It is unlikely but possible that you may fall making the same mistake( probably the saying "Man makes mistakes but fools repeats them" was an indication towards the fact all brain except foolish ones learn from their mistakes).It is that people do not accept the fact that the best way of learning is by commiting mistakes because if they did the education system wouldn't treat failure as bad but as a step forward in the learning process.

We all have led a different life and had our own journey of learning by practicing and commiting mistakes since our birth to where ever we stand now in the road of life.So we have all acquired a specific set of sense, actually the learned memory of our brain which we apply every day in our lives.This is common sense and only things i find common here is that we all acquire it through a process of learning and that we all seem to find it natural once we acquire it. A person who spent his school days solving math problems may find a certain way of solving those problems as common sense while others may not.Similarly a person who never actually looked at those idiotic fashion magazines or paid attention to how people dress in TV or real life may lack fashion sense according to some others(fashion sense is a subset of common sense that some of us acquire as we grow up and so is civic sense and many others). So what may seem common sense to you , may not seem the same to me and saying  "you DONT HAVE COMMON SENSE" reflects nothing but the fact that you dont really understand common sense or you dont define it as i do or the fact that i misunderstood what common sense is (take your pick, i dont want to know which option you chose)

If you read the entire of this ****  and if it makes no sense to you , dont worry, even our way of understanding and looking at things is acquired and therefore not common (that tells you something about my  life , doesn't it? )

Friday, March 19, 2010

Reflections

I have had quite a few blogs on various subjectsand almost all were poorly updated  and maintained. A busy life ( yes , my life sucks )  makes it almost impossible to maintain so many blogs. I have been thinking of uniting all these blogs into one blog for quite some time. Its far easier to write one blog for someone with as less time (read: lazy) as me. But being lazy as i am ( its congenital , i cant help) i never really made such a blog until now.
Finally , here is the blog called "Reflections" , a united blog which i promise myself to update regularly. However , as you know , i am lazy...

Saturday, November 28, 2009

KDE4-Another theme for ATM

KDE4 is a dark theme with a big panel/taskbar. I got the original icewm theme from here.After downloading it, I customized it to include the AsusLauncher tabs of similar color as the panel.This theme doesn't include a default wallpaper.A default background for the theme can be set by copying your wallpaper jpg file to /home/user/.icewm/themes/KDE4/AsusLauncher/wallpapers/business_tab_wallpaper.jpg

screenshot

download
you can download the customised theme from here

Saturday, September 26, 2009

my girlfriends sketch


this is a portrait sketch of my girlfriend that i made while she was lying on the bed....not so good in sketching someone live but i think it resembles her a lot

Friday, September 11, 2009

Some Themes to use with ATM-the theme manager(Nano Blue and GoldiKwarc)

There are a lot of themes made for icewm but not many of them are customised to provide images for tabs of the launcher on easy mode of eeepc .So when i started using ATM to customise themes, i started customising some of the themes to include the images for tabs.i will be adding new customised themes in this post.Most of the themes are not mine but downloaded over internet.

Before using any of the themes make sure that you are using AsusSilver theme using personalization under settings tab.Also make a text file in /home/user/.Asuslauncher called launchermode (if it is not already there) and replace any content inside it with single word "business"

Nano Blu

I got this theme from www.box-look.org and customised it to include images for tabs.The original theme can be downloaded here
screenshot













download

the customised theme can be downloaded here


goldikwark

First saw this theme in the screenshot posted by eeeuser member rokytnji and i liked it.I got this theme from a zip folder called FMThemes.zip posted on the antix forums.The look of this theme is quite like kde (kwarc theme)with the default kicker look (the kde panel)
After downloading the theme , i made the following customizations:

  • added a taskbarbg.xpm which is a gradient image to make the icewm taskbar look more close to kde(actually i downloaded the kicker.deb file and extracted it to get the background for kicker and resized and processed it to make a taskbarbg.xpm)
  • changed the menubg.xpm to make its look more gradient-like by overlaying resized taskbarbg.xpm over original menubg.xpm
  • changed the menuseparater(menusep.xpm) to make it look thicker
  • downloaded a wallpaper that would suit with the theme
  • and ,off course ,added the backgrounds for tabs to complete the look for easy mode
screenshot












download
Download the customised theme here.The wallpaper shown in the screenshot is copyrighted and not included with the theme.Download the wallpaper here or any other wallpaper you would like to use with the theme

useful tip:
To make sure every time you apply the theme the background also gets changed to the wallpaper image , copy the wallpaper to /home/user/.icewm/themes/goldikwarc/AsusLauncher/wallpapers and rename it as business_all_wallpaper.jpg .Now every time you apply the theme using ATM the wallpaper will also get applied(this works for other themes too)


KDE4


KDE4 is a dark theme with a big panel/taskbar. I got the original icewm theme from here.After downloading it, I customized it to include the same colored tabs for AsusLauncher.This theme doesn't include a default wallpaper.A default background for the theme can be set by copying your wallpaper jpg file to /home/user/.icewm/themes/KDE4/AsusLauncher/wallpapers/business_tab_wallpaper.jpg

screenshot


download
you can download the customised theme from here

original post
kde4-Another theme for ATM 

Wednesday, September 9, 2009

ATM-the theme manager for my eeepc

For my eeepc vanity needs i used to use theemer a wonderfully designed application with great gui.However,of late,i have not been using theemer as there is a bug in it on using gradient based images for the tabs in AsusLauncher the text on the tabs disappear which seems to be due to resizing of images in theeemer.I like changing my desktop look often and since i was unable to get theemer to work the way i wanted it to , I decided to make my own little script with gui using gtkdialog .

ATM which is short for Anshul's Theme Manager originally started developing as a command line tool to change themes and backgrounds.However while changing backgrounds i realised a simple gui would do a better justice to the tool by showing previews of the backgrounds selected(still working on preview part though).As i started developing it ,i started learning many things about bash scripting ,gtkdialog and how the icons and other components of gui work in the easy mode.New ideas started developing and i started implementing some of them into my new gui based script.Now ATM is the theme manager with features that include changing themes , backgrounds , and icon modes.
The bouncy icon mode is the one i like the most it increases the size of selected icons on the launcher by 12% compared to normal icons displaying a nice and cool bounce roll over effect when the icon is highlighted.

For changing the icon modes i settled for no backup of previous icons for restoration as space on eeepc is less.I would recommend any one using it to make a backup of /opt/xandros/share/AsusLauncher folder on memorycard or usb to easily restore in case something doesnt work out as expected.

summary
ATM- Anshul's Theme Manager-The beauty kit to beautify my eeepc
features

  • tabbed interface with a tab each for applying entire theme(icewm,tabs and background);applying background for selected tabs;appling only icewm theme;and changing icon mode
  • icon modes include bouncy , silver, sunset,blue and green
  • bouncy icon mode creates a larger icon for highlighted image as compared to normal icons
  • silver , sunset ,blue and green give the normal highlighted background as found in the original themes of eeepc
features to be implemented
  • easy installation of packaged themes(like theeemer)
  • themes containing a .mode file and highlighted icon background to direct their own icon background (changing the icon background is rather slow that is why it is yet unimplemented)
ps:this is something i designed for my own use and has been designed to work on some customized settings in the easy mode

image
here is a preview image of my eeepc with bouncy icon mode the webcam icon is larger than others as it is highlighted













video
here is a link to original video posted by me on you tube of my desktop using bouncy icon mode...in reality it is smoother than it appears in the video




download
(updated)
i noticed today that the beta version ATM-0.1-1.0 has a problem that the tab for applying icewm theme didnt use to work properly.Here is the download link to beta ATM-0.1-1.1.The beta ATM-0.1-1.1 version could not parse simpleui.rc for 900 so the updated script ATM-0.1-1.2 can be downloaded here.The beta ATM-0.1-1.2 has been verified to be working fine on eeepc 701 and eeepc 900 .So, here is the first stable release of the script ATM-0.1
click to download ATM-0.1
(a deb file shall be available soon)
a minor feature update of ATM is available as beta.For features added refer here
click to download beta ATM-0.2-0.3
(beta version 0.2-0.3 has been updated to include a feature in the installation script to have seperate wallpapers for favorites and internet tabs in the default tab setup)


installation

you can either use the installation script or do this manually

manual installation

  1. install gtkdialog using synaptic,apt-get or aptitude(the version that i am using is 2:0.7.9-1(stable) and is preferable as it is verified to work properly)
  2. install imagemagick (the version i am using is 7:6.2.4.5.dfsg1-0.14(update.eeepc.asus.com) and is again preferable)
  3. download the tar.gz and extract it( i will assume you have extracted it to /home/user)
  4. issue the following commands in a terminal:
    #sudo mkdir /opt/ATM/
    #cd /home/user/ATM/
    #sudo cp libthemechanger.sh themechangergui.sh /opt/ATM/
    #sudo ln -s /opt/ATM/themechangergui.sh /usr/bin/ATM
  5. to use the script you need to do the following:
  • copy simpleui.rc from /opt/xandros/share/AsusLauncher/ (for 701) or /var/lib/AsusLauncher/(for 901 and people who have updated 701 with Asus updates) to /home/user/.AsusLauncher/
  • make sure you have ~/.icewm/ containing the file theme and themes directory containing all the themes you would like to use
  • it is recommended you make a backup copy of /opt/xandros/share/AsusLauncher/ directory if you are planning to change the icon modes
    installation using the installer script

    1. follow the steps till step 3 of manual way
    2. open a terminal and type cd /home/user/ATM
    3. type bash ATMinstaller.sh
    4. the script will ask you wether to copy themes file from system directory to /home/user/.icewm/themes.Answer "y" if you want to use the themes file stored in the system directory /usr/share/icewm/themes/.Answer "n" if you already have themes in the /home/user/.icewm/themes and you dont intend to use the themes in /usr/share/icewm/themes/
    5. next the script will ask you wether you want to make a backup copy of the icons in /opt/xandros/share/AsusLauncher/..(it is recommended you make a backup on some external drive)answer "y" to make a backup and type in your preffered location.The script will make a backup with a folder named AsusLauncher_bkup in your specified location
    6. after creating the backup the script will complete the installation
    changelog

    0.2-0.4 beta
    updated the installer script to ask the user if they want to edit simpleui.rc to have seperate wallpapers for favorites and internte tab

    0.2-0.3 beta
    added a reload of launcher screen on changing of icon modes and creation of new icons

    0.2-0.2 beta
    fixed the entry field bug and made the libthemechanger code clearer and less ambiguos

    0.2-0.1 beta
    added new feature to create individual icons in easy mode.More details are here

    0.1

    first stable release


    0.1-1.2 beta

    fixed the simpleui parsing issue on 900 and made the parsing more generic in libthemechanger.sh

    0.1-1.1 beta
    fixed the icewm theme changing issue under icewm tab