2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * gator_curseswindows.c
14 * Implementation of the gator curses window facility.
16 *------------------------------------------------------------------------*/
18 #include <afs/param.h>
19 #include <afsconfig.h>
24 #if defined(AFS_HPUX110_ENV) && !defined(__HP_CURSES)
29 #include <curses.h> /*Curses library*/
31 #include <sys/types.h>
33 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
40 #include "gtxcurseswin.h" /*Interface definition*/
41 #include "gtxobjects.h"
44 extern int errno; /* everybody else puts it in errno.h */
46 int curses_debug; /*Is debugging turned on?*/
47 static char mn[] = "gator_curseswindows"; /*Module name*/
50 * Version of standard operations for a curses window.
52 struct gwinops curses_gwinops = {
54 gator_cursesgwin_clear,
55 gator_cursesgwin_destroy,
56 gator_cursesgwin_display,
57 gator_cursesgwin_drawline,
58 gator_cursesgwin_drawrectangle,
59 gator_cursesgwin_drawchar,
60 gator_cursesgwin_drawstring,
61 gator_cursesgwin_invert,
62 gator_cursesgwin_getchar,
63 gator_cursesgwin_getdimensions,
64 gator_cursesgwin_wait,
67 struct gwinbaseops gator_curses_gwinbops = {
68 gator_cursesgwin_create,
69 gator_cursesgwin_cleanup,
74 * Macros to map pixel positions to row & column positions.
75 * (Note: for now, they are the identity function!!)
77 #define GATOR_MAP_X_TO_COL(w, x) (x)
78 #define GATOR_MAP_Y_TO_LINE(w, y) (y)
80 /*------------------------------------------------------------------------
81 * gator_cursesgwin_init
84 * Initialize the curses window package.
87 * int adebug: Is debugging turned on?
91 * Error value otherwise.
94 * Nothing interesting.
98 *------------------------------------------------------------------------*/
100 int gator_cursesgwin_init(adebug)
103 { /*gator_cursesgwin_init*/
105 static char rn[] = "gator_cursesgwin_init"; /*Routine name*/
106 struct gator_cursesgwin *c_data; /*Ptr to curses-specific data*/
109 * Remember if we'll be doing debugging, then init the curses package.
111 curses_debug = adebug;
114 fprintf(stderr, "[%s:%s] Calling initscr()\n", mn, rn);
118 * Fill out the base window structure for curses.
121 fprintf(stderr, "[%s:%s] Allocating %d bytes for curses window private space in base window\n", mn, rn, sizeof(struct gator_cursesgwin));
122 c_data = (struct gator_cursesgwin *) malloc(sizeof(struct gator_cursesgwin));
123 if (c_data == (struct gator_cursesgwin *)0){
124 fprintf(stderr, "[%s:%s] Can't allocate %d bytes for curses window private space in base window\n", mn, rn, sizeof(struct gator_cursesgwin));
129 * Fill in the curses-specific base window info. We assume that chars are 8x13.
132 c_data->charwidth = 8;
133 c_data->charheight = 13;
134 c_data->box_vertchar = '|';
135 c_data->box_horizchar = '-';
138 * Fill in the generic base window info.
140 gator_basegwin.w_type = GATOR_WIN_CURSES;
141 gator_basegwin.w_x = 0;
142 gator_basegwin.w_y = 0;
143 gator_basegwin.w_width = c_data->charwidth * COLS;
144 gator_basegwin.w_height = c_data->charheight * LINES;
145 gator_basegwin.w_changed = 0;
146 gator_basegwin.w_op = &curses_gwinops;
147 gator_basegwin.w_parent = (struct gwin *)0;
150 * Plug the private data into the generic part of the base window.
152 gator_basegwin.w_data = (int *)c_data;
155 * Now, set the terminal into the right mode for handling input
157 raw(); /* curses raw mode */
160 gator_basegwin.w_frame = gtxframe_Create();
163 * Clear out the screen and return the good news.
165 wclear(((struct gator_cursesgwin *)(gator_basegwin.w_data))->wp);
168 } /*gator_cursesgwin_init*/
170 /*------------------------------------------------------------------------
171 * gator_cursesgwin_create
174 * Create a curses window (incorrectly).
177 * struct gator_cursesgwin_params *params : Ptr to creation parameters.
180 * Ptr to the created curses window if successful,
181 * Null ptr otherwise.
184 * Nothing interesting.
188 *------------------------------------------------------------------------*/
190 struct gwin *gator_cursesgwin_create(params)
191 struct gator_cursesgwin_params *params;
193 { /*gator_cursesgwin_create*/
195 static char rn[] = "gator_cursesgwin_create"; /*Routine name*/
196 struct gwin *newgwin; /*Ptr to new curses window*/
197 struct gator_cursesgwin *c_data; /*Ptr to curses-specific data*/
198 WINDOW *newcursgwin; /*Ptr to new curses window*/
201 fprintf(stderr, "[%s:%s] Allocating %d bytes for new gwin structure\n", mn, rn, sizeof(struct gwin));
202 newgwin = (struct gwin *) malloc(sizeof(struct gwin));
203 if (newgwin == (struct gwin *)0) {
204 fprintf(stderr, "[%s:%s] Can't malloc() %d bytes for new gwin structure: Errno is %d\n", mn, rn, sizeof(struct gwin), errno);
205 return((struct gwin *)0);
208 newgwin->w_type = GATOR_WIN_CURSES;
209 newgwin->w_x = params->gwin_params.cr_x;
210 newgwin->w_y = params->gwin_params.cr_y;
211 newgwin->w_width = params->gwin_params.cr_width;
212 newgwin->w_height = params->gwin_params.cr_height;
213 newgwin->w_changed = 1;
214 newgwin->w_op = &curses_gwinops;
215 newgwin->w_parent = params->gwin_params.cr_parentwin;
218 fprintf(stderr, "[%s:%s] Allocating %d bytes for curses window private space\n", mn, rn, sizeof(struct gator_cursesgwin));
219 c_data = (struct gator_cursesgwin *) malloc(sizeof(struct gator_cursesgwin));
220 if (c_data == (struct gator_cursesgwin *)0){
221 fprintf(stderr, "[%s:%s] Can't allocate %d bytes for curses window private space\n", mn, rn, sizeof(struct gator_cursesgwin));
223 return((struct gwin *)0);
226 newcursgwin = newwin(newgwin->w_height, /*Number of lines*/
227 newgwin->w_width, /*Number of columns*/
228 newgwin->w_y, /*Beginning y value*/
229 newgwin->w_x); /*Beginning x value*/
230 if (newcursgwin == (WINDOW *)0) {
231 fprintf(stderr, "[%s:%s] Failed to create curses window via newwin()\n", mn, rn);
234 return((struct gwin *)0);
238 * Now, fill in the curses-specific window info.
240 c_data->wp = newcursgwin;
241 c_data->charwidth = params->charwidth;
242 c_data->charheight = params->charheight;
243 c_data->box_vertchar = params->box_vertchar;
244 c_data->box_horizchar = params->box_horizchar;
247 * Plug in a frame at the top-level.
249 newgwin->w_frame = gtxframe_Create();
252 * Plug the curses private data into the generic window object, then
253 * return the new window's info.
255 newgwin->w_data = (int *) c_data;
258 } /*gator_cursesgwin_create*/
260 /*------------------------------------------------------------------------
261 * gator_cursesgwin_cleanup
264 * Clean up, probably right before the caller exits.
267 * struct gwin *gwp : Ptr to base window.
271 * Error value otherwise.
274 * Nothing interesting.
278 *------------------------------------------------------------------------*/
280 int gator_cursesgwin_cleanup(gwp)
283 { /*gator_cursesgwin_cleanup*/
285 static char rn[] = "gator_cursesgwin_cleanup"; /*Routine name*/
286 struct gator_cursesgwin *cwp; /*Curses private area ptr*/
288 cwp = (struct gator_cursesgwin *)(gwp->w_data);
291 * Cleaning up in curses is extremely easy - one simple call. We also
292 * want to clear the screen before we go.
295 fprintf(stderr, "[%s:%s] Calling wclear() on window at 0x%x\n", mn, rn, cwp->wp);
300 * Now, set the terminal back into normal mode.
305 fprintf(stderr, "[%s:%s] Calling endwin()\n", mn, rn);
310 } /*gator_cursesgwin_cleanup*/
312 /*------------------------------------------------------------------------
313 * gator_cursesgwin_box
316 * Draw a box around the given curses window.
319 * struct gwin *gwp : Ptr to the curses window to draw
324 * Error value otherwise.
327 * Nothing interesting.
331 *------------------------------------------------------------------------*/
333 int gator_cursesgwin_box(gwp)
336 { /*gator_cursesgwin_box*/
338 static char rn[] = "gator_cursesgwin_box"; /*Routine name*/
339 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
341 cwp = (struct gator_cursesgwin *)(gwp->w_data);
343 fprintf(stderr, "[%s:%s] Calling box() on window at 0x%x\n", mn, rn, cwp->wp);
344 box(cwp->wp, cwp->box_vertchar, cwp->box_horizchar);
348 } /*gator_cursesgwin_box*/
350 /*------------------------------------------------------------------------
351 * gator_cursesgwin_clear
354 * Clear out the given curses window.
357 * struct gwin *gwp : Ptr to the curses window to clear out.
361 * Error value otherwise.
364 * Nothing interesting.
368 *------------------------------------------------------------------------*/
370 int gator_cursesgwin_clear(gwp)
373 { /*gator_cursesgwin_clear*/
375 static char rn[] = "gator_cursesgwin_clear"; /*Routine name*/
376 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
379 * Clearing windows is very easy in curses; just one call will do it.
381 cwp = (struct gator_cursesgwin *)(gwp->w_data);
383 fprintf(stderr, "[%s:%s] Calling wclear() on window at 0x%x\n", mn, rn, cwp->wp);
388 } /*gator_cursesgwin_clear*/
390 /*------------------------------------------------------------------------
391 * gator_cursesgwin_destroy
394 * Destroy the given curses window.
397 * struct gwin *gwp : Ptr to the curses window to destroy.
401 * Error value otherwise.
404 * Nothing interesting.
408 *------------------------------------------------------------------------*/
410 int gator_cursesgwin_destroy(gwp)
413 { /*gator_cursesgwin_destroy*/
415 static char rn[] = "gator_cursesgwin_destroy"; /*Routine name*/
416 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
418 cwp = (struct gator_cursesgwin *)(gwp->w_data);
420 fprintf(stderr, "[%s:%s] Calling delwin() on window at 0x%x\n", mn, rn, cwp->wp);
425 } /*gator_cursesgwin_destroy*/
427 /*------------------------------------------------------------------------
428 * gator_cursesgwin_display
431 * Display/redraw the given curses window.
434 * struct gwin *gwp : Ptr to the curses window to draw.
438 * Error value otherwise.
441 * Nothing interesting.
445 *------------------------------------------------------------------------*/
447 int gator_cursesgwin_display(gwp)
450 { /*gator_cursesgwin_display*/
452 struct gator_cursesgwin *cwp; /*Curses private area ptr*/
454 cwp = (struct gator_cursesgwin *)(gwp->w_data);
456 wclear(cwp->wp); /* clear screen */
457 gtxframe_Display(gwp->w_frame, gwp); /* display the frame */
458 wrefresh(cwp->wp); /* redraw the guy */
461 } /*gator_cursesgwin_display*/
463 /*------------------------------------------------------------------------
464 * gator_cursesgwin_drawline
467 * Draw a line between two points in the given curses
471 * struct gwin *gwp : Ptr to the curses window in which
472 * the line is to be drawn.
473 * struct gwin_lineparams *params : Ptr to other params.
477 * Error value otherwise.
480 * Nothing interesting.
484 *------------------------------------------------------------------------*/
486 int gator_cursesgwin_drawline(gwp, params)
488 struct gwin_lineparams *params;
490 { /*gator_cursesgwin_drawline*/
492 static char rn[] = "gator_cursesgwin_drawline"; /*Routine name*/
495 fprintf(stderr, "[%s:%s] This routine is currently a no-op\n", mn, rn);
499 } /*gator_cursesgwin_drawline*/
501 /*------------------------------------------------------------------------
502 * gator_cursesgwin_drawrectangle
505 * Draw a rectangle in the given curses window.
508 * struct gwin *gwp : Ptr to the curses window in which
509 * the rectangle is to be drawn.
510 * struct gwin_rectparams *params : Ptr to other params.
514 * Error value otherwise.
517 * Nothing interesting.
521 *------------------------------------------------------------------------*/
523 int gator_cursesgwin_drawrectangle(gwp, params)
525 struct gwin_rectparams *params;
527 { /*gator_cursesgwin_drawrectangle*/
529 static char rn[] = "gator_cursesgwin_drawrectangle"; /*Routine name*/
532 fprintf(stderr, "[%s:%s] This routine is currently a no-op\n", mn, rn);
536 } /*gator_cursesgwin_drawrectangle*/
538 /*------------------------------------------------------------------------
539 * gator_cursesgwin_drawchar
542 * Draw a character in the given curses window.
545 * struct gwin *gwp : Ptr to the curses window in which
546 * the character is to be drawn.
547 * struct gwin_charparams *params : Ptr to other params.
551 * Error value otherwise.
554 * Nothing interesting.
558 *------------------------------------------------------------------------*/
560 int gator_cursesgwin_drawchar(gwp, params)
562 struct gwin_charparams *params;
564 { /*gator_cursesgwin_drawchar*/
566 static char rn[] = "gator_cursesgwin_drawchar"; /*Routine name*/
567 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
568 int curses_x, curses_y; /*Mapped x,y positions*/
570 cwp = (struct gator_cursesgwin *)(gwp->w_data);
571 curses_x = GATOR_MAP_X_TO_COL(cwp, params->x);
572 curses_y = GATOR_MAP_Y_TO_LINE(cwp, params->y);
574 fprintf(stderr, "[%s:%s] Drawing char '%c' on window at 0x%x at (%d, %d) [line %d, column %d]%s\n", mn, rn, params->c, cwp->wp, params->x, params->y, curses_y, curses_x, (params->highlight ? ", using standout mode" : ""));
575 wmove(cwp->wp, curses_y, curses_x);
576 if (params->highlight)
578 waddch(cwp->wp, params->c);
579 if (params->highlight)
584 } /*gator_cursesgwin_drawchar*/
586 /*------------------------------------------------------------------------
587 * gator_cursesgwin_drawstring
590 * Draw a string in the given curses window.
593 * struct gwin *gwp : Ptr to the curses window in which
594 * the string is to be drawn.
595 * struct gwin_strparams *params : Ptr to other params.
599 * Error value otherwise.
602 * Nothing interesting.
606 *------------------------------------------------------------------------*/
608 int gator_cursesgwin_drawstring(gwp, params)
610 struct gwin_strparams *params;
612 { /*gator_cursesgwin_drawstring*/
614 static char rn[] = "gator_cursesgwin_drawstring"; /*Routine name*/
615 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
616 int curses_x, curses_y; /*Mapped x,y positions*/
618 cwp = (struct gator_cursesgwin *)(gwp->w_data);
619 curses_x = GATOR_MAP_X_TO_COL(cwp, params->x);
620 curses_y = GATOR_MAP_Y_TO_LINE(cwp, params->y);
622 fprintf(stderr, "[%s:%s] Drawing string '%s' on window at 0x%x at (%d, %d) [line %d, column %d]%s\n", mn, rn, params->s, cwp->wp, params->x, params->y, curses_y, curses_x, (params->highlight ? ", using standout mode" : ""));
623 wmove(cwp->wp, curses_y, curses_x);
624 if (params->highlight)
626 waddstr(cwp->wp, params->s);
627 if (params->highlight)
632 } /*gator_cursesgwin_drawstring*/
634 /*------------------------------------------------------------------------
635 * gator_cursesgwin_invert
638 * Invert a region in the given curses window.
641 * struct gwin *gwp : Ptr to the curses window in which
642 * the inverted region lies.
643 * struct gwin_invparams *params : Ptr to other params.
647 * Error value otherwise.
650 * Nothing interesting.
654 *------------------------------------------------------------------------*/
656 int gator_cursesgwin_invert(gwp, params)
658 struct gwin_invparams *params;
660 { /*gator_cursesgwin_invert*/
662 static char rn[] = "gator_cursesgwin_invert"; /*Routine name*/
665 fprintf(stderr, "[%s:%s] This routine is currently a no-op\n", mn, rn);
669 } /*gator_cursesgwin_invert*/
671 /*------------------------------------------------------------------------
672 * gator_cursesgwin_getchar
675 * Pick up a character from the given window.
678 * struct gwin *gwp : Ptr to the curses window to listen to.
681 * Value of the character read,
685 * Nothing interesting.
689 *------------------------------------------------------------------------*/
691 int gator_cursesgwin_getchar(gwp)
694 { /*gator_cursesgwin_getchar*/
698 } /*gator_cursesgwin_getchar*/
700 /*------------------------------------------------------------------------
701 * gator_cursesgwin_wait
704 * Wait until input is available.
707 * struct gwin *gwp : Ptr to the curses window to wait on.
711 * Error value otherwise.
714 * Nothing interesting.
718 *------------------------------------------------------------------------*/
720 int gator_cursesgwin_wait(gwp)
723 { /*gator_cursesgwin_wait*/
725 while (!LWP_WaitForKeystroke(-1));
729 } /*gator_cursesgwin_wait*/
731 /*------------------------------------------------------------------------
732 * gator_cursesgwin_getdimensions
735 * Get the window's X,Y dimensions.
738 * struct gwin *gwp : Ptr to the curses window to examine.
739 * struct gwin_sizeparams *params : Ptr to the size params to set.
743 * Error value otherwise.
746 * Nothing interesting.
750 *------------------------------------------------------------------------*/
752 int gator_cursesgwin_getdimensions(gwp, aparms)
753 struct gwin_sizeparams *aparms;
756 { /*gator_cursesgwin_getdimensions*/
758 struct gator_cursesgwin *cwp; /*Curses-specific data*/
760 cwp = (struct gator_cursesgwin *)(gwp->w_data);
761 #ifdef AFS_DARWIN_ENV
762 aparms->maxx = cwp->wp->maxx;
763 aparms->maxy = cwp->wp->maxy;
765 aparms->maxx = cwp->wp->_maxx;
766 aparms->maxy = cwp->wp->_maxy;
771 } /*gator_cursesgwin_getdimensions*/