lunedì 13 settembre 2010

Enable and disable bluetooth on Thinkpad x61/s

I wrote this code  to enable and/or disable bluetooth on Ibm Thinkpad models x61 and  x61s (maybe also other models) with a linux system. Tested on x61s model with slackware linux and debian linux.

How to compile on debian system and derivants :
 
gcc $(pkg-config --cflags --libs gtk+-2.0) x61_bt.c -o x61_bt

How to compile on slackware or generic linux system:
 
gcc -g x61_bt.c -o x61_bt `gtk-config --cflags` `gtk-config --libs`

require:
gtk, thinkpad_acpi module, kdesu or gksu or su or sudo

I tried it on Ubuntu but doesen’t work fine like other linux distribution.
Here the code:
/* ********************************
   author: Muratore Alessandro
   e.mail: muratore.ale@gmail.com 
   x61_bt    
*********************************** */
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <unistd.h>

#define bt_files "/proc/acpi/ibm/bluetooth"

void delete_event (GtkWidget *widget, gpointer data)
{
     gtk_main_quit();
}

/* error button */
void not_fnd (){
 GtkWidget *window;
 GtkWidget  *button;
 GtkWidget  *table;
     GtkWidget  *label;

     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

     gtk_window_set_title (GTK_WINDOW (window), "Bluetooth Error");

 gtk_container_border_width (GTK_CONTAINER (window), 5);

     table = gtk_table_new (2, 2, TRUE);
     gtk_container_add (GTK_CONTAINER (window), table);

     label = gtk_label_new ("Error -  could not found :  /proc/acpi/ibm/bluetooth");
     gtk_table_attach_defaults (GTK_TABLE(table), label, 0, 2, 1, 2);
     gtk_widget_show (label);

     button = gtk_button_new_with_label ("Quit");
     gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (delete_event), NULL);
    gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 2, 0, 1);
     gtk_widget_show (button);

     gtk_widget_show (table);
     gtk_widget_show (window);
     gtk_main ();
}

/* check /proc/acpi/ibm/bluetooth */
int chk_bt_file (){
        FILE  *prc_bt_x61;
        if ( (prc_bt_x61 = fopen (bt_files, "r") ) == NULL)
        {
                printf ("nError: could not found: /proc/acpi/bluetooth/nn");
                not_fnd ();
  exit (1);
        }
 return 0;
}

/* enable function as root */
void enbl_bt_root (void){

 chk_bt_file(); 

 if (getuid () == 0){
  return ;
 }
 execl ("/usr/bin/kdesu", "kdesu", "-d", "-u", "root", "-c", "echo enable > /proc/acpi/ibm/bluetooth", NULL);
 execl ("/usr/bin/gksu", "gksu",  "echo enable > /proc/acpi/ibm/bluetooth", NULL);
 execl ("/bin/su", "su", "root", "-c", "echo enable > /proc/acpi/ibm/bluetooth", NULL);
 execl ("/bin/sudo", "sudo", "echo enable > /proc/acpi/ibm/bluetooth", NULL);
 exit (1);
}

/* disable function as root */
void dsbl_bt_root (void){

 chk_bt_file();  

 if (getuid () == 0){
  return ;
 }
 execl ("/usr/bin/kdesu", "kdesu", "-d", "-u", "root", "-c", "echo disable > /proc/acpi/ibm/bluetooth", NULL);
 execl ("/usr/bin/gksu", "gksu",  "echo disable > /proc/acpi/ibm/bluetooth", NULL);
 execl ("/bin/su", "su", "root", "-c", "echo disable > /proc/acpi/ibm/bluetooth", NULL);
 execl ("/bin/sudo", "sudo", "echo disable > /proc/acpi/ibm/bluetooth", NULL);
 exit (1);
}

int main (int argc, char *argv[])
{
 GtkWidget *window;
 GtkWidget  *button;
 GtkWidget  *table;
     GtkWidget  *label;

 gtk_init (&argc, &argv);

     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

     gtk_window_set_title (GTK_WINDOW (window), "thinkpad x61* bluetooth");

     gtk_signal_connect (GTK_OBJECT (window), "delete_event", GTK_SIGNAL_FUNC (delete_event), NULL);

 gtk_container_border_width (GTK_CONTAINER (window), 5);

     table = gtk_table_new (2, 5, TRUE);
     gtk_container_add (GTK_CONTAINER (window), table);

     label = gtk_label_new ("enable/disable bluetooth");
     gtk_table_attach_defaults (GTK_TABLE (table), label, 0,2,1,2);
     gtk_widget_show (label);
     label = gtk_label_new ("zogs");
     gtk_table_attach_defaults (GTK_TABLE (table), label, 2,3,1,2);
     gtk_widget_show (label);
     label = gtk_label_new ("  www.autistici.org/c0de");
     gtk_table_attach_defaults (GTK_TABLE(table), label, 3,5,1,2);
     gtk_widget_show (label);

     /* enable button */
     button = gtk_button_new_with_label ("enable");
     gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (enbl_bt_root), NULL);
     gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 1, 0, 1);
     gtk_widget_show (button);

     /* disable button */
     button = gtk_button_new_with_label ("disable");
     gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (dsbl_bt_root), NULL);
     gtk_table_attach_defaults (GTK_TABLE(table), button, 2, 3, 0, 1);
     gtk_widget_show (button);

     /* quit button */
     button = gtk_button_new_with_label ("Quit");
     gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (delete_event), NULL);
    gtk_table_attach_defaults (GTK_TABLE(table), button, 4, 5, 0, 1);
     gtk_widget_show (button);

     gtk_widget_show (table);
     gtk_widget_show (window);
     gtk_main ();

     return 0;

}

Nessun commento:

Posta un commento