]> gitweb.factorcode.org Git - factor.git/commitdiff
improved SDL console
authorSlava Pestov <slava@factorcode.org>
Wed, 19 Jan 2005 02:42:29 +0000 (02:42 +0000)
committerSlava Pestov <slava@factorcode.org>
Wed, 19 Jan 2005 02:42:29 +0000 (02:42 +0000)
library/compiler/simplifier.factor
library/sdl/sdl-event.factor
library/ui/console.factor
native/bignum.c
native/bignum.h
native/fixnum.c
native/types.c

index 6ab799283be3cc62d2fce0b4c7c17fde4935f3ae..6f429469f75236d2ef5fa1fa47951459501dbd14 100644 (file)
@@ -69,7 +69,14 @@ SYMBOL: simplifying
     ] with-scope ;
 
 : label-called? ( label linear -- ? )
-    [ unswons #label = [ drop f ] [ over = ] ifte ] some? nip ;
+    [ uncons pick = swap #label = not and ] some? nip ;
+
+#label [
+    [
+        dup car cdr simplifying get label-called?
+        [ f ] [ cdr t ] ifte
+    ]
+] "simplifiers" set-word-property
 
 : next-physical? ( op linear -- ? )
     cdr dup [ car car = ] [ 2drop f ] ifte ;
@@ -79,18 +86,28 @@ SYMBOL: simplifying
     #! its param.
     over next-physical? [ cdr unswons cdr t ] [ f f ] ifte ;
 
-#label [
-    [
-        dup car cdr simplifying get
-        label-called? [ f ] [ cdr t ] ifte
-    ]
-] "simplifiers" set-word-property
-
 \ >r [ [ \ r> cancel nip ] ] "simplifiers" set-word-property
 \ r> [ [ \ >r cancel nip ] ] "simplifiers" set-word-property
 \ dup [ [ \ drop cancel nip ] ] "simplifiers" set-word-property
 \ swap [ [ \ swap cancel nip ] ] "simplifiers" set-word-property
 
+\ drop [
+    [
+        #push-immediate cancel [
+            #replace-immediate swons swons t
+        ] when
+    ] [
+        #push-indirect cancel [
+            #replace-indirect swons swons t
+        ] when
+    ]
+] "simplifiers" set-word-property
+
+: find-label ( label -- rest )
+    simplifying get [
+        uncons pick = swap #label = and
+    ] some? nip ;
+
 : next-logical ( linear -- linear )
     dup car car "next-logical" word-property call ;
 
@@ -98,11 +115,6 @@ SYMBOL: simplifying
     cdr next-logical
 ] "next-logical" set-word-property
 
-: find-label ( label -- rest )
-    simplifying get [
-        uncons pick = swap #label = and
-    ] some? nip ;
-
 #jump-label [
     car cdr find-label cdr
 ] "next-logical" set-word-property
@@ -122,15 +134,11 @@ SYMBOL: simplifying
     ] ifte ;
 
 #call [
-    [
-        #return #jump reduce
-    ]
+    [ #return #jump reduce ]
 ] "simplifiers" set-word-property
 
 #call-label [
-    [
-        #return #jump-label reduce
-    ]
+    [ #return #jump-label reduce ]
 ] "simplifiers" set-word-property
 
 : double-jump ( linear op1 op2 -- linear ? )
@@ -163,39 +171,18 @@ SYMBOL: simplifying
     uncons (dead-code) >r cons r> ;
 
 #jump-label [
