]> Dogcows Code - chaz/openbox/blob - obcl/process.c
eb7cafd2ac3b8480addb1bee8e2c55e99a9ffc83
[chaz/openbox] / obcl / process.c
1 #include "obcl.h"
2
3 static void cl_proc_intern_handler(CLNode *node)
4 {
5 CL_ASSERT_NODE(node);
6 g_warning("Unhandled node %s on line %d\n",
7 CL_ID(node), CL_LINE(node));
8 }
9
10 static CLProcHandler *default_handler(void)
11 {
12 static CLProcHandler *ph = 0;
13 if (!ph)
14 ph = cl_proc_handler_new_func(cl_proc_intern_handler);
15 return ph;
16 }
17
18 CLProcHandler *cl_proc_handler_new_func(CLProcFunc f)
19 {
20 CLProcHandler *cph = g_new(CLProcHandler,1);
21 cph->type = CLPROC_FUNC;
22 cph->u.func = f;
23 return cph;
24 }
25
26 CLProcHandler *cl_proc_handler_new_proc(CLProc *cp)
27 {
28 CLProcHandler *cph = g_new(CLProcHandler,1);
29 cph->type = CLPROC_PROC;
30 cph->u.proc = cp;
31 return cph;
32 }
33
34 CLProc *cl_proc_new(void)
35 {
36 CLProc *ret = g_new(CLProc,1);
37 ret->table = g_hash_table_new(g_str_hash,g_str_equal);
38 ret->default_h = default_handler();
39 return ret;
40 }
41
42 void cl_proc_free(CLProc *proc)
43 {
44
45 }
46
47 void cl_proc_add_handler(CLProc *proc, gchar *str,
48 CLProcHandler *handler)
49 {
50 g_assert(proc != NULL);
51 g_hash_table_replace(proc->table, str, handler);
52 }
53
54 void cl_proc_add_handler_func(CLProc *proc, gchar *str,
55 CLProcFunc func)
56 {
57 CLProcHandler *ph;
58
59 g_assert(proc != NULL);
60 ph = cl_proc_handler_new_func(func);
61 cl_proc_add_handler(proc, str, ph);
62 }
63
64 void cl_proc_add_handler_proc(CLProc *proc, gchar *str,
65 CLProc *hproc)
66 {
67 CLProcHandler *ph;
68
69 g_assert(proc != NULL);
70 ph = cl_proc_handler_new_proc(hproc);
71 cl_proc_add_handler(proc, str, ph);
72 }
73
74 void cl_proc_set_default(CLProc *proc, CLProcHandler *ph)
75 {
76 g_assert(proc != NULL);
77 proc->default_h = ph;
78 }
79
80 void cl_proc_register_keywords(CLProc *proc, ...)
81 {
82 va_list args;
83 g_assert(proc != NULL);
84
85 va_start(args,proc);
86 for (;;) {
87 gchar *k = va_arg(args, gchar*);
88 if (k == NULL)
89 break;
90 if (g_hash_table_lookup(proc->table, k) != NULL)
91 g_hash_table_insert(proc->table, k, default_handler());
92 }
93 va_end(args);
94 }
95
96 void cl_process(GList *tree, CLProc *proc)
97 {
98 GList *lst;
99 CLProcHandler *handler;
100
101 g_assert(proc != NULL);
102
103 if (!tree) return;
104
105 for (lst = tree; lst != NULL; lst = lst->next) {
106 CL_ASSERT_NODE(lst->data);
107 handler = g_hash_table_lookup(proc->table, CL_ID(lst->data));
108 if (!handler)
109 handler = default_handler();
110 if (handler->type == CLPROC_FUNC)
111 handler->u.func(CL_NODE(lst->data));
112 else
113 cl_process(CL_BLOCK(lst->data), handler->u.proc);
114 }
115 }
This page took 0.038054 seconds and 4 git commands to generate.