rofi 2.1.2
view.c
Go to the documentation of this file.
1/*
2 * rofi
3 *
4 * MIT/X11 License
5 * Copyright © 2013-2020 Qball Cow <qball@gmpclient.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
29#define G_LOG_DOMAIN "View"
30
31#include <config.h>
32#include <stdint.h>
33#include <unistd.h>
34
35#include <cairo.h>
36
37#include "rofi.h"
38#include "settings.h"
39#include "timings.h"
40
41#include "display.h"
42#include "helper-theme.h"
43#include "mode.h"
44
45#include "view-internal.h"
46#include "view.h"
47
48#include "theme.h"
49
56static void wayland_rofi_view_update(RofiViewState *state, gboolean qr);
57
58void process_result(RofiViewState *state);
62static struct {
66 guint idle_timeout;
68 unsigned long long count;
70 guint repaint_source;
72 gboolean fullscreen;
73
74 int monitor_width;
76
77 int surface_width;
79
80 int menu_width;
81 int menu_height;
82
83 int menu_x;
84 int menu_y;
85
86} WlState = {
87 .flags = MENU_NORMAL,
88 .idle_timeout = 0,
89 .count = 0L,
90 .repaint_source = 0,
91 .fullscreen = FALSE,
92 .monitor_width = 0,
93 .monitor_height = 0,
94
95 .surface_width = 0,
96 .surface_height = 0,
97
98 .menu_width = 0,
99 .menu_height = 0,
100
101 .menu_x = 0,
102 .menu_y = 0,
104
105static void wayland_rofi_view_get_current_monitor(int *width, int *height) {
106 // TODO: handle changing monitor resolution
107 if (WlState.monitor_width == 0 && WlState.monitor_height == 0) {
109 &WlState.monitor_height);
110 }
111
112 if (width) {
113 *width = WlState.monitor_width;
114 }
115 if (height) {
116 *height = WlState.monitor_height;
117 }
118}
119
120static gboolean wayland_rofi_view_repaint(G_GNUC_UNUSED void *data) {
122 if (state) {
123 // Repaint the view (if needed).
124 // After a resize the edit_pixmap surface might not contain anything
125 // anymore. If we already re-painted, this does nothing.
127 WlState.repaint_source = 0;
128 }
129 return G_SOURCE_REMOVE;
130}
131
136
138 G_GNUC_UNUSED RofiViewState *state) {}
139
141 return rofi_theme_get_position(WIDGET(state->main_window), "location",
143}
144
146 char *property = ori == ROFI_ORIENTATION_HORIZONTAL ? "x-offset" : "y-offset";
147
148 RofiDistance offset =
149 rofi_theme_get_distance(WIDGET(state->main_window), property, 0);
150 return distance_get_pixel(offset, ori);
151}
152
153static void window_update_size_normal(RofiViewState *state, int offset_x,
154 int offset_y) {
155 WlState.surface_width = state->width;
156 WlState.surface_height = state->height;
157 WlState.menu_x = 0;
158 WlState.menu_y = 0;
159
160 display_set_surface_dimensions(state->width, state->height, offset_x,
161 offset_y, rofi_get_location(state));
162}
163
165 int offset_x, int offset_y) {
166 int screen_width = 0;
167 int screen_height = 0;
168 int loc = rofi_get_location(state);
169
170 wayland_rofi_view_get_current_monitor(&screen_width, &screen_height);
171
172 if (screen_width <= 0 || screen_height <= 0) {
173 screen_width = state->width;
174 screen_height = state->height;
175 }
176
177 WlState.surface_width = screen_width;
178 WlState.surface_height = screen_height;
179
180 switch (loc) {
181 case WL_NORTH_WEST:
182 WlState.menu_x = offset_x;
183 WlState.menu_y = offset_y;
184 break;
185 case WL_NORTH:
186 WlState.menu_x =
187 (WlState.surface_width - WlState.menu_width) / 2 + offset_x;
188 WlState.menu_y = offset_y;
189 break;
190 case WL_NORTH_EAST:
191 WlState.menu_x = WlState.surface_width - WlState.menu_width + offset_x;
192 WlState.menu_y = offset_y;
193 break;
194 case WL_EAST:
195 WlState.menu_x = WlState.surface_width - WlState.menu_width + offset_x;
196 WlState.menu_y =
197 (WlState.surface_height - WlState.menu_height) / 2 + offset_y;
198 break;
199 case WL_SOUTH_EAST:
200 WlState.menu_x = WlState.surface_width - WlState.menu_width + offset_x;
201 WlState.menu_y = WlState.surface_height - WlState.menu_height + offset_y;
202 break;
203 case WL_SOUTH:
204 WlState.menu_x =
205 (WlState.surface_width - WlState.menu_width) / 2 + offset_x;
206 WlState.menu_y = WlState.surface_height - WlState.menu_height + offset_y;
207 break;
208 case WL_SOUTH_WEST:
209 WlState.menu_x = offset_x;
210 WlState.menu_y = WlState.surface_height - WlState.menu_height + offset_y;
211 break;
212 case WL_WEST:
213 WlState.menu_x = offset_x;
214 WlState.menu_y =
215 (WlState.surface_height - WlState.menu_height) / 2 + offset_y;
216 break;
217 case WL_CENTER:
218 default:
219 WlState.menu_x =
220 (WlState.surface_width - WlState.menu_width) / 2 + offset_x;
221 WlState.menu_y =
222 (WlState.surface_height - WlState.menu_height) / 2 + offset_y;
223 break;
224 }
225
226 /* Click-capture is limited to the current output. If the menu is larger than
227 * the output and visually overflows onto another output, the overflowed area
228 * will not be part of the capture surface and clicks there will not trigger
229 * click-to-exit.
230 */
231 display_set_surface_dimensions(WlState.surface_width, WlState.surface_height,
232 0, 0, WL_NORTH_WEST);
233}
234
236 if (state == NULL) {
237 return;
238 }
239
240 int offset_x = rofi_get_offset_px(state, ROFI_ORIENTATION_HORIZONTAL);
241 int offset_y = rofi_get_offset_px(state, ROFI_ORIENTATION_VERTICAL);
242
243 widget_resize(WIDGET(state->main_window), state->width, state->height);
244
245 WlState.menu_width = state->width;
246 WlState.menu_height = state->height;
247
248 if (config.click_to_exit) {
249 window_update_size_with_outside_click(state, offset_x, offset_y);
250 } else {
251 window_update_size_normal(state, offset_x, offset_y);
252 }
254}
255
256static void wayland_rofi_view_set_size(RofiViewState *state, gint width,
257 gint height) {
258 if (width > -1) {
259 state->width = width;
260 }
261 if (height > -1) {
262 state->height = height;
263 }
265}
266
267static void wayland_rofi_view_get_size(RofiViewState *state, gint *width,
268 gint *height) {
269 *width = state->width;
270 *height = state->height;
271}
272
273static void wayland_rofi_view_ping_mouse(RofiViewState *state) { (void)state; }
274
275static gboolean wayland_rofi_view_reload_idle(G_GNUC_UNUSED gpointer data) {
277
278 /* Clear before refiltering: a reload signalled during the pass, such as a
279 * finished icon fetch, would otherwise coalesce into it and be lost. */
280 WlState.idle_timeout = 0;
281
282 if (state) {
283 // For UI update on this.
284 if (state->tb_total_rows) {
285 char *r = g_strdup_printf("%u", mode_get_num_entries(state->sw));
286 textbox_text(state->tb_total_rows, r);
287 g_free(r);
288 }
289 state->reload = TRUE;
290 state->refilter = TRUE;
291
293 }
294 return G_SOURCE_REMOVE;
295}
296
297static void wayland_rofi_view_reload(void) {
298 // @TODO add check if current view is equal to the callee
299 if (WlState.idle_timeout == 0) {
300 WlState.idle_timeout =
301 g_timeout_add(1000 / 15, wayland_rofi_view_reload_idle, NULL);
302 }
303}
306 if (state && WlState.repaint_source == 0) {
307 WlState.count++;
308 g_debug("redraw %llu", WlState.count);
309
311
312 WlState.repaint_source = g_idle_add_full(
313 G_PRIORITY_HIGH_IDLE, wayland_rofi_view_repaint, NULL, NULL);
314 }
315}
316
317static void wayland___create_window(MenuFlags menu_flags) {
318
320
321 TICK_N("create cairo surface");
322 PangoContext *p = pango_context_new();
323 pango_context_set_font_map(p, pango_cairo_font_map_get_default());
324 TICK_N("pango cairo font setup");
325
326 WlState.flags = menu_flags;
327
328 // Setup dpi
329 PangoFontMap *font_map = pango_cairo_font_map_get_default();
330 if (config.dpi > 1) {
331 pango_cairo_font_map_set_resolution((PangoCairoFontMap *)font_map,
332 (double)config.dpi);
333 } else if (config.dpi == 0 || config.dpi == 1) {
334 // This is an heuristic that works well for simple cases (eg single monitor)
335 // Accurate dpi information only comes after we display the first surface on
336 // the screen when it's too late to use for metric units.
337 double dpi = wayland_get_dpi_estimation();
338 if (dpi >= 0) {
339 config.dpi = dpi;
340 pango_cairo_font_map_set_resolution((PangoCairoFontMap *)font_map, dpi);
341 } else {
342 g_warning(
343 "DPI auto-detect failed, the output is not known yet or does not "
344 "provide a physical size");
345 config.dpi =
346 pango_cairo_font_map_get_resolution((PangoCairoFontMap *)font_map);
347 }
348 } else {
349 // default pango is 96.
350 config.dpi =
351 pango_cairo_font_map_get_resolution((PangoCairoFontMap *)font_map);
352 }
353 // Setup font.
354 // Dummy widget.
355 box *win = box_create(NULL, "window", ROFI_ORIENTATION_HORIZONTAL);
356 const char *font =
358 if (font) {
359 PangoFontDescription *pfd = pango_font_description_from_string(font);
360 if (helper_validate_font(pfd, font)) {
361 pango_context_set_font_description(p, pfd);
362 }
363 pango_font_description_free(pfd);
364 }
365 PangoLanguage *l = pango_language_get_default();
366 pango_context_set_language(p, l);
367 TICK_N("configure font");
368
369 // Tell textbox to use this context.
371 // cleanup
372 g_object_unref(p);
373
374 WlState.fullscreen = rofi_theme_get_boolean(WIDGET(win), "fullscreen", FALSE);
375
376 if (WlState.fullscreen) {
378 }
379
380 widget_free(WIDGET(win));
381
382 TICK_N("done");
383}
384
391 int screen_width = 1920;
392 display_get_surface_dimensions(&screen_width, NULL);
393
394 if (WlState.fullscreen == TRUE) {
395 state->width = screen_width;
396 return;
397 }
398
399 // Calculate as float to stop silly, big rounding down errors.
400 state->width = (screen_width / 100.0f) * DEFAULT_MENU_WIDTH;
401 // Use theme configured width, if set.
403 "width", state->width);
405}
406
407static void wayland_rofi_view_update(RofiViewState *state, gboolean qr) {
408 if (!widget_need_redraw(WIDGET(state->main_window))) {
409 return;
410 }
411 g_debug("Redraw view");
412 TICK();
413
414 int buffer_width = state->width;
415 int buffer_height = state->height;
416
417 if (config.click_to_exit) {
418 buffer_width = WlState.surface_width;
419 buffer_height = WlState.surface_height;
420 }
421
422 if (state->pool == NULL) {
423 state->pool = display_buffer_pool_new(buffer_width, buffer_height);
424 if (state->pool == NULL) {
425 return;
426 }
427 }
428
429 cairo_surface_t *surface = display_buffer_pool_get_next_buffer(state->pool);
430 if (surface == NULL) {
431 // no available buffer, bail out
432 return;
433 }
434
435 cairo_t *d = cairo_create(surface);
436 cairo_set_operator(d, CAIRO_OPERATOR_SOURCE);
437
438 // Paint the background transparent.
439 cairo_set_source_rgba(d, 0, 0, 0, 0);
440 guint scale = display_scale();
441 cairo_surface_set_device_scale(surface, scale, scale);
442 cairo_paint(d);
443 TICK_N("Background");
444
445 // Always paint as overlay over the background.
446 cairo_set_operator(d, CAIRO_OPERATOR_OVER);
447
448 if (config.click_to_exit) {
449 g_debug("draw capture mode: surface=%dx%d menu=%dx%d pos=(%d,%d)",
450 WlState.surface_width, WlState.surface_height, WlState.menu_width,
451 WlState.menu_height, WlState.menu_x, WlState.menu_y);
452 cairo_translate(d, WlState.menu_x, WlState.menu_y);
453 } else {
454 g_debug("draw normal mode: surface=%dx%d", buffer_width, buffer_height);
455 }
456
457 widget_draw(WIDGET(state->main_window), d);
458
459 TICK_N("widgets");
460 cairo_destroy(d);
461 display_surface_commit(surface);
462
463 if (qr) {
465 }
466}
467
469 if (WlState.repaint_source == 0) {
470 WlState.repaint_source = g_idle_add_full(
471 G_PRIORITY_HIGH_IDLE, wayland_rofi_view_repaint, NULL, NULL);
472 }
473}
474
476 if (WlState.fullscreen == TRUE) {
477 int height = 1080;
478 display_get_surface_dimensions(NULL, &height);
479 return height;
480 }
481
482 RofiDistance h =
483 rofi_theme_get_distance(WIDGET(state->main_window), "height", 0);
484 unsigned int height = distance_get_pixel(h, ROFI_ORIENTATION_VERTICAL);
485 // If height is set, return it.
486 if (height > 0) {
487 return height;
488 }
489 // Autosize based on widgets.
490 widget *main_window = WIDGET(state->main_window);
491
492 return widget_get_desired_height(main_window, state->width);
493}
494
496
497static void wayland_rofi_view_cleanup(void) {
498 g_debug("Cleanup.");
499 if (WlState.idle_timeout > 0) {
500 g_source_remove(WlState.idle_timeout);
501 WlState.idle_timeout = 0;
502 }
504 g_source_remove(CacheState.refilter_timeout);
506 }
508 g_source_remove(CacheState.overlay_timeout);
510 }
511 if (CacheState.user_timeout > 0) {
512 g_source_remove(CacheState.user_timeout);
514 }
515 if (WlState.repaint_source > 0) {
516 g_source_remove(WlState.repaint_source);
517 WlState.repaint_source = 0;
518 }
519
521}
522
523static void
524wayland_rofi_view_set_window_title(G_GNUC_UNUSED const char *title) {}
525
528 if (state == NULL) {
529 return;
530 }
531 display_buffer_pool_free(state->pool);
532 state->pool = NULL;
533 wayland_rofi_view_update(state, TRUE);
534}
535
536static void wayland_rofi_view_get_menu_rect(int *x, int *y, int *w, int *h) {
537 if (x)
538 *x = WlState.menu_x;
539 if (y)
540 *y = WlState.menu_y;
541 if (w)
542 *w = WlState.menu_width;
543 if (h)
544 *h = WlState.menu_height;
545}
546
548 .update = wayland_rofi_view_update,
549 .temp_configure_notify = NULL,
550 .temp_click_to_exit = NULL,
551 .frame_callback = wayland_rofi_view_frame_callback,
552 .queue_redraw = wayland_rofi_view_queue_redraw,
553
554 .set_window_title = wayland_rofi_view_set_window_title,
555 .calculate_window_position = wayland_rofi_view_calculate_window_position,
556 .calculate_window_height = wayland_rofi_view_calculate_window_height,
557 .calculate_window_width = wayland_rofi_view_calculate_window_width,
558 .window_update_size = wayland_rofi_view_window_update_size,
560 .ping_mouse = wayland_rofi_view_ping_mouse,
561
562 .cleanup = wayland_rofi_view_cleanup,
564 .reload = wayland_rofi_view_reload,
565
566 .__create_window = wayland___create_window,
567 .get_window = NULL,
568 .get_current_monitor = wayland_rofi_view_get_current_monitor,
569
570 .set_size = wayland_rofi_view_set_size,
571 .get_size = wayland_rofi_view_get_size,
572
573 .pool_refresh = wayland_rofi_view_pool_refresh,
574
575 .get_menu_rect = wayland_rofi_view_get_menu_rect,
576};
577
void display_early_cleanup(void)
Definition display.c:52
guint display_scale(void)
Definition display.c:78
void display_set_fullscreen_mode(void)
Definition display.c:92
@ WL_SOUTH_EAST
Definition rofi-types.h:249
@ WL_CENTER
Definition rofi-types.h:235
@ WL_NORTH_WEST
Definition rofi-types.h:245
@ WL_SOUTH
Definition rofi-types.h:241
@ WL_NORTH_EAST
Definition rofi-types.h:247
@ WL_WEST
Definition rofi-types.h:243
@ WL_NORTH
Definition rofi-types.h:237
@ WL_EAST
Definition rofi-types.h:239
@ WL_SOUTH_WEST
Definition rofi-types.h:251
gboolean helper_validate_font(PangoFontDescription *pfd, const char *font)
Definition helper.c:652
unsigned int mode_get_num_entries(const Mode *mode)
Definition mode.c:70
#define TICK()
Definition timings.h:64
#define TICK_N(a)
Definition timings.h:69
void textbox_set_pango_context(const char *font, PangoContext *p)
Definition textbox.c:1034
void textbox_text(textbox *tb, const char *text)
Definition textbox.c:412
RofiViewState * rofi_view_get_active(void)
Definition view.c:321
MenuFlags
Definition view.h:55
void rofi_view_maybe_update(RofiViewState *state)
Definition view.c:1560
@ MENU_NORMAL
Definition view.h:57
void input_history_save(void)
Definition view.c:580
void input_history_initialize(void)
Definition view.c:537
void rofi_view_pool_refresh(void)
Definition view.c:2243
box * box_create(widget *parent, const char *name, RofiOrientation type)
Definition box.c:347
struct _box box
Definition box.h:49
void widget_queue_redraw(widget *wid)
Definition widget.c:487
void widget_free(widget *wid)
Definition widget.c:425
void widget_draw(widget *wid, cairo_t *d)
Definition widget.c:140
struct _widget widget
Definition widget.h:49
void widget_resize(widget *wid, short w, short h)
Definition widget.c:92
#define WIDGET(a)
Definition widget.h:117
int widget_get_desired_height(widget *wid, const int width)
Definition widget.c:644
gboolean widget_need_redraw(widget *wid)
Definition widget.c:500
RofiOrientation
Definition rofi-types.h:139
@ ROFI_ORIENTATION_HORIZONTAL
Definition rofi-types.h:141
@ ROFI_ORIENTATION_VERTICAL
Definition rofi-types.h:140
void process_result(RofiViewState *state)
Definition rofi.c:235
Settings config
#define DEFAULT_MENU_WIDTH
Definition settings.h:235
textbox * tb_total_rows
WindowLocation location
Definition settings.h:110
int click_to_exit
Definition settings.h:178
char * menu_font
Definition settings.h:65
int dpi
Definition settings.h:171
gboolean fullscreen
unsigned long long count
int rofi_theme_get_position(const widget *wid, const char *property, int def)
Definition theme.c:818
int distance_get_pixel(RofiDistance d, RofiOrientation ori)
Definition theme.c:1404
int rofi_theme_get_boolean(const widget *wid, const char *property, int def)
Definition theme.c:903
RofiDistance rofi_theme_get_distance(const widget *wid, const char *property, int def)
Definition theme.c:877
const char * rofi_theme_get_string(const widget *wid, const char *property, const char *def)
Definition theme.c:989
struct _view_proxy view_proxy
struct _rofi_view_cache_state CacheState
Definition view.c:75
double wayland_get_dpi_estimation(void)
Definition display.c:1556
void display_surface_commit(cairo_surface_t *surface)
Definition display.c:343
cairo_surface_t * display_buffer_pool_get_next_buffer(wayland_buffer_pool *pool)
Definition display.c:320
gboolean display_get_surface_dimensions(int *width, int *height)
Definition display.c:2031
void wayland_display_set_cursor_type(RofiCursorType type)
Definition display.c:835
void display_buffer_pool_free(wayland_buffer_pool *self)
Definition display.c:264
void display_set_surface_dimensions(int width, int height, int x_margin, int y_margin, int loc)
Definition display.c:2049
wayland_buffer_pool * display_buffer_pool_new(gint width, gint height)
Definition display.c:194
static int rofi_get_offset_px(RofiViewState *state, RofiOrientation ori)
Definition view.c:145
static void wayland_rofi_view_get_size(RofiViewState *state, gint *width, gint *height)
Definition view.c:267
const view_proxy * wayland_view_proxy
Definition view.c:578
static int rofi_get_location(RofiViewState *state)
Definition view.c:140
static gboolean wayland_rofi_view_reload_idle(G_GNUC_UNUSED gpointer data)
Definition view.c:275
static void wayland_rofi_view_get_menu_rect(int *x, int *y, int *w, int *h)
Definition view.c:536
static void wayland_rofi_view_hide(void)
Definition view.c:495
static void wayland_rofi_view_set_window_title(G_GNUC_UNUSED const char *title)
Definition view.c:524
static void wayland_rofi_view_calculate_window_width(RofiViewState *state)
Definition view.c:390
static struct @205261147150251365057026313374050053065255232215 WlState
static void wayland_rofi_view_calculate_window_position(G_GNUC_UNUSED RofiViewState *state)
Definition view.c:137
static void wayland_rofi_view_reload(void)
Definition view.c:297
static int wayland_rofi_view_calculate_window_height(RofiViewState *state)
Definition view.c:475
static void wayland_rofi_view_set_size(RofiViewState *state, gint width, gint height)
Definition view.c:256
static const int loc_transtable[9]
Definition view.c:132
static void wayland_rofi_view_cleanup(void)
Definition view.c:497
static void wayland_rofi_view_frame_callback(void)
Definition view.c:468
static void wayland_rofi_view_get_current_monitor(int *width, int *height)
Definition view.c:105
static void wayland_rofi_view_ping_mouse(RofiViewState *state)
Definition view.c:273
static void wayland_rofi_view_window_update_size(RofiViewState *state)
Definition view.c:235
static void wayland___create_window(MenuFlags menu_flags)
Definition view.c:317
static view_proxy view_
Definition view.c:547
static void wayland_rofi_view_queue_redraw(void)
Definition view.c:304
static gboolean wayland_rofi_view_repaint(G_GNUC_UNUSED void *data)
Definition view.c:120
static void wayland_rofi_view_update(RofiViewState *state, gboolean qr)
Definition view.c:407
static void wayland_rofi_view_pool_refresh(void)
Definition view.c:526
static void window_update_size_with_outside_click(RofiViewState *state, int offset_x, int offset_y)
Definition view.c:164
static void window_update_size_normal(RofiViewState *state, int offset_x, int offset_y)
Definition view.c:153