MainPanel.c 5.58 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
htop - ColumnsPanel.c
(C) 2004-2015 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "MainPanel.h"
#include "Process.h"
#include "Platform.h"
#include "CRT.h"

#include <stdlib.h>

/*{
#include "Panel.h"
#include "Action.h"
#include "Settings.h"

typedef struct MainPanel_ {
   Panel super;
   State* state;
23
   IncSet* inc;
Hisham Muhammad's avatar
Hisham Muhammad committed
24
25
26
27
   Htop_Action *keys;
   pid_t pidSearch;
} MainPanel;

28
29
30
31
32
33
typedef union {
   int i;
   void* v;
} Arg;

typedef bool(*MainPanel_ForeachProcessFn)(Process*, Arg);
Hisham Muhammad's avatar
Hisham Muhammad committed
34

35
36
#define MainPanel_getFunctionBar(this_) (((Panel*)(this_))->defaultBar)

Hisham Muhammad's avatar
Hisham Muhammad committed
37
38
}*/

Richard's avatar
Richard committed
39
static const char* const MainFunctions[]  = {"Help  ", "Setup ", "Search", "Filter", "Tree  ", "SortBy", "Nice -", "Nice +", "Kill  ", "Quit  ", NULL};
40
41

void MainPanel_updateTreeFunctions(MainPanel* this, bool mode) {
42
   FunctionBar* bar = MainPanel_getFunctionBar(this);
Hisham Muhammad's avatar
Hisham Muhammad committed
43
   if (mode) {
44
45
      FunctionBar_setLabel(bar, KEY_F(5), "Sorted");
      FunctionBar_setLabel(bar, KEY_F(6), "Collap");
Hisham Muhammad's avatar
Hisham Muhammad committed
46
   } else {
47
48
      FunctionBar_setLabel(bar, KEY_F(5), "Tree  ");
      FunctionBar_setLabel(bar, KEY_F(6), "SortBy");
Hisham Muhammad's avatar
Hisham Muhammad committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
   }
}

void MainPanel_pidSearch(MainPanel* this, int ch) {
   Panel* super = (Panel*) this;
   pid_t pid = ch-48 + this->pidSearch;
   for (int i = 0; i < Panel_size(super); i++) {
      Process* p = (Process*) Panel_get(super, i);
      if (p && p->pid == pid) {
         Panel_setSelected(super, i);
         break;
      }
   }
   this->pidSearch = pid * 10;
   if (this->pidSearch > 10000000) {
      this->pidSearch = 0;
   }
}

