void bar_run(struct bar *bar) { int pfds = bar->outputs->length + 2; struct pollfd *pfd = malloc(pfds * sizeof(struct pollfd)); bool dirty = true; pfd[0].fd = bar->ipc_event_socketfd; pfd[0].events = POLLIN; pfd[1].fd = bar->status_read_fd; pfd[1].events = POLLIN; int i; for (i = 0; i < bar->outputs->length; ++i) { struct output *output = bar->outputs->items[i]; pfd[i+2].fd = wl_display_get_fd(output->registry->display); pfd[i+2].events = POLLIN; } while (1) { if (dirty) { int i; for (i = 0; i < bar->outputs->length; ++i) { struct output *output = bar->outputs->items[i]; if (window_prerender(output->window) && output->window->cairo) { render(output, bar->config, bar->status); window_render(output->window); wl_display_flush(output->registry->display); } } } dirty = false; poll(pfd, pfds, -1); if (pfd[0].revents & POLLIN) { sway_log(L_DEBUG, "Got IPC event."); dirty = handle_ipc_event(bar); } if (bar->config->status_command && pfd[1].revents & POLLIN) { sway_log(L_DEBUG, "Got update from status command."); dirty = handle_status_line(bar); } // dispatch wl_display events for (i = 0; i < bar->outputs->length; ++i) { struct output *output = bar->outputs->items[i]; if (pfd[i+2].revents & POLLIN) { if (wl_display_dispatch(output->registry->display) == -1) { sway_log(L_ERROR, "failed to dispatch wl: %d", errno); } } else { wl_display_dispatch_pending(output->registry->display); } } } }
int main(int argc, char **argv) { init_log(L_INFO); surfaces = create_list(); registry = registry_poll(); if (argc < 4) { sway_abort("Do not run this program manually. See man 5 sway and look for output options."); } if (!registry->desktop_shell) { sway_abort("swaybg requires the compositor to support the desktop-shell extension."); } int desired_output = atoi(argv[1]); sway_log(L_INFO, "Using output %d of %d", desired_output, registry->outputs->length); int i; struct output_state *output = registry->outputs->items[desired_output]; struct window *window = window_setup(registry, 100, 100, false); if (!window) { sway_abort("Failed to create surfaces."); } window->width = output->width; window->height = output->height; desktop_shell_set_background(registry->desktop_shell, output->output, window->surface); list_add(surfaces, window); char *scaling_mode = argv[3]; cairo_surface_t *image = cairo_image_surface_create_from_png(argv[2]); double width = cairo_image_surface_get_width(image); double height = cairo_image_surface_get_height(image); for (i = 0; i < surfaces->length; ++i) { struct window *window = surfaces->items[i]; if (window_prerender(window) && window->cairo) { cairo_scale(window->cairo, window->width / width, window->height / height); cairo_set_source_surface(window->cairo, image, 0, 0); cairo_paint(window->cairo); window_render(window); } } while (wl_display_dispatch(registry->display) != -1); for (i = 0; i < surfaces->length; ++i) { struct window *window = surfaces->items[i]; window_teardown(window); } list_free(surfaces); registry_teardown(registry); return 0; }
/** * @brief Renders the windows. */ void toolkit_render (void) { Window *w; /* Render base. */ for (w = windows; w!=NULL; w = w->next) { if (!window_isFlag(w, WINDOW_NORENDER) && !window_isFlag(w, WINDOW_KILL)) { window_render(w); window_renderOverlay(w); } } }
/** * @brief Renders a button widget. * * @param tab WIDGET_BUTTON widget to render. * @param bx Base X position. * @param by Base Y position. */ static void tab_render( Widget* tab, double bx, double by ) { int i, x; Window *wdw; glColour *c, *lc; /** Get window. */ wdw = window_wget( tab->dat.tab.windows[ tab->dat.tab.active ] ); if (wdw == NULL) { WARN("Active window in widget '%s' not found in stack.", tab->name); return; } /* Render the active window. */ window_render( wdw ); /* Render tabs ontop. */ x = 20; for (i=0; i<tab->dat.tab.ntabs; i++) { if (i!=tab->dat.tab.active) { lc = toolkit_col; c = toolkit_colDark; /* Draw border. */ toolkit_drawRect( bx+x, by+0, tab->dat.tab.namelen[i] + 10, TAB_HEIGHT, lc, c ); toolkit_drawOutline( bx+x+1, by+1, tab->dat.tab.namelen[i] + 8, TAB_HEIGHT-1, 1., c, &cBlack ); } else { if (i==0) toolkit_drawRect( bx+x-1, by+0, 1, TAB_HEIGHT+1, toolkit_colDark, &cGrey20 ); else if (i==tab->dat.tab.ntabs-1) toolkit_drawRect( bx+x+tab->dat.tab.namelen[i]+9, by+0, 1, TAB_HEIGHT+1, toolkit_colDark, &cGrey20 ); } /* Draw text. */ gl_printRaw( &gl_defFont, bx+x + 5 + SCREEN_W/2, by + (TAB_HEIGHT-gl_defFont.h)/2 + SCREEN_H/2, &cBlack, tab->dat.tab.tabnames[i] ); /* Go to next line. */ x += 10 + tab->dat.tab.namelen[i]; } }
void bar_run(struct bar *bar) { fd_set readfds; int activity; bool dirty = true; while (1) { if (dirty) { struct output *output = bar->output; if (window_prerender(output->window) && output->window->cairo) { render(output, bar->config, bar->status); window_render(output->window); if (wl_display_dispatch(output->registry->display) == -1) { break; } } } dirty = false; FD_ZERO(&readfds); FD_SET(bar->ipc_event_socketfd, &readfds); FD_SET(bar->status_read_fd, &readfds); activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); if (activity < 0) { sway_log(L_ERROR, "polling failed: %d", errno); } if (FD_ISSET(bar->ipc_event_socketfd, &readfds)) { sway_log(L_DEBUG, "Got IPC event."); dirty = handle_ipc_event(bar); } if (bar->config->status_command && FD_ISSET(bar->status_read_fd, &readfds)) { sway_log(L_DEBUG, "Got update from status command."); dirty = handle_status_line(bar); } } }
int main(int argc, char **argv) { init_log(L_INFO); password = malloc(1024); // TODO: Let this grow password[0] = '\0'; surfaces = create_list(); registry = registry_poll(); if (!registry->swaylock) { sway_abort("swaylock requires the compositor to support the swaylock extension."); } int i; for (i = 0; i < registry->outputs->length; ++i) { struct output_state *output = registry->outputs->items[i]; struct window *window = window_setup(registry, output->width, output->height, true); if (!window) { sway_abort("Failed to create surfaces."); } list_add(surfaces, window); } registry->input->notify = notify_key; #ifdef WITH_GDK_PIXBUF GError *err = NULL; GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(argv[1], &err); // TODO: Parse i3lock arguments if (!pixbuf) { sway_abort("Failed to load background image."); } cairo_surface_t *image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf); g_object_unref(pixbuf); #else cairo_surface_t *image = cairo_image_surface_create_from_png(argv[1]); #endif //WITH_GDK_PIXBUF if (!image) { sway_abort("Failed to read background image."); } double width = cairo_image_surface_get_width(image); double height = cairo_image_surface_get_height(image); const char *scaling_mode_str = argv[2]; enum scaling_mode scaling_mode = SCALING_MODE_STRETCH; if (strcmp(scaling_mode_str, "stretch") == 0) { scaling_mode = SCALING_MODE_STRETCH; } else if (strcmp(scaling_mode_str, "fill") == 0) { scaling_mode = SCALING_MODE_FILL; } else if (strcmp(scaling_mode_str, "fit") == 0) { scaling_mode = SCALING_MODE_FIT; } else if (strcmp(scaling_mode_str, "center") == 0) { scaling_mode = SCALING_MODE_CENTER; } else if (strcmp(scaling_mode_str, "tile") == 0) { scaling_mode = SCALING_MODE_TILE; } else { sway_abort("Unsupported scaling mode: %s", scaling_mode_str); } for (i = 0; i < surfaces->length; ++i) { struct window *window = surfaces->items[i]; if (window_prerender(window) && window->cairo) { switch (scaling_mode) { case SCALING_MODE_STRETCH: cairo_scale(window->cairo, (double) window->width / width, (double) window->height / height); cairo_set_source_surface(window->cairo, image, 0, 0); break; case SCALING_MODE_FILL: { double window_ratio = (double) window->width / window->height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { double scale = (double) window->width / width; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, 0, (double) window->height/2 / scale - height/2); } else { double scale = (double) window->height / height; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, (double) window->width/2 / scale - width/2, 0); } break; } case SCALING_MODE_FIT: { double window_ratio = (double) window->width / window->height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { double scale = (double) window->height / height; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, (double) window->width/2 / scale - width/2, 0); } else { double scale = (double) window->width / width; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, 0, (double) window->height/2 / scale - height/2); } break; } case SCALING_MODE_CENTER: cairo_set_source_surface(window->cairo, image, (double) window->width/2 - width/2, (double) window->height/2 - height/2); break; case SCALING_MODE_TILE: { cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); cairo_set_source(window->cairo, pattern); break; } default: sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str); } cairo_paint(window->cairo); window_render(window); } } cairo_surface_destroy(image); bool locked = false; while (wl_display_dispatch(registry->display) != -1) { if (!locked) { for (i = 0; i < registry->outputs->length; ++i) { struct output_state *output = registry->outputs->items[i]; struct window *window = surfaces->items[i]; lock_set_lock_surface(registry->swaylock, output->output, window->surface); } locked = true; } } for (i = 0; i < surfaces->length; ++i) { struct window *window = surfaces->items[i]; window_teardown(window); } list_free(surfaces); registry_teardown(registry); return 0; }
void render(struct render_data *render_data) { int i; for (i = 0; i < render_data->surfaces->length; ++i) { sway_log(L_DEBUG, "Render surface %d of %d", i, render_data->surfaces->length); struct window *window = render_data->surfaces->items[i]; if (!window_prerender(window) || !window->cairo) { continue; } // Reset the transformation matrix cairo_identity_matrix(window->cairo); if (render_data->num_images == 0 || render_data->color_set) { render_color(window, render_data->color); } if (render_data->num_images == -1) { // One background for all render_image(window, render_data->image, render_data->scaling_mode); } else if (render_data->num_images >= 1) { // Different backgrounds if (render_data->images[i] != NULL) { render_image(window, render_data->images[i], render_data->scaling_mode); } } // Reset the transformation matrix again cairo_identity_matrix(window->cairo); // Draw specific values (copied from i3) const int ARC_RADIUS = 50; const int ARC_THICKNESS = 10; const float TYPE_INDICATOR_RANGE = M_PI / 3.0f; const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f; // Add visual indicator if (show_indicator && render_data->auth_state != AUTH_STATE_IDLE) { // Draw circle cairo_set_line_width(window->cairo, ARC_THICKNESS); cairo_arc(window->cairo, window->width/2, window->height/2, ARC_RADIUS, 0, 2 * M_PI); switch (render_data->auth_state) { case AUTH_STATE_INPUT: case AUTH_STATE_BACKSPACE: { cairo_set_source_rgba(window->cairo, 0, 0, 0, 0.75); cairo_fill_preserve(window->cairo); cairo_set_source_rgb(window->cairo, 51.0 / 255, 125.0 / 255, 0); cairo_stroke(window->cairo); } break; case AUTH_STATE_VALIDATING: { cairo_set_source_rgba(window->cairo, 0, 114.0 / 255, 255.0 / 255, 0.75); cairo_fill_preserve(window->cairo); cairo_set_source_rgb(window->cairo, 51.0 / 255, 0, 250.0 / 255); cairo_stroke(window->cairo); } break; case AUTH_STATE_INVALID: { cairo_set_source_rgba(window->cairo, 250.0 / 255, 0, 0, 0.75); cairo_fill_preserve(window->cairo); cairo_set_source_rgb(window->cairo, 125.0 / 255, 51.0 / 255, 0); cairo_stroke(window->cairo); } break; default: break; } // Draw a message char *text = NULL; cairo_set_source_rgb(window->cairo, 0, 0, 0); cairo_set_font_size(window->cairo, ARC_RADIUS/3.0f); switch (render_data->auth_state) { case AUTH_STATE_VALIDATING: text = "verifying"; break; case AUTH_STATE_INVALID: text = "wrong"; break; default: break; } if (text) { cairo_text_extents_t extents; double x, y; cairo_text_extents(window->cairo, text, &extents); x = window->width/2 - ((extents.width/2) + extents.x_bearing); y = window->height/2 - ((extents.height/2) + extents.y_bearing); cairo_move_to(window->cairo, x, y); cairo_show_text(window->cairo, text); cairo_close_path(window->cairo); cairo_new_sub_path(window->cairo); } // Typing indicator: Highlight random part on keypress if (render_data->auth_state == AUTH_STATE_INPUT || render_data->auth_state == AUTH_STATE_BACKSPACE) { static double highlight_start = 0; highlight_start += (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5; cairo_arc(window->cairo, window->width/2, window->height/2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_RANGE); if (render_data->auth_state == AUTH_STATE_INPUT) { cairo_set_source_rgb(window->cairo, 51.0 / 255, 219.0 / 255, 0); } else { cairo_set_source_rgb(window->cairo, 219.0 / 255, 51.0 / 255, 0); } cairo_stroke(window->cairo); // Draw borders cairo_set_source_rgb(window->cairo, 0, 0, 0); cairo_arc(window->cairo, window->width/2, window->height/2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_BORDER_THICKNESS); cairo_stroke(window->cairo); cairo_arc(window->cairo, window->width/2, window->height/2, ARC_RADIUS, highlight_start + TYPE_INDICATOR_RANGE, (highlight_start + TYPE_INDICATOR_RANGE) + TYPE_INDICATOR_BORDER_THICKNESS); cairo_stroke(window->cairo); } // Draw inner + outer border of the circle cairo_set_source_rgb(window->cairo, 0, 0, 0); cairo_set_line_width(window->cairo, 2.0); cairo_arc(window->cairo, window->width/2, window->height/2, ARC_RADIUS - ARC_THICKNESS/2, 0, 2*M_PI); cairo_stroke(window->cairo); cairo_arc(window->cairo, window->width/2, window->height/2, ARC_RADIUS + ARC_THICKNESS/2, 0, 2*M_PI); cairo_stroke(window->cairo); } window_render(window); } }
int main(int argc, char **argv) { char *image_path = NULL; char *scaling_mode_str = "fit"; init_log(L_INFO); static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"image", required_argument, NULL, 'i'}, {"scaling", required_argument, NULL, 's'}, {"tiling", no_argument, NULL, 't'}, {"version", no_argument, NULL, 'v'}, {0, 0, 0, 0} }; const char *usage = "Usage: swaylock [options...]\n" "\n" " -h, --help Show help message and quit.\n" " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n" " -t, --tiling Same as --scaling=tile.\n" " -v, --version Show the version number and quit.\n" " -i, --image <path> Display the given image.\n"; int c; while (1) { int option_index = 0; c = getopt_long(argc, argv, "hi:s:tv", long_options, &option_index); if (c == -1) { break; } switch (c) { case 'i': image_path = optarg; break; case 's': scaling_mode_str = optarg; break; case 't': scaling_mode_str = "tile"; break; case 'v': #if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE fprintf(stdout, "swaylock version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH); #else fprintf(stdout, "version not detected\n"); #endif exit(EXIT_SUCCESS); break; default: fprintf(stderr, "%s", usage); exit(EXIT_FAILURE); } } // TODO: support locking without image if (!image_path) { fprintf(stderr, "No image specified!\n"); exit(EXIT_FAILURE); } password = malloc(1024); // TODO: Let this grow password[0] = '\0'; surfaces = create_list(); registry = registry_poll(); if (!registry->swaylock) { sway_abort("swaylock requires the compositor to support the swaylock extension."); } int i; for (i = 0; i < registry->outputs->length; ++i) { struct output_state *output = registry->outputs->items[i]; struct window *window = window_setup(registry, output->width, output->height, true); if (!window) { sway_abort("Failed to create surfaces."); } list_add(surfaces, window); } registry->input->notify = notify_key; #ifdef WITH_GDK_PIXBUF GError *err = NULL; GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err); if (!pixbuf) { sway_abort("Failed to load background image."); } cairo_surface_t *image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf); g_object_unref(pixbuf); #else cairo_surface_t *image = cairo_image_surface_create_from_png(argv[1]); #endif //WITH_GDK_PIXBUF if (!image) { sway_abort("Failed to read background image."); } double width = cairo_image_surface_get_width(image); double height = cairo_image_surface_get_height(image); enum scaling_mode scaling_mode = SCALING_MODE_STRETCH; if (strcmp(scaling_mode_str, "stretch") == 0) { scaling_mode = SCALING_MODE_STRETCH; } else if (strcmp(scaling_mode_str, "fill") == 0) { scaling_mode = SCALING_MODE_FILL; } else if (strcmp(scaling_mode_str, "fit") == 0) { scaling_mode = SCALING_MODE_FIT; } else if (strcmp(scaling_mode_str, "center") == 0) { scaling_mode = SCALING_MODE_CENTER; } else if (strcmp(scaling_mode_str, "tile") == 0) { scaling_mode = SCALING_MODE_TILE; } else { sway_abort("Unsupported scaling mode: %s", scaling_mode_str); } for (i = 0; i < surfaces->length; ++i) { struct window *window = surfaces->items[i]; if (window_prerender(window) && window->cairo) { switch (scaling_mode) { case SCALING_MODE_STRETCH: cairo_scale(window->cairo, (double) window->width / width, (double) window->height / height); cairo_set_source_surface(window->cairo, image, 0, 0); break; case SCALING_MODE_FILL: { double window_ratio = (double) window->width / window->height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { double scale = (double) window->width / width; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, 0, (double) window->height/2 / scale - height/2); } else { double scale = (double) window->height / height; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, (double) window->width/2 / scale - width/2, 0); } break; } case SCALING_MODE_FIT: { double window_ratio = (double) window->width / window->height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { double scale = (double) window->height / height; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, (double) window->width/2 / scale - width/2, 0); } else { double scale = (double) window->width / width; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, 0, (double) window->height/2 / scale - height/2); } break; } case SCALING_MODE_CENTER: cairo_set_source_surface(window->cairo, image, (double) window->width/2 - width/2, (double) window->height/2 - height/2); break; case SCALING_MODE_TILE: { cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); cairo_set_source(window->cairo, pattern); break; } default: sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str); } cairo_paint(window->cairo); window_render(window); } } cairo_surface_destroy(image); bool locked = false; while (wl_display_dispatch(registry->display) != -1) { if (!locked) { for (i = 0; i < registry->outputs->length; ++i) { struct output_state *output = registry->outputs->items[i]; struct window *window = surfaces->items[i]; lock_set_lock_surface(registry->swaylock, output->output, window->surface); } locked = true; } } for (i = 0; i < surfaces->length; ++i) { struct window *window = surfaces->items[i]; window_teardown(window); } list_free(surfaces); registry_teardown(registry); return 0; }
void render_image(struct window *window, cairo_surface_t *image, enum scaling_mode scaling_mode) { double width = cairo_image_surface_get_width(image); double height = cairo_image_surface_get_height(image); switch (scaling_mode) { case SCALING_MODE_STRETCH: cairo_scale(window->cairo, (double) window->width / width, (double) window->height / height); cairo_set_source_surface(window->cairo, image, 0, 0); break; case SCALING_MODE_FILL: { double window_ratio = (double) window->width / window->height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { double scale = (double) window->width / width; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, 0, (double) window->height/2 / scale - height/2); } else { double scale = (double) window->height / height; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, (double) window->width/2 / scale - width/2, 0); } break; } case SCALING_MODE_FIT: { double window_ratio = (double) window->width / window->height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { double scale = (double) window->height / height; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, (double) window->width/2 / scale - width/2, 0); } else { double scale = (double) window->width / width; cairo_scale(window->cairo, scale, scale); cairo_set_source_surface(window->cairo, image, 0, (double) window->height/2 / scale - height/2); } break; } case SCALING_MODE_CENTER: cairo_set_source_surface(window->cairo, image, (double) window->width/2 - width/2, (double) window->height/2 - height/2); break; case SCALING_MODE_TILE: { cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); cairo_set_source(window->cairo, pattern); break; } } cairo_paint(window->cairo); window_render(window); }
void render_color(struct window *window, uint32_t color) { cairo_set_source_u32(window->cairo, color); cairo_paint(window->cairo); window_render(window); }