From 0bcbc969c7dfa362cfffad3f92d375329fee289f Mon Sep 17 00:00:00 2001 From: Wolf Bergenheim Date: Mon, 23 Aug 2010 12:15:15 +0300 Subject: * Moved CommandLineParser to its own files * Removed broken timer, replaced it with a simpler timer made in C * Sanitized the use of resources (Use Qt types instead of custom bitmap) * Added a MessageHandler so we can later play with verbosity level --- resourceqt-client/client.cpp | 365 ++++++++++---------------------- resourceqt-client/client.h | 90 +------- resourceqt-client/commandlineparser.cpp | 223 +++++++++++++++++++ resourceqt-client/commandlineparser.h | 47 ++++ resourceqt-client/resourceqt-client.cpp | 224 +++----------------- resourceqt-client/resourceqt-client.pro | 4 +- resourceqt-client/time-stat.c | 33 +++ resourceqt-client/time-stat.h | 16 ++ 8 files changed, 466 insertions(+), 536 deletions(-) create mode 100644 resourceqt-client/commandlineparser.cpp create mode 100644 resourceqt-client/commandlineparser.h create mode 100644 resourceqt-client/time-stat.c create mode 100644 resourceqt-client/time-stat.h diff --git a/resourceqt-client/client.cpp b/resourceqt-client/client.cpp index 5f1875b..f1e17d2 100644 --- a/resourceqt-client/client.cpp +++ b/resourceqt-client/client.cpp @@ -9,12 +9,9 @@ using namespace ResourcePolicy; -Client::Client(QString appClass) : QObject() +Client::Client() + : QObject(), standardInput(stdin, QFile::ReadOnly), applicationClass(), resourceSet(NULL) { - applicationClass = appClass; - resourceSet = NULL; - - standardInput = new QTextStream(stdin, QFile::ReadOnly); mainTimerID = startTimer(0); } @@ -22,68 +19,7 @@ Client::~Client() { killTimer(mainTimerID); - if (resourceSet != NULL) { - delete resourceSet; - resourceSet = NULL; - } - - if (standardInput != NULL) { - delete standardInput; - standardInput = NULL; - } -} - -uint32_t Client::parseResourceList(QString resourceListStr) -{ - struct { - uint32_t resourceType; - const char* resourceName; - } - resourceDef[] = { - { RES_AUDIO_PLAYBACK , "AudioPlayback" }, - { RES_VIDEO_PLAYBACK , "VideoPlayback" }, - { RES_AUDIO_RECORDING, "AudioRecording" }, - { RES_VIDEO_RECORDING, "VideoRecording" }, - { RES_VIBRA , "Vibra" }, - { RES_LEDS , "Leds" }, - { RES_BACKLIGHT , "BackLight" }, - { RES_SYSTEM_BUTTON , "SystemButton" }, - { RES_LOCK_BUTTON , "LockButton" }, - { RES_SCALE_BUTTON , "ScaleButton" }, - { RES_SNAP_BUTTON , "SnapButton" }, - { RES_LENS_COVER , "LensCover" }, - { RES_HEADSET_BUTTONS, "HeadsetButtons" }, - { 0 , NULL } - }; - - uint32_t resourceList = 0; - - if (resourceListStr.isEmpty() || resourceListStr.isNull()) { - return 0; - } - else { - QStringList resList = resourceListStr.split(",", QString::SkipEmptyParts); - - for (int i = 0; i < resList.count(); i++) { - int pos = 0; - while (resourceDef[pos].resourceName != NULL) { - if (resList[i] == resourceDef[pos].resourceName) - break; - - pos++; - } - - if (!resourceDef[pos].resourceName) { - const char* res = qPrintable(resList[i]); - printf("Ignoring invalid resource name '%s'\n", res); - } - else { - resourceList |= resourceDef[pos].resourceType; - } - } - } - - return resourceList; + delete resourceSet; } void Client::showPrompt() @@ -92,73 +28,33 @@ void Client::showPrompt() fflush(stdout); } -void Client::updateSet(uint32_t list, uint32_t optional, bool remove) +bool Client::initialize(const CommandLineParser &parser) { - uint32_t resources[] = { - RES_AUDIO_PLAYBACK, RES_VIDEO_PLAYBACK, RES_AUDIO_RECORDING, RES_VIDEO_RECORDING, - RES_VIBRA, RES_LEDS, RES_BACKLIGHT, RES_SYSTEM_BUTTON, RES_LOCK_BUTTON, - RES_SCALE_BUTTON, RES_SNAP_BUTTON, RES_LENS_COVER, RES_HEADSET_BUTTONS, 0 - }; - - int pos = 0; - while (resources[pos]) { - if ((list & resources[pos]) == resources[pos]) { - Resource* resource = NULL; - ResourceType res = getResourceType(resources[pos]); - bool opt = (optional & resources[pos]) == resources[pos]; - - if (remove) { - if (!resourceSet->contains(res)) { - continue; - } - - if (optional) { - resource = resourceSet->resource(res); - resource->setOptional(false); - } - else { - resourceSet->deleteResource(res); - } - } - else { - if (resourceSet->contains(res)) { - resource = resourceSet->resource(res); - if (resource->isOptional() != opt) { - resource->setOptional(opt); - } - - continue; - } - - resource = allocateResource(res, opt); - if (resource) { - resourceSet->addResourceObject(resource); - } - } - } - - pos++; - } -} - -bool Client::initialize(uint32_t all, uint32_t optional, bool alwaysReply, bool autoRelease) -{ - resourceSet = new ResourceSet(applicationClass); + QSet allResources; + QSet optionalResources; + resourceSet = new ResourceSet(parser.resourceApplicationClass()); if (resourceSet == NULL) { return false; } - if (alwaysReply) { - qDebug("client: alwaysReply"); + if (parser.shouldAlwaysReply()) { + qDebug("client: AlwaysReply"); resourceSet->setAlwaysReply(); } - if (autoRelease) { - qDebug("client: autoRelease"); + if (parser.shouldAutoRelease()) { + qDebug("client: AutoRelease"); resourceSet->setAutoRelease(); } - updateSet(all, optional, false); + allResources.unite(parser.resources()); + optionalResources.unite(parser.optionalResources()); + foreach (ResourcePolicy::ResourceType resource, allResources) { + resourceSet->addResource(resource); + if (optionalResources.contains(resource)) { + resourceSet->resource(resource)->setOptional(); + } + } if (!connect(resourceSet, SIGNAL(resourcesGranted(QList)), this, SLOT(resourceAcquiredHandler(QList)))) @@ -173,141 +69,75 @@ bool Client::initialize(uint32_t all, uint32_t optional, bool alwaysReply, bool if (!connect(resourceSet, SIGNAL(lostResources()), this, SLOT(resourceLostHandler()))) { return false; } - connect(resourceSet, SIGNAL(resourcesReleased()), this, SLOT(resourceReleasedHandler())); - - connect(resourceSet, SIGNAL(resourcesBecameAvailable(const QList &)), - this, SLOT(resourcesBecameAvailableHandler(const QList &))); + if (!connect(resourceSet, SIGNAL(resourcesReleased()), this, SLOT(resourceReleasedHandler()))) { + return false; + } + if (!connect(resourceSet, SIGNAL(resourcesBecameAvailable(const QList &)), + this, SLOT(resourcesBecameAvailableHandler(const QList &)))) + { + return false; + } showPrompt(); return true; } -Resource* Client::allocateResource(ResourceType resource, bool optional) +const char * resourceTypeToString(ResourceType type) { - Resource* retValue = NULL; - - switch (resource) { - case AudioPlaybackType: - retValue = new AudioResource(); - break; - case VideoPlaybackType: - retValue = new VideoResource(); - break; - case AudioRecorderType: - retValue = new AudioRecorderResource(); - break; - case VideoRecorderType: - retValue = new VideoRecorderResource(); - break; - case VibraType: - retValue = new VibraResource(); - break; - case LedsType: - retValue = new LedsResource(); - break; - case BacklightType: - retValue = new BacklightResource(); - break; - case SystemButtonType: - retValue = new SystemButtonResource(); - break; - case LockButtonType: - retValue = new LockButtonResource(); - break; - case ScaleButtonType: - retValue = new ScaleButtonResource(); - break; - case SnapButtonType: - retValue = new SnapButtonResource(); - break; - case LensCoverType: - retValue = new LensCoverResource(); - break; - case HeadsetButtonsType: - retValue = new HeadsetButtonsResource(); - break; - case NumberOfTypes: - return NULL; - } - - if (retValue) { - retValue->setOptional(optional); + switch (type) { + case AudioPlaybackType: + return "AudioPlayback"; + case AudioRecorderType: + return "AudioRecording"; + case VideoPlaybackType: + return "VideoPlayback"; + case VideoRecorderType: + return "VideoRegording"; + case VibraType: + return "Vibra"; + case LedsType: + return "Leds"; + case BacklightType: + return "Backlight"; + case SystemButtonType: + return "SystemButton"; + case LockButtonType: + return "LockButton"; + case ScaleButtonType: + return "ScaleButton"; + case SnapButtonType: + return "SnapButton"; + case LensCoverType: + return "LensCover"; + case HeadsetButtonsType: + return "HeadsetButtons"; + default: + return "Unknown/Invalid Resource"; } - else { - printf("Unknown resource type - %d\n", resource); - } - - return retValue; -} - -ResourceType Client::getResourceType(uint32_t resource) -{ - switch (resource) { - case RES_AUDIO_PLAYBACK: - return AudioPlaybackType; - case RES_VIDEO_PLAYBACK: - return VideoPlaybackType; - case RES_AUDIO_RECORDING: - return AudioRecorderType; - case RES_VIDEO_RECORDING: - return VideoRecorderType; - case RES_VIBRA: - return VibraType; - case RES_LEDS: - return LedsType; - case RES_BACKLIGHT: - return BacklightType; - case RES_SYSTEM_BUTTON: - return SystemButtonType; - case RES_LOCK_BUTTON: - return LockButtonType; - case RES_SCALE_BUTTON: - return ScaleButtonType; - case RES_SNAP_BUTTON: - return SnapButtonType; - case RES_LENS_COVER: - return LensCoverType; - case RES_HEADSET_BUTTONS: - return HeadsetButtonsType; - } - - return NumberOfTypes; } void Client::showResources(const QList resList) { - const char* resTypes[] = { - "AudioPlayback", "VideoPlayback", "AudioRecorder", "VideoRecorder", "Vibra", - "Leds", "Backlight", "SystemButton", "LockButton", "ScaleButton", "SnapButton", - "LensCover", "HeadsetButtons" - }; - - for (int i = 0; i < resList.count(); i++) { - printf("\t%s\n", resTypes[resList[i]]); + foreach (ResourceType resource, resList) { + printf("\t%s\n", resourceTypeToString(resource)); } } void Client::showResources(const QList resList) { - const char* resTypes[] = { - "AudioPlayback", "VideoPlayback", "AudioRecorder", "VideoRecorder", "Vibra", - "Leds", "Backlight", "SystemButton", "LockButton", "ScaleButton", "SnapButton", - "LensCover", "HeadsetButtons" - }; - - for (int i = 0; i < resList.count(); i++) { - Resource* r = resList[i]; - printf("\t%s%s%s\n", resTypes[r->type()], - r->isOptional() ? " (optional)" : "", - r->isGranted() ? " (granted)" : ""); + foreach (Resource* resource, resList) { + printf("\t%s%s%s\n", resourceTypeToString(resource->type()), + resource->isOptional() ? " (optional)" : "", + resource->isGranted() ? " (granted)" : ""); } } void Client::resourceAcquiredHandler(const QList& /*grantedResList*/) { - if( timeStat.markEnd() ) { - timeStat.report("\nOperation took %.2f ms\n"); + double ms = stop_timer(); + if( ms > 0.0 ) { + printf("Operation took %.2f ms\n", ms); } QList list = resourceSet->resources(); @@ -324,8 +154,9 @@ void Client::resourceAcquiredHandler(const QList& /*grantedResList void Client::resourceDeniedHandler() { - if( timeStat.markEnd() ) { - timeStat.report("\nOperation took %.2f ms\n"); + double ms = stop_timer(); + if( ms > 0.0 ) { + printf("Operation took %.2f ms\n", ms); } printf("\nManager denies access to resources!\n"); @@ -334,8 +165,9 @@ void Client::resourceDeniedHandler() void Client::resourceLostHandler() { - if( timeStat.markEnd() ) { - timeStat.report("\nOperation took %.2f ms\n"); + double ms = stop_timer(); + if( ms > 0.0 ) { + printf("Operation took %.2f ms\n", ms); } printf("\nLost resources from manager!\n"); @@ -344,8 +176,9 @@ void Client::resourceLostHandler() void Client::resourceReleasedHandler() { - if( timeStat.markEnd() ) { - timeStat.report("\nOperation took %.2f ms\n"); + double ms = stop_timer(); + if( ms > 0.0 ) { + printf("Operation took %.2f ms\n", ms); } printf("\nAll resources released\n"); @@ -373,7 +206,7 @@ void Client::timerEvent(QTimerEvent*) int ready = select(1, &stdinfd, NULL, NULL, &tv); if (ready > 0) { - QString line = standardInput->readLine(); + QString line = standardInput.readLine(); if (!line.isNull()) { QStringList params = line.split(" "); if (params[0] == "quit") { @@ -410,20 +243,22 @@ void Client::timerEvent(QTimerEvent*) } } else if (params[0] == "acquire") { - timeStat.markStart(); + start_timer(); if (!resourceSet || !resourceSet->acquire()) { - if( timeStat.markEnd() ) { - timeStat.report("Operation took %.2f ms\n"); + double ms = stop_timer(); + if( ms > 0.0 ) { + printf("Operation took %.2f ms\n", ms); } printf("Acquire failed!\n"); } } else if (params[0] == "release") { - timeStat.markStart(); + start_timer(); if (!resourceSet || !resourceSet->release()) { - if( timeStat.markEnd() ) { - timeStat.report("Operation took %.2f ms\n"); + double ms = stop_timer(); + if( ms > 0.0 ) { + printf("Operation took %.2f ms\n", ms); } printf("Release failed!\n"); @@ -431,35 +266,47 @@ void Client::timerEvent(QTimerEvent*) } else if (params[0] == "add") { if (!resourceSet) { - printf("Update failed!\n"); + printf("Add failed!\n"); } else if (params.count() == 1 || params[1].isEmpty() || params[1].isNull()) { printf("List of desired resources is missing. See help ...\n"); } else { - uint32_t temp = Client::parseResourceList(params[1]); - if (params.count() > 2 && params[2] == "-o") { - updateSet(temp, temp, false); + bool optional=false; + QSet resToAdd; + CommandLineParser::parseResourceList(params[1], resToAdd); + if ((params.count() > 2) && (params[2] == "-o")) { + optional = true; } - else { - updateSet(temp, 0, false); + foreach (ResourcePolicy::ResourceType resource, resToAdd) { + if (!resourceSet->contains(resource)) { + resourceSet->addResource(resource); + } + if (optional) { + resourceSet->resource(resource)->setOptional(); + } } } } else if (params[0] == "remove") { if (!resourceSet || params.count() == 1) { - printf("Update failed!\n"); + printf("Remove failed!\n"); } else if (params.count() == 1 || params[1].isEmpty() || params[1].isNull()) { printf("List of desired resources is missing. See help ...\n"); } else { - uint32_t temp = Client::parseResourceList(params[1]); + QSet resToRemove; + CommandLineParser::parseResourceList(params[1], resToRemove); if (params.count() > 2 && params[2] == "-o") { - updateSet(temp, temp, true); + foreach (ResourcePolicy::ResourceType resource, resToRemove) { + resourceSet->resource(resource)->setOptional(false); + } } else { - updateSet(temp, 0, true); + foreach (ResourcePolicy::ResourceType resource, resToRemove) { + resourceSet->deleteResource(resource); + } } } } diff --git a/resourceqt-client/client.h b/resourceqt-client/client.h index 200e4b6..249a7a8 100644 --- a/resourceqt-client/client.h +++ b/resourceqt-client/client.h @@ -8,87 +8,18 @@ #include #include -#define RES_AUDIO_PLAYBACK (1<<0) -#define RES_VIDEO_PLAYBACK (1<<1) -#define RES_AUDIO_RECORDING (1<<2) -#define RES_VIDEO_RECORDING (1<<3) -#define RES_VIBRA (1<<4) -#define RES_LEDS (1<<5) -#define RES_BACKLIGHT (1<<6) -#define RES_SYSTEM_BUTTON (1<<7) -#define RES_LOCK_BUTTON (1<<8) -#define RES_SCALE_BUTTON (1<<9) -#define RES_SNAP_BUTTON (1<<10) -#define RES_LENS_COVER (1<<11) -#define RES_HEADSET_BUTTONS (1<<12) - -class TimeStat -{ -public: - double totalTime; - - TimeStat() - { - running = false; - totalTime = 0; - }; - - inline void markStart() - { - running = true; - getrusage(RUSAGE_SELF, &start); - } - - inline bool markEnd() - { - getrusage(RUSAGE_SELF, &end); - - if( !running ) - return false; - - running = false; - timevalSub(&end.ru_utime, &start.ru_utime, &end.ru_utime); - timevalSub(&end.ru_stime, &start.ru_stime, &end.ru_stime); - - double sys = end.ru_stime.tv_sec * 1000.0 + end.ru_stime.tv_usec / 1000.0; - double usr = end.ru_utime.tv_sec * 1000.0 + end.ru_utime.tv_usec / 1000.0; - totalTime = sys + usr; - - return true; - } - - void report(const char* format) - { - printf(format, totalTime); - } - -private: - bool running; - struct rusage start; - struct rusage end; - - void timevalSub(struct timeval *a, struct timeval *b, struct timeval *diff) - { - diff->tv_sec = a->tv_sec - b->tv_sec; - if( a->tv_usec < b->tv_usec ) - { - diff->tv_sec--; - diff->tv_usec = 1000000 - b->tv_usec + a->tv_usec; - } - else - diff->tv_usec = a->tv_usec - b->tv_usec; - } -}; +#include "commandlineparser.h" +#include "time-stat.h" class Client : public QObject { Q_OBJECT public: - Client(QString appClass); + Client(); ~Client(); - bool initialize(uint32_t all, uint32_t optional, bool alwaysReply, bool autoRelease); + bool initialize(const CommandLineParser &commandParser); static uint32_t parseResourceList(QString resourceListStr); private slots: @@ -102,16 +33,12 @@ protected: void timerEvent(QTimerEvent *e); private: - QTextStream* standardInput; - int mainTimerID; + QTextStream standardInput; + int mainTimerID; - uint32_t resourcesAll; - uint32_t resourcesOptional; - QString applicationClass; + QString applicationClass; - TimeStat timeStat; - - ResourcePolicy::ResourceSet* resourceSet; + ResourcePolicy::ResourceSet *resourceSet; ResourcePolicy::Resource* allocateResource(ResourcePolicy::ResourceType resource, bool optional); ResourcePolicy::ResourceType getResourceType(uint32_t resource); @@ -123,3 +50,4 @@ private: }; #endif + diff --git a/resourceqt-client/commandlineparser.cpp b/resourceqt-client/commandlineparser.cpp new file mode 100644 index 0000000..86e5d53 --- /dev/null +++ b/resourceqt-client/commandlineparser.cpp @@ -0,0 +1,223 @@ +#include "commandlineparser.h" +#include + +QHash CommandLineParser::resourceValues; + +CommandLineParser::CommandLineParser(): + allResources(), optResources(), autoRelease(false), alwaysReply(false), + verbose(false), allowUnkownResourceClass(false), output(stdout) +{ + resourceValues["AudioPlayback"] = ResourcePolicy::AudioPlaybackType; + resourceValues["VideoPlayback"] = ResourcePolicy::VideoPlaybackType; + resourceValues["AudioRecording"] = ResourcePolicy::AudioRecorderType; + resourceValues["VideoRecording"] = ResourcePolicy::VideoRecorderType; + resourceValues["Vibra"] = ResourcePolicy::VibraType; + resourceValues["Leds"] = ResourcePolicy::LedsType; + resourceValues["Backlight"] = ResourcePolicy::BacklightType; + resourceValues["SystemButton"] = ResourcePolicy::SystemButtonType; + resourceValues["LockButton"] = ResourcePolicy::LockButtonType; + resourceValues["ScaleButton"] = ResourcePolicy::ScaleButtonType; + resourceValues["SnapButton"] = ResourcePolicy::SnapButtonType; + resourceValues["LensCover"] = ResourcePolicy::LensCoverType; + resourceValues["HeadsetButtons"] = ResourcePolicy::HeadsetButtonsType; +} + +CommandLineParser::~CommandLineParser() +{ +} + +bool CommandLineParser::parseArguments() +{ + QStringList args = QCoreApplication::arguments(); + QStringList::const_iterator ci = args.constBegin(); + // skip the first arg, which is program name + ++ci; + while (ci != args.constEnd()) { + if ((*ci).at(0) == QChar('-')) { + switch ((*ci).at(1).toAscii()) { + case 'd': + case 'm': + case 's': + case 't': + output << "The switch '" << (*ci).at(1) << "' is only present for compatibility reasons." << endl; + break; + case 'f': + if (!parseModeValues(*(++ci))) { + return false; + } + break; + case 'o': + if (!parseResourceList(*(++ci), optResources)) { + output << "Failed to parse resources: " << *ci << endl; + return false; + } + break; + case 'u': + allowUnkownResourceClass = true; + break; + case 'h': + default: + usage(); + return false; + } + } + else { + //assume there are no more args + break; + } + ++ci; + } + + if (ci == args.constEnd()) { + usage(); + return false; + } + else { + if (!parseClassString(*ci)) { + return false; + } + ++ci; + if (ci != args.constEnd()) { + if (!parseResourceList(*ci, allResources)) { + output << "Error while parsing resource list" << endl; + } + } + } + + if (!allResources.contains(optResources)) { + output << "optional resources are not subset of all resources" << endl; + return false; + } + return true; +} + +bool CommandLineParser::parseClassString(const QString &str) +{ + if (!allowUnkownResourceClass && + (str != "call") && + (str != "camera") && + (str != "ringtone") && + (str != "alarm") && + (str != "navigator") && + (str != "game") && + (str != "player") && + (str != "event") && + (str != "background") && + (str != "videoeditor") ) + { + output << "invalid class " << str; + return false; + } + + applicationClass = str; + return true; +} + +bool CommandLineParser::parseResourceList(const QString &resourceListStr, + QSet &resources) +{ + if (resourceListStr.isEmpty() || resourceListStr.isNull()) { + return false; + } + else { + QStringList resList = resourceListStr.split(",", QString::SkipEmptyParts); + + foreach(QString res, resList) { + if (resourceValues.contains(res)) { + resources.insert(resourceValues[res]); + } + } + } + + return true; +} + +bool CommandLineParser::parseModeValues(const QString &modeListStr) +{ + if (modeListStr.isEmpty() || modeListStr.isNull()) { + return false; + } + + QStringList modeList = modeListStr.split(",", QString::SkipEmptyParts); + + foreach(QString mode, modeList) { + if (mode == "AutoRelease") { + autoRelease = true; + } + else if (mode == "AlwaysReply") { + alwaysReply = true; + } + else { + output << "Ignoring unknown mode '" << mode << "'!" << endl; + } + } + return true; +} + +void CommandLineParser::usage() +{ + output << "usage: resourceqt-client [-h] [-m mode-values]" << + "[-o optional-resources] [-s shared-resources -m shared-mask] " << + "class all-resources" << endl; + output << "\toptions:" << endl; + output << "\t h\tprint this help message and exit" << endl; + output << "\t f\tmode values. See 'modes' below for the " + "\n\t\tsyntax of " << endl; + output << "\t o\toptional resources. See 'resources' below for the " + "syntax of\n\t\t" << endl; + output << "\tclass:" << endl; + output << "\t\tcall\t - for native or 3rd party telephony" << endl; + output << "\t\tcamera\t - for photo applications" << endl; + output << "\t\tringtone - for ringtones" << endl; + output << "\t\talarm\t - for alarm clock" << endl; + output << "\t\tnavigator - for mapping applications" << endl; + output << "\t\tgame\t - for gaming" << endl; + output << "\t\tplayer\t - for media playback/recording" << endl; + output << "\t\tevent\t - for messaging and other event notifications" << endl; + output << "\t\tbackground - for thumbnailing etc" << endl; + output << "\t\tvideoeditor\t - for video editing/MMS" << endl; + output << "\tresources:" << endl; + output << "\t comma separated list of the following resources" << endl; + + QHash::const_iterator ci = resourceValues.constBegin(); + while (ci != resourceValues.constEnd()) { + output << "\t\t" << ci.key() << endl; + ++ci; + } + output << "\t no whitespace allowed in the resource list." << endl; + output << "\tmodes:" << endl; + output << "\t comma separated list of the following modes" << endl; + output << "\t\tAutoRelease" << endl; + output << "\t\tAlwaysReply" << endl; +} + +const QSet& CommandLineParser::resources() const +{ + return allResources; +} + +const QSet& CommandLineParser::optionalResources() const +{ + return optResources; +} + +QString CommandLineParser::resourceApplicationClass() const +{ + return applicationClass; +} + +bool CommandLineParser::shouldAutoRelease() const +{ + return autoRelease; +} + +bool CommandLineParser::shouldAlwaysReply() const +{ + return alwaysReply; +} + +bool CommandLineParser::shouldBeVerbose() const +{ + return verbose; +} + diff --git a/resourceqt-client/commandlineparser.h b/resourceqt-client/commandlineparser.h new file mode 100644 index 0000000..c799f9b --- /dev/null +++ b/resourceqt-client/commandlineparser.h @@ -0,0 +1,47 @@ +#ifndef COMMANDLINEPARSER_H +#define COMMANDLINEPARSER_H +#include +#include +#include +#include +#include +#include + +class CommandLineParser +{ +public: + CommandLineParser(); + ~CommandLineParser(); + +public: + bool parseArguments(); + + static bool parseResourceList(const QString &resourceListStr, + QSet &resourceList); + + const QSet& resources() const; + const QSet& optionalResources() const; + QString resourceApplicationClass() const; + bool shouldAutoRelease() const; + bool shouldAlwaysReply() const; + bool shouldBeVerbose() const; + +private: + QSet allResources; + QSet optResources; + static QHash resourceValues; + QString applicationClass; + bool autoRelease; + bool alwaysReply; + bool verbose; + bool allowUnkownResourceClass; + QTextStream output; + + bool parseClassString(const QString &str); + bool parseModeValues(const QString &modeListStr); + + void usage(); + +}; +#endif + diff --git a/resourceqt-client/resourceqt-client.cpp b/resourceqt-client/resourceqt-client.cpp index 522ce8d..e2468c5 100644 --- a/resourceqt-client/resourceqt-client.cpp +++ b/resourceqt-client/resourceqt-client.cpp @@ -1,210 +1,46 @@ #include #include #include -#include -#include -#include -#include #include "client.h" +#include "commandlineparser.h" -class CommandLineParser -{ -public: - CommandLineParser(int argc, char** argv) { - exitCode = 0; - exitFlag = false; - exeName = strdup(basename(argv[0])); - - resourcesAll = 0; - resourcesOptional = 0; - autoRelease = false; - alwaysReply = false; - - parseArguments(argc, argv); - } - - ~CommandLineParser() { - if (exeName != NULL) { - delete exeName; - exeName = NULL; - } - } - -public: - bool exitFlag; - int exitCode; - - uint32_t resourcesAll; - uint32_t resourcesOptional; - QString applicationClass; - bool autoRelease; - bool alwaysReply; - - void printMessage(const char *fmt, ...) { - va_list ap; - char fmtbuf[512]; - - snprintf(fmtbuf, sizeof(fmtbuf), "%s\n", fmt); - va_start(ap, fmt); - vprintf(fmtbuf, ap); - va_end(ap); - } - -private: - char* exeName; - - void printError(const char *fmt, ...) { - va_list ap; - char fmtbuf[512]; - - snprintf(fmtbuf, sizeof(fmtbuf), "%s\n", fmt); - va_start(ap, fmt); - vprintf(fmtbuf, ap); - va_end(ap); - - exitCode = errno; - exitFlag = true; - } - - void parseArguments(int argc, char** argv) { - int option; - - while ((option = getopt(argc, argv, "hm:o:")) != -1) { - switch (option) { - case 'h': - usage(0); - return; - case 'm': - parseModeValues(optarg); - break; - case 'o': - resourcesOptional = Client::parseResourceList(optarg); - break; - default: - usage(EINVAL); - return; - } - } +bool verbose = true; - if ((optind != argc - 2) && (optind != argc - 1)) { - usage(EINVAL); - return; - } - else { - applicationClass = parseClassString(argv[optind]); - if (argc > optind + 1) - resourcesAll = Client::parseResourceList(argv[optind+1]); - else - resourcesAll = 0; - } - - if (!resourcesAll) { - printMessage("No resources found, use add command to add some ..."); - } - - if ((resourcesAll | resourcesOptional) != resourcesAll) { - printError("optional resources are not subset of all resources"); - } - } - - char* parseClassString(char *str) { - if (strcmp(str, "call") && - strcmp(str, "camera") && - strcmp(str, "ringtone") && - strcmp(str, "alarm") && - strcmp(str, "navigator") && - strcmp(str, "game") && - strcmp(str, "player") && - strcmp(str, "event") && - strcmp(str, "background")) { - printError("invalid class '%s'", str); - return NULL; - } - - return str; - } - - void parseModeValues(QString modeListStr) { - if (!modeListStr.isEmpty() && !modeListStr.isNull()) { - QStringList modeList = modeListStr.split(",", QString::SkipEmptyParts); - - for (int i = 0; i < modeList.count(); i++) { - if (modeList[i] == "AutoRelease") { - autoRelease = true; - } - else if (modeList[i] == "AlwaysReply") { - alwaysReply = true; - } - else { - const char* mode = qPrintable(modeList[i]); - printMessage("Ignoring unknown mode '%s'!", mode); - } - } - } - } - - void usage(int theExitCode) { - printf("usage: %s [-h] [-m mode-values]" - "[-o optional-resources] [-s shared-resources -m shared-mask] " - "class all-resources\n", - exeName); - printf("\toptions:\n"); - printf("\t h\tprint this help message and exit\n"); - printf("\t m\tmode values. See 'modes' below for the " - "\n\t\tsyntax of \n"); - printf("\t o\toptional resources. See 'resources' below for the " - "syntax of\n\t\t\n"); - printf("\tclass:\n"); - printf("\t\tcall\t - for native or 3rd party telephony\n"); - printf("\t\tcamera\t - for photo applications\n"); - printf("\t\tringtone - for ringtones\n"); - printf("\t\talarm\t - for alarm clock\n"); - printf("\t\tnavigator - for mapping applications\n"); - printf("\t\tgame\t - for gaming\n"); - printf("\t\tplayer\t - for media playback/recording\n"); - printf("\t\tevent\t - for messaging and other event notifications\n"); - printf("\t\tbackground - for thumbnailing etc\n"); - printf("\tresources:\n"); - printf("\t comma separated list of the following resources\n"); - printf("\t\tAudioPlayback\n"); - printf("\t\tVideoPlayback\n"); - printf("\t\tAudioRecording\n"); - printf("\t\tVideoRecording\n"); - printf("\t\tVibra\n"); - printf("\t\tLeds\n"); - printf("\t\tBackLight\n"); - printf("\t\tSystemButton\n"); - printf("\t\tLockButton\n"); - printf("\t\tScaleButton\n"); - printf("\t\tSnapButton\n"); - printf("\t\tLensCover\n"); - printf("\t\tHeadsetButtons\n"); - printf("\t no whitespace allowed in the resource list.\n"); - printf("\tmodes:\n"); - printf("\t comma separated list of the following modes\n"); - printf("\t\tAutoRelease\n"); - printf("\t\tAlwaysReply\n"); - fflush(stdout); - - exitCode = theExitCode; - exitFlag = true; +void debugOutput(QtMsgType type, const char *msg) +{ + switch (type) { + case QtDebugMsg: + if (verbose) + fprintf(stderr, "Debug: %s\n", msg); + break; + case QtWarningMsg: + fprintf(stderr, "Warning: %s\n", msg); + break; + case QtCriticalMsg: + fprintf(stderr, "Critical: %s\n", msg); + break; + case QtFatalMsg: + fprintf(stderr, "Fatal: %s\n", msg); + abort(); } -}; +} int main(int argc, char *argv[]) { - CommandLineParser parser(argc, argv); - - if (parser.exitFlag) - return parser.exitCode; - + qInstallMsgHandler(debugOutput); QCoreApplication app(argc, argv); + CommandLineParser parser; + Client client; + + if (!parser.parseArguments()) { + return 1; + } - Client client(parser.applicationClass); - if (!client.initialize(parser.resourcesAll, parser.resourcesOptional, parser.alwaysReply, parser.autoRelease)) { - parser.printMessage("initialization failed!"); - return EINVAL; + if (!client.initialize(parser)) { + printf("Initialization failed\n"); + return 2; } return app.exec(); } + diff --git a/resourceqt-client/resourceqt-client.pro b/resourceqt-client/resourceqt-client.pro index 7b4e7ff..bd2c178 100644 --- a/resourceqt-client/resourceqt-client.pro +++ b/resourceqt-client/resourceqt-client.pro @@ -14,8 +14,8 @@ QMAKE_CXXFLAGS += -Wall LIBS += -L$${LIBRESOURCEQT}/build -lresourceqt -L$${LIBDBUSQEVENTLOOP}/build -ldbus-qeventloop # Input -HEADERS = client.h -SOURCES += resourceqt-client.cpp client.cpp +HEADERS = client.h commandlineparser.h time-stat.h +SOURCES += resourceqt-client.cpp commandlineparser.cpp client.cpp time-stat.c QMAKE_DISTCLEAN += -r .moc .obj diff --git a/resourceqt-client/time-stat.c b/resourceqt-client/time-stat.c new file mode 100644 index 0000000..6fb4fdc --- /dev/null +++ b/resourceqt-client/time-stat.c @@ -0,0 +1,33 @@ +#include "time-stat.h" + +static struct timespec start_time = {0,0}; + +int start_timer(void) +{ + int r; + r = clock_gettime(CLOCK_REALTIME, &start_time); + + if (r == 0) + return 1; + else + return 0; +} + +double stop_timer(void) +{ + struct timespec end_time; + int r; + double milliseconds = 0.0; + + r = clock_gettime(CLOCK_REALTIME, &end_time); + + if (r == 0) { + milliseconds = 1000.0 * (end_time.tv_sec - start_time.tv_sec) + + (end_time.tv_nsec - start_time.tv_nsec) / 1000000.0; + } + start_time.tv_sec = 0; + start_time.tv_nsec = 0; + + return milliseconds; +} + diff --git a/resourceqt-client/time-stat.h b/resourceqt-client/time-stat.h new file mode 100644 index 0000000..0bd5eaf --- /dev/null +++ b/resourceqt-client/time-stat.h @@ -0,0 +1,16 @@ +#ifndef TIME_STAT_H +#define TIME_STAT_H +#include + +#ifdef __cplusplus +extern "C" { +#endif +int start_timer(void); + +double stop_timer(void); +#ifdef __cplusplus +} +#endif + +#endif + -- cgit v1.2.3