cmCursesMainForm.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCursesMainForm.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmCursesCacheEntryComposite.h"
  6. #include "cmCursesDummyWidget.h"
  7. #include "cmCursesForm.h"
  8. #include "cmCursesLabelWidget.h"
  9. #include "cmCursesLongMessageForm.h"
  10. #include "cmCursesStandardIncludes.h"
  11. #include "cmCursesStringWidget.h"
  12. #include "cmCursesWidget.h"
  13. #include "cmState.h"
  14. #include "cmStateTypes.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmSystemTools.h"
  17. #include "cmVersion.h"
  18. #include "cmake.h"
  19. #include <algorithm>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <utility>
  23. inline int ctrl(int z)
  24. {
  25. return (z & 037);
  26. }
  27. cmCursesMainForm::cmCursesMainForm(std::vector<std::string> args,
  28. int initWidth)
  29. : Args(std::move(args))
  30. , InitialWidth(initWidth)
  31. {
  32. this->NumberOfPages = 0;
  33. this->Fields = nullptr;
  34. this->Entries = nullptr;
  35. this->AdvancedMode = false;
  36. this->NumberOfVisibleEntries = 0;
  37. this->OkToGenerate = false;
  38. this->HelpMessage.emplace_back(
  39. "Welcome to ccmake, curses based user interface for CMake.");
  40. this->HelpMessage.emplace_back();
  41. this->HelpMessage.emplace_back(s_ConstHelpMessage);
  42. this->CMakeInstance = new cmake(cmake::RoleProject, cmState::Project);
  43. this->CMakeInstance->SetCMakeEditCommand(
  44. cmSystemTools::GetCMakeCursesCommand());
  45. // create the arguments for the cmake object
  46. std::string whereCMake = cmSystemTools::GetProgramPath(this->Args[0]);
  47. whereCMake += "/cmake";
  48. this->Args[0] = whereCMake;
  49. this->CMakeInstance->SetArgs(this->Args);
  50. this->SearchString = "";
  51. this->OldSearchString = "";
  52. this->SearchMode = false;
  53. }
  54. cmCursesMainForm::~cmCursesMainForm()
  55. {
  56. if (this->Form) {
  57. unpost_form(this->Form);
  58. free_form(this->Form);
  59. this->Form = nullptr;
  60. }
  61. delete[] this->Fields;
  62. // Clean-up composites
  63. if (this->Entries) {
  64. cmDeleteAll(*this->Entries);
  65. }
  66. delete this->Entries;
  67. if (this->CMakeInstance) {
  68. delete this->CMakeInstance;
  69. this->CMakeInstance = nullptr;
  70. }
  71. }
  72. // See if a cache entry is in the list of entries in the ui.
  73. bool cmCursesMainForm::LookForCacheEntry(const std::string& key)
  74. {
  75. return this->Entries &&
  76. std::any_of(this->Entries->begin(), this->Entries->end(),
  77. [&key](cmCursesCacheEntryComposite* entry) {
  78. return key == entry->Key;
  79. });
  80. }
  81. // Create new cmCursesCacheEntryComposite entries from the cache
  82. void cmCursesMainForm::InitializeUI()
  83. {
  84. // Create a vector of cmCursesCacheEntryComposite's
  85. // which contain labels, entries and new entry markers
  86. std::vector<cmCursesCacheEntryComposite*>* newEntries =
  87. new std::vector<cmCursesCacheEntryComposite*>;
  88. std::vector<std::string> cacheKeys =
  89. this->CMakeInstance->GetState()->GetCacheEntryKeys();
  90. newEntries->reserve(cacheKeys.size());
  91. // Count non-internal and non-static entries
  92. int count = 0;
  93. for (std::string const& key : cacheKeys) {
  94. cmStateEnums::CacheEntryType t =
  95. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  96. if (t != cmStateEnums::INTERNAL && t != cmStateEnums::STATIC &&
  97. t != cmStateEnums::UNINITIALIZED) {
  98. ++count;
  99. }
  100. }
  101. int entrywidth = this->InitialWidth - 35;
  102. cmCursesCacheEntryComposite* comp;
  103. if (count == 0) {
  104. // If cache is empty, display a label saying so and a
  105. // dummy entry widget (does not respond to input)
  106. comp = new cmCursesCacheEntryComposite("EMPTY CACHE", 30, 30);
  107. comp->Entry = new cmCursesDummyWidget(1, 1, 1, 1);
  108. newEntries->push_back(comp);
  109. } else {
  110. // Create the composites.
  111. // First add entries which are new
  112. for (std::string const& key : cacheKeys) {
  113. cmStateEnums::CacheEntryType t =
  114. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  115. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC ||
  116. t == cmStateEnums::UNINITIALIZED) {
  117. continue;
  118. }
  119. if (!this->LookForCacheEntry(key)) {
  120. newEntries->push_back(new cmCursesCacheEntryComposite(
  121. key, this->CMakeInstance, true, 30, entrywidth));
  122. this->OkToGenerate = false;
  123. }
  124. }
  125. // then add entries which are old
  126. for (std::string const& key : cacheKeys) {
  127. cmStateEnums::CacheEntryType t =
  128. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  129. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC ||
  130. t == cmStateEnums::UNINITIALIZED) {
  131. continue;
  132. }
  133. if (this->LookForCacheEntry(key)) {
  134. newEntries->push_back(new cmCursesCacheEntryComposite(
  135. key, this->CMakeInstance, false, 30, entrywidth));
  136. }
  137. }
  138. }
  139. // Clean old entries
  140. if (this->Entries) {
  141. cmDeleteAll(*this->Entries);
  142. }
  143. delete this->Entries;
  144. this->Entries = newEntries;
  145. // Compute fields from composites
  146. this->RePost();
  147. }
  148. void cmCursesMainForm::RePost()
  149. {
  150. // Create the fields to be passed to the form.
  151. if (this->Form) {
  152. unpost_form(this->Form);
  153. free_form(this->Form);
  154. this->Form = nullptr;
  155. }
  156. delete[] this->Fields;
  157. if (this->AdvancedMode) {
  158. this->NumberOfVisibleEntries = this->Entries->size();
  159. } else {
  160. // If normal mode, count only non-advanced entries
  161. this->NumberOfVisibleEntries = 0;
  162. for (cmCursesCacheEntryComposite* entry : *this->Entries) {
  163. const char* existingValue =
  164. this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue());
  165. bool advanced =
  166. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  167. entry->GetValue(), "ADVANCED");
  168. if (!existingValue || (!this->AdvancedMode && advanced)) {
  169. continue;
  170. }
  171. this->NumberOfVisibleEntries++;
  172. }
  173. }
  174. // there is always one even if it is the dummy one
  175. if (this->NumberOfVisibleEntries == 0) {
  176. this->NumberOfVisibleEntries = 1;
  177. }
  178. // Assign the fields: 3 for each entry: label, new entry marker
  179. // ('*' or ' ') and entry widget
  180. this->Fields = new FIELD*[3 * this->NumberOfVisibleEntries + 1];
  181. size_t cc;
  182. for (cc = 0; cc < 3 * this->NumberOfVisibleEntries + 1; cc++) {
  183. this->Fields[cc] = nullptr;
  184. }
  185. // Assign fields
  186. int j = 0;
  187. for (cmCursesCacheEntryComposite* entry : *this->Entries) {
  188. const char* existingValue =
  189. this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue());
  190. bool advanced =
  191. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  192. entry->GetValue(), "ADVANCED");
  193. if (!existingValue || (!this->AdvancedMode && advanced)) {
  194. continue;
  195. }
  196. this->Fields[3 * j] = entry->Label->Field;
  197. this->Fields[3 * j + 1] = entry->IsNewLabel->Field;
  198. this->Fields[3 * j + 2] = entry->Entry->Field;
  199. j++;
  200. }
  201. // if no cache entries there should still be one dummy field
  202. if (j == 0) {
  203. const auto& front = *this->Entries->front();
  204. this->Fields[0] = front.Label->Field;
  205. this->Fields[1] = front.IsNewLabel->Field;
  206. this->Fields[2] = front.Entry->Field;
  207. this->NumberOfVisibleEntries = 1;
  208. }
  209. // Has to be null terminated.
  210. this->Fields[3 * this->NumberOfVisibleEntries] = nullptr;
  211. }
  212. void cmCursesMainForm::Render(int left, int top, int width, int height)
  213. {
  214. if (this->Form) {
  215. FIELD* currentField = current_field(this->Form);
  216. cmCursesWidget* cw =
  217. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  218. // If in edit mode, get out of it
  219. if (cw->GetType() == cmStateEnums::STRING ||
  220. cw->GetType() == cmStateEnums::PATH ||
  221. cw->GetType() == cmStateEnums::FILEPATH) {
  222. cmCursesStringWidget* sw = static_cast<cmCursesStringWidget*>(cw);
  223. sw->SetInEdit(false);
  224. }
  225. // Delete the previous form
  226. unpost_form(this->Form);
  227. free_form(this->Form);
  228. this->Form = nullptr;
  229. }
  230. // Wrong window size
  231. if (width < cmCursesMainForm::MIN_WIDTH || width < this->InitialWidth ||
  232. height < cmCursesMainForm::MIN_HEIGHT) {
  233. return;
  234. }
  235. // Leave room for toolbar
  236. height -= 7;
  237. if (this->AdvancedMode) {
  238. this->NumberOfVisibleEntries = this->Entries->size();
  239. } else {
  240. // If normal, display only non-advanced entries
  241. this->NumberOfVisibleEntries = 0;
  242. for (cmCursesCacheEntryComposite* entry : *this->Entries) {
  243. const char* existingValue =
  244. this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue());
  245. bool advanced =
  246. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  247. entry->GetValue(), "ADVANCED");
  248. if (!existingValue || (!this->AdvancedMode && advanced)) {
  249. continue;
  250. }
  251. this->NumberOfVisibleEntries++;
  252. }
  253. }
  254. // Re-adjust the fields according to their place
  255. this->NumberOfPages = 1;
  256. if (height > 0) {
  257. bool isNewPage;
  258. int i = 0;
  259. for (cmCursesCacheEntryComposite* entry : *this->Entries) {
  260. const char* existingValue =
  261. this->CMakeInstance->GetState()->GetCacheEntryValue(entry->GetValue());
  262. bool advanced =
  263. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  264. entry->GetValue(), "ADVANCED");
  265. if (!existingValue || (!this->AdvancedMode && advanced)) {
  266. continue;
  267. }
  268. int row = (i % height) + 1;
  269. int page = (i / height) + 1;
  270. isNewPage = (page > 1) && (row == 1);
  271. if (isNewPage) {
  272. this->NumberOfPages++;
  273. }
  274. entry->Label->Move(left, top + row - 1, isNewPage);
  275. entry->IsNewLabel->Move(left + 32, top + row - 1, false);
  276. entry->Entry->Move(left + 33, top + row - 1, false);
  277. entry->Entry->SetPage(this->NumberOfPages);
  278. i++;
  279. }
  280. }
  281. // Post the form
  282. this->Form = new_form(this->Fields);
  283. post_form(this->Form);
  284. // Update toolbar
  285. this->UpdateStatusBar();
  286. this->PrintKeys();
  287. touchwin(stdscr);
  288. refresh();
  289. }
  290. void cmCursesMainForm::PrintKeys(int process /* = 0 */)
  291. {
  292. int x;
  293. int y;
  294. getmaxyx(stdscr, y, x);
  295. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  296. y < cmCursesMainForm::MIN_HEIGHT) {
  297. return;
  298. }
  299. // Give the current widget (if it exists), a chance to print keys
  300. cmCursesWidget* cw = nullptr;
  301. if (this->Form) {
  302. FIELD* currentField = current_field(this->Form);
  303. cw = reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  304. }
  305. char fmt_s[] = "%s";
  306. if (cw == nullptr || !cw->PrintKeys()) {
  307. char firstLine[512] = "";
  308. char secondLine[512] = "";
  309. char thirdLine[512] = "";
  310. if (process) {
  311. memset(firstLine, ' ', 68);
  312. memset(secondLine, ' ', 68);
  313. memset(thirdLine, ' ', 68);
  314. } else {
  315. if (this->OkToGenerate) {
  316. sprintf(firstLine,
  317. "Press [c] to configure Press [g] to generate and exit");
  318. } else {
  319. sprintf(firstLine,
  320. "Press [c] to configure ");
  321. }
  322. {
  323. const char* toggleKeyInstruction =
  324. "Press [t] to toggle advanced mode (Currently %s)";
  325. sprintf(thirdLine, toggleKeyInstruction,
  326. this->AdvancedMode ? "On" : "Off");
  327. }
  328. sprintf(secondLine,
  329. "Press [h] for help "
  330. "Press [q] to quit without generating");
  331. }
  332. curses_move(y - 4, 0);
  333. char fmt[512] =
  334. "Press [enter] to edit option Press [d] to delete an entry";
  335. if (process) {
  336. memset(fmt, ' ', 27);
  337. }
  338. printw(fmt_s, fmt);
  339. curses_move(y - 3, 0);
  340. printw(fmt_s, firstLine);
  341. curses_move(y - 2, 0);
  342. printw(fmt_s, secondLine);
  343. curses_move(y - 1, 0);
  344. printw(fmt_s, thirdLine);
  345. }
  346. if (cw) {
  347. char pageLine[512] = "";
  348. sprintf(pageLine, "Page %d of %d", cw->GetPage(), this->NumberOfPages);
  349. curses_move(0, 65 - static_cast<unsigned int>(strlen(pageLine)) - 1);
  350. printw(fmt_s, pageLine);
  351. }
  352. pos_form_cursor(this->Form);
  353. }
  354. // Print the key of the current entry and the CMake version
  355. // on the status bar. Designed for a width of 80 chars.
  356. void cmCursesMainForm::UpdateStatusBar(const char* message)
  357. {
  358. int x;
  359. int y;
  360. getmaxyx(stdscr, y, x);
  361. // If window size is too small, display error and return
  362. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  363. y < cmCursesMainForm::MIN_HEIGHT) {
  364. curses_clear();
  365. curses_move(0, 0);
  366. char fmt[] = "Window is too small. A size of at least %dx%d is required.";
  367. printw(fmt,
  368. (cmCursesMainForm::MIN_WIDTH < this->InitialWidth
  369. ? this->InitialWidth
  370. : cmCursesMainForm::MIN_WIDTH),
  371. cmCursesMainForm::MIN_HEIGHT);
  372. touchwin(stdscr);
  373. wrefresh(stdscr);
  374. return;
  375. }
  376. // Get the key of the current entry
  377. FIELD* cur = current_field(this->Form);
  378. int findex = field_index(cur);
  379. cmCursesWidget* lbl = nullptr;
  380. if (findex >= 0) {
  381. lbl = reinterpret_cast<cmCursesWidget*>(
  382. field_userptr(this->Fields[findex - 2]));
  383. }
  384. char help[128] = "";
  385. const char* curField = "";
  386. if (lbl) {
  387. curField = lbl->GetValue();
  388. // Get the help string of the current entry
  389. // and add it to the help string
  390. const char* existingValue =
  391. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  392. if (existingValue) {
  393. const char* hs = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  394. curField, "HELPSTRING");
  395. if (hs) {
  396. strncpy(help, hs, 127);
  397. help[127] = '\0';
  398. } else {
  399. help[0] = 0;
  400. }
  401. } else {
  402. sprintf(help, " ");
  403. }
  404. }
  405. // Join the key, help string and pad with spaces
  406. // (or truncate) as necessary
  407. char bar[cmCursesMainForm::MAX_WIDTH];
  408. size_t curFieldLen = strlen(curField);
  409. size_t helpLen = strlen(help);
  410. size_t width = std::min<size_t>(x, cmCursesMainForm::MAX_WIDTH);
  411. if (message) {
  412. curField = message;
  413. curFieldLen = strlen(message);
  414. strncpy(bar, curField, width);
  415. if (curFieldLen < width) {
  416. memset(bar + curFieldLen, ' ', width - curFieldLen);
  417. }
  418. } else {
  419. strncpy(bar, curField, width);
  420. if (curFieldLen < width) {
  421. bar[curFieldLen] = ':';
  422. bar[curFieldLen + 1] = ' ';
  423. strncpy(bar + curFieldLen + 2, help, width - curFieldLen - 2);
  424. if (curFieldLen + helpLen + 2 < width) {
  425. memset(bar + curFieldLen + helpLen + 2, ' ',
  426. width - (curFieldLen + helpLen + 2));
  427. }
  428. }
  429. }
  430. bar[width] = '\0';
  431. // Display CMake version info on the next line
  432. // We want to display this on the right
  433. char version[cmCursesMainForm::MAX_WIDTH];
  434. char vertmp[128];
  435. sprintf(vertmp, "CMake Version %s", cmVersion::GetCMakeVersion());
  436. size_t sideSpace = (width - strlen(vertmp));
  437. memset(version, ' ', sideSpace);
  438. sprintf(version + sideSpace, "%s", vertmp);
  439. version[width] = '\0';
  440. // Now print both lines
  441. char fmt_s[] = "%s";
  442. curses_move(y - 5, 0);
  443. attron(A_STANDOUT);
  444. printw(fmt_s, bar);
  445. attroff(A_STANDOUT);
  446. curses_move(y - 4, 0);
  447. printw(fmt_s, version);
  448. pos_form_cursor(this->Form);
  449. }
  450. void cmCursesMainForm::UpdateProgress(const std::string& msg, float prog)
  451. {
  452. char tmp[1024];
  453. const char* cmsg = tmp;
  454. if (prog >= 0) {
  455. sprintf(tmp, "%s %i%%", msg.c_str(), static_cast<int>(100 * prog));
  456. } else {
  457. cmsg = msg.c_str();
  458. }
  459. this->UpdateStatusBar(cmsg);
  460. this->PrintKeys(1);
  461. curses_move(1, 1);
  462. touchwin(stdscr);
  463. refresh();
  464. }
  465. int cmCursesMainForm::Configure(int noconfigure)
  466. {
  467. int xi;
  468. int yi;
  469. getmaxyx(stdscr, yi, xi);
  470. curses_move(1, 1);
  471. this->UpdateStatusBar("Configuring, please wait...");
  472. this->PrintKeys(1);
  473. touchwin(stdscr);
  474. refresh();
  475. this->CMakeInstance->SetProgressCallback(
  476. [this](const std::string& msg, float prog) {
  477. this->UpdateProgress(msg, prog);
  478. });
  479. // always save the current gui values to disk
  480. this->FillCacheManagerFromUI();
  481. this->CMakeInstance->SaveCache(
  482. this->CMakeInstance->GetHomeOutputDirectory());
  483. this->LoadCache(nullptr);
  484. // Get rid of previous errors
  485. this->Errors = std::vector<std::string>();
  486. // run the generate process
  487. this->OkToGenerate = true;
  488. int retVal;
  489. if (noconfigure) {
  490. retVal = this->CMakeInstance->DoPreConfigureChecks();
  491. this->OkToGenerate = false;
  492. if (retVal > 0) {
  493. retVal = 0;
  494. }
  495. } else {
  496. retVal = this->CMakeInstance->Configure();
  497. }
  498. this->CMakeInstance->SetProgressCallback(nullptr);
  499. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  500. if (retVal != 0 || !this->Errors.empty()) {
  501. // see if there was an error
  502. if (cmSystemTools::GetErrorOccuredFlag()) {
  503. this->OkToGenerate = false;
  504. }
  505. int xx;
  506. int yy;
  507. getmaxyx(stdscr, yy, xx);
  508. cmCursesLongMessageForm* msgs =
  509. new cmCursesLongMessageForm(this->Errors,
  510. cmSystemTools::GetErrorOccuredFlag()
  511. ? "Errors occurred during the last pass."
  512. : "CMake produced the following output.");
  513. // reset error condition
  514. cmSystemTools::ResetErrorOccuredFlag();
  515. CurrentForm = msgs;
  516. msgs->Render(1, 1, xx, yy);
  517. msgs->HandleInput();
  518. // If they typed the wrong source directory, we report
  519. // an error and exit
  520. if (retVal == -2) {
  521. return retVal;
  522. }
  523. CurrentForm = this;
  524. this->Render(1, 1, xx, yy);
  525. }
  526. this->InitializeUI();
  527. this->Render(1, 1, xi, yi);
  528. return 0;
  529. }
  530. int cmCursesMainForm::Generate()
  531. {
  532. int xi;
  533. int yi;
  534. getmaxyx(stdscr, yi, xi);
  535. curses_move(1, 1);
  536. this->UpdateStatusBar("Generating, please wait...");
  537. this->PrintKeys(1);
  538. touchwin(stdscr);
  539. refresh();
  540. this->CMakeInstance->SetProgressCallback(
  541. [this](const std::string& msg, float prog) {
  542. this->UpdateProgress(msg, prog);
  543. });
  544. // Get rid of previous errors
  545. this->Errors = std::vector<std::string>();
  546. // run the generate process
  547. int retVal = this->CMakeInstance->Generate();
  548. this->CMakeInstance->SetProgressCallback(nullptr);
  549. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  550. if (retVal != 0 || !this->Errors.empty()) {
  551. // see if there was an error
  552. if (cmSystemTools::GetErrorOccuredFlag()) {
  553. this->OkToGenerate = false;
  554. }
  555. // reset error condition
  556. cmSystemTools::ResetErrorOccuredFlag();
  557. int xx;
  558. int yy;
  559. getmaxyx(stdscr, yy, xx);
  560. const char* title = "Messages during last pass.";
  561. if (cmSystemTools::GetErrorOccuredFlag()) {
  562. title = "Errors occurred during the last pass.";
  563. }
  564. cmCursesLongMessageForm* msgs =
  565. new cmCursesLongMessageForm(this->Errors, title);
  566. CurrentForm = msgs;
  567. msgs->Render(1, 1, xx, yy);
  568. msgs->HandleInput();
  569. // If they typed the wrong source directory, we report
  570. // an error and exit
  571. if (retVal == -2) {
  572. return retVal;
  573. }
  574. CurrentForm = this;
  575. this->Render(1, 1, xx, yy);
  576. }
  577. this->InitializeUI();
  578. this->Render(1, 1, xi, yi);
  579. return 0;
  580. }
  581. void cmCursesMainForm::AddError(const std::string& message,
  582. const char* /*unused*/)
  583. {
  584. this->Errors.emplace_back(message);
  585. }
  586. void cmCursesMainForm::RemoveEntry(const char* value)
  587. {
  588. if (!value) {
  589. return;
  590. }
  591. auto removeIt =
  592. std::find_if(this->Entries->begin(), this->Entries->end(),
  593. [value](cmCursesCacheEntryComposite* entry) -> bool {
  594. const char* val = entry->GetValue();
  595. return val != nullptr && !strcmp(value, val);
  596. });
  597. if (removeIt != this->Entries->end()) {
  598. this->CMakeInstance->UnwatchUnusedCli(value);
  599. this->Entries->erase(removeIt);
  600. }
  601. }
  602. // copy from the list box to the cache manager
  603. void cmCursesMainForm::FillCacheManagerFromUI()
  604. {
  605. for (cmCursesCacheEntryComposite* entry : *this->Entries) {
  606. const std::string& cacheKey = entry->Key;
  607. const char* existingValue =
  608. this->CMakeInstance->GetState()->GetCacheEntryValue(cacheKey);
  609. if (existingValue) {
  610. std::string oldValue = existingValue;
  611. std::string newValue = entry->Entry->GetValue();
  612. std::string fixedOldValue;
  613. std::string fixedNewValue;
  614. cmStateEnums::CacheEntryType t =
  615. this->CMakeInstance->GetState()->GetCacheEntryType(cacheKey);
  616. this->FixValue(t, oldValue, fixedOldValue);
  617. this->FixValue(t, newValue, fixedNewValue);
  618. if (!(fixedOldValue == fixedNewValue)) {
  619. // The user has changed the value. Mark it as modified.
  620. this->CMakeInstance->GetState()->SetCacheEntryBoolProperty(
  621. cacheKey, "MODIFIED", true);
  622. this->CMakeInstance->GetState()->SetCacheEntryValue(cacheKey,
  623. fixedNewValue);
  624. }
  625. }
  626. }
  627. }
  628. void cmCursesMainForm::FixValue(cmStateEnums::CacheEntryType type,
  629. const std::string& in, std::string& out) const
  630. {
  631. out = in.substr(0, in.find_last_not_of(' ') + 1);
  632. if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) {
  633. cmSystemTools::ConvertToUnixSlashes(out);
  634. }
  635. if (type == cmStateEnums::BOOL) {
  636. if (cmIsOff(out)) {
  637. out = "OFF";
  638. } else {
  639. out = "ON";
  640. }
  641. }
  642. }
  643. void cmCursesMainForm::HandleInput()
  644. {
  645. int x = 0;
  646. int y = 0;
  647. if (!this->Form) {
  648. return;
  649. }
  650. FIELD* currentField;
  651. cmCursesWidget* currentWidget;
  652. char debugMessage[128];
  653. for (;;) {
  654. this->UpdateStatusBar();
  655. this->PrintKeys();
  656. if (this->SearchMode) {
  657. std::string searchstr = "Search: " + this->SearchString;
  658. this->UpdateStatusBar(searchstr.c_str());
  659. this->PrintKeys(1);
  660. curses_move(y - 5, static_cast<unsigned int>(searchstr.size()));
  661. // curses_move(1,1);
  662. touchwin(stdscr);
  663. refresh();
  664. }
  665. int key = getch();
  666. getmaxyx(stdscr, y, x);
  667. // If window too small, handle 'q' only
  668. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  669. // quit
  670. if (key == 'q') {
  671. break;
  672. }
  673. continue;
  674. }
  675. currentField = current_field(this->Form);
  676. currentWidget =
  677. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  678. bool widgetHandled = false;
  679. if (this->SearchMode) {
  680. if (key == 10 || key == KEY_ENTER) {
  681. this->SearchMode = false;
  682. if (!this->SearchString.empty()) {
  683. this->JumpToCacheEntry(this->SearchString.c_str());
  684. this->OldSearchString = this->SearchString;
  685. }
  686. this->SearchString = "";
  687. }
  688. /*
  689. else if ( key == KEY_ESCAPE )
  690. {
  691. this->SearchMode = false;
  692. }
  693. */
  694. else if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
  695. (key >= '0' && key <= '9') || (key == '_')) {
  696. if (this->SearchString.size() <
  697. static_cast<std::string::size_type>(x - 10)) {
  698. this->SearchString += static_cast<char>(key);
  699. }
  700. } else if (key == ctrl('h') || key == KEY_BACKSPACE || key == KEY_DC) {
  701. if (!this->SearchString.empty()) {
  702. this->SearchString.resize(this->SearchString.size() - 1);
  703. }
  704. }
  705. } else if (currentWidget && !this->SearchMode) {
  706. // Ask the current widget if it wants to handle input
  707. widgetHandled = currentWidget->HandleInput(key, this, stdscr);
  708. if (widgetHandled) {
  709. this->OkToGenerate = false;
  710. this->UpdateStatusBar();
  711. this->PrintKeys();
  712. }
  713. }
  714. if ((!currentWidget || !widgetHandled) && !this->SearchMode) {
  715. // If the current widget does not want to handle input,
  716. // we handle it.
  717. sprintf(debugMessage, "Main form handling input, key: %d", key);
  718. cmCursesForm::LogMessage(debugMessage);
  719. // quit
  720. if (key == 'q') {
  721. break;
  722. }
  723. // if not end of page, next field otherwise next page
  724. // each entry consists of fields: label, isnew, value
  725. // therefore, the label field for the prev. entry is index-5
  726. // and the label field for the next entry is index+1
  727. // (index always corresponds to the value field)
  728. // scroll down with arrow down, ctrl+n (emacs binding), or j (vim
  729. // binding)
  730. if (key == KEY_DOWN || key == ctrl('n') || key == 'j') {
  731. FIELD* cur = current_field(this->Form);
  732. size_t findex = field_index(cur);
  733. if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  734. continue;
  735. }
  736. if (new_page(this->Fields[findex + 1])) {
  737. form_driver(this->Form, REQ_NEXT_PAGE);
  738. } else {
  739. form_driver(this->Form, REQ_NEXT_FIELD);
  740. }
  741. }
  742. // if not beginning of page, previous field, otherwise previous page
  743. // each entry consists of fields: label, isnew, value
  744. // therefore, the label field for the prev. entry is index-5
  745. // and the label field for the next entry is index+1
  746. // (index always corresponds to the value field)
  747. // scroll down with arrow up, ctrl+p (emacs binding), or k (vim binding)
  748. else if (key == KEY_UP || key == ctrl('p') || key == 'k') {
  749. FIELD* cur = current_field(this->Form);
  750. int findex = field_index(cur);
  751. if (findex == 2) {
  752. continue;
  753. }
  754. if (new_page(this->Fields[findex - 2])) {
  755. form_driver(this->Form, REQ_PREV_PAGE);
  756. set_current_field(this->Form, this->Fields[findex - 3]);
  757. } else {
  758. form_driver(this->Form, REQ_PREV_FIELD);
  759. }
  760. }
  761. // pg down
  762. else if (key == KEY_NPAGE || key == ctrl('d')) {
  763. form_driver(this->Form, REQ_NEXT_PAGE);
  764. }
  765. // pg up
  766. else if (key == KEY_PPAGE || key == ctrl('u')) {
  767. form_driver(this->Form, REQ_PREV_PAGE);
  768. }
  769. // configure
  770. else if (key == 'c') {
  771. this->Configure();
  772. }
  773. // display help
  774. else if (key == 'h') {
  775. getmaxyx(stdscr, y, x);
  776. FIELD* cur = current_field(this->Form);
  777. int findex = field_index(cur);
  778. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  779. field_userptr(this->Fields[findex - 2]));
  780. const char* curField = lbl->GetValue();
  781. const char* helpString = nullptr;
  782. const char* existingValue =
  783. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  784. if (existingValue) {
  785. helpString = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  786. curField, "HELPSTRING");
  787. }
  788. if (helpString) {
  789. char* message = new char
  790. [strlen(curField) + strlen(helpString) +
  791. strlen(
  792. "Current option is: \n Help string for this option is: \n") +
  793. 10];
  794. sprintf(
  795. message,
  796. "Current option is: %s\nHelp string for this option is: %s\n",
  797. curField, helpString);
  798. this->HelpMessage[1] = message;
  799. delete[] message;
  800. } else {
  801. this->HelpMessage[1] = "";
  802. }
  803. cmCursesLongMessageForm* msgs =
  804. new cmCursesLongMessageForm(this->HelpMessage, "Help.");
  805. CurrentForm = msgs;
  806. msgs->Render(1, 1, x, y);
  807. msgs->HandleInput();
  808. CurrentForm = this;
  809. this->Render(1, 1, x, y);
  810. set_current_field(this->Form, cur);
  811. }
  812. // display last errors
  813. else if (key == 'l') {
  814. getmaxyx(stdscr, y, x);
  815. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  816. this->Errors, "Errors occurred during the last pass.");
  817. CurrentForm = msgs;
  818. msgs->Render(1, 1, x, y);
  819. msgs->HandleInput();
  820. CurrentForm = this;
  821. this->Render(1, 1, x, y);
  822. } else if (key == '/') {
  823. this->SearchMode = true;
  824. this->UpdateStatusBar("Search");
  825. this->PrintKeys(1);
  826. touchwin(stdscr);
  827. refresh();
  828. } else if (key == 'n') {
  829. if (!this->OldSearchString.empty()) {
  830. this->JumpToCacheEntry(this->OldSearchString.c_str());
  831. }
  832. }
  833. // switch advanced on/off
  834. else if (key == 't') {
  835. if (this->AdvancedMode) {
  836. this->AdvancedMode = false;
  837. } else {
  838. this->AdvancedMode = true;
  839. }
  840. getmaxyx(stdscr, y, x);
  841. this->RePost();
  842. this->Render(1, 1, x, y);
  843. }
  844. // generate and exit
  845. else if (key == 'g') {
  846. if (this->OkToGenerate) {
  847. this->Generate();
  848. break;
  849. }
  850. }
  851. // delete cache entry
  852. else if (key == 'd' && this->NumberOfVisibleEntries) {
  853. this->OkToGenerate = false;
  854. FIELD* cur = current_field(this->Form);
  855. size_t findex = field_index(cur);
  856. // make the next or prev. current field after deletion
  857. // each entry consists of fields: label, isnew, value
  858. // therefore, the label field for the prev. entry is findex-5
  859. // and the label field for the next entry is findex+1
  860. // (findex always corresponds to the value field)
  861. FIELD* nextCur;
  862. if (findex == 2) {
  863. nextCur = nullptr;
  864. } else if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  865. nextCur = this->Fields[findex - 5];
  866. } else {
  867. nextCur = this->Fields[findex + 1];
  868. }
  869. // Get the label widget
  870. // each entry consists of fields: label, isnew, value
  871. // therefore, the label field for the is findex-2
  872. // (findex always corresponds to the value field)
  873. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  874. field_userptr(this->Fields[findex - 2]));
  875. if (lbl) {
  876. this->CMakeInstance->GetState()->RemoveCacheEntry(lbl->GetValue());
  877. std::string nextVal;
  878. if (nextCur) {
  879. nextVal =
  880. (reinterpret_cast<cmCursesWidget*>(field_userptr(nextCur))
  881. ->GetValue());
  882. }
  883. getmaxyx(stdscr, y, x);
  884. this->RemoveEntry(lbl->GetValue());
  885. this->RePost();
  886. this->Render(1, 1, x, y);
  887. if (nextCur) {
  888. // make the next or prev. current field after deletion
  889. auto nextEntryIt =
  890. std::find_if(this->Entries->begin(), this->Entries->end(),
  891. [&nextVal](cmCursesCacheEntryComposite* entry) {
  892. return nextVal == entry->Key;
  893. });
  894. if (nextEntryIt != this->Entries->end()) {
  895. set_current_field(this->Form, (*nextEntryIt)->Entry->Field);
  896. }
  897. }
  898. }
  899. }
  900. }
  901. touchwin(stdscr);
  902. wrefresh(stdscr);
  903. }
  904. }
  905. int cmCursesMainForm::LoadCache(const char* /*unused*/)
  906. {
  907. int r = this->CMakeInstance->LoadCache();
  908. if (r < 0) {
  909. return r;
  910. }
  911. this->CMakeInstance->SetCacheArgs(this->Args);
  912. this->CMakeInstance->PreLoadCMakeFiles();
  913. return r;
  914. }
  915. void cmCursesMainForm::JumpToCacheEntry(const char* astr)
  916. {
  917. std::string str;
  918. if (astr) {
  919. str = cmSystemTools::LowerCase(astr);
  920. }
  921. if (str.empty()) {
  922. return;
  923. }
  924. FIELD* cur = current_field(this->Form);
  925. int start_index = field_index(cur);
  926. int findex = start_index;
  927. for (;;) {
  928. if (!str.empty()) {
  929. cmCursesWidget* lbl = nullptr;
  930. if (findex >= 0) {
  931. lbl = reinterpret_cast<cmCursesWidget*>(
  932. field_userptr(this->Fields[findex - 2]));
  933. }
  934. if (lbl) {
  935. const char* curField = lbl->GetValue();
  936. if (curField) {
  937. std::string cfld = cmSystemTools::LowerCase(curField);
  938. if (cfld.find(str) != std::string::npos && findex != start_index) {
  939. break;
  940. }
  941. }
  942. }
  943. }
  944. if (size_t(findex) >= 3 * this->NumberOfVisibleEntries - 1) {
  945. set_current_field(this->Form, this->Fields[2]);
  946. } else if (new_page(this->Fields[findex + 1])) {
  947. form_driver(this->Form, REQ_NEXT_PAGE);
  948. } else {
  949. form_driver(this->Form, REQ_NEXT_FIELD);
  950. }
  951. /*
  952. char buffer[1024];
  953. sprintf(buffer, "Line: %d != %d / %d\n", findex, idx,
  954. this->NumberOfVisibleEntries);
  955. touchwin(stdscr);
  956. refresh();
  957. this->UpdateStatusBar( buffer );
  958. usleep(100000);
  959. */
  960. cur = current_field(this->Form);
  961. findex = field_index(cur);
  962. if (findex == start_index) {
  963. break;
  964. }
  965. }
  966. }
  967. const char* cmCursesMainForm::s_ConstHelpMessage =
  968. "CMake is used to configure and generate build files for software projects. "
  969. "The basic steps for configuring a project with ccmake are as follows:\n\n"
  970. "1. Run ccmake in the directory where you want the object and executable "
  971. "files to be placed (build directory). If the source directory is not the "
  972. "same as this build directory, you have to specify it as an argument on the "
  973. "command line.\n\n"
  974. "2. When ccmake is run, it will read the configuration files and display "
  975. "the current build options. "
  976. "If you have run CMake before and have updated the configuration files "
  977. "since then, any new entries will be displayed on top and will be marked "
  978. "with a *. "
  979. "On the other hand, the first time you run ccmake, all build options will "
  980. "be new and will be marked as such. "
  981. "At this point, you can modify any options (see keys below) you want to "
  982. "change. "
  983. "When you are satisfied with your changes, press 'c' to have CMake process "
  984. "the configuration files. "
  985. "Please note that changing some options may cause new ones to appear. These "
  986. "will be shown on top and will be marked with *. "
  987. "Repeat this procedure until you are satisfied with all the options and "
  988. "there are no new entries. "
  989. "At this point, a new command will appear: G)enerate and Exit. You can now "
  990. "hit 'g' to have CMake generate all the build files (i.e. makefiles or "
  991. "project files) and exit. "
  992. "At any point during the process, you can exit ccmake with 'q'. However, "
  993. "this will not generate/change any build files.\n\n"
  994. "ccmake KEYS:\n\n"
  995. "Navigation: "
  996. "You can use the arrow keys and page up, down to navigate the options. "
  997. "Alternatively, you can use the following keys: \n"
  998. " C-n or j : next option\n"
  999. " C-p or k : previous options\n"
  1000. " C-d : down one page\n"
  1001. " C-u : up one page\n\n"
  1002. "Editing options: "
  1003. "To change an option press enter or return. If the current options is a "
  1004. "boolean, this will toggle its value. "
  1005. "Otherwise, ccmake will enter edit mode. Alternatively, you can toggle "
  1006. "a bool variable by pressing space, and enter edit mode with i."
  1007. "In this mode you can edit an option using arrow keys and backspace. "
  1008. "Alternatively, you can use the following keys:\n"
  1009. " C-b : back one character\n"
  1010. " C-f : forward one character\n"
  1011. " C-a : go to the beginning of the field\n"
  1012. " C-e : go to the end of the field\n"
  1013. " C-d : delete previous character\n"
  1014. " C-k : kill the rest of the field\n"
  1015. " Esc : Restore field (discard last changes)\n"
  1016. " Enter : Leave edit mode\n"
  1017. "Commands:\n"
  1018. " q : quit ccmake without generating build files\n"
  1019. " h : help, shows this screen\n"
  1020. " c : process the configuration files with the current options\n"
  1021. " g : generate build files and exit, only available when there are no "
  1022. "new options and no errors have been detected during last configuration.\n"
  1023. " l : shows last errors\n"
  1024. " d : delete an option\n"
  1025. " t : toggles advanced mode. In normal mode, only the most important "
  1026. "options are shown. In advanced mode, all options are shown. We recommend "
  1027. "using normal mode unless you are an expert.\n"
  1028. " / : search for a variable name.\n";