menu.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <poll.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <signal.h>
  27. #include <time.h>
  28. #include <wait.h>
  29. #include "../common/common_defs.h"
  30. #include "../commhub/commhub.h"
  31. #include "../commhub/client_utils.h"
  32. #include "fbutil.h"
  33. #include "expat-2.0.1/lib/expat.h"
  34. #include "menu.h"
  35. char *nodetype_words[NUM_NODETYPES] = {"driver_ui", "menu", "action", "widget", "class"};
  36. char *action_words[NUM_ACTIONS] = {"callrule", "setvar", "append", "backspace", "setmenu", "login", "logout", "setpaddle", "prevstop", "nextstop", "seteqnum", "geteqnum", "clearmessages", "shellcall"};
  37. char *widget_words[NUM_WIDGETS] = {"init", "button", "statusbar", "messages", "input", "text", "box", "filled_box", "line", "fullstatus"};
  38. char *font_words[NUM_FONTS] = {"small", "medium", "large"};
  39. extern int diu_fd;
  40. extern int hub_fd;
  41. extern int token_diag_serial;
  42. extern char token_diag_string[LINE_BUFFER_SIZE];
  43. //---------------------------------------------------------------
  44. // This structure is used to keep a buffer of messages to the driver
  45. //---------------------------------------------------------------
  46. typedef struct diu_message_line_struct
  47. {
  48. pixel bgcolor;
  49. pixel textcolor;
  50. int num;
  51. char message[DIU_MESSAGE_CHARS];
  52. } diu_message_line;
  53. diu_message_line diu_messages[DIU_MESSAGE_LINES] = {{0}};
  54. int diu_message_head = 0;
  55. int diu_message_num = 0;
  56. //--------------------------------------------------------------
  57. void clear_diu_messages()
  58. {
  59. diu_message_head = 0;
  60. diu_message_num = 0;
  61. memset(diu_messages, 0, sizeof(diu_messages));
  62. }
  63. void add_diu_message(pixel bgcolor, pixel textcolor, char *message)
  64. {
  65. diu_messages[diu_message_head].bgcolor = bgcolor;
  66. diu_messages[diu_message_head].textcolor = textcolor;
  67. strncpy(diu_messages[diu_message_head].message, message, DIU_MESSAGE_CHARS - 1);
  68. diu_messages[diu_message_head].message[DIU_MESSAGE_CHARS - 1] = '\0';
  69. diu_messages[diu_message_head++].num = diu_message_num++;
  70. if(diu_message_head >= DIU_MESSAGE_LINES)
  71. {
  72. diu_message_head = 0;
  73. }
  74. // printf("Head = %d, msg = %s\n", diu_message_head, message);
  75. }
  76. void replace_diu_message(pixel bgcolor, pixel textcolor, char *message)
  77. {
  78. int tmp_head = diu_message_head - 1;
  79. if(tmp_head < 0)
  80. {
  81. tmp_head = DIU_MESSAGE_LINES - 1;
  82. }
  83. diu_messages[tmp_head].bgcolor = bgcolor;
  84. diu_messages[tmp_head].textcolor = textcolor;
  85. strncpy(diu_messages[tmp_head].message, message, DIU_MESSAGE_CHARS - 1);
  86. diu_messages[tmp_head].message[DIU_MESSAGE_CHARS - 1] = '\0';
  87. diu_messages[tmp_head].num = diu_message_num++;
  88. }
  89. //This function does an inefficient linear search for an attribute value on a menu node
  90. //It is rarely used since most common widget and menu parameters are pre-parsed at load time
  91. //inline char *get_attr_val(menunode *p, char *attr)
  92. char *get_attr_val(menunode *p, char *attr)
  93. {
  94. int i;
  95. if(!p || ! attr)
  96. {
  97. return NULL;
  98. }
  99. for(i = 0; i < p->keys; i++)
  100. {
  101. if(!strcasecmp(p->key[i], attr))
  102. {
  103. return p->value[i];
  104. }
  105. }
  106. return NULL;
  107. }
  108. //inline int attr_match(menunode *p, char *attr, char *value)
  109. int attr_match(menunode *p, char *attr, char *value)
  110. {
  111. char *val = get_attr_val(p, attr);
  112. if(!val || !value)
  113. {
  114. return 0;
  115. }
  116. return !strcasecmp(val, value);
  117. }
  118. time_t paddle_req_timeout = 0;
  119. set_paddle_req paddle_req = {0};
  120. char *paddle_ok_menu = "";
  121. char *paddle_fail_menu = "";
  122. driver_status my_driver_status={0};
  123. int update_driver_status = 0;
  124. //This function searches the driver file and checks a driver, pin pair returning 0 on success
  125. //or -1 on failure.
  126. int driver_login(int drivernum, char *pinnum)
  127. {
  128. FILE *f;
  129. char name[DRIVER_NAME_LEN]={0};
  130. char pin[64];
  131. char line[LINE_BUFFER_SIZE]={0};
  132. char *inc_line;
  133. struct message_record outgoing_msg;
  134. int drv;
  135. int retval;
  136. if(!drivernum || !pinnum) { return -1; }
  137. f=fopen(DRIVERS_FILE,"rb"); //open the driver file
  138. if(!f)
  139. {
  140. format_log_message(&outgoing_msg, LOGLEVEL_ERROR, "Cannot open %s\n", DRIVERS_FILE);
  141. send_message(hub_fd,&outgoing_msg);
  142. return -1; //fail if we can't read it
  143. }
  144. while(!feof(f)) //while we haven't hit EOF
  145. {
  146. if(!fgets(line,sizeof(line)-1,f)) //read it line by line
  147. break;
  148. line[sizeof(line)-1] = '\0'; //terminating each line safely
  149. inc_line = line;
  150. while(1) //and ignoring any leading whitespace
  151. {
  152. char c = *inc_line;
  153. if( (c == ' ') ||(c == '\t') ||(c == '\r') ||(c == '\n') )
  154. {
  155. inc_line++;
  156. continue;
  157. }
  158. break;
  159. }
  160. if( (inc_line[0] == '#') || (inc_line[0] == '\0')) //and ignoring blank/comment lines
  161. continue;
  162. name[0]='\0';
  163. pin[0]='\0';
  164. retval=sscanf(inc_line,"%d %63[0123456789] %63[^\r\n]",&drv,pin,name); //parse the line format (drivernum pin_num drivername)
  165. if(retval >= 2) //if we get at least a driver number and pin number
  166. {
  167. if(drv != drivernum) //check to see if we've found a match
  168. continue;
  169. if(!strcmp(pinnum, pin)) //if the PIN matches
  170. {
  171. my_driver_status.logged_in_driver = drivernum;
  172. strncpy(my_driver_status.driver_name, name, DRIVER_NAME_LEN);
  173. my_driver_status.driver_name[DRIVER_NAME_LEN - 1] = '\0';
  174. my_driver_status.equip_num = get_equip_num();
  175. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Successful login for driver %d\n", drivernum);
  176. send_message(hub_fd,&outgoing_msg);
  177. update_driver_status |= 1;
  178. fclose(f);
  179. return 0;
  180. }
  181. else
  182. {
  183. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Failed login for driver %d\n", drivernum);
  184. send_message(hub_fd,&outgoing_msg);
  185. fclose(f);
  186. return -1;
  187. }
  188. }
  189. }
  190. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Failed login for unknown driver %d\n", drivernum);
  191. send_message(hub_fd,&outgoing_msg);
  192. fclose(f);
  193. return -1;
  194. }
  195. //Log a driver out and cancel any pending set paddle request
  196. int driver_logout()
  197. {
  198. struct message_record outgoing_msg;
  199. if(my_driver_status.logged_in_driver)
  200. {
  201. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Driver logout for driver %d\n", my_driver_status.logged_in_driver);
  202. send_message(hub_fd,&outgoing_msg);
  203. }
  204. my_driver_status.logged_in_driver = 0;
  205. my_driver_status.driver_name[0] = '\0';
  206. paddle_req_timeout = 0;
  207. update_driver_status |= 1;
  208. return 0;
  209. }
  210. //This gets the value of a menu variable by name. Again not the fastest way, but it is simple
  211. //and these operations don't occur very often
  212. static char *menuvar_getval(menutree *mt, char *ident)
  213. {
  214. menuvar *p;
  215. if(!mt || !ident)
  216. {
  217. return NULL;
  218. }
  219. p = mt->menuvars;
  220. while(p)
  221. {
  222. if(!strcasecmp(p->ident, ident))
  223. {
  224. return p->value;
  225. }
  226. p = p->next;
  227. }
  228. return NULL;
  229. }
  230. static int menuvar_setval(menutree *mt, char *ident, char *val, int limit)
  231. {
  232. menuvar *p, *q;
  233. if(!mt || !ident)
  234. {
  235. return -1;
  236. }
  237. if(val == NULL)
  238. {
  239. val = "";
  240. }
  241. if( (limit < 1) || (limit >= MENUVAR_VALUELEN) )
  242. {
  243. limit = MENUVAR_VALUELEN - 1;
  244. }
  245. q = NULL;
  246. p = mt->menuvars;
  247. while(p)
  248. {
  249. if(!strcasecmp(p->ident, ident))
  250. {
  251. break;
  252. }
  253. q = p;
  254. p = p->next;
  255. }
  256. if(p == NULL)
  257. {
  258. p = (menuvar *)malloc(sizeof(menuvar));
  259. if(p == NULL)
  260. {
  261. return -1;
  262. }
  263. p->next = NULL;
  264. strncpy(p->ident, ident, MENUVAR_IDENTLEN);
  265. p->ident[limit] = '\0';
  266. if(q)
  267. {
  268. q->next = p;
  269. }
  270. else
  271. {
  272. mt->menuvars = p;
  273. }
  274. }
  275. strncpy(p->value, val, limit);
  276. p->value[limit]='\0';
  277. return 0;
  278. }
  279. static int menuvar_append(menutree *mt, char *ident, char *val, int limit)
  280. {
  281. char temp[MENUVAR_VALUELEN] = {0};
  282. char *getret;
  283. if(!mt || !ident)
  284. {
  285. return -1;
  286. }
  287. if(val == NULL)
  288. {
  289. val = "";
  290. }
  291. getret = menuvar_getval(mt, ident);
  292. if(getret == NULL)
  293. {
  294. getret = "";
  295. }
  296. snprintf(temp, MENUVAR_IDENTLEN, "%s%s", getret, val);
  297. return menuvar_setval(mt, ident, temp, limit);
  298. }
  299. static int menuvar_backspace(menutree *mt, char *ident)
  300. {
  301. char *getret;
  302. int n;
  303. if(!mt || !ident)
  304. {
  305. return -1;
  306. }
  307. //NOTE: ---- We are counting on the fact that menuvar_getvalue returns a pointer to the ACTUAL string
  308. getret = menuvar_getval(mt, ident);
  309. if(getret == NULL)
  310. {
  311. return -1;
  312. }
  313. n = strlen(getret);
  314. if(n > 0)
  315. {
  316. getret[--n] = '\0'; //so we can modify it in this ridiculous way...
  317. }
  318. return 0;
  319. }
  320. //This function appends a node to the tail of child list of the node at the top of the stack
  321. static int append_node(menutree *mt, menunode *p)
  322. {
  323. if(mt->stack[mt->sp].n == NULL)
  324. {
  325. mt->head = p;
  326. return 0;
  327. }
  328. if(mt->stack[mt->sp].n->subtree == NULL)
  329. {
  330. mt->stack[mt->sp].n->subtree = p;
  331. }
  332. else
  333. {
  334. if(mt->stack[mt->sp].n->subtree->next == NULL)
  335. {
  336. mt->stack[mt->sp].n->subtree->next = p;
  337. mt->stack[mt->sp].tail = p;
  338. }
  339. else
  340. {
  341. mt->stack[mt->sp].tail->next = p;
  342. mt->stack[mt->sp].tail = p;
  343. }
  344. }
  345. return 0;
  346. }
  347. //This pushes the menu stack and sets the parser to expect the noted type of node
  348. static int push_menustack(menutree *mt, menunode *p, menunode_type t)
  349. {
  350. if(mt->sp < (MENU_STACK_DEPTH - 1))
  351. {
  352. mt->sp++;
  353. mt->stack[mt->sp].n = p;
  354. mt->stack[mt->sp].expect = t;
  355. mt->stack[mt->sp].tail = NULL;
  356. return 0;
  357. }
  358. return -1;
  359. }
  360. //This function takes the attribute list in the libExpat style and allocates a local copy to live
  361. //in the menu node (unfolding the alternate index key/value scheme in the process)
  362. static int gather_args(menunode *p, const char **args)
  363. {
  364. int count = 0;
  365. int i;
  366. if(p == NULL)
  367. return -1;
  368. if(args == NULL)
  369. return 0;
  370. count = 0;
  371. while(args[count * 2])
  372. {
  373. count++;
  374. }
  375. p->keys = count;
  376. if(count == 0)
  377. {
  378. return 0;
  379. }
  380. p->key = (char **)malloc(sizeof(char *) * count);
  381. if(p->key == NULL)
  382. {
  383. p->keys = 0;
  384. fprintf(stderr,"Cannot allocate memory for key list!\n");
  385. return -1;
  386. }
  387. p->value = (char **)malloc(sizeof(char *) * count);
  388. if(p->key == NULL)
  389. {
  390. free(p->key);
  391. p->key = NULL;
  392. p->keys = 0;
  393. fprintf(stderr,"Cannot allocate memory for value list!\n");
  394. return -1;
  395. }
  396. for(i=0; i < count; i++)
  397. {
  398. p->key[i] = strdup(args[i * 2]);
  399. if(p->key[i] == NULL)
  400. {
  401. int j;
  402. for(j=0; j < i; j++)
  403. {
  404. free(p->key[j]);
  405. free(p->value[j]);
  406. }
  407. free(p->key);
  408. free(p->value);
  409. p->key = NULL;
  410. p->value = NULL;
  411. p->keys = 0;
  412. fprintf(stderr,"Cannot allocate memory for key!\n");
  413. return -1;
  414. }
  415. p->value[i] = strdup(args[(i * 2) + 1]);
  416. if(p->value[i] == NULL)
  417. {
  418. int j;
  419. for(j=0; j < i; j++)
  420. {
  421. free(p->key[j]);
  422. free(p->value[j]);
  423. }
  424. free(p->key[i]);
  425. free(p->key);
  426. free(p->value);
  427. p->key = NULL;
  428. p->value = NULL;
  429. p->keys = 0;
  430. fprintf(stderr,"Cannot allocate memory for value!\n");
  431. return -1;
  432. }
  433. }
  434. return 0;
  435. }
  436. static menunode *alloc_menunode(menunode_type t)
  437. {
  438. menunode *r;
  439. r = (menunode *)malloc(sizeof(menunode));
  440. if(r == NULL)
  441. {
  442. return NULL;
  443. }
  444. memset(r, 0, sizeof(menunode));
  445. r->type = t;
  446. return r;
  447. }
  448. #define SET_WIDGET_BINARY_FLAG(str, flag) \
  449. switch((str)[0]) \
  450. { \
  451. case '1': \
  452. case 'Y': \
  453. case 'y': \
  454. r->wid.flags |= (flag); \
  455. break; \
  456. \
  457. case '0': \
  458. case 'N': \
  459. case 'n': \
  460. r->wid.flags &= ~(flag); \
  461. break; \
  462. \
  463. default: \
  464. break; \
  465. } \
  466. //----------------------------------------------
  467. //Go and look through a list of text attributes and extract common ones into packed binary data
  468. //structures for quick widget drawing at runtime...
  469. static int extract_common_widget_attributes(menunode *r)
  470. {
  471. if(!r)
  472. return -1;
  473. int j,k;
  474. for(j = 0; j < r->keys; j++)
  475. {
  476. if(!strcasecmp(r->key[j], "x"))
  477. {
  478. r->wid.x = atoi(r->value[j]);
  479. }
  480. else if(!strcasecmp(r->key[j], "y"))
  481. {
  482. r->wid.y = atoi(r->value[j]);
  483. }
  484. else if(!strcasecmp(r->key[j], "width") || !strcasecmp(r->key[j], "w"))
  485. {
  486. r->wid.width = atoi(r->value[j]);
  487. }
  488. else if(!strcasecmp(r->key[j], "height") || !strcasecmp(r->key[j], "h"))
  489. {
  490. r->wid.height = atoi(r->value[j]);
  491. }
  492. else if(!strcasecmp(r->key[j], "font"))
  493. {
  494. for(k=0; k < NUM_FONTS; k++)
  495. {
  496. if(!strcasecmp(r->value[j], font_words[k]))
  497. {
  498. r->wid.font = k;
  499. break;
  500. }
  501. }
  502. }
  503. else if(!strcasecmp(r->key[j], "label") || !strcasecmp(r->key[j], "text"))
  504. {
  505. r->wid.text = r->value[j];
  506. }
  507. else if(!strcasecmp(r->key[j], "color"))
  508. {
  509. r->wid.color = string_to_color(r->value[j]);
  510. }
  511. else if(!strcasecmp(r->key[j], "bgcolor"))
  512. {
  513. r->wid.bgcolor = string_to_color(r->value[j]);
  514. }
  515. else if(!strcasecmp(r->key[j], "textcolor"))
  516. {
  517. r->wid.textcolor = string_to_color(r->value[j]);
  518. }
  519. else if(!strcasecmp(r->key[j], "hotcolor"))
  520. {
  521. r->wid.hotcolor = string_to_color(r->value[j]);
  522. }
  523. else if(!strcasecmp(r->key[j], "disabled"))
  524. {
  525. SET_WIDGET_BINARY_FLAG(r->value[j], WF_DISABLED);
  526. }
  527. else if(!strcasecmp(r->key[j], "hidden"))
  528. {
  529. SET_WIDGET_BINARY_FLAG(r->value[j], WF_DONTDRAW);
  530. }
  531. else if(!strcasecmp(r->key[j], "shadow"))
  532. {
  533. SET_WIDGET_BINARY_FLAG(r->value[j], WF_SHADOW);
  534. }
  535. else if(!strcasecmp(r->key[j], "password"))
  536. {
  537. SET_WIDGET_BINARY_FLAG(r->value[j], WF_PASSWORD);
  538. }
  539. }
  540. return 0;
  541. }
  542. //Do the same for actions
  543. static int extract_common_action_attributes(menunode *r)
  544. {
  545. if(!r)
  546. return -1;
  547. int j;
  548. for(j = 0; j < r->keys; j++)
  549. {
  550. if(!strcasecmp(r->key[j], "limit"))
  551. {
  552. r->act.limit = atoi(r->value[j]);
  553. }
  554. else if(!strcasecmp(r->key[j], "var") || !strcasecmp(r->key[j], "variable"))
  555. {
  556. r->act.var = r->value[j];
  557. }
  558. else if(!strcasecmp(r->key[j], "value"))
  559. {
  560. r->act.value = r->value[j];
  561. }
  562. else if(!strcasecmp(r->key[j], "name"))
  563. {
  564. r->act.name = r->value[j];
  565. }
  566. }
  567. return 0;
  568. }
  569. static void XMLCALL start_tag(void *data, const char *element, const char **attrib)
  570. {
  571. menutree *mt = (menutree *)data;
  572. menunode *r = NULL;
  573. if(mt->user_error)
  574. return;
  575. int i;
  576. // printf("[%d] BEGIN %s\n", mt->sp, element);
  577. if(mt->parse_flags & PARSE_IN_CLASS_TAG)
  578. {
  579. fprintf(stderr, "%d: <class> tag may not have any children!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  580. mt->user_error = 1;
  581. return;
  582. }
  583. switch(mt->stack[mt->sp].expect)
  584. {
  585. case NODE_ROOT:
  586. if(!strcasecmp(element, "driver_ui"))
  587. {
  588. r = alloc_menunode(NODE_ROOT);
  589. if(r)
  590. {
  591. if(gather_args(r, attrib))
  592. {
  593. free(r);
  594. fprintf(stderr, "%d: Error loading attributes!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  595. mt->user_error = 1;
  596. return;
  597. }
  598. append_node(mt, r);
  599. push_menustack(mt, r, NODE_MENU);
  600. }
  601. }
  602. else
  603. {
  604. fprintf(stderr, "%d: Expecting root tag of <driver_ui>\n", (int)XML_GetCurrentLineNumber(mt->parser));
  605. mt->user_error = 1;
  606. }
  607. break;
  608. case NODE_MENU:
  609. if(!strcasecmp(element, "menu"))
  610. {
  611. r = alloc_menunode(NODE_MENU);
  612. if(r)
  613. {
  614. if(gather_args(r, attrib))
  615. {
  616. free(r);
  617. fprintf(stderr, "%d: Error loading attributes!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  618. mt->user_error = 1;
  619. return;
  620. }
  621. append_node(mt, r);
  622. push_menustack(mt, r, NODE_WIDGET);
  623. }
  624. }
  625. else if(!strcasecmp(element, "class"))
  626. {
  627. r = alloc_menunode(NODE_CLASS);
  628. if(r)
  629. {
  630. if(gather_args(r, attrib))
  631. {
  632. free(r);
  633. fprintf(stderr, "%d: Error loading attributes!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  634. mt->user_error = 1;
  635. return;
  636. }
  637. extract_common_widget_attributes(r);
  638. r->next = mt->classlist;
  639. mt->classlist = r;
  640. mt->parse_flags |= PARSE_IN_CLASS_TAG;
  641. return;
  642. }
  643. }
  644. else
  645. {
  646. fprintf(stderr, "%d: Expecting <class/> or <menu> tag!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  647. mt->user_error = 1;
  648. }
  649. break;
  650. case NODE_ACTION:
  651. for(i = 0; i < NUM_ACTIONS; i++)
  652. {
  653. if(!strcasecmp(element, action_words[i]))
  654. {
  655. r = alloc_menunode(NODE_ACTION);
  656. if(r)
  657. {
  658. if(gather_args(r, attrib))
  659. {
  660. free(r);
  661. fprintf(stderr, "%d: Error loading attributes!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  662. mt->user_error = 1;
  663. return;
  664. }
  665. r->act.type = i;
  666. extract_common_action_attributes(r);
  667. append_node(mt, r);
  668. push_menustack(mt, r, NODE_ACTION);
  669. return;
  670. }
  671. else
  672. {
  673. fprintf(stderr, "%d: Out of memory!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  674. mt->user_error = 1;
  675. return;
  676. }
  677. }
  678. }
  679. fprintf(stderr, "%d: Expecting action tag!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  680. mt->user_error = 1;
  681. break;
  682. case NODE_WIDGET:
  683. for(i = 0; i < NUM_WIDGETS; i++)
  684. {
  685. if(!strcasecmp(element, widget_words[i]))
  686. {
  687. r = alloc_menunode(NODE_WIDGET);
  688. if(r)
  689. {
  690. int j;
  691. if(gather_args(r, attrib))
  692. {
  693. free(r);
  694. fprintf(stderr, "%d: Error loading attributes!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  695. mt->user_error = 1;
  696. return;
  697. }
  698. //Scan the attribute list for a widget class specifier to pick defaults from
  699. for(j = 0; j < r->keys; j++)
  700. {
  701. if(!strcasecmp(r->key[j], "class")) //if we have a class specifier
  702. {
  703. menunode *pp = mt->classlist; //start looking through the class pist
  704. while(pp)
  705. {
  706. if(attr_match(pp, "name", r->value[j])) //If the name of this class matches our class request
  707. {
  708. //copy the attribues from the class to our widget
  709. memcpy(&r->wid, &pp->wid, sizeof(struct widget_struct));
  710. break; //break the while(pp)
  711. }
  712. pp = pp->next;
  713. }
  714. if(pp == NULL) //if we exhausted the list but found no match, complain bitterly...
  715. {
  716. fprintf(stderr, "%d: Widget invokes unknown class \"%s\"!\n", (int)XML_GetCurrentLineNumber(mt->parser), r->value[j]);
  717. mt->user_error = 1;
  718. }
  719. break;
  720. }
  721. }
  722. //Now that the widget class data have been copied, it's safe to override the type to our actual type.
  723. r->wid.type = i;
  724. extract_common_widget_attributes(r);
  725. append_node(mt, r);
  726. push_menustack(mt, r, NODE_ACTION);
  727. return;
  728. }
  729. else
  730. {
  731. fprintf(stderr, "%d: Out of memory!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  732. mt->user_error = 1;
  733. return;
  734. }
  735. }
  736. }
  737. fprintf(stderr, "%d: Expecting widget tag!\n", (int)XML_GetCurrentLineNumber(mt->parser));
  738. mt->user_error = 1;
  739. break;
  740. default:
  741. break;
  742. }
  743. }
  744. static void XMLCALL end_tag(void *data, const char *element)
  745. {
  746. menutree *mt = (menutree *)data;
  747. if(mt->user_error)
  748. return;
  749. // printf("[%d] END %s\n", mt->sp, element);
  750. if(!strcasecmp(element,"class")) //if this is the tail end of a <class/> tag, clear the in_class flag
  751. {
  752. mt->parse_flags &= ~PARSE_IN_CLASS_TAG;
  753. }
  754. else //otherwise, pop the menu stack
  755. {
  756. mt->sp--;
  757. }
  758. }
  759. void free_menunode(menunode *r)
  760. {
  761. int i;
  762. if(r->subtree)
  763. {
  764. free_menunode(r->subtree);
  765. }
  766. if(r->next)
  767. {
  768. free_menunode(r->next);
  769. }
  770. if(r->key)
  771. {
  772. for(i=0; i < r->keys; i++)
  773. {
  774. if(r->key[i])
  775. {
  776. free(r->key[i]);
  777. }
  778. }
  779. free(r->key);
  780. }
  781. if(r->value)
  782. {
  783. for(i=0; i < r->keys; i++)
  784. {
  785. if(r->value[i])
  786. {
  787. free(r->value[i]);
  788. }
  789. }
  790. free(r->value);
  791. }
  792. free(r);
  793. }
  794. void free_menuvars(menuvar *v)
  795. {
  796. menuvar *p, *q;
  797. p = v;
  798. q = NULL;
  799. while(p)
  800. {
  801. q = p;
  802. p = p->next;
  803. free(q);
  804. }
  805. }
  806. void free_menutree(menutree *mt)
  807. {
  808. if(!mt)
  809. return;
  810. driver_logout();
  811. if(mt->head)
  812. {
  813. free_menunode(mt->head);
  814. }
  815. if(mt->classlist)
  816. {
  817. free_menunode(mt->classlist);
  818. }
  819. if(mt->menuvars)
  820. {
  821. free_menuvars(mt->menuvars);
  822. }
  823. free(mt);
  824. }
  825. menutree *load_menutree(char *fname)
  826. {
  827. menutree *mt = NULL;
  828. FILE *f = NULL;
  829. char chunk[XML_CHUNK_SIZE] = {0};
  830. int len = 0;
  831. int done = 0;
  832. XML_Parser p = NULL;
  833. if(!fname)
  834. return NULL;
  835. f = fopen(fname, "rb");
  836. if(!f)
  837. {
  838. fprintf(stderr, "Cannot read menutree file %s!\n", fname);
  839. return NULL;
  840. }
  841. p = XML_ParserCreate(NULL);
  842. if(!p)
  843. {
  844. fprintf(stderr,"No memory for parser!\n");
  845. fclose(f);
  846. return NULL;
  847. }
  848. XML_SetElementHandler(p, start_tag, end_tag);
  849. mt = (menutree *)malloc(sizeof(menutree));
  850. if(!mt)
  851. {
  852. fprintf(stderr, "Cannot allocate memory for menutree!\n");
  853. fclose(f);
  854. XML_ParserFree(p);
  855. return NULL;
  856. }
  857. memset(mt, 0, sizeof(menutree));
  858. XML_SetUserData(p, (void *)mt);
  859. mt->parser = p;
  860. while(!done)
  861. {
  862. len = fread(chunk, 1, XML_CHUNK_SIZE, f);
  863. if(feof(f))
  864. {
  865. done = 1;
  866. }
  867. if( XML_Parse(p, chunk, len, done) == XML_STATUS_ERROR )
  868. {
  869. fprintf(stderr, "Parse error at line %d: %s\n", (int)XML_GetCurrentLineNumber(p), XML_ErrorString(XML_GetErrorCode(p)));
  870. fclose(f);
  871. XML_ParserFree(p);
  872. free_menutree(mt);
  873. return NULL;
  874. }
  875. if(mt->user_error)
  876. {
  877. fclose(f);
  878. XML_ParserFree(p);
  879. free_menutree(mt);
  880. return NULL;
  881. }
  882. }
  883. fclose(f);
  884. XML_ParserFree(p);
  885. mt->parser = NULL;
  886. return mt;
  887. }
  888. void print_menu_tree_inner(menunode *p, int indent)
  889. {
  890. int i;
  891. for(i=0; i < indent; i++)
  892. {
  893. printf(" ");
  894. }
  895. printf("[BEGIN %s]", nodetype_words[p->type]);
  896. switch(p->type)
  897. {
  898. case NODE_ACTION:
  899. printf(" (%s)", action_words[p->act.type]);
  900. break;
  901. case NODE_WIDGET:
  902. printf(" (%s)", widget_words[p->wid.type]);
  903. break;
  904. default:
  905. break;
  906. }
  907. for(i = 0; i < p->keys; i++)
  908. {
  909. printf(" %s=\"%s\"", p->key[i], p->value[i]);
  910. }
  911. printf("\n");
  912. if(p->subtree)
  913. {
  914. print_menu_tree_inner(p->subtree, indent + 1);
  915. }
  916. for(i=0; i < indent; i++)
  917. {
  918. printf(" ");
  919. }
  920. printf("[END %s]\n", nodetype_words[p->type]);
  921. if(p->next)
  922. {
  923. print_menu_tree_inner(p->next, indent);
  924. }
  925. }
  926. void print_widget_class_list(menunode *classes)
  927. {
  928. menunode *p = classes;
  929. int i;
  930. while(p)
  931. {
  932. if(p->type != NODE_CLASS)
  933. {
  934. printf("\tExpecting widget class...\n");
  935. p = p->next;
  936. continue;
  937. }
  938. printf("\t");
  939. for(i = 0; i < p->keys; i++)
  940. {
  941. printf(" %s=\"%s\"", p->key[i], p->value[i]);
  942. }
  943. printf("\n");
  944. p = p->next;
  945. }
  946. }
  947. void print_menu_tree(menutree *mt)
  948. {
  949. printf("Widget Classes:\n");
  950. print_widget_class_list(mt->classlist);
  951. printf("\nMenu Definitions:\n");
  952. print_menu_tree_inner(mt->head, 0);
  953. }
  954. void do_menu_action(menutree *mt, menunode *act);
  955. int setmenu(menutree *mt, char *name)
  956. {
  957. menunode *p;
  958. if(!mt)
  959. return -1;
  960. p = mt->head->subtree;
  961. while(p)
  962. {
  963. if(attr_match(p, "name", name))
  964. {
  965. mt->current = p;
  966. p = p->subtree;
  967. while(p)
  968. {
  969. if( (p->type == NODE_WIDGET) && (p->wid.type == WIDGET_MENUINIT) )
  970. {
  971. do_menu_action(mt, p->subtree);
  972. }
  973. p = p->next;
  974. }
  975. return 0;
  976. }
  977. p = p->next;
  978. }
  979. return -1;
  980. }
  981. int init_menutree(menutree *mt)
  982. {
  983. char *start_menu;
  984. if(!mt)
  985. {
  986. return -1;
  987. }
  988. if(!mt->head)
  989. {
  990. return -1;
  991. }
  992. if(!mt->head->subtree)
  993. {
  994. return -1;
  995. }
  996. start_menu = get_attr_val(mt->head, "startup");
  997. if(setmenu(mt, start_menu))
  998. {
  999. mt->current = mt->head->subtree;
  1000. }
  1001. return 0;
  1002. }
  1003. int make_paddle_request(int paddle, char *okmenu, char *failmenu)
  1004. {
  1005. struct message_record outgoing_msg;
  1006. paddle_req.result = 0;
  1007. paddle_req.request = paddle;
  1008. paddle_req_timeout = time(NULL) + PADDLE_REQ_TIMEOUT;
  1009. paddle_ok_menu = okmenu;
  1010. paddle_fail_menu = failmenu;
  1011. prepare_message(&outgoing_msg, MAILBOX_SET_PADDLE, &paddle_req, sizeof(paddle_req));
  1012. return send_message(hub_fd, &outgoing_msg);
  1013. }
  1014. int check_paddle_request(menutree *mt)
  1015. {
  1016. struct message_record outgoing_msg;
  1017. if(paddle_req_timeout != 0)
  1018. {
  1019. if(paddle_req.request == paddle_req.result)
  1020. {
  1021. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Set Paddle %d\n", paddle_req.request);
  1022. send_message(hub_fd, &outgoing_msg);
  1023. paddle_req_timeout = 0;
  1024. setmenu(mt, paddle_ok_menu);
  1025. return 1;
  1026. }
  1027. else if( (paddle_req.result < 0) || (paddle_req_timeout < time(NULL)) )
  1028. {
  1029. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Failed to set Paddle %d\n", paddle_req.request);
  1030. send_message(hub_fd, &outgoing_msg);
  1031. paddle_req.result = 0;
  1032. paddle_req.request = 0;
  1033. paddle_req_timeout = 0;
  1034. setmenu(mt, paddle_fail_menu);
  1035. return 1;
  1036. }
  1037. }
  1038. return 0;
  1039. }
  1040. void action_nextstop()
  1041. {
  1042. struct message_record outgoing_msg;
  1043. prepare_message(&outgoing_msg, MAILBOX_NEXT_STOP, "", 0);
  1044. send_message(hub_fd, &outgoing_msg);
  1045. }
  1046. void action_prevstop()
  1047. {
  1048. struct message_record outgoing_msg;
  1049. prepare_message(&outgoing_msg, MAILBOX_PREV_STOP, "", 0);
  1050. send_message(hub_fd, &outgoing_msg);
  1051. }
  1052. int draw_menu(menutree *mt)
  1053. {
  1054. menunode *p;
  1055. if(!mt->current)
  1056. {
  1057. return -1;
  1058. }
  1059. p = mt->current->subtree;
  1060. int x, y;
  1061. int xx, yy;
  1062. int tmp_flags;
  1063. set_rawcolor(FBCOLOR_WHITE);
  1064. cls();
  1065. while(p)
  1066. {
  1067. if(p->type != NODE_WIDGET)
  1068. {
  1069. fprintf(stderr, "Expecting Widget!\n");
  1070. print_menu_tree_inner(p, 2);
  1071. p = p->next;
  1072. continue;
  1073. }
  1074. if(p->wid.flags & WF_DONTDRAW)
  1075. {
  1076. p = p->next;
  1077. continue;
  1078. }
  1079. tmp_flags = p->wid.flags;
  1080. if(p == mt->pressed)
  1081. {
  1082. tmp_flags = mt->pressed_flags;
  1083. }
  1084. x = p->wid.x * GRID;
  1085. y = p->wid.y * GRID;
  1086. xx = (p->wid.x + p->wid.width) * GRID;
  1087. yy = (p->wid.y + p->wid.height) * GRID;
  1088. switch(p->wid.type)
  1089. {
  1090. case WIDGET_BUTTON:
  1091. if(tmp_flags & WF_SHADOW)
  1092. {
  1093. select_blend_mode(BLENDMODE_LIGHT);
  1094. set_rawcolor(FBCOLOR_BLACK | FBCOLOR_TRANSLUCENT);
  1095. if(tmp_flags & WF_PRESSED)
  1096. {
  1097. set_pos(x + SHADOW_OFFSET_DOWN, y + SHADOW_OFFSET_DOWN);
  1098. box_fill(xx + SHADOW_OFFSET_DOWN, yy + SHADOW_OFFSET_DOWN);
  1099. }
  1100. else
  1101. {
  1102. set_pos(x + SHADOW_OFFSET_UP, y + SHADOW_OFFSET_UP);
  1103. box_fill(xx + SHADOW_OFFSET_UP, yy + SHADOW_OFFSET_UP);
  1104. }
  1105. select_blend_mode(BLENDMODE_DARK);
  1106. }
  1107. if(tmp_flags & WF_PRESSED)
  1108. {
  1109. set_rawcolor(p->wid.hotcolor);
  1110. }
  1111. else
  1112. {
  1113. set_rawcolor(p->wid.color);
  1114. }
  1115. set_pos(x, y);
  1116. box_fill(xx, yy);
  1117. set_rawcolor(FBCOLOR_BLACK);
  1118. set_pos(x, y);
  1119. box(xx, yy);
  1120. if(p->wid.text)
  1121. {
  1122. set_clip_rect(x + 1, y + 1, xx, yy);
  1123. int strw, strh;
  1124. int dx, dy;
  1125. set_font(p->wid.font);
  1126. font_cell_size(&strw, &strh);
  1127. strw *= strlen(p->wid.text);
  1128. dx = ((xx - x) - strw) / 2;
  1129. dy = ((yy - y) - strh) / 2;
  1130. set_pos(x + dx, y + dy);
  1131. set_rawcolor(p->wid.textcolor);
  1132. print_string(p->wid.text, 0);
  1133. reset_clip_rect();
  1134. }
  1135. break;
  1136. case WIDGET_TEXT:
  1137. {
  1138. char *label = (p->wid.text)?p->wid.text:"";
  1139. set_pos(x, y);
  1140. set_font(p->wid.font);
  1141. set_rawcolor(p->wid.textcolor);
  1142. print_string(label, 0);
  1143. }
  1144. break;
  1145. case WIDGET_BOX:
  1146. set_pos(x, y);
  1147. set_rawcolor(p->wid.color);
  1148. box(xx, yy);
  1149. break;
  1150. case WIDGET_FILLEDBOX:
  1151. set_pos(x, y);
  1152. set_rawcolor(p->wid.color);
  1153. box_fill(xx, yy);
  1154. break;
  1155. case WIDGET_LINE:
  1156. set_pos(x, y);
  1157. set_rawcolor(p->wid.color);
  1158. line_to(xx, yy);
  1159. break;
  1160. case WIDGET_MESSAGES:
  1161. {
  1162. int charw, charh;
  1163. int numlines;
  1164. int idx;
  1165. int dy;
  1166. int numchars;
  1167. int splitchars;
  1168. char tmptext[DIU_MESSAGE_CHARS];
  1169. set_rawcolor(p->wid.color);
  1170. set_pos(x, y);
  1171. box_fill(xx, yy);
  1172. set_rawcolor(FBCOLOR_BLACK);
  1173. set_pos(x, y);
  1174. box(xx, yy);
  1175. set_clip_rect(x + 1, y + 1, xx, yy);
  1176. set_font(p->wid.font);
  1177. font_cell_size(&charw, &charh);
  1178. numlines = ((p->wid.height * GRID) - 2) / charh;
  1179. numchars = ((p->wid.width * GRID) - 2) / charw;
  1180. splitchars = numchars - 5;
  1181. idx = diu_message_head - 1;
  1182. if(idx < 0) idx += DIU_MESSAGE_LINES;
  1183. dy = (yy - 1) - charh;
  1184. while(numlines)
  1185. {
  1186. // printf("dy = %d, idx = %d, numlines = %d\n", dy, idx, numlines);
  1187. if(diu_messages[idx].message[0])
  1188. {
  1189. set_rawcolor(diu_messages[idx].bgcolor);
  1190. set_pos(x+1, dy);
  1191. box_fill(xx - 1, dy + charh);
  1192. set_pos(x+1, dy);
  1193. set_rawcolor(diu_messages[idx].textcolor);
  1194. snprintf(tmptext, DIU_MESSAGE_CHARS, "%-*.*s (%02d)", splitchars, splitchars, diu_messages[idx].message, diu_messages[idx].num);
  1195. print_string(tmptext, 0);
  1196. // printf("Printed \"%s\" at %d,%d\n", tmptext, x+1, dy);
  1197. }
  1198. dy -= charh;
  1199. numlines--;
  1200. idx--;
  1201. if(idx < 0) idx += DIU_MESSAGE_LINES;
  1202. }
  1203. reset_clip_rect();
  1204. }
  1205. break;
  1206. case WIDGET_INPUT:
  1207. if(tmp_flags & WF_SHADOW)
  1208. {
  1209. select_blend_mode(BLENDMODE_LIGHT);
  1210. set_rawcolor(FBCOLOR_BLACK | FBCOLOR_TRANSLUCENT);
  1211. if(tmp_flags & WF_PRESSED)
  1212. {
  1213. set_pos(x + SHADOW_OFFSET_DOWN, y + SHADOW_OFFSET_DOWN);
  1214. box_fill(xx + SHADOW_OFFSET_DOWN, yy + SHADOW_OFFSET_DOWN);
  1215. }
  1216. else
  1217. {
  1218. set_pos(x + SHADOW_OFFSET_UP, y + SHADOW_OFFSET_UP);
  1219. box_fill(xx + SHADOW_OFFSET_UP, yy + SHADOW_OFFSET_UP);
  1220. }
  1221. select_blend_mode(BLENDMODE_DARK);
  1222. }
  1223. set_rawcolor(p->wid.color);
  1224. set_pos(x, y);
  1225. box_fill(xx, yy);
  1226. set_rawcolor(FBCOLOR_BLACK);
  1227. set_pos(x, y);
  1228. box(xx, yy);
  1229. {
  1230. char *varname;
  1231. char *content;
  1232. char *label;
  1233. int strw, strh;
  1234. int dx, dy;
  1235. label = (p->wid.text)?p->wid.text:"";
  1236. varname = get_attr_val(p, "var");
  1237. if(varname == NULL)
  1238. {
  1239. fprintf(stderr, "Input box lacks content variable!\n");
  1240. fflush(stderr);
  1241. content = "";
  1242. }
  1243. else
  1244. {
  1245. content = menuvar_getval(mt, varname);
  1246. if(content == NULL)
  1247. {
  1248. content = "";
  1249. }
  1250. }
  1251. set_clip_rect(x + 1, y + 1, xx, yy);
  1252. set_font(p->wid.font);
  1253. font_cell_size(&dx, &strh);
  1254. strw = dx * strlen(label);
  1255. dy = ((yy - y) - strh) / 2;
  1256. set_pos(x + dx, y + dy);
  1257. set_rawcolor(p->wid.textcolor);
  1258. print_string(label, 0);
  1259. dx += strw;
  1260. if(p->wid.flags & WF_PASSWORD)
  1261. {
  1262. int n;
  1263. int i;
  1264. char crapval[MENUVAR_VALUELEN] = {0};
  1265. n = strlen(content);
  1266. if(n >= MENUVAR_VALUELEN)
  1267. {
  1268. n = MENUVAR_VALUELEN - 1;
  1269. }
  1270. for(i=0; i < n; i++)
  1271. {
  1272. crapval[i]='*';
  1273. }
  1274. set_pos(x + dx, y + dy);
  1275. print_string(crapval, 0);
  1276. }
  1277. else
  1278. {
  1279. set_pos(x + dx, y + dy);
  1280. print_string(content, 0);
  1281. }
  1282. reset_clip_rect();
  1283. }
  1284. break;
  1285. case WIDGET_STATUSBAR:
  1286. {
  1287. char statline[DIU_MESSAGE_CHARS];
  1288. char date_str[32];
  1289. time_t t;
  1290. struct tm tm;
  1291. switch(pass_stat.flush_status)
  1292. {
  1293. case FLUSH_STATUS_DOWNLOAD:
  1294. snprintf(statline, DIU_MESSAGE_CHARS, "DB Flush Downloading %d%%", pass_stat.progress_indicator);
  1295. break;
  1296. case FLUSH_STATUS_APPLY:
  1297. snprintf(statline, DIU_MESSAGE_CHARS, "DB Flush Applying %d%%", pass_stat.progress_indicator);
  1298. break;
  1299. case FLUSH_STATUS_WRITE:
  1300. snprintf(statline, DIU_MESSAGE_CHARS, "Saving New Database. Please Wait.");
  1301. break;
  1302. default:
  1303. /*
  1304. snprintf(statline, DIU_MESSAGE_CHARS, "Rte %d Trip %d Stop %d GPS %-3s Tun %-3s",
  1305. stop_stat.route, stop_stat.trip, stop_stat.stop,
  1306. gps_stat.gps_good?"YES":"NO", tunnel_is_up()?"YES":"NO");
  1307. */
  1308. t = time(NULL);
  1309. localtime_r(&t, &tm);
  1310. strftime(date_str, 32, "%Y-%m-%d", &tm);
  1311. snprintf(statline, DIU_MESSAGE_CHARS, "Rt %d Trp %d Stop %d GPS %-3s Tun %-3s %s",
  1312. stop_stat.route, stop_stat.trip, stop_stat.stop,
  1313. gps_stat.gps_good?"YES":"NO", tunnel_is_up()?"YES":"NO",
  1314. date_str);
  1315. break;
  1316. }
  1317. set_pos(x, y);
  1318. set_rawcolor(p->wid.color);
  1319. box_fill(xx,yy);
  1320. set_clip_rect(x + 1, y + 1, xx, yy);
  1321. set_pos(x, y);
  1322. set_rawcolor(p->wid.textcolor);
  1323. set_font(p->wid.font);
  1324. print_string(statline, 0);
  1325. reset_clip_rect();
  1326. }
  1327. break;
  1328. #define MAX_PKGS (16)
  1329. case WIDGET_FULLSTATUS:
  1330. {
  1331. int i;
  1332. int npkgs;
  1333. FILE *f;
  1334. char statline[DIU_MESSAGE_CHARS];
  1335. package_signature pkgs[MAX_PKGS];
  1336. int dy = 0, dx = 0;
  1337. int cellwidth, cellheight;
  1338. struct tm pkgtime;
  1339. set_font(p->wid.font);
  1340. set_rawcolor(FBCOLOR_BLACK);
  1341. set_pos(x, y);
  1342. box(xx,yy);
  1343. set_clip_rect(x + 1, y + 1, xx, yy);
  1344. set_pos(x, y);
  1345. font_cell_size(&cellwidth, &cellheight);
  1346. dx = cellwidth >> 1;
  1347. dy = cellheight >> 1;
  1348. snprintf(statline, DIU_MESSAGE_CHARS, "Tunnel: %-3s GPRS: %-3s Eq# %d #Msg: %d", tunnel_is_up()?"YES":"NO", gprs_is_up()?"YES":"NO", get_equip_num(), bill_stat.unsynced_messages);
  1349. set_pos(x + dx, y + dy);
  1350. print_string(statline, 0);
  1351. dy += cellheight;
  1352. snprintf(statline, DIU_MESSAGE_CHARS, "Last Token Read (%02d): \"%s\"", token_diag_serial % 100, token_diag_string);
  1353. set_pos(x + dx, y + dy);
  1354. print_string(statline, 0);
  1355. dy += cellheight;
  1356. if(gps_stat.gps_good)
  1357. {
  1358. snprintf(statline, DIU_MESSAGE_CHARS, "GPS: nSat = %d Loc = (%11.4f,%11.4f)", gps_stat.num_sats, gps_stat.lat, gps_stat.lon);
  1359. }
  1360. else
  1361. {
  1362. snprintf(statline, DIU_MESSAGE_CHARS, "GPS: NO / Stale");
  1363. }
  1364. set_pos(x + dx, y + dy);
  1365. print_string(statline, 0);
  1366. dy += cellheight;
  1367. dy += cellheight;
  1368. snprintf(statline, DIU_MESSAGE_CHARS, "Package Version Installed");
  1369. set_pos(x + dx, y + dy);
  1370. print_string(statline, 0);
  1371. dy += cellheight;
  1372. npkgs = find_packages(pkgs, MAX_PKGS);
  1373. for(i=0; i < npkgs; i++)
  1374. {
  1375. localtime_r(&pkgs[i].installed, &pkgtime);
  1376. snprintf(statline, DIU_MESSAGE_CHARS, "%-8s %-16s %02d/%02d/%02d %02d:%02d:%02d",
  1377. pkgs[i].pkgname, pkgs[i].pkgver,
  1378. pkgtime.tm_mon + 1, pkgtime.tm_mday, pkgtime.tm_year + 1900,
  1379. pkgtime.tm_hour, pkgtime.tm_min, pkgtime.tm_sec);
  1380. set_pos(x + dx, y + dy);
  1381. print_string(statline, 0);
  1382. dy += cellheight;
  1383. }
  1384. f = fopen("/tmp/net_ids", "rb");
  1385. if(f)
  1386. {
  1387. dy += cellheight;
  1388. while(fgets(statline, DIU_MESSAGE_CHARS, f))
  1389. {
  1390. set_pos(x + dx, y + dy);
  1391. strip_crlf(statline);
  1392. print_string(statline, 0);
  1393. dy += cellheight;
  1394. }
  1395. fclose(f);
  1396. }
  1397. reset_clip_rect();
  1398. }
  1399. break;
  1400. default:
  1401. break;
  1402. }
  1403. p = p->next;
  1404. }
  1405. present_framebuffer();
  1406. return 0;
  1407. }
  1408. void do_menu_action(menutree *mt, menunode *act)
  1409. {
  1410. menunode *p = act;
  1411. while(p)
  1412. {
  1413. if(p->type != NODE_ACTION)
  1414. {
  1415. fprintf(stderr, "Expecting Action!\n");
  1416. print_menu_tree_inner(p, 2);
  1417. p = p->next;
  1418. break;
  1419. }
  1420. // fprintf(stderr,"Action: %d\n", p->act.type);
  1421. // fflush(stderr);
  1422. switch(p->act.type)
  1423. {
  1424. case ACTION_SETMENU:
  1425. if(p->act.name)
  1426. {
  1427. setmenu(mt, p->act.name);
  1428. }
  1429. else
  1430. {
  1431. fprintf(stderr, "setmenu expects name=\"...\"\n");
  1432. }
  1433. break;
  1434. case ACTION_SETVAR:
  1435. if(p->act.var == NULL)
  1436. {
  1437. fprintf(stderr, "setvar expects var=\"...\"\n");
  1438. }
  1439. else
  1440. {
  1441. menuvar_setval(mt, p->act.var, p->act.value, p->act.limit);
  1442. }
  1443. break;
  1444. case ACTION_APPEND:
  1445. if(p->act.var == NULL)
  1446. {
  1447. fprintf(stderr, "append expects var=\"...\"\n");
  1448. }
  1449. else
  1450. {
  1451. menuvar_append(mt, p->act.var, p->act.value, p->act.limit);
  1452. }
  1453. break;
  1454. case ACTION_BKSPACE:
  1455. if(p->act.var == NULL)
  1456. {
  1457. fprintf(stderr, "backspace expects var=\"...\"\n");
  1458. }
  1459. else
  1460. {
  1461. menuvar_backspace(mt, p->act.var);
  1462. }
  1463. break;
  1464. case ACTION_LOGIN:
  1465. {
  1466. char *driver_var, *pin_var, *okmenu, *failmenu;
  1467. char *driver_val, *pin_val;
  1468. driver_var = get_attr_val(p, "drivervar");
  1469. pin_var = get_attr_val(p, "pinvar");
  1470. okmenu = get_attr_val(p, "okmenu");
  1471. failmenu = get_attr_val(p, "failmenu");
  1472. if(!driver_var || !pin_var || !okmenu || !failmenu)
  1473. {
  1474. fprintf(stderr, "login expects parameters: drivervar, pinvar, okmenu, failmenu\n");
  1475. }
  1476. else
  1477. {
  1478. driver_val = menuvar_getval(mt, driver_var);
  1479. pin_val = menuvar_getval(mt, pin_var);
  1480. if(!driver_val || !pin_val)
  1481. {
  1482. setmenu(mt, failmenu);
  1483. return;
  1484. }
  1485. else
  1486. {
  1487. if(driver_login(atoi(driver_val), pin_val))
  1488. {
  1489. setmenu(mt, failmenu);
  1490. return;
  1491. }
  1492. else
  1493. {
  1494. //If this is the "magic" driver name, go to admin mode menu
  1495. if(!strcmp(my_driver_status.driver_name, MAGIC_DRIVER_NAME))
  1496. {
  1497. //(named by the #define'd value of MACIC_DRIVER_MENU)
  1498. setmenu(mt, MACIC_DRIVER_MENU);
  1499. }
  1500. else
  1501. {
  1502. //otherwise, go the the config-file specified OK menu
  1503. setmenu(mt, okmenu);
  1504. }
  1505. return;
  1506. }
  1507. }
  1508. }
  1509. }
  1510. break;
  1511. case ACTION_SETPADDLE:
  1512. {
  1513. char *paddle_var, *okmenu, *failmenu;
  1514. char *paddle_val;
  1515. paddle_var = get_attr_val(p, "paddlevar");
  1516. okmenu = get_attr_val(p, "okmenu");
  1517. failmenu = get_attr_val(p, "failmenu");
  1518. if(!paddle_var || !okmenu || !failmenu)
  1519. {
  1520. fprintf(stderr, "setpaddle expects parameters: paddlevar, okmenu, failmenu\n");
  1521. }
  1522. else
  1523. {
  1524. paddle_val = menuvar_getval(mt, paddle_var);
  1525. if(!paddle_val || (atoi(paddle_val) <= 0) )
  1526. {
  1527. setmenu(mt, failmenu);
  1528. return;
  1529. }
  1530. else
  1531. {
  1532. make_paddle_request(atoi(paddle_val), okmenu, failmenu);
  1533. }
  1534. }
  1535. }
  1536. break;
  1537. case ACTION_PREVSTOP:
  1538. action_prevstop();
  1539. break;
  1540. case ACTION_NEXTSTOP:
  1541. action_nextstop();
  1542. break;
  1543. case ACTION_LOGOUT:
  1544. driver_logout();
  1545. break;
  1546. case ACTION_RULE:
  1547. {
  1548. struct message_record outgoing;
  1549. driver_rulecall foo = {{0}};
  1550. char *rule_var, *rule_val;
  1551. char *param_var, *param_val;
  1552. rule_var = get_attr_val(p, "rulevar");
  1553. rule_val = get_attr_val(p, "rule");
  1554. if(rule_val)
  1555. {
  1556. if(rule_var)
  1557. {
  1558. fprintf(stderr, "callrule expects rulevar OR rule, not both!\n");
  1559. break;
  1560. }
  1561. }
  1562. else
  1563. {
  1564. if(rule_var)
  1565. {
  1566. rule_val = menuvar_getval(mt, rule_var);
  1567. if(!rule_val)
  1568. {
  1569. fprintf(stderr, "callrule expects non-null value in variable %s specified by rulevar!\n", rule_var);
  1570. break;
  1571. }
  1572. }
  1573. else
  1574. {
  1575. fprintf(stderr, "callrule expects rulevar OR rule!\n");
  1576. break;
  1577. }
  1578. }
  1579. param_var = get_attr_val(p, "paramvar");
  1580. param_val = get_attr_val(p, "param");
  1581. if(param_val)
  1582. {
  1583. if(param_var)
  1584. {
  1585. fprintf(stderr, "callrule expects paramvar OR param, not both!\n");
  1586. break;
  1587. }
  1588. }
  1589. else
  1590. {
  1591. if(param_var)
  1592. {
  1593. param_val = menuvar_getval(mt, param_var);
  1594. if(!param_val)
  1595. {
  1596. param_val = "";
  1597. }
  1598. }
  1599. else
  1600. {
  1601. fprintf(stderr, "callrule expects paramvar OR param!\n");
  1602. break;
  1603. }
  1604. }
  1605. if(hub_fd >= 0)
  1606. {
  1607. strncpy(foo.rulename, rule_val, DRIVER_RULECALL_LEN - 1);
  1608. strncpy(foo.ruleparam, param_val, DRIVER_RULECALL_LEN - 1);
  1609. prepare_message(&outgoing, MAILBOX_RULE_CALL, &foo, sizeof(foo));
  1610. send_message(hub_fd, &outgoing);
  1611. }
  1612. }
  1613. break;
  1614. case ACTION_SETEQNUM:
  1615. {
  1616. char *eqval;
  1617. int retval;
  1618. if(p->act.var)
  1619. {
  1620. eqval = menuvar_getval(mt, p->act.var);
  1621. if(eqval)
  1622. {
  1623. retval = set_equip_num(strtol(eqval, NULL, 10));
  1624. if(retval)
  1625. {
  1626. fprintf(stderr, "Failed to set Equipment Number to %s\n", eqval);
  1627. }
  1628. else
  1629. {
  1630. system("/mnt/data2/bin/showmessage -b 'Restarting...'");
  1631. sleep(1);
  1632. system("/sbin/shutdown -r now");
  1633. sleep(30);
  1634. }
  1635. }
  1636. }
  1637. else
  1638. {
  1639. fprintf(stderr, "Action seteqnum expects: var\n");
  1640. }
  1641. }
  1642. break;
  1643. case ACTION_GETEQNUM:
  1644. {
  1645. char tmp[MENUVAR_VALUELEN];
  1646. int retval;
  1647. retval = get_equip_num();
  1648. snprintf(tmp, MENUVAR_VALUELEN, "%d", retval);
  1649. if(p->act.var)
  1650. {
  1651. menuvar_setval(mt, p->act.var, tmp, p->act.limit);
  1652. }
  1653. else
  1654. {
  1655. fprintf(stderr, "Action geteqnum expects: var\n");
  1656. }
  1657. }
  1658. break;
  1659. case ACTION_CLRMSGS:
  1660. {
  1661. clear_diu_messages();
  1662. }
  1663. break;
  1664. case ACTION_SHELLCALL:
  1665. {
  1666. char tmp[SHELLCALL_BUFFER_SIZE] = {0};
  1667. char varname[MENUVAR_IDENTLEN] = {0};
  1668. char *cmd_val = NULL;
  1669. char *var_val = NULL;
  1670. char *okmenu = NULL;
  1671. char *failmenu = NULL;
  1672. char c;
  1673. int in_idx = 0;
  1674. int out_idx = 0;
  1675. int fmt_mode = 0;
  1676. int fmt_idx = 0;
  1677. int ret = 0;
  1678. int fail_flag = 0;
  1679. //Gather the command line template before we run it through formatting
  1680. cmd_val = get_attr_val(p, "cmdline");
  1681. //Bitch loudly and give up if we don't get it...
  1682. if(!cmd_val)
  1683. {
  1684. fprintf(stderr, "Action shellcall requires: cmdline\n");
  1685. break;
  1686. }
  1687. okmenu = get_attr_val(p, "okmenu");
  1688. failmenu = get_attr_val(p, "failmenu");
  1689. //While we still have format string to process and buffer space to put the result
  1690. while( (c = cmd_val[in_idx]) && (out_idx < (SHELLCALL_BUFFER_SIZE - 1)) )
  1691. {
  1692. if(fmt_mode) //------------------------------------------ FORMAT MODE
  1693. {
  1694. if(c == SHELLCALL_VAR_ESC) //and we just got an escape character to terminate the variable name
  1695. {
  1696. if(fmt_idx > 0) //if we have a non-zero length variable name
  1697. {
  1698. varname[fmt_idx] = '\0'; //nul terminate it
  1699. if( (var_val = menuvar_getval(mt, varname)) ) //and look it up
  1700. {
  1701. //if we find it, tack its value onto our output buffer
  1702. out_idx += snprintf(tmp + out_idx, SHELLCALL_BUFFER_SIZE - 1, "%s", var_val);
  1703. }
  1704. else //if we don't find it, mark failure and break out
  1705. {
  1706. fail_flag = 1;
  1707. fprintf(stderr,"Action shellcall aborted: variable ~%s~ not defined\n", varname);
  1708. break;
  1709. }
  1710. }
  1711. else //If we have a zero-length variable name, that just means we want the escape character
  1712. {
  1713. tmp[out_idx++] = SHELLCALL_VAR_ESC;
  1714. }
  1715. fmt_mode = 0; //Either way, if we were in format mode and got the escape, we're in normal mode now
  1716. }
  1717. else //if we are in format mode and processing a normal character
  1718. {
  1719. if(fmt_idx == (MENUVAR_IDENTLEN - 1)) //make sure we have room for the identifier
  1720. {
  1721. fail_flag = 1;
  1722. varname[MENUVAR_IDENTLEN - 1] = '\0';
  1723. fprintf(stderr,"Action shellcall aborted: variable name too long ~%s...\n", varname);
  1724. break;
  1725. }
  1726. varname[fmt_idx++] = c; //then store the character to the end...
  1727. }
  1728. }
  1729. else //------------------------------------------ NORMAL MODE
  1730. {
  1731. if(c == SHELLCALL_VAR_ESC) //if we're in normal mode and encounter an escape
  1732. {
  1733. fmt_mode = 1; //set escape mode
  1734. fmt_idx = 0; //reset variable name index
  1735. }
  1736. else
  1737. {
  1738. tmp[out_idx++] = c; //copy the character verbatim
  1739. }
  1740. }
  1741. in_idx++; //no matter what, we want to advance to the next format string character
  1742. }
  1743. if(fail_flag) //if we hit a failure during format expansion, bail now...
  1744. {
  1745. break;
  1746. }
  1747. if(fmt_mode)
  1748. {
  1749. varname[fmt_idx] = '\0';
  1750. fprintf(stderr, "Action shellcall aborted: unterminated variable expansion ~%s...\n", varname);
  1751. break;
  1752. }
  1753. //If we've overflowed and constructed a partially formed command line, play it safe and give up now before
  1754. //we blow up the world and poison everybody with radioactive waste.
  1755. if(out_idx >= SHELLCALL_BUFFER_SIZE)
  1756. {
  1757. fprintf(stderr, "Action shellcall aborted: assembled command exceeds %d character limit!\n", SHELLCALL_BUFFER_SIZE);
  1758. break;
  1759. }
  1760. tmp[out_idx] = '\0'; //Slap the terminating nul on the newly minted shell script. Wo0t!
  1761. //Here goes nothing, we make our call to system() here to invoke the shell...
  1762. ret = system(tmp);
  1763. if( WEXITSTATUS(ret) == 0 ) //If the shell return value is 0 (true / success in bash-ese)
  1764. {
  1765. if(okmenu) //and the config file specifies a menu to jump to on success
  1766. {
  1767. setmenu(mt, okmenu); //then by all means do so...
  1768. }
  1769. }
  1770. else //otherwise, it's the same drill with failure
  1771. { //now with 30% MORE FAIL!
  1772. if(failmenu)
  1773. {
  1774. setmenu(mt, failmenu);
  1775. }
  1776. }
  1777. }
  1778. break;
  1779. default:
  1780. fprintf(stderr, "Menu Action %s Currently Unimplemented...\n", action_words[p->act.type]);
  1781. break;
  1782. }
  1783. p = p->next;
  1784. }
  1785. }
  1786. int process_pen(menutree *mt, int x, int y, int dwn)
  1787. {
  1788. int return_code = 0;
  1789. menunode *p;
  1790. menunode *pp = mt->pressed;
  1791. int tx,ty,txx,tyy;
  1792. if(!mt->current)
  1793. {
  1794. mt->pressed = NULL;
  1795. mt->pressed_flags = 0;
  1796. mt->down = 0;
  1797. mt->down_x = 0;
  1798. mt->down_y = 0;
  1799. return -1;
  1800. }
  1801. p = mt->current->subtree;
  1802. if(dwn) //if the pen is down
  1803. {
  1804. mt->pressed = NULL;
  1805. mt->pressed_flags = 0;
  1806. if(!mt->down) //and this is news...
  1807. {
  1808. mt->down = 1; //record a pen down event
  1809. mt->down_x = x;
  1810. mt->down_y = y;
  1811. }
  1812. while(p)
  1813. {
  1814. if(p->type != NODE_WIDGET)
  1815. {
  1816. fprintf(stderr, "Expecting Widget!\n");
  1817. print_menu_tree_inner(p, 2);
  1818. p = p->next;
  1819. continue;
  1820. }
  1821. switch(p->wid.type)
  1822. {
  1823. case WIDGET_BUTTON:
  1824. break;
  1825. default:
  1826. p = p->next;
  1827. continue;
  1828. }
  1829. if(p->wid.flags & WF_DISABLED)
  1830. {
  1831. p = p->next;
  1832. continue;
  1833. }
  1834. tx = p->wid.x * GRID;
  1835. ty = p->wid.y * GRID;
  1836. txx = tx + p->wid.width * GRID;
  1837. tyy = ty + p->wid.height * GRID;
  1838. // printf("%d %d %d %d\n", p->wid.x, p->wid.y, p->wid.width, p->wid.height);
  1839. // printf("(%d,%d)-(%d,%d) %s / (%d,%d) (%d,%d)\n", tx, ty, txx, tyy, p->wid.text, x, y, mt->down_x, mt->down_y);
  1840. if( (x > tx) && (x < txx) && (y > ty) && (y < tyy) &&
  1841. (mt->down_x > tx) && (mt->down_x < txx) && (mt->down_y > ty) && (mt->down_y < tyy) )
  1842. {
  1843. mt->pressed = p;
  1844. mt->pressed_flags = p->wid.flags | WF_PRESSED;
  1845. }
  1846. p = p->next;
  1847. }
  1848. }
  1849. else
  1850. {
  1851. if(mt->down)
  1852. {
  1853. while(p)
  1854. {
  1855. if(p->type != NODE_WIDGET)
  1856. {
  1857. fprintf(stderr, "Expecting Widget!\n");
  1858. print_menu_tree_inner(p, 2);
  1859. p = p->next;
  1860. continue;
  1861. }
  1862. if(p == mt->pressed)
  1863. break;
  1864. p = p->next;
  1865. }
  1866. if(p != NULL)
  1867. {
  1868. // printf("Button: %s\n", p->wid.text);
  1869. DIU_BUTTON_BEEP(diu_fd);
  1870. do_menu_action(mt, p->subtree);
  1871. }
  1872. }
  1873. else
  1874. {
  1875. //take no action on a redundant pen up
  1876. }
  1877. mt->pressed = NULL;
  1878. mt->pressed_flags = 0;
  1879. mt->down = 0;
  1880. mt->down_x = 0;
  1881. mt->down_y = 0;
  1882. }
  1883. if(pp != mt->pressed)
  1884. {
  1885. return_code |= 1;
  1886. }
  1887. return return_code;
  1888. }