static HandlerResult MainPanel_eventHandler(Panel* super, int ch) {
   MainPanel* this = (MainPanel*) super;

   HandlerResult result = IGNORED;
   
   Htop_Reaction reaction = HTOP_OK;

75
76
77
   Settings* settings = this->state->settings;
   ScreenSettings* ss = settings->ss;

78
79
80
81
82
   if (EVENT_IS_HEADER_CLICK(ch)) {
      int x = EVENT_HEADER_CLICK_GET_X(ch);
      ProcessList* pl = this->state->pl;
      int hx = super->scrollH + x + 1;
      ProcessField field = ProcessList_keyAt(pl, hx);
83
84
85
      if (field == ss->sortKey) {
         ScreenSettings_invertSortOrder(ss);
         ss->treeView = false;
86
87
88
      } else {
         reaction |= Action_setSortKey(settings, field);
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
89
      reaction |= HTOP_RECALCULATE | HTOP_REDRAW_BAR | HTOP_SAVE_SETTINGS; 
90
91
      result = HANDLED;
   } else if (ch != ERR && this->inc->active) {
92
93
94
      bool filterChanged = IncSet_handleKey(this->inc, ch, super, (IncMode_GetPanelValue) MainPanel_getValue, NULL);
      if (filterChanged) {
         this->state->pl->incFilter = IncSet_filter(this->inc);
95
96
         reaction = HTOP_REFRESH | HTOP_REDRAW_BAR;
      }
97
98
99
100
      if (this->inc->found) {
         reaction |= Action_follow(this->state);
         reaction |= HTOP_KEEP_FOLLOWING;
      }
101
102
103
      result = HANDLED;
   } else if (ch == 27) {
      return HANDLED;
104
   } else if (ch != ERR && ch > 0 && ch < KEY_MAX && this->keys[ch]) {
Hisham Muhammad's avatar
Hisham Muhammad committed
105
106
107
108
109
110
111
      reaction |= (this->keys[ch])(this->state);
      result = HANDLED;
   } else if (isdigit(ch)) {
      MainPanel_pidSearch(this, ch);
   } else {
      if (ch != ERR) {
         this->pidSearch = 0;
112
113
      } else {
         reaction |= HTOP_KEEP_FOLLOWING;
Hisham Muhammad's avatar
Hisham Muhammad committed
114
115
116
117
      }
   }

   if (reaction & HTOP_REDRAW_BAR) {
118
119
      MainPanel_updateTreeFunctions(this, settings->ss->treeView);
      IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]);
Hisham Muhammad's avatar
Hisham Muhammad committed
120
121
122
123
124
   }
   if (reaction & HTOP_UPDATE_PANELHDR) {
      ProcessList_printHeader(this->state->pl, Panel_getHeader(super));
   }
   if (reaction & HTOP_REFRESH) {
125
      result |= REDRAW;
Hisham Muhammad's avatar
Hisham Muhammad committed
126
127
   }      
   if (reaction & HTOP_RECALCULATE) {
128
      result |= RESCAN;
Hisham Muhammad's avatar
Hisham Muhammad committed
129
130
131
132
133
134
135
136
137
   }
   if (reaction & HTOP_SAVE_SETTINGS) {
      this->state->settings->changed = true;
   }
   if (reaction & HTOP_QUIT) {
      return BREAK_LOOP;
   }
   if (!(reaction & HTOP_KEEP_FOLLOWING)) {
      this->state->pl->following = -1;
138
      Panel_setSelectionColor(super, CRT_colors[PANEL_SELECTION_FOCUS]);
Hisham Muhammad's avatar
Hisham Muhammad committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
   }
   return result;
}

int MainPanel_selectedPid(MainPanel* this) {
   Process* p = (Process*) Panel_getSelected((Panel*)this);
   if (p) {
      return p->pid;
   }
   return -1;
}

const char* MainPanel_getValue(MainPanel* this, int i) {
   Process* p = (Process*) Panel_get((Panel*)this, i);
   if (p)
      return p->comm;
   return "";
}

158
bool MainPanel_foreachProcess(MainPanel* this, MainPanel_ForeachProcessFn fn, Arg arg, bool* wasAnyTagged) {
Hisham Muhammad's avatar
Hisham Muhammad committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
   Panel* super = (Panel*) this;
   bool ok = true;
   bool anyTagged = false;
   for (int i = 0; i < Panel_size(super); i++) {
      Process* p = (Process*) Panel_get(super, i);
      if (p->tag) {
         ok = fn(p, arg) && ok;
         anyTagged = true;
      }
   }
   if (!anyTagged) {
      Process* p = (Process*) Panel_getSelected(super);
      if (p) ok = fn(p, arg) && ok;
   }
   if (wasAnyTagged)
      *wasAnyTagged = anyTagged;
   return ok;
}

PanelClass MainPanel_class = {
   .super = {
      .extends = Class(Panel),
      .delete = MainPanel_delete
   },
   .eventHandler = MainPanel_eventHandler
};

186
MainPanel* MainPanel_new() {
Hisham Muhammad's avatar
Hisham Muhammad committed
187
   MainPanel* this = AllocThis(MainPanel);
188
   Panel_init((Panel*) this, 1, 1, 1, 1, Class(Process), false, FunctionBar_new(MainFunctions, NULL, NULL));
Hisham's avatar
Hisham committed
189
   this->keys = xCalloc(KEY_MAX, sizeof(Htop_Action));
190
   this->inc = IncSet_new(MainPanel_getFunctionBar(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

   Action_setBindings(this->keys);
   Platform_setBindings(this->keys);

   return this;
}

void MainPanel_setState(MainPanel* this, State* state) {
   this->state = state;
}

void MainPanel_delete(Object* object) {
   Panel* super = (Panel*) object;
   MainPanel* this = (MainPanel*) object;
   Panel_done(super);
206
   IncSet_delete(this->inc);
Hisham Muhammad's avatar
Hisham Muhammad committed
207
208
209
   free(this->keys);
   free(this);
}