MainPanel.c 5.06 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
28
29
   Htop_Action *keys;
   pid_t pidSearch;
} MainPanel;

typedef bool(*MainPanel_ForeachProcessFn)(Process*, size_t);

30
31
#define MainPanel_getFunctionBar(this_) (((Panel*)(this_))->defaultBar)

Hisham Muhammad's avatar
Hisham Muhammad committed
32
33
}*/

34
35
36
static const char* MainFunctions[]  = {"Help  ", "Setup ", "Search", "Filter", "Tree  ", "SortBy", "Nice -", "Nice +", "Kill  ", "Quit  ", NULL};

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

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;

70
   if (ch != ERR && this->inc->active) {
71
72
73
      bool filterChanged = IncSet_handleKey(this->inc, ch, super, (IncMode_GetPanelValue) MainPanel_getValue, NULL);
      if (filterChanged) {
         this->state->pl->incFilter = IncSet_filter(this->inc);
74
75
76
         reaction = HTOP_REFRESH | HTOP_REDRAW_BAR;
      }
      reaction |= HTOP_KEEP_FOLLOWING;
77
78
79
      result = HANDLED;
   } else if (ch == 27) {
      return HANDLED;
80
   } else if (ch != ERR && this->keys[ch]) {
Hisham Muhammad's avatar
Hisham Muhammad committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
      reaction |= (this->keys[ch])(this->state);
      result = HANDLED;
   } else if (isdigit(ch)) {
      MainPanel_pidSearch(this, ch);
   } else {
      if (ch != ERR) {
         this->pidSearch = 0;
      }
      switch (ch) {
      case KEY_LEFT:
      case KEY_CTRLB:
         if (super->scrollH > 0) {
            super->scrollH -= CRT_scrollHAmount;
            super->needsRedraw = true;
         }
         return HANDLED;
      case KEY_RIGHT:
      case KEY_CTRLF:
         super->scrollH += CRT_scrollHAmount;
         super->needsRedraw = true;
         return HANDLED;
      }
   }

   if (reaction & HTOP_REDRAW_BAR) {
106
      MainPanel_updateTreeFunctions(this, this->state->settings->treeView);
107
      IncSet_drawBar(this->inc);
Hisham Muhammad's avatar
Hisham Muhammad committed
108
109
110
111
112
   }
   if (reaction & HTOP_UPDATE_PANELHDR) {
      ProcessList_printHeader(this->state->pl, Panel_getHeader(super));
   }
   if (reaction & HTOP_REFRESH) {
113
      result |= REDRAW;
Hisham Muhammad's avatar
Hisham Muhammad committed
114
115
   }      
   if (reaction & HTOP_RECALCULATE) {
116
      result |= RESCAN;
Hisham Muhammad's avatar
Hisham Muhammad committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
   }
   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;
   }
   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 "";
}

bool MainPanel_foreachProcess(MainPanel* this, MainPanel_ForeachProcessFn fn, int arg, bool* wasAnyTagged) {
   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
};

173
MainPanel* MainPanel_new() {
Hisham Muhammad's avatar
Hisham Muhammad committed
174
   MainPanel* this = AllocThis(MainPanel);
175
   Panel_init((Panel*) this, 1, 1, 1, 1, Class(Process), false, FunctionBar_new(MainFunctions, NULL, NULL));
Hisham Muhammad's avatar
Hisham Muhammad committed
176
   this->keys = calloc(KEY_MAX, sizeof(Htop_Action));
177
   this->inc = IncSet_new(MainPanel_getFunctionBar(this));
Hisham Muhammad's avatar
Hisham Muhammad committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

   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);
193
   IncSet_delete(this->inc);
Hisham Muhammad's avatar
Hisham Muhammad committed
194
195
196
   free(this->keys);
   free(this);
}