Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

lshal.c

00001 /***************************************************************************
00002  * CVSID: $Id: lshal.c,v 1.5 2004/01/13 02:01:04 david Exp $
00003  *
00004  * lshal.c : Show devices managed by HAL
00005  *
00006  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
00007  *
00008  * Licensed under the Academic Free License version 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  **************************************************************************/
00025 
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035 #include <getopt.h>
00036 
00037 #include <glib.h>
00038 #include <dbus/dbus-glib.h>
00039 
00040 #include <libhal/libhal.h>
00041 
00053 #define DIE(expr) do {printf("*** [DIE] %s:%s():%d : ", __FILE__, __FUNCTION__, __LINE__); printf expr; printf("\n"); exit(1); } while(0)
00054 
00058 static void dump_devices()
00059 {
00060     int i;
00061     int num_devices;
00062     char** device_names;
00063 
00064     device_names = hal_get_all_devices(&num_devices);
00065 
00066     if( device_names==NULL )
00067         DIE(("Couldn't obtain list of devices\n"));
00068 
00069     printf("\n"
00070            "Dumping %d device(s) from the Global Device List:\n"
00071            "-------------------------------------------------\n", num_devices);
00072 
00073     for(i=0; i<num_devices; i++)
00074     {
00075         LibHalPropertySet* props;
00076         LibHalPropertySetIterator it;
00077         int type;
00078 
00079         props = hal_device_get_all_properties(device_names[i]);
00080 
00081         /* NOTE NOTE NOTE: This may be NULL if the device was removed
00082          *                 in the daemon; this is because hal_device_get_all_
00083          *                 properties() is a in essence an IPC call and
00084          *                 other stuff may be happening..
00085          */
00086         if( props==NULL )
00087             continue;
00088 
00089         printf("udi = '%s'\n", device_names[i]);
00090         
00091         for(hal_psi_init(&it, props); hal_psi_has_more(&it); hal_psi_next(&it))
00092         {
00093             type = hal_psi_get_type(&it);
00094             switch( type )
00095             {
00096             case DBUS_TYPE_STRING:
00097                 printf("  %s = '%s'  (string)\n", 
00098                        hal_psi_get_key(&it), hal_psi_get_string(&it));
00099                 break;
00100 
00101             case DBUS_TYPE_INT32:
00102                 printf("  %s = %d  (0x%x)  (int)\n", 
00103                        hal_psi_get_key(&it), 
00104                        hal_psi_get_int(&it), hal_psi_get_int(&it));
00105                 break;
00106 
00107             case DBUS_TYPE_DOUBLE:
00108                 printf("  %s = %g  (double)\n", 
00109                        hal_psi_get_key(&it), hal_psi_get_double(&it));
00110                 break;
00111 
00112             case DBUS_TYPE_BOOLEAN:
00113                 printf("  %s = %s  (bool)\n", 
00114                        hal_psi_get_key(&it), 
00115                        hal_psi_get_bool(&it) ? "true" : "false");
00116                 break;
00117             }
00118         }
00119         hal_free_property_set(props);
00120         printf("\n");
00121     }
00122 
00123     hal_free_string_array(device_names);
00124 
00125     printf("\n"
00126            "Dumped %d device(s) from the Global Device List:\n"
00127            "------------------------------------------------\n", num_devices);
00128 
00129     printf("\n");
00130 }
00131 
00137 static void device_added(const char* udi)
00138 {
00139     fprintf(stderr, "*** lshal: device_added, udi='%s'\n", udi);
00140     dump_devices();
00141 }
00142 
00148 static void device_removed(const char* udi)
00149 {
00150     fprintf(stderr, "*** lshal: device_removed, udi='%s'\n", udi);
00151     dump_devices();
00152 }
00153 
00160 static void device_new_capability(const char* udi, const char* capability)
00161 {
00162     fprintf(stderr, "*** lshal: new_capability, udi='%s'\n", udi);
00163     fprintf(stderr, "*** capability: %s\n", capability);
00164     /*dump_devices();*/
00165 }
00166 
00167 
00173 static void print_property(const char* udi, const char* key)
00174 {
00175     int type;
00176     char* str;
00177 
00178     type = hal_device_get_property_type(udi, key);
00179 
00180     switch( type )
00181     {
00182     case DBUS_TYPE_STRING:
00183         str = hal_device_get_property_string(udi, key);
00184         fprintf(stderr, "*** new value: '%s'  (string)\n", str);
00185         hal_free_string(str);
00186         break;
00187     case DBUS_TYPE_INT32:
00188     {
00189         dbus_int32_t value = hal_device_get_property_int(udi, key);
00190         fprintf(stderr, "*** new value: %d (0x%x)  (int)\n", value, value);
00191     }
00192     break;
00193     case DBUS_TYPE_DOUBLE:
00194         fprintf(stderr, "*** new value: %g  (double)\n", 
00195                 hal_device_get_property_double(udi, key));
00196         break;
00197     case DBUS_TYPE_BOOLEAN:
00198         fprintf(stderr, "*** new value: %s  (bool)\n", 
00199                 hal_device_get_property_bool(udi, key) ? "true" : "false");
00200         break;
00201         
00202     default:
00203         fprintf(stderr, "Unknown type %d='%c'\n", type, type);
00204         break;
00205     }
00206 }
00207 
00214 static void property_modified(const char* udi, const char* key,
00215                               dbus_bool_t is_removed, dbus_bool_t is_added)
00216 {
00217     fprintf(stderr, "*** lshal: property_modified, udi=%s, key=%s\n", 
00218             udi, key);
00219     fprintf(stderr, "           is_removed=%s, is_added=%s\n", 
00220             is_removed ? "true" : "false", 
00221             is_added ? "true" : "false" );
00222     if( !is_removed )
00223         print_property(udi, key);
00224     fprintf(stderr, "\n");
00225     /*dump_devices();*/
00226 }
00227 
00228 
00236 static void device_condition(const char* udi, const char* condition_name,
00237                              DBusMessage* message)
00238 {
00239     fprintf(stderr, "*** lshal: device_condition, udi=%s\n", udi);
00240     fprintf(stderr, "           condition_name=%s\n", condition_name);
00242     fprintf(stderr, "\n");
00243     /*dump_devices();*/
00244 }
00245 
00246 
00252 static void mainloop_integration(DBusConnection* dbus_connection)
00253 {
00254     dbus_connection_setup_with_g_main(dbus_connection, NULL);
00255 }
00256 
00257 
00263 static void usage(int argc, char* argv[])
00264 {
00265     fprintf(stderr, 
00266 "\n"
00267 "usage : %s --monitor [--help]\n", argv[0]);
00268     fprintf(stderr, 
00269 "\n"
00270 "        --monitor        Monitor device list\n"
00271 "        --help           Show this information and exit\n"
00272 "\n"
00273 "Shows all devices and their properties. If the --monitor option is given\n"
00274 "then the device list and all devices are monitored for changes.\n"
00275 "\n");
00276 }
00277 
00284 int main(int argc, char* argv[])
00285 {
00286     dbus_bool_t do_monitor = FALSE;
00287     GMainLoop* loop;
00288     LibHalFunctions hal_functions = { mainloop_integration,
00289                                       device_added, 
00290                                       device_removed, 
00291                                       device_new_capability,
00292                                       property_modified,
00293                                       device_condition };
00294 
00295     fprintf(stderr, "lshal version " PACKAGE_VERSION "\n");
00296 
00297     loop = g_main_loop_new (NULL, FALSE);
00298 
00299     while(1)
00300     {
00301         int c;
00302         int option_index = 0;
00303         const char* opt;
00304         static struct option long_options[] = 
00305         {
00306             {"monitor", 0, NULL, 0},
00307             {"help", 0, NULL, 0},
00308             {NULL, 0, NULL, 0}
00309         };
00310 
00311         c = getopt_long(argc, argv, "",
00312                         long_options, &option_index);
00313         if (c == -1)
00314             break;
00315         
00316         switch(c)
00317         {
00318         case 0:
00319             opt = long_options[option_index].name;
00320 
00321             if( strcmp(opt, "help")==0 )
00322             {
00323                 usage(argc, argv);
00324                 return 0;
00325             }
00326             else if( strcmp(opt, "monitor")==0 )
00327             {
00328                 do_monitor = TRUE;
00329             }
00330             break;        
00331 
00332         default:
00333             usage(argc, argv);
00334             return 1;
00335             break;
00336         }         
00337     }
00338 
00339 
00340     if( hal_initialize(&hal_functions, FALSE)  )
00341     {
00342         fprintf(stderr, "error: hal_initialize failed\n");
00343         exit(1);
00344     }
00345 
00346     dump_devices();    
00347 
00348     /* run the main loop only if we should monitor */
00349     if( do_monitor )
00350     {
00351         hal_device_property_watch_all();
00352         g_main_loop_run(loop);
00353     }
00354 
00355     hal_shutdown();
00356     return 0;
00357 }
00358 
00359 

Generated on Sat Feb 7 22:11:48 2004 for HAL by doxygen 1.3.5