-    [
-        #return #return double-jump
-    ] [
-        #jump-label #jump-label double-jump
-    ] [
-        #jump #jump double-jump
-    ] [
-        useless-jump
-    ] [
-        dead-code
-    ]
+    [ #return #return double-jump ]
+    [ #jump-label #jump-label double-jump ]
+    [ #jump #jump double-jump ]
+    [ useless-jump ]
+    [ dead-code ]
 ] "simplifiers" set-word-property
 
 #target-label [
-    [
-        #jump-label #target-label double-jump
-    ] [
-        #jump #target double-jump
-    ]
+    [ #jump-label #target-label double-jump ]
+    [ #jump #target double-jump ]
 ] "simplifiers" set-word-property
 
 #jump [ [ dead-code ] ] "simplifiers" set-word-property
 #return [ [ dead-code ] ] "simplifiers" set-word-property
 #end-dispatch [ [ dead-code ] ] "simplifiers" set-word-property
-
-\ drop [
-    [
-        #push-immediate cancel [
-            #replace-immediate swons swons t
-        ] when
-    ] [
-        #push-indirect cancel [
-            #replace-indirect swons swons t
-        ] when
-    ]
-] "simplifiers" set-word-property
index 3781d023fa5cc2d4c820b22656fbecc5c6abb03b..d1043f7b4ba43d274722b73f09c1279c0ceee285 100644 (file)
@@ -2,7 +2,7 @@
 
 ! $Id$
 !
-! Copyright (C) 2004 Slava Pestov.
+! Copyright (C) 2004, 2005 Slava Pestov.
 ! 
 ! Redistribution and use in source and binary forms, with or without
 ! modification, are permitted provided that the following conditions are met:
@@ -27,6 +27,8 @@
 
 IN: sdl-event
 USE: alien
+USE: generic
+USE: kernel
 
 BEGIN-ENUM: 0
     ENUM: SDL_NOEVENT         ! Unused (do not remove)
@@ -100,6 +102,12 @@ BEGIN-STRUCT: keyboard-event
     FIELD: ushort unicode
 END-STRUCT
 
+PREDICATE: alien key-down-event
+    keyboard-event-type SDL_KEYDOWN = ;
+
+PREDICATE: alien key-up-event
+    keyboard-event-type SDL_KEYUP = ;
+
 BEGIN-STRUCT: motion-event
     FIELD: uchar type  ! SDL_MOUSEMOTION
     FIELD: uchar which ! The mouse device index
@@ -108,7 +116,10 @@ BEGIN-STRUCT: motion-event
     FIELD: ushort y
     FIELD: short xrel  ! The relative motion in the X direction
     FIELD: short yrel  ! The relative motion in the Y direction 
-END-STRUCT             
+END-STRUCT
+
+PREDICATE: alien motion-event
+    motion-event-type SDL_MOUSEMOTION = ;
 
 BEGIN-STRUCT: button-event
        FIELD: uchar type    ! SDL_MOUSEBUTTONDOWN or SDL_MOUSEBUTTONUP
@@ -119,6 +130,12 @@ BEGIN-STRUCT: button-event
     FIELD: ushort y      ! The X/Y coordinates of the mouse at press time
 END-STRUCT
 
+PREDICATE: alien button-down-event
+    button-event-type SDL_MOUSEBUTTONDOWN = ;
+
+PREDICATE: alien button-up-event
+    button-event-type SDL_MOUSEBUTTONUP = ;
+
 BEGIN-STRUCT: joy-axis-event
        FIELD: uchar type   ! SDL_JOYAXISMOTION
     FIELD: uchar which  ! The joystick device index
@@ -126,6 +143,9 @@ BEGIN-STRUCT: joy-axis-event
     FIELD: short value  ! The axis value
 END-STRUCT
 
+PREDICATE: alien joy-axis-event
+    joy-axis-event-type SDL_JOYAXISMOTION = ;
+
 BEGIN-STRUCT: joy-ball-event
     FIELD: uchar type  ! SDL_JOYBALLMOTION
     FIELD: uchar which ! The joystick device index
@@ -134,6 +154,9 @@ BEGIN-STRUCT: joy-ball-event
     FIELD: short yrel  ! The relative motion in the Y direction
 END-STRUCT
 
+PREDICATE: alien joy-ball-event
+    joy-ball-event-type SDL_JOYBALLMOTION = ;
+
 BEGIN-STRUCT: joy-hat-event
     FIELD: uchar type  ! SDL_JOYHATMOTION
     FIELD: uchar which ! The joystick device index
@@ -145,6 +168,9 @@ BEGIN-STRUCT: joy-hat-event
         ! Note that zero means the POV is centered.
 END-STRUCT
 
+PREDICATE: alien joy-hat-event
+    joy-hat-event-type SDL_JOYHATMOTION = ;
+
 BEGIN-STRUCT: joy-button-event
        FIELD: uchar type   ! SDL_JOYBUTTONDOWN or SDL_JOYBUTTONUP
        FIELD: uchar which  ! The joystick device index
@@ -152,6 +178,12 @@ BEGIN-STRUCT: joy-button-event
        FIELD: uchar state  ! SDL_PRESSED or SDL_RELEASED
 END-STRUCT
 
+PREDICATE: alien joy-button-down-event
+    joy-button-event-type SDL_JOYBUTTONDOWN = ;
+
+PREDICATE: alien joy-button-up-event
+    joy-button-event-type SDL_JOYBUTTONUP = ;
+
 BEGIN-STRUCT: resize-event
     FIELD: uchar type ! SDL_VIDEORESIZE
     FIELD: int w      ! New width
@@ -162,10 +194,16 @@ BEGIN-STRUCT: expose-event
     FIELD: uchar type ! SDL_VIDEOEXPOSE
 END-STRUCT
 
+PREDICATE: alien resize-event
+    resize-event-type SDL_VIDEORESIZE = ;
+
 BEGIN-STRUCT: quit-event
     FIELD: uchar type ! SDL_QUIT
 END-STRUCT
 
+PREDICATE: alien quit-event
+    quit-event-type SDL_QUIT = ;
+
 BEGIN-STRUCT: user-event
     FIELD: uchar type ! SDL_USREVENT through SDL_NUMEVENTS-1
     FIELD: int code
@@ -173,6 +211,9 @@ BEGIN-STRUCT: user-event
     FIELD: void* data2
 END-STRUCT
 
+PREDICATE: alien user-event
+    user-event-type SDL_QUIT = ;
+
 BEGIN-STRUCT: event
     FIELD: uchar type
 END-STRUCT
index f2d2c3b3a94f48835dda81e49839fa6d9607a275..33a548e8c9309d113744dafda0d9f36c4967199b 100644 (file)
@@ -38,8 +38,8 @@
 ! Then, start Factor as usual (./f factor.image) and enter this
 ! at the listener:
 !
-! USE: console
-! start-console
+! USE: shells
+! sdl
 
 IN: console
 USE: generic
@@ -62,6 +62,7 @@ USE: stdio
 USE: errors
 USE: line-editor
 USE: hashtables
+USE: lists
 
 #! A namespace holding console state.
 SYMBOL: console
@@ -78,10 +79,31 @@ SYMBOL: output-line
 #! A line editor object.
 SYMBOL: input-line
 
+! Scrolling
+: visible-lines ( -- n ) height get line-height /i ;
+: total-lines ( -- n ) lines get vector-length ;
+: available-lines ( -- ) total-lines first-line get - ;
+
+: fix-first-line ( line -- line )
+    total-lines visible-lines - 1 + min 0 max ;
+
+: change-first-line ( quot -- )
+    first-line get
+    swap call fix-first-line
+    first-line set ; inline
+
+: line-scroll-up   ( -- ) [ 1 - ] change-first-line ;
+: line-scroll-down ( -- ) [ 1 + ] change-first-line ;
+: page-scroll-up   ( -- ) [ visible-lines - ] change-first-line ;
+: page-scroll-down ( -- ) [ visible-lines + ] change-first-line ;
+
+: scroll-to-bottom ( -- )
+    total-lines fix-first-line first-line set ;
+
 ! Rendering
-: background HEX: 0000dbff ;
-: foreground HEX: 6d92ffff ;
-: cursor     HEX: ffff24ff ;
+: background white ;
+: foreground black ;
+: cursor     red   ;
 
 #! The font size is hardcoded here.
 : line-height 8 ;
@@ -97,12 +119,6 @@ SYMBOL: input-line
 : clear-display ( -- )
     surface get 0 0 width get height get background boxColor ;
 
-: visible-lines ( -- n )
-    height get line-height /i ;
-
-: available-lines ( -- )
-    lines get vector-length first-line get - ;
-
 : draw-lines ( -- )
     visible-lines available-lines min [
         first-line get +
@@ -131,6 +147,19 @@ SYMBOL: input-line
     input-line get [ line-text get ] bind draw-line
     r> draw-cursor ;
 
+: scrollbar-width 16 ;
+: scroll-y ( line -- y ) total-lines 1 + / height get * ;
+: scrollbar-top ( -- y ) first-line get scroll-y ;
+: scrollbar-bottom ( -- y ) first-line get visible-lines + scroll-y ;
+
+: draw-scrollbar ( -- )
+    surface get
+    width get scrollbar-width -
+    scrollbar-top
+    width get
+    scrollbar-bottom
+    black boxColor ;
+
 : draw-console ( -- )
     [
         0 x set
@@ -139,19 +168,14 @@ SYMBOL: input-line
         draw-lines
         draw-current
         draw-input
+        draw-scrollbar
     ] with-surface ;
 
 : empty-buffer ( sbuf -- str )
     dup sbuf>str 0 rot set-sbuf-length ;
 
 : add-line ( text -- )
-    lines get vector-push
-    lines get vector-length 1 + first-line get - visible-lines -
-    dup 0 >= [
-        first-line [ + ] change
-    ] [
-        drop
-    ] ifte ;
+    lines get vector-push scroll-to-bottom ;
 
 : console-write ( text -- )
     "\n" split1 [       
@@ -222,34 +246,85 @@ SYMBOL: event
 
 GENERIC: handle-event ( event -- ? )
 
-PREDICATE: alien key-down-event
-    keyboard-event-type SDL_KEYDOWN = ;
-
 SYMBOL: keymap
 
 {{
-        [[ [ "RETURN" ] [ return-key ] ]]
-        [[ [ "BACKSPACE" ] [ input-line get [ backspace ] bind ] ]]
-        [[ [ "LEFT" ] [ input-line get [ left ] bind ] ]]
-        [[ [ "RIGHT" ] [ input-line get [ right ] bind ] ]]
-        [[ [ "UP" ] [ input-line get [ history-prev ] bind ] ]]
-        [[ [ "DOWN" ] [ input-line get [ history-next ] bind ] ]]
-        [[ [ "CTRL" "k" ] [ input-line get [ line-clear ] bind ] ]]
+    [[ [ "RETURN" ] [ return-key ] ]]
+    [[ [ "BACKSPACE" ] [ input-line get [ backspace ] bind ] ]]
+    [[ [ "LEFT" ] [ input-line get [ left ] bind ] ]]
+    [[ [ "RIGHT" ] [ input-line get [ right ] bind ] ]]
+    [[ [ "UP" ] [ input-line get [ history-prev ] bind ] ]]
+    [[ [ "SHIFT" "DOWN" ] [ line-scroll-down ] ]]
+    [[ [ "SHIFT" "UP" ] [ line-scroll-up ] ]]
+    [[ [ "PAGEDOWN" ] [ page-scroll-down ] ]]
+    [[ [ "PAGEUP" ] [ page-scroll-up ] ]]
+    [[ [ "DOWN" ] [ input-line get [ history-next ] bind ] ]]
+    [[ [ "CTRL" "k" ] [ input-line get [ line-clear ] bind ] ]]
 }} keymap set
 
+: input-key? ( event -- ? )
+    #! Is this a keystroke that potentially inserts input, or
+    #! does it have modifiers?
+    keyboard-event-unicode valid-char? ;
+
+: user-input ( char -- )
+    input-line get [ insert-char ] bind  scroll-to-bottom ;
+
 M: key-down-event handle-event ( event -- ? )
     dup keyboard-event>binding keymap get hash [
         call draw-console
     ] [
-        keyboard-event-unicode dup valid-char? [
-            input-line get [ insert-char ] bind draw-console
+        dup input-key? [
+            keyboard-event-unicode user-input draw-console
         ] [
             drop
         ] ifte
     ] ?ifte t ;
 
-PREDICATE: alien quit-event
-    quit-event-type SDL_QUIT = ;
+! The y co-ordinate of the start of the drag.
+SYMBOL: drag-start-y
+! The first line at the time
+SYMBOL: drag-start-line
+
+: scrollbar-click ( y -- )
+    dup scrollbar-top < [
+        drop page-scroll-up draw-console
+    ] [
+        dup scrollbar-bottom > [
+            drop page-scroll-down draw-console
+        ] [
+            drag-start-y set
+            first-line get drag-start-line set
+        ] ifte
+    ] ifte ;
+
+M: button-down-event handle-event ( event -- ? )
+    dup button-event-x width get scrollbar-width - >= [
+        button-event-y scrollbar-click
+    ] [
+        drop
+    ] ifte t ;
+
+M: button-up-event handle-event ( event -- ? )
+    drop
+    drag-start-y off
+    drag-start-line off t ;
+
+M: motion-event handle-event ( event -- ? )
+    drag-start-y get [
+        motion-event-y drag-start-y get -
+        height get / total-lines * drag-start-line get +
+        >fixnum fix-first-line first-line set
+        draw-console
+    ] [
+        drop
+    ] ifte t ;
+
+M: resize-event handle-event ( event -- ? )
+    dup resize-event-w swap resize-event-h
+    0 SDL_HWSURFACE SDL_RESIZABLE bitor init-screen
+    scroll-to-bottom
+    draw-console t ;
 
 M: quit-event handle-event ( event -- ? )
     drop f ;
@@ -290,7 +365,7 @@ IN: shells
 
 : sdl ( -- )
     <namespace> [
-        800 600 32 SDL_HWSURFACE init-screen
+        640 480 0 SDL_HWSURFACE SDL_RESIZABLE bitor init-screen
         init-console
     ] extend console set
 
index 3f3bbd797d0afb16e415b35e45dbd810f63167e4..c077eddaef9a72e6ccdd08434f728e8c67b241e8 100644 (file)
@@ -1,19 +1,5 @@
 #include "factor.h"
 
-F_FIXNUM to_integer(CELL x)
-{
-       switch(TAG(x))
-       {
-       case FIXNUM_TYPE:
-               return untag_fixnum_fast(x);
-       case BIGNUM_TYPE:
-               return s48_bignum_to_long(untag_bignum(x));
-       default:
-               type_error(BIGNUM_TYPE,x);
-               return 0;
-       }
-}
-
 /* FFI calls this */
 void box_integer(F_FIXNUM integer)
 {
@@ -29,7 +15,7 @@ void box_cell(CELL cell)
 /* FFI calls this */
 F_FIXNUM unbox_integer(void)
 {
-       return to_integer(dpop());
+       return to_fixnum(dpop());
 }
 
 CELL to_cell(CELL x)
index c5ac7b514a77658787b76db265950067a0941da6..b8bb5fc5a83629627c7561bc7877d08d7554e002 100644 (file)
@@ -18,7 +18,6 @@ INLINE CELL tag_bignum(F_ARRAY* bignum)
        return RETAG(bignum,BIGNUM_TYPE);
 }
 
-F_FIXNUM to_integer(CELL x);
 CELL to_cell(CELL x);
 
 DLLEXPORT void box_integer(F_FIXNUM integer);
index 8e48d2d019e1c2dadb0b495d230e673f4744cec3..589131b455abd8ba7bc8b2a62d95dadc9c3b906a 100644 (file)
@@ -218,11 +218,11 @@ void box_signed_2(signed short integer)
 /* FFI calls this */
 signed char unbox_signed_1(void)
 {
-       return to_integer(dpop());
+       return to_fixnum(dpop());
 }
 
 /* FFI calls this */
 signed short unbox_signed_2(void)
 {
-       return to_integer(dpop());
+       return to_fixnum(dpop());
 }
index ffc559340a841bc25359dcb6f56e9d399d5a36f8..cc81333e63c3b83b8ae88216b3500ea74e4e9f52 100644 (file)
@@ -118,6 +118,6 @@ void primitive_set_integer_slot(void)
 {
        F_FIXNUM slot = untag_fixnum_fast(dpop());
        CELL obj = dpop();
-       F_FIXNUM value = to_integer(dpop());
+       F_FIXNUM value = to_fixnum(dpop());
        put(SLOT(obj,slot),value);
 }