1
0
Fork 0

now works with motif, sort of

This commit is contained in:
dtb 2022-10-05 23:38:28 -04:00
parent 2de50f184b
commit e1805936ef
2 changed files with 91 additions and 10 deletions

View File

@ -1,8 +1,18 @@
CFLAGS = -g -DGTK4=1 `pkg-config --cflags gtk4` `pkg-config --libs gtk4`
CFLAGS = -g
RM = rm -f
SOURCE = bitch.c
TARGETS = bitch
all: $(TARGETS)
motif: $(SOURCE)
$(CC) $(CFLAGS) -o $@ \
-DMOTIF -I/usr/pkg/include/ -I/usr/X11R7/include/ \
-L/usr/pkg/lib/ -L/usr/X11R7/lib -lXm -lX11 \
$(SOURCE)
gtk4: $(SOURCE)
$(CC) $(CFLAGS) -o $@ \
-DGTK4 `pkg-config --cflags gtk4` `pkg-config --libs gtk4` \
$(SOURCE)
clean:
$(RM) $(TARGETS)
%: %.c

View File

@ -1,13 +1,18 @@
#include <stdio.h>
#include <stdlib.h> /* stderr, atoi(3) */
#include <string.h> /* strchr(3) */
#include <sysexits.h> /* EX_USAGE */
#include <unistd.h> /* getopt(3) */
/* Written for Gtk4 */
#ifdef GTK4
# include <gtk/gtk.h>
#endif
#ifdef MOTIF
# include <Xm/Xm.h>
# include <Xm/PushB.h>
#endif
#define BUF 100
/* Pinephone dimensions as default */
@ -19,6 +24,7 @@ static char buf[BUF];
static char f;
static unsigned char shy = 0;
static int fake_argc = 1;
static char *fake_argv[2] = { NULL, NULL };
#ifdef GTK4
@ -28,6 +34,15 @@ static void activate(GtkApplication *app, gpointer user_data);
static void button_pressed(GtkWidget *button, gpointer data);
#endif
#ifdef MOTIF
static XtAppContext app_context;
static void activate(void);
static void button_pressed(
Widget w, XtPointer client_data, XmPushButtonCallbackStruct *cbs
);
#endif
int main(int argc, char *argv[]){
int c;
extern char *optarg;
@ -69,9 +84,36 @@ Usage: %s (-rx)\n\
g_object_unref(app);
#endif
#ifdef MOTIF
activate();
#endif
return status;
}
char *
sip(void){
int c;
char *s;
if(fgets(buf, sizeof(buf) / sizeof(*buf), stdin) == NULL){
if(ferror(stdin) != 0)
#ifdef GTK4
g_print
#else
printf
#endif
("%s: stdin: file read error\n", argv0);
return NULL;
}
if((s = strchr(buf, '\n')) == NULL) /* flush */
while((c = getc(stdin)) != '\n' && c != EOF);
else
*s = '\0';
return buf;
}
#ifdef GTK4
static void
activate(GtkApplication *app, gpointer user_data){
@ -96,15 +138,8 @@ activate(GtkApplication *app, gpointer user_data){
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolledwindow), box);
for(;;){
if(fgets(buf, sizeof(buf) / sizeof(*buf), stdin) == NULL){
if(ferror(stdin) != 0)
g_print("%s: stdin: file read error\n", argv0);
if(sip() == NULL)
break;
}
if((s = strchr(buf, '\n')) == NULL) /* flush */
while((c = getc(stdin)) != '\n' && c != EOF);
else
*s = '\0';
button = gtk_button_new_with_label(buf);
g_signal_connect(
button, "clicked", G_CALLBACK(button_pressed), NULL
@ -134,4 +169,40 @@ button_pressed(GtkWidget *button, gpointer data){
return;
}
#endif /* ifdef GTK4 */
#ifdef MOTIF
static void
activate(void){
Widget button;
Widget top_wid;
top_wid = XtVaAppInitialize(
&app_context, "Bitch", NULL, 0, &fake_argc, fake_argv, NULL,
NULL
);
for(;;){
if(sip() == NULL)
break;
button = XmCreatePushButton(top_wid, buf, NULL, 0);
XtManageChild(button);
XtAddCallback(button, XmNactivateCallback, button_pressed, NULL);
}
XtRealizeWidget(top_wid);
XtAppMainLoop(app_context);
}
static void
button_pressed(
Widget w,
XtPointer client_data,
XmPushButtonCallbackStruct *cbs
){
printf("pressed\n");
if(shy)
XtAppSetExitFlag(app_context);
}
#endif