Add switch_profile.sh mode

This commit is contained in:
QC 2014-01-27 22:15:05 +01:00
parent f1b1bd0b31
commit 8508c73b12
7 changed files with 226 additions and 3 deletions

View File

@ -38,6 +38,10 @@ CFLAGS+=-DTIMING=1 -g3
LDADD+=-lrt
endif
ifeq (${QC_MODE},1)
CFLAGS+=-D__QC_MODE__
endif
##
# Check dependencies

View File

@ -51,7 +51,7 @@ Settings config = {
.menu_hlbg = "#005577",
// Border color.
.menu_bc = "black",
// Directly select when only 1 choice is left
// Directly select when only 1 choice is left
.zeltak_mode = 0,
// Terminal to use. (for ssh and open in terminal)
.terminal_emulator = "x-terminal-emulator",
@ -64,7 +64,7 @@ Settings config = {
#ifdef I3
.mark_key = "mod1+F5",
#endif
// Location of the window. CENTER, NORTH_WEST, NORTH,NORTH_EAST, etc.
// Location of the window. CENTER, NORTH_WEST, NORTH,NORTH_EAST, etc.
.location = CENTER,
// Mode of window, list (Vertical) or dmenu like (Horizontal)
.wmode = VERTICAL,

10
include/profile-dialog.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __PROFILE_DIALOG_H__
#define __PROFILE_DIALOG_H__
#ifdef __QC_MODE__
SwitcherMode profile_switcher_dialog ( char **input );
#endif //__QC_MODE__
#endif

View File

@ -20,6 +20,9 @@ typedef enum {
SSH_DIALOG,
#ifdef I3
MARK_DIALOG,
#endif
#ifdef __QC_MODE__
PROFILE_DIALOG,
#endif
NUM_DIALOGS,
MODE_EXIT,

View File

@ -189,7 +189,7 @@ static char ** get_mark ( )
ssize_t index = 0;
// Grab results.
while ( index < (ssize_t)head.size ) {
while ( index < ( ssize_t )head.size ) {
t= recv( s, &result[index], ( head.size-t ), 0 );
if ( t < 0 ) break;

199
source/profile-dialog.c Normal file
View File

@ -0,0 +1,199 @@
/**
* simpleswitcher
*
* 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.
*
*/
#ifdef __QC_MODE__
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <X11/X.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <dirent.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "simpleswitcher.h"
#include "profile-dialog.h"
#ifdef TIMING
#include <time.h>
#endif
static inline int execshprofile( const char *profile )
{
return execlp( config.terminal_emulator, config.terminal_emulator, "-e","switch_profile.sh", profile, NULL );
}
// execute sub-process
static pid_t exec_profile( const char *cmd )
{
if ( !cmd || !cmd[0] ) return -1;
signal( SIGCHLD, catch_exit );
pid_t pid = fork();
if ( !pid ) {
setsid();
execshprofile( cmd );
exit( EXIT_FAILURE );
}
return pid;
}
static char ** add_elements( char **retv, char *element, unsigned int *retv_index )
{
retv = realloc( retv, ( ( *retv_index )+2 )*sizeof( char* ) );
retv[( *retv_index )] = element;
retv[( *retv_index )+1] = NULL;
( *retv_index )++;
return retv;
}
static char ** get_profile ( )
{
unsigned int retv_index = 0;
char *path;
char **retv = NULL;
#ifdef TIMING
struct timespec start, stop;
clock_gettime( CLOCK_REALTIME, &start );
#endif
if ( getenv( "HOME" ) == NULL ) return NULL;
const char *hd = getenv( "HOME" );
path = allocate( strlen( hd ) + strlen( ".switch_profile.conf" )+3 );
sprintf( path, "%s/%s", hd, ".switch_profile.conf" );
FILE *fd = fopen ( path, "r" );
if ( fd != NULL ) {
char buffer[1024];
char *element = NULL;
int enabled = 0;
while ( fgets( buffer,1024,fd ) != NULL ) {
buffer[strlen( buffer )-1] = '\0';
char *entry = index( buffer, '=' );
// skip
if ( entry == NULL ) {
if ( element != NULL && enabled ) {
retv = add_elements( retv, element, &retv_index );
}
element = NULL;
continue;
}
*entry = '\0';
if ( element == NULL ) {
element = strdup( buffer );
enabled = strtol( entry+1, NULL, 10 );
} else {
if ( strncmp( buffer, element, strlen( element ) ) == 0 )
continue;
// New entry? Store old one
if ( enabled ) {
retv = add_elements( retv, element, &retv_index );
}
element = strdup( buffer );
enabled = strtol( entry+1, NULL, 10 );
}
}
if ( element != NULL && enabled ) {
retv = add_elements( retv, element, &retv_index );
}
fclose( fd );
}
free( path );
#ifdef TIMING
clock_gettime( CLOCK_REALTIME, &stop );
if ( stop.tv_sec != start.tv_sec ) {
stop.tv_nsec += ( stop.tv_sec-start.tv_sec )*1e9;
}
long diff = stop.tv_nsec-start.tv_nsec;
printf( "Time elapsed: %ld us\n", diff/1000 );
#endif
return retv;
}
static int token_match ( char **tokens, const char *input,
__attribute__( ( unused ) )int index,
__attribute__( ( unused ) )void *data )
{
int match = 1;
// Do a tokenized match.
if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) {
match = ( strcasestr( input, tokens[j] ) != NULL );
}
return match;
}
SwitcherMode profile_switcher_dialog ( char **input )
{
SwitcherMode retv = MODE_EXIT;
// act as a launcher
char **cmd_list = get_profile( );
if ( cmd_list == NULL ) {
cmd_list = allocate( 2*sizeof( char * ) );
cmd_list[0] = strdup( "No profiles found" );
cmd_list[1] = NULL;
}
int shift=0;
int n = menu( cmd_list, input, "profile ", NULL, &shift,token_match, NULL );
if ( n == -2 ) {
retv = NEXT_DIALOG;
} else if ( n >=0 && cmd_list[n] != NULL ) {
exec_profile( cmd_list[n] );
} else if ( n == -3 && *input != NULL && *input[0] != '\0' ) {
exec_profile( *input );
}
for ( int i=0; cmd_list[i] != NULL; i++ ) {
free( cmd_list[i] );
}
free( cmd_list );
return retv;
}
#endif //__QC_MODE__

View File

@ -65,6 +65,7 @@
#include "run-dialog.h"
#include "ssh-dialog.h"
#include "mark-dialog.h"
#include "profile-dialog.h"
#define LINE_MARGIN 4
@ -1192,6 +1193,12 @@ void run_switcher( int fmode, SwitcherMode mode )
}
#endif
#ifdef __QC_MODE__
else if ( mode == PROFILE_DIALOG ) {
retv = profile_switcher_dialog ( &input );
}
#endif // __QC_MODE__
if ( retv == NEXT_DIALOG ) {
mode = ( mode+1 )%NUM_DIALOGS;