src/hbtools/hbbincssmaker/main.cpp
changeset 21 4633027730f5
parent 7 923ff622b8b9
child 30 80e4d18b72f5
equal deleted inserted replaced
7:923ff622b8b9 21:4633027730f5
    32 #include <hbsharedmemorymanager_p.h>
    32 #include <hbsharedmemorymanager_p.h>
    33 #include <hbsharedcache_p.h>
    33 #include <hbsharedcache_p.h>
    34 #include <hbcssconverterutils_p.h>
    34 #include <hbcssconverterutils_p.h>
    35 #include <hboffsetmapbuilder_p.h>
    35 #include <hboffsetmapbuilder_p.h>
    36 #include <hbwidgetloader_p.h>
    36 #include <hbwidgetloader_p.h>
       
    37 #include <hblayoutparameters_p.h>
    37 
    38 
    38 // Global variables
    39 // Global variables
    39 static const QString CSSFileExtension = ".css";
    40 static const QString CSSFileExtension = ".css";
    40 static const QString WMLFileExtension = ".widgetml";
    41 static const QString WMLFileExtension = ".widgetml";
    41 static const QString ColorCSSEnding = "_color.css";
    42 static const QString ColorCSSEnding = "_color.css";
       
    43 static const QString GlobalParameterCssFile = "hbglobalparameters";
       
    44 static const QString ColorGroupCssFile = "hbcolorgroup.css";
    42 
    45 
    43 static const QString AppName = "hbbincssmaker";
    46 static const QString AppName = "hbbincssmaker";
    44 static bool verboseOn = false;
    47 static bool verboseOn = false;
    45 
    48 
    46 QTextStream out(stdout);
    49 QTextStream out(stdout);
   170                             HbMemoryManager::SharedMemory, ptr->offset);
   173                             HbMemoryManager::SharedMemory, ptr->offset);
   171                 testLayoutDef(layoutDef);
   174                 testLayoutDef(layoutDef);
   172             }
   175             }
   173         }
   176         }
   174     }
   177     }
       
   178     qint32 size = 0;
       
   179     const HbParameterItem *item = cache->parameterItemBegin(&size);
       
   180     const char* base = reinterpret_cast<const char*>(item);
       
   181     for (int i = 0; i < size; ++i, ++item) {
       
   182         QLatin1String tmp(base + item->nameOffset);
       
   183         VERBOSE(QString(tmp));
       
   184         if (!item->special) {
       
   185             HbCss::Value *value = HbMemoryUtils::getAddress<HbCss::Value>(
       
   186                     HbMemoryManager::SharedMemory, item->valueOffset);
       
   187             VERBOSELN(" = " << value->variant.toString());
       
   188         } else {
       
   189             VERBOSELN(" (special)");
       
   190         }
       
   191     }
   175     return true;
   192     return true;
   176 }
   193 }
   177 
   194 
   178 /*!
   195 /*!
   179     Collects and return layout names from \a styleRules.
   196     Collects and return layout names from \a styleRules.
   288         }
   305         }
   289     }
   306     }
   290     return cssFileMap;
   307     return cssFileMap;
   291 }
   308 }
   292 
   309 
       
   310 bool parseGlobalParameters(HbCss::StyleSheet *styleSheet,
       
   311                            const QFileInfo &fileInfo,
       
   312                            HbOffsetMapBuilder &offsetMap)
       
   313 {
       
   314     bool ok = true;
       
   315     int zoomLevel = fileInfo.dir().dirName().toInt(&ok, 10);
       
   316     if (!ok) {
       
   317         err << "global parameter zoom level invalid: " << fileInfo.dir().dirName();
       
   318         return false;
       
   319     }
       
   320     QHash<QString, HbParameterValueItem> parameters;
       
   321     GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
   322     VERBOSELN("processing global parameters for zoom level " << zoomLevel);
       
   323     foreach(const HbCss::VariableRule &rule, styleSheet->variableRules) {
       
   324         foreach(const HbCss::Declaration &decl, rule.declarations) {
       
   325             if (decl.values.count() > 1) {
       
   326                 return false;
       
   327             }
       
   328             qptrdiff offset = manager->alloc(sizeof(HbCss::Value));
       
   329             new ((char*)manager->base() + offset) HbCss::Value(decl.values.at(0));
       
   330             QString parameterName(decl.property);
       
   331             VERBOSELN(parameterName << " = " << decl.values.at(0).variant.toString());
       
   332             qptrdiff prevOffset = parameters.value(parameterName, -1).offset;
       
   333             if (prevOffset >= 0) {
       
   334                 err << "Warning: global parameter: " << parameterName <<
       
   335                        " already assigned. Using the latest value." << endl;
       
   336                 HbCss::Value *v = reinterpret_cast<HbCss::Value*>((char*)manager->base() + prevOffset);
       
   337                 HbMemoryUtils::release(v);
       
   338                 parameters.remove(parameterName);
       
   339             }
       
   340             parameters.insert(decl.property, offset);
       
   341         }
       
   342     }
       
   343     //add special variables
       
   344     QStringList variables = HbLayoutParameters::specialVariableNames();
       
   345     for(int i = 0; i < variables.count(); ++i) {
       
   346         parameters.insert(variables.at(i), HbParameterValueItem(i, true));
       
   347     }
       
   348 
       
   349     offsetMap.addGlobalParameters(zoomLevel, parameters);
       
   350     VERBOSELN("end processing global parameters");
       
   351     return true;
       
   352 }
       
   353 
   293 /*!
   354 /*!
   294     Collects the css files from \a inputFiles, parses them to shared memory, stores
   355     Collects the css files from \a inputFiles, parses them to shared memory, stores
   295     offsets to \a offsetMap.
   356     offsets to \a offsetMap.
   296     returns true on success.
   357     returns true on success.
   297 */
   358 */
   298 bool parseCss(const QStringList &inputFiles, HbOffsetMapBuilder &offsetMap)
   359 bool parseCss(const QStringList &inputFiles, HbOffsetMapBuilder &offsetMap)
   299 {
   360 {
   300     if (inputFiles.isEmpty()) return false;
   361     if (inputFiles.isEmpty()) {
       
   362         return false;
       
   363     }
   301     QMap<QString, QFileInfo> cssFiles = collectCssFiles(inputFiles);
   364     QMap<QString, QFileInfo> cssFiles = collectCssFiles(inputFiles);
   302 
   365 
   303     HbCss::Parser parser;
   366     HbCss::Parser parser;
   304     HbCss::StyleSheet *styleSheet = 0;
   367     HbCss::StyleSheet *styleSheet = 0;
   305     bool success = false;
   368     bool success = false;
   308     while (!cssFiles.isEmpty()) {
   371     while (!cssFiles.isEmpty()) {
   309         QMap<QString, QFileInfo>::iterator first = cssFiles.begin();
   372         QMap<QString, QFileInfo>::iterator first = cssFiles.begin();
   310         QMap<QString, QFileInfo>::iterator CSSFiles[CSSFileTypeEnd];
   373         QMap<QString, QFileInfo>::iterator CSSFiles[CSSFileTypeEnd];
   311 
   374 
   312         QString widgetName(first.key());
   375         QString widgetName(first.key());
   313         if (widgetName.endsWith(ColorCSSEnding)) {
   376         if (widgetName == ColorGroupCssFile) {
       
   377             //don't parse colorgroup-files to binary.
       
   378             cssFiles.erase(first);
       
   379             continue;
       
   380         } else if (widgetName.endsWith(ColorCSSEnding)) {
   314             //color css file, find the layout css pair.
   381             //color css file, find the layout css pair.
   315             CSSFiles[ColorCSSFile] = first;
   382             CSSFiles[ColorCSSFile] = first;
   316             widgetName.remove(widgetName.size() - ColorCSSEnding.size(),
   383             widgetName.remove(widgetName.size() - ColorCSSEnding.size(),
   317                               ColorCSSEnding.size());
   384                               ColorCSSEnding.size());
   318             CSSFiles[CSSFile] = cssFiles.find(widgetName + CSSFileExtension);
   385             CSSFiles[CSSFile] = cssFiles.find(widgetName + CSSFileExtension);
   321             CSSFiles[CSSFile] = first;
   388             CSSFiles[CSSFile] = first;
   322             widgetName.remove(widgetName.size() - CSSFileExtension.size(),
   389             widgetName.remove(widgetName.size() - CSSFileExtension.size(),
   323                               CSSFileExtension.size());
   390                               CSSFileExtension.size());
   324             CSSFiles[ColorCSSFile] = cssFiles.find(widgetName + ColorCSSEnding);
   391             CSSFiles[ColorCSSFile] = cssFiles.find(widgetName + ColorCSSEnding);
   325         }
   392         }
   326         int offsets[] = {-1, -1};
   393         qptrdiff offsets[] = {-1, -1};
   327 
   394 
   328         for (int i = 0; i < CSSFileTypeEnd; ++i) {
   395         for (int i = 0; i < CSSFileTypeEnd; ++i) {
   329             if (CSSFiles[i] != cssFiles.end()) {
   396             if (CSSFiles[i] != cssFiles.end()) {
   330                 const QFileInfo &file = CSSFiles[i].value();
   397                 const QFileInfo &file = CSSFiles[i].value();
   331                 VERBOSE("processing " << file.absoluteFilePath() << "...");
   398                 VERBOSE("processing " << file.absoluteFilePath() << "...");
   332                 offsets[i] = manager->alloc(sizeof(HbCss::StyleSheet));
   399                 offsets[i] = manager->alloc(sizeof(HbCss::StyleSheet));
   333                 if (offsets[i] >= 0) {
   400                 if (offsets[i] >= 0) {
   334                     styleSheet = new (static_cast<char*>(manager->base()) + offsets[i])
   401                     styleSheet = new (static_cast<char*>(manager->base()) + offsets[i])
   335                                  HbCss::StyleSheet(HbMemoryManager::SharedMemory);
   402                                  HbCss::StyleSheet(HbMemoryManager::SharedMemory);
   336                     parser.init(file.absoluteFilePath(), true);
   403                     success = parser.init(file.absoluteFilePath(), true);
   337                     success = parser.parse(styleSheet);
   404                     if (success) {
       
   405                         success = parser.parse(styleSheet);
       
   406                     }
   338                     VERBOSE("cache key = " << CSSFiles[i].key() << "...");
   407                     VERBOSE("cache key = " << CSSFiles[i].key() << "...");
   339                 }
   408                 }
   340                 if (success) {
   409                 if (success) {
   341                     VERBOSELN("ok");
   410                     VERBOSELN("ok");
   342                 } else {
   411                 } else {
   351         QString tmp;
   420         QString tmp;
   352         if (CSSFiles[CSSFile] != cssFiles.end()) {
   421         if (CSSFiles[CSSFile] != cssFiles.end()) {
   353             tmp = CSSFiles[CSSFile].key();
   422             tmp = CSSFiles[CSSFile].key();
   354             info = &CSSFiles[CSSFile].value();
   423             info = &CSSFiles[CSSFile].value();
   355         }
   424         }
   356         if (!offsetMap.addWidgetOffsets(widgetName, info, offsets)) {
   425         if (widgetName == GlobalParameterCssFile) {
       
   426             bool ok = parseGlobalParameters(styleSheet, CSSFiles[CSSFile].value(), offsetMap);
       
   427             //remove parsed global parameter stylesheet from binary, when processed.
       
   428             HbMemoryUtils::release(styleSheet);
       
   429             if (!ok) {
       
   430                 err << "Failed to parse global parameters. " << endl;
       
   431                 return false;
       
   432             }
       
   433         } else if (!offsetMap.addWidgetOffsets(widgetName, info, offsets)) {
   357             return false;
   434             return false;
   358         }
   435         }
   359 
       
   360         //remove processed files from the map.
   436         //remove processed files from the map.
   361         cssFiles.erase(CSSFiles[ColorCSSFile]);
   437         cssFiles.erase(CSSFiles[ColorCSSFile]);
   362         if (!tmp.isEmpty()) {
   438         if (!tmp.isEmpty()) {
   363             cssFiles.remove(tmp);
   439             cssFiles.remove(tmp);
   364         }
   440         }
   381     if (!file.open(QFile::ReadOnly | QFile::Text)) {
   457     if (!file.open(QFile::ReadOnly | QFile::Text)) {
   382         VERBOSELN("unable to open file: " << parseInput.filename);
   458         VERBOSELN("unable to open file: " << parseInput.filename);
   383         return false;
   459         return false;
   384     }
   460     }
   385     HbWidgetLoader::LayoutDefinition *layoutDef = 0;
   461     HbWidgetLoader::LayoutDefinition *layoutDef = 0;
   386     int layoutDefOffset = -1;
   462     qptrdiff layoutDefOffset = -1;
   387     bool success = true;
   463     bool success = true;
   388 
   464 
   389     GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
   465     GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
   390     QList<LayoutItem> layoutInfoList;
   466     QList<LayoutItem> layoutInfoList;
   391     Q_FOREACH(const CSSLayoutInfo &info, parseInput.layouts) {
   467     Q_FOREACH(const CSSLayoutInfo &info, parseInput.layouts) {
   442 
   518 
   443     // Create shared cache to shared memory.
   519     // Create shared cache to shared memory.
   444     QByteArray data(offsetMap.result());
   520     QByteArray data(offsetMap.result());
   445     bool success = false;
   521     bool success = false;
   446 
   522 
   447     if (shared->createSharedCache(data.data(), data.size(), offsetMap.size())) {
   523     if (shared->createSharedCache(data.data(), data.size(),
       
   524                                   offsetMap.size(), offsetMap.globalParameterOffset())) {
   448         // Defragment the chunk contents before dumping it in a file
   525         // Defragment the chunk contents before dumping it in a file
   449         int endOffset = HbCssConverterUtils::defragmentChunk();
   526         qptrdiff endOffset = HbCssConverterUtils::defragmentChunk();
   450         if (verboseOn) testCss();
   527         if (verboseOn) testCss();
   451 
   528 
   452         VERBOSELN("writing the binary file");
   529         VERBOSELN("writing the binary file");
   453         QFile binFile(targetFile);
   530         QFile binFile(targetFile);
   454         success = binFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
   531         success = binFile.open(QIODevice::WriteOnly | QIODevice::Truncate);