4 * Construct and manage the controller configuration pane
6 * Copyright (c) 2008 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
22 #include "maple/maple.h"
24 #include <glib/gstrfuncs.h>
28 static void cocoa_config_keysym_hook(void *data, const gchar *keysym);
30 @interface KeyBindingEditor (Private)
31 - (void)updateKeysym: (const gchar *)sym;
34 @implementation KeyBindingEditor
40 [self setFieldEditor: YES];
41 [self setEditable: FALSE];
46 if( lastValue != nil ) {
52 - (void)setPrimed: (BOOL)primed
54 if( primed != isPrimed ) {
57 lastValue = [[NSString stringWithString: [self string]] retain];
58 [self setString: @"<press key>"];
59 input_set_keysym_hook(cocoa_config_keysym_hook, self);
63 input_set_keysym_hook(NULL,NULL);
67 - (void)resignFirstResponder
70 [self setString: lastValue];
73 [super resignFirstResponder];
75 - (void)fireBindingChanged
77 id delegate = [self delegate];
78 if( delegate != nil && [delegate respondsToSelector:@selector(textDidChange:)] ) {
79 [delegate textDidChange: [NSNotification notificationWithName: NSTextDidChangeNotification object: self]];
83 - (void)updateKeysym: (const gchar *)sym
86 [self setString: [NSString stringWithCString: sym]];
88 [self fireBindingChanged];
91 - (void)keyPressed: (int)keycode
93 gchar *keysym = input_keycode_to_keysym(NULL, keycode);
94 if( keysym != NULL ) {
95 [self updateKeysym: keysym];
99 - (void)insertText:(id)string
103 - (void)mouseDown: (NSEvent *)event
105 [self setPrimed: YES];
106 [super mouseDown: event];
108 - (void)keyDown: (NSEvent *) event
110 NSString *chars = [event characters];
112 if( chars != NULL && [chars length] == 1 && [chars characterAtIndex: 0] == 27 ) {
113 // Escape char = abort change
114 [self setString: lastValue];
115 [self setPrimed: NO];
117 [self keyPressed: ([event keyCode]+1)];
120 if( chars != NULL && [chars length] == 1 ) {
121 int ch = [chars characterAtIndex: 0];
124 [self setString: @""];
125 [self fireBindingChanged];
128 [self setPrimed: YES];
131 [super keyDown: event];
135 [super keyDown: event];
139 - (void)flagsChanged: (NSEvent *) event
142 [self keyPressed: ([event keyCode]+1)];
144 [super flagsChanged: event];
148 static void cocoa_config_keysym_hook(void *data, const gchar *keysym)
150 KeyBindingEditor *editor = (KeyBindingEditor *)data;
151 [editor updateKeysym: keysym];
155 @implementation KeyBindingField
158 /*************************** Key-binding sub-view ***********************/
160 #define MAX_KEY_BINDINGS 32
162 @interface ControllerKeyBindingView : NSView
164 maple_device_t device;
165 KeyBindingField *field[MAX_KEY_BINDINGS][2];
167 - (id)initWithFrame: (NSRect)frameRect;
168 - (void)setDevice: (maple_device_t)device;
171 @implementation ControllerKeyBindingView
172 - (id)initWithFrame: (NSRect)frameRect
174 if( [super initWithFrame: frameRect] == nil ) {
185 - (void)removeSubviews
187 [[self subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
189 - (void)controlTextDidChange: (NSNotification *)notify
191 int binding = [[notify object] tag];
192 NSString *val1 = [field[binding][0] stringValue];
193 NSString *val2 = [field[binding][1] stringValue];
194 char buf[ [val1 length] + [val2 length] + 2 ];
195 const gchar *p = NULL;
197 if( [val1 length] == 0 ) {
198 if( [val2 length] != 0 ) {
199 p = [val2 UTF8String];
201 } else if( [val2 length] == 0 ) {
202 p = [val1 UTF8String];
204 sprintf( buf, "%s,%s", [val1 UTF8String], [val2 UTF8String] );
207 maple_set_device_config_value( device, binding, p );
208 lxdream_save_config();
210 - (void)setDevice: (maple_device_t)newDevice
213 [self removeSubviews];
214 if( device != NULL ) {
215 lxdream_config_entry_t config = maple_get_device_config(device);
216 if( config != NULL ) {
219 for( count=0; config[count].key != NULL; count++ );
221 NSSize size = NSMakeSize(85*3+TEXT_GAP*4, count*(TEXT_HEIGHT+TEXT_GAP)+TEXT_GAP);
222 [self setFrameSize: size];
223 [self scrollRectToVisible: NSMakeRect(0,0,1,1)];
225 for( i=0; config[i].key != NULL; i++ ) {
226 NSRect frame = NSMakeRect(x, y + 2, 85, LABEL_HEIGHT);
227 NSTextField *label = cocoa_gui_add_label(self, NS_(config[i].label), frame);
228 [label setAlignment: NSRightTextAlignment];
230 frame = NSMakeRect( x + 85 + TEXT_GAP, y, 85, TEXT_HEIGHT);
231 field[i][0] = [[KeyBindingField alloc] initWithFrame: frame];
232 [field[i][0] setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
233 [field[i][0] setTag: i];
234 [field[i][0] setDelegate: self];
235 [self addSubview: field[i][0]];
237 frame = NSMakeRect( x + (85*2) + (TEXT_GAP*2), y, 85, TEXT_HEIGHT);
238 field[i][1] = [[KeyBindingField alloc] initWithFrame: frame];
239 [field[i][1] setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
240 [field[i][1] setTag: i];
241 [field[i][1] setDelegate: self];
242 [self addSubview: field[i][1]];
244 if( config[i].value != NULL ) {
245 gchar **parts = g_strsplit(config[i].value,",",3);
246 if( parts[0] != NULL ) {
247 [field[i][0] setStringValue: [NSString stringWithCString: parts[0]]];
248 if( parts[1] != NULL ) {
249 [field[i][1] setStringValue: [NSString stringWithCString: parts[1]]];
255 y += (TEXT_HEIGHT + TEXT_GAP);
258 [self setFrameSize: NSMakeSize(100,TEXT_HEIGHT+TEXT_GAP) ];
261 [self setFrameSize: NSMakeSize(100,TEXT_HEIGHT+TEXT_GAP) ];
266 /*************************** Top-level controller pane ***********************/
268 @interface LxdreamPrefsControllerPane: LxdreamPrefsPane
270 struct maple_device *save_controller[4];
272 ControllerKeyBindingView *key_bindings;
274 + (LxdreamPrefsControllerPane *)new;
277 @implementation LxdreamPrefsControllerPane
278 + (LxdreamPrefsControllerPane *)new
280 return [[LxdreamPrefsControllerPane alloc] initWithFrame: NSMakeRect(0,0,600,400)];
282 - (id)initWithFrame: (NSRect)frameRect
284 if( [super initWithFrame: frameRect title: NS_("Controllers")] == nil ) {
287 const struct maple_device_class **devices = maple_get_device_classes();
290 int y = [self contentHeight] - TEXT_HEIGHT - TEXT_GAP;
292 NSBox *rule = [[NSBox alloc] initWithFrame:
293 NSMakeRect(210+(TEXT_GAP*3), 1, 1, [self contentHeight] + TEXT_GAP - 2)];
294 [rule setAutoresizingMask: (NSViewMaxXMargin|NSViewHeightSizable)];
295 [rule setBoxType: NSBoxSeparator];
296 [self addSubview: rule];
298 NSRect bindingFrame = NSMakeRect(210+(TEXT_GAP*4), 0,
299 frameRect.size.width - (210+(TEXT_GAP*4)), [self contentHeight] + TEXT_GAP );
300 NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame: bindingFrame];
301 key_bindings = [[ControllerKeyBindingView alloc] initWithFrame: bindingFrame ];
302 [scrollView setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
303 [scrollView setDocumentView: key_bindings];
304 [scrollView setDrawsBackground: NO];
305 [scrollView setHasVerticalScroller: YES];
306 [scrollView setAutohidesScrollers: YES];
308 [self addSubview: scrollView];
309 [key_bindings setDevice: maple_get_device(0,0)];
311 for( i=0; i<MAX_DEVICES; i++ ) {
313 save_controller[i] = NULL;
314 maple_device_t device = maple_get_device(i,0);
316 snprintf( buf, sizeof(buf), _("Slot %d."), i );
317 radio[i] = [[NSButton alloc] initWithFrame: NSMakeRect( x, y, 60, TEXT_HEIGHT )];
318 [radio[i] setTitle: [NSString stringWithUTF8String: buf]];
319 [radio[i] setTag: i];
320 [radio[i] setButtonType: NSRadioButton];
321 [radio[i] setAlignment: NSRightTextAlignment];
322 [radio[i] setTarget: self];
323 [radio[i] setAction: @selector(radioChanged:)];
324 [radio[i] setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
325 [self addSubview: radio[i]];
328 NSPopUpButton *popup = [[NSPopUpButton alloc] initWithFrame: NSMakeRect(x,y,150,TEXT_HEIGHT)
330 [popup addItemWithTitle: NS_("<empty>")];
331 [popup setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
332 [[popup itemAtIndex: 0] setTag: 0];
333 for( j=0; devices[j] != NULL; j++ ) {
334 [popup addItemWithTitle: [NSString stringWithUTF8String: devices[j]->name]];
335 if( device != NULL && device->device_class == devices[j] ) {
336 [popup selectItemAtIndex: (j+1)];
338 [[popup itemAtIndex: (j+1)] setTag: (j+1)];
340 [popup setTarget: self];
341 [popup setAction: @selector(deviceChanged:)];
343 [self addSubview: popup];
344 y -= (TEXT_HEIGHT+TEXT_GAP);
347 [radio[0] setState: NSOnState];
351 - (void)radioChanged: (id)sender
353 int slot = [sender tag];
355 for( i=0; i<MAX_DEVICES; i++ ) {
357 [radio[i] setState: NSOffState];
360 [key_bindings setDevice: maple_get_device(slot,0)];
362 - (void)deviceChanged: (id)sender
364 int slot = [sender tag];
365 int new_device_idx = [sender indexOfSelectedItem] - 1, i;
366 maple_device_class_t new_device_class = NULL;
368 for( i=0; i<MAX_DEVICES; i++ ) {
370 [radio[i] setState: NSOnState];
372 [radio[i] setState: NSOffState];
376 maple_device_t current = maple_get_device(slot,0);
377 maple_device_t new_device = NULL;
378 if( new_device_idx != -1 ) {
379 new_device_class = maple_get_device_classes()[new_device_idx];
381 if( current == NULL ? new_device_class == NULL : current->device_class == new_device_class ) {
383 [key_bindings setDevice: current];
386 if( current != NULL && current->device_class == &controller_class ) {
387 save_controller[slot] = current->clone(current);
389 if( new_device_class == NULL ) {
390 maple_detach_device(slot,0);
392 if( new_device_class == &controller_class && save_controller[slot] != NULL ) {
393 new_device = save_controller[slot];
394 save_controller[slot] = NULL;
396 new_device = maple_new_device( new_device_class->name );
398 maple_attach_device(new_device,slot,0);
400 [key_bindings setDevice: maple_get_device(slot,0)];
401 lxdream_save_config();
405 NSView *cocoa_gui_create_prefs_controller_pane()
407 return [LxdreamPrefsControllerPane new];
.