From patchwork Mon Jun 20 22:58:19 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 2099 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 3D63523E52 for ; Mon, 20 Jun 2011 22:58:59 +0000 (UTC) Received: from mail-vx0-f180.google.com (mail-vx0-f180.google.com [209.85.220.180]) by fiordland.canonical.com (Postfix) with ESMTP id 0F2C4A18350 for ; Mon, 20 Jun 2011 22:58:58 +0000 (UTC) Received: by mail-vx0-f180.google.com with SMTP id 7so2987210vxd.11 for ; Mon, 20 Jun 2011 15:58:58 -0700 (PDT) Received: by 10.52.112.106 with SMTP id ip10mr4671425vdb.127.1308610738792; Mon, 20 Jun 2011 15:58:58 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.52.183.130 with SMTP id em2cs32813vdc; Mon, 20 Jun 2011 15:58:58 -0700 (PDT) Received: by 10.227.55.74 with SMTP id t10mr271375wbg.91.1308610735759; Mon, 20 Jun 2011 15:58:55 -0700 (PDT) Received: from smtp.smtpout.orange.fr (smtp03.smtpout.orange.fr [80.12.242.125]) by mx.google.com with ESMTP id fr1si10283195wbb.36.2011.06.20.15.58.55; Mon, 20 Jun 2011 15:58:55 -0700 (PDT) Received-SPF: neutral (google.com: 80.12.242.125 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) client-ip=80.12.242.125; Authentication-Results: mx.google.com; spf=neutral (google.com: 80.12.242.125 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) smtp.mail=daniel.lezcano@linaro.org Received: from monster.dhcp.lxc ([92.134.95.191]) by mwinf5d06 with ME id yNyp1g00347kPVY03NyuDD; Tue, 21 Jun 2011 00:58:55 +0200 From: Daniel Lezcano To: linaro-dev@lists.linaro.org Subject: [powerdebug 11/17] implement the find callback Date: Tue, 21 Jun 2011 00:58:19 +0200 Message-Id: <1308610705-23281-11-git-send-email-daniel.lezcano@linaro.org> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1308610705-23281-1-git-send-email-daniel.lezcano@linaro.org> References: <1308610705-23281-1-git-send-email-daniel.lezcano@linaro.org> Signed-off-by: Daniel Lezcano --- display.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 113 insertions(+), 8 deletions(-) diff --git a/display.c b/display.c index be78ce0..12eb052 100644 --- a/display.c +++ b/display.c @@ -17,7 +17,8 @@ #include #include #include -#include +#include +#include #include "powerdebug.h" #include "mainloop.h" #include "regulator.h" @@ -98,11 +99,11 @@ static int display_show_header(int win) #define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \ "'Right' , 'Up', 'Down', 'enter', , 'Esc'" -static int display_show_footer(int win) +static int display_show_footer(int win, char *string) { werase(footer_win); wattron(footer_win, A_REVERSE); - mvwprintw(footer_win, 0, 0, "%s", footer_label); + mvwprintw(footer_win, 0, 0, "%s", string ? string : footer_label); wattroff(footer_win, A_REVERSE); wrefresh(footer_win); @@ -274,12 +275,78 @@ int display_print_line(int win, int line, char *str, int bold, void *data) static int display_find_keystroke(int fd, void *data); +struct find_data { + size_t len; + char *string; + regex_t *reg; +}; + +struct find_data *display_find_form_init(void) +{ + const char *regexp = "^[a-z|0-9|_|-|.]"; + struct find_data *findd; + const size_t len = 64; + regex_t *reg; + char *search4; + int maxx, maxy; + + getmaxyx(stdscr, maxy, maxx); + + reg = malloc(sizeof(*reg)); + if (!reg) + return NULL; + + if (regcomp(reg, regexp, REG_ICASE)) + goto out_free_reg; + + search4 = malloc(len); + if (!search4) + goto out_free_regcomp; + memset(search4, '\0', len); + + findd = malloc(sizeof(*findd)); + if (!findd) + goto out_free_search4; + + findd->string = search4; + findd->reg = reg; + findd->len = len; +out: + return findd; + +out_free_search4: + free(search4); +out_free_regcomp: + regfree(reg); +out_free_reg: + free(reg); + + goto out; +} + +static void display_find_form_fini(struct find_data *fd) +{ + regfree(fd->reg); + free(fd->string); + free(fd); + curs_set(0); +} + static int display_switch_to_find(int fd) { + struct find_data *findd; + + findd = display_find_form_init(); + if (!findd) + return -1; + if (mainloop_del(fd)) return -1; - if (mainloop_add(fd, display_find_keystroke, NULL)) + if (mainloop_add(fd, display_find_keystroke, findd)) + return -1; + + if (display_show_footer(current_win, "find (esc to exit)?")) return -1; return 0; @@ -342,24 +409,62 @@ static int display_switch_to_main(int fd) if (mainloop_add(fd, display_keystroke, NULL)) return -1; - display_refresh(current_win); + if (display_show_header(current_win)) + return -1; - return 0; -} + if (display_show_footer(current_win, NULL)) + return -1; + return display_refresh(current_win); +} static int display_find_keystroke(int fd, void *data) { + struct find_data *findd = data; + regex_t *reg = findd->reg; + char *string = findd->string; int keystroke = getch(); + char match[2] = { [0] = (char)keystroke, [1] = '\0' }; + regmatch_t m[1]; + switch (keystroke) { case '\e': + display_find_form_fini(findd); return display_switch_to_main(fd); + + case KEY_BACKSPACE: + if (strlen(string)) + string[strlen(string) - 1] = '\0'; + break; + + case KEY_ENTER: + /* next patch */ + break; + default: + + /* We don't want invalid characters for a name */ + if (regexec(reg, match, 1, m, 0)) + return 0; + + if (strlen(string) < findd->len - 1) + string[strlen(string)] = (char)keystroke; + break; } + if (display_show_header(current_win)) + return -1; + + if (display_refresh(current_win)) + return -1; + + if (display_show_footer(current_win, strlen(string) ? string : + "find (esc to exit)?")) + return -1; + return 0; } @@ -423,7 +528,7 @@ int display_init(int wdefault) if (display_show_header(wdefault)) return -1; - if (display_show_footer(wdefault)) + if (display_show_footer(wdefault, NULL)) return -1; return display_refresh(wdefault);