2014-02-03 15:46:44 -05:00
|
|
|
/**
|
2014-03-01 11:27:52 -05:00
|
|
|
* rofi
|
2014-02-03 15:46:44 -05:00
|
|
|
*
|
|
|
|
* MIT/X11 License
|
|
|
|
* Copyright 2013-2014 Qball Cow <qball@gmpclient.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2014-05-19 11:54:53 -04:00
|
|
|
#define _GNU_SOURCE
|
2014-02-02 04:54:01 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <X11/X.h>
|
|
|
|
#include <X11/Xresource.h>
|
2014-03-12 03:41:38 -04:00
|
|
|
#include "rofi.h"
|
2014-02-02 04:54:01 -05:00
|
|
|
#include "xrmoptions.h"
|
|
|
|
|
|
|
|
// Big thanks to Sean Pringle for this code.
|
|
|
|
// This maps xresource options to config structure.
|
2014-03-22 16:04:19 -04:00
|
|
|
typedef enum
|
|
|
|
{
|
2014-05-22 03:33:32 -04:00
|
|
|
xrm_String = 0,
|
|
|
|
xrm_Number = 1,
|
|
|
|
xrm_Boolean = 2
|
2014-02-02 04:54:01 -05:00
|
|
|
} XrmOptionType;
|
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int type;
|
|
|
|
char * name;
|
|
|
|
union
|
|
|
|
{
|
2014-02-02 04:54:01 -05:00
|
|
|
unsigned int * num;
|
2014-03-22 16:04:19 -04:00
|
|
|
char ** str;
|
2014-02-02 04:54:01 -05:00
|
|
|
};
|
2014-05-19 15:58:13 -04:00
|
|
|
char *mem;
|
2014-02-02 04:54:01 -05:00
|
|
|
} XrmOption;
|
2014-05-19 11:54:53 -04:00
|
|
|
/**
|
2014-05-19 15:58:13 -04:00
|
|
|
* Map X resource settings to internal options
|
2014-05-19 11:54:53 -04:00
|
|
|
* Currently supports string and number.
|
|
|
|
*/
|
2014-05-17 17:06:45 -04:00
|
|
|
static XrmOption xrmOptions[] = {
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_Number, "opacity", { .num = &config.window_opacity }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_Number, "width", { .num = &config.menu_width }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_Number, "lines", { .num = &config.menu_lines }, NULL },
|
|
|
|
{ xrm_Number, "columns", { .num = &config.menu_columns }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_String, "font", { .str = &config.menu_font }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
/* Foreground color */
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_String, "foreground", { .str = &config.menu_fg }, NULL },
|
|
|
|
{ xrm_String, "fg", { .str = &config.menu_fg }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_String, "background", { .str = &config.menu_bg }, NULL },
|
|
|
|
{ xrm_String, "bg", { .str = &config.menu_bg }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_String, "highlightfg", { .str = &config.menu_hlfg }, NULL },
|
2014-05-22 15:59:27 -04:00
|
|
|
{ xrm_String, "hlfg", { .str = &config.menu_hlfg }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_String, "highlightbg", { .str = &config.menu_hlbg }, NULL },
|
|
|
|
{ xrm_String, "hlbg", { .str = &config.menu_hlbg }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_String, "bordercolor", { .str = &config.menu_bc }, NULL },
|
|
|
|
{ xrm_String, "bc", { .str = &config.menu_bc }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_Number, "borderwidth", { .num = &config.menu_bw }, NULL },
|
|
|
|
{ xrm_Number, "bw", { .num = &config.menu_bw }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_Number, "location", { .num = &config.location }, NULL },
|
|
|
|
{ xrm_Number, "loc", { .num = &config.location }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_Number, "padding", { .num = &config.padding }, NULL },
|
|
|
|
{ xrm_Number, "yoffset", { .num = &config.y_offset }, NULL },
|
|
|
|
{ xrm_Number, "xoffset", { .num = &config.x_offset }, NULL },
|
|
|
|
{ xrm_Boolean, "fixed-num-lines", { .num = &config.fixed_num_lines }, NULL },
|
|
|
|
{ xrm_Boolean, "hmode", { .num = &config.hmode }, NULL },
|
2014-05-22 03:18:16 -04:00
|
|
|
|
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_String, "terminal", { .str = &config.terminal_emulator }, NULL },
|
2014-05-22 04:03:36 -04:00
|
|
|
|
|
|
|
{ xrm_Boolean, "ssh-set-title", { .num = &config.ssh_set_title }, NULL },
|
2014-06-05 15:55:47 -04:00
|
|
|
{ xrm_Boolean, "disable-history", { .num = &config.disable_history }, NULL },
|
2014-07-16 02:42:42 -04:00
|
|
|
{ xrm_Boolean, "levenshtein-sort",{ .num = &config.levenshtein_sort }, NULL },
|
2014-05-20 03:43:45 -04:00
|
|
|
/* Key bindings */
|
2014-05-22 03:33:32 -04:00
|
|
|
{ xrm_String, "key", { .str = &config.window_key }, NULL },
|
|
|
|
{ xrm_String, "rkey", { .str = &config.run_key }, NULL },
|
|
|
|
{ xrm_String, "skey", { .str = &config.ssh_key }, NULL },
|
2014-02-02 04:54:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
void parse_xresource_options ( Display *display )
|
2014-02-02 04:54:01 -05:00
|
|
|
{
|
2014-05-17 17:06:45 -04:00
|
|
|
char *xRMS;
|
2014-05-19 11:54:53 -04:00
|
|
|
// Map Xresource entries to rofi config options.
|
2014-03-22 16:04:19 -04:00
|
|
|
XrmInitialize ();
|
2014-05-17 17:06:45 -04:00
|
|
|
xRMS = XResourceManagerString ( display );
|
2014-02-02 04:54:01 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( xRMS == NULL ) {
|
2014-05-19 11:54:53 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
XrmDatabase xDB = XrmGetStringDatabase ( xRMS );
|
2014-02-02 04:54:01 -05:00
|
|
|
|
2014-05-19 11:54:53 -04:00
|
|
|
char * xrmType;
|
|
|
|
XrmValue xrmValue;
|
|
|
|
const char * namePrefix = "rofi";
|
2014-05-19 15:58:13 -04:00
|
|
|
const char * classPrefix = "rofi";
|
2014-02-02 04:54:01 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) {
|
2014-05-19 15:58:13 -04:00
|
|
|
char *name, *class;
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( asprintf ( &name, "%s.%s", namePrefix, xrmOptions[i].name ) == -1 ) {
|
2014-05-19 15:58:13 -04:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( asprintf ( &class, "%s.%s", classPrefix, xrmOptions[i].name ) > 0 ) {
|
|
|
|
if ( XrmGetResource ( xDB, name, class, &xrmType, &xrmValue ) ) {
|
|
|
|
if ( xrmOptions[i].type == xrm_String ) {
|
|
|
|
if ( xrmOptions[i].mem != NULL ) {
|
2014-05-19 15:58:13 -04:00
|
|
|
free ( xrmOptions[i].mem );
|
|
|
|
xrmOptions[i].mem = NULL;
|
|
|
|
}
|
2014-05-19 10:48:50 -04:00
|
|
|
*xrmOptions[i].str = ( char * ) malloc ( xrmValue.size * sizeof ( char ) );
|
2014-02-02 04:54:01 -05:00
|
|
|
strncpy ( *xrmOptions[i].str, xrmValue.addr, xrmValue.size );
|
2014-05-19 15:58:13 -04:00
|
|
|
|
|
|
|
// Memory
|
|
|
|
xrmOptions[i].mem = ( *xrmOptions[i].str );
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else if ( xrmOptions[i].type == xrm_Number ) {
|
2014-02-02 04:54:01 -05:00
|
|
|
*xrmOptions[i].num = strtol ( xrmValue.addr, NULL, 10 );
|
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else if ( xrmOptions[i].type == xrm_Boolean ) {
|
|
|
|
if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 ) {
|
2014-05-21 18:38:14 -04:00
|
|
|
*xrmOptions[i].num = TRUE;
|
2014-05-22 03:33:32 -04:00
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else{
|
2014-05-21 18:38:14 -04:00
|
|
|
*xrmOptions[i].num = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2014-02-02 04:54:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
free ( class );
|
|
|
|
}
|
2014-05-19 11:54:53 -04:00
|
|
|
free ( name );
|
2014-02-02 04:54:01 -05:00
|
|
|
}
|
2014-05-19 15:58:13 -04:00
|
|
|
XrmDestroyDatabase ( xDB );
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_xresource_free ( void )
|
|
|
|
{
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) {
|
|
|
|
if ( xrmOptions[i].mem != NULL ) {
|
2014-05-19 15:58:13 -04:00
|
|
|
free ( xrmOptions[i].mem );
|
|
|
|
xrmOptions[i].mem = NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-02-02 04:54:01 -05:00
|
|
|
}
|
2014-05-22 15:56:57 -04:00
|
|
|
|
|
|
|
void xresource_dump ( void )
|
|
|
|
{
|
2014-05-24 10:23:01 -04:00
|
|
|
const char * namePrefix = "rofi";
|
|
|
|
unsigned int entries = sizeof ( xrmOptions ) / sizeof ( *xrmOptions );
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int i = 0; i < entries; ++i ) {
|
2014-05-22 15:56:57 -04:00
|
|
|
// Skip duplicates.
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( ( i + 1 ) < entries ) {
|
|
|
|
if ( xrmOptions[i].str == xrmOptions[i + 1].str ) {
|
2014-05-22 15:56:57 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ( "%s.%s: ", namePrefix, xrmOptions[i].name );
|
|
|
|
switch ( xrmOptions[i].type )
|
|
|
|
{
|
|
|
|
case xrm_Number:
|
|
|
|
printf ( "%i", *xrmOptions[i].num );
|
|
|
|
break;
|
|
|
|
case xrm_String:
|
|
|
|
printf ( "%s", *xrmOptions[i].str );
|
|
|
|
break;
|
|
|
|
case xrm_Boolean:
|
|
|
|
printf ( "%s", ( ( *xrmOptions[i].num ) == TRUE ) ? "true" : "false" );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf ( "\n" );
|
|
|
|
}
|
|
|
|
}
|