[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 4 | // %flair:license}
|
---|
| 5 | // created: 2012/05/02
|
---|
| 6 | // filename: Widget.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: classe pour widget
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "Widget.h"
|
---|
| 19 | #include "Widget_impl.h"
|
---|
| 20 | #include "Object_impl.h"
|
---|
| 21 | #include "FrameworkManager.h"
|
---|
| 22 | #include "FrameworkManager_impl.h"
|
---|
| 23 | #include "config.h"
|
---|
| 24 | #ifdef __XENO__
|
---|
| 25 | #include <native/heap.h>
|
---|
| 26 | #else
|
---|
| 27 | #include <errno.h>
|
---|
| 28 | #include <cstring>
|
---|
| 29 | #endif
|
---|
| 30 |
|
---|
| 31 | using std::string;
|
---|
| 32 | using std::vector;
|
---|
| 33 | using namespace flair::core;
|
---|
| 34 | using namespace flair::gui;
|
---|
| 35 |
|
---|
[15] | 36 | namespace {
|
---|
[2] | 37 | #ifdef __XENO__
|
---|
[15] | 38 | RT_HEAP xml_heap;
|
---|
[2] | 39 |
|
---|
[15] | 40 | void xml2_free(void *mem) {
|
---|
| 41 | // Printf("free %x\n",mem);
|
---|
| 42 | if (mem == NULL)
|
---|
| 43 | return;
|
---|
| 44 | int status = rt_heap_free(&xml_heap, mem);
|
---|
[2] | 45 |
|
---|
[15] | 46 | if (status != 0) {
|
---|
| 47 | printf("libxml2: rt_heap_free error (%s)\n", strerror(-status));
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
[2] | 50 |
|
---|
[15] | 51 | void *xml2_alloc(size_t sz) {
|
---|
| 52 | void *ptr;
|
---|
| 53 | // printf("alloc %i\n",sz);
|
---|
| 54 | int status = rt_heap_alloc(&xml_heap, sz, TM_NONBLOCK, &ptr);
|
---|
| 55 | if (status != 0) {
|
---|
| 56 | printf("libxml2: rt_heap_alloc error (%s)\n", strerror(-status));
|
---|
| 57 | }
|
---|
| 58 | // Printf("alloc %x %i\n",ptr,sz);
|
---|
[2] | 59 |
|
---|
[15] | 60 | return ptr;
|
---|
| 61 | }
|
---|
[2] | 62 |
|
---|
[15] | 63 | void *xml2_realloc(void *emem, size_t sz) {
|
---|
| 64 | // Printf("realloc %x %i -> %i\n",emem,sz,sz);
|
---|
| 65 | void *mem_re;
|
---|
[2] | 66 |
|
---|
[15] | 67 | if (emem == NULL) {
|
---|
| 68 | return xml2_alloc(sz);
|
---|
| 69 | } else if (sz == 0) {
|
---|
| 70 | xml2_free(emem);
|
---|
| 71 | }
|
---|
[2] | 72 |
|
---|
[15] | 73 | mem_re = xml2_alloc(sz);
|
---|
[2] | 74 |
|
---|
[15] | 75 | memcpy(mem_re, emem, sz);
|
---|
[2] | 76 |
|
---|
[15] | 77 | xml2_free(emem);
|
---|
[2] | 78 |
|
---|
[15] | 79 | return mem_re;
|
---|
| 80 | }
|
---|
[2] | 81 |
|
---|
[15] | 82 | char *xml2_strdup(const char *str) {
|
---|
| 83 | // printf("strdup %s\n",str);
|
---|
| 84 | char *s = (char *)xml2_alloc(strlen(str) + 1);
|
---|
| 85 | if (s == NULL)
|
---|
| 86 | return NULL;
|
---|
[2] | 87 |
|
---|
[15] | 88 | strcpy(s, str);
|
---|
| 89 | return s;
|
---|
| 90 | }
|
---|
[2] | 91 | #endif //__XENO__
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[15] | 94 | Widget_impl::Widget_impl(Widget *self, const Widget *parent, string name,
|
---|
| 95 | string type) {
|
---|
| 96 | // Printf("Widget %s\n",name.c_str());
|
---|
| 97 | isenabled = true;
|
---|
| 98 | file_node = NULL;
|
---|
| 99 | this->self = self;
|
---|
[2] | 100 |
|
---|
[15] | 101 | // n'est execute qu'une fois lorsqu'on construit le FrameworkManager
|
---|
| 102 | if (parent == NULL) {
|
---|
[2] | 103 | #ifdef __XENO__
|
---|
[15] | 104 | string tmp_name;
|
---|
| 105 | tmp_name = name + "-xml";
|
---|
| 106 | int status = rt_heap_create(&xml_heap, tmp_name.c_str(), XML_HEAP, H_FIFO);
|
---|
| 107 | if (status != 0) {
|
---|
| 108 | self->Err("rt_heap_create error (%s)\n", strerror(-status));
|
---|
| 109 | }
|
---|
[2] | 110 |
|
---|
[15] | 111 | xmlMemSetup(xml2_free, xml2_alloc, xml2_realloc, xml2_strdup);
|
---|
[2] | 112 | #endif //__XENO__
|
---|
[15] | 113 | }
|
---|
| 114 |
|
---|
| 115 | // xml init
|
---|
| 116 | if (parent == NULL) {
|
---|
| 117 | // create local doc
|
---|
| 118 | send_doc = xmlNewDoc((xmlChar *)"1.0");
|
---|
| 119 | send_node = xmlNewNode(NULL, (xmlChar *)type.c_str());
|
---|
| 120 | xmlDocSetRootElement(send_doc, send_node);
|
---|
| 121 | xmlSetProp(send_node, (xmlChar *)"name",
|
---|
| 122 | (xmlChar *)self->ObjectName().c_str());
|
---|
| 123 | } else {
|
---|
| 124 | parent->pimpl_->AddChild(self);
|
---|
| 125 |
|
---|
| 126 | // get node corresponding to this widget in the xml file
|
---|
| 127 | if (parent->pimpl_->file_node != NULL) {
|
---|
| 128 | xmlChar *search = xmlCharStrdup(type.c_str());
|
---|
| 129 | file_node =
|
---|
| 130 | GetNodeByProp(parent->pimpl_->file_node->xmlChildrenNode, search,
|
---|
| 131 | (xmlChar *)"name", (xmlChar *)name.c_str());
|
---|
| 132 | xmlFree(search);
|
---|
| 133 | } else {
|
---|
| 134 | self->Err("parent->file_node is NULL\n");
|
---|
[2] | 135 | }
|
---|
| 136 |
|
---|
[15] | 137 | if (file_node == NULL) {
|
---|
| 138 | self->Warn("%s, no match found in xml file\n", type.c_str());
|
---|
| 139 | xmlNode *node;
|
---|
| 140 | node = xmlNewNode(NULL, (xmlChar *)type.c_str());
|
---|
| 141 | xmlSetProp(node, (xmlChar *)"name", (xmlChar *)name.c_str());
|
---|
| 142 | file_node = xmlAddChild(parent->pimpl_->file_node, node);
|
---|
| 143 | //((Widget*)getFrameworkManager())->pimpl_->PrintXml();
|
---|
[2] | 144 | }
|
---|
| 145 |
|
---|
[15] | 146 | send_doc = parent->pimpl_->CopyDoc();
|
---|
[2] | 147 |
|
---|
[15] | 148 | // on recupere le dernier node
|
---|
| 149 | xmlNodePtr node = xmlDocGetRootElement(send_doc);
|
---|
| 150 | while (node->children != NULL)
|
---|
| 151 | node = node->children;
|
---|
[2] | 152 |
|
---|
[15] | 153 | // on ajoute le node du widget
|
---|
| 154 | send_node = xmlNewNode(NULL, (xmlChar *)type.c_str());
|
---|
| 155 | xmlSetProp(send_node, (xmlChar *)"name", (xmlChar *)name.c_str());
|
---|
| 156 | xmlAddChild(node, send_node);
|
---|
| 157 | }
|
---|
[2] | 158 |
|
---|
[15] | 159 | // Printf("Widget ok %s\n",name.c_str());
|
---|
[2] | 160 | }
|
---|
| 161 |
|
---|
[15] | 162 | Widget_impl::~Widget_impl() {
|
---|
| 163 | // Printf("destruction widget %s\n",self->ObjectName().c_str());
|
---|
[2] | 164 |
|
---|
[15] | 165 | if (self->Parent() != NULL)
|
---|
| 166 | ((Widget *)(self->Parent()))->pimpl_->RemoveChild(self);
|
---|
[2] | 167 |
|
---|
[15] | 168 | // on efface les widgets enfants avant d'envoyer son propre delete au sol
|
---|
| 169 | // dans le delete child on modifie le child du parent, donc on se refere
|
---|
| 170 | // toujours au premier
|
---|
| 171 | while (childs.size() != 0) {
|
---|
| 172 | // Printf("child %i
|
---|
| 173 | // %s\n",childs.size(),childs.front()->ObjectName().c_str());
|
---|
| 174 | if (childs.front() != NULL)
|
---|
| 175 | delete childs.front();
|
---|
| 176 | }
|
---|
| 177 | childs.clear();
|
---|
[2] | 178 |
|
---|
[15] | 179 | self->SetVolatileXmlProp("Delete", true);
|
---|
[2] | 180 |
|
---|
[15] | 181 | if (self->Parent() != NULL)
|
---|
| 182 | SendXml();
|
---|
[2] | 183 |
|
---|
[15] | 184 | xmlFreeDoc(send_doc);
|
---|
[2] | 185 |
|
---|
[15] | 186 | if (self->Parent() == NULL) {
|
---|
| 187 | xmlCleanupParser();
|
---|
[2] | 188 | #ifdef __XENO__
|
---|
[15] | 189 | int status;
|
---|
| 190 | RT_HEAP_INFO info;
|
---|
| 191 | status = rt_heap_inquire(&xml_heap, &info);
|
---|
| 192 | if (status != 0) {
|
---|
| 193 | self->Err("rt_heap_inquire error (%s)\n", strerror(-status));
|
---|
| 194 | }
|
---|
| 195 | if (info.usedmem != 0)
|
---|
| 196 | self->Err("fuite memoire xml heap (%ld)\n", info.usedmem);
|
---|
| 197 | // Printf("fin heap xml\n");
|
---|
| 198 | status = rt_heap_delete(&xml_heap);
|
---|
| 199 | if (status != 0) {
|
---|
| 200 | self->Err("rt_heap_delete error (%s)\n", strerror(-status));
|
---|
| 201 | }
|
---|
[2] | 202 | #endif
|
---|
[15] | 203 | }
|
---|
| 204 | // Printf("destruction widget %s ok\n",self->ObjectName().c_str());
|
---|
[2] | 205 | }
|
---|
| 206 |
|
---|
[15] | 207 | void Widget_impl::AddChild(const Widget *child) {
|
---|
| 208 | childs.push_back((Widget *)child);
|
---|
[2] | 209 | }
|
---|
| 210 |
|
---|
[15] | 211 | void Widget_impl::RemoveChild(const Widget *child) {
|
---|
| 212 | for (vector<Widget *>::iterator it = childs.begin(); it < childs.end();
|
---|
| 213 | it++) {
|
---|
| 214 | if (*it == child) {
|
---|
| 215 | childs.erase(it);
|
---|
| 216 | break;
|
---|
[2] | 217 | }
|
---|
[15] | 218 | }
|
---|
[2] | 219 | }
|
---|
| 220 |
|
---|
[15] | 221 | xmlDocPtr Widget_impl::CopyDoc(void) { return xmlCopyDoc(send_doc, 1); }
|
---|
[2] | 222 |
|
---|
| 223 | void Widget_impl::ClearXmlProps(void) {
|
---|
[15] | 224 | xmlUnlinkNode(send_node);
|
---|
| 225 | xmlFreeNode(send_node);
|
---|
[2] | 226 |
|
---|
[15] | 227 | xmlNodePtr node;
|
---|
| 228 | node = xmlDocGetRootElement(send_doc);
|
---|
[2] | 229 |
|
---|
[15] | 230 | if (node == NULL) { // il ne reste plus rien, on refait le rootelement
|
---|
| 231 | send_node = xmlNewNode(NULL, (xmlChar *)XML_ROOT_TYPE);
|
---|
| 232 | xmlSetProp(send_node, (xmlChar *)"name",
|
---|
| 233 | (xmlChar *)self->ObjectName().c_str());
|
---|
| 234 | xmlDocSetRootElement(send_doc, send_node);
|
---|
| 235 | } else {
|
---|
| 236 | while (node->children != NULL)
|
---|
| 237 | node = node->children;
|
---|
| 238 | send_node = xmlNewNode(NULL, (xmlChar *)self->ObjectType().c_str());
|
---|
| 239 | xmlSetProp(send_node, (xmlChar *)"name",
|
---|
| 240 | (xmlChar *)self->ObjectName().c_str());
|
---|
| 241 | xmlAddChild(node, send_node);
|
---|
| 242 | }
|
---|
[2] | 243 | }
|
---|
| 244 |
|
---|
| 245 | void Widget_impl::printSendNode() {
|
---|
| 246 |
|
---|
[15] | 247 | xmlChar *xmlbuff;
|
---|
| 248 | int buffersize;
|
---|
| 249 | xmlDocDumpFormatMemory(send_doc, &xmlbuff, &buffersize, 1);
|
---|
| 250 | Printf("xml:\n%s\n", xmlbuff);
|
---|
| 251 | xmlFree(xmlbuff);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | void Widget_impl::SendXml(void) {
|
---|
| 255 | if (getUiCom() != NULL) {
|
---|
[2] | 256 | xmlChar *xmlbuff;
|
---|
| 257 | int buffersize;
|
---|
[15] | 258 | xmlDocDumpFormatMemory(send_doc, &xmlbuff, &buffersize, 0);
|
---|
| 259 | getUiCom()->Send((char *)xmlbuff, buffersize);
|
---|
[2] | 260 | xmlFree(xmlbuff);
|
---|
| 261 |
|
---|
[15] | 262 | ClearXmlProps();
|
---|
| 263 | }
|
---|
[2] | 264 | }
|
---|
| 265 |
|
---|
| 266 | void Widget_impl::setEnabled(bool status) {
|
---|
[15] | 267 | self->SetVolatileXmlProp("IsEnabled", status);
|
---|
| 268 | SendXml();
|
---|
[2] | 269 | }
|
---|
| 270 |
|
---|
[15] | 271 | void Widget_impl::ProcessXML(xmlNode *node) {
|
---|
| 272 | // Printf("ProcessXML %s %s\n",xmlGetProp(send_node,
|
---|
| 273 | // (xmlChar*)"name"),send_node->name);
|
---|
| 274 | xmlNode *cur_node = NULL;
|
---|
[2] | 275 |
|
---|
[15] | 276 | for (size_t i = 0; i < childs.size(); i++) {
|
---|
| 277 | for (cur_node = node; cur_node; cur_node = cur_node->next) {
|
---|
| 278 | if (cur_node->type == XML_ELEMENT_NODE) {
|
---|
| 279 | // Printf("recherche %s\n", xmlGetProp(cur_node, (xmlChar*)"name"));
|
---|
| 280 | xmlChar *name = NULL;
|
---|
| 281 | name = xmlGetProp(cur_node, (xmlChar *)"name");
|
---|
| 282 | if (!xmlStrcmp((xmlChar *)childs[i]->ObjectName().c_str(), name)) {
|
---|
| 283 | // Printf("correspond %s\n",childs[i]->ObjectName().c_str());
|
---|
| 284 | xmlChar *new_name = NULL;
|
---|
| 285 | new_name = xmlGetProp(cur_node, (xmlChar *)"new_name");
|
---|
| 286 | if (new_name != NULL) {
|
---|
| 287 | unsigned char *ren_name =
|
---|
| 288 | new_name; // xmlGetProp(cur_node, (xmlChar*)"new_name");
|
---|
| 289 | self->Warn(
|
---|
| 290 | "%s existe deja, renommage en %s; possiblite de problemes!!\n",
|
---|
| 291 | childs[i]->ObjectName().c_str(), new_name);
|
---|
| 292 | childs[i]->Object::pimpl_->name = ((const char *)ren_name);
|
---|
| 293 | xmlSetProp(childs[i]->pimpl_->send_node, (xmlChar *)"name",
|
---|
| 294 | xmlGetProp(cur_node, (xmlChar *)"new_name"));
|
---|
| 295 | xmlFree(new_name);
|
---|
| 296 | if (name != NULL)
|
---|
| 297 | xmlFree(name);
|
---|
| 298 | break;
|
---|
| 299 | }
|
---|
[2] | 300 |
|
---|
[15] | 301 | if (name != NULL)
|
---|
| 302 | xmlFree(name);
|
---|
[2] | 303 |
|
---|
[15] | 304 | // update file node
|
---|
| 305 | xmlAttr *cur_prop = NULL;
|
---|
| 306 | // xmlAttr *file_prop = NULL;
|
---|
| 307 | for (cur_prop = cur_node->properties; cur_prop;
|
---|
| 308 | cur_prop = cur_prop->next) {
|
---|
| 309 | // Printf("rcv prop %s
|
---|
| 310 | // %s\n",cur_prop->name,cur_prop->children->content);
|
---|
| 311 | xmlSetProp(childs[i]->pimpl_->file_node, cur_prop->name,
|
---|
| 312 | cur_prop->children->content);
|
---|
| 313 | }
|
---|
[2] | 314 |
|
---|
[15] | 315 | childs[i]->XmlEvent();
|
---|
[2] | 316 |
|
---|
[15] | 317 | childs[i]->pimpl_->ProcessXML(cur_node->children);
|
---|
| 318 | break;
|
---|
[2] | 319 | }
|
---|
[15] | 320 | if (name != NULL)
|
---|
| 321 | xmlFree(name);
|
---|
| 322 | }
|
---|
[2] | 323 | }
|
---|
[15] | 324 | }
|
---|
[2] | 325 |
|
---|
[15] | 326 | // printf("fin ProcessXML %s %s\n",xmlGetProp(send_node,
|
---|
| 327 | // (xmlChar*)"name"),send_node->name);
|
---|
[2] | 328 | }
|
---|
| 329 |
|
---|
[15] | 330 | xmlNodePtr Widget_impl::GetNodeByProp(xmlNodePtr doc, xmlChar *type,
|
---|
| 331 | xmlChar *prop, xmlChar *value) {
|
---|
| 332 | // printf("cherche keyword: %s %s %s\n", type,prop,value);
|
---|
| 333 | xmlNode *cur_node = NULL;
|
---|
| 334 | for (cur_node = doc; cur_node; cur_node = cur_node->next) {
|
---|
| 335 | if (cur_node->type == XML_ELEMENT_NODE) {
|
---|
| 336 | // printf("node %s %s\n",xmlGetProp(cur_node,
|
---|
| 337 | // (xmlChar*)"name"),cur_node->name);
|
---|
| 338 | xmlChar *test = NULL;
|
---|
| 339 | test = xmlGetProp(cur_node, prop);
|
---|
| 340 | if (!xmlStrcmp(test, value) && !xmlStrcmp(cur_node->name, type)) {
|
---|
| 341 | // printf("ok\n");
|
---|
| 342 | if (test != NULL)
|
---|
| 343 | xmlFree(test);
|
---|
| 344 | return cur_node;
|
---|
| 345 | }
|
---|
| 346 | if (test != NULL)
|
---|
| 347 | xmlFree(test);
|
---|
[2] | 348 | }
|
---|
[15] | 349 | }
|
---|
[2] | 350 |
|
---|
[15] | 351 | return NULL;
|
---|
[2] | 352 | }
|
---|