Saturday, May 30, 2009

Simplifying GCC

GCC is the GNU Compiler Collection which provides C, C++ etc compilers. These compilers are used by default in all *nixes .

Here i provide simple command line options which can prove to be quite useful.

  1. The simplest way to use GCC to compile a C source file is

    $ gcc -o test test1.c test2.c

    gcc is the C compiler, test1.c and test2.c are the input C source files and -o lets us specify the name of the output file. Here it is "test". Without the -o option, "a.out" is the default executable that gets created.

  2. The preprocessor:

    $ gcc -E test.c > test.out

    This option, ensures the compilation process stops after the pre-processor has run. This helps us in figuring out issues/problems in macros.


  3. The Compiler:

    $ gcc -c test.c -o test.o

    This option ensures the compilation process completes but doesn't invoke the linker/loader. This is useful if you want to just remove compilation warnings and errors.


  4. Header Files:

    $gcc -c test.c -I /location/of/header/files -o test

    Many a times the headers files you want to use, is located is some other directory. A "bad" practice followed is to include the direct path of the header files in the C src file.
    Instead use this option. It tells the compiler which directories to look in for the mentioned header files. The -I options can be used multiple times for multiple directories where header files are located.

  5. Library Files:

    $ gcc -c test.c -lpthread -L /usr/lib/libpthread


    Another requirement that is frequently required is using standard libraries ( NPTL Threads etc) or non-standard ones (expat etc). '-l' option tells which library to use while linking while '-L' tells where the find this library. In the above example during linking, it will search for pthread library in the dir /usr/lib/libpthread.

  6. Warnings, Errors, etc:

    $gcc test.c -o test -Wall -Werror


    -Wall options shows all warnings that are typically not shown during regular compilation. These errors are easy fixable like "Unused varniables", "implicit function declaration" etc. -Werror options tells the compiler to treat all warnings as errors and stop compilation instantly.
    Sometimes -Werror can be too strict for our purpose. Instead you can treat only certain warnings are errors.
    eg. -Werror-implicit-function-declaration: Treat only implicit function declaration warnings as errors. For more such options check the gcc man pages.

  7. Debugging:

    $gcc -g test.c -o test

    This option activates all the debugging symbols. This is required if one plans to use gdb for debugging (which is mostly the case).

  8. Optimizations:
    $gcc -O2 test.c -o test

    This option lets the compiler optimize the code . -O can take 0,1,2 levels of optimizations.
    More info is available in the man pages of gcc.
These options are the ones that are most frequently used. Obviously there are many more options available . Use them as per your needs and refer the man pages for the exhaustive list of options.

Tuesday, May 19, 2009

Fix Thinkpad Function Keys/Hot Keys in kde 4.2 (Jaunty) - patch to display brightness levels.

A small patch for the script to display the brightness level of your Thinkpad display with the brightness Increase/Decrease keys.


--- softkeys.py 2009-05-08 11:51:28.000000000 +0530                       
+++ softkeys-0.1.py     2009-05-19 12:43:58.000000000 +0530               
@@ -1,6 +1,7 @@                                                           
 #!/usr/bin/env python                                                    
 # -*- coding: utf-8 -*-                                                  
 # Copyright: 2009 by Tillmann Falck <tillmann _at_ falcken _dot_ de>     
+# Uttaran Dutta added the funtion for the display for the brightness for T60p <linux-hacks.blogspot.com>
 # License: GPL v2 or later                                                                             
                                                                                                        
 from Xlib.display import Display                                                                       
@@ -16,8 +17,11 @@                                                                                      
 XF86Battery = 244                                                                                      
 XF86Display = 235                                                                                      
 KeyUndock = 202                                                                                        
+XF86MonBrightnessDown = 232                                                                            
+XF86MonBrightnessUp = 233                                                                              
                                                                                                        
-keys = [XF86Sleep, XF86Standby, XF86Screensaver, XF86Battery, XF86Display, KeyUndock]                  
+                                                                                                       
+keys = [XF86Sleep, XF86Standby, XF86Screensaver, XF86Battery, XF86Display, KeyUndock, XF86MonBrightnessDown, XF86MonBrightnessUp]
                                                                                                                                 
 def callDisplay():                                                                                                              
   try:                                                                                                                          
@@ -63,6 +67,14 @@                                                                                                               
     print >>sys.stderr, 'Error while trying to change output, try full auto'                                                    
     os.system('xrandr --auto')                                                                                                  
                                                                                                                                 
+def briStats(bus):                                                                                                              
+  bri = bus.get_object( \                                                                                                       
+    'org.freedesktop.Hal', \                                                                                                    
+    '/org/freedesktop/Hal/devices/computer_backlight')                                                                          
+  briLevel = (bri.GetBrightness(dbus_interface='org.freedesktop.Hal.Device.LaptopPanel')*100)/(bri.GetProperty('laptop_panel.num_levels', dbus_interface='org.freedesktop.Hal.Device')-1)                                                                                         
+  s = 'Brightness level is %d%% <br />' % (briLevel)                                                                                    
+  return s                                                                                                                              
+                                                                                                                                        
 def acStats(bus):                                                                                                                       
   ac = bus.get_object( \                                                                                                                
     'org.freedesktop.Hal', \
@@ -91,6 +103,26 @@
     s = 'Battery %d level %d%%<br />' % (n+1, batLevel)
   return s

+def callBrightness():
+  try:
+    sessBus = dbus.SessionBus()
+    sysBus = dbus.SystemBus()
+
+    hal = dbus.Interface( \
+      sysBus.get_object('org.freedesktop.Hal', \
+        '/org/freedesktop/Hal/Manager'), \
+      dbus_interface='org.freedesktop.Hal.Manager')
+
+    s = briStats(sysBus)
+    ea = dbus.Array([], 's')
+    notify = sessBus.get_object('org.kde.VisualNotifications', '/VisualNotifications')
+    notify.Notify('brightness information',0,'brightness information','','Brightness Information', \
+      s,ea,ea,5000, \
+      dbus_interface='org.kde.VisualNotifications')
+  except:
+    print >>sys.stderr, 'No notifications or no information'
+
+
 def callBattery():
   try:
     sessBus = dbus.SessionBus()
@@ -101,6 +133,7 @@
         '/org/freedesktop/Hal/Manager'), \
       dbus_interface='org.freedesktop.Hal.Manager')

+    briStats(sysBus)
     if hal.DeviceExists('/org/freedesktop/Hal/devices/computer_power_supply_ac_adapter_AC'):
       ac = acStats(sysBus)
     else:
@@ -197,6 +230,8 @@
     callSuspend()
   elif event.detail == KeyUndock:
     callUndock()
+  elif event.detail == XF86MonBrightnessUp or event.detail == XF86MonBrightnessDown:
+    callBrightness()
   else:
     print >>sys.stderr, "Key %d not handeled" % event.detail


Note- The patch just enables the display changing for display brightness should be already working through acpi.

Friday, May 8, 2009

Fix Thinkpad Function Keys/Hot Keys in kde 4.2 (Jaunty)

Function keys doent work by default in kde 4.2. Those who migrate from gnome to kde 4 find it difficult to adjust without the 'fn' keys. The problem is with powerdevil. The new applet does not have options to configure the acpi events. This will exits till powerdevil is fixed. As of now, we can use a python script to handle these events. Make sure python-2.6, python-dbus and python-xlib are installed in your system. Download the script from here or here.
Give exe permission
#chmod +x softkeys.py

Place the script in ~/.kde/Autostart/ and restart your system. The function keys must start working now.