]> icculus.org git repositories - dana/openbox.git/blob - openbox/engine.c
put focus_cycle into focus.c, use it there in the action. improved it as well to...
[dana/openbox.git] / openbox / engine.c
1 #include "engine.h"
2 #include "parse.h"
3
4 #include <glib.h>
5 #include <gmodule.h>
6 #ifdef HAVE_STDLIB_H
7 #  include <stdlib.h>
8 #endif
9
10 char *engine_name;
11 char *engine_theme;
12 char *engine_layout;
13 char *engine_font;
14 gboolean engine_shadow;
15 int engine_shadow_offset;
16 int engine_shadow_tint;
17
18 EngineFrameNew *engine_frame_new;
19 EngineFrameGrabClient *engine_frame_grab_client;
20 EngineFrameReleaseClient *engine_frame_release_client;
21 EngineFrameAdjustArea *engine_frame_adjust_area;
22 EngineFrameAdjustShape *engine_frame_adjust_shape;
23 EngineFrameAdjustState *engine_frame_adjust_state;
24 EngineFrameAdjustFocus *engine_frame_adjust_focus;
25 EngineFrameAdjustTitle *engine_frame_adjust_title;
26 EngineFrameAdjustIcon *engine_frame_adjust_icon;
27 EngineFrameShow *engine_frame_show;
28 EngineFrameHide *engine_frame_hide;
29 EngineGetContext *engine_get_context;
30 EngineRenderLabel *engine_render_label;
31 EngineSizeLabel *engine_size_label;
32
33 static GModule *module = NULL;
34 static EngineStartup *estartup = NULL;
35 static EngineShutdown *eshutdown = NULL;
36
37 #define LOADSYM(name, var) \
38     if (!g_module_symbol(module, #name, (gpointer*)&var)) { \
39         g_warning("Failed to load symbol %s from engine", #name); \
40         return FALSE; \
41     }
42
43 static gboolean load(char *name)
44 {
45     char *path;
46
47     g_assert(module == NULL);
48
49     path = g_build_filename(g_get_home_dir(), ".openbox", "engines", name,
50                             NULL);
51     module = g_module_open(path, 0);
52     g_free(path);
53
54     if (module == NULL) {
55         path = g_build_filename(ENGINEDIR, name, NULL);
56         module = g_module_open(path, 0);
57         g_free(path);
58     }
59
60     if (module == NULL) {
61         g_warning(g_module_error());
62         return FALSE;
63     }
64
65     /* load the engine's symbols */
66     LOADSYM(startup, estartup);
67     LOADSYM(shutdown, eshutdown);
68     LOADSYM(frame_new, engine_frame_new);
69     LOADSYM(frame_grab_client, engine_frame_grab_client);
70     LOADSYM(frame_release_client, engine_frame_release_client);
71     LOADSYM(frame_adjust_area, engine_frame_adjust_area);
72     LOADSYM(frame_adjust_shape, engine_frame_adjust_shape);
73     LOADSYM(frame_adjust_state, engine_frame_adjust_state);
74     LOADSYM(frame_adjust_focus, engine_frame_adjust_focus);
75     LOADSYM(frame_adjust_title, engine_frame_adjust_title);
76     LOADSYM(frame_adjust_icon, engine_frame_adjust_icon);
77     LOADSYM(frame_show, engine_frame_show);
78     LOADSYM(frame_hide, engine_frame_hide);
79     LOADSYM(get_context, engine_get_context);
80     LOADSYM(render_label, engine_render_label);
81     LOADSYM(size_label, engine_size_label);
82
83     if (!estartup())
84         return FALSE;
85
86     return TRUE;
87 }
88
89 static void parse_assign(char *name, ParseToken *value)
90 {
91     if (!g_ascii_strcasecmp(name, "engine")) {
92         if (value->type != TOKEN_STRING)
93             yyerror("invalid value");
94         else {
95             g_free(engine_name);
96             engine_name = g_strdup(value->data.string);
97         }
98     } else if (!g_ascii_strcasecmp(name, "theme")) {
99         if (value->type != TOKEN_STRING)
100             yyerror("invalid value");
101         else {
102             g_free(engine_theme);
103             engine_theme = g_strdup(value->data.string);
104         }
105     } else if (!g_ascii_strcasecmp(name, "titlebarlayout")) {
106         if (value->type != TOKEN_STRING)
107             yyerror("invalid value");
108         else {
109             g_free(engine_layout);
110             engine_layout = g_strdup(value->data.string);
111         }
112     } else if (!g_ascii_strcasecmp(name, "font.title")) {
113         if (value->type != TOKEN_STRING)
114             yyerror("invalid value");
115         else {
116             g_free(engine_font);
117             engine_font = g_strdup(value->data.string);
118         }
119     } else if (!g_ascii_strcasecmp(name, "font.title.shadow")) {
120         if (value->type != TOKEN_BOOL)
121             yyerror("invalid value");
122         else {
123             engine_shadow = value->data.bool;
124         }
125     } else if (!g_ascii_strcasecmp(name, "font.title.shadow.offset")) {
126         if (value->type != TOKEN_INTEGER)
127             yyerror("invalid value");
128         else {
129             engine_shadow_offset = value->data.integer;
130         }
131     } else if (!g_ascii_strcasecmp(name, "font.title.shadow.tint")) {
132         if (value->type != TOKEN_INTEGER)
133             yyerror("invalid value");
134         else {
135             engine_shadow_tint = value->data.integer;
136             if (engine_shadow_tint < -100) engine_shadow_tint = -100;
137             else if (engine_shadow_tint > 100) engine_shadow_tint = 100;
138         }
139     } else
140         yyerror("invalid option");
141     parse_free_token(value);
142 }
143
144 void engine_startup()
145 {
146     module = NULL;
147     engine_name = g_strdup(DEFAULT_ENGINE);
148     engine_theme = NULL;
149     engine_layout = g_strdup("NLIMC");
150     engine_font = g_strdup("Sans-7");
151     engine_shadow = FALSE;
152     engine_shadow_offset = 1;
153     engine_shadow_tint = 25;
154
155     parse_reg_section("engine", NULL, parse_assign);
156 }
157
158 void engine_load()
159 {
160     if (load(engine_name))
161         return;
162     g_warning("Failed to load the engine '%s'", engine_name);
163     g_message("Falling back to the default: '%s'", DEFAULT_ENGINE);
164     if (module != NULL) {
165         g_module_close(module);
166         module = NULL;
167     }
168     if (!load(DEFAULT_ENGINE)) {
169         g_critical("Failed to load the engine '%s'. Aborting", DEFAULT_ENGINE);
170         exit(1);
171     }
172 }
173
174 void engine_shutdown()
175 {
176     g_free(engine_name);
177     if (module != NULL) {
178         eshutdown();
179         g_module_close(module);
180     }
181 }