all repos — tint2 @ main

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

src/mouse_actions.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
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
/**************************************************************************
* Copyright (C) 2017 tint2 authors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "drag_and_drop.h"
#include "panel.h"
#include "server.h"
#include "task.h"
#include "window.h"

gboolean tint2_handles_click(Panel *panel, XButtonEvent *e)
{
    Task *task = click_task(panel, e->x, e->y);
    if (task) {
        if ((e->button == 1 && mouse_left != 0) || (e->button == 2 && mouse_middle != 0) ||
            (e->button == 3 && mouse_right != 0) || (e->button == 4 && mouse_scroll_up != 0) ||
            (e->button == 5 && mouse_scroll_down != 0)) {
            return TRUE;
        } else
            return FALSE;
    }
    LauncherIcon *icon = click_launcher_icon(panel, e->x, e->y);
    if (icon) {
        if (e->button == 1) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    // no launcher/task clicked --> check if taskbar clicked
    Taskbar *taskbar = click_taskbar(panel, e->x, e->y);
    if (taskbar && e->button == 1 && taskbar_mode == MULTI_DESKTOP)
        return TRUE;
    if (click_clock(panel, e->x, e->y)) {
        if ((e->button == 1 && clock_lclick_command) || (e->button == 2 && clock_mclick_command) ||
            (e->button == 3 && clock_rclick_command) || (e->button == 4 && clock_uwheel_command) ||
            (e->button == 5 && clock_dwheel_command))
            return TRUE;
        else
            return FALSE;
    }
#ifdef ENABLE_BATTERY
    if (click_battery(panel, e->x, e->y)) {
        if ((e->button == 1 && battery_lclick_command) || (e->button == 2 && battery_mclick_command) ||
            (e->button == 3 && battery_rclick_command) || (e->button == 4 && battery_uwheel_command) ||
            (e->button == 5 && battery_dwheel_command))
            return TRUE;
        else
            return FALSE;
    }
#endif
    if (click_execp(panel, e->x, e->y))
        return TRUE;
    if (click_button(panel, e->x, e->y))
        return TRUE;
    return FALSE;
}

void handle_mouse_press_event(XEvent *e)
{
    Panel *panel = get_panel(e->xany.window);
    if (!panel)
        return;

    if (wm_menu && !tint2_handles_click(panel, &e->xbutton)) {
        forward_click(e);
        return;
    }
    task_drag = click_task(panel, e->xbutton.x, e->xbutton.y);

    if (panel_layer == BOTTOM_LAYER)
        XLowerWindow(server.display, panel->main_win);
}

void handle_mouse_move_event(XEvent *e)
{
    Panel *panel = get_panel(e->xany.window);
    if (!panel || !task_drag)
        return;

    // Find the taskbar on the event's location
    Taskbar *event_taskbar = click_taskbar(panel, e->xbutton.x, e->xbutton.y);
    if (event_taskbar == NULL)
        return;

    // Find the task on the event's location
    Task *event_task = click_task(panel, e->xbutton.x, e->xbutton.y);

    // If the event takes place on the same taskbar as the task being dragged
    if (&event_taskbar->area == task_drag->area.parent) {
        if (taskbar_sort_method != TASKBAR_NOSORT) {
            sort_tasks(event_taskbar);
        } else {
            // Swap the task_drag with the task on the event's location (if they differ)
            if (event_task && event_task != task_drag) {
                GList *drag_iter = g_list_find(event_taskbar->area.children, task_drag);
                GList *task_iter = g_list_find(event_taskbar->area.children, event_task);
                if (drag_iter && task_iter) {
                    gpointer temp = task_iter->data;
                    task_iter->data = drag_iter->data;
                    drag_iter->data = temp;
                    event_taskbar->area.resize_needed = 1;
                    schedule_panel_redraw();
                    task_dragged = 1;
                }
            }
        }
    } else { // The event is on another taskbar than the task being dragged
        if (task_drag->desktop == ALL_DESKTOPS || taskbar_mode != MULTI_DESKTOP)
            return;

        Taskbar *drag_taskbar = (Taskbar *)task_drag->area.parent;
        remove_area((Area *)task_drag);

        if (event_taskbar->area.posx > drag_taskbar->area.posx || event_taskbar->area.posy > drag_taskbar->area.posy) {
            int i = (taskbarname_enabled) ? 1 : 0;
            event_taskbar->area.children = g_list_insert(event_taskbar->area.children, task_drag, i);
        } else
            event_taskbar->area.children = g_list_append(event_taskbar->area.children, task_drag);

        // Move task to other desktop (but avoid the 'Window desktop changed' code in 'event_property_notify')
        task_drag->area.parent = &event_taskbar->area;
        task_drag->desktop = event_taskbar->desktop;

        change_window_desktop(task_drag->win, event_taskbar->desktop);
        if (hide_task_diff_desktop)
            change_desktop(event_taskbar->desktop);

        if (taskbar_sort_method != TASKBAR_NOSORT) {
            sort_tasks(event_taskbar);
        }

        event_taskbar->area.resize_needed = 1;
        drag_taskbar->area.resize_needed = 1;
        task_dragged = 1;
        schedule_panel_redraw();
        panel->area.resize_needed = 1;
    }
}

void handle_mouse_release_event(XEvent *e)
{
    Panel *panel = get_panel(e->xany.window);
    if (!panel)
        return;

    if (wm_menu && !tint2_handles_click(panel, &e->xbutton)) {
        forward_click(e);
        if (panel_layer == BOTTOM_LAYER)
            XLowerWindow(server.display, panel->main_win);
        task_drag = 0;
        return;
    }

    MouseAction action = TOGGLE_ICONIFY;
    switch (e->xbutton.button) {
    case 1:
        action = mouse_left;
        break;
    case 2:
        action = mouse_middle;
        break;
    case 3:
        action = mouse_right;
        break;
    case 4:
        action = mouse_scroll_up;
        break;
    case 5:
        action = mouse_scroll_down;
        break;
    case 6:
        action = mouse_tilt_left;
        break;
    case 7:
        action = mouse_tilt_right;
        break;
    }

    Clock *clock = click_clock(panel, e->xbutton.x, e->xbutton.y);
    if (clock) {
        clock_action(clock, e->xbutton.button, e->xbutton.x - clock->area.posx, e->xbutton.y - clock->area.posy, e->xbutton.time);
        if (panel_layer == BOTTOM_LAYER)
            XLowerWindow(server.display, panel->main_win);
        task_drag = 0;
        return;
    }

#ifdef ENABLE_BATTERY
    Battery *battery = click_battery(panel, e->xbutton.x, e->xbutton.y);
    if (battery) {
        battery_action(battery, e->xbutton.button, e->xbutton.x - battery->area.posx, e->xbutton.y - battery->area.posy, e->xbutton.time);
        if (panel_layer == BOTTOM_LAYER)
            XLowerWindow(server.display, panel->main_win);
        task_drag = 0;
        return;
    }
#endif

    Execp *execp = click_execp(panel, e->xbutton.x, e->xbutton.y);
    if (execp) {
        execp_action(execp, e->xbutton.button, e->xbutton.x - execp->area.posx, e->xbutton.y - execp->area.posy, e->xbutton.time);
        if (panel_layer == BOTTOM_LAYER)
            XLowerWindow(server.display, panel->main_win);
        task_drag = 0;
        return;
    }

    Button *button = click_button(panel, e->xbutton.x, e->xbutton.y);
    if (button) {
        button_action(button, e->xbutton.button, e->xbutton.x - button->area.posx, e->xbutton.y - button->area.posy, e->xbutton.time);
        if (panel_layer == BOTTOM_LAYER)
            XLowerWindow(server.display, panel->main_win);
        task_drag = 0;
        return;
    }

    if (e->xbutton.button == 1 && click_launcher(panel, e->xbutton.x, e->xbutton.y)) {
        LauncherIcon *icon = click_launcher_icon(panel, e->xbutton.x, e->xbutton.y);
        if (icon) {
            launcher_action(icon, e, e->xbutton.x - icon->area.posx, e->xbutton.y - icon->area.posy);
        }
        task_drag = 0;
        return;
    }

    Taskbar *taskbar;
    if (!(taskbar = click_taskbar(panel, e->xbutton.x, e->xbutton.y))) {
        // TODO: check better solution to keep window below
        if (panel_layer == BOTTOM_LAYER)
            XLowerWindow(server.display, panel->main_win);
        task_drag = 0;
        return;
    }

    // drag and drop task
    if (task_dragged) {
        task_drag = 0;
        task_dragged = 0;
        return;
    }

    // switch desktop
    if (taskbar_mode == MULTI_DESKTOP) {
        gboolean diff_desktop = FALSE;
        if (taskbar->desktop != server.desktop && action != CLOSE && action != DESKTOP_LEFT &&
            action != DESKTOP_RIGHT) {
            diff_desktop = TRUE;
            change_desktop(taskbar->desktop);
        }
        Task *task = click_task(panel, e->xbutton.x, e->xbutton.y);
        if (task) {
            if (diff_desktop) {
                if (action == TOGGLE_ICONIFY) {
                    if (!window_is_active(task->win))
                        activate_window(task->win);
                } else {
                    task_handle_mouse_event(task, action);
                }
            } else {
                task_handle_mouse_event(task, action);
            }
        }
    } else {
        task_handle_mouse_event(click_task(panel, e->xbutton.x, e->xbutton.y), action);
    }

    // to keep window below
    if (panel_layer == BOTTOM_LAYER)
        XLowerWindow(server.display, panel->main_win);
}