2017-04-15 10:32:05 +00:00
|
|
|
/*
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2021-06-09 12:50:39 +00:00
|
|
|
* Copyright © 2013-2021 Qball Cow <qball@gmpclient.org>
|
2017-04-15 10:32:05 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-09 15:22:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
2016-01-07 18:47:37 +00:00
|
|
|
#include "rofi.h"
|
2016-01-07 20:27:20 +00:00
|
|
|
#include "xrmoptions.h"
|
2016-01-07 18:47:37 +00:00
|
|
|
#include "mode.h"
|
|
|
|
|
|
|
|
// This one should only be in mode implementations.
|
|
|
|
#include "mode-private.h"
|
|
|
|
/**
|
|
|
|
* @ingroup MODE
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2016-01-08 08:16:59 +00:00
|
|
|
int mode_init ( Mode *mode )
|
2016-01-07 18:47:37 +00:00
|
|
|
{
|
2016-01-08 08:16:59 +00:00
|
|
|
g_return_val_if_fail ( mode != NULL, FALSE );
|
|
|
|
g_return_val_if_fail ( mode->_init != NULL, FALSE );
|
2017-03-28 20:27:16 +00:00
|
|
|
return mode->_init ( mode );
|
2016-01-07 18:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mode_destroy ( Mode *mode )
|
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
|
|
|
g_assert ( mode->_destroy != NULL );
|
|
|
|
mode->_destroy ( mode );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int mode_get_num_entries ( const Mode *mode )
|
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
|
|
|
g_assert ( mode->_get_num_entries != NULL );
|
|
|
|
return mode->_get_num_entries ( mode );
|
|
|
|
}
|
|
|
|
|
2017-04-23 13:17:15 +00:00
|
|
|
char * mode_get_display_value ( const Mode *mode, unsigned int selected_line, int *state, GList **attribute_list, int get_entry )
|
2016-01-07 18:47:37 +00:00
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
2016-01-07 20:37:03 +00:00
|
|
|
g_assert ( state != NULL );
|
2016-01-07 20:27:20 +00:00
|
|
|
g_assert ( mode->_get_display_value != NULL );
|
2016-01-07 18:47:37 +00:00
|
|
|
|
2017-04-23 13:17:15 +00:00
|
|
|
return mode->_get_display_value ( mode, selected_line, state, attribute_list, get_entry );
|
2016-01-07 20:27:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 15:58:32 +00:00
|
|
|
cairo_surface_t * mode_get_icon ( const Mode *mode, unsigned int selected_line, int height )
|
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
|
|
|
|
|
|
|
if ( mode->_get_icon != NULL ) {
|
|
|
|
return mode->_get_icon ( mode, selected_line, height );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:27:20 +00:00
|
|
|
char * mode_get_completion ( const Mode *mode, unsigned int selected_line )
|
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
|
|
|
if ( mode->_get_completion != NULL ) {
|
|
|
|
return mode->_get_completion ( mode, selected_line );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int state;
|
|
|
|
g_assert ( mode->_get_display_value != NULL );
|
2017-03-17 13:07:11 +00:00
|
|
|
return mode->_get_display_value ( mode, selected_line, &state, NULL, TRUE );
|
2016-01-07 20:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ModeMode mode_result ( Mode *mode, int menu_retv, char **input, unsigned int selected_line )
|
|
|
|
{
|
2020-09-08 15:30:03 +00:00
|
|
|
if ( menu_retv & MENU_NEXT ) {
|
|
|
|
return NEXT_DIALOG;
|
|
|
|
}
|
|
|
|
else if ( menu_retv & MENU_PREVIOUS ) {
|
|
|
|
return PREVIOUS_DIALOG;
|
|
|
|
}
|
|
|
|
else if ( menu_retv & MENU_QUICK_SWITCH ) {
|
2020-11-03 22:57:02 +00:00
|
|
|
return menu_retv & MENU_LOWER_MASK;
|
2020-09-08 15:30:03 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 20:27:20 +00:00
|
|
|
g_assert ( mode != NULL );
|
|
|
|
g_assert ( mode->_result != NULL );
|
tests: initialize char* in mode test (#875)
* tests: initialize char* in mode test
Currently, test_mode_result relies on undefined behavior.
The test calls mode_result, which checks whether the pointer is NULL.
However, the pointer was never initialized, so it may or may not be
NULL, depending on the compiler.
This caused a test failure on ppc64 and Fedora 28, apparently because in
this setting, gcc sets uninitialized pointers to NULL.
By initializing the pointer to the empty string, the behavior is defined
and the test passes on all architectures.
* mode: fix input pointer check in mode_result
Do not check whether *input (i.e., the char* the input points to) is
NULL, as this is valid. Instead, check whether the input itself is NULL.
* tests: make char* input arg in test_mode_result modifiable
The function mode_result expects a modifiable char*, initialize the
argument properly so it can be modified.
2018-12-14 16:05:07 +00:00
|
|
|
g_assert ( input != NULL );
|
2020-09-08 15:30:03 +00:00
|
|
|
|
2016-01-07 20:27:20 +00:00
|
|
|
return mode->_result ( mode, menu_retv, input, selected_line );
|
|
|
|
}
|
|
|
|
|
2017-09-29 06:32:09 +00:00
|
|
|
int mode_token_match ( const Mode *mode, rofi_int_matcher **tokens, unsigned int selected_line )
|
2016-01-07 20:27:20 +00:00
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
|
|
|
g_assert ( mode->_token_match != NULL );
|
2016-05-22 15:47:34 +00:00
|
|
|
return mode->_token_match ( mode, tokens, selected_line );
|
2016-01-07 20:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *mode_get_name ( const Mode *mode )
|
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
|
|
|
return mode->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mode_free ( Mode **mode )
|
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
2016-01-08 08:16:59 +00:00
|
|
|
g_assert ( ( *mode ) != NULL );
|
2016-01-07 20:27:20 +00:00
|
|
|
if ( ( *mode )->free != NULL ) {
|
|
|
|
( *mode )->free ( *mode );
|
|
|
|
}
|
|
|
|
( *mode ) = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *mode_get_private_data ( const Mode *mode )
|
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
|
|
|
return mode->private_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mode_set_private_data ( Mode *mode, void *pd )
|
|
|
|
{
|
|
|
|
g_assert ( mode != NULL );
|
|
|
|
if ( pd != NULL ) {
|
|
|
|
g_assert ( mode->private_data == NULL );
|
|
|
|
}
|
|
|
|
mode->private_data = pd;
|
2016-01-07 18:47:37 +00:00
|
|
|
}
|
2016-01-12 21:17:53 +00:00
|
|
|
|
|
|
|
const char *mode_get_display_name ( const Mode *mode )
|
|
|
|
{
|
|
|
|
if ( mode->display_name != NULL ) {
|
|
|
|
return mode->display_name;
|
|
|
|
}
|
|
|
|
return mode->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mode_set_config ( Mode *mode )
|
|
|
|
{
|
|
|
|
snprintf ( mode->cfg_name_key, 128, "display-%s", mode->name );
|
|
|
|
config_parser_add_option ( xrm_String, mode->cfg_name_key, (void * *) &( mode->display_name ), "The display name of this browser" );
|
|
|
|
}
|
2016-05-26 06:39:33 +00:00
|
|
|
|
|
|
|
char * mode_preprocess_input ( Mode *mode, const char *input )
|
|
|
|
{
|
|
|
|
if ( mode->_preprocess_input ) {
|
|
|
|
return mode->_preprocess_input ( mode, input );
|
|
|
|
}
|
|
|
|
return g_strdup ( input );
|
|
|
|
}
|
2017-03-01 08:57:54 +00:00
|
|
|
char *mode_get_message ( const Mode *mode )
|
|
|
|
{
|
|
|
|
if ( mode->_get_message ) {
|
|
|
|
return mode->_get_message ( mode );
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-12 19:39:36 +00:00
|
|
|
/**@}*/
|