summaryrefslogtreecommitdiff
path: root/src/perl/ui/Themes.xs
blob: 7f1c5277f14470e74be46f9fd68316b93828001f (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
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
#include "module.h"

void printformat_perl(TEXT_DEST_REC *dest, char *format, char **arglist)
{
	THEME_REC *theme;
	char *module, *str;
	int formatnum;

	module = g_strdup(perl_get_package());
	formatnum = format_find_tag(module, format);
	if (formatnum < 0) {
		die("printformat(): unregistered format '%s'", format);
                g_free(module);
		return;
	}

	theme = dest->window->theme == NULL ? current_theme :
		dest->window->theme;
	signal_emit("print format", 5, theme, module,
		    dest, GINT_TO_POINTER(formatnum), arglist);

        str = format_get_text_theme_charargs(theme, module, dest, formatnum, arglist);
	if (*str != '\0') printtext_dest(dest, "%s", str);
	g_free(str);
	g_free(module);
}

static void perl_unregister_theme(const char *package)
{
	FORMAT_REC *formats;
	int n;

	formats = g_hash_table_lookup(default_formats, package);
	if (formats == NULL) return;

	for (n = 0; formats[n].def != NULL; n++) {
		g_free(formats[n].tag);
		g_free(formats[n].def);
	}
	g_free(formats);
	theme_unregister_module(package);
}

static void sig_script_destroyed(PERL_SCRIPT_REC *script)
{
	perl_unregister_theme(script->package);
}

void perl_themes_init(void)
{
	signal_add("script destroyed", (SIGNAL_FUNC) sig_script_destroyed);
}

void perl_themes_deinit(void)
{
	signal_remove("script destroyed", (SIGNAL_FUNC) sig_script_destroyed);
}

MODULE = Irssi::UI::Themes  PACKAGE = Irssi
PROTOTYPES: ENABLE

Irssi::UI::Theme
current_theme()
CODE:
	RETVAL = current_theme;
OUTPUT:
	RETVAL

int
EXPAND_FLAG_IGNORE_REPLACES()
CODE:
	RETVAL = EXPAND_FLAG_IGNORE_REPLACES;
OUTPUT:
	RETVAL

int
EXPAND_FLAG_IGNORE_EMPTY()
CODE:
	RETVAL = EXPAND_FLAG_IGNORE_EMPTY;
OUTPUT:
	RETVAL

int
EXPAND_FLAG_RECURSIVE_MASK()
CODE:
	RETVAL = EXPAND_FLAG_RECURSIVE_MASK;
OUTPUT:
	RETVAL

void
theme_register(formats)
	SV *formats
PREINIT:
	AV *av;
	FORMAT_REC *formatrecs;
	char *key, *value;
	int len, n, fpos;
CODE:

        if (!SvROK(formats))
        	croak("formats is not a reference to list");
	av = (AV *) SvRV(formats);
	len = av_len(av)+1;
	if (len == 0 || (len & 1) != 0)
        	croak("formats list is invalid - not divisible by 2 (%d)", len);

	formatrecs = g_new0(FORMAT_REC, len/2+2);
	formatrecs[0].tag = g_strdup(perl_get_package());
	formatrecs[0].def = g_strdup("Perl script");

        for (fpos = 1, n = 0; n < len; n++, fpos++) {
		key = SvPV(*av_fetch(av, n, 0), PL_na); n++;
		value = SvPV(*av_fetch(av, n, 0), PL_na);

		formatrecs[fpos].tag = g_strdup(key);
		formatrecs[fpos].def = g_strdup(value);
		formatrecs[fpos].params = MAX_FORMAT_PARAMS;
	}

	theme_register_module(perl_get_package(), formatrecs);

void
printformat(level, format, ...)
	int level
	char *format
PREINIT:
	TEXT_DEST_REC dest;
	char *arglist[MAX_FORMAT_PARAMS+1];
	int n;
CODE:
	format_create_dest(&dest, NULL, NULL, level, NULL);
	memset(arglist, 0, sizeof(arglist));
	for (n = 2; n < items && n < MAX_FORMAT_PARAMS+2; n++) {
		arglist[n-2] = SvPV(ST(n), PL_na);
	}

        printformat_perl(&dest, format, arglist);

#*******************************
MODULE = Irssi::UI::Themes  PACKAGE = Irssi::Server
#*******************************

void
printformat(server, target, level, format, ...)
	Irssi::Server server
	char *target
	int level
	char *format
PREINIT:
	TEXT_DEST_REC dest;
	char *arglist[MAX_FORMAT_PARAMS+1];
	int n;
CODE:
	format_create_dest(&dest, server, target, level, NULL);
	memset(arglist, 0, sizeof(arglist));
	for (n = 4; n < items && n < MAX_FORMAT_PARAMS+4; n++) {
		arglist[n-4] = SvPV(ST(n), PL_na);
	}

        printformat_perl(&dest, format, arglist);

#*******************************
MODULE = Irssi::UI::Themes  PACKAGE = Irssi::UI::Window
#*******************************

void
printformat(window, level, format, ...)
	Irssi::UI::Window window
	int level
	char *format
PREINIT:
	TEXT_DEST_REC dest;
	char *arglist[MAX_FORMAT_PARAMS+1];
	int n;
CODE:
	format_create_dest(&dest, NULL, NULL, level, window);
	memset(arglist, 0, sizeof(arglist));
	for (n = 3; n < items && n < MAX_FORMAT_PARAMS+3; n++) {
		arglist[n-3] = SvPV(ST(n), PL_na);
	}

        printformat_perl(&dest, format, arglist);

#*******************************
MODULE = Irssi::UI::Themes  PACKAGE = Irssi::Windowitem
#*******************************

void
printformat(item, level, format, ...)
	Irssi::Windowitem item
	int level
	char *format
PREINIT:
	TEXT_DEST_REC dest;
	char *arglist[MAX_FORMAT_PARAMS+1];
	int n;
CODE:
	format_create_dest(&dest, item->server, item->visible_name, level, NULL);
	memset(arglist, 0, sizeof(arglist));
	for (n = 3; n < items && n < MAX_FORMAT_PARAMS+3; n++) {
		arglist[n-3] = SvPV(ST(n), PL_na);
	}

        printformat_perl(&dest, format, arglist);

#*******************************
MODULE = Irssi::UI::Themes  PACKAGE = Irssi::UI::Theme  PREFIX = theme_
#*******************************

void
theme_format_expand(theme, format, flags=0)
	Irssi::UI::Theme theme
	char *format
        int flags
PREINIT:
	char *ret;
PPCODE:
	if (flags == 0) {
		ret = theme_format_expand(theme, format);
	} else {
		ret = theme_format_expand_data(theme, (const char **) &format, 'n', 'n',
					       NULL, NULL, EXPAND_FLAG_ROOT | flags);
	}
	XPUSHs(sv_2mortal(new_pv(ret)));
	g_free_not_null(ret);

char *
theme_get_format(theme, module, tag)
	Irssi::UI::Theme theme
	char *module
	char *tag
PREINIT:
	MODULE_THEME_REC *modtheme;
	FORMAT_REC *formats;
	int i;
CODE:
	formats = g_hash_table_lookup(default_formats, module);
	if (formats == NULL)
		croak("Unknown module: %s", module);

	for (i = 0; formats[i].def != NULL; i++) {
		if (formats[i].tag != NULL &&
		    g_strcasecmp(formats[i].tag, tag) == 0)
			break;
	}

	if (formats[i].def == NULL)
		croak("Unknown format tag: %s", tag);

	modtheme = g_hash_table_lookup(theme->modules, module);
	RETVAL = modtheme == NULL ? NULL : modtheme->formats[i];
	if (RETVAL == NULL)
		RETVAL = formats[i].def;
OUTPUT:
	RETVAL