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

lshal.c

00001 /***************************************************************************
00002  * CVSID: $Id: lshal.c,v 1.7 2004/04/10 16:51: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 
00055 static LibHalContext *hal_ctx;
00056 
00060 static void
00061 dump_devices ()
00062 {
00063     int i;
00064     int num_devices;
00065     char **device_names;
00066 
00067     device_names = hal_get_all_devices (hal_ctx, &num_devices);
00068 
00069     if (device_names == NULL)
00070         DIE (("Couldn't obtain list of devices\n"));
00071 
00072     printf ("\n"
00073         "Dumping %d device(s) from the Global Device List:\n"
00074         "-------------------------------------------------\n",
00075         num_devices);
00076 
00077     for (i = 0; i < num_devices; i++) {
00078         LibHalPropertySet *props;
00079         LibHalPropertySetIterator it;
00080         int type;
00081 
00082         props = hal_device_get_all_properties (hal_ctx, 
00083                                device_names[i]);
00084 
00085         /* NOTE NOTE NOTE: This may be NULL if the device was removed
00086          *                 in the daemon; this is because 
00087          *                 hal_device_get_all_properties() is a in
00088          *                 essence an IPC call and other stuff may 
00089          *                 be happening..
00090          */
00091         if (props == NULL)
00092             continue;
00093 
00094         printf ("udi = '%s'\n", device_names[i]);
00095 
00096         for (hal_psi_init (&it, props); hal_psi_has_more (&it);
00097              hal_psi_next (&it)) {
00098             type = hal_psi_get_type (&it);
00099             switch (type) {
00100             case DBUS_TYPE_STRING:
00101                 printf ("  %s = '%s'  (string)\n",
00102                     hal_psi_get_key (&it),
00103                     hal_psi_get_string (&it));
00104                 break;
00105 
00106             case DBUS_TYPE_INT32:
00107                 printf ("  %s = %d  (0x%x)  (int)\n",
00108                     hal_psi_get_key (&it),
00109                     hal_psi_get_int (&it),
00110                     hal_psi_get_int (&it));
00111                 break;
00112 
00113             case DBUS_TYPE_DOUBLE:
00114                 printf ("  %s = %g  (double)\n",
00115                     hal_psi_get_key (&it),
00116                     hal_psi_get_double (&it));
00117                 break;
00118 
00119             case DBUS_TYPE_BOOLEAN:
00120                 printf ("  %s = %s  (bool)\n",
00121                     hal_psi_get_key (&it),
00122                     hal_psi_get_bool (&it) ? "true" :
00123                     "false");
00124                 break;
00125             }
00126         }
00127         hal_free_property_set (props);
00128         printf ("\n");
00129     }
00130 
00131     hal_free_string_array (device_names);
00132 
00133     printf ("\n"
00134         "Dumped %d device(s) from the Global Device List:\n"
00135         "------------------------------------------------\n",
00136         num_devices);
00137 
00138     printf ("\n");
00139 }
00140 
00146 static void
00147 device_added (LibHalContext *ctx,
00148           const char *udi)
00149 {
00150     fprintf (stderr, "*** lshal: device_added, udi='%s'\n", udi);
00151     dump_devices ();
00152 }
00153 
00159 static void
00160 device_removed (LibHalContext *ctx,
00161         const char *udi)
00162 {
00163     fprintf (stderr, "*** lshal: device_removed, udi='%s'\n", udi);
00164     dump_devices ();
00165 }
00166 
00173 static void
00174 device_new_capability (LibHalContext *ctx,
00175                const char *udi, 
00176                const char *capability)
00177 {
00178     fprintf (stderr, "*** lshal: new_capability, udi='%s'\n", udi);
00179     fprintf (stderr, "*** capability: %s\n", capability);
00180     /*dump_devices(); */
00181 }
00182 
00189 static void
00190 device_lost_capability (LibHalContext *ctx,
00191             const char *udi, 
00192             const char *capability)
00193 {
00194     fprintf (stderr, "*** lshal: lost_capability, udi='%s'\n", udi);
00195     fprintf (stderr, "*** capability: %s\n", capability);
00196     /*dump_devices(); */
00197 }
00198 
00199 
00205 static void
00206 print_property (const char *udi, const char *key)
00207 {
00208     int type;
00209     char *str;
00210 
00211     type = hal_device_get_property_type (hal_ctx, udi, key);
00212 
00213     switch (type) {
00214     case DBUS_TYPE_STRING:
00215         str = hal_device_get_property_string (hal_ctx, udi, key);
00216         fprintf (stderr, "*** new value: '%s'  (string)\n", str);
00217         hal_free_string (str);
00218         break;
00219     case DBUS_TYPE_INT32:
00220         {
00221             dbus_int32_t value =
00222                 hal_device_get_property_int (hal_ctx, udi, key);
00223             fprintf (stderr,
00224                  "*** new value: %d (0x%x)  (int)\n",
00225                  value, value);
00226         }
00227         break;
00228     case DBUS_TYPE_DOUBLE:
00229         fprintf (stderr, "*** new value: %g  (double)\n",
00230              hal_device_get_property_double (hal_ctx, udi, key));
00231         break;
00232     case DBUS_TYPE_BOOLEAN:
00233         fprintf (stderr, "*** new value: %s  (bool)\n",
00234              hal_device_get_property_bool (hal_ctx, udi,
00235                                key) ? "true" :
00236              "false");
00237         break;
00238 
00239     default:
00240         fprintf (stderr, "Unknown type %d='%c'\n", type, type);
00241         break;
00242     }
00243 }
00244 
00251 static void
00252 property_modified (LibHalContext *ctx,
00253            const char *udi, 
00254            const char *key,
00255            dbus_bool_t is_removed, 
00256            dbus_bool_t is_added)
00257 {
00258     fprintf (stderr, "*** lshal: property_modified, udi=%s, key=%s\n",
00259          udi, key);
00260     fprintf (stderr, "           is_removed=%s, is_added=%s\n",
00261          is_removed ? "true" : "false",
00262          is_added ? "true" : "false");
00263     if (!is_removed)
00264         print_property (udi, key);
00265     fprintf (stderr, "\n");
00266     /*dump_devices(); */
00267 }
00268 
00269 
00277 static void
00278 device_condition (LibHalContext *ctx,
00279           const char *udi, 
00280           const char *condition_name,
00281           DBusMessage * message)
00282 {
00283     fprintf (stderr, "*** lshal: device_condition, udi=%s\n", udi);
00284     fprintf (stderr, "           condition_name=%s\n", condition_name);
00286     fprintf (stderr, "\n");
00287     /*dump_devices(); */
00288 }
00289 
00290 
00296 static void
00297 mainloop_integration (LibHalContext *ctx, DBusConnection * dbus_connection)
00298 {
00299     dbus_connection_setup_with_g_main (dbus_connection, NULL);
00300 }
00301 
00302 
00308 static void
00309 usage (int argc, char *argv[])
00310 {
00311     fprintf (stderr, "\n" "usage : %s --monitor [--help]\n", argv[0]);
00312     fprintf (stderr,
00313          "\n"
00314          "        --monitor        Monitor device list\n"
00315          "        --help           Show this information and exit\n"
00316          "\n"
00317          "Shows all devices and their properties. If the --monitor option is given\n"
00318          "then the device list and all devices are monitored for changes.\n"
00319          "\n");
00320 }
00321 
00328 int
00329 main (int argc, char *argv[])
00330 {
00331     dbus_bool_t do_monitor = FALSE;
00332     GMainLoop *loop;
00333     LibHalFunctions hal_functions = { mainloop_integration,
00334         device_added,
00335         device_removed,
00336         device_new_capability,
00337         device_lost_capability,
00338         property_modified,
00339         device_condition
00340     };
00341 
00342     fprintf (stderr, "lshal version " PACKAGE_VERSION "\n");
00343 
00344     loop = g_main_loop_new (NULL, FALSE);
00345 
00346     while (1) {
00347         int c;
00348         int option_index = 0;
00349         const char *opt;
00350         static struct option long_options[] = {
00351             {"monitor", 0, NULL, 0},
00352             {"help", 0, NULL, 0},
00353             {NULL, 0, NULL, 0}
00354         };
00355 
00356         c = getopt_long (argc, argv, "",
00357                  long_options, &option_index);
00358         if (c == -1)
00359             break;
00360 
00361         switch (c) {
00362         case 0:
00363             opt = long_options[option_index].name;
00364 
00365             if (strcmp (opt, "help") == 0) {
00366                 usage (argc, argv);
00367                 return 0;
00368             } else if (strcmp (opt, "monitor") == 0) {
00369                 do_monitor = TRUE;
00370             }
00371             break;
00372 
00373         default:
00374             usage (argc, argv);
00375             return 1;
00376             break;
00377         }
00378     }
00379 
00380 
00381     if ((hal_ctx = hal_initialize (&hal_functions, FALSE)) == NULL) {
00382         fprintf (stderr, "error: hal_initialize failed\n");
00383         exit (1);
00384     }
00385 
00386     dump_devices ();
00387 
00388     /* run the main loop only if we should monitor */
00389     if (do_monitor) {
00390         hal_device_property_watch_all (hal_ctx);
00391         g_main_loop_run (loop);
00392     }
00393 
00394     hal_shutdown (hal_ctx);
00395     return 0;
00396 }
00397 
00398 

Generated on Sat Apr 24 19:57:45 2004 for HAL by doxygen 1.3.6-20040222