home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************/
- /* Copyright 1988 by Chuck Musciano and Harris Corporation */
- /* */
- /* Permission to use, copy, modify, and distribute this software */
- /* and its documentation for any purpose and without fee is */
- /* hereby granted, provided that the above copyright notice */
- /* appear in all copies and that both that copyright notice and */
- /* this permission notice appear in supporting documentation, and */
- /* that the name of Chuck Musciano and Harris Corporation not be */
- /* used in advertising or publicity pertaining to distribution */
- /* of the software without specific, written prior permission. */
- /* Chuck Musciano and Harris Corporation make no representations */
- /* about the suitability of this software for any purpose. It is */
- /* provided "as is" without express or implied warranty. */
- /************************************************************************/
-
-
- #include <suntool/sunview.h>
- #include <suntool/panel.h>
- #include <suntool/canvas.h>
-
- #include <stdio.h>
-
- #define LPQ "/usr/ucb/lpq"
-
- #define ICON_PATH "laserwriter.icon"
-
- #define NORMAL_FONT "/usr/lib/fonts/fixedwidthfonts/screen.r.14"
- #define BOLD_FONT "/usr/lib/fonts/fixedwidthfonts/screen.b.14"
-
- #define NO_STATE 0
- #define IDLE_STATE 1
- #define ACTIVE_STATE 2
- #define CHECK_STATE 3
-
- static char *lp_usage = "usage: lpqtool [-a <interval>] [-c] [-i <interval>] [-o] [-r <rows>] [-t <idle threshold>] [ <printer> ]\n";
-
- static short lp_icon_image[] = {
- #include ICON_PATH
- };
- mpr_static(lp_icon_pixrect, 64, 30, 1, lp_icon_image);
-
- struct itimerval active = {{10, 0}, {10, 0}};
- struct itimerval idle = {{60, 0}, {60, 0}};
- int active_open = FALSE;
- int idle_closed = FALSE;
- int idle_threshold = 6;
- int queue_rows = 5;
- int idle_count, stay_idle;
- int state = NO_STATE;
- char user[30];
- char *printer_name;
-
- Icon icon;
- Frame frame;
- Canvas status, queue;
- Panel panel;
- Pixwin *spw, *qpw;
- struct pixfont *normal_font, *bold_font;
- Panel_item a_item, i_item, a_light, i_light, c_light, a_button, i_button, c_button;
-
- /************************************************************************/
- static char *right_str(val, width)
-
- int val;
- int width;
-
- { static char buf[20];
-
- sprintf(buf, "%*d", width, val);
- return(buf);
- }
-
- /************************************************************************/
- static set_status(st)
-
- int st;
-
- {
- panel_set(a_light, PANEL_SHOW_ITEM, FALSE, 0);
- panel_set(i_light, PANEL_SHOW_ITEM, FALSE, 0);
- panel_set(c_light, PANEL_SHOW_ITEM, FALSE, 0);
- if (st == IDLE_STATE)
- panel_set(i_light, PANEL_SHOW_ITEM, TRUE, 0);
- else if (st == ACTIVE_STATE)
- panel_set(a_light, PANEL_SHOW_ITEM, TRUE, 0);
- else if (st == CHECK_STATE)
- panel_set(c_light, PANEL_SHOW_ITEM, TRUE, 0);
- }
- /************************************************************************/
- static update_queue()
-
- { FILE *f;
- char buf[256];
- int line = 2, new_state = NO_STATE;
-
- set_status(CHECK_STATE);
- sprintf(buf, "%s%s%s", LPQ, *printer_name? " -P" : "", printer_name);
- if ((f = popen(buf, "r")) == NULL) {
- pw_output(spw, 2, 5, TRUE, "Unable to obtain queue information");
- pw_rop(qpw, 0, 0, window_get(queue, WIN_WIDTH), window_get(queue, WIN_HEIGHT), PIX_SRC | PIX_COLOR(0), NULL, 0, 0);
- pclose(f);
- return;
- }
- if (fgets(buf, 256, f) != NULL) {
- buf[strlen(buf) - 1] = '\0';
- pw_output(spw, 1, 1, TRUE, "Status:");
- pw_output(spw, 2, 5, TRUE, buf);
- pw_rop(qpw, 0, 0, window_get(queue, WIN_WIDTH), window_get(queue, WIN_HEIGHT), PIX_SRC | PIX_COLOR(0), NULL, 0, 0);
- if (strcmp(buf, "no entries") == 0)
- new_state = IDLE_STATE;
- else
- new_state = ACTIVE_STATE;
- }
- while (fgets(buf, 256, f) != NULL) {
- buf[strlen(buf) - 1] = '\0';
- if (strncmp(buf, "Rank", 4) == 0) {
- if (new_state == IDLE_STATE)
- new_state = ACTIVE_STATE;
- continue;
- }
- else if (verify(buf, " \t\n\r"))
- continue;
- else {
- if (line == 2)
- pw_output(qpw, 1, 1, TRUE, "Rank Owner Job Files Total Size");
- pw_output(qpw, line++, 1, strindex(buf, user), buf);
- }
- }
- pclose(f);
- if (new_state == IDLE_STATE) {
- stay_idle = FALSE;
- if (state == ACTIVE_STATE && --idle_count <= 0)
- go_idle();
- }
- else if (new_state == ACTIVE_STATE && !stay_idle)
- go_active();
- set_status(state);
- }
-
- /************************************************************************/
- static Panel_setting number_proc(item, event)
-
- Panel_item item;
- Event *event;
-
- { char buf[80];
- int val;
-
- strcpy(buf, panel_get_value(item));
- val = atoi(buf);
- if (event_id(event) >= '1' && event_id(event) <= '9') {
- val = val * 10 + event_id(event) - '0';
- panel_set(item, PANEL_VALUE, right_str(val, 5), 0);
- return(PANEL_NONE);
- }
- else if (event_id(event) == '0')
- if (item == a_item && val == 0)
- return(PANEL_NONE);
- else {
- val = val * 10 + event_id(event) - '0';
- panel_set(item, PANEL_VALUE, right_str(val, 5), 0);
- return(PANEL_NONE);
- }
- else if (event_id(event) == '\010') {
- val /= 10;
- panel_set(item, PANEL_VALUE, right_str(val, 5), 0);
- return(PANEL_NONE);
- }
- else if (event_id(event) == '\177') {
- panel_set(item, PANEL_VALUE, " ", 0);
- return(PANEL_NONE);
- }
- else if (event_id(event) == '\n' || event_id(event) == '\r') {
- if (item == a_item) {
- active.it_value.tv_sec = active.it_interval.tv_sec = val;
- if (state == ACTIVE_STATE)
- notify_set_itimer_func(frame, update_queue, ITIMER_REAL, &active, NULL);
- }
- else {
- idle.it_value.tv_sec = idle.it_interval.tv_sec = val;
- if (state == IDLE_STATE)
- notify_set_itimer_func(frame, update_queue, ITIMER_REAL, &idle, NULL);
- }
- return(PANEL_NONE);
- }
- else
- return(PANEL_NONE);
- }
-
- /************************************************************************/
- static go_idle()
-
- {
- if (state == IDLE_STATE)
- return;
- state = IDLE_STATE;
- if (idle_closed)
- window_set(frame, FRAME_CLOSED, TRUE, 0);
- if (idle.it_value.tv_sec != 0)
- notify_set_itimer_func(frame, update_queue, ITIMER_REAL, &idle, NULL);
- else
- notify_set_itimer_func(frame, update_queue, ITIMER_REAL, NULL, NULL);
- set_status(IDLE_STATE);
- }
-
- /************************************************************************/
- static go_active()
-
- {
- idle_count = idle_threshold;
- if (state == ACTIVE_STATE)
- return;
- state = ACTIVE_STATE;
- if (active_open)
- window_set(frame, FRAME_CLOSED, FALSE, 0);
- update_queue();
- notify_set_itimer_func(frame, update_queue, ITIMER_REAL, &active, NULL);
- set_status(ACTIVE_STATE);
- }
-
- /************************************************************************/
- static pw_output(pw, row, col, bold, str)
-
- Pixwin *pw;
- int row;
- int col;
- int bold;
- char *str;
-
- {
- row = (row - 1) * normal_font->pf_defaultsize.y;
- col = (col - 1) * normal_font->pf_defaultsize.x + 4;
- pw_text(pw, col, row - normal_font->pf_char['0'].pc_home.y, PIX_SRC, normal_font, str);
- if (bold)
- pw_text(pw, col + 1, row - normal_font->pf_char['0'].pc_home.y, PIX_SRC | PIX_DST, normal_font, str);
- col += strlen(str) * normal_font->pf_defaultsize.x;
- pw_rop(pw, col, row, pw->pw_pixrect->pr_size.x - col, normal_font->pf_defaultsize.y, PIX_SRC | PIX_COLOR(0), NULL, 0, 0);
- }
-
- /************************************************************************/
- static Notify_value close_proc(frame, event, arg, type)
-
- Frame frame;
- Event *event;
- Notify_arg arg;
- Notify_event_type type;
-
- { int init_closed, curr_closed, is_resize;
- Notify_value value;
- Rect *temp;
-
- init_closed = (int) window_get(frame, FRAME_CLOSED);
- value = notify_next_event_func(frame, event, arg, type);
- curr_closed = (int) window_get(frame, FRAME_CLOSED);
- if (init_closed != curr_closed)
- if (curr_closed) {
- stay_idle = (state == ACTIVE_STATE);
- go_idle();
- }
- else
- go_active();
- return(value);
- }
-
- /************************************************************************/
- main(argc, argv)
-
- int argc;
- char **argv;
-
- { char c, *s, **saveargs();
- int i, interval = 10;
- Rect *r;
-
- normal_font = pf_open(NORMAL_FONT);
- bold_font = pf_open(BOLD_FONT);
- icon = icon_create(ICON_IMAGE, &lp_icon_pixrect,
- ICON_LABEL, NULL,
- ICON_WIDTH, lp_icon_pixrect.pr_size.x,
- ICON_HEIGHT, lp_icon_pixrect.pr_size.y,
- 0);
- frame = window_create(NULL, FRAME,
- FRAME_ICON, icon,
- FRAME_LABEL, "<< LaserWriter Queue Status >>",
- FRAME_ARGC_PTR_ARGV, &argc, argv,
- 0);
-
- argv = saveargs(argc, argv);
- while ((c = getopt(&argc, argv, "a:ci:or:t:", &s)) != EOF)
- switch (c) {
- case 'a' : if (verify(s, "0123456789")) {
- active.it_value.tv_sec = active.it_interval.tv_sec = atoi(s);
- if (active.it_value.tv_sec <= 0)
- abend("invalid active interval: %s\n", s);
- }
- else
- abend("invalid active interval: %s\n", s);
- break;
- case 'c' : idle_closed = TRUE;
- break;
- case 'i' : if (verify(s, "0123456789")) {
- idle.it_value.tv_sec = idle.it_interval.tv_sec = atoi(s);
- if (idle.it_value.tv_sec < 0)
- abend("invalid idle interval: %s\n", s);
- }
- else
- abend("invalid idle interval: %s\n", s);
- break;
- case 'o' : active_open = TRUE;
- break;
- case 'r' : if (verify(s, "0123456789")) {
- queue_rows = atoi(s);
- if (queue_rows <= 0)
- abend("invalid number of queue lines: %s\n", s);
- }
- else
- abend("invalid number of queue lines: %s\n", s);
- break;
- case 't' : if (verify(s, "0123456789")) {
- idle_threshold = atoi(s);
- if (idle_threshold <= 0)
- abend("invalid idle thresold: %s\n", s);
- }
- else
- abend("invalid idle threshold: %s\n", s);
- break;
- case '?' : fprintf(stderr, lp_usage);
- break;
- default : abend(lp_usage);
- break;
- }
-
- panel = window_create(frame, PANEL,
- PANEL_FONT, normal_font,
- WIN_COLUMNS, 75,
- 0);
- a_item = panel_create_item(panel, PANEL_TEXT,
- PANEL_LABEL_STRING, "Active Interval:",
- PANEL_LABEL_BOLD, TRUE,
- PANEL_ITEM_Y, ATTR_ROW(0) + 1,
- PANEL_VALUE_DISPLAY_LENGTH, 5,
- PANEL_VALUE, right_str(active.it_value.tv_sec, 5),
- PANEL_NOTIFY_PROC, number_proc,
- PANEL_NOTIFY_LEVEL, PANEL_ALL,
- 0);
- a_light = panel_create_item(panel, PANEL_MESSAGE,
- PANEL_LABEL_STRING, "Active",
- PANEL_LABEL_BOLD, TRUE,
- 0);
- i_light = panel_create_item(panel, PANEL_MESSAGE,
- PANEL_LABEL_STRING, "Idle",
- PANEL_LABEL_BOLD, TRUE,
- 0);
- c_light = panel_create_item(panel, PANEL_MESSAGE,
- PANEL_LABEL_STRING, "Checking",
- PANEL_LABEL_BOLD, TRUE,
- 0);
- i_item = panel_create_item(panel, PANEL_TEXT,
- PANEL_LABEL_STRING, "Idle Interval: ",
- PANEL_LABEL_BOLD, TRUE,
- PANEL_VALUE_DISPLAY_LENGTH, 5,
- PANEL_ITEM_X, ATTR_COL(0),
- PANEL_ITEM_Y, ATTR_ROW(1) + 4,
- PANEL_VALUE, right_str(idle.it_value.tv_sec, 5),
- PANEL_NOTIFY_PROC, number_proc,
- PANEL_NOTIFY_LEVEL, PANEL_ALL,
- 0);
- a_button = panel_create_item(panel, PANEL_BUTTON,
- PANEL_ITEM_Y, ATTR_ROW(1),
- PANEL_ITEM_X, ATTR_COL(26),
- PANEL_LABEL_IMAGE, better_button_image(panel, "Go Active", 12, 5, bold_font),
- PANEL_NOTIFY_PROC, go_active,
- 0);
- i_button = panel_create_item(panel, PANEL_BUTTON,
- PANEL_ITEM_Y, ATTR_ROW(1),
- PANEL_LABEL_IMAGE, better_button_image(panel, "Go Idle", 12, 5, bold_font),
- PANEL_NOTIFY_PROC, go_idle,
- 0);
- c_button = panel_create_item(panel, PANEL_BUTTON,
- PANEL_ITEM_Y, ATTR_ROW(1),
- PANEL_LABEL_IMAGE, better_button_image(panel, "Check Queue", 12, 5, bold_font),
- PANEL_NOTIFY_PROC, update_queue,
- 0);
- r = (Rect *) panel_get(a_button, PANEL_ITEM_RECT);
- i = r->r_left + r->r_width / 2;
- r = (Rect *) panel_get(a_light, PANEL_ITEM_RECT);
- panel_set(a_light, PANEL_ITEM_X, i - r->r_width / 2, 0);
- r = (Rect *) panel_get(i_button, PANEL_ITEM_RECT);
- i = r->r_left + r->r_width / 2;
- r = (Rect *) panel_get(i_light, PANEL_ITEM_RECT);
- panel_set(i_light, PANEL_ITEM_X, i - r->r_width / 2, 0);
- r = (Rect *) panel_get(c_button, PANEL_ITEM_RECT);
- i = r->r_left + r->r_width / 2;
- r = (Rect *) panel_get(c_light, PANEL_ITEM_RECT);
- panel_set(c_light, PANEL_ITEM_X, i - r->r_width / 2, 0);
- window_fit_height(panel);
-
- status = window_create(frame, CANVAS,
- WIN_HEIGHT, normal_font->pf_defaultsize.y * 2,
- WIN_WIDTH, normal_font->pf_defaultsize.x * 75,
- WIN_X, 0,
- WIN_BELOW, panel,
- 0);
- spw = canvas_pixwin(status);
- queue = window_create(frame, CANVAS,
- WIN_HEIGHT, normal_font->pf_defaultsize.y * (queue_rows + 1),
- WIN_WIDTH, normal_font->pf_defaultsize.x * 75,
- WIN_X, 0,
- WIN_BELOW, status,
- 0);
- qpw = canvas_pixwin(queue);
- window_set(panel, WIN_WIDTH, window_get(queue, WIN_WIDTH), 0);
- window_fit(frame);
-
- if (argc == 1)
- printer_name = "";
- else if (argc == 2)
- printer_name = argv[1];
- else
- abend(lp_usage);
-
- strcpy(user, user_name());
- set_status(NO_STATE);
- if (window_get(frame, FRAME_CLOSED))
- go_idle();
- else
- go_active();
- notify_interpose_event_func(frame, close_proc, NOTIFY_SAFE);
- window_main_loop(frame);
- pf_close(normal_font);
- pf_close(bold_font);
- }
-