summaryrefslogtreecommitdiff
path: root/openbox/actions/showmenu.c
blob: 00adc8797071861e1b2adcd5417dcf9159d9ad42 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "openbox/actions.h"
#include "openbox/menu.h"
#include "openbox/place.h"
#include "openbox/geom.h"
#include "openbox/screen.h"
#include <glib.h>

typedef struct {
    gchar         *name;
    GravityCoord   x, y;
    ObPlaceMonitor monitor_type;
    gint           monitor;
    gboolean       use_position;
} Options;

static gpointer setup_func(xmlNodePtr node);
static void     free_func(gpointer options);
static gboolean run_func(ObActionsData *data, gpointer options);

void action_showmenu_startup(void)
{
    actions_register("ShowMenu", setup_func, free_func, run_func);
}

static gpointer setup_func(xmlNodePtr node)
{
    xmlNodePtr n, c;
    Options *o;
    gboolean x_pos_given = FALSE;

    o = g_slice_new0(Options);
    o->monitor = -1;

    if ((n = obt_xml_find_node(node, "menu")))
        o->name = obt_xml_node_string(n);

    if ((n = obt_xml_find_node(node, "position"))) {
        if ((c = obt_xml_find_node(n->children, "x"))) {
            if (!obt_xml_node_contains(c, "default")) {
                config_parse_gravity_coord(c, &o->x);
                x_pos_given = TRUE;
            }
        }

        if (x_pos_given && (c = obt_xml_find_node(n->children, "y"))) {
            if (!obt_xml_node_contains(c, "default")) {
                config_parse_gravity_coord(c, &o->y);
                o->use_position = TRUE;
            }
        }

        /* unlike client placement, x/y is needed to specify a monitor,
         * either it's under the mouse or it's in an exact actual position */
        if (o->use_position && (c = obt_xml_find_node(n->children, "monitor"))) {
            if (!obt_xml_node_contains(c, "default")) {
                gchar *s = obt_xml_node_string(c);
                if (!g_ascii_strcasecmp(s, "mouse"))
                    o->monitor_type = OB_PLACE_MONITOR_MOUSE;
                else if (!g_ascii_strcasecmp(s, "active"))
                    o->monitor_type = OB_PLACE_MONITOR_ACTIVE;
                else if (!g_ascii_strcasecmp(s, "primary"))
                    o->monitor_type = OB_PLACE_MONITOR_PRIMARY;
                else if (!g_ascii_strcasecmp(s, "all"))
                    o->monitor_type = OB_PLACE_MONITOR_ALL;
                else
                    o->monitor = obt_xml_node_int(c) - 1;
                g_free(s);
            }
        }
    }
    return o;
}

static void free_func(gpointer options)
{
    Options *o = options;
    g_free(o->name);
    g_slice_free(Options, o);
}

static void calc_position(Options *o, gint *x, gint *y)
{
    gint monitor = -1;
    const Rect *area;
    if (o->monitor >= 0)
        monitor = o->monitor;
    else switch (o->monitor_type) {
        case OB_PLACE_MONITOR_ANY:
        case OB_PLACE_MONITOR_PRIMARY:
            monitor = screen_monitor_primary(FALSE);
            break;
        case OB_PLACE_MONITOR_MOUSE:
            monitor = screen_monitor_pointer();
            break;
        case OB_PLACE_MONITOR_ACTIVE:
            monitor = screen_monitor_active();
            break;
        case OB_PLACE_MONITOR_ALL:
            monitor = screen_num_monitors;
            break;
        default:
            g_assert_not_reached();
    }
    area = screen_physical_area_monitor(monitor);

    if (o->x.center)
        *x = area->width / 2; /* - client->area.width / 2; */
    else {
        *x = o->x.pos;
        if (o->x.denom)
            *x = (*x * area->width) / o->x.denom;
        if (o->x.opposite)
            *x = area->width /* - frame_size.width */ - *x;
    }

    if (o->y.center)
        *y = area->height / 2; /* - client->area.height / 2; */
    else {
        *y = o->y.pos;
        if (o->y.denom)
            *y = (*y * area->height) / o->y.denom;
        if (o->y.opposite)
            *y = area->height /* - frame_size.height */ - *y;
    }

    *x += area->x;
    *y += area->y;
}

/* Always return FALSE because its not interactive */
static gboolean run_func(ObActionsData *data, gpointer options)
{
    Options *o = options;
    gint x, y;

    if (o->use_position) {
        calc_position(o, &x, &y);
    } else {
        x = data->x;
        y = data->y;
    }

    /* you cannot call ShowMenu from inside a menu */
    if (data->uact != OB_USER_ACTION_MENU_SELECTION && o->name)
        menu_show(o->name, x, y, data->button != 0, data->client);

    return FALSE;
}