summaryrefslogtreecommitdiff
path: root/src/util.h
blob: 36c0a89e873b0fc6537f03ea2d9f1b4e1c2ef081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
70
71
72
73
74
75
76
77
78
79
80
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// -*- mode: c++ -*-

#ifndef H_MATE_SYSTEM_MONITOR_UTIL_1123178725
#define H_MATE_SYSTEM_MONITOR_UTIL_1123178725

#include <glib.h>
#include <gtk/gtk.h>
#include <time.h>
#include <string>

using std::string;

/* check if logind is running */
#define LOGIND_RUNNING() (access("/run/systemd/seats/", F_OK) >= 0)

GtkWidget*
procman_make_label_for_mmaps_or_ofiles(const char *format,
                                       const char *process_name,
                                       unsigned pid);

gboolean
load_symbols(const char *module, ...) G_GNUC_NULL_TERMINATED;

gchar *
procman_format_date_for_display(time_t time_raw);

const char*
format_process_state(guint state);

void
procman_debug_real(const char *file, int line, const char *func,
                   const char *format, ...) G_GNUC_PRINTF(4, 5);

#define procman_debug(FMT, ...) procman_debug_real(__FILE__, __LINE__, __func__, FMT, ##__VA_ARGS__)

inline string make_string(char *c_str)
{
    if (!c_str) {
        procman_debug("NULL string");
        return string();
    }

    string s(c_str);
    g_free(c_str);
    return s;
}

namespace procman
{
    gchar* format_duration_for_display(unsigned centiseconds);

    void memory_size_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                                    GtkTreeModel *model, GtkTreeIter *iter,
                                    gpointer user_data);

    void io_rate_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                                GtkTreeModel *model, GtkTreeIter *iter,
                                gpointer user_data);

    void memory_size_na_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                                       GtkTreeModel *model, GtkTreeIter *iter,
                                       gpointer user_data);

    void storage_size_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                                     GtkTreeModel *model, GtkTreeIter *iter,
                                     gpointer user_data);

    void storage_size_na_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                                        GtkTreeModel *model, GtkTreeIter *iter,
                                        gpointer user_data);

    void duration_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                   GtkTreeModel *model, GtkTreeIter *iter,
                   gpointer user_data);

    void time_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                             GtkTreeModel *model, GtkTreeIter *iter,
                             gpointer user_data);

    void status_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                               GtkTreeModel *model, GtkTreeIter *iter,
                               gpointer user_data);
    void priority_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                               GtkTreeModel *model, GtkTreeIter *iter,
                               gpointer user_data);
    gint priority_compare_func(GtkTreeModel* model, GtkTreeIter* first,
                            GtkTreeIter* second, gpointer user_data);
    gint number_compare_func(GtkTreeModel* model, GtkTreeIter* first,
                            GtkTreeIter* second, gpointer user_data);

    template<typename T>
    void poison(T &t, char c)
    {
        memset(&t, c, sizeof t);
    }

    //
    // Stuff to update a tree_store in a smart way
    //

    template<typename T>
    void tree_store_update(GtkTreeModel* model, GtkTreeIter* iter, int column, const T& new_value)
    {
        T current_value;

        gtk_tree_model_get(model, iter, column, &current_value, -1);

        if (current_value != new_value)
            gtk_tree_store_set(GTK_TREE_STORE(model), iter, column, new_value, -1);
    }

    // undefined
    // catch every thing about pointers
    // just to make sure i'm not doing anything wrong
    template<typename T>
    void tree_store_update(GtkTreeModel* model, GtkTreeIter* iter, int column, T* new_value);

    // specialized versions for strings
    template<>
    void tree_store_update<const char>(GtkTreeModel* model, GtkTreeIter* iter, int column, const char* new_value);

    template<>
    inline void tree_store_update<char>(GtkTreeModel* model, GtkTreeIter* iter, int column, char* new_value)
    {
         tree_store_update<const char>(model, iter, column, new_value);
    }

    gchar* get_nice_level (gint nice);
}

#endif /* H_MATE_SYSTEM_MONITOR_UTIL_1123178725 */