1/* 2 * Copyright (c) 2008 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * The contents of this file constitute Original Code as defined in and 7 * are subject to the Apple Public Source License Version 1.1 (the 8 * "License"). You may not use this file except in compliance with the 9 * License. Please obtain a copy of the License at 10 * http://www.apple.com/publicsource and read it before using this file. 11 * 12 * This Original Code and all software distributed under the License are 13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the 17 * License for the specific language governing rights and limitations 18 * under the License. 19 * 20 * @APPLE_LICENSE_HEADER_END@ 21 */ 22 23#include <stdbool.h> 24#include <stdlib.h> 25#include <ctype.h> 26#include <string.h> 27#include <curses.h> 28#include "preferences.h" 29#include "userinput.h" 30 31static void sleep_completion(void *tinst, struct user_input_state *s) { 32 int delay = 0; 33 char *p; 34 bool got_digit = false; 35 36 if(!strlen(s->buf)) { 37 /* Use the current default. */ 38 user_input_set_state(NULL); 39 return; 40 } 41 42 for(p = s->buf; *p; ++p) { 43 if(isdigit(*p)) { 44 got_digit = true; 45 } else { 46 user_input_set_error_state("not a valid sleep delay"); 47 return; 48 } 49 } 50 51 if(!strlen(s->buf) || !got_digit) { 52 user_input_set_error_state("not a valid sleep delay"); 53 return; 54 } 55 56 delay = atoi(s->buf); 57 58 if(delay < 0) { 59 user_input_set_error_state("delay is negative"); 60 return; 61 } 62 63 top_prefs_set_sleep(delay); 64 65 user_input_set_state(NULL); 66} 67 68static void sleep_draw(void *tinst, struct user_input_state *s, WINDOW *win, 69 int row, int column) { 70 char display[60]; 71 72 if(-1 == snprintf(display, sizeof(display), "update interval[%d]: %s\n", 73 top_prefs_get_sleep(), s->buf)) 74 return; 75 76 mvwaddstr(win, row, column, display); 77} 78 79struct user_input_state top_user_input_sleep_state = { 80 .offset = 0, 81 .completion = sleep_completion, 82 .draw = sleep_draw 83}; 84