baselist.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * baselist.cc - list of somethings
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2001 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <ctype.h>
  27. #include <unistd.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/termios.h>
  30. #include <dpkg.h>
  31. #include <dpkg-db.h>
  32. #include "dselect.h"
  33. #include "bindings.h"
  34. void mywerase(WINDOW *win) {
  35. int my,mx,y,x;
  36. getmaxyx(win,my,mx);
  37. for (y=0; y<my; y++) {
  38. wmove(win,y,0); for (x=0; x<mx; x++) waddch(win,' ');
  39. }
  40. wmove(win,0,0);
  41. }
  42. baselist *baselist::signallist= 0;
  43. void baselist::sigwinchhandler(int) {
  44. struct winsize size;
  45. if (debug) fprintf(debug,"baselist::sigwinchhandler(), signallist=%p\n",signallist);
  46. baselist *p= signallist;
  47. p->enddisplay();
  48. endwin(); initscr();
  49. if (ioctl(fileno(stdout), TIOCGWINSZ, &size) != 0) ohshite(_("ioctl(TIOCGWINSZ) failed"));
  50. resizeterm(size.ws_row, size.ws_col); wrefresh(curscr);
  51. p->startdisplay();
  52. if (doupdate() == ERR) ohshite(_("doupdate in SIGWINCH handler failed"));
  53. }
  54. static void cu_sigwinch(int, void **argv) {
  55. struct sigaction *osigactp= (struct sigaction*)argv[0];
  56. sigset_t *oblockedp= (sigset_t*)argv[1];
  57. if (sigaction(SIGWINCH,osigactp,0)) ohshite(_("failed to restore old SIGWINCH sigact"));
  58. delete osigactp;
  59. if (sigprocmask(SIG_SETMASK,oblockedp,0)) ohshite(_("failed to restore old signal mask"));
  60. delete oblockedp;
  61. }
  62. void baselist::setupsigwinch() {
  63. sigemptyset(&sigwinchset);
  64. sigaddset(&sigwinchset,SIGWINCH);
  65. osigactp= new(struct sigaction);
  66. oblockedp= new(sigset_t);
  67. if (sigprocmask(0,0,oblockedp)) ohshite(_("failed to get old signal mask"));
  68. if (sigaction(SIGWINCH,0,osigactp)) ohshite(_("failed to get old SIGWINCH sigact"));
  69. push_cleanup(cu_sigwinch,~0, 0,0, 2,(void*)osigactp,(void*)oblockedp);
  70. if (sigprocmask(SIG_BLOCK,&sigwinchset,0)) ohshite(_("failed to block SIGWINCH"));
  71. memset(&nsigact,0,sizeof(nsigact));
  72. nsigact.sa_handler= sigwinchhandler;
  73. sigemptyset(&nsigact.sa_mask);
  74. //nsigact.sa_flags= SA_INTERRUPT;
  75. if (sigaction(SIGWINCH,&nsigact,0)) ohshite(_("failed to set new SIGWINCH sigact"));
  76. }
  77. void baselist::setheights() {
  78. int y= ymax - (title_height + colheads_height + thisstate_height);
  79. assert(y>=1);
  80. if (showinfo==2 && y>=7) {
  81. list_height= 5;
  82. whatinfo_height= 1;
  83. info_height= y-6;
  84. } else if (showinfo==1 && y>=10) {
  85. list_height= y/2;
  86. info_height= (y-1)/2;
  87. whatinfo_height= 1;
  88. } else {
  89. list_height= y;
  90. info_height= 0;
  91. whatinfo_height= 0;
  92. }
  93. colheads_row= title_height;
  94. list_row= colheads_row + colheads_height;
  95. thisstate_row= list_row + list_height;
  96. info_row= thisstate_row + thisstate_height;
  97. whatinfo_row= ymax - 1;
  98. }
  99. void baselist::startdisplay() {
  100. if (debug) fprintf(debug,"baselist[%p]::startdisplay()\n",this);
  101. cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  102. clear(); wnoutrefresh(stdscr);
  103. // find attributes
  104. if (has_colors() && start_color()==OK && COLOR_PAIRS >= numscreenparts) {
  105. int i;
  106. printf("allocing\n");
  107. for (i = 1; i < numscreenparts; i++) {
  108. if (init_pair(i, color[i].fore, color[i].back) != OK)
  109. ohshite(_("failed to allocate colour pair"));
  110. }
  111. /* TODO: should use an array of attr's, indexed by attr name. Oh well. */
  112. list_attr= COLOR_PAIR(list) | color[list].attr;
  113. listsel_attr= COLOR_PAIR(listsel) | color[listsel].attr;
  114. title_attr= COLOR_PAIR(title) | color[title].attr;
  115. thisstate_attr= COLOR_PAIR(thisstate) | color[thisstate].attr;
  116. selstate_attr= COLOR_PAIR(selstate) | color[selstate].attr;
  117. selstatesel_attr= COLOR_PAIR(selstatesel) | color[selstatesel].attr;
  118. colheads_attr= COLOR_PAIR(colheads) | color[colheads].attr;
  119. query_attr= COLOR_PAIR(query) | color[query].attr;
  120. info_attr= COLOR_PAIR(info) | color[info].attr;
  121. info_headattr= COLOR_PAIR(info_head) | color[info_head].attr;
  122. whatinfo_attr= COLOR_PAIR(whatinfo) | color[whatinfo].attr;
  123. helpscreen_attr= COLOR_PAIR(helpscreen) | color[helpscreen].attr;
  124. } else {
  125. /* User defined attributes for B&W mode are not currently supported. */
  126. title_attr= A_REVERSE;
  127. thisstate_attr= A_STANDOUT;
  128. list_attr= 0;
  129. listsel_attr= A_STANDOUT;
  130. selstate_attr= A_BOLD;
  131. selstatesel_attr= A_STANDOUT;
  132. colheads_attr= A_BOLD;
  133. query_attr= title_attr;
  134. info_attr= list_attr;
  135. info_headattr= A_BOLD;
  136. whatinfo_attr= thisstate_attr;
  137. helpscreen_attr= A_NORMAL;
  138. }
  139. // set up windows and pads, based on screen size
  140. getmaxyx(stdscr,ymax,xmax);
  141. title_height= ymax>=6;
  142. colheads_height= ymax>=5;
  143. thisstate_height= ymax>=3;
  144. setheights();
  145. setwidths();
  146. titlewin= newwin(1,xmax, 0,0);
  147. if (!titlewin) ohshite(_("failed to create title window"));
  148. wattrset(titlewin,title_attr);
  149. whatinfowin= newwin(1,xmax, whatinfo_row,0);
  150. if (!whatinfowin) ohshite(_("failed to create whatinfo window"));
  151. wattrset(whatinfowin,whatinfo_attr);
  152. listpad = newpad(ymax, total_width);
  153. if (!listpad) ohshite(_("failed to create baselist pad"));
  154. colheadspad= newpad(1, total_width);
  155. if (!colheadspad) ohshite(_("failed to create heading pad"));
  156. wattrset(colheadspad,colheads_attr);
  157. thisstatepad= newpad(1, total_width);
  158. if (!thisstatepad) ohshite(_("failed to create thisstate pad"));
  159. wattrset(thisstatepad,thisstate_attr);
  160. infopad= newpad(MAX_DISPLAY_INFO, total_width);
  161. if (!infopad) ohshite(_("failed to create info pad"));
  162. wattrset(infopad,info_attr);
  163. wbkgdset(infopad, ' ' | info_attr);
  164. querywin= newwin(1,xmax,ymax-1,0);
  165. if (!querywin) ohshite(_("failed to create query window"));
  166. wbkgdset(querywin, ' ' | query_attr);
  167. if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
  168. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  169. if (topofscreen < 0) topofscreen= 0;
  170. infotopofscreen= 0; leftofscreen= 0;
  171. redrawall();
  172. if (debug)
  173. fprintf(debug,
  174. "baselist::startdisplay() done ...\n\n"
  175. " xmax=%d, ymax=%d;\n\n"
  176. " title_height=%d, colheads_height=%d, list_height=%d;\n"
  177. " thisstate_height=%d, info_height=%d, whatinfo_height=%d;\n\n"
  178. " colheads_row=%d, thisstate_row=%d, info_row=%d;\n"
  179. " whatinfo_row=%d, list_row=%d;\n\n",
  180. xmax, ymax,
  181. title_height, colheads_height, list_height,
  182. thisstate_height, info_height, whatinfo_height,
  183. colheads_row, thisstate_row, info_row,
  184. whatinfo_row, list_row);
  185. }
  186. void baselist::enddisplay() {
  187. delwin(titlewin);
  188. delwin(whatinfowin);
  189. delwin(listpad);
  190. delwin(colheadspad);
  191. delwin(thisstatepad);
  192. delwin(infopad);
  193. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  194. listpad= 0;
  195. }
  196. void baselist::redrawall() {
  197. redrawtitle();
  198. redrawcolheads();
  199. wattrset(listpad,list_attr); mywerase(listpad);
  200. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  201. refreshlist();
  202. redrawthisstate();
  203. redrawinfo();
  204. }
  205. void baselist::redraw1item(int index) {
  206. redraw1itemsel(index, index == cursorline);
  207. }
  208. baselist::baselist(keybindings *kb) {
  209. if (debug)
  210. fprintf(debug,"baselist[%p]::baselist()\n",this);
  211. bindings= kb;
  212. nitems= 0;
  213. xmax= -1;
  214. list_height=0; info_height=0;
  215. topofscreen= 0; leftofscreen= 0;
  216. listpad= 0; cursorline= -1;
  217. showinfo= 1;
  218. searchstring[0]= 0;
  219. }
  220. void baselist::itd_keys() {
  221. whatinfovb(_("Keybindings"));
  222. const int givek= xmax/3;
  223. bindings->describestart();
  224. const char **ta;
  225. while ((ta= bindings->describenext()) != 0) {
  226. const char **tap= ta+1;
  227. for (;;) {
  228. waddstr(infopad, gettext(*tap));
  229. tap++; if (!*tap) break;
  230. waddstr(infopad, ", ");
  231. }
  232. int y,x;
  233. getyx(infopad,y,x);
  234. if (x >= givek) y++;
  235. mvwaddstr(infopad, y,givek, ta[0]);
  236. waddch(infopad,'\n');
  237. }
  238. }
  239. void baselist::dosearch() {
  240. int offset, index;
  241. if (debug) fprintf(debug,"baselist[%p]::dosearch(); searchstring=`%s'\n",
  242. this,searchstring);
  243. for (offset=1, index=greaterint(topofscreen,cursorline+1);
  244. offset<nitems;
  245. offset++, index++) {
  246. if (index >= nitems) index -= nitems;
  247. if (matchsearch(index)) {
  248. topofscreen= index-1;
  249. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  250. if (topofscreen < 0) topofscreen= 0;
  251. setcursor(index);
  252. return;
  253. }
  254. }
  255. beep();
  256. }
  257. void baselist::refreshinfo() {
  258. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  259. lesserint(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  260. lesserint(total_width - leftofscreen - 1, xmax - 1));
  261. if (whatinfo_height) {
  262. mywerase(whatinfowin);
  263. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  264. if (infolines > info_height) {
  265. wprintw(whatinfowin,_(" -- %d%%, press "),
  266. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  267. if (infotopofscreen + info_height < infolines) {
  268. wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
  269. if (infotopofscreen) waddstr(whatinfowin, ", ");
  270. }
  271. if (infotopofscreen)
  272. wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
  273. waddch(whatinfowin,'.');
  274. }
  275. wnoutrefresh(whatinfowin);
  276. }
  277. }
  278. void baselist::wordwrapinfo(int offset, const char *m) {
  279. int usemax= xmax-5;
  280. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo(%d, `%s')\n",this,offset,m);
  281. int wrapping=0;
  282. for (;;) {
  283. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  284. const char *p= strchr(m,'\n');
  285. int l= p ? (int)(p-m) : strlen(m);
  286. while (l && isspace(m[l-1])) l--;
  287. if (!l || (*m == '.' && l == 1)) {
  288. if (wrapping) waddch(infopad,'\n');
  289. waddch(infopad,'\n');
  290. wrapping= 0;
  291. } else if (*m == ' ' || usemax < 10) {
  292. if (wrapping) waddch(infopad,'\n');
  293. waddnstr(infopad, m, l);
  294. waddch(infopad,'\n'); wrapping= 0;
  295. } else {
  296. int x,y;
  297. if (wrapping) {
  298. getyx(infopad, y,x);
  299. if (x+1 >= usemax) {
  300. waddch(infopad,'\n');
  301. } else {
  302. waddch(infopad,' ');
  303. }
  304. }
  305. for (;;) {
  306. getyx(infopad, y,x);
  307. int dosend= usemax-x;
  308. if (l <= dosend) {
  309. dosend=l;
  310. } else {
  311. int i=dosend;
  312. while (i > 0 && m[i] != ' ') i--;
  313. if (i > 0 || x > 0) dosend=i;
  314. }
  315. if (dosend) waddnstr(infopad, m, dosend);
  316. while (dosend < l && m[dosend] == ' ') dosend++;
  317. l-= dosend; m+= dosend;
  318. if (l <= 0) break;
  319. waddch(infopad,'\n');
  320. }
  321. wrapping= 1;
  322. }
  323. if (!p) break;
  324. if (getcury(infopad) == (MAX_DISPLAY_INFO - 1)) {
  325. waddstr(infopad,
  326. "[The package description is too long and has been truncated...]");
  327. break;
  328. }
  329. m= ++p;
  330. }
  331. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo() done\n",this);
  332. }
  333. baselist::~baselist() { }
  334. /* vi: sw=2 ts=8
  335. */