cleanup exit path
[lttngtop.git] / src / cursesdisplay.c
index 577643064685eb64066e49d4146eeadf7d664530..02764dd19976996f13fb1f11a580531aa3285b60 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Julien Desfossez
+ * Copyright (C) 2011-2012 Julien Desfossez
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License Version 2 as
@@ -43,6 +43,7 @@ PANEL *pref_panel, *main_panel;
 
 int pref_panel_visible = 0;
 int pref_line_selected = 0;
+int pref_current_sort = 0;
 
 int last_display_index, currently_displayed_index;
 
@@ -73,11 +74,15 @@ void reset_ncurses()
 {
        curs_set(1);
        endwin();
-       exit(0);
+       quit = 1;
+       sem_post(&pause_sem);
+       sem_post(&timer);
+       sem_post(&goodtodisplay);
 }
 
 static void handle_sigterm(int signal)
 {
+       pthread_cancel(keyboard_thread);
        reset_ncurses();
 }
 
@@ -118,6 +123,7 @@ void init_screen()
                define_key("\033[17;2~", KEY_F(18));
        }
        signal(SIGTERM, handle_sigterm);
+       signal(SIGINT, handle_sigterm);
        mousemask(BUTTON1_CLICKED, NULL);
        refresh();
 }
@@ -301,23 +307,6 @@ void basic_header()
        wrefresh(header);
 }
 
-struct tm format_timestamp(uint64_t timestamp)
-{
-       struct tm tm;
-       uint64_t ts_sec = 0, ts_nsec;
-       time_t time_s;
-
-       ts_nsec = timestamp;
-       ts_sec += ts_nsec / NSEC_PER_SEC;
-       ts_nsec = ts_nsec % NSEC_PER_SEC;
-
-       time_s = (time_t) ts_sec;
-
-       localtime_r(&time_s, &tm);
-
-       return tm;
-}
-
 static void scale_unit(uint64_t bytes, char *ret)
 {
        if (bytes >= 1000000000)
@@ -329,6 +318,7 @@ static void scale_unit(uint64_t bytes, char *ret)
        else
                sprintf(ret, "%" PRIu64, bytes);
 }
