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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*
* buflist-mouse.c - mouse actions for buflist
*
* Copyright (C) 2003-2017 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "buflist.h"
#include "buflist-mouse.h"
#include "buflist-bar-item.h"
#include "buflist-config.h"
/*
* Callback called when a mouse action occurs in buflist bar item.
*/
struct t_hashtable *
buflist_focus_cb (const void *pointer, void *data, struct t_hashtable *info)
{
const char *ptr_bar_item_name, *ptr_bar_item_line, *keys, *ptr_value;
long item_line;
char *error, str_value[128], **list_keys;
int i, num_keys, type;
struct t_gui_buffer *ptr_buffer;
struct t_hdata *ptr_hdata;
/* make C compiler happy */
(void) pointer;
(void) data;
ptr_buffer = NULL;
if (!buflist_list_buffers)
goto end;
/* check bar item name */
ptr_bar_item_name = weechat_hashtable_get (info, "_bar_item_name");
if (strcmp (ptr_bar_item_name, BUFLIST_BAR_ITEM_NAME) != 0)
goto end;
/* check bar item line */
ptr_bar_item_line = weechat_hashtable_get (info, "_bar_item_line");
if (!ptr_bar_item_line)
goto end;
item_line = strtol (ptr_bar_item_line, &error, 10);
if (!error || error[0])
goto end;
if ((item_line < 0)
|| (item_line >= weechat_arraylist_size (buflist_list_buffers)))
{
goto end;
}
/* check if buffer pointer is still valid */
ptr_buffer = weechat_arraylist_get (buflist_list_buffers, item_line);
if (!ptr_buffer)
goto end;
ptr_hdata = weechat_hdata_get ("buffer");
if (!weechat_hdata_check_pointer (
ptr_hdata,
weechat_hdata_get_list (ptr_hdata, "gui_buffers"),
ptr_buffer))
{
ptr_buffer = NULL;
}
end:
/* get list of keys */
keys = weechat_hdata_get_string (buflist_hdata_buffer, "var_keys");
list_keys = weechat_string_split (keys, ",", 0, 0, &num_keys);
if (!list_keys)
return info;
/* browse keys and add values in hashtable */
for (i = 0; i < num_keys; i++)
{
type = weechat_hdata_get_var_type (ptr_hdata, list_keys[i]);
switch (type)
{
case WEECHAT_HDATA_CHAR:
snprintf (str_value, sizeof (str_value),
"%c",
weechat_hdata_char (buflist_hdata_buffer,
ptr_buffer, list_keys[i]));
weechat_hashtable_set (info, list_keys[i], str_value);
break;
case WEECHAT_HDATA_INTEGER:
snprintf (str_value, sizeof (str_value),
"%d",
(ptr_buffer) ?
weechat_hdata_integer (buflist_hdata_buffer,
ptr_buffer, list_keys[i]) : -1);
weechat_hashtable_set (info, list_keys[i], str_value);
break;
case WEECHAT_HDATA_LONG:
snprintf (str_value, sizeof (str_value),
"%ld",
(ptr_buffer) ?
weechat_hdata_long (buflist_hdata_buffer,
ptr_buffer, list_keys[i]) : -1);
weechat_hashtable_set (info, list_keys[i], str_value);
break;
case WEECHAT_HDATA_STRING:
case WEECHAT_HDATA_SHARED_STRING:
ptr_value = weechat_hdata_string (buflist_hdata_buffer,
ptr_buffer,
list_keys[i]);
weechat_hashtable_set (info, list_keys[i],
(ptr_value) ? ptr_value : "");
break;
case WEECHAT_HDATA_TIME:
snprintf (str_value, sizeof (str_value),
"%ld",
(ptr_buffer) ?
(long int)weechat_hdata_time (buflist_hdata_buffer,
ptr_buffer, list_keys[i]) : -1);
weechat_hashtable_set (info, list_keys[i], str_value);
break;
default: /* ignore other types */
break;
}
}
/* add pointer and plugin name */
snprintf (str_value, sizeof (str_value),
"0x%lx", (long unsigned int)ptr_buffer);
weechat_hashtable_set (info, "pointer", str_value);
weechat_hashtable_set (info, "plugin",
weechat_buffer_get_string (ptr_buffer, "plugin"));
/* add some local variables */
ptr_value = weechat_buffer_get_string (ptr_buffer, "localvar_type");
weechat_hashtable_set (info, "localvar_type",
(ptr_value) ? ptr_value : "");
ptr_value = weechat_buffer_get_string (ptr_buffer, "localvar_server");
weechat_hashtable_set (info, "localvar_server",
(ptr_value) ? ptr_value : "");
ptr_value = weechat_buffer_get_string (ptr_buffer, "localvar_channel");
weechat_hashtable_set (info, "localvar_channel",
(ptr_value) ? ptr_value : "");
ptr_value = weechat_buffer_get_string (ptr_buffer, "localvar_lag");
weechat_hashtable_set (info, "localvar_lag",
(ptr_value) ? ptr_value : "");
weechat_string_free_split (list_keys);
return info;
}
/*
* Moves a buffer after a mouse gesture in buflist bar.
*/
void
buflist_mouse_move_buffer (const char *key, struct t_gui_buffer *buffer,
int number2)
{
struct t_hdata *ptr_hdata;
struct t_gui_buffer *ptr_last_gui_buffer;
char str_command[128];
if (!weechat_config_boolean (buflist_config_look_mouse_move_buffer))
return;
if (number2 < 0)
{
/*
* if number is now known (end of gesture outside buflist),
* then set it according to mouse gesture
*/
number2 = 1;
if (weechat_string_match (key, "*gesture-right", 1)
|| weechat_string_match (key, "*gesture-down", 1))
{
number2 = 999999;
ptr_hdata = weechat_hdata_get ("buffer");
ptr_last_gui_buffer = weechat_hdata_get_list (ptr_hdata,
"last_gui_buffer");
if (ptr_last_gui_buffer)
{
number2 = weechat_hdata_integer (ptr_hdata,
ptr_last_gui_buffer,
"number") + 1;
}
}
}
snprintf (str_command, sizeof (str_command),
"/buffer move %d", number2);
weechat_command (buffer, str_command);
}
/*
* Callback called when a mouse action occurs in buflist bar or bar item.
*/
int
buflist_hsignal_cb (const void *pointer, void *data, const char *signal,
struct t_hashtable *hashtable)
{
const char *ptr_key, *ptr_pointer, *ptr_number, *ptr_number2;
const char *ptr_full_name;
struct t_gui_buffer *ptr_buffer;
char *error, str_command[1024];
long number, number2;
long unsigned int value;
int rc, current_buffer_number;
/* make C compiler happy */
(void) pointer;
(void) data;
(void) signal;
ptr_key = weechat_hashtable_get (hashtable, "_key");
ptr_pointer = weechat_hashtable_get (hashtable, "pointer");
ptr_number = weechat_hashtable_get (hashtable, "number");
ptr_number2 = weechat_hashtable_get (hashtable, "number2");
ptr_full_name = weechat_hashtable_get (hashtable, "full_name");
if (!ptr_key || !ptr_pointer || !ptr_number || !ptr_number2
|| !ptr_full_name)
{
return WEECHAT_RC_OK;
}
rc = sscanf (ptr_pointer, "%lx", &value);
if ((rc == EOF) || (rc == 0))
return WEECHAT_RC_OK;
ptr_buffer = (struct t_gui_buffer *)value;
number = strtol (ptr_number, &error, 10);
if (!error || error[0])
return WEECHAT_RC_OK;
number2 = strtol (ptr_number2, &error, 10);
if (!error || error[0])
return WEECHAT_RC_OK;
current_buffer_number = weechat_buffer_get_integer (
weechat_current_buffer (), "number");
if (strcmp (ptr_key, "button1") == 0)
{
/* left mouse button */
if (number == number2)
{
if (weechat_config_boolean (
buflist_config_look_mouse_jump_visited_buffer)
&& (current_buffer_number == number))
{
weechat_command (NULL, "/input jump_previously_visited_buffer");
}
else
{
snprintf (str_command, sizeof (str_command),
"/buffer %s", ptr_full_name);
weechat_command (NULL, str_command);
}
}
else
{
/* move buffer */
buflist_mouse_move_buffer (ptr_key, ptr_buffer, number2);
}
}
else if (strcmp (ptr_key, "button2") == 0)
{
if (weechat_config_boolean (
buflist_config_look_mouse_jump_visited_buffer)
&& (current_buffer_number == number))
{
weechat_command (NULL, "/input jump_next_visited_buffer");
}
}
else if (weechat_string_match (ptr_key, "*wheelup", 1))
{
if (weechat_config_boolean (buflist_config_look_mouse_wheel))
{
weechat_command (NULL, "/buffer -1");
}
}
else if (weechat_string_match (ptr_key, "*wheeldown", 1))
{
if (weechat_config_boolean (buflist_config_look_mouse_wheel))
{
weechat_command (NULL, "/buffer +1");
}
}
else
{
/* move buffer */
buflist_mouse_move_buffer (ptr_key, ptr_buffer, number2);
}
return WEECHAT_RC_OK;
}
/*
* Initializes mouse.
*
* Returns:
* 1: OK
* 0: error
*/
int
buflist_mouse_init ()
{
struct t_hashtable *keys;
keys = weechat_hashtable_new (4,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL, NULL);
if (!keys)
return 0;
weechat_hook_focus (BUFLIST_BAR_ITEM_NAME, &buflist_focus_cb, NULL, NULL);
weechat_hook_hsignal(BUFLIST_MOUSE_HSIGNAL,
&buflist_hsignal_cb, NULL, NULL);
weechat_hashtable_set (keys,
"@item(" BUFLIST_BAR_ITEM_NAME "):button1*",
"hsignal:" BUFLIST_MOUSE_HSIGNAL);
weechat_hashtable_set (keys,
"@item(" BUFLIST_BAR_ITEM_NAME "):button2*",
"hsignal:" BUFLIST_MOUSE_HSIGNAL);
weechat_hashtable_set (keys,
"@bar(" BUFLIST_BAR_NAME "):ctrl-wheelup",
"hsignal:" BUFLIST_MOUSE_HSIGNAL);
weechat_hashtable_set (keys,
"@bar(" BUFLIST_BAR_NAME "):ctrl-wheeldown",
"hsignal:" BUFLIST_MOUSE_HSIGNAL);
weechat_hashtable_set (keys, "__quiet", "1");
weechat_key_bind ("mouse", keys);
weechat_hashtable_free (keys);
return 1;
}
/*
* Ends mouse.
*/
void
buflist_mouse_end ()
{
}
|