all repos — openbox @ cbbf90a718ecc6836ef7a77b9040aebb9da348b8

openbox fork - make it a bit more like ryudo

plugins/placement/history.c (raw)

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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "kernel/openbox.h"
#include "kernel/dispatch.h"
#include "kernel/frame.h"
#include "kernel/client.h"
#include <glib.h>
#include <string.h>
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif

struct HistoryItem {
    char *name;
    char *class;
    char *role;
    int x;
    int y;
    gboolean placed;
};

static GSList *history = NULL;
static char *history_path = NULL;

static struct HistoryItem *find_history(Client *c)
{
    GSList *it;
    struct HistoryItem *hi = NULL;

    /* find the client */
    for (it = history; it != NULL; it = it->next) {
        hi = it->data;
        g_assert(hi->name != NULL);
        g_assert(hi->class != NULL);
        g_assert(hi->role != NULL);
        g_assert(c->name != NULL);
        g_assert(c->class != NULL);
        g_assert(c->role != NULL);
        if (!strcmp(hi->name, c->name) &&
            !strcmp(hi->class, c->class) &&
            !strcmp(hi->role, c->role))
            return hi;
    }
    return NULL;
}

gboolean place_history(Client *c)
{
    struct HistoryItem *hi;
    int x, y;

    hi = find_history(c);

    if (hi != NULL && !hi->placed) {
        hi->placed = TRUE;
        if (ob_state != State_Starting) {
            x = hi->x;
            y = hi->y;

            frame_frame_gravity(c->frame, &x, &y); /* get where the client
                                                      should be */
            client_configure(c, Corner_TopLeft, x, y,
                             c->area.width, c->area.height,
                             TRUE, TRUE);
        }
        return TRUE;
    }

    return FALSE;
}

static void strip_tabs(char *s)
{
    while (*s != '\0') {
        if (*s == '\t')
            *s = ' ';
        ++s;
    }
}

static void set_history(Client *c)
{
    struct HistoryItem *hi;

    hi = find_history(c);

    if (hi == NULL) {
        hi = g_new(struct HistoryItem, 1);
        history = g_slist_append(history, hi);
        hi->name = g_strdup(c->name);
        strip_tabs(hi->name);
        hi->class = g_strdup(c->class);
        strip_tabs(hi->class);
        hi->role = g_strdup(c->role);
        strip_tabs(hi->role);
    }

    hi->x = c->frame->area.x;
    hi->y = c->frame->area.y;
    hi->placed = FALSE;
}

static void event(ObEvent *e, void *foo)
{
    g_assert(e->type == Event_Client_Destroy);

    set_history(e->data.c.client);
}

static void save_history()
{
    GError *err = NULL;
    GIOChannel *io;
    GString *buf;
    GSList *it;
    struct HistoryItem *hi;
    gsize ret;

    io = g_io_channel_new_file(history_path, "w", &err);
    if (io != NULL) {
        for (it = history; it != NULL; it = it->next) {
            hi = it->data;
            buf = g_string_sized_new(0);
            buf=g_string_append(buf, hi->name);
            g_string_append_c(buf, '\t');
            buf=g_string_append(buf, hi->class);
            g_string_append_c(buf, '\t');
            buf=g_string_append(buf, hi->role);
            g_string_append_c(buf, '\t');
            g_string_append_printf(buf, "%d", hi->x);
            buf=g_string_append_c(buf, '\t');
            g_string_append_printf(buf, "%d", hi->y);
            buf=g_string_append_c(buf, '\n');
            if (g_io_channel_write_chars(io, buf->str, buf->len, &ret, &err) !=
                G_IO_STATUS_NORMAL)
                break;
            g_string_free(buf, TRUE);
        }
        g_io_channel_unref(io);
    }
}

static void load_history()
{
    GError *err = NULL;
    GIOChannel *io;
    char *buf = NULL;
    char *b, *c;
    struct HistoryItem *hi = NULL;

    io = g_io_channel_new_file(history_path, "r", &err);
    if (io != NULL) {
        while (g_io_channel_read_line(io, &buf, NULL, NULL, &err) ==
               G_IO_STATUS_NORMAL) {
            hi = g_new0(struct HistoryItem, 1);

            b = buf;
            if ((c = strchr(b, '\t')) == NULL) break;
            *c = '\0';
            hi->name = g_strdup(b);

            b = c + 1;
            if ((c = strchr(b, '\t')) == NULL) break;
            *c = '\0';
            hi->class = g_strdup(b);

            b = c + 1;
            if ((c = strchr(b, '\t')) == NULL) break;
            *c = '\0';
            hi->role = g_strdup(b);

            b = c + 1;
            if ((c = strchr(b, '\t')) == NULL) break;
            *c = '\0';
            hi->x = atoi(b);

            b = c + 1;
            if ((c = strchr(b, '\n')) == NULL) break;
            *c = '\0';
            hi->y = atoi(b);

            hi->placed = FALSE;

            g_free(buf);
            buf = NULL;

            history = g_slist_append(history, hi);
            hi = NULL;
        }
        g_io_channel_unref(io);
    }
        
    g_free(buf);

    if (hi != NULL) {
        g_free(hi->name);
        g_free(hi->class);
        g_free(hi->role);
    }
    g_free(hi);
}

void history_startup()
{
    char *path;

    history = NULL;

    path = g_build_filename(g_get_home_dir(), ".openbox", "history", NULL);
    history_path = g_strdup_printf("%s.%d", path, ob_screen);
    g_free(path);

    load_history(); /* load from the historydb file */

    dispatch_register(Event_Client_Destroy, (EventHandler)event, NULL);
}

void history_shutdown()
{
    GSList *it;

    save_history(); /* save to the historydb file */
    for (it = history; it != NULL; it = it->next)
        g_free(it->data);
    g_slist_free(history);

    dispatch_register(0, (EventHandler)event, NULL);

    g_free(history_path);
}