+
 uint64_t total_io()
 {
        int i;
@@ -555,8 +545,10 @@ void update_cputop_display()
        wattron(center, A_BOLD);
        column = 1;
        for (i = 0; i < 4; i++) {
-               if (cputopview[i].sort)
+               if (cputopview[i].sort) {
                        wattron(center, A_UNDERLINE);
+                       pref_current_sort = i;
+               }
                mvwprintw(center, 1, column, cputopview[i].title);
                wattroff(center, A_UNDERLINE);
                column += 10;
@@ -702,8 +694,10 @@ void update_process_details()
        wattron(center, A_BOLD);
        column = 1;
        for (i = 0; i < 3; i++) {
-               if (fileview[i].sort)
+               if (fileview[i].sort) {
+                       pref_current_sort = i;
                        wattron(center, A_UNDERLINE);
+               }
                mvwprintw(center, line, column, fileview[i].title);
                wattroff(center, A_UNDERLINE);
                column += 10;
@@ -772,16 +766,20 @@ void update_perf()
        g_hash_table_iter_init(&iter, global_perf_liszt);
        while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
                if (perfn1->visible) {
+                       if (perfn1->sort) {
+                               /* pref_current_sort = i; */
+                               wattron(center, A_UNDERLINE);
+                       }
                        /* + 5 to strip the "perf_" prefix */
                        mvwprintw(center, 1, perf_row, "%s",
                                        (char *) key + 5);
+                       wattroff(center, A_UNDERLINE);
                        perf_row += 20;
                }
                if (perfn1->sort) {
                        perf_key = (char *) key;
                }
        }
-
        wattroff(center, A_BOLD);
 
        g_ptr_array_sort_with_data(data->process_table, sort_perf, perf_key);
@@ -848,8 +846,10 @@ void update_iostream()
        mvwprintw(center, 1, 22, "NAME");
        column = 40;
        for (i = 0; i < 3; i++) {
-               if (iostreamtopview[i].sort)
+               if (iostreamtopview[i].sort) {
+                       pref_current_sort = i;
                        wattron(center, A_UNDERLINE);
+               }
                mvwprintw(center, 1, column, iostreamtopview[i].title);
                wattroff(center, A_UNDERLINE);
                column += 12;
@@ -943,6 +943,25 @@ void update_current_view()
        sem_post(&update_display_sem);
 }
 
+void update_process_detail_sort(int *line_selected)
+{
+       int i;
+       int size;
+
+       size = 3;
+
+       if (*line_selected > (size - 1))
+               *line_selected = size - 1;
+       else if (*line_selected < 0)
+               *line_selected = 0;
+
+       if (fileview[*line_selected].sort == 1)
+               fileview[*line_selected].reverse = 1;
+       for (i = 0; i < size; i++)
+               fileview[i].sort = 0;
+       fileview[*line_selected].sort = 1;
+}
+
 void update_process_detail_pref(int *line_selected, int toggle_view, int toggle_sort)
 {
        int i;
@@ -969,12 +988,10 @@ void update_process_detail_pref(int *line_selected, int toggle_view, int toggle_
 
        if (*line_selected > (size - 1))
                *line_selected = size - 1;
+       else if (*line_selected < 0)
+               *line_selected = 0;
        if (toggle_sort == 1) {
-               if (fileview[*line_selected].sort == 1)
-                       fileview[*line_selected].reverse = 1;
-               for (i = 0; i < size; i++)
-                       fileview[i].sort = 0;
-               fileview[*line_selected].sort = 1;
+               update_process_detail_sort(line_selected);
                update_current_view();
        }
 
@@ -995,6 +1012,24 @@ void update_process_detail_pref(int *line_selected, int toggle_view, int toggle_
        doupdate();
 }
 
+void update_iostream_sort(int *line_selected)
+{
+       int i;
+       int size;
+
+       size = 3;
+       if (*line_selected > (size - 1))
+               *line_selected = size - 1;
+       else if (*line_selected < 0)
+               *line_selected = 0;
+       if (iostreamtopview[*line_selected].sort == 1)
+               iostreamtopview[*line_selected].reverse = 1;
+       for (i = 0; i < size; i++)
+               iostreamtopview[i].sort = 0;
+       iostreamtopview[*line_selected].sort = 1;
+
+}
+
 void update_iostream_pref(int *line_selected, int toggle_view, int toggle_sort)
 {
        int i;
@@ -1021,12 +1056,10 @@ void update_iostream_pref(int *line_selected, int toggle_view, int toggle_sort)
 
        if (*line_selected > (size - 1))
                *line_selected = size - 1;
+       else if (*line_selected < 0)
+               *line_selected = 0;
        if (toggle_sort == 1) {
-               if (iostreamtopview[*line_selected].sort == 1)
-                       iostreamtopview[*line_selected].reverse = 1;
-               for (i = 0; i < size; i++)
-                       iostreamtopview[i].sort = 0;
-               iostreamtopview[*line_selected].sort = 1;
+               update_iostream_sort(line_selected);
                update_current_view();
        }
 
@@ -1047,6 +1080,26 @@ void update_iostream_pref(int *line_selected, int toggle_view, int toggle_sort)
        doupdate();
 }
 
+void update_cpu_sort(int *line_selected)
+{
+       int i;
+       int size = 3;
+
+       if (*line_selected > (size - 1))
+               *line_selected = size - 1;
+       else if (*line_selected < 0)
+               *line_selected = 0;
+
+       /* special case, we don't support sorting by procname for now */
+       if (*line_selected != 3) {
+               if (cputopview[*line_selected].sort == 1)
+                       cputopview[*line_selected].reverse = 1;
+               for (i = 0; i < size; i++)
+                       cputopview[i].sort = 0;
+               cputopview[*line_selected].sort = 1;
+       }
+}
+
 void update_cpu_pref(int *line_selected, int toggle_view, int toggle_sort)
 {
        int i;
@@ -1073,16 +1126,11 @@ void update_cpu_pref(int *line_selected, int toggle_view, int toggle_sort)
 
        if (*line_selected > (size - 1))
                *line_selected = size - 1;
+       else if (*line_selected < 0)
+               *line_selected = 0;
        if (toggle_sort == 1) {
-               /* special case, we don't support sorting by procname for now */
-               if (*line_selected != 3) {
-                       if (cputopview[*line_selected].sort == 1)
-                               cputopview[*line_selected].reverse = 1;
-                       for (i = 0; i < size; i++)
-                               cputopview[i].sort = 0;
-                       cputopview[*line_selected].sort = 1;
-                       update_current_view();
-               }
+               update_cpu_sort(line_selected);
+               update_current_view();
        }
 
        for (i = 0; i < size; i++) {
@@ -1102,6 +1150,32 @@ void update_cpu_pref(int *line_selected, int toggle_view, int toggle_sort)
        doupdate();
 }
 
+void update_perf_sort(int *line_selected)
+{
+       int i;
+       struct perfcounter *perf;
+       GList *perflist;
+       int size;
+
+       size = g_hash_table_size(global_perf_liszt);
+       if (*line_selected > (size - 1))
+               *line_selected = size - 1;
+       else if (*line_selected < 0)
+               *line_selected = 0;
+
+       i = 0;
+       perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt));
+       while (perflist) {
+               perf = g_hash_table_lookup(global_perf_liszt, perflist->data);
+               if (i != *line_selected)
+                       perf->sort = 0;
+               else
+                       perf->sort = 1;
+               i++;
+               perflist = g_list_next(perflist);
+       }
+}
+
 void update_perf_pref(int *line_selected, int toggle_view, int toggle_sort)
 {
        int i;
@@ -1128,18 +1202,13 @@ void update_perf_pref(int *line_selected, int toggle_view, int toggle_sort)
                        " 's' : sort, space : toggle");
        wattroff(pref_panel_window, A_BOLD);
 
+       if (*line_selected > (size - 1))
+               *line_selected = size - 1;
+       else if (*line_selected < 0)
+               *line_selected = 0;
+
        if (toggle_sort == 1) {
-               i = 0;
-               perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt));
-               while (perflist) {
-                       perf = g_hash_table_lookup(global_perf_liszt, perflist->data);
-                       if (i != *line_selected)
-                               perf->sort = 0;
-                       else
-                               perf->sort = 1;
-                       i++;
-                       perflist = g_list_next(perflist);
-               }
+               update_perf_sort(line_selected);
                update_current_view();
        }
 
@@ -1194,6 +1263,32 @@ int update_preference_panel(int *line_selected, int toggle_view, int toggle_sort
        return ret;
 }
 
+int update_sort(int *line_selected)
+{
+       int ret = 0;
+
+       switch(current_view) {
+               case perf:
+                       update_perf_sort(line_selected);
+                       break;
+               case cpu:
+                       update_cpu_sort(line_selected);
+                       break;
+               case iostream:
+                       update_iostream_sort(line_selected);
+                       break;
+               case process_details:
+                       update_process_detail_sort(line_selected);
+                       break;
+               default:
+                       ret = -1;
+                       break;
+       }
+
+       return ret;
+}
+
+
 void toggle_pref_panel(void)
 {
        int ret;
@@ -1335,6 +1430,26 @@ void *handle_keyboard(void *p)
                        if (pref_panel_visible)
                                update_preference_panel(&pref_line_selected, 0, 1);
                        break;
+               case '>':
+                       /* perf uses a hashtable, it is ordered backward */
+                       if (current_view == perf) {
+                               pref_current_sort--;
+                       } else if (!pref_panel_visible) {
+                               pref_current_sort++;
+                       }
+                       update_sort(&pref_current_sort);
+                       update_current_view();
+                       break;
+               case '<':
+                       /* perf uses a hashtable, it is ordered backward */
+                       if (current_view == perf) {
+                               pref_current_sort++;
+                       } else if (!pref_panel_visible) {
+                               pref_current_sort--;
+                       }
+                       update_sort(&pref_current_sort);
+                       update_current_view();
+                       break;
 
                case 13: /* FIXME : KEY_ENTER ?? */
                        if (pref_panel_visible)
@@ -1346,6 +1461,7 @@ void *handle_keyboard(void *p)
                                current_view = previous_view;
                                previous_view = process_details;
                        }
+                       selected_line = 0;
                        update_current_view();
                        break;
 
@@ -1380,6 +1496,8 @@ void *handle_keyboard(void *p)
                case KEY_F(10):
                case 'q':
                        reset_ncurses();
+                       /* exit keyboard thread */
+                       pthread_exit(0);
                        break;
                case 't':
                        toggle_threads *= -1;
This page took 0.026358 seconds and 4 git commands to generate.