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

lshal.c

00001 /***************************************************************************
00002  * CVSID: $Id: lshal.c,v 1.6 2004/03/03 17:56:56 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
00059 dump_devices ()
00060 {
00061     int i;
00062     int num_devices;
00063     char **device_names;
00064 
00065     device_names = hal_get_all_devices (&num_devices);
00066 
00067     if (device_names == NULL)
00068         DIE (("Couldn't obtain list of devices\n"));
00069 
00070     printf ("\n"
00071         "Dumping %d device(s) from the Global Device List:\n"
00072         "-------------------------------------------------\n",
00073         num_devices);
00074 
00075     for (i = 0; i < num_devices; i++) {
00076         LibHalPropertySet *props;
00077         LibHalPropertySetIterator it;
00078         int type;
00079 
00080         props = hal_device_get_all_properties (device_names[i]);
00081 
00082         /* NOTE NOTE NOTE: This may be NULL if the device was removed
00083          *                 in the daemon; this is because 
00084          *                 hal_device_get_all_properties() is a in
00085          *                 essence an IPC call and other stuff may 
00086          *                 be happening..
00087          */
00088         if (props == NULL)
00089             continue;
00090 
00091         printf ("udi = '%s'\n", device_names[i]);
00092 
00093         for (hal_psi_init (&it, props); hal_psi_has_more (&it);
00094              hal_psi_next (&it)) {
00095             type = hal_psi_get_type (&it);
00096             switch (type) {
00097             case DBUS_TYPE_STRING:
00098                 printf ("  %s = '%s'  (string)\n",
00099                     hal_psi_get_key (&it),
00100                     hal_psi_get_string (&it));
00101                 break;
00102 
00103             case DBUS_TYPE_INT32:
00104                 printf ("  %s = %d  (0x%x)  (int)\n",
00105                     hal_psi_get_key (&it),
00106                     hal_psi_get_int (&it),
00107                     hal_psi_get_int (&it));
00108                 break;
00109 
00110             case DBUS_TYPE_DOUBLE:
00111                 printf ("  %s = %g  (double)\n",
00112                     hal_psi_get_key (&it),
00113                     hal_psi_get_double (&it));
00114                 break;
00115 
00116             case DBUS_TYPE_BOOLEAN:
00117                 printf ("  %s = %s  (bool)\n",
00118                     hal_psi_get_key (&it),
00119                     hal_psi_get_bool (&it) ? "true" :
00120                     "false");
00121                 break;
00122             }
00123         }
00124         hal_free_property_set (props);
00125         printf ("\n");
00126     }
00127 
00128     hal_free_string_array (device_names);
00129 
00130     printf ("\n"
00131         "Dumped %d device(s) from the Global Device List:\n"
00132         "------------------------------------------------\n",
00133         num_devices);
00134 
00135     printf ("\n");
00136 }
00137 
00143 static void
00144 device_added (const char *udi)
00145 {
00146     fprintf (stderr, "*** lshal: device_added, udi='%s'\n", udi);
00147     dump_devices ();
00148 }
00149 
00155 static void
00156 device_removed (const char *udi)
00157 {
00158     fprintf (stderr, "*** lshal: device_removed, udi='%s'\n", udi);
00159     dump_devices ();
00160 }
00161 
00168 static void
00169 device_new_capability (const char *udi, const char *capability)
00170 {
00171     fprintf (stderr, "*** lshal: new_capability, udi='%s'\n", udi);
00172     fprintf (stderr, "*** capability: %s\n", capability);
00173     /*dump_devices(); */
00174 }
00175 
00176 
00182 static void
00183 print_property (const char *udi, const char *key)
00184 {
00185     int type;
00186     char *str;
00187 
00188     type = hal_device_get_property_type (udi, key);
00189 
00190     switch (type) {
00191     case DBUS_TYPE_STRING:
00192         str = hal_device_get_property_string (udi, key);
00193         fprintf (stderr, "*** new value: '%s'  (string)\n", str);
00194         hal_free_string (str);
00195         break;
00196     case DBUS_TYPE_INT32:
00197         {
00198             dbus_int32_t value =
00199                 hal_device_get_property_int (udi, key);
00200             fprintf (stderr,
00201                  "*** new value: %d (0x%x)  (int)\n",
00202                  value, value);
00203         }
00204         break;
00205     case DBUS_TYPE_DOUBLE:
00206         fprintf (stderr, "*** new value: %g  (double)\n",
00207              hal_device_get_property_double (udi, key));
00208         break;
00209     case DBUS_TYPE_BOOLEAN:
00210         fprintf (stderr, "*** new value: %s  (bool)\n",
00211              hal_device_get_property_bool (udi,
00212                                key) ? "true" :
00213              "false");
00214         break;
00215 
00216     default:
00217         fprintf (stderr, "Unknown type %d='%c'\n", type, type);
00218         break;
00219     }
00220 }
00221 
00228 static void
00229 property_modified (const char *udi, const char *key,
00230            dbus_bool_t is_removed, dbus_bool_t is_added)
00231 {
00232     fprintf (stderr, "*** lshal: property_modified, udi=%s, key=%s\n",
00233          udi, key);
00234     fprintf (stderr, "           is_removed=%s, is_added=%s\n",
00235          is_removed ? "true" : "false",
00236          is_added ? "true" : "false");
00237     if (!is_removed)
00238         print_property (udi, key);
00239     fprintf (stderr, "\n");
00240     /*dump_devices(); */
00241 }
00242 
00243 
00251 static void
00252 device_condition (const char *udi, const char *condition_name,
00253           DBusMessage * message)
00254 {
00255     fprintf (stderr, "*** lshal: device_condition, udi=%s\n", udi);
00256     fprintf (stderr, "           condition_name=%s\n", condition_name);
00258     fprintf (stderr, "\n");
00259     /*dump_devices(); */
00260 }
00261 
00262 
00268 static void
00269 mainloop_integration (DBusConnection * dbus_connection)
00270 {
00271     dbus_connection_setup_with_g_main (dbus_connection, NULL);
00272 }
00273 
00274 
00280 static void
00281 usage (int argc, char *argv[])
00282 {
00283     fprintf (stderr, "\n" "usage : %s --monitor [--help]\n", argv[0]);
00284     fprintf (stderr,
00285          "\n"
00286          "        --monitor        Monitor device list\n"
00287          "        --help           Show this information and exit\n"
00288          "\n"
00289          "Shows all devices and their properties. If the --monitor option is given\n"
00290          "then the device list and all devices are monitored for changes.\n"
00291          "\n");
00292 }
00293 
00300 int
00301 main (int argc, char *argv[])
00302 {
00303     dbus_bool_t do_monitor = FALSE;
00304     GMainLoop *loop;
00305     LibHalFunctions hal_functions = { mainloop_integration,
00306         device_added,
00307         device_removed,
00308         device_new_capability,
00309         property_modified,
00310         device_condition
00311     };
00312 
00313     fprintf (stderr, "lshal version " PACKAGE_VERSION "\n");
00314 
00315     loop = g_main_loop_new (NULL, FALSE);
00316 
00317     while (1) {
00318         int c;
00319         int option_index = 0;
00320         const char *opt;
00321         static struct option long_options[] = {
00322             {"monitor", 0, NULL, 0},
00323             {"help", 0, NULL, 0},
00324             {NULL, 0, NULL, 0}
00325         };
00326 
00327         c = getopt_long (argc, argv, "",
00328                  long_options, &option_index);
00329         if (c == -1)
00330             break;
00331 
00332         switch (c) {
00333         case 0:
00334             opt = long_options[option_index].name;
00335 
00336             if (strcmp (opt, "help") == 0) {
00337                 usage (argc, argv);
00338                 return 0;
00339             } else if (strcmp (opt, "monitor") == 0) {
00340                 do_monitor = TRUE;
00341             }
00342             break;
00343 
00344         default:
00345             usage (argc, argv);
00346             return 1;
00347             break;
00348         }
00349     }
00350 
00351 
00352     if (hal_initialize (&hal_functions, FALSE)) {
00353         fprintf (stderr, "error: hal_initialize failed\n");
00354         exit (1);
00355     }
00356 
00357     dump_devices ();
00358 
00359     /* run the main loop only if we should monitor */
00360     if (do_monitor) {
00361         hal_device_property_watch_all ();
00362         g_main_loop_run (loop);
00363     }
00364 
00365     hal_shutdown ();
00366     return 0;
00367 }
00368 
00369 

Generated on Thu Mar 11 21:32:23 2004 for HAL by doxygen 1.3.6-20040222