Skip to content

Commit d3e7077

Browse files
committed
split code into files
1 parent 06c4e61 commit d3e7077

22 files changed

+704
-541
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CFLAGS+= -Wall
1+
CFLAGS+= -Wall -Os
22
LDADD+= -lxcb -lxcb-keysyms
33
LDFLAGS=
44
EXEC=yawn
@@ -13,8 +13,8 @@ CC=clang
1313

1414
all: $(EXEC)
1515

16-
yawn: yawn.o
17-
$(CC) $(LDFLAGS) -Os -o $@ $+ $(LDADD)
16+
yawn: action.o client.o configuration.o desktop.o event.o keyboard.o stack.o string.o window.o yawn.o
17+
$(CC) $(LDFLAGS) -o $@ $+ $(LDADD)
1818

1919
install: all
2020
install -Dm 755 yawn $(DESTDIR)$(BINDIR)/yawn

action.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <string.h>
2+
#include "action.h"
3+
#include "yawn.h"
4+
#include "keyboard.h"
5+
#include "client.h"
6+
#include "desktop.h"
7+
8+
int (*get_action(char * name ))(int , char **) {
9+
if ( !strcmp( name, "bind" ) ) {
10+
return &keyboard_bind;
11+
}
12+
else if ( !strcmp( name, "change_desktop" ) ) {
13+
return &desktop_change;
14+
}
15+
else if ( !strcmp( name, "quit" ) ) {
16+
return &quit;
17+
}
18+
else if ( !strcmp( name, "kill_client" ) ) {
19+
return &client_kill;
20+
}
21+
else {
22+
return &spawn;
23+
}
24+
}

action.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef ACTION_H
2+
#define ACTION_H
3+
4+
typedef struct action_t {
5+
int (*action)(int argc, char ** argv);
6+
char * name;
7+
} action_t;
8+
9+
/*
10+
action_t actions[] = {
11+
{ spawn, "spawn" }
12+
};
13+
*/
14+
15+
int (*get_action(char * name ))(int , char **);
16+
17+
#endif

client.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "client.h"
2+
#include "globals.h"
3+
#include "keyboard.h"
4+
#include "window.h"
5+
6+
void client_manage( xcb_window_t w ) {
7+
const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK };
8+
xcb_map_window( xconn, w );
9+
xcb_change_window_attributes( xconn, w, XCB_CW_EVENT_MASK, select_input_val );
10+
xcb_configure_window(xconn, w, XCB_CONFIG_WINDOW_STACK_MODE, (uint32_t[]) { XCB_STACK_MODE_ABOVE } );
11+
window_grabkeys( w, keybindings );
12+
}
13+
14+
struct client_t * client_get_by_window( xcb_window_t w ) {
15+
struct client_t * c;
16+
for ( c = head; c; c = c->next ) {
17+
if ( c->win == w ) {
18+
return c;
19+
}
20+
}
21+
return NULL;
22+
}
23+
24+
int client_kill( int argc, char ** argv ) {
25+
xcb_destroy_window( xconn, current->win );
26+
27+
/* Remove this window from the save set since this shouldn't be made visible
28+
* after a restart anymore. */
29+
xcb_change_save_set( xconn, XCB_SET_MODE_DELETE, current->win );
30+
31+
/* Do this last to avoid races with clients. According to ICCCM, clients
32+
* arent allowed to re-use the window until after this. */
33+
// xwindow_set_state(c->window, XCB_ICCCM_WM_STATE_WITHDRAWN);
34+
35+
current->win = XCB_NONE;
36+
37+
return 0;
38+
}

client.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef CLIENT_H
2+
#define CLIENT_H
3+
4+
#include <xcb/xcb.h>
5+
6+
#define CLIENT_SELECT_INPUT_EVENT_MASK ( XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_KEY_PRESS )
7+
8+
struct client_t {
9+
struct client_t *next;
10+
struct client_t *prev;
11+
xcb_window_t win;
12+
};
13+
14+
void client_manage( xcb_window_t w );
15+
struct client_t *client_get_by_window( xcb_window_t w );
16+
int client_kill( int argc, char ** argv );
17+
18+
#endif

configuration.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include "configuration.h"
5+
#include "string.h"
6+
#include "yawn.h"
7+
#include "action.h"
8+
9+
char * xdg_config_dir( int directory_type ) {
10+
char * config_dir;
11+
if ( directory_type == CONFIG_DIR_HOME ) {
12+
config_dir = getenv( "XDG_CONFIG_HOME" );
13+
if ( config_dir == NULL ) {
14+
config_dir = getenv( "HOME" );
15+
if ( config_dir == NULL ) {
16+
die( "Please define HOME environment variable.\n" );
17+
}
18+
}
19+
}
20+
else {
21+
config_dir = getenv( "XDG_CONFIG_DIRS" );
22+
if ( config_dir == NULL ) {
23+
config_dir = (char*)malloc( 4 * sizeof( char ) );
24+
strcpy( config_dir, "/etc" );
25+
}
26+
}
27+
return config_dir;
28+
}
29+
30+
char * xdg_config_path( char * filename, int directory_type ) {
31+
char * config_path, * basedir = xdg_config_dir( directory_type );
32+
struct stat st;
33+
34+
config_path = (char*)malloc( ( strlen( basedir ) + strlen( "/yawn/" ) + strlen( filename ) ) * sizeof( char ) );
35+
sprintf( config_path, "%s/yawn/%s", basedir, filename );
36+
37+
if ( stat( config_path, &st ) != 0 ) {
38+
if ( directory_type == CONFIG_DIR_HOME ) {
39+
// local configuration file not found. Try reading system configuration file instead.
40+
return xdg_config_path( filename, CONFIG_DIR_SYSTEM );
41+
}
42+
else {
43+
printf( "System configuration file %s not found. Please reinstall yawn.\n", config_path );
44+
exit( 1 );
45+
}
46+
}
47+
48+
return config_path;
49+
}
50+
51+
void configuration_read( char * filename ) {
52+
int argc, (*callback)( int, char ** );
53+
char line[ 256 ], section[ 256 ], * argv[ 128 ];
54+
FILE * fp = fopen( xdg_config_path( "yawn.conf", CONFIG_DIR_HOME ), "r" );
55+
56+
while ( freadline( fp, line ) != EOF ) {
57+
int length = strlen( line );
58+
if ( !length || line[ 0 ] == '#' ) {
59+
continue;
60+
}
61+
if ( line[ 0 ] == '[' && line[ length - 1 ] == ']' ) {
62+
line[ length - 1 ] = '\0';
63+
strcpy( section, line + 1 );
64+
continue;
65+
}
66+
argc = string_split( line, " ", argv );
67+
callback = get_action( section );
68+
callback( argc, argv );
69+
}
70+
}

configuration.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef CONFIGURATION_H
2+
#define CONFIGURATION_H
3+
4+
#define CONFIG_DIR_HOME 0
5+
#define CONFIG_DIR_SYSTEM 1
6+
7+
char * xdg_config_dir( int directory_type );
8+
char * xdg_config_path( char * filename, int directory_type );
9+
void configuration_read( char * filename );
10+
11+
#endif
12+

desktop.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#include "globals.h"
3+
#include "desktop.h"
4+
#include "client.h"
5+
#include "stack.h"
6+
#include "window.h"
7+
8+
int desktop_change( int argc, char ** argv ) {
9+
int i;
10+
struct client_t *c;
11+
12+
sscanf( argv[ 0 ], "%s", &i );
13+
14+
if ( i == current_desktop ) {
15+
return 0;
16+
}
17+
18+
if ( head != NULL ) {
19+
for( c = head; c; c= c->next ) {
20+
xcb_unmap_window( xconn, c->win );
21+
}
22+
}
23+
24+
desktop_save( current_desktop );
25+
desktop_select( i );
26+
27+
if ( head != NULL ) {
28+
for( c = head; c; c = c->next ) {
29+
xcb_map_window( xconn, c->win );
30+
}
31+
}
32+
33+
stack_tile();
34+
window_current_update();
35+
36+
return 0;
37+
}
38+
39+
void desktop_save( int i ) {
40+
desktops[ i ].head = head;
41+
desktops[ i ].current = current;
42+
}
43+
44+
void desktop_select( int i ) {
45+
head = desktops[ i ].head;
46+
current = desktops[ i ].current;
47+
current_desktop = i;
48+
}

desktop.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef DESKTOP_H
2+
#define DESKTOP_H
3+
4+
struct desktop_t {
5+
struct client_t *head;
6+
struct client_t *current;
7+
};
8+
9+
int desktop_change( int argc, char ** argv );
10+
void desktop_save( int i );
11+
void desktop_select( int i );
12+
13+
#endif

event.c

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <stdio.h>
2+
#include "event.h"
3+
#include "globals.h"
4+
5+
void (*events[256])( xcb_generic_event_t * ) = {
6+
[XCB_KEY_PRESS] = event_keypress,
7+
[XCB_CONFIGURE_REQUEST] = event_configurerequest,
8+
[XCB_DESTROY_NOTIFY] = event_destroynotify,
9+
[XCB_CONFIGURE_NOTIFY] = event_configurenotify,
10+
[XCB_MAP_REQUEST] = event_maprequest,
11+
[XCB_ENTER_NOTIFY] = event_enternotify
12+
};
13+
14+
void event_keypress( xcb_generic_event_t* e ) {
15+
xcb_key_press_event_t * ev = (xcb_key_press_event_t*)e;
16+
timestamp = ev->time;
17+
xcb_keysym_t keysym = xcb_key_symbols_get_keysym( symbols, ev->detail, 0 );
18+
printf( "yawn: keypress %i %i %i\n", ev->detail, keysym, ev->state );
19+
int i = 0;
20+
printf( "keybinding count: %i\n", bindingsCount );
21+
for ( i = 0; i < bindingsCount; ++i ) {
22+
struct keybinding_t k = keybindings[ i ];
23+
printf( "key sym: %i state: %i\n", k.key, k.state );
24+
if ( k.key == keysym && ev->state & k.state ) {
25+
printf( "found key binding! calling callback! %i %s\n", k.argc, k.argv[ 0 ] );
26+
k.callback( k.argc, k.argv );
27+
}
28+
}
29+
}
30+
31+
void event_maprequest( xcb_generic_event_t* e ) {
32+
printf( "yawn: maprequest\n" );
33+
34+
xcb_map_request_event_t* ev = (xcb_map_request_event_t*)e;
35+
stack_add( ev->window );
36+
client_manage( ev->window );
37+
stack_tile();
38+
window_current_update();
39+
}
40+
41+
void event_destroynotify( xcb_generic_event_t* e ) {
42+
printf( "yawn: destroynotify\n" );
43+
xcb_destroy_notify_event_t* ev = (xcb_destroy_notify_event_t*)e;
44+
stack_remove( ev->window );
45+
stack_tile();
46+
window_current_update();
47+
}
48+
49+
void event_configurenotify( xcb_generic_event_t* e ) {
50+
printf( "yawn: configurenotify\n" );
51+
}
52+
53+
void event_configurerequest( xcb_generic_event_t * e ) {
54+
xcb_configure_request_event_t * ev = (xcb_configure_request_event_t*)e;
55+
56+
printf( "yawn: configurerequest\n" );
57+
58+
uint16_t config_win_mask = 0;
59+
uint32_t config_win_vals[ 7 ];
60+
61+
uint16_t masks[ 7 ] = { XCB_CONFIG_WINDOW_X, XCB_CONFIG_WINDOW_Y, XCB_CONFIG_WINDOW_WIDTH, XCB_CONFIG_WINDOW_HEIGHT, XCB_CONFIG_WINDOW_BORDER_WIDTH, XCB_CONFIG_WINDOW_SIBLING, XCB_CONFIG_WINDOW_STACK_MODE };
62+
63+
uint32_t values[ 7 ] = { ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->sibling, ev->stack_mode };
64+
65+
unsigned int i;
66+
unsigned int count = 0;
67+
for ( i = 0; i < 7; ++i ) {
68+
uint16_t mask = masks[ i ];
69+
if ( ev->value_mask & mask ) {
70+
config_win_mask |= mask;
71+
config_win_vals[ count++ ] = values[ i ];
72+
}
73+
}
74+
75+
xcb_configure_window( xconn, ev->window, config_win_mask, config_win_vals );
76+
xcb_flush( xconn );
77+
}
78+
79+
void event_enternotify( xcb_generic_event_t * e ) {
80+
printf( "yawn: enternotify\n" );
81+
xcb_enter_notify_event_t * ev = (xcb_enter_notify_event_t*)e;
82+
timestamp = ev->time;
83+
}

event.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef EVENT_H
2+
#define EVENT_H
3+
4+
#include <xcb/xcb.h>
5+
#include "client.h"
6+
#include "keyboard.h"
7+
#include "stack.h"
8+
#include "window.h"
9+
10+
void event_keypress( xcb_generic_event_t *e );
11+
void event_maprequest( xcb_generic_event_t *e );
12+
void event_destroynotify( xcb_generic_event_t *e );
13+
void event_configurenotify( xcb_generic_event_t *e );
14+
void event_configurerequest( xcb_generic_event_t *e );
15+
void event_enternotify( xcb_generic_event_t *e );
16+
17+
#endif

globals.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef GLOBALS_H
2+
#define GLOBALS_H
3+
4+
#include <xcb/xcb.h>
5+
#include <xcb/xcb_keysyms.h>
6+
#include "client.h"
7+
#include "desktop.h"
8+
9+
extern int current_desktop;
10+
extern struct desktop_t desktops[10];
11+
extern xcb_connection_t * xconn;
12+
extern xcb_screen_t * screen;
13+
extern xcb_timestamp_t timestamp;
14+
extern struct client_t *head;
15+
extern struct client_t *current;
16+
extern xcb_key_symbols_t *symbols;
17+
extern uint32_t sh;
18+
extern uint32_t sw;
19+
extern uint8_t default_depth;
20+
extern int bindingsCount;
21+
extern int default_screen;
22+
extern xcb_query_tree_cookie_t tree_c;
23+
extern void (*events[256])( xcb_generic_event_t * );
24+
extern int modifierAsciiToMask[ 256 ];
25+
26+
27+
#endif
28+

0 commit comments

Comments
 (0)