shmem.cpp

00001
00002 /***************************************************************************
00003  *  shmem.cpp - Shared memory management tool
00004  *
00005  *  Generated: Mon Jan 16 22:51:34 2006
00006  *  Copyright  2005-2006  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
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 Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022
00023 #include <fvutils/ipc/shm_image.h>
00024 #include <fvutils/ipc/shm_lut.h>
00025 #include <utils/system/argparser.h>
00026 #include <fvutils/writers/fvraw.h>
00027
00028 #include <iostream>
00029 #include <cstring>
00030 #include <cstdio>
00031
00032 using namespace std;
00033 using namespace fawkes;
00034
00035 int
00036 main(int argc, char **argv)
00037 {
00038
00039   ArgumentParser *argp = new ArgumentParser(argc, argv, "c::hl::i:");
00040   bool action_done = false;
00041
00042   if ( argp->has_arg("h") ) {
00043     // Show usage note
00044     cout << endl << "Usage: " << argv[0] << " [-h] [-c] [-c[t]] [-l] [-m] [-i image_id] [file]" << endl
00045          << " -h     Show this help message" << endl
00046          << " -i id  Save image ID to file" << endl
00047          << " -c[t]  Cleanup (remove all FireVision related shmem segments of given type)"
00048          << endl
00049          << " -l[t]  List shared memory segments of given type" << endl
00050          << endl
00051          << "        [t] type is a combination of" << endl
00052          << "          i  images" << endl
00053          << "          l  lookup tables" << endl
00054          << "        or empty in which case all known shared memory segments are mangled" << endl
00055          << endl
00056          << "        [file] is a file name. Content depends on the action. The possibilities are: " << endl
00057          << "        for  -i   File where the saved image is stored" << endl
00058          << endl
00059          << "By default all known shared memory segments are listed" << endl
00060          << endl;
00061     action_done = true;
00062   } else {
00063     if ( argp->has_arg("i") ) {
00064       if ( argp->num_items() == 0 ) {
00065         printf("You have to give a file name where to store the image\n");
00066       } else {
00067         const char *image_id = argp->arg("i");
00068
00069         try {
00070           SharedMemoryImageBuffer *b = new SharedMemoryImageBuffer(image_id);
00071
00072           FvRawWriter *w = new FvRawWriter(argp->items()[0], b->width(), b->height(),
00073                                          b->colorspace(), b->buffer());
00074           w->write();
00075           delete w;
00076           delete b;
00077           printf("Image '%s' saved to %s\n", image_id, argp->items()[0]);
00078         } catch (Exception &e) {
00079           printf("Failed top save image\n");
00080           e.print_trace();
00081         }
00082       }
00083     }
00084     if ( argp->has_arg("c") ) {
00085       const char *tmp;
00086       if ( (tmp = argp->arg("c")) != NULL) {
00087         if ( strchr(tmp, 'i') != NULL) {
00088           SharedMemoryImageBuffer::cleanup();
00089         }
00090         if ( strchr(tmp, 'l') != NULL) {
00091           SharedMemoryLookupTable::cleanup();
00092         }
00093       } else {
00094         SharedMemoryImageBuffer::cleanup();
00095         SharedMemoryLookupTable::cleanup();
00096       }
00097
00098       action_done = true;
00099     }
00100     if ( argp->has_arg("l") ) {
00101       const char *tmp;
00102       if ( (tmp = argp->arg("l")) != NULL) {
00103         if ( strchr(tmp, 'i') != NULL) {
00104           SharedMemoryImageBuffer::list();
00105         }
00106         if ( strchr(tmp, 'l') != NULL) {
00107           SharedMemoryLookupTable::list();
00108         }
00109       } else {
00110         SharedMemoryImageBuffer::list();
00111         SharedMemoryLookupTable::list();
00112       }
00113
00114       action_done = true;
00115     }
00116   }
00117
00118   if (! action_done) {
00119     SharedMemoryImageBuffer::list();
00120     cout << endl;
00121     SharedMemoryLookupTable::list();
00122   }
00123
00124   cout << endl;
00125 }