desktop-shell: Adapt to the new config parser API

This lets the code for adding panel launchers and setting up the
background to be moved into panel_* and background_* functions.

Note that this changes the behavior of the default launcher. Before
this change a default launcher would be added only if there was no
config file. Now a launcher is also added if there is no valid
launcher section.
dev
Ander Conselvan de Oliveira 12 years ago committed by Kristian Høgsberg
parent 3c36bf3486
commit 6d75da7906
  1. 171
      clients/desktop-shell.c

@ -57,6 +57,9 @@ struct desktop {
struct window *grab_window; struct window *grab_window;
struct widget *grab_widget; struct widget *grab_widget;
struct weston_config *config;
int locking;
enum cursor_type grab_cursor; enum cursor_type grab_cursor;
int painted; int painted;
@ -76,6 +79,7 @@ struct panel {
struct wl_list launcher_list; struct wl_list launcher_list;
struct panel_clock *clock; struct panel_clock *clock;
int painted; int painted;
uint32_t color;
}; };
struct background { struct background {
@ -83,6 +87,10 @@ struct background {
struct window *window; struct window *window;
struct widget *widget; struct widget *widget;
int painted; int painted;
char *image;
int type;
uint32_t color;
}; };
struct output { struct output {
@ -120,35 +128,8 @@ struct unlock_dialog {
struct desktop *desktop; struct desktop *desktop;
}; };
static char *key_background_image = DATADIR "/weston/pattern.png"; static void
static char *key_background_type = "tile"; panel_add_launchers(struct panel *panel, struct desktop *desktop);
static uint32_t key_panel_color = 0xaa000000;
static uint32_t key_background_color = 0xff002244;
static char *key_launcher_icon;
static char *key_launcher_path;
static void launcher_section_done(void *data);
static int key_locking = 1;
static const struct config_key shell_config_keys[] = {
{ "background-image", CONFIG_KEY_STRING, &key_background_image },
{ "background-type", CONFIG_KEY_STRING, &key_background_type },
{ "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
{ "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
{ "locking", CONFIG_KEY_BOOLEAN, &key_locking },
};
static const struct config_key launcher_config_keys[] = {
{ "icon", CONFIG_KEY_STRING, &key_launcher_icon },
{ "path", CONFIG_KEY_STRING, &key_launcher_path },
};
static const struct config_section config_sections[] = {
{ "shell",
shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
{ "launcher",
launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
launcher_section_done }
};
static void static void
sigchild_handler(int s) sigchild_handler(int s)
@ -292,7 +273,7 @@ panel_redraw_handler(struct widget *widget, void *data)
cr = widget_cairo_create(panel->widget); cr = widget_cairo_create(panel->widget);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
set_hex_color(cr, key_panel_color); set_hex_color(cr, panel->color);
cairo_paint(cr); cairo_paint(cr);
cairo_destroy(cr); cairo_destroy(cr);
@ -526,15 +507,16 @@ panel_destroy(struct panel *panel)
} }
static struct panel * static struct panel *
panel_create(struct display *display) panel_create(struct desktop *desktop)
{ {
struct panel *panel; struct panel *panel;
struct weston_config_section *s;
panel = malloc(sizeof *panel); panel = malloc(sizeof *panel);
memset(panel, 0, sizeof *panel); memset(panel, 0, sizeof *panel);
panel->base.configure = panel_configure; panel->base.configure = panel_configure;
panel->window = window_create_custom(display); panel->window = window_create_custom(desktop->display);
panel->widget = window_add_widget(panel->window, panel); panel->widget = window_add_widget(panel->window, panel);
wl_list_init(&panel->launcher_list); wl_list_init(&panel->launcher_list);
@ -547,6 +529,12 @@ panel_create(struct display *display)
panel_add_clock(panel); panel_add_clock(panel);
s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
weston_config_section_get_uint(s, "panel-color",
&panel->color, 0xaa000000);
panel_add_launchers(panel, desktop);
return panel; return panel;
} }
@ -676,7 +664,6 @@ background_draw(struct widget *widget, void *data)
double sx, sy, s; double sx, sy, s;
double tx, ty; double tx, ty;
struct rectangle allocation; struct rectangle allocation;
int type = -1;
struct display *display; struct display *display;
struct wl_region *opaque; struct wl_region *opaque;
@ -689,20 +676,10 @@ background_draw(struct widget *widget, void *data)
widget_get_allocation(widget, &allocation); widget_get_allocation(widget, &allocation);
image = NULL; image = NULL;
if (key_background_image) if (background->image)
image = load_cairo_surface(key_background_image); image = load_cairo_surface(background->image);
if (strcmp(key_background_type, "scale") == 0)
type = BACKGROUND_SCALE;
else if (strcmp(key_background_type, "scale-crop") == 0)
type = BACKGROUND_SCALE_CROP;
else if (strcmp(key_background_type, "tile") == 0)
type = BACKGROUND_TILE;
else
fprintf(stderr, "invalid background-type: %s\n",
key_background_type);
if (image && type != -1) { if (image && background->type != -1) {
im_w = cairo_image_surface_get_width(image); im_w = cairo_image_surface_get_width(image);
im_h = cairo_image_surface_get_height(image); im_h = cairo_image_surface_get_height(image);
sx = im_w / allocation.width; sx = im_w / allocation.width;
@ -710,7 +687,7 @@ background_draw(struct widget *widget, void *data)
pattern = cairo_pattern_create_for_surface(image); pattern = cairo_pattern_create_for_surface(image);
switch (type) { switch (background->type) {
case BACKGROUND_SCALE: case BACKGROUND_SCALE:
cairo_matrix_init_scale(&matrix, sx, sy); cairo_matrix_init_scale(&matrix, sx, sy);
cairo_pattern_set_matrix(pattern, &matrix); cairo_pattern_set_matrix(pattern, &matrix);
@ -733,7 +710,7 @@ background_draw(struct widget *widget, void *data)
cairo_pattern_destroy (pattern); cairo_pattern_destroy (pattern);
cairo_surface_destroy(image); cairo_surface_destroy(image);
} else { } else {
set_hex_color(cr, key_background_color); set_hex_color(cr, background->color);
} }
cairo_paint(cr); cairo_paint(cr);
@ -931,7 +908,7 @@ desktop_shell_prepare_lock_surface(void *data,
{ {
struct desktop *desktop = data; struct desktop *desktop = data;
if (!key_locking) { if (!desktop->locking) {
desktop_shell_unlock(desktop->shell); desktop_shell_unlock(desktop->shell);
return; return;
} }
@ -1001,6 +978,7 @@ background_destroy(struct background *background)
widget_destroy(background->widget); widget_destroy(background->widget);
window_destroy(background->window); window_destroy(background->window);
free(background->image);
free(background); free(background);
} }
@ -1008,6 +986,8 @@ static struct background *
background_create(struct desktop *desktop) background_create(struct desktop *desktop)
{ {
struct background *background; struct background *background;
struct weston_config_section *s;
char *type;
background = malloc(sizeof *background); background = malloc(sizeof *background);
memset(background, 0, sizeof *background); memset(background, 0, sizeof *background);
@ -1018,6 +998,29 @@ background_create(struct desktop *desktop)
window_set_user_data(background->window, background); window_set_user_data(background->window, background);
widget_set_redraw_handler(background->widget, background_draw); widget_set_redraw_handler(background->widget, background_draw);
s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
weston_config_section_get_string(s, "background-image",
&background->image,
DATADIR "/weston/pattern.png");
weston_config_section_get_uint(s, "background-color",
&background->color, 0xff002244);
weston_config_section_get_string(s, "background-type",
&type, "tile");
if (strcmp(type, "scale") == 0) {
background->type = BACKGROUND_SCALE;
} else if (strcmp(type, "scale-crop") == 0) {
background->type = BACKGROUND_SCALE_CROP;
} else if (strcmp(type, "tile") == 0) {
background->type = BACKGROUND_TILE;
} else {
background->type = -1;
fprintf(stderr, "invalid background-type: %s\n",
type);
}
free(type);
return background; return background;
} }
@ -1165,36 +1168,41 @@ global_handler(struct display *display, uint32_t id,
} }
static void static void
launcher_section_done(void *data) panel_add_launchers(struct panel *panel, struct desktop *desktop)
{ {
struct desktop *desktop = data; struct weston_config_section *s;
struct output *output; char *icon, *path;
const char *name;
int count;
count = 0;
s = NULL;
while (weston_config_next_section(desktop->config, &s, &name)) {
if (strcmp(name, "launcher") != 0)
continue;
weston_config_section_get_string(s, "icon", &icon, NULL);
weston_config_section_get_string(s, "path", &path, NULL);
if (icon != NULL && path != NULL) {
panel_add_launcher(panel, icon, path);
} else {
fprintf(stderr, "invalid launcher section\n");
continue;
}
if (key_launcher_icon == NULL || key_launcher_path == NULL) { free(icon);
fprintf(stderr, "invalid launcher section\n"); free(path);
return;
}
wl_list_for_each(output, &desktop->outputs, link) { count++;
panel_add_launcher(output->panel,
key_launcher_icon, key_launcher_path);
} }
free(key_launcher_icon); if (count == 0) {
key_launcher_icon = NULL; /* add default launcher */
free(key_launcher_path); panel_add_launcher(panel,
key_launcher_path = NULL;
}
static void
add_default_launcher(struct desktop *desktop)
{
struct output *output;
wl_list_for_each(output, &desktop->outputs, link)
panel_add_launcher(output->panel,
DATADIR "/weston/terminal.png", DATADIR "/weston/terminal.png",
BINDIR "/weston-terminal"); BINDIR "/weston-terminal");
}
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -1202,11 +1210,18 @@ int main(int argc, char *argv[])
struct desktop desktop = { 0 }; struct desktop desktop = { 0 };
int config_fd; int config_fd;
struct output *output; struct output *output;
int ret; struct weston_config_section *s;
desktop.unlock_task.run = unlock_dialog_finish; desktop.unlock_task.run = unlock_dialog_finish;
wl_list_init(&desktop.outputs); wl_list_init(&desktop.outputs);
config_fd = open_config_file("weston.ini");
desktop.config = weston_config_parse(config_fd);
close(config_fd);
s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
desktop.display = display_create(&argc, argv); desktop.display = display_create(&argc, argv);
if (desktop.display == NULL) { if (desktop.display == NULL) {
fprintf(stderr, "failed to create display: %m\n"); fprintf(stderr, "failed to create display: %m\n");
@ -1219,7 +1234,7 @@ int main(int argc, char *argv[])
wl_list_for_each(output, &desktop.outputs, link) { wl_list_for_each(output, &desktop.outputs, link) {
struct wl_surface *surface; struct wl_surface *surface;
output->panel = panel_create(desktop.display); output->panel = panel_create(&desktop);
surface = window_get_wl_surface(output->panel->window); surface = window_get_wl_surface(output->panel->window);
desktop_shell_set_panel(desktop.shell, desktop_shell_set_panel(desktop.shell,
output->output, surface); output->output, surface);
@ -1232,14 +1247,6 @@ int main(int argc, char *argv[])
grab_surface_create(&desktop); grab_surface_create(&desktop);
config_fd = open_config_file("weston.ini");
ret = parse_config_file(config_fd,
config_sections, ARRAY_LENGTH(config_sections),
&desktop);
close(config_fd);
if (ret < 0)
add_default_launcher(&desktop);
signal(SIGCHLD, sigchild_handler); signal(SIGCHLD, sigchild_handler);
display_run(desktop.display); display_run(desktop.display);

Loading…
Cancel